Skip to content

Commit de89966

Browse files
committed
fix(specs): add a linter to assert that type is present
1 parent 1116113 commit de89966

File tree

11 files changed

+112
-8
lines changed

11 files changed

+112
-8
lines changed

.eslintrc.cjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ module.exports = {
5454
'automation-custom/no-big-int': 'error',
5555
'automation-custom/no-final-dot': 'error',
5656
'automation-custom/single-quote-ref': 'error',
57+
'automation-custom/has-type': 'error',
5758
},
5859
overrides: [
5960
{

eslint/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { endWithDot } from './rules/endWithDot.js';
2+
import { hasType } from './rules/hasType.js';
23
import { noBigInt } from './rules/noBigInt.js';
34
import { noFinalDot } from './rules/noFinalDot.js';
45
import { noNewLine } from './rules/noNewLine.js';
@@ -10,6 +11,7 @@ import { validInlineTitle } from './rules/validInlineTitle.js';
1011

1112
const rules = {
1213
'end-with-dot': endWithDot,
14+
'has-type': hasType,
1315
'no-big-int': noBigInt,
1416
'no-final-dot': noFinalDot,
1517
'no-new-line': noNewLine,

eslint/src/rules/hasType.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { createRule } from 'eslint-plugin-yml/lib/utils';
2+
3+
import { isPairWithKey } from '../utils.js';
4+
5+
export const hasType = createRule('hasType', {
6+
meta: {
7+
docs: {
8+
description: '`type` must be specified with `properties` or `items`',
9+
categories: null,
10+
extensionRule: false,
11+
layout: false,
12+
},
13+
messages: {
14+
hasType: '`type` must be specified with `properties` or `items`',
15+
},
16+
type: 'problem',
17+
schema: [],
18+
},
19+
create(context) {
20+
if (!context.getSourceCode().parserServices?.isYAML) {
21+
return {};
22+
}
23+
24+
return {
25+
YAMLPair(node): void {
26+
if (isPairWithKey(node.parent.parent, 'properties')) {
27+
return; // allow everything in properties
28+
}
29+
30+
const hasType = !!node.parent.pairs.find((pair) => isPairWithKey(pair, 'type'));
31+
if (isPairWithKey(node, 'properties') && !hasType) {
32+
return context.report({
33+
node: node as any,
34+
messageId: 'hasType',
35+
});
36+
}
37+
38+
if (isPairWithKey(node, 'items') && !hasType) {
39+
return context.report({
40+
node: node as any,
41+
messageId: 'hasType',
42+
});
43+
}
44+
},
45+
};
46+
},
47+
});

eslint/src/rules/noBigInt.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ export const noBigInt = createRule('noBigInt', {
3434

3535
// check the format next to the type
3636
node.parent.pairs.find((pair) => {
37-
if (isPairWithKey(pair, 'format') && isScalar(pair.value) && (pair.value.value === 'int32' || pair.value.value === 'int64')) {
37+
if (
38+
isPairWithKey(pair, 'format') &&
39+
isScalar(pair.value) &&
40+
(pair.value.value === 'int32' || pair.value.value === 'int64')
41+
) {
3842
context.report({
3943
node: pair.value as any,
4044
messageId: 'noBigInt',

eslint/src/rules/outOfLineRule.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { RuleModule } from 'eslint-plugin-yml/lib/types.js';
1+
import type { RuleModule } from 'eslint-plugin-yml/lib/types.js';
22
import { createRule } from 'eslint-plugin-yml/lib/utils';
33

44
import { isNullable, isPairWithKey } from '../utils.js';

eslint/tests/hasType.test.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { runClassic } from 'eslint-vitest-rule-tester';
2+
import yamlParser from 'yaml-eslint-parser';
3+
4+
import { hasType } from '../src/rules/hasType.js';
5+
6+
runClassic(
7+
'has-type',
8+
hasType,
9+
{
10+
valid: [
11+
`
12+
simple:
13+
type: object
14+
properties:
15+
prop1:
16+
`,
17+
`
18+
withArray:
19+
type: array
20+
items:
21+
type: string
22+
`,
23+
],
24+
invalid: [
25+
{
26+
code: `
27+
simple:
28+
properties:
29+
noType:
30+
type: string
31+
`,
32+
errors: [{ messageId: 'hasType' }],
33+
},
34+
{
35+
code: `
36+
array:
37+
items:
38+
type: string
39+
`,
40+
errors: [{ messageId: 'hasType' }],
41+
},
42+
],
43+
},
44+
{
45+
parser: yamlParser,
46+
},
47+
);

eslint/tests/noBigInt.test.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import { runClassic } from 'eslint-vitest-rule-tester';
22
import yamlParser from 'yaml-eslint-parser';
33
import { noBigInt } from '../src/rules/noBigInt.js';
44

5-
65
runClassic(
76
'no-big-int',
87
noBigInt,
98
{
10-
valid: [`
9+
valid: [
10+
`
1111
type: object
1212
properties:
1313
id:
@@ -16,11 +16,13 @@ properties:
1616
url:
1717
type: string
1818
format: uri
19-
`, `
19+
`,
20+
`
2021
prop:
2122
type: integer
2223
format: int32
23-
`],
24+
`,
25+
],
2426
invalid: [
2527
{
2628
code: `

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"postinstall": "husky && yarn workspace eslint-plugin-automation-custom build",
2424
"playground:browser": "yarn workspace javascript-browser-playground start",
2525
"scripts:build": "yarn workspace scripts build:actions",
26-
"scripts:lint": "yarn workspace scripts lint",
26+
"scripts:lint": "yarn cli format javascript scripts && yarn cli format javascript eslint",
2727
"scripts:test": "yarn workspace scripts test",
2828
"specs:fix": "eslint --ext=yml $0 --fix",
2929
"specs:lint": "eslint --ext=yml $0",

scripts/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
"build:actions": "cd ci/actions/restore-artifacts && esbuild --bundle --format=cjs --minify --platform=node --outfile=builddir/index.cjs --log-level=error src/index.ts",
77
"createGitHubReleases": "yarn runScript ci/codegen/createGitHubReleases.ts",
88
"createMatrix": "yarn runScript ci/githubActions/createMatrix.ts",
9-
"lint": "yarn start format javascript scripts",
109
"lint:deadcode": "knip",
1110
"pre-commit": "node ./ci/husky/pre-commit.mjs",
1211
"pushGeneratedCode": "yarn runScript ci/codegen/pushGeneratedCode.ts",

specs/crawler/common/schemas/configuration.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ requestOptions:
285285
$ref: '#/headers'
286286

287287
waitTime:
288+
type: object
288289
description: Timeout for the HTTP request.
289290
properties:
290291
min:

specs/monitoring/common/schemas/Server.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
title: server
2+
type: object
23
additionalProperties: false
34
properties:
45
name:

0 commit comments

Comments
 (0)