-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(config): add support for restrictionKeys
- Loading branch information
1 parent
ebd320a
commit 7e80aed
Showing
12 changed files
with
352 additions
and
3 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
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
131 changes: 131 additions & 0 deletions
131
...int-plugin/src/rules/typescript/o3r-restriction-key-tags/o3r-restriction-key-tags.spec.ts
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,131 @@ | ||
import o3rRestrictionKeyRule, { | ||
O3rRestrictionKeyTagsRuleOption, | ||
} from './o3r-restriction-key-tags'; | ||
const { | ||
RuleTester | ||
} = require('@typescript-eslint/rule-tester'); | ||
|
||
const ruleTester = new RuleTester(); | ||
|
||
const code = ` | ||
export interface Config extends Configuration { | ||
/** | ||
* @o3rRestrictionKey valid | ||
* @o3rRestrictionKey valid_with_underscore | ||
* @o3rRestrictionKey 'valid with space (single quote)' | ||
* @o3rRestrictionKey "valid with space (double quote)" | ||
* @o3rRestrictionKey '"valid" with double quote inside' | ||
* @o3rRestrictionKey "'valid' with single quote inside" | ||
* @o3rRestrictionKey valid_with_number_1 | ||
*/ | ||
prop: string; | ||
} | ||
`; | ||
|
||
const supportedKeys = [ | ||
'valid', | ||
'valid_with_underscore', | ||
'valid with space', | ||
'valid with space (single quote)', | ||
'valid with space (double quote)', | ||
'"valid" with double quote inside', | ||
"'valid' with single quote inside", | ||
'valid_with_number_1' | ||
]; | ||
|
||
const options = [{ supportedKeys }] as const satisfies Readonly<[O3rRestrictionKeyTagsRuleOption]>; | ||
|
||
const unknownKeys = [ | ||
`unknown_restriction`, | ||
`"invalid quote'`, | ||
`'another invalid quote"`, | ||
`'unknown with single quote'`, | ||
`"unknown with double quote"` | ||
]; | ||
|
||
const getCodeFor = (key: string) => ` | ||
export interface Config extends Configuration { | ||
/** | ||
* @o3rRestrictionKey ${key} | ||
*/ | ||
prop: string; | ||
} | ||
`; | ||
|
||
const getSuggestionFor = (actualKey: string) => supportedKeys.map((supportedKey) => ({ | ||
messageId: 'suggestUseSupportedKey', | ||
data: { | ||
actualKey, | ||
supportedKey | ||
}, | ||
output: getCodeFor(/[^\w]/.test(supportedKey) ? `"${supportedKey}"` : supportedKey) | ||
})); | ||
|
||
ruleTester.run('o3r-restriction-key-tags', o3rRestrictionKeyRule, { | ||
valid: [ | ||
{ | ||
code, | ||
options | ||
}, | ||
{ | ||
code: ` | ||
export interface Config extends Configuration { | ||
/** | ||
* Property without restriction | ||
*/ | ||
prop: string; | ||
}`, | ||
options | ||
} | ||
], | ||
invalid: [ | ||
{ | ||
code: getCodeFor('"at least one key provided"'), | ||
options: [{}], | ||
errors: [ | ||
{ messageId: 'noRestrictionKeyProvided' } | ||
] | ||
}, | ||
{ | ||
code: code.replace(' extends Configuration', ''), | ||
options, | ||
errors: [ | ||
{ | ||
messageId: 'notInConfigurationInterface' | ||
} | ||
] | ||
}, | ||
{ | ||
code: getCodeFor('valid with space'), | ||
options, | ||
output: getCodeFor(`"valid with space"`), | ||
errors: [ | ||
{ | ||
messageId: 'notWrapWithQuotes', | ||
data: { | ||
actualKey: 'valid with space' | ||
}, | ||
suggestions: [{ | ||
messageId: 'suggestWrapWithQuotes', | ||
data: { | ||
actualKey: 'valid with space' | ||
}, | ||
output: getCodeFor(`"valid with space"`) | ||
}] | ||
} | ||
] | ||
}, | ||
...unknownKeys.map((key) => ({ | ||
code: getCodeFor(key), | ||
options, | ||
errors: [{ | ||
messageId: 'notSupportedKey', | ||
data: { | ||
actualKey: key, | ||
supportedKeys: supportedKeys.join(', ') | ||
}, | ||
suggestions: getSuggestionFor(key) | ||
}] | ||
})) | ||
] | ||
}); |
163 changes: 163 additions & 0 deletions
163
...r/eslint-plugin/src/rules/typescript/o3r-restriction-key-tags/o3r-restriction-key-tags.ts
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,163 @@ | ||
import { | ||
TSESLint, | ||
type TSESTree, | ||
} from '@typescript-eslint/utils'; | ||
import { | ||
createCommentString, | ||
createRule, | ||
defaultSupportedInterfaceNames, | ||
getNodeComment, | ||
isExtendingConfiguration, | ||
} from '../../utils'; | ||
|
||
export interface O3rRestrictionKeyTagsRuleOption { | ||
supportedInterfaceNames?: string[]; | ||
supportedKeys?: string[]; | ||
} | ||
|
||
type O3rWidgetRuleErrorId = | ||
| 'notSupportedKey' | ||
| 'notWrapWithQuotes' | ||
| 'suggestWrapWithQuotes' | ||
| 'suggestUseSupportedKey' | ||
| 'noRestrictionKeyProvided' | ||
| 'notInConfigurationInterface'; | ||
|
||
const defaultOptions: [Required<O3rRestrictionKeyTagsRuleOption>] = [{ | ||
supportedInterfaceNames: defaultSupportedInterfaceNames, | ||
supportedKeys: [] | ||
}]; | ||
|
||
export default createRule<[Readonly<O3rRestrictionKeyTagsRuleOption>, ...any], O3rWidgetRuleErrorId>({ | ||
name: 'o3r-restriction-key-tags', | ||
meta: { | ||
hasSuggestions: true, | ||
fixable: 'code', | ||
type: 'problem', | ||
docs: { | ||
description: 'Ensures that @o3rRestrictionKey is used with a correct value' | ||
}, | ||
schema: [ | ||
{ | ||
type: 'object', | ||
properties: { | ||
supportedInterfaceNames: { | ||
type: 'array', | ||
items: { | ||
type: 'string' | ||
} | ||
}, | ||
supportedKeys: { | ||
type: 'array', | ||
items: { | ||
type: 'string' | ||
}, | ||
minItems: 1 | ||
} | ||
} | ||
} | ||
], | ||
messages: { | ||
notSupportedKey: '{{ actualKey }} is not supported. Supported restriction keys: {{ supportedKeys }}.', | ||
suggestUseSupportedKey: '{{ actualKey }} is not supported. Replace with {{ supportedKey }}.', | ||
notWrapWithQuotes: '`{{ actualKey }}` is not wrapped with quotes.', | ||
suggestWrapWithQuotes: 'Wrap `{{ actualKey }}` with quotes.', | ||
notInConfigurationInterface: '@o3rRestrictionKey can only be used in a `Configuration` interface.', | ||
noRestrictionKeyProvided: 'You must have at least one restriction key.' | ||
} | ||
}, | ||
defaultOptions, | ||
create: (context, [options]: Readonly<[O3rRestrictionKeyTagsRuleOption, ...any]>) => { | ||
const rule = (node: TSESTree.TSPropertySignature) => { | ||
const { sourceCode } = context; | ||
const comment = getNodeComment(node, sourceCode); | ||
|
||
if (!comment || comment.value.length === 0) { | ||
return; | ||
Check warning on line 76 in packages/@o3r/eslint-plugin/src/rules/typescript/o3r-restriction-key-tags/o3r-restriction-key-tags.ts Codecov / codecov/patchpackages/@o3r/eslint-plugin/src/rules/typescript/o3r-restriction-key-tags/o3r-restriction-key-tags.ts#L76
|
||
} | ||
|
||
const { loc, value: docText } = comment; | ||
const supportedKeys = options.supportedKeys || defaultOptions[0].supportedKeys; | ||
const supportedKeysSet = new Set(supportedKeys); | ||
|
||
if (supportedKeys.length === 0) { | ||
return context.report({ | ||
messageId: 'noRestrictionKeyProvided', | ||
node, | ||
loc | ||
}); | ||
} | ||
|
||
const actualKeys = Array.from(docText.matchAll(/@o3rRestrictionKey\s+(.*)/g)).map((match) => match[1]); | ||
|
||
if (actualKeys.length === 0) { | ||
return; | ||
} | ||
|
||
const interfaceDeclNode = node.parent?.parent; | ||
if (!isExtendingConfiguration(interfaceDeclNode, options.supportedInterfaceNames)) { | ||
return context.report({ | ||
messageId: 'notInConfigurationInterface', | ||
node, | ||
loc | ||
}); | ||
} | ||
|
||
actualKeys.forEach((actualKey) => { | ||
if (!supportedKeysSet.has(actualKey)) { | ||
if ( | ||
/((["']).*?\2)/.test(actualKey) | ||
&& supportedKeysSet.has(actualKey.replace(/(^["']|["']$)/g, '')) | ||
) { | ||
return; | ||
} | ||
const fix: (key: string) => TSESLint.ReportFixFunction = (key) => (fixer) => { | ||
return fixer.replaceTextRange(comment.range, createCommentString(comment.value.replace(actualKey, /[^\w]/.test(key) ? `"${key}"` : key))); | ||
}; | ||
return context.report({ | ||
messageId: 'notSupportedKey', | ||
node, | ||
loc, | ||
data: { | ||
actualKey, | ||
supportedKeys: supportedKeys.join(', ') | ||
}, | ||
suggest: supportedKeys.map((supportedKey) => ({ | ||
messageId: 'suggestUseSupportedKey', | ||
data: { | ||
actualKey, | ||
supportedKey | ||
}, | ||
fix: fix(supportedKey) | ||
})) | ||
}); | ||
} | ||
if (/[^\w]/.test(actualKey)) { | ||
const fix: TSESLint.ReportFixFunction = (fixer) => { | ||
return fixer.replaceTextRange(comment.range, createCommentString(comment.value.replace(actualKey, `"${actualKey}"`))); | ||
}; | ||
return context.report({ | ||
messageId: 'notWrapWithQuotes', | ||
data: { | ||
actualKey | ||
}, | ||
node, | ||
loc, | ||
fix, | ||
suggest: [{ | ||
messageId: 'suggestWrapWithQuotes', | ||
data: { | ||
actualKey | ||
}, | ||
fix | ||
}] | ||
}); | ||
} | ||
}); | ||
}; | ||
|
||
return { | ||
TSPropertySignature: rule | ||
}; | ||
} | ||
}); |
Oops, something went wrong.