From 95b30324428ccf28d89296a4474900066f7536a4 Mon Sep 17 00:00:00 2001 From: Alexander T Date: Tue, 3 Sep 2019 17:49:51 +0300 Subject: [PATCH 1/4] feat(eslint-plugin): [no-magic-numbers] ignoreReadonlyClassProperties --- .../src/rules/no-magic-numbers.ts | 25 +++++++++++ .../tests/rules/no-magic-numbers.test.ts | 44 +++++++++++++++++++ .../eslint-plugin/typings/eslint-rules.d.ts | 1 + 3 files changed, 70 insertions(+) diff --git a/packages/eslint-plugin/src/rules/no-magic-numbers.ts b/packages/eslint-plugin/src/rules/no-magic-numbers.ts index 005bae9eb0a..649ed6a715f 100644 --- a/packages/eslint-plugin/src/rules/no-magic-numbers.ts +++ b/packages/eslint-plugin/src/rules/no-magic-numbers.ts @@ -33,6 +33,9 @@ export default util.createRule({ ignoreEnums: { type: 'boolean', }, + ignoreReadonlyClassProperties: { + type: 'boolean', + }, }, }, ], @@ -46,6 +49,7 @@ export default util.createRule({ detectObjects: false, ignoreNumericLiteralTypes: false, ignoreEnums: false, + ignoreReadonlyClassProperties: false, }, ], create(context, [options]) { @@ -149,6 +153,20 @@ export default util.createRule({ return false; } + /** + * Checks if the node parent is a readonly class property + * @param node the node to be validated. + * @returns true if the node parent is a readonly class property + * @private + */ + function isParentTSReadonlyClassProperty(node: TSESTree.Node): boolean { + return ( + typeof node.parent !== 'undefined' && + node.parent.type === AST_NODE_TYPES.ClassProperty && + !!node.parent.readonly + ); + } + return { Literal(node): void { // Check if the node is a TypeScript enum declaration @@ -156,6 +174,13 @@ export default util.createRule({ return; } + if ( + options.ignoreReadonlyClassProperties && + isParentTSReadonlyClassProperty(node) + ) { + return; + } + // Check TypeScript specific nodes for Numeric Literal if ( options.ignoreNumericLiteralTypes && diff --git a/packages/eslint-plugin/tests/rules/no-magic-numbers.test.ts b/packages/eslint-plugin/tests/rules/no-magic-numbers.test.ts index 09b6c35d0b4..1ab26cfa704 100644 --- a/packages/eslint-plugin/tests/rules/no-magic-numbers.test.ts +++ b/packages/eslint-plugin/tests/rules/no-magic-numbers.test.ts @@ -41,6 +41,17 @@ ruleTester.run('no-magic-numbers', rule, { code: 'enum foo { SECOND = 1000, NUM = "0123456789" }', options: [{ ignoreEnums: true }], }, + { + code: ` +class Foo { + readonly A = 1; + readonly B = 2; + public static readonly C = 1; + static readonly D = 1; +} + `, + options: [{ ignoreReadonlyClassProperties: true }], + }, ], invalid: [ @@ -166,5 +177,38 @@ ruleTester.run('no-magic-numbers', rule, { }, ], }, + { + code: ` +class Foo { + readonly A = 1; + readonly B = 2; + public static readonly C = 1; + static readonly D = 1; +} + `, + options: [{ ignoreReadonlyClassProperties: false }], + errors: [ + { + messageId: 'noMagic', + line: 3, + column: 16, + }, + { + messageId: 'noMagic', + line: 4, + column: 16, + }, + { + messageId: 'noMagic', + line: 5, + column: 30, + }, + { + messageId: 'noMagic', + line: 6, + column: 23, + }, + ], + }, ], }); diff --git a/packages/eslint-plugin/typings/eslint-rules.d.ts b/packages/eslint-plugin/typings/eslint-rules.d.ts index 9eb02d5cce0..2b93e714378 100644 --- a/packages/eslint-plugin/typings/eslint-rules.d.ts +++ b/packages/eslint-plugin/typings/eslint-rules.d.ts @@ -199,6 +199,7 @@ declare module 'eslint/lib/rules/no-magic-numbers' { detectObjects?: boolean; ignoreNumericLiteralTypes?: boolean; ignoreEnums?: boolean; + ignoreReadonlyClassProperties?: boolean; }, ], { From 6310717690dd32163eb76dac7c3210015eeed2f1 Mon Sep 17 00:00:00 2001 From: Alexander T Date: Tue, 3 Sep 2019 18:36:15 +0300 Subject: [PATCH 2/4] feat(eslint-plugin): [no-magic-numbers] update docs --- .../docs/rules/no-magic-numbers.md | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/packages/eslint-plugin/docs/rules/no-magic-numbers.md b/packages/eslint-plugin/docs/rules/no-magic-numbers.md index cf647833887..215890a2e83 100644 --- a/packages/eslint-plugin/docs/rules/no-magic-numbers.md +++ b/packages/eslint-plugin/docs/rules/no-magic-numbers.md @@ -41,6 +41,34 @@ Examples of **correct** code for the `{ "ignoreNumericLiteralTypes": true }` opt type SmallPrimes = 2 | 3 | 5 | 7 | 11; ``` +### ignoreReadonlyClassProperties + +Examples of **incorrect** code for the `{ "ignoreReadonlyClassProperties": false }` option: + +```ts +/*eslint @typescript-eslint/no-magic-numbers: ["error", { "ignoreReadonlyClassProperties": false }]*/ + +class Foo { + readonly A = 1; + readonly B = 2; + public static readonly C = 1; + static readonly D = 1; +} +``` + +Examples of **correct** code for the `{ "ignoreReadonlyClassProperties": true }` option: + +```ts +/*eslint @typescript-eslint/no-magic-numbers: ["error", { "ignoreReadonlyClassProperties": true }]*/ + +class Foo { + readonly A = 1; + readonly B = 2; + public static readonly C = 1; + static readonly D = 1; +} +``` + ### ignoreEnums A boolean to specify if enums used in Typescript are considered okay. `false` by default. From 4f7be95af7efeccfac74a37ed48d5391a6f3a619 Mon Sep 17 00:00:00 2001 From: Alexander T Date: Tue, 3 Sep 2019 18:38:37 +0300 Subject: [PATCH 3/4] feat(eslint-plugin): [no-magic-numbers] remove typeof --- packages/eslint-plugin/src/rules/no-magic-numbers.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/eslint-plugin/src/rules/no-magic-numbers.ts b/packages/eslint-plugin/src/rules/no-magic-numbers.ts index 649ed6a715f..01a2505498f 100644 --- a/packages/eslint-plugin/src/rules/no-magic-numbers.ts +++ b/packages/eslint-plugin/src/rules/no-magic-numbers.ts @@ -161,7 +161,7 @@ export default util.createRule({ */ function isParentTSReadonlyClassProperty(node: TSESTree.Node): boolean { return ( - typeof node.parent !== 'undefined' && + !!node.parent && node.parent.type === AST_NODE_TYPES.ClassProperty && !!node.parent.readonly ); From 3bdadee1b20a7695418955eab3f7bd648e1ece83 Mon Sep 17 00:00:00 2001 From: Alexander T Date: Tue, 3 Sep 2019 18:40:50 +0300 Subject: [PATCH 4/4] feat(bug): [explicit-member-accessibility] fix formatting --- .../eslint-plugin/docs/rules/explicit-member-accessibility.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/eslint-plugin/docs/rules/explicit-member-accessibility.md b/packages/eslint-plugin/docs/rules/explicit-member-accessibility.md index 38be4f21a3c..96295809138 100644 --- a/packages/eslint-plugin/docs/rules/explicit-member-accessibility.md +++ b/packages/eslint-plugin/docs/rules/explicit-member-accessibility.md @@ -27,7 +27,7 @@ type Options = { properties?: AccessibilityLevel; parameterProperties?: AccessibilityLevel; }; -} +}; const defaultOptions: Options = { accessibility: 'explicit',