Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion validator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ It is configured [in the specification directory](../specification/eslint.config
|---------------------------------------| - |
| `single-key-dictionary-key-is-string` | `SingleKeyDictionary` keys must be strings. |
| `dictionary-key-is-string` | `Dictionary` keys must be strings. |
| `no-native-types` | `Typescript native types not allowed, use aliases. |
| `no-native-types` | TypeScript native utility types (`Record`, `Partial`, etc.) and collection types (`Map`, `Set`, etc.) are not allowed. Use spec-defined aliases like `Dictionary` instead. |
| `invalid-node-types` | The spec uses a subset of TypeScript, so some types, clauses and expressions are not allowed. |
| `no-generic-number` | Generic `number` type is not allowed outside of `_types/Numeric.ts`. Use concrete numeric types like `integer`, `long`, `float`, `double`, etc. |
| `request-must-have-urls` | All Request interfaces extending `RequestBase` must have a `urls` property defining their endpoint paths and HTTP methods. |
Expand Down
28 changes: 23 additions & 5 deletions validator/rules/no-native-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,43 @@ 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 TYPE_SUGGESTIONS = {
'Record': 'Use Dictionary instead',
'Partial': 'Use spec-defined aliases instead',
'Required': 'Use spec-defined aliases instead',
'Pick': 'Use spec-defined aliases instead',
'Omit': 'Use spec-defined aliases 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 (TYPE_SUGGESTIONS[typeName]) {
context.report({
node,
messageId: 'noNativeType',
data: {
type: typeName,
suggestion: TYPE_SUGGESTIONS[typeName]
}
})
}
},
}
},
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',
},
Expand Down
51 changes: 35 additions & 16 deletions validator/test/no-native-types.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -32,32 +32,51 @@ 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>`,
`type MyMapping = Dictionary<string, any>`,
`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' }]
},
],
})