Skip to content

Bug: no-magic-numbers Doesn't apply ignore to type definitions #9161

@anthony-unicare

Description

@anthony-unicare

Before You File a Bug Report Please Confirm You Have Done The Following...

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have searched for related issues and found none that matched my issue.
  • I have read the FAQ and my problem is not listed.

Playground Link

https://typescript-eslint.io/play/#ts=5.4.5&fileType=.tsx&code=KYDwDg9gTgLgBDAnmYcDyA7YaoBUDuEcAvHAIxwA%2BcATANxA&eslintrc=N4KABGBEAOA2CuBzAlgO0gLjAbXBKAAgC4Ce0ApgM4DGATstEQLRWxpGR4C6ANHpLXiwqmMKHyFSFGvUYtKbVEQD0qAPZMAtgEMU1JqniaARuVqVR2SAHdttdDzGRkidbXKWAjI4BMXAL5ceP4g-kA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA&tokens=false

Repro Code

export type OneOrTwo = 1 | 2;

ESLint Config

{
  "plugin": [
    "@typescript-eslint"
  ],
  "rules": {
    "@typescript-eslint/no-magic-numbers": ["warn", {"ignore": [1, 2]}]
  }
}

tsconfig

No response

Expected Result

I expected that because the numeric literals 1 and 2 are in the "ignore" setting of the "no-magic-numbers" rule, they would not be reported as magic numbers.

Actual Result

The numeric literals 1 and 2 are reported as magic numbers, despite the rule being configured to ignore them.

Additional Info

Here's a patch:

diff --git a/node_modules/@typescript-eslint/eslint-plugin/dist/rules/no-magic-numbers.js b/node_modules/@typescript-eslint/eslint-plugin/dist/rules/no-magic-numbers.js
index 07b7055..c5b6014 100644
--- a/node_modules/@typescript-eslint/eslint-plugin/dist/rules/no-magic-numbers.js
+++ b/node_modules/@typescript-eslint/eslint-plugin/dist/rules/no-magic-numbers.js
@@ -25,6 +25,17 @@ Array.isArray(baseRule.meta.schema)
         },
     },
 });
+/**
+ * Convert the value to bigint if it's a string. Otherwise return the value as-is.
+ * @param {bigint|number|string} x The value to normalize.
+ * @returns {bigint|number} The normalized value.
+ */
+function normalizeIgnoreValue(x) {
+    if (typeof x === "string") {
+        return BigInt(x.slice(0, -1));
+    }
+    return x;
+}
 exports.default = (0, util_1.createRule)({
     name: 'no-magic-numbers',
     meta: {
@@ -49,6 +60,7 @@ exports.default = (0, util_1.createRule)({
     ],
     create(context, [options]) {
         const rules = baseRule.create(context);
+        const ignore = new Set((options.ignore || []).map(normalizeIgnoreValue));
         return {
             Literal(node) {
                 // If it’s not a numeric literal we’re not interested
@@ -76,6 +88,10 @@ exports.default = (0, util_1.createRule)({
                 else if (isParentTSReadonlyPropertyDefinition(node)) {
                     isAllowed = options.ignoreReadonlyClassProperties === true;
                 }
+                // Check if the value is allowed
+                if (ignore.has(node.value)) {
+                    isAllowed = true;
+                }
                 // If we’ve hit a case where the ignore option is true we can return now
                 if (isAllowed === true) {
                     return;

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepting prsGo ahead, send a pull request that resolves this issuebugSomething isn't workinglocked due to agePlease open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing.package: eslint-pluginIssues related to @typescript-eslint/eslint-plugin

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions