Skip to content

Commit f98dcea

Browse files
committedMar 7, 2025·
Revert "Add tests for parser rules"
This reverts commit ed8eb2c.
1 parent ed8eb2c commit f98dcea

31 files changed

+503
-636
lines changed
 

‎src/parser/source/index.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import { parse as acornParse, Token, tokenizer } from 'acorn'
2-
import type * as es from 'estree'
2+
import * as es from 'estree'
33

44
import { DEFAULT_ECMA_VERSION } from '../../constants'
5-
import { Chapter, Context, Node, SourceError, Variant } from '../../types'
5+
import { Chapter, Context, Node, Rule, SourceError, Variant } from '../../types'
66
import { ancestor, AncestorWalkerFn } from '../../utils/walkers'
77
import { DisallowedConstructError, FatalSyntaxError } from '../errors'
8-
import type { AcornOptions, Rule, Parser } from '../types'
8+
import { AcornOptions, Parser } from '../types'
99
import { createAcornParserOptions, positionToSourceLocation } from '../utils'
10-
import { mapToObj } from '../../utils/misc'
1110
import defaultRules from './rules'
1211
import syntaxBlacklist from './syntax'
1312

@@ -18,6 +17,9 @@ const combineAncestorWalkers =
1817
w2(node, state, ancestors)
1918
}
2019

20+
const mapToObj = <T>(map: Map<string, T>) =>
21+
Array.from(map).reduce((obj, [k, v]) => Object.assign(obj, { [k]: v }), {})
22+
2123
export class SourceParser implements Parser<AcornOptions> {
2224
private chapter: Chapter
2325
private variant: Variant

‎src/parser/source/rules/__tests__/rules.ts

-97
This file was deleted.

‎src/parser/source/rules/bracesAroundFor.ts

+17-16
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
import { generate } from 'astring'
2-
import type { ForStatement } from 'estree'
3-
import { type Rule, RuleError } from '../../types'
2+
import * as es from 'estree'
43

5-
const errorMsg = 'Missing curly braces around "for" block.'
4+
import { UNKNOWN_LOCATION } from '../../../constants'
5+
import { ErrorSeverity, ErrorType, Node, Rule, SourceError } from '../../../types'
6+
7+
export class BracesAroundForError implements SourceError {
8+
public type = ErrorType.SYNTAX
9+
public severity = ErrorSeverity.ERROR
10+
11+
constructor(public node: es.ForStatement) {}
12+
13+
get location() {
14+
return this.node.loc ?? UNKNOWN_LOCATION
15+
}
616

7-
export class BracesAroundForError extends RuleError<ForStatement> {
817
public explain() {
9-
return errorMsg
18+
return 'Missing curly braces around "for" block.'
1019
}
1120

1221
public elaborate() {
@@ -20,19 +29,11 @@ export class BracesAroundForError extends RuleError<ForStatement> {
2029
}
2130
}
2231

23-
const bracesAroundFor: Rule<ForStatement> = {
32+
const bracesAroundFor: Rule<es.ForStatement> = {
2433
name: 'braces-around-for',
25-
testSnippets: [
26-
[
27-
`
28-
let j = 0;
29-
for (let i = 0; i < 1; i = i + 1) j = j + 1;
30-
`,
31-
errorMsg
32-
]
33-
],
34+
3435
checkers: {
35-
ForStatement(node: ForStatement) {
36+
ForStatement(node: es.ForStatement, _ancestors: [Node]) {
3637
if (node.body.type !== 'BlockStatement') {
3738
return [new BracesAroundForError(node)]
3839
} else {

‎src/parser/source/rules/bracesAroundIfElse.ts

+15-19
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
import { generate } from 'astring'
2-
import type { IfStatement } from 'estree'
3-
import type { SourceError } from '../../../types'
4-
import { type Rule, RuleError } from '../../types'
2+
import * as es from 'estree'
3+
4+
import { UNKNOWN_LOCATION } from '../../../constants'
5+
import { ErrorSeverity, ErrorType, Node, Rule, SourceError } from '../../../types'
56
import { stripIndent } from '../../../utils/formatters'
67

7-
export class BracesAroundIfElseError extends RuleError<IfStatement> {
8-
constructor(public node: IfStatement, private branch: 'consequent' | 'alternate') {
9-
super(node)
8+
export class BracesAroundIfElseError implements SourceError {
9+
public type = ErrorType.SYNTAX
10+
public severity = ErrorSeverity.ERROR
11+
12+
constructor(public node: es.IfStatement, private branch: 'consequent' | 'alternate') {}
13+
14+
get location() {
15+
return this.node.loc ?? UNKNOWN_LOCATION
1016
}
1117

1218
public explain() {
@@ -63,21 +69,11 @@ export class BracesAroundIfElseError extends RuleError<IfStatement> {
6369
}
6470
}
6571

66-
const bracesAroundIfElse: Rule<IfStatement> = {
72+
const bracesAroundIfElse: Rule<es.IfStatement> = {
6773
name: 'braces-around-if-else',
68-
testSnippets: [
69-
[
70-
`
71-
function f() {
72-
if (true) return false;
73-
else return true;
74-
}
75-
`,
76-
'Line 3: Missing curly braces around "if" block.'
77-
]
78-
],
74+
7975
checkers: {
80-
IfStatement(node: IfStatement) {
76+
IfStatement(node: es.IfStatement, _ancestors: [Node]) {
8177
const errors: SourceError[] = []
8278
if (node.consequent && node.consequent.type !== 'BlockStatement') {
8379
errors.push(new BracesAroundIfElseError(node, 'consequent'))

‎src/parser/source/rules/bracesAroundWhile.ts

+17-14
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
import { generate } from 'astring'
2-
import type { WhileStatement } from 'estree'
3-
import { type Rule, RuleError } from '../../types'
2+
import * as es from 'estree'
3+
4+
import { UNKNOWN_LOCATION } from '../../../constants'
5+
import { ErrorSeverity, ErrorType, Node, Rule, SourceError } from '../../../types'
6+
7+
export class BracesAroundWhileError implements SourceError {
8+
public type = ErrorType.SYNTAX
9+
public severity = ErrorSeverity.ERROR
10+
11+
constructor(public node: es.WhileStatement) {}
12+
13+
get location() {
14+
return this.node.loc ?? UNKNOWN_LOCATION
15+
}
416

5-
export class BracesAroundWhileError extends RuleError<WhileStatement> {
617
public explain() {
718
return 'Missing curly braces around "while" block.'
819
}
@@ -15,19 +26,11 @@ export class BracesAroundWhileError extends RuleError<WhileStatement> {
1526
}
1627
}
1728

18-
const bracesAroundWhile: Rule<WhileStatement> = {
29+
const bracesAroundWhile: Rule<es.WhileStatement> = {
1930
name: 'braces-around-while',
20-
testSnippets: [
21-
[
22-
`
23-
let i = 0;
24-
while (true) i = i + 1;
25-
`,
26-
'Line 3: Missing curly braces around "while" block.'
27-
]
28-
],
31+
2932
checkers: {
30-
WhileStatement(node: WhileStatement) {
33+
WhileStatement(node: es.WhileStatement, _ancestors: [Node]) {
3134
if (node.body.type !== 'BlockStatement') {
3235
return [new BracesAroundWhileError(node)]
3336
} else {

‎src/parser/source/rules/forStatementMustHaveAllParts.ts

+15-12
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1-
import type { ForStatement } from 'estree'
2-
import { type Rule, RuleError } from '../../types'
1+
import * as es from 'estree'
2+
3+
import { UNKNOWN_LOCATION } from '../../../constants'
4+
import { ErrorSeverity, ErrorType, Rule, SourceError } from '../../../types'
35
import { stripIndent } from '../../../utils/formatters'
46

5-
export class ForStatmentMustHaveAllParts extends RuleError<ForStatement> {
6-
constructor(public node: ForStatement, private missingParts: string[]) {
7-
super(node)
7+
export class ForStatmentMustHaveAllParts implements SourceError {
8+
public type = ErrorType.SYNTAX
9+
public severity = ErrorSeverity.ERROR
10+
11+
constructor(public node: es.ForStatement, private missingParts: string[]) {}
12+
13+
get location() {
14+
return this.node.loc ?? UNKNOWN_LOCATION
815
}
916

1017
public explain() {
@@ -20,15 +27,11 @@ export class ForStatmentMustHaveAllParts extends RuleError<ForStatement> {
2027
}
2128
}
2229

23-
const forStatementMustHaveAllParts: Rule<ForStatement> = {
30+
const forStatementMustHaveAllParts: Rule<es.ForStatement> = {
2431
name: 'for-statement-must-have-all-parts',
25-
testSnippets: [
26-
['let i = 0; for (; i < 0; i = i + 1) {}', 'Line 1: Missing init expression in for statement.'],
27-
['for (let i = 0; ; i = i + 1) {}', 'Line 1: Missing test expression in for statement.'],
28-
['for (let i = 0; i < 0;) {}', 'Line 1: Missing update expression in for statement']
29-
],
32+
3033
checkers: {
31-
ForStatement(node: ForStatement) {
34+
ForStatement(node: es.ForStatement) {
3235
const missingParts = ['init', 'test', 'update'].filter(part => node[part] === null)
3336
if (missingParts.length > 0) {
3437
return [new ForStatmentMustHaveAllParts(node, missingParts)]

‎src/parser/source/rules/index.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import type { Node } from '../../../types'
2-
import type { Rule } from '../../types'
1+
import { Node, Rule } from '../../../types'
32
import bracesAroundFor from './bracesAroundFor'
43
import bracesAroundIfElse from './bracesAroundIfElse'
54
import bracesAroundWhile from './bracesAroundWhile'
@@ -11,6 +10,7 @@ import noExportNamedDeclarationWithDefault from './noExportNamedDeclarationWithD
1110
import noFunctionDeclarationWithoutIdentifier from './noFunctionDeclarationWithoutIdentifier'
1211
import noHolesInArrays from './noHolesInArrays'
1312
import noIfWithoutElse from './noIfWithoutElse'
13+
import noImplicitDeclareUndefined from './noImplicitDeclareUndefined'
1414
import noImplicitReturnUndefined from './noImplicitReturnUndefined'
1515
import noImportSpecifierWithDefault from './noImportSpecifierWithDefault'
1616
import noNull from './noNull'
@@ -34,6 +34,7 @@ const rules: Rule<Node>[] = [
3434
noFunctionDeclarationWithoutIdentifier,
3535
noIfWithoutElse,
3636
noImportSpecifierWithDefault,
37+
noImplicitDeclareUndefined,
3738
noImplicitReturnUndefined,
3839
noNull,
3940
noUnspecifiedLiteral,

‎src/parser/source/rules/noDeclareMutable.ts

+18-11
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,41 @@
11
import { generate } from 'astring'
2-
import type { VariableDeclaration } from 'estree'
3-
import { Chapter } from '../../../types'
4-
import { type Rule, RuleError } from '../../types'
5-
import { getVariableDeclarationName } from '../../../utils/ast/astCreator'
2+
import * as es from 'estree'
3+
4+
import { UNKNOWN_LOCATION } from '../../../constants'
5+
import { Chapter, ErrorSeverity, ErrorType, Node, Rule, SourceError } from '../../../types'
66

77
const mutableDeclarators = ['let', 'var']
88

9-
export class NoDeclareMutableError extends RuleError<VariableDeclaration> {
9+
export class NoDeclareMutableError implements SourceError {
10+
public type = ErrorType.SYNTAX
11+
public severity = ErrorSeverity.ERROR
12+
13+
constructor(public node: es.VariableDeclaration) {}
14+
15+
get location() {
16+
return this.node.loc ?? UNKNOWN_LOCATION
17+
}
18+
1019
public explain() {
1120
return (
1221
'Mutable variable declaration using keyword ' + `'${this.node.kind}'` + ' is not allowed.'
1322
)
1423
}
1524

1625
public elaborate() {
17-
const name = getVariableDeclarationName(this.node)
26+
const name = (this.node.declarations[0].id as es.Identifier).name
1827
const value = generate(this.node.declarations[0].init)
1928

2029
return `Use keyword "const" instead, to declare a constant:\n\n\tconst ${name} = ${value};`
2130
}
2231
}
2332

24-
const noDeclareMutable: Rule<VariableDeclaration> = {
33+
const noDeclareMutable: Rule<es.VariableDeclaration> = {
2534
name: 'no-declare-mutable',
2635
disableFromChapter: Chapter.SOURCE_3,
27-
testSnippets: [
28-
['let i = 0;', "Line 1: Mutable variable declaration using keyword 'let' is not allowed."]
29-
],
36+
3037
checkers: {
31-
VariableDeclaration(node: VariableDeclaration) {
38+
VariableDeclaration(node: es.VariableDeclaration, _ancestors: [Node]) {
3239
if (mutableDeclarators.includes(node.kind)) {
3340
return [new NoDeclareMutableError(node)]
3441
} else {

0 commit comments

Comments
 (0)
Please sign in to comment.