Skip to content

Commit

Permalink
feat(ts): ban const enum (#201)
Browse files Browse the repository at this point in the history
  • Loading branch information
zanminkian committed Jun 28, 2023
1 parent 504de83 commit 2bf0c8d
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/eslint-config-ts/index.js
Expand Up @@ -159,6 +159,7 @@ module.exports = {
'antfu/generic-spacing': 'error',
'antfu/no-cjs-exports': 'error',
'antfu/no-ts-export-equal': 'error',
'antfu/no-const-enum': 'error',

// off
'@typescript-eslint/consistent-indexed-object-style': 'off',
Expand Down
2 changes: 2 additions & 0 deletions packages/eslint-plugin-antfu/src/index.ts
Expand Up @@ -5,6 +5,7 @@ import preferInlineTypeImport from './rules/prefer-inline-type-import'
import topLevelFunction from './rules/top-level-function'
import noTsExportEqual from './rules/no-ts-export-equal'
import noCjsExports from './rules/no-cjs-exports'
import noConstEnum from './rules/no-const-enum'

export default {
rules: {
Expand All @@ -15,5 +16,6 @@ export default {
'top-level-function': topLevelFunction,
'no-cjs-exports': noCjsExports,
'no-ts-export-equal': noTsExportEqual,
'no-const-enum': noConstEnum,
},
}
25 changes: 25 additions & 0 deletions packages/eslint-plugin-antfu/src/rules/no-const-enum.test.ts
@@ -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' }],
})),
})
})
33 changes: 33 additions & 0 deletions packages/eslint-plugin-antfu/src/rules/no-const-enum.ts
@@ -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',
})
}
},
}
},
})

0 comments on commit 2bf0c8d

Please sign in to comment.