-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(specs): proper title with linter (#3444)
- Loading branch information
Showing
74 changed files
with
313 additions
and
94 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
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,103 @@ | ||
import type { Rule } from 'eslint'; | ||
|
||
import { isNullable, isPairWithKey } from '../utils'; | ||
|
||
export const validInlineTitle: Rule.RuleModule = { | ||
meta: { | ||
docs: { | ||
description: | ||
'title must be set in inline models, should be the first property and start with a lowercase', | ||
}, | ||
messages: { | ||
inlineTitleExists: 'title must be set in inline models', | ||
lowercaseTitle: 'title must start with a lowercase', | ||
firstProperty: 'title must be the first property', | ||
noSpaceInTitle: 'title must not contain spaces', | ||
}, | ||
}, | ||
create(context) { | ||
if (!context.sourceCode.parserServices.isYAML) { | ||
return {}; | ||
} | ||
|
||
return { | ||
YAMLPair(node): void { | ||
if ( | ||
!isPairWithKey(node, 'type') || | ||
node.value?.type !== 'YAMLScalar' || | ||
node.value.value !== 'object' | ||
) { | ||
return; | ||
} | ||
|
||
// we don't enforce it for root level object | ||
if (node.parent.parent.loc.start.column === 0) { | ||
return; | ||
} | ||
|
||
// make sure title starts with a lowercase | ||
const title = node.parent.pairs.find((pair) => | ||
isPairWithKey(pair, 'title') | ||
); | ||
const titleNode = title?.value; | ||
const titleValue = (titleNode as any)?.value as string; | ||
if ( | ||
titleNode && | ||
(titleNode.type !== 'YAMLScalar' || !/^[a-z]/.test(titleValue)) | ||
) { | ||
context.report({ | ||
node: title, | ||
messageId: 'lowercaseTitle', | ||
}); | ||
} | ||
|
||
// make sure title doesn't contain spaces | ||
if (titleValue?.includes(' ')) { | ||
context.report({ | ||
node: title, | ||
messageId: 'noSpaceInTitle', | ||
}); | ||
} | ||
|
||
// if there are no properties, we don't need a title | ||
const properties = node.parent.pairs.find((pair) => | ||
isPairWithKey(pair, 'properties') | ||
); | ||
if (!properties) { | ||
return; | ||
} | ||
|
||
// allow it on nullable objects | ||
if ( | ||
isPairWithKey(node.parent.parent.parent, 'oneOf') && | ||
isNullable(node.parent.parent.parent.value) | ||
) { | ||
return; | ||
} | ||
|
||
// allow on allOf too, since they are not generated | ||
if (isPairWithKey(node.parent.parent.parent, 'allOf')) { | ||
return; | ||
} | ||
|
||
// make sure the title is set on the same object | ||
if (!title) { | ||
context.report({ | ||
node: node.value, | ||
messageId: 'inlineTitleExists', | ||
}); | ||
|
||
return; | ||
} | ||
|
||
// make sure title is the first property | ||
if (!isPairWithKey(node.parent.pairs[0], 'title')) { | ||
context.report({ | ||
node: title, | ||
messageId: 'firstProperty', | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
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,95 @@ | ||
import { RuleTester } from 'eslint'; | ||
|
||
import { validInlineTitle } from '../src/rules/validInlineTitle'; | ||
|
||
const ruleTester = new RuleTester({ | ||
parser: require.resolve('yaml-eslint-parser'), | ||
}); | ||
|
||
ruleTester.run('valid-inline-title', validInlineTitle, { | ||
valid: [ | ||
` | ||
currencies: | ||
type: object | ||
properties: | ||
inner: | ||
type: object | ||
`, | ||
` | ||
currencies: | ||
type: object | ||
properties: | ||
inner: | ||
title: currency | ||
type: object | ||
properties: | ||
currency: | ||
type: string | ||
title: Currency | ||
`, | ||
` | ||
dictionaryLanguage: | ||
oneOf: | ||
- type: object | ||
properties: | ||
prop: | ||
type: integer | ||
- type: 'null' | ||
`, | ||
], | ||
invalid: [ | ||
{ | ||
code: ` | ||
currencies: | ||
type: object | ||
properties: | ||
inner: | ||
type: object | ||
properties: | ||
currency: | ||
type: string | ||
title: Currency | ||
`, | ||
errors: [{ messageId: 'inlineTitleExists' }], | ||
}, | ||
{ | ||
code: ` | ||
currencies: | ||
type: object | ||
properties: | ||
inner: | ||
type: object | ||
title: currency | ||
properties: | ||
currency: | ||
type: string | ||
title: Currency | ||
`, | ||
errors: [{ messageId: 'firstProperty' }], | ||
}, | ||
{ | ||
code: ` | ||
currencies: | ||
title: UpperCaseFine | ||
type: object | ||
properties: | ||
inner: | ||
title: UpperCaseNotFine | ||
type: object | ||
`, | ||
errors: [{ messageId: 'lowercaseTitle' }], | ||
}, | ||
{ | ||
code: ` | ||
currencies: | ||
title: spaces are fine | ||
type: object | ||
properties: | ||
inner: | ||
title: spaces are not fine | ||
type: object | ||
`, | ||
errors: [{ messageId: 'noSpaceInTitle' }], | ||
}, | ||
], | ||
}); |
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
Oops, something went wrong.