Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(eslint-plugin): [no-magic-numbers] add support for enums #543

Merged
merged 3 commits into from Jun 16, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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