Skip to content

Commit 2bf0c8d

Browse files
authored
feat(ts): ban const enum (#201)
1 parent 504de83 commit 2bf0c8d

File tree

4 files changed

+61
-0
lines changed

4 files changed

+61
-0
lines changed

packages/eslint-config-ts/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ module.exports = {
159159
'antfu/generic-spacing': 'error',
160160
'antfu/no-cjs-exports': 'error',
161161
'antfu/no-ts-export-equal': 'error',
162+
'antfu/no-const-enum': 'error',
162163

163164
// off
164165
'@typescript-eslint/consistent-indexed-object-style': 'off',

packages/eslint-plugin-antfu/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import preferInlineTypeImport from './rules/prefer-inline-type-import'
55
import topLevelFunction from './rules/top-level-function'
66
import noTsExportEqual from './rules/no-ts-export-equal'
77
import noCjsExports from './rules/no-cjs-exports'
8+
import noConstEnum from './rules/no-const-enum'
89

910
export default {
1011
rules: {
@@ -15,5 +16,6 @@ export default {
1516
'top-level-function': topLevelFunction,
1617
'no-cjs-exports': noCjsExports,
1718
'no-ts-export-equal': noTsExportEqual,
19+
'no-const-enum': noConstEnum,
1820
},
1921
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { RuleTester } from '@typescript-eslint/utils/dist/ts-eslint'
2+
import { it } from 'vitest'
3+
import rule, { RULE_NAME } from './no-const-enum'
4+
5+
const valids = [
6+
'enum E {}',
7+
]
8+
9+
const invalids = [
10+
'const enum E {}',
11+
]
12+
13+
it('runs', () => {
14+
const ruleTester: RuleTester = new RuleTester({
15+
parser: require.resolve('@typescript-eslint/parser'),
16+
})
17+
18+
ruleTester.run(RULE_NAME, rule, {
19+
valid: valids,
20+
invalid: invalids.map(i => ({
21+
code: i,
22+
errors: [{ messageId: 'noConstEnum' }],
23+
})),
24+
})
25+
})
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { createEslintRule } from '../utils'
2+
3+
export const RULE_NAME = 'no-const-enum'
4+
export type MessageIds = 'noConstEnum'
5+
export type Options = []
6+
7+
export default createEslintRule<Options, MessageIds>({
8+
name: RULE_NAME,
9+
meta: {
10+
type: 'problem',
11+
docs: {
12+
description: 'Disallow using `const enum` expression',
13+
recommended: 'error',
14+
},
15+
schema: [],
16+
messages: {
17+
noConstEnum: 'Do not use `const enum` expression',
18+
},
19+
},
20+
defaultOptions: [],
21+
create: (context) => {
22+
return {
23+
TSEnumDeclaration: (node) => {
24+
if (node.const) {
25+
context.report({
26+
node,
27+
messageId: 'noConstEnum',
28+
})
29+
}
30+
},
31+
}
32+
},
33+
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy