-
Notifications
You must be signed in to change notification settings - Fork 125
Forbid native TypeScript collection types #5598
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,25 +20,45 @@ import { ESLintUtils } from '@typescript-eslint/utils'; | |
|
|
||
| const createRule = ESLintUtils.RuleCreator(name => `https://example.com/rule/${name}`) | ||
|
|
||
| const TYPES_TO_AVOID = ['Record', 'Partial', 'Required', 'Pick', 'Omit']; | ||
| const UTILITY_TYPES = ['Record', 'Partial', 'Required', 'Pick', 'Omit']; | ||
|
|
||
| const COLLECTION_TYPES = ['Map', 'Set', 'WeakMap', 'WeakSet']; | ||
|
|
||
| const TYPES_TO_AVOID = [...UTILITY_TYPES, ...COLLECTION_TYPES]; | ||
|
|
||
| const TYPE_SUGGESTIONS = { | ||
| 'Record': 'Use Dictionary instead', | ||
| 'Map': 'Use Dictionary instead', | ||
| 'Set': 'Use an array type instead (e.g., string[])', | ||
| 'WeakMap': 'Use Dictionary instead', | ||
| 'WeakSet': 'Use an array type instead', | ||
| }; | ||
|
||
|
|
||
| export default createRule({ | ||
| name: 'no-native-types', | ||
| create(context) { | ||
| return { | ||
| TSTypeReference(node) { | ||
| if (TYPES_TO_AVOID.includes(node.typeName.name)) { | ||
| context.report({ node, messageId: 'stringKey' }) | ||
| const typeName = node.typeName.name; | ||
| if (TYPES_TO_AVOID.includes(typeName)) { | ||
| context.report({ | ||
| node, | ||
| messageId: 'noNativeType', | ||
| data: { | ||
| type: typeName, | ||
| suggestion: TYPE_SUGGESTIONS[typeName] || 'Use spec-defined aliases instead' | ||
| } | ||
| }) | ||
| } | ||
| }, | ||
| } | ||
| }, | ||
| meta: { | ||
| docs: { | ||
| description: 'Typescript native types not allowed, use aliases', | ||
| description: 'TypeScript native utility and collection types not allowed, use spec-defined aliases', | ||
| }, | ||
| messages: { | ||
| stringKey: "Typescript native types not allowed, use aliases" | ||
| noNativeType: 'Native TypeScript type "{{type}}" is not allowed. {{suggestion}}.' | ||
| }, | ||
| type: 'suggestion', | ||
| }, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,7 +17,7 @@ | |
| * under the License. | ||
| */ | ||
| import { RuleTester } from '@typescript-eslint/rule-tester' | ||
| import rule from '../rules/dictionary-key-is-string.js' | ||
| import rule from '../rules/no-native-types.js' | ||
|
|
||
| const ruleTester = new RuleTester({ | ||
| languageOptions: { | ||
|
|
@@ -32,32 +32,53 @@ const ruleTester = new RuleTester({ | |
|
|
||
| ruleTester.run('no-native-types', rule, { | ||
| valid: [ | ||
| `type MyRecord = Record<string, object>`, | ||
| `type MyPart = Partial<Record>`, | ||
| `type MyReq = Required<string>`, | ||
| `type MyPick Pick<integer,"something">`, | ||
| `type MyOmit = Omit<Record, "something">`, | ||
| `type MyDict = Dictionary<string, object>`, | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The original test file had a bug...it included native types like I corrected the tests to properly reflect the rule's intent:
also added test coverage for the new collection types ( |
||
| `type MyMapping = Dictionary<string, any>`, | ||
| `type MyArray = string[]`, | ||
| `type MyList = Array<integer>`, | ||
| `type MyType = { field: string }`, | ||
| `class MyClass { prop: integer }`, | ||
| ], | ||
| invalid: [ | ||
| { | ||
| code: `type MyRecord = Record<string, object>`, | ||
| errors: [{ messageId: 'stringKey' }] | ||
| errors: [{ messageId: 'noNativeType' }] | ||
| }, | ||
| { | ||
| code: `type MyPart = Partial<Record>`, | ||
| errors: [{ messageId: 'stringKey' }] | ||
| code: `type MyPart = Partial<SomeType>`, | ||
| errors: [{ messageId: 'noNativeType' }] | ||
| }, | ||
| { | ||
| code: `type MyReq = Required<string>`, | ||
| errors: [{ messageId: 'stringKey' }] | ||
| code: `type MyReq = Required<SomeType>`, | ||
| errors: [{ messageId: 'noNativeType' }] | ||
| }, | ||
| { | ||
| code: `type MyPick Pick<integer,"something">`, | ||
| errors: [{ messageId: 'stringKey' }] | ||
| code: `type MyPick = Pick<SomeType, "field">`, | ||
| errors: [{ messageId: 'noNativeType' }] | ||
| }, | ||
| { | ||
| code: `type MyOmit = Omit<Record, "something">`, | ||
| errors: [{ messageId: 'stringKey' }] | ||
| } | ||
| code: `type MyOmit = Omit<SomeType, "field">`, | ||
| errors: [{ messageId: 'noNativeType' }] | ||
| }, | ||
| { | ||
| code: `type MyMap = Map<string, object>`, | ||
| errors: [{ messageId: 'noNativeType' }] | ||
| }, | ||
| { | ||
| code: `type MySet = Set<string>`, | ||
| errors: [{ messageId: 'noNativeType' }] | ||
| }, | ||
| { | ||
| code: `type MyWeakMap = WeakMap<object, string>`, | ||
| errors: [{ messageId: 'noNativeType' }] | ||
| }, | ||
| { | ||
| code: `type MyWeakSet = WeakSet<object>`, | ||
| errors: [{ messageId: 'noNativeType' }] | ||
| }, | ||
| { | ||
| code: `class MyClass { items: Map<string, number> }`, | ||
| errors: [{ messageId: 'noNativeType' }] | ||
| }, | ||
| ], | ||
| }) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we please add
Arrayto the list? It could also be a follow-up, as it generates 106 errors.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will do this in a follow up pr