-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(specs): add a linter to assert that type is present (#4393)
- Loading branch information
Showing
16 changed files
with
185 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { createRule } from 'eslint-plugin-yml/lib/utils'; | ||
|
||
import { isPairWithKey, isPairWithValue } from '../utils.js'; | ||
|
||
export const hasType = createRule('hasType', { | ||
meta: { | ||
docs: { | ||
description: '`type` must be specified with `properties` or `items`', | ||
categories: null, | ||
extensionRule: false, | ||
layout: false, | ||
}, | ||
messages: { | ||
hasType: '`type` must be specified with `properties` or `items`', | ||
}, | ||
type: 'problem', | ||
schema: [], | ||
}, | ||
create(context) { | ||
if (!context.getSourceCode().parserServices?.isYAML) { | ||
return {}; | ||
} | ||
|
||
return { | ||
YAMLPair(node): void { | ||
if (isPairWithKey(node.parent.parent, 'properties')) { | ||
return; // allow everything in properties | ||
} | ||
|
||
const type = node.parent.pairs.find((pair) => isPairWithKey(pair, 'type')); | ||
if (isPairWithKey(node, 'properties') && (!type || !isPairWithValue(type, 'object'))) { | ||
return context.report({ | ||
node: node as any, | ||
messageId: 'hasType', | ||
}); | ||
} | ||
|
||
if (isPairWithKey(node, 'items') && (!type || !isPairWithValue(type, 'array'))) { | ||
return context.report({ | ||
node: node as any, | ||
messageId: 'hasType', | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { runClassic } from 'eslint-vitest-rule-tester'; | ||
import yamlParser from 'yaml-eslint-parser'; | ||
|
||
import { hasType } from '../src/rules/hasType.js'; | ||
|
||
runClassic( | ||
'has-type', | ||
hasType, | ||
{ | ||
valid: [ | ||
` | ||
simple: | ||
type: object | ||
properties: | ||
prop1: | ||
`, | ||
` | ||
withArray: | ||
type: array | ||
items: | ||
type: string | ||
`, | ||
], | ||
invalid: [ | ||
{ | ||
code: ` | ||
simple: | ||
properties: | ||
noType: | ||
type: string | ||
`, | ||
errors: [{ messageId: 'hasType' }], | ||
}, | ||
{ | ||
code: ` | ||
wrongType: | ||
type: string | ||
properties: | ||
noType: | ||
type: string | ||
`, | ||
errors: [{ messageId: 'hasType' }], | ||
}, | ||
{ | ||
code: ` | ||
array: | ||
items: | ||
type: string | ||
`, | ||
errors: [{ messageId: 'hasType' }], | ||
}, | ||
], | ||
}, | ||
{ | ||
parser: yamlParser, | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
title: server | ||
type: object | ||
additionalProperties: false | ||
properties: | ||
name: | ||
|