From b834e6df12d96d5625d37ea5a61b43da7effe595 Mon Sep 17 00:00:00 2001 From: jkuehner Date: Mon, 12 Apr 2021 18:12:27 +0200 Subject: [PATCH 1/2] bugfix es2018 import() --- src/nodes.ts | 4 +- src/parser.ts | 13 +- .../dynamic-import/await-import.tree.json | 290 ++++++------ ...t-call-import-declaration.module.tree.json | 336 +++++++------- .../import-call-string.tree.json | 248 +++++------ .../import-call-template.tree.json | 380 ++++++++-------- .../dynamic-import/import-call-var.tree.json | 388 ++++++++--------- ...id-import-call-many-arguments.failure.json | 1 + ...valid-import-call-many-arguments.tree.json | 411 ------------------ ...valid-import-call-no-argument.failure.json | 1 + .../invalid-import-call-no-argument.tree.json | 320 -------------- .../loader-using-import.tree.json | 324 +++++++------- 12 files changed, 940 insertions(+), 1776 deletions(-) create mode 100644 test/fixtures/es2018/dynamic-import/invalid-import-call-many-arguments.failure.json delete mode 100644 test/fixtures/es2018/dynamic-import/invalid-import-call-many-arguments.tree.json create mode 100644 test/fixtures/es2018/dynamic-import/invalid-import-call-no-argument.failure.json delete mode 100644 test/fixtures/es2018/dynamic-import/invalid-import-call-no-argument.tree.json diff --git a/src/nodes.ts b/src/nodes.ts index ae7bbf566..4320f54fc 100644 --- a/src/nodes.ts +++ b/src/nodes.ts @@ -485,8 +485,10 @@ export class IfStatement { export class Import { readonly type: string; - constructor() { + readonly source: Literal; + constructor(source) { this.type = Syntax.Import; + this.source = source; } } diff --git a/src/parser.ts b/src/parser.ts index 64e58dd08..81d4e81b6 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -1282,7 +1282,18 @@ export class Parser { parseImportCall(): Node.Import { const node = this.createNode(); this.expectKeyword('import'); - return this.finalize(node, new Node.Import()); + this.expect("("); + + const source = this.parseAssignmentExpression(); + if (!this.match(")") && this.config.tolerant) { + this.tolerateUnexpectedToken(this.nextToken()); + } else { + this.expect(")"); + if (this.match(";")) { + this.nextToken(); + } + } + return this.finalize(node, new Node.Import(source)); } parseLeftHandSideExpressionAllowCall(): Node.Expression { diff --git a/test/fixtures/es2018/dynamic-import/await-import.tree.json b/test/fixtures/es2018/dynamic-import/await-import.tree.json index 282c7a916..30354c2bc 100644 --- a/test/fixtures/es2018/dynamic-import/await-import.tree.json +++ b/test/fixtures/es2018/dynamic-import/await-import.tree.json @@ -1,11 +1,37 @@ { + "range": [ + 0, + 39 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 39 + } + }, "type": "Program", "body": [ { + "range": [ + 0, + 39 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 39 + } + }, "type": "FunctionDeclaration", "id": { - "type": "Identifier", - "name": "f", "range": [ 15, 16 @@ -19,12 +45,12 @@ "line": 1, "column": 16 } - } + }, + "type": "Identifier", + "name": "f" }, "params": [ { - "type": "Identifier", - "name": "x", "range": [ 17, 18 @@ -38,56 +64,61 @@ "line": 1, "column": 18 } - } + }, + "type": "Identifier", + "name": "x" } ], "body": { + "range": [ + 20, + 39 + ], + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 1, + "column": 39 + } + }, "type": "BlockStatement", "body": [ { + "range": [ + 22, + 38 + ], + "loc": { + "start": { + "line": 1, + "column": 22 + }, + "end": { + "line": 1, + "column": 38 + } + }, "type": "ExpressionStatement", "expression": { + "range": [ + 22, + 37 + ], + "loc": { + "start": { + "line": 1, + "column": 22 + }, + "end": { + "line": 1, + "column": 37 + } + }, "type": "AwaitExpression", "argument": { - "type": "CallExpression", - "callee": { - "type": "Import", - "range": [ - 28, - 34 - ], - "loc": { - "start": { - "line": 1, - "column": 28 - }, - "end": { - "line": 1, - "column": 34 - } - } - }, - "arguments": [ - { - "type": "Identifier", - "name": "x", - "range": [ - 35, - 36 - ], - "loc": { - "start": { - "line": 1, - "column": 35 - }, - "end": { - "line": 1, - "column": 36 - } - } - } - ], - "optional": false, "range": [ 28, 37 @@ -101,92 +132,39 @@ "line": 1, "column": 37 } - } - }, - "range": [ - 22, - 37 - ], - "loc": { - "start": { - "line": 1, - "column": 22 }, - "end": { - "line": 1, - "column": 37 + "type": "Import", + "source": { + "range": [ + 35, + 36 + ], + "loc": { + "start": { + "line": 1, + "column": 35 + }, + "end": { + "line": 1, + "column": 36 + } + }, + "type": "Identifier", + "name": "x" } } - }, - "range": [ - 22, - 38 - ], - "loc": { - "start": { - "line": 1, - "column": 22 - }, - "end": { - "line": 1, - "column": 38 - } } } - ], - "range": [ - 20, - 39 - ], - "loc": { - "start": { - "line": 1, - "column": 20 - }, - "end": { - "line": 1, - "column": 39 - } - } + ] }, "generator": false, "expression": false, - "async": true, - "range": [ - 0, - 39 - ], - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 1, - "column": 39 - } - } + "async": true } ], "sourceType": "script", - "range": [ - 0, - 39 - ], - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 1, - "column": 39 - } - }, "tokens": [ { - "type": "Identifier", - "value": "async", "range": [ 0, 5 @@ -200,11 +178,11 @@ "line": 1, "column": 5 } - } + }, + "type": "Identifier", + "value": "async" }, { - "type": "Keyword", - "value": "function", "range": [ 6, 14 @@ -218,11 +196,11 @@ "line": 1, "column": 14 } - } + }, + "type": "Keyword", + "value": "function" }, { - "type": "Identifier", - "value": "f", "range": [ 15, 16 @@ -236,11 +214,11 @@ "line": 1, "column": 16 } - } + }, + "type": "Identifier", + "value": "f" }, { - "type": "Punctuator", - "value": "(", "range": [ 16, 17 @@ -254,11 +232,11 @@ "line": 1, "column": 17 } - } + }, + "type": "Punctuator", + "value": "(" }, { - "type": "Identifier", - "value": "x", "range": [ 17, 18 @@ -272,11 +250,11 @@ "line": 1, "column": 18 } - } + }, + "type": "Identifier", + "value": "x" }, { - "type": "Punctuator", - "value": ")", "range": [ 18, 19 @@ -290,11 +268,11 @@ "line": 1, "column": 19 } - } + }, + "type": "Punctuator", + "value": ")" }, { - "type": "Punctuator", - "value": "{", "range": [ 20, 21 @@ -308,11 +286,11 @@ "line": 1, "column": 21 } - } + }, + "type": "Punctuator", + "value": "{" }, { - "type": "Identifier", - "value": "await", "range": [ 22, 27 @@ -326,11 +304,11 @@ "line": 1, "column": 27 } - } + }, + "type": "Identifier", + "value": "await" }, { - "type": "Keyword", - "value": "import", "range": [ 28, 34 @@ -344,11 +322,11 @@ "line": 1, "column": 34 } - } + }, + "type": "Keyword", + "value": "import" }, { - "type": "Punctuator", - "value": "(", "range": [ 34, 35 @@ -362,11 +340,11 @@ "line": 1, "column": 35 } - } + }, + "type": "Punctuator", + "value": "(" }, { - "type": "Identifier", - "value": "x", "range": [ 35, 36 @@ -380,11 +358,11 @@ "line": 1, "column": 36 } - } + }, + "type": "Identifier", + "value": "x" }, { - "type": "Punctuator", - "value": ")", "range": [ 36, 37 @@ -398,11 +376,11 @@ "line": 1, "column": 37 } - } + }, + "type": "Punctuator", + "value": ")" }, { - "type": "Punctuator", - "value": "}", "range": [ 38, 39 @@ -416,7 +394,9 @@ "line": 1, "column": 39 } - } + }, + "type": "Punctuator", + "value": "}" } ] } \ No newline at end of file diff --git a/test/fixtures/es2018/dynamic-import/coexist-import-call-import-declaration.module.tree.json b/test/fixtures/es2018/dynamic-import/coexist-import-call-import-declaration.module.tree.json index 77b07237a..97f0fe654 100644 --- a/test/fixtures/es2018/dynamic-import/coexist-import-call-import-declaration.module.tree.json +++ b/test/fixtures/es2018/dynamic-import/coexist-import-call-import-declaration.module.tree.json @@ -1,14 +1,54 @@ { + "range": [ + 0, + 39 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 20 + } + }, "type": "Program", "body": [ { + "range": [ + 0, + 18 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 18 + } + }, "type": "ImportDeclaration", "specifiers": [ { + "range": [ + 7, + 8 + ], + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 8 + } + }, "type": "ImportDefaultSpecifier", "local": { - "type": "Identifier", - "name": "$", "range": [ 7, 8 @@ -22,28 +62,13 @@ "line": 1, "column": 8 } - } - }, - "range": [ - 7, - 8 - ], - "loc": { - "start": { - "line": 1, - "column": 7 }, - "end": { - "line": 1, - "column": 8 - } + "type": "Identifier", + "name": "$" } } ], "source": { - "type": "Literal", - "value": "a", - "raw": "\"a\"", "range": [ 14, 17 @@ -57,71 +82,62 @@ "line": 1, "column": 17 } - } - }, + }, + "type": "Literal", + "value": "a", + "raw": "\"a\"" + } + }, + { "range": [ - 0, - 18 + 19, + 39 ], "loc": { "start": { - "line": 1, + "line": 2, "column": 0 }, "end": { - "line": 1, - "column": 18 + "line": 2, + "column": 20 } - } - }, - { + }, "type": "ExpressionStatement", "expression": { + "range": [ + 19, + 38 + ], + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 19 + } + }, "type": "CallExpression", "callee": { + "range": [ + 19, + 35 + ], + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 16 + } + }, "type": "MemberExpression", "computed": false, "object": { - "type": "CallExpression", - "callee": { - "type": "Import", - "range": [ - 19, - 25 - ], - "loc": { - "start": { - "line": 2, - "column": 0 - }, - "end": { - "line": 2, - "column": 6 - } - } - }, - "arguments": [ - { - "type": "Literal", - "value": "b", - "raw": "\"b\"", - "range": [ - 26, - 29 - ], - "loc": { - "start": { - "line": 2, - "column": 7 - }, - "end": { - "line": 2, - "column": 10 - } - } - } - ], - "optional": false, "range": [ 19, 30 @@ -135,11 +151,29 @@ "line": 2, "column": 11 } + }, + "type": "Import", + "source": { + "range": [ + 26, + 29 + ], + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 10 + } + }, + "type": "Literal", + "value": "b", + "raw": "\"b\"" } }, "property": { - "type": "Identifier", - "name": "then", "range": [ 31, 35 @@ -153,28 +187,14 @@ "line": 2, "column": 16 } - } - }, - "optional": false, - "range": [ - 19, - 35 - ], - "loc": { - "start": { - "line": 2, - "column": 0 }, - "end": { - "line": 2, - "column": 16 - } - } + "type": "Identifier", + "name": "then" + }, + "optional": false }, "arguments": [ { - "type": "Identifier", - "name": "c", "range": [ 36, 37 @@ -188,60 +208,18 @@ "line": 2, "column": 18 } - } + }, + "type": "Identifier", + "name": "c" } ], - "optional": false, - "range": [ - 19, - 38 - ], - "loc": { - "start": { - "line": 2, - "column": 0 - }, - "end": { - "line": 2, - "column": 19 - } - } - }, - "range": [ - 19, - 39 - ], - "loc": { - "start": { - "line": 2, - "column": 0 - }, - "end": { - "line": 2, - "column": 20 - } + "optional": false } } ], "sourceType": "module", - "range": [ - 0, - 39 - ], - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 2, - "column": 20 - } - }, "tokens": [ { - "type": "Keyword", - "value": "import", "range": [ 0, 6 @@ -255,11 +233,11 @@ "line": 1, "column": 6 } - } + }, + "type": "Keyword", + "value": "import" }, { - "type": "Identifier", - "value": "$", "range": [ 7, 8 @@ -273,11 +251,11 @@ "line": 1, "column": 8 } - } + }, + "type": "Identifier", + "value": "$" }, { - "type": "Identifier", - "value": "from", "range": [ 9, 13 @@ -291,11 +269,11 @@ "line": 1, "column": 13 } - } + }, + "type": "Identifier", + "value": "from" }, { - "type": "String", - "value": "\"a\"", "range": [ 14, 17 @@ -309,11 +287,11 @@ "line": 1, "column": 17 } - } + }, + "type": "String", + "value": "\"a\"" }, { - "type": "Punctuator", - "value": ";", "range": [ 17, 18 @@ -327,11 +305,11 @@ "line": 1, "column": 18 } - } + }, + "type": "Punctuator", + "value": ";" }, { - "type": "Keyword", - "value": "import", "range": [ 19, 25 @@ -345,11 +323,11 @@ "line": 2, "column": 6 } - } + }, + "type": "Keyword", + "value": "import" }, { - "type": "Punctuator", - "value": "(", "range": [ 25, 26 @@ -363,11 +341,11 @@ "line": 2, "column": 7 } - } + }, + "type": "Punctuator", + "value": "(" }, { - "type": "String", - "value": "\"b\"", "range": [ 26, 29 @@ -381,11 +359,11 @@ "line": 2, "column": 10 } - } + }, + "type": "String", + "value": "\"b\"" }, { - "type": "Punctuator", - "value": ")", "range": [ 29, 30 @@ -399,11 +377,11 @@ "line": 2, "column": 11 } - } + }, + "type": "Punctuator", + "value": ")" }, { - "type": "Punctuator", - "value": ".", "range": [ 30, 31 @@ -417,11 +395,11 @@ "line": 2, "column": 12 } - } + }, + "type": "Punctuator", + "value": "." }, { - "type": "Identifier", - "value": "then", "range": [ 31, 35 @@ -435,11 +413,11 @@ "line": 2, "column": 16 } - } + }, + "type": "Identifier", + "value": "then" }, { - "type": "Punctuator", - "value": "(", "range": [ 35, 36 @@ -453,11 +431,11 @@ "line": 2, "column": 17 } - } + }, + "type": "Punctuator", + "value": "(" }, { - "type": "Identifier", - "value": "c", "range": [ 36, 37 @@ -471,11 +449,11 @@ "line": 2, "column": 18 } - } + }, + "type": "Identifier", + "value": "c" }, { - "type": "Punctuator", - "value": ")", "range": [ 37, 38 @@ -489,11 +467,11 @@ "line": 2, "column": 19 } - } + }, + "type": "Punctuator", + "value": ")" }, { - "type": "Punctuator", - "value": ";", "range": [ 38, 39 @@ -507,7 +485,9 @@ "line": 2, "column": 20 } - } + }, + "type": "Punctuator", + "value": ";" } ] } \ No newline at end of file diff --git a/test/fixtures/es2018/dynamic-import/import-call-string.tree.json b/test/fixtures/es2018/dynamic-import/import-call-string.tree.json index f2653fe39..0a96f732d 100644 --- a/test/fixtures/es2018/dynamic-import/import-call-string.tree.json +++ b/test/fixtures/es2018/dynamic-import/import-call-string.tree.json @@ -1,54 +1,70 @@ { + "range": [ + 0, + 30 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 30 + } + }, "type": "Program", "body": [ { + "range": [ + 0, + 30 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 30 + } + }, "type": "ExpressionStatement", "expression": { + "range": [ + 0, + 29 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 29 + } + }, "type": "CallExpression", "callee": { + "range": [ + 0, + 21 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 21 + } + }, "type": "MemberExpression", "computed": false, "object": { - "type": "CallExpression", - "callee": { - "type": "Import", - "range": [ - 0, - 6 - ], - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 1, - "column": 6 - } - } - }, - "arguments": [ - { - "type": "Literal", - "value": "lib.js", - "raw": "'lib.js'", - "range": [ - 7, - 15 - ], - "loc": { - "start": { - "line": 1, - "column": 7 - }, - "end": { - "line": 1, - "column": 15 - } - } - } - ], - "optional": false, "range": [ 0, 16 @@ -62,11 +78,29 @@ "line": 1, "column": 16 } + }, + "type": "Import", + "source": { + "range": [ + 7, + 15 + ], + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 15 + } + }, + "type": "Literal", + "value": "lib.js", + "raw": "'lib.js'" } }, "property": { - "type": "Identifier", - "name": "then", "range": [ 17, 21 @@ -80,28 +114,14 @@ "line": 1, "column": 21 } - } - }, - "optional": false, - "range": [ - 0, - 21 - ], - "loc": { - "start": { - "line": 1, - "column": 0 }, - "end": { - "line": 1, - "column": 21 - } - } + "type": "Identifier", + "name": "then" + }, + "optional": false }, "arguments": [ { - "type": "Identifier", - "name": "doThis", "range": [ 22, 28 @@ -115,60 +135,18 @@ "line": 1, "column": 28 } - } + }, + "type": "Identifier", + "name": "doThis" } ], - "optional": false, - "range": [ - 0, - 29 - ], - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 1, - "column": 29 - } - } - }, - "range": [ - 0, - 30 - ], - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 1, - "column": 30 - } + "optional": false } } ], "sourceType": "script", - "range": [ - 0, - 30 - ], - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 1, - "column": 30 - } - }, "tokens": [ { - "type": "Keyword", - "value": "import", "range": [ 0, 6 @@ -182,11 +160,11 @@ "line": 1, "column": 6 } - } + }, + "type": "Keyword", + "value": "import" }, { - "type": "Punctuator", - "value": "(", "range": [ 6, 7 @@ -200,11 +178,11 @@ "line": 1, "column": 7 } - } + }, + "type": "Punctuator", + "value": "(" }, { - "type": "String", - "value": "'lib.js'", "range": [ 7, 15 @@ -218,11 +196,11 @@ "line": 1, "column": 15 } - } + }, + "type": "String", + "value": "'lib.js'" }, { - "type": "Punctuator", - "value": ")", "range": [ 15, 16 @@ -236,11 +214,11 @@ "line": 1, "column": 16 } - } + }, + "type": "Punctuator", + "value": ")" }, { - "type": "Punctuator", - "value": ".", "range": [ 16, 17 @@ -254,11 +232,11 @@ "line": 1, "column": 17 } - } + }, + "type": "Punctuator", + "value": "." }, { - "type": "Identifier", - "value": "then", "range": [ 17, 21 @@ -272,11 +250,11 @@ "line": 1, "column": 21 } - } + }, + "type": "Identifier", + "value": "then" }, { - "type": "Punctuator", - "value": "(", "range": [ 21, 22 @@ -290,11 +268,11 @@ "line": 1, "column": 22 } - } + }, + "type": "Punctuator", + "value": "(" }, { - "type": "Identifier", - "value": "doThis", "range": [ 22, 28 @@ -308,11 +286,11 @@ "line": 1, "column": 28 } - } + }, + "type": "Identifier", + "value": "doThis" }, { - "type": "Punctuator", - "value": ")", "range": [ 28, 29 @@ -326,11 +304,11 @@ "line": 1, "column": 29 } - } + }, + "type": "Punctuator", + "value": ")" }, { - "type": "Punctuator", - "value": ";", "range": [ 29, 30 @@ -344,7 +322,9 @@ "line": 1, "column": 30 } - } + }, + "type": "Punctuator", + "value": ";" } ] } \ No newline at end of file diff --git a/test/fixtures/es2018/dynamic-import/import-call-template.tree.json b/test/fixtures/es2018/dynamic-import/import-call-template.tree.json index 8b31c6281..604532184 100644 --- a/test/fixtures/es2018/dynamic-import/import-call-template.tree.json +++ b/test/fixtures/es2018/dynamic-import/import-call-template.tree.json @@ -1,136 +1,170 @@ { + "range": [ + 0, + 44 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 44 + } + }, "type": "Program", "body": [ { + "range": [ + 0, + 44 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 44 + } + }, "type": "ExpressionStatement", "expression": { + "range": [ + 0, + 43 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 43 + } + }, "type": "CallExpression", "callee": { + "range": [ + 0, + 30 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 30 + } + }, "type": "MemberExpression", "computed": false, "object": { - "type": "CallExpression", - "callee": { - "type": "Import", + "range": [ + 0, + 25 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 25 + } + }, + "type": "Import", + "source": { "range": [ - 0, - 6 + 7, + 24 ], "loc": { "start": { "line": 1, - "column": 0 + "column": 7 }, "end": { "line": 1, - "column": 6 + "column": 24 } - } - }, - "arguments": [ - { - "type": "TemplateLiteral", - "quasis": [ - { - "type": "TemplateElement", - "value": { - "raw": "lib/", - "cooked": "lib/" + }, + "type": "TemplateLiteral", + "quasis": [ + { + "range": [ + 7, + 14 + ], + "loc": { + "start": { + "line": 1, + "column": 7 }, - "tail": false, - "range": [ - 7, - 14 - ], - "loc": { - "start": { - "line": 1, - "column": 7 - }, - "end": { - "line": 1, - "column": 14 - } + "end": { + "line": 1, + "column": 14 } }, - { - "type": "TemplateElement", - "value": { - "raw": ".js", - "cooked": ".js" + "type": "TemplateElement", + "value": { + "raw": "lib/", + "cooked": "lib/" + }, + "tail": false + }, + { + "range": [ + 19, + 24 + ], + "loc": { + "start": { + "line": 1, + "column": 19 }, - "tail": true, - "range": [ - 19, - 24 - ], - "loc": { - "start": { - "line": 1, - "column": 19 - }, - "end": { - "line": 1, - "column": 24 - } + "end": { + "line": 1, + "column": 24 } - } - ], - "expressions": [ - { - "type": "Identifier", - "name": "fname", - "range": [ - 14, - 19 - ], - "loc": { - "start": { - "line": 1, - "column": 14 - }, - "end": { - "line": 1, - "column": 19 - } + }, + "type": "TemplateElement", + "value": { + "raw": ".js", + "cooked": ".js" + }, + "tail": true + } + ], + "expressions": [ + { + "range": [ + 14, + 19 + ], + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 19 } - } - ], - "range": [ - 7, - 24 - ], - "loc": { - "start": { - "line": 1, - "column": 7 }, - "end": { - "line": 1, - "column": 24 - } + "type": "Identifier", + "name": "fname" } - } - ], - "optional": false, - "range": [ - 0, - 25 - ], - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 1, - "column": 25 - } + ] } }, "property": { - "type": "Identifier", - "name": "then", "range": [ 26, 30 @@ -144,28 +178,14 @@ "line": 1, "column": 30 } - } - }, - "optional": false, - "range": [ - 0, - 30 - ], - "loc": { - "start": { - "line": 1, - "column": 0 }, - "end": { - "line": 1, - "column": 30 - } - } + "type": "Identifier", + "name": "then" + }, + "optional": false }, "arguments": [ { - "type": "Identifier", - "name": "doSomething", "range": [ 31, 42 @@ -179,60 +199,18 @@ "line": 1, "column": 42 } - } + }, + "type": "Identifier", + "name": "doSomething" } ], - "optional": false, - "range": [ - 0, - 43 - ], - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 1, - "column": 43 - } - } - }, - "range": [ - 0, - 44 - ], - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 1, - "column": 44 - } + "optional": false } } ], "sourceType": "script", - "range": [ - 0, - 44 - ], - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 1, - "column": 44 - } - }, "tokens": [ { - "type": "Keyword", - "value": "import", "range": [ 0, 6 @@ -246,11 +224,11 @@ "line": 1, "column": 6 } - } + }, + "type": "Keyword", + "value": "import" }, { - "type": "Punctuator", - "value": "(", "range": [ 6, 7 @@ -264,11 +242,11 @@ "line": 1, "column": 7 } - } + }, + "type": "Punctuator", + "value": "(" }, { - "type": "Template", - "value": "`lib/${", "range": [ 7, 14 @@ -282,11 +260,11 @@ "line": 1, "column": 14 } - } + }, + "type": "Template", + "value": "`lib/${" }, { - "type": "Identifier", - "value": "fname", "range": [ 14, 19 @@ -300,11 +278,11 @@ "line": 1, "column": 19 } - } + }, + "type": "Identifier", + "value": "fname" }, { - "type": "Template", - "value": "}.js`", "range": [ 19, 24 @@ -318,11 +296,11 @@ "line": 1, "column": 24 } - } + }, + "type": "Template", + "value": "}.js`" }, { - "type": "Punctuator", - "value": ")", "range": [ 24, 25 @@ -336,11 +314,11 @@ "line": 1, "column": 25 } - } + }, + "type": "Punctuator", + "value": ")" }, { - "type": "Punctuator", - "value": ".", "range": [ 25, 26 @@ -354,11 +332,11 @@ "line": 1, "column": 26 } - } + }, + "type": "Punctuator", + "value": "." }, { - "type": "Identifier", - "value": "then", "range": [ 26, 30 @@ -372,11 +350,11 @@ "line": 1, "column": 30 } - } + }, + "type": "Identifier", + "value": "then" }, { - "type": "Punctuator", - "value": "(", "range": [ 30, 31 @@ -390,11 +368,11 @@ "line": 1, "column": 31 } - } + }, + "type": "Punctuator", + "value": "(" }, { - "type": "Identifier", - "value": "doSomething", "range": [ 31, 42 @@ -408,11 +386,11 @@ "line": 1, "column": 42 } - } + }, + "type": "Identifier", + "value": "doSomething" }, { - "type": "Punctuator", - "value": ")", "range": [ 42, 43 @@ -426,11 +404,11 @@ "line": 1, "column": 43 } - } + }, + "type": "Punctuator", + "value": ")" }, { - "type": "Punctuator", - "value": ";", "range": [ 43, 44 @@ -444,7 +422,9 @@ "line": 1, "column": 44 } - } + }, + "type": "Punctuator", + "value": ";" } ] } \ No newline at end of file diff --git a/test/fixtures/es2018/dynamic-import/import-call-var.tree.json b/test/fixtures/es2018/dynamic-import/import-call-var.tree.json index b7e024bd5..e67bd26e2 100644 --- a/test/fixtures/es2018/dynamic-import/import-call-var.tree.json +++ b/test/fixtures/es2018/dynamic-import/import-call-var.tree.json @@ -1,14 +1,54 @@ { + "range": [ + 0, + 49 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 25 + } + }, "type": "Program", "body": [ { + "range": [ + 0, + 23 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 23 + } + }, "type": "VariableDeclaration", "declarations": [ { + "range": [ + 6, + 22 + ], + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 22 + } + }, "type": "VariableDeclarator", "id": { - "type": "Identifier", - "name": "m", "range": [ 6, 7 @@ -22,12 +62,11 @@ "line": 1, "column": 7 } - } + }, + "type": "Identifier", + "name": "m" }, "init": { - "type": "Literal", - "value": "library.js", - "raw": "'library.js'", "range": [ 10, 22 @@ -41,87 +80,65 @@ "line": 1, "column": 22 } - } - }, - "range": [ - 6, - 22 - ], - "loc": { - "start": { - "line": 1, - "column": 6 }, - "end": { - "line": 1, - "column": 22 - } + "type": "Literal", + "value": "library.js", + "raw": "'library.js'" } } ], - "kind": "const", + "kind": "const" + }, + { "range": [ - 0, - 23 + 24, + 49 ], "loc": { "start": { - "line": 1, + "line": 2, "column": 0 }, "end": { - "line": 1, - "column": 23 + "line": 2, + "column": 25 } - } - }, - { + }, "type": "ExpressionStatement", "expression": { + "range": [ + 24, + 48 + ], + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 24 + } + }, "type": "CallExpression", "callee": { + "range": [ + 24, + 38 + ], + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 14 + } + }, "type": "MemberExpression", "computed": false, "object": { - "type": "CallExpression", - "callee": { - "type": "Import", - "range": [ - 24, - 30 - ], - "loc": { - "start": { - "line": 2, - "column": 0 - }, - "end": { - "line": 2, - "column": 6 - } - } - }, - "arguments": [ - { - "type": "Identifier", - "name": "m", - "range": [ - 31, - 32 - ], - "loc": { - "start": { - "line": 2, - "column": 7 - }, - "end": { - "line": 2, - "column": 8 - } - } - } - ], - "optional": false, "range": [ 24, 33 @@ -135,11 +152,28 @@ "line": 2, "column": 9 } + }, + "type": "Import", + "source": { + "range": [ + 31, + 32 + ], + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 8 + } + }, + "type": "Identifier", + "name": "m" } }, "property": { - "type": "Identifier", - "name": "then", "range": [ 34, 38 @@ -153,32 +187,32 @@ "line": 2, "column": 14 } - } - }, - "optional": false, - "range": [ - 24, - 38 - ], - "loc": { - "start": { - "line": 2, - "column": 0 }, - "end": { - "line": 2, - "column": 14 - } - } + "type": "Identifier", + "name": "then" + }, + "optional": false }, "arguments": [ { + "range": [ + 39, + 47 + ], + "loc": { + "start": { + "line": 2, + "column": 15 + }, + "end": { + "line": 2, + "column": 23 + } + }, "type": "ArrowFunctionExpression", "id": null, "params": [], "body": { - "type": "BlockStatement", - "body": [], "range": [ 45, 47 @@ -192,78 +226,22 @@ "line": 2, "column": 23 } - } + }, + "type": "BlockStatement", + "body": [] }, "generator": false, "expression": false, - "async": false, - "range": [ - 39, - 47 - ], - "loc": { - "start": { - "line": 2, - "column": 15 - }, - "end": { - "line": 2, - "column": 23 - } - } + "async": false } ], - "optional": false, - "range": [ - 24, - 48 - ], - "loc": { - "start": { - "line": 2, - "column": 0 - }, - "end": { - "line": 2, - "column": 24 - } - } - }, - "range": [ - 24, - 49 - ], - "loc": { - "start": { - "line": 2, - "column": 0 - }, - "end": { - "line": 2, - "column": 25 - } + "optional": false } } ], "sourceType": "script", - "range": [ - 0, - 49 - ], - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 2, - "column": 25 - } - }, "tokens": [ { - "type": "Keyword", - "value": "const", "range": [ 0, 5 @@ -277,11 +255,11 @@ "line": 1, "column": 5 } - } + }, + "type": "Keyword", + "value": "const" }, { - "type": "Identifier", - "value": "m", "range": [ 6, 7 @@ -295,11 +273,11 @@ "line": 1, "column": 7 } - } + }, + "type": "Identifier", + "value": "m" }, { - "type": "Punctuator", - "value": "=", "range": [ 8, 9 @@ -313,11 +291,11 @@ "line": 1, "column": 9 } - } + }, + "type": "Punctuator", + "value": "=" }, { - "type": "String", - "value": "'library.js'", "range": [ 10, 22 @@ -331,11 +309,11 @@ "line": 1, "column": 22 } - } + }, + "type": "String", + "value": "'library.js'" }, { - "type": "Punctuator", - "value": ";", "range": [ 22, 23 @@ -349,11 +327,11 @@ "line": 1, "column": 23 } - } + }, + "type": "Punctuator", + "value": ";" }, { - "type": "Keyword", - "value": "import", "range": [ 24, 30 @@ -367,11 +345,11 @@ "line": 2, "column": 6 } - } + }, + "type": "Keyword", + "value": "import" }, { - "type": "Punctuator", - "value": "(", "range": [ 30, 31 @@ -385,11 +363,11 @@ "line": 2, "column": 7 } - } + }, + "type": "Punctuator", + "value": "(" }, { - "type": "Identifier", - "value": "m", "range": [ 31, 32 @@ -403,11 +381,11 @@ "line": 2, "column": 8 } - } + }, + "type": "Identifier", + "value": "m" }, { - "type": "Punctuator", - "value": ")", "range": [ 32, 33 @@ -421,11 +399,11 @@ "line": 2, "column": 9 } - } + }, + "type": "Punctuator", + "value": ")" }, { - "type": "Punctuator", - "value": ".", "range": [ 33, 34 @@ -439,11 +417,11 @@ "line": 2, "column": 10 } - } + }, + "type": "Punctuator", + "value": "." }, { - "type": "Identifier", - "value": "then", "range": [ 34, 38 @@ -457,11 +435,11 @@ "line": 2, "column": 14 } - } + }, + "type": "Identifier", + "value": "then" }, { - "type": "Punctuator", - "value": "(", "range": [ 38, 39 @@ -475,11 +453,11 @@ "line": 2, "column": 15 } - } + }, + "type": "Punctuator", + "value": "(" }, { - "type": "Punctuator", - "value": "(", "range": [ 39, 40 @@ -493,11 +471,11 @@ "line": 2, "column": 16 } - } + }, + "type": "Punctuator", + "value": "(" }, { - "type": "Punctuator", - "value": ")", "range": [ 40, 41 @@ -511,11 +489,11 @@ "line": 2, "column": 17 } - } + }, + "type": "Punctuator", + "value": ")" }, { - "type": "Punctuator", - "value": "=>", "range": [ 42, 44 @@ -529,11 +507,11 @@ "line": 2, "column": 20 } - } + }, + "type": "Punctuator", + "value": "=>" }, { - "type": "Punctuator", - "value": "{", "range": [ 45, 46 @@ -547,11 +525,11 @@ "line": 2, "column": 22 } - } + }, + "type": "Punctuator", + "value": "{" }, { - "type": "Punctuator", - "value": "}", "range": [ 46, 47 @@ -565,11 +543,11 @@ "line": 2, "column": 23 } - } + }, + "type": "Punctuator", + "value": "}" }, { - "type": "Punctuator", - "value": ")", "range": [ 47, 48 @@ -583,11 +561,11 @@ "line": 2, "column": 24 } - } + }, + "type": "Punctuator", + "value": ")" }, { - "type": "Punctuator", - "value": ";", "range": [ 48, 49 @@ -601,7 +579,9 @@ "line": 2, "column": 25 } - } + }, + "type": "Punctuator", + "value": ";" } ] } \ No newline at end of file diff --git a/test/fixtures/es2018/dynamic-import/invalid-import-call-many-arguments.failure.json b/test/fixtures/es2018/dynamic-import/invalid-import-call-many-arguments.failure.json new file mode 100644 index 000000000..4cd0c8583 --- /dev/null +++ b/test/fixtures/es2018/dynamic-import/invalid-import-call-many-arguments.failure.json @@ -0,0 +1 @@ +{"index":8,"lineNumber":1,"column":9,"message":"Error: Line 1: Unexpected token ,","description":"Unexpected token ,"} \ No newline at end of file diff --git a/test/fixtures/es2018/dynamic-import/invalid-import-call-many-arguments.tree.json b/test/fixtures/es2018/dynamic-import/invalid-import-call-many-arguments.tree.json deleted file mode 100644 index b3ba798ce..000000000 --- a/test/fixtures/es2018/dynamic-import/invalid-import-call-many-arguments.tree.json +++ /dev/null @@ -1,411 +0,0 @@ -{ - "type": "Program", - "body": [ - { - "type": "ExpressionStatement", - "expression": { - "type": "CallExpression", - "callee": { - "type": "MemberExpression", - "computed": false, - "object": { - "type": "CallExpression", - "callee": { - "type": "Import", - "range": [ - 0, - 6 - ], - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 1, - "column": 6 - } - } - }, - "arguments": [ - { - "type": "Identifier", - "name": "x", - "range": [ - 7, - 8 - ], - "loc": { - "start": { - "line": 1, - "column": 7 - }, - "end": { - "line": 1, - "column": 8 - } - } - }, - { - "type": "Identifier", - "name": "y", - "range": [ - 10, - 11 - ], - "loc": { - "start": { - "line": 1, - "column": 10 - }, - "end": { - "line": 1, - "column": 11 - } - } - } - ], - "optional": false, - "range": [ - 0, - 12 - ], - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 1, - "column": 12 - } - } - }, - "property": { - "type": "Identifier", - "name": "then", - "range": [ - 13, - 17 - ], - "loc": { - "start": { - "line": 1, - "column": 13 - }, - "end": { - "line": 1, - "column": 17 - } - } - }, - "optional": false, - "range": [ - 0, - 17 - ], - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 1, - "column": 17 - } - } - }, - "arguments": [ - { - "type": "Identifier", - "name": "z", - "range": [ - 18, - 19 - ], - "loc": { - "start": { - "line": 1, - "column": 18 - }, - "end": { - "line": 1, - "column": 19 - } - } - } - ], - "optional": false, - "range": [ - 0, - 20 - ], - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 1, - "column": 20 - } - } - }, - "range": [ - 0, - 21 - ], - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 1, - "column": 21 - } - } - } - ], - "sourceType": "script", - "range": [ - 0, - 21 - ], - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 1, - "column": 21 - } - }, - "tokens": [ - { - "type": "Keyword", - "value": "import", - "range": [ - 0, - 6 - ], - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 1, - "column": 6 - } - } - }, - { - "type": "Punctuator", - "value": "(", - "range": [ - 6, - 7 - ], - "loc": { - "start": { - "line": 1, - "column": 6 - }, - "end": { - "line": 1, - "column": 7 - } - } - }, - { - "type": "Identifier", - "value": "x", - "range": [ - 7, - 8 - ], - "loc": { - "start": { - "line": 1, - "column": 7 - }, - "end": { - "line": 1, - "column": 8 - } - } - }, - { - "type": "Punctuator", - "value": ",", - "range": [ - 8, - 9 - ], - "loc": { - "start": { - "line": 1, - "column": 8 - }, - "end": { - "line": 1, - "column": 9 - } - } - }, - { - "type": "Identifier", - "value": "y", - "range": [ - 10, - 11 - ], - "loc": { - "start": { - "line": 1, - "column": 10 - }, - "end": { - "line": 1, - "column": 11 - } - } - }, - { - "type": "Punctuator", - "value": ")", - "range": [ - 11, - 12 - ], - "loc": { - "start": { - "line": 1, - "column": 11 - }, - "end": { - "line": 1, - "column": 12 - } - } - }, - { - "type": "Punctuator", - "value": ".", - "range": [ - 12, - 13 - ], - "loc": { - "start": { - "line": 1, - "column": 12 - }, - "end": { - "line": 1, - "column": 13 - } - } - }, - { - "type": "Identifier", - "value": "then", - "range": [ - 13, - 17 - ], - "loc": { - "start": { - "line": 1, - "column": 13 - }, - "end": { - "line": 1, - "column": 17 - } - } - }, - { - "type": "Punctuator", - "value": "(", - "range": [ - 17, - 18 - ], - "loc": { - "start": { - "line": 1, - "column": 17 - }, - "end": { - "line": 1, - "column": 18 - } - } - }, - { - "type": "Identifier", - "value": "z", - "range": [ - 18, - 19 - ], - "loc": { - "start": { - "line": 1, - "column": 18 - }, - "end": { - "line": 1, - "column": 19 - } - } - }, - { - "type": "Punctuator", - "value": ")", - "range": [ - 19, - 20 - ], - "loc": { - "start": { - "line": 1, - "column": 19 - }, - "end": { - "line": 1, - "column": 20 - } - } - }, - { - "type": "Punctuator", - "value": ";", - "range": [ - 20, - 21 - ], - "loc": { - "start": { - "line": 1, - "column": 20 - }, - "end": { - "line": 1, - "column": 21 - } - } - } - ], - "errors": [ - { - "index": 12, - "lineNumber": 1, - "column": 13, - "message": "Error: Line 1: Unexpected token" - } - ] -} \ No newline at end of file diff --git a/test/fixtures/es2018/dynamic-import/invalid-import-call-no-argument.failure.json b/test/fixtures/es2018/dynamic-import/invalid-import-call-no-argument.failure.json new file mode 100644 index 000000000..d364bcf23 --- /dev/null +++ b/test/fixtures/es2018/dynamic-import/invalid-import-call-no-argument.failure.json @@ -0,0 +1 @@ +{"index":7,"lineNumber":1,"column":8,"message":"Error: Line 1: Unexpected token )","description":"Unexpected token )"} \ No newline at end of file diff --git a/test/fixtures/es2018/dynamic-import/invalid-import-call-no-argument.tree.json b/test/fixtures/es2018/dynamic-import/invalid-import-call-no-argument.tree.json deleted file mode 100644 index f16eb71fc..000000000 --- a/test/fixtures/es2018/dynamic-import/invalid-import-call-no-argument.tree.json +++ /dev/null @@ -1,320 +0,0 @@ -{ - "type": "Program", - "body": [ - { - "type": "ExpressionStatement", - "expression": { - "type": "CallExpression", - "callee": { - "type": "MemberExpression", - "computed": false, - "object": { - "type": "CallExpression", - "callee": { - "type": "Import", - "range": [ - 0, - 6 - ], - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 1, - "column": 6 - } - } - }, - "arguments": [], - "optional": false, - "range": [ - 0, - 8 - ], - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 1, - "column": 8 - } - } - }, - "property": { - "type": "Identifier", - "name": "then", - "range": [ - 9, - 13 - ], - "loc": { - "start": { - "line": 1, - "column": 9 - }, - "end": { - "line": 1, - "column": 13 - } - } - }, - "optional": false, - "range": [ - 0, - 13 - ], - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 1, - "column": 13 - } - } - }, - "arguments": [ - { - "type": "Identifier", - "name": "doThat", - "range": [ - 14, - 20 - ], - "loc": { - "start": { - "line": 1, - "column": 14 - }, - "end": { - "line": 1, - "column": 20 - } - } - } - ], - "optional": false, - "range": [ - 0, - 21 - ], - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 1, - "column": 21 - } - } - }, - "range": [ - 0, - 22 - ], - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 1, - "column": 22 - } - } - } - ], - "sourceType": "script", - "range": [ - 0, - 22 - ], - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 1, - "column": 22 - } - }, - "tokens": [ - { - "type": "Keyword", - "value": "import", - "range": [ - 0, - 6 - ], - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 1, - "column": 6 - } - } - }, - { - "type": "Punctuator", - "value": "(", - "range": [ - 6, - 7 - ], - "loc": { - "start": { - "line": 1, - "column": 6 - }, - "end": { - "line": 1, - "column": 7 - } - } - }, - { - "type": "Punctuator", - "value": ")", - "range": [ - 7, - 8 - ], - "loc": { - "start": { - "line": 1, - "column": 7 - }, - "end": { - "line": 1, - "column": 8 - } - } - }, - { - "type": "Punctuator", - "value": ".", - "range": [ - 8, - 9 - ], - "loc": { - "start": { - "line": 1, - "column": 8 - }, - "end": { - "line": 1, - "column": 9 - } - } - }, - { - "type": "Identifier", - "value": "then", - "range": [ - 9, - 13 - ], - "loc": { - "start": { - "line": 1, - "column": 9 - }, - "end": { - "line": 1, - "column": 13 - } - } - }, - { - "type": "Punctuator", - "value": "(", - "range": [ - 13, - 14 - ], - "loc": { - "start": { - "line": 1, - "column": 13 - }, - "end": { - "line": 1, - "column": 14 - } - } - }, - { - "type": "Identifier", - "value": "doThat", - "range": [ - 14, - 20 - ], - "loc": { - "start": { - "line": 1, - "column": 14 - }, - "end": { - "line": 1, - "column": 20 - } - } - }, - { - "type": "Punctuator", - "value": ")", - "range": [ - 20, - 21 - ], - "loc": { - "start": { - "line": 1, - "column": 20 - }, - "end": { - "line": 1, - "column": 21 - } - } - }, - { - "type": "Punctuator", - "value": ";", - "range": [ - 21, - 22 - ], - "loc": { - "start": { - "line": 1, - "column": 21 - }, - "end": { - "line": 1, - "column": 22 - } - } - } - ], - "errors": [ - { - "index": 8, - "lineNumber": 1, - "column": 9, - "message": "Error: Line 1: Unexpected token" - } - ] -} \ No newline at end of file diff --git a/test/fixtures/es2018/dynamic-import/loader-using-import.tree.json b/test/fixtures/es2018/dynamic-import/loader-using-import.tree.json index eb7d45a8b..d9b31c9cc 100644 --- a/test/fixtures/es2018/dynamic-import/loader-using-import.tree.json +++ b/test/fixtures/es2018/dynamic-import/loader-using-import.tree.json @@ -1,11 +1,37 @@ { + "range": [ + 0, + 46 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 46 + } + }, "type": "Program", "body": [ { + "range": [ + 0, + 46 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 46 + } + }, "type": "FunctionDeclaration", "id": { - "type": "Identifier", - "name": "load", "range": [ 9, 13 @@ -19,12 +45,12 @@ "line": 1, "column": 13 } - } + }, + "type": "Identifier", + "name": "load" }, "params": [ { - "type": "Identifier", - "name": "f", "range": [ 14, 15 @@ -38,175 +64,127 @@ "line": 1, "column": 15 } - } + }, + "type": "Identifier", + "name": "f" } ], "body": { + "range": [ + 17, + 46 + ], + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 46 + } + }, "type": "BlockStatement", "body": [ { + "range": [ + 19, + 45 + ], + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 45 + } + }, "type": "ReturnStatement", "argument": { - "type": "CallExpression", - "callee": { - "type": "Import", + "range": [ + 26, + 44 + ], + "loc": { + "start": { + "line": 1, + "column": 26 + }, + "end": { + "line": 1, + "column": 44 + } + }, + "type": "Import", + "source": { "range": [ - 26, - 32 + 33, + 43 ], "loc": { "start": { "line": 1, - "column": 26 + "column": 33 }, "end": { "line": 1, - "column": 32 + "column": 43 } - } - }, - "arguments": [ - { - "type": "BinaryExpression", - "operator": "+", - "left": { - "type": "Literal", - "value": "lib/", - "raw": "'lib/'", - "range": [ - 33, - 39 - ], - "loc": { - "start": { - "line": 1, - "column": 33 - }, - "end": { - "line": 1, - "column": 39 - } - } - }, - "right": { - "type": "Identifier", - "name": "f", - "range": [ - 42, - 43 - ], - "loc": { - "start": { - "line": 1, - "column": 42 - }, - "end": { - "line": 1, - "column": 43 - } + }, + "type": "BinaryExpression", + "operator": "+", + "left": { + "range": [ + 33, + 39 + ], + "loc": { + "start": { + "line": 1, + "column": 33 + }, + "end": { + "line": 1, + "column": 39 } }, + "type": "Literal", + "value": "lib/", + "raw": "'lib/'" + }, + "right": { "range": [ - 33, + 42, 43 ], "loc": { "start": { "line": 1, - "column": 33 + "column": 42 }, "end": { "line": 1, "column": 43 } - } - } - ], - "optional": false, - "range": [ - 26, - 44 - ], - "loc": { - "start": { - "line": 1, - "column": 26 - }, - "end": { - "line": 1, - "column": 44 + }, + "type": "Identifier", + "name": "f" } } - }, - "range": [ - 19, - 45 - ], - "loc": { - "start": { - "line": 1, - "column": 19 - }, - "end": { - "line": 1, - "column": 45 - } } } - ], - "range": [ - 17, - 46 - ], - "loc": { - "start": { - "line": 1, - "column": 17 - }, - "end": { - "line": 1, - "column": 46 - } - } + ] }, "generator": false, "expression": false, - "async": false, - "range": [ - 0, - 46 - ], - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 1, - "column": 46 - } - } + "async": false } ], "sourceType": "script", - "range": [ - 0, - 46 - ], - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 1, - "column": 46 - } - }, "tokens": [ { - "type": "Keyword", - "value": "function", "range": [ 0, 8 @@ -220,11 +198,11 @@ "line": 1, "column": 8 } - } + }, + "type": "Keyword", + "value": "function" }, { - "type": "Identifier", - "value": "load", "range": [ 9, 13 @@ -238,11 +216,11 @@ "line": 1, "column": 13 } - } + }, + "type": "Identifier", + "value": "load" }, { - "type": "Punctuator", - "value": "(", "range": [ 13, 14 @@ -256,11 +234,11 @@ "line": 1, "column": 14 } - } + }, + "type": "Punctuator", + "value": "(" }, { - "type": "Identifier", - "value": "f", "range": [ 14, 15 @@ -274,11 +252,11 @@ "line": 1, "column": 15 } - } + }, + "type": "Identifier", + "value": "f" }, { - "type": "Punctuator", - "value": ")", "range": [ 15, 16 @@ -292,11 +270,11 @@ "line": 1, "column": 16 } - } + }, + "type": "Punctuator", + "value": ")" }, { - "type": "Punctuator", - "value": "{", "range": [ 17, 18 @@ -310,11 +288,11 @@ "line": 1, "column": 18 } - } + }, + "type": "Punctuator", + "value": "{" }, { - "type": "Keyword", - "value": "return", "range": [ 19, 25 @@ -328,11 +306,11 @@ "line": 1, "column": 25 } - } + }, + "type": "Keyword", + "value": "return" }, { - "type": "Keyword", - "value": "import", "range": [ 26, 32 @@ -346,11 +324,11 @@ "line": 1, "column": 32 } - } + }, + "type": "Keyword", + "value": "import" }, { - "type": "Punctuator", - "value": "(", "range": [ 32, 33 @@ -364,11 +342,11 @@ "line": 1, "column": 33 } - } + }, + "type": "Punctuator", + "value": "(" }, { - "type": "String", - "value": "'lib/'", "range": [ 33, 39 @@ -382,11 +360,11 @@ "line": 1, "column": 39 } - } + }, + "type": "String", + "value": "'lib/'" }, { - "type": "Punctuator", - "value": "+", "range": [ 40, 41 @@ -400,11 +378,11 @@ "line": 1, "column": 41 } - } + }, + "type": "Punctuator", + "value": "+" }, { - "type": "Identifier", - "value": "f", "range": [ 42, 43 @@ -418,11 +396,11 @@ "line": 1, "column": 43 } - } + }, + "type": "Identifier", + "value": "f" }, { - "type": "Punctuator", - "value": ")", "range": [ 43, 44 @@ -436,11 +414,11 @@ "line": 1, "column": 44 } - } + }, + "type": "Punctuator", + "value": ")" }, { - "type": "Punctuator", - "value": "}", "range": [ 45, 46 @@ -454,7 +432,9 @@ "line": 1, "column": 46 } - } + }, + "type": "Punctuator", + "value": "}" } ] } \ No newline at end of file From 571b2359da129c70942bb601573084c84b4c190e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jochen=20K=C3=BChner?= Date: Wed, 3 Nov 2021 20:01:30 +0100 Subject: [PATCH 2/2] support bigint and numeric seperators --- src/character.ts | 9 ++++++- src/messages.ts | 2 ++ src/nodes.ts | 13 +++++++++ src/parser.ts | 17 +++++++++--- src/scanner.ts | 70 ++++++++++++++++++++++++++---------------------- src/token.ts | 4 ++- 6 files changed, 77 insertions(+), 38 deletions(-) diff --git a/src/character.ts b/src/character.ts index 0520ddc3b..29babdb71 100644 --- a/src/character.ts +++ b/src/character.ts @@ -64,8 +64,15 @@ export const Character = { (cp >= 0x61 && cp <= 0x66); // a..f }, + isHexDigitChar(ch: string): ch is '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' | 'a'| 'b'| 'c'| 'd'| 'e'| 'f'| 'A'| 'B'| 'C'| 'D'| 'E'| 'F' { + return ch.length === 1 && Character.isHexDigit(ch.charCodeAt(0)); + }, + isOctalDigit(cp: number): boolean { return (cp >= 0x30 && cp <= 0x37); // 0..7 - } + }, + isOctalDigitChar(ch: string): ch is '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' { + return ch.length === 1 && Character.isOctalDigit(ch.charCodeAt(0)); + } }; diff --git a/src/messages.ts b/src/messages.ts index 4b76c2589..2f26388fa 100644 --- a/src/messages.ts +++ b/src/messages.ts @@ -35,6 +35,8 @@ export const Messages = { NewlineAfterThrow: 'Illegal newline after throw', NoAsAfterImportNamespace: 'Unexpected token', NoCatchOrFinally: 'Missing catch or finally after try', + NumericSeperatorOneUnderscore: 'Numeric separator must be exactly one underscore', + NumericSeperatorNotAllowedHere: 'Numeric separator is not allowed here', ParameterAfterRestParameter: 'Rest parameter must be last formal parameter', PropertyAfterRestProperty: 'Unexpected token', Redeclaration: '%0 \'%1\' has already been declared', diff --git a/src/nodes.ts b/src/nodes.ts index 4320f54fc..80549d35a 100644 --- a/src/nodes.ts +++ b/src/nodes.ts @@ -154,6 +154,19 @@ export class AwaitExpression { } } +export class BigIntLiteral { + readonly type: string; + readonly value: null | string; //should be bigint + readonly raw: string; + readonly bigint: string; + constructor(value: null | string /*should be bigint*/, raw: string, bigint: string) { + this.type = Syntax.Literal; + this.value = value; + this.raw = raw; + this.bigint = bigint; + } +} + export class BinaryExpression { readonly type: string; readonly operator: string; diff --git a/src/parser.ts b/src/parser.ts index 81d4e81b6..68d18b6e8 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -609,7 +609,7 @@ export class Parser { const node = this.createNode(); let expr: Node.Expression; - let token, raw; + let token: RawToken, raw; switch (this.lookahead.type) { case Token.Identifier: @@ -620,6 +620,7 @@ export class Parser { break; case Token.NumericLiteral: + case Token.BigIntLiteral: case Token.StringLiteral: if (this.context.strict && this.lookahead.octal) { this.tolerateUnexpectedToken(this.lookahead, Messages.StrictOctalLiteral); @@ -628,7 +629,10 @@ export class Parser { this.context.isBindingElement = false; token = this.nextToken(); raw = this.getTokenRaw(token); - expr = this.finalize(node, new Node.Literal(token.value, raw)); + if (token.type == Token.BigIntLiteral) + expr = this.finalize(node, new Node.BigIntLiteral(token.value as string, raw, token.value.toString())); + else + expr = this.finalize(node, new Node.Literal(token.value as string, raw)); break; case Token.BooleanLiteral: @@ -670,7 +674,7 @@ export class Parser { this.scanner.index = this.startMarker.index; token = this.nextRegexToken(); raw = this.getTokenRaw(token); - expr = this.finalize(node, new Node.RegexLiteral(token.regex as RegExp, raw, token.pattern, token.flags)); + expr = this.finalize(node, new Node.RegexLiteral(token.regex as RegExp, raw, token.pattern as string, token.flags as string)); break; default: expr = this.throwUnexpectedToken(this.nextToken()); @@ -803,11 +807,16 @@ export class Parser { switch (token.type) { case Token.StringLiteral: case Token.NumericLiteral: + case Token.BigIntLiteral: if (this.context.strict && token.octal) { this.tolerateUnexpectedToken(token, Messages.StrictOctalLiteral); } const raw = this.getTokenRaw(token); - key = this.finalize(node, new Node.Literal(token.value as string, raw)); + if (token.type === Token.BigIntLiteral) + key = this.finalize(node, new Node.BigIntLiteral(token.value as string, raw, token.value.toString())); + else + key = this.finalize(node, new Node.Literal(token.value as string, raw)); + break; case Token.Identifier: diff --git a/src/scanner.ts b/src/scanner.ts index 2a746e029..96fe1c2e3 100644 --- a/src/scanner.ts +++ b/src/scanner.ts @@ -35,7 +35,7 @@ type NotEscapeSequenceHead = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' export interface RawToken { type: Token; - value: string | number; + value: string | number; //add bigint pattern?: string; flags?: string; regex?: RegExp | null; @@ -686,14 +686,7 @@ export class Scanner { // https://tc39.github.io/ecma262/#sec-literals-numeric-literals private scanHexLiteral(start: number): RawToken { - let num = ''; - - while (!this.eof()) { - if (!Character.isHexDigit(this.source.charCodeAt(this.index))) { - break; - } - num += this.source[this.index++]; - } + let num = this.scanLiteralPart(Character.isHexDigitChar); if (num.length === 0) { this.throwUnexpectedToken(); @@ -714,16 +707,9 @@ export class Scanner { } private scanBinaryLiteral(start: number): RawToken { - let num = ''; let ch; - while (!this.eof()) { - ch = this.source[this.index]; - if (ch !== '0' && ch !== '1') { - break; - } - num += this.source[this.index++]; - } + let num = this.scanLiteralPart(c => c === '0' || c === '1'); if (num.length === 0) { // only 0b or 0B @@ -759,12 +745,7 @@ export class Scanner { ++this.index; } - while (!this.eof()) { - if (!Character.isOctalDigit(this.source.charCodeAt(this.index))) { - break; - } - num += this.source[this.index++]; - } + num += this.scanLiteralPart(Character.isOctalDigitChar); if (!octal && num.length === 0) { // only 0o or 0O @@ -802,6 +783,26 @@ export class Scanner { return true; } + private scanLiteralPart(check: (str: string) => boolean) { + let num = ''; + + if (this.source[this.index] === '_') + this.throwUnexpectedToken(Messages.NumericSeperatorNotAllowedHere); + + while (this.source[this.index] && (check(this.source[this.index]) || this.source[this.index] === '_')) { + if (this.source[this.index] !== '_') + num += this.source[this.index]; + this.index++; + if (this.source[this.index - 1] === '_' && this.source[this.index] === '_') + this.throwUnexpectedToken(Messages.NumericSeperatorOneUnderscore); + } + + if (this.source[this.index - 1] === '_') + this.throwUnexpectedToken(Messages.NumericSeperatorNotAllowedHere); + + return num; + } + private scanNumericLiteral(): RawToken { const start = this.index; let ch = this.source[start]; @@ -837,17 +838,14 @@ export class Scanner { } } - while (Character.isDecimalDigit(this.source.charCodeAt(this.index))) { - num += this.source[this.index++]; - } + this.index--; + num = this.scanLiteralPart(Character.isDecimalDigitChar); ch = this.source[this.index]; } if (ch === '.') { num += this.source[this.index++]; - while (Character.isDecimalDigit(this.source.charCodeAt(this.index))) { - num += this.source[this.index++]; - } + num += this.scanLiteralPart(Character.isDecimalDigitChar); ch = this.source[this.index]; } @@ -859,12 +857,20 @@ export class Scanner { num += this.source[this.index++]; } if (Character.isDecimalDigit(this.source.charCodeAt(this.index))) { - while (Character.isDecimalDigit(this.source.charCodeAt(this.index))) { - num += this.source[this.index++]; - } + num += this.scanLiteralPart(Character.isDecimalDigitChar); } else { this.throwUnexpectedToken(); } + } else if (ch === 'n') { + this.index++; + return { + type: Token.BigIntLiteral, + value: num, // should be BigInt(num), + lineNumber: this.lineNumber, + lineStart: this.lineStart, + start: start, + end: this.index + }; } if (Character.isIdentifierStart(this.source.charCodeAt(this.index))) { diff --git a/src/token.ts b/src/token.ts index d5e1bc40e..1af3a3b51 100644 --- a/src/token.ts +++ b/src/token.ts @@ -8,7 +8,8 @@ export const enum Token { Punctuator, StringLiteral, RegularExpression, - Template + Template, + BigIntLiteral } export const TokenName = {}; @@ -22,3 +23,4 @@ TokenName[Token.Punctuator] = 'Punctuator'; TokenName[Token.StringLiteral] = 'String'; TokenName[Token.RegularExpression] = 'RegularExpression'; TokenName[Token.Template] = 'Template'; +TokenName[Token.BigIntLiteral] = 'BigIntLiteral';