Skip to content

Commit 110aa81

Browse files
committed
check the type
1 parent de89966 commit 110aa81

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

eslint/src/rules/hasType.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createRule } from 'eslint-plugin-yml/lib/utils';
22

3-
import { isPairWithKey } from '../utils.js';
3+
import { isPairWithKey, isPairWithValue } from '../utils.js';
44

55
export const hasType = createRule('hasType', {
66
meta: {
@@ -27,15 +27,15 @@ export const hasType = createRule('hasType', {
2727
return; // allow everything in properties
2828
}
2929

30-
const hasType = !!node.parent.pairs.find((pair) => isPairWithKey(pair, 'type'));
31-
if (isPairWithKey(node, 'properties') && !hasType) {
30+
const type = node.parent.pairs.find((pair) => isPairWithKey(pair, 'type'));
31+
if (isPairWithKey(node, 'properties') && (!type || !isPairWithValue(type, 'object'))) {
3232
return context.report({
3333
node: node as any,
3434
messageId: 'hasType',
3535
});
3636
}
3737

38-
if (isPairWithKey(node, 'items') && !hasType) {
38+
if (isPairWithKey(node, 'items') && (!type || !isPairWithValue(type, 'array'))) {
3939
return context.report({
4040
node: node as any,
4141
messageId: 'hasType',

eslint/src/utils.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ export function isPairWithKey(node: AST.YAMLNode | null, key: string): node is A
2626
return isScalar(node.key) && node.key.value === key;
2727
}
2828

29+
export function isPairWithValue(node: AST.YAMLNode | null, value: string): node is AST.YAMLPair {
30+
if (node === null || node.type !== 'YAMLPair' || node.value === null) {
31+
return false;
32+
}
33+
return isScalar(node.value) && node.value.value === value;
34+
}
35+
2936
export function isNullable(node: AST.YAMLNode | null): boolean {
3037
return (
3138
isSequence(node) &&

eslint/tests/hasType.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ simple:
3333
},
3434
{
3535
code: `
36+
wrongType:
37+
type: string
38+
properties:
39+
noType:
40+
type: string
41+
`,
42+
errors: [{ messageId: 'hasType' }],
43+
},
44+
{
45+
code: `
3646
array:
3747
items:
3848
type: string

0 commit comments

Comments
 (0)