Skip to content

Commit aeea4cd

Browse files
a-tarasyukJamesHenry
authored andcommitted
feat(eslint-plugin): [no-magic-numbers] add ignoreReadonlyClassProperties option (#938)
1 parent 46ee4c9 commit aeea4cd

File tree

4 files changed

+98
-0
lines changed

4 files changed

+98
-0
lines changed

packages/eslint-plugin/docs/rules/no-magic-numbers.md

+28
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,34 @@ Examples of **correct** code for the `{ "ignoreNumericLiteralTypes": true }` opt
4141
type SmallPrimes = 2 | 3 | 5 | 7 | 11;
4242
```
4343

44+
### ignoreReadonlyClassProperties
45+
46+
Examples of **incorrect** code for the `{ "ignoreReadonlyClassProperties": false }` option:
47+
48+
```ts
49+
/*eslint @typescript-eslint/no-magic-numbers: ["error", { "ignoreReadonlyClassProperties": false }]*/
50+
51+
class Foo {
52+
readonly A = 1;
53+
readonly B = 2;
54+
public static readonly C = 1;
55+
static readonly D = 1;
56+
}
57+
```
58+
59+
Examples of **correct** code for the `{ "ignoreReadonlyClassProperties": true }` option:
60+
61+
```ts
62+
/*eslint @typescript-eslint/no-magic-numbers: ["error", { "ignoreReadonlyClassProperties": true }]*/
63+
64+
class Foo {
65+
readonly A = 1;
66+
readonly B = 2;
67+
public static readonly C = 1;
68+
static readonly D = 1;
69+
}
70+
```
71+
4472
### ignoreEnums
4573

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

packages/eslint-plugin/src/rules/no-magic-numbers.ts

+25
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ export default util.createRule<Options, MessageIds>({
3333
ignoreEnums: {
3434
type: 'boolean',
3535
},
36+
ignoreReadonlyClassProperties: {
37+
type: 'boolean',
38+
},
3639
},
3740
},
3841
],
@@ -46,6 +49,7 @@ export default util.createRule<Options, MessageIds>({
4649
detectObjects: false,
4750
ignoreNumericLiteralTypes: false,
4851
ignoreEnums: false,
52+
ignoreReadonlyClassProperties: false,
4953
},
5054
],
5155
create(context, [options]) {
@@ -149,13 +153,34 @@ export default util.createRule<Options, MessageIds>({
149153
return false;
150154
}
151155

156+
/**
157+
* Checks if the node parent is a readonly class property
158+
* @param node the node to be validated.
159+
* @returns true if the node parent is a readonly class property
160+
* @private
161+
*/
162+
function isParentTSReadonlyClassProperty(node: TSESTree.Node): boolean {
163+
return (
164+
!!node.parent &&
165+
node.parent.type === AST_NODE_TYPES.ClassProperty &&
166+
!!node.parent.readonly
167+
);
168+
}
169+
152170
return {
153171
Literal(node): void {
154172
// Check if the node is a TypeScript enum declaration
155173
if (options.ignoreEnums && isParentTSEnumDeclaration(node)) {
156174
return;
157175
}
158176

177+
if (
178+
options.ignoreReadonlyClassProperties &&
179+
isParentTSReadonlyClassProperty(node)
180+
) {
181+
return;
182+
}
183+
159184
// Check TypeScript specific nodes for Numeric Literal
160185
if (
161186
options.ignoreNumericLiteralTypes &&

packages/eslint-plugin/tests/rules/no-magic-numbers.test.ts

+44
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,17 @@ ruleTester.run('no-magic-numbers', rule, {
4141
code: 'enum foo { SECOND = 1000, NUM = "0123456789" }',
4242
options: [{ ignoreEnums: true }],
4343
},
44+
{
45+
code: `
46+
class Foo {
47+
readonly A = 1;
48+
readonly B = 2;
49+
public static readonly C = 1;
50+
static readonly D = 1;
51+
}
52+
`,
53+
options: [{ ignoreReadonlyClassProperties: true }],
54+
},
4455
],
4556

4657
invalid: [
@@ -166,5 +177,38 @@ ruleTester.run('no-magic-numbers', rule, {
166177
},
167178
],
168179
},
180+
{
181+
code: `
182+
class Foo {
183+
readonly A = 1;
184+
readonly B = 2;
185+
public static readonly C = 1;
186+
static readonly D = 1;
187+
}
188+
`,
189+
options: [{ ignoreReadonlyClassProperties: false }],
190+
errors: [
191+
{
192+
messageId: 'noMagic',
193+
line: 3,
194+
column: 16,
195+
},
196+
{
197+
messageId: 'noMagic',
198+
line: 4,
199+
column: 16,
200+
},
201+
{
202+
messageId: 'noMagic',
203+
line: 5,
204+
column: 30,
205+
},
206+
{
207+
messageId: 'noMagic',
208+
line: 6,
209+
column: 23,
210+
},
211+
],
212+
},
169213
],
170214
});

packages/eslint-plugin/typings/eslint-rules.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ declare module 'eslint/lib/rules/no-magic-numbers' {
199199
detectObjects?: boolean;
200200
ignoreNumericLiteralTypes?: boolean;
201201
ignoreEnums?: boolean;
202+
ignoreReadonlyClassProperties?: boolean;
202203
},
203204
],
204205
{

0 commit comments

Comments
 (0)