aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2021-03-06 23:11:36 +0000
committerGitHub <noreply@github.com>2021-03-07 07:11:36 +0800
commitc7520b4b971c0afcfb66a9ffa215d0d8e4a6fabe (patch)
treea80fef13044751527724eab0c0d0bfc9d99187f3
parentad903e924050067a96a33f2eb5a0b9ab3e5454c1 (diff)
downloadtracifyjs-c7520b4b971c0afcfb66a9ffa215d0d8e4a6fabe.tar.gz
tracifyjs-c7520b4b971c0afcfb66a9ffa215d0d8e4a6fabe.zip
support `new.target` (#4746)
fixes #4745
-rw-r--r--lib/ast.js9
-rw-r--r--lib/parse.js9
-rw-r--r--test/compress/classes.js19
-rw-r--r--test/compress/functions.js19
4 files changed, 55 insertions, 1 deletions
diff --git a/lib/ast.js b/lib/ast.js
index e46fb2e3..1a1e0454 100644
--- a/lib/ast.js
+++ b/lib/ast.js
@@ -1812,10 +1812,17 @@ var AST_Super = DEFNODE("Super", null, {
var AST_This = DEFNODE("This", null, {
$documentation: "The `this` symbol",
_validate: function() {
- if (this.name !== "this") throw new Error('name must be "this"');
+ if (this.TYPE == "This" && this.name !== "this") throw new Error('name must be "this"');
},
}, AST_ObjectIdentity);
+var AST_NewTarget = DEFNODE("NewTarget", null, {
+ $documentation: "The `new.target` symbol",
+ _validate: function() {
+ if (this.name !== "new.target") throw new Error('name must be "new.target": ' + this.name);
+ },
+}, AST_This);
+
var AST_Template = DEFNODE("Template", "expressions strings tag", {
$documentation: "A template literal, i.e. tag`str1${expr1}...strN${exprN}strN+1`",
$propdoc: {
diff --git a/lib/parse.js b/lib/parse.js
index 0798062d..835ac21c 100644
--- a/lib/parse.js
+++ b/lib/parse.js
@@ -1736,6 +1736,15 @@ function parse($TEXT, options) {
var new_ = function(allow_calls) {
var start = S.token;
expect_token("operator", "new");
+ if (is("punc", ".") && is_token(peek(), "name", "target")) {
+ next();
+ next();
+ return new AST_NewTarget({
+ name: "new.target",
+ start: start,
+ end: prev(),
+ })
+ }
var newexp = expr_atom(false), args;
if (is("punc", "(")) {
next();
diff --git a/test/compress/classes.js b/test/compress/classes.js
index 93819bfa..a1b4f0cd 100644
--- a/test/compress/classes.js
+++ b/test/compress/classes.js
@@ -1225,3 +1225,22 @@ issue_4725_2: {
expect_stdout: "PASS"
node_version: ">=4"
}
+
+new_target: {
+ input: {
+ console.log(typeof new class {
+ constructor() {
+ this.f = () => new.target;
+ }
+ }().f());
+ }
+ expect: {
+ console.log(typeof new class {
+ constructor() {
+ this.f = () => new.target;
+ }
+ }().f());
+ }
+ expect_stdout: "function"
+ node_version: ">=6"
+}
diff --git a/test/compress/functions.js b/test/compress/functions.js
index 30779c15..856dbcea 100644
--- a/test/compress/functions.js
+++ b/test/compress/functions.js
@@ -5752,3 +5752,22 @@ issue_4725_2: {
expect_stdout: "PASS"
node_version: ">=4"
}
+
+new_target: {
+ input: {
+ console.log(typeof new function() {
+ return new.target;
+ }, function() {
+ return new.target;
+ }());
+ }
+ expect: {
+ console.log(typeof new function() {
+ return new.target;
+ }(), function() {
+ return new.target;
+ }());
+ }
+ expect_stdout: "function undefined"
+ node_version: ">=6"
+}