Skip to content

Commit 96fda16

Browse files
committedMar 14, 2025
Move rule type into parser
1 parent 7f2d14d commit 96fda16

29 files changed

+104
-41
lines changed
 

‎src/parser/source/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { parse as acornParse, Token, tokenizer } from 'acorn'
22
import * as es from 'estree'
33

44
import { DEFAULT_ECMA_VERSION } from '../../constants'
5-
import { Chapter, Context, Node, Rule, SourceError, Variant } from '../../types'
5+
import { Chapter, Context, Node, SourceError, Variant } from '../../types'
6+
import { Rule } from '../types'
67
import { ancestor, AncestorWalkerFn } from '../../utils/walkers'
78
import { DisallowedConstructError, FatalSyntaxError } from '../errors'
89
import { AcornOptions, Parser } from '../types'

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { generate } from 'astring'
22
import * as es from 'estree'
33

44
import { UNKNOWN_LOCATION } from '../../../constants'
5-
import { ErrorSeverity, ErrorType, Node, Rule, SourceError } from '../../../types'
5+
import { ErrorSeverity, ErrorType, Node, SourceError } from '../../../types'
6+
import { Rule } from '../../types'
67

78
export class BracesAroundForError implements SourceError {
89
public type = ErrorType.SYNTAX

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { generate } from 'astring'
22
import * as es from 'estree'
33

44
import { UNKNOWN_LOCATION } from '../../../constants'
5-
import { ErrorSeverity, ErrorType, Node, Rule, SourceError } from '../../../types'
5+
import { ErrorSeverity, ErrorType, Node, SourceError } from '../../../types'
6+
import { Rule } from '../../types'
67
import { stripIndent } from '../../../utils/formatters'
78

89
export class BracesAroundIfElseError implements SourceError {

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { generate } from 'astring'
22
import * as es from 'estree'
33

44
import { UNKNOWN_LOCATION } from '../../../constants'
5-
import { ErrorSeverity, ErrorType, Node, Rule, SourceError } from '../../../types'
5+
import { ErrorSeverity, ErrorType, Node, SourceError } from '../../../types'
6+
import { Rule } from '../../types'
67

78
export class BracesAroundWhileError implements SourceError {
89
public type = ErrorType.SYNTAX

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import * as es from 'estree'
22

33
import { UNKNOWN_LOCATION } from '../../../constants'
4-
import { ErrorSeverity, ErrorType, Rule, SourceError } from '../../../types'
4+
import { ErrorSeverity, ErrorType, SourceError } from '../../../types'
5+
import { Rule } from '../../types'
56
import { stripIndent } from '../../../utils/formatters'
67

78
export class ForStatmentMustHaveAllParts implements SourceError {

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { Node, Rule } from '../../../types'
1+
import { Node } from '../../../types'
2+
import { Rule } from '../../types'
23
import bracesAroundFor from './bracesAroundFor'
34
import bracesAroundIfElse from './bracesAroundIfElse'
45
import bracesAroundWhile from './bracesAroundWhile'
@@ -7,6 +8,7 @@ import noDeclareMutable from './noDeclareMutable'
78
import noDotAbbreviation from './noDotAbbreviation'
89
import noEval from './noEval'
910
import noExportNamedDeclarationWithDefault from './noExportNamedDeclarationWithDefault'
11+
import noExportNamedDeclarationWithSource from './noExportNamedDeclarationWithSource'
1012
import noFunctionDeclarationWithoutIdentifier from './noFunctionDeclarationWithoutIdentifier'
1113
import noHolesInArrays from './noHolesInArrays'
1214
import noIfWithoutElse from './noIfWithoutElse'
@@ -31,6 +33,7 @@ const rules: Rule<Node>[] = [
3133
noDeclareMutable,
3234
noDotAbbreviation,
3335
noExportNamedDeclarationWithDefault,
36+
noExportNamedDeclarationWithSource,
3437
noFunctionDeclarationWithoutIdentifier,
3538
noIfWithoutElse,
3639
noImportSpecifierWithDefault,

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { generate } from 'astring'
22
import * as es from 'estree'
33

44
import { UNKNOWN_LOCATION } from '../../../constants'
5-
import { Chapter, ErrorSeverity, ErrorType, Node, Rule, SourceError } from '../../../types'
5+
import { Chapter, ErrorSeverity, ErrorType, Node, SourceError } from '../../../types'
6+
import { Rule } from '../../types'
67

78
const mutableDeclarators = ['let', 'var']
89

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import * as es from 'estree'
22

33
import { UNKNOWN_LOCATION } from '../../../constants'
4-
import { Chapter, ErrorSeverity, ErrorType, Node, Rule, SourceError } from '../../../types'
4+
import { Chapter, ErrorSeverity, ErrorType, Node, SourceError } from '../../../types'
5+
import { Rule } from '../../types'
56

67
export class NoDotAbbreviationError implements SourceError {
78
public type = ErrorType.SYNTAX

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import * as es from 'estree'
22

33
import { UNKNOWN_LOCATION } from '../../../constants'
4-
import { ErrorSeverity, ErrorType, Node, Rule, SourceError } from '../../../types'
4+
import { ErrorSeverity, ErrorType, Node, SourceError } from '../../../types'
5+
import { Rule } from '../../types'
56

67
export class NoEval implements SourceError {
78
public type = ErrorType.SYNTAX

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import * as es from 'estree'
22

33
import { UNKNOWN_LOCATION } from '../../../constants'
44
import { defaultExportLookupName } from '../../../stdlib/localImport.prelude'
5-
import { ErrorSeverity, ErrorType, Node, Rule, SourceError } from '../../../types'
5+
import { ErrorSeverity, ErrorType, Node, SourceError } from '../../../types'
6+
import { Rule } from '../../types'
67
import syntaxBlacklist from '../syntax'
78

89
export class NoExportNamedDeclarationWithDefaultError implements SourceError {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import type { ExportNamedDeclaration } from "estree";
2+
import { UNKNOWN_LOCATION } from "../../../constants";
3+
import { ErrorSeverity, ErrorType, type SourceError } from "../../../types";
4+
import { type Rule } from '../../types';
5+
6+
export class NoExportNamedDeclarationWithSourceError implements SourceError {
7+
public type = ErrorType.SYNTAX
8+
public severity = ErrorSeverity.ERROR
9+
10+
constructor(public node: ExportNamedDeclaration) {}
11+
12+
get location() {
13+
return this.node.loc ?? UNKNOWN_LOCATION
14+
}
15+
16+
public explain() {
17+
return 'exports of the form export { a } from "./file.js"; are not allowed.'
18+
}
19+
20+
public elaborate() {
21+
return 'Import what you are trying to export, then export it again.'
22+
}
23+
}
24+
25+
const noExportNamedDeclarationWithSource: Rule<ExportNamedDeclaration> = {
26+
name: 'no-export-named-declaration-with-source',
27+
checkers: {
28+
ExportNamedDeclaration(node) {
29+
if (node.source !== null) {
30+
return [new NoExportNamedDeclarationWithSourceError(node)]
31+
}
32+
return []
33+
}
34+
}
35+
}
36+
37+
export default noExportNamedDeclarationWithSource

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import * as es from 'estree'
22

33
import { UNKNOWN_LOCATION } from '../../../constants'
4-
import { ErrorSeverity, ErrorType, Node, Rule, SourceError } from '../../../types'
4+
import { ErrorSeverity, ErrorType, Node, SourceError } from '../../../types'
5+
import { Rule } from '../../types'
56

67
export class NoFunctionDeclarationWithoutIdentifierError implements SourceError {
78
public type = ErrorType.SYNTAX

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import * as es from 'estree'
22

33
import { UNKNOWN_LOCATION } from '../../../constants'
4-
import { ErrorSeverity, ErrorType, Rule, SourceError } from '../../../types'
4+
import { ErrorSeverity, ErrorType, SourceError } from '../../../types'
5+
import { Rule } from '../../types'
56
import { stripIndent } from '../../../utils/formatters'
67

78
export class NoHolesInArrays implements SourceError {

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { generate } from 'astring'
22
import * as es from 'estree'
33

44
import { UNKNOWN_LOCATION } from '../../../constants'
5-
import { Chapter, ErrorSeverity, ErrorType, Node, Rule, SourceError } from '../../../types'
5+
import { Chapter, ErrorSeverity, ErrorType, Node, SourceError } from '../../../types'
6+
import { Rule } from '../../types'
67
import { stripIndent } from '../../../utils/formatters'
78

89
export class NoIfWithoutElseError implements SourceError {

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import * as es from 'estree'
22

33
import { UNKNOWN_LOCATION } from '../../../constants'
4-
import { ErrorSeverity, ErrorType, Node, Rule, SourceError } from '../../../types'
4+
import { ErrorSeverity, ErrorType, Node, SourceError } from '../../../types'
5+
import { Rule } from '../../types'
56
import { stripIndent } from '../../../utils/formatters'
67

78
export class NoImplicitDeclareUndefinedError implements SourceError {

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import * as es from 'estree'
22

33
import { UNKNOWN_LOCATION } from '../../../constants'
4-
import { ErrorSeverity, ErrorType, Node, Rule, SourceError } from '../../../types'
4+
import { ErrorSeverity, ErrorType, Node, SourceError } from '../../../types'
5+
import { Rule } from '../../types'
56
import { stripIndent } from '../../../utils/formatters'
67

78
export class NoImplicitReturnUndefinedError implements SourceError {

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import * as es from 'estree'
22

33
import { UNKNOWN_LOCATION } from '../../../constants'
44
import { defaultExportLookupName } from '../../../stdlib/localImport.prelude'
5-
import { ErrorSeverity, ErrorType, Node, Rule, SourceError } from '../../../types'
5+
import { ErrorSeverity, ErrorType, Node, SourceError } from '../../../types'
6+
import { Rule } from '../../types'
67
import syntaxBlacklist from '../syntax'
78

89
export class NoImportSpecifierWithDefaultError implements SourceError {

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import * as es from 'estree'
22

33
import { UNKNOWN_LOCATION } from '../../../constants'
4-
import { Chapter, ErrorSeverity, ErrorType, Node, Rule, SourceError } from '../../../types'
4+
import { Chapter, ErrorSeverity, ErrorType, Node, SourceError } from '../../../types'
5+
import { Rule } from '../../types'
56

67
export class NoNullError implements SourceError {
78
public type = ErrorType.SYNTAX

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import * as es from 'estree'
22

33
import { UNKNOWN_LOCATION } from '../../../constants'
4-
import { ErrorSeverity, ErrorType, Node, Rule, SourceError } from '../../../types'
4+
import { ErrorSeverity, ErrorType, Node, SourceError } from '../../../types'
5+
import { Rule } from '../../types'
56

67
export class NoSpreadInArray implements SourceError {
78
public type = ErrorType.SYNTAX

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import * as es from 'estree'
22

33
import { UNKNOWN_LOCATION } from '../../../constants'
4-
import { ErrorSeverity, ErrorType, Node, Rule, SourceError } from '../../../types'
4+
import { ErrorSeverity, ErrorType, Node, SourceError } from '../../../types'
5+
import { Rule } from '../../types'
56

67
export class NoTemplateExpressionError implements SourceError {
78
public type = ErrorType.SYNTAX

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as es from 'estree'
22

3-
import { Rule, Variant } from '../../../types'
3+
import { Variant } from '../../../types'
4+
import { Rule } from '../../types'
45
import { NoUnspecifiedOperatorError } from './noUnspecifiedOperator'
56

67
const noTypeofOperator: Rule<es.UnaryExpression> = {

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import * as es from 'estree'
22

33
import { UNKNOWN_LOCATION } from '../../../constants'
4-
import { ErrorSeverity, ErrorType, Node, Rule, SourceError } from '../../../types'
4+
import { ErrorSeverity, ErrorType, Node, SourceError } from '../../../types'
5+
import { Rule } from '../../types'
56

67
const specifiedLiterals = ['boolean', 'string', 'number']
78

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import * as es from 'estree'
22

33
import { UNKNOWN_LOCATION } from '../../../constants'
4-
import { ErrorSeverity, ErrorType, Node, Rule, SourceError } from '../../../types'
4+
import { ErrorSeverity, ErrorType, Node, SourceError } from '../../../types'
5+
import { Rule } from '../../types'
56

67
export class NoUnspecifiedOperatorError implements SourceError {
78
public type = ErrorType.SYNTAX

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { generate } from 'astring'
22
import * as es from 'estree'
33

44
import { UNKNOWN_LOCATION } from '../../../constants'
5-
import { ErrorSeverity, ErrorType, Node, Rule, SourceError } from '../../../types'
5+
import { ErrorSeverity, ErrorType, Node, SourceError } from '../../../types'
6+
import { Rule } from '../../types'
67

78
export class NoUpdateAssignment implements SourceError {
89
public type = ErrorType.SYNTAX

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { generate } from 'astring'
22
import * as es from 'estree'
33

44
import { UNKNOWN_LOCATION } from '../../../constants'
5-
import { ErrorSeverity, ErrorType, Node, Rule, SourceError } from '../../../types'
5+
import { ErrorSeverity, ErrorType, Node, SourceError } from '../../../types'
6+
import { Rule } from '../../types'
67

78
export class NoVarError implements SourceError {
89
public type = ErrorType.SYNTAX

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { generate } from 'astring'
22
import * as es from 'estree'
33

44
import { UNKNOWN_LOCATION } from '../../../constants'
5-
import { ErrorSeverity, ErrorType, Node, Rule, SourceError } from '../../../types'
5+
import { ErrorSeverity, ErrorType, Node, SourceError } from '../../../types'
6+
import { Rule } from '../../types'
67

78
export class MultipleDeclarationsError implements SourceError {
89
public type = ErrorType.SYNTAX

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import * as es from 'estree'
22

33
import { UNKNOWN_LOCATION } from '../../../constants'
4-
import { ErrorSeverity, ErrorType, Node, Rule, SourceError } from '../../../types'
4+
import { ErrorSeverity, ErrorType, Node, SourceError } from '../../../types'
5+
import { Rule } from '../../types'
56

67
export class StrictEqualityError implements SourceError {
78
public type = ErrorType.SYNTAX

‎src/parser/types.ts

+13-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
import { ParserOptions } from '@babel/parser'
2-
import { Options } from 'acorn'
3-
import { Program } from 'estree'
1+
import type { Program } from 'estree'
42

5-
import { Context } from '../types'
3+
import type { Context, Chapter, Node, SourceError, Variant } from '../types'
64

7-
export type AcornOptions = Options
8-
export type BabelOptions = ParserOptions
5+
export type { Options as AcornOptions } from 'acorn';
6+
export type { ParserOptions as BabelOptions } from '@babel/parser'
97

108
export interface Parser<TOptions> {
119
parse(
@@ -16,3 +14,12 @@ export interface Parser<TOptions> {
1614
): Program | null
1715
validate(ast: Program, context: Context, throwOnError?: boolean): boolean
1816
}
17+
18+
export interface Rule<T extends Node> {
19+
name: string
20+
disableFromChapter?: Chapter
21+
disableForVariants?: Variant[]
22+
checkers: {
23+
[name: string]: (node: T, ancestors: Node[]) => SourceError[]
24+
}
25+
}

‎src/types.ts

-9
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,6 @@ export interface SourceError {
4747
elaborate(): string
4848
}
4949

50-
export interface Rule<T extends Node> {
51-
name: string
52-
disableFromChapter?: Chapter
53-
disableForVariants?: Variant[]
54-
checkers: {
55-
[name: string]: (node: T, ancestors: Node[]) => SourceError[]
56-
}
57-
}
58-
5950
export interface Comment {
6051
type: 'Line' | 'Block'
6152
value: string

0 commit comments

Comments
 (0)
Please sign in to comment.