fix(linter/eslint/no-magic-numbers): restrict unary exemptions to numeric signs (#27723)

This commit is contained in:
Yunus Ozen authored and GitHub committed 2026-10-15 13:06:05 +00:00
1 parent ae7e44be49
commit 0e40acb4a5
2 files changed
+39 -1

No files matched your search

@@ -258,7 +258,11 @@ struct InternConfig<'a> {
impl InternConfig<'_> {
pub fn from<'a>(node: &'a AstNode<'a>, parent_node: &'a AstNode<'a>) -> InternConfig<'a> {
let is_unary = matches!(parent_node.kind(), AstKind::UnaryExpression(_));
let is_unary = matches!(
parent_node.kind(),
AstKind::UnaryExpression(unary)
if matches!(unary.operator, UnaryOperator::UnaryNegation | UnaryOperator::UnaryPlus)
);
let is_negative = matches!(parent_node.kind(), AstKind::UnaryExpression(unary) if unary.operator == UnaryOperator::UnaryNegation);
match node.kind() {
@@ -734,6 +738,8 @@ fn test() {
",
Some(serde_json::json!([{ "ignoreTypeIndexes": true, "ignore": [0, 3] }])),
),
("exports.c = -7;", None),
("exports.d = +9;", None),
];
let fail = vec![
@@ -951,6 +957,10 @@ fn test() {
"type Foo = { bar: Bar[((1 & -2) | 3) | 4] };",
Some(serde_json::json!([{ "ignoreNumericLiteralTypes": true }])),
),
("exports.a = void 0;", None),
("exports.b = ~5;", None),
("exports.c = !3;", None),
("var x = typeof 4;", None),
];
Tester::new(NoMagicNumbers::NAME, NoMagicNumbers::PLUGIN, pass, fail).test_and_snapshot();
@@ -1017,3 +1017,31 @@ source: crates/oxc_linter/src/tester.rs
· ─
╰────
help: Use a named constant instead of a magic number to make the code more readable and maintainable.
⚠ eslint(no-magic-numbers): No magic number: 0
╭─[no_magic_numbers.tsx:1:18]
1 │ exports.a = void 0;
· ─
╰────
help: Use a named constant instead of a magic number to make the code more readable and maintainable.
⚠ eslint(no-magic-numbers): No magic number: 5
╭─[no_magic_numbers.tsx:1:14]
1 │ exports.b = ~5;
· ─
╰────
help: Use a named constant instead of a magic number to make the code more readable and maintainable.
⚠ eslint(no-magic-numbers): No magic number: 3
╭─[no_magic_numbers.tsx:1:14]
1 │ exports.c = !3;
· ─
╰────
help: Use a named constant instead of a magic number to make the code more readable and maintainable.
⚠ eslint(no-magic-numbers): No magic number: 4
╭─[no_magic_numbers.tsx:1:16]
1 │ var x = typeof 4;
· ─
╰────
help: Use a named constant instead of a magic number to make the code more readable and maintainable.