diff --git a/crates/swc_ecma_parser/src/error.rs b/crates/swc_ecma_parser/src/error.rs index 3b8bc14840a..35a0a30c0eb 100644 --- a/crates/swc_ecma_parser/src/error.rs +++ b/crates/swc_ecma_parser/src/error.rs @@ -195,6 +195,7 @@ pub enum SyntaxError { TsBindingPatCannotBeOptional, SuperCallOptional, OptChainCannotFollowConstructorCall, + TaggedTplInOptChain, TrailingCommaInsideImport, @@ -470,6 +471,9 @@ impl SyntaxError { SyntaxError::OptChainCannotFollowConstructorCall => { "Constructor in/after an optional chaining is not allowed.".into() } + SyntaxError::TaggedTplInOptChain => { + "Tagged template literal is not allowed in optional chain.".into() + } SyntaxError::TsInvalidParamPropPat => { "Typescript parameter property must be an identifier or assignment pattern".into() } diff --git a/crates/swc_ecma_parser/src/parser/expr.rs b/crates/swc_ecma_parser/src/parser/expr.rs index c1cf78c82ce..7e7ac71577a 100644 --- a/crates/swc_ecma_parser/src/parser/expr.rs +++ b/crates/swc_ecma_parser/src/parser/expr.rs @@ -1060,6 +1060,11 @@ impl Parser { let tpl = Box::new(self.parse_tpl(true)?); let span = span!(self, tagged_tpl_start); + + if tag.is_opt_chain() { + self.emit_err(span, SyntaxError::TaggedTplInOptChain); + } + Ok(TaggedTpl { span, tag, diff --git a/crates/swc_ecma_parser/tests/errors/optional-chaining/tagged-template.js b/crates/swc_ecma_parser/tests/errors/optional-chaining/tagged-template.js new file mode 100644 index 00000000000..6a5a3e6d395 --- /dev/null +++ b/crates/swc_ecma_parser/tests/errors/optional-chaining/tagged-template.js @@ -0,0 +1 @@ +a?.b`` diff --git a/crates/swc_ecma_parser/tests/errors/optional-chaining/tagged-template.js.swc-stderr b/crates/swc_ecma_parser/tests/errors/optional-chaining/tagged-template.js.swc-stderr new file mode 100644 index 00000000000..40dba980032 --- /dev/null +++ b/crates/swc_ecma_parser/tests/errors/optional-chaining/tagged-template.js.swc-stderr @@ -0,0 +1,6 @@ + + x Tagged template literal is not allowed in optional chain. + ,-[$DIR/tests/errors/optional-chaining/tagged-template.js:1:1] + 1 | a?.b`` + : ^^^^^^ + `----