@addon24/eslint-config
Version:
ESLint configuration rules for WorldOfTextcraft projects - Centralized configuration for all project types
86 lines (79 loc) • 2.58 kB
JavaScript
export default {
rules: {
"no-magic-values": {
meta: {
type: "problem",
docs: {
description: "Disallow magic numbers and magic strings",
category: "Best Practices",
recommended: true,
},
messages: {
magicNumber: "Magic Number '{{value}}' found. Use a named constant instead.",
magicString: "Magic String '{{value}}' found. Use a named constant instead.",
},
schema: [
{
type: "object",
properties: {
ignoreNumbers: {
type: "array",
items: { type: "number" },
description: "Numbers to ignore (e.g., 0, 1, -1)",
default: [0, 1, -1]
},
ignoreStrings: {
type: "array",
items: { type: "string" },
description: "Strings to ignore (e.g., empty string)",
default: [""]
}
},
additionalProperties: false
}
]
},
create(context) {
const options = context.options[0] || {};
const ignoreNumbers = options.ignoreNumbers || [0, 1, -1];
const ignoreStrings = options.ignoreStrings || [""];
function checkLiteral(node) {
if (node.type === "Literal") {
// Skip if it's inside a variable declaration (constant definition)
if (node.parent && node.parent.type === "VariableDeclarator") {
return;
}
if (typeof node.value === "number") {
// Ignore configured numbers
if (ignoreNumbers.includes(node.value)) {
return;
}
context.report({
node,
messageId: "magicNumber",
data: {
value: node.value,
},
});
} else if (typeof node.value === "string") {
// Ignore configured strings
if (ignoreStrings.includes(node.value)) {
return;
}
context.report({
node,
messageId: "magicString",
data: {
value: node.value,
},
});
}
}
}
return {
Literal: checkLiteral,
};
},
},
},
};