Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

fix(semantic_analyzers): style/noShoutyConstants does not recognize multiple uses of a constant. #3789

Merged
merged 20 commits into from
Nov 24, 2022
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use rome_diagnostics::Applicability;
use rome_js_semantic::{AllReferencesExtensions, Reference};
use rome_js_syntax::{
JsAnyExpression, JsAnyLiteralExpression, JsIdentifierBinding, JsIdentifierExpression,
JsStringLiteralExpression, JsVariableDeclaration, JsVariableDeclarator,
JsReferenceIdentifier, JsStringLiteralExpression, JsVariableDeclaration, JsVariableDeclarator,
JsVariableDeclaratorList,
};
use rome_rowan::{AstNode, BatchMutationExt, SyntaxNodeCast};
Expand Down Expand Up @@ -101,6 +101,9 @@ impl Rule for NoShoutyConstants {
return None;
mzbac marked this conversation as resolved.
Show resolved Hide resolved
}

if binding.all_references(ctx.model()).count() > 1 {
mzbac marked this conversation as resolved.
Show resolved Hide resolved
return None;
}
return Some(State {
literal,
references: binding.all_references(ctx.model()).collect(),
Expand Down
11 changes: 9 additions & 2 deletions crates/rome_js_analyze/tests/specs/style/noShoutyConstants.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ const FOO2 = "FOO2", a = "FOO3", FOO4 = "FOO4";
console.log(FOO, FOO4);

let foo = "foo";
const B = "B";
export default B;
export const A = "A";

const A = "A";
export default A;
const BAR = "BAR";

export const bar = {
foo: BAR,
bar: BAR,
};
60 changes: 10 additions & 50 deletions crates/rome_js_analyze/tests/specs/style/noShoutyConstants.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
source: crates/rome_js_analyze/tests/spec_tests.rs
assertion_line: 73
expression: noShoutyConstants.js
---
# Input
Expand All @@ -12,60 +13,19 @@ const FOO2 = "FOO2", a = "FOO3", FOO4 = "FOO4";
console.log(FOO, FOO4);

let foo = "foo";
const B = "B";
export default B;
export const A = "A";

const A = "A";
export default A;
```

# Diagnostics
```
noShoutyConstants.js:1:7 lint/style/noShoutyConstants FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! Redundant constant declaration.

> 1 │ const FOO = "FOO";
│ ^^^^^^^^^^^
2 │ console.log(FOO, FOO2);
3 │

i Used here.

1 │ const FOO = "FOO";
> 2 │ console.log(FOO, FOO2);
│ ^^^
3 │
4 │ const FOO2 = "FOO2", a = "FOO3", FOO4 = "FOO4";

i Used here.

4 │ const FOO2 = "FOO2", a = "FOO3", FOO4 = "FOO4";
5 │
> 6 │ console.log(FOO, FOO4);
│ ^^^
7 │
8 │ let foo = "foo";

i You should avoid declaring constants with a string that's the same
value as the variable name. It introduces a level of unnecessary
indirection when it's only two additional characters to inline.

i Suggested fix: Use the constant value directly

1 │ - const·FOO·=·"FOO";
2 │ - console.log(FOO,·FOO2);
1 │ +
2 │ + console.log("FOO",·FOO2);
3 3 │
4 4 │ const FOO2 = "FOO2", a = "FOO3", FOO4 = "FOO4";
5 5 │
6 │ - console.log(FOO,·FOO4);
6 │ + console.log("FOO",·FOO4);
7 7 │
8 8 │ let foo = "foo";

const BAR = "BAR";

export const bar = {
foo: BAR,
bar: BAR,
};
```

# Diagnostics
```
noShoutyConstants.js:4:7 lint/style/noShoutyConstants FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Expand Down