From 6dc64c9ca0bccf27b325d3298f4ed7f07eaaa076 Mon Sep 17 00:00:00 2001 From: Pig Fang Date: Sun, 24 Apr 2022 11:04:38 +0800 Subject: [PATCH] feat(es/parser): Reject `'use strict'` with non-simple params list in TS (#4416) --- crates/swc_ecma_parser/src/parser/class_and_fn.rs | 4 ++-- .../typescript-errors/use-strict/arrow-fn-expr/input.ts | 1 + .../use-strict/arrow-fn-expr/input.ts.stderr | 6 ++++++ .../tests/typescript-errors/use-strict/fn-block/input.ts | 1 + .../typescript-errors/use-strict/fn-block/input.ts.stderr | 6 ++++++ 5 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 crates/swc_ecma_parser/tests/typescript-errors/use-strict/arrow-fn-expr/input.ts create mode 100644 crates/swc_ecma_parser/tests/typescript-errors/use-strict/arrow-fn-expr/input.ts.stderr create mode 100644 crates/swc_ecma_parser/tests/typescript-errors/use-strict/fn-block/input.ts create mode 100644 crates/swc_ecma_parser/tests/typescript-errors/use-strict/fn-block/input.ts.stderr diff --git a/crates/swc_ecma_parser/src/parser/class_and_fn.rs b/crates/swc_ecma_parser/src/parser/class_and_fn.rs index 904de98ccf8..cc4ec1c7a60 100644 --- a/crates/swc_ecma_parser/src/parser/class_and_fn.rs +++ b/crates/swc_ecma_parser/src/parser/class_and_fn.rs @@ -1527,7 +1527,7 @@ impl FnBodyParser for Parser { }; let result = self.with_ctx(ctx).parse_block(false); result.map(|block_stmt| { - if !self.input.syntax().typescript() && !is_simple_parameter_list { + if !is_simple_parameter_list { if let Some(span) = has_use_strict(&block_stmt) { self.emit_err(span, SyntaxError::IllegalLanguageModeDirective); } @@ -1558,7 +1558,7 @@ impl FnBodyParser> for Parser { } let block = self.include_in_expr(true).parse_block(true); block.map(|block_stmt| { - if !self.input.syntax().typescript() && !is_simple_parameter_list { + if !is_simple_parameter_list { if let Some(span) = has_use_strict(&block_stmt) { self.emit_err(span, SyntaxError::IllegalLanguageModeDirective); } diff --git a/crates/swc_ecma_parser/tests/typescript-errors/use-strict/arrow-fn-expr/input.ts b/crates/swc_ecma_parser/tests/typescript-errors/use-strict/arrow-fn-expr/input.ts new file mode 100644 index 00000000000..3f977d124d1 --- /dev/null +++ b/crates/swc_ecma_parser/tests/typescript-errors/use-strict/arrow-fn-expr/input.ts @@ -0,0 +1 @@ +const f = ([x]: string) => { 'use strict' } diff --git a/crates/swc_ecma_parser/tests/typescript-errors/use-strict/arrow-fn-expr/input.ts.stderr b/crates/swc_ecma_parser/tests/typescript-errors/use-strict/arrow-fn-expr/input.ts.stderr new file mode 100644 index 00000000000..9778625539b --- /dev/null +++ b/crates/swc_ecma_parser/tests/typescript-errors/use-strict/arrow-fn-expr/input.ts.stderr @@ -0,0 +1,6 @@ + + x Illegal 'use strict' directive in function with non-simple parameter list. + ,-[$DIR/tests/typescript-errors/use-strict/arrow-fn-expr/input.ts:1:1] + 1 | const f = ([x]: string) => { 'use strict' } + : ^^^^^^^^^^^^ + `---- diff --git a/crates/swc_ecma_parser/tests/typescript-errors/use-strict/fn-block/input.ts b/crates/swc_ecma_parser/tests/typescript-errors/use-strict/fn-block/input.ts new file mode 100644 index 00000000000..b196fd81226 --- /dev/null +++ b/crates/swc_ecma_parser/tests/typescript-errors/use-strict/fn-block/input.ts @@ -0,0 +1 @@ +function f([x]: string) { 'use strict' } diff --git a/crates/swc_ecma_parser/tests/typescript-errors/use-strict/fn-block/input.ts.stderr b/crates/swc_ecma_parser/tests/typescript-errors/use-strict/fn-block/input.ts.stderr new file mode 100644 index 00000000000..d0fa548c6bb --- /dev/null +++ b/crates/swc_ecma_parser/tests/typescript-errors/use-strict/fn-block/input.ts.stderr @@ -0,0 +1,6 @@ + + x Illegal 'use strict' directive in function with non-simple parameter list. + ,-[$DIR/tests/typescript-errors/use-strict/fn-block/input.ts:1:1] + 1 | function f([x]: string) { 'use strict' } + : ^^^^^^^^^^^^ + `----