forked from typescript-eslint/typescript-eslint
/
no-duplicate-enum-values.ts
72 lines (65 loc) · 1.84 KB
/
no-duplicate-enum-values.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils';
import * as util from '../util';
export default util.createRule({
name: 'no-duplicate-enum-values',
meta: {
type: 'problem',
docs: {
description: 'Disallow duplicate enum member values',
recommended: 'strict',
},
hasSuggestions: false,
messages: {
duplicateValue: 'Duplicate enum member value {{value}}.',
},
schema: [],
},
defaultOptions: [],
create(context) {
function isStringLiteral(
node: TSESTree.Expression,
): node is TSESTree.StringLiteral {
return (
node.type === AST_NODE_TYPES.Literal && typeof node.value === 'string'
);
}
function isNumberLiteral(
node: TSESTree.Expression,
): node is TSESTree.NumberLiteral {
return (
node.type === AST_NODE_TYPES.Literal && typeof node.value === 'number'
);
}
return {
TSEnumDeclaration(node: TSESTree.TSEnumDeclaration): void {
const enumMembers = node.members;
const seenValues = new Set<number | string>();
enumMembers.forEach(member => {
if (member.initializer === undefined) {
return;
}
let value: string | number | undefined;
if (isStringLiteral(member.initializer)) {
value = String(member.initializer.value);
} else if (isNumberLiteral(member.initializer)) {
value = Number(member.initializer.value);
}
if (value === undefined) {
return;
}
if (seenValues.has(value)) {
context.report({
node: member,
messageId: 'duplicateValue',
data: {
value,
},
});
} else {
seenValues.add(value);
}
});
},
};
},
});