From 571c5e31233d017cf88b374c88224960f6cb570a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bojan=20=C4=90ur=C4=91evi=C4=87?= Date: Sat, 13 Nov 2021 15:34:28 -0500 Subject: [PATCH] fix(es/transforms/compat): Fix `new_target` (#2736) --- .../src/es2015/new_target.rs | 2 ++ .../fixture/new-target/exec/block/exec.js | 22 +++++++++++++++++++ .../fixture/new-target/general/block/input.js | 11 ++++++++++ .../new-target/general/block/output.js | 10 +++++++++ .../new-target/general/iiaf-class/input.js | 11 ++++++++++ .../new-target/general/iiaf-class/output.js | 10 +++++++++ 6 files changed, 66 insertions(+) create mode 100644 crates/swc_ecma_transforms_compat/tests/fixture/new-target/exec/block/exec.js create mode 100644 crates/swc_ecma_transforms_compat/tests/fixture/new-target/general/block/input.js create mode 100644 crates/swc_ecma_transforms_compat/tests/fixture/new-target/general/block/output.js create mode 100644 crates/swc_ecma_transforms_compat/tests/fixture/new-target/general/iiaf-class/input.js create mode 100644 crates/swc_ecma_transforms_compat/tests/fixture/new-target/general/iiaf-class/output.js diff --git a/crates/swc_ecma_transforms_compat/src/es2015/new_target.rs b/crates/swc_ecma_transforms_compat/src/es2015/new_target.rs index 6bd2d462fba..7a444ec6cd1 100644 --- a/crates/swc_ecma_transforms_compat/src/es2015/new_target.rs +++ b/crates/swc_ecma_transforms_compat/src/es2015/new_target.rs @@ -85,6 +85,8 @@ impl VisitMut for NewTarget { let old = self.in_constructor; self.in_constructor = true; + self.in_arrow_expr = false; + self.var = None; c.visit_mut_children_with(self); diff --git a/crates/swc_ecma_transforms_compat/tests/fixture/new-target/exec/block/exec.js b/crates/swc_ecma_transforms_compat/tests/fixture/new-target/exec/block/exec.js new file mode 100644 index 00000000000..22a21c5e0c2 --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/fixture/new-target/exec/block/exec.js @@ -0,0 +1,22 @@ +const targets = []; + +{ + function foo() { + targets.push(new.target); + } + foo(); +} + +expect(targets[0]).toBeUndefined(); + +{ + class Foo { + constructor() { + targets.push(new.target); + } + } + + new Foo(); +} + +expect(targets[1]).not.toBeUndefined(); diff --git a/crates/swc_ecma_transforms_compat/tests/fixture/new-target/general/block/input.js b/crates/swc_ecma_transforms_compat/tests/fixture/new-target/general/block/input.js new file mode 100644 index 00000000000..7e9333e9155 --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/fixture/new-target/general/block/input.js @@ -0,0 +1,11 @@ +{ + class Foo { + constructor() { + new.target; + } + + test() { + new.target; + } + } +} diff --git a/crates/swc_ecma_transforms_compat/tests/fixture/new-target/general/block/output.js b/crates/swc_ecma_transforms_compat/tests/fixture/new-target/general/block/output.js new file mode 100644 index 00000000000..a109de483bf --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/fixture/new-target/general/block/output.js @@ -0,0 +1,10 @@ +{ + class Foo { + constructor() { + this.constructor; + } + test() { + void 0; + } + } +} diff --git a/crates/swc_ecma_transforms_compat/tests/fixture/new-target/general/iiaf-class/input.js b/crates/swc_ecma_transforms_compat/tests/fixture/new-target/general/iiaf-class/input.js new file mode 100644 index 00000000000..7f56abe8577 --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/fixture/new-target/general/iiaf-class/input.js @@ -0,0 +1,11 @@ +(() => { + class Foo { + constructor() { + new.target; + } + + test() { + new.target; + } + } +})() diff --git a/crates/swc_ecma_transforms_compat/tests/fixture/new-target/general/iiaf-class/output.js b/crates/swc_ecma_transforms_compat/tests/fixture/new-target/general/iiaf-class/output.js new file mode 100644 index 00000000000..3a20bf75213 --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/fixture/new-target/general/iiaf-class/output.js @@ -0,0 +1,10 @@ +(() => { + class Foo { + constructor() { + this.constructor; + } + test() { + void 0; + } + } +})()