Merge branch 'master' into feat-optional-chaining

This commit is contained in:
Henry Zhu 2017-05-31 14:33:47 -04:00 committed by GitHub
commit 2dd624b44e
21 changed files with 1325 additions and 17 deletions

View File

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

View File

@ -1,6 +1,6 @@
{
"name": "babylon",
"version": "7.0.0-beta.11",
"version": "7.0.0-beta.12",
"description": "A JavaScript parser",
"author": "Sebastian McKenzie <sebmck@gmail.com>",
"homepage": "https://babeljs.io/",

View File

@ -459,6 +459,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();
@ -620,12 +624,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

@ -17,18 +17,24 @@ import State from "./state";
// The following character codes are forbidden from being
// an immediate sibling of NumericLiteralSeparator _
const forbiddenNumericLiteralSeparatorSiblings = [
46, // .
66, // B
69, // E
79, // O
88, // X
95, // _ (multiple separators are not allowed)
98, // b
101, // e
111, // o
120, // x
];
const forbiddenNumericSeparatorSiblings = {
decBinOct: [
46, // .
66, // B
69, // E
79, // O
95, // _ (multiple separators are not allowed)
98, // b
101, // e
111, // o
],
hex: [
46, // .
88, // X
95, // _ (multiple separators are not allowed)
120, // x
],
};
// Object type used to represent tokens. Note that normally, tokens
// simply exist as properties on the parser object. This is only
@ -579,6 +585,9 @@ export default class Tokenizer extends LocationParser {
readInt(radix: number, len?: number): number | null {
const start = this.state.pos;
const forbiddenSiblings = radix === 16 ?
forbiddenNumericSeparatorSiblings.hex :
forbiddenNumericSeparatorSiblings.decBinOct;
let total = 0;
for (let i = 0, e = len == null ? Infinity : len; i < e; ++i) {
@ -589,8 +598,8 @@ export default class Tokenizer extends LocationParser {
const prev = this.input.charCodeAt(this.state.pos - 1);
const next = this.input.charCodeAt(this.state.pos + 1);
if (code === 95) {
if ((forbiddenNumericLiteralSeparatorSiblings.indexOf(prev) > -1) ||
(forbiddenNumericLiteralSeparatorSiblings.indexOf(next) > -1) ||
if ((forbiddenSiblings.indexOf(prev) > -1) ||
(forbiddenSiblings.indexOf(next) > -1) ||
Number.isNaN(next)) {
this.raise(this.state.pos, "Invalid NumericLiteralSeparator");
}

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"]
}

View File

@ -0,0 +1 @@
_123

View File

@ -0,0 +1,66 @@
{
"type": "File",
"start": 0,
"end": 4,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 4
}
},
"program": {
"type": "Program",
"start": 0,
"end": 4,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 4
}
},
"sourceType": "script",
"body": [
{
"type": "ExpressionStatement",
"start": 0,
"end": 4,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 4
}
},
"expression": {
"type": "Identifier",
"start": 0,
"end": 4,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 4
},
"identifierName": "_123"
},
"name": "_123"
}
}
],
"directives": []
}
}

View File

@ -0,0 +1 @@
0xBE_be_EB_eb;

View File

@ -0,0 +1,69 @@
{
"type": "File",
"start": 0,
"end": 14,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 14
}
},
"program": {
"type": "Program",
"start": 0,
"end": 14,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 14
}
},
"sourceType": "script",
"body": [
{
"type": "ExpressionStatement",
"start": 0,
"end": 14,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 14
}
},
"expression": {
"type": "NumericLiteral",
"start": 0,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 13
}
},
"extra": {
"rawValue": 3200183275,
"raw": "0xBE_be_EB_eb"
},
"value": 3200183275
}
}
],
"directives": []
}
}