🌐 AI搜索 & 代理 主页
Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 38 additions & 12 deletions packages/eslint-plugin/src/rules/no-duplicate-enum-values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ export default createRule({
);
}

function isSupportedUnary(
node: TSESTree.Expression,
): node is TSESTree.UnaryExpression {
return (
node.type === AST_NODE_TYPES.UnaryExpression &&
['-', '+'].includes(node.operator)
);
}

function isStaticTemplateLiteral(
node: TSESTree.Expression,
): node is TSESTree.TemplateLiteral {
Expand All @@ -46,30 +55,47 @@ export default createRule({
);
}

function getMemberValue(
initializer: TSESTree.Expression,
): number | string | undefined {
switch (true) {
case isStringLiteral(initializer):
case isNumberLiteral(initializer):
return initializer.value;
case isSupportedUnary(initializer): {
const inner = Number(getMemberValue(initializer.argument));
if (Number.isNaN(inner)) {
return undefined;
}

return initializer.operator === '-' ? -inner : inner;
}
case isStaticTemplateLiteral(initializer):
return initializer.quasis[0].value.cooked;
default:
return undefined;
}
}

return {
TSEnumDeclaration(node: TSESTree.TSEnumDeclaration): void {
const enumMembers = node.body.members;
const seenValues = new Set<number | string>();
const seenValues: (number | string)[] = [];

enumMembers.forEach(member => {
if (member.initializer == null) {
return;
}

let value: number | string | undefined;
if (isStringLiteral(member.initializer)) {
value = member.initializer.value;
} else if (isNumberLiteral(member.initializer)) {
value = member.initializer.value;
} else if (isStaticTemplateLiteral(member.initializer)) {
value = member.initializer.quasis[0].value.cooked;
}

const value = getMemberValue(member.initializer);
if (value == null) {
return;
}

if (seenValues.has(value)) {
const isAlreadyPresent = seenValues.some(seenValue =>
Object.is(seenValue, value),
);
if (isAlreadyPresent) {
context.report({
node: member,
messageId: 'duplicateValue',
Expand All @@ -78,7 +104,7 @@ export default createRule({
},
});
} else {
seenValues.add(value);
seenValues.push(value);
}
});
},
Expand Down
244 changes: 244 additions & 0 deletions packages/eslint-plugin/tests/rules/no-duplicate-enum-values.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,54 @@ enum E {
}
`,
`
enum E {
A = -1,
B = -2,
}
`,
`
enum E {
A = +1,
B = +2,
}
`,
`
enum E {
A = +1,
B = -1,
}
`,
`
enum E {
A = 1,
B = -1,
}
`,
`
enum E {
A = -0,
B = +0,
}
`,
`
enum E {
A = -0,
B = 0,
}
`,
`
enum E {
A = 1,
B = '1',
}
`,
`
enum E {
A = -1,
B = '-1',
}
`,
`
enum E {
A = 'A',
B = 'B',
Expand Down Expand Up @@ -74,6 +122,42 @@ enum E {
}
`,
`
enum E {
A = NaN,
B = NaN,
}
`,
`
enum E {
A = NaN,
B = -NaN,
}
`,
`
enum E {
A = 'NaN',
B = NaN,
}
`,
`
enum E {
A = -+-0,
B = +-+0,
}
`,
`
enum E {
A = -'',
B = 0,
}
`,
`
enum E {
A = Infinity,
B = Infinity,
}
`,
`
const A = 'A';
enum E {
A = 'A',
Expand All @@ -100,6 +184,166 @@ enum E {
},
{
code: `
enum E {
A = -1,
B = -1,
}
`,
errors: [
{
column: 3,
data: { value: -1 },
line: 4,
messageId: 'duplicateValue',
},
],
},
{
code: `
enum E {
A = +1,
B = +1,
}
`,
errors: [
{
column: 3,
data: { value: 1 },
line: 4,
messageId: 'duplicateValue',
},
],
},
{
code: `
enum E {
A = +0,
B = 0,
}
`,
errors: [
{
column: 3,
data: { value: 0 },
line: 4,
messageId: 'duplicateValue',
},
],
},
{
code: `
enum E {
A = -0,
B = -0,
}
`,
errors: [
{
column: 3,
data: { value: -0 },
line: 4,
messageId: 'duplicateValue',
},
],
},
{
code: `
enum E {
A = +'0',
B = 0,
}
`,
errors: [
{
column: 3,
data: { value: 0 },
line: 4,
messageId: 'duplicateValue',
},
],
},
{
code: `
enum E {
A = 0x10,
B = 16,
}
`,
errors: [
{
column: 3,
data: { value: 0x10 },
line: 4,
messageId: 'duplicateValue',
},
],
},
{
code: `
enum E {
A = +'1e2',
B = 100,
}
`,
errors: [
{
column: 3,
data: { value: 1e2 },
line: 4,
messageId: 'duplicateValue',
},
],
},
{
code: `
enum E {
A = +'',
B = 0,
}
`,
errors: [
{
column: 3,
data: { value: 0 },
line: 4,
messageId: 'duplicateValue',
},
],
},
{
code: `
enum E {
A = -+1,
B = +-1,
}
`,
errors: [
{
column: 3,
data: { value: -1 },
line: 4,
messageId: 'duplicateValue',
},
],
},
{
code: `
enum E {
A = -\`0\`,
B = -0,
}
`,
errors: [
{
column: 3,
data: { value: -0 },
line: 4,
messageId: 'duplicateValue',
},
],
},
{
code: `
enum E {
A = 'A',
B = 'A',
Expand Down
Loading