Skip to content

Commit 051da08

Browse files
committed
added dictionary rule eslint
1 parent 67c5c15 commit 051da08

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed

specification/eslint.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export default defineConfig({
3232
plugins: { 'es-spec-validator': validator },
3333
rules: {
3434
'es-spec-validator/single-key-dictionary-key-is-string': 'error',
35+
'es-spec-validator/dictionary-key-is-string': 'error',
3536
'es-spec-validator/invalid-node-types': 'warn'
3637
}
3738
})

validator/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ It is configured [in the specification directory](../specification/eslint.config
88
| Name | Description |
99
| - | - |
1010
| `single-key-dictionary-key-is-string` | `SingleKeyDictionary` keys must be strings. |
11+
| `dictionary-key-is-string` | `Dictionary` keys must be strings. |
1112
| `invalid-node-types` | The spec uses a subset of TypeScript, so some types, clauses and expressions are not allowed. |
1213

1314
## Usage

validator/eslint-plugin-es-spec.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717
* under the License.
1818
*/
1919
import singleKeyDict from './rules/single-key-dictionary-key-is-string.js'
20+
import Dict from './rules/dictionary-key-is-string.js'
2021
import invalidNodeTypes from './rules/invalid-node-types.js'
2122

2223
export default {
2324
rules: {
2425
'single-key-dictionary-key-is-string': singleKeyDict,
26+
'dictionary-key-is-string': Dict,
2527
'invalid-node-types': invalidNodeTypes,
2628
}
2729
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
import { ESLintUtils } from '@typescript-eslint/utils';
20+
import ts from 'typescript'
21+
22+
const createRule = ESLintUtils.RuleCreator(name => `https://example.com/rule/${name}`)
23+
24+
export default createRule({
25+
name: 'dictionary-key-is-string',
26+
create(context) {
27+
return {
28+
TSTypeReference(node) {
29+
if (node.typeName.name === 'Dictionary') {
30+
const key = node.typeArguments.params[0]
31+
switch (key.type) {
32+
case 'TSTypeReference':
33+
// trace the reference to its original type definition
34+
const services = ESLintUtils.getParserServices(context)
35+
const type = services.getTypeAtLocation(key)
36+
37+
// check that the type is a string or an enum (enum members evaluate to strings)
38+
if (type.intrinsicName !== 'string' && !(type.symbol?.flags & ts.SymbolFlags.RegularEnum)) {
39+
context.report({ node, messageId: 'stringKey' })
40+
}
41+
break
42+
case 'TSStringKeyword':
43+
// type is string, skip
44+
break
45+
default:
46+
// unknown type!
47+
context.report({ node, messageId: 'stringKey' })
48+
break
49+
}
50+
}
51+
},
52+
}
53+
},
54+
meta: {
55+
docs: {
56+
description: 'Dictionary keys must be strings',
57+
},
58+
messages: {
59+
stringKey: "Dictionary's key must be a string"
60+
},
61+
type: 'suggestion',
62+
},
63+
defaultOptions: []
64+
})

0 commit comments

Comments
 (0)