I'm extremely stupid and didn't commit as I go. To anyone reading this
I'm extremely sorry. A lot of these changes are very broad and I plan on
releasing Babel 6.0.0 today live on stage at Ember Camp London so I'm
afraid I couldn't wait. If you're ever in London I'll buy you a beer
(or assorted beverage!) to make up for it, also I'll kiss your feet and
give you a back massage, maybe.
This commit is contained in:
Sebastian McKenzie
2015-10-29 17:51:24 +00:00
parent 3974dd762d
commit ae7d5367f1
1501 changed files with 16477 additions and 19786 deletions

View File

@@ -40,9 +40,9 @@ pp.addComment = function (comment) {
pp.processComment = function (node) {
if (node.type === "Program" && node.body.length > 0) return;
var stack = this.state.commentStack;
let stack = this.state.commentStack;
var lastChild, trailingComments, i;
let lastChild, trailingComments, i;
if (this.state.trailingComments.length > 0) {
// If the first comment in trailingComments comes after the
@@ -62,7 +62,7 @@ pp.processComment = function (node) {
this.state.trailingComments.length = 0;
}
} else {
var lastInStack = last(stack);
let lastInStack = last(stack);
if (stack.length > 0 && lastInStack.trailingComments && lastInStack.trailingComments[0].start >= node.end) {
trailingComments = lastInStack.trailingComments;
lastInStack.trailingComments = null;
@@ -80,7 +80,7 @@ pp.processComment = function (node) {
node.leadingComments = lastChild.leadingComments;
lastChild.leadingComments = null;
} else {
// A leading comment for an anonymous class had been stolen by its first MethodDefinition,
// A leading comment for an anonymous class had been stolen by its first ClassMethod,
// so this takes back the leading comment.
// See also: https://github.com/eslint/espree/issues/158
for (i = lastChild.leadingComments.length - 2; i >= 0; --i) {

View File

@@ -30,9 +30,10 @@ const pp = Parser.prototype;
// strict mode, init properties are also not allowed to be repeated.
pp.checkPropClash = function (prop, propHash) {
if (prop.computed || prop.method || prop.shorthand) return;
if (prop.computed || prop.method) return;
let key = prop.key, name;
let key = prop.key;
let name;
switch (key.type) {
case "Identifier":
name = key.name;
@@ -47,8 +48,7 @@ pp.checkPropClash = function (prop, propHash) {
return;
}
let kind = prop.kind;
if (name === "__proto__" && kind === "init") {
if (name === "__proto__" && prop.kind === "init") {
if (propHash.proto) this.raise(key.start, "Redefinition of __proto__ property");
propHash.proto = true;
}
@@ -225,11 +225,11 @@ pp.parseExprSubscripts = function (refShorthandDefaultPos) {
if (refShorthandDefaultPos && refShorthandDefaultPos.start) {
return expr;
} else {
return this.parseSubscripts(expr, startPos, startLoc);
return this.parseSubscripts(expr, startPos, startLoc);
}
};
pp.parseSubscripts = function(base, startPos, startLoc, noCalls) {
pp.parseSubscripts = function (base, startPos, startLoc, noCalls) {
for (;;) {
if (!noCalls && this.eat(tt.doubleColon)) {
let node = this.startNodeAt(startPos, startLoc);
@@ -255,11 +255,11 @@ pp.parseSubscripts = function(base, startPos, startLoc, noCalls) {
let node = this.startNodeAt(startPos, startLoc);
node.callee = base;
node.arguments = this.parseCallExpressionArguments(tt.parenR, this.hasFeature("trailingFunctionCommas"), possibleAsync);
node.arguments = this.parseCallExpressionArguments(tt.parenR, this.hasPlugin("trailingFunctionCommas"), possibleAsync);
base = this.finishNode(node, "CallExpression");
if (possibleAsync && this.shouldParseAsyncArrow()) {
base = this.parseAsyncArrowFromCallExpression(this.startNodeAt(startPos, startLoc), node);
return this.parseAsyncArrowFromCallExpression(this.startNodeAt(startPos, startLoc), node);
} else {
this.toReferencedList(node.arguments);
}
@@ -307,7 +307,7 @@ pp.shouldParseAsyncArrow = function () {
};
pp.parseAsyncArrowFromCallExpression = function (node, call) {
if (!this.hasFeature("asyncFunctions")) this.unexpected();
if (!this.hasPlugin("asyncFunctions")) this.unexpected();
this.expect(tt.arrow);
return this.parseArrowExpression(node, call.arguments, true);
};
@@ -328,7 +328,7 @@ pp.parseExprAtom = function (refShorthandDefaultPos) {
let node, canBeArrow = this.state.potentialArrowAt === this.state.start;
switch (this.state.type) {
case tt._super:
if (!this.state.inFunction) {
if (!this.state.inMethod && !this.options.allowSuperOutsideMethod) {
this.raise(this.state.start, "'super' outside of function or class");
}
@@ -337,6 +337,9 @@ pp.parseExprAtom = function (refShorthandDefaultPos) {
if (!this.match(tt.parenL) && !this.match(tt.bracketL) && !this.match(tt.dot)) {
this.unexpected();
}
if (this.match(tt.parenL) && this.state.inMethod !== "constructor" && !this.options.allowSuperOutsideMethod) {
this.raise(node.start, "super() outside of class constructor");
}
return this.finishNode(node, "Super");
case tt._this:
@@ -349,18 +352,22 @@ pp.parseExprAtom = function (refShorthandDefaultPos) {
case tt.name:
node = this.startNode();
let id = this.parseIdentifier(true);
let allowAwait = this.hasPlugin("asyncFunctions") && this.state.value === "await" && this.state.inAsync;
let allowYield = this.shouldAllowYieldIdentifier();
let id = this.parseIdentifier(allowAwait || allowYield);
if (this.hasFeature("asyncFunctions")) {
if (this.hasPlugin("asyncFunctions")) {
if (id.name === "await") {
if (this.inAsync) return this.parseAwait(node);
if (this.state.inAsync || this.inModule) {
return this.parseAwait(node);
}
} else if (id.name === "async" && this.match(tt._function) && !this.canInsertSemicolon()) {
this.next();
return this.parseFunction(node, false, false, true);
} else if (canBeArrow && id.name === "async" && this.match(tt.name)) {
var params = [this.parseIdentifier()];
let params = [this.parseIdentifier()];
this.expect(tt.arrow);
// var foo = bar => {};
// let foo = bar => {};
return this.parseArrowExpression(node, params, true);
}
}
@@ -372,14 +379,14 @@ pp.parseExprAtom = function (refShorthandDefaultPos) {
return id;
case tt._do:
if (this.hasFeature("doExpressions")) {
if (this.hasPlugin("doExpressions")) {
let node = this.startNode();
this.next();
var oldInFunction = this.state.inFunction;
var oldLabels = this.state.labels;
let oldInFunction = this.state.inFunction;
let oldLabels = this.state.labels;
this.state.labels = [];
this.state.inFunction = false;
node.body = this.parseBlock();
node.body = this.parseBlock(false, true);
this.state.inFunction = oldInFunction;
this.state.labels = oldLabels;
return this.finishNode(node, "DoExpression");
@@ -405,8 +412,7 @@ pp.parseExprAtom = function (refShorthandDefaultPos) {
case tt._true: case tt._false:
node = this.startNode();
node.rawValue = node.value = this.match(tt._true);
node.raw = this.state.type.keyword;
node.value = this.match(tt._true);
this.next();
return this.finishNode(node, "BooleanLiteral");
@@ -416,10 +422,6 @@ pp.parseExprAtom = function (refShorthandDefaultPos) {
case tt.bracketL:
node = this.startNode();
this.next();
// check whether this is array comprehension or regular array
if (this.hasFeature("comprehensions") && this.match(tt._for)) {
return this.parseComprehension(node, false);
}
node.elements = this.parseExprList(tt.bracketR, true, true, refShorthandDefaultPos);
this.toReferencedList(node.elements);
return this.finishNode(node, "ArrayExpression");
@@ -464,8 +466,9 @@ pp.parseExprAtom = function (refShorthandDefaultPos) {
pp.parseLiteral = function (value, type) {
let node = this.startNode();
node.rawValue = node.value = value;
node.raw = this.input.slice(this.state.start, this.state.end);
this.addExtra(node, "rawValue", value);
this.addExtra(node, "raw", this.input.slice(this.state.start, this.state.end));
node.value = value;
this.next();
return this.finishNode(node, type);
};
@@ -483,10 +486,6 @@ pp.parseParenAndDistinguishExpression = function (startPos, startLoc, canBeArrow
let val;
this.next();
if (this.hasFeature("comprehensions") && this.match(tt._for)) {
return this.parseComprehension(this.startNodeAt(startPos, startLoc), true);
}
let innerStartPos = this.state.start, innerStartLoc = this.state.startLoc;
let exprList = [], first = true;
let refShorthandDefaultPos = { start: 0 }, spreadStart, innerParenStart, optionalCommaStart;
@@ -495,7 +494,7 @@ pp.parseParenAndDistinguishExpression = function (startPos, startLoc, canBeArrow
first = false;
} else {
this.expect(tt.comma);
if (this.match(tt.parenR) && this.hasFeature("trailingFunctionCommas")) {
if (this.match(tt.parenR) && this.hasPlugin("trailingFunctionCommas")) {
optionalCommaStart = this.state.start;
break;
}
@@ -572,7 +571,7 @@ pp.parseNew = function () {
node.callee = this.parseNoCallExpr();
if (this.eat(tt.parenL)) {
node.arguments = this.parseExprList(tt.parenR, this.hasFeature("trailingFunctionCommas"));
node.arguments = this.parseExprList(tt.parenR, this.hasPlugin("trailingFunctionCommas"));
this.toReferencedList(node.arguments);
} else {
node.arguments = [];
@@ -613,10 +612,14 @@ pp.parseTemplate = function () {
// Parse an object literal or binding pattern.
pp.parseObj = function (isPattern, refShorthandDefaultPos) {
let node = this.startNode(), first = true, propHash = Object.create(null);
node.properties = [];
let decorators = [];
let propHash = Object.create(null);
let first = true;
let node = this.startNode();
node.properties = [];
this.next();
while (!this.eat(tt.braceR)) {
if (first) {
first = false;
@@ -634,24 +637,30 @@ pp.parseObj = function (isPattern, refShorthandDefaultPos) {
prop.decorators = decorators;
decorators = [];
}
if (this.hasFeature("objectRestSpread") && this.match(tt.ellipsis)) {
if (this.hasPlugin("objectRestSpread") && this.match(tt.ellipsis)) {
prop = this.parseSpread();
prop.type = isPattern ? "RestProperty" : "SpreadProperty";
node.properties.push(prop);
continue;
}
prop.method = false;
prop.shorthand = false;
if (isPattern || refShorthandDefaultPos) {
startPos = this.state.start;
startLoc = this.state.startLoc;
}
if (!isPattern) {
isGenerator = this.eat(tt.star);
}
if (!isPattern && this.hasFeature("asyncFunctions") && this.isContextual("async")) {
if (!isPattern && this.hasPlugin("asyncFunctions") && this.isContextual("async")) {
if (isGenerator) this.unexpected();
var asyncId = this.parseIdentifier();
let asyncId = this.parseIdentifier();
if (this.match(tt.colon) || this.match(tt.parenL) || this.match(tt.braceR)) {
prop.key = asyncId;
} else {
@@ -661,43 +670,58 @@ pp.parseObj = function (isPattern, refShorthandDefaultPos) {
} else {
this.parsePropertyName(prop);
}
this.parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, refShorthandDefaultPos);
this.checkPropClash(prop, propHash);
node.properties.push(this.finishNode(prop, "Property"));
if (prop.shorthand) {
this.addExtra(prop, "shorthand", true);
}
node.properties.push(prop);
}
if (decorators.length) {
this.raise(this.state.start, "You have trailing decorators with no property");
}
return this.finishNode(node, isPattern ? "ObjectPattern" : "ObjectExpression");
};
pp.parseObjPropValue = function (prop, startPos, startLoc, isGenerator, isAsync, isPattern, refShorthandDefaultPos) {
if (this.eat(tt.colon)) {
prop.value = isPattern ? this.parseMaybeDefault(this.state.start, this.state.startLoc) : this.parseMaybeAssign(false, refShorthandDefaultPos);
prop.kind = "init";
} else if (this.match(tt.parenL)) {
return this.finishNode(prop, "ObjectProperty");
}
if (this.match(tt.parenL)) {
if (isPattern) this.unexpected();
prop.kind = "init";
prop.kind = "method";
prop.method = true;
prop.value = this.parseMethod(isGenerator, isAsync);
} else if (!prop.computed && prop.key.type === "Identifier" && (prop.key.name === "get" || prop.key.name === "set") && (!this.match(tt.comma) && !this.match(tt.braceR))) {
this.parseMethod(prop, isGenerator, isAsync);
return this.finishNode(prop, "ObjectMethod");
}
if (!prop.computed && prop.key.type === "Identifier" && (prop.key.name === "get" || prop.key.name === "set") && (!this.match(tt.comma) && !this.match(tt.braceR))) {
if (isGenerator || isAsync || isPattern) this.unexpected();
prop.kind = prop.key.name;
this.parsePropertyName(prop);
prop.value = this.parseMethod(false);
this.parseMethod(prop, false);
let paramCount = prop.kind === "get" ? 0 : 1;
if (prop.value.params.length !== paramCount) {
let start = prop.value.start;
if (prop.params.length !== paramCount) {
let start = prop.start;
if (prop.kind === "get") {
this.raise(start, "getter should have no params");
} else {
this.raise(start, "setter should have exactly one param");
}
}
} else if (!prop.computed && prop.key.type === "Identifier") {
prop.kind = "init";
return this.finishNode(prop, "ObjectMethod");
}
if (!prop.computed && prop.key.type === "Identifier") {
if (isPattern) {
var illegalBinding = this.isKeyword(prop.key.name);
let illegalBinding = this.isKeyword(prop.key.name);
if (!illegalBinding && this.state.strict) {
illegalBinding = reservedWords.strictBind(prop.key.name) || reservedWords.strict(prop.key.name);
}
@@ -714,9 +738,10 @@ pp.parseObjPropValue = function (prop, startPos, startLoc, isGenerator, isAsync,
prop.value = prop.key.__clone();
}
prop.shorthand = true;
} else {
this.unexpected();
return this.finishNode(prop, "ObjectProperty");
}
this.unexpected();
};
pp.parsePropertyName = function (prop) {
@@ -737,21 +762,23 @@ pp.initFunction = function (node, isAsync) {
node.id = null;
node.generator = false;
node.expression = false;
if (this.hasFeature("asyncFunctions")) {
if (this.hasPlugin("asyncFunctions")) {
node.async = !!isAsync;
}
};
// Parse object or class method.
pp.parseMethod = function (isGenerator, isAsync) {
let node = this.startNode();
pp.parseMethod = function (node, isGenerator, isAsync) {
let oldInMethod = this.state.inMethod;
this.state.inMethod = node.kind || true;
this.initFunction(node, isAsync);
this.expect(tt.parenL);
node.params = this.parseBindingList(tt.parenR, false, this.hasFeature("trailingFunctionCommas"));
node.params = this.parseBindingList(tt.parenR, false, this.hasPlugin("trailingFunctionCommas"));
node.generator = isGenerator;
this.parseFunctionBody(node);
return this.finishNode(node, "FunctionExpression");
this.state.inMethod = oldInMethod;
return node;
};
// Parse arrow function expression with given parameters.
@@ -768,8 +795,8 @@ pp.parseArrowExpression = function (node, params, isAsync) {
pp.parseFunctionBody = function (node, allowExpression) {
let isExpression = allowExpression && !this.match(tt.braceL);
var oldInAsync = this.inAsync;
this.inAsync = node.async;
let oldInAsync = this.state.inAsync;
this.state.inAsync = node.async;
if (isExpression) {
node.body = this.parseMaybeAssign();
node.expression = true;
@@ -782,21 +809,23 @@ pp.parseFunctionBody = function (node, allowExpression) {
node.expression = false;
this.state.inFunction = oldInFunc; this.state.inGenerator = oldInGen; this.state.labels = oldLabels;
}
this.inAsync = oldInAsync;
this.state.inAsync = oldInAsync;
// If this is a strict mode function, verify that argument names
// are not repeated, and it does not try to bind the words `eval`
// or `arguments`.
var checkLVal = this.state.strict;
var checkLValStrict = false;
let checkLVal = this.state.strict;
let checkLValStrict = false;
let isStrict = false;
// arrow function
if (allowExpression) checkLVal = true;
// normal function
if (!isExpression && node.body.directives.length) {
for (var directive of (node.body.directives: Array<Object>)) {
if (directive.value === "use strict") {
for (let directive of (node.body.directives: Array<Object>)) {
if (directive.value.value === "use strict") {
isStrict = true;
checkLVal = true;
checkLValStrict = true;
break;
@@ -804,6 +833,11 @@ pp.parseFunctionBody = function (node, allowExpression) {
}
}
//
if (isStrict && node.id && node.id.type === "Identifier" && node.id.name === "yield") {
this.raise(node.id.start, "Binding yield in strict mode");
}
if (checkLVal) {
let nameHash = Object.create(null);
let oldStrict = this.state.strict;
@@ -870,6 +904,10 @@ pp.parseIdentifier = function (liberal) {
this.unexpected();
}
if (!liberal && node.name === "await" && this.state.inAsync) {
this.raise(node.start, "invalid use of await inside of an async function");
}
this.next();
return this.finishNode(node, "Identifier");
};
@@ -899,25 +937,3 @@ pp.parseYield = function () {
}
return this.finishNode(node, "YieldExpression");
};
// Parses array and generator comprehensions.
pp.parseComprehension = function (node, isGenerator) {
node.blocks = [];
while (this.match(tt._for)) {
let block = this.startNode();
this.next();
this.expect(tt.parenL);
block.left = this.parseBindingAtom();
this.checkLVal(block.left, true);
this.expectContextual("of");
block.right = this.parseExpression();
this.expect(tt.parenR);
node.blocks.push(this.finishNode(block, "ComprehensionBlock"));
}
node.filter = this.eat(tt._if) ? this.parseParenExpression() : null;
node.body = this.parseExpression();
this.expect(isGenerator ? tt.parenR : tt.bracketR);
node.generator = isGenerator;
return this.finishNode(node, "ComprehensionExpression");
};

View File

@@ -4,8 +4,6 @@ import { reservedWords } from "../util/identifier";
import { getOptions } from "../options";
import Tokenizer from "../tokenizer";
// Registered plugins
export const plugins = {};
export default class Parser extends Tokenizer {
@@ -14,12 +12,10 @@ export default class Parser extends Tokenizer {
super(options, input);
this.options = options;
this.inModule = this.options.sourceType === "module";
this.isReservedWord = reservedWords[6];
this.input = input;
this.loadPlugins(this.options.plugins);
// Figure out if it's a module code.
this.inModule = this.options.sourceType === "module";
this.plugins = this.loadPlugins(this.options.plugins);
// If enabled, skip leading hashbang line.
if (this.state.pos === 0 && this.input[0] === "#" && this.input[1] === "!") {
@@ -27,20 +23,31 @@ export default class Parser extends Tokenizer {
}
}
hasFeature(name: string): boolean {
return !!this.options.features[name];
hasPlugin(name: string): boolean {
return !!(this.plugins["*"] || this.plugins[name]);
}
extend(name: string, f: Function) {
this[name] = f(this[name]);
}
loadPlugins(plugins) {
for (let name in plugins) {
let plugin = exports.plugins[name];
if (!plugin) throw new Error(`Plugin '${name}' not found`);
plugin(this, plugins[name]);
loadPlugins(plugins: Array<string>) {
let pluginMap = {};
if (plugins.indexOf("flow") >= 0) {
// ensure flow plugin loads last
plugins.splice(plugins.indexOf("flow"), 1);
plugins.push("flow");
}
for (let name of plugins) {
pluginMap[name] = true;
let plugin = exports.plugins[name];
if (plugin) plugin(this);
}
return pluginMap;
}
parse(): {

View File

@@ -22,8 +22,18 @@ pp.toAssignable = function (node, isBinding) {
node.type = "ObjectPattern";
for (let prop of (node.properties: Array<Object>)) {
if (prop.type === "SpreadProperty") continue;
if (prop.kind !== "init") this.raise(prop.key.start, "Object pattern can't contain getter or setter");
this.toAssignable(prop.value, isBinding);
if (prop.type === "ObjectMethod") {
if (prop.kind === "get" || prop.kind === "set") {
this.raise(prop.key.start, "Object pattern can't contain getter or setter");
} else {
this.raise(prop.key.start, "Object pattern can't contain methods");
}
}
if (prop.type === "ObjectProperty") {
this.toAssignable(prop.value, isBinding);
}
}
break;
@@ -94,14 +104,25 @@ pp.parseSpread = function (refShorthandDefaultPos) {
pp.parseRest = function () {
let node = this.startNode();
this.next();
node.argument = this.parseIdentifier();
node.argument = this.parseBindingIdentifier();
return this.finishNode(node, "RestElement");
};
pp.shouldAllowYieldIdentifier = function () {
return this.match(tt._yield) && !this.state.strict && !this.state.inGenerator;
};
pp.parseBindingIdentifier = function () {
return this.parseIdentifier(this.shouldAllowYieldIdentifier());
};
// Parses lvalue (assignable) atom.
pp.parseBindingAtom = function () {
switch (this.state.type) {
case tt._yield:
if (this.state.strict || this.state.inGenerator) this.unexpected();
case tt.name:
return this.parseIdentifier(true);
@@ -120,7 +141,8 @@ pp.parseBindingAtom = function () {
};
pp.parseBindingList = function (close, allowEmpty, allowTrailingComma) {
var elts = [], first = true;
let elts = [];
let first = true;
while (!this.eat(close)) {
if (first) {
first = false;
@@ -136,7 +158,7 @@ pp.parseBindingList = function (close, allowEmpty, allowTrailingComma) {
this.expect(close);
break;
} else {
var left = this.parseMaybeDefault();
let left = this.parseMaybeDefault();
this.parseAssignableListItemTypes(left);
elts.push(this.parseMaybeDefault(null, null, left));
}
@@ -187,7 +209,7 @@ pp.checkLVal = function (expr, isBinding, checkClashes) {
case "ObjectPattern":
for (let prop of (expr.properties: Array<Object>)) {
if (prop.type === "Property") prop = prop.value;
if (prop.type === "ObjectProperty") prop = prop.value;
this.checkLVal(prop, isBinding, checkClashes);
}
break;

View File

@@ -21,8 +21,8 @@ class Node {
loc: SourceLocation;
__clone(): Node {
var node2 = new Node;
for (var key in this) node2[key] = this[key];
let node2 = new Node;
for (let key in this) node2[key] = this[key];
return node2;
}
}

View File

@@ -16,7 +16,7 @@ const pp = Parser.prototype;
pp.parseTopLevel = function (file, program) {
program.sourceType = this.options.sourceType;
this.parseBlockBody(program, true, tt.eof);
this.parseBlockBody(program, true, true, tt.eof);
file.program = this.finishNode(program, "Program");
file.comments = this.state.comments;
@@ -30,11 +30,21 @@ const loopLabel = {kind: "loop"}, switchLabel = {kind: "switch"};
// TODO
pp.parseDirective = function () {
let node = this.startNode();
node.raw = this.input.slice(this.state.start, this.state.end);
node.value = node.raw.slice(1, -1); // remove quotes
let directiveLiteral = this.startNode();
let directive = this.startNode();
let raw = this.input.slice(this.state.start, this.state.end);
let val = directiveLiteral.value = raw.slice(1, -1); // remove quotes
this.addExtra(directiveLiteral, "raw", raw);
this.addExtra(directiveLiteral, "rawValue", val);
this.next();
return this.finishNode(node, "Directive");
directive.value = this.finishNode(directiveLiteral, "DirectiveLiteral");
this.semicolon();
return this.finishNode(directive, "Directive");
};
// Parse a single statement.
@@ -89,16 +99,18 @@ pp.parseStatement = function (declaration, topLevel) {
case tt._export:
case tt._import:
if (!this.options.allowImportExportEverywhere) {
if (!topLevel)
if (!topLevel) {
this.raise(this.state.start, "'import' and 'export' may only appear at the top level");
}
if (!this.inModule)
if (!this.inModule) {
this.raise(this.state.start, "'import' and 'export' may appear only with 'sourceType: module'");
}
}
return starttype === tt._import ? this.parseImport(node) : this.parseExport(node);
case tt.name:
if (this.hasFeature("asyncFunctions") && this.state.value === "async") {
if (this.hasPlugin("asyncFunctions") && this.state.value === "async") {
// peek ahead and see if next token is a function
let state = this.state.clone();
this.next();
@@ -147,7 +159,7 @@ pp.parseDecorators = function (allowExport) {
};
pp.parseDecorator = function () {
if (!this.hasFeature("decorators")) {
if (!this.hasPlugin("decorators")) {
this.unexpected();
}
let node = this.startNode();
@@ -171,7 +183,8 @@ pp.parseBreakContinueStatement = function (node, keyword) {
// Verify that there is an actual destination to break or
// continue to.
for (var i = 0; i < this.state.labels.length; ++i) {
let i;
for (i = 0; i < this.state.labels.length; ++i) {
let lab = this.state.labels[i];
if (node.label == null || lab.name === node.label.name) {
if (lab.kind != null && (isBreak || lab.kind === "loop")) break;
@@ -288,7 +301,8 @@ pp.parseSwitchStatement = function (node) {
// nodes. `cur` is used to keep the node that we are currently
// adding statements to.
for (var cur, sawDefault; !this.match(tt.braceR); ) {
let cur;
for (let sawDefault; !this.match(tt.braceR); ) {
if (this.match(tt._case) || this.match(tt._default)) {
let isCase = this.match(tt._case);
if (cur) this.finishNode(cur, "SwitchCase");
@@ -304,8 +318,11 @@ pp.parseSwitchStatement = function (node) {
}
this.expect(tt.colon);
} else {
if (!cur) this.unexpected();
cur.consequent.push(this.parseStatement(true));
if (cur) {
cur.consequent.push(this.parseStatement(true));
} else {
this.unexpected();
}
}
}
if (cur) this.finishNode(cur, "SwitchCase");
@@ -325,19 +342,23 @@ pp.parseThrowStatement = function (node) {
// Reused empty array added for node fields that are always empty.
var empty = [];
let empty = [];
pp.parseTryStatement = function (node) {
this.next();
node.block = this.parseBlock();
node.handler = null;
if (this.match(tt._catch)) {
let clause = this.startNode();
this.next();
this.expect(tt.parenL);
clause.param = this.parseBindingAtom();
this.checkLVal(clause.param, true, Object.create(null));
this.expect(tt.parenR);
clause.body = this.parseBlock();
node.handler = this.finishNode(clause, "CatchClause");
}
@@ -416,35 +437,55 @@ pp.parseExpressionStatement = function (node, expr) {
// strict"` declarations when `allowStrict` is true (used for
// function bodies).
pp.parseBlock = function (allowStrict) {
pp.parseBlock = function (allowDirectives?) {
let node = this.startNode();
this.expect(tt.braceL);
this.parseBlockBody(node, allowStrict, tt.braceR);
this.parseBlockBody(node, allowDirectives, false, tt.braceR);
return this.finishNode(node, "BlockStatement");
};
// TODO
pp.parseBlockBody = function (node, allowStrict, end) {
pp.parseBlockBody = function (node, allowDirectives, topLevel, end) {
node.body = [];
node.directives = [];
let parsedNonDirective = false;
let oldStrict;
let octalPosition;
while (!this.eat(end)) {
if (!parsedNonDirective && this.match(tt.string)) {
let stmt = this.parseDirective();
node.directives.push(stmt);
if (allowDirectives && !parsedNonDirective && this.match(tt.string)) {
let oldState = this.state;
let lookahead = this.lookahead();
this.state = lookahead;
let isDirective = this.isLineTerminator();
this.state = oldState;
if (allowStrict && stmt.value === "use strict") {
oldStrict = this.state.strict;
this.setStrict(this.state.strict = true);
if (isDirective) {
if (this.state.containsOctal && !octalPosition) {
octalPosition = this.state.octalPosition;
}
let stmt = this.parseDirective();
node.directives.push(stmt);
if (allowDirectives && stmt.value.value === "use strict") {
oldStrict = this.state.strict;
this.state.strict = true;
this.setStrict(true);
if (octalPosition) {
this.raise(octalPosition, "Octal literal in strict mode");
}
}
continue;
}
} else {
parsedNonDirective = true;
node.body.push(this.parseStatement(true));
}
parsedNonDirective = true;
node.body.push(this.parseStatement(true, topLevel));
}
if (oldStrict === false) {
@@ -514,25 +555,39 @@ pp.parseVarHead = function (decl) {
// `isStatement` parameter).
pp.parseFunction = function (node, isStatement, allowExpressionBody, isAsync, optionalId) {
this.initFunction(node, isAsync);
node.generator = this.eat(tt.star);
let oldInMethod = this.state.inMethod;
this.state.inMethod = false;
if (isStatement && !optionalId && !this.match(tt.name)) {
this.initFunction(node, isAsync);
if (this.match(tt.star)) {
if (node.async && !this.hasPlugin("asyncGenerators")) {
this.unexpected();
} else {
node.generator = true;
this.next();
}
}
if (isStatement && !optionalId && !this.match(tt.name) && !this.match(tt._yield)) {
this.unexpected();
}
if (this.match(tt.name)) {
node.id = this.parseIdentifier();
if (this.match(tt.name) || this.match(tt._yield)) {
node.id = this.parseBindingIdentifier();
}
this.parseFunctionParams(node);
this.parseFunctionBody(node, allowExpressionBody);
this.state.inMethod = oldInMethod;
return this.finishNode(node, isStatement ? "FunctionDeclaration" : "FunctionExpression");
};
pp.parseFunctionParams = function (node) {
this.expect(tt.parenL);
node.params = this.parseBindingList(tt.parenR, false, this.hasFeature("trailingFunctionCommas"));
node.params = this.parseBindingList(tt.parenR, false, this.hasPlugin("trailingFunctionCommas"));
};
// Parse a class declaration or literal (depending on the
@@ -542,51 +597,99 @@ pp.parseClass = function (node, isStatement, optionalId) {
this.next();
this.parseClassId(node, isStatement, optionalId);
this.parseClassSuper(node);
var classBody = this.startNode();
this.parseClassBody(node);
return this.finishNode(node, isStatement ? "ClassDeclaration" : "ClassExpression");
};
pp.isClassProperty = function () {
return this.match(tt.eq) || this.isLineTerminator();
};
pp.parseClassBody = function (node) {
// class bodies are implicitly strict
let oldStrict = this.state.strict;
this.state.strict = true;
let hadConstructorCall = false;
let hadConstructor = false;
classBody.body = [];
this.expect(tt.braceL);
let decorators = [];
let classBody = this.startNode();
classBody.body = [];
this.expect(tt.braceL);
while (!this.eat(tt.braceR)) {
if (this.eat(tt.semi)) continue;
if (this.eat(tt.semi)) {
continue;
}
if (this.match(tt.at)) {
decorators.push(this.parseDecorator());
continue;
}
var method = this.startNode();
let method = this.startNode();
// steal the decorators if there are any
if (decorators.length) {
method.decorators = decorators;
decorators = [];
}
let isConstructorCall = false;
let isMaybeStatic = this.match(tt.name) && this.state.value === "static";
var isGenerator = this.eat(tt.star), isAsync = false;
let isGenerator = this.eat(tt.star);
let isGetSet = false;
let isAsync = false;
this.parsePropertyName(method);
method.static = isMaybeStatic && !this.match(tt.parenL);
if (method.static) {
if (isGenerator) this.unexpected();
isGenerator = this.eat(tt.star);
this.parsePropertyName(method);
}
if (!isGenerator && method.key.type === "Identifier" && !method.computed && this.isClassProperty()) {
classBody.body.push(this.parseClassProperty(method));
continue;
if (!isGenerator && method.key.type === "Identifier" && !method.computed) {
if (this.isClassProperty()) {
classBody.body.push(this.parseClassProperty(method));
continue;
}
if (this.hasPlugin("classConstructorCall") && method.key.name === "call" && this.match(tt.name) && this.state.value === "constructor") {
isConstructorCall = true;
this.parsePropertyName(method);
}
}
if (this.hasFeature("asyncFunctions") && !this.match(tt.parenL) &&
!method.computed && method.key.type === "Identifier" && method.key.name === "async") {
let isAsyncMethod = this.hasPlugin("asyncFunctions") && !this.match(tt.parenL) && !method.computed && method.key.type === "Identifier" && method.key.name === "async";
if (isAsyncMethod) {
if (this.hasPlugin("asyncGenerators") && this.eat(tt.star)) isGenerator = true;
isAsync = true;
this.parsePropertyName(method);
}
let isGetSet = false;
method.kind = "method";
if (!method.computed) {
let {key} = method;
let { key } = method;
// handle get/set methods
// eg. class Foo { get bar() {} set bar() {} }
if (!isAsync && !isGenerator && key.type === "Identifier" && !this.match(tt.parenL) && (key.name === "get" || key.name === "set")) {
isGetSet = true;
method.kind = key.name;
key = this.parsePropertyName(method);
}
if (!method.static && (key.type === "Identifier" && key.name === "constructor" ||
key.type === "StringLiteral" && key.value === "constructor")) {
// disallow invalid constructors
let isConstructor = !isConstructorCall && !method.static && (
(key.type === "Identifier" && key.name === "constructor") ||
(key.type === "StringLiteral" && key.value === "constructor")
);
if (isConstructor) {
if (hadConstructor) this.raise(key.start, "Duplicate constructor in the same class");
if (isGetSet) this.raise(key.start, "Constructor can't have get/set modifier");
if (isGenerator) this.raise(key.start, "Constructor can't be a generator");
@@ -594,15 +697,37 @@ pp.parseClass = function (node, isStatement, optionalId) {
method.kind = "constructor";
hadConstructor = true;
}
// disallow static prototype method
let isStaticPrototype = method.static && (
(key.type === "Identifier" && key.name === "prototype") ||
(key.type === "StringLiteral" && key.value === "prototype")
);
if (isStaticPrototype) {
this.raise(key.start, "Classes may not have static property named prototype");
}
}
if (method.kind === "constructor" && method.decorators) {
// convert constructor to a constructor call
if (isConstructorCall) {
if (hadConstructorCall) this.raise(method.start, "Duplicate constructor call in the same class");
method.kind = "constructorCall";
hadConstructorCall = true;
}
// disallow decorators on class constructors
if ((method.kind === "constructor" || method.kind === "constructorCall") && method.decorators) {
this.raise(method.start, "You can't attach decorators to a class constructor");
}
this.parseClassMethod(classBody, method, isGenerator, isAsync);
// get methods aren't allowed to have any parameters
// set methods must have exactly 1 parameter
if (isGetSet) {
let paramCount = method.kind === "get" ? 0 : 1;
if (method.value.params.length !== paramCount) {
let start = method.value.start;
if (method.params.length !== paramCount) {
let start = method.start;
if (method.kind === "get") {
this.raise(start, "getter should have no params");
} else {
@@ -617,16 +742,13 @@ pp.parseClass = function (node, isStatement, optionalId) {
}
node.body = this.finishNode(classBody, "ClassBody");
return this.finishNode(node, isStatement ? "ClassDeclaration" : "ClassExpression");
};
pp.isClassProperty = function () {
return this.match(tt.eq) || (this.match(tt.semi) || this.canInsertSemicolon());
this.state.strict = oldStrict;
};
pp.parseClassProperty = function (node) {
if (this.match(tt.eq)) {
if (!this.hasFeature("classProperties")) this.unexpected();
if (!this.hasPlugin("classProperties")) this.unexpected();
this.next();
node.value = this.parseMaybeAssign();
} else {
@@ -637,8 +759,8 @@ pp.parseClassProperty = function (node) {
};
pp.parseClassMethod = function (classBody, method, isGenerator, isAsync) {
method.value = this.parseMethod(isGenerator, isAsync);
classBody.body.push(this.finishNode(method, "MethodDefinition"));
this.parseMethod(method, isGenerator, isAsync);
classBody.body.push(this.finishNode(method, "ClassMethod"));
};
pp.parseClassId = function (node, isStatement, optionalId) {
@@ -665,7 +787,7 @@ pp.parseExport = function (node) {
if (this.match(tt.star)) {
let specifier = this.startNode();
this.next();
if (this.hasFeature("exportExtensions") && this.eatContextual("as")) {
if (this.hasPlugin("exportExtensions") && this.eatContextual("as")) {
specifier.exported = this.parseIdentifier();
node.specifiers = [this.finishNode(specifier, "ExportNamespaceSpecifier")];
this.parseExportSpecifiersMaybe(node);
@@ -674,7 +796,7 @@ pp.parseExport = function (node) {
this.parseExportFrom(node, true);
return this.finishNode(node, "ExportAllDeclaration");
}
} else if (this.hasFeature("exportExtensions") && this.isExportDefaultSpecifier()) {
} else if (this.hasPlugin("exportExtensions") && this.isExportDefaultSpecifier()) {
let specifier = this.startNode();
specifier.exported = this.parseIdentifier(true);
node.specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")];
@@ -730,7 +852,7 @@ pp.isExportDefaultSpecifier = function () {
return false;
}
var lookahead = this.lookahead();
let lookahead = this.lookahead();
return lookahead.type === tt.comma || (lookahead.type === tt.name && lookahead.value === "from");
};
@@ -756,12 +878,12 @@ pp.parseExportFrom = function (node, expect?) {
};
pp.shouldParseExportDeclaration = function () {
return this.hasFeature("asyncFunctions") && this.isContextual("async");
return this.hasPlugin("asyncFunctions") && this.isContextual("async");
};
pp.checkExport = function (node) {
if (this.state.decorators.length) {
var isClass = node.declaration && (node.declaration.type === "ClassDeclaration" || node.declaration.type === "ClassExpression");
let isClass = node.declaration && (node.declaration.type === "ClassDeclaration" || node.declaration.type === "ClassExpression");
if (!node.declaration || !isClass) {
this.raise(node.start, "You can only use decorators on an export when exporting a class");
}
@@ -826,10 +948,10 @@ pp.parseImport = function (node) {
// Parses a comma-separated list of module imports.
pp.parseImportSpecifiers = function (node) {
var first = true;
let first = true;
if (this.match(tt.name)) {
// import defaultObj, { x, y as z } from '...'
var startPos = this.state.start, startLoc = this.state.startLoc;
let startPos = this.state.start, startLoc = this.state.startLoc;
node.specifiers.push(this.parseImportSpecifierDefault(this.parseIdentifier(), startPos, startLoc));
if (!this.eat(tt.comma)) return;
}
@@ -862,7 +984,7 @@ pp.parseImportSpecifiers = function (node) {
};
pp.parseImportSpecifierDefault = function (id, startPos, startLoc) {
var node = this.startNodeAt(startPos, startLoc);
let node = this.startNodeAt(startPos, startLoc);
node.local = id;
this.checkLVal(node.local, true);
return this.finishNode(node, "ImportDefaultSpecifier");

View File

@@ -13,7 +13,7 @@ const pp = Parser.prototype;
pp.addExtra = function (node, key, val) {
if (!node) return;
var extra = node.extra = node.extra || {};
let extra = node.extra = node.extra || {};
extra[key] = val;
};