useConst
Ce contenu n’est pas encore disponible dans votre langue.
Diagnostic Category: lint/style/useConst
Since: v1.0.0
Sources:
- Same as:
prefer-const
Require const
declarations for variables that are only assigned once.
Variables that are initialized and never reassigned and
variables that are only assigned once can be declared as const
.
Examples
Section titled ExamplesInvalid
Section titled Invalidcode-block.js:1:1 lint/style/useConst FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ This let declares a variable that is only assigned once.
> 1 │ let a = 3;
│ ^^^
2 │ console.log(a);
3 │
ℹ ‘a’ is never reassigned.
> 1 │ let a = 3;
│ ^
2 │ console.log(a);
3 │
ℹ Safe fix: Use const instead.
1 │ - let·a·=·3;
1 │ + const·a·=·3;
2 2 │ console.log(a);
3 3 │
code-block.js:2:6 lint/style/useConst FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ This let declares a variable that is only assigned once.
1 │ // a
is redefined (not reassigned) on each loop step.
> 2 │ for (let a of [1, 2, 3]) {
│ ^^^
3 │ console.log(a);
4 │ }
ℹ ‘a’ is never reassigned.
1 │ // a
is redefined (not reassigned) on each loop step.
> 2 │ for (let a of [1, 2, 3]) {
│ ^
3 │ console.log(a);
4 │ }
ℹ Safe fix: Use const instead.
1 1 │ // a
is redefined (not reassigned) on each loop step.
2 │ - for·(let·a·of·[1,·2,·3])·{
2 │ + for·(const·a·of·[1,·2,·3])·{
3 3 │ console.log(a);
4 4 │ }
code-block.js:2:6 lint/style/useConst FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ This let declares a variable that is only assigned once.
1 │ // a
is redefined (not reassigned) on each loop step.
> 2 │ for (let a in [1, 2, 3]) {
│ ^^^
3 │ console.log(a);
4 │ }
ℹ ‘a’ is never reassigned.
1 │ // a
is redefined (not reassigned) on each loop step.
> 2 │ for (let a in [1, 2, 3]) {
│ ^
3 │ console.log(a);
4 │ }
ℹ Safe fix: Use const instead.
1 1 │ // a
is redefined (not reassigned) on each loop step.
2 │ - for·(let·a·in·[1,·2,·3])·{
2 │ + for·(const·a·in·[1,·2,·3])·{
3 3 │ console.log(a);
4 4 │ }
code-block.js:1:1 lint/style/useConst ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ This let declares a variable that is only assigned once.
> 1 │ let a;
│ ^^^
2 │ a = 0;
3 │
ℹ ‘a’ is only assigned here.
1 │ let a;
> 2 │ a = 0;
│ ^
3 │
code-block.js:1:1 lint/style/useConst FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ This let declares a variable that is only assigned once.
> 1 │ let a = 3;
│ ^^^
2 │ {
3 │ let a = 4;
ℹ ‘a’ is never reassigned.
> 1 │ let a = 3;
│ ^
2 │ {
3 │ let a = 4;
ℹ Safe fix: Use const instead.
1 │ - let·a·=·3;
1 │ + const·a·=·3;
2 2 │ {
3 3 │ let a = 4;