-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Closed
Labels
accepting prsGo ahead, send a pull request that resolves this issueGo ahead, send a pull request that resolves this issuebugSomething isn't workingSomething isn't workinglocked due to agePlease open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing.Please 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-pluginIssues related to @typescript-eslint/eslint-plugin
Description
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
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
Labels
accepting prsGo ahead, send a pull request that resolves this issueGo ahead, send a pull request that resolves this issuebugSomething isn't workingSomething isn't workinglocked due to agePlease open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing.Please 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-pluginIssues related to @typescript-eslint/eslint-plugin