Skip to content
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

feat(ts): ban const enum #201

Merged
merged 1 commit into from Jun 28, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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',
})
}
},
}
},
})