Skip to content

Commit 7970f8e

Browse files
committed
fix(parser): produce syntax error for export enum and similar ts syntaxes
fixes #13205 closes #13206
1 parent 63abd7c commit 7970f8e

File tree

4 files changed

+53
-65
lines changed

4 files changed

+53
-65
lines changed

crates/oxc_parser/src/lexer/kind.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -313,11 +313,6 @@ impl Kind {
313313
self.is_literal_property_name() || self == Self::PrivateIdentifier
314314
}
315315

316-
#[inline]
317-
pub fn is_variable_declaration(self) -> bool {
318-
matches!(self, Var | Let | Const)
319-
}
320-
321316
#[rustfmt::skip]
322317
#[inline]
323318
pub fn is_assignment_operator(self) -> bool {

crates/oxc_parser/src/ts/statement.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -419,15 +419,16 @@ impl<'a> ParserImpl<'a> {
419419
}
420420
}
421421
match kind {
422-
Kind::Global | Kind::Module | Kind::Namespace => {
423-
let decl = self.parse_ts_module_declaration(start_span, modifiers);
424-
Declaration::TSModuleDeclaration(decl)
425-
}
426-
Kind::Type => self.parse_ts_type_alias_declaration(start_span, modifiers),
427-
Kind::Enum => self.parse_ts_enum_declaration(start_span, modifiers),
428-
Kind::Interface => {
422+
Kind::Var | Kind::Let | Kind::Const => {
423+
let kind = self.get_variable_declaration_kind();
429424
self.bump_any();
430-
self.parse_ts_interface_declaration(start_span, modifiers)
425+
let decl = self.parse_variable_declaration(
426+
start_span,
427+
kind,
428+
VariableDeclarationParent::Statement,
429+
modifiers,
430+
);
431+
Declaration::VariableDeclaration(decl)
431432
}
432433
Kind::Class => {
433434
let decl = self.parse_class_declaration(start_span, modifiers, decorators);
@@ -451,16 +452,15 @@ impl<'a> ParserImpl<'a> {
451452
}
452453
self.parse_ts_import_equals_declaration(import_kind, identifier, start_span)
453454
}
454-
kind if kind.is_variable_declaration() => {
455-
let kind = self.get_variable_declaration_kind();
455+
Kind::Global | Kind::Module | Kind::Namespace if self.is_ts => {
456+
let decl = self.parse_ts_module_declaration(start_span, modifiers);
457+
Declaration::TSModuleDeclaration(decl)
458+
}
459+
Kind::Type if self.is_ts => self.parse_ts_type_alias_declaration(start_span, modifiers),
460+
Kind::Enum if self.is_ts => self.parse_ts_enum_declaration(start_span, modifiers),
461+
Kind::Interface if self.is_ts => {
456462
self.bump_any();
457-
let decl = self.parse_variable_declaration(
458-
start_span,
459-
kind,
460-
VariableDeclarationParent::Statement,
461-
modifiers,
462-
);
463-
Declaration::VariableDeclaration(decl)
463+
self.parse_ts_interface_declaration(start_span, modifiers)
464464
}
465465
_ if self.at_function_with_async() => {
466466
let declare = modifiers.contains(ModifierKind::Declare);

tasks/coverage/snapshots/parser_babel.snap

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ commit: 98d18aa4
33
parser_babel Summary:
44
AST Parsed : 2403/2419 (99.34%)
55
Positive Passed: 2381/2419 (98.43%)
6-
Negative Passed: 1640/1756 (93.39%)
6+
Negative Passed: 1642/1756 (93.51%)
77
Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/core/categorized/invalid-startindex-and-startline-specified-without-startcolumn/input.js
88

99
Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/core/categorized/startline-and-startcolumn-specified/input.js
@@ -70,10 +70,6 @@ Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/es
7070

7171
Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/estree/class-private-property/typescript-invalid-abstract-babel-7/input.ts
7272

73-
Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/flow/expect-plugin/export-interface/input.js
74-
75-
Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/flow/expect-plugin/export-type/input.js
76-
7773
Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/arrow-function/arrow-like-in-conditional-2/input.ts
7874

7975
Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/cast/satisfies-const-error/input.ts
@@ -12077,10 +12073,22 @@ Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typesc
1207712073
╰────
1207812074

1207912075
× Unexpected token
12080-
╭─[babel/packages/babel-parser/test/fixtures/flow/expect-plugin/export-type-named/input.js:2:13]
12076+
╭─[babel/packages/babel-parser/test/fixtures/flow/expect-plugin/export-interface/input.js:1:8]
12077+
1 │ export interface Foo {}
12078+
· ─────────
12079+
╰────
12080+
12081+
× Unexpected token
12082+
╭─[babel/packages/babel-parser/test/fixtures/flow/expect-plugin/export-type/input.js:1:8]
12083+
1 │ export type Foo = number;
12084+
· ────
12085+
╰────
12086+
12087+
× Unexpected token
12088+
╭─[babel/packages/babel-parser/test/fixtures/flow/expect-plugin/export-type-named/input.js:2:8]
1208112089
1 │ var Foo;
1208212090
2 │ export type { Foo };
12083-
·
12091+
· ───
1208412092
╰────
1208512093

1208612094
× Unexpected token

tasks/coverage/snapshots/parser_typescript.snap

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8927,19 +8927,12 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/parser/ecmasc
89278927
╰────
89288928
help: Try insert a semicolon here
89298929

8930-
× Duplicated export 'foo'
8931-
╭─[typescript/tests/cases/compiler/jsdocTypedefNoCrash2.ts:1:13]
8930+
× Unexpected token
8931+
╭─[typescript/tests/cases/compiler/jsdocTypedefNoCrash2.ts:1:8]
89328932
1 │ export type foo = 5;
8933-
· ─┬─
8934-
· ╰── Export has already been declared here
8933+
· ────
89358934
2 │ /**
89368935
╰────
8937-
╭─[typescript/tests/cases/compiler/jsdocTypedefNoCrash2.ts:6:14]
8938-
5 │ */
8939-
6 │ export const foo = 5;
8940-
· ─┬─
8941-
· ╰── It cannot be redeclared here
8942-
╰────
89438936

89448937
× Unexpected token
89458938
╭─[typescript/tests/cases/compiler/jsxAttributeMissingInitializer.tsx:1:21]
@@ -20723,9 +20716,9 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/parser/ecmasc
2072320716
╰────
2072420717

2072520718
× Unexpected token
20726-
╭─[typescript/tests/cases/conformance/externalModules/typeOnly/exportNamespace_js.ts:1:13]
20719+
╭─[typescript/tests/cases/conformance/externalModules/typeOnly/exportNamespace_js.ts:1:8]
2072720720
1 │ export type * from './a';
20728-
·
20721+
· ───
2072920722
╰────
2073020723

2073120724
× TS(2207): The 'type' modifier cannot be used on a named export when 'export type' is used on its export statement.
@@ -21204,12 +21197,12 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/parser/ecmasc
2120421197
╰────
2120521198

2120621199
× Unexpected token
21207-
╭─[typescript/tests/cases/conformance/jsdoc/declarations/jsDeclarationsEnums.ts:10:1]
21208-
9
21209-
10 │ enum C {}
21210-
· ────
21211-
11
21212-
╰────
21200+
╭─[typescript/tests/cases/conformance/jsdoc/declarations/jsDeclarationsEnums.ts:4:8]
21201+
3
21202+
4export enum A {}
21203+
· ────
21204+
5
21205+
╰────
2121321206

2121421207
× TS(8002): 'import ... =' can only be used in TypeScript files.
2121521208
╭─[typescript/tests/cases/conformance/jsdoc/declarations/jsDeclarationsExportFormsErr.ts:1:1]
@@ -21226,27 +21219,19 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/parser/ecmasc
2122621219
╰────
2122721220

2122821221
× Unexpected token
21229-
╭─[typescript/tests/cases/conformance/jsdoc/declarations/jsDeclarationsInterfaces.ts:7:8]
21230-
6export interface B {
21231-
7 cat: string;
21232-
· ─
21233-
8}
21222+
╭─[typescript/tests/cases/conformance/jsdoc/declarations/jsDeclarationsInterfaces.ts:4:8]
21223+
3
21224+
4export interface A {}
21225+
· ─────────
21226+
5
2123421227
╰────
2123521228

21236-
× 'export statement' declaration can only be used at the top level of a module
21237-
╭─[typescript/tests/cases/conformance/jsdoc/declarations/jsDeclarationsTypeReferences4.ts:10:9]
21238-
9 │ // @ts-ignore
21239-
10 │ export { thing };
21240-
· ──────
21241-
11 │ }
21242-
╰────
21243-
21244-
× 'export statement' declaration can only be used at the top level of a module
21245-
╭─[typescript/tests/cases/conformance/jsdoc/declarations/jsDeclarationsTypeReferences4.ts:6:5]
21229+
× Unexpected token
21230+
╭─[typescript/tests/cases/conformance/jsdoc/declarations/jsDeclarationsTypeReferences4.ts:4:8]
21231+
3 │ // @ts-ignore
21232+
4 │ export namespace A {
21233+
· ─────────
2124621234
5 │ // @ts-ignore
21247-
6 │ export namespace B {
21248-
· ──────
21249-
7 │ const Something = require("fs").Something;
2125021235
╰────
2125121236

2125221237
× Identifier `err` has already been declared

0 commit comments

Comments
 (0)