Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(eslint-plugin): [no-magic-numbers] add support for enums (#543)
  • Loading branch information
jonathanrdelgado authored and bradzacher committed Jun 16, 2019
1 parent 753ad75 commit 5c40d01
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 1 deletion.
24 changes: 24 additions & 0 deletions packages/eslint-plugin/docs/rules/no-magic-numbers.md
Expand Up @@ -41,4 +41,28 @@ Examples of **correct** code for the `{ "ignoreNumericLiteralTypes": true }` opt
type SmallPrimes = 2 | 3 | 5 | 7 | 11;
```

### ignoreEnums

A boolean to specify if enums used in Typescript are considered okay. `false` by default.

Examples of **incorrect** code for the `{ "ignoreEnum": false }` option:

```ts
/*eslint @typescript-eslint/no-magic-numbers: ["error", { "ignoreEnum": false }]*/

enum foo = {
SECOND = 1000,
}
```

Examples of **correct** code for the `{ "ignoreEnum": true }` option:

```ts
/*eslint @typescript-eslint/no-magic-numbers: ["error", { "ignoreEnum": true }]*/

enum foo = {
SECOND = 1000,
}
```

<sup>Taken with ❤️ [from ESLint core](https://github.com/eslint/eslint/blob/master/docs/rules/no-magic-numbers.md)</sup>
24 changes: 23 additions & 1 deletion packages/eslint-plugin/src/rules/no-magic-numbers.ts
Expand Up @@ -29,6 +29,9 @@ export default util.createRule<Options, MessageIds>({
ignoreNumericLiteralTypes: {
type: 'boolean',
},
ignoreEnums: {
type: 'boolean',
},
},
},
],
Expand All @@ -41,6 +44,7 @@ export default util.createRule<Options, MessageIds>({
enforceConst: false,
detectObjects: false,
ignoreNumericLiteralTypes: false,
ignoreEnums: false,
},
],
create(context, [options]) {
Expand Down Expand Up @@ -85,6 +89,19 @@ export default util.createRule<Options, MessageIds>({
return false;
}

/**
* Checks if the node parent is a Typescript enum member
* @param node the node to be validated.
* @returns true if the node parent is a Typescript enum member
* @private
*/
function isParentTSEnumDeclaration(node: TSESTree.Node): boolean {
return (
typeof node.parent !== 'undefined' &&
node.parent.type === AST_NODE_TYPES.TSEnumMember
);
}

/**
* Checks if the node parent is a Typescript literal type
* @param node the node to be validated.
Expand Down Expand Up @@ -133,7 +150,12 @@ export default util.createRule<Options, MessageIds>({

return {
Literal(node) {
// Check TypeScript specific nodes
// Check if the node is a TypeScript enum declaration
if (options.ignoreEnums && isParentTSEnumDeclaration(node)) {
return;
}

// Check TypeScript specific nodes for Numeric Literal
if (
options.ignoreNumericLiteralTypes &&
isNumber(node) &&
Expand Down
36 changes: 36 additions & 0 deletions packages/eslint-plugin/tests/rules/no-magic-numbers.test.ts
Expand Up @@ -33,6 +33,14 @@ ruleTester.run('no-magic-numbers', rule, {
code: 'type Foo = 1 | -1;',
options: [{ ignoreNumericLiteralTypes: true }],
},
{
code: 'enum foo { SECOND = 1000 }',
options: [{ ignoreEnums: true }],
},
{
code: 'enum foo { SECOND = 1000, NUM = "0123456789" }',
options: [{ ignoreEnums: true }],
},
],

invalid: [
Expand Down Expand Up @@ -130,5 +138,33 @@ ruleTester.run('no-magic-numbers', rule, {
},
],
},
{
code: 'enum foo { SECOND = 1000 }',
options: [{ ignoreEnums: false }],
errors: [
{
messageId: 'noMagic',
data: {
raw: '1000',
},
line: 1,
column: 21,
},
],
},
{
code: 'enum foo { SECOND = 1000, NUM = "0123456789" }',
options: [{ ignoreEnums: false }],
errors: [
{
messageId: 'noMagic',
data: {
raw: '1000',
},
line: 1,
column: 21,
},
],
},
],
});
1 change: 1 addition & 0 deletions packages/eslint-plugin/typings/eslint-rules.d.ts
Expand Up @@ -180,6 +180,7 @@ declare module 'eslint/lib/rules/no-magic-numbers' {
enforceConst?: boolean;
detectObjects?: boolean;
ignoreNumericLiteralTypes?: boolean;
ignoreEnums?: boolean;
}
],
{
Expand Down

0 comments on commit 5c40d01

Please sign in to comment.