-
Notifications
You must be signed in to change notification settings - Fork 103
Dictionary allow only string as key lint #4543
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 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
import { ESLintUtils } from '@typescript-eslint/utils'; | ||
import ts from 'typescript' | ||
|
||
const createRule = ESLintUtils.RuleCreator(name => `https://example.com/rule/${name}`) | ||
|
||
export default createRule({ | ||
name: 'dictionary-key-is-string', | ||
create(context) { | ||
return { | ||
TSTypeReference(node) { | ||
if (node.typeName.name === 'Dictionary') { | ||
const key = node.typeArguments.params[0] | ||
switch (key.type) { | ||
case 'TSTypeReference': | ||
// trace the reference to its original type definition | ||
const services = ESLintUtils.getParserServices(context) | ||
const type = services.getTypeAtLocation(key) | ||
|
||
// check that the type is a string or an enum (enum members evaluate to strings) | ||
if (type.intrinsicName !== 'string' && !(type.symbol?.flags & ts.SymbolFlags.RegularEnum)) { | ||
context.report({ node, messageId: 'stringKey' }) | ||
} | ||
break | ||
case 'TSStringKeyword': | ||
// type is string, skip | ||
break | ||
default: | ||
// unknown type! | ||
context.report({ node, messageId: 'stringKey' }) | ||
break | ||
} | ||
} | ||
}, | ||
} | ||
}, | ||
meta: { | ||
docs: { | ||
description: 'Dictionary keys must be strings', | ||
}, | ||
messages: { | ||
stringKey: "Dictionary's key must be a string" | ||
}, | ||
type: 'suggestion', | ||
}, | ||
defaultOptions: [] | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
import { RuleTester } from '@typescript-eslint/rule-tester' | ||
import rule from '../rules/dictionary-key-is-string.js' | ||
|
||
const ruleTester = new RuleTester({ | ||
languageOptions: { | ||
parserOptions: { | ||
projectService: { | ||
allowDefaultProject: ['*.ts*'], | ||
}, | ||
tsconfigRootDir: import.meta.dirname, | ||
}, | ||
}, | ||
}) | ||
|
||
ruleTester.run('dictionary-key-is-string', rule, { | ||
valid: [ | ||
`type MyDict = Dictionary<string, object>`, | ||
`type MyDict = Dictionary<string, any>`, | ||
`type MyDict = Dictionary<string, number>`, | ||
`enum MyEnum { foo, bar, baz } | ||
type MyDict = Dictionary<MyEnum, object>`, | ||
], | ||
invalid: [ | ||
{ | ||
code: | ||
`type MyKey = string | boolean | ||
type MyDict = Dictionary<MyKey, any>`, | ||
errors: [{ messageId: 'stringKey' }] | ||
}, | ||
{ | ||
code: `type MyDict = Dictionary<string | number, any>`, | ||
errors: [{ messageId: 'stringKey' }] | ||
}, | ||
{ | ||
code: `type MyDict = Dictionary<boolean, any>`, | ||
errors: [{ messageId: 'stringKey' }] | ||
}, | ||
{ | ||
code: `type MyDict = Dictionary<object, any>`, | ||
errors: [{ messageId: 'stringKey' }] | ||
} | ||
], | ||
}) |
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.
Nit: code style.