Merge branch 'master' into decorators-stage-2

This commit is contained in:
Peeyush Kushwaha
2017-06-18 01:33:16 +05:30
12 changed files with 814 additions and 91 deletions

View File

@@ -294,99 +294,109 @@ export default class ExpressionParser extends LValParser {
return this.parseSubscripts(expr, startPos, startLoc);
}
parseSubscripts(base: N.Expression, startPos: number, startLoc: Position, noCalls?: boolean): N.Expression {
for (;;) {
if (!noCalls && this.eat(tt.doubleColon)) {
const node = this.startNodeAt(startPos, startLoc);
node.object = base;
node.callee = this.parseNoCallExpr();
return this.parseSubscripts(this.finishNode(node, "BindExpression"), startPos, startLoc, noCalls);
parseSubscripts(base: N.Expression, startPos: number, startLoc: Position, noCalls?: ?boolean) {
const state = { stop: false };
do {
base = this.parseSubscript(base, startPos, startLoc, noCalls, state);
} while (!state.stop);
return base;
}
} else if (this.match(tt.questionDot)) {
if (!this.hasPlugin("optionalChaining")) {
this.raise(startPos, "You can only use optional-chaining when the 'optionalChaining' plugin is enabled.");
}
/** @param state Set 'state.stop = true' to indicate that we should stop parsing subscripts. */
parseSubscript(base: N.Expression, startPos: number, startLoc: Position, noCalls: ?boolean, state: { stop: boolean }): N.Expression {
if (!noCalls && this.eat(tt.doubleColon)) {
const node = this.startNodeAt(startPos, startLoc);
node.object = base;
node.callee = this.parseNoCallExpr();
state.stop = true;
return this.parseSubscripts(this.finishNode(node, "BindExpression"), startPos, startLoc, noCalls);
if (noCalls && this.lookahead().type == tt.parenL) {
return base;
}
this.next();
} else if (this.match(tt.questionDot)) {
if (!this.hasPlugin("optionalChaining")) {
this.raise(startPos, "You can only use optional-chaining when the 'optionalChaining' plugin is enabled.");
}
const node = this.startNodeAt(startPos, startLoc);
if (noCalls && this.lookahead().type == tt.parenL) {
state.stop = true;
return base;
}
this.next();
if (this.eat(tt.bracketL)) {
node.object = base;
node.property = this.parseExpression();
node.computed = true;
node.optional = true;
this.expect(tt.bracketR);
base = this.finishNode(node, "MemberExpression");
} else if (this.eat(tt.parenL)) {
const possibleAsync = this.state.potentialArrowAt === base.start &&
base.type === "Identifier" &&
base.name === "async" &&
!this.canInsertSemicolon();
const node = this.startNodeAt(startPos, startLoc);
node.callee = base;
node.arguments = this.parseCallExpressionArguments(tt.parenR, possibleAsync);
node.optional = true;
base = this.finishNode(node, "CallExpression");
} else {
node.object = base;
node.property = this.parseIdentifier(true);
node.computed = false;
node.optional = true;
base = this.finishNode(node, "MemberExpression");
}
} else if (this.eat(tt.dot)) {
const node = this.startNodeAt(startPos, startLoc);
node.object = base;
node.property = this.hasPlugin("classPrivateProperties") ? this.parseMaybePrivateName() : this.parseIdentifier(true);
node.computed = false;
base = this.finishNode(node, "MemberExpression");
} else if (this.eat(tt.bracketL)) {
const node = this.startNodeAt(startPos, startLoc);
if (this.eat(tt.bracketL)) {
node.object = base;
node.property = this.parseExpression();
node.computed = true;
node.optional = true;
this.expect(tt.bracketR);
base = this.finishNode(node, "MemberExpression");
} else if (!noCalls && this.match(tt.parenL)) {
const possibleAsync = this.state.potentialArrowAt === base.start && base.type === "Identifier" && base.name === "async" && !this.canInsertSemicolon();
this.next();
return this.finishNode(node, "MemberExpression");
} else if (this.eat(tt.parenL)) {
const possibleAsync = this.state.potentialArrowAt === base.start &&
base.type === "Identifier" &&
base.name === "async" &&
!this.canInsertSemicolon();
const node = this.startNodeAt(startPos, startLoc);
node.callee = base;
node.arguments = this.parseCallExpressionArguments(tt.parenR, possibleAsync);
if (node.callee.type === "Import") {
if (node.arguments.length !== 1) {
this.raise(node.start, "import() requires exactly one argument");
}
node.optional = true;
const importArg = node.arguments[0];
if (importArg && importArg.type === "SpreadElement") {
this.raise(importArg.start, "... is not allowed in import()");
}
}
base = this.finishNode(node, "CallExpression");
if (possibleAsync && this.shouldParseAsyncArrow()) {
return this.parseAsyncArrowFromCallExpression(this.startNodeAt(startPos, startLoc), node);
} else {
this.toReferencedList(node.arguments);
}
} else if (this.match(tt.backQuote)) {
const node = this.startNodeAt(startPos, startLoc);
node.tag = base;
node.quasi = this.parseTemplate(true);
base = this.finishNode(node, "TaggedTemplateExpression");
return this.finishNode(node, "CallExpression");
} else {
return base;
node.object = base;
node.property = this.parseIdentifier(true);
node.computed = false;
node.optional = true;
return this.finishNode(node, "MemberExpression");
}
} else if (this.eat(tt.dot)) {
const node = this.startNodeAt(startPos, startLoc);
node.object = base;
node.property = this.hasPlugin("classPrivateProperties") ? this.parseMaybePrivateName() : this.parseIdentifier(true);
node.computed = false;
return this.finishNode(node, "MemberExpression");
} else if (this.eat(tt.bracketL)) {
const node = this.startNodeAt(startPos, startLoc);
node.object = base;
node.property = this.parseExpression();
node.computed = true;
this.expect(tt.bracketR);
return this.finishNode(node, "MemberExpression");
} else if (!noCalls && this.match(tt.parenL)) {
const possibleAsync = this.state.potentialArrowAt === base.start && base.type === "Identifier" && base.name === "async" && !this.canInsertSemicolon();
this.next();
const node = this.startNodeAt(startPos, startLoc);
node.callee = base;
node.arguments = this.parseCallExpressionArguments(tt.parenR, possibleAsync);
if (node.callee.type === "Import") {
if (node.arguments.length !== 1) {
this.raise(node.start, "import() requires exactly one argument");
}
const importArg = node.arguments[0];
if (importArg && importArg.type === "SpreadElement") {
this.raise(importArg.start, "... is not allowed in import()");
}
}
this.finishNode(node, "CallExpression");
if (possibleAsync && this.shouldParseAsyncArrow()) {
state.stop = true;
return this.parseAsyncArrowFromCallExpression(this.startNodeAt(startPos, startLoc), node);
} else {
this.toReferencedList(node.arguments);
}
return node;
} else if (this.match(tt.backQuote)) {
const node = this.startNodeAt(startPos, startLoc);
node.tag = base;
node.quasi = this.parseTemplate(true);
return this.finishNode(node, "TaggedTemplateExpression");
} else {
state.stop = true;
return base;
}
// istanbul ignore next
throw new Error("Unreachable");
}
parseCallExpressionArguments(close: TokenType, possibleAsyncArrow: boolean): $ReadOnlyArray<?N.Expression> {

View File

@@ -161,6 +161,11 @@ export default (superClass: Class<Parser>): Class<Parser> => class extends super
isAsync: boolean
): void {
this.parseMethod(method, isGenerator, isAsync);
if (method.typeParameters) {
// $FlowIgnore
method.value.typeParameters = method.typeParameters;
delete method.typeParameters;
}
classBody.body.push(this.finishNode(method, "MethodDefinition"));
}
@@ -221,16 +226,15 @@ export default (superClass: Class<Parser>): Class<Parser> => class extends super
isAsync: boolean,
isPattern: boolean
): ?N.ObjectMethod {
const node = super.parseObjectMethod(prop, isGenerator, isAsync, isPattern);
const node: N.EstreeProperty = (super.parseObjectMethod(prop, isGenerator, isAsync, isPattern): any);
if (node) {
// $FlowIgnore
if (node.kind === "method") node.kind = "init";
// $FlowIgnore
node.type = "Property";
if (node.kind === "method") node.kind = "init";
node.shorthand = false;
}
return node;
return (node: any);
}
parseObjectProperty(
@@ -240,16 +244,16 @@ export default (superClass: Class<Parser>): Class<Parser> => class extends super
isPattern: boolean,
refShorthandDefaultPos: ?Pos
): ?N.ObjectProperty {
const node = super.parseObjectProperty(prop, startPos, startLoc, isPattern, refShorthandDefaultPos);
const node: N.EstreeProperty = (
super.parseObjectProperty(prop, startPos, startLoc, isPattern, refShorthandDefaultPos): any
);
if (node) {
// $FlowIgnore
node.kind = "init";
// $FlowIgnore
node.type = "Property";
}
return node;
return (node: any);
}
toAssignable(

View File

@@ -958,6 +958,7 @@ export default (superClass: Class<Parser>): Class<Parser> => class extends super
this.state.inType = true;
const type = this.flowParseUnionType();
this.state.inType = oldInType;
this.state.exprAllowed = false;
return type;
}

View File

@@ -78,6 +78,7 @@ export default class Tokenizer extends LocationParser {
super();
this.state = new State;
this.state.init(options, input);
this.isLookahead = false;
}
// Move to the next token
@@ -127,7 +128,7 @@ export default class Tokenizer extends LocationParser {
this.next();
this.isLookahead = false;
const curr = this.state.clone(true);
const curr = this.state;
this.state = old;
return curr;
}
@@ -225,9 +226,10 @@ export default class Tokenizer extends LocationParser {
const start = this.state.pos;
const startLoc = this.state.curPosition();
let ch = this.input.charCodeAt(this.state.pos += startSkip);
while (this.state.pos < this.input.length && ch !== 10 && ch !== 13 && ch !== 8232 && ch !== 8233) {
++this.state.pos;
ch = this.input.charCodeAt(this.state.pos);
if (this.state.pos < this.input.length) {
while (ch !== 10 && ch !== 13 && ch !== 8232 && ch !== 8233 && ++this.state.pos < this.input.length) {
ch = this.input.charCodeAt(this.state.pos);
}
}
this.pushComment(false, this.input.slice(start + startSkip, this.state.pos), start, this.state.pos, startLoc, this.state.curPosition());

View File

@@ -40,6 +40,8 @@ export default class State {
this.trailingComments = [];
this.leadingComments = [];
this.commentStack = [];
// $FlowIgnore
this.commentPreviousNode = null;
this.pos = this.lineStart = 0;
this.curLine = options.startLine;

View File

@@ -724,3 +724,18 @@ export type FlowFunctionTypeParam = Node;
export type FlowTypeAnnotation = Node;
export type FlowVariance = Node;
export type FlowClassImplements = Node;
// estree
export type EstreeProperty = NodeBase & {
type: "Property";
shorthand: boolean;
key: Expression;
computed: boolean;
value: Expression;
decorators: $ReadOnlyArray<Decorator>;
kind?: "get" | "set" | "init";
variance?: ?FlowVariance;
};

View File

@@ -23,6 +23,7 @@ export class SourceLocation {
start: Position;
end: Position;
filename: string;
identifierName: ?string;
constructor(start: Position, end?: Position) {
this.start = start;