Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
31 changes: 31 additions & 0 deletions src/parser/__tests__/disallowed-syntax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,37 @@ test('Cannot leave blank expressions in for loop - verbose', () => {
`)
})

test('Cannot use const declaration in for loop init', () => {
return expectParsedError(
stripIndent`
let b = 1;
for (const a = 4; b < 10; b = b + 1) {
break;
}
`,
{ chapter: Chapter.LIBRARY_PARSER }
).toMatchInlineSnapshot(
`"Line 2: Const declaration in init part of for statement is not allowed."`
)
})

test('Cannot use const declaration in for loop init - verbose', () => {
return expectParsedError(
stripIndent`
"enable verbose";
let b = 1;
for (const a = 4; b < 10; b = b + 1) {
break;
}
`,
{ chapter: Chapter.LIBRARY_PARSER }
).toMatchInlineSnapshot(`
"Line 3, Column 0: Const declaration in init part of for statement is not allowed.
The init part of this statement cannot contain a const declaration, use a let declaration instead.
"
`)
})

test('Cannot leave while loop predicate blank', () => {
return expectParsedError(
stripIndent`
Expand Down
2 changes: 2 additions & 0 deletions src/parser/source/rules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import bracesAroundFor from './bracesAroundFor'
import bracesAroundIfElse from './bracesAroundIfElse'
import bracesAroundWhile from './bracesAroundWhile'
import forStatementMustHaveAllParts from './forStatementMustHaveAllParts'
import { noConstDeclarationInForLoopInit } from './noConstDeclarationInForLoopInit'
import noDeclareMutable from './noDeclareMutable'
import noDotAbbreviation from './noDotAbbreviation'
import noEval from './noEval'
Expand All @@ -30,6 +31,7 @@ const rules: Rule<Node>[] = [
bracesAroundIfElse,
bracesAroundWhile,
forStatementMustHaveAllParts,
noConstDeclarationInForLoopInit,
noDeclareMutable,
noDotAbbreviation,
noExportNamedDeclarationWithDefault,
Expand Down
30 changes: 30 additions & 0 deletions src/parser/source/rules/noConstDeclarationInForLoopInit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import type { ForStatement } from 'estree'
import type { Rule } from '../../types'
import { stripIndent } from '../../../utils/formatters'
import { RuleError } from '../../errors'

export class NoConstDeclarationInForLoopInit extends RuleError<ForStatement> {
public explain(): string {
return 'Const declaration in init part of for statement is not allowed.'
}
public elaborate(): string {
return stripIndent`
The init part of this statement cannot contain a const declaration, use a let declaration instead.
`
}
}
const noConstDeclarationInForLoopInit: Rule<ForStatement> = {
name: 'no-const-declaration-in-for-loop-init',

checkers: {
ForStatement(node) {
if (node.init && node.init.type === 'VariableDeclaration' && node.init.kind === 'const') {
return [new NoConstDeclarationInForLoopInit(node)]
}

return []
}
}
}

export { noConstDeclarationInForLoopInit }
Loading