feat(ts): ban const enum (#201)

This commit is contained in:
曾明健 2023-06-28 20:05:23 +08:00 committed by GitHub
parent 504de8392a
commit 2bf0c8d727
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 61 additions and 0 deletions

View File

@ -159,6 +159,7 @@ module.exports = {
'antfu/generic-spacing': 'error', 'antfu/generic-spacing': 'error',
'antfu/no-cjs-exports': 'error', 'antfu/no-cjs-exports': 'error',
'antfu/no-ts-export-equal': 'error', 'antfu/no-ts-export-equal': 'error',
'antfu/no-const-enum': 'error',
// off // off
'@typescript-eslint/consistent-indexed-object-style': 'off', '@typescript-eslint/consistent-indexed-object-style': 'off',

View File

@ -5,6 +5,7 @@ import preferInlineTypeImport from './rules/prefer-inline-type-import'
import topLevelFunction from './rules/top-level-function' import topLevelFunction from './rules/top-level-function'
import noTsExportEqual from './rules/no-ts-export-equal' import noTsExportEqual from './rules/no-ts-export-equal'
import noCjsExports from './rules/no-cjs-exports' import noCjsExports from './rules/no-cjs-exports'
import noConstEnum from './rules/no-const-enum'
export default { export default {
rules: { rules: {
@ -15,5 +16,6 @@ export default {
'top-level-function': topLevelFunction, 'top-level-function': topLevelFunction,
'no-cjs-exports': noCjsExports, 'no-cjs-exports': noCjsExports,
'no-ts-export-equal': noTsExportEqual, 'no-ts-export-equal': noTsExportEqual,
'no-const-enum': noConstEnum,
}, },
} }

View File

@ -0,0 +1,25 @@
import { RuleTester } from '@typescript-eslint/utils/dist/ts-eslint'
import { it } from 'vitest'
import rule, { RULE_NAME } from './no-const-enum'
const valids = [
'enum E {}',
]
const invalids = [
'const enum E {}',
]
it('runs', () => {
const ruleTester: RuleTester = new RuleTester({
parser: require.resolve('@typescript-eslint/parser'),
})
ruleTester.run(RULE_NAME, rule, {
valid: valids,
invalid: invalids.map(i => ({
code: i,
errors: [{ messageId: 'noConstEnum' }],
})),
})
})

View File

@ -0,0 +1,33 @@
import { createEslintRule } from '../utils'
export const RULE_NAME = 'no-const-enum'
export type MessageIds = 'noConstEnum'
export type Options = []
export default createEslintRule<Options, MessageIds>({
name: RULE_NAME,
meta: {
type: 'problem',
docs: {
description: 'Disallow using `const enum` expression',
recommended: 'error',
},
schema: [],
messages: {
noConstEnum: 'Do not use `const enum` expression',
},
},
defaultOptions: [],
create: (context) => {
return {
TSEnumDeclaration: (node) => {
if (node.const) {
context.report({
node,
messageId: 'noConstEnum',
})
}
},
}
},
})