Skip to content

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

Merged
merged 5 commits into from
Jun 13, 2025
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
9 changes: 1 addition & 8 deletions output/openapi/elasticsearch-openapi.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 1 addition & 8 deletions output/openapi/elasticsearch-serverless-openapi.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 5 additions & 17 deletions output/schema/schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion output/typescript/types.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion specification/_types/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export type SequenceNumber = long

export type PropertyName = string
export type RelationName = string
export type TaskId = string | integer
export type TaskId = string
/** @doc_id fuzziness */
export type Fuzziness = string | integer
/** @doc_id query-dsl-multi-term-rewrite */
Expand Down
1 change: 1 addition & 0 deletions specification/eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export default defineConfig({
plugins: { 'es-spec-validator': validator },
rules: {
'es-spec-validator/single-key-dictionary-key-is-string': 'error',
'es-spec-validator/dictionary-key-is-string': 'error',
'es-spec-validator/invalid-node-types': 'warn'
}
})
1 change: 1 addition & 0 deletions validator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ It is configured [in the specification directory](../specification/eslint.config
| Name | Description |
| - | - |
| `single-key-dictionary-key-is-string` | `SingleKeyDictionary` keys must be strings. |
| `dictionary-key-is-string` | `Dictionary` keys must be strings. |
| `invalid-node-types` | The spec uses a subset of TypeScript, so some types, clauses and expressions are not allowed. |

## Usage
Expand Down
2 changes: 2 additions & 0 deletions validator/eslint-plugin-es-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
* under the License.
*/
import singleKeyDict from './rules/single-key-dictionary-key-is-string.js'
import dict from './rules/dictionary-key-is-string.js'
import invalidNodeTypes from './rules/invalid-node-types.js'

export default {
rules: {
'single-key-dictionary-key-is-string': singleKeyDict,
'dictionary-key-is-string': dict,
'invalid-node-types': invalidNodeTypes,
}
}
64 changes: 64 additions & 0 deletions validator/rules/dictionary-key-is-string.js
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: []
})
61 changes: 61 additions & 0 deletions validator/test/dictionary-key-is-string.test.js
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' }]
}
],
})