From 7e1a7837b99b2ac0305344007217cff6a24567d0 Mon Sep 17 00:00:00 2001 From: Vivek Nayyar Date: Sun, 5 Jan 2020 16:45:34 +0100 Subject: [PATCH 01/63] :recycle: added basic support for module attributes and tests updated --- .../babel-parser/src/parser/expression.js | 20 ++ packages/babel-parser/src/parser/statement.js | 22 +++ .../interpreter-directive-import/output.json | 3 +- .../comments/regression/10892/output.json | 1 + .../sourcetype-unambiguous/flow/output.json | 3 +- .../module-import/output.json | 3 +- .../invalid-escape-seq-import/output.json | 3 +- .../output.json | 3 +- .../import-invalid-keyword-flow/output.json | 3 +- .../output.json | 3 +- .../import-invalid-keyword-typeof/output.json | 3 +- .../import-invalid-keyword/output.json | 3 +- .../es2015/uncategorised/291/output.json | 3 +- .../es2015/uncategorised/301/output.json | 3 +- .../es2015/uncategorised/88/output.json | 3 +- .../es2015/uncategorised/91/output.json | 3 +- .../es2015/uncategorised/92/output.json | 3 +- .../es2015/uncategorised/93/output.json | 3 +- .../es2015/uncategorised/94/output.json | 3 +- .../es2015/uncategorised/95/output.json | 3 +- .../es2015/uncategorised/97/output.json | 3 +- .../es2015/uncategorised/98/output.json | 3 +- .../output.json | 3 +- .../output.json | 3 +- .../import-default-as/output.json | 3 +- .../import-default/output.json | 3 +- .../import-jquery/output.json | 3 +- .../import-module/output.json | 3 +- .../import-named-as-specifier/output.json | 3 +- .../import-named-as-specifiers/output.json | 3 +- .../import-named-empty/output.json | 3 +- .../import-named-specifier/output.json | 3 +- .../import-named-specifiers-comma/output.json | 3 +- .../import-named-specifiers/output.json | 3 +- .../import-namespace-specifier/output.json | 3 +- .../import-null-as-nil/output.json | 3 +- .../import-module-attributes/input.js | 1 + .../import-module-attributes/options.json | 5 + .../valid-syntax-with-attributes/input.js | 1 + .../valid-syntax-with-attributes/options.json | 4 + .../valid-syntax-with-attributes/output.json | 176 ++++++++++++++++++ .../valid-syntax-without-attributes/input.js | 1 + .../options.json | 4 + .../output.json | 105 +++++++++++ .../flow/declare-module/import/output.json | 6 +- .../declare-module/invalid-import/output.json | 3 +- .../flow/sourcetype-script/import/output.json | 6 +- .../type-imports/import-type-2/output.json | 3 +- .../import-type-shorthand/output.json | 30 ++- .../flow/type-imports/import-type/output.json | 39 ++-- .../invalid-import-type-2/output.json | 3 +- .../invalid-import-type-3/output.json | 3 +- .../invalid-import-type-4/output.json | 3 +- .../invalid-import-type-as/output.json | 3 +- .../output.json | 3 +- .../output.json | 3 +- .../output.json | 3 +- .../invalid-import-type-shorthand/output.json | 3 +- .../invalid-import-type/output.json | 3 +- .../import/default-named/output.json | 3 +- .../import/default-star-2/output.json | 3 +- .../import/file-empty/output.json | 3 +- .../import/named-alias-2/output.json | 3 +- .../import/named-alias-3/output.json | 3 +- .../import/named-alias/output.json | 3 +- .../placeholders/import/named/output.json | 3 +- .../placeholders/import/star/output.json | 3 +- .../import/not-top-level/output.json | 3 +- .../output.json | 3 +- 69 files changed, 502 insertions(+), 81 deletions(-) create mode 100644 packages/babel-parser/test/fixtures/experimental/_no-plugin/import-module-attributes/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/_no-plugin/import-module-attributes/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes/output.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-without-attributes/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-without-attributes/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-without-attributes/output.json diff --git a/packages/babel-parser/src/parser/expression.js b/packages/babel-parser/src/parser/expression.js index 1ef6fcf82f96..45347ea713b4 100644 --- a/packages/babel-parser/src/parser/expression.js +++ b/packages/babel-parser/src/parser/expression.js @@ -1460,6 +1460,26 @@ export default class ExpressionParser extends LValParser { return this.finishNode(node, "TemplateLiteral"); } + parseKeyValuePairs() { + const propHash: any = Object.create(null); + const node = this.startNode(); + node.properties = []; + + do { + const prop = this.parseObjectMember(); + // $FlowIgnore + if (prop.shorthand) { + this.addExtra(prop, "shorthand", true); + } + + node.properties.push(prop); + } while (this.eat(tt.comma)); + if (!this.match(tt.eq) && propHash.start !== undefined) { + this.raise(propHash.start, "Redefinition of __proto__ property"); + } + return this.finishNode(node, "ObjectExpression"); + } + // Parse an object literal or binding pattern. parseObj( diff --git a/packages/babel-parser/src/parser/statement.js b/packages/babel-parser/src/parser/statement.js index be090db6edf9..6ad9ace8b953 100644 --- a/packages/babel-parser/src/parser/statement.js +++ b/packages/babel-parser/src/parser/statement.js @@ -2005,13 +2005,26 @@ export default class StatementParser extends ExpressionParser { // import '...' node.specifiers = []; if (!this.match(tt.string)) { + // check if we have a default import like + // import React from "react"; const hasDefault = this.maybeParseDefaultImportSpecifier(node); + /* we are checking if we do not have a default import, then it is obvious that we need named imports + * import { get } from "axios"; + * but if we do have a default import + * we need to check if we have a comma after that and + * that is where this || this.eat comes into play + */ const parseNext = !hasDefault || this.eat(tt.comma); + // if we do have to parse the next set of specifiers, we first check for start imports + // import React, * from "react"; const hasStar = parseNext && this.maybeParseStarImportSpecifier(node); + // now we check that if we need to parse the next imports + // but only if he/she is not importing * which means all if (parseNext && !hasStar) this.parseNamedImportSpecifiers(node); this.expectContextual("from"); } node.source = this.parseImportSource(); + node.attributes = this.maybeParseModuleAttributes(); this.semicolon(); return this.finishNode(node, "ImportDeclaration"); } @@ -2042,6 +2055,15 @@ export default class StatementParser extends ExpressionParser { node.specifiers.push(this.finishNode(specifier, type)); } + maybeParseModuleAttributes() { + if (this.match(tt._with) && !this.hasPlugin("moduleAttributes")) { + this.expectPlugin("moduleAttributes"); + } else if (this.eat(tt._with)) { + return this.parseKeyValuePairs(); + } + return []; + } + maybeParseDefaultImportSpecifier(node: N.ImportDeclaration): boolean { if (this.shouldParseDefaultImport(node)) { // import defaultObj, { x, y as z } from '...' diff --git a/packages/babel-parser/test/fixtures/comments/interpreter-directive/interpreter-directive-import/output.json b/packages/babel-parser/test/fixtures/comments/interpreter-directive/interpreter-directive-import/output.json index 2967587b808d..212043448865 100644 --- a/packages/babel-parser/test/fixtures/comments/interpreter-directive/interpreter-directive-import/output.json +++ b/packages/babel-parser/test/fixtures/comments/interpreter-directive/interpreter-directive-import/output.json @@ -128,7 +128,8 @@ "raw": "'foobar'" }, "value": "foobar" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/comments/regression/10892/output.json b/packages/babel-parser/test/fixtures/comments/regression/10892/output.json index 1ca2d06ab94e..ea79ea02105a 100644 --- a/packages/babel-parser/test/fixtures/comments/regression/10892/output.json +++ b/packages/babel-parser/test/fixtures/comments/regression/10892/output.json @@ -114,6 +114,7 @@ }, "value": "bar" }, + "attributes": [], "trailingComments": [ { "type": "CommentBlock", diff --git a/packages/babel-parser/test/fixtures/core/sourcetype-unambiguous/flow/output.json b/packages/babel-parser/test/fixtures/core/sourcetype-unambiguous/flow/output.json index 970a82515bc0..91d8c27e2ec2 100644 --- a/packages/babel-parser/test/fixtures/core/sourcetype-unambiguous/flow/output.json +++ b/packages/babel-parser/test/fixtures/core/sourcetype-unambiguous/flow/output.json @@ -115,7 +115,8 @@ "raw": "\"bar\"" }, "value": "bar" - } + }, + "attributes": [] }, { "type": "ExportNamedDeclaration", diff --git a/packages/babel-parser/test/fixtures/core/sourcetype-unambiguous/module-import/output.json b/packages/babel-parser/test/fixtures/core/sourcetype-unambiguous/module-import/output.json index 329db7b04655..bf3ca00e92f4 100644 --- a/packages/babel-parser/test/fixtures/core/sourcetype-unambiguous/module-import/output.json +++ b/packages/babel-parser/test/fixtures/core/sourcetype-unambiguous/module-import/output.json @@ -63,7 +63,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/es2015/identifiers/invalid-escape-seq-import/output.json b/packages/babel-parser/test/fixtures/es2015/identifiers/invalid-escape-seq-import/output.json index 8842ac412ce6..01742c7bc7d4 100644 --- a/packages/babel-parser/test/fixtures/es2015/identifiers/invalid-escape-seq-import/output.json +++ b/packages/babel-parser/test/fixtures/es2015/identifiers/invalid-escape-seq-import/output.json @@ -138,7 +138,8 @@ "raw": "\"x\"" }, "value": "x" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/es2015/modules/import-declaration-trailing-comma/output.json b/packages/babel-parser/test/fixtures/es2015/modules/import-declaration-trailing-comma/output.json index a5ebc224c99b..e01aa8aef7c6 100644 --- a/packages/babel-parser/test/fixtures/es2015/modules/import-declaration-trailing-comma/output.json +++ b/packages/babel-parser/test/fixtures/es2015/modules/import-declaration-trailing-comma/output.json @@ -181,7 +181,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword-flow/output.json b/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword-flow/output.json index d1f169eb7647..59d281fdd09f 100644 --- a/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword-flow/output.json +++ b/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword-flow/output.json @@ -118,7 +118,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword-typeof-flow/output.json b/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword-typeof-flow/output.json index 62e2b403628d..e2f1e675a0f9 100644 --- a/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword-typeof-flow/output.json +++ b/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword-typeof-flow/output.json @@ -118,7 +118,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword-typeof/output.json b/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword-typeof/output.json index cd60a3c49fe9..089108d42fa1 100644 --- a/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword-typeof/output.json +++ b/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword-typeof/output.json @@ -116,7 +116,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword/output.json b/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword/output.json index 40242124c0c4..fce361acf523 100644 --- a/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword/output.json +++ b/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword/output.json @@ -116,7 +116,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/es2015/uncategorised/291/output.json b/packages/babel-parser/test/fixtures/es2015/uncategorised/291/output.json index 42655f4b978c..564e1d8c3e67 100644 --- a/packages/babel-parser/test/fixtures/es2015/uncategorised/291/output.json +++ b/packages/babel-parser/test/fixtures/es2015/uncategorised/291/output.json @@ -101,7 +101,8 @@ "raw": "\"acorn\"" }, "value": "acorn" - } + }, + "attributes": [] }, "alternate": null } diff --git a/packages/babel-parser/test/fixtures/es2015/uncategorised/301/output.json b/packages/babel-parser/test/fixtures/es2015/uncategorised/301/output.json index bf47299eba50..1fd791c445ea 100644 --- a/packages/babel-parser/test/fixtures/es2015/uncategorised/301/output.json +++ b/packages/babel-parser/test/fixtures/es2015/uncategorised/301/output.json @@ -128,7 +128,8 @@ "raw": "'baz'" }, "value": "baz" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/es2015/uncategorised/88/output.json b/packages/babel-parser/test/fixtures/es2015/uncategorised/88/output.json index 25b0fd97418a..5c8fd00673fe 100644 --- a/packages/babel-parser/test/fixtures/es2015/uncategorised/88/output.json +++ b/packages/babel-parser/test/fixtures/es2015/uncategorised/88/output.json @@ -96,7 +96,8 @@ "raw": "\"\"" }, "value": "" - } + }, + "attributes": [] }, { "type": "ExportNamedDeclaration", diff --git a/packages/babel-parser/test/fixtures/es2015/uncategorised/91/output.json b/packages/babel-parser/test/fixtures/es2015/uncategorised/91/output.json index 12c4be422437..13a8c9886a2a 100644 --- a/packages/babel-parser/test/fixtures/es2015/uncategorised/91/output.json +++ b/packages/babel-parser/test/fixtures/es2015/uncategorised/91/output.json @@ -63,7 +63,8 @@ "raw": "\"jquery\"" }, "value": "jquery" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/es2015/uncategorised/92/output.json b/packages/babel-parser/test/fixtures/es2015/uncategorised/92/output.json index 30cae98f649b..cd29739fee09 100644 --- a/packages/babel-parser/test/fixtures/es2015/uncategorised/92/output.json +++ b/packages/babel-parser/test/fixtures/es2015/uncategorised/92/output.json @@ -96,7 +96,8 @@ "raw": "\"jquery\"" }, "value": "jquery" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/es2015/uncategorised/93/output.json b/packages/babel-parser/test/fixtures/es2015/uncategorised/93/output.json index 4591fadf23df..a804315ed006 100644 --- a/packages/babel-parser/test/fixtures/es2015/uncategorised/93/output.json +++ b/packages/babel-parser/test/fixtures/es2015/uncategorised/93/output.json @@ -162,7 +162,8 @@ "raw": "\"crypto\"" }, "value": "crypto" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/es2015/uncategorised/94/output.json b/packages/babel-parser/test/fixtures/es2015/uncategorised/94/output.json index f2cd05d58507..6f9a7f3c3eb6 100644 --- a/packages/babel-parser/test/fixtures/es2015/uncategorised/94/output.json +++ b/packages/babel-parser/test/fixtures/es2015/uncategorised/94/output.json @@ -113,7 +113,8 @@ "raw": "\"crypto\"" }, "value": "crypto" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/es2015/uncategorised/95/output.json b/packages/babel-parser/test/fixtures/es2015/uncategorised/95/output.json index 4d1f5f67a529..ee8a3c6b3838 100644 --- a/packages/babel-parser/test/fixtures/es2015/uncategorised/95/output.json +++ b/packages/babel-parser/test/fixtures/es2015/uncategorised/95/output.json @@ -194,7 +194,8 @@ "raw": "\"crypto\"" }, "value": "crypto" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/es2015/uncategorised/97/output.json b/packages/babel-parser/test/fixtures/es2015/uncategorised/97/output.json index d589597abfc8..4810416f47a3 100644 --- a/packages/babel-parser/test/fixtures/es2015/uncategorised/97/output.json +++ b/packages/babel-parser/test/fixtures/es2015/uncategorised/97/output.json @@ -113,7 +113,8 @@ "raw": "\"bar\"" }, "value": "bar" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/es2015/uncategorised/98/output.json b/packages/babel-parser/test/fixtures/es2015/uncategorised/98/output.json index 6e021038f2b9..af03a34bccd1 100644 --- a/packages/babel-parser/test/fixtures/es2015/uncategorised/98/output.json +++ b/packages/babel-parser/test/fixtures/es2015/uncategorised/98/output.json @@ -96,7 +96,8 @@ "raw": "\"crypto\"" }, "value": "crypto" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default-and-named-specifiers/output.json b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default-and-named-specifiers/output.json index 13a5ef7f4abd..a277f4aed728 100644 --- a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default-and-named-specifiers/output.json +++ b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default-and-named-specifiers/output.json @@ -145,7 +145,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default-and-namespace-specifiers/output.json b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default-and-namespace-specifiers/output.json index 0458b69aea73..6754bdb3ef82 100644 --- a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default-and-namespace-specifiers/output.json +++ b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default-and-namespace-specifiers/output.json @@ -128,7 +128,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default-as/output.json b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default-as/output.json index 347b3be44413..128e524a863e 100644 --- a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default-as/output.json +++ b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default-as/output.json @@ -113,7 +113,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default/output.json b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default/output.json index 5a75f863c506..14498b0a42b2 100644 --- a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default/output.json +++ b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default/output.json @@ -96,7 +96,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-jquery/output.json b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-jquery/output.json index 30cae98f649b..cd29739fee09 100644 --- a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-jquery/output.json +++ b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-jquery/output.json @@ -96,7 +96,8 @@ "raw": "\"jquery\"" }, "value": "jquery" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-module/output.json b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-module/output.json index 329db7b04655..bf3ca00e92f4 100644 --- a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-module/output.json +++ b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-module/output.json @@ -63,7 +63,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-as-specifier/output.json b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-as-specifier/output.json index 7051af9018ad..84797c48f678 100644 --- a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-as-specifier/output.json +++ b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-as-specifier/output.json @@ -113,7 +113,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-as-specifiers/output.json b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-as-specifiers/output.json index bad31cd6088a..f3d8ee0cdd65 100644 --- a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-as-specifiers/output.json +++ b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-as-specifiers/output.json @@ -162,7 +162,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-empty/output.json b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-empty/output.json index b149b8db92f9..120ecc8784a8 100644 --- a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-empty/output.json +++ b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-empty/output.json @@ -63,7 +63,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-specifier/output.json b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-specifier/output.json index 3a62a458d615..f51337e0e616 100644 --- a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-specifier/output.json +++ b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-specifier/output.json @@ -113,7 +113,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-specifiers-comma/output.json b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-specifiers-comma/output.json index ec9898e4ef40..fd35a849ba58 100644 --- a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-specifiers-comma/output.json +++ b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-specifiers-comma/output.json @@ -162,7 +162,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-specifiers/output.json b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-specifiers/output.json index cc54edc053cb..8e3b2c7a05a0 100644 --- a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-specifiers/output.json +++ b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-specifiers/output.json @@ -162,7 +162,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-namespace-specifier/output.json b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-namespace-specifier/output.json index 9c4d87ac49a4..5fa52440ae2b 100644 --- a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-namespace-specifier/output.json +++ b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-namespace-specifier/output.json @@ -96,7 +96,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-null-as-nil/output.json b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-null-as-nil/output.json index d589597abfc8..4810416f47a3 100644 --- a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-null-as-nil/output.json +++ b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-null-as-nil/output.json @@ -113,7 +113,8 @@ "raw": "\"bar\"" }, "value": "bar" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-module-attributes/input.js b/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-module-attributes/input.js new file mode 100644 index 000000000000..fb31508a0860 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-module-attributes/input.js @@ -0,0 +1 @@ +import foo from "foo.json" with type: "json"; diff --git a/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-module-attributes/options.json b/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-module-attributes/options.json new file mode 100644 index 000000000000..3f9b696c8de9 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-module-attributes/options.json @@ -0,0 +1,5 @@ +{ + "throws": "This experimental syntax requires enabling the parser plugin: 'moduleAttributes' (1:27)", + "sourceType": "module", + "plugins": [] +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes/input.js b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes/input.js new file mode 100644 index 000000000000..fb31508a0860 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes/input.js @@ -0,0 +1 @@ +import foo from "foo.json" with type: "json"; diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes/options.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes/options.json new file mode 100644 index 000000000000..9399e0bedd3f --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes/options.json @@ -0,0 +1,4 @@ +{ + "plugins": ["moduleAttributes"], + "sourceType": "module" +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes/output.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes/output.json new file mode 100644 index 000000000000..490e4ee43d32 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes/output.json @@ -0,0 +1,176 @@ +{ + "type": "File", + "start": 0, + "end": 45, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 45 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 45, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 45 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start": 0, + "end": 45, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 45 + } + }, + "specifiers": [ + { + "type": "ImportDefaultSpecifier", + "start": 7, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "local": { + "type": "Identifier", + "start": 7, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 10 + }, + "identifierName": "foo" + }, + "name": "foo" + } + } + ], + "source": { + "type": "StringLiteral", + "start": 16, + "end": 26, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 26 + } + }, + "extra": { + "rawValue": "foo.json", + "raw": "\"foo.json\"" + }, + "value": "foo.json" + }, + "attributes": { + "type": "ObjectExpression", + "start": 32, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 32 + }, + "end": { + "line": 1, + "column": 44 + } + }, + "properties": [ + { + "type": "ObjectProperty", + "start": 32, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 32 + }, + "end": { + "line": 1, + "column": 44 + } + }, + "method": false, + "key": { + "type": "Identifier", + "start": 32, + "end": 36, + "loc": { + "start": { + "line": 1, + "column": 32 + }, + "end": { + "line": 1, + "column": 36 + }, + "identifierName": "type" + }, + "name": "type" + }, + "computed": false, + "shorthand": false, + "value": { + "type": "StringLiteral", + "start": 38, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 38 + }, + "end": { + "line": 1, + "column": 44 + } + }, + "extra": { + "rawValue": "json", + "raw": "\"json\"" + }, + "value": "json" + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-without-attributes/input.js b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-without-attributes/input.js new file mode 100644 index 000000000000..92901ed56b38 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-without-attributes/input.js @@ -0,0 +1 @@ +import foo from "foo.json"; diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-without-attributes/options.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-without-attributes/options.json new file mode 100644 index 000000000000..9399e0bedd3f --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-without-attributes/options.json @@ -0,0 +1,4 @@ +{ + "plugins": ["moduleAttributes"], + "sourceType": "module" +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-without-attributes/output.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-without-attributes/output.json new file mode 100644 index 000000000000..dec2f8457cbd --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-without-attributes/output.json @@ -0,0 +1,105 @@ +{ + "type": "File", + "start": 0, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 27 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 27 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start": 0, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 27 + } + }, + "specifiers": [ + { + "type": "ImportDefaultSpecifier", + "start": 7, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "local": { + "type": "Identifier", + "start": 7, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 10 + }, + "identifierName": "foo" + }, + "name": "foo" + } + } + ], + "source": { + "type": "StringLiteral", + "start": 16, + "end": 26, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 26 + } + }, + "extra": { + "rawValue": "foo.json", + "raw": "\"foo.json\"" + }, + "value": "foo.json" + }, + "attributes": [] + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/flow/declare-module/import/output.json b/packages/babel-parser/test/fixtures/flow/declare-module/import/output.json index f7bb57172e32..adfc27d63004 100644 --- a/packages/babel-parser/test/fixtures/flow/declare-module/import/output.json +++ b/packages/babel-parser/test/fixtures/flow/declare-module/import/output.json @@ -146,7 +146,8 @@ "raw": "\"TM\"" }, "value": "TM" - } + }, + "attributes": [] }, { "type": "ImportDeclaration", @@ -216,7 +217,8 @@ "raw": "\"UM\"" }, "value": "UM" - } + }, + "attributes": [] } ] }, diff --git a/packages/babel-parser/test/fixtures/flow/declare-module/invalid-import/output.json b/packages/babel-parser/test/fixtures/flow/declare-module/invalid-import/output.json index 2d33b963c778..99001ac1179b 100644 --- a/packages/babel-parser/test/fixtures/flow/declare-module/invalid-import/output.json +++ b/packages/babel-parser/test/fixtures/flow/declare-module/invalid-import/output.json @@ -149,7 +149,8 @@ "raw": "\"TM\"" }, "value": "TM" - } + }, + "attributes": [] } ] }, diff --git a/packages/babel-parser/test/fixtures/flow/sourcetype-script/import/output.json b/packages/babel-parser/test/fixtures/flow/sourcetype-script/import/output.json index d49e3470fffd..8d8b0e2a4d33 100644 --- a/packages/babel-parser/test/fixtures/flow/sourcetype-script/import/output.json +++ b/packages/babel-parser/test/fixtures/flow/sourcetype-script/import/output.json @@ -115,7 +115,8 @@ "raw": "\"\"" }, "value": "" - } + }, + "attributes": [] }, { "type": "ImportDeclaration", @@ -185,7 +186,8 @@ "raw": "\"\"" }, "value": "" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/flow/type-imports/import-type-2/output.json b/packages/babel-parser/test/fixtures/flow/type-imports/import-type-2/output.json index f5fc078a42ce..543d787fa85d 100644 --- a/packages/babel-parser/test/fixtures/flow/type-imports/import-type-2/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-imports/import-type-2/output.json @@ -147,7 +147,8 @@ "raw": "\"bar\"" }, "value": "bar" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/flow/type-imports/import-type-shorthand/output.json b/packages/babel-parser/test/fixtures/flow/type-imports/import-type-shorthand/output.json index aeb7eded0711..bb7c3d6a00ef 100644 --- a/packages/babel-parser/test/fixtures/flow/type-imports/import-type-shorthand/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-imports/import-type-shorthand/output.json @@ -115,7 +115,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] }, { "type": "ImportDeclaration", @@ -203,7 +204,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] }, { "type": "ImportDeclaration", @@ -291,7 +293,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] }, { "type": "ImportDeclaration", @@ -379,7 +382,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] }, { "type": "ImportDeclaration", @@ -467,7 +471,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] }, { "type": "ImportDeclaration", @@ -555,7 +560,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] }, { "type": "ImportDeclaration", @@ -643,7 +649,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] }, { "type": "ImportDeclaration", @@ -731,7 +738,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] }, { "type": "ImportDeclaration", @@ -819,7 +827,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] }, { "type": "ImportDeclaration", @@ -907,7 +916,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/flow/type-imports/import-type/output.json b/packages/babel-parser/test/fixtures/flow/type-imports/import-type/output.json index 8e626c8ad0a5..970128f4c86e 100644 --- a/packages/babel-parser/test/fixtures/flow/type-imports/import-type/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-imports/import-type/output.json @@ -97,7 +97,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] }, { "type": "ImportDeclaration", @@ -185,7 +186,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] }, { "type": "ImportDeclaration", @@ -305,7 +307,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] }, { "type": "ImportDeclaration", @@ -375,7 +378,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] }, { "type": "ImportDeclaration", @@ -463,7 +467,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] }, { "type": "ImportDeclaration", @@ -601,7 +606,8 @@ "raw": "\"baz\"" }, "value": "baz" - } + }, + "attributes": [] }, { "type": "ImportDeclaration", @@ -671,7 +677,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] }, { "type": "ImportDeclaration", @@ -741,7 +748,8 @@ "raw": "\"bar\"" }, "value": "bar" - } + }, + "attributes": [] }, { "type": "ImportDeclaration", @@ -811,7 +819,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] }, { "type": "ImportDeclaration", @@ -899,7 +908,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] }, { "type": "ImportDeclaration", @@ -987,7 +997,8 @@ "raw": "\"baz\"" }, "value": "baz" - } + }, + "attributes": [] }, { "type": "ImportDeclaration", @@ -1057,7 +1068,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] }, { "type": "ImportDeclaration", @@ -1127,7 +1139,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-2/output.json b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-2/output.json index 5ac98d7b46b7..e752885d3e69 100644 --- a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-2/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-2/output.json @@ -118,7 +118,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-3/output.json b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-3/output.json index 3a1010d807fe..7923e739b575 100644 --- a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-3/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-3/output.json @@ -100,7 +100,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-4/output.json b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-4/output.json index dcbca6978a12..b6bc765c560b 100644 --- a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-4/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-4/output.json @@ -100,7 +100,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-as/output.json b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-as/output.json index 05cc09533a23..f137b4663de0 100644 --- a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-as/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-as/output.json @@ -118,7 +118,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand-2/output.json b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand-2/output.json index 90585f1b4f80..f14f3469465c 100644 --- a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand-2/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand-2/output.json @@ -118,7 +118,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand-3/output.json b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand-3/output.json index 4969f577a320..b7da867ff7b0 100644 --- a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand-3/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand-3/output.json @@ -118,7 +118,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand-4/output.json b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand-4/output.json index 2b57e99a30e7..47e4251b4bff 100644 --- a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand-4/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand-4/output.json @@ -118,7 +118,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand/output.json b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand/output.json index 174e856304f7..1f96244b89f0 100644 --- a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand/output.json @@ -118,7 +118,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type/output.json b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type/output.json index 95872d960a5c..569b6b3e5715 100644 --- a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type/output.json @@ -100,7 +100,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/placeholders/import/default-named/output.json b/packages/babel-parser/test/fixtures/placeholders/import/default-named/output.json index 62d764263d3d..69bc35d7d25e 100644 --- a/packages/babel-parser/test/fixtures/placeholders/import/default-named/output.json +++ b/packages/babel-parser/test/fixtures/placeholders/import/default-named/output.json @@ -177,7 +177,8 @@ "raw": "\"file\"" }, "value": "file" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/placeholders/import/default-star-2/output.json b/packages/babel-parser/test/fixtures/placeholders/import/default-star-2/output.json index 6fd8a2dbed1f..5f0045e738ec 100644 --- a/packages/babel-parser/test/fixtures/placeholders/import/default-star-2/output.json +++ b/packages/babel-parser/test/fixtures/placeholders/import/default-star-2/output.json @@ -144,7 +144,8 @@ "raw": "\"file\"" }, "value": "file" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/placeholders/import/file-empty/output.json b/packages/babel-parser/test/fixtures/placeholders/import/file-empty/output.json index 5690ee81f00f..3f8e2f091bb1 100644 --- a/packages/babel-parser/test/fixtures/placeholders/import/file-empty/output.json +++ b/packages/babel-parser/test/fixtures/placeholders/import/file-empty/output.json @@ -76,7 +76,8 @@ "name": "FILE" }, "expectedNode": "StringLiteral" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/placeholders/import/named-alias-2/output.json b/packages/babel-parser/test/fixtures/placeholders/import/named-alias-2/output.json index c7d936e96c64..6f15e484218f 100644 --- a/packages/babel-parser/test/fixtures/placeholders/import/named-alias-2/output.json +++ b/packages/babel-parser/test/fixtures/placeholders/import/named-alias-2/output.json @@ -129,7 +129,8 @@ "raw": "\"file\"" }, "value": "file" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/placeholders/import/named-alias-3/output.json b/packages/babel-parser/test/fixtures/placeholders/import/named-alias-3/output.json index bfae4a044410..73c301a18f0d 100644 --- a/packages/babel-parser/test/fixtures/placeholders/import/named-alias-3/output.json +++ b/packages/babel-parser/test/fixtures/placeholders/import/named-alias-3/output.json @@ -145,7 +145,8 @@ "raw": "\"file\"" }, "value": "file" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/placeholders/import/named-alias/output.json b/packages/babel-parser/test/fixtures/placeholders/import/named-alias/output.json index 5477bef4ca9e..fedf25cb9509 100644 --- a/packages/babel-parser/test/fixtures/placeholders/import/named-alias/output.json +++ b/packages/babel-parser/test/fixtures/placeholders/import/named-alias/output.json @@ -129,7 +129,8 @@ "raw": "\"file\"" }, "value": "file" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/placeholders/import/named/output.json b/packages/babel-parser/test/fixtures/placeholders/import/named/output.json index 9b3f08486a88..cef9cf373dad 100644 --- a/packages/babel-parser/test/fixtures/placeholders/import/named/output.json +++ b/packages/babel-parser/test/fixtures/placeholders/import/named/output.json @@ -145,7 +145,8 @@ "raw": "\"file\"" }, "value": "file" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/placeholders/import/star/output.json b/packages/babel-parser/test/fixtures/placeholders/import/star/output.json index 7ff6bfd05846..b785c2b3f14b 100644 --- a/packages/babel-parser/test/fixtures/placeholders/import/star/output.json +++ b/packages/babel-parser/test/fixtures/placeholders/import/star/output.json @@ -112,7 +112,8 @@ "raw": "\"file\"" }, "value": "file" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/typescript/import/not-top-level/output.json b/packages/babel-parser/test/fixtures/typescript/import/not-top-level/output.json index d8c247aa8f57..e72c858cc59e 100644 --- a/packages/babel-parser/test/fixtures/typescript/import/not-top-level/output.json +++ b/packages/babel-parser/test/fixtures/typescript/import/not-top-level/output.json @@ -145,7 +145,8 @@ "raw": "\"a\"" }, "value": "a" - } + }, + "attributes": [] } ] }, diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-import-ambient-class/output.json b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-import-ambient-class/output.json index fe9e84eee59d..e642bced5b50 100644 --- a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-import-ambient-class/output.json +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-import-ambient-class/output.json @@ -96,7 +96,8 @@ "raw": "'./somewhere.js'" }, "value": "./somewhere.js" - } + }, + "attributes": [] }, { "type": "ClassDeclaration", From b810e311de12ac1f040768e155fc5ab0569b00de Mon Sep 17 00:00:00 2001 From: Vivek Nayyar Date: Sun, 5 Jan 2020 23:43:14 +0100 Subject: [PATCH 02/63] attributes should be set only if moduleAttributes plugin is enabled, refactored the parsing logic --- .../babel-parser/src/parser/expression.js | 50 +++++---- packages/babel-parser/src/parser/statement.js | 27 +++-- .../interpreter-directive-import/output.json | 3 +- .../comments/regression/10892/output.json | 1 - .../sourcetype-unambiguous/flow/output.json | 3 +- .../module-import/output.json | 3 +- .../invalid-escape-seq-import/output.json | 3 +- .../output.json | 3 +- .../import-invalid-keyword-flow/output.json | 3 +- .../output.json | 3 +- .../import-invalid-keyword-typeof/output.json | 3 +- .../import-invalid-keyword/output.json | 3 +- .../es2015/uncategorised/291/output.json | 3 +- .../es2015/uncategorised/301/output.json | 3 +- .../es2015/uncategorised/88/output.json | 3 +- .../es2015/uncategorised/91/output.json | 3 +- .../es2015/uncategorised/92/output.json | 3 +- .../es2015/uncategorised/93/output.json | 3 +- .../es2015/uncategorised/94/output.json | 3 +- .../es2015/uncategorised/95/output.json | 3 +- .../es2015/uncategorised/97/output.json | 3 +- .../es2015/uncategorised/98/output.json | 3 +- .../output.json | 3 +- .../output.json | 3 +- .../import-default-as/output.json | 3 +- .../import-default/output.json | 3 +- .../import-jquery/output.json | 3 +- .../import-module/output.json | 3 +- .../import-named-as-specifier/output.json | 3 +- .../import-named-as-specifiers/output.json | 3 +- .../import-named-empty/output.json | 3 +- .../import-named-specifier/output.json | 3 +- .../import-named-specifiers-comma/output.json | 3 +- .../import-named-specifiers/output.json | 3 +- .../import-namespace-specifier/output.json | 3 +- .../import-null-as-nil/output.json | 3 +- .../import-module-attributes/options.json | 2 +- .../valid-syntax-with-attributes/output.json | 101 ++++++++---------- .../flow/declare-module/import/output.json | 6 +- .../declare-module/invalid-import/output.json | 3 +- .../flow/sourcetype-script/import/output.json | 6 +- .../type-imports/import-type-2/output.json | 3 +- .../import-type-shorthand/output.json | 30 ++---- .../flow/type-imports/import-type/output.json | 39 +++---- .../invalid-import-type-2/output.json | 3 +- .../invalid-import-type-3/output.json | 3 +- .../invalid-import-type-4/output.json | 3 +- .../invalid-import-type-as/output.json | 3 +- .../output.json | 3 +- .../output.json | 3 +- .../output.json | 3 +- .../invalid-import-type-shorthand/output.json | 3 +- .../invalid-import-type/output.json | 3 +- .../import/default-named/output.json | 3 +- .../import/default-star-2/output.json | 3 +- .../import/file-empty/output.json | 3 +- .../import/named-alias-2/output.json | 3 +- .../import/named-alias-3/output.json | 3 +- .../import/named-alias/output.json | 3 +- .../placeholders/import/named/output.json | 3 +- .../placeholders/import/star/output.json | 3 +- .../import/not-top-level/output.json | 3 +- .../output.json | 3 +- 63 files changed, 175 insertions(+), 249 deletions(-) diff --git a/packages/babel-parser/src/parser/expression.js b/packages/babel-parser/src/parser/expression.js index 45347ea713b4..64c0a4f7ffce 100644 --- a/packages/babel-parser/src/parser/expression.js +++ b/packages/babel-parser/src/parser/expression.js @@ -852,6 +852,35 @@ export default class ExpressionParser extends LValParser { return this.parseSubscripts(this.parseExprAtom(), startPos, startLoc, true); } + /** + * Parse a primitive value + * primitive values are null, undefined, number, bigint, string, boolean + */ + parsePrimitiveValue(): N.Expression { + let node; + switch (this.state.type) { + case tt.num: + return this.parseLiteral(this.state.value, "NumericLiteral"); + + case tt.bigint: + return this.parseLiteral(this.state.value, "BigIntLiteral"); + + case tt.string: + return this.parseLiteral(this.state.value, "StringLiteral"); + + case tt._null: + node = this.startNode(); + this.next(); + return this.finishNode(node, "NullLiteral"); + + case tt._true: + case tt._false: + return this.parseBooleanLiteral(); + default: + throw this.unexpected(); + } + } + // Parse an atomic expression — either a single token that is an // expression, an expression started by a keyword like `function` or // `new`, or an expression wrapped in punctuation like `()`, `[]`, @@ -1459,27 +1488,6 @@ export default class ExpressionParser extends LValParser { this.next(); return this.finishNode(node, "TemplateLiteral"); } - - parseKeyValuePairs() { - const propHash: any = Object.create(null); - const node = this.startNode(); - node.properties = []; - - do { - const prop = this.parseObjectMember(); - // $FlowIgnore - if (prop.shorthand) { - this.addExtra(prop, "shorthand", true); - } - - node.properties.push(prop); - } while (this.eat(tt.comma)); - if (!this.match(tt.eq) && propHash.start !== undefined) { - this.raise(propHash.start, "Redefinition of __proto__ property"); - } - return this.finishNode(node, "ObjectExpression"); - } - // Parse an object literal or binding pattern. parseObj( diff --git a/packages/babel-parser/src/parser/statement.js b/packages/babel-parser/src/parser/statement.js index 6ad9ace8b953..aacb044a836b 100644 --- a/packages/babel-parser/src/parser/statement.js +++ b/packages/babel-parser/src/parser/statement.js @@ -2024,7 +2024,10 @@ export default class StatementParser extends ExpressionParser { this.expectContextual("from"); } node.source = this.parseImportSource(); - node.attributes = this.maybeParseModuleAttributes(); + const attributes = this.maybeParseModuleAttributes(); + if (Array.isArray(attributes)) { + node.attributes = attributes; + } this.semicolon(); return this.finishNode(node, "ImportDeclaration"); } @@ -2056,12 +2059,24 @@ export default class StatementParser extends ExpressionParser { } maybeParseModuleAttributes() { - if (this.match(tt._with) && !this.hasPlugin("moduleAttributes")) { - this.expectPlugin("moduleAttributes"); - } else if (this.eat(tt._with)) { - return this.parseKeyValuePairs(); + if (!this.eat(tt._with)) { + if (this.hasPlugin("moduleAttributes")) return []; + return null; } - return []; + this.expectPlugin("moduleAttributes"); + const attrs = []; + do { + const node = this.startNode(); + node.key = this.parseIdentifier(); + node.computed = false; + node.method = false; + node.shorthand = false; + this.expect(tt.colon); + node.value = this.parsePrimitiveValue(); + this.finishNode(node, "ObjectProperty"); + attrs.push(node); + } while (this.eat(tt.comma)); + return attrs; } maybeParseDefaultImportSpecifier(node: N.ImportDeclaration): boolean { diff --git a/packages/babel-parser/test/fixtures/comments/interpreter-directive/interpreter-directive-import/output.json b/packages/babel-parser/test/fixtures/comments/interpreter-directive/interpreter-directive-import/output.json index 212043448865..2967587b808d 100644 --- a/packages/babel-parser/test/fixtures/comments/interpreter-directive/interpreter-directive-import/output.json +++ b/packages/babel-parser/test/fixtures/comments/interpreter-directive/interpreter-directive-import/output.json @@ -128,8 +128,7 @@ "raw": "'foobar'" }, "value": "foobar" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/comments/regression/10892/output.json b/packages/babel-parser/test/fixtures/comments/regression/10892/output.json index ea79ea02105a..1ca2d06ab94e 100644 --- a/packages/babel-parser/test/fixtures/comments/regression/10892/output.json +++ b/packages/babel-parser/test/fixtures/comments/regression/10892/output.json @@ -114,7 +114,6 @@ }, "value": "bar" }, - "attributes": [], "trailingComments": [ { "type": "CommentBlock", diff --git a/packages/babel-parser/test/fixtures/core/sourcetype-unambiguous/flow/output.json b/packages/babel-parser/test/fixtures/core/sourcetype-unambiguous/flow/output.json index 91d8c27e2ec2..970a82515bc0 100644 --- a/packages/babel-parser/test/fixtures/core/sourcetype-unambiguous/flow/output.json +++ b/packages/babel-parser/test/fixtures/core/sourcetype-unambiguous/flow/output.json @@ -115,8 +115,7 @@ "raw": "\"bar\"" }, "value": "bar" - }, - "attributes": [] + } }, { "type": "ExportNamedDeclaration", diff --git a/packages/babel-parser/test/fixtures/core/sourcetype-unambiguous/module-import/output.json b/packages/babel-parser/test/fixtures/core/sourcetype-unambiguous/module-import/output.json index bf3ca00e92f4..329db7b04655 100644 --- a/packages/babel-parser/test/fixtures/core/sourcetype-unambiguous/module-import/output.json +++ b/packages/babel-parser/test/fixtures/core/sourcetype-unambiguous/module-import/output.json @@ -63,8 +63,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/es2015/identifiers/invalid-escape-seq-import/output.json b/packages/babel-parser/test/fixtures/es2015/identifiers/invalid-escape-seq-import/output.json index 01742c7bc7d4..8842ac412ce6 100644 --- a/packages/babel-parser/test/fixtures/es2015/identifiers/invalid-escape-seq-import/output.json +++ b/packages/babel-parser/test/fixtures/es2015/identifiers/invalid-escape-seq-import/output.json @@ -138,8 +138,7 @@ "raw": "\"x\"" }, "value": "x" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/es2015/modules/import-declaration-trailing-comma/output.json b/packages/babel-parser/test/fixtures/es2015/modules/import-declaration-trailing-comma/output.json index e01aa8aef7c6..a5ebc224c99b 100644 --- a/packages/babel-parser/test/fixtures/es2015/modules/import-declaration-trailing-comma/output.json +++ b/packages/babel-parser/test/fixtures/es2015/modules/import-declaration-trailing-comma/output.json @@ -181,8 +181,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword-flow/output.json b/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword-flow/output.json index 59d281fdd09f..d1f169eb7647 100644 --- a/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword-flow/output.json +++ b/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword-flow/output.json @@ -118,8 +118,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword-typeof-flow/output.json b/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword-typeof-flow/output.json index e2f1e675a0f9..62e2b403628d 100644 --- a/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword-typeof-flow/output.json +++ b/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword-typeof-flow/output.json @@ -118,8 +118,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword-typeof/output.json b/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword-typeof/output.json index 089108d42fa1..cd60a3c49fe9 100644 --- a/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword-typeof/output.json +++ b/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword-typeof/output.json @@ -116,8 +116,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword/output.json b/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword/output.json index fce361acf523..40242124c0c4 100644 --- a/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword/output.json +++ b/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword/output.json @@ -116,8 +116,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/es2015/uncategorised/291/output.json b/packages/babel-parser/test/fixtures/es2015/uncategorised/291/output.json index 564e1d8c3e67..42655f4b978c 100644 --- a/packages/babel-parser/test/fixtures/es2015/uncategorised/291/output.json +++ b/packages/babel-parser/test/fixtures/es2015/uncategorised/291/output.json @@ -101,8 +101,7 @@ "raw": "\"acorn\"" }, "value": "acorn" - }, - "attributes": [] + } }, "alternate": null } diff --git a/packages/babel-parser/test/fixtures/es2015/uncategorised/301/output.json b/packages/babel-parser/test/fixtures/es2015/uncategorised/301/output.json index 1fd791c445ea..bf47299eba50 100644 --- a/packages/babel-parser/test/fixtures/es2015/uncategorised/301/output.json +++ b/packages/babel-parser/test/fixtures/es2015/uncategorised/301/output.json @@ -128,8 +128,7 @@ "raw": "'baz'" }, "value": "baz" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/es2015/uncategorised/88/output.json b/packages/babel-parser/test/fixtures/es2015/uncategorised/88/output.json index 5c8fd00673fe..25b0fd97418a 100644 --- a/packages/babel-parser/test/fixtures/es2015/uncategorised/88/output.json +++ b/packages/babel-parser/test/fixtures/es2015/uncategorised/88/output.json @@ -96,8 +96,7 @@ "raw": "\"\"" }, "value": "" - }, - "attributes": [] + } }, { "type": "ExportNamedDeclaration", diff --git a/packages/babel-parser/test/fixtures/es2015/uncategorised/91/output.json b/packages/babel-parser/test/fixtures/es2015/uncategorised/91/output.json index 13a8c9886a2a..12c4be422437 100644 --- a/packages/babel-parser/test/fixtures/es2015/uncategorised/91/output.json +++ b/packages/babel-parser/test/fixtures/es2015/uncategorised/91/output.json @@ -63,8 +63,7 @@ "raw": "\"jquery\"" }, "value": "jquery" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/es2015/uncategorised/92/output.json b/packages/babel-parser/test/fixtures/es2015/uncategorised/92/output.json index cd29739fee09..30cae98f649b 100644 --- a/packages/babel-parser/test/fixtures/es2015/uncategorised/92/output.json +++ b/packages/babel-parser/test/fixtures/es2015/uncategorised/92/output.json @@ -96,8 +96,7 @@ "raw": "\"jquery\"" }, "value": "jquery" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/es2015/uncategorised/93/output.json b/packages/babel-parser/test/fixtures/es2015/uncategorised/93/output.json index a804315ed006..4591fadf23df 100644 --- a/packages/babel-parser/test/fixtures/es2015/uncategorised/93/output.json +++ b/packages/babel-parser/test/fixtures/es2015/uncategorised/93/output.json @@ -162,8 +162,7 @@ "raw": "\"crypto\"" }, "value": "crypto" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/es2015/uncategorised/94/output.json b/packages/babel-parser/test/fixtures/es2015/uncategorised/94/output.json index 6f9a7f3c3eb6..f2cd05d58507 100644 --- a/packages/babel-parser/test/fixtures/es2015/uncategorised/94/output.json +++ b/packages/babel-parser/test/fixtures/es2015/uncategorised/94/output.json @@ -113,8 +113,7 @@ "raw": "\"crypto\"" }, "value": "crypto" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/es2015/uncategorised/95/output.json b/packages/babel-parser/test/fixtures/es2015/uncategorised/95/output.json index ee8a3c6b3838..4d1f5f67a529 100644 --- a/packages/babel-parser/test/fixtures/es2015/uncategorised/95/output.json +++ b/packages/babel-parser/test/fixtures/es2015/uncategorised/95/output.json @@ -194,8 +194,7 @@ "raw": "\"crypto\"" }, "value": "crypto" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/es2015/uncategorised/97/output.json b/packages/babel-parser/test/fixtures/es2015/uncategorised/97/output.json index 4810416f47a3..d589597abfc8 100644 --- a/packages/babel-parser/test/fixtures/es2015/uncategorised/97/output.json +++ b/packages/babel-parser/test/fixtures/es2015/uncategorised/97/output.json @@ -113,8 +113,7 @@ "raw": "\"bar\"" }, "value": "bar" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/es2015/uncategorised/98/output.json b/packages/babel-parser/test/fixtures/es2015/uncategorised/98/output.json index af03a34bccd1..6e021038f2b9 100644 --- a/packages/babel-parser/test/fixtures/es2015/uncategorised/98/output.json +++ b/packages/babel-parser/test/fixtures/es2015/uncategorised/98/output.json @@ -96,8 +96,7 @@ "raw": "\"crypto\"" }, "value": "crypto" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default-and-named-specifiers/output.json b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default-and-named-specifiers/output.json index a277f4aed728..13a5ef7f4abd 100644 --- a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default-and-named-specifiers/output.json +++ b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default-and-named-specifiers/output.json @@ -145,8 +145,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default-and-namespace-specifiers/output.json b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default-and-namespace-specifiers/output.json index 6754bdb3ef82..0458b69aea73 100644 --- a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default-and-namespace-specifiers/output.json +++ b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default-and-namespace-specifiers/output.json @@ -128,8 +128,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default-as/output.json b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default-as/output.json index 128e524a863e..347b3be44413 100644 --- a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default-as/output.json +++ b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default-as/output.json @@ -113,8 +113,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default/output.json b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default/output.json index 14498b0a42b2..5a75f863c506 100644 --- a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default/output.json +++ b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default/output.json @@ -96,8 +96,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-jquery/output.json b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-jquery/output.json index cd29739fee09..30cae98f649b 100644 --- a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-jquery/output.json +++ b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-jquery/output.json @@ -96,8 +96,7 @@ "raw": "\"jquery\"" }, "value": "jquery" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-module/output.json b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-module/output.json index bf3ca00e92f4..329db7b04655 100644 --- a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-module/output.json +++ b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-module/output.json @@ -63,8 +63,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-as-specifier/output.json b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-as-specifier/output.json index 84797c48f678..7051af9018ad 100644 --- a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-as-specifier/output.json +++ b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-as-specifier/output.json @@ -113,8 +113,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-as-specifiers/output.json b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-as-specifiers/output.json index f3d8ee0cdd65..bad31cd6088a 100644 --- a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-as-specifiers/output.json +++ b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-as-specifiers/output.json @@ -162,8 +162,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-empty/output.json b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-empty/output.json index 120ecc8784a8..b149b8db92f9 100644 --- a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-empty/output.json +++ b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-empty/output.json @@ -63,8 +63,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-specifier/output.json b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-specifier/output.json index f51337e0e616..3a62a458d615 100644 --- a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-specifier/output.json +++ b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-specifier/output.json @@ -113,8 +113,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-specifiers-comma/output.json b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-specifiers-comma/output.json index fd35a849ba58..ec9898e4ef40 100644 --- a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-specifiers-comma/output.json +++ b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-specifiers-comma/output.json @@ -162,8 +162,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-specifiers/output.json b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-specifiers/output.json index 8e3b2c7a05a0..cc54edc053cb 100644 --- a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-specifiers/output.json +++ b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-specifiers/output.json @@ -162,8 +162,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-namespace-specifier/output.json b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-namespace-specifier/output.json index 5fa52440ae2b..9c4d87ac49a4 100644 --- a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-namespace-specifier/output.json +++ b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-namespace-specifier/output.json @@ -96,8 +96,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-null-as-nil/output.json b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-null-as-nil/output.json index 4810416f47a3..d589597abfc8 100644 --- a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-null-as-nil/output.json +++ b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-null-as-nil/output.json @@ -113,8 +113,7 @@ "raw": "\"bar\"" }, "value": "bar" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-module-attributes/options.json b/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-module-attributes/options.json index 3f9b696c8de9..9acd1c25f418 100644 --- a/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-module-attributes/options.json +++ b/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-module-attributes/options.json @@ -1,5 +1,5 @@ { - "throws": "This experimental syntax requires enabling the parser plugin: 'moduleAttributes' (1:27)", + "throws": "This experimental syntax requires enabling the parser plugin: 'moduleAttributes' (1:32)", "sourceType": "module", "plugins": [] } \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes/output.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes/output.json index 490e4ee43d32..ca14238db509 100644 --- a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes/output.json +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes/output.json @@ -97,78 +97,63 @@ }, "value": "foo.json" }, - "attributes": { - "type": "ObjectExpression", - "start": 32, - "end": 44, - "loc": { - "start": { - "line": 1, - "column": 32 + "attributes": [ + { + "type": "ObjectProperty", + "start": 32, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 32 + }, + "end": { + "line": 1, + "column": 44 + } }, - "end": { - "line": 1, - "column": 44 - } - }, - "properties": [ - { - "type": "ObjectProperty", + "key": { + "type": "Identifier", "start": 32, - "end": 44, + "end": 36, "loc": { "start": { "line": 1, "column": 32 }, + "end": { + "line": 1, + "column": 36 + }, + "identifierName": "type" + }, + "name": "type" + }, + "computed": false, + "method": false, + "shorthand": false, + "value": { + "type": "StringLiteral", + "start": 38, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 38 + }, "end": { "line": 1, "column": 44 } }, - "method": false, - "key": { - "type": "Identifier", - "start": 32, - "end": 36, - "loc": { - "start": { - "line": 1, - "column": 32 - }, - "end": { - "line": 1, - "column": 36 - }, - "identifierName": "type" - }, - "name": "type" + "extra": { + "rawValue": "json", + "raw": "\"json\"" }, - "computed": false, - "shorthand": false, - "value": { - "type": "StringLiteral", - "start": 38, - "end": 44, - "loc": { - "start": { - "line": 1, - "column": 38 - }, - "end": { - "line": 1, - "column": 44 - } - }, - "extra": { - "rawValue": "json", - "raw": "\"json\"" - }, - "value": "json" - } + "value": "json" } - ] - } + } + ] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/flow/declare-module/import/output.json b/packages/babel-parser/test/fixtures/flow/declare-module/import/output.json index adfc27d63004..f7bb57172e32 100644 --- a/packages/babel-parser/test/fixtures/flow/declare-module/import/output.json +++ b/packages/babel-parser/test/fixtures/flow/declare-module/import/output.json @@ -146,8 +146,7 @@ "raw": "\"TM\"" }, "value": "TM" - }, - "attributes": [] + } }, { "type": "ImportDeclaration", @@ -217,8 +216,7 @@ "raw": "\"UM\"" }, "value": "UM" - }, - "attributes": [] + } } ] }, diff --git a/packages/babel-parser/test/fixtures/flow/declare-module/invalid-import/output.json b/packages/babel-parser/test/fixtures/flow/declare-module/invalid-import/output.json index 99001ac1179b..2d33b963c778 100644 --- a/packages/babel-parser/test/fixtures/flow/declare-module/invalid-import/output.json +++ b/packages/babel-parser/test/fixtures/flow/declare-module/invalid-import/output.json @@ -149,8 +149,7 @@ "raw": "\"TM\"" }, "value": "TM" - }, - "attributes": [] + } } ] }, diff --git a/packages/babel-parser/test/fixtures/flow/sourcetype-script/import/output.json b/packages/babel-parser/test/fixtures/flow/sourcetype-script/import/output.json index 8d8b0e2a4d33..d49e3470fffd 100644 --- a/packages/babel-parser/test/fixtures/flow/sourcetype-script/import/output.json +++ b/packages/babel-parser/test/fixtures/flow/sourcetype-script/import/output.json @@ -115,8 +115,7 @@ "raw": "\"\"" }, "value": "" - }, - "attributes": [] + } }, { "type": "ImportDeclaration", @@ -186,8 +185,7 @@ "raw": "\"\"" }, "value": "" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/flow/type-imports/import-type-2/output.json b/packages/babel-parser/test/fixtures/flow/type-imports/import-type-2/output.json index 543d787fa85d..f5fc078a42ce 100644 --- a/packages/babel-parser/test/fixtures/flow/type-imports/import-type-2/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-imports/import-type-2/output.json @@ -147,8 +147,7 @@ "raw": "\"bar\"" }, "value": "bar" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/flow/type-imports/import-type-shorthand/output.json b/packages/babel-parser/test/fixtures/flow/type-imports/import-type-shorthand/output.json index bb7c3d6a00ef..aeb7eded0711 100644 --- a/packages/babel-parser/test/fixtures/flow/type-imports/import-type-shorthand/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-imports/import-type-shorthand/output.json @@ -115,8 +115,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } }, { "type": "ImportDeclaration", @@ -204,8 +203,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } }, { "type": "ImportDeclaration", @@ -293,8 +291,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } }, { "type": "ImportDeclaration", @@ -382,8 +379,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } }, { "type": "ImportDeclaration", @@ -471,8 +467,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } }, { "type": "ImportDeclaration", @@ -560,8 +555,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } }, { "type": "ImportDeclaration", @@ -649,8 +643,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } }, { "type": "ImportDeclaration", @@ -738,8 +731,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } }, { "type": "ImportDeclaration", @@ -827,8 +819,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } }, { "type": "ImportDeclaration", @@ -916,8 +907,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/flow/type-imports/import-type/output.json b/packages/babel-parser/test/fixtures/flow/type-imports/import-type/output.json index 970128f4c86e..8e626c8ad0a5 100644 --- a/packages/babel-parser/test/fixtures/flow/type-imports/import-type/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-imports/import-type/output.json @@ -97,8 +97,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } }, { "type": "ImportDeclaration", @@ -186,8 +185,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } }, { "type": "ImportDeclaration", @@ -307,8 +305,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } }, { "type": "ImportDeclaration", @@ -378,8 +375,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } }, { "type": "ImportDeclaration", @@ -467,8 +463,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } }, { "type": "ImportDeclaration", @@ -606,8 +601,7 @@ "raw": "\"baz\"" }, "value": "baz" - }, - "attributes": [] + } }, { "type": "ImportDeclaration", @@ -677,8 +671,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } }, { "type": "ImportDeclaration", @@ -748,8 +741,7 @@ "raw": "\"bar\"" }, "value": "bar" - }, - "attributes": [] + } }, { "type": "ImportDeclaration", @@ -819,8 +811,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } }, { "type": "ImportDeclaration", @@ -908,8 +899,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } }, { "type": "ImportDeclaration", @@ -997,8 +987,7 @@ "raw": "\"baz\"" }, "value": "baz" - }, - "attributes": [] + } }, { "type": "ImportDeclaration", @@ -1068,8 +1057,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } }, { "type": "ImportDeclaration", @@ -1139,8 +1127,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-2/output.json b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-2/output.json index e752885d3e69..5ac98d7b46b7 100644 --- a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-2/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-2/output.json @@ -118,8 +118,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-3/output.json b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-3/output.json index 7923e739b575..3a1010d807fe 100644 --- a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-3/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-3/output.json @@ -100,8 +100,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-4/output.json b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-4/output.json index b6bc765c560b..dcbca6978a12 100644 --- a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-4/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-4/output.json @@ -100,8 +100,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-as/output.json b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-as/output.json index f137b4663de0..05cc09533a23 100644 --- a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-as/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-as/output.json @@ -118,8 +118,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand-2/output.json b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand-2/output.json index f14f3469465c..90585f1b4f80 100644 --- a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand-2/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand-2/output.json @@ -118,8 +118,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand-3/output.json b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand-3/output.json index b7da867ff7b0..4969f577a320 100644 --- a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand-3/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand-3/output.json @@ -118,8 +118,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand-4/output.json b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand-4/output.json index 47e4251b4bff..2b57e99a30e7 100644 --- a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand-4/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand-4/output.json @@ -118,8 +118,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand/output.json b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand/output.json index 1f96244b89f0..174e856304f7 100644 --- a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand/output.json @@ -118,8 +118,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type/output.json b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type/output.json index 569b6b3e5715..95872d960a5c 100644 --- a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type/output.json @@ -100,8 +100,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/placeholders/import/default-named/output.json b/packages/babel-parser/test/fixtures/placeholders/import/default-named/output.json index 69bc35d7d25e..62d764263d3d 100644 --- a/packages/babel-parser/test/fixtures/placeholders/import/default-named/output.json +++ b/packages/babel-parser/test/fixtures/placeholders/import/default-named/output.json @@ -177,8 +177,7 @@ "raw": "\"file\"" }, "value": "file" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/placeholders/import/default-star-2/output.json b/packages/babel-parser/test/fixtures/placeholders/import/default-star-2/output.json index 5f0045e738ec..6fd8a2dbed1f 100644 --- a/packages/babel-parser/test/fixtures/placeholders/import/default-star-2/output.json +++ b/packages/babel-parser/test/fixtures/placeholders/import/default-star-2/output.json @@ -144,8 +144,7 @@ "raw": "\"file\"" }, "value": "file" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/placeholders/import/file-empty/output.json b/packages/babel-parser/test/fixtures/placeholders/import/file-empty/output.json index 3f8e2f091bb1..5690ee81f00f 100644 --- a/packages/babel-parser/test/fixtures/placeholders/import/file-empty/output.json +++ b/packages/babel-parser/test/fixtures/placeholders/import/file-empty/output.json @@ -76,8 +76,7 @@ "name": "FILE" }, "expectedNode": "StringLiteral" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/placeholders/import/named-alias-2/output.json b/packages/babel-parser/test/fixtures/placeholders/import/named-alias-2/output.json index 6f15e484218f..c7d936e96c64 100644 --- a/packages/babel-parser/test/fixtures/placeholders/import/named-alias-2/output.json +++ b/packages/babel-parser/test/fixtures/placeholders/import/named-alias-2/output.json @@ -129,8 +129,7 @@ "raw": "\"file\"" }, "value": "file" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/placeholders/import/named-alias-3/output.json b/packages/babel-parser/test/fixtures/placeholders/import/named-alias-3/output.json index 73c301a18f0d..bfae4a044410 100644 --- a/packages/babel-parser/test/fixtures/placeholders/import/named-alias-3/output.json +++ b/packages/babel-parser/test/fixtures/placeholders/import/named-alias-3/output.json @@ -145,8 +145,7 @@ "raw": "\"file\"" }, "value": "file" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/placeholders/import/named-alias/output.json b/packages/babel-parser/test/fixtures/placeholders/import/named-alias/output.json index fedf25cb9509..5477bef4ca9e 100644 --- a/packages/babel-parser/test/fixtures/placeholders/import/named-alias/output.json +++ b/packages/babel-parser/test/fixtures/placeholders/import/named-alias/output.json @@ -129,8 +129,7 @@ "raw": "\"file\"" }, "value": "file" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/placeholders/import/named/output.json b/packages/babel-parser/test/fixtures/placeholders/import/named/output.json index cef9cf373dad..9b3f08486a88 100644 --- a/packages/babel-parser/test/fixtures/placeholders/import/named/output.json +++ b/packages/babel-parser/test/fixtures/placeholders/import/named/output.json @@ -145,8 +145,7 @@ "raw": "\"file\"" }, "value": "file" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/placeholders/import/star/output.json b/packages/babel-parser/test/fixtures/placeholders/import/star/output.json index b785c2b3f14b..7ff6bfd05846 100644 --- a/packages/babel-parser/test/fixtures/placeholders/import/star/output.json +++ b/packages/babel-parser/test/fixtures/placeholders/import/star/output.json @@ -112,8 +112,7 @@ "raw": "\"file\"" }, "value": "file" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/typescript/import/not-top-level/output.json b/packages/babel-parser/test/fixtures/typescript/import/not-top-level/output.json index e72c858cc59e..d8c247aa8f57 100644 --- a/packages/babel-parser/test/fixtures/typescript/import/not-top-level/output.json +++ b/packages/babel-parser/test/fixtures/typescript/import/not-top-level/output.json @@ -145,8 +145,7 @@ "raw": "\"a\"" }, "value": "a" - }, - "attributes": [] + } } ] }, diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-import-ambient-class/output.json b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-import-ambient-class/output.json index e642bced5b50..fe9e84eee59d 100644 --- a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-import-ambient-class/output.json +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-import-ambient-class/output.json @@ -96,8 +96,7 @@ "raw": "'./somewhere.js'" }, "value": "./somewhere.js" - }, - "attributes": [] + } }, { "type": "ClassDeclaration", From 4243ec10dc7dad03ecc45303f3a29fa270e23f12 Mon Sep 17 00:00:00 2001 From: Vivek Nayyar Date: Sat, 25 Jan 2020 19:40:53 +0100 Subject: [PATCH 03/63] :recycle: use new AST Node ImportAttribute for module attributes, tests updated and only allow string literals for attribute values --- .../babel-parser/src/parser/expression.js | 30 +-- packages/babel-parser/src/parser/statement.js | 29 ++- .../input.js | 2 + .../options.json | 8 + .../input.js | 2 + .../options.json | 4 + .../output.json | 177 ++++++++++++++++++ .../input.js | 3 + .../options.json | 4 + .../output.json | 125 +++++++++++++ .../valid-syntax-with-attributes/output.json | 5 +- .../valid-syntax-with-invalid-value/input.js | 1 + .../options.json | 7 + .../without-plugin/input.js | 1 + .../without-plugin/options.json | 4 + 15 files changed, 359 insertions(+), 43 deletions(-) create mode 100644 packages/babel-parser/test/fixtures/experimental/import-module-attributes/invalid-syntax-with-no-attributes-identifier/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/import-module-attributes/invalid-syntax-with-no-attributes-identifier/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-and-value/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-and-value/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-and-value/output.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-multiple-lines/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-multiple-lines/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-multiple-lines/output.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-value/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-value/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-module-attributes/without-plugin/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/import-module-attributes/without-plugin/options.json diff --git a/packages/babel-parser/src/parser/expression.js b/packages/babel-parser/src/parser/expression.js index 66608349f916..74f78c6047ac 100644 --- a/packages/babel-parser/src/parser/expression.js +++ b/packages/babel-parser/src/parser/expression.js @@ -852,35 +852,6 @@ export default class ExpressionParser extends LValParser { return this.parseSubscripts(this.parseExprAtom(), startPos, startLoc, true); } - /** - * Parse a primitive value - * primitive values are null, undefined, number, bigint, string, boolean - */ - parsePrimitiveValue(): N.Expression { - let node; - switch (this.state.type) { - case tt.num: - return this.parseLiteral(this.state.value, "NumericLiteral"); - - case tt.bigint: - return this.parseLiteral(this.state.value, "BigIntLiteral"); - - case tt.string: - return this.parseLiteral(this.state.value, "StringLiteral"); - - case tt._null: - node = this.startNode(); - this.next(); - return this.finishNode(node, "NullLiteral"); - - case tt._true: - case tt._false: - return this.parseBooleanLiteral(); - default: - throw this.unexpected(); - } - } - // Parse an atomic expression — either a single token that is an // expression, an expression started by a keyword like `function` or // `new`, or an expression wrapped in punctuation like `()`, `[]`, @@ -1505,6 +1476,7 @@ export default class ExpressionParser extends LValParser { this.next(); return this.finishNode(node, "TemplateLiteral"); } + // Parse an object literal or binding pattern. parseObj( diff --git a/packages/babel-parser/src/parser/statement.js b/packages/babel-parser/src/parser/statement.js index feaa08c46b76..ef265eb14f70 100644 --- a/packages/babel-parser/src/parser/statement.js +++ b/packages/babel-parser/src/parser/statement.js @@ -2038,20 +2038,23 @@ export default class StatementParser extends ExpressionParser { * import { get } from "axios"; * but if we do have a default import * we need to check if we have a comma after that and - * that is where this || this.eat comes into play + * that is where this `|| this.eat` condition comes into play */ const parseNext = !hasDefault || this.eat(tt.comma); - // if we do have to parse the next set of specifiers, we first check for start imports + // if we do have to parse the next set of specifiers, we first check for star imports // import React, * from "react"; const hasStar = parseNext && this.maybeParseStarImportSpecifier(node); - // now we check that if we need to parse the next imports - // but only if he/she is not importing * which means all + // now we check if we need to parse the next imports + // but only if they are not importing * (everything) if (parseNext && !hasStar) this.parseNamedImportSpecifiers(node); this.expectContextual("from"); } node.source = this.parseImportSource(); + // new proposal + // https://github.com/tc39/proposal-module-attributes + // parse module attributes if the next token is with or ignore and finish the ImportDeclaration node. const attributes = this.maybeParseModuleAttributes(); - if (Array.isArray(attributes)) { + if (attributes) { node.attributes = attributes; } this.semicolon(); @@ -2092,14 +2095,20 @@ export default class StatementParser extends ExpressionParser { this.expectPlugin("moduleAttributes"); const attrs = []; do { + // we are trying to parse a node which has the following syntax + // with type: "json" + // [with -> keyword], [type -> Identifier], [":" -> token for colon], ["json" -> StringLiteral] const node = this.startNode(); node.key = this.parseIdentifier(); - node.computed = false; - node.method = false; - node.shorthand = false; this.expect(tt.colon); - node.value = this.parsePrimitiveValue(); - this.finishNode(node, "ObjectProperty"); + if (!this.match(tt.string)) { + return this.unexpected( + this.state.start, + "Only string literal values are allowed", + ); + } + node.value = this.parseLiteral(this.state.value, "StringLiteral"); + this.finishNode(node, "ImportAttribute"); attrs.push(node); } while (this.eat(tt.comma)); return attrs; diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/invalid-syntax-with-no-attributes-identifier/input.js b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/invalid-syntax-with-no-attributes-identifier/input.js new file mode 100644 index 000000000000..bfa86f1acde6 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/invalid-syntax-with-no-attributes-identifier/input.js @@ -0,0 +1,2 @@ +import "x" +with ({}); diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/invalid-syntax-with-no-attributes-identifier/options.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/invalid-syntax-with-no-attributes-identifier/options.json new file mode 100644 index 000000000000..77e11f22643a --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/invalid-syntax-with-no-attributes-identifier/options.json @@ -0,0 +1,8 @@ +{ + "plugins": [ + "moduleAttributes", + "estree" + ], + "sourceType": "module", + "throws": "Unexpected token (2:5)" +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-and-value/input.js b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-and-value/input.js new file mode 100644 index 000000000000..ec62816617cd --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-and-value/input.js @@ -0,0 +1,2 @@ +import "x" with type: "json" +[0] diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-and-value/options.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-and-value/options.json new file mode 100644 index 000000000000..9399e0bedd3f --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-and-value/options.json @@ -0,0 +1,4 @@ +{ + "plugins": ["moduleAttributes"], + "sourceType": "module" +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-and-value/output.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-and-value/output.json new file mode 100644 index 000000000000..b8c8d5f9cff4 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-and-value/output.json @@ -0,0 +1,177 @@ +{ + "type": "File", + "start": 0, + "end": 32, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 3 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 32, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 3 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start": 0, + "end": 28, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 28 + } + }, + "specifiers": [], + "source": { + "type": "StringLiteral", + "start": 7, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "extra": { + "rawValue": "x", + "raw": "\"x\"" + }, + "value": "x" + }, + "attributes": [ + { + "type": "ImportAttribute", + "start": 16, + "end": 28, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 28 + } + }, + "key": { + "type": "Identifier", + "start": 16, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 20 + }, + "identifierName": "type" + }, + "name": "type" + }, + "value": { + "type": "StringLiteral", + "start": 22, + "end": 28, + "loc": { + "start": { + "line": 1, + "column": 22 + }, + "end": { + "line": 1, + "column": 28 + } + }, + "extra": { + "rawValue": "json", + "raw": "\"json\"" + }, + "value": "json" + } + } + ] + }, + { + "type": "ExpressionStatement", + "start": 29, + "end": 32, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 3 + } + }, + "expression": { + "type": "ArrayExpression", + "start": 29, + "end": 32, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 3 + } + }, + "elements": [ + { + "type": "NumericLiteral", + "start": 30, + "end": 31, + "loc": { + "start": { + "line": 2, + "column": 1 + }, + "end": { + "line": 2, + "column": 2 + } + }, + "extra": { + "rawValue": 0, + "raw": "0" + }, + "value": 0 + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-multiple-lines/input.js b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-multiple-lines/input.js new file mode 100644 index 000000000000..506d38a7ee01 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-multiple-lines/input.js @@ -0,0 +1,3 @@ +import "x" +with +type: "json" diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-multiple-lines/options.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-multiple-lines/options.json new file mode 100644 index 000000000000..9399e0bedd3f --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-multiple-lines/options.json @@ -0,0 +1,4 @@ +{ + "plugins": ["moduleAttributes"], + "sourceType": "module" +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-multiple-lines/output.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-multiple-lines/output.json new file mode 100644 index 000000000000..7ab9b34a1162 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-multiple-lines/output.json @@ -0,0 +1,125 @@ +{ + "type": "File", + "start": 0, + "end": 28, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 12 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 28, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 12 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start": 0, + "end": 28, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 12 + } + }, + "specifiers": [], + "source": { + "type": "StringLiteral", + "start": 7, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "extra": { + "rawValue": "x", + "raw": "\"x\"" + }, + "value": "x" + }, + "attributes": [ + { + "type": "ImportAttribute", + "start": 16, + "end": 28, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 12 + } + }, + "key": { + "type": "Identifier", + "start": 16, + "end": 20, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 4 + }, + "identifierName": "type" + }, + "name": "type" + }, + "value": { + "type": "StringLiteral", + "start": 22, + "end": 28, + "loc": { + "start": { + "line": 3, + "column": 6 + }, + "end": { + "line": 3, + "column": 12 + } + }, + "extra": { + "rawValue": "json", + "raw": "\"json\"" + }, + "value": "json" + } + } + ] + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes/output.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes/output.json index ca14238db509..d2db79b27ddb 100644 --- a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes/output.json +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes/output.json @@ -99,7 +99,7 @@ }, "attributes": [ { - "type": "ObjectProperty", + "type": "ImportAttribute", "start": 32, "end": 44, "loc": { @@ -129,9 +129,6 @@ }, "name": "type" }, - "computed": false, - "method": false, - "shorthand": false, "value": { "type": "StringLiteral", "start": 38, diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-value/input.js b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-value/input.js new file mode 100644 index 000000000000..cfb5372f6b12 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-value/input.js @@ -0,0 +1 @@ +import foo from "foo.json" with type: "json", lazy: true, startAtLine: 1; diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-value/options.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-value/options.json new file mode 100644 index 000000000000..b4911175a067 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-value/options.json @@ -0,0 +1,7 @@ +{ + "plugins": [ + "moduleAttributes" + ], + "sourceType": "module", + "throws": "Only string literal values are allowed (1:52)" +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/without-plugin/input.js b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/without-plugin/input.js new file mode 100644 index 000000000000..fb31508a0860 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/without-plugin/input.js @@ -0,0 +1 @@ +import foo from "foo.json" with type: "json"; diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/without-plugin/options.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/without-plugin/options.json new file mode 100644 index 000000000000..d5829ef69927 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/without-plugin/options.json @@ -0,0 +1,4 @@ +{ + "throws": "This experimental syntax requires enabling the parser plugin: 'moduleAttributes' (1:32)", + "plugins": [] +} \ No newline at end of file From 14aae84e59fbf35e664f2a4ed6c5914a8b9cae66 Mon Sep 17 00:00:00 2001 From: Vivek Nayyar Date: Wed, 11 Mar 2020 23:23:50 +0100 Subject: [PATCH 04/63] chore: fixed the eslint issue and sorted the errors in ascending order --- packages/babel-parser/src/parser/location.js | 3 +++ packages/babel-parser/src/parser/statement.js | 24 +++++++++++++++++-- .../input.js | 1 + .../options.json | 7 ++++++ .../valid-syntax-with-repeated-type/input.js | 1 + .../options.json | 7 ++++++ 6 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-no-type-attribute/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-no-type-attribute/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-repeated-type/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-repeated-type/options.json diff --git a/packages/babel-parser/src/parser/location.js b/packages/babel-parser/src/parser/location.js index fc548b0b726e..c9cd755f73f0 100644 --- a/packages/babel-parser/src/parser/location.js +++ b/packages/babel-parser/src/parser/location.js @@ -98,6 +98,9 @@ export const Errors = Object.freeze({ MissingUnicodeEscape: "Expecting Unicode escape sequence \\uXXXX", MixingCoalesceWithLogical: "Nullish coalescing operator(??) requires parens when mixing with logical operators", + ModuleAttributesWithRepeatedType: + "Module attributes should include only one type", + ModuleAttributesWithoutType: "Module attributes should include a type", ModuleExportUndefined: "Export '%0' is not defined", MultipleDefaultsInSwitch: "Multiple default clauses", NewlineAfterThrow: "Illegal newline after throw", diff --git a/packages/babel-parser/src/parser/statement.js b/packages/babel-parser/src/parser/statement.js index 9697d280cec2..1b6aafde3c4b 100644 --- a/packages/babel-parser/src/parser/statement.js +++ b/packages/babel-parser/src/parser/statement.js @@ -2026,7 +2026,7 @@ export default class StatementParser extends ExpressionParser { node.source = this.parseImportSource(); // new proposal // https://github.com/tc39/proposal-module-attributes - // parse module attributes if the next token is with or ignore and finish the ImportDeclaration node. + // parse module attributes if the next token is `with` or ignore and finish the ImportDeclaration node. const attributes = this.maybeParseModuleAttributes(); if (attributes) { node.attributes = attributes; @@ -2068,13 +2068,28 @@ export default class StatementParser extends ExpressionParser { } this.expectPlugin("moduleAttributes"); const attrs = []; + let hasTypeAttribute = false; do { // we are trying to parse a node which has the following syntax // with type: "json" // [with -> keyword], [type -> Identifier], [":" -> token for colon], ["json" -> StringLiteral] const node = this.startNode(); - node.key = this.parseIdentifier(); + node.key = this.parseIdentifier(true); + + // check if we have a type attribute + if (node.key.name === "type") { + // check if we do not have two duplicate type attributes defined + if (hasTypeAttribute) { + throw this.raise( + this.state.start, + Errors.ModuleAttributesWithRepeatedType, + ); + } + hasTypeAttribute = true; + } + // check for colon this.expect(tt.colon); + // check if the value set to the module attribute is a string as we only allow string literals if (!this.match(tt.string)) { return this.unexpected( this.state.start, @@ -2085,6 +2100,11 @@ export default class StatementParser extends ExpressionParser { this.finishNode(node, "ImportAttribute"); attrs.push(node); } while (this.eat(tt.comma)); + + // check if module attributes do not contain any type key and throw an error + if (!hasTypeAttribute) { + throw this.raise(this.state.start, Errors.ModuleAttributesWithoutType); + } return attrs; } diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-no-type-attribute/input.js b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-no-type-attribute/input.js new file mode 100644 index 000000000000..c2ae7496dcb7 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-no-type-attribute/input.js @@ -0,0 +1 @@ +import foo from "foo.json" with lazy: "true"; diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-no-type-attribute/options.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-no-type-attribute/options.json new file mode 100644 index 000000000000..1e4da5a2f09b --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-no-type-attribute/options.json @@ -0,0 +1,7 @@ +{ + "plugins": [ + "moduleAttributes" + ], + "sourceType": "module", + "throws": "Module attributes should include a type (1:44)" +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-repeated-type/input.js b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-repeated-type/input.js new file mode 100644 index 000000000000..f742d7cc15d1 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-repeated-type/input.js @@ -0,0 +1 @@ +import foo from "foo.json" with type: "json", lazy: "true", type: "html"; diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-repeated-type/options.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-repeated-type/options.json new file mode 100644 index 000000000000..44fae505d71c --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-repeated-type/options.json @@ -0,0 +1,7 @@ +{ + "plugins": [ + "moduleAttributes" + ], + "sourceType": "module", + "throws": "Module attributes should include only one type (1:64)" +} \ No newline at end of file From 8c4ecf99a3879971c48a125fdc1fab9e10cd11b6 Mon Sep 17 00:00:00 2001 From: Vivek Nayyar Date: Wed, 11 Mar 2020 23:53:12 +0100 Subject: [PATCH 05/63] improvement: added support to throw error if duplicate attributes found --- packages/babel-parser/src/parser/location.js | 4 ++-- packages/babel-parser/src/parser/statement.js | 23 +++++++++---------- .../options.json | 2 +- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/packages/babel-parser/src/parser/location.js b/packages/babel-parser/src/parser/location.js index c9cd755f73f0..84c08adfdea5 100644 --- a/packages/babel-parser/src/parser/location.js +++ b/packages/babel-parser/src/parser/location.js @@ -98,8 +98,8 @@ export const Errors = Object.freeze({ MissingUnicodeEscape: "Expecting Unicode escape sequence \\uXXXX", MixingCoalesceWithLogical: "Nullish coalescing operator(??) requires parens when mixing with logical operators", - ModuleAttributesWithRepeatedType: - "Module attributes should include only one type", + ModuleAttributesWithDuplicateKeys: + "Module attributes should not include duplicate keys", ModuleAttributesWithoutType: "Module attributes should include a type", ModuleExportUndefined: "Export '%0' is not defined", MultipleDefaultsInSwitch: "Multiple default clauses", diff --git a/packages/babel-parser/src/parser/statement.js b/packages/babel-parser/src/parser/statement.js index 1b6aafde3c4b..80494df61502 100644 --- a/packages/babel-parser/src/parser/statement.js +++ b/packages/babel-parser/src/parser/statement.js @@ -2068,7 +2068,7 @@ export default class StatementParser extends ExpressionParser { } this.expectPlugin("moduleAttributes"); const attrs = []; - let hasTypeAttribute = false; + const attributeMap = {}; do { // we are trying to parse a node which has the following syntax // with type: "json" @@ -2076,17 +2076,16 @@ export default class StatementParser extends ExpressionParser { const node = this.startNode(); node.key = this.parseIdentifier(true); - // check if we have a type attribute - if (node.key.name === "type") { - // check if we do not have two duplicate type attributes defined - if (hasTypeAttribute) { - throw this.raise( - this.state.start, - Errors.ModuleAttributesWithRepeatedType, - ); - } - hasTypeAttribute = true; + // check if we already have an entry for an attribute + // if a duplicate entry found, throw an error + if (attributeMap[node.key.name]) { + throw this.raise( + this.state.start, + Errors.ModuleAttributesWithDuplicateKeys, + ); } + // set the attribute name entry in the attributeMap to true + attributeMap[node.key.name] = true; // check for colon this.expect(tt.colon); // check if the value set to the module attribute is a string as we only allow string literals @@ -2102,7 +2101,7 @@ export default class StatementParser extends ExpressionParser { } while (this.eat(tt.comma)); // check if module attributes do not contain any type key and throw an error - if (!hasTypeAttribute) { + if (!attributeMap["type"]) { throw this.raise(this.state.start, Errors.ModuleAttributesWithoutType); } return attrs; diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-repeated-type/options.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-repeated-type/options.json index 44fae505d71c..0c6a87381963 100644 --- a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-repeated-type/options.json +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-repeated-type/options.json @@ -3,5 +3,5 @@ "moduleAttributes" ], "sourceType": "module", - "throws": "Module attributes should include only one type (1:64)" + "throws": "Module attributes should not include duplicate keys (1:64)" } \ No newline at end of file From bbb6020e15b8c19a1601e14206103ce81cdfec2d Mon Sep 17 00:00:00 2001 From: Vivek Nayyar Date: Thu, 12 Mar 2020 00:08:51 +0100 Subject: [PATCH 06/63] chore: fixed the failing test of babel plugin dotall regex --- .../dotall-regex/with-unicode-property-escape/output.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/babel-plugin-transform-dotall-regex/test/fixtures/dotall-regex/with-unicode-property-escape/output.js b/packages/babel-plugin-transform-dotall-regex/test/fixtures/dotall-regex/with-unicode-property-escape/output.js index a8ddf757d926..c00aa15a9c1f 100644 --- a/packages/babel-plugin-transform-dotall-regex/test/fixtures/dotall-regex/with-unicode-property-escape/output.js +++ b/packages/babel-plugin-transform-dotall-regex/test/fixtures/dotall-regex/with-unicode-property-escape/output.js @@ -1,2 +1,2 @@ -var a = /[\u3400-\u4DB5\u4E00-\u9FEF\uFA0E\uFA0F\uFA11\uFA13\uFA14\uFA1F\uFA21\uFA23\uFA24\uFA27-\uFA29\u{20000}-\u{2A6D6}\u{2A700}-\u{2B734}\u{2B740}-\u{2B81D}\u{2B820}-\u{2CEA1}\u{2CEB0}-\u{2EBE0}][\0-\t\x0B\f\x0E-\u2027\u202A-\u{10FFFF}]/u; -var b = /[\u3400-\u4DB5\u4E00-\u9FEF\uFA0E\uFA0F\uFA11\uFA13\uFA14\uFA1F\uFA21\uFA23\uFA24\uFA27-\uFA29\u{20000}-\u{2A6D6}\u{2A700}-\u{2B734}\u{2B740}-\u{2B81D}\u{2B820}-\u{2CEA1}\u{2CEB0}-\u{2EBE0}][\0-\u{10FFFF}]/u; +var a = /[\u3400-\u4DBF\u4E00-\u9FFC\uFA0E\uFA0F\uFA11\uFA13\uFA14\uFA1F\uFA21\uFA23\uFA24\uFA27-\uFA29\u{20000}-\u{2A6DD}\u{2A700}-\u{2B734}\u{2B740}-\u{2B81D}\u{2B820}-\u{2CEA1}\u{2CEB0}-\u{2EBE0}\u{30000}-\u{3134A}][\0-\t\x0B\f\x0E-\u2027\u202A-\u{10FFFF}]/u; +var b = /[\u3400-\u4DBF\u4E00-\u9FFC\uFA0E\uFA0F\uFA11\uFA13\uFA14\uFA1F\uFA21\uFA23\uFA24\uFA27-\uFA29\u{20000}-\u{2A6DD}\u{2A700}-\u{2B734}\u{2B740}-\u{2B81D}\u{2B820}-\u{2CEA1}\u{2CEB0}-\u{2EBE0}\u{30000}-\u{3134A}][\0-\u{10FFFF}]/u; From ba5482e7d4a7a1b8604cf44c975aac3c5e35057e Mon Sep 17 00:00:00 2001 From: Vivek Nayyar Date: Thu, 12 Mar 2020 22:27:43 +0100 Subject: [PATCH 07/63] improvement: fixed error messages, confition to check presence of with rectified and tests updated --- packages/babel-parser/src/parser/location.js | 4 +- packages/babel-parser/src/parser/statement.js | 15 +- .../import-module-attributes/options.json | 2 +- .../options.json | 2 +- .../options.json | 9 +- .../output.json | 265 ++++++++++++++++++ .../without-plugin/options.json | 2 +- 7 files changed, 283 insertions(+), 16 deletions(-) create mode 100644 packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-repeated-type/output.json diff --git a/packages/babel-parser/src/parser/location.js b/packages/babel-parser/src/parser/location.js index 84c08adfdea5..c5a18ff33504 100644 --- a/packages/babel-parser/src/parser/location.js +++ b/packages/babel-parser/src/parser/location.js @@ -98,8 +98,10 @@ export const Errors = Object.freeze({ MissingUnicodeEscape: "Expecting Unicode escape sequence \\uXXXX", MixingCoalesceWithLogical: "Nullish coalescing operator(??) requires parens when mixing with logical operators", + ModuleAttributesIncorrectValue: + "Only string literals are allowed as module attributes values", ModuleAttributesWithDuplicateKeys: - "Module attributes should not include duplicate keys", + "Module attributes should not include duplicate keys (%0)", ModuleAttributesWithoutType: "Module attributes should include a type", ModuleExportUndefined: "Export '%0' is not defined", MultipleDefaultsInSwitch: "Multiple default clauses", diff --git a/packages/babel-parser/src/parser/statement.js b/packages/babel-parser/src/parser/statement.js index 80494df61502..16c19c4f955c 100644 --- a/packages/babel-parser/src/parser/statement.js +++ b/packages/babel-parser/src/parser/statement.js @@ -2062,13 +2062,15 @@ export default class StatementParser extends ExpressionParser { } maybeParseModuleAttributes() { - if (!this.eat(tt._with)) { + if (this.match(tt._with)) { + this.expectPlugin("moduleAttributes"); + this.next(); + } else { if (this.hasPlugin("moduleAttributes")) return []; return null; } - this.expectPlugin("moduleAttributes"); const attrs = []; - const attributeMap = {}; + const attributeMap = Object.create(null); do { // we are trying to parse a node which has the following syntax // with type: "json" @@ -2079,9 +2081,10 @@ export default class StatementParser extends ExpressionParser { // check if we already have an entry for an attribute // if a duplicate entry found, throw an error if (attributeMap[node.key.name]) { - throw this.raise( + this.raise( this.state.start, Errors.ModuleAttributesWithDuplicateKeys, + node.key.name, ); } // set the attribute name entry in the attributeMap to true @@ -2090,9 +2093,9 @@ export default class StatementParser extends ExpressionParser { this.expect(tt.colon); // check if the value set to the module attribute is a string as we only allow string literals if (!this.match(tt.string)) { - return this.unexpected( + throw this.unexpected( this.state.start, - "Only string literal values are allowed", + Errors.ModuleAttributesIncorrectValue, ); } node.value = this.parseLiteral(this.state.value, "StringLiteral"); diff --git a/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-module-attributes/options.json b/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-module-attributes/options.json index 9acd1c25f418..3f9b696c8de9 100644 --- a/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-module-attributes/options.json +++ b/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-module-attributes/options.json @@ -1,5 +1,5 @@ { - "throws": "This experimental syntax requires enabling the parser plugin: 'moduleAttributes' (1:32)", + "throws": "This experimental syntax requires enabling the parser plugin: 'moduleAttributes' (1:27)", "sourceType": "module", "plugins": [] } \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-value/options.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-value/options.json index b4911175a067..31ae88c8c2b1 100644 --- a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-value/options.json +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-value/options.json @@ -3,5 +3,5 @@ "moduleAttributes" ], "sourceType": "module", - "throws": "Only string literal values are allowed (1:52)" + "throws": "Only string literals are allowed as module attributes values (1:52)" } \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-repeated-type/options.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-repeated-type/options.json index 0c6a87381963..9399e0bedd3f 100644 --- a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-repeated-type/options.json +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-repeated-type/options.json @@ -1,7 +1,4 @@ { - "plugins": [ - "moduleAttributes" - ], - "sourceType": "module", - "throws": "Module attributes should not include duplicate keys (1:64)" -} \ No newline at end of file + "plugins": ["moduleAttributes"], + "sourceType": "module" +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-repeated-type/output.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-repeated-type/output.json new file mode 100644 index 000000000000..0bb5aebcc7ce --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-repeated-type/output.json @@ -0,0 +1,265 @@ +{ + "type": "File", + "start": 0, + "end": 73, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 73 + } + }, + "errors": [ + "SyntaxError: Module attributes should not include duplicate keys (type) (1:64)" + ], + "program": { + "type": "Program", + "start": 0, + "end": 73, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 73 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start": 0, + "end": 73, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 73 + } + }, + "specifiers": [ + { + "type": "ImportDefaultSpecifier", + "start": 7, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "local": { + "type": "Identifier", + "start": 7, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 10 + }, + "identifierName": "foo" + }, + "name": "foo" + } + } + ], + "source": { + "type": "StringLiteral", + "start": 16, + "end": 26, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 26 + } + }, + "extra": { + "rawValue": "foo.json", + "raw": "\"foo.json\"" + }, + "value": "foo.json" + }, + "attributes": [ + { + "type": "ImportAttribute", + "start": 32, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 32 + }, + "end": { + "line": 1, + "column": 44 + } + }, + "key": { + "type": "Identifier", + "start": 32, + "end": 36, + "loc": { + "start": { + "line": 1, + "column": 32 + }, + "end": { + "line": 1, + "column": 36 + }, + "identifierName": "type" + }, + "name": "type" + }, + "value": { + "type": "StringLiteral", + "start": 38, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 38 + }, + "end": { + "line": 1, + "column": 44 + } + }, + "extra": { + "rawValue": "json", + "raw": "\"json\"" + }, + "value": "json" + } + }, + { + "type": "ImportAttribute", + "start": 46, + "end": 58, + "loc": { + "start": { + "line": 1, + "column": 46 + }, + "end": { + "line": 1, + "column": 58 + } + }, + "key": { + "type": "Identifier", + "start": 46, + "end": 50, + "loc": { + "start": { + "line": 1, + "column": 46 + }, + "end": { + "line": 1, + "column": 50 + }, + "identifierName": "lazy" + }, + "name": "lazy" + }, + "value": { + "type": "StringLiteral", + "start": 52, + "end": 58, + "loc": { + "start": { + "line": 1, + "column": 52 + }, + "end": { + "line": 1, + "column": 58 + } + }, + "extra": { + "rawValue": "true", + "raw": "\"true\"" + }, + "value": "true" + } + }, + { + "type": "ImportAttribute", + "start": 60, + "end": 72, + "loc": { + "start": { + "line": 1, + "column": 60 + }, + "end": { + "line": 1, + "column": 72 + } + }, + "key": { + "type": "Identifier", + "start": 60, + "end": 64, + "loc": { + "start": { + "line": 1, + "column": 60 + }, + "end": { + "line": 1, + "column": 64 + }, + "identifierName": "type" + }, + "name": "type" + }, + "value": { + "type": "StringLiteral", + "start": 66, + "end": 72, + "loc": { + "start": { + "line": 1, + "column": 66 + }, + "end": { + "line": 1, + "column": 72 + } + }, + "extra": { + "rawValue": "html", + "raw": "\"html\"" + }, + "value": "html" + } + } + ] + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/without-plugin/options.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/without-plugin/options.json index d5829ef69927..8645af2af8c7 100644 --- a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/without-plugin/options.json +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/without-plugin/options.json @@ -1,4 +1,4 @@ { - "throws": "This experimental syntax requires enabling the parser plugin: 'moduleAttributes' (1:32)", + "throws": "This experimental syntax requires enabling the parser plugin: 'moduleAttributes' (1:27)", "plugins": [] } \ No newline at end of file From a03860197be1b50d2b7acb438661eb55c0db5dc8 Mon Sep 17 00:00:00 2001 From: Vivek Nayyar Date: Thu, 12 Mar 2020 22:35:58 +0100 Subject: [PATCH 08/63] test: added a new test for allowing hasOwnProperty as one of the module attributes --- .../input.js | 1 + .../options.json | 4 + .../output.json | 210 ++++++++++++++++++ 3 files changed, 215 insertions(+) create mode 100644 packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-object-method-attribute/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-object-method-attribute/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-object-method-attribute/output.json diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-object-method-attribute/input.js b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-object-method-attribute/input.js new file mode 100644 index 000000000000..d027b991db49 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-object-method-attribute/input.js @@ -0,0 +1 @@ +import foo from "foo.json" with type: "json", hasOwnProperty: "true"; diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-object-method-attribute/options.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-object-method-attribute/options.json new file mode 100644 index 000000000000..9399e0bedd3f --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-object-method-attribute/options.json @@ -0,0 +1,4 @@ +{ + "plugins": ["moduleAttributes"], + "sourceType": "module" +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-object-method-attribute/output.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-object-method-attribute/output.json new file mode 100644 index 000000000000..dd61984b92cf --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-object-method-attribute/output.json @@ -0,0 +1,210 @@ +{ + "type": "File", + "start": 0, + "end": 69, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 69 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 69, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 69 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start": 0, + "end": 69, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 69 + } + }, + "specifiers": [ + { + "type": "ImportDefaultSpecifier", + "start": 7, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "local": { + "type": "Identifier", + "start": 7, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 10 + }, + "identifierName": "foo" + }, + "name": "foo" + } + } + ], + "source": { + "type": "StringLiteral", + "start": 16, + "end": 26, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 26 + } + }, + "extra": { + "rawValue": "foo.json", + "raw": "\"foo.json\"" + }, + "value": "foo.json" + }, + "attributes": [ + { + "type": "ImportAttribute", + "start": 32, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 32 + }, + "end": { + "line": 1, + "column": 44 + } + }, + "key": { + "type": "Identifier", + "start": 32, + "end": 36, + "loc": { + "start": { + "line": 1, + "column": 32 + }, + "end": { + "line": 1, + "column": 36 + }, + "identifierName": "type" + }, + "name": "type" + }, + "value": { + "type": "StringLiteral", + "start": 38, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 38 + }, + "end": { + "line": 1, + "column": 44 + } + }, + "extra": { + "rawValue": "json", + "raw": "\"json\"" + }, + "value": "json" + } + }, + { + "type": "ImportAttribute", + "start": 46, + "end": 68, + "loc": { + "start": { + "line": 1, + "column": 46 + }, + "end": { + "line": 1, + "column": 68 + } + }, + "key": { + "type": "Identifier", + "start": 46, + "end": 60, + "loc": { + "start": { + "line": 1, + "column": 46 + }, + "end": { + "line": 1, + "column": 60 + }, + "identifierName": "hasOwnProperty" + }, + "name": "hasOwnProperty" + }, + "value": { + "type": "StringLiteral", + "start": 62, + "end": 68, + "loc": { + "start": { + "line": 1, + "column": 62 + }, + "end": { + "line": 1, + "column": 68 + } + }, + "extra": { + "rawValue": "true", + "raw": "\"true\"" + }, + "value": "true" + } + } + ] + } + ], + "directives": [] + } +} \ No newline at end of file From cde4f92fa5c01c8f1cac9cbf7abb26027d97edd7 Mon Sep 17 00:00:00 2001 From: Vivek Nayyar Date: Thu, 12 Mar 2020 23:42:12 +0100 Subject: [PATCH 09/63] chore: added a mandatory version option to use along with module attributes plugin. tests updated --- packages/babel-parser/src/plugin-utils.js | 14 ++++++++++++++ .../options.json | 9 +++++++-- .../options.json | 9 ++++++++- .../options.json | 9 ++++++++- .../valid-syntax-with-attributes/options.json | 9 ++++++++- .../input.js | 1 + .../options.json | 12 ++++++++++++ .../valid-syntax-with-invalid-value/options.json | 9 +++++++-- .../options.json | 9 +++++++-- .../options.json | 9 ++++++++- .../valid-syntax-with-repeated-type/options.json | 9 ++++++++- .../valid-syntax-without-attributes/options.json | 9 ++++++++- 12 files changed, 96 insertions(+), 12 deletions(-) create mode 100644 packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-plugin-option/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-plugin-option/options.json diff --git a/packages/babel-parser/src/plugin-utils.js b/packages/babel-parser/src/plugin-utils.js index 2cf522532197..657d86813f71 100644 --- a/packages/babel-parser/src/plugin-utils.js +++ b/packages/babel-parser/src/plugin-utils.js @@ -84,6 +84,20 @@ export function validatePlugins(plugins: PluginList) { PIPELINE_PROPOSALS.map(p => `'${p}'`).join(", "), ); } + + if (hasPlugin(plugins, "moduleAttributes")) { + const moduleAttributesVerionPluginOption = getPluginOption( + plugins, + "moduleAttributes", + "version", + ); + if (moduleAttributesVerionPluginOption !== "feb-2020") { + throw new Error( + "The 'moduleAttributes' plugin requires a 'version' option," + + " whose value must be feb-2020.", + ); + } + } } // These plugins are defined using a mixin which extends the parser class. diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/invalid-syntax-with-no-attributes-identifier/options.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/invalid-syntax-with-no-attributes-identifier/options.json index 77e11f22643a..cbb08d0c69a4 100644 --- a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/invalid-syntax-with-no-attributes-identifier/options.json +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/invalid-syntax-with-no-attributes-identifier/options.json @@ -1,8 +1,13 @@ { "plugins": [ - "moduleAttributes", + [ + "moduleAttributes", + { + "version": "feb-2020" + } + ], "estree" ], "sourceType": "module", "throws": "Unexpected token (2:5)" -} \ No newline at end of file +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-and-value/options.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-and-value/options.json index 9399e0bedd3f..442f499887f3 100644 --- a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-and-value/options.json +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-and-value/options.json @@ -1,4 +1,11 @@ { - "plugins": ["moduleAttributes"], + "plugins": [ + [ + "moduleAttributes", + { + "version": "feb-2020" + } + ] + ], "sourceType": "module" } diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-multiple-lines/options.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-multiple-lines/options.json index 9399e0bedd3f..442f499887f3 100644 --- a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-multiple-lines/options.json +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-multiple-lines/options.json @@ -1,4 +1,11 @@ { - "plugins": ["moduleAttributes"], + "plugins": [ + [ + "moduleAttributes", + { + "version": "feb-2020" + } + ] + ], "sourceType": "module" } diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes/options.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes/options.json index 9399e0bedd3f..442f499887f3 100644 --- a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes/options.json +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes/options.json @@ -1,4 +1,11 @@ { - "plugins": ["moduleAttributes"], + "plugins": [ + [ + "moduleAttributes", + { + "version": "feb-2020" + } + ] + ], "sourceType": "module" } diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-plugin-option/input.js b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-plugin-option/input.js new file mode 100644 index 000000000000..fb31508a0860 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-plugin-option/input.js @@ -0,0 +1 @@ +import foo from "foo.json" with type: "json"; diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-plugin-option/options.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-plugin-option/options.json new file mode 100644 index 000000000000..92566a76e0f3 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-plugin-option/options.json @@ -0,0 +1,12 @@ +{ + "plugins": [ + [ + "moduleAttributes", + { + "version": "jan-2020" + } + ] + ], + "sourceType": "module", + "throws": "The 'moduleAttributes' plugin requires a 'version' option, whose value must be feb-2020." +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-value/options.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-value/options.json index 31ae88c8c2b1..b28c434f1369 100644 --- a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-value/options.json +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-value/options.json @@ -1,7 +1,12 @@ { "plugins": [ - "moduleAttributes" + [ + "moduleAttributes", + { + "version": "feb-2020" + } + ] ], "sourceType": "module", "throws": "Only string literals are allowed as module attributes values (1:52)" -} \ No newline at end of file +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-no-type-attribute/options.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-no-type-attribute/options.json index 1e4da5a2f09b..d207edb90981 100644 --- a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-no-type-attribute/options.json +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-no-type-attribute/options.json @@ -1,7 +1,12 @@ { "plugins": [ - "moduleAttributes" + [ + "moduleAttributes", + { + "version": "feb-2020" + } + ] ], "sourceType": "module", "throws": "Module attributes should include a type (1:44)" -} \ No newline at end of file +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-object-method-attribute/options.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-object-method-attribute/options.json index 9399e0bedd3f..442f499887f3 100644 --- a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-object-method-attribute/options.json +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-object-method-attribute/options.json @@ -1,4 +1,11 @@ { - "plugins": ["moduleAttributes"], + "plugins": [ + [ + "moduleAttributes", + { + "version": "feb-2020" + } + ] + ], "sourceType": "module" } diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-repeated-type/options.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-repeated-type/options.json index 9399e0bedd3f..442f499887f3 100644 --- a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-repeated-type/options.json +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-repeated-type/options.json @@ -1,4 +1,11 @@ { - "plugins": ["moduleAttributes"], + "plugins": [ + [ + "moduleAttributes", + { + "version": "feb-2020" + } + ] + ], "sourceType": "module" } diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-without-attributes/options.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-without-attributes/options.json index 9399e0bedd3f..442f499887f3 100644 --- a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-without-attributes/options.json +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-without-attributes/options.json @@ -1,4 +1,11 @@ { - "plugins": ["moduleAttributes"], + "plugins": [ + [ + "moduleAttributes", + { + "version": "feb-2020" + } + ] + ], "sourceType": "module" } From 4f16179c0239d2cdf65db2a6d387880adf68a866 Mon Sep 17 00:00:00 2001 From: Vivek Nayyar Date: Fri, 13 Mar 2020 02:27:01 +0100 Subject: [PATCH 10/63] improvement: replaced object create with set and renamed test folder to module-attributes --- packages/babel-parser/src/parser/location.js | 6 +++--- packages/babel-parser/src/parser/statement.js | 12 ++++++------ .../input.js | 0 .../options.json | 0 .../input.js | 0 .../options.json | 0 .../valid-syntax-with-attributes-and-value/input.js | 0 .../options.json | 0 .../output.json | 0 .../input.js | 0 .../options.json | 0 .../output.json | 0 .../valid-syntax-with-attributes/input.js | 0 .../valid-syntax-with-attributes/options.json | 0 .../valid-syntax-with-attributes/output.json | 0 .../valid-syntax-with-invalid-plugin-option/input.js | 0 .../options.json | 0 .../valid-syntax-with-invalid-value/input.js | 0 .../valid-syntax-with-invalid-value/options.json | 0 .../valid-syntax-with-no-type-attribute/input.js | 0 .../valid-syntax-with-no-type-attribute/options.json | 4 ++-- .../input.js | 0 .../options.json | 0 .../output.json | 0 .../valid-syntax-with-repeated-type/input.js | 0 .../valid-syntax-with-repeated-type/options.json | 0 .../valid-syntax-with-repeated-type/output.json | 2 +- .../valid-syntax-without-attributes/input.js | 0 .../valid-syntax-without-attributes/options.json | 0 .../valid-syntax-without-attributes/output.json | 0 .../without-plugin/input.js | 0 .../without-plugin/options.json | 0 32 files changed, 12 insertions(+), 12 deletions(-) rename packages/babel-parser/test/fixtures/experimental/_no-plugin/{import-module-attributes => module-attributes}/input.js (100%) rename packages/babel-parser/test/fixtures/experimental/_no-plugin/{import-module-attributes => module-attributes}/options.json (100%) rename packages/babel-parser/test/fixtures/experimental/{import-module-attributes => module-attributes}/invalid-syntax-with-no-attributes-identifier/input.js (100%) rename packages/babel-parser/test/fixtures/experimental/{import-module-attributes => module-attributes}/invalid-syntax-with-no-attributes-identifier/options.json (100%) rename packages/babel-parser/test/fixtures/experimental/{import-module-attributes => module-attributes}/valid-syntax-with-attributes-and-value/input.js (100%) rename packages/babel-parser/test/fixtures/experimental/{import-module-attributes => module-attributes}/valid-syntax-with-attributes-and-value/options.json (100%) rename packages/babel-parser/test/fixtures/experimental/{import-module-attributes => module-attributes}/valid-syntax-with-attributes-and-value/output.json (100%) rename packages/babel-parser/test/fixtures/experimental/{import-module-attributes => module-attributes}/valid-syntax-with-attributes-multiple-lines/input.js (100%) rename packages/babel-parser/test/fixtures/experimental/{import-module-attributes => module-attributes}/valid-syntax-with-attributes-multiple-lines/options.json (100%) rename packages/babel-parser/test/fixtures/experimental/{import-module-attributes => module-attributes}/valid-syntax-with-attributes-multiple-lines/output.json (100%) rename packages/babel-parser/test/fixtures/experimental/{import-module-attributes => module-attributes}/valid-syntax-with-attributes/input.js (100%) rename packages/babel-parser/test/fixtures/experimental/{import-module-attributes => module-attributes}/valid-syntax-with-attributes/options.json (100%) rename packages/babel-parser/test/fixtures/experimental/{import-module-attributes => module-attributes}/valid-syntax-with-attributes/output.json (100%) rename packages/babel-parser/test/fixtures/experimental/{import-module-attributes => module-attributes}/valid-syntax-with-invalid-plugin-option/input.js (100%) rename packages/babel-parser/test/fixtures/experimental/{import-module-attributes => module-attributes}/valid-syntax-with-invalid-plugin-option/options.json (100%) rename packages/babel-parser/test/fixtures/experimental/{import-module-attributes => module-attributes}/valid-syntax-with-invalid-value/input.js (100%) rename packages/babel-parser/test/fixtures/experimental/{import-module-attributes => module-attributes}/valid-syntax-with-invalid-value/options.json (100%) rename packages/babel-parser/test/fixtures/experimental/{import-module-attributes => module-attributes}/valid-syntax-with-no-type-attribute/input.js (100%) rename packages/babel-parser/test/fixtures/experimental/{import-module-attributes => module-attributes}/valid-syntax-with-no-type-attribute/options.json (65%) rename packages/babel-parser/test/fixtures/experimental/{import-module-attributes => module-attributes}/valid-syntax-with-object-method-attribute/input.js (100%) rename packages/babel-parser/test/fixtures/experimental/{import-module-attributes => module-attributes}/valid-syntax-with-object-method-attribute/options.json (100%) rename packages/babel-parser/test/fixtures/experimental/{import-module-attributes => module-attributes}/valid-syntax-with-object-method-attribute/output.json (100%) rename packages/babel-parser/test/fixtures/experimental/{import-module-attributes => module-attributes}/valid-syntax-with-repeated-type/input.js (100%) rename packages/babel-parser/test/fixtures/experimental/{import-module-attributes => module-attributes}/valid-syntax-with-repeated-type/options.json (100%) rename packages/babel-parser/test/fixtures/experimental/{import-module-attributes => module-attributes}/valid-syntax-with-repeated-type/output.json (98%) rename packages/babel-parser/test/fixtures/experimental/{import-module-attributes => module-attributes}/valid-syntax-without-attributes/input.js (100%) rename packages/babel-parser/test/fixtures/experimental/{import-module-attributes => module-attributes}/valid-syntax-without-attributes/options.json (100%) rename packages/babel-parser/test/fixtures/experimental/{import-module-attributes => module-attributes}/valid-syntax-without-attributes/output.json (100%) rename packages/babel-parser/test/fixtures/experimental/{import-module-attributes => module-attributes}/without-plugin/input.js (100%) rename packages/babel-parser/test/fixtures/experimental/{import-module-attributes => module-attributes}/without-plugin/options.json (100%) diff --git a/packages/babel-parser/src/parser/location.js b/packages/babel-parser/src/parser/location.js index c5a18ff33504..8f4e250af32f 100644 --- a/packages/babel-parser/src/parser/location.js +++ b/packages/babel-parser/src/parser/location.js @@ -98,11 +98,11 @@ export const Errors = Object.freeze({ MissingUnicodeEscape: "Expecting Unicode escape sequence \\uXXXX", MixingCoalesceWithLogical: "Nullish coalescing operator(??) requires parens when mixing with logical operators", - ModuleAttributesIncorrectValue: + ModuleAttributeInvalidValue: "Only string literals are allowed as module attributes values", ModuleAttributesWithDuplicateKeys: - "Module attributes should not include duplicate keys (%0)", - ModuleAttributesWithoutType: "Module attributes should include a type", + 'Duplicate key "%0" is not allowed in module attributes', + ModuleAttributesWithoutType: 'Module attributes should include a "type" key', ModuleExportUndefined: "Export '%0' is not defined", MultipleDefaultsInSwitch: "Multiple default clauses", NewlineAfterThrow: "Illegal newline after throw", diff --git a/packages/babel-parser/src/parser/statement.js b/packages/babel-parser/src/parser/statement.js index 16c19c4f955c..840145ab2d81 100644 --- a/packages/babel-parser/src/parser/statement.js +++ b/packages/babel-parser/src/parser/statement.js @@ -2070,7 +2070,7 @@ export default class StatementParser extends ExpressionParser { return null; } const attrs = []; - const attributeMap = Object.create(null); + const attributes = new Set(); do { // we are trying to parse a node which has the following syntax // with type: "json" @@ -2080,22 +2080,22 @@ export default class StatementParser extends ExpressionParser { // check if we already have an entry for an attribute // if a duplicate entry found, throw an error - if (attributeMap[node.key.name]) { + if (attributes.has(node.key.name)) { this.raise( this.state.start, Errors.ModuleAttributesWithDuplicateKeys, node.key.name, ); } - // set the attribute name entry in the attributeMap to true - attributeMap[node.key.name] = true; + // set the attribute name entry in the attributeMap + attributes.add(node.key.name); // check for colon this.expect(tt.colon); // check if the value set to the module attribute is a string as we only allow string literals if (!this.match(tt.string)) { throw this.unexpected( this.state.start, - Errors.ModuleAttributesIncorrectValue, + Errors.ModuleAttributeInvalidValue, ); } node.value = this.parseLiteral(this.state.value, "StringLiteral"); @@ -2104,7 +2104,7 @@ export default class StatementParser extends ExpressionParser { } while (this.eat(tt.comma)); // check if module attributes do not contain any type key and throw an error - if (!attributeMap["type"]) { + if (!attributes.has("type")) { throw this.raise(this.state.start, Errors.ModuleAttributesWithoutType); } return attrs; diff --git a/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-module-attributes/input.js b/packages/babel-parser/test/fixtures/experimental/_no-plugin/module-attributes/input.js similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/_no-plugin/import-module-attributes/input.js rename to packages/babel-parser/test/fixtures/experimental/_no-plugin/module-attributes/input.js diff --git a/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-module-attributes/options.json b/packages/babel-parser/test/fixtures/experimental/_no-plugin/module-attributes/options.json similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/_no-plugin/import-module-attributes/options.json rename to packages/babel-parser/test/fixtures/experimental/_no-plugin/module-attributes/options.json diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/invalid-syntax-with-no-attributes-identifier/input.js b/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-no-attributes-identifier/input.js similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/import-module-attributes/invalid-syntax-with-no-attributes-identifier/input.js rename to packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-no-attributes-identifier/input.js diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/invalid-syntax-with-no-attributes-identifier/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-no-attributes-identifier/options.json similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/import-module-attributes/invalid-syntax-with-no-attributes-identifier/options.json rename to packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-no-attributes-identifier/options.json diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-and-value/input.js b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-and-value/input.js similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-and-value/input.js rename to packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-and-value/input.js diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-and-value/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-and-value/options.json similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-and-value/options.json rename to packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-and-value/options.json diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-and-value/output.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-and-value/output.json similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-and-value/output.json rename to packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-and-value/output.json diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-multiple-lines/input.js b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-multiple-lines/input.js similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-multiple-lines/input.js rename to packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-multiple-lines/input.js diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-multiple-lines/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-multiple-lines/options.json similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-multiple-lines/options.json rename to packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-multiple-lines/options.json diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-multiple-lines/output.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-multiple-lines/output.json similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-multiple-lines/output.json rename to packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-multiple-lines/output.json diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes/input.js b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes/input.js similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes/input.js rename to packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes/input.js diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes/options.json similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes/options.json rename to packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes/options.json diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes/output.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes/output.json similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes/output.json rename to packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes/output.json diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-plugin-option/input.js b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-plugin-option/input.js similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-plugin-option/input.js rename to packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-plugin-option/input.js diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-plugin-option/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-plugin-option/options.json similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-plugin-option/options.json rename to packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-plugin-option/options.json diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-value/input.js b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-value/input.js similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-value/input.js rename to packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-value/input.js diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-value/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-value/options.json similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-value/options.json rename to packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-value/options.json diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-no-type-attribute/input.js b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/input.js similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-no-type-attribute/input.js rename to packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/input.js diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-no-type-attribute/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/options.json similarity index 65% rename from packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-no-type-attribute/options.json rename to packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/options.json index d207edb90981..0b87b1133d2d 100644 --- a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-no-type-attribute/options.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/options.json @@ -8,5 +8,5 @@ ] ], "sourceType": "module", - "throws": "Module attributes should include a type (1:44)" -} + "throws": "Module attributes should include a \"type\" key (1:44)" +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-object-method-attribute/input.js b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/input.js similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-object-method-attribute/input.js rename to packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/input.js diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-object-method-attribute/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/options.json similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-object-method-attribute/options.json rename to packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/options.json diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-object-method-attribute/output.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/output.json similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-object-method-attribute/output.json rename to packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/output.json diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-repeated-type/input.js b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/input.js similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-repeated-type/input.js rename to packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/input.js diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-repeated-type/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/options.json similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-repeated-type/options.json rename to packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/options.json diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-repeated-type/output.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/output.json similarity index 98% rename from packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-repeated-type/output.json rename to packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/output.json index 0bb5aebcc7ce..7ca48ebcffec 100644 --- a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-repeated-type/output.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/output.json @@ -13,7 +13,7 @@ } }, "errors": [ - "SyntaxError: Module attributes should not include duplicate keys (type) (1:64)" + "SyntaxError: Duplicate key \"type\" is not allowed in module attributes (1:64)" ], "program": { "type": "Program", diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-without-attributes/input.js b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-without-attributes/input.js similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-without-attributes/input.js rename to packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-without-attributes/input.js diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-without-attributes/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-without-attributes/options.json similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-without-attributes/options.json rename to packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-without-attributes/options.json diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-without-attributes/output.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-without-attributes/output.json similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-without-attributes/output.json rename to packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-without-attributes/output.json diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/without-plugin/input.js b/packages/babel-parser/test/fixtures/experimental/module-attributes/without-plugin/input.js similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/import-module-attributes/without-plugin/input.js rename to packages/babel-parser/test/fixtures/experimental/module-attributes/without-plugin/input.js diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/without-plugin/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/without-plugin/options.json similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/import-module-attributes/without-plugin/options.json rename to packages/babel-parser/test/fixtures/experimental/module-attributes/without-plugin/options.json From a264662504034539cbdf4a3d93667842f6a92e43 Mon Sep 17 00:00:00 2001 From: Vivek Nayyar Date: Fri, 13 Mar 2020 11:19:19 +0100 Subject: [PATCH 11/63] Update packages/babel-parser/src/plugin-utils.js MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit improved error message on version mismatch Co-Authored-By: Nicolò Ribaudo --- packages/babel-parser/src/plugin-utils.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/babel-parser/src/plugin-utils.js b/packages/babel-parser/src/plugin-utils.js index 657d86813f71..29337da49485 100644 --- a/packages/babel-parser/src/plugin-utils.js +++ b/packages/babel-parser/src/plugin-utils.js @@ -94,7 +94,8 @@ export function validatePlugins(plugins: PluginList) { if (moduleAttributesVerionPluginOption !== "feb-2020") { throw new Error( "The 'moduleAttributes' plugin requires a 'version' option," + - " whose value must be feb-2020.", + " representing the last proposal update. Currently, the" + + " only supported value is 'feb-2020'.", ); } } From c7c2c7853e594f8baa88ea1e7e09fc7ff23497a1 Mon Sep 17 00:00:00 2001 From: Vivek Nayyar Date: Fri, 13 Mar 2020 11:19:54 +0100 Subject: [PATCH 12/63] Update packages/babel-parser/src/parser/statement.js MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit comment rectified Co-Authored-By: Nicolò Ribaudo --- packages/babel-parser/src/parser/statement.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/babel-parser/src/parser/statement.js b/packages/babel-parser/src/parser/statement.js index 840145ab2d81..c65aa6d9ef45 100644 --- a/packages/babel-parser/src/parser/statement.js +++ b/packages/babel-parser/src/parser/statement.js @@ -2079,7 +2079,7 @@ export default class StatementParser extends ExpressionParser { node.key = this.parseIdentifier(true); // check if we already have an entry for an attribute - // if a duplicate entry found, throw an error + // if a duplicate entry is found, throw an error if (attributes.has(node.key.name)) { this.raise( this.state.start, From 2b5b437b01e8ad6c4ded8a0a51f3d4634e81a0c3 Mon Sep 17 00:00:00 2001 From: Vivek Nayyar Date: Fri, 13 Mar 2020 11:25:43 +0100 Subject: [PATCH 13/63] chore: unnecesaary comment removed and tests updated --- packages/babel-parser/src/parser/statement.js | 2 -- .../valid-syntax-with-invalid-plugin-option/options.json | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/babel-parser/src/parser/statement.js b/packages/babel-parser/src/parser/statement.js index c65aa6d9ef45..3cf51740086f 100644 --- a/packages/babel-parser/src/parser/statement.js +++ b/packages/babel-parser/src/parser/statement.js @@ -2087,9 +2087,7 @@ export default class StatementParser extends ExpressionParser { node.key.name, ); } - // set the attribute name entry in the attributeMap attributes.add(node.key.name); - // check for colon this.expect(tt.colon); // check if the value set to the module attribute is a string as we only allow string literals if (!this.match(tt.string)) { diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-plugin-option/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-plugin-option/options.json index 92566a76e0f3..29907e14ccd5 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-plugin-option/options.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-plugin-option/options.json @@ -8,5 +8,5 @@ ] ], "sourceType": "module", - "throws": "The 'moduleAttributes' plugin requires a 'version' option, whose value must be feb-2020." + "throws": "The 'moduleAttributes' plugin requires a 'version' option, representing the last proposal update. Currently, the only supported value is 'feb-2020'." } \ No newline at end of file From d36d314ea6427b9ce100a16ee0799299757441e4 Mon Sep 17 00:00:00 2001 From: Vivek Nayyar Date: Fri, 13 Mar 2020 11:31:53 +0100 Subject: [PATCH 14/63] improvement: using node.key.start instead of this.node.start to point at the exact duplicate module attribute name --- packages/babel-parser/src/parser/statement.js | 2 +- .../valid-syntax-with-repeated-type/output.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/babel-parser/src/parser/statement.js b/packages/babel-parser/src/parser/statement.js index 3cf51740086f..d683b829983a 100644 --- a/packages/babel-parser/src/parser/statement.js +++ b/packages/babel-parser/src/parser/statement.js @@ -2082,7 +2082,7 @@ export default class StatementParser extends ExpressionParser { // if a duplicate entry is found, throw an error if (attributes.has(node.key.name)) { this.raise( - this.state.start, + node.key.start, Errors.ModuleAttributesWithDuplicateKeys, node.key.name, ); diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/output.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/output.json index 7ca48ebcffec..39f3a86e0788 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/output.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/output.json @@ -13,7 +13,7 @@ } }, "errors": [ - "SyntaxError: Duplicate key \"type\" is not allowed in module attributes (1:64)" + "SyntaxError: Duplicate key \"type\" is not allowed in module attributes (1:60)" ], "program": { "type": "Program", From 0a0958d2e6e01fe54e3dfdc1e52ed8b681d9be55 Mon Sep 17 00:00:00 2001 From: Vivek Nayyar Date: Sat, 14 Mar 2020 22:41:14 +0100 Subject: [PATCH 15/63] improvement: added check for line terminator --- packages/babel-parser/src/parser/statement.js | 2 +- .../options.json | 3 +- .../output.json | 117 ++++++++++++++++ .../options.json | 5 +- .../output.json | 125 ------------------ 5 files changed, 122 insertions(+), 130 deletions(-) create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-no-attributes-identifier/output.json delete mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-multiple-lines/output.json diff --git a/packages/babel-parser/src/parser/statement.js b/packages/babel-parser/src/parser/statement.js index d683b829983a..6ad7274b128a 100644 --- a/packages/babel-parser/src/parser/statement.js +++ b/packages/babel-parser/src/parser/statement.js @@ -2062,7 +2062,7 @@ export default class StatementParser extends ExpressionParser { } maybeParseModuleAttributes() { - if (this.match(tt._with)) { + if (this.match(tt._with) && !this.isLineTerminator()) { this.expectPlugin("moduleAttributes"); this.next(); } else { diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-no-attributes-identifier/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-no-attributes-identifier/options.json index cbb08d0c69a4..d8393072fd10 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-no-attributes-identifier/options.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-no-attributes-identifier/options.json @@ -8,6 +8,5 @@ ], "estree" ], - "sourceType": "module", - "throws": "Unexpected token (2:5)" + "sourceType": "module" } diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-no-attributes-identifier/output.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-no-attributes-identifier/output.json new file mode 100644 index 000000000000..44515fdf2d4a --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-no-attributes-identifier/output.json @@ -0,0 +1,117 @@ +{ + "type": "File", + "start": 0, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 10 + } + }, + "errors": [ + "SyntaxError: 'with' in strict mode (2:0)" + ], + "program": { + "type": "Program", + "start": 0, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 10 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start": 0, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "specifiers": [], + "source": { + "type": "Literal", + "start": 7, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "value": "x", + "raw": "\"x\"" + }, + "attributes": [] + }, + { + "type": "WithStatement", + "start": 11, + "end": 21, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 10 + } + }, + "object": { + "type": "ObjectExpression", + "start": 17, + "end": 19, + "loc": { + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 8 + } + }, + "properties": [] + }, + "body": { + "type": "EmptyStatement", + "start": 20, + "end": 21, + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 10 + } + } + } + } + ] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-multiple-lines/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-multiple-lines/options.json index 442f499887f3..1314343331ed 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-multiple-lines/options.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-multiple-lines/options.json @@ -7,5 +7,6 @@ } ] ], - "sourceType": "module" -} + "sourceType": "module", + "throws": "Unexpected token, expected \"(\" (3:0)" +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-multiple-lines/output.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-multiple-lines/output.json deleted file mode 100644 index 7ab9b34a1162..000000000000 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-multiple-lines/output.json +++ /dev/null @@ -1,125 +0,0 @@ -{ - "type": "File", - "start": 0, - "end": 28, - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 3, - "column": 12 - } - }, - "program": { - "type": "Program", - "start": 0, - "end": 28, - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 3, - "column": 12 - } - }, - "sourceType": "module", - "interpreter": null, - "body": [ - { - "type": "ImportDeclaration", - "start": 0, - "end": 28, - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 3, - "column": 12 - } - }, - "specifiers": [], - "source": { - "type": "StringLiteral", - "start": 7, - "end": 10, - "loc": { - "start": { - "line": 1, - "column": 7 - }, - "end": { - "line": 1, - "column": 10 - } - }, - "extra": { - "rawValue": "x", - "raw": "\"x\"" - }, - "value": "x" - }, - "attributes": [ - { - "type": "ImportAttribute", - "start": 16, - "end": 28, - "loc": { - "start": { - "line": 3, - "column": 0 - }, - "end": { - "line": 3, - "column": 12 - } - }, - "key": { - "type": "Identifier", - "start": 16, - "end": 20, - "loc": { - "start": { - "line": 3, - "column": 0 - }, - "end": { - "line": 3, - "column": 4 - }, - "identifierName": "type" - }, - "name": "type" - }, - "value": { - "type": "StringLiteral", - "start": 22, - "end": 28, - "loc": { - "start": { - "line": 3, - "column": 6 - }, - "end": { - "line": 3, - "column": 12 - } - }, - "extra": { - "rawValue": "json", - "raw": "\"json\"" - }, - "value": "json" - } - } - ] - } - ], - "directives": [] - } -} \ No newline at end of file From f28f6ad95f52baa9460584cd9e408a47a7347279 Mon Sep 17 00:00:00 2001 From: Vivek Nayyar Date: Sat, 14 Mar 2020 22:46:48 +0100 Subject: [PATCH 16/63] chore: renamed valid-syntax-with-attributes-multiple-lines to invalid-syntax-with-attributes-multiple-lines --- .../input.js | 0 .../options.json | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename packages/babel-parser/test/fixtures/experimental/module-attributes/{valid-syntax-with-attributes-multiple-lines => invalid-syntax-with-attributes-multiple-lines}/input.js (100%) rename packages/babel-parser/test/fixtures/experimental/module-attributes/{valid-syntax-with-attributes-multiple-lines => invalid-syntax-with-attributes-multiple-lines}/options.json (100%) diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-multiple-lines/input.js b/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-attributes-multiple-lines/input.js similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-multiple-lines/input.js rename to packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-attributes-multiple-lines/input.js diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-multiple-lines/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-attributes-multiple-lines/options.json similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-multiple-lines/options.json rename to packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-attributes-multiple-lines/options.json From 78068099d12638f962033ca09e94929681510ab9 Mon Sep 17 00:00:00 2001 From: Vivek Nayyar Date: Sun, 15 Mar 2020 23:23:26 +0100 Subject: [PATCH 17/63] improvement: changed the logic and tests to only support type as the allowed module attribute --- packages/babel-parser/src/parser/location.js | 4 +- packages/babel-parser/src/parser/statement.js | 10 + .../options.json | 4 +- .../options.json | 2 +- .../options.json | 5 +- .../output.json | 210 ------------------ .../valid-syntax-with-repeated-type/input.js | 2 +- .../output.json | 70 +----- 8 files changed, 29 insertions(+), 278 deletions(-) delete mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/output.json diff --git a/packages/babel-parser/src/parser/location.js b/packages/babel-parser/src/parser/location.js index 8f4e250af32f..32df01d54a3b 100644 --- a/packages/babel-parser/src/parser/location.js +++ b/packages/babel-parser/src/parser/location.js @@ -98,8 +98,10 @@ export const Errors = Object.freeze({ MissingUnicodeEscape: "Expecting Unicode escape sequence \\uXXXX", MixingCoalesceWithLogical: "Nullish coalescing operator(??) requires parens when mixing with logical operators", + ModuleAttributeDifferentFromType: + "The only accepted module attribute is `type`", ModuleAttributeInvalidValue: - "Only string literals are allowed as module attributes values", + "Only string literals are allowed as module attribute values", ModuleAttributesWithDuplicateKeys: 'Duplicate key "%0" is not allowed in module attributes', ModuleAttributesWithoutType: 'Module attributes should include a "type" key', diff --git a/packages/babel-parser/src/parser/statement.js b/packages/babel-parser/src/parser/statement.js index 6ad7274b128a..a721b728697d 100644 --- a/packages/babel-parser/src/parser/statement.js +++ b/packages/babel-parser/src/parser/statement.js @@ -2078,8 +2078,18 @@ export default class StatementParser extends ExpressionParser { const node = this.startNode(); node.key = this.parseIdentifier(true); + // for now we are only allowing `type` as the only allowed module attribute + if (node.key.name !== "type") { + throw this.raise( + node.key.start, + Errors.ModuleAttributeDifferentFromType, + node.key.name, + ); + } + // check if we already have an entry for an attribute // if a duplicate entry is found, throw an error + // for now this logic will come into play only when someone declares `type` twice if (attributes.has(node.key.name)) { this.raise( node.key.start, diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-value/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-value/options.json index b28c434f1369..37f2ffe2a74f 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-value/options.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-value/options.json @@ -8,5 +8,5 @@ ] ], "sourceType": "module", - "throws": "Only string literals are allowed as module attributes values (1:52)" -} + "throws": "The only accepted module attribute is `type` (1:46)" +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/options.json index 0b87b1133d2d..989cd557d8c4 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/options.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/options.json @@ -8,5 +8,5 @@ ] ], "sourceType": "module", - "throws": "Module attributes should include a \"type\" key (1:44)" + "throws": "The only accepted module attribute is `type` (1:32)" } \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/options.json index 442f499887f3..37f2ffe2a74f 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/options.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/options.json @@ -7,5 +7,6 @@ } ] ], - "sourceType": "module" -} + "sourceType": "module", + "throws": "The only accepted module attribute is `type` (1:46)" +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/output.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/output.json deleted file mode 100644 index dd61984b92cf..000000000000 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/output.json +++ /dev/null @@ -1,210 +0,0 @@ -{ - "type": "File", - "start": 0, - "end": 69, - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 1, - "column": 69 - } - }, - "program": { - "type": "Program", - "start": 0, - "end": 69, - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 1, - "column": 69 - } - }, - "sourceType": "module", - "interpreter": null, - "body": [ - { - "type": "ImportDeclaration", - "start": 0, - "end": 69, - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 1, - "column": 69 - } - }, - "specifiers": [ - { - "type": "ImportDefaultSpecifier", - "start": 7, - "end": 10, - "loc": { - "start": { - "line": 1, - "column": 7 - }, - "end": { - "line": 1, - "column": 10 - } - }, - "local": { - "type": "Identifier", - "start": 7, - "end": 10, - "loc": { - "start": { - "line": 1, - "column": 7 - }, - "end": { - "line": 1, - "column": 10 - }, - "identifierName": "foo" - }, - "name": "foo" - } - } - ], - "source": { - "type": "StringLiteral", - "start": 16, - "end": 26, - "loc": { - "start": { - "line": 1, - "column": 16 - }, - "end": { - "line": 1, - "column": 26 - } - }, - "extra": { - "rawValue": "foo.json", - "raw": "\"foo.json\"" - }, - "value": "foo.json" - }, - "attributes": [ - { - "type": "ImportAttribute", - "start": 32, - "end": 44, - "loc": { - "start": { - "line": 1, - "column": 32 - }, - "end": { - "line": 1, - "column": 44 - } - }, - "key": { - "type": "Identifier", - "start": 32, - "end": 36, - "loc": { - "start": { - "line": 1, - "column": 32 - }, - "end": { - "line": 1, - "column": 36 - }, - "identifierName": "type" - }, - "name": "type" - }, - "value": { - "type": "StringLiteral", - "start": 38, - "end": 44, - "loc": { - "start": { - "line": 1, - "column": 38 - }, - "end": { - "line": 1, - "column": 44 - } - }, - "extra": { - "rawValue": "json", - "raw": "\"json\"" - }, - "value": "json" - } - }, - { - "type": "ImportAttribute", - "start": 46, - "end": 68, - "loc": { - "start": { - "line": 1, - "column": 46 - }, - "end": { - "line": 1, - "column": 68 - } - }, - "key": { - "type": "Identifier", - "start": 46, - "end": 60, - "loc": { - "start": { - "line": 1, - "column": 46 - }, - "end": { - "line": 1, - "column": 60 - }, - "identifierName": "hasOwnProperty" - }, - "name": "hasOwnProperty" - }, - "value": { - "type": "StringLiteral", - "start": 62, - "end": 68, - "loc": { - "start": { - "line": 1, - "column": 62 - }, - "end": { - "line": 1, - "column": 68 - } - }, - "extra": { - "rawValue": "true", - "raw": "\"true\"" - }, - "value": "true" - } - } - ] - } - ], - "directives": [] - } -} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/input.js b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/input.js index f742d7cc15d1..856373edcf34 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/input.js +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/input.js @@ -1 +1 @@ -import foo from "foo.json" with type: "json", lazy: "true", type: "html"; +import foo from "foo.json" with type: "json", type: "html"; diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/output.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/output.json index 39f3a86e0788..cd32d74344f6 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/output.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/output.json @@ -1,7 +1,7 @@ { "type": "File", "start": 0, - "end": 73, + "end": 59, "loc": { "start": { "line": 1, @@ -9,16 +9,16 @@ }, "end": { "line": 1, - "column": 73 + "column": 59 } }, "errors": [ - "SyntaxError: Duplicate key \"type\" is not allowed in module attributes (1:60)" + "SyntaxError: Duplicate key \"type\" is not allowed in module attributes (1:46)" ], "program": { "type": "Program", "start": 0, - "end": 73, + "end": 59, "loc": { "start": { "line": 1, @@ -26,7 +26,7 @@ }, "end": { "line": 1, - "column": 73 + "column": 59 } }, "sourceType": "module", @@ -35,7 +35,7 @@ { "type": "ImportDeclaration", "start": 0, - "end": 73, + "end": 59, "loc": { "start": { "line": 1, @@ -43,7 +43,7 @@ }, "end": { "line": 1, - "column": 73 + "column": 59 } }, "specifiers": [ @@ -180,9 +180,9 @@ "line": 1, "column": 50 }, - "identifierName": "lazy" + "identifierName": "type" }, - "name": "lazy" + "name": "type" }, "value": { "type": "StringLiteral", @@ -198,58 +198,6 @@ "column": 58 } }, - "extra": { - "rawValue": "true", - "raw": "\"true\"" - }, - "value": "true" - } - }, - { - "type": "ImportAttribute", - "start": 60, - "end": 72, - "loc": { - "start": { - "line": 1, - "column": 60 - }, - "end": { - "line": 1, - "column": 72 - } - }, - "key": { - "type": "Identifier", - "start": 60, - "end": 64, - "loc": { - "start": { - "line": 1, - "column": 60 - }, - "end": { - "line": 1, - "column": 64 - }, - "identifierName": "type" - }, - "name": "type" - }, - "value": { - "type": "StringLiteral", - "start": 66, - "end": 72, - "loc": { - "start": { - "line": 1, - "column": 66 - }, - "end": { - "line": 1, - "column": 72 - } - }, "extra": { "rawValue": "html", "raw": "\"html\"" From a3c6a55c8d2d0ce2d2636c401da8e26ea663ec85 Mon Sep 17 00:00:00 2001 From: Vivek Nayyar Date: Mon, 16 Mar 2020 00:18:45 +0100 Subject: [PATCH 18/63] improvement: don't throw an error when attribute name is not type, just raise it and continue parsing --- packages/babel-parser/src/parser/statement.js | 3 +- .../options.json | 4 +- .../options.json | 2 +- .../options.json | 3 +- .../output.json | 213 ++++++++++++++++++ 5 files changed, 219 insertions(+), 6 deletions(-) create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/output.json diff --git a/packages/babel-parser/src/parser/statement.js b/packages/babel-parser/src/parser/statement.js index a721b728697d..a936b139a611 100644 --- a/packages/babel-parser/src/parser/statement.js +++ b/packages/babel-parser/src/parser/statement.js @@ -2080,7 +2080,7 @@ export default class StatementParser extends ExpressionParser { // for now we are only allowing `type` as the only allowed module attribute if (node.key.name !== "type") { - throw this.raise( + this.raise( node.key.start, Errors.ModuleAttributeDifferentFromType, node.key.name, @@ -2115,6 +2115,7 @@ export default class StatementParser extends ExpressionParser { if (!attributes.has("type")) { throw this.raise(this.state.start, Errors.ModuleAttributesWithoutType); } + return attrs; } diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-value/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-value/options.json index 37f2ffe2a74f..03ae01b6b106 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-value/options.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-value/options.json @@ -8,5 +8,5 @@ ] ], "sourceType": "module", - "throws": "The only accepted module attribute is `type` (1:46)" -} \ No newline at end of file + "throws": "Only string literals are allowed as module attribute values (1:52)" +} diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/options.json index 989cd557d8c4..0b87b1133d2d 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/options.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/options.json @@ -8,5 +8,5 @@ ] ], "sourceType": "module", - "throws": "The only accepted module attribute is `type` (1:32)" + "throws": "Module attributes should include a \"type\" key (1:44)" } \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/options.json index 37f2ffe2a74f..319126fb3325 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/options.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/options.json @@ -7,6 +7,5 @@ } ] ], - "sourceType": "module", - "throws": "The only accepted module attribute is `type` (1:46)" + "sourceType": "module" } \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/output.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/output.json new file mode 100644 index 000000000000..87230474782c --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/output.json @@ -0,0 +1,213 @@ +{ + "type": "File", + "start": 0, + "end": 69, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 69 + } + }, + "errors": [ + "SyntaxError: The only accepted module attribute is `type` (1:46)" + ], + "program": { + "type": "Program", + "start": 0, + "end": 69, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 69 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start": 0, + "end": 69, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 69 + } + }, + "specifiers": [ + { + "type": "ImportDefaultSpecifier", + "start": 7, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "local": { + "type": "Identifier", + "start": 7, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 10 + }, + "identifierName": "foo" + }, + "name": "foo" + } + } + ], + "source": { + "type": "StringLiteral", + "start": 16, + "end": 26, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 26 + } + }, + "extra": { + "rawValue": "foo.json", + "raw": "\"foo.json\"" + }, + "value": "foo.json" + }, + "attributes": [ + { + "type": "ImportAttribute", + "start": 32, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 32 + }, + "end": { + "line": 1, + "column": 44 + } + }, + "key": { + "type": "Identifier", + "start": 32, + "end": 36, + "loc": { + "start": { + "line": 1, + "column": 32 + }, + "end": { + "line": 1, + "column": 36 + }, + "identifierName": "type" + }, + "name": "type" + }, + "value": { + "type": "StringLiteral", + "start": 38, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 38 + }, + "end": { + "line": 1, + "column": 44 + } + }, + "extra": { + "rawValue": "json", + "raw": "\"json\"" + }, + "value": "json" + } + }, + { + "type": "ImportAttribute", + "start": 46, + "end": 68, + "loc": { + "start": { + "line": 1, + "column": 46 + }, + "end": { + "line": 1, + "column": 68 + } + }, + "key": { + "type": "Identifier", + "start": 46, + "end": 60, + "loc": { + "start": { + "line": 1, + "column": 46 + }, + "end": { + "line": 1, + "column": 60 + }, + "identifierName": "hasOwnProperty" + }, + "name": "hasOwnProperty" + }, + "value": { + "type": "StringLiteral", + "start": 62, + "end": 68, + "loc": { + "start": { + "line": 1, + "column": 62 + }, + "end": { + "line": 1, + "column": 68 + } + }, + "extra": { + "rawValue": "true", + "raw": "\"true\"" + }, + "value": "true" + } + } + ] + } + ], + "directives": [] + } +} \ No newline at end of file From 65f9f5c16600bdfab665892bfae5bf86f3c2fac1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Mon, 16 Mar 2020 00:29:49 +0100 Subject: [PATCH 19/63] Revert output.js --- .../dotall-regex/with-unicode-property-escape/output.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/babel-plugin-transform-dotall-regex/test/fixtures/dotall-regex/with-unicode-property-escape/output.js b/packages/babel-plugin-transform-dotall-regex/test/fixtures/dotall-regex/with-unicode-property-escape/output.js index c00aa15a9c1f..a8ddf757d926 100644 --- a/packages/babel-plugin-transform-dotall-regex/test/fixtures/dotall-regex/with-unicode-property-escape/output.js +++ b/packages/babel-plugin-transform-dotall-regex/test/fixtures/dotall-regex/with-unicode-property-escape/output.js @@ -1,2 +1,2 @@ -var a = /[\u3400-\u4DBF\u4E00-\u9FFC\uFA0E\uFA0F\uFA11\uFA13\uFA14\uFA1F\uFA21\uFA23\uFA24\uFA27-\uFA29\u{20000}-\u{2A6DD}\u{2A700}-\u{2B734}\u{2B740}-\u{2B81D}\u{2B820}-\u{2CEA1}\u{2CEB0}-\u{2EBE0}\u{30000}-\u{3134A}][\0-\t\x0B\f\x0E-\u2027\u202A-\u{10FFFF}]/u; -var b = /[\u3400-\u4DBF\u4E00-\u9FFC\uFA0E\uFA0F\uFA11\uFA13\uFA14\uFA1F\uFA21\uFA23\uFA24\uFA27-\uFA29\u{20000}-\u{2A6DD}\u{2A700}-\u{2B734}\u{2B740}-\u{2B81D}\u{2B820}-\u{2CEA1}\u{2CEB0}-\u{2EBE0}\u{30000}-\u{3134A}][\0-\u{10FFFF}]/u; +var a = /[\u3400-\u4DB5\u4E00-\u9FEF\uFA0E\uFA0F\uFA11\uFA13\uFA14\uFA1F\uFA21\uFA23\uFA24\uFA27-\uFA29\u{20000}-\u{2A6D6}\u{2A700}-\u{2B734}\u{2B740}-\u{2B81D}\u{2B820}-\u{2CEA1}\u{2CEB0}-\u{2EBE0}][\0-\t\x0B\f\x0E-\u2027\u202A-\u{10FFFF}]/u; +var b = /[\u3400-\u4DB5\u4E00-\u9FEF\uFA0E\uFA0F\uFA11\uFA13\uFA14\uFA1F\uFA21\uFA23\uFA24\uFA27-\uFA29\u{20000}-\u{2A6D6}\u{2A700}-\u{2B734}\u{2B740}-\u{2B81D}\u{2B820}-\u{2CEA1}\u{2CEB0}-\u{2EBE0}][\0-\u{10FFFF}]/u; From 1dacc99011200a29a8b9e4d9b532fbf19983bce8 Mon Sep 17 00:00:00 2001 From: Vivek Nayyar Date: Mon, 16 Mar 2020 00:42:18 +0100 Subject: [PATCH 20/63] improvement: removed the check for presence of tyep attribute as an earlier check will already catch this --- packages/babel-parser/src/parser/statement.js | 5 - .../options.json | 3 +- .../output.json | 161 ++++++++++++++++++ 3 files changed, 162 insertions(+), 7 deletions(-) create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/output.json diff --git a/packages/babel-parser/src/parser/statement.js b/packages/babel-parser/src/parser/statement.js index a936b139a611..5810ac1d9a49 100644 --- a/packages/babel-parser/src/parser/statement.js +++ b/packages/babel-parser/src/parser/statement.js @@ -2111,11 +2111,6 @@ export default class StatementParser extends ExpressionParser { attrs.push(node); } while (this.eat(tt.comma)); - // check if module attributes do not contain any type key and throw an error - if (!attributes.has("type")) { - throw this.raise(this.state.start, Errors.ModuleAttributesWithoutType); - } - return attrs; } diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/options.json index 0b87b1133d2d..319126fb3325 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/options.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/options.json @@ -7,6 +7,5 @@ } ] ], - "sourceType": "module", - "throws": "Module attributes should include a \"type\" key (1:44)" + "sourceType": "module" } \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/output.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/output.json new file mode 100644 index 000000000000..9b41293d33a0 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/output.json @@ -0,0 +1,161 @@ +{ + "type": "File", + "start": 0, + "end": 45, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 45 + } + }, + "errors": [ + "SyntaxError: The only accepted module attribute is `type` (1:32)" + ], + "program": { + "type": "Program", + "start": 0, + "end": 45, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 45 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start": 0, + "end": 45, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 45 + } + }, + "specifiers": [ + { + "type": "ImportDefaultSpecifier", + "start": 7, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "local": { + "type": "Identifier", + "start": 7, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 10 + }, + "identifierName": "foo" + }, + "name": "foo" + } + } + ], + "source": { + "type": "StringLiteral", + "start": 16, + "end": 26, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 26 + } + }, + "extra": { + "rawValue": "foo.json", + "raw": "\"foo.json\"" + }, + "value": "foo.json" + }, + "attributes": [ + { + "type": "ImportAttribute", + "start": 32, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 32 + }, + "end": { + "line": 1, + "column": 44 + } + }, + "key": { + "type": "Identifier", + "start": 32, + "end": 36, + "loc": { + "start": { + "line": 1, + "column": 32 + }, + "end": { + "line": 1, + "column": 36 + }, + "identifierName": "lazy" + }, + "name": "lazy" + }, + "value": { + "type": "StringLiteral", + "start": 38, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 38 + }, + "end": { + "line": 1, + "column": 44 + } + }, + "extra": { + "rawValue": "true", + "raw": "\"true\"" + }, + "value": "true" + } + } + ] + } + ], + "directives": [] + } +} \ No newline at end of file From 40ddfabf383736e4f52a3f3ae4da298ab5d25711 Mon Sep 17 00:00:00 2001 From: Vivek Nayyar Date: Mon, 16 Mar 2020 01:11:18 +0100 Subject: [PATCH 21/63] feat: added syntax-import-module-attributes --- .../.npmignore | 3 +++ .../README.md | 19 +++++++++++++++ .../package.json | 23 +++++++++++++++++++ .../src/index.js | 21 +++++++++++++++++ 4 files changed, 66 insertions(+) create mode 100644 packages/babel-plugin-syntax-import-module-attributes/.npmignore create mode 100644 packages/babel-plugin-syntax-import-module-attributes/README.md create mode 100644 packages/babel-plugin-syntax-import-module-attributes/package.json create mode 100644 packages/babel-plugin-syntax-import-module-attributes/src/index.js diff --git a/packages/babel-plugin-syntax-import-module-attributes/.npmignore b/packages/babel-plugin-syntax-import-module-attributes/.npmignore new file mode 100644 index 000000000000..2b1fceba679b --- /dev/null +++ b/packages/babel-plugin-syntax-import-module-attributes/.npmignore @@ -0,0 +1,3 @@ +*.log +src +test diff --git a/packages/babel-plugin-syntax-import-module-attributes/README.md b/packages/babel-plugin-syntax-import-module-attributes/README.md new file mode 100644 index 000000000000..4a3dadb0cf96 --- /dev/null +++ b/packages/babel-plugin-syntax-import-module-attributes/README.md @@ -0,0 +1,19 @@ +# @babel/plugin-syntax-import-module-attributes + +> Allow parsing of the pipeline operator + +See our website [@babel/plugin-syntax-import-module-attributes](https://babeljs.io/docs/en/next/babel-plugin-syntax-import-module-attributes.html) for more information. + +## Install + +Using npm: + +```sh +npm install --save-dev @babel/plugin-syntax-import-module-attributes +``` + +or using yarn: + +```sh +yarn add @babel/plugin-syntax-import-module-attributes --dev +``` diff --git a/packages/babel-plugin-syntax-import-module-attributes/package.json b/packages/babel-plugin-syntax-import-module-attributes/package.json new file mode 100644 index 000000000000..8cf463f52e21 --- /dev/null +++ b/packages/babel-plugin-syntax-import-module-attributes/package.json @@ -0,0 +1,23 @@ +{ + "name": "@babel/plugin-syntax-import-module-attributes", + "version": "7.8.3", + "description": "Allow parsing of the module attributes in the import statement", + "repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-syntax-import-module-attributes", + "license": "MIT", + "publishConfig": { + "access": "public" + }, + "main": "lib/index.js", + "keywords": [ + "babel-plugin" + ], + "dependencies": { + "@babel/helper-plugin-utils": "^7.8.3" + }, + "peerDependencies": { + "@babel/core": "^7.0.0-0" + }, + "devDependencies": { + "@babel/core": "^7.8.3" + } +} diff --git a/packages/babel-plugin-syntax-import-module-attributes/src/index.js b/packages/babel-plugin-syntax-import-module-attributes/src/index.js new file mode 100644 index 000000000000..6136484b9ad5 --- /dev/null +++ b/packages/babel-plugin-syntax-import-module-attributes/src/index.js @@ -0,0 +1,21 @@ +import { declare } from "@babel/helper-plugin-utils"; + +export default declare((api, { version }) => { + api.assertVersion(7); + + if (typeof version !== "string" || version !== "feb-2020") { + throw new Error( + "The 'moduleAttributes' plugin requires a 'version' option," + + " representing the last proposal update. Currently, the" + + " only supported value is 'feb-2020'.", + ); + } + + return { + name: "syntax-import-module-attributes", + + manipulateOptions(opts, parserOpts) { + parserOpts.plugins.push(["moduleAttributes", { version }]); + }, + }; +}); From 41bb994d04aa54ef107da9f40eec68df94fa9845 Mon Sep 17 00:00:00 2001 From: Vivek Nayyar Date: Mon, 16 Mar 2020 01:14:36 +0100 Subject: [PATCH 22/63] fix: fixed a message test in the readme for syntax-import-module-attributes --- packages/babel-plugin-syntax-import-module-attributes/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/babel-plugin-syntax-import-module-attributes/README.md b/packages/babel-plugin-syntax-import-module-attributes/README.md index 4a3dadb0cf96..1ba0cabbf67a 100644 --- a/packages/babel-plugin-syntax-import-module-attributes/README.md +++ b/packages/babel-plugin-syntax-import-module-attributes/README.md @@ -1,6 +1,6 @@ # @babel/plugin-syntax-import-module-attributes -> Allow parsing of the pipeline operator +> Allow parsing of the module attributes in the import statements See our website [@babel/plugin-syntax-import-module-attributes](https://babeljs.io/docs/en/next/babel-plugin-syntax-import-module-attributes.html) for more information. From 4030426644470e7504ca32320a17e219387f66fd Mon Sep 17 00:00:00 2001 From: Vivek Nayyar Date: Mon, 16 Mar 2020 01:29:57 +0100 Subject: [PATCH 23/63] refactor: removed the error key ModuleAttributesWithoutType as it is not needed now --- packages/babel-parser/src/parser/location.js | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/babel-parser/src/parser/location.js b/packages/babel-parser/src/parser/location.js index 32df01d54a3b..a9eb9cecb24e 100644 --- a/packages/babel-parser/src/parser/location.js +++ b/packages/babel-parser/src/parser/location.js @@ -104,7 +104,6 @@ export const Errors = Object.freeze({ "Only string literals are allowed as module attribute values", ModuleAttributesWithDuplicateKeys: 'Duplicate key "%0" is not allowed in module attributes', - ModuleAttributesWithoutType: 'Module attributes should include a "type" key', ModuleExportUndefined: "Export '%0' is not defined", MultipleDefaultsInSwitch: "Multiple default clauses", NewlineAfterThrow: "Illegal newline after throw", From 792d09642b9bc5a8760d62881bdf31b41f22cd61 Mon Sep 17 00:00:00 2001 From: Vivek Nayyar Date: Mon, 16 Mar 2020 01:41:30 +0100 Subject: [PATCH 24/63] refactor: renamed syntax-import-module-attributes to syntax-module-attributes --- .../README.md | 19 ------------------- .../.npmignore | 0 .../README.md | 19 +++++++++++++++++++ .../package.json | 4 ++-- .../src/index.js | 2 +- 5 files changed, 22 insertions(+), 22 deletions(-) delete mode 100644 packages/babel-plugin-syntax-import-module-attributes/README.md rename packages/{babel-plugin-syntax-import-module-attributes => babel-plugin-syntax-module-attributes}/.npmignore (100%) create mode 100644 packages/babel-plugin-syntax-module-attributes/README.md rename packages/{babel-plugin-syntax-import-module-attributes => babel-plugin-syntax-module-attributes}/package.json (81%) rename packages/{babel-plugin-syntax-import-module-attributes => babel-plugin-syntax-module-attributes}/src/index.js (92%) diff --git a/packages/babel-plugin-syntax-import-module-attributes/README.md b/packages/babel-plugin-syntax-import-module-attributes/README.md deleted file mode 100644 index 1ba0cabbf67a..000000000000 --- a/packages/babel-plugin-syntax-import-module-attributes/README.md +++ /dev/null @@ -1,19 +0,0 @@ -# @babel/plugin-syntax-import-module-attributes - -> Allow parsing of the module attributes in the import statements - -See our website [@babel/plugin-syntax-import-module-attributes](https://babeljs.io/docs/en/next/babel-plugin-syntax-import-module-attributes.html) for more information. - -## Install - -Using npm: - -```sh -npm install --save-dev @babel/plugin-syntax-import-module-attributes -``` - -or using yarn: - -```sh -yarn add @babel/plugin-syntax-import-module-attributes --dev -``` diff --git a/packages/babel-plugin-syntax-import-module-attributes/.npmignore b/packages/babel-plugin-syntax-module-attributes/.npmignore similarity index 100% rename from packages/babel-plugin-syntax-import-module-attributes/.npmignore rename to packages/babel-plugin-syntax-module-attributes/.npmignore diff --git a/packages/babel-plugin-syntax-module-attributes/README.md b/packages/babel-plugin-syntax-module-attributes/README.md new file mode 100644 index 000000000000..3ae0f5a00927 --- /dev/null +++ b/packages/babel-plugin-syntax-module-attributes/README.md @@ -0,0 +1,19 @@ +# @babel/plugin-syntax-module-attributes + +> Allow parsing of the module attributes in the import statements + +See our website [@babel/plugin-syntax-module-attributes](https://babeljs.io/docs/en/next/babel-plugin-syntax-module-attributes.html) for more information. + +## Install + +Using npm: + +```sh +npm install --save-dev @babel/plugin-syntax-module-attributes +``` + +or using yarn: + +```sh +yarn add @babel/plugin-syntax-module-attributes --dev +``` diff --git a/packages/babel-plugin-syntax-import-module-attributes/package.json b/packages/babel-plugin-syntax-module-attributes/package.json similarity index 81% rename from packages/babel-plugin-syntax-import-module-attributes/package.json rename to packages/babel-plugin-syntax-module-attributes/package.json index 8cf463f52e21..3a828f67decd 100644 --- a/packages/babel-plugin-syntax-import-module-attributes/package.json +++ b/packages/babel-plugin-syntax-module-attributes/package.json @@ -1,8 +1,8 @@ { - "name": "@babel/plugin-syntax-import-module-attributes", + "name": "@babel/plugin-syntax-module-attributes", "version": "7.8.3", "description": "Allow parsing of the module attributes in the import statement", - "repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-syntax-import-module-attributes", + "repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-syntax-module-attributes", "license": "MIT", "publishConfig": { "access": "public" diff --git a/packages/babel-plugin-syntax-import-module-attributes/src/index.js b/packages/babel-plugin-syntax-module-attributes/src/index.js similarity index 92% rename from packages/babel-plugin-syntax-import-module-attributes/src/index.js rename to packages/babel-plugin-syntax-module-attributes/src/index.js index 6136484b9ad5..e3bf31baaf0c 100644 --- a/packages/babel-plugin-syntax-import-module-attributes/src/index.js +++ b/packages/babel-plugin-syntax-module-attributes/src/index.js @@ -12,7 +12,7 @@ export default declare((api, { version }) => { } return { - name: "syntax-import-module-attributes", + name: "syntax-module-attributes", manipulateOptions(opts, parserOpts) { parserOpts.plugins.push(["moduleAttributes", { version }]); From a3c56ff3879184c8abd0e6d2104548c0d2f0e62c Mon Sep 17 00:00:00 2001 From: Vivek Nayyar Date: Sun, 5 Jan 2020 16:45:34 +0100 Subject: [PATCH 25/63] :recycle: added basic support for module attributes and tests updated --- .../babel-parser/src/parser/expression.js | 20 ++ packages/babel-parser/src/parser/statement.js | 22 +++ .../interpreter-directive-import/output.json | 3 +- .../comments/regression/10892/output.json | 1 + .../sourcetype-unambiguous/flow/output.json | 3 +- .../module-import/output.json | 3 +- .../invalid-escape-seq-import/output.json | 3 +- .../output.json | 3 +- .../import-invalid-keyword-flow/output.json | 3 +- .../output.json | 3 +- .../import-invalid-keyword-typeof/output.json | 3 +- .../import-invalid-keyword/output.json | 3 +- .../es2015/uncategorised/291/output.json | 3 +- .../es2015/uncategorised/301/output.json | 3 +- .../es2015/uncategorised/88/output.json | 3 +- .../es2015/uncategorised/91/output.json | 3 +- .../es2015/uncategorised/92/output.json | 3 +- .../es2015/uncategorised/93/output.json | 3 +- .../es2015/uncategorised/94/output.json | 3 +- .../es2015/uncategorised/95/output.json | 3 +- .../es2015/uncategorised/97/output.json | 3 +- .../es2015/uncategorised/98/output.json | 3 +- .../output.json | 3 +- .../output.json | 3 +- .../import-default-as/output.json | 3 +- .../import-default/output.json | 3 +- .../import-jquery/output.json | 3 +- .../import-module/output.json | 3 +- .../import-named-as-specifier/output.json | 3 +- .../import-named-as-specifiers/output.json | 3 +- .../import-named-empty/output.json | 3 +- .../import-named-specifier/output.json | 3 +- .../import-named-specifiers-comma/output.json | 3 +- .../import-named-specifiers/output.json | 3 +- .../import-namespace-specifier/output.json | 3 +- .../import-null-as-nil/output.json | 3 +- .../import-module-attributes/input.js | 1 + .../import-module-attributes/options.json | 5 + .../valid-syntax-with-attributes/input.js | 1 + .../valid-syntax-with-attributes/options.json | 4 + .../valid-syntax-with-attributes/output.json | 176 ++++++++++++++++++ .../valid-syntax-without-attributes/input.js | 1 + .../options.json | 4 + .../output.json | 105 +++++++++++ .../flow/declare-module/import/output.json | 6 +- .../declare-module/invalid-import/output.json | 3 +- .../flow/sourcetype-script/import/output.json | 6 +- .../type-imports/import-type-2/output.json | 3 +- .../import-type-shorthand/output.json | 30 ++- .../flow/type-imports/import-type/output.json | 39 ++-- .../invalid-import-type-2/output.json | 3 +- .../invalid-import-type-3/output.json | 3 +- .../invalid-import-type-4/output.json | 3 +- .../invalid-import-type-as/output.json | 3 +- .../output.json | 3 +- .../output.json | 3 +- .../output.json | 3 +- .../invalid-import-type-shorthand/output.json | 3 +- .../invalid-import-type/output.json | 3 +- .../import/default-named/output.json | 3 +- .../import/default-star-2/output.json | 3 +- .../import/file-empty/output.json | 3 +- .../import/named-alias-2/output.json | 3 +- .../import/named-alias-3/output.json | 3 +- .../import/named-alias/output.json | 3 +- .../placeholders/import/named/output.json | 3 +- .../placeholders/import/star/output.json | 3 +- .../import/not-top-level/output.json | 3 +- .../output.json | 3 +- 69 files changed, 502 insertions(+), 81 deletions(-) create mode 100644 packages/babel-parser/test/fixtures/experimental/_no-plugin/import-module-attributes/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/_no-plugin/import-module-attributes/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes/output.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-without-attributes/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-without-attributes/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-without-attributes/output.json diff --git a/packages/babel-parser/src/parser/expression.js b/packages/babel-parser/src/parser/expression.js index 06999c46f24b..622ff9b77588 100644 --- a/packages/babel-parser/src/parser/expression.js +++ b/packages/babel-parser/src/parser/expression.js @@ -1498,6 +1498,26 @@ export default class ExpressionParser extends LValParser { return this.finishNode(node, "TemplateLiteral"); } + parseKeyValuePairs() { + const propHash: any = Object.create(null); + const node = this.startNode(); + node.properties = []; + + do { + const prop = this.parseObjectMember(); + // $FlowIgnore + if (prop.shorthand) { + this.addExtra(prop, "shorthand", true); + } + + node.properties.push(prop); + } while (this.eat(tt.comma)); + if (!this.match(tt.eq) && propHash.start !== undefined) { + this.raise(propHash.start, "Redefinition of __proto__ property"); + } + return this.finishNode(node, "ObjectExpression"); + } + // Parse an object literal, binding pattern, or record. parseObj( diff --git a/packages/babel-parser/src/parser/statement.js b/packages/babel-parser/src/parser/statement.js index 5b1d1077712e..7815b7e44704 100644 --- a/packages/babel-parser/src/parser/statement.js +++ b/packages/babel-parser/src/parser/statement.js @@ -2041,13 +2041,26 @@ export default class StatementParser extends ExpressionParser { // import '...' node.specifiers = []; if (!this.match(tt.string)) { + // check if we have a default import like + // import React from "react"; const hasDefault = this.maybeParseDefaultImportSpecifier(node); + /* we are checking if we do not have a default import, then it is obvious that we need named imports + * import { get } from "axios"; + * but if we do have a default import + * we need to check if we have a comma after that and + * that is where this || this.eat comes into play + */ const parseNext = !hasDefault || this.eat(tt.comma); + // if we do have to parse the next set of specifiers, we first check for start imports + // import React, * from "react"; const hasStar = parseNext && this.maybeParseStarImportSpecifier(node); + // now we check that if we need to parse the next imports + // but only if he/she is not importing * which means all if (parseNext && !hasStar) this.parseNamedImportSpecifiers(node); this.expectContextual("from"); } node.source = this.parseImportSource(); + node.attributes = this.maybeParseModuleAttributes(); this.semicolon(); return this.finishNode(node, "ImportDeclaration"); } @@ -2078,6 +2091,15 @@ export default class StatementParser extends ExpressionParser { node.specifiers.push(this.finishNode(specifier, type)); } + maybeParseModuleAttributes() { + if (this.match(tt._with) && !this.hasPlugin("moduleAttributes")) { + this.expectPlugin("moduleAttributes"); + } else if (this.eat(tt._with)) { + return this.parseKeyValuePairs(); + } + return []; + } + maybeParseDefaultImportSpecifier(node: N.ImportDeclaration): boolean { if (this.shouldParseDefaultImport(node)) { // import defaultObj, { x, y as z } from '...' diff --git a/packages/babel-parser/test/fixtures/comments/interpreter-directive/interpreter-directive-import/output.json b/packages/babel-parser/test/fixtures/comments/interpreter-directive/interpreter-directive-import/output.json index 722c14c6842e..bd3e1b897a8c 100644 --- a/packages/babel-parser/test/fixtures/comments/interpreter-directive/interpreter-directive-import/output.json +++ b/packages/babel-parser/test/fixtures/comments/interpreter-directive/interpreter-directive-import/output.json @@ -38,7 +38,8 @@ "raw": "'foobar'" }, "value": "foobar" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/comments/regression/10892/output.json b/packages/babel-parser/test/fixtures/comments/regression/10892/output.json index efa9a47df667..f26703dfb3d3 100644 --- a/packages/babel-parser/test/fixtures/comments/regression/10892/output.json +++ b/packages/babel-parser/test/fixtures/comments/regression/10892/output.json @@ -35,6 +35,7 @@ }, "value": "bar" }, + "attributes": [], "trailingComments": [ { "type": "CommentBlock", diff --git a/packages/babel-parser/test/fixtures/core/sourcetype-unambiguous/flow/output.json b/packages/babel-parser/test/fixtures/core/sourcetype-unambiguous/flow/output.json index 69c62635c89e..1f0e6d691536 100644 --- a/packages/babel-parser/test/fixtures/core/sourcetype-unambiguous/flow/output.json +++ b/packages/babel-parser/test/fixtures/core/sourcetype-unambiguous/flow/output.json @@ -36,7 +36,8 @@ "raw": "\"bar\"" }, "value": "bar" - } + }, + "attributes": [] }, { "type": "ExportNamedDeclaration", diff --git a/packages/babel-parser/test/fixtures/core/sourcetype-unambiguous/module-import/output.json b/packages/babel-parser/test/fixtures/core/sourcetype-unambiguous/module-import/output.json index 95736fdee6c1..fd54d2d84204 100644 --- a/packages/babel-parser/test/fixtures/core/sourcetype-unambiguous/module-import/output.json +++ b/packages/babel-parser/test/fixtures/core/sourcetype-unambiguous/module-import/output.json @@ -19,7 +19,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/es2015/identifiers/invalid-escape-seq-import/output.json b/packages/babel-parser/test/fixtures/es2015/identifiers/invalid-escape-seq-import/output.json index 0533a2a4de2a..e7e8fe7b678d 100644 --- a/packages/babel-parser/test/fixtures/es2015/identifiers/invalid-escape-seq-import/output.json +++ b/packages/babel-parser/test/fixtures/es2015/identifiers/invalid-escape-seq-import/output.json @@ -49,7 +49,8 @@ "raw": "\"x\"" }, "value": "x" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/es2015/modules/import-declaration-trailing-comma/output.json b/packages/babel-parser/test/fixtures/es2015/modules/import-declaration-trailing-comma/output.json index a968e0267d23..6830520178c8 100644 --- a/packages/babel-parser/test/fixtures/es2015/modules/import-declaration-trailing-comma/output.json +++ b/packages/babel-parser/test/fixtures/es2015/modules/import-declaration-trailing-comma/output.json @@ -58,7 +58,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword-flow/output.json b/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword-flow/output.json index ab2ca9601162..15d369aaaf56 100644 --- a/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword-flow/output.json +++ b/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword-flow/output.json @@ -39,7 +39,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword-typeof-flow/output.json b/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword-typeof-flow/output.json index d4df45baefea..ef8aa95aef9a 100644 --- a/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword-typeof-flow/output.json +++ b/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword-typeof-flow/output.json @@ -39,7 +39,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword-typeof/output.json b/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword-typeof/output.json index 95070d9dab76..72c00d3b2a9a 100644 --- a/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword-typeof/output.json +++ b/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword-typeof/output.json @@ -37,7 +37,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword/output.json b/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword/output.json index a627dce5e719..fe0c6038a3ef 100644 --- a/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword/output.json +++ b/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword/output.json @@ -37,7 +37,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/es2015/uncategorised/291/output.json b/packages/babel-parser/test/fixtures/es2015/uncategorised/291/output.json index c87a88a757b7..07e64148911e 100644 --- a/packages/babel-parser/test/fixtures/es2015/uncategorised/291/output.json +++ b/packages/babel-parser/test/fixtures/es2015/uncategorised/291/output.json @@ -35,7 +35,8 @@ "raw": "\"acorn\"" }, "value": "acorn" - } + }, + "attributes": [] }, "alternate": null } diff --git a/packages/babel-parser/test/fixtures/es2015/uncategorised/301/output.json b/packages/babel-parser/test/fixtures/es2015/uncategorised/301/output.json index fc986dc29395..4c1fe101ce9f 100644 --- a/packages/babel-parser/test/fixtures/es2015/uncategorised/301/output.json +++ b/packages/babel-parser/test/fixtures/es2015/uncategorised/301/output.json @@ -38,7 +38,8 @@ "raw": "'baz'" }, "value": "baz" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/es2015/uncategorised/88/output.json b/packages/babel-parser/test/fixtures/es2015/uncategorised/88/output.json index bbc9bf9f48c8..a02838cd1f5f 100644 --- a/packages/babel-parser/test/fixtures/es2015/uncategorised/88/output.json +++ b/packages/babel-parser/test/fixtures/es2015/uncategorised/88/output.json @@ -29,7 +29,8 @@ "raw": "\"\"" }, "value": "" - } + }, + "attributes": [] }, { "type": "ExportNamedDeclaration", diff --git a/packages/babel-parser/test/fixtures/es2015/uncategorised/91/output.json b/packages/babel-parser/test/fixtures/es2015/uncategorised/91/output.json index 7a55897d48e9..c32ed6c01721 100644 --- a/packages/babel-parser/test/fixtures/es2015/uncategorised/91/output.json +++ b/packages/babel-parser/test/fixtures/es2015/uncategorised/91/output.json @@ -19,7 +19,8 @@ "raw": "\"jquery\"" }, "value": "jquery" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/es2015/uncategorised/92/output.json b/packages/babel-parser/test/fixtures/es2015/uncategorised/92/output.json index 3e36b1ca7765..6a457940f682 100644 --- a/packages/babel-parser/test/fixtures/es2015/uncategorised/92/output.json +++ b/packages/babel-parser/test/fixtures/es2015/uncategorised/92/output.json @@ -29,7 +29,8 @@ "raw": "\"jquery\"" }, "value": "jquery" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/es2015/uncategorised/93/output.json b/packages/babel-parser/test/fixtures/es2015/uncategorised/93/output.json index 6f8d6a652542..52526286a8b4 100644 --- a/packages/babel-parser/test/fixtures/es2015/uncategorised/93/output.json +++ b/packages/babel-parser/test/fixtures/es2015/uncategorised/93/output.json @@ -48,7 +48,8 @@ "raw": "\"crypto\"" }, "value": "crypto" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/es2015/uncategorised/94/output.json b/packages/babel-parser/test/fixtures/es2015/uncategorised/94/output.json index e36df8cbfd12..95777822f184 100644 --- a/packages/babel-parser/test/fixtures/es2015/uncategorised/94/output.json +++ b/packages/babel-parser/test/fixtures/es2015/uncategorised/94/output.json @@ -34,7 +34,8 @@ "raw": "\"crypto\"" }, "value": "crypto" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/es2015/uncategorised/95/output.json b/packages/babel-parser/test/fixtures/es2015/uncategorised/95/output.json index a391944e5ad3..4165c72b6896 100644 --- a/packages/babel-parser/test/fixtures/es2015/uncategorised/95/output.json +++ b/packages/babel-parser/test/fixtures/es2015/uncategorised/95/output.json @@ -57,7 +57,8 @@ "raw": "\"crypto\"" }, "value": "crypto" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/es2015/uncategorised/97/output.json b/packages/babel-parser/test/fixtures/es2015/uncategorised/97/output.json index 0c902bf05c85..f999c9c20ca7 100644 --- a/packages/babel-parser/test/fixtures/es2015/uncategorised/97/output.json +++ b/packages/babel-parser/test/fixtures/es2015/uncategorised/97/output.json @@ -34,7 +34,8 @@ "raw": "\"bar\"" }, "value": "bar" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/es2015/uncategorised/98/output.json b/packages/babel-parser/test/fixtures/es2015/uncategorised/98/output.json index 628f749c47c5..19a191f41b7e 100644 --- a/packages/babel-parser/test/fixtures/es2015/uncategorised/98/output.json +++ b/packages/babel-parser/test/fixtures/es2015/uncategorised/98/output.json @@ -29,7 +29,8 @@ "raw": "\"crypto\"" }, "value": "crypto" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default-and-named-specifiers/output.json b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default-and-named-specifiers/output.json index 05f4b187ee88..2e62e45d0e31 100644 --- a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default-and-named-specifiers/output.json +++ b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default-and-named-specifiers/output.json @@ -43,7 +43,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default-and-namespace-specifiers/output.json b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default-and-namespace-specifiers/output.json index 9985ba0bfaed..4b1f4f243cc3 100644 --- a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default-and-namespace-specifiers/output.json +++ b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default-and-namespace-specifiers/output.json @@ -38,7 +38,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default-as/output.json b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default-as/output.json index b14f5cdb95e0..29ee5f91c3c7 100644 --- a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default-as/output.json +++ b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default-as/output.json @@ -34,7 +34,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default/output.json b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default/output.json index 22597cae7cae..2c5a920fb1fd 100644 --- a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default/output.json +++ b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default/output.json @@ -29,7 +29,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-jquery/output.json b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-jquery/output.json index 3e36b1ca7765..6a457940f682 100644 --- a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-jquery/output.json +++ b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-jquery/output.json @@ -29,7 +29,8 @@ "raw": "\"jquery\"" }, "value": "jquery" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-module/output.json b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-module/output.json index 95736fdee6c1..fd54d2d84204 100644 --- a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-module/output.json +++ b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-module/output.json @@ -19,7 +19,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-as-specifier/output.json b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-as-specifier/output.json index cf9dd503a4c4..ef10f1a837dd 100644 --- a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-as-specifier/output.json +++ b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-as-specifier/output.json @@ -34,7 +34,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-as-specifiers/output.json b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-as-specifiers/output.json index 6bcc40f19ba1..e3e3e6069c6f 100644 --- a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-as-specifiers/output.json +++ b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-as-specifiers/output.json @@ -48,7 +48,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-empty/output.json b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-empty/output.json index b5f711497e31..1a195b03046d 100644 --- a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-empty/output.json +++ b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-empty/output.json @@ -19,7 +19,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-specifier/output.json b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-specifier/output.json index a54a3104a79a..4becc99d3eb2 100644 --- a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-specifier/output.json +++ b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-specifier/output.json @@ -34,7 +34,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-specifiers-comma/output.json b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-specifiers-comma/output.json index 388349480ddf..39047aa5cb0f 100644 --- a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-specifiers-comma/output.json +++ b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-specifiers-comma/output.json @@ -48,7 +48,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-specifiers/output.json b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-specifiers/output.json index 88915ea0223a..96197dd33c42 100644 --- a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-specifiers/output.json +++ b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-specifiers/output.json @@ -48,7 +48,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-namespace-specifier/output.json b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-namespace-specifier/output.json index 3d082bcf7a65..83eeba133356 100644 --- a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-namespace-specifier/output.json +++ b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-namespace-specifier/output.json @@ -29,7 +29,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-null-as-nil/output.json b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-null-as-nil/output.json index 0c902bf05c85..f999c9c20ca7 100644 --- a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-null-as-nil/output.json +++ b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-null-as-nil/output.json @@ -34,7 +34,8 @@ "raw": "\"bar\"" }, "value": "bar" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-module-attributes/input.js b/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-module-attributes/input.js new file mode 100644 index 000000000000..fb31508a0860 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-module-attributes/input.js @@ -0,0 +1 @@ +import foo from "foo.json" with type: "json"; diff --git a/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-module-attributes/options.json b/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-module-attributes/options.json new file mode 100644 index 000000000000..3f9b696c8de9 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-module-attributes/options.json @@ -0,0 +1,5 @@ +{ + "throws": "This experimental syntax requires enabling the parser plugin: 'moduleAttributes' (1:27)", + "sourceType": "module", + "plugins": [] +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes/input.js b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes/input.js new file mode 100644 index 000000000000..fb31508a0860 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes/input.js @@ -0,0 +1 @@ +import foo from "foo.json" with type: "json"; diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes/options.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes/options.json new file mode 100644 index 000000000000..9399e0bedd3f --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes/options.json @@ -0,0 +1,4 @@ +{ + "plugins": ["moduleAttributes"], + "sourceType": "module" +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes/output.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes/output.json new file mode 100644 index 000000000000..490e4ee43d32 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes/output.json @@ -0,0 +1,176 @@ +{ + "type": "File", + "start": 0, + "end": 45, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 45 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 45, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 45 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start": 0, + "end": 45, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 45 + } + }, + "specifiers": [ + { + "type": "ImportDefaultSpecifier", + "start": 7, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "local": { + "type": "Identifier", + "start": 7, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 10 + }, + "identifierName": "foo" + }, + "name": "foo" + } + } + ], + "source": { + "type": "StringLiteral", + "start": 16, + "end": 26, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 26 + } + }, + "extra": { + "rawValue": "foo.json", + "raw": "\"foo.json\"" + }, + "value": "foo.json" + }, + "attributes": { + "type": "ObjectExpression", + "start": 32, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 32 + }, + "end": { + "line": 1, + "column": 44 + } + }, + "properties": [ + { + "type": "ObjectProperty", + "start": 32, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 32 + }, + "end": { + "line": 1, + "column": 44 + } + }, + "method": false, + "key": { + "type": "Identifier", + "start": 32, + "end": 36, + "loc": { + "start": { + "line": 1, + "column": 32 + }, + "end": { + "line": 1, + "column": 36 + }, + "identifierName": "type" + }, + "name": "type" + }, + "computed": false, + "shorthand": false, + "value": { + "type": "StringLiteral", + "start": 38, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 38 + }, + "end": { + "line": 1, + "column": 44 + } + }, + "extra": { + "rawValue": "json", + "raw": "\"json\"" + }, + "value": "json" + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-without-attributes/input.js b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-without-attributes/input.js new file mode 100644 index 000000000000..92901ed56b38 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-without-attributes/input.js @@ -0,0 +1 @@ +import foo from "foo.json"; diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-without-attributes/options.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-without-attributes/options.json new file mode 100644 index 000000000000..9399e0bedd3f --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-without-attributes/options.json @@ -0,0 +1,4 @@ +{ + "plugins": ["moduleAttributes"], + "sourceType": "module" +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-without-attributes/output.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-without-attributes/output.json new file mode 100644 index 000000000000..dec2f8457cbd --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-without-attributes/output.json @@ -0,0 +1,105 @@ +{ + "type": "File", + "start": 0, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 27 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 27 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start": 0, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 27 + } + }, + "specifiers": [ + { + "type": "ImportDefaultSpecifier", + "start": 7, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "local": { + "type": "Identifier", + "start": 7, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 10 + }, + "identifierName": "foo" + }, + "name": "foo" + } + } + ], + "source": { + "type": "StringLiteral", + "start": 16, + "end": 26, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 26 + } + }, + "extra": { + "rawValue": "foo.json", + "raw": "\"foo.json\"" + }, + "value": "foo.json" + }, + "attributes": [] + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/flow/declare-module/import/output.json b/packages/babel-parser/test/fixtures/flow/declare-module/import/output.json index f559a7704e8c..1427fa357570 100644 --- a/packages/babel-parser/test/fixtures/flow/declare-module/import/output.json +++ b/packages/babel-parser/test/fixtures/flow/declare-module/import/output.json @@ -46,7 +46,8 @@ "raw": "\"TM\"" }, "value": "TM" - } + }, + "attributes": [] }, { "type": "ImportDeclaration", @@ -71,7 +72,8 @@ "raw": "\"UM\"" }, "value": "UM" - } + }, + "attributes": [] } ] }, diff --git a/packages/babel-parser/test/fixtures/flow/declare-module/invalid-import/output.json b/packages/babel-parser/test/fixtures/flow/declare-module/invalid-import/output.json index 6155f8eda4f7..6c53f46014a2 100644 --- a/packages/babel-parser/test/fixtures/flow/declare-module/invalid-import/output.json +++ b/packages/babel-parser/test/fixtures/flow/declare-module/invalid-import/output.json @@ -49,7 +49,8 @@ "raw": "\"TM\"" }, "value": "TM" - } + }, + "attributes": [] } ] }, diff --git a/packages/babel-parser/test/fixtures/flow/sourcetype-script/import/output.json b/packages/babel-parser/test/fixtures/flow/sourcetype-script/import/output.json index 54d736943f7f..96e56935c53f 100644 --- a/packages/babel-parser/test/fixtures/flow/sourcetype-script/import/output.json +++ b/packages/babel-parser/test/fixtures/flow/sourcetype-script/import/output.json @@ -36,7 +36,8 @@ "raw": "\"\"" }, "value": "" - } + }, + "attributes": [] }, { "type": "ImportDeclaration", @@ -61,7 +62,8 @@ "raw": "\"\"" }, "value": "" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/flow/type-imports/import-type-2/output.json b/packages/babel-parser/test/fixtures/flow/type-imports/import-type-2/output.json index 2406bba2f4f2..a60601d0601f 100644 --- a/packages/babel-parser/test/fixtures/flow/type-imports/import-type-2/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-imports/import-type-2/output.json @@ -45,7 +45,8 @@ "raw": "\"bar\"" }, "value": "bar" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/flow/type-imports/import-type-shorthand/output.json b/packages/babel-parser/test/fixtures/flow/type-imports/import-type-shorthand/output.json index 437eac470802..7c7fa9bcd76f 100644 --- a/packages/babel-parser/test/fixtures/flow/type-imports/import-type-shorthand/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-imports/import-type-shorthand/output.json @@ -36,7 +36,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] }, { "type": "ImportDeclaration", @@ -67,7 +68,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] }, { "type": "ImportDeclaration", @@ -98,7 +100,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] }, { "type": "ImportDeclaration", @@ -129,7 +132,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] }, { "type": "ImportDeclaration", @@ -160,7 +164,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] }, { "type": "ImportDeclaration", @@ -191,7 +196,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] }, { "type": "ImportDeclaration", @@ -222,7 +228,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] }, { "type": "ImportDeclaration", @@ -253,7 +260,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] }, { "type": "ImportDeclaration", @@ -284,7 +292,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] }, { "type": "ImportDeclaration", @@ -315,7 +324,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/flow/type-imports/import-type/output.json b/packages/babel-parser/test/fixtures/flow/type-imports/import-type/output.json index fcc0c31ed6dd..cd336d76f4aa 100644 --- a/packages/babel-parser/test/fixtures/flow/type-imports/import-type/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-imports/import-type/output.json @@ -30,7 +30,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] }, { "type": "ImportDeclaration", @@ -61,7 +62,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] }, { "type": "ImportDeclaration", @@ -101,7 +103,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] }, { "type": "ImportDeclaration", @@ -126,7 +129,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] }, { "type": "ImportDeclaration", @@ -157,7 +161,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] }, { "type": "ImportDeclaration", @@ -203,7 +208,8 @@ "raw": "\"baz\"" }, "value": "baz" - } + }, + "attributes": [] }, { "type": "ImportDeclaration", @@ -228,7 +234,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] }, { "type": "ImportDeclaration", @@ -253,7 +260,8 @@ "raw": "\"bar\"" }, "value": "bar" - } + }, + "attributes": [] }, { "type": "ImportDeclaration", @@ -278,7 +286,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] }, { "type": "ImportDeclaration", @@ -309,7 +318,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] }, { "type": "ImportDeclaration", @@ -340,7 +350,8 @@ "raw": "\"baz\"" }, "value": "baz" - } + }, + "attributes": [] }, { "type": "ImportDeclaration", @@ -365,7 +376,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] }, { "type": "ImportDeclaration", @@ -390,7 +402,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-2/output.json b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-2/output.json index cbd99a19951e..0b47f59fb3b0 100644 --- a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-2/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-2/output.json @@ -39,7 +39,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-3/output.json b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-3/output.json index 1e241ce54643..aa69ad7cf042 100644 --- a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-3/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-3/output.json @@ -33,7 +33,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-4/output.json b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-4/output.json index 4598f3355fcf..564aa10525db 100644 --- a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-4/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-4/output.json @@ -33,7 +33,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-as/output.json b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-as/output.json index 20c6c607313f..4afb6bf0172e 100644 --- a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-as/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-as/output.json @@ -39,7 +39,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand-2/output.json b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand-2/output.json index 37af63c0996a..68ba6fe9cef4 100644 --- a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand-2/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand-2/output.json @@ -39,7 +39,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand-3/output.json b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand-3/output.json index 56fba75c81f5..59a0f2b81932 100644 --- a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand-3/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand-3/output.json @@ -39,7 +39,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand-4/output.json b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand-4/output.json index ff46538a95f6..5986e389bcec 100644 --- a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand-4/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand-4/output.json @@ -39,7 +39,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand/output.json b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand/output.json index 8d2dd58fa474..ade8b4c6fdb7 100644 --- a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand/output.json @@ -39,7 +39,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type/output.json b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type/output.json index 78cb69bb8dea..924ead3fa2b9 100644 --- a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type/output.json @@ -33,7 +33,8 @@ "raw": "\"foo\"" }, "value": "foo" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/placeholders/import/default-named/output.json b/packages/babel-parser/test/fixtures/placeholders/import/default-named/output.json index d46d85c82643..ac8bd5d4dff1 100644 --- a/packages/babel-parser/test/fixtures/placeholders/import/default-named/output.json +++ b/packages/babel-parser/test/fixtures/placeholders/import/default-named/output.json @@ -53,7 +53,8 @@ "raw": "\"file\"" }, "value": "file" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/placeholders/import/default-star-2/output.json b/packages/babel-parser/test/fixtures/placeholders/import/default-star-2/output.json index 1fb751ada336..b296a9dccf62 100644 --- a/packages/babel-parser/test/fixtures/placeholders/import/default-star-2/output.json +++ b/packages/babel-parser/test/fixtures/placeholders/import/default-star-2/output.json @@ -43,7 +43,8 @@ "raw": "\"file\"" }, "value": "file" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/placeholders/import/file-empty/output.json b/packages/babel-parser/test/fixtures/placeholders/import/file-empty/output.json index 2b7e6735068e..977725e1ed59 100644 --- a/packages/babel-parser/test/fixtures/placeholders/import/file-empty/output.json +++ b/packages/babel-parser/test/fixtures/placeholders/import/file-empty/output.json @@ -20,7 +20,8 @@ "name": "FILE" }, "expectedNode": "StringLiteral" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/placeholders/import/named-alias-2/output.json b/packages/babel-parser/test/fixtures/placeholders/import/named-alias-2/output.json index b52ba4d5dd43..c385d4de9e76 100644 --- a/packages/babel-parser/test/fixtures/placeholders/import/named-alias-2/output.json +++ b/packages/babel-parser/test/fixtures/placeholders/import/named-alias-2/output.json @@ -39,7 +39,8 @@ "raw": "\"file\"" }, "value": "file" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/placeholders/import/named-alias-3/output.json b/packages/babel-parser/test/fixtures/placeholders/import/named-alias-3/output.json index fe0f44bed277..20927b85e6f0 100644 --- a/packages/babel-parser/test/fixtures/placeholders/import/named-alias-3/output.json +++ b/packages/babel-parser/test/fixtures/placeholders/import/named-alias-3/output.json @@ -44,7 +44,8 @@ "raw": "\"file\"" }, "value": "file" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/placeholders/import/named-alias/output.json b/packages/babel-parser/test/fixtures/placeholders/import/named-alias/output.json index aa6ab6e293a6..171e144df48c 100644 --- a/packages/babel-parser/test/fixtures/placeholders/import/named-alias/output.json +++ b/packages/babel-parser/test/fixtures/placeholders/import/named-alias/output.json @@ -39,7 +39,8 @@ "raw": "\"file\"" }, "value": "file" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/placeholders/import/named/output.json b/packages/babel-parser/test/fixtures/placeholders/import/named/output.json index 1508f63de6f9..bc246007adfd 100644 --- a/packages/babel-parser/test/fixtures/placeholders/import/named/output.json +++ b/packages/babel-parser/test/fixtures/placeholders/import/named/output.json @@ -44,7 +44,8 @@ "raw": "\"file\"" }, "value": "file" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/placeholders/import/star/output.json b/packages/babel-parser/test/fixtures/placeholders/import/star/output.json index 2d8f5acbca2c..54cd48d51621 100644 --- a/packages/babel-parser/test/fixtures/placeholders/import/star/output.json +++ b/packages/babel-parser/test/fixtures/placeholders/import/star/output.json @@ -34,7 +34,8 @@ "raw": "\"file\"" }, "value": "file" - } + }, + "attributes": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/typescript/import/not-top-level/output.json b/packages/babel-parser/test/fixtures/typescript/import/not-top-level/output.json index dd3c9e4ddda7..5484ad7003cd 100644 --- a/packages/babel-parser/test/fixtures/typescript/import/not-top-level/output.json +++ b/packages/babel-parser/test/fixtures/typescript/import/not-top-level/output.json @@ -46,7 +46,8 @@ "raw": "\"a\"" }, "value": "a" - } + }, + "attributes": [] } ] }, diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-import-ambient-class/output.json b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-import-ambient-class/output.json index 479d69c1a16c..ea1420f8d3ce 100644 --- a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-import-ambient-class/output.json +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-import-ambient-class/output.json @@ -30,7 +30,8 @@ "raw": "'./somewhere.js'" }, "value": "./somewhere.js" - } + }, + "attributes": [] }, { "type": "ClassDeclaration", From cbcdb3d47f4c76e177d30029281acb15fb1410fb Mon Sep 17 00:00:00 2001 From: Vivek Nayyar Date: Sun, 5 Jan 2020 23:43:14 +0100 Subject: [PATCH 26/63] attributes should be set only if moduleAttributes plugin is enabled, refactored the parsing logic --- .../babel-parser/src/parser/expression.js | 50 +++++---- packages/babel-parser/src/parser/statement.js | 27 +++-- .../interpreter-directive-import/output.json | 3 +- .../comments/regression/10892/output.json | 1 - .../sourcetype-unambiguous/flow/output.json | 3 +- .../module-import/output.json | 3 +- .../invalid-escape-seq-import/output.json | 3 +- .../output.json | 3 +- .../import-invalid-keyword-flow/output.json | 3 +- .../output.json | 3 +- .../import-invalid-keyword-typeof/output.json | 3 +- .../import-invalid-keyword/output.json | 3 +- .../es2015/uncategorised/291/output.json | 3 +- .../es2015/uncategorised/301/output.json | 3 +- .../es2015/uncategorised/88/output.json | 3 +- .../es2015/uncategorised/91/output.json | 3 +- .../es2015/uncategorised/92/output.json | 3 +- .../es2015/uncategorised/93/output.json | 3 +- .../es2015/uncategorised/94/output.json | 3 +- .../es2015/uncategorised/95/output.json | 3 +- .../es2015/uncategorised/97/output.json | 3 +- .../es2015/uncategorised/98/output.json | 3 +- .../output.json | 3 +- .../output.json | 3 +- .../import-default-as/output.json | 3 +- .../import-default/output.json | 3 +- .../import-jquery/output.json | 3 +- .../import-module/output.json | 3 +- .../import-named-as-specifier/output.json | 3 +- .../import-named-as-specifiers/output.json | 3 +- .../import-named-empty/output.json | 3 +- .../import-named-specifier/output.json | 3 +- .../import-named-specifiers-comma/output.json | 3 +- .../import-named-specifiers/output.json | 3 +- .../import-namespace-specifier/output.json | 3 +- .../import-null-as-nil/output.json | 3 +- .../import-module-attributes/options.json | 2 +- .../valid-syntax-with-attributes/output.json | 101 ++++++++---------- .../flow/declare-module/import/output.json | 6 +- .../declare-module/invalid-import/output.json | 3 +- .../flow/sourcetype-script/import/output.json | 6 +- .../type-imports/import-type-2/output.json | 3 +- .../import-type-shorthand/output.json | 30 ++---- .../flow/type-imports/import-type/output.json | 39 +++---- .../invalid-import-type-2/output.json | 3 +- .../invalid-import-type-3/output.json | 3 +- .../invalid-import-type-4/output.json | 3 +- .../invalid-import-type-as/output.json | 3 +- .../output.json | 3 +- .../output.json | 3 +- .../output.json | 3 +- .../invalid-import-type-shorthand/output.json | 3 +- .../invalid-import-type/output.json | 3 +- .../import/default-named/output.json | 3 +- .../import/default-star-2/output.json | 3 +- .../import/file-empty/output.json | 3 +- .../import/named-alias-2/output.json | 3 +- .../import/named-alias-3/output.json | 3 +- .../import/named-alias/output.json | 3 +- .../placeholders/import/named/output.json | 3 +- .../placeholders/import/star/output.json | 3 +- .../import/not-top-level/output.json | 3 +- .../output.json | 3 +- 63 files changed, 175 insertions(+), 249 deletions(-) diff --git a/packages/babel-parser/src/parser/expression.js b/packages/babel-parser/src/parser/expression.js index 622ff9b77588..4db60db16133 100644 --- a/packages/babel-parser/src/parser/expression.js +++ b/packages/babel-parser/src/parser/expression.js @@ -869,6 +869,35 @@ export default class ExpressionParser extends LValParser { return this.parseSubscripts(this.parseExprAtom(), startPos, startLoc, true); } + /** + * Parse a primitive value + * primitive values are null, undefined, number, bigint, string, boolean + */ + parsePrimitiveValue(): N.Expression { + let node; + switch (this.state.type) { + case tt.num: + return this.parseLiteral(this.state.value, "NumericLiteral"); + + case tt.bigint: + return this.parseLiteral(this.state.value, "BigIntLiteral"); + + case tt.string: + return this.parseLiteral(this.state.value, "StringLiteral"); + + case tt._null: + node = this.startNode(); + this.next(); + return this.finishNode(node, "NullLiteral"); + + case tt._true: + case tt._false: + return this.parseBooleanLiteral(); + default: + throw this.unexpected(); + } + } + // Parse an atomic expression — either a single token that is an // expression, an expression started by a keyword like `function` or // `new`, or an expression wrapped in punctuation like `()`, `[]`, @@ -1498,28 +1527,7 @@ export default class ExpressionParser extends LValParser { return this.finishNode(node, "TemplateLiteral"); } - parseKeyValuePairs() { - const propHash: any = Object.create(null); - const node = this.startNode(); - node.properties = []; - - do { - const prop = this.parseObjectMember(); - // $FlowIgnore - if (prop.shorthand) { - this.addExtra(prop, "shorthand", true); - } - - node.properties.push(prop); - } while (this.eat(tt.comma)); - if (!this.match(tt.eq) && propHash.start !== undefined) { - this.raise(propHash.start, "Redefinition of __proto__ property"); - } - return this.finishNode(node, "ObjectExpression"); - } - // Parse an object literal, binding pattern, or record. - parseObj( close: TokenType, isPattern: boolean, diff --git a/packages/babel-parser/src/parser/statement.js b/packages/babel-parser/src/parser/statement.js index 7815b7e44704..ebab219644df 100644 --- a/packages/babel-parser/src/parser/statement.js +++ b/packages/babel-parser/src/parser/statement.js @@ -2060,7 +2060,10 @@ export default class StatementParser extends ExpressionParser { this.expectContextual("from"); } node.source = this.parseImportSource(); - node.attributes = this.maybeParseModuleAttributes(); + const attributes = this.maybeParseModuleAttributes(); + if (Array.isArray(attributes)) { + node.attributes = attributes; + } this.semicolon(); return this.finishNode(node, "ImportDeclaration"); } @@ -2092,12 +2095,24 @@ export default class StatementParser extends ExpressionParser { } maybeParseModuleAttributes() { - if (this.match(tt._with) && !this.hasPlugin("moduleAttributes")) { - this.expectPlugin("moduleAttributes"); - } else if (this.eat(tt._with)) { - return this.parseKeyValuePairs(); + if (!this.eat(tt._with)) { + if (this.hasPlugin("moduleAttributes")) return []; + return null; } - return []; + this.expectPlugin("moduleAttributes"); + const attrs = []; + do { + const node = this.startNode(); + node.key = this.parseIdentifier(); + node.computed = false; + node.method = false; + node.shorthand = false; + this.expect(tt.colon); + node.value = this.parsePrimitiveValue(); + this.finishNode(node, "ObjectProperty"); + attrs.push(node); + } while (this.eat(tt.comma)); + return attrs; } maybeParseDefaultImportSpecifier(node: N.ImportDeclaration): boolean { diff --git a/packages/babel-parser/test/fixtures/comments/interpreter-directive/interpreter-directive-import/output.json b/packages/babel-parser/test/fixtures/comments/interpreter-directive/interpreter-directive-import/output.json index bd3e1b897a8c..722c14c6842e 100644 --- a/packages/babel-parser/test/fixtures/comments/interpreter-directive/interpreter-directive-import/output.json +++ b/packages/babel-parser/test/fixtures/comments/interpreter-directive/interpreter-directive-import/output.json @@ -38,8 +38,7 @@ "raw": "'foobar'" }, "value": "foobar" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/comments/regression/10892/output.json b/packages/babel-parser/test/fixtures/comments/regression/10892/output.json index f26703dfb3d3..efa9a47df667 100644 --- a/packages/babel-parser/test/fixtures/comments/regression/10892/output.json +++ b/packages/babel-parser/test/fixtures/comments/regression/10892/output.json @@ -35,7 +35,6 @@ }, "value": "bar" }, - "attributes": [], "trailingComments": [ { "type": "CommentBlock", diff --git a/packages/babel-parser/test/fixtures/core/sourcetype-unambiguous/flow/output.json b/packages/babel-parser/test/fixtures/core/sourcetype-unambiguous/flow/output.json index 1f0e6d691536..69c62635c89e 100644 --- a/packages/babel-parser/test/fixtures/core/sourcetype-unambiguous/flow/output.json +++ b/packages/babel-parser/test/fixtures/core/sourcetype-unambiguous/flow/output.json @@ -36,8 +36,7 @@ "raw": "\"bar\"" }, "value": "bar" - }, - "attributes": [] + } }, { "type": "ExportNamedDeclaration", diff --git a/packages/babel-parser/test/fixtures/core/sourcetype-unambiguous/module-import/output.json b/packages/babel-parser/test/fixtures/core/sourcetype-unambiguous/module-import/output.json index fd54d2d84204..95736fdee6c1 100644 --- a/packages/babel-parser/test/fixtures/core/sourcetype-unambiguous/module-import/output.json +++ b/packages/babel-parser/test/fixtures/core/sourcetype-unambiguous/module-import/output.json @@ -19,8 +19,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/es2015/identifiers/invalid-escape-seq-import/output.json b/packages/babel-parser/test/fixtures/es2015/identifiers/invalid-escape-seq-import/output.json index e7e8fe7b678d..0533a2a4de2a 100644 --- a/packages/babel-parser/test/fixtures/es2015/identifiers/invalid-escape-seq-import/output.json +++ b/packages/babel-parser/test/fixtures/es2015/identifiers/invalid-escape-seq-import/output.json @@ -49,8 +49,7 @@ "raw": "\"x\"" }, "value": "x" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/es2015/modules/import-declaration-trailing-comma/output.json b/packages/babel-parser/test/fixtures/es2015/modules/import-declaration-trailing-comma/output.json index 6830520178c8..a968e0267d23 100644 --- a/packages/babel-parser/test/fixtures/es2015/modules/import-declaration-trailing-comma/output.json +++ b/packages/babel-parser/test/fixtures/es2015/modules/import-declaration-trailing-comma/output.json @@ -58,8 +58,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword-flow/output.json b/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword-flow/output.json index 15d369aaaf56..ab2ca9601162 100644 --- a/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword-flow/output.json +++ b/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword-flow/output.json @@ -39,8 +39,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword-typeof-flow/output.json b/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword-typeof-flow/output.json index ef8aa95aef9a..d4df45baefea 100644 --- a/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword-typeof-flow/output.json +++ b/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword-typeof-flow/output.json @@ -39,8 +39,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword-typeof/output.json b/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword-typeof/output.json index 72c00d3b2a9a..95070d9dab76 100644 --- a/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword-typeof/output.json +++ b/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword-typeof/output.json @@ -37,8 +37,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword/output.json b/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword/output.json index fe0c6038a3ef..a627dce5e719 100644 --- a/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword/output.json +++ b/packages/babel-parser/test/fixtures/es2015/modules/import-invalid-keyword/output.json @@ -37,8 +37,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/es2015/uncategorised/291/output.json b/packages/babel-parser/test/fixtures/es2015/uncategorised/291/output.json index 07e64148911e..c87a88a757b7 100644 --- a/packages/babel-parser/test/fixtures/es2015/uncategorised/291/output.json +++ b/packages/babel-parser/test/fixtures/es2015/uncategorised/291/output.json @@ -35,8 +35,7 @@ "raw": "\"acorn\"" }, "value": "acorn" - }, - "attributes": [] + } }, "alternate": null } diff --git a/packages/babel-parser/test/fixtures/es2015/uncategorised/301/output.json b/packages/babel-parser/test/fixtures/es2015/uncategorised/301/output.json index 4c1fe101ce9f..fc986dc29395 100644 --- a/packages/babel-parser/test/fixtures/es2015/uncategorised/301/output.json +++ b/packages/babel-parser/test/fixtures/es2015/uncategorised/301/output.json @@ -38,8 +38,7 @@ "raw": "'baz'" }, "value": "baz" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/es2015/uncategorised/88/output.json b/packages/babel-parser/test/fixtures/es2015/uncategorised/88/output.json index a02838cd1f5f..bbc9bf9f48c8 100644 --- a/packages/babel-parser/test/fixtures/es2015/uncategorised/88/output.json +++ b/packages/babel-parser/test/fixtures/es2015/uncategorised/88/output.json @@ -29,8 +29,7 @@ "raw": "\"\"" }, "value": "" - }, - "attributes": [] + } }, { "type": "ExportNamedDeclaration", diff --git a/packages/babel-parser/test/fixtures/es2015/uncategorised/91/output.json b/packages/babel-parser/test/fixtures/es2015/uncategorised/91/output.json index c32ed6c01721..7a55897d48e9 100644 --- a/packages/babel-parser/test/fixtures/es2015/uncategorised/91/output.json +++ b/packages/babel-parser/test/fixtures/es2015/uncategorised/91/output.json @@ -19,8 +19,7 @@ "raw": "\"jquery\"" }, "value": "jquery" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/es2015/uncategorised/92/output.json b/packages/babel-parser/test/fixtures/es2015/uncategorised/92/output.json index 6a457940f682..3e36b1ca7765 100644 --- a/packages/babel-parser/test/fixtures/es2015/uncategorised/92/output.json +++ b/packages/babel-parser/test/fixtures/es2015/uncategorised/92/output.json @@ -29,8 +29,7 @@ "raw": "\"jquery\"" }, "value": "jquery" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/es2015/uncategorised/93/output.json b/packages/babel-parser/test/fixtures/es2015/uncategorised/93/output.json index 52526286a8b4..6f8d6a652542 100644 --- a/packages/babel-parser/test/fixtures/es2015/uncategorised/93/output.json +++ b/packages/babel-parser/test/fixtures/es2015/uncategorised/93/output.json @@ -48,8 +48,7 @@ "raw": "\"crypto\"" }, "value": "crypto" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/es2015/uncategorised/94/output.json b/packages/babel-parser/test/fixtures/es2015/uncategorised/94/output.json index 95777822f184..e36df8cbfd12 100644 --- a/packages/babel-parser/test/fixtures/es2015/uncategorised/94/output.json +++ b/packages/babel-parser/test/fixtures/es2015/uncategorised/94/output.json @@ -34,8 +34,7 @@ "raw": "\"crypto\"" }, "value": "crypto" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/es2015/uncategorised/95/output.json b/packages/babel-parser/test/fixtures/es2015/uncategorised/95/output.json index 4165c72b6896..a391944e5ad3 100644 --- a/packages/babel-parser/test/fixtures/es2015/uncategorised/95/output.json +++ b/packages/babel-parser/test/fixtures/es2015/uncategorised/95/output.json @@ -57,8 +57,7 @@ "raw": "\"crypto\"" }, "value": "crypto" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/es2015/uncategorised/97/output.json b/packages/babel-parser/test/fixtures/es2015/uncategorised/97/output.json index f999c9c20ca7..0c902bf05c85 100644 --- a/packages/babel-parser/test/fixtures/es2015/uncategorised/97/output.json +++ b/packages/babel-parser/test/fixtures/es2015/uncategorised/97/output.json @@ -34,8 +34,7 @@ "raw": "\"bar\"" }, "value": "bar" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/es2015/uncategorised/98/output.json b/packages/babel-parser/test/fixtures/es2015/uncategorised/98/output.json index 19a191f41b7e..628f749c47c5 100644 --- a/packages/babel-parser/test/fixtures/es2015/uncategorised/98/output.json +++ b/packages/babel-parser/test/fixtures/es2015/uncategorised/98/output.json @@ -29,8 +29,7 @@ "raw": "\"crypto\"" }, "value": "crypto" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default-and-named-specifiers/output.json b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default-and-named-specifiers/output.json index 2e62e45d0e31..05f4b187ee88 100644 --- a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default-and-named-specifiers/output.json +++ b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default-and-named-specifiers/output.json @@ -43,8 +43,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default-and-namespace-specifiers/output.json b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default-and-namespace-specifiers/output.json index 4b1f4f243cc3..9985ba0bfaed 100644 --- a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default-and-namespace-specifiers/output.json +++ b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default-and-namespace-specifiers/output.json @@ -38,8 +38,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default-as/output.json b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default-as/output.json index 29ee5f91c3c7..b14f5cdb95e0 100644 --- a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default-as/output.json +++ b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default-as/output.json @@ -34,8 +34,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default/output.json b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default/output.json index 2c5a920fb1fd..22597cae7cae 100644 --- a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default/output.json +++ b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-default/output.json @@ -29,8 +29,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-jquery/output.json b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-jquery/output.json index 6a457940f682..3e36b1ca7765 100644 --- a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-jquery/output.json +++ b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-jquery/output.json @@ -29,8 +29,7 @@ "raw": "\"jquery\"" }, "value": "jquery" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-module/output.json b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-module/output.json index fd54d2d84204..95736fdee6c1 100644 --- a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-module/output.json +++ b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-module/output.json @@ -19,8 +19,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-as-specifier/output.json b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-as-specifier/output.json index ef10f1a837dd..cf9dd503a4c4 100644 --- a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-as-specifier/output.json +++ b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-as-specifier/output.json @@ -34,8 +34,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-as-specifiers/output.json b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-as-specifiers/output.json index e3e3e6069c6f..6bcc40f19ba1 100644 --- a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-as-specifiers/output.json +++ b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-as-specifiers/output.json @@ -48,8 +48,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-empty/output.json b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-empty/output.json index 1a195b03046d..b5f711497e31 100644 --- a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-empty/output.json +++ b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-empty/output.json @@ -19,8 +19,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-specifier/output.json b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-specifier/output.json index 4becc99d3eb2..a54a3104a79a 100644 --- a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-specifier/output.json +++ b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-specifier/output.json @@ -34,8 +34,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-specifiers-comma/output.json b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-specifiers-comma/output.json index 39047aa5cb0f..388349480ddf 100644 --- a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-specifiers-comma/output.json +++ b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-specifiers-comma/output.json @@ -48,8 +48,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-specifiers/output.json b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-specifiers/output.json index 96197dd33c42..88915ea0223a 100644 --- a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-specifiers/output.json +++ b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-named-specifiers/output.json @@ -48,8 +48,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-namespace-specifier/output.json b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-namespace-specifier/output.json index 83eeba133356..3d082bcf7a65 100644 --- a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-namespace-specifier/output.json +++ b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-namespace-specifier/output.json @@ -29,8 +29,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-null-as-nil/output.json b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-null-as-nil/output.json index f999c9c20ca7..0c902bf05c85 100644 --- a/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-null-as-nil/output.json +++ b/packages/babel-parser/test/fixtures/esprima/es2015-import-declaration/import-null-as-nil/output.json @@ -34,8 +34,7 @@ "raw": "\"bar\"" }, "value": "bar" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-module-attributes/options.json b/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-module-attributes/options.json index 3f9b696c8de9..9acd1c25f418 100644 --- a/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-module-attributes/options.json +++ b/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-module-attributes/options.json @@ -1,5 +1,5 @@ { - "throws": "This experimental syntax requires enabling the parser plugin: 'moduleAttributes' (1:27)", + "throws": "This experimental syntax requires enabling the parser plugin: 'moduleAttributes' (1:32)", "sourceType": "module", "plugins": [] } \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes/output.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes/output.json index 490e4ee43d32..ca14238db509 100644 --- a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes/output.json +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes/output.json @@ -97,78 +97,63 @@ }, "value": "foo.json" }, - "attributes": { - "type": "ObjectExpression", - "start": 32, - "end": 44, - "loc": { - "start": { - "line": 1, - "column": 32 + "attributes": [ + { + "type": "ObjectProperty", + "start": 32, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 32 + }, + "end": { + "line": 1, + "column": 44 + } }, - "end": { - "line": 1, - "column": 44 - } - }, - "properties": [ - { - "type": "ObjectProperty", + "key": { + "type": "Identifier", "start": 32, - "end": 44, + "end": 36, "loc": { "start": { "line": 1, "column": 32 }, + "end": { + "line": 1, + "column": 36 + }, + "identifierName": "type" + }, + "name": "type" + }, + "computed": false, + "method": false, + "shorthand": false, + "value": { + "type": "StringLiteral", + "start": 38, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 38 + }, "end": { "line": 1, "column": 44 } }, - "method": false, - "key": { - "type": "Identifier", - "start": 32, - "end": 36, - "loc": { - "start": { - "line": 1, - "column": 32 - }, - "end": { - "line": 1, - "column": 36 - }, - "identifierName": "type" - }, - "name": "type" + "extra": { + "rawValue": "json", + "raw": "\"json\"" }, - "computed": false, - "shorthand": false, - "value": { - "type": "StringLiteral", - "start": 38, - "end": 44, - "loc": { - "start": { - "line": 1, - "column": 38 - }, - "end": { - "line": 1, - "column": 44 - } - }, - "extra": { - "rawValue": "json", - "raw": "\"json\"" - }, - "value": "json" - } + "value": "json" } - ] - } + } + ] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/flow/declare-module/import/output.json b/packages/babel-parser/test/fixtures/flow/declare-module/import/output.json index 1427fa357570..f559a7704e8c 100644 --- a/packages/babel-parser/test/fixtures/flow/declare-module/import/output.json +++ b/packages/babel-parser/test/fixtures/flow/declare-module/import/output.json @@ -46,8 +46,7 @@ "raw": "\"TM\"" }, "value": "TM" - }, - "attributes": [] + } }, { "type": "ImportDeclaration", @@ -72,8 +71,7 @@ "raw": "\"UM\"" }, "value": "UM" - }, - "attributes": [] + } } ] }, diff --git a/packages/babel-parser/test/fixtures/flow/declare-module/invalid-import/output.json b/packages/babel-parser/test/fixtures/flow/declare-module/invalid-import/output.json index 6c53f46014a2..6155f8eda4f7 100644 --- a/packages/babel-parser/test/fixtures/flow/declare-module/invalid-import/output.json +++ b/packages/babel-parser/test/fixtures/flow/declare-module/invalid-import/output.json @@ -49,8 +49,7 @@ "raw": "\"TM\"" }, "value": "TM" - }, - "attributes": [] + } } ] }, diff --git a/packages/babel-parser/test/fixtures/flow/sourcetype-script/import/output.json b/packages/babel-parser/test/fixtures/flow/sourcetype-script/import/output.json index 96e56935c53f..54d736943f7f 100644 --- a/packages/babel-parser/test/fixtures/flow/sourcetype-script/import/output.json +++ b/packages/babel-parser/test/fixtures/flow/sourcetype-script/import/output.json @@ -36,8 +36,7 @@ "raw": "\"\"" }, "value": "" - }, - "attributes": [] + } }, { "type": "ImportDeclaration", @@ -62,8 +61,7 @@ "raw": "\"\"" }, "value": "" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/flow/type-imports/import-type-2/output.json b/packages/babel-parser/test/fixtures/flow/type-imports/import-type-2/output.json index a60601d0601f..2406bba2f4f2 100644 --- a/packages/babel-parser/test/fixtures/flow/type-imports/import-type-2/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-imports/import-type-2/output.json @@ -45,8 +45,7 @@ "raw": "\"bar\"" }, "value": "bar" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/flow/type-imports/import-type-shorthand/output.json b/packages/babel-parser/test/fixtures/flow/type-imports/import-type-shorthand/output.json index 7c7fa9bcd76f..437eac470802 100644 --- a/packages/babel-parser/test/fixtures/flow/type-imports/import-type-shorthand/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-imports/import-type-shorthand/output.json @@ -36,8 +36,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } }, { "type": "ImportDeclaration", @@ -68,8 +67,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } }, { "type": "ImportDeclaration", @@ -100,8 +98,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } }, { "type": "ImportDeclaration", @@ -132,8 +129,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } }, { "type": "ImportDeclaration", @@ -164,8 +160,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } }, { "type": "ImportDeclaration", @@ -196,8 +191,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } }, { "type": "ImportDeclaration", @@ -228,8 +222,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } }, { "type": "ImportDeclaration", @@ -260,8 +253,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } }, { "type": "ImportDeclaration", @@ -292,8 +284,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } }, { "type": "ImportDeclaration", @@ -324,8 +315,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/flow/type-imports/import-type/output.json b/packages/babel-parser/test/fixtures/flow/type-imports/import-type/output.json index cd336d76f4aa..fcc0c31ed6dd 100644 --- a/packages/babel-parser/test/fixtures/flow/type-imports/import-type/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-imports/import-type/output.json @@ -30,8 +30,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } }, { "type": "ImportDeclaration", @@ -62,8 +61,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } }, { "type": "ImportDeclaration", @@ -103,8 +101,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } }, { "type": "ImportDeclaration", @@ -129,8 +126,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } }, { "type": "ImportDeclaration", @@ -161,8 +157,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } }, { "type": "ImportDeclaration", @@ -208,8 +203,7 @@ "raw": "\"baz\"" }, "value": "baz" - }, - "attributes": [] + } }, { "type": "ImportDeclaration", @@ -234,8 +228,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } }, { "type": "ImportDeclaration", @@ -260,8 +253,7 @@ "raw": "\"bar\"" }, "value": "bar" - }, - "attributes": [] + } }, { "type": "ImportDeclaration", @@ -286,8 +278,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } }, { "type": "ImportDeclaration", @@ -318,8 +309,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } }, { "type": "ImportDeclaration", @@ -350,8 +340,7 @@ "raw": "\"baz\"" }, "value": "baz" - }, - "attributes": [] + } }, { "type": "ImportDeclaration", @@ -376,8 +365,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } }, { "type": "ImportDeclaration", @@ -402,8 +390,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-2/output.json b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-2/output.json index 0b47f59fb3b0..cbd99a19951e 100644 --- a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-2/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-2/output.json @@ -39,8 +39,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-3/output.json b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-3/output.json index aa69ad7cf042..1e241ce54643 100644 --- a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-3/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-3/output.json @@ -33,8 +33,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-4/output.json b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-4/output.json index 564aa10525db..4598f3355fcf 100644 --- a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-4/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-4/output.json @@ -33,8 +33,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-as/output.json b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-as/output.json index 4afb6bf0172e..20c6c607313f 100644 --- a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-as/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-as/output.json @@ -39,8 +39,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand-2/output.json b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand-2/output.json index 68ba6fe9cef4..37af63c0996a 100644 --- a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand-2/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand-2/output.json @@ -39,8 +39,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand-3/output.json b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand-3/output.json index 59a0f2b81932..56fba75c81f5 100644 --- a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand-3/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand-3/output.json @@ -39,8 +39,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand-4/output.json b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand-4/output.json index 5986e389bcec..ff46538a95f6 100644 --- a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand-4/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand-4/output.json @@ -39,8 +39,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand/output.json b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand/output.json index ade8b4c6fdb7..8d2dd58fa474 100644 --- a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type-shorthand/output.json @@ -39,8 +39,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type/output.json b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type/output.json index 924ead3fa2b9..78cb69bb8dea 100644 --- a/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-imports/invalid-import-type/output.json @@ -33,8 +33,7 @@ "raw": "\"foo\"" }, "value": "foo" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/placeholders/import/default-named/output.json b/packages/babel-parser/test/fixtures/placeholders/import/default-named/output.json index ac8bd5d4dff1..d46d85c82643 100644 --- a/packages/babel-parser/test/fixtures/placeholders/import/default-named/output.json +++ b/packages/babel-parser/test/fixtures/placeholders/import/default-named/output.json @@ -53,8 +53,7 @@ "raw": "\"file\"" }, "value": "file" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/placeholders/import/default-star-2/output.json b/packages/babel-parser/test/fixtures/placeholders/import/default-star-2/output.json index b296a9dccf62..1fb751ada336 100644 --- a/packages/babel-parser/test/fixtures/placeholders/import/default-star-2/output.json +++ b/packages/babel-parser/test/fixtures/placeholders/import/default-star-2/output.json @@ -43,8 +43,7 @@ "raw": "\"file\"" }, "value": "file" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/placeholders/import/file-empty/output.json b/packages/babel-parser/test/fixtures/placeholders/import/file-empty/output.json index 977725e1ed59..2b7e6735068e 100644 --- a/packages/babel-parser/test/fixtures/placeholders/import/file-empty/output.json +++ b/packages/babel-parser/test/fixtures/placeholders/import/file-empty/output.json @@ -20,8 +20,7 @@ "name": "FILE" }, "expectedNode": "StringLiteral" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/placeholders/import/named-alias-2/output.json b/packages/babel-parser/test/fixtures/placeholders/import/named-alias-2/output.json index c385d4de9e76..b52ba4d5dd43 100644 --- a/packages/babel-parser/test/fixtures/placeholders/import/named-alias-2/output.json +++ b/packages/babel-parser/test/fixtures/placeholders/import/named-alias-2/output.json @@ -39,8 +39,7 @@ "raw": "\"file\"" }, "value": "file" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/placeholders/import/named-alias-3/output.json b/packages/babel-parser/test/fixtures/placeholders/import/named-alias-3/output.json index 20927b85e6f0..fe0f44bed277 100644 --- a/packages/babel-parser/test/fixtures/placeholders/import/named-alias-3/output.json +++ b/packages/babel-parser/test/fixtures/placeholders/import/named-alias-3/output.json @@ -44,8 +44,7 @@ "raw": "\"file\"" }, "value": "file" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/placeholders/import/named-alias/output.json b/packages/babel-parser/test/fixtures/placeholders/import/named-alias/output.json index 171e144df48c..aa6ab6e293a6 100644 --- a/packages/babel-parser/test/fixtures/placeholders/import/named-alias/output.json +++ b/packages/babel-parser/test/fixtures/placeholders/import/named-alias/output.json @@ -39,8 +39,7 @@ "raw": "\"file\"" }, "value": "file" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/placeholders/import/named/output.json b/packages/babel-parser/test/fixtures/placeholders/import/named/output.json index bc246007adfd..1508f63de6f9 100644 --- a/packages/babel-parser/test/fixtures/placeholders/import/named/output.json +++ b/packages/babel-parser/test/fixtures/placeholders/import/named/output.json @@ -44,8 +44,7 @@ "raw": "\"file\"" }, "value": "file" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/placeholders/import/star/output.json b/packages/babel-parser/test/fixtures/placeholders/import/star/output.json index 54cd48d51621..2d8f5acbca2c 100644 --- a/packages/babel-parser/test/fixtures/placeholders/import/star/output.json +++ b/packages/babel-parser/test/fixtures/placeholders/import/star/output.json @@ -34,8 +34,7 @@ "raw": "\"file\"" }, "value": "file" - }, - "attributes": [] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/typescript/import/not-top-level/output.json b/packages/babel-parser/test/fixtures/typescript/import/not-top-level/output.json index 5484ad7003cd..dd3c9e4ddda7 100644 --- a/packages/babel-parser/test/fixtures/typescript/import/not-top-level/output.json +++ b/packages/babel-parser/test/fixtures/typescript/import/not-top-level/output.json @@ -46,8 +46,7 @@ "raw": "\"a\"" }, "value": "a" - }, - "attributes": [] + } } ] }, diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-import-ambient-class/output.json b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-import-ambient-class/output.json index ea1420f8d3ce..479d69c1a16c 100644 --- a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-import-ambient-class/output.json +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-import-ambient-class/output.json @@ -30,8 +30,7 @@ "raw": "'./somewhere.js'" }, "value": "./somewhere.js" - }, - "attributes": [] + } }, { "type": "ClassDeclaration", From e392984964bdf854f9114b5649515382752b8edf Mon Sep 17 00:00:00 2001 From: Vivek Nayyar Date: Sat, 25 Jan 2020 19:40:53 +0100 Subject: [PATCH 27/63] :recycle: use new AST Node ImportAttribute for module attributes, tests updated and only allow string literals for attribute values --- .../babel-parser/src/parser/expression.js | 29 --- packages/babel-parser/src/parser/statement.js | 29 ++- .../input.js | 2 + .../options.json | 8 + .../input.js | 2 + .../options.json | 4 + .../output.json | 177 ++++++++++++++++++ .../input.js | 3 + .../options.json | 4 + .../output.json | 125 +++++++++++++ .../valid-syntax-with-attributes/output.json | 5 +- .../valid-syntax-with-invalid-value/input.js | 1 + .../options.json | 7 + .../without-plugin/input.js | 1 + .../without-plugin/options.json | 4 + 15 files changed, 358 insertions(+), 43 deletions(-) create mode 100644 packages/babel-parser/test/fixtures/experimental/import-module-attributes/invalid-syntax-with-no-attributes-identifier/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/import-module-attributes/invalid-syntax-with-no-attributes-identifier/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-and-value/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-and-value/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-and-value/output.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-multiple-lines/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-multiple-lines/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-multiple-lines/output.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-value/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-value/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-module-attributes/without-plugin/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/import-module-attributes/without-plugin/options.json diff --git a/packages/babel-parser/src/parser/expression.js b/packages/babel-parser/src/parser/expression.js index 4db60db16133..31a4bdbee7ed 100644 --- a/packages/babel-parser/src/parser/expression.js +++ b/packages/babel-parser/src/parser/expression.js @@ -869,35 +869,6 @@ export default class ExpressionParser extends LValParser { return this.parseSubscripts(this.parseExprAtom(), startPos, startLoc, true); } - /** - * Parse a primitive value - * primitive values are null, undefined, number, bigint, string, boolean - */ - parsePrimitiveValue(): N.Expression { - let node; - switch (this.state.type) { - case tt.num: - return this.parseLiteral(this.state.value, "NumericLiteral"); - - case tt.bigint: - return this.parseLiteral(this.state.value, "BigIntLiteral"); - - case tt.string: - return this.parseLiteral(this.state.value, "StringLiteral"); - - case tt._null: - node = this.startNode(); - this.next(); - return this.finishNode(node, "NullLiteral"); - - case tt._true: - case tt._false: - return this.parseBooleanLiteral(); - default: - throw this.unexpected(); - } - } - // Parse an atomic expression — either a single token that is an // expression, an expression started by a keyword like `function` or // `new`, or an expression wrapped in punctuation like `()`, `[]`, diff --git a/packages/babel-parser/src/parser/statement.js b/packages/babel-parser/src/parser/statement.js index ebab219644df..7dbca6dd3ac4 100644 --- a/packages/babel-parser/src/parser/statement.js +++ b/packages/babel-parser/src/parser/statement.js @@ -2048,20 +2048,23 @@ export default class StatementParser extends ExpressionParser { * import { get } from "axios"; * but if we do have a default import * we need to check if we have a comma after that and - * that is where this || this.eat comes into play + * that is where this `|| this.eat` condition comes into play */ const parseNext = !hasDefault || this.eat(tt.comma); - // if we do have to parse the next set of specifiers, we first check for start imports + // if we do have to parse the next set of specifiers, we first check for star imports // import React, * from "react"; const hasStar = parseNext && this.maybeParseStarImportSpecifier(node); - // now we check that if we need to parse the next imports - // but only if he/she is not importing * which means all + // now we check if we need to parse the next imports + // but only if they are not importing * (everything) if (parseNext && !hasStar) this.parseNamedImportSpecifiers(node); this.expectContextual("from"); } node.source = this.parseImportSource(); + // new proposal + // https://github.com/tc39/proposal-module-attributes + // parse module attributes if the next token is with or ignore and finish the ImportDeclaration node. const attributes = this.maybeParseModuleAttributes(); - if (Array.isArray(attributes)) { + if (attributes) { node.attributes = attributes; } this.semicolon(); @@ -2102,14 +2105,20 @@ export default class StatementParser extends ExpressionParser { this.expectPlugin("moduleAttributes"); const attrs = []; do { + // we are trying to parse a node which has the following syntax + // with type: "json" + // [with -> keyword], [type -> Identifier], [":" -> token for colon], ["json" -> StringLiteral] const node = this.startNode(); node.key = this.parseIdentifier(); - node.computed = false; - node.method = false; - node.shorthand = false; this.expect(tt.colon); - node.value = this.parsePrimitiveValue(); - this.finishNode(node, "ObjectProperty"); + if (!this.match(tt.string)) { + return this.unexpected( + this.state.start, + "Only string literal values are allowed", + ); + } + node.value = this.parseLiteral(this.state.value, "StringLiteral"); + this.finishNode(node, "ImportAttribute"); attrs.push(node); } while (this.eat(tt.comma)); return attrs; diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/invalid-syntax-with-no-attributes-identifier/input.js b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/invalid-syntax-with-no-attributes-identifier/input.js new file mode 100644 index 000000000000..bfa86f1acde6 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/invalid-syntax-with-no-attributes-identifier/input.js @@ -0,0 +1,2 @@ +import "x" +with ({}); diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/invalid-syntax-with-no-attributes-identifier/options.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/invalid-syntax-with-no-attributes-identifier/options.json new file mode 100644 index 000000000000..77e11f22643a --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/invalid-syntax-with-no-attributes-identifier/options.json @@ -0,0 +1,8 @@ +{ + "plugins": [ + "moduleAttributes", + "estree" + ], + "sourceType": "module", + "throws": "Unexpected token (2:5)" +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-and-value/input.js b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-and-value/input.js new file mode 100644 index 000000000000..ec62816617cd --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-and-value/input.js @@ -0,0 +1,2 @@ +import "x" with type: "json" +[0] diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-and-value/options.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-and-value/options.json new file mode 100644 index 000000000000..9399e0bedd3f --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-and-value/options.json @@ -0,0 +1,4 @@ +{ + "plugins": ["moduleAttributes"], + "sourceType": "module" +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-and-value/output.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-and-value/output.json new file mode 100644 index 000000000000..b8c8d5f9cff4 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-and-value/output.json @@ -0,0 +1,177 @@ +{ + "type": "File", + "start": 0, + "end": 32, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 3 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 32, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 3 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start": 0, + "end": 28, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 28 + } + }, + "specifiers": [], + "source": { + "type": "StringLiteral", + "start": 7, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "extra": { + "rawValue": "x", + "raw": "\"x\"" + }, + "value": "x" + }, + "attributes": [ + { + "type": "ImportAttribute", + "start": 16, + "end": 28, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 28 + } + }, + "key": { + "type": "Identifier", + "start": 16, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 20 + }, + "identifierName": "type" + }, + "name": "type" + }, + "value": { + "type": "StringLiteral", + "start": 22, + "end": 28, + "loc": { + "start": { + "line": 1, + "column": 22 + }, + "end": { + "line": 1, + "column": 28 + } + }, + "extra": { + "rawValue": "json", + "raw": "\"json\"" + }, + "value": "json" + } + } + ] + }, + { + "type": "ExpressionStatement", + "start": 29, + "end": 32, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 3 + } + }, + "expression": { + "type": "ArrayExpression", + "start": 29, + "end": 32, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 3 + } + }, + "elements": [ + { + "type": "NumericLiteral", + "start": 30, + "end": 31, + "loc": { + "start": { + "line": 2, + "column": 1 + }, + "end": { + "line": 2, + "column": 2 + } + }, + "extra": { + "rawValue": 0, + "raw": "0" + }, + "value": 0 + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-multiple-lines/input.js b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-multiple-lines/input.js new file mode 100644 index 000000000000..506d38a7ee01 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-multiple-lines/input.js @@ -0,0 +1,3 @@ +import "x" +with +type: "json" diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-multiple-lines/options.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-multiple-lines/options.json new file mode 100644 index 000000000000..9399e0bedd3f --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-multiple-lines/options.json @@ -0,0 +1,4 @@ +{ + "plugins": ["moduleAttributes"], + "sourceType": "module" +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-multiple-lines/output.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-multiple-lines/output.json new file mode 100644 index 000000000000..7ab9b34a1162 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-multiple-lines/output.json @@ -0,0 +1,125 @@ +{ + "type": "File", + "start": 0, + "end": 28, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 12 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 28, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 12 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start": 0, + "end": 28, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 12 + } + }, + "specifiers": [], + "source": { + "type": "StringLiteral", + "start": 7, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "extra": { + "rawValue": "x", + "raw": "\"x\"" + }, + "value": "x" + }, + "attributes": [ + { + "type": "ImportAttribute", + "start": 16, + "end": 28, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 12 + } + }, + "key": { + "type": "Identifier", + "start": 16, + "end": 20, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 4 + }, + "identifierName": "type" + }, + "name": "type" + }, + "value": { + "type": "StringLiteral", + "start": 22, + "end": 28, + "loc": { + "start": { + "line": 3, + "column": 6 + }, + "end": { + "line": 3, + "column": 12 + } + }, + "extra": { + "rawValue": "json", + "raw": "\"json\"" + }, + "value": "json" + } + } + ] + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes/output.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes/output.json index ca14238db509..d2db79b27ddb 100644 --- a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes/output.json +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes/output.json @@ -99,7 +99,7 @@ }, "attributes": [ { - "type": "ObjectProperty", + "type": "ImportAttribute", "start": 32, "end": 44, "loc": { @@ -129,9 +129,6 @@ }, "name": "type" }, - "computed": false, - "method": false, - "shorthand": false, "value": { "type": "StringLiteral", "start": 38, diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-value/input.js b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-value/input.js new file mode 100644 index 000000000000..cfb5372f6b12 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-value/input.js @@ -0,0 +1 @@ +import foo from "foo.json" with type: "json", lazy: true, startAtLine: 1; diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-value/options.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-value/options.json new file mode 100644 index 000000000000..b4911175a067 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-value/options.json @@ -0,0 +1,7 @@ +{ + "plugins": [ + "moduleAttributes" + ], + "sourceType": "module", + "throws": "Only string literal values are allowed (1:52)" +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/without-plugin/input.js b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/without-plugin/input.js new file mode 100644 index 000000000000..fb31508a0860 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/without-plugin/input.js @@ -0,0 +1 @@ +import foo from "foo.json" with type: "json"; diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/without-plugin/options.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/without-plugin/options.json new file mode 100644 index 000000000000..d5829ef69927 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/without-plugin/options.json @@ -0,0 +1,4 @@ +{ + "throws": "This experimental syntax requires enabling the parser plugin: 'moduleAttributes' (1:32)", + "plugins": [] +} \ No newline at end of file From 256510bafe9ef4c5dad8349bc517d5a733af48cc Mon Sep 17 00:00:00 2001 From: Vivek Nayyar Date: Wed, 11 Mar 2020 23:23:50 +0100 Subject: [PATCH 28/63] chore: fixed the eslint issue and sorted the errors in ascending order --- packages/babel-parser/src/parser/location.js | 3 +++ packages/babel-parser/src/parser/statement.js | 24 +++++++++++++++++-- .../input.js | 1 + .../options.json | 7 ++++++ .../valid-syntax-with-repeated-type/input.js | 1 + .../options.json | 7 ++++++ 6 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-no-type-attribute/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-no-type-attribute/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-repeated-type/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-repeated-type/options.json diff --git a/packages/babel-parser/src/parser/location.js b/packages/babel-parser/src/parser/location.js index 035bbc7d0452..73ed02ec2d90 100644 --- a/packages/babel-parser/src/parser/location.js +++ b/packages/babel-parser/src/parser/location.js @@ -97,6 +97,9 @@ export const Errors = Object.freeze({ MissingUnicodeEscape: "Expecting Unicode escape sequence \\uXXXX", MixingCoalesceWithLogical: "Nullish coalescing operator(??) requires parens when mixing with logical operators", + ModuleAttributesWithRepeatedType: + "Module attributes should include only one type", + ModuleAttributesWithoutType: "Module attributes should include a type", ModuleExportUndefined: "Export '%0' is not defined", MultipleDefaultsInSwitch: "Multiple default clauses", NewlineAfterThrow: "Illegal newline after throw", diff --git a/packages/babel-parser/src/parser/statement.js b/packages/babel-parser/src/parser/statement.js index 7dbca6dd3ac4..7e0414d538bb 100644 --- a/packages/babel-parser/src/parser/statement.js +++ b/packages/babel-parser/src/parser/statement.js @@ -2062,7 +2062,7 @@ export default class StatementParser extends ExpressionParser { node.source = this.parseImportSource(); // new proposal // https://github.com/tc39/proposal-module-attributes - // parse module attributes if the next token is with or ignore and finish the ImportDeclaration node. + // parse module attributes if the next token is `with` or ignore and finish the ImportDeclaration node. const attributes = this.maybeParseModuleAttributes(); if (attributes) { node.attributes = attributes; @@ -2104,13 +2104,28 @@ export default class StatementParser extends ExpressionParser { } this.expectPlugin("moduleAttributes"); const attrs = []; + let hasTypeAttribute = false; do { // we are trying to parse a node which has the following syntax // with type: "json" // [with -> keyword], [type -> Identifier], [":" -> token for colon], ["json" -> StringLiteral] const node = this.startNode(); - node.key = this.parseIdentifier(); + node.key = this.parseIdentifier(true); + + // check if we have a type attribute + if (node.key.name === "type") { + // check if we do not have two duplicate type attributes defined + if (hasTypeAttribute) { + throw this.raise( + this.state.start, + Errors.ModuleAttributesWithRepeatedType, + ); + } + hasTypeAttribute = true; + } + // check for colon this.expect(tt.colon); + // check if the value set to the module attribute is a string as we only allow string literals if (!this.match(tt.string)) { return this.unexpected( this.state.start, @@ -2121,6 +2136,11 @@ export default class StatementParser extends ExpressionParser { this.finishNode(node, "ImportAttribute"); attrs.push(node); } while (this.eat(tt.comma)); + + // check if module attributes do not contain any type key and throw an error + if (!hasTypeAttribute) { + throw this.raise(this.state.start, Errors.ModuleAttributesWithoutType); + } return attrs; } diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-no-type-attribute/input.js b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-no-type-attribute/input.js new file mode 100644 index 000000000000..c2ae7496dcb7 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-no-type-attribute/input.js @@ -0,0 +1 @@ +import foo from "foo.json" with lazy: "true"; diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-no-type-attribute/options.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-no-type-attribute/options.json new file mode 100644 index 000000000000..1e4da5a2f09b --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-no-type-attribute/options.json @@ -0,0 +1,7 @@ +{ + "plugins": [ + "moduleAttributes" + ], + "sourceType": "module", + "throws": "Module attributes should include a type (1:44)" +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-repeated-type/input.js b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-repeated-type/input.js new file mode 100644 index 000000000000..f742d7cc15d1 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-repeated-type/input.js @@ -0,0 +1 @@ +import foo from "foo.json" with type: "json", lazy: "true", type: "html"; diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-repeated-type/options.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-repeated-type/options.json new file mode 100644 index 000000000000..44fae505d71c --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-repeated-type/options.json @@ -0,0 +1,7 @@ +{ + "plugins": [ + "moduleAttributes" + ], + "sourceType": "module", + "throws": "Module attributes should include only one type (1:64)" +} \ No newline at end of file From 1ee26a7521627e5ebd008ac8c7ca9cba1e8fb3a6 Mon Sep 17 00:00:00 2001 From: Vivek Nayyar Date: Wed, 11 Mar 2020 23:53:12 +0100 Subject: [PATCH 29/63] improvement: added support to throw error if duplicate attributes found --- packages/babel-parser/src/parser/location.js | 4 ++-- packages/babel-parser/src/parser/statement.js | 23 +++++++++---------- .../options.json | 2 +- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/packages/babel-parser/src/parser/location.js b/packages/babel-parser/src/parser/location.js index 73ed02ec2d90..8124f4d506cd 100644 --- a/packages/babel-parser/src/parser/location.js +++ b/packages/babel-parser/src/parser/location.js @@ -97,8 +97,8 @@ export const Errors = Object.freeze({ MissingUnicodeEscape: "Expecting Unicode escape sequence \\uXXXX", MixingCoalesceWithLogical: "Nullish coalescing operator(??) requires parens when mixing with logical operators", - ModuleAttributesWithRepeatedType: - "Module attributes should include only one type", + ModuleAttributesWithDuplicateKeys: + "Module attributes should not include duplicate keys", ModuleAttributesWithoutType: "Module attributes should include a type", ModuleExportUndefined: "Export '%0' is not defined", MultipleDefaultsInSwitch: "Multiple default clauses", diff --git a/packages/babel-parser/src/parser/statement.js b/packages/babel-parser/src/parser/statement.js index 7e0414d538bb..073b747e512c 100644 --- a/packages/babel-parser/src/parser/statement.js +++ b/packages/babel-parser/src/parser/statement.js @@ -2104,7 +2104,7 @@ export default class StatementParser extends ExpressionParser { } this.expectPlugin("moduleAttributes"); const attrs = []; - let hasTypeAttribute = false; + const attributeMap = {}; do { // we are trying to parse a node which has the following syntax // with type: "json" @@ -2112,17 +2112,16 @@ export default class StatementParser extends ExpressionParser { const node = this.startNode(); node.key = this.parseIdentifier(true); - // check if we have a type attribute - if (node.key.name === "type") { - // check if we do not have two duplicate type attributes defined - if (hasTypeAttribute) { - throw this.raise( - this.state.start, - Errors.ModuleAttributesWithRepeatedType, - ); - } - hasTypeAttribute = true; + // check if we already have an entry for an attribute + // if a duplicate entry found, throw an error + if (attributeMap[node.key.name]) { + throw this.raise( + this.state.start, + Errors.ModuleAttributesWithDuplicateKeys, + ); } + // set the attribute name entry in the attributeMap to true + attributeMap[node.key.name] = true; // check for colon this.expect(tt.colon); // check if the value set to the module attribute is a string as we only allow string literals @@ -2138,7 +2137,7 @@ export default class StatementParser extends ExpressionParser { } while (this.eat(tt.comma)); // check if module attributes do not contain any type key and throw an error - if (!hasTypeAttribute) { + if (!attributeMap["type"]) { throw this.raise(this.state.start, Errors.ModuleAttributesWithoutType); } return attrs; diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-repeated-type/options.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-repeated-type/options.json index 44fae505d71c..0c6a87381963 100644 --- a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-repeated-type/options.json +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-repeated-type/options.json @@ -3,5 +3,5 @@ "moduleAttributes" ], "sourceType": "module", - "throws": "Module attributes should include only one type (1:64)" + "throws": "Module attributes should not include duplicate keys (1:64)" } \ No newline at end of file From 2a708a50a89e8f1c287ea9a59d3291a312084106 Mon Sep 17 00:00:00 2001 From: Vivek Nayyar Date: Thu, 12 Mar 2020 22:27:43 +0100 Subject: [PATCH 30/63] improvement: fixed error messages, confition to check presence of with rectified and tests updated --- packages/babel-parser/src/parser/location.js | 4 +- packages/babel-parser/src/parser/statement.js | 15 +- .../import-module-attributes/options.json | 2 +- .../options.json | 2 +- .../options.json | 9 +- .../output.json | 265 ++++++++++++++++++ .../without-plugin/options.json | 2 +- 7 files changed, 283 insertions(+), 16 deletions(-) create mode 100644 packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-repeated-type/output.json diff --git a/packages/babel-parser/src/parser/location.js b/packages/babel-parser/src/parser/location.js index 8124f4d506cd..b583ad5ceb78 100644 --- a/packages/babel-parser/src/parser/location.js +++ b/packages/babel-parser/src/parser/location.js @@ -97,8 +97,10 @@ export const Errors = Object.freeze({ MissingUnicodeEscape: "Expecting Unicode escape sequence \\uXXXX", MixingCoalesceWithLogical: "Nullish coalescing operator(??) requires parens when mixing with logical operators", + ModuleAttributesIncorrectValue: + "Only string literals are allowed as module attributes values", ModuleAttributesWithDuplicateKeys: - "Module attributes should not include duplicate keys", + "Module attributes should not include duplicate keys (%0)", ModuleAttributesWithoutType: "Module attributes should include a type", ModuleExportUndefined: "Export '%0' is not defined", MultipleDefaultsInSwitch: "Multiple default clauses", diff --git a/packages/babel-parser/src/parser/statement.js b/packages/babel-parser/src/parser/statement.js index 073b747e512c..ce6860d3a801 100644 --- a/packages/babel-parser/src/parser/statement.js +++ b/packages/babel-parser/src/parser/statement.js @@ -2098,13 +2098,15 @@ export default class StatementParser extends ExpressionParser { } maybeParseModuleAttributes() { - if (!this.eat(tt._with)) { + if (this.match(tt._with)) { + this.expectPlugin("moduleAttributes"); + this.next(); + } else { if (this.hasPlugin("moduleAttributes")) return []; return null; } - this.expectPlugin("moduleAttributes"); const attrs = []; - const attributeMap = {}; + const attributeMap = Object.create(null); do { // we are trying to parse a node which has the following syntax // with type: "json" @@ -2115,9 +2117,10 @@ export default class StatementParser extends ExpressionParser { // check if we already have an entry for an attribute // if a duplicate entry found, throw an error if (attributeMap[node.key.name]) { - throw this.raise( + this.raise( this.state.start, Errors.ModuleAttributesWithDuplicateKeys, + node.key.name, ); } // set the attribute name entry in the attributeMap to true @@ -2126,9 +2129,9 @@ export default class StatementParser extends ExpressionParser { this.expect(tt.colon); // check if the value set to the module attribute is a string as we only allow string literals if (!this.match(tt.string)) { - return this.unexpected( + throw this.unexpected( this.state.start, - "Only string literal values are allowed", + Errors.ModuleAttributesIncorrectValue, ); } node.value = this.parseLiteral(this.state.value, "StringLiteral"); diff --git a/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-module-attributes/options.json b/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-module-attributes/options.json index 9acd1c25f418..3f9b696c8de9 100644 --- a/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-module-attributes/options.json +++ b/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-module-attributes/options.json @@ -1,5 +1,5 @@ { - "throws": "This experimental syntax requires enabling the parser plugin: 'moduleAttributes' (1:32)", + "throws": "This experimental syntax requires enabling the parser plugin: 'moduleAttributes' (1:27)", "sourceType": "module", "plugins": [] } \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-value/options.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-value/options.json index b4911175a067..31ae88c8c2b1 100644 --- a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-value/options.json +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-value/options.json @@ -3,5 +3,5 @@ "moduleAttributes" ], "sourceType": "module", - "throws": "Only string literal values are allowed (1:52)" + "throws": "Only string literals are allowed as module attributes values (1:52)" } \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-repeated-type/options.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-repeated-type/options.json index 0c6a87381963..9399e0bedd3f 100644 --- a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-repeated-type/options.json +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-repeated-type/options.json @@ -1,7 +1,4 @@ { - "plugins": [ - "moduleAttributes" - ], - "sourceType": "module", - "throws": "Module attributes should not include duplicate keys (1:64)" -} \ No newline at end of file + "plugins": ["moduleAttributes"], + "sourceType": "module" +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-repeated-type/output.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-repeated-type/output.json new file mode 100644 index 000000000000..0bb5aebcc7ce --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-repeated-type/output.json @@ -0,0 +1,265 @@ +{ + "type": "File", + "start": 0, + "end": 73, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 73 + } + }, + "errors": [ + "SyntaxError: Module attributes should not include duplicate keys (type) (1:64)" + ], + "program": { + "type": "Program", + "start": 0, + "end": 73, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 73 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start": 0, + "end": 73, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 73 + } + }, + "specifiers": [ + { + "type": "ImportDefaultSpecifier", + "start": 7, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "local": { + "type": "Identifier", + "start": 7, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 10 + }, + "identifierName": "foo" + }, + "name": "foo" + } + } + ], + "source": { + "type": "StringLiteral", + "start": 16, + "end": 26, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 26 + } + }, + "extra": { + "rawValue": "foo.json", + "raw": "\"foo.json\"" + }, + "value": "foo.json" + }, + "attributes": [ + { + "type": "ImportAttribute", + "start": 32, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 32 + }, + "end": { + "line": 1, + "column": 44 + } + }, + "key": { + "type": "Identifier", + "start": 32, + "end": 36, + "loc": { + "start": { + "line": 1, + "column": 32 + }, + "end": { + "line": 1, + "column": 36 + }, + "identifierName": "type" + }, + "name": "type" + }, + "value": { + "type": "StringLiteral", + "start": 38, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 38 + }, + "end": { + "line": 1, + "column": 44 + } + }, + "extra": { + "rawValue": "json", + "raw": "\"json\"" + }, + "value": "json" + } + }, + { + "type": "ImportAttribute", + "start": 46, + "end": 58, + "loc": { + "start": { + "line": 1, + "column": 46 + }, + "end": { + "line": 1, + "column": 58 + } + }, + "key": { + "type": "Identifier", + "start": 46, + "end": 50, + "loc": { + "start": { + "line": 1, + "column": 46 + }, + "end": { + "line": 1, + "column": 50 + }, + "identifierName": "lazy" + }, + "name": "lazy" + }, + "value": { + "type": "StringLiteral", + "start": 52, + "end": 58, + "loc": { + "start": { + "line": 1, + "column": 52 + }, + "end": { + "line": 1, + "column": 58 + } + }, + "extra": { + "rawValue": "true", + "raw": "\"true\"" + }, + "value": "true" + } + }, + { + "type": "ImportAttribute", + "start": 60, + "end": 72, + "loc": { + "start": { + "line": 1, + "column": 60 + }, + "end": { + "line": 1, + "column": 72 + } + }, + "key": { + "type": "Identifier", + "start": 60, + "end": 64, + "loc": { + "start": { + "line": 1, + "column": 60 + }, + "end": { + "line": 1, + "column": 64 + }, + "identifierName": "type" + }, + "name": "type" + }, + "value": { + "type": "StringLiteral", + "start": 66, + "end": 72, + "loc": { + "start": { + "line": 1, + "column": 66 + }, + "end": { + "line": 1, + "column": 72 + } + }, + "extra": { + "rawValue": "html", + "raw": "\"html\"" + }, + "value": "html" + } + } + ] + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/without-plugin/options.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/without-plugin/options.json index d5829ef69927..8645af2af8c7 100644 --- a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/without-plugin/options.json +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/without-plugin/options.json @@ -1,4 +1,4 @@ { - "throws": "This experimental syntax requires enabling the parser plugin: 'moduleAttributes' (1:32)", + "throws": "This experimental syntax requires enabling the parser plugin: 'moduleAttributes' (1:27)", "plugins": [] } \ No newline at end of file From f795d36e0a7587b2ad99152e682a0d66c340bf9b Mon Sep 17 00:00:00 2001 From: Vivek Nayyar Date: Thu, 12 Mar 2020 22:35:58 +0100 Subject: [PATCH 31/63] test: added a new test for allowing hasOwnProperty as one of the module attributes --- .../input.js | 1 + .../options.json | 4 + .../output.json | 210 ++++++++++++++++++ 3 files changed, 215 insertions(+) create mode 100644 packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-object-method-attribute/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-object-method-attribute/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-object-method-attribute/output.json diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-object-method-attribute/input.js b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-object-method-attribute/input.js new file mode 100644 index 000000000000..d027b991db49 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-object-method-attribute/input.js @@ -0,0 +1 @@ +import foo from "foo.json" with type: "json", hasOwnProperty: "true"; diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-object-method-attribute/options.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-object-method-attribute/options.json new file mode 100644 index 000000000000..9399e0bedd3f --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-object-method-attribute/options.json @@ -0,0 +1,4 @@ +{ + "plugins": ["moduleAttributes"], + "sourceType": "module" +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-object-method-attribute/output.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-object-method-attribute/output.json new file mode 100644 index 000000000000..dd61984b92cf --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-object-method-attribute/output.json @@ -0,0 +1,210 @@ +{ + "type": "File", + "start": 0, + "end": 69, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 69 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 69, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 69 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start": 0, + "end": 69, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 69 + } + }, + "specifiers": [ + { + "type": "ImportDefaultSpecifier", + "start": 7, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "local": { + "type": "Identifier", + "start": 7, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 10 + }, + "identifierName": "foo" + }, + "name": "foo" + } + } + ], + "source": { + "type": "StringLiteral", + "start": 16, + "end": 26, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 26 + } + }, + "extra": { + "rawValue": "foo.json", + "raw": "\"foo.json\"" + }, + "value": "foo.json" + }, + "attributes": [ + { + "type": "ImportAttribute", + "start": 32, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 32 + }, + "end": { + "line": 1, + "column": 44 + } + }, + "key": { + "type": "Identifier", + "start": 32, + "end": 36, + "loc": { + "start": { + "line": 1, + "column": 32 + }, + "end": { + "line": 1, + "column": 36 + }, + "identifierName": "type" + }, + "name": "type" + }, + "value": { + "type": "StringLiteral", + "start": 38, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 38 + }, + "end": { + "line": 1, + "column": 44 + } + }, + "extra": { + "rawValue": "json", + "raw": "\"json\"" + }, + "value": "json" + } + }, + { + "type": "ImportAttribute", + "start": 46, + "end": 68, + "loc": { + "start": { + "line": 1, + "column": 46 + }, + "end": { + "line": 1, + "column": 68 + } + }, + "key": { + "type": "Identifier", + "start": 46, + "end": 60, + "loc": { + "start": { + "line": 1, + "column": 46 + }, + "end": { + "line": 1, + "column": 60 + }, + "identifierName": "hasOwnProperty" + }, + "name": "hasOwnProperty" + }, + "value": { + "type": "StringLiteral", + "start": 62, + "end": 68, + "loc": { + "start": { + "line": 1, + "column": 62 + }, + "end": { + "line": 1, + "column": 68 + } + }, + "extra": { + "rawValue": "true", + "raw": "\"true\"" + }, + "value": "true" + } + } + ] + } + ], + "directives": [] + } +} \ No newline at end of file From 35a85e9db9490d2eac2cb86fb99913b3b3ce42ae Mon Sep 17 00:00:00 2001 From: Vivek Nayyar Date: Thu, 12 Mar 2020 23:42:12 +0100 Subject: [PATCH 32/63] chore: added a mandatory version option to use along with module attributes plugin. tests updated --- packages/babel-parser/src/plugin-utils.js | 14 ++++++++++++++ .../options.json | 9 +++++++-- .../options.json | 9 ++++++++- .../options.json | 9 ++++++++- .../valid-syntax-with-attributes/options.json | 9 ++++++++- .../input.js | 1 + .../options.json | 12 ++++++++++++ .../valid-syntax-with-invalid-value/options.json | 9 +++++++-- .../options.json | 9 +++++++-- .../options.json | 9 ++++++++- .../valid-syntax-with-repeated-type/options.json | 9 ++++++++- .../valid-syntax-without-attributes/options.json | 9 ++++++++- 12 files changed, 96 insertions(+), 12 deletions(-) create mode 100644 packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-plugin-option/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-plugin-option/options.json diff --git a/packages/babel-parser/src/plugin-utils.js b/packages/babel-parser/src/plugin-utils.js index 021f98f1c083..acc4a6b304eb 100644 --- a/packages/babel-parser/src/plugin-utils.js +++ b/packages/babel-parser/src/plugin-utils.js @@ -97,6 +97,20 @@ export function validatePlugins(plugins: PluginList) { RECORD_AND_TUPLE_SYNTAX_TYPES.map(p => `'${p}'`).join(", "), ); } + + if (hasPlugin(plugins, "moduleAttributes")) { + const moduleAttributesVerionPluginOption = getPluginOption( + plugins, + "moduleAttributes", + "version", + ); + if (moduleAttributesVerionPluginOption !== "feb-2020") { + throw new Error( + "The 'moduleAttributes' plugin requires a 'version' option," + + " whose value must be feb-2020.", + ); + } + } } // These plugins are defined using a mixin which extends the parser class. diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/invalid-syntax-with-no-attributes-identifier/options.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/invalid-syntax-with-no-attributes-identifier/options.json index 77e11f22643a..cbb08d0c69a4 100644 --- a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/invalid-syntax-with-no-attributes-identifier/options.json +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/invalid-syntax-with-no-attributes-identifier/options.json @@ -1,8 +1,13 @@ { "plugins": [ - "moduleAttributes", + [ + "moduleAttributes", + { + "version": "feb-2020" + } + ], "estree" ], "sourceType": "module", "throws": "Unexpected token (2:5)" -} \ No newline at end of file +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-and-value/options.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-and-value/options.json index 9399e0bedd3f..442f499887f3 100644 --- a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-and-value/options.json +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-and-value/options.json @@ -1,4 +1,11 @@ { - "plugins": ["moduleAttributes"], + "plugins": [ + [ + "moduleAttributes", + { + "version": "feb-2020" + } + ] + ], "sourceType": "module" } diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-multiple-lines/options.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-multiple-lines/options.json index 9399e0bedd3f..442f499887f3 100644 --- a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-multiple-lines/options.json +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-multiple-lines/options.json @@ -1,4 +1,11 @@ { - "plugins": ["moduleAttributes"], + "plugins": [ + [ + "moduleAttributes", + { + "version": "feb-2020" + } + ] + ], "sourceType": "module" } diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes/options.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes/options.json index 9399e0bedd3f..442f499887f3 100644 --- a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes/options.json +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes/options.json @@ -1,4 +1,11 @@ { - "plugins": ["moduleAttributes"], + "plugins": [ + [ + "moduleAttributes", + { + "version": "feb-2020" + } + ] + ], "sourceType": "module" } diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-plugin-option/input.js b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-plugin-option/input.js new file mode 100644 index 000000000000..fb31508a0860 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-plugin-option/input.js @@ -0,0 +1 @@ +import foo from "foo.json" with type: "json"; diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-plugin-option/options.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-plugin-option/options.json new file mode 100644 index 000000000000..92566a76e0f3 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-plugin-option/options.json @@ -0,0 +1,12 @@ +{ + "plugins": [ + [ + "moduleAttributes", + { + "version": "jan-2020" + } + ] + ], + "sourceType": "module", + "throws": "The 'moduleAttributes' plugin requires a 'version' option, whose value must be feb-2020." +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-value/options.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-value/options.json index 31ae88c8c2b1..b28c434f1369 100644 --- a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-value/options.json +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-value/options.json @@ -1,7 +1,12 @@ { "plugins": [ - "moduleAttributes" + [ + "moduleAttributes", + { + "version": "feb-2020" + } + ] ], "sourceType": "module", "throws": "Only string literals are allowed as module attributes values (1:52)" -} \ No newline at end of file +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-no-type-attribute/options.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-no-type-attribute/options.json index 1e4da5a2f09b..d207edb90981 100644 --- a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-no-type-attribute/options.json +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-no-type-attribute/options.json @@ -1,7 +1,12 @@ { "plugins": [ - "moduleAttributes" + [ + "moduleAttributes", + { + "version": "feb-2020" + } + ] ], "sourceType": "module", "throws": "Module attributes should include a type (1:44)" -} \ No newline at end of file +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-object-method-attribute/options.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-object-method-attribute/options.json index 9399e0bedd3f..442f499887f3 100644 --- a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-object-method-attribute/options.json +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-object-method-attribute/options.json @@ -1,4 +1,11 @@ { - "plugins": ["moduleAttributes"], + "plugins": [ + [ + "moduleAttributes", + { + "version": "feb-2020" + } + ] + ], "sourceType": "module" } diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-repeated-type/options.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-repeated-type/options.json index 9399e0bedd3f..442f499887f3 100644 --- a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-repeated-type/options.json +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-repeated-type/options.json @@ -1,4 +1,11 @@ { - "plugins": ["moduleAttributes"], + "plugins": [ + [ + "moduleAttributes", + { + "version": "feb-2020" + } + ] + ], "sourceType": "module" } diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-without-attributes/options.json b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-without-attributes/options.json index 9399e0bedd3f..442f499887f3 100644 --- a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-without-attributes/options.json +++ b/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-without-attributes/options.json @@ -1,4 +1,11 @@ { - "plugins": ["moduleAttributes"], + "plugins": [ + [ + "moduleAttributes", + { + "version": "feb-2020" + } + ] + ], "sourceType": "module" } From e3976f2038f6fdf75808a0e4e8ee348a2191e917 Mon Sep 17 00:00:00 2001 From: Vivek Nayyar Date: Fri, 13 Mar 2020 02:27:01 +0100 Subject: [PATCH 33/63] improvement: replaced object create with set and renamed test folder to module-attributes --- packages/babel-parser/src/parser/location.js | 6 +++--- packages/babel-parser/src/parser/statement.js | 12 ++++++------ .../input.js | 0 .../options.json | 0 .../input.js | 0 .../options.json | 0 .../valid-syntax-with-attributes-and-value/input.js | 0 .../options.json | 0 .../output.json | 0 .../input.js | 0 .../options.json | 0 .../output.json | 0 .../valid-syntax-with-attributes/input.js | 0 .../valid-syntax-with-attributes/options.json | 0 .../valid-syntax-with-attributes/output.json | 0 .../valid-syntax-with-invalid-plugin-option/input.js | 0 .../options.json | 0 .../valid-syntax-with-invalid-value/input.js | 0 .../valid-syntax-with-invalid-value/options.json | 0 .../valid-syntax-with-no-type-attribute/input.js | 0 .../valid-syntax-with-no-type-attribute/options.json | 4 ++-- .../input.js | 0 .../options.json | 0 .../output.json | 0 .../valid-syntax-with-repeated-type/input.js | 0 .../valid-syntax-with-repeated-type/options.json | 0 .../valid-syntax-with-repeated-type/output.json | 2 +- .../valid-syntax-without-attributes/input.js | 0 .../valid-syntax-without-attributes/options.json | 0 .../valid-syntax-without-attributes/output.json | 0 .../without-plugin/input.js | 0 .../without-plugin/options.json | 0 32 files changed, 12 insertions(+), 12 deletions(-) rename packages/babel-parser/test/fixtures/experimental/_no-plugin/{import-module-attributes => module-attributes}/input.js (100%) rename packages/babel-parser/test/fixtures/experimental/_no-plugin/{import-module-attributes => module-attributes}/options.json (100%) rename packages/babel-parser/test/fixtures/experimental/{import-module-attributes => module-attributes}/invalid-syntax-with-no-attributes-identifier/input.js (100%) rename packages/babel-parser/test/fixtures/experimental/{import-module-attributes => module-attributes}/invalid-syntax-with-no-attributes-identifier/options.json (100%) rename packages/babel-parser/test/fixtures/experimental/{import-module-attributes => module-attributes}/valid-syntax-with-attributes-and-value/input.js (100%) rename packages/babel-parser/test/fixtures/experimental/{import-module-attributes => module-attributes}/valid-syntax-with-attributes-and-value/options.json (100%) rename packages/babel-parser/test/fixtures/experimental/{import-module-attributes => module-attributes}/valid-syntax-with-attributes-and-value/output.json (100%) rename packages/babel-parser/test/fixtures/experimental/{import-module-attributes => module-attributes}/valid-syntax-with-attributes-multiple-lines/input.js (100%) rename packages/babel-parser/test/fixtures/experimental/{import-module-attributes => module-attributes}/valid-syntax-with-attributes-multiple-lines/options.json (100%) rename packages/babel-parser/test/fixtures/experimental/{import-module-attributes => module-attributes}/valid-syntax-with-attributes-multiple-lines/output.json (100%) rename packages/babel-parser/test/fixtures/experimental/{import-module-attributes => module-attributes}/valid-syntax-with-attributes/input.js (100%) rename packages/babel-parser/test/fixtures/experimental/{import-module-attributes => module-attributes}/valid-syntax-with-attributes/options.json (100%) rename packages/babel-parser/test/fixtures/experimental/{import-module-attributes => module-attributes}/valid-syntax-with-attributes/output.json (100%) rename packages/babel-parser/test/fixtures/experimental/{import-module-attributes => module-attributes}/valid-syntax-with-invalid-plugin-option/input.js (100%) rename packages/babel-parser/test/fixtures/experimental/{import-module-attributes => module-attributes}/valid-syntax-with-invalid-plugin-option/options.json (100%) rename packages/babel-parser/test/fixtures/experimental/{import-module-attributes => module-attributes}/valid-syntax-with-invalid-value/input.js (100%) rename packages/babel-parser/test/fixtures/experimental/{import-module-attributes => module-attributes}/valid-syntax-with-invalid-value/options.json (100%) rename packages/babel-parser/test/fixtures/experimental/{import-module-attributes => module-attributes}/valid-syntax-with-no-type-attribute/input.js (100%) rename packages/babel-parser/test/fixtures/experimental/{import-module-attributes => module-attributes}/valid-syntax-with-no-type-attribute/options.json (65%) rename packages/babel-parser/test/fixtures/experimental/{import-module-attributes => module-attributes}/valid-syntax-with-object-method-attribute/input.js (100%) rename packages/babel-parser/test/fixtures/experimental/{import-module-attributes => module-attributes}/valid-syntax-with-object-method-attribute/options.json (100%) rename packages/babel-parser/test/fixtures/experimental/{import-module-attributes => module-attributes}/valid-syntax-with-object-method-attribute/output.json (100%) rename packages/babel-parser/test/fixtures/experimental/{import-module-attributes => module-attributes}/valid-syntax-with-repeated-type/input.js (100%) rename packages/babel-parser/test/fixtures/experimental/{import-module-attributes => module-attributes}/valid-syntax-with-repeated-type/options.json (100%) rename packages/babel-parser/test/fixtures/experimental/{import-module-attributes => module-attributes}/valid-syntax-with-repeated-type/output.json (98%) rename packages/babel-parser/test/fixtures/experimental/{import-module-attributes => module-attributes}/valid-syntax-without-attributes/input.js (100%) rename packages/babel-parser/test/fixtures/experimental/{import-module-attributes => module-attributes}/valid-syntax-without-attributes/options.json (100%) rename packages/babel-parser/test/fixtures/experimental/{import-module-attributes => module-attributes}/valid-syntax-without-attributes/output.json (100%) rename packages/babel-parser/test/fixtures/experimental/{import-module-attributes => module-attributes}/without-plugin/input.js (100%) rename packages/babel-parser/test/fixtures/experimental/{import-module-attributes => module-attributes}/without-plugin/options.json (100%) diff --git a/packages/babel-parser/src/parser/location.js b/packages/babel-parser/src/parser/location.js index b583ad5ceb78..dfedb3dbe9d4 100644 --- a/packages/babel-parser/src/parser/location.js +++ b/packages/babel-parser/src/parser/location.js @@ -97,11 +97,11 @@ export const Errors = Object.freeze({ MissingUnicodeEscape: "Expecting Unicode escape sequence \\uXXXX", MixingCoalesceWithLogical: "Nullish coalescing operator(??) requires parens when mixing with logical operators", - ModuleAttributesIncorrectValue: + ModuleAttributeInvalidValue: "Only string literals are allowed as module attributes values", ModuleAttributesWithDuplicateKeys: - "Module attributes should not include duplicate keys (%0)", - ModuleAttributesWithoutType: "Module attributes should include a type", + 'Duplicate key "%0" is not allowed in module attributes', + ModuleAttributesWithoutType: 'Module attributes should include a "type" key', ModuleExportUndefined: "Export '%0' is not defined", MultipleDefaultsInSwitch: "Multiple default clauses", NewlineAfterThrow: "Illegal newline after throw", diff --git a/packages/babel-parser/src/parser/statement.js b/packages/babel-parser/src/parser/statement.js index ce6860d3a801..799200e66cca 100644 --- a/packages/babel-parser/src/parser/statement.js +++ b/packages/babel-parser/src/parser/statement.js @@ -2106,7 +2106,7 @@ export default class StatementParser extends ExpressionParser { return null; } const attrs = []; - const attributeMap = Object.create(null); + const attributes = new Set(); do { // we are trying to parse a node which has the following syntax // with type: "json" @@ -2116,22 +2116,22 @@ export default class StatementParser extends ExpressionParser { // check if we already have an entry for an attribute // if a duplicate entry found, throw an error - if (attributeMap[node.key.name]) { + if (attributes.has(node.key.name)) { this.raise( this.state.start, Errors.ModuleAttributesWithDuplicateKeys, node.key.name, ); } - // set the attribute name entry in the attributeMap to true - attributeMap[node.key.name] = true; + // set the attribute name entry in the attributeMap + attributes.add(node.key.name); // check for colon this.expect(tt.colon); // check if the value set to the module attribute is a string as we only allow string literals if (!this.match(tt.string)) { throw this.unexpected( this.state.start, - Errors.ModuleAttributesIncorrectValue, + Errors.ModuleAttributeInvalidValue, ); } node.value = this.parseLiteral(this.state.value, "StringLiteral"); @@ -2140,7 +2140,7 @@ export default class StatementParser extends ExpressionParser { } while (this.eat(tt.comma)); // check if module attributes do not contain any type key and throw an error - if (!attributeMap["type"]) { + if (!attributes.has("type")) { throw this.raise(this.state.start, Errors.ModuleAttributesWithoutType); } return attrs; diff --git a/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-module-attributes/input.js b/packages/babel-parser/test/fixtures/experimental/_no-plugin/module-attributes/input.js similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/_no-plugin/import-module-attributes/input.js rename to packages/babel-parser/test/fixtures/experimental/_no-plugin/module-attributes/input.js diff --git a/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-module-attributes/options.json b/packages/babel-parser/test/fixtures/experimental/_no-plugin/module-attributes/options.json similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/_no-plugin/import-module-attributes/options.json rename to packages/babel-parser/test/fixtures/experimental/_no-plugin/module-attributes/options.json diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/invalid-syntax-with-no-attributes-identifier/input.js b/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-no-attributes-identifier/input.js similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/import-module-attributes/invalid-syntax-with-no-attributes-identifier/input.js rename to packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-no-attributes-identifier/input.js diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/invalid-syntax-with-no-attributes-identifier/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-no-attributes-identifier/options.json similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/import-module-attributes/invalid-syntax-with-no-attributes-identifier/options.json rename to packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-no-attributes-identifier/options.json diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-and-value/input.js b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-and-value/input.js similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-and-value/input.js rename to packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-and-value/input.js diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-and-value/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-and-value/options.json similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-and-value/options.json rename to packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-and-value/options.json diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-and-value/output.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-and-value/output.json similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-and-value/output.json rename to packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-and-value/output.json diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-multiple-lines/input.js b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-multiple-lines/input.js similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-multiple-lines/input.js rename to packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-multiple-lines/input.js diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-multiple-lines/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-multiple-lines/options.json similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-multiple-lines/options.json rename to packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-multiple-lines/options.json diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-multiple-lines/output.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-multiple-lines/output.json similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes-multiple-lines/output.json rename to packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-multiple-lines/output.json diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes/input.js b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes/input.js similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes/input.js rename to packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes/input.js diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes/options.json similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes/options.json rename to packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes/options.json diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes/output.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes/output.json similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-attributes/output.json rename to packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes/output.json diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-plugin-option/input.js b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-plugin-option/input.js similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-plugin-option/input.js rename to packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-plugin-option/input.js diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-plugin-option/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-plugin-option/options.json similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-plugin-option/options.json rename to packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-plugin-option/options.json diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-value/input.js b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-value/input.js similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-value/input.js rename to packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-value/input.js diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-value/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-value/options.json similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-invalid-value/options.json rename to packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-value/options.json diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-no-type-attribute/input.js b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/input.js similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-no-type-attribute/input.js rename to packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/input.js diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-no-type-attribute/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/options.json similarity index 65% rename from packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-no-type-attribute/options.json rename to packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/options.json index d207edb90981..0b87b1133d2d 100644 --- a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-no-type-attribute/options.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/options.json @@ -8,5 +8,5 @@ ] ], "sourceType": "module", - "throws": "Module attributes should include a type (1:44)" -} + "throws": "Module attributes should include a \"type\" key (1:44)" +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-object-method-attribute/input.js b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/input.js similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-object-method-attribute/input.js rename to packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/input.js diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-object-method-attribute/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/options.json similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-object-method-attribute/options.json rename to packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/options.json diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-object-method-attribute/output.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/output.json similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-object-method-attribute/output.json rename to packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/output.json diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-repeated-type/input.js b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/input.js similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-repeated-type/input.js rename to packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/input.js diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-repeated-type/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/options.json similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-repeated-type/options.json rename to packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/options.json diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-repeated-type/output.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/output.json similarity index 98% rename from packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-repeated-type/output.json rename to packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/output.json index 0bb5aebcc7ce..7ca48ebcffec 100644 --- a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-with-repeated-type/output.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/output.json @@ -13,7 +13,7 @@ } }, "errors": [ - "SyntaxError: Module attributes should not include duplicate keys (type) (1:64)" + "SyntaxError: Duplicate key \"type\" is not allowed in module attributes (1:64)" ], "program": { "type": "Program", diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-without-attributes/input.js b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-without-attributes/input.js similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-without-attributes/input.js rename to packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-without-attributes/input.js diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-without-attributes/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-without-attributes/options.json similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-without-attributes/options.json rename to packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-without-attributes/options.json diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-without-attributes/output.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-without-attributes/output.json similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/import-module-attributes/valid-syntax-without-attributes/output.json rename to packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-without-attributes/output.json diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/without-plugin/input.js b/packages/babel-parser/test/fixtures/experimental/module-attributes/without-plugin/input.js similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/import-module-attributes/without-plugin/input.js rename to packages/babel-parser/test/fixtures/experimental/module-attributes/without-plugin/input.js diff --git a/packages/babel-parser/test/fixtures/experimental/import-module-attributes/without-plugin/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/without-plugin/options.json similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/import-module-attributes/without-plugin/options.json rename to packages/babel-parser/test/fixtures/experimental/module-attributes/without-plugin/options.json From efacdaf7807a13fb7c3e42af539d1b4993e2e63f Mon Sep 17 00:00:00 2001 From: Vivek Nayyar Date: Fri, 13 Mar 2020 11:19:19 +0100 Subject: [PATCH 34/63] Update packages/babel-parser/src/plugin-utils.js MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit improved error message on version mismatch Co-Authored-By: Nicolò Ribaudo --- packages/babel-parser/src/plugin-utils.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/babel-parser/src/plugin-utils.js b/packages/babel-parser/src/plugin-utils.js index acc4a6b304eb..1d2193dee6b4 100644 --- a/packages/babel-parser/src/plugin-utils.js +++ b/packages/babel-parser/src/plugin-utils.js @@ -107,7 +107,8 @@ export function validatePlugins(plugins: PluginList) { if (moduleAttributesVerionPluginOption !== "feb-2020") { throw new Error( "The 'moduleAttributes' plugin requires a 'version' option," + - " whose value must be feb-2020.", + " representing the last proposal update. Currently, the" + + " only supported value is 'feb-2020'.", ); } } From 4202fa9e0e94bc3a0da9d0001f71774e4093929c Mon Sep 17 00:00:00 2001 From: Vivek Nayyar Date: Fri, 13 Mar 2020 11:19:54 +0100 Subject: [PATCH 35/63] Update packages/babel-parser/src/parser/statement.js MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit comment rectified Co-Authored-By: Nicolò Ribaudo --- packages/babel-parser/src/parser/statement.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/babel-parser/src/parser/statement.js b/packages/babel-parser/src/parser/statement.js index 799200e66cca..1d31dc5baec2 100644 --- a/packages/babel-parser/src/parser/statement.js +++ b/packages/babel-parser/src/parser/statement.js @@ -2115,7 +2115,7 @@ export default class StatementParser extends ExpressionParser { node.key = this.parseIdentifier(true); // check if we already have an entry for an attribute - // if a duplicate entry found, throw an error + // if a duplicate entry is found, throw an error if (attributes.has(node.key.name)) { this.raise( this.state.start, From e1016ad570d3feead22d959ca427501c5f0bbb7d Mon Sep 17 00:00:00 2001 From: Vivek Nayyar Date: Fri, 13 Mar 2020 11:25:43 +0100 Subject: [PATCH 36/63] chore: unnecesaary comment removed and tests updated --- packages/babel-parser/src/parser/statement.js | 2 -- .../valid-syntax-with-invalid-plugin-option/options.json | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/babel-parser/src/parser/statement.js b/packages/babel-parser/src/parser/statement.js index 1d31dc5baec2..a063998af358 100644 --- a/packages/babel-parser/src/parser/statement.js +++ b/packages/babel-parser/src/parser/statement.js @@ -2123,9 +2123,7 @@ export default class StatementParser extends ExpressionParser { node.key.name, ); } - // set the attribute name entry in the attributeMap attributes.add(node.key.name); - // check for colon this.expect(tt.colon); // check if the value set to the module attribute is a string as we only allow string literals if (!this.match(tt.string)) { diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-plugin-option/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-plugin-option/options.json index 92566a76e0f3..29907e14ccd5 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-plugin-option/options.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-plugin-option/options.json @@ -8,5 +8,5 @@ ] ], "sourceType": "module", - "throws": "The 'moduleAttributes' plugin requires a 'version' option, whose value must be feb-2020." + "throws": "The 'moduleAttributes' plugin requires a 'version' option, representing the last proposal update. Currently, the only supported value is 'feb-2020'." } \ No newline at end of file From f89c15fab237b6ab0c8602667837862fb57ddbc0 Mon Sep 17 00:00:00 2001 From: Vivek Nayyar Date: Fri, 13 Mar 2020 11:31:53 +0100 Subject: [PATCH 37/63] improvement: using node.key.start instead of this.node.start to point at the exact duplicate module attribute name --- packages/babel-parser/src/parser/statement.js | 2 +- .../valid-syntax-with-repeated-type/output.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/babel-parser/src/parser/statement.js b/packages/babel-parser/src/parser/statement.js index a063998af358..a35c0ee65d29 100644 --- a/packages/babel-parser/src/parser/statement.js +++ b/packages/babel-parser/src/parser/statement.js @@ -2118,7 +2118,7 @@ export default class StatementParser extends ExpressionParser { // if a duplicate entry is found, throw an error if (attributes.has(node.key.name)) { this.raise( - this.state.start, + node.key.start, Errors.ModuleAttributesWithDuplicateKeys, node.key.name, ); diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/output.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/output.json index 7ca48ebcffec..39f3a86e0788 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/output.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/output.json @@ -13,7 +13,7 @@ } }, "errors": [ - "SyntaxError: Duplicate key \"type\" is not allowed in module attributes (1:64)" + "SyntaxError: Duplicate key \"type\" is not allowed in module attributes (1:60)" ], "program": { "type": "Program", From 18dd064cc362295408276abf574c01587864ff9a Mon Sep 17 00:00:00 2001 From: Vivek Nayyar Date: Sat, 14 Mar 2020 22:41:14 +0100 Subject: [PATCH 38/63] improvement: added check for line terminator --- packages/babel-parser/src/parser/statement.js | 2 +- .../options.json | 3 +- .../output.json | 117 ++++++++++++++++ .../options.json | 5 +- .../output.json | 125 ------------------ 5 files changed, 122 insertions(+), 130 deletions(-) create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-no-attributes-identifier/output.json delete mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-multiple-lines/output.json diff --git a/packages/babel-parser/src/parser/statement.js b/packages/babel-parser/src/parser/statement.js index a35c0ee65d29..81fb1a6b0b42 100644 --- a/packages/babel-parser/src/parser/statement.js +++ b/packages/babel-parser/src/parser/statement.js @@ -2098,7 +2098,7 @@ export default class StatementParser extends ExpressionParser { } maybeParseModuleAttributes() { - if (this.match(tt._with)) { + if (this.match(tt._with) && !this.isLineTerminator()) { this.expectPlugin("moduleAttributes"); this.next(); } else { diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-no-attributes-identifier/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-no-attributes-identifier/options.json index cbb08d0c69a4..d8393072fd10 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-no-attributes-identifier/options.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-no-attributes-identifier/options.json @@ -8,6 +8,5 @@ ], "estree" ], - "sourceType": "module", - "throws": "Unexpected token (2:5)" + "sourceType": "module" } diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-no-attributes-identifier/output.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-no-attributes-identifier/output.json new file mode 100644 index 000000000000..44515fdf2d4a --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-no-attributes-identifier/output.json @@ -0,0 +1,117 @@ +{ + "type": "File", + "start": 0, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 10 + } + }, + "errors": [ + "SyntaxError: 'with' in strict mode (2:0)" + ], + "program": { + "type": "Program", + "start": 0, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 10 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start": 0, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "specifiers": [], + "source": { + "type": "Literal", + "start": 7, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "value": "x", + "raw": "\"x\"" + }, + "attributes": [] + }, + { + "type": "WithStatement", + "start": 11, + "end": 21, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 10 + } + }, + "object": { + "type": "ObjectExpression", + "start": 17, + "end": 19, + "loc": { + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 8 + } + }, + "properties": [] + }, + "body": { + "type": "EmptyStatement", + "start": 20, + "end": 21, + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 10 + } + } + } + } + ] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-multiple-lines/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-multiple-lines/options.json index 442f499887f3..1314343331ed 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-multiple-lines/options.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-multiple-lines/options.json @@ -7,5 +7,6 @@ } ] ], - "sourceType": "module" -} + "sourceType": "module", + "throws": "Unexpected token, expected \"(\" (3:0)" +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-multiple-lines/output.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-multiple-lines/output.json deleted file mode 100644 index 7ab9b34a1162..000000000000 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-multiple-lines/output.json +++ /dev/null @@ -1,125 +0,0 @@ -{ - "type": "File", - "start": 0, - "end": 28, - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 3, - "column": 12 - } - }, - "program": { - "type": "Program", - "start": 0, - "end": 28, - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 3, - "column": 12 - } - }, - "sourceType": "module", - "interpreter": null, - "body": [ - { - "type": "ImportDeclaration", - "start": 0, - "end": 28, - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 3, - "column": 12 - } - }, - "specifiers": [], - "source": { - "type": "StringLiteral", - "start": 7, - "end": 10, - "loc": { - "start": { - "line": 1, - "column": 7 - }, - "end": { - "line": 1, - "column": 10 - } - }, - "extra": { - "rawValue": "x", - "raw": "\"x\"" - }, - "value": "x" - }, - "attributes": [ - { - "type": "ImportAttribute", - "start": 16, - "end": 28, - "loc": { - "start": { - "line": 3, - "column": 0 - }, - "end": { - "line": 3, - "column": 12 - } - }, - "key": { - "type": "Identifier", - "start": 16, - "end": 20, - "loc": { - "start": { - "line": 3, - "column": 0 - }, - "end": { - "line": 3, - "column": 4 - }, - "identifierName": "type" - }, - "name": "type" - }, - "value": { - "type": "StringLiteral", - "start": 22, - "end": 28, - "loc": { - "start": { - "line": 3, - "column": 6 - }, - "end": { - "line": 3, - "column": 12 - } - }, - "extra": { - "rawValue": "json", - "raw": "\"json\"" - }, - "value": "json" - } - } - ] - } - ], - "directives": [] - } -} \ No newline at end of file From 31f6a383c9e0d58961a5a53edb529dbb447f64fb Mon Sep 17 00:00:00 2001 From: Vivek Nayyar Date: Sat, 14 Mar 2020 22:46:48 +0100 Subject: [PATCH 39/63] chore: renamed valid-syntax-with-attributes-multiple-lines to invalid-syntax-with-attributes-multiple-lines --- .../input.js | 0 .../options.json | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename packages/babel-parser/test/fixtures/experimental/module-attributes/{valid-syntax-with-attributes-multiple-lines => invalid-syntax-with-attributes-multiple-lines}/input.js (100%) rename packages/babel-parser/test/fixtures/experimental/module-attributes/{valid-syntax-with-attributes-multiple-lines => invalid-syntax-with-attributes-multiple-lines}/options.json (100%) diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-multiple-lines/input.js b/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-attributes-multiple-lines/input.js similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-multiple-lines/input.js rename to packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-attributes-multiple-lines/input.js diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-multiple-lines/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-attributes-multiple-lines/options.json similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-multiple-lines/options.json rename to packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-attributes-multiple-lines/options.json From b98824f6708d27bf27f5a65d5d5cab76862c1246 Mon Sep 17 00:00:00 2001 From: Vivek Nayyar Date: Sun, 15 Mar 2020 23:23:26 +0100 Subject: [PATCH 40/63] improvement: changed the logic and tests to only support type as the allowed module attribute --- packages/babel-parser/src/parser/location.js | 4 +- packages/babel-parser/src/parser/statement.js | 10 + .../options.json | 4 +- .../options.json | 2 +- .../options.json | 5 +- .../output.json | 210 ------------------ .../valid-syntax-with-repeated-type/input.js | 2 +- .../output.json | 70 +----- 8 files changed, 29 insertions(+), 278 deletions(-) delete mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/output.json diff --git a/packages/babel-parser/src/parser/location.js b/packages/babel-parser/src/parser/location.js index dfedb3dbe9d4..33253d1e63de 100644 --- a/packages/babel-parser/src/parser/location.js +++ b/packages/babel-parser/src/parser/location.js @@ -97,8 +97,10 @@ export const Errors = Object.freeze({ MissingUnicodeEscape: "Expecting Unicode escape sequence \\uXXXX", MixingCoalesceWithLogical: "Nullish coalescing operator(??) requires parens when mixing with logical operators", + ModuleAttributeDifferentFromType: + "The only accepted module attribute is `type`", ModuleAttributeInvalidValue: - "Only string literals are allowed as module attributes values", + "Only string literals are allowed as module attribute values", ModuleAttributesWithDuplicateKeys: 'Duplicate key "%0" is not allowed in module attributes', ModuleAttributesWithoutType: 'Module attributes should include a "type" key', diff --git a/packages/babel-parser/src/parser/statement.js b/packages/babel-parser/src/parser/statement.js index 81fb1a6b0b42..dd85de9d7688 100644 --- a/packages/babel-parser/src/parser/statement.js +++ b/packages/babel-parser/src/parser/statement.js @@ -2114,8 +2114,18 @@ export default class StatementParser extends ExpressionParser { const node = this.startNode(); node.key = this.parseIdentifier(true); + // for now we are only allowing `type` as the only allowed module attribute + if (node.key.name !== "type") { + throw this.raise( + node.key.start, + Errors.ModuleAttributeDifferentFromType, + node.key.name, + ); + } + // check if we already have an entry for an attribute // if a duplicate entry is found, throw an error + // for now this logic will come into play only when someone declares `type` twice if (attributes.has(node.key.name)) { this.raise( node.key.start, diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-value/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-value/options.json index b28c434f1369..37f2ffe2a74f 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-value/options.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-value/options.json @@ -8,5 +8,5 @@ ] ], "sourceType": "module", - "throws": "Only string literals are allowed as module attributes values (1:52)" -} + "throws": "The only accepted module attribute is `type` (1:46)" +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/options.json index 0b87b1133d2d..989cd557d8c4 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/options.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/options.json @@ -8,5 +8,5 @@ ] ], "sourceType": "module", - "throws": "Module attributes should include a \"type\" key (1:44)" + "throws": "The only accepted module attribute is `type` (1:32)" } \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/options.json index 442f499887f3..37f2ffe2a74f 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/options.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/options.json @@ -7,5 +7,6 @@ } ] ], - "sourceType": "module" -} + "sourceType": "module", + "throws": "The only accepted module attribute is `type` (1:46)" +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/output.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/output.json deleted file mode 100644 index dd61984b92cf..000000000000 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/output.json +++ /dev/null @@ -1,210 +0,0 @@ -{ - "type": "File", - "start": 0, - "end": 69, - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 1, - "column": 69 - } - }, - "program": { - "type": "Program", - "start": 0, - "end": 69, - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 1, - "column": 69 - } - }, - "sourceType": "module", - "interpreter": null, - "body": [ - { - "type": "ImportDeclaration", - "start": 0, - "end": 69, - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 1, - "column": 69 - } - }, - "specifiers": [ - { - "type": "ImportDefaultSpecifier", - "start": 7, - "end": 10, - "loc": { - "start": { - "line": 1, - "column": 7 - }, - "end": { - "line": 1, - "column": 10 - } - }, - "local": { - "type": "Identifier", - "start": 7, - "end": 10, - "loc": { - "start": { - "line": 1, - "column": 7 - }, - "end": { - "line": 1, - "column": 10 - }, - "identifierName": "foo" - }, - "name": "foo" - } - } - ], - "source": { - "type": "StringLiteral", - "start": 16, - "end": 26, - "loc": { - "start": { - "line": 1, - "column": 16 - }, - "end": { - "line": 1, - "column": 26 - } - }, - "extra": { - "rawValue": "foo.json", - "raw": "\"foo.json\"" - }, - "value": "foo.json" - }, - "attributes": [ - { - "type": "ImportAttribute", - "start": 32, - "end": 44, - "loc": { - "start": { - "line": 1, - "column": 32 - }, - "end": { - "line": 1, - "column": 44 - } - }, - "key": { - "type": "Identifier", - "start": 32, - "end": 36, - "loc": { - "start": { - "line": 1, - "column": 32 - }, - "end": { - "line": 1, - "column": 36 - }, - "identifierName": "type" - }, - "name": "type" - }, - "value": { - "type": "StringLiteral", - "start": 38, - "end": 44, - "loc": { - "start": { - "line": 1, - "column": 38 - }, - "end": { - "line": 1, - "column": 44 - } - }, - "extra": { - "rawValue": "json", - "raw": "\"json\"" - }, - "value": "json" - } - }, - { - "type": "ImportAttribute", - "start": 46, - "end": 68, - "loc": { - "start": { - "line": 1, - "column": 46 - }, - "end": { - "line": 1, - "column": 68 - } - }, - "key": { - "type": "Identifier", - "start": 46, - "end": 60, - "loc": { - "start": { - "line": 1, - "column": 46 - }, - "end": { - "line": 1, - "column": 60 - }, - "identifierName": "hasOwnProperty" - }, - "name": "hasOwnProperty" - }, - "value": { - "type": "StringLiteral", - "start": 62, - "end": 68, - "loc": { - "start": { - "line": 1, - "column": 62 - }, - "end": { - "line": 1, - "column": 68 - } - }, - "extra": { - "rawValue": "true", - "raw": "\"true\"" - }, - "value": "true" - } - } - ] - } - ], - "directives": [] - } -} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/input.js b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/input.js index f742d7cc15d1..856373edcf34 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/input.js +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/input.js @@ -1 +1 @@ -import foo from "foo.json" with type: "json", lazy: "true", type: "html"; +import foo from "foo.json" with type: "json", type: "html"; diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/output.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/output.json index 39f3a86e0788..cd32d74344f6 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/output.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/output.json @@ -1,7 +1,7 @@ { "type": "File", "start": 0, - "end": 73, + "end": 59, "loc": { "start": { "line": 1, @@ -9,16 +9,16 @@ }, "end": { "line": 1, - "column": 73 + "column": 59 } }, "errors": [ - "SyntaxError: Duplicate key \"type\" is not allowed in module attributes (1:60)" + "SyntaxError: Duplicate key \"type\" is not allowed in module attributes (1:46)" ], "program": { "type": "Program", "start": 0, - "end": 73, + "end": 59, "loc": { "start": { "line": 1, @@ -26,7 +26,7 @@ }, "end": { "line": 1, - "column": 73 + "column": 59 } }, "sourceType": "module", @@ -35,7 +35,7 @@ { "type": "ImportDeclaration", "start": 0, - "end": 73, + "end": 59, "loc": { "start": { "line": 1, @@ -43,7 +43,7 @@ }, "end": { "line": 1, - "column": 73 + "column": 59 } }, "specifiers": [ @@ -180,9 +180,9 @@ "line": 1, "column": 50 }, - "identifierName": "lazy" + "identifierName": "type" }, - "name": "lazy" + "name": "type" }, "value": { "type": "StringLiteral", @@ -198,58 +198,6 @@ "column": 58 } }, - "extra": { - "rawValue": "true", - "raw": "\"true\"" - }, - "value": "true" - } - }, - { - "type": "ImportAttribute", - "start": 60, - "end": 72, - "loc": { - "start": { - "line": 1, - "column": 60 - }, - "end": { - "line": 1, - "column": 72 - } - }, - "key": { - "type": "Identifier", - "start": 60, - "end": 64, - "loc": { - "start": { - "line": 1, - "column": 60 - }, - "end": { - "line": 1, - "column": 64 - }, - "identifierName": "type" - }, - "name": "type" - }, - "value": { - "type": "StringLiteral", - "start": 66, - "end": 72, - "loc": { - "start": { - "line": 1, - "column": 66 - }, - "end": { - "line": 1, - "column": 72 - } - }, "extra": { "rawValue": "html", "raw": "\"html\"" From 1c2be0155223f9ef54d5677a8415aca1f8515c53 Mon Sep 17 00:00:00 2001 From: Vivek Nayyar Date: Mon, 16 Mar 2020 00:18:45 +0100 Subject: [PATCH 41/63] improvement: don't throw an error when attribute name is not type, just raise it and continue parsing --- packages/babel-parser/src/parser/statement.js | 3 +- .../options.json | 4 +- .../options.json | 2 +- .../options.json | 3 +- .../output.json | 213 ++++++++++++++++++ 5 files changed, 219 insertions(+), 6 deletions(-) create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/output.json diff --git a/packages/babel-parser/src/parser/statement.js b/packages/babel-parser/src/parser/statement.js index dd85de9d7688..59e4a53bfe1e 100644 --- a/packages/babel-parser/src/parser/statement.js +++ b/packages/babel-parser/src/parser/statement.js @@ -2116,7 +2116,7 @@ export default class StatementParser extends ExpressionParser { // for now we are only allowing `type` as the only allowed module attribute if (node.key.name !== "type") { - throw this.raise( + this.raise( node.key.start, Errors.ModuleAttributeDifferentFromType, node.key.name, @@ -2151,6 +2151,7 @@ export default class StatementParser extends ExpressionParser { if (!attributes.has("type")) { throw this.raise(this.state.start, Errors.ModuleAttributesWithoutType); } + return attrs; } diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-value/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-value/options.json index 37f2ffe2a74f..03ae01b6b106 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-value/options.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-value/options.json @@ -8,5 +8,5 @@ ] ], "sourceType": "module", - "throws": "The only accepted module attribute is `type` (1:46)" -} \ No newline at end of file + "throws": "Only string literals are allowed as module attribute values (1:52)" +} diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/options.json index 989cd557d8c4..0b87b1133d2d 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/options.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/options.json @@ -8,5 +8,5 @@ ] ], "sourceType": "module", - "throws": "The only accepted module attribute is `type` (1:32)" + "throws": "Module attributes should include a \"type\" key (1:44)" } \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/options.json index 37f2ffe2a74f..319126fb3325 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/options.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/options.json @@ -7,6 +7,5 @@ } ] ], - "sourceType": "module", - "throws": "The only accepted module attribute is `type` (1:46)" + "sourceType": "module" } \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/output.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/output.json new file mode 100644 index 000000000000..87230474782c --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/output.json @@ -0,0 +1,213 @@ +{ + "type": "File", + "start": 0, + "end": 69, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 69 + } + }, + "errors": [ + "SyntaxError: The only accepted module attribute is `type` (1:46)" + ], + "program": { + "type": "Program", + "start": 0, + "end": 69, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 69 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start": 0, + "end": 69, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 69 + } + }, + "specifiers": [ + { + "type": "ImportDefaultSpecifier", + "start": 7, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "local": { + "type": "Identifier", + "start": 7, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 10 + }, + "identifierName": "foo" + }, + "name": "foo" + } + } + ], + "source": { + "type": "StringLiteral", + "start": 16, + "end": 26, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 26 + } + }, + "extra": { + "rawValue": "foo.json", + "raw": "\"foo.json\"" + }, + "value": "foo.json" + }, + "attributes": [ + { + "type": "ImportAttribute", + "start": 32, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 32 + }, + "end": { + "line": 1, + "column": 44 + } + }, + "key": { + "type": "Identifier", + "start": 32, + "end": 36, + "loc": { + "start": { + "line": 1, + "column": 32 + }, + "end": { + "line": 1, + "column": 36 + }, + "identifierName": "type" + }, + "name": "type" + }, + "value": { + "type": "StringLiteral", + "start": 38, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 38 + }, + "end": { + "line": 1, + "column": 44 + } + }, + "extra": { + "rawValue": "json", + "raw": "\"json\"" + }, + "value": "json" + } + }, + { + "type": "ImportAttribute", + "start": 46, + "end": 68, + "loc": { + "start": { + "line": 1, + "column": 46 + }, + "end": { + "line": 1, + "column": 68 + } + }, + "key": { + "type": "Identifier", + "start": 46, + "end": 60, + "loc": { + "start": { + "line": 1, + "column": 46 + }, + "end": { + "line": 1, + "column": 60 + }, + "identifierName": "hasOwnProperty" + }, + "name": "hasOwnProperty" + }, + "value": { + "type": "StringLiteral", + "start": 62, + "end": 68, + "loc": { + "start": { + "line": 1, + "column": 62 + }, + "end": { + "line": 1, + "column": 68 + } + }, + "extra": { + "rawValue": "true", + "raw": "\"true\"" + }, + "value": "true" + } + } + ] + } + ], + "directives": [] + } +} \ No newline at end of file From 180b75704179cf83eb42216cd48f7cac28a0e0d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Mon, 16 Mar 2020 00:29:49 +0100 Subject: [PATCH 42/63] Revert output.js --- .../dotall-regex/with-unicode-property-escape/output.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/babel-plugin-transform-dotall-regex/test/fixtures/dotall-regex/with-unicode-property-escape/output.js b/packages/babel-plugin-transform-dotall-regex/test/fixtures/dotall-regex/with-unicode-property-escape/output.js index c00aa15a9c1f..a8ddf757d926 100644 --- a/packages/babel-plugin-transform-dotall-regex/test/fixtures/dotall-regex/with-unicode-property-escape/output.js +++ b/packages/babel-plugin-transform-dotall-regex/test/fixtures/dotall-regex/with-unicode-property-escape/output.js @@ -1,2 +1,2 @@ -var a = /[\u3400-\u4DBF\u4E00-\u9FFC\uFA0E\uFA0F\uFA11\uFA13\uFA14\uFA1F\uFA21\uFA23\uFA24\uFA27-\uFA29\u{20000}-\u{2A6DD}\u{2A700}-\u{2B734}\u{2B740}-\u{2B81D}\u{2B820}-\u{2CEA1}\u{2CEB0}-\u{2EBE0}\u{30000}-\u{3134A}][\0-\t\x0B\f\x0E-\u2027\u202A-\u{10FFFF}]/u; -var b = /[\u3400-\u4DBF\u4E00-\u9FFC\uFA0E\uFA0F\uFA11\uFA13\uFA14\uFA1F\uFA21\uFA23\uFA24\uFA27-\uFA29\u{20000}-\u{2A6DD}\u{2A700}-\u{2B734}\u{2B740}-\u{2B81D}\u{2B820}-\u{2CEA1}\u{2CEB0}-\u{2EBE0}\u{30000}-\u{3134A}][\0-\u{10FFFF}]/u; +var a = /[\u3400-\u4DB5\u4E00-\u9FEF\uFA0E\uFA0F\uFA11\uFA13\uFA14\uFA1F\uFA21\uFA23\uFA24\uFA27-\uFA29\u{20000}-\u{2A6D6}\u{2A700}-\u{2B734}\u{2B740}-\u{2B81D}\u{2B820}-\u{2CEA1}\u{2CEB0}-\u{2EBE0}][\0-\t\x0B\f\x0E-\u2027\u202A-\u{10FFFF}]/u; +var b = /[\u3400-\u4DB5\u4E00-\u9FEF\uFA0E\uFA0F\uFA11\uFA13\uFA14\uFA1F\uFA21\uFA23\uFA24\uFA27-\uFA29\u{20000}-\u{2A6D6}\u{2A700}-\u{2B734}\u{2B740}-\u{2B81D}\u{2B820}-\u{2CEA1}\u{2CEB0}-\u{2EBE0}][\0-\u{10FFFF}]/u; From ca2608fcfa0dd2eaab59438fb56b46a564737a86 Mon Sep 17 00:00:00 2001 From: Vivek Nayyar Date: Mon, 16 Mar 2020 00:42:18 +0100 Subject: [PATCH 43/63] improvement: removed the check for presence of tyep attribute as an earlier check will already catch this --- packages/babel-parser/src/parser/statement.js | 5 - .../options.json | 3 +- .../output.json | 161 ++++++++++++++++++ 3 files changed, 162 insertions(+), 7 deletions(-) create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/output.json diff --git a/packages/babel-parser/src/parser/statement.js b/packages/babel-parser/src/parser/statement.js index 59e4a53bfe1e..8049f38bb8f9 100644 --- a/packages/babel-parser/src/parser/statement.js +++ b/packages/babel-parser/src/parser/statement.js @@ -2147,11 +2147,6 @@ export default class StatementParser extends ExpressionParser { attrs.push(node); } while (this.eat(tt.comma)); - // check if module attributes do not contain any type key and throw an error - if (!attributes.has("type")) { - throw this.raise(this.state.start, Errors.ModuleAttributesWithoutType); - } - return attrs; } diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/options.json index 0b87b1133d2d..319126fb3325 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/options.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/options.json @@ -7,6 +7,5 @@ } ] ], - "sourceType": "module", - "throws": "Module attributes should include a \"type\" key (1:44)" + "sourceType": "module" } \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/output.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/output.json new file mode 100644 index 000000000000..9b41293d33a0 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/output.json @@ -0,0 +1,161 @@ +{ + "type": "File", + "start": 0, + "end": 45, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 45 + } + }, + "errors": [ + "SyntaxError: The only accepted module attribute is `type` (1:32)" + ], + "program": { + "type": "Program", + "start": 0, + "end": 45, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 45 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start": 0, + "end": 45, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 45 + } + }, + "specifiers": [ + { + "type": "ImportDefaultSpecifier", + "start": 7, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "local": { + "type": "Identifier", + "start": 7, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 10 + }, + "identifierName": "foo" + }, + "name": "foo" + } + } + ], + "source": { + "type": "StringLiteral", + "start": 16, + "end": 26, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 26 + } + }, + "extra": { + "rawValue": "foo.json", + "raw": "\"foo.json\"" + }, + "value": "foo.json" + }, + "attributes": [ + { + "type": "ImportAttribute", + "start": 32, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 32 + }, + "end": { + "line": 1, + "column": 44 + } + }, + "key": { + "type": "Identifier", + "start": 32, + "end": 36, + "loc": { + "start": { + "line": 1, + "column": 32 + }, + "end": { + "line": 1, + "column": 36 + }, + "identifierName": "lazy" + }, + "name": "lazy" + }, + "value": { + "type": "StringLiteral", + "start": 38, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 38 + }, + "end": { + "line": 1, + "column": 44 + } + }, + "extra": { + "rawValue": "true", + "raw": "\"true\"" + }, + "value": "true" + } + } + ] + } + ], + "directives": [] + } +} \ No newline at end of file From 3581a82efcbb554f283728f2d16af0e2f5b6c1ce Mon Sep 17 00:00:00 2001 From: Vivek Nayyar Date: Mon, 16 Mar 2020 01:11:18 +0100 Subject: [PATCH 44/63] feat: added syntax-import-module-attributes --- .../.npmignore | 3 +++ .../README.md | 19 +++++++++++++++ .../package.json | 23 +++++++++++++++++++ .../src/index.js | 21 +++++++++++++++++ 4 files changed, 66 insertions(+) create mode 100644 packages/babel-plugin-syntax-import-module-attributes/.npmignore create mode 100644 packages/babel-plugin-syntax-import-module-attributes/README.md create mode 100644 packages/babel-plugin-syntax-import-module-attributes/package.json create mode 100644 packages/babel-plugin-syntax-import-module-attributes/src/index.js diff --git a/packages/babel-plugin-syntax-import-module-attributes/.npmignore b/packages/babel-plugin-syntax-import-module-attributes/.npmignore new file mode 100644 index 000000000000..2b1fceba679b --- /dev/null +++ b/packages/babel-plugin-syntax-import-module-attributes/.npmignore @@ -0,0 +1,3 @@ +*.log +src +test diff --git a/packages/babel-plugin-syntax-import-module-attributes/README.md b/packages/babel-plugin-syntax-import-module-attributes/README.md new file mode 100644 index 000000000000..4a3dadb0cf96 --- /dev/null +++ b/packages/babel-plugin-syntax-import-module-attributes/README.md @@ -0,0 +1,19 @@ +# @babel/plugin-syntax-import-module-attributes + +> Allow parsing of the pipeline operator + +See our website [@babel/plugin-syntax-import-module-attributes](https://babeljs.io/docs/en/next/babel-plugin-syntax-import-module-attributes.html) for more information. + +## Install + +Using npm: + +```sh +npm install --save-dev @babel/plugin-syntax-import-module-attributes +``` + +or using yarn: + +```sh +yarn add @babel/plugin-syntax-import-module-attributes --dev +``` diff --git a/packages/babel-plugin-syntax-import-module-attributes/package.json b/packages/babel-plugin-syntax-import-module-attributes/package.json new file mode 100644 index 000000000000..8cf463f52e21 --- /dev/null +++ b/packages/babel-plugin-syntax-import-module-attributes/package.json @@ -0,0 +1,23 @@ +{ + "name": "@babel/plugin-syntax-import-module-attributes", + "version": "7.8.3", + "description": "Allow parsing of the module attributes in the import statement", + "repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-syntax-import-module-attributes", + "license": "MIT", + "publishConfig": { + "access": "public" + }, + "main": "lib/index.js", + "keywords": [ + "babel-plugin" + ], + "dependencies": { + "@babel/helper-plugin-utils": "^7.8.3" + }, + "peerDependencies": { + "@babel/core": "^7.0.0-0" + }, + "devDependencies": { + "@babel/core": "^7.8.3" + } +} diff --git a/packages/babel-plugin-syntax-import-module-attributes/src/index.js b/packages/babel-plugin-syntax-import-module-attributes/src/index.js new file mode 100644 index 000000000000..6136484b9ad5 --- /dev/null +++ b/packages/babel-plugin-syntax-import-module-attributes/src/index.js @@ -0,0 +1,21 @@ +import { declare } from "@babel/helper-plugin-utils"; + +export default declare((api, { version }) => { + api.assertVersion(7); + + if (typeof version !== "string" || version !== "feb-2020") { + throw new Error( + "The 'moduleAttributes' plugin requires a 'version' option," + + " representing the last proposal update. Currently, the" + + " only supported value is 'feb-2020'.", + ); + } + + return { + name: "syntax-import-module-attributes", + + manipulateOptions(opts, parserOpts) { + parserOpts.plugins.push(["moduleAttributes", { version }]); + }, + }; +}); From b9d2939734d0900b2d19ea7093a19100ddbbbb9b Mon Sep 17 00:00:00 2001 From: Vivek Nayyar Date: Mon, 16 Mar 2020 01:14:36 +0100 Subject: [PATCH 45/63] fix: fixed a message test in the readme for syntax-import-module-attributes --- packages/babel-plugin-syntax-import-module-attributes/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/babel-plugin-syntax-import-module-attributes/README.md b/packages/babel-plugin-syntax-import-module-attributes/README.md index 4a3dadb0cf96..1ba0cabbf67a 100644 --- a/packages/babel-plugin-syntax-import-module-attributes/README.md +++ b/packages/babel-plugin-syntax-import-module-attributes/README.md @@ -1,6 +1,6 @@ # @babel/plugin-syntax-import-module-attributes -> Allow parsing of the pipeline operator +> Allow parsing of the module attributes in the import statements See our website [@babel/plugin-syntax-import-module-attributes](https://babeljs.io/docs/en/next/babel-plugin-syntax-import-module-attributes.html) for more information. From 24f7830a8fadde2cccae93d91159d1da54e392b4 Mon Sep 17 00:00:00 2001 From: Vivek Nayyar Date: Mon, 16 Mar 2020 01:29:57 +0100 Subject: [PATCH 46/63] refactor: removed the error key ModuleAttributesWithoutType as it is not needed now --- packages/babel-parser/src/parser/location.js | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/babel-parser/src/parser/location.js b/packages/babel-parser/src/parser/location.js index 33253d1e63de..17650e72b614 100644 --- a/packages/babel-parser/src/parser/location.js +++ b/packages/babel-parser/src/parser/location.js @@ -103,7 +103,6 @@ export const Errors = Object.freeze({ "Only string literals are allowed as module attribute values", ModuleAttributesWithDuplicateKeys: 'Duplicate key "%0" is not allowed in module attributes', - ModuleAttributesWithoutType: 'Module attributes should include a "type" key', ModuleExportUndefined: "Export '%0' is not defined", MultipleDefaultsInSwitch: "Multiple default clauses", NewlineAfterThrow: "Illegal newline after throw", From b1270c6f1db30a487ffe448bfd94da75663b1c67 Mon Sep 17 00:00:00 2001 From: Vivek Nayyar Date: Mon, 16 Mar 2020 01:41:30 +0100 Subject: [PATCH 47/63] refactor: renamed syntax-import-module-attributes to syntax-module-attributes --- .../README.md | 19 ------------------- .../.npmignore | 0 .../README.md | 19 +++++++++++++++++++ .../package.json | 4 ++-- .../src/index.js | 2 +- 5 files changed, 22 insertions(+), 22 deletions(-) delete mode 100644 packages/babel-plugin-syntax-import-module-attributes/README.md rename packages/{babel-plugin-syntax-import-module-attributes => babel-plugin-syntax-module-attributes}/.npmignore (100%) create mode 100644 packages/babel-plugin-syntax-module-attributes/README.md rename packages/{babel-plugin-syntax-import-module-attributes => babel-plugin-syntax-module-attributes}/package.json (81%) rename packages/{babel-plugin-syntax-import-module-attributes => babel-plugin-syntax-module-attributes}/src/index.js (92%) diff --git a/packages/babel-plugin-syntax-import-module-attributes/README.md b/packages/babel-plugin-syntax-import-module-attributes/README.md deleted file mode 100644 index 1ba0cabbf67a..000000000000 --- a/packages/babel-plugin-syntax-import-module-attributes/README.md +++ /dev/null @@ -1,19 +0,0 @@ -# @babel/plugin-syntax-import-module-attributes - -> Allow parsing of the module attributes in the import statements - -See our website [@babel/plugin-syntax-import-module-attributes](https://babeljs.io/docs/en/next/babel-plugin-syntax-import-module-attributes.html) for more information. - -## Install - -Using npm: - -```sh -npm install --save-dev @babel/plugin-syntax-import-module-attributes -``` - -or using yarn: - -```sh -yarn add @babel/plugin-syntax-import-module-attributes --dev -``` diff --git a/packages/babel-plugin-syntax-import-module-attributes/.npmignore b/packages/babel-plugin-syntax-module-attributes/.npmignore similarity index 100% rename from packages/babel-plugin-syntax-import-module-attributes/.npmignore rename to packages/babel-plugin-syntax-module-attributes/.npmignore diff --git a/packages/babel-plugin-syntax-module-attributes/README.md b/packages/babel-plugin-syntax-module-attributes/README.md new file mode 100644 index 000000000000..3ae0f5a00927 --- /dev/null +++ b/packages/babel-plugin-syntax-module-attributes/README.md @@ -0,0 +1,19 @@ +# @babel/plugin-syntax-module-attributes + +> Allow parsing of the module attributes in the import statements + +See our website [@babel/plugin-syntax-module-attributes](https://babeljs.io/docs/en/next/babel-plugin-syntax-module-attributes.html) for more information. + +## Install + +Using npm: + +```sh +npm install --save-dev @babel/plugin-syntax-module-attributes +``` + +or using yarn: + +```sh +yarn add @babel/plugin-syntax-module-attributes --dev +``` diff --git a/packages/babel-plugin-syntax-import-module-attributes/package.json b/packages/babel-plugin-syntax-module-attributes/package.json similarity index 81% rename from packages/babel-plugin-syntax-import-module-attributes/package.json rename to packages/babel-plugin-syntax-module-attributes/package.json index 8cf463f52e21..3a828f67decd 100644 --- a/packages/babel-plugin-syntax-import-module-attributes/package.json +++ b/packages/babel-plugin-syntax-module-attributes/package.json @@ -1,8 +1,8 @@ { - "name": "@babel/plugin-syntax-import-module-attributes", + "name": "@babel/plugin-syntax-module-attributes", "version": "7.8.3", "description": "Allow parsing of the module attributes in the import statement", - "repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-syntax-import-module-attributes", + "repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-syntax-module-attributes", "license": "MIT", "publishConfig": { "access": "public" diff --git a/packages/babel-plugin-syntax-import-module-attributes/src/index.js b/packages/babel-plugin-syntax-module-attributes/src/index.js similarity index 92% rename from packages/babel-plugin-syntax-import-module-attributes/src/index.js rename to packages/babel-plugin-syntax-module-attributes/src/index.js index 6136484b9ad5..e3bf31baaf0c 100644 --- a/packages/babel-plugin-syntax-import-module-attributes/src/index.js +++ b/packages/babel-plugin-syntax-module-attributes/src/index.js @@ -12,7 +12,7 @@ export default declare((api, { version }) => { } return { - name: "syntax-import-module-attributes", + name: "syntax-module-attributes", manipulateOptions(opts, parserOpts) { parserOpts.plugins.push(["moduleAttributes", { version }]); From b7280e20810d69828240836acb875cc17c285532 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Tue, 28 Apr 2020 23:08:16 +0200 Subject: [PATCH 48/63] Format output files --- .../output.json | 91 +--------- .../output.json | 131 ++------------- .../valid-syntax-with-attributes/output.json | 119 +------------ .../output.json | 119 +------------ .../output.json | 159 ++---------------- .../output.json | 159 ++---------------- .../output.json | 79 +-------- 7 files changed, 65 insertions(+), 792 deletions(-) diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-no-attributes-identifier/output.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-no-attributes-identifier/output.json index 44515fdf2d4a..cccb9e1b329d 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-no-attributes-identifier/output.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-no-attributes-identifier/output.json @@ -1,66 +1,22 @@ { "type": "File", - "start": 0, - "end": 21, - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 2, - "column": 10 - } - }, + "start":0,"end":21,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":10}}, "errors": [ "SyntaxError: 'with' in strict mode (2:0)" ], "program": { "type": "Program", - "start": 0, - "end": 21, - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 2, - "column": 10 - } - }, + "start":0,"end":21,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":10}}, "sourceType": "module", "interpreter": null, "body": [ { "type": "ImportDeclaration", - "start": 0, - "end": 10, - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 1, - "column": 10 - } - }, + "start":0,"end":10,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":10}}, "specifiers": [], "source": { "type": "Literal", - "start": 7, - "end": 10, - "loc": { - "start": { - "line": 1, - "column": 7 - }, - "end": { - "line": 1, - "column": 10 - } - }, + "start":7,"end":10,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":10}}, "value": "x", "raw": "\"x\"" }, @@ -68,48 +24,15 @@ }, { "type": "WithStatement", - "start": 11, - "end": 21, - "loc": { - "start": { - "line": 2, - "column": 0 - }, - "end": { - "line": 2, - "column": 10 - } - }, + "start":11,"end":21,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":10}}, "object": { "type": "ObjectExpression", - "start": 17, - "end": 19, - "loc": { - "start": { - "line": 2, - "column": 6 - }, - "end": { - "line": 2, - "column": 8 - } - }, + "start":17,"end":19,"loc":{"start":{"line":2,"column":6},"end":{"line":2,"column":8}}, "properties": [] }, "body": { "type": "EmptyStatement", - "start": 20, - "end": 21, - "loc": { - "start": { - "line": 2, - "column": 9 - }, - "end": { - "line": 2, - "column": 10 - } - } + "start":20,"end":21,"loc":{"start":{"line":2,"column":9},"end":{"line":2,"column":10}} } } ] diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-and-value/output.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-and-value/output.json index b8c8d5f9cff4..e830da53a1c5 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-and-value/output.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-and-value/output.json @@ -1,63 +1,19 @@ { "type": "File", - "start": 0, - "end": 32, - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 2, - "column": 3 - } - }, + "start":0,"end":32,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":3}}, "program": { "type": "Program", - "start": 0, - "end": 32, - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 2, - "column": 3 - } - }, + "start":0,"end":32,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":3}}, "sourceType": "module", "interpreter": null, "body": [ { "type": "ImportDeclaration", - "start": 0, - "end": 28, - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 1, - "column": 28 - } - }, + "start":0,"end":28,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":28}}, "specifiers": [], "source": { "type": "StringLiteral", - "start": 7, - "end": 10, - "loc": { - "start": { - "line": 1, - "column": 7 - }, - "end": { - "line": 1, - "column": 10 - } - }, + "start":7,"end":10,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":10}}, "extra": { "rawValue": "x", "raw": "\"x\"" @@ -67,49 +23,15 @@ "attributes": [ { "type": "ImportAttribute", - "start": 16, - "end": 28, - "loc": { - "start": { - "line": 1, - "column": 16 - }, - "end": { - "line": 1, - "column": 28 - } - }, + "start":16,"end":28,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":28}}, "key": { "type": "Identifier", - "start": 16, - "end": 20, - "loc": { - "start": { - "line": 1, - "column": 16 - }, - "end": { - "line": 1, - "column": 20 - }, - "identifierName": "type" - }, + "start":16,"end":20,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":20},"identifierName":"type"}, "name": "type" }, "value": { "type": "StringLiteral", - "start": 22, - "end": 28, - "loc": { - "start": { - "line": 1, - "column": 22 - }, - "end": { - "line": 1, - "column": 28 - } - }, + "start":22,"end":28,"loc":{"start":{"line":1,"column":22},"end":{"line":1,"column":28}}, "extra": { "rawValue": "json", "raw": "\"json\"" @@ -121,47 +43,14 @@ }, { "type": "ExpressionStatement", - "start": 29, - "end": 32, - "loc": { - "start": { - "line": 2, - "column": 0 - }, - "end": { - "line": 2, - "column": 3 - } - }, + "start":29,"end":32,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":3}}, "expression": { "type": "ArrayExpression", - "start": 29, - "end": 32, - "loc": { - "start": { - "line": 2, - "column": 0 - }, - "end": { - "line": 2, - "column": 3 - } - }, + "start":29,"end":32,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":3}}, "elements": [ { "type": "NumericLiteral", - "start": 30, - "end": 31, - "loc": { - "start": { - "line": 2, - "column": 1 - }, - "end": { - "line": 2, - "column": 2 - } - }, + "start":30,"end":31,"loc":{"start":{"line":2,"column":1},"end":{"line":2,"column":2}}, "extra": { "rawValue": 0, "raw": "0" diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes/output.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes/output.json index d2db79b27ddb..dd150dd9dbb1 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes/output.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes/output.json @@ -1,96 +1,29 @@ { "type": "File", - "start": 0, - "end": 45, - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 1, - "column": 45 - } - }, + "start":0,"end":45,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":45}}, "program": { "type": "Program", - "start": 0, - "end": 45, - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 1, - "column": 45 - } - }, + "start":0,"end":45,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":45}}, "sourceType": "module", "interpreter": null, "body": [ { "type": "ImportDeclaration", - "start": 0, - "end": 45, - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 1, - "column": 45 - } - }, + "start":0,"end":45,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":45}}, "specifiers": [ { "type": "ImportDefaultSpecifier", - "start": 7, - "end": 10, - "loc": { - "start": { - "line": 1, - "column": 7 - }, - "end": { - "line": 1, - "column": 10 - } - }, + "start":7,"end":10,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":10}}, "local": { "type": "Identifier", - "start": 7, - "end": 10, - "loc": { - "start": { - "line": 1, - "column": 7 - }, - "end": { - "line": 1, - "column": 10 - }, - "identifierName": "foo" - }, + "start":7,"end":10,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":10},"identifierName":"foo"}, "name": "foo" } } ], "source": { "type": "StringLiteral", - "start": 16, - "end": 26, - "loc": { - "start": { - "line": 1, - "column": 16 - }, - "end": { - "line": 1, - "column": 26 - } - }, + "start":16,"end":26,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":26}}, "extra": { "rawValue": "foo.json", "raw": "\"foo.json\"" @@ -100,49 +33,15 @@ "attributes": [ { "type": "ImportAttribute", - "start": 32, - "end": 44, - "loc": { - "start": { - "line": 1, - "column": 32 - }, - "end": { - "line": 1, - "column": 44 - } - }, + "start":32,"end":44,"loc":{"start":{"line":1,"column":32},"end":{"line":1,"column":44}}, "key": { "type": "Identifier", - "start": 32, - "end": 36, - "loc": { - "start": { - "line": 1, - "column": 32 - }, - "end": { - "line": 1, - "column": 36 - }, - "identifierName": "type" - }, + "start":32,"end":36,"loc":{"start":{"line":1,"column":32},"end":{"line":1,"column":36},"identifierName":"type"}, "name": "type" }, "value": { "type": "StringLiteral", - "start": 38, - "end": 44, - "loc": { - "start": { - "line": 1, - "column": 38 - }, - "end": { - "line": 1, - "column": 44 - } - }, + "start":38,"end":44,"loc":{"start":{"line":1,"column":38},"end":{"line":1,"column":44}}, "extra": { "rawValue": "json", "raw": "\"json\"" diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/output.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/output.json index 9b41293d33a0..a3bbfd621562 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/output.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/output.json @@ -1,99 +1,32 @@ { "type": "File", - "start": 0, - "end": 45, - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 1, - "column": 45 - } - }, + "start":0,"end":45,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":45}}, "errors": [ "SyntaxError: The only accepted module attribute is `type` (1:32)" ], "program": { "type": "Program", - "start": 0, - "end": 45, - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 1, - "column": 45 - } - }, + "start":0,"end":45,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":45}}, "sourceType": "module", "interpreter": null, "body": [ { "type": "ImportDeclaration", - "start": 0, - "end": 45, - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 1, - "column": 45 - } - }, + "start":0,"end":45,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":45}}, "specifiers": [ { "type": "ImportDefaultSpecifier", - "start": 7, - "end": 10, - "loc": { - "start": { - "line": 1, - "column": 7 - }, - "end": { - "line": 1, - "column": 10 - } - }, + "start":7,"end":10,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":10}}, "local": { "type": "Identifier", - "start": 7, - "end": 10, - "loc": { - "start": { - "line": 1, - "column": 7 - }, - "end": { - "line": 1, - "column": 10 - }, - "identifierName": "foo" - }, + "start":7,"end":10,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":10},"identifierName":"foo"}, "name": "foo" } } ], "source": { "type": "StringLiteral", - "start": 16, - "end": 26, - "loc": { - "start": { - "line": 1, - "column": 16 - }, - "end": { - "line": 1, - "column": 26 - } - }, + "start":16,"end":26,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":26}}, "extra": { "rawValue": "foo.json", "raw": "\"foo.json\"" @@ -103,49 +36,15 @@ "attributes": [ { "type": "ImportAttribute", - "start": 32, - "end": 44, - "loc": { - "start": { - "line": 1, - "column": 32 - }, - "end": { - "line": 1, - "column": 44 - } - }, + "start":32,"end":44,"loc":{"start":{"line":1,"column":32},"end":{"line":1,"column":44}}, "key": { "type": "Identifier", - "start": 32, - "end": 36, - "loc": { - "start": { - "line": 1, - "column": 32 - }, - "end": { - "line": 1, - "column": 36 - }, - "identifierName": "lazy" - }, + "start":32,"end":36,"loc":{"start":{"line":1,"column":32},"end":{"line":1,"column":36},"identifierName":"lazy"}, "name": "lazy" }, "value": { "type": "StringLiteral", - "start": 38, - "end": 44, - "loc": { - "start": { - "line": 1, - "column": 38 - }, - "end": { - "line": 1, - "column": 44 - } - }, + "start":38,"end":44,"loc":{"start":{"line":1,"column":38},"end":{"line":1,"column":44}}, "extra": { "rawValue": "true", "raw": "\"true\"" diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/output.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/output.json index 87230474782c..12af675ae8b5 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/output.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/output.json @@ -1,99 +1,32 @@ { "type": "File", - "start": 0, - "end": 69, - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 1, - "column": 69 - } - }, + "start":0,"end":69,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":69}}, "errors": [ "SyntaxError: The only accepted module attribute is `type` (1:46)" ], "program": { "type": "Program", - "start": 0, - "end": 69, - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 1, - "column": 69 - } - }, + "start":0,"end":69,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":69}}, "sourceType": "module", "interpreter": null, "body": [ { "type": "ImportDeclaration", - "start": 0, - "end": 69, - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 1, - "column": 69 - } - }, + "start":0,"end":69,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":69}}, "specifiers": [ { "type": "ImportDefaultSpecifier", - "start": 7, - "end": 10, - "loc": { - "start": { - "line": 1, - "column": 7 - }, - "end": { - "line": 1, - "column": 10 - } - }, + "start":7,"end":10,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":10}}, "local": { "type": "Identifier", - "start": 7, - "end": 10, - "loc": { - "start": { - "line": 1, - "column": 7 - }, - "end": { - "line": 1, - "column": 10 - }, - "identifierName": "foo" - }, + "start":7,"end":10,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":10},"identifierName":"foo"}, "name": "foo" } } ], "source": { "type": "StringLiteral", - "start": 16, - "end": 26, - "loc": { - "start": { - "line": 1, - "column": 16 - }, - "end": { - "line": 1, - "column": 26 - } - }, + "start":16,"end":26,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":26}}, "extra": { "rawValue": "foo.json", "raw": "\"foo.json\"" @@ -103,49 +36,15 @@ "attributes": [ { "type": "ImportAttribute", - "start": 32, - "end": 44, - "loc": { - "start": { - "line": 1, - "column": 32 - }, - "end": { - "line": 1, - "column": 44 - } - }, + "start":32,"end":44,"loc":{"start":{"line":1,"column":32},"end":{"line":1,"column":44}}, "key": { "type": "Identifier", - "start": 32, - "end": 36, - "loc": { - "start": { - "line": 1, - "column": 32 - }, - "end": { - "line": 1, - "column": 36 - }, - "identifierName": "type" - }, + "start":32,"end":36,"loc":{"start":{"line":1,"column":32},"end":{"line":1,"column":36},"identifierName":"type"}, "name": "type" }, "value": { "type": "StringLiteral", - "start": 38, - "end": 44, - "loc": { - "start": { - "line": 1, - "column": 38 - }, - "end": { - "line": 1, - "column": 44 - } - }, + "start":38,"end":44,"loc":{"start":{"line":1,"column":38},"end":{"line":1,"column":44}}, "extra": { "rawValue": "json", "raw": "\"json\"" @@ -155,49 +54,15 @@ }, { "type": "ImportAttribute", - "start": 46, - "end": 68, - "loc": { - "start": { - "line": 1, - "column": 46 - }, - "end": { - "line": 1, - "column": 68 - } - }, + "start":46,"end":68,"loc":{"start":{"line":1,"column":46},"end":{"line":1,"column":68}}, "key": { "type": "Identifier", - "start": 46, - "end": 60, - "loc": { - "start": { - "line": 1, - "column": 46 - }, - "end": { - "line": 1, - "column": 60 - }, - "identifierName": "hasOwnProperty" - }, + "start":46,"end":60,"loc":{"start":{"line":1,"column":46},"end":{"line":1,"column":60},"identifierName":"hasOwnProperty"}, "name": "hasOwnProperty" }, "value": { "type": "StringLiteral", - "start": 62, - "end": 68, - "loc": { - "start": { - "line": 1, - "column": 62 - }, - "end": { - "line": 1, - "column": 68 - } - }, + "start":62,"end":68,"loc":{"start":{"line":1,"column":62},"end":{"line":1,"column":68}}, "extra": { "rawValue": "true", "raw": "\"true\"" diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/output.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/output.json index cd32d74344f6..ad53ece6e4dc 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/output.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/output.json @@ -1,99 +1,32 @@ { "type": "File", - "start": 0, - "end": 59, - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 1, - "column": 59 - } - }, + "start":0,"end":59,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":59}}, "errors": [ "SyntaxError: Duplicate key \"type\" is not allowed in module attributes (1:46)" ], "program": { "type": "Program", - "start": 0, - "end": 59, - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 1, - "column": 59 - } - }, + "start":0,"end":59,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":59}}, "sourceType": "module", "interpreter": null, "body": [ { "type": "ImportDeclaration", - "start": 0, - "end": 59, - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 1, - "column": 59 - } - }, + "start":0,"end":59,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":59}}, "specifiers": [ { "type": "ImportDefaultSpecifier", - "start": 7, - "end": 10, - "loc": { - "start": { - "line": 1, - "column": 7 - }, - "end": { - "line": 1, - "column": 10 - } - }, + "start":7,"end":10,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":10}}, "local": { "type": "Identifier", - "start": 7, - "end": 10, - "loc": { - "start": { - "line": 1, - "column": 7 - }, - "end": { - "line": 1, - "column": 10 - }, - "identifierName": "foo" - }, + "start":7,"end":10,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":10},"identifierName":"foo"}, "name": "foo" } } ], "source": { "type": "StringLiteral", - "start": 16, - "end": 26, - "loc": { - "start": { - "line": 1, - "column": 16 - }, - "end": { - "line": 1, - "column": 26 - } - }, + "start":16,"end":26,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":26}}, "extra": { "rawValue": "foo.json", "raw": "\"foo.json\"" @@ -103,49 +36,15 @@ "attributes": [ { "type": "ImportAttribute", - "start": 32, - "end": 44, - "loc": { - "start": { - "line": 1, - "column": 32 - }, - "end": { - "line": 1, - "column": 44 - } - }, + "start":32,"end":44,"loc":{"start":{"line":1,"column":32},"end":{"line":1,"column":44}}, "key": { "type": "Identifier", - "start": 32, - "end": 36, - "loc": { - "start": { - "line": 1, - "column": 32 - }, - "end": { - "line": 1, - "column": 36 - }, - "identifierName": "type" - }, + "start":32,"end":36,"loc":{"start":{"line":1,"column":32},"end":{"line":1,"column":36},"identifierName":"type"}, "name": "type" }, "value": { "type": "StringLiteral", - "start": 38, - "end": 44, - "loc": { - "start": { - "line": 1, - "column": 38 - }, - "end": { - "line": 1, - "column": 44 - } - }, + "start":38,"end":44,"loc":{"start":{"line":1,"column":38},"end":{"line":1,"column":44}}, "extra": { "rawValue": "json", "raw": "\"json\"" @@ -155,49 +54,15 @@ }, { "type": "ImportAttribute", - "start": 46, - "end": 58, - "loc": { - "start": { - "line": 1, - "column": 46 - }, - "end": { - "line": 1, - "column": 58 - } - }, + "start":46,"end":58,"loc":{"start":{"line":1,"column":46},"end":{"line":1,"column":58}}, "key": { "type": "Identifier", - "start": 46, - "end": 50, - "loc": { - "start": { - "line": 1, - "column": 46 - }, - "end": { - "line": 1, - "column": 50 - }, - "identifierName": "type" - }, + "start":46,"end":50,"loc":{"start":{"line":1,"column":46},"end":{"line":1,"column":50},"identifierName":"type"}, "name": "type" }, "value": { "type": "StringLiteral", - "start": 52, - "end": 58, - "loc": { - "start": { - "line": 1, - "column": 52 - }, - "end": { - "line": 1, - "column": 58 - } - }, + "start":52,"end":58,"loc":{"start":{"line":1,"column":52},"end":{"line":1,"column":58}}, "extra": { "rawValue": "html", "raw": "\"html\"" diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-without-attributes/output.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-without-attributes/output.json index dec2f8457cbd..9a295afe63f1 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-without-attributes/output.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-without-attributes/output.json @@ -1,96 +1,29 @@ { "type": "File", - "start": 0, - "end": 27, - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 1, - "column": 27 - } - }, + "start":0,"end":27,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":27}}, "program": { "type": "Program", - "start": 0, - "end": 27, - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 1, - "column": 27 - } - }, + "start":0,"end":27,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":27}}, "sourceType": "module", "interpreter": null, "body": [ { "type": "ImportDeclaration", - "start": 0, - "end": 27, - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 1, - "column": 27 - } - }, + "start":0,"end":27,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":27}}, "specifiers": [ { "type": "ImportDefaultSpecifier", - "start": 7, - "end": 10, - "loc": { - "start": { - "line": 1, - "column": 7 - }, - "end": { - "line": 1, - "column": 10 - } - }, + "start":7,"end":10,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":10}}, "local": { "type": "Identifier", - "start": 7, - "end": 10, - "loc": { - "start": { - "line": 1, - "column": 7 - }, - "end": { - "line": 1, - "column": 10 - }, - "identifierName": "foo" - }, + "start":7,"end":10,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":10},"identifierName":"foo"}, "name": "foo" } } ], "source": { "type": "StringLiteral", - "start": 16, - "end": 26, - "loc": { - "start": { - "line": 1, - "column": 16 - }, - "end": { - "line": 1, - "column": 26 - } - }, + "start":16,"end":26,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":26}}, "extra": { "rawValue": "foo.json", "raw": "\"foo.json\"" From 6e4e730fe0e33a8130554c534c31154a3ec4f400 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Tue, 28 Apr 2020 23:10:38 +0200 Subject: [PATCH 49/63] feb-2020 -> apr-2020 --- packages/babel-parser/src/plugin-utils.js | 4 ++-- .../options.json | 4 ++-- .../invalid-syntax-with-no-attributes-identifier/options.json | 2 +- .../valid-syntax-with-attributes-and-value/options.json | 2 +- .../valid-syntax-with-attributes/options.json | 2 +- .../valid-syntax-with-invalid-plugin-option/options.json | 4 ++-- .../valid-syntax-with-invalid-value/options.json | 2 +- .../valid-syntax-with-no-type-attribute/options.json | 4 ++-- .../valid-syntax-with-object-method-attribute/options.json | 4 ++-- .../valid-syntax-with-repeated-type/options.json | 2 +- .../valid-syntax-without-attributes/options.json | 2 +- packages/babel-plugin-syntax-module-attributes/src/index.js | 4 ++-- 12 files changed, 18 insertions(+), 18 deletions(-) diff --git a/packages/babel-parser/src/plugin-utils.js b/packages/babel-parser/src/plugin-utils.js index 1d2193dee6b4..b52080840e08 100644 --- a/packages/babel-parser/src/plugin-utils.js +++ b/packages/babel-parser/src/plugin-utils.js @@ -104,11 +104,11 @@ export function validatePlugins(plugins: PluginList) { "moduleAttributes", "version", ); - if (moduleAttributesVerionPluginOption !== "feb-2020") { + if (moduleAttributesVerionPluginOption !== "apr-2020") { throw new Error( "The 'moduleAttributes' plugin requires a 'version' option," + " representing the last proposal update. Currently, the" + - " only supported value is 'feb-2020'.", + " only supported value is 'apr-2020'.", ); } } diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-attributes-multiple-lines/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-attributes-multiple-lines/options.json index 1314343331ed..1965f198bb3b 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-attributes-multiple-lines/options.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-attributes-multiple-lines/options.json @@ -3,10 +3,10 @@ [ "moduleAttributes", { - "version": "feb-2020" + "version": "apr-2020" } ] ], "sourceType": "module", "throws": "Unexpected token, expected \"(\" (3:0)" -} \ No newline at end of file +} diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-no-attributes-identifier/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-no-attributes-identifier/options.json index d8393072fd10..ed78a6ddc602 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-no-attributes-identifier/options.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-no-attributes-identifier/options.json @@ -3,7 +3,7 @@ [ "moduleAttributes", { - "version": "feb-2020" + "version": "apr-2020" } ], "estree" diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-and-value/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-and-value/options.json index 442f499887f3..3fb83668e5d9 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-and-value/options.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-and-value/options.json @@ -3,7 +3,7 @@ [ "moduleAttributes", { - "version": "feb-2020" + "version": "apr-2020" } ] ], diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes/options.json index 442f499887f3..3fb83668e5d9 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes/options.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes/options.json @@ -3,7 +3,7 @@ [ "moduleAttributes", { - "version": "feb-2020" + "version": "apr-2020" } ] ], diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-plugin-option/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-plugin-option/options.json index 29907e14ccd5..cea5d223cdf1 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-plugin-option/options.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-plugin-option/options.json @@ -8,5 +8,5 @@ ] ], "sourceType": "module", - "throws": "The 'moduleAttributes' plugin requires a 'version' option, representing the last proposal update. Currently, the only supported value is 'feb-2020'." -} \ No newline at end of file + "throws": "The 'moduleAttributes' plugin requires a 'version' option, representing the last proposal update. Currently, the only supported value is 'apr-2020'." +} diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-value/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-value/options.json index 03ae01b6b106..8ea9b78d3d72 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-value/options.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-value/options.json @@ -3,7 +3,7 @@ [ "moduleAttributes", { - "version": "feb-2020" + "version": "apr-2020" } ] ], diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/options.json index 319126fb3325..3fb83668e5d9 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/options.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/options.json @@ -3,9 +3,9 @@ [ "moduleAttributes", { - "version": "feb-2020" + "version": "apr-2020" } ] ], "sourceType": "module" -} \ No newline at end of file +} diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/options.json index 319126fb3325..3fb83668e5d9 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/options.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/options.json @@ -3,9 +3,9 @@ [ "moduleAttributes", { - "version": "feb-2020" + "version": "apr-2020" } ] ], "sourceType": "module" -} \ No newline at end of file +} diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/options.json index 442f499887f3..3fb83668e5d9 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/options.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/options.json @@ -3,7 +3,7 @@ [ "moduleAttributes", { - "version": "feb-2020" + "version": "apr-2020" } ] ], diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-without-attributes/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-without-attributes/options.json index 442f499887f3..3fb83668e5d9 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-without-attributes/options.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-without-attributes/options.json @@ -3,7 +3,7 @@ [ "moduleAttributes", { - "version": "feb-2020" + "version": "apr-2020" } ] ], diff --git a/packages/babel-plugin-syntax-module-attributes/src/index.js b/packages/babel-plugin-syntax-module-attributes/src/index.js index e3bf31baaf0c..b0aa860d78b1 100644 --- a/packages/babel-plugin-syntax-module-attributes/src/index.js +++ b/packages/babel-plugin-syntax-module-attributes/src/index.js @@ -3,11 +3,11 @@ import { declare } from "@babel/helper-plugin-utils"; export default declare((api, { version }) => { api.assertVersion(7); - if (typeof version !== "string" || version !== "feb-2020") { + if (typeof version !== "string" || version !== "apr-2020") { throw new Error( "The 'moduleAttributes' plugin requires a 'version' option," + " representing the last proposal update. Currently, the" + - " only supported value is 'feb-2020'.", + " only supported value is 'apr-2020'.", ); } From 7a2f4f80e1e7831fbf8e58c4532ccb0e558c532f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Tue, 28 Apr 2020 23:21:07 +0200 Subject: [PATCH 50/63] Revert output.js --- .../dotall-regex/with-unicode-property-escape/output.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/babel-plugin-transform-dotall-regex/test/fixtures/dotall-regex/with-unicode-property-escape/output.js b/packages/babel-plugin-transform-dotall-regex/test/fixtures/dotall-regex/with-unicode-property-escape/output.js index a8ddf757d926..c00aa15a9c1f 100644 --- a/packages/babel-plugin-transform-dotall-regex/test/fixtures/dotall-regex/with-unicode-property-escape/output.js +++ b/packages/babel-plugin-transform-dotall-regex/test/fixtures/dotall-regex/with-unicode-property-escape/output.js @@ -1,2 +1,2 @@ -var a = /[\u3400-\u4DB5\u4E00-\u9FEF\uFA0E\uFA0F\uFA11\uFA13\uFA14\uFA1F\uFA21\uFA23\uFA24\uFA27-\uFA29\u{20000}-\u{2A6D6}\u{2A700}-\u{2B734}\u{2B740}-\u{2B81D}\u{2B820}-\u{2CEA1}\u{2CEB0}-\u{2EBE0}][\0-\t\x0B\f\x0E-\u2027\u202A-\u{10FFFF}]/u; -var b = /[\u3400-\u4DB5\u4E00-\u9FEF\uFA0E\uFA0F\uFA11\uFA13\uFA14\uFA1F\uFA21\uFA23\uFA24\uFA27-\uFA29\u{20000}-\u{2A6D6}\u{2A700}-\u{2B734}\u{2B740}-\u{2B81D}\u{2B820}-\u{2CEA1}\u{2CEB0}-\u{2EBE0}][\0-\u{10FFFF}]/u; +var a = /[\u3400-\u4DBF\u4E00-\u9FFC\uFA0E\uFA0F\uFA11\uFA13\uFA14\uFA1F\uFA21\uFA23\uFA24\uFA27-\uFA29\u{20000}-\u{2A6DD}\u{2A700}-\u{2B734}\u{2B740}-\u{2B81D}\u{2B820}-\u{2CEA1}\u{2CEB0}-\u{2EBE0}\u{30000}-\u{3134A}][\0-\t\x0B\f\x0E-\u2027\u202A-\u{10FFFF}]/u; +var b = /[\u3400-\u4DBF\u4E00-\u9FFC\uFA0E\uFA0F\uFA11\uFA13\uFA14\uFA1F\uFA21\uFA23\uFA24\uFA27-\uFA29\u{20000}-\u{2A6DD}\u{2A700}-\u{2B734}\u{2B740}-\u{2B81D}\u{2B820}-\u{2CEA1}\u{2CEB0}-\u{2EBE0}\u{30000}-\u{3134A}][\0-\u{10FFFF}]/u; From 69f153ef710270811ad6bef37386f68bdb298f23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Tue, 28 Apr 2020 23:21:20 +0200 Subject: [PATCH 51/63] Revert expression.js --- packages/babel-parser/src/parser/expression.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/babel-parser/src/parser/expression.js b/packages/babel-parser/src/parser/expression.js index 31a4bdbee7ed..06999c46f24b 100644 --- a/packages/babel-parser/src/parser/expression.js +++ b/packages/babel-parser/src/parser/expression.js @@ -1499,6 +1499,7 @@ export default class ExpressionParser extends LValParser { } // Parse an object literal, binding pattern, or record. + parseObj( close: TokenType, isPattern: boolean, From 522bdc6ebcd49634112eeb9c2115e123c9465a44 Mon Sep 17 00:00:00 2001 From: Vivek Nayyar Date: Thu, 30 Apr 2020 17:24:02 +0200 Subject: [PATCH 52/63] relaxed the argument condition on dynamic imports from 1 to 2 for proposal module attributes --- .../babel-parser/src/parser/expression.js | 9 +- packages/babel-parser/src/parser/location.js | 2 +- .../dynamic-import-with-valid-syntax/input.js | 1 + .../options.json | 11 + .../output.json | 226 ++++++++++++++++++ 5 files changed, 246 insertions(+), 3 deletions(-) create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/dynamic-import-with-valid-syntax/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/dynamic-import-with-valid-syntax/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/dynamic-import-with-valid-syntax/output.json diff --git a/packages/babel-parser/src/parser/expression.js b/packages/babel-parser/src/parser/expression.js index 91f49064c372..7d756c759782 100644 --- a/packages/babel-parser/src/parser/expression.js +++ b/packages/babel-parser/src/parser/expression.js @@ -758,8 +758,13 @@ export default class ExpressionParser extends LValParser { optional: boolean, ): N.Expression { if (node.callee.type === "Import") { - if (node.arguments.length !== 1) { - this.raise(node.start, Errors.ImportCallArity); + const maxArity = this.hasPlugin("moduleAttributes") ? 2 : 1; + if (node.arguments.length === 0 || node.arguments.length > maxArity) { + this.raise( + node.start, + Errors.ImportCallArity, + this.hasPlugin("moduleAttributes") ? "two arguments" : "one argument", + ); } else { const importArg = node.arguments[0]; if (importArg && importArg.type === "SpreadElement") { diff --git a/packages/babel-parser/src/parser/location.js b/packages/babel-parser/src/parser/location.js index a9eb9cecb24e..a96c38dbb649 100644 --- a/packages/babel-parser/src/parser/location.js +++ b/packages/babel-parser/src/parser/location.js @@ -68,7 +68,7 @@ export const Errors = Object.freeze({ IllegalReturn: "'return' outside of function", ImportCallArgumentTrailingComma: "Trailing comma is disallowed inside import(...) arguments", - ImportCallArity: "import() requires exactly one argument", + ImportCallArity: "import() requires exactly %0", ImportCallArityLtOne: "Dynamic imports require a parameter: import('a.js')", ImportCallNotNewExpression: "Cannot use new with import(...)", ImportCallSpreadArgument: "... is not allowed in import()", diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/dynamic-import-with-valid-syntax/input.js b/packages/babel-parser/test/fixtures/experimental/module-attributes/dynamic-import-with-valid-syntax/input.js new file mode 100644 index 000000000000..baa60fc43c5f --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/dynamic-import-with-valid-syntax/input.js @@ -0,0 +1 @@ +import("foo.json", { with: { type: "json" } }) diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/dynamic-import-with-valid-syntax/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/dynamic-import-with-valid-syntax/options.json new file mode 100644 index 000000000000..442f499887f3 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/dynamic-import-with-valid-syntax/options.json @@ -0,0 +1,11 @@ +{ + "plugins": [ + [ + "moduleAttributes", + { + "version": "feb-2020" + } + ] + ], + "sourceType": "module" +} diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/dynamic-import-with-valid-syntax/output.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/dynamic-import-with-valid-syntax/output.json new file mode 100644 index 000000000000..c754570e8fa1 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/dynamic-import-with-valid-syntax/output.json @@ -0,0 +1,226 @@ +{ + "type": "File", + "start": 0, + "end": 46, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 46 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 46, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 46 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 46, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 46 + } + }, + "expression": { + "type": "CallExpression", + "start": 0, + "end": 46, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 46 + } + }, + "callee": { + "type": "Import", + "start": 0, + "end": 6, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 6 + } + } + }, + "arguments": [ + { + "type": "StringLiteral", + "start": 7, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "extra": { + "rawValue": "foo.json", + "raw": "\"foo.json\"" + }, + "value": "foo.json" + }, + { + "type": "ObjectExpression", + "start": 19, + "end": 45, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 45 + } + }, + "properties": [ + { + "type": "ObjectProperty", + "start": 21, + "end": 43, + "loc": { + "start": { + "line": 1, + "column": 21 + }, + "end": { + "line": 1, + "column": 43 + } + }, + "method": false, + "key": { + "type": "Identifier", + "start": 21, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 21 + }, + "end": { + "line": 1, + "column": 25 + }, + "identifierName": "with" + }, + "name": "with" + }, + "computed": false, + "shorthand": false, + "value": { + "type": "ObjectExpression", + "start": 27, + "end": 43, + "loc": { + "start": { + "line": 1, + "column": 27 + }, + "end": { + "line": 1, + "column": 43 + } + }, + "properties": [ + { + "type": "ObjectProperty", + "start": 29, + "end": 41, + "loc": { + "start": { + "line": 1, + "column": 29 + }, + "end": { + "line": 1, + "column": 41 + } + }, + "method": false, + "key": { + "type": "Identifier", + "start": 29, + "end": 33, + "loc": { + "start": { + "line": 1, + "column": 29 + }, + "end": { + "line": 1, + "column": 33 + }, + "identifierName": "type" + }, + "name": "type" + }, + "computed": false, + "shorthand": false, + "value": { + "type": "StringLiteral", + "start": 35, + "end": 41, + "loc": { + "start": { + "line": 1, + "column": 35 + }, + "end": { + "line": 1, + "column": 41 + } + }, + "extra": { + "rawValue": "json", + "raw": "\"json\"" + }, + "value": "json" + } + } + ] + } + } + ] + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file From 2e243af204c4a166eb69bf5ea3c1b9f47c2adf63 Mon Sep 17 00:00:00 2001 From: Vivek Nayyar Date: Thu, 30 Apr 2020 21:08:58 +0200 Subject: [PATCH 53/63] bug: fixed the version to apr-2020 --- packages/babel-parser/src/plugin-utils.js | 2 +- .../valid-syntax-with-invalid-plugin-option/options.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/babel-parser/src/plugin-utils.js b/packages/babel-parser/src/plugin-utils.js index e281ae758d7a..cba0c3b5c84e 100644 --- a/packages/babel-parser/src/plugin-utils.js +++ b/packages/babel-parser/src/plugin-utils.js @@ -96,7 +96,7 @@ export function validatePlugins(plugins: PluginList) { throw new Error( "The 'moduleAttributes' plugin requires a 'version' option," + " representing the last proposal update. Currently, the" + - " only supported value is 'feb-2020'.", + " only supported value is 'apr-2020'.", ); } } diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-plugin-option/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-plugin-option/options.json index 29907e14ccd5..f6cd7a594d5c 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-plugin-option/options.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-plugin-option/options.json @@ -8,5 +8,5 @@ ] ], "sourceType": "module", - "throws": "The 'moduleAttributes' plugin requires a 'version' option, representing the last proposal update. Currently, the only supported value is 'feb-2020'." + "throws": "The 'moduleAttributes' plugin requires a 'version' option, representing the last proposal update. Currently, the only supported value is 'apr-2020'." } \ No newline at end of file From f25093767ad458dda6ceff1f0ad424443458ed97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Wed, 20 May 2020 09:39:06 -0400 Subject: [PATCH 54/63] polish: tune importArity error message --- .../babel-parser/src/parser/expression.js | 4 +- .../incorrect-arity/input.js | 2 + .../incorrect-arity/options.json | 11 ++ .../incorrect-arity/output.json | 107 ++++++++++++++++++ 4 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/incorrect-arity/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/incorrect-arity/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/incorrect-arity/output.json diff --git a/packages/babel-parser/src/parser/expression.js b/packages/babel-parser/src/parser/expression.js index 1784e94e3b66..66f67c5b7a7c 100644 --- a/packages/babel-parser/src/parser/expression.js +++ b/packages/babel-parser/src/parser/expression.js @@ -770,7 +770,9 @@ export default class ExpressionParser extends LValParser { this.raise( node.start, Errors.ImportCallArity, - this.hasPlugin("moduleAttributes") ? "two arguments" : "one argument", + this.hasPlugin("moduleAttributes") + ? "one or two arguments" + : "one argument", ); } else { const importArg = node.arguments[0]; diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/incorrect-arity/input.js b/packages/babel-parser/test/fixtures/experimental/module-attributes/incorrect-arity/input.js new file mode 100644 index 000000000000..7f6df49300e8 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/incorrect-arity/input.js @@ -0,0 +1,2 @@ +import(); +import("./foo.json", { with: { type: "json"} }, "unsupported"); diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/incorrect-arity/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/incorrect-arity/options.json new file mode 100644 index 000000000000..3fb83668e5d9 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/incorrect-arity/options.json @@ -0,0 +1,11 @@ +{ + "plugins": [ + [ + "moduleAttributes", + { + "version": "apr-2020" + } + ] + ], + "sourceType": "module" +} diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/incorrect-arity/output.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/incorrect-arity/output.json new file mode 100644 index 000000000000..fbc2d14ad598 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/incorrect-arity/output.json @@ -0,0 +1,107 @@ +{ + "type": "File", + "start":0,"end":73,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":63}}, + "errors": [ + "SyntaxError: import() requires exactly one or two arguments (1:0)", + "SyntaxError: import() requires exactly one or two arguments (2:0)" + ], + "program": { + "type": "Program", + "start":0,"end":73,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":63}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":9,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":9}}, + "expression": { + "type": "CallExpression", + "start":0,"end":8,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":8}}, + "callee": { + "type": "Import", + "start":0,"end":6,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":6}} + }, + "arguments": [] + } + }, + { + "type": "ExpressionStatement", + "start":10,"end":73,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":63}}, + "expression": { + "type": "CallExpression", + "start":10,"end":72,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":62}}, + "callee": { + "type": "Import", + "start":10,"end":16,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":6}} + }, + "arguments": [ + { + "type": "StringLiteral", + "start":17,"end":29,"loc":{"start":{"line":2,"column":7},"end":{"line":2,"column":19}}, + "extra": { + "rawValue": "./foo.json", + "raw": "\"./foo.json\"" + }, + "value": "./foo.json" + }, + { + "type": "ObjectExpression", + "start":31,"end":56,"loc":{"start":{"line":2,"column":21},"end":{"line":2,"column":46}}, + "properties": [ + { + "type": "ObjectProperty", + "start":33,"end":54,"loc":{"start":{"line":2,"column":23},"end":{"line":2,"column":44}}, + "method": false, + "key": { + "type": "Identifier", + "start":33,"end":37,"loc":{"start":{"line":2,"column":23},"end":{"line":2,"column":27},"identifierName":"with"}, + "name": "with" + }, + "computed": false, + "shorthand": false, + "value": { + "type": "ObjectExpression", + "start":39,"end":54,"loc":{"start":{"line":2,"column":29},"end":{"line":2,"column":44}}, + "properties": [ + { + "type": "ObjectProperty", + "start":41,"end":53,"loc":{"start":{"line":2,"column":31},"end":{"line":2,"column":43}}, + "method": false, + "key": { + "type": "Identifier", + "start":41,"end":45,"loc":{"start":{"line":2,"column":31},"end":{"line":2,"column":35},"identifierName":"type"}, + "name": "type" + }, + "computed": false, + "shorthand": false, + "value": { + "type": "StringLiteral", + "start":47,"end":53,"loc":{"start":{"line":2,"column":37},"end":{"line":2,"column":43}}, + "extra": { + "rawValue": "json", + "raw": "\"json\"" + }, + "value": "json" + } + } + ] + } + } + ] + }, + { + "type": "StringLiteral", + "start":58,"end":71,"loc":{"start":{"line":2,"column":48},"end":{"line":2,"column":61}}, + "extra": { + "rawValue": "unsupported", + "raw": "\"unsupported\"" + }, + "value": "unsupported" + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file From dbf36f6a84ac0c0aa2e04512845c18154e50a9db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Wed, 20 May 2020 09:40:43 -0400 Subject: [PATCH 55/63] fix: allow trailing comma in import() --- .../babel-parser/src/parser/expression.js | 2 +- .../module-attributes/trailing-comma/input.js | 2 + .../trailing-comma/options.json | 11 ++ .../trailing-comma/output.json | 110 ++++++++++++++++++ 4 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/trailing-comma/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/trailing-comma/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/trailing-comma/output.json diff --git a/packages/babel-parser/src/parser/expression.js b/packages/babel-parser/src/parser/expression.js index 66f67c5b7a7c..0838189c525f 100644 --- a/packages/babel-parser/src/parser/expression.js +++ b/packages/babel-parser/src/parser/expression.js @@ -806,7 +806,7 @@ export default class ExpressionParser extends LValParser { } else { this.expect(tt.comma); if (this.match(close)) { - if (dynamicImport) { + if (dynamicImport && !this.hasPlugin("moduleAttributes")) { this.raise( this.state.lastTokStart, Errors.ImportCallArgumentTrailingComma, diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/trailing-comma/input.js b/packages/babel-parser/test/fixtures/experimental/module-attributes/trailing-comma/input.js new file mode 100644 index 000000000000..0be461789922 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/trailing-comma/input.js @@ -0,0 +1,2 @@ +import("foo.js",); +import("foo.json", { with: { type: "json" } },); diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/trailing-comma/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/trailing-comma/options.json new file mode 100644 index 000000000000..3fb83668e5d9 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/trailing-comma/options.json @@ -0,0 +1,11 @@ +{ + "plugins": [ + [ + "moduleAttributes", + { + "version": "apr-2020" + } + ] + ], + "sourceType": "module" +} diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/trailing-comma/output.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/trailing-comma/output.json new file mode 100644 index 000000000000..599f4fe5488d --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/trailing-comma/output.json @@ -0,0 +1,110 @@ +{ + "type": "File", + "start":0,"end":67,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":48}}, + "program": { + "type": "Program", + "start":0,"end":67,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":48}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":18,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":18}}, + "expression": { + "type": "CallExpression", + "start":0,"end":17,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":17}}, + "callee": { + "type": "Import", + "start":0,"end":6,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":6}} + }, + "extra": { + "trailingComma": 15 + }, + "arguments": [ + { + "type": "StringLiteral", + "start":7,"end":15,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":15}}, + "extra": { + "rawValue": "foo.js", + "raw": "\"foo.js\"" + }, + "value": "foo.js" + } + ] + } + }, + { + "type": "ExpressionStatement", + "start":19,"end":67,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":48}}, + "expression": { + "type": "CallExpression", + "start":19,"end":66,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":47}}, + "callee": { + "type": "Import", + "start":19,"end":25,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":6}} + }, + "extra": { + "trailingComma": 64 + }, + "arguments": [ + { + "type": "StringLiteral", + "start":26,"end":36,"loc":{"start":{"line":2,"column":7},"end":{"line":2,"column":17}}, + "extra": { + "rawValue": "foo.json", + "raw": "\"foo.json\"" + }, + "value": "foo.json" + }, + { + "type": "ObjectExpression", + "start":38,"end":64,"loc":{"start":{"line":2,"column":19},"end":{"line":2,"column":45}}, + "properties": [ + { + "type": "ObjectProperty", + "start":40,"end":62,"loc":{"start":{"line":2,"column":21},"end":{"line":2,"column":43}}, + "method": false, + "key": { + "type": "Identifier", + "start":40,"end":44,"loc":{"start":{"line":2,"column":21},"end":{"line":2,"column":25},"identifierName":"with"}, + "name": "with" + }, + "computed": false, + "shorthand": false, + "value": { + "type": "ObjectExpression", + "start":46,"end":62,"loc":{"start":{"line":2,"column":27},"end":{"line":2,"column":43}}, + "properties": [ + { + "type": "ObjectProperty", + "start":48,"end":60,"loc":{"start":{"line":2,"column":29},"end":{"line":2,"column":41}}, + "method": false, + "key": { + "type": "Identifier", + "start":48,"end":52,"loc":{"start":{"line":2,"column":29},"end":{"line":2,"column":33},"identifierName":"type"}, + "name": "type" + }, + "computed": false, + "shorthand": false, + "value": { + "type": "StringLiteral", + "start":54,"end":60,"loc":{"start":{"line":2,"column":35},"end":{"line":2,"column":41}}, + "extra": { + "rawValue": "json", + "raw": "\"json\"" + }, + "value": "json" + } + } + ] + } + } + ] + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file From 0ce6e62671dc6292e0e387a5b63cb8117e2d5178 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Wed, 20 May 2020 11:05:22 -0400 Subject: [PATCH 56/63] chore: update version to latest month --- packages/babel-parser/src/plugin-utils.js | 4 ++-- .../dynamic-import-with-valid-syntax/options.json | 2 +- .../module-attributes/incorrect-arity/options.json | 2 +- .../options.json | 4 ++-- .../invalid-syntax-with-no-attributes-identifier/options.json | 2 +- .../module-attributes/trailing-comma/options.json | 2 +- .../valid-syntax-with-attributes-and-value/options.json | 2 +- .../valid-syntax-with-attributes/options.json | 2 +- .../valid-syntax-with-invalid-plugin-option/options.json | 4 ++-- .../valid-syntax-with-invalid-value/options.json | 4 ++-- .../valid-syntax-with-no-type-attribute/options.json | 2 +- .../valid-syntax-with-object-method-attribute/options.json | 2 +- .../valid-syntax-with-repeated-type/options.json | 2 +- .../valid-syntax-without-attributes/options.json | 2 +- 14 files changed, 18 insertions(+), 18 deletions(-) diff --git a/packages/babel-parser/src/plugin-utils.js b/packages/babel-parser/src/plugin-utils.js index cba0c3b5c84e..f87f37fe3449 100644 --- a/packages/babel-parser/src/plugin-utils.js +++ b/packages/babel-parser/src/plugin-utils.js @@ -92,11 +92,11 @@ export function validatePlugins(plugins: PluginList) { "moduleAttributes", "version", ); - if (moduleAttributesVerionPluginOption !== "apr-2020") { + if (moduleAttributesVerionPluginOption !== "may-2020") { throw new Error( "The 'moduleAttributes' plugin requires a 'version' option," + " representing the last proposal update. Currently, the" + - " only supported value is 'apr-2020'.", + " only supported value is 'may-2020'.", ); } } diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/dynamic-import-with-valid-syntax/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/dynamic-import-with-valid-syntax/options.json index 3fb83668e5d9..14244aa04de3 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/dynamic-import-with-valid-syntax/options.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/dynamic-import-with-valid-syntax/options.json @@ -3,7 +3,7 @@ [ "moduleAttributes", { - "version": "apr-2020" + "version": "may-2020" } ] ], diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/incorrect-arity/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/incorrect-arity/options.json index 3fb83668e5d9..14244aa04de3 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/incorrect-arity/options.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/incorrect-arity/options.json @@ -3,7 +3,7 @@ [ "moduleAttributes", { - "version": "apr-2020" + "version": "may-2020" } ] ], diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-attributes-multiple-lines/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-attributes-multiple-lines/options.json index 4fca6665a7e4..c66af74f9371 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-attributes-multiple-lines/options.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-attributes-multiple-lines/options.json @@ -3,10 +3,10 @@ [ "moduleAttributes", { - "version": "apr-2020" + "version": "may-2020" } ] ], "sourceType": "module", "throws": "Unexpected token, expected \"(\" (3:0)" -} \ No newline at end of file +} diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-no-attributes-identifier/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-no-attributes-identifier/options.json index ed78a6ddc602..54d338484c57 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-no-attributes-identifier/options.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-no-attributes-identifier/options.json @@ -3,7 +3,7 @@ [ "moduleAttributes", { - "version": "apr-2020" + "version": "may-2020" } ], "estree" diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/trailing-comma/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/trailing-comma/options.json index 3fb83668e5d9..14244aa04de3 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/trailing-comma/options.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/trailing-comma/options.json @@ -3,7 +3,7 @@ [ "moduleAttributes", { - "version": "apr-2020" + "version": "may-2020" } ] ], diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-and-value/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-and-value/options.json index 3fb83668e5d9..14244aa04de3 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-and-value/options.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-and-value/options.json @@ -3,7 +3,7 @@ [ "moduleAttributes", { - "version": "apr-2020" + "version": "may-2020" } ] ], diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes/options.json index 3fb83668e5d9..14244aa04de3 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes/options.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes/options.json @@ -3,7 +3,7 @@ [ "moduleAttributes", { - "version": "apr-2020" + "version": "may-2020" } ] ], diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-plugin-option/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-plugin-option/options.json index f6cd7a594d5c..5cfe6ac69c2e 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-plugin-option/options.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-plugin-option/options.json @@ -8,5 +8,5 @@ ] ], "sourceType": "module", - "throws": "The 'moduleAttributes' plugin requires a 'version' option, representing the last proposal update. Currently, the only supported value is 'apr-2020'." -} \ No newline at end of file + "throws": "The 'moduleAttributes' plugin requires a 'version' option, representing the last proposal update. Currently, the only supported value is 'may-2020'." +} diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-value/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-value/options.json index 629cfd198671..59c7acf5410e 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-value/options.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-value/options.json @@ -3,10 +3,10 @@ [ "moduleAttributes", { - "version": "apr-2020" + "version": "may-2020" } ] ], "sourceType": "module", "throws": "Only string literals are allowed as module attribute values (1:52)" -} \ No newline at end of file +} diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/options.json index 3fb83668e5d9..14244aa04de3 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/options.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/options.json @@ -3,7 +3,7 @@ [ "moduleAttributes", { - "version": "apr-2020" + "version": "may-2020" } ] ], diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/options.json index 3fb83668e5d9..14244aa04de3 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/options.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/options.json @@ -3,7 +3,7 @@ [ "moduleAttributes", { - "version": "apr-2020" + "version": "may-2020" } ] ], diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/options.json index 3fb83668e5d9..14244aa04de3 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/options.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/options.json @@ -3,7 +3,7 @@ [ "moduleAttributes", { - "version": "apr-2020" + "version": "may-2020" } ] ], diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-without-attributes/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-without-attributes/options.json index 3fb83668e5d9..14244aa04de3 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-without-attributes/options.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-without-attributes/options.json @@ -3,7 +3,7 @@ [ "moduleAttributes", { - "version": "apr-2020" + "version": "may-2020" } ] ], From 9f80885f54628e59fdfa6e26e2cfe9ecb8bbe28d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Wed, 20 May 2020 11:17:18 -0400 Subject: [PATCH 57/63] add moduleAttributes to parse typings --- packages/babel-parser/typings/babel-parser.d.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/babel-parser/typings/babel-parser.d.ts b/packages/babel-parser/typings/babel-parser.d.ts index 40161080996b..99aac0c1efd7 100644 --- a/packages/babel-parser/typings/babel-parser.d.ts +++ b/packages/babel-parser/typings/babel-parser.d.ts @@ -114,6 +114,7 @@ export type ParserPlugin = 'importMeta' | 'jsx' | 'logicalAssignment' | + 'moduleAttributes' | 'nullishCoalescingOperator' | 'numericSeparator' | 'objectRestSpread' | From aa22f1a9df6dc4826736ba7537c33a084637049d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Wed, 20 May 2020 11:17:41 -0400 Subject: [PATCH 58/63] feat: add syntax-module-attributes to babel-standalone --- packages/babel-standalone/package.json | 1 + packages/babel-standalone/scripts/pluginConfig.json | 1 + packages/babel-standalone/src/generated/plugins.js | 3 +++ packages/babel-standalone/src/preset-stage-1.js | 5 +++++ 4 files changed, 10 insertions(+) diff --git a/packages/babel-standalone/package.json b/packages/babel-standalone/package.json index a95c4a5d0e24..b78155175010 100644 --- a/packages/babel-standalone/package.json +++ b/packages/babel-standalone/package.json @@ -44,6 +44,7 @@ "@babel/plugin-syntax-function-sent": "^7.8.3", "@babel/plugin-syntax-import-meta": "^7.8.3", "@babel/plugin-syntax-jsx": "^7.8.3", + "@babel/plugin-syntax-module-attributes": "^7.8.3", "@babel/plugin-syntax-object-rest-spread": "^7.8.0", "@babel/plugin-syntax-optional-catch-binding": "^7.8.0", "@babel/plugin-syntax-pipeline-operator": "^7.8.3", diff --git a/packages/babel-standalone/scripts/pluginConfig.json b/packages/babel-standalone/scripts/pluginConfig.json index 856d0869b5e5..9d54879e20c0 100644 --- a/packages/babel-standalone/scripts/pluginConfig.json +++ b/packages/babel-standalone/scripts/pluginConfig.json @@ -10,6 +10,7 @@ "syntax-function-sent", "syntax-import-meta", "syntax-jsx", + "syntax-module-attributes", "syntax-object-rest-spread", "syntax-optional-catch-binding", "syntax-pipeline-operator", diff --git a/packages/babel-standalone/src/generated/plugins.js b/packages/babel-standalone/src/generated/plugins.js index 0b55279003db..ffbe499a13d2 100644 --- a/packages/babel-standalone/src/generated/plugins.js +++ b/packages/babel-standalone/src/generated/plugins.js @@ -14,6 +14,7 @@ import syntaxFunctionBind from "@babel/plugin-syntax-function-bind"; import syntaxFunctionSent from "@babel/plugin-syntax-function-sent"; import syntaxImportMeta from "@babel/plugin-syntax-import-meta"; import syntaxJsx from "@babel/plugin-syntax-jsx"; +import syntaxModuleAttributes from "@babel/plugin-syntax-module-attributes"; import syntaxObjectRestSpread from "@babel/plugin-syntax-object-rest-spread"; import syntaxOptionalCatchBinding from "@babel/plugin-syntax-optional-catch-binding"; import syntaxPipelineOperator from "@babel/plugin-syntax-pipeline-operator"; @@ -103,6 +104,7 @@ export { syntaxFunctionSent, syntaxImportMeta, syntaxJsx, + syntaxModuleAttributes, syntaxObjectRestSpread, syntaxOptionalCatchBinding, syntaxPipelineOperator, @@ -193,6 +195,7 @@ export const all = { "syntax-function-sent": syntaxFunctionSent, "syntax-import-meta": syntaxImportMeta, "syntax-jsx": syntaxJsx, + "syntax-module-attributes": syntaxModuleAttributes, "syntax-object-rest-spread": syntaxObjectRestSpread, "syntax-optional-catch-binding": syntaxOptionalCatchBinding, "syntax-pipeline-operator": syntaxPipelineOperator, diff --git a/packages/babel-standalone/src/preset-stage-1.js b/packages/babel-standalone/src/preset-stage-1.js index 2d8aba73a2db..eaff6973106f 100644 --- a/packages/babel-standalone/src/preset-stage-1.js +++ b/packages/babel-standalone/src/preset-stage-1.js @@ -10,6 +10,7 @@ export default (_: any, opts: Object = {}) => { decoratorsBeforeExport, pipelineProposal = "minimal", recordAndTupleSyntax: recordAndTupleSyntax = "hash", + moduleAttributesVersion = "may-2020", } = opts; return { @@ -20,6 +21,10 @@ export default (_: any, opts: Object = {}) => { ], ], plugins: [ + [ + babelPlugins.syntaxModuleAttributes, + { version: moduleAttributesVersion }, + ], [babelPlugins.syntaxRecordAndTuple, { syntaxType: recordAndTupleSyntax }], babelPlugins.proposalExportDefaultFrom, [babelPlugins.proposalPipelineOperator, { proposal: pipelineProposal }], From 967a9bea88c3c428e375b89b9504c2d280dfd4db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Wed, 20 May 2020 11:45:41 -0400 Subject: [PATCH 59/63] fix: forbid spread element in second argument of import() --- .../babel-parser/src/parser/expression.js | 14 ++++-- packages/babel-parser/src/parser/statement.js | 1 - .../input.js | 1 + .../options.json | 11 +++++ .../output.json | 48 +++++++++++++++++++ 5 files changed, 71 insertions(+), 4 deletions(-) create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-spread-element-import-call/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-spread-element-import-call/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-spread-element-import-call/output.json diff --git a/packages/babel-parser/src/parser/expression.js b/packages/babel-parser/src/parser/expression.js index 0838189c525f..e6961230d919 100644 --- a/packages/babel-parser/src/parser/expression.js +++ b/packages/babel-parser/src/parser/expression.js @@ -766,6 +766,9 @@ export default class ExpressionParser extends LValParser { ): N.Expression { if (node.callee.type === "Import") { const maxArity = this.hasPlugin("moduleAttributes") ? 2 : 1; + if (node.arguments.length === 2) { + this.expectPlugin("moduleAttributes"); + } if (node.arguments.length === 0 || node.arguments.length > maxArity) { this.raise( node.start, @@ -775,9 +778,14 @@ export default class ExpressionParser extends LValParser { : "one argument", ); } else { - const importArg = node.arguments[0]; - if (importArg && importArg.type === "SpreadElement") { - this.raise(importArg.start, Errors.ImportCallSpreadArgument); + const importArgSpreadElement = node.arguments.find( + arg => arg.type === "SpreadElement", + ); + if (importArgSpreadElement) { + this.raise( + importArgSpreadElement.start, + Errors.ImportCallSpreadArgument, + ); } } } diff --git a/packages/babel-parser/src/parser/statement.js b/packages/babel-parser/src/parser/statement.js index 8049f38bb8f9..8407d35dd5f6 100644 --- a/packages/babel-parser/src/parser/statement.js +++ b/packages/babel-parser/src/parser/statement.js @@ -2060,7 +2060,6 @@ export default class StatementParser extends ExpressionParser { this.expectContextual("from"); } node.source = this.parseImportSource(); - // new proposal // https://github.com/tc39/proposal-module-attributes // parse module attributes if the next token is `with` or ignore and finish the ImportDeclaration node. const attributes = this.maybeParseModuleAttributes(); diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-spread-element-import-call/input.js b/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-spread-element-import-call/input.js new file mode 100644 index 000000000000..0a9420c378f9 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-spread-element-import-call/input.js @@ -0,0 +1 @@ +import("./foo.json", ...[]); diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-spread-element-import-call/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-spread-element-import-call/options.json new file mode 100644 index 000000000000..14244aa04de3 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-spread-element-import-call/options.json @@ -0,0 +1,11 @@ +{ + "plugins": [ + [ + "moduleAttributes", + { + "version": "may-2020" + } + ] + ], + "sourceType": "module" +} diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-spread-element-import-call/output.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-spread-element-import-call/output.json new file mode 100644 index 000000000000..f016f97336e9 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-spread-element-import-call/output.json @@ -0,0 +1,48 @@ +{ + "type": "File", + "start":0,"end":28,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":28}}, + "errors": [ + "SyntaxError: ... is not allowed in import() (1:21)" + ], + "program": { + "type": "Program", + "start":0,"end":28,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":28}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":28,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":28}}, + "expression": { + "type": "CallExpression", + "start":0,"end":27,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":27}}, + "callee": { + "type": "Import", + "start":0,"end":6,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":6}} + }, + "arguments": [ + { + "type": "StringLiteral", + "start":7,"end":19,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":19}}, + "extra": { + "rawValue": "./foo.json", + "raw": "\"./foo.json\"" + }, + "value": "./foo.json" + }, + { + "type": "SpreadElement", + "start":21,"end":26,"loc":{"start":{"line":1,"column":21},"end":{"line":1,"column":26}}, + "argument": { + "type": "ArrayExpression", + "start":24,"end":26,"loc":{"start":{"line":1,"column":24},"end":{"line":1,"column":26}}, + "elements": [] + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file From d06d08b460f2df074534879e0d113120316e12f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Wed, 20 May 2020 11:49:01 -0400 Subject: [PATCH 60/63] expect moduleAttributes when there are two arguments of import() --- .../dynamic-import/multiple-args/input.js | 2 +- .../dynamic-import/multiple-args/output.json | 17 +++++++++++++---- .../module-attributes-dynamic/input.js | 1 + .../module-attributes-dynamic/options.json | 5 +++++ 4 files changed, 20 insertions(+), 5 deletions(-) create mode 100644 packages/babel-parser/test/fixtures/experimental/_no-plugin/module-attributes-dynamic/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/_no-plugin/module-attributes-dynamic/options.json diff --git a/packages/babel-parser/test/fixtures/es2020/dynamic-import/multiple-args/input.js b/packages/babel-parser/test/fixtures/es2020/dynamic-import/multiple-args/input.js index 0f1447dbe608..94e059c54bac 100644 --- a/packages/babel-parser/test/fixtures/es2020/dynamic-import/multiple-args/input.js +++ b/packages/babel-parser/test/fixtures/es2020/dynamic-import/multiple-args/input.js @@ -1 +1 @@ -import('hello', 'world'); +import('hello', 'world', '!'); diff --git a/packages/babel-parser/test/fixtures/es2020/dynamic-import/multiple-args/output.json b/packages/babel-parser/test/fixtures/es2020/dynamic-import/multiple-args/output.json index 46409dca7a6b..0c67a4f67b22 100644 --- a/packages/babel-parser/test/fixtures/es2020/dynamic-import/multiple-args/output.json +++ b/packages/babel-parser/test/fixtures/es2020/dynamic-import/multiple-args/output.json @@ -1,21 +1,21 @@ { "type": "File", - "start":0,"end":25,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":25}}, + "start":0,"end":30,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":30}}, "errors": [ "SyntaxError: import() requires exactly one argument (1:0)" ], "program": { "type": "Program", - "start":0,"end":25,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":25}}, + "start":0,"end":30,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":30}}, "sourceType": "script", "interpreter": null, "body": [ { "type": "ExpressionStatement", - "start":0,"end":25,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":25}}, + "start":0,"end":30,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":30}}, "expression": { "type": "CallExpression", - "start":0,"end":24,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":24}}, + "start":0,"end":29,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":29}}, "callee": { "type": "Import", "start":0,"end":6,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":6}} @@ -38,6 +38,15 @@ "raw": "'world'" }, "value": "world" + }, + { + "type": "StringLiteral", + "start":25,"end":28,"loc":{"start":{"line":1,"column":25},"end":{"line":1,"column":28}}, + "extra": { + "rawValue": "!", + "raw": "'!'" + }, + "value": "!" } ] } diff --git a/packages/babel-parser/test/fixtures/experimental/_no-plugin/module-attributes-dynamic/input.js b/packages/babel-parser/test/fixtures/experimental/_no-plugin/module-attributes-dynamic/input.js new file mode 100644 index 000000000000..0f1447dbe608 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/_no-plugin/module-attributes-dynamic/input.js @@ -0,0 +1 @@ +import('hello', 'world'); diff --git a/packages/babel-parser/test/fixtures/experimental/_no-plugin/module-attributes-dynamic/options.json b/packages/babel-parser/test/fixtures/experimental/_no-plugin/module-attributes-dynamic/options.json new file mode 100644 index 000000000000..6b7622b8eeb2 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/_no-plugin/module-attributes-dynamic/options.json @@ -0,0 +1,5 @@ +{ + "throws": "This experimental syntax requires enabling the parser plugin: 'moduleAttributes' (1:24)", + "sourceType": "module", + "plugins": [] +} \ No newline at end of file From f051fa1bcff159b0eb6318d9cc8280efff73361f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Wed, 20 May 2020 11:58:26 -0400 Subject: [PATCH 61/63] refactor: minor tweaks --- packages/babel-parser/src/parser/expression.js | 3 +-- packages/babel-parser/src/parser/statement.js | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/babel-parser/src/parser/expression.js b/packages/babel-parser/src/parser/expression.js index e6961230d919..f4c7bfd25123 100644 --- a/packages/babel-parser/src/parser/expression.js +++ b/packages/babel-parser/src/parser/expression.js @@ -765,11 +765,10 @@ export default class ExpressionParser extends LValParser { optional: boolean, ): N.Expression { if (node.callee.type === "Import") { - const maxArity = this.hasPlugin("moduleAttributes") ? 2 : 1; if (node.arguments.length === 2) { this.expectPlugin("moduleAttributes"); } - if (node.arguments.length === 0 || node.arguments.length > maxArity) { + if (node.arguments.length === 0 || node.arguments.length > 2) { this.raise( node.start, Errors.ImportCallArity, diff --git a/packages/babel-parser/src/parser/statement.js b/packages/babel-parser/src/parser/statement.js index 8407d35dd5f6..eb97738b62fc 100644 --- a/packages/babel-parser/src/parser/statement.js +++ b/packages/babel-parser/src/parser/statement.js @@ -2097,7 +2097,7 @@ export default class StatementParser extends ExpressionParser { } maybeParseModuleAttributes() { - if (this.match(tt._with) && !this.isLineTerminator()) { + if (this.match(tt._with) && !this.hasPrecedingLineBreak()) { this.expectPlugin("moduleAttributes"); this.next(); } else { From dde629446279bd592d9234723e333229d5cd2b9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Wed, 20 May 2020 12:35:52 -0400 Subject: [PATCH 62/63] chore: rearrange tests --- .../es2015/regression/11183/output.json | 184 ++---------------- .../output.json | 171 ++-------------- .../input.js | 0 .../options.json | 0 .../output.json | 0 .../input.js | 3 +- .../options.json | 3 +- .../output.json | 47 +++++ .../input.js | 1 + .../options.json | 12 ++ .../options.json | 2 +- .../options.json | 2 +- .../anonymous-function-generator/output.json | 2 +- .../anonymous-function-generator/output.json | 2 +- 14 files changed, 93 insertions(+), 336 deletions(-) rename packages/babel-parser/test/fixtures/experimental/module-attributes/{invalid-syntax-with-no-attributes-identifier => import-with-statement}/input.js (100%) rename packages/babel-parser/test/fixtures/experimental/module-attributes/{invalid-syntax-with-no-attributes-identifier => import-with-statement}/options.json (100%) rename packages/babel-parser/test/fixtures/experimental/module-attributes/{invalid-syntax-with-no-attributes-identifier => import-with-statement}/output.json (100%) create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-attributes-multiple-lines/output.json create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-without-attributes-identifier/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-without-attributes-identifier/options.json diff --git a/packages/babel-parser/test/fixtures/es2015/regression/11183/output.json b/packages/babel-parser/test/fixtures/es2015/regression/11183/output.json index ea1e5e19d850..8f8a462425e7 100644 --- a/packages/babel-parser/test/fixtures/es2015/regression/11183/output.json +++ b/packages/babel-parser/test/fixtures/es2015/regression/11183/output.json @@ -1,111 +1,33 @@ { "type": "File", - "start": 0, - "end": 47, - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 5, - "column": 2 - } - }, + "start":0,"end":47,"loc":{"start":{"line":1,"column":0},"end":{"line":5,"column":2}}, "program": { "type": "Program", - "start": 0, - "end": 47, - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 5, - "column": 2 - } - }, + "start":0,"end":47,"loc":{"start":{"line":1,"column":0},"end":{"line":5,"column":2}}, "sourceType": "script", "interpreter": null, "body": [ { "type": "ClassDeclaration", - "start": 0, - "end": 10, - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 1, - "column": 10 - } - }, + "start":0,"end":10,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":10}}, "id": { "type": "Identifier", - "start": 6, - "end": 7, - "loc": { - "start": { - "line": 1, - "column": 6 - }, - "end": { - "line": 1, - "column": 7 - }, - "identifierName": "X" - }, + "start":6,"end":7,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":7},"identifierName":"X"}, "name": "X" }, "superClass": null, "body": { "type": "ClassBody", - "start": 8, - "end": 10, - "loc": { - "start": { - "line": 1, - "column": 8 - }, - "end": { - "line": 1, - "column": 10 - } - }, + "start":8,"end":10,"loc":{"start":{"line":1,"column":8},"end":{"line":1,"column":10}}, "body": [] } }, { "type": "ExpressionStatement", - "start": 11, - "end": 13, - "loc": { - "start": { - "line": 2, - "column": 0 - }, - "end": { - "line": 2, - "column": 2 - } - }, + "start":11,"end":13,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":2}}, "expression": { "type": "NumericLiteral", - "start": 11, - "end": 13, - "loc": { - "start": { - "line": 2, - "column": 0 - }, - "end": { - "line": 2, - "column": 2 - } - }, + "start":11,"end":13,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":2}}, "extra": { "rawValue": 5, "raw": "05" @@ -115,33 +37,10 @@ }, { "type": "FunctionDeclaration", - "start": 15, - "end": 44, - "loc": { - "start": { - "line": 4, - "column": 0 - }, - "end": { - "line": 4, - "column": 29 - } - }, + "start":15,"end":44,"loc":{"start":{"line":4,"column":0},"end":{"line":4,"column":29}}, "id": { "type": "Identifier", - "start": 24, - "end": 25, - "loc": { - "start": { - "line": 4, - "column": 9 - }, - "end": { - "line": 4, - "column": 10 - }, - "identifierName": "x" - }, + "start":24,"end":25,"loc":{"start":{"line":4,"column":9},"end":{"line":4,"column":10},"identifierName":"x"}, "name": "x" }, "generator": false, @@ -149,48 +48,15 @@ "params": [], "body": { "type": "BlockStatement", - "start": 28, - "end": 44, - "loc": { - "start": { - "line": 4, - "column": 13 - }, - "end": { - "line": 4, - "column": 29 - } - }, + "start":28,"end":44,"loc":{"start":{"line":4,"column":13},"end":{"line":4,"column":29}}, "body": [], "directives": [ { "type": "Directive", - "start": 30, - "end": 42, - "loc": { - "start": { - "line": 4, - "column": 15 - }, - "end": { - "line": 4, - "column": 27 - } - }, + "start":30,"end":42,"loc":{"start":{"line":4,"column":15},"end":{"line":4,"column":27}}, "value": { "type": "DirectiveLiteral", - "start": 30, - "end": 42, - "loc": { - "start": { - "line": 4, - "column": 15 - }, - "end": { - "line": 4, - "column": 27 - } - }, + "start":30,"end":42,"loc":{"start":{"line":4,"column":15},"end":{"line":4,"column":27}}, "value": "use strict", "extra": { "raw": "'use strict'", @@ -203,32 +69,10 @@ }, { "type": "ExpressionStatement", - "start": 45, - "end": 47, - "loc": { - "start": { - "line": 5, - "column": 0 - }, - "end": { - "line": 5, - "column": 2 - } - }, + "start":45,"end":47,"loc":{"start":{"line":5,"column":0},"end":{"line":5,"column":2}}, "expression": { "type": "NumericLiteral", - "start": 45, - "end": 47, - "loc": { - "start": { - "line": 5, - "column": 0 - }, - "end": { - "line": 5, - "column": 2 - } - }, + "start":45,"end":47,"loc":{"start":{"line":5,"column":0},"end":{"line":5,"column":2}}, "extra": { "rawValue": 5, "raw": "05" diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/dynamic-import-with-valid-syntax/output.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/dynamic-import-with-valid-syntax/output.json index c754570e8fa1..eef6b877df49 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/dynamic-import-with-valid-syntax/output.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/dynamic-import-with-valid-syntax/output.json @@ -1,92 +1,26 @@ { "type": "File", - "start": 0, - "end": 46, - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 1, - "column": 46 - } - }, + "start":0,"end":46,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":46}}, "program": { "type": "Program", - "start": 0, - "end": 46, - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 1, - "column": 46 - } - }, + "start":0,"end":46,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":46}}, "sourceType": "module", "interpreter": null, "body": [ { "type": "ExpressionStatement", - "start": 0, - "end": 46, - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 1, - "column": 46 - } - }, + "start":0,"end":46,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":46}}, "expression": { "type": "CallExpression", - "start": 0, - "end": 46, - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 1, - "column": 46 - } - }, + "start":0,"end":46,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":46}}, "callee": { "type": "Import", - "start": 0, - "end": 6, - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 1, - "column": 6 - } - } + "start":0,"end":6,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":6}} }, "arguments": [ { "type": "StringLiteral", - "start": 7, - "end": 17, - "loc": { - "start": { - "line": 1, - "column": 7 - }, - "end": { - "line": 1, - "column": 17 - } - }, + "start":7,"end":17,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":17}}, "extra": { "rawValue": "foo.json", "raw": "\"foo.json\"" @@ -95,116 +29,37 @@ }, { "type": "ObjectExpression", - "start": 19, - "end": 45, - "loc": { - "start": { - "line": 1, - "column": 19 - }, - "end": { - "line": 1, - "column": 45 - } - }, + "start":19,"end":45,"loc":{"start":{"line":1,"column":19},"end":{"line":1,"column":45}}, "properties": [ { "type": "ObjectProperty", - "start": 21, - "end": 43, - "loc": { - "start": { - "line": 1, - "column": 21 - }, - "end": { - "line": 1, - "column": 43 - } - }, + "start":21,"end":43,"loc":{"start":{"line":1,"column":21},"end":{"line":1,"column":43}}, "method": false, "key": { "type": "Identifier", - "start": 21, - "end": 25, - "loc": { - "start": { - "line": 1, - "column": 21 - }, - "end": { - "line": 1, - "column": 25 - }, - "identifierName": "with" - }, + "start":21,"end":25,"loc":{"start":{"line":1,"column":21},"end":{"line":1,"column":25},"identifierName":"with"}, "name": "with" }, "computed": false, "shorthand": false, "value": { "type": "ObjectExpression", - "start": 27, - "end": 43, - "loc": { - "start": { - "line": 1, - "column": 27 - }, - "end": { - "line": 1, - "column": 43 - } - }, + "start":27,"end":43,"loc":{"start":{"line":1,"column":27},"end":{"line":1,"column":43}}, "properties": [ { "type": "ObjectProperty", - "start": 29, - "end": 41, - "loc": { - "start": { - "line": 1, - "column": 29 - }, - "end": { - "line": 1, - "column": 41 - } - }, + "start":29,"end":41,"loc":{"start":{"line":1,"column":29},"end":{"line":1,"column":41}}, "method": false, "key": { "type": "Identifier", - "start": 29, - "end": 33, - "loc": { - "start": { - "line": 1, - "column": 29 - }, - "end": { - "line": 1, - "column": 33 - }, - "identifierName": "type" - }, + "start":29,"end":33,"loc":{"start":{"line":1,"column":29},"end":{"line":1,"column":33},"identifierName":"type"}, "name": "type" }, "computed": false, "shorthand": false, "value": { "type": "StringLiteral", - "start": 35, - "end": 41, - "loc": { - "start": { - "line": 1, - "column": 35 - }, - "end": { - "line": 1, - "column": 41 - } - }, + "start":35,"end":41,"loc":{"start":{"line":1,"column":35},"end":{"line":1,"column":41}}, "extra": { "rawValue": "json", "raw": "\"json\"" diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-no-attributes-identifier/input.js b/packages/babel-parser/test/fixtures/experimental/module-attributes/import-with-statement/input.js similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-no-attributes-identifier/input.js rename to packages/babel-parser/test/fixtures/experimental/module-attributes/import-with-statement/input.js diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-no-attributes-identifier/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/import-with-statement/options.json similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-no-attributes-identifier/options.json rename to packages/babel-parser/test/fixtures/experimental/module-attributes/import-with-statement/options.json diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-no-attributes-identifier/output.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/import-with-statement/output.json similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-no-attributes-identifier/output.json rename to packages/babel-parser/test/fixtures/experimental/module-attributes/import-with-statement/output.json diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-attributes-multiple-lines/input.js b/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-attributes-multiple-lines/input.js index 506d38a7ee01..4fb4f13bf6f9 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-attributes-multiple-lines/input.js +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-attributes-multiple-lines/input.js @@ -1,3 +1,2 @@ -import "x" -with +import "x" with type: "json" diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-attributes-multiple-lines/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-attributes-multiple-lines/options.json index c66af74f9371..14244aa04de3 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-attributes-multiple-lines/options.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-attributes-multiple-lines/options.json @@ -7,6 +7,5 @@ } ] ], - "sourceType": "module", - "throws": "Unexpected token, expected \"(\" (3:0)" + "sourceType": "module" } diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-attributes-multiple-lines/output.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-attributes-multiple-lines/output.json new file mode 100644 index 000000000000..9dcc39ab57b9 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-attributes-multiple-lines/output.json @@ -0,0 +1,47 @@ +{ + "type": "File", + "start":0,"end":28,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":12}}, + "program": { + "type": "Program", + "start":0,"end":28,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":12}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start":0,"end":28,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":12}}, + "specifiers": [], + "source": { + "type": "StringLiteral", + "start":7,"end":10,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":10}}, + "extra": { + "rawValue": "x", + "raw": "\"x\"" + }, + "value": "x" + }, + "attributes": [ + { + "type": "ImportAttribute", + "start":16,"end":28,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":12}}, + "key": { + "type": "Identifier", + "start":16,"end":20,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":4},"identifierName":"type"}, + "name": "type" + }, + "value": { + "type": "StringLiteral", + "start":22,"end":28,"loc":{"start":{"line":2,"column":6},"end":{"line":2,"column":12}}, + "extra": { + "rawValue": "json", + "raw": "\"json\"" + }, + "value": "json" + } + } + ] + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-without-attributes-identifier/input.js b/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-without-attributes-identifier/input.js new file mode 100644 index 000000000000..af54005dade7 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-without-attributes-identifier/input.js @@ -0,0 +1 @@ +import "x" with; diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-without-attributes-identifier/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-without-attributes-identifier/options.json new file mode 100644 index 000000000000..49737184d7fd --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-without-attributes-identifier/options.json @@ -0,0 +1,12 @@ +{ + "throws": "Unexpected token (1:15)", + "plugins": [ + [ + "moduleAttributes", + { + "version": "may-2020" + } + ] + ], + "sourceType": "module" +} diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-plugin-option/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-plugin-option/options.json index 5cfe6ac69c2e..a8be45900fa0 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-plugin-option/options.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-plugin-option/options.json @@ -9,4 +9,4 @@ ], "sourceType": "module", "throws": "The 'moduleAttributes' plugin requires a 'version' option, representing the last proposal update. Currently, the only supported value is 'may-2020'." -} +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-value/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-value/options.json index 59c7acf5410e..f21e649b0f1d 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-value/options.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-value/options.json @@ -9,4 +9,4 @@ ], "sourceType": "module", "throws": "Only string literals are allowed as module attribute values (1:52)" -} +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/flow/type-generics/anonymous-function-generator/output.json b/packages/babel-parser/test/fixtures/flow/type-generics/anonymous-function-generator/output.json index 064d5577f360..b02c4bbd98fa 100644 --- a/packages/babel-parser/test/fixtures/flow/type-generics/anonymous-function-generator/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-generics/anonymous-function-generator/output.json @@ -115,4 +115,4 @@ ], "directives": [] } -} +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/tsx/anonymous-function-generator/output.json b/packages/babel-parser/test/fixtures/typescript/tsx/anonymous-function-generator/output.json index 937ca57900ca..b48b432ce19a 100644 --- a/packages/babel-parser/test/fixtures/typescript/tsx/anonymous-function-generator/output.json +++ b/packages/babel-parser/test/fixtures/typescript/tsx/anonymous-function-generator/output.json @@ -112,4 +112,4 @@ ], "directives": [] } -} +} \ No newline at end of file From 907aacd689d2ce22396118b20496f33beccd64a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Thu, 21 May 2020 13:04:35 -0400 Subject: [PATCH 63/63] Update packages/babel-parser/src/parser/expression.js MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Nicolò Ribaudo --- packages/babel-parser/src/parser/expression.js | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/packages/babel-parser/src/parser/expression.js b/packages/babel-parser/src/parser/expression.js index a12a3bf48cff..4bd8028e6227 100644 --- a/packages/babel-parser/src/parser/expression.js +++ b/packages/babel-parser/src/parser/expression.js @@ -777,14 +777,10 @@ export default class ExpressionParser extends LValParser { : "one argument", ); } else { - const importArgSpreadElement = node.arguments.find( - arg => arg.type === "SpreadElement", - ); - if (importArgSpreadElement) { - this.raise( - importArgSpreadElement.start, - Errors.ImportCallSpreadArgument, - ); + for (const arg of node.arguments) { + if (arg.type === "SpreadElement") { + this.raise(arg.start, Errors.ImportCallSpreadArgument); + } } } }