From 3a0f2aa7efd954a10cc30d0f5197d592f5e0dfd9 Mon Sep 17 00:00:00 2001 From: DonIsaac <22823424+DonIsaac@users.noreply.github.com> Date: Tue, 9 Jul 2024 02:54:06 +0000 Subject: [PATCH] feat(parser): check for illegal modifiers in modules and namespaces (#4126) --- crates/oxc_parser/src/ts/statement.rs | 5 +++ tasks/coverage/parser_typescript.snap | 45 +++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/crates/oxc_parser/src/ts/statement.rs b/crates/oxc_parser/src/ts/statement.rs index ae76a82719b22..dd60745217ce3 100644 --- a/crates/oxc_parser/src/ts/statement.rs +++ b/crates/oxc_parser/src/ts/statement.rs @@ -265,6 +265,11 @@ impl<'a> ParserImpl<'a> { kind: TSModuleDeclarationKind, modifiers: &Modifiers<'a>, ) -> Result>> { + self.verify_modifiers( + modifiers, + ModifierFlags::DECLARE | ModifierFlags::EXPORT, + diagnostics::modifier_cannot_be_used_here, + ); let id = match self.cur_kind() { Kind::Str => self.parse_literal_string().map(TSModuleDeclarationName::StringLiteral), _ => self.parse_identifier_name().map(TSModuleDeclarationName::Identifier), diff --git a/tasks/coverage/parser_typescript.snap b/tasks/coverage/parser_typescript.snap index ccfb3c9954f3e..a4fe5cd551996 100644 --- a/tasks/coverage/parser_typescript.snap +++ b/tasks/coverage/parser_typescript.snap @@ -10826,6 +10826,13 @@ Expect to Parse: "conformance/salsa/plainJSRedeclare3.ts" 2 │ } ╰──── + × 'async' modifier cannot be used here. + ╭─[conformance/async/es5/asyncModule_es5.ts:1:1] + 1 │ async module M { + · ───── + 2 │ } + ╰──── + × Expected `(` but found `Identifier` ╭─[conformance/async/es5/asyncSetter_es5.ts:2:13] 1 │ class C { @@ -10961,6 +10968,13 @@ Expect to Parse: "conformance/salsa/plainJSRedeclare3.ts" 2 │ } ╰──── + × 'async' modifier cannot be used here. + ╭─[conformance/async/es6/asyncModule_es6.ts:1:1] + 1 │ async module M { + · ───── + 2 │ } + ╰──── + × Expected `(` but found `Identifier` ╭─[conformance/async/es6/asyncSetter_es6.ts:2:13] 1 │ class C { @@ -16569,6 +16583,14 @@ Expect to Parse: "conformance/salsa/plainJSRedeclare3.ts" × 'public' modifier cannot be used here. ╭─[conformance/internalModules/moduleBody/invalidModuleWithStatementsOfEveryKind.ts:19:5] 18 │ module Y3 { + 19 │ public module Module { + · ────── + 20 │ class A { s: string } + ╰──── + + × 'public' modifier cannot be used here. + ╭─[conformance/internalModules/moduleBody/invalidModuleWithStatementsOfEveryKind.ts:19:5] + 18 │ module Y3 { 19 │ public module Module { · ────── 20 │ class A { s: string } @@ -16625,6 +16647,14 @@ Expect to Parse: "conformance/salsa/plainJSRedeclare3.ts" × 'private' modifier cannot be used here. ╭─[conformance/internalModules/moduleBody/invalidModuleWithStatementsOfEveryKind.ts:44:5] 43 │ module YY3 { + 44 │ private module Module { + · ─────── + 45 │ class A { s: string } + ╰──── + + × 'private' modifier cannot be used here. + ╭─[conformance/internalModules/moduleBody/invalidModuleWithStatementsOfEveryKind.ts:44:5] + 43 │ module YY3 { 44 │ private module Module { · ─────── 45 │ class A { s: string } @@ -16681,6 +16711,14 @@ Expect to Parse: "conformance/salsa/plainJSRedeclare3.ts" × 'static' modifier cannot be used here. ╭─[conformance/internalModules/moduleBody/invalidModuleWithStatementsOfEveryKind.ts:70:5] 69 │ module YYY3 { + 70 │ static module Module { + · ────── + 71 │ class A { s: string } + ╰──── + + × 'static' modifier cannot be used here. + ╭─[conformance/internalModules/moduleBody/invalidModuleWithStatementsOfEveryKind.ts:70:5] + 69 │ module YYY3 { 70 │ static module Module { · ────── 71 │ class A { s: string } @@ -18128,6 +18166,13 @@ Expect to Parse: "conformance/salsa/plainJSRedeclare3.ts" 2 │ } ╰──── + × 'protected' modifier cannot be used here. + ╭─[conformance/parser/ecmascript5/Protected/Protected2.ts:1:1] + 1 │ protected module M { + · ───────── + 2 │ } + ╰──── + × Expected a semicolon or an implicit semicolon after a statement, but found none ╭─[conformance/parser/ecmascript5/RealWorld/parserharness.ts:1430:16] 1429 │ // Regex for parsing options in the format "@Alpha: Value of any sort"