Merge pull request #515 from babel/backport1

Backport #499, #510, #475, #479
This commit is contained in:
Daniel Tschinder 2017-05-10 18:03:58 +02:00 committed by GitHub
commit 8c885eaef6
21 changed files with 1403 additions and 7 deletions

View File

@ -382,7 +382,11 @@ pp.parseExprAtom = function (refShorthandDefaultPos) {
switch (this.state.type) { switch (this.state.type) {
case tt._super: case tt._super:
if (!this.state.inMethod && !this.options.allowSuperOutsideMethod) { if (
!this.state.inMethod &&
!this.state.inClassProperty &&
!this.options.allowSuperOutsideMethod
) {
this.raise(this.state.start, "'super' outside of function or class"); this.raise(this.state.start, "'super' outside of function or class");
} }
@ -880,8 +884,9 @@ pp.parseObjectProperty = function (prop, startPos, startLoc, isPattern, refShort
} }
if (!prop.computed && prop.key.type === "Identifier") { if (!prop.computed && prop.key.type === "Identifier") {
this.checkReservedWord(prop.key.name, prop.key.start, true, true);
if (isPattern) { if (isPattern) {
this.checkReservedWord(prop.key.name, prop.key.start, true, true);
prop.value = this.parseMaybeDefault(startPos, startLoc, prop.key.__clone()); prop.value = this.parseMaybeDefault(startPos, startLoc, prop.key.__clone());
} else if (this.match(tt.eq) && refShorthandDefaultPos) { } else if (this.match(tt.eq) && refShorthandDefaultPos) {
if (!refShorthandDefaultPos.start) { if (!refShorthandDefaultPos.start) {

View File

@ -783,6 +783,7 @@ pp.parseClassBody = function (node) {
}; };
pp.parseClassProperty = function (node) { pp.parseClassProperty = function (node) {
this.state.inClassProperty = true;
if (this.match(tt.eq)) { if (this.match(tt.eq)) {
if (!this.hasPlugin("classProperties")) this.unexpected(); if (!this.hasPlugin("classProperties")) this.unexpected();
this.next(); this.next();
@ -791,6 +792,7 @@ pp.parseClassProperty = function (node) {
node.value = null; node.value = null;
} }
this.semicolon(); this.semicolon();
this.state.inClassProperty = false;
return this.finishNode(node, "ClassProperty"); return this.finishNode(node, "ClassProperty");
}; };

View File

@ -1,7 +1,6 @@
/* eslint max-len: 0 */ /* eslint max-len: 0 */
import { types as tt } from "../tokenizer/types"; import { types as tt } from "../tokenizer/types";
import { types as ct } from "../tokenizer/context";
import Parser from "../parser"; import Parser from "../parser";
const primitiveTypes = [ const primitiveTypes = [
@ -1139,6 +1138,12 @@ export default function (instance) {
}; };
}); });
instance.extend("isNonstaticConstructor", function(inner) {
return function (method) {
return !this.match(tt.colon) && inner.call(this, method);
};
});
// parse type parameters for class methods // parse type parameters for class methods
instance.extend("parseClassMethod", function (inner) { instance.extend("parseClassMethod", function (inner) {
return function (classBody, method, ...args) { return function (classBody, method, ...args) {
@ -1386,6 +1391,12 @@ export default function (instance) {
} catch (err) { } catch (err) {
if (err instanceof SyntaxError) { if (err instanceof SyntaxError) {
this.state = state; this.state = state;
// Remove `tc.j_expr` and `tc.j_oTag` from context added
// by parsing `jsxTagStart` to stop the JSX plugin from
// messing with the tokens
this.state.context.length -= 2;
jsxError = err; jsxError = err;
} else { } else {
// istanbul ignore next: no such error is expected // istanbul ignore next: no such error is expected
@ -1394,9 +1405,6 @@ export default function (instance) {
} }
} }
// Need to push something onto the context to stop
// the JSX plugin from messing with the tokens
this.state.context.push(ct.parenExpression);
if (jsxError != null || this.isRelational("<")) { if (jsxError != null || this.isRelational("<")) {
let arrowExpression; let arrowExpression;
let typeParameters; let typeParameters;
@ -1422,7 +1430,6 @@ export default function (instance) {
); );
} }
} }
this.state.context.pop();
return inner.apply(this, args); return inner.apply(this, args);
}; };

View File

@ -18,6 +18,7 @@ export default class State {
this.inAsync = this.inAsync =
this.inPropertyName = this.inPropertyName =
this.inType = this.inType =
this.inClassProperty =
this.noAnonFunctionType = this.noAnonFunctionType =
false; false;
@ -73,6 +74,7 @@ export default class State {
inAsync: boolean; inAsync: boolean;
inType: boolean; inType: boolean;
inPropertyName: boolean; inPropertyName: boolean;
inClassProperty: boolean;
// Labels in scope. // Labels in scope.
labels: Array<Object>; labels: Array<Object>;

View File

@ -0,0 +1 @@
var x = ({ const });

View File

@ -0,0 +1,3 @@
{
"throws": "const is a reserved word (1:11)"
}

View File

@ -0,0 +1 @@
({ get, this, if });

View File

@ -0,0 +1,3 @@
{
"throws": "this is a reserved word (1:8)"
}

View File

@ -0,0 +1,3 @@
class Fails extends class { c(){} } {
c = super.c();
}

View File

@ -0,0 +1,272 @@
{
"type": "File",
"start": 0,
"end": 56,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"program": {
"type": "Program",
"start": 0,
"end": 56,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"sourceType": "script",
"body": [
{
"type": "ClassDeclaration",
"start": 0,
"end": 56,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"id": {
"type": "Identifier",
"start": 6,
"end": 11,
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 11
},
"identifierName": "Fails"
},
"name": "Fails"
},
"superClass": {
"type": "ClassExpression",
"start": 20,
"end": 35,
"loc": {
"start": {
"line": 1,
"column": 20
},
"end": {
"line": 1,
"column": 35
}
},
"id": null,
"superClass": null,
"body": {
"type": "ClassBody",
"start": 26,
"end": 35,
"loc": {
"start": {
"line": 1,
"column": 26
},
"end": {
"line": 1,
"column": 35
}
},
"body": [
{
"type": "ClassMethod",
"start": 28,
"end": 33,
"loc": {
"start": {
"line": 1,
"column": 28
},
"end": {
"line": 1,
"column": 33
}
},
"static": false,
"computed": false,
"key": {
"type": "Identifier",
"start": 28,
"end": 29,
"loc": {
"start": {
"line": 1,
"column": 28
},
"end": {
"line": 1,
"column": 29
},
"identifierName": "c"
},
"name": "c"
},
"kind": "method",
"id": null,
"generator": false,
"expression": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start": 31,
"end": 33,
"loc": {
"start": {
"line": 1,
"column": 31
},
"end": {
"line": 1,
"column": 33
}
},
"body": [],
"directives": []
}
}
]
}
},
"body": {
"type": "ClassBody",
"start": 36,
"end": 56,
"loc": {
"start": {
"line": 1,
"column": 36
},
"end": {
"line": 3,
"column": 1
}
},
"body": [
{
"type": "ClassProperty",
"start": 40,
"end": 54,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 16
}
},
"static": false,
"computed": false,
"key": {
"type": "Identifier",
"start": 40,
"end": 41,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 3
},
"identifierName": "c"
},
"name": "c"
},
"value": {
"type": "CallExpression",
"start": 44,
"end": 53,
"loc": {
"start": {
"line": 2,
"column": 6
},
"end": {
"line": 2,
"column": 15
}
},
"callee": {
"type": "MemberExpression",
"start": 44,
"end": 51,
"loc": {
"start": {
"line": 2,
"column": 6
},
"end": {
"line": 2,
"column": 13
}
},
"object": {
"type": "Super",
"start": 44,
"end": 49,
"loc": {
"start": {
"line": 2,
"column": 6
},
"end": {
"line": 2,
"column": 11
}
}
},
"property": {
"type": "Identifier",
"start": 50,
"end": 51,
"loc": {
"start": {
"line": 2,
"column": 12
},
"end": {
"line": 2,
"column": 13
},
"identifierName": "c"
},
"name": "c"
},
"computed": false
},
"arguments": []
}
}
]
}
}
],
"directives": []
}
}

View File

@ -0,0 +1,3 @@
{
"plugins": ["classProperties"]
}

View File

@ -0,0 +1,3 @@
class Foo {
constructor: () => this;
}

View File

@ -0,0 +1,169 @@
{
"type": "File",
"start": 0,
"end": 40,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"program": {
"type": "Program",
"start": 0,
"end": 40,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"sourceType": "module",
"body": [
{
"type": "ClassDeclaration",
"start": 0,
"end": 40,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"id": {
"type": "Identifier",
"start": 6,
"end": 9,
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 9
},
"identifierName": "Foo"
},
"name": "Foo"
},
"superClass": null,
"body": {
"type": "ClassBody",
"start": 10,
"end": 40,
"loc": {
"start": {
"line": 1,
"column": 10
},
"end": {
"line": 3,
"column": 1
}
},
"body": [
{
"type": "ClassProperty",
"start": 14,
"end": 38,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 26
}
},
"static": false,
"computed": false,
"key": {
"type": "Identifier",
"start": 14,
"end": 25,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 13
},
"identifierName": "constructor"
},
"name": "constructor"
},
"variance": null,
"typeAnnotation": {
"type": "TypeAnnotation",
"start": 25,
"end": 37,
"loc": {
"start": {
"line": 2,
"column": 13
},
"end": {
"line": 2,
"column": 25
}
},
"typeAnnotation": {
"type": "FunctionTypeAnnotation",
"start": 27,
"end": 37,
"loc": {
"start": {
"line": 2,
"column": 15
},
"end": {
"line": 2,
"column": 25
}
},
"params": [],
"rest": null,
"returnType": {
"type": "ThisTypeAnnotation",
"start": 33,
"end": 37,
"loc": {
"start": {
"line": 2,
"column": 21
},
"end": {
"line": 2,
"column": 25
}
},
"value": true
},
"typeParameters": null
}
},
"value": null
}
]
}
}
],
"directives": []
}
}

View File

@ -0,0 +1,3 @@
{
"plugins": ["jsx", "flow", "classProperties"]
}

View File

@ -0,0 +1,5 @@
class A {
constructor(): Object {
return {};
}
}

View File

@ -0,0 +1,222 @@
{
"type": "File",
"start": 0,
"end": 56,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 5,
"column": 1
}
},
"program": {
"type": "Program",
"start": 0,
"end": 56,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 5,
"column": 1
}
},
"sourceType": "module",
"body": [
{
"type": "ClassDeclaration",
"start": 0,
"end": 56,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 5,
"column": 1
}
},
"id": {
"type": "Identifier",
"start": 6,
"end": 7,
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 7
},
"identifierName": "A"
},
"name": "A"
},
"superClass": null,
"body": {
"type": "ClassBody",
"start": 8,
"end": 56,
"loc": {
"start": {
"line": 1,
"column": 8
},
"end": {
"line": 5,
"column": 1
}
},
"body": [
{
"type": "ClassMethod",
"start": 12,
"end": 54,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 4,
"column": 3
}
},
"static": false,
"computed": false,
"key": {
"type": "Identifier",
"start": 12,
"end": 23,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 13
},
"identifierName": "constructor"
},
"name": "constructor"
},
"kind": "constructor",
"id": null,
"generator": false,
"expression": false,
"async": false,
"params": [],
"returnType": {
"type": "TypeAnnotation",
"start": 25,
"end": 33,
"loc": {
"start": {
"line": 2,
"column": 15
},
"end": {
"line": 2,
"column": 23
}
},
"typeAnnotation": {
"type": "GenericTypeAnnotation",
"start": 27,
"end": 33,
"loc": {
"start": {
"line": 2,
"column": 17
},
"end": {
"line": 2,
"column": 23
}
},
"typeParameters": null,
"id": {
"type": "Identifier",
"start": 27,
"end": 33,
"loc": {
"start": {
"line": 2,
"column": 17
},
"end": {
"line": 2,
"column": 23
},
"identifierName": "Object"
},
"name": "Object"
}
},
"predicate": null
},
"body": {
"type": "BlockStatement",
"start": 34,
"end": 54,
"loc": {
"start": {
"line": 2,
"column": 24
},
"end": {
"line": 4,
"column": 3
}
},
"body": [
{
"type": "ReturnStatement",
"start": 40,
"end": 50,
"loc": {
"start": {
"line": 3,
"column": 4
},
"end": {
"line": 3,
"column": 14
}
},
"argument": {
"type": "ObjectExpression",
"start": 47,
"end": 49,
"loc": {
"start": {
"line": 3,
"column": 11
},
"end": {
"line": 3,
"column": 13
}
},
"properties": []
}
}
],
"directives": []
}
}
]
}
}
],
"directives": []
}
}

View File

@ -0,0 +1,3 @@
{
"plugins": ["jsx", "flow", "classProperties"]
}

View File

@ -0,0 +1 @@
const functionReturningIdentityAsAField = () => ({ id: <T>(value: T): T => value });

View File

@ -0,0 +1,346 @@
{
"type": "File",
"start": 0,
"end": 84,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 84
}
},
"program": {
"type": "Program",
"start": 0,
"end": 84,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 84
}
},
"sourceType": "module",
"body": [
{
"type": "VariableDeclaration",
"start": 0,
"end": 84,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 84
}
},
"declarations": [
{
"type": "VariableDeclarator",
"start": 6,
"end": 83,
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 83
}
},
"id": {
"type": "Identifier",
"start": 6,
"end": 39,
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 39
},
"identifierName": "functionReturningIdentityAsAField"
},
"name": "functionReturningIdentityAsAField"
},
"init": {
"type": "ArrowFunctionExpression",
"start": 42,
"end": 83,
"loc": {
"start": {
"line": 1,
"column": 42
},
"end": {
"line": 1,
"column": 83
}
},
"id": null,
"generator": false,
"expression": true,
"async": false,
"params": [],
"body": {
"type": "ObjectExpression",
"start": 49,
"end": 82,
"loc": {
"start": {
"line": 1,
"column": 49
},
"end": {
"line": 1,
"column": 82
}
},
"properties": [
{
"type": "ObjectProperty",
"start": 51,
"end": 80,
"loc": {
"start": {
"line": 1,
"column": 51
},
"end": {
"line": 1,
"column": 80
}
},
"method": false,
"shorthand": false,
"computed": false,
"key": {
"type": "Identifier",
"start": 51,
"end": 53,
"loc": {
"start": {
"line": 1,
"column": 51
},
"end": {
"line": 1,
"column": 53
},
"identifierName": "id"
},
"name": "id"
},
"value": {
"type": "ArrowFunctionExpression",
"start": 55,
"end": 80,
"loc": {
"start": {
"line": 1,
"column": 55
},
"end": {
"line": 1,
"column": 80
}
},
"returnType": {
"type": "TypeAnnotation",
"start": 68,
"end": 71,
"loc": {
"start": {
"line": 1,
"column": 68
},
"end": {
"line": 1,
"column": 71
}
},
"typeAnnotation": {
"type": "GenericTypeAnnotation",
"start": 70,
"end": 71,
"loc": {
"start": {
"line": 1,
"column": 70
},
"end": {
"line": 1,
"column": 71
}
},
"typeParameters": null,
"id": {
"type": "Identifier",
"start": 70,
"end": 71,
"loc": {
"start": {
"line": 1,
"column": 70
},
"end": {
"line": 1,
"column": 71
},
"identifierName": "T"
},
"name": "T"
}
},
"predicate": null
},
"id": null,
"generator": false,
"expression": true,
"async": false,
"params": [
{
"type": "Identifier",
"start": 59,
"end": 67,
"loc": {
"start": {
"line": 1,
"column": 59
},
"end": {
"line": 1,
"column": 67
},
"identifierName": "value"
},
"name": "value",
"typeAnnotation": {
"type": "TypeAnnotation",
"start": 64,
"end": 67,
"loc": {
"start": {
"line": 1,
"column": 64
},
"end": {
"line": 1,
"column": 67
}
},
"typeAnnotation": {
"type": "GenericTypeAnnotation",
"start": 66,
"end": 67,
"loc": {
"start": {
"line": 1,
"column": 66
},
"end": {
"line": 1,
"column": 67
}
},
"typeParameters": null,
"id": {
"type": "Identifier",
"start": 66,
"end": 67,
"loc": {
"start": {
"line": 1,
"column": 66
},
"end": {
"line": 1,
"column": 67
},
"identifierName": "T"
},
"name": "T"
}
}
}
}
],
"body": {
"type": "Identifier",
"start": 75,
"end": 80,
"loc": {
"start": {
"line": 1,
"column": 75
},
"end": {
"line": 1,
"column": 80
},
"identifierName": "value"
},
"name": "value"
},
"typeParameters": {
"type": "TypeParameterDeclaration",
"start": 55,
"end": 58,
"loc": {
"start": {
"line": 1,
"column": 55
},
"end": {
"line": 1,
"column": 58
}
},
"params": [
{
"type": "TypeParameter",
"start": 56,
"end": 57,
"loc": {
"start": {
"line": 1,
"column": 56
},
"end": {
"line": 1,
"column": 57
}
},
"name": "T",
"variance": null
}
]
}
}
}
],
"extra": {
"parenthesized": true,
"parenStart": 48
}
}
}
}
],
"kind": "const"
}
],
"directives": []
}
}

View File

@ -0,0 +1,2 @@
const identity = <T>(t: T): T => t;
const a = 1;

View File

@ -0,0 +1,340 @@
{
"type": "File",
"start": 0,
"end": 48,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 12
}
},
"program": {
"type": "Program",
"start": 0,
"end": 48,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 12
}
},
"sourceType": "module",
"body": [
{
"type": "VariableDeclaration",
"start": 0,
"end": 35,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 35
}
},
"declarations": [
{
"type": "VariableDeclarator",
"start": 6,
"end": 34,
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 34
}
},
"id": {
"type": "Identifier",
"start": 6,
"end": 14,
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 14
},
"identifierName": "identity"
},
"name": "identity"
},
"init": {
"type": "ArrowFunctionExpression",
"start": 17,
"end": 34,
"loc": {
"start": {
"line": 1,
"column": 17
},
"end": {
"line": 1,
"column": 34
}
},
"returnType": {
"type": "TypeAnnotation",
"start": 26,
"end": 29,
"loc": {
"start": {
"line": 1,
"column": 26
},
"end": {
"line": 1,
"column": 29
}
},
"typeAnnotation": {
"type": "GenericTypeAnnotation",
"start": 28,
"end": 29,
"loc": {
"start": {
"line": 1,
"column": 28
},
"end": {
"line": 1,
"column": 29
}
},
"typeParameters": null,
"id": {
"type": "Identifier",
"start": 28,
"end": 29,
"loc": {
"start": {
"line": 1,
"column": 28
},
"end": {
"line": 1,
"column": 29
},
"identifierName": "T"
},
"name": "T"
}
},
"predicate": null
},
"id": null,
"generator": false,
"expression": true,
"async": false,
"params": [
{
"type": "Identifier",
"start": 21,
"end": 25,
"loc": {
"start": {
"line": 1,
"column": 21
},
"end": {
"line": 1,
"column": 25
},
"identifierName": "t"
},
"name": "t",
"typeAnnotation": {
"type": "TypeAnnotation",
"start": 22,
"end": 25,
"loc": {
"start": {
"line": 1,
"column": 22
},
"end": {
"line": 1,
"column": 25
}
},
"typeAnnotation": {
"type": "GenericTypeAnnotation",
"start": 24,
"end": 25,
"loc": {
"start": {
"line": 1,
"column": 24
},
"end": {
"line": 1,
"column": 25
}
},
"typeParameters": null,
"id": {
"type": "Identifier",
"start": 24,
"end": 25,
"loc": {
"start": {
"line": 1,
"column": 24
},
"end": {
"line": 1,
"column": 25
},
"identifierName": "T"
},
"name": "T"
}
}
}
}
],
"body": {
"type": "Identifier",
"start": 33,
"end": 34,
"loc": {
"start": {
"line": 1,
"column": 33
},
"end": {
"line": 1,
"column": 34
},
"identifierName": "t"
},
"name": "t"
},
"typeParameters": {
"type": "TypeParameterDeclaration",
"start": 17,
"end": 20,
"loc": {
"start": {
"line": 1,
"column": 17
},
"end": {
"line": 1,
"column": 20
}
},
"params": [
{
"type": "TypeParameter",
"start": 18,
"end": 19,
"loc": {
"start": {
"line": 1,
"column": 18
},
"end": {
"line": 1,
"column": 19
}
},
"name": "T",
"variance": null
}
]
}
}
}
],
"kind": "const"
},
{
"type": "VariableDeclaration",
"start": 36,
"end": 48,
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 2,
"column": 12
}
},
"declarations": [
{
"type": "VariableDeclarator",
"start": 42,
"end": 47,
"loc": {
"start": {
"line": 2,
"column": 6
},
"end": {
"line": 2,
"column": 11
}
},
"id": {
"type": "Identifier",
"start": 42,
"end": 43,
"loc": {
"start": {
"line": 2,
"column": 6
},
"end": {
"line": 2,
"column": 7
},
"identifierName": "a"
},
"name": "a"
},
"init": {
"type": "NumericLiteral",
"start": 46,
"end": 47,
"loc": {
"start": {
"line": 2,
"column": 10
},
"end": {
"line": 2,
"column": 11
}
},
"extra": {
"rawValue": 1,
"raw": "1"
},
"value": 1
}
}
],
"kind": "const"
}
],
"directives": []
}
}