Add plugin for import.meta proposal (#544)

* Add plugin for import.meta proposal

Fixes https://github.com/babel/babylon/issues/539

* Tests for assignment/mutation of import.meta

* Use correct identifier in failure message

* Simpler & more consistent script errors for import.meta
This commit is contained in:
Jan Olaf Krems 2017-05-30 16:28:51 -07:00 committed by Henry Zhu
parent 2f5d146d54
commit d4e842d4eb
15 changed files with 1164 additions and 2 deletions

View File

@ -139,6 +139,7 @@ require("babylon").parse("code", {
- `functionSent`
- `dynamicImport` ([proposal](https://github.com/tc39/proposal-dynamic-import))
- `numericSeparator` ([proposal](https://github.com/samuelgoto/proposal-numeric-separator))
- `importMeta` ([proposal](https://github.com/tc39/proposal-import-meta))
### FAQ

View File

@ -427,6 +427,10 @@ export default class ExpressionParser extends LValParser {
return this.finishNode(node, "Super");
case tt._import:
if (this.hasPlugin("importMeta") && this.lookahead().type === tt.dot) {
return this.parseImportMetaProperty();
}
if (!this.hasPlugin("dynamicImport")) this.unexpected();
node = this.startNode();
@ -588,12 +592,22 @@ export default class ExpressionParser extends LValParser {
node.property = this.parseIdentifier(true);
if (node.property.name !== propertyName) {
this.raise(node.property.start, `The only valid meta property for new is ${meta.name}.${propertyName}`);
this.raise(node.property.start, `The only valid meta property for ${meta.name} is ${meta.name}.${propertyName}`);
}
return this.finishNode(node, "MetaProperty");
}
parseImportMetaProperty(): N.MetaProperty {
const node = this.startNode();
const id = this.parseIdentifier(true);
this.expect(tt.dot);
if (!this.inModule) {
this.raise(id.start, "import.meta may appear only with 'sourceType: module'");
}
return this.parseMetaProperty(node, id, "meta");
}
parseLiteral<T : N.Literal>(value: any, type: /*T["kind"]*/string, startPos?: number, startLoc?: Position): T {
startPos = startPos || this.state.start;
startLoc = startLoc || this.state.startLoc;

View File

@ -106,7 +106,8 @@ export default class StatementParser extends ExpressionParser {
case tt.semi: return this.parseEmptyStatement(node);
case tt._export:
case tt._import:
if (this.hasPlugin("dynamicImport") && this.lookahead().type === tt.parenL) break;
if ((this.hasPlugin("dynamicImport") && this.lookahead().type === tt.parenL) ||
(this.hasPlugin("importMeta") && this.lookahead().type === tt.dot)) break;
if (!this.options.allowImportExportEverywhere) {
if (!topLevel) {

View File

@ -0,0 +1 @@
const x = import.meta;

View File

@ -0,0 +1,5 @@
{
"throws": "import.meta may appear only with 'sourceType: module' (1:10)",
"plugins": ["dynamicImport", "importMeta"],
"sourceType": "script"
}

View File

@ -0,0 +1 @@
import.notMeta;

View File

@ -0,0 +1,5 @@
{
"throws": "The only valid meta property for import is import.meta (1:7)",
"sourceType": "module",
"plugins": ["dynamicImport", "importMeta"]
}

View File

@ -0,0 +1 @@
import.meta = true;

View File

@ -0,0 +1,5 @@
{
"throws": "Invalid left-hand side in assignment expression (1:0)",
"sourceType": "module",
"plugins": ["dynamicImport", "importMeta"]
}

View File

@ -0,0 +1,5 @@
const x = import.meta;
const url = import.meta.url;
import.meta;
import.meta.url;
import.meta.couldBeMutable = true;

View File

@ -0,0 +1,555 @@
{
"type": "File",
"start": 0,
"end": 116,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 5,
"column": 34
}
},
"program": {
"type": "Program",
"start": 0,
"end": 116,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 5,
"column": 34
}
},
"sourceType": "module",
"body": [
{
"type": "VariableDeclaration",
"start": 0,
"end": 22,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 22
}
},
"declarations": [
{
"type": "VariableDeclarator",
"start": 6,
"end": 21,
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 21
}
},
"id": {
"type": "Identifier",
"start": 6,
"end": 7,
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 7
},
"identifierName": "x"
},
"name": "x"
},
"init": {
"type": "MetaProperty",
"start": 10,
"end": 21,
"loc": {
"start": {
"line": 1,
"column": 10
},
"end": {
"line": 1,
"column": 21
}
},
"meta": {
"type": "Identifier",
"start": 10,
"end": 16,
"loc": {
"start": {
"line": 1,
"column": 10
},
"end": {
"line": 1,
"column": 16
},
"identifierName": "import"
},
"name": "import"
},
"property": {
"type": "Identifier",
"start": 17,
"end": 21,
"loc": {
"start": {
"line": 1,
"column": 17
},
"end": {
"line": 1,
"column": 21
},
"identifierName": "meta"
},
"name": "meta"
}
}
}
],
"kind": "const"
},
{
"type": "VariableDeclaration",
"start": 23,
"end": 51,
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 2,
"column": 28
}
},
"declarations": [
{
"type": "VariableDeclarator",
"start": 29,
"end": 50,
"loc": {
"start": {
"line": 2,
"column": 6
},
"end": {
"line": 2,
"column": 27
}
},
"id": {
"type": "Identifier",
"start": 29,
"end": 32,
"loc": {
"start": {
"line": 2,
"column": 6
},
"end": {
"line": 2,
"column": 9
},
"identifierName": "url"
},
"name": "url"
},
"init": {
"type": "MemberExpression",
"start": 35,
"end": 50,
"loc": {
"start": {
"line": 2,
"column": 12
},
"end": {
"line": 2,
"column": 27
}
},
"object": {
"type": "MetaProperty",
"start": 35,
"end": 46,
"loc": {
"start": {
"line": 2,
"column": 12
},
"end": {
"line": 2,
"column": 23
}
},
"meta": {
"type": "Identifier",
"start": 35,
"end": 41,
"loc": {
"start": {
"line": 2,
"column": 12
},
"end": {
"line": 2,
"column": 18
},
"identifierName": "import"
},
"name": "import"
},
"property": {
"type": "Identifier",
"start": 42,
"end": 46,
"loc": {
"start": {
"line": 2,
"column": 19
},
"end": {
"line": 2,
"column": 23
},
"identifierName": "meta"
},
"name": "meta"
}
},
"property": {
"type": "Identifier",
"start": 47,
"end": 50,
"loc": {
"start": {
"line": 2,
"column": 24
},
"end": {
"line": 2,
"column": 27
},
"identifierName": "url"
},
"name": "url"
},
"computed": false
}
}
],
"kind": "const"
},
{
"type": "ExpressionStatement",
"start": 52,
"end": 64,
"loc": {
"start": {
"line": 3,
"column": 0
},
"end": {
"line": 3,
"column": 12
}
},
"expression": {
"type": "MetaProperty",
"start": 52,
"end": 63,
"loc": {
"start": {
"line": 3,
"column": 0
},
"end": {
"line": 3,
"column": 11
}
},
"meta": {
"type": "Identifier",
"start": 52,
"end": 58,
"loc": {
"start": {
"line": 3,
"column": 0
},
"end": {
"line": 3,
"column": 6
},
"identifierName": "import"
},
"name": "import"
},
"property": {
"type": "Identifier",
"start": 59,
"end": 63,
"loc": {
"start": {
"line": 3,
"column": 7
},
"end": {
"line": 3,
"column": 11
},
"identifierName": "meta"
},
"name": "meta"
}
}
},
{
"type": "ExpressionStatement",
"start": 65,
"end": 81,
"loc": {
"start": {
"line": 4,
"column": 0
},
"end": {
"line": 4,
"column": 16
}
},
"expression": {
"type": "MemberExpression",
"start": 65,
"end": 80,
"loc": {
"start": {
"line": 4,
"column": 0
},
"end": {
"line": 4,
"column": 15
}
},
"object": {
"type": "MetaProperty",
"start": 65,
"end": 76,
"loc": {
"start": {
"line": 4,
"column": 0
},
"end": {
"line": 4,
"column": 11
}
},
"meta": {
"type": "Identifier",
"start": 65,
"end": 71,
"loc": {
"start": {
"line": 4,
"column": 0
},
"end": {
"line": 4,
"column": 6
},
"identifierName": "import"
},
"name": "import"
},
"property": {
"type": "Identifier",
"start": 72,
"end": 76,
"loc": {
"start": {
"line": 4,
"column": 7
},
"end": {
"line": 4,
"column": 11
},
"identifierName": "meta"
},
"name": "meta"
}
},
"property": {
"type": "Identifier",
"start": 77,
"end": 80,
"loc": {
"start": {
"line": 4,
"column": 12
},
"end": {
"line": 4,
"column": 15
},
"identifierName": "url"
},
"name": "url"
},
"computed": false
}
},
{
"type": "ExpressionStatement",
"start": 82,
"end": 116,
"loc": {
"start": {
"line": 5,
"column": 0
},
"end": {
"line": 5,
"column": 34
}
},
"expression": {
"type": "AssignmentExpression",
"start": 82,
"end": 115,
"loc": {
"start": {
"line": 5,
"column": 0
},
"end": {
"line": 5,
"column": 33
}
},
"operator": "=",
"left": {
"type": "MemberExpression",
"start": 82,
"end": 108,
"loc": {
"start": {
"line": 5,
"column": 0
},
"end": {
"line": 5,
"column": 26
}
},
"object": {
"type": "MetaProperty",
"start": 82,
"end": 93,
"loc": {
"start": {
"line": 5,
"column": 0
},
"end": {
"line": 5,
"column": 11
}
},
"meta": {
"type": "Identifier",
"start": 82,
"end": 88,
"loc": {
"start": {
"line": 5,
"column": 0
},
"end": {
"line": 5,
"column": 6
},
"identifierName": "import"
},
"name": "import"
},
"property": {
"type": "Identifier",
"start": 89,
"end": 93,
"loc": {
"start": {
"line": 5,
"column": 7
},
"end": {
"line": 5,
"column": 11
},
"identifierName": "meta"
},
"name": "meta"
}
},
"property": {
"type": "Identifier",
"start": 94,
"end": 108,
"loc": {
"start": {
"line": 5,
"column": 12
},
"end": {
"line": 5,
"column": 26
},
"identifierName": "couldBeMutable"
},
"name": "couldBeMutable"
},
"computed": false
},
"right": {
"type": "BooleanLiteral",
"start": 111,
"end": 115,
"loc": {
"start": {
"line": 5,
"column": 29
},
"end": {
"line": 5,
"column": 33
}
},
"value": true
}
}
}
],
"directives": []
}
}

View File

@ -0,0 +1,4 @@
{
"sourceType": "module",
"plugins": ["dynamicImport", "importMeta"]
}

View File

@ -0,0 +1,5 @@
const x = import.meta;
const url = import.meta.url;
import.meta;
import.meta.url;
import.meta.couldBeMutable = true;

View File

@ -0,0 +1,555 @@
{
"type": "File",
"start": 0,
"end": 116,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 5,
"column": 34
}
},
"program": {
"type": "Program",
"start": 0,
"end": 116,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 5,
"column": 34
}
},
"sourceType": "module",
"body": [
{
"type": "VariableDeclaration",
"start": 0,
"end": 22,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 22
}
},
"declarations": [
{
"type": "VariableDeclarator",
"start": 6,
"end": 21,
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 21
}
},
"id": {
"type": "Identifier",
"start": 6,
"end": 7,
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 7
},
"identifierName": "x"
},
"name": "x"
},
"init": {
"type": "MetaProperty",
"start": 10,
"end": 21,
"loc": {
"start": {
"line": 1,
"column": 10
},
"end": {
"line": 1,
"column": 21
}
},
"meta": {
"type": "Identifier",
"start": 10,
"end": 16,
"loc": {
"start": {
"line": 1,
"column": 10
},
"end": {
"line": 1,
"column": 16
},
"identifierName": "import"
},
"name": "import"
},
"property": {
"type": "Identifier",
"start": 17,
"end": 21,
"loc": {
"start": {
"line": 1,
"column": 17
},
"end": {
"line": 1,
"column": 21
},
"identifierName": "meta"
},
"name": "meta"
}
}
}
],
"kind": "const"
},
{
"type": "VariableDeclaration",
"start": 23,
"end": 51,
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 2,
"column": 28
}
},
"declarations": [
{
"type": "VariableDeclarator",
"start": 29,
"end": 50,
"loc": {
"start": {
"line": 2,
"column": 6
},
"end": {
"line": 2,
"column": 27
}
},
"id": {
"type": "Identifier",
"start": 29,
"end": 32,
"loc": {
"start": {
"line": 2,
"column": 6
},
"end": {
"line": 2,
"column": 9
},
"identifierName": "url"
},
"name": "url"
},
"init": {
"type": "MemberExpression",
"start": 35,
"end": 50,
"loc": {
"start": {
"line": 2,
"column": 12
},
"end": {
"line": 2,
"column": 27
}
},
"object": {
"type": "MetaProperty",
"start": 35,
"end": 46,
"loc": {
"start": {
"line": 2,
"column": 12
},
"end": {
"line": 2,
"column": 23
}
},
"meta": {
"type": "Identifier",
"start": 35,
"end": 41,
"loc": {
"start": {
"line": 2,
"column": 12
},
"end": {
"line": 2,
"column": 18
},
"identifierName": "import"
},
"name": "import"
},
"property": {
"type": "Identifier",
"start": 42,
"end": 46,
"loc": {
"start": {
"line": 2,
"column": 19
},
"end": {
"line": 2,
"column": 23
},
"identifierName": "meta"
},
"name": "meta"
}
},
"property": {
"type": "Identifier",
"start": 47,
"end": 50,
"loc": {
"start": {
"line": 2,
"column": 24
},
"end": {
"line": 2,
"column": 27
},
"identifierName": "url"
},
"name": "url"
},
"computed": false
}
}
],
"kind": "const"
},
{
"type": "ExpressionStatement",
"start": 52,
"end": 64,
"loc": {
"start": {
"line": 3,
"column": 0
},
"end": {
"line": 3,
"column": 12
}
},
"expression": {
"type": "MetaProperty",
"start": 52,
"end": 63,
"loc": {
"start": {
"line": 3,
"column": 0
},
"end": {
"line": 3,
"column": 11
}
},
"meta": {
"type": "Identifier",
"start": 52,
"end": 58,
"loc": {
"start": {
"line": 3,
"column": 0
},
"end": {
"line": 3,
"column": 6
},
"identifierName": "import"
},
"name": "import"
},
"property": {
"type": "Identifier",
"start": 59,
"end": 63,
"loc": {
"start": {
"line": 3,
"column": 7
},
"end": {
"line": 3,
"column": 11
},
"identifierName": "meta"
},
"name": "meta"
}
}
},
{
"type": "ExpressionStatement",
"start": 65,
"end": 81,
"loc": {
"start": {
"line": 4,
"column": 0
},
"end": {
"line": 4,
"column": 16
}
},
"expression": {
"type": "MemberExpression",
"start": 65,
"end": 80,
"loc": {
"start": {
"line": 4,
"column": 0
},
"end": {
"line": 4,
"column": 15
}
},
"object": {
"type": "MetaProperty",
"start": 65,
"end": 76,
"loc": {
"start": {
"line": 4,
"column": 0
},
"end": {
"line": 4,
"column": 11
}
},
"meta": {
"type": "Identifier",
"start": 65,
"end": 71,
"loc": {
"start": {
"line": 4,
"column": 0
},
"end": {
"line": 4,
"column": 6
},
"identifierName": "import"
},
"name": "import"
},
"property": {
"type": "Identifier",
"start": 72,
"end": 76,
"loc": {
"start": {
"line": 4,
"column": 7
},
"end": {
"line": 4,
"column": 11
},
"identifierName": "meta"
},
"name": "meta"
}
},
"property": {
"type": "Identifier",
"start": 77,
"end": 80,
"loc": {
"start": {
"line": 4,
"column": 12
},
"end": {
"line": 4,
"column": 15
},
"identifierName": "url"
},
"name": "url"
},
"computed": false
}
},
{
"type": "ExpressionStatement",
"start": 82,
"end": 116,
"loc": {
"start": {
"line": 5,
"column": 0
},
"end": {
"line": 5,
"column": 34
}
},
"expression": {
"type": "AssignmentExpression",
"start": 82,
"end": 115,
"loc": {
"start": {
"line": 5,
"column": 0
},
"end": {
"line": 5,
"column": 33
}
},
"operator": "=",
"left": {
"type": "MemberExpression",
"start": 82,
"end": 108,
"loc": {
"start": {
"line": 5,
"column": 0
},
"end": {
"line": 5,
"column": 26
}
},
"object": {
"type": "MetaProperty",
"start": 82,
"end": 93,
"loc": {
"start": {
"line": 5,
"column": 0
},
"end": {
"line": 5,
"column": 11
}
},
"meta": {
"type": "Identifier",
"start": 82,
"end": 88,
"loc": {
"start": {
"line": 5,
"column": 0
},
"end": {
"line": 5,
"column": 6
},
"identifierName": "import"
},
"name": "import"
},
"property": {
"type": "Identifier",
"start": 89,
"end": 93,
"loc": {
"start": {
"line": 5,
"column": 7
},
"end": {
"line": 5,
"column": 11
},
"identifierName": "meta"
},
"name": "meta"
}
},
"property": {
"type": "Identifier",
"start": 94,
"end": 108,
"loc": {
"start": {
"line": 5,
"column": 12
},
"end": {
"line": 5,
"column": 26
},
"identifierName": "couldBeMutable"
},
"name": "couldBeMutable"
},
"computed": false
},
"right": {
"type": "BooleanLiteral",
"start": 111,
"end": 115,
"loc": {
"start": {
"line": 5,
"column": 29
},
"end": {
"line": 5,
"column": 33
}
},
"value": true
}
}
}
],
"directives": []
}
}

View File

@ -0,0 +1,4 @@
{
"sourceType": "module",
"plugins": ["importMeta"]
}