Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions specification/eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export default defineConfig({
rules: {
'es-spec-validator/single-key-dictionary-key-is-string': 'error',
'es-spec-validator/dictionary-key-is-string': 'error',
'es-spec-validator/no-native-types': 'error',
'es-spec-validator/invalid-node-types': 'warn'
}
})
9 changes: 5 additions & 4 deletions validator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ It is configured [in the specification directory](../specification/eslint.config

## Rules

| Name | Description |
| - | - |
| 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. |
| `dictionary-key-is-string` | `Dictionary` keys must be strings. |
| `no-native-types` | `Typescript native types not allowed, use aliases. |
| `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 @@ -18,12 +18,14 @@
*/
import singleKeyDict from './rules/single-key-dictionary-key-is-string.js'
import dict from './rules/dictionary-key-is-string.js'
import noNativeTypes from './rules/no-native-types.js'
import invalidNodeTypes from './rules/invalid-node-types.js'

export default {
rules: {
'single-key-dictionary-key-is-string': singleKeyDict,
'dictionary-key-is-string': dict,
'no-native-types': noNativeTypes,
'invalid-node-types': invalidNodeTypes,
}
}
46 changes: 46 additions & 0 deletions validator/rules/no-native-types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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';

const createRule = ESLintUtils.RuleCreator(name => `https://example.com/rule/${name}`)

const TYPES_TO_AVOID = ['Record', 'Partial', 'Required', 'Pick', 'Omit'];

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' })
}
},
}
},
meta: {
docs: {
description: 'Typescript native types not allowed, use aliases',
},
messages: {
stringKey: "Typescript native types not allowed, use aliases"
},
type: 'suggestion',
},
defaultOptions: []
})
63 changes: 63 additions & 0 deletions validator/test/no-native-types.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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('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">`,
],
invalid: [
{
code: `type MyRecord = Record<string, object>`,
errors: [{ messageId: 'stringKey' }]
},
{
code: `type MyPart = Partial<Record>`,
errors: [{ messageId: 'stringKey' }]
},
{
code: `type MyReq = Required<string>`,
errors: [{ messageId: 'stringKey' }]
},
{
code: `type MyPick Pick<integer,"something">`,
errors: [{ messageId: 'stringKey' }]
},
{
code: `type MyOmit = Omit<Record, "something">`,
errors: [{ messageId: 'stringKey' }]
}
],
})
Loading