Skip to content

Commit 45ec35b

Browse files
authored
Check for const declaration in for statement init (#1821)
* Check for const declaration in for statement init * Remove useless constructor * Add test for new rule
1 parent 4105ea0 commit 45ec35b

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

src/parser/__tests__/disallowed-syntax.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,37 @@ test('Cannot leave blank expressions in for loop - verbose', () => {
112112
`)
113113
})
114114

115+
test('Cannot use const declaration in for loop init', () => {
116+
return expectParsedError(
117+
stripIndent`
118+
let b = 1;
119+
for (const a = 4; b < 10; b = b + 1) {
120+
break;
121+
}
122+
`,
123+
{ chapter: Chapter.LIBRARY_PARSER }
124+
).toMatchInlineSnapshot(
125+
`"Line 2: Const declaration in init part of for statement is not allowed."`
126+
)
127+
})
128+
129+
test('Cannot use const declaration in for loop init - verbose', () => {
130+
return expectParsedError(
131+
stripIndent`
132+
"enable verbose";
133+
let b = 1;
134+
for (const a = 4; b < 10; b = b + 1) {
135+
break;
136+
}
137+
`,
138+
{ chapter: Chapter.LIBRARY_PARSER }
139+
).toMatchInlineSnapshot(`
140+
"Line 3, Column 0: Const declaration in init part of for statement is not allowed.
141+
The init part of this statement cannot contain a const declaration, use a let declaration instead.
142+
"
143+
`)
144+
})
145+
115146
test('Cannot leave while loop predicate blank', () => {
116147
return expectParsedError(
117148
stripIndent`

src/parser/source/rules/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import bracesAroundFor from './bracesAroundFor'
44
import bracesAroundIfElse from './bracesAroundIfElse'
55
import bracesAroundWhile from './bracesAroundWhile'
66
import forStatementMustHaveAllParts from './forStatementMustHaveAllParts'
7+
import { noConstDeclarationInForLoopInit } from './noConstDeclarationInForLoopInit'
78
import noDeclareMutable from './noDeclareMutable'
89
import noDotAbbreviation from './noDotAbbreviation'
910
import noEval from './noEval'
@@ -30,6 +31,7 @@ const rules: Rule<Node>[] = [
3031
bracesAroundIfElse,
3132
bracesAroundWhile,
3233
forStatementMustHaveAllParts,
34+
noConstDeclarationInForLoopInit,
3335
noDeclareMutable,
3436
noDotAbbreviation,
3537
noExportNamedDeclarationWithDefault,
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import type { ForStatement } from 'estree'
2+
import type { Rule } from '../../types'
3+
import { stripIndent } from '../../../utils/formatters'
4+
import { RuleError } from '../../errors'
5+
6+
export class NoConstDeclarationInForLoopInit extends RuleError<ForStatement> {
7+
public explain(): string {
8+
return 'Const declaration in init part of for statement is not allowed.'
9+
}
10+
public elaborate(): string {
11+
return stripIndent`
12+
The init part of this statement cannot contain a const declaration, use a let declaration instead.
13+
`
14+
}
15+
}
16+
const noConstDeclarationInForLoopInit: Rule<ForStatement> = {
17+
name: 'no-const-declaration-in-for-loop-init',
18+
19+
checkers: {
20+
ForStatement(node) {
21+
if (node.init && node.init.type === 'VariableDeclaration' && node.init.kind === 'const') {
22+
return [new NoConstDeclarationInForLoopInit(node)]
23+
}
24+
25+
return []
26+
}
27+
}
28+
}
29+
30+
export { noConstDeclarationInForLoopInit }

0 commit comments

Comments
 (0)