Skip to content

Commit 00ac9c3

Browse files
authored
fix(eslint-plugin): [prefer-literal-enum-member] allow negative numbers (typescript-eslint#2277)
1 parent ced6591 commit 00ac9c3

File tree

2 files changed

+66
-7
lines changed

2 files changed

+66
-7
lines changed

packages/eslint-plugin/src/rules/prefer-literal-enum-member.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { AST_NODE_TYPES } from '@typescript-eslint/experimental-utils';
22
import { createRule } from '../util';
33

4-
export default createRule<[], 'notLiteral'>({
4+
export default createRule({
55
name: 'prefer-literal-enum-member',
66
meta: {
77
type: 'suggestion',
@@ -22,15 +22,26 @@ export default createRule<[], 'notLiteral'>({
2222
return {
2323
TSEnumMember(node): void {
2424
// If there is no initializer, then this node is just the name of the member, so ignore.
25+
if (node.initializer == null) {
26+
return;
27+
}
28+
// any old literal
29+
if (node.initializer.type === AST_NODE_TYPES.Literal) {
30+
return;
31+
}
32+
// -1 and +1
2533
if (
26-
node.initializer != null &&
27-
node.initializer.type !== AST_NODE_TYPES.Literal
34+
node.initializer.type === AST_NODE_TYPES.UnaryExpression &&
35+
['+', '-'].includes(node.initializer.operator) &&
36+
node.initializer.argument.type === AST_NODE_TYPES.Literal
2837
) {
29-
context.report({
30-
node: node.id,
31-
messageId: 'notLiteral',
32-
});
38+
return;
3339
}
40+
41+
context.report({
42+
node: node.id,
43+
messageId: 'notLiteral',
44+
});
3445
},
3546
};
3647
},

packages/eslint-plugin/tests/rules/prefer-literal-enum-member.test.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ enum ValidNumber {
2323
}
2424
`,
2525
`
26+
enum ValidNumber {
27+
A = -42,
28+
}
29+
`,
30+
`
31+
enum ValidNumber {
32+
A = +42,
33+
}
34+
`,
35+
`
2636
enum ValidNull {
2737
A = null,
2838
}
@@ -121,6 +131,44 @@ enum InvalidExpression {
121131
},
122132
{
123133
code: `
134+
enum InvalidExpression {
135+
A = delete 2,
136+
B = -a,
137+
C = void 2,
138+
D = ~2,
139+
E = !0,
140+
}
141+
`,
142+
errors: [
143+
{
144+
messageId: 'notLiteral',
145+
line: 3,
146+
column: 3,
147+
},
148+
{
149+
messageId: 'notLiteral',
150+
line: 4,
151+
column: 3,
152+
},
153+
{
154+
messageId: 'notLiteral',
155+
line: 5,
156+
column: 3,
157+
},
158+
{
159+
messageId: 'notLiteral',
160+
line: 6,
161+
column: 3,
162+
},
163+
{
164+
messageId: 'notLiteral',
165+
line: 7,
166+
column: 3,
167+
},
168+
],
169+
},
170+
{
171+
code: `
124172
const variable = 'Test';
125173
enum InvalidVariable {
126174
A = 'TestStr',

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