From 6773279039f88cdbca5bc3fc0b930c0c07a894c7 Mon Sep 17 00:00:00 2001 From: "greenkeeper[bot]" Date: Tue, 10 Jan 2017 19:42:43 +0100 Subject: [PATCH] =?UTF-8?q?Update=20eslint-config-babel=20to=20the=20lates?= =?UTF-8?q?t=20version=20=F0=9F=9A=80=20(#273)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * chore(package): update eslint-config-babel to version 4.0.0 https://greenkeeper.io/ * Fix prefer-const * Update package.json --- package.json | 2 +- src/options.js | 4 +- src/parser/comments.js | 4 +- src/parser/expression.js | 169 ++++++++++++++++++++++----------------- src/parser/index.js | 12 +-- src/parser/location.js | 4 +- src/parser/lval.js | 26 +++--- src/parser/node.js | 2 +- src/parser/statement.js | 107 +++++++++++++------------ src/parser/util.js | 2 +- src/plugins/flow.js | 121 ++++++++++++++-------------- src/plugins/jsx/index.js | 53 ++++++------ src/tokenizer/context.js | 4 +- src/tokenizer/index.js | 102 ++++++++++++----------- src/tokenizer/state.js | 4 +- src/util/location.js | 2 +- yarn.lock | 18 ++--- 17 files changed, 338 insertions(+), 298 deletions(-) diff --git a/package.json b/package.json index df4765f2bf..c1544b013e 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,7 @@ "codecov": "^1.0.1", "cross-env": "^2.0.0", "eslint": "^3.7.1", - "eslint-config-babel": "^3.0.0", + "eslint-config-babel": "^4.0.1", "eslint-plugin-babel": "^4.0.0", "eslint-plugin-flowtype": "^2.20.0", "flow-bin": "^0.37.0", diff --git a/src/options.js b/src/options.js index 59bd2d35d7..8e39e451e8 100755 --- a/src/options.js +++ b/src/options.js @@ -31,8 +31,8 @@ export const defaultOptions: { // Interpret and default an options object export function getOptions(opts?: Object): Object { - let options = {}; - for (let key in defaultOptions) { + const options = {}; + for (const key in defaultOptions) { options[key] = opts && key in opts ? opts[key] : defaultOptions[key]; } return options; diff --git a/src/parser/comments.js b/src/parser/comments.js index 648c5601f7..c032e85e07 100644 --- a/src/parser/comments.js +++ b/src/parser/comments.js @@ -41,7 +41,7 @@ pp.addComment = function (comment) { pp.processComment = function (node) { if (node.type === "Program" && node.body.length > 0) return; - let stack = this.state.commentStack; + const stack = this.state.commentStack; let lastChild, trailingComments, i, j; @@ -63,7 +63,7 @@ pp.processComment = function (node) { this.state.trailingComments.length = 0; } } else { - let lastInStack = last(stack); + const lastInStack = last(stack); if (stack.length > 0 && lastInStack.trailingComments && lastInStack.trailingComments[0].start >= node.end) { trailingComments = lastInStack.trailingComments; lastInStack.trailingComments = null; diff --git a/src/parser/expression.js b/src/parser/expression.js index 1e9f4197e3..9deb74c686 100644 --- a/src/parser/expression.js +++ b/src/parser/expression.js @@ -33,7 +33,7 @@ const pp = Parser.prototype; pp.checkPropClash = function (prop, propHash) { if (prop.computed) return; - let key = prop.key; + const key = prop.key; let name; switch (key.type) { case "Identifier": @@ -72,10 +72,11 @@ pp.checkPropClash = function (prop, propHash) { // delayed syntax error at correct position). pp.parseExpression = function (noIn, refShorthandDefaultPos) { - let startPos = this.state.start, startLoc = this.state.startLoc; - let expr = this.parseMaybeAssign(noIn, refShorthandDefaultPos); + const startPos = this.state.start; + const startLoc = this.state.startLoc; + const expr = this.parseMaybeAssign(noIn, refShorthandDefaultPos); if (this.match(tt.comma)) { - let node = this.startNodeAt(startPos, startLoc); + const node = this.startNodeAt(startPos, startLoc); node.expressions = [expr]; while (this.eat(tt.comma)) { node.expressions.push(this.parseMaybeAssign(noIn, refShorthandDefaultPos)); @@ -90,8 +91,8 @@ pp.parseExpression = function (noIn, refShorthandDefaultPos) { // operators like `+=`. pp.parseMaybeAssign = function (noIn, refShorthandDefaultPos, afterLeftParse, refNeedsArrowPos) { - let startPos = this.state.start; - let startLoc = this.state.startLoc; + const startPos = this.state.start; + const startLoc = this.state.startLoc; if (this.match(tt._yield) && this.state.inGenerator) { let left = this.parseYield(); @@ -114,7 +115,7 @@ pp.parseMaybeAssign = function (noIn, refShorthandDefaultPos, afterLeftParse, re let left = this.parseMaybeConditional(noIn, refShorthandDefaultPos, refNeedsArrowPos); if (afterLeftParse) left = afterLeftParse.call(this, left, startPos, startLoc); if (this.state.type.isAssign) { - let node = this.startNodeAt(startPos, startLoc); + const node = this.startNodeAt(startPos, startLoc); node.operator = this.state.value; node.left = this.match(tt.eq) ? this.toAssignable(left, undefined, "assignment expression") : left; refShorthandDefaultPos.start = 0; // reset because shorthand default was used correctly @@ -146,8 +147,9 @@ pp.parseMaybeAssign = function (noIn, refShorthandDefaultPos, afterLeftParse, re // Parse a ternary conditional (`?:`) operator. pp.parseMaybeConditional = function (noIn, refShorthandDefaultPos, refNeedsArrowPos) { - let startPos = this.state.start, startLoc = this.state.startLoc; - let expr = this.parseExprOps(noIn, refShorthandDefaultPos); + const startPos = this.state.start; + const startLoc = this.state.startLoc; + const expr = this.parseExprOps(noIn, refShorthandDefaultPos); if (refShorthandDefaultPos && refShorthandDefaultPos.start) return expr; return this.parseConditional(expr, noIn, startPos, startLoc, refNeedsArrowPos); @@ -155,7 +157,7 @@ pp.parseMaybeConditional = function (noIn, refShorthandDefaultPos, refNeedsArrow pp.parseConditional = function (expr, noIn, startPos, startLoc) { if (this.eat(tt.question)) { - let node = this.startNodeAt(startPos, startLoc); + const node = this.startNodeAt(startPos, startLoc); node.test = expr; node.consequent = this.parseMaybeAssign(); this.expect(tt.colon); @@ -168,8 +170,9 @@ pp.parseConditional = function (expr, noIn, startPos, startLoc) { // Start the precedence parser. pp.parseExprOps = function (noIn, refShorthandDefaultPos) { - let startPos = this.state.start, startLoc = this.state.startLoc; - let expr = this.parseMaybeUnary(refShorthandDefaultPos); + const startPos = this.state.start; + const startLoc = this.state.startLoc; + const expr = this.parseMaybeUnary(refShorthandDefaultPos); if (refShorthandDefaultPos && refShorthandDefaultPos.start) { return expr; } else { @@ -184,10 +187,10 @@ pp.parseExprOps = function (noIn, refShorthandDefaultPos) { // operator that has a lower precedence than the set it is parsing. pp.parseExprOp = function(left, leftStartPos, leftStartLoc, minPrec, noIn) { - let prec = this.state.type.binop; + const prec = this.state.type.binop; if (prec != null && (!noIn || !this.match(tt._in))) { if (prec > minPrec) { - let node = this.startNodeAt(leftStartPos, leftStartLoc); + const node = this.startNodeAt(leftStartPos, leftStartLoc); node.left = left; node.operator = this.state.value; @@ -201,11 +204,11 @@ pp.parseExprOp = function(left, leftStartPos, leftStartLoc, minPrec, noIn) { this.raise(left.argument.start, "Illegal expression. Wrap left hand side or entire exponentiation in parentheses."); } - let op = this.state.type; + const op = this.state.type; this.next(); - let startPos = this.state.start; - let startLoc = this.state.startLoc; + const startPos = this.state.start; + const startLoc = this.state.startLoc; node.right = this.parseExprOp(this.parseMaybeUnary(), startPos, startLoc, op.rightAssociative ? prec - 1 : prec, noIn); this.finishNode(node, (op === tt.logicalOR || op === tt.logicalAND) ? "LogicalExpression" : "BinaryExpression"); @@ -219,13 +222,13 @@ pp.parseExprOp = function(left, leftStartPos, leftStartLoc, minPrec, noIn) { pp.parseMaybeUnary = function (refShorthandDefaultPos) { if (this.state.type.prefix) { - let node = this.startNode(); - let update = this.match(tt.incDec); + const node = this.startNode(); + const update = this.match(tt.incDec); node.operator = this.state.value; node.prefix = true; this.next(); - let argType = this.state.type; + const argType = this.state.type; node.argument = this.parseMaybeUnary(); this.addExtra(node, "parenthesizedArgument", argType === tt.parenL && (!node.argument.extra || !node.argument.extra.parenthesized)); @@ -243,11 +246,12 @@ pp.parseMaybeUnary = function (refShorthandDefaultPos) { return this.finishNode(node, update ? "UpdateExpression" : "UnaryExpression"); } - let startPos = this.state.start, startLoc = this.state.startLoc; + const startPos = this.state.start; + const startLoc = this.state.startLoc; let expr = this.parseExprSubscripts(refShorthandDefaultPos); if (refShorthandDefaultPos && refShorthandDefaultPos.start) return expr; while (this.state.type.postfix && !this.canInsertSemicolon()) { - let node = this.startNodeAt(startPos, startLoc); + const node = this.startNodeAt(startPos, startLoc); node.operator = this.state.value; node.prefix = false; node.argument = expr; @@ -261,9 +265,10 @@ pp.parseMaybeUnary = function (refShorthandDefaultPos) { // Parse call, dot, and `[]`-subscript expressions. pp.parseExprSubscripts = function (refShorthandDefaultPos) { - let startPos = this.state.start, startLoc = this.state.startLoc; - let potentialArrowAt = this.state.potentialArrowAt; - let expr = this.parseExprAtom(refShorthandDefaultPos); + const startPos = this.state.start; + const startLoc = this.state.startLoc; + const potentialArrowAt = this.state.potentialArrowAt; + const expr = this.parseExprAtom(refShorthandDefaultPos); if (expr.type === "ArrowFunctionExpression" && expr.start === potentialArrowAt) { return expr; @@ -279,28 +284,28 @@ pp.parseExprSubscripts = function (refShorthandDefaultPos) { pp.parseSubscripts = function (base, startPos, startLoc, noCalls) { for (;;) { if (!noCalls && this.eat(tt.doubleColon)) { - let node = this.startNodeAt(startPos, startLoc); + const node = this.startNodeAt(startPos, startLoc); node.object = base; node.callee = this.parseNoCallExpr(); return this.parseSubscripts(this.finishNode(node, "BindExpression"), startPos, startLoc, noCalls); } else if (this.eat(tt.dot)) { - let node = this.startNodeAt(startPos, startLoc); + const node = this.startNodeAt(startPos, startLoc); node.object = base; node.property = this.parseIdentifier(true); node.computed = false; base = this.finishNode(node, "MemberExpression"); } else if (this.eat(tt.bracketL)) { - let node = this.startNodeAt(startPos, startLoc); + const node = this.startNodeAt(startPos, startLoc); node.object = base; node.property = this.parseExpression(); node.computed = true; this.expect(tt.bracketR); base = this.finishNode(node, "MemberExpression"); } else if (!noCalls && this.match(tt.parenL)) { - let possibleAsync = this.state.potentialArrowAt === base.start && base.type === "Identifier" && base.name === "async" && !this.canInsertSemicolon(); + const possibleAsync = this.state.potentialArrowAt === base.start && base.type === "Identifier" && base.name === "async" && !this.canInsertSemicolon(); this.next(); - let node = this.startNodeAt(startPos, startLoc); + const node = this.startNodeAt(startPos, startLoc); node.callee = base; node.arguments = this.parseCallExpressionArguments(tt.parenR, possibleAsync); if (node.callee.type === "Import" && node.arguments.length !== 1) { @@ -314,7 +319,7 @@ pp.parseSubscripts = function (base, startPos, startLoc, noCalls) { this.toReferencedList(node.arguments); } } else if (this.match(tt.backQuote)) { - let node = this.startNodeAt(startPos, startLoc); + const node = this.startNodeAt(startPos, startLoc); node.tag = base; node.quasi = this.parseTemplate(); base = this.finishNode(node, "TaggedTemplateExpression"); @@ -325,9 +330,10 @@ pp.parseSubscripts = function (base, startPos, startLoc, noCalls) { }; pp.parseCallExpressionArguments = function (close, possibleAsyncArrow) { + const elts = []; let innerParenStart; + let first = true; - let elts = [], first = true; while (!this.eat(close)) { if (first) { first = false; @@ -364,7 +370,8 @@ pp.parseAsyncArrowFromCallExpression = function (node, call) { // Parse a no-call expression (like argument of `new` or `::` operators). pp.parseNoCallExpr = function () { - let startPos = this.state.start, startLoc = this.state.startLoc; + const startPos = this.state.start; + const startLoc = this.state.startLoc; return this.parseSubscripts(this.parseExprAtom(), startPos, startLoc, true); }; @@ -374,7 +381,9 @@ pp.parseNoCallExpr = function () { // or `{}`. pp.parseExprAtom = function (refShorthandDefaultPos) { - let node, canBeArrow = this.state.potentialArrowAt === this.state.start; + const canBeArrow = this.state.potentialArrowAt === this.state.start; + let node; + switch (this.state.type) { case tt._super: if (!this.state.inMethod && !this.options.allowSuperOutsideMethod) { @@ -411,9 +420,9 @@ pp.parseExprAtom = function (refShorthandDefaultPos) { case tt.name: node = this.startNode(); - let allowAwait = this.state.value === "await" && this.state.inAsync; - let allowYield = this.shouldAllowYieldIdentifier(); - let id = this.parseIdentifier(allowAwait || allowYield); + const allowAwait = this.state.value === "await" && this.state.inAsync; + const allowYield = this.shouldAllowYieldIdentifier(); + const id = this.parseIdentifier(allowAwait || allowYield); if (id.name === "await") { if (this.state.inAsync || this.inModule) { @@ -423,7 +432,7 @@ pp.parseExprAtom = function (refShorthandDefaultPos) { this.next(); return this.parseFunction(node, false, false, true); } else if (canBeArrow && id.name === "async" && this.match(tt.name)) { - let params = [this.parseIdentifier()]; + const params = [this.parseIdentifier()]; this.expect(tt.arrow); // let foo = bar => {}; return this.parseArrowExpression(node, params, true); @@ -437,10 +446,10 @@ pp.parseExprAtom = function (refShorthandDefaultPos) { case tt._do: if (this.hasPlugin("doExpressions")) { - let node = this.startNode(); + const node = this.startNode(); this.next(); - let oldInFunction = this.state.inFunction; - let oldLabels = this.state.labels; + const oldInFunction = this.state.inFunction; + const oldLabels = this.state.labels; this.state.labels = []; this.state.inFunction = false; node.body = this.parseBlock(false, true); @@ -450,7 +459,7 @@ pp.parseExprAtom = function (refShorthandDefaultPos) { } case tt.regexp: - let value = this.state.value; + const value = this.state.value; node = this.parseLiteral(value.value, "RegExpLiteral"); node.pattern = value.pattern; node.flags = value.flags; @@ -507,7 +516,7 @@ pp.parseExprAtom = function (refShorthandDefaultPos) { node = this.startNode(); this.next(); node.object = null; - let callee = node.callee = this.parseNoCallExpr(); + const callee = node.callee = this.parseNoCallExpr(); if (callee.type === "MemberExpression") { return this.finishNode(node, "BindExpression"); } else { @@ -520,8 +529,8 @@ pp.parseExprAtom = function (refShorthandDefaultPos) { }; pp.parseFunctionExpression = function () { - let node = this.startNode(); - let meta = this.parseIdentifier(true); + const node = this.startNode(); + const meta = this.parseIdentifier(true); if (this.state.inGenerator && this.eat(tt.dot) && this.hasPlugin("functionSent")) { return this.parseMetaProperty(node, meta, "sent"); } else { @@ -541,7 +550,7 @@ pp.parseMetaProperty = function (node, meta, propertyName) { }; pp.parseLiteral = function (value, type) { - let node = this.startNode(); + const node = this.startNode(); this.addExtra(node, "rawValue", value); this.addExtra(node, "raw", this.input.slice(this.state.start, this.state.end)); node.value = value; @@ -551,7 +560,7 @@ pp.parseLiteral = function (value, type) { pp.parseParenExpression = function () { this.expect(tt.parenL); - let val = this.parseExpression(); + const val = this.parseExpression(); this.expect(tt.parenR); return val; }; @@ -563,10 +572,15 @@ pp.parseParenAndDistinguishExpression = function (startPos, startLoc, canBeArrow let val; this.expect(tt.parenL); - let innerStartPos = this.state.start, innerStartLoc = this.state.startLoc; - let exprList = [], first = true; - let refShorthandDefaultPos = { start: 0 }, spreadStart, optionalCommaStart; - let refNeedsArrowPos = { start: 0 }; + const innerStartPos = this.state.start; + const innerStartLoc = this.state.startLoc; + const exprList = []; + const refShorthandDefaultPos = { start: 0 }; + const refNeedsArrowPos = { start: 0 }; + let first = true; + let spreadStart; + let optionalCommaStart; + while (!this.match(tt.parenR)) { if (first) { first = false; @@ -579,7 +593,8 @@ pp.parseParenAndDistinguishExpression = function (startPos, startLoc, canBeArrow } if (this.match(tt.ellipsis)) { - let spreadNodeStartPos = this.state.start, spreadNodeStartLoc = this.state.startLoc; + const spreadNodeStartPos = this.state.start; + const spreadNodeStartLoc = this.state.startLoc; spreadStart = this.state.start; exprList.push(this.parseParenItem(this.parseRest(), spreadNodeStartLoc, spreadNodeStartPos)); break; @@ -588,13 +603,13 @@ pp.parseParenAndDistinguishExpression = function (startPos, startLoc, canBeArrow } } - let innerEndPos = this.state.start; - let innerEndLoc = this.state.startLoc; + const innerEndPos = this.state.start; + const innerEndLoc = this.state.startLoc; this.expect(tt.parenR); let arrowNode = this.startNodeAt(startPos, startLoc); if (canBeArrow && this.shouldParseArrow() && (arrowNode = this.parseArrow(arrowNode))) { - for (let param of exprList) { + for (const param of exprList) { if (param.extra && param.extra.parenthesized) this.unexpected(param.extra.parenStart); } @@ -644,8 +659,8 @@ pp.parseParenItem = function (node) { // least, not without wrapping it in parentheses. Thus, it uses the pp.parseNew = function () { - let node = this.startNode(); - let meta = this.parseIdentifier(true); + const node = this.startNode(); + const meta = this.parseIdentifier(true); if (this.eat(tt.dot)) { return this.parseMetaProperty(node, meta, "target"); @@ -666,7 +681,7 @@ pp.parseNew = function () { // Parse template expression. pp.parseTemplateElement = function () { - let elem = this.startNode(); + const elem = this.startNode(); elem.value = { raw: this.input.slice(this.state.start, this.state.end).replace(/\r\n?/g, "\n"), cooked: this.state.value @@ -677,7 +692,7 @@ pp.parseTemplateElement = function () { }; pp.parseTemplate = function () { - let node = this.startNode(); + const node = this.startNode(); this.next(); node.expressions = []; let curElt = this.parseTemplateElement(); @@ -696,9 +711,9 @@ pp.parseTemplate = function () { pp.parseObj = function (isPattern, refShorthandDefaultPos) { let decorators = []; - let propHash = Object.create(null); + const propHash = Object.create(null); let first = true; - let node = this.startNode(); + const node = this.startNode(); node.properties = []; this.next(); @@ -761,7 +776,7 @@ pp.parseObj = function (isPattern, refShorthandDefaultPos) { if (!isPattern && this.isContextual("async")) { if (isGenerator) this.unexpected(); - let asyncId = this.parseIdentifier(); + const asyncId = this.parseIdentifier(); if (this.match(tt.colon) || this.match(tt.parenL) || this.match(tt.braceR) || this.match(tt.eq) || this.match(tt.comma)) { prop.key = asyncId; } else { @@ -813,9 +828,9 @@ pp.parseObjPropValue = function (prop, startPos, startLoc, isGenerator, isAsync, prop.kind = prop.key.name; this.parsePropertyName(prop); this.parseMethod(prop, false); - let paramCount = prop.kind === "get" ? 0 : 1; + const paramCount = prop.kind === "get" ? 0 : 1; if (prop.params.length !== paramCount) { - let start = prop.start; + const start = prop.start; if (prop.kind === "get") { this.raise(start, "getter should have no params"); } else { @@ -872,7 +887,7 @@ pp.initFunction = function (node, isAsync) { // Parse object or class method. pp.parseMethod = function (node, isGenerator, isAsync) { - let oldInMethod = this.state.inMethod; + const oldInMethod = this.state.inMethod; this.state.inMethod = node.kind || true; this.initFunction(node, isAsync); this.expect(tt.parenL); @@ -895,9 +910,9 @@ pp.parseArrowExpression = function (node, params, isAsync) { // Parse function body and check parameters. pp.parseFunctionBody = function (node, allowExpression) { - let isExpression = allowExpression && !this.match(tt.braceL); + const isExpression = allowExpression && !this.match(tt.braceL); - let oldInAsync = this.state.inAsync; + const oldInAsync = this.state.inAsync; this.state.inAsync = node.async; if (isExpression) { node.body = this.parseMaybeAssign(); @@ -905,7 +920,9 @@ pp.parseFunctionBody = function (node, allowExpression) { } else { // Start a new scope with regard to labels and the `inFunction` // flag (restore them to their old value afterwards). - let oldInFunc = this.state.inFunction, oldInGen = this.state.inGenerator, oldLabels = this.state.labels; + const oldInFunc = this.state.inFunction; + const oldInGen = this.state.inGenerator; + const oldLabels = this.state.labels; this.state.inFunction = true; this.state.inGenerator = node.generator; this.state.labels = []; node.body = this.parseBlock(true); node.expression = false; @@ -924,7 +941,7 @@ pp.parseFunctionBody = function (node, allowExpression) { // normal function if (!isExpression && node.body.directives.length) { - for (let directive of (node.body.directives: Array)) { + for (const directive of (node.body.directives: Array)) { if (directive.value.value === "use strict") { isStrict = true; checkLVal = true; @@ -939,13 +956,13 @@ pp.parseFunctionBody = function (node, allowExpression) { } if (checkLVal) { - let nameHash = Object.create(null); - let oldStrict = this.state.strict; + const nameHash = Object.create(null); + const oldStrict = this.state.strict; if (isStrict) this.state.strict = true; if (node.id) { this.checkLVal(node.id, true, undefined, "function name"); } - for (let param of (node.params: Array)) { + for (const param of (node.params: Array)) { if (isStrict && param.type !== "Identifier") { this.raise(param.start, "Non-simple parameter in strict mode"); } @@ -962,7 +979,9 @@ pp.parseFunctionBody = function (node, allowExpression) { // for array literals). pp.parseExprList = function (close, allowEmpty, refShorthandDefaultPos) { - let elts = [], first = true; + const elts = []; + let first = true; + while (!this.eat(close)) { if (first) { first = false; @@ -993,7 +1012,7 @@ pp.parseExprListItem = function (allowEmpty, refShorthandDefaultPos) { // identifiers. pp.parseIdentifier = function (liberal) { - let node = this.startNode(); + const node = this.startNode(); if (this.match(tt.name)) { if (!liberal) { @@ -1044,7 +1063,7 @@ pp.parseAwait = function (node) { // Parses yield expression inside generator. pp.parseYield = function () { - let node = this.startNode(); + const node = this.startNode(); this.next(); if (this.match(tt.semi) || this.canInsertSemicolon() || (!this.match(tt.star) && !this.state.type.startsExpr)) { node.delegate = false; diff --git a/src/parser/index.js b/src/parser/index.js index de7eb11ba4..fc6a98bd4e 100644 --- a/src/parser/index.js +++ b/src/parser/index.js @@ -60,7 +60,7 @@ export default class Parser extends Tokenizer { pluginNames.push("flow"); pluginNames.forEach((name) => { - let plugin = plugins[name]; + const plugin = plugins[name]; if (plugin) plugin(this); }); } @@ -73,7 +73,7 @@ export default class Parser extends Tokenizer { return { "*": true }; } - let pluginMap = {}; + const pluginMap = {}; if (pluginList.indexOf("flow") >= 0) { // ensure flow plugin loads last @@ -81,11 +81,11 @@ export default class Parser extends Tokenizer { pluginList.push("flow"); } - for (let name of pluginList) { + for (const name of pluginList) { if (!pluginMap[name]) { pluginMap[name] = true; - let plugin = plugins[name]; + const plugin = plugins[name]; if (plugin) plugin(this); } } @@ -100,8 +100,8 @@ export default class Parser extends Tokenizer { body: Array } } { - let file = this.startNode(); - let program = this.startNode(); + const file = this.startNode(); + const program = this.startNode(); this.nextToken(); return this.parseTopLevel(file, program); } diff --git a/src/parser/location.js b/src/parser/location.js index c8f10cc172..2a408a5100 100644 --- a/src/parser/location.js +++ b/src/parser/location.js @@ -10,9 +10,9 @@ const pp = Parser.prototype; // message. pp.raise = function (pos, message) { - let loc = getLineInfo(this.input, pos); + const loc = getLineInfo(this.input, pos); message += ` (${loc.line}:${loc.column})`; - let err = new SyntaxError(message); + const err = new SyntaxError(message); err.pos = pos; err.loc = loc; throw err; diff --git a/src/parser/lval.js b/src/parser/lval.js index c74e88a5dd..4258506020 100644 --- a/src/parser/lval.js +++ b/src/parser/lval.js @@ -19,7 +19,7 @@ pp.toAssignable = function (node, isBinding, contextDescription) { case "ObjectExpression": node.type = "ObjectPattern"; - for (let prop of (node.properties: Array)) { + for (const prop of (node.properties: Array)) { if (prop.type === "ObjectMethod") { if (prop.kind === "get" || prop.kind === "set") { this.raise(prop.key.start, "Object pattern can't contain getter or setter"); @@ -72,12 +72,12 @@ pp.toAssignable = function (node, isBinding, contextDescription) { pp.toAssignableList = function (exprList, isBinding, contextDescription) { let end = exprList.length; if (end) { - let last = exprList[end - 1]; + const last = exprList[end - 1]; if (last && last.type === "RestElement") { --end; } else if (last && last.type === "SpreadElement") { last.type = "RestElement"; - let arg = last.argument; + const arg = last.argument; this.toAssignable(arg, isBinding, contextDescription); if (arg.type !== "Identifier" && arg.type !== "MemberExpression" && arg.type !== "ArrayPattern") { this.unexpected(arg.start); @@ -86,7 +86,7 @@ pp.toAssignableList = function (exprList, isBinding, contextDescription) { } } for (let i = 0; i < end; i++) { - let elt = exprList[i]; + const elt = exprList[i]; if (elt) this.toAssignable(elt, isBinding, contextDescription); } return exprList; @@ -101,14 +101,14 @@ pp.toReferencedList = function (exprList) { // Parses spread element. pp.parseSpread = function (refShorthandDefaultPos) { - let node = this.startNode(); + const node = this.startNode(); this.next(); node.argument = this.parseMaybeAssign(false, refShorthandDefaultPos); return this.finishNode(node, "SpreadElement"); }; pp.parseRest = function () { - let node = this.startNode(); + const node = this.startNode(); this.next(); node.argument = this.parseBindingIdentifier(); return this.finishNode(node, "RestElement"); @@ -133,7 +133,7 @@ pp.parseBindingAtom = function () { return this.parseIdentifier(true); case tt.bracketL: - let node = this.startNode(); + const node = this.startNode(); this.next(); node.elements = this.parseBindingList(tt.bracketR, true); return this.finishNode(node, "ArrayPattern"); @@ -147,7 +147,7 @@ pp.parseBindingAtom = function () { }; pp.parseBindingList = function (close, allowEmpty) { - let elts = []; + const elts = []; let first = true; while (!this.eat(close)) { if (first) { @@ -164,11 +164,11 @@ pp.parseBindingList = function (close, allowEmpty) { this.expect(close); break; } else { - let decorators = []; + const decorators = []; while (this.match(tt.at)) { decorators.push(this.parseDecorator()); } - let left = this.parseMaybeDefault(); + const left = this.parseMaybeDefault(); if (decorators.length) { left.decorators = decorators; } @@ -191,7 +191,7 @@ pp.parseMaybeDefault = function (startPos, startLoc, left) { left = left || this.parseBindingAtom(); if (!this.eat(tt.eq)) return left; - let node = this.startNodeAt(startPos, startLoc); + const node = this.startNodeAt(startPos, startLoc); node.left = left; node.right = this.parseMaybeAssign(); return this.finishNode(node, "AssignmentPattern"); @@ -217,7 +217,7 @@ pp.checkLVal = function (expr, isBinding, checkClashes, contextDescription) { // true // > obj.__proto__ // null - let key = `_${expr.name}`; + const key = `_${expr.name}`; if (checkClashes[key]) { this.raise(expr.start, "Argument name clash in strict mode"); @@ -239,7 +239,7 @@ pp.checkLVal = function (expr, isBinding, checkClashes, contextDescription) { break; case "ArrayPattern": - for (let elem of (expr.elements: Array)) { + for (const elem of (expr.elements: Array)) { if (elem) this.checkLVal(elem, isBinding, checkClashes, "array destructuring pattern"); } break; diff --git a/src/parser/node.js b/src/parser/node.js index 43146fd7ff..7af6c29687 100644 --- a/src/parser/node.js +++ b/src/parser/node.js @@ -22,7 +22,7 @@ class Node { __clone(): Node { const node2 = new Node; - for (let key in this) { + for (const key in this) { // Do not clone comments that are already attached to the node if (commentKeys.indexOf(key) < 0) { node2[key] = this[key]; diff --git a/src/parser/statement.js b/src/parser/statement.js index 053241298b..cfff7be0a7 100644 --- a/src/parser/statement.js +++ b/src/parser/statement.js @@ -31,13 +31,13 @@ const loopLabel = {kind: "loop"}, switchLabel = {kind: "switch"}; // TODO pp.stmtToDirective = function (stmt) { - let expr = stmt.expression; + const expr = stmt.expression; - let directiveLiteral = this.startNodeAt(expr.start, expr.loc.start); - let directive = this.startNodeAt(stmt.start, stmt.loc.start); + const directiveLiteral = this.startNodeAt(expr.start, expr.loc.start); + const directive = this.startNodeAt(stmt.start, stmt.loc.start); - let raw = this.input.slice(expr.start, expr.end); - let val = directiveLiteral.value = raw.slice(1, -1); // remove quotes + const raw = this.input.slice(expr.start, expr.end); + const val = directiveLiteral.value = raw.slice(1, -1); // remove quotes this.addExtra(directiveLiteral, "raw", raw); this.addExtra(directiveLiteral, "rawValue", val); @@ -59,7 +59,8 @@ pp.parseStatement = function (declaration, topLevel) { this.parseDecorators(true); } - let starttype = this.state.type, node = this.startNode(); + const starttype = this.state.type; + const node = this.startNode(); // Most types of statements are recognized by the keyword they // start with. Many are trivial to parse, some require a bit of @@ -114,7 +115,7 @@ pp.parseStatement = function (declaration, topLevel) { case tt.name: if (this.state.value === "async") { // peek ahead and see if next token is a function - let state = this.state.clone(); + const state = this.state.clone(); this.next(); if (this.match(tt._function) && !this.canInsertSemicolon()) { this.expect(tt._function); @@ -130,8 +131,8 @@ pp.parseStatement = function (declaration, topLevel) { // simply start parsing an expression, and afterwards, if the // next token is a colon and the expression was a simple // Identifier node, we switch to interpreting it as a label. - let maybeName = this.state.value; - let expr = this.parseExpression(); + const maybeName = this.state.value; + const expr = this.parseExpression(); if (starttype === tt.name && expr.type === "Identifier" && this.eat(tt.colon)) { return this.parseLabeledStatement(node, maybeName, expr); @@ -149,7 +150,7 @@ pp.takeDecorators = function (node) { pp.parseDecorators = function (allowExport) { while (this.match(tt.at)) { - let decorator = this.parseDecorator(); + const decorator = this.parseDecorator(); this.state.decorators.push(decorator); } @@ -166,14 +167,14 @@ pp.parseDecorator = function () { if (!this.hasPlugin("decorators")) { this.unexpected(); } - let node = this.startNode(); + const node = this.startNode(); this.next(); node.expression = this.parseMaybeAssign(); return this.finishNode(node, "Decorator"); }; pp.parseBreakContinueStatement = function (node, keyword) { - let isBreak = keyword === "break"; + const isBreak = keyword === "break"; this.next(); if (this.isLineTerminator()) { @@ -189,7 +190,7 @@ pp.parseBreakContinueStatement = function (node, keyword) { // continue to. let i; for (i = 0; i < this.state.labels.length; ++i) { - let lab = this.state.labels[i]; + const lab = this.state.labels[i]; if (node.label == null || lab.name === node.label.name) { if (lab.kind != null && (isBreak || lab.kind === "loop")) break; if (node.label && isBreak) break; @@ -243,7 +244,8 @@ pp.parseForStatement = function (node) { } if (this.match(tt._var) || this.match(tt._let) || this.match(tt._const)) { - let init = this.startNode(), varKind = this.state.type; + const init = this.startNode(); + const varKind = this.state.type; this.next(); this.parseVar(init, true, varKind); this.finishNode(init, "VariableDeclaration"); @@ -259,8 +261,8 @@ pp.parseForStatement = function (node) { return this.parseFor(node, init); } - let refShorthandDefaultPos = {start: 0}; - let init = this.parseExpression(true, refShorthandDefaultPos); + const refShorthandDefaultPos = {start: 0}; + const init = this.parseExpression(true, refShorthandDefaultPos); if (this.match(tt._in) || this.isContextual("of")) { const description = this.isContextual("of") ? "for-of statement" : "for-in statement"; this.toAssignable(init, undefined, description); @@ -323,7 +325,7 @@ pp.parseSwitchStatement = function (node) { let cur; for (let sawDefault; !this.match(tt.braceR); ) { if (this.match(tt._case) || this.match(tt._default)) { - let isCase = this.match(tt._case); + const isCase = this.match(tt._case); if (cur) this.finishNode(cur, "SwitchCase"); node.cases.push(cur = this.startNode()); cur.consequent = []; @@ -361,7 +363,7 @@ pp.parseThrowStatement = function (node) { // Reused empty array added for node fields that are always empty. -let empty = []; +const empty = []; pp.parseTryStatement = function (node) { this.next(); @@ -370,7 +372,7 @@ pp.parseTryStatement = function (node) { node.handler = null; if (this.match(tt._catch)) { - let clause = this.startNode(); + const clause = this.startNode(); this.next(); this.expect(tt.parenL); @@ -422,15 +424,15 @@ pp.parseEmptyStatement = function (node) { }; pp.parseLabeledStatement = function (node, maybeName, expr) { - for (let label of (this.state.labels: Array)) { + for (const label of (this.state.labels: Array)) { if (label.name === maybeName) { this.raise(expr.start, `Label '${maybeName}' is already declared`); } } - let kind = this.state.type.isLoop ? "loop" : this.match(tt._switch) ? "switch" : null; + const kind = this.state.type.isLoop ? "loop" : this.match(tt._switch) ? "switch" : null; for (let i = this.state.labels.length - 1; i >= 0; i--) { - let label = this.state.labels[i]; + const label = this.state.labels[i]; if (label.statementStart === node.start) { label.statementStart = this.state.start; label.kind = kind; @@ -457,7 +459,7 @@ pp.parseExpressionStatement = function (node, expr) { // function bodies). pp.parseBlock = function (allowDirectives?) { - let node = this.startNode(); + const node = this.startNode(); this.expect(tt.braceL); this.parseBlockBody(node, allowDirectives, false, tt.braceR); return this.finishNode(node, "BlockStatement"); @@ -478,12 +480,12 @@ pp.parseBlockBody = function (node, allowDirectives, topLevel, end) { octalPosition = this.state.octalPosition; } - let stmt = this.parseStatement(true, topLevel); + const stmt = this.parseStatement(true, topLevel); if (allowDirectives && !parsedNonDirective && stmt.type === "ExpressionStatement" && stmt.expression.type === "StringLiteral" && !stmt.expression.extra.parenthesized) { - let directive = this.stmtToDirective(stmt); + const directive = this.stmtToDirective(stmt); node.directives.push(directive); if (oldStrict === undefined && directive.value.value === "use strict") { @@ -549,7 +551,7 @@ pp.parseVar = function (node, isFor, kind) { node.declarations = []; node.kind = kind.keyword; for (;;) { - let decl = this.startNode(); + const decl = this.startNode(); this.parseVarHead(decl); if (this.eat(tt.eq)) { decl.init = this.parseMaybeAssign(isFor); @@ -575,7 +577,7 @@ pp.parseVarHead = function (decl) { // `isStatement` parameter). pp.parseFunction = function (node, isStatement, allowExpressionBody, isAsync, optionalId) { - let oldInMethod = this.state.inMethod; + const oldInMethod = this.state.inMethod; this.state.inMethod = false; this.initFunction(node, isAsync); @@ -631,13 +633,13 @@ pp.isClassMutatorStarter = function () { pp.parseClassBody = function (node) { // class bodies are implicitly strict - let oldStrict = this.state.strict; + const oldStrict = this.state.strict; this.state.strict = true; let hadConstructorCall = false; let hadConstructor = false; let decorators = []; - let classBody = this.startNode(); + const classBody = this.startNode(); classBody.body = []; @@ -653,7 +655,7 @@ pp.parseClassBody = function (node) { continue; } - let method = this.startNode(); + const method = this.startNode(); // steal the decorators if there are any if (decorators.length) { @@ -662,7 +664,7 @@ pp.parseClassBody = function (node) { } let isConstructorCall = false; - let isMaybeStatic = this.match(tt.name) && this.state.value === "static"; + const isMaybeStatic = this.match(tt.name) && this.state.value === "static"; let isGenerator = this.eat(tt.star); let isGetSet = false; let isAsync = false; @@ -687,7 +689,7 @@ pp.parseClassBody = function (node) { } } - let isAsyncMethod = !this.match(tt.parenL) && !method.computed && method.key.type === "Identifier" && method.key.name === "async"; + const isAsyncMethod = !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; @@ -708,7 +710,7 @@ pp.parseClassBody = function (node) { } // disallow invalid constructors - let isConstructor = !isConstructorCall && !method.static && ( + const isConstructor = !isConstructorCall && !method.static && ( (key.type === "Identifier" && key.name === "constructor") || (key.type === "StringLiteral" && key.value === "constructor") ); @@ -722,7 +724,7 @@ pp.parseClassBody = function (node) { } // disallow static prototype method - let isStaticPrototype = method.static && ( + const isStaticPrototype = method.static && ( (key.type === "Identifier" && key.name === "prototype") || (key.type === "StringLiteral" && key.value === "prototype") ); @@ -748,9 +750,9 @@ pp.parseClassBody = function (node) { // 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; + const paramCount = method.kind === "get" ? 0 : 1; if (method.params.length !== paramCount) { - let start = method.start; + const start = method.start; if (method.kind === "get") { this.raise(start, "getter should have no params"); } else { @@ -808,7 +810,7 @@ pp.parseExport = function (node) { this.next(); // export * from '...' if (this.match(tt.star)) { - let specifier = this.startNode(); + const specifier = this.startNode(); this.next(); if (this.hasPlugin("exportExtensions") && this.eatContextual("as")) { specifier.exported = this.parseIdentifier(); @@ -820,12 +822,12 @@ pp.parseExport = function (node) { return this.finishNode(node, "ExportAllDeclaration"); } } else if (this.hasPlugin("exportExtensions") && this.isExportDefaultSpecifier()) { - let specifier = this.startNode(); + const specifier = this.startNode(); specifier.exported = this.parseIdentifier(true); node.specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")]; if (this.match(tt.comma) && this.lookahead().type === tt.star) { this.expect(tt.comma); - let specifier = this.startNode(); + const specifier = this.startNode(); this.expect(tt.star); this.expectContextual("as"); specifier.exported = this.parseIdentifier(); @@ -877,7 +879,7 @@ pp.isExportDefaultSpecifier = function () { return false; } - let lookahead = this.lookahead(); + const lookahead = this.lookahead(); return lookahead.type === tt.comma || (lookahead.type === tt.name && lookahead.value === "from"); }; @@ -919,7 +921,7 @@ pp.checkExport = function (node, checkNames, isDefault) { this.checkDuplicateExports(node, "default"); } else if (node.specifiers && node.specifiers.length) { // Named exports - for (let specifier of node.specifiers) { + for (const specifier of node.specifiers) { this.checkDuplicateExports(specifier, specifier.exported.name); } } else if (node.declaration) { @@ -927,7 +929,7 @@ pp.checkExport = function (node, checkNames, isDefault) { if (node.declaration.type === "FunctionDeclaration" || node.declaration.type === "ClassDeclaration") { this.checkDuplicateExports(node, node.declaration.id.name); } else if (node.declaration.type === "VariableDeclaration") { - for (let declaration of node.declaration.declarations) { + for (const declaration of node.declaration.declarations) { this.checkDeclaration(declaration.id); } } @@ -935,7 +937,7 @@ pp.checkExport = function (node, checkNames, isDefault) { } if (this.state.decorators.length) { - let isClass = node.declaration && (node.declaration.type === "ClassDeclaration" || node.declaration.type === "ClassExpression"); + const 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"); } @@ -945,11 +947,11 @@ pp.checkExport = function (node, checkNames, isDefault) { pp.checkDeclaration = function(node) { if (node.type === "ObjectPattern") { - for (let prop of node.properties) { + for (const prop of node.properties) { this.checkDeclaration(prop); } } else if (node.type === "ArrayPattern") { - for (let elem of node.elements) { + for (const elem of node.elements) { if (elem) { this.checkDeclaration(elem); } @@ -980,7 +982,7 @@ pp.raiseDuplicateExportError = function(node, name) { // Parses a comma-separated list of module exports. pp.parseExportSpecifiers = function () { - let nodes = []; + const nodes = []; let first = true; let needsFrom; @@ -995,10 +997,10 @@ pp.parseExportSpecifiers = function () { if (this.eat(tt.braceR)) break; } - let isDefault = this.match(tt._default); + const isDefault = this.match(tt._default); if (isDefault && !needsFrom) needsFrom = true; - let node = this.startNode(); + const node = this.startNode(); node.local = this.parseIdentifier(isDefault); node.exported = this.eatContextual("as") ? this.parseIdentifier(true) : node.local.__clone(); nodes.push(this.finishNode(node, "ExportSpecifier")); @@ -1037,13 +1039,14 @@ pp.parseImportSpecifiers = function (node) { let first = true; if (this.match(tt.name)) { // import defaultObj, { x, y as z } from '...' - let startPos = this.state.start, startLoc = this.state.startLoc; + const startPos = this.state.start; + const startLoc = this.state.startLoc; node.specifiers.push(this.parseImportSpecifierDefault(this.parseIdentifier(), startPos, startLoc)); if (!this.eat(tt.comma)) return; } if (this.match(tt.star)) { - let specifier = this.startNode(); + const specifier = this.startNode(); this.next(); this.expectContextual("as"); specifier.local = this.parseIdentifier(); @@ -1066,7 +1069,7 @@ pp.parseImportSpecifiers = function (node) { }; pp.parseImportSpecifier = function (node) { - let specifier = this.startNode(); + const specifier = this.startNode(); specifier.imported = this.parseIdentifier(true); specifier.local = this.eatContextual("as") ? this.parseIdentifier() : specifier.imported.__clone(); this.checkLVal(specifier.local, true, undefined, "import specifier"); @@ -1074,7 +1077,7 @@ pp.parseImportSpecifier = function (node) { }; pp.parseImportSpecifierDefault = function (id, startPos, startLoc) { - let node = this.startNodeAt(startPos, startLoc); + const node = this.startNodeAt(startPos, startLoc); node.local = id; this.checkLVal(node.local, true, undefined, "default import specifier"); return this.finishNode(node, "ImportDefaultSpecifier"); diff --git a/src/parser/util.js b/src/parser/util.js index c8ac407a8c..86724e67c6 100644 --- a/src/parser/util.js +++ b/src/parser/util.js @@ -11,7 +11,7 @@ const pp = Parser.prototype; pp.addExtra = function (node, key, val) { if (!node) return; - let extra = node.extra = node.extra || {}; + const extra = node.extra = node.extra || {}; extra[key] = val; }; diff --git a/src/plugins/flow.js b/src/plugins/flow.js index cc4c7a1e76..db2fc7724e 100644 --- a/src/plugins/flow.js +++ b/src/plugins/flow.js @@ -5,14 +5,14 @@ import { types as tt } from "../tokenizer/types"; import { types as ct } from "../tokenizer/context"; import Parser from "../parser"; -let pp = Parser.prototype; +const pp = Parser.prototype; pp.flowParseTypeInitialiser = function (tok) { - let oldInType = this.state.inType; + const oldInType = this.state.inType; this.state.inType = true; this.expect(tok || tt.colon); - let type = this.flowParseType(); + const type = this.flowParseType(); this.state.inType = oldInType; return type; }; @@ -26,10 +26,10 @@ pp.flowParseDeclareClass = function (node) { pp.flowParseDeclareFunction = function (node) { this.next(); - let id = node.id = this.parseIdentifier(); + const id = node.id = this.parseIdentifier(); - let typeNode = this.startNode(); - let typeContainer = this.startNode(); + const typeNode = this.startNode(); + const typeContainer = this.startNode(); if (this.isRelational("<")) { typeNode.typeParameters = this.flowParseTypeParameterDeclaration(); @@ -38,7 +38,7 @@ pp.flowParseDeclareFunction = function (node) { } this.expect(tt.parenL); - let tmp = this.flowParseFunctionTypeParams(); + const tmp = this.flowParseFunctionTypeParams(); typeNode.params = tmp.params; typeNode.rest = tmp.rest; this.expect(tt.parenR); @@ -92,11 +92,11 @@ pp.flowParseDeclareModule = function (node) { node.id = this.parseIdentifier(); } - let bodyNode = node.body = this.startNode(); - let body = bodyNode.body = []; + const bodyNode = node.body = this.startNode(); + const body = bodyNode.body = []; this.expect(tt.braceL); while (!this.match(tt.braceR)) { - let node2 = this.startNode(); + const node2 = this.startNode(); this.expectContextual("declare", "Unexpected token. Only declares are allowed inside declare module"); @@ -161,7 +161,7 @@ pp.flowParseInterfaceish = function (node, allowStatic) { }; pp.flowParseInterfaceExtends = function () { - let node = this.startNode(); + const node = this.startNode(); node.id = this.flowParseQualifiedTypeIdentifier(); if (this.isRelational("<")) { @@ -198,11 +198,11 @@ pp.flowParseTypeAlias = function (node) { // Type annotations pp.flowParseTypeParameter = function () { - let node = this.startNode(); + const node = this.startNode(); - let variance = this.flowParseVariance(); + const variance = this.flowParseVariance(); - let ident = this.flowParseTypeAnnotatableIdentifier(); + const ident = this.flowParseTypeAnnotatableIdentifier(); node.name = ident.name; node.variance = variance; node.bound = ident.typeAnnotation; @@ -217,7 +217,7 @@ pp.flowParseTypeParameter = function () { pp.flowParseTypeParameterDeclaration = function () { const oldInType = this.state.inType; - let node = this.startNode(); + const node = this.startNode(); node.params = []; this.state.inType = true; @@ -243,7 +243,8 @@ pp.flowParseTypeParameterDeclaration = function () { }; pp.flowParseTypeParameterInstantiation = function () { - let node = this.startNode(), oldInType = this.state.inType; + const node = this.startNode(); + const oldInType = this.state.inType; node.params = []; this.state.inType = true; @@ -312,7 +313,7 @@ pp.flowParseObjectTypeMethodish = function (node) { }; pp.flowParseObjectTypeMethod = function (startPos, startLoc, isStatic, key) { - let node = this.startNodeAt(startPos, startLoc); + const node = this.startNodeAt(startPos, startLoc); node.value = this.flowParseObjectTypeMethodish(this.startNodeAt(startPos, startLoc)); node.static = isStatic; node.key = key; @@ -322,7 +323,7 @@ pp.flowParseObjectTypeMethod = function (startPos, startLoc, isStatic, key) { }; pp.flowParseObjectTypeCallProperty = function (node, isStatic) { - let valueNode = this.startNode(); + const valueNode = this.startNode(); node.static = isStatic; node.value = this.flowParseObjectTypeMethodish(valueNode); this.flowObjectTypeSemicolon(); @@ -333,7 +334,7 @@ pp.flowParseObjectType = function (allowStatic, allowExact) { const oldInType = this.state.inType; this.state.inType = true; - let nodeStart = this.startNode(); + const nodeStart = this.startNode(); let node; let propertyKey; let isStatic = false; @@ -358,15 +359,16 @@ pp.flowParseObjectType = function (allowStatic, allowExact) { while (!this.match(endDelim)) { let optional = false; - let startPos = this.state.start, startLoc = this.state.startLoc; + const startPos = this.state.start; + const startLoc = this.state.startLoc; node = this.startNode(); if (allowStatic && this.isContextual("static") && this.lookahead().type !== tt.colon) { this.next(); isStatic = true; } - let variancePos = this.state.start; - let variance = this.flowParseVariance(); + const variancePos = this.state.start; + const variance = this.flowParseVariance(); if (this.match(tt.bracketL)) { nodeStart.indexers.push(this.flowParseObjectTypeIndexer(node, isStatic, variance)); @@ -422,7 +424,7 @@ pp.flowParseQualifiedTypeIdentifier = function (startPos, startLoc, id) { let node = id || this.parseIdentifier(); while (this.eat(tt.dot)) { - let node2 = this.startNodeAt(startPos, startLoc); + const node2 = this.startNodeAt(startPos, startLoc); node2.qualification = node; node2.id = this.parseIdentifier(); node = this.finishNode(node2, "QualifiedTypeIdentifier"); @@ -432,7 +434,7 @@ pp.flowParseQualifiedTypeIdentifier = function (startPos, startLoc, id) { }; pp.flowParseGenericType = function (startPos, startLoc, id) { - let node = this.startNodeAt(startPos, startLoc); + const node = this.startNodeAt(startPos, startLoc); node.typeParameters = null; node.id = this.flowParseQualifiedTypeIdentifier(startPos, startLoc, id); @@ -445,14 +447,14 @@ pp.flowParseGenericType = function (startPos, startLoc, id) { }; pp.flowParseTypeofType = function () { - let node = this.startNode(); + const node = this.startNode(); this.expect(tt._typeof); node.argument = this.flowParsePrimaryType(); return this.finishNode(node, "TypeofTypeAnnotation"); }; pp.flowParseTupleType = function () { - let node = this.startNode(); + const node = this.startNode(); node.types = []; this.expect(tt.bracketL); // We allow trailing commas @@ -469,7 +471,7 @@ pp.flowParseFunctionTypeParam = function () { let name = null; let optional = false; let typeAnnotation = null; - let node = this.startNode(); + const node = this.startNode(); const lh = this.lookahead(); if (lh.type === tt.colon || lh.type === tt.question) { @@ -488,7 +490,7 @@ pp.flowParseFunctionTypeParam = function () { }; pp.reinterpretTypeAsFunctionTypeParam = function (type) { - let node = this.startNodeAt(type.start, type.loc); + const node = this.startNodeAt(type.start, type.loc); node.name = null; node.optional = false; node.typeAnnotation = type; @@ -496,7 +498,7 @@ pp.reinterpretTypeAsFunctionTypeParam = function (type) { }; pp.flowParseFunctionTypeParams = function (params = []) { - let ret = { params, rest: null }; + const ret = { params, rest: null }; while (this.match(tt.name)) { ret.params.push(this.flowParseFunctionTypeParam()); if (!this.match(tt.parenR)) { @@ -542,12 +544,13 @@ pp.flowIdentToTypeAnnotation = function (startPos, startLoc, node, id) { // primary types are kind of like primary expressions...they're the // primitives with which other types are constructed. pp.flowParsePrimaryType = function () { - let startPos = this.state.start, startLoc = this.state.startLoc; - let node = this.startNode(); + const startPos = this.state.start; + const startLoc = this.state.startLoc; + const node = this.startNode(); let tmp; let type; let isGroupedType = false; - let oldNoAnonFunctionType = this.state.noAnonFunctionType; + const oldNoAnonFunctionType = this.state.noAnonFunctionType; switch (this.state.type) { case tt.name: @@ -585,7 +588,7 @@ pp.flowParsePrimaryType = function () { // Check to see if this is actually a grouped type if (!this.match(tt.parenR) && !this.match(tt.ellipsis)) { if (this.match(tt.name)) { - let token = this.lookahead().type; + const token = this.lookahead().type; isGroupedType = token !== tt.question && token !== tt.colon; } else { isGroupedType = true; @@ -698,7 +701,7 @@ pp.flowParsePostfixType = function () { }; pp.flowParsePrefixType = function () { - let node = this.startNode(); + const node = this.startNode(); if (this.eat(tt.question)) { node.typeAnnotation = this.flowParsePrefixType(); return this.finishNode(node, "NullableTypeAnnotation"); @@ -721,9 +724,9 @@ pp.flowParseAnonFunctionWithoutParens = function () { }; pp.flowParseIntersectionType = function () { - let node = this.startNode(); + const node = this.startNode(); this.eat(tt.bitwiseAND); - let type = this.flowParseAnonFunctionWithoutParens(); + const type = this.flowParseAnonFunctionWithoutParens(); node.types = [type]; while (this.eat(tt.bitwiseAND)) { node.types.push(this.flowParseAnonFunctionWithoutParens()); @@ -732,9 +735,9 @@ pp.flowParseIntersectionType = function () { }; pp.flowParseUnionType = function () { - let node = this.startNode(); + const node = this.startNode(); this.eat(tt.bitwiseOR); - let type = this.flowParseIntersectionType(); + const type = this.flowParseIntersectionType(); node.types = [type]; while (this.eat(tt.bitwiseOR)) { node.types.push(this.flowParseIntersectionType()); @@ -743,21 +746,21 @@ pp.flowParseUnionType = function () { }; pp.flowParseType = function () { - let oldInType = this.state.inType; + const oldInType = this.state.inType; this.state.inType = true; - let type = this.flowParseUnionType(); + const type = this.flowParseUnionType(); this.state.inType = oldInType; return type; }; pp.flowParseTypeAnnotation = function () { - let node = this.startNode(); + const node = this.startNode(); node.typeAnnotation = this.flowParseTypeInitialiser(); return this.finishNode(node, "TypeAnnotation"); }; pp.flowParseTypeAnnotatableIdentifier = function () { - let ident = this.parseIdentifier(); + const ident = this.parseIdentifier(); if (this.match(tt.colon)) { ident.typeAnnotation = this.flowParseTypeAnnotation(); this.finishNode(ident, ident.type); @@ -808,7 +811,7 @@ export default function (instance) { return function (declaration, topLevel) { // strict mode handling of `interface` since it's a reserved word if (this.state.strict && this.match(tt.name) && this.state.value === "interface") { - let node = this.startNode(); + const node = this.startNode(); this.next(); return this.flowParseInterface(node); } else { @@ -879,7 +882,7 @@ export default function (instance) { } if (this.match(tt.colon)) { - let typeCastNode = this.startNodeAt(startLoc, startPos); + const typeCastNode = this.startNodeAt(startLoc, startPos); typeCastNode.expression = node; typeCastNode.typeAnnotation = this.flowParseTypeAnnotation(); @@ -905,7 +908,7 @@ export default function (instance) { if (this.isContextual("type")) { node.exportKind = "type"; - let declarationNode = this.startNode(); + const declarationNode = this.startNode(); this.next(); if (this.match(tt.braceL)) { @@ -919,7 +922,7 @@ export default function (instance) { } } else if (this.isContextual("interface")) { node.exportKind = "type"; - let declarationNode = this.startNode(); + const declarationNode = this.startNode(); this.next(); return this.flowParseInterface(declarationNode); } else { @@ -981,7 +984,7 @@ export default function (instance) { instance.extend("toAssignableList", function (inner) { return function (exprList, isBinding, contextDescription) { for (let i = 0; i < exprList.length; i++) { - let expr = exprList[i]; + const expr = exprList[i]; if (expr && expr.type === "TypeCastExpression") { exprList[i] = this.typeCastToParameter(expr); } @@ -995,7 +998,7 @@ export default function (instance) { instance.extend("toReferencedList", function () { return function (exprList) { for (let i = 0; i < exprList.length; i++) { - let expr = exprList[i]; + const expr = exprList[i]; if (expr && expr._exprListItem && expr.type === "TypeCastExpression") { this.raise(expr.start, "Unexpected type cast"); } @@ -1009,8 +1012,8 @@ export default function (instance) { // the position where this function is called instance.extend("parseExprListItem", function (inner) { return function (allowEmpty, refShorthandDefaultPos) { - let container = this.startNode(); - let node = inner.call(this, allowEmpty, refShorthandDefaultPos); + const container = this.startNode(); + const node = inner.call(this, allowEmpty, refShorthandDefaultPos); if (this.match(tt.colon)) { container._exprListItem = true; container.expression = node; @@ -1073,9 +1076,9 @@ export default function (instance) { } if (this.isContextual("implements")) { this.next(); - let implemented = node.implements = []; + const implemented = node.implements = []; do { - let node = this.startNode(); + const node = this.startNode(); node.id = this.parseIdentifier(); if (this.isRelational("<")) { node.typeParameters = this.flowParseTypeParameterInstantiation(); @@ -1090,9 +1093,9 @@ export default function (instance) { instance.extend("parsePropertyName", function (inner) { return function (node) { - let variancePos = this.state.start; - let variance = this.flowParseVariance(); - let key = inner.call(this, node); + const variancePos = this.state.start; + const variance = this.flowParseVariance(); + const key = inner.call(this, node); node.variance = variance; node.variancePos = variancePos; return key; @@ -1163,7 +1166,7 @@ export default function (instance) { kind = "type"; } if (kind) { - let lh = this.lookahead(); + const lh = this.lookahead(); if ((lh.type === tt.name && lh.value !== "from") || lh.type === tt.braceL || lh.type === tt.star) { this.next(); node.importKind = kind; @@ -1177,7 +1180,7 @@ export default function (instance) { // parse import-type/typeof shorthand instance.extend("parseImportSpecifier", function () { return function (node) { - let specifier = this.startNode(); + const specifier = this.startNode(); const firstIdentLoc = this.state.start; const firstIdent = this.parseIdentifier(true); @@ -1334,11 +1337,11 @@ export default function (instance) { instance.extend("parseArrow", function (inner) { return function (node) { if (this.match(tt.colon)) { - let state = this.state.clone(); + const state = this.state.clone(); try { const oldNoAnonFunctionType = this.state.noAnonFunctionType; this.state.noAnonFunctionType = true; - let returnType = this.flowParseTypeAnnotation(); + const returnType = this.flowParseTypeAnnotation(); this.state.noAnonFunctionType = oldNoAnonFunctionType; if (this.canInsertSemicolon()) this.unexpected(); diff --git a/src/plugins/jsx/index.js b/src/plugins/jsx/index.js index bd4633a095..859a6c879b 100644 --- a/src/plugins/jsx/index.js +++ b/src/plugins/jsx/index.js @@ -28,7 +28,7 @@ tt.jsxTagStart.updateContext = function() { }; tt.jsxTagEnd.updateContext = function(prevType) { - let out = this.state.context.pop(); + const out = this.state.context.pop(); if (out === tc.j_oTag && prevType === tt.slash || out === tc.j_cTag) { this.state.context.pop(); this.state.exprAllowed = this.curContext() === tc.j_expr; @@ -37,7 +37,7 @@ tt.jsxTagEnd.updateContext = function(prevType) { } }; -let pp = Parser.prototype; +const pp = Parser.prototype; // Reads inline JSX contents token. @@ -49,7 +49,7 @@ pp.jsxReadToken = function() { this.raise(this.state.start, "Unterminated JSX contents"); } - let ch = this.input.charCodeAt(this.state.pos); + const ch = this.input.charCodeAt(this.state.pos); switch (ch) { case 60: // "<" @@ -83,7 +83,7 @@ pp.jsxReadToken = function() { }; pp.jsxReadNewLine = function(normalizeCRLF) { - let ch = this.input.charCodeAt(this.state.pos); + const ch = this.input.charCodeAt(this.state.pos); let out; ++this.state.pos; if (ch === 13 && this.input.charCodeAt(this.state.pos) === 10) { @@ -106,7 +106,7 @@ pp.jsxReadString = function(quote) { this.raise(this.state.start, "Unterminated string constant"); } - let ch = this.input.charCodeAt(this.state.pos); + const ch = this.input.charCodeAt(this.state.pos); if (ch === quote) break; if (ch === 38) { // "&" out += this.input.slice(chunkStart, this.state.pos); @@ -130,7 +130,7 @@ pp.jsxReadEntity = function() { let entity; let ch = this.input[this.state.pos]; - let startPos = ++this.state.pos; + const startPos = ++this.state.pos; while (this.state.pos < this.input.length && count++ < 10) { ch = this.input[this.state.pos++]; if (ch === ";") { @@ -168,7 +168,7 @@ pp.jsxReadEntity = function() { pp.jsxReadWord = function() { let ch; - let start = this.state.pos; + const start = this.state.pos; do { ch = this.input.charCodeAt(++this.state.pos); } while (isIdentifierChar(ch) || ch === 45); // "-" @@ -194,7 +194,7 @@ function getQualifiedJSXName(object) { // Parse next token as JSX identifier pp.jsxParseIdentifier = function() { - let node = this.startNode(); + const node = this.startNode(); if (this.match(tt.jsxName)) { node.name = this.state.value; } else if (this.state.type.keyword) { @@ -209,11 +209,12 @@ pp.jsxParseIdentifier = function() { // Parse namespaced identifier. pp.jsxParseNamespacedName = function() { - let startPos = this.state.start, startLoc = this.state.startLoc; - let name = this.jsxParseIdentifier(); + const startPos = this.state.start; + const startLoc = this.state.startLoc; + const name = this.jsxParseIdentifier(); if (!this.eat(tt.colon)) return name; - let node = this.startNodeAt(startPos, startLoc); + const node = this.startNodeAt(startPos, startLoc); node.namespace = name; node.name = this.jsxParseIdentifier(); return this.finishNode(node, "JSXNamespacedName"); @@ -223,10 +224,11 @@ pp.jsxParseNamespacedName = function() { // or single identifier. pp.jsxParseElementName = function() { - let startPos = this.state.start, startLoc = this.state.startLoc; + const startPos = this.state.start; + const startLoc = this.state.startLoc; let node = this.jsxParseNamespacedName(); while (this.eat(tt.dot)) { - let newNode = this.startNodeAt(startPos, startLoc); + const newNode = this.startNodeAt(startPos, startLoc); newNode.object = node; newNode.property = this.jsxParseIdentifier(); node = this.finishNode(newNode, "JSXMemberExpression"); @@ -263,14 +265,14 @@ pp.jsxParseAttributeValue = function() { // at the beginning of the next one (right brace). pp.jsxParseEmptyExpression = function() { - let node = this.startNodeAt(this.state.lastTokEnd, this.state.lastTokEndLoc); + const node = this.startNodeAt(this.state.lastTokEnd, this.state.lastTokEndLoc); return this.finishNodeAt(node, "JSXEmptyExpression", this.state.start, this.state.startLoc); }; // Parse JSX spread child pp.jsxParseSpreadChild = function() { - let node = this.startNode(); + const node = this.startNode(); this.expect(tt.braceL); this.expect(tt.ellipsis); node.expression = this.parseExpression(); @@ -283,7 +285,7 @@ pp.jsxParseSpreadChild = function() { pp.jsxParseExpressionContainer = function() { - let node = this.startNode(); + const node = this.startNode(); this.next(); if (this.match(tt.braceR)) { node.expression = this.jsxParseEmptyExpression(); @@ -297,7 +299,7 @@ pp.jsxParseExpressionContainer = function() { // Parses following JSX attribute name-value pair. pp.jsxParseAttribute = function() { - let node = this.startNode(); + const node = this.startNode(); if (this.eat(tt.braceL)) { this.expect(tt.ellipsis); node.argument = this.parseMaybeAssign(); @@ -312,7 +314,7 @@ pp.jsxParseAttribute = function() { // Parses JSX opening tag starting after "<". pp.jsxParseOpeningElementAt = function(startPos, startLoc) { - let node = this.startNodeAt(startPos, startLoc); + const node = this.startNodeAt(startPos, startLoc); node.attributes = []; node.name = this.jsxParseElementName(); while (!this.match(tt.slash) && !this.match(tt.jsxTagEnd)) { @@ -326,7 +328,7 @@ pp.jsxParseOpeningElementAt = function(startPos, startLoc) { // Parses JSX closing tag starting after "= 0xe000) return code; - let next = this.input.charCodeAt(this.state.pos + 1); + const next = this.input.charCodeAt(this.state.pos + 1); return (code << 10) + next - 0x35fdc00; } pushComment(block, text, start, end, startLoc, endLoc) { - let comment = { + const comment = { type: block ? "CommentBlock" : "CommentLine", value: text, start: start, @@ -171,8 +171,9 @@ export default class Tokenizer { } skipBlockComment() { - let startLoc = this.state.curPosition(); - let start = this.state.pos, end = this.input.indexOf("*/", this.state.pos += 2); + const startLoc = this.state.curPosition(); + const start = this.state.pos; + const end = this.input.indexOf("*/", this.state.pos += 2); if (end === -1) this.raise(this.state.pos - 2, "Unterminated comment"); this.state.pos = end + 2; @@ -187,8 +188,8 @@ export default class Tokenizer { } skipLineComment(startSkip) { - let start = this.state.pos; - let startLoc = this.state.curPosition(); + 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; @@ -203,7 +204,7 @@ export default class Tokenizer { skipSpace() { loop: while (this.state.pos < this.input.length) { - let ch = this.input.charCodeAt(this.state.pos); + const ch = this.input.charCodeAt(this.state.pos); switch (ch) { case 32: case 160: // ' ' ++this.state.pos; @@ -253,7 +254,7 @@ export default class Tokenizer { finishToken(type, val) { this.state.end = this.state.pos; this.state.endLoc = this.state.curPosition(); - let prevType = this.state.type; + const prevType = this.state.type; this.state.type = type; this.state.value = val; @@ -270,12 +271,12 @@ export default class Tokenizer { // All in the name of speed. // readToken_dot() { - let next = this.input.charCodeAt(this.state.pos + 1); + const next = this.input.charCodeAt(this.state.pos + 1); if (next >= 48 && next <= 57) { return this.readNumber(true); } - let next2 = this.input.charCodeAt(this.state.pos + 2); + const next2 = this.input.charCodeAt(this.state.pos + 2); if (next === 46 && next2 === 46) { // 46 = dot '.' this.state.pos += 3; return this.finishToken(tt.ellipsis); @@ -291,7 +292,7 @@ export default class Tokenizer { return this.readRegexp(); } - let next = this.input.charCodeAt(this.state.pos + 1); + const next = this.input.charCodeAt(this.state.pos + 1); if (next === 61) { return this.finishOp(tt.assign, 2); } else { @@ -319,7 +320,7 @@ export default class Tokenizer { } readToken_pipe_amp(code) { // '|&' - let next = this.input.charCodeAt(this.state.pos + 1); + const next = this.input.charCodeAt(this.state.pos + 1); if (next === code) return this.finishOp(code === 124 ? tt.logicalOR : tt.logicalAND, 2); if (next === 61) return this.finishOp(tt.assign, 2); if (code === 124 && next === 125 && this.hasPlugin("flow")) return this.finishOp(tt.braceBarR, 2); @@ -327,7 +328,7 @@ export default class Tokenizer { } readToken_caret() { // '^' - let next = this.input.charCodeAt(this.state.pos + 1); + const next = this.input.charCodeAt(this.state.pos + 1); if (next === 61) { return this.finishOp(tt.assign, 2); } else { @@ -336,7 +337,7 @@ export default class Tokenizer { } readToken_plus_min(code) { // '+-' - let next = this.input.charCodeAt(this.state.pos + 1); + const next = this.input.charCodeAt(this.state.pos + 1); if (next === code) { if (next === 45 && this.input.charCodeAt(this.state.pos + 2) === 62 && lineBreak.test(this.input.slice(this.state.lastTokEnd, this.state.pos))) { @@ -356,7 +357,7 @@ export default class Tokenizer { } readToken_lt_gt(code) { // '<>' - let next = this.input.charCodeAt(this.state.pos + 1); + const next = this.input.charCodeAt(this.state.pos + 1); let size = 1; if (next === code) { @@ -382,7 +383,7 @@ export default class Tokenizer { } readToken_eq_excl(code) { // '=!' - let next = this.input.charCodeAt(this.state.pos + 1); + const next = this.input.charCodeAt(this.state.pos + 1); if (next === 61) return this.finishOp(tt.equality, this.input.charCodeAt(this.state.pos + 2) === 61 ? 3 : 2); if (code === 61 && next === 62) { // '=>' this.state.pos += 2; @@ -433,7 +434,7 @@ export default class Tokenizer { return this.finishToken(tt.backQuote); case 48: // '0' - let next = this.input.charCodeAt(this.state.pos + 1); + const next = this.input.charCodeAt(this.state.pos + 1); if (next === 120 || next === 88) return this.readRadixNumber(16); // '0x', '0X' - hex number if (next === 111 || next === 79) return this.readRadixNumber(8); // '0o', '0O' - octal number if (next === 98 || next === 66) return this.readRadixNumber(2); // '0b', '0B' - binary number @@ -480,16 +481,17 @@ export default class Tokenizer { } finishOp(type, size) { - let str = this.input.slice(this.state.pos, this.state.pos + size); + const str = this.input.slice(this.state.pos, this.state.pos + size); this.state.pos += size; return this.finishToken(type, str); } readRegexp() { - let escaped, inClass, start = this.state.pos; + const start = this.state.pos; + let escaped, inClass; for (;;) { if (this.state.pos >= this.input.length) this.raise(start, "Unterminated regular expression"); - let ch = this.input.charAt(this.state.pos); + const ch = this.input.charAt(this.state.pos); if (lineBreak.test(ch)) { this.raise(start, "Unterminated regular expression"); } @@ -507,13 +509,13 @@ export default class Tokenizer { } ++this.state.pos; } - let content = this.input.slice(start, this.state.pos); + const content = this.input.slice(start, this.state.pos); ++this.state.pos; // Need to use `readWord1` because '\uXXXX' sequences are allowed // here (don't ask). - let mods = this.readWord1(); + const mods = this.readWord1(); if (mods) { - let validFlags = /^[gmsiyu]*$/; + const validFlags = /^[gmsiyu]*$/; if (!validFlags.test(mods)) this.raise(start, "Invalid regular expression flag"); } return this.finishToken(tt.regexp, { @@ -527,9 +529,12 @@ export default class Tokenizer { // will return `null` unless the integer has exactly `len` digits. readInt(radix, len) { - let start = this.state.pos, total = 0; + const start = this.state.pos; + let total = 0; + for (let i = 0, e = len == null ? Infinity : len; i < e; ++i) { - let code = this.input.charCodeAt(this.state.pos), val; + const code = this.input.charCodeAt(this.state.pos); + let val; if (code >= 97) { val = code - 97 + 10; // a } else if (code >= 65) { @@ -550,7 +555,7 @@ export default class Tokenizer { readRadixNumber(radix) { this.state.pos += 2; // 0x - let val = this.readInt(radix); + const val = this.readInt(radix); if (val == null) this.raise(this.state.start + 2, "Expected number in radix " + radix); if (isIdentifierStart(this.fullCharCodeAtPos())) this.raise(this.state.pos, "Identifier directly after number"); return this.finishToken(tt.num, val); @@ -559,7 +564,10 @@ export default class Tokenizer { // Read an integer, octal integer, or floating-point number. readNumber(startsWithDot) { - let start = this.state.pos, isFloat = false, octal = this.input.charCodeAt(this.state.pos) === 48; + const start = this.state.pos; + const octal = this.input.charCodeAt(this.state.pos) === 48; + let isFloat = false; + if (!startsWithDot && this.readInt(10) === null) this.raise(start, "Invalid number"); let next = this.input.charCodeAt(this.state.pos); if (next === 46) { // '.' @@ -576,7 +584,8 @@ export default class Tokenizer { } if (isIdentifierStart(this.fullCharCodeAtPos())) this.raise(this.state.pos, "Identifier directly after number"); - let str = this.input.slice(start, this.state.pos), val; + const str = this.input.slice(start, this.state.pos); + let val; if (isFloat) { val = parseFloat(str); } else if (!octal || str.length === 1) { @@ -592,10 +601,11 @@ export default class Tokenizer { // Read a string value, interpreting backslash-escapes. readCodePoint() { - let ch = this.input.charCodeAt(this.state.pos), code; + const ch = this.input.charCodeAt(this.state.pos); + let code; if (ch === 123) { - let codePos = ++this.state.pos; + const codePos = ++this.state.pos; code = this.readHexChar(this.input.indexOf("}", this.state.pos) - this.state.pos); ++this.state.pos; if (code > 0x10FFFF) this.raise(codePos, "Code point out of bounds"); @@ -609,7 +619,7 @@ export default class Tokenizer { let out = "", chunkStart = ++this.state.pos; for (;;) { if (this.state.pos >= this.input.length) this.raise(this.state.start, "Unterminated string constant"); - let ch = this.input.charCodeAt(this.state.pos); + const ch = this.input.charCodeAt(this.state.pos); if (ch === quote) break; if (ch === 92) { // '\' out += this.input.slice(chunkStart, this.state.pos); @@ -630,7 +640,7 @@ export default class Tokenizer { let out = "", chunkStart = this.state.pos; for (;;) { if (this.state.pos >= this.input.length) this.raise(this.state.start, "Unterminated template"); - let ch = this.input.charCodeAt(this.state.pos); + const ch = this.input.charCodeAt(this.state.pos); if (ch === 96 || ch === 36 && this.input.charCodeAt(this.state.pos + 1) === 123) { // '`', '${' if (this.state.pos === this.state.start && this.match(tt.template)) { if (ch === 36) { @@ -673,7 +683,7 @@ export default class Tokenizer { // Used to read escaped characters readEscapedChar(inTemplate) { - let ch = this.input.charCodeAt(++this.state.pos); + const ch = this.input.charCodeAt(++this.state.pos); ++this.state.pos; switch (ch) { case 110: return "\n"; // 'n' -> '\n' @@ -716,8 +726,8 @@ export default class Tokenizer { // Used to read character escape sequences ('\x', '\u', '\U'). readHexChar(len) { - let codePos = this.state.pos; - let n = this.readInt(16, len); + const codePos = this.state.pos; + const n = this.readInt(16, len); if (n === null) this.raise(codePos, "Bad character escape sequence"); return n; } @@ -732,21 +742,21 @@ export default class Tokenizer { this.state.containsEsc = false; let word = "", first = true, chunkStart = this.state.pos; while (this.state.pos < this.input.length) { - let ch = this.fullCharCodeAtPos(); + const ch = this.fullCharCodeAtPos(); if (isIdentifierChar(ch)) { this.state.pos += ch <= 0xffff ? 1 : 2; } else if (ch === 92) { // "\" this.state.containsEsc = true; word += this.input.slice(chunkStart, this.state.pos); - let escStart = this.state.pos; + const escStart = this.state.pos; if (this.input.charCodeAt(++this.state.pos) !== 117) { // "u" this.raise(this.state.pos, "Expecting Unicode escape sequence \\uXXXX"); } ++this.state.pos; - let esc = this.readCodePoint(); + const esc = this.readCodePoint(); if (!(first ? isIdentifierStart : isIdentifierChar)(esc, true)) { this.raise(escStart, "Invalid Unicode escape"); } @@ -765,7 +775,7 @@ export default class Tokenizer { // words when necessary. readWord() { - let word = this.readWord1(); + const word = this.readWord1(); let type = tt.name; if (!this.state.containsEsc && this.isKeyword(word)) { type = keywordTypes[word]; @@ -775,7 +785,7 @@ export default class Tokenizer { braceIsBlock(prevType) { if (prevType === tt.colon) { - let parent = this.curContext(); + const parent = this.curContext(); if (parent === ct.braceStatement || parent === ct.braceExpression) { return !parent.isExpr; } @@ -797,7 +807,9 @@ export default class Tokenizer { } updateContext(prevType) { - let update, type = this.state.type; + const type = this.state.type; + let update; + if (type.keyword && prevType === tt.dot) { this.state.exprAllowed = false; } else if (update = type.updateContext) { diff --git a/src/tokenizer/state.js b/src/tokenizer/state.js index b526ca22eb..878bf6b932 100644 --- a/src/tokenizer/state.js +++ b/src/tokenizer/state.js @@ -140,8 +140,8 @@ export default class State { } clone(skipArrays?) { - let state = new State; - for (let key in this) { + const state = new State; + for (const key in this) { let val = this[key]; if ((!skipArrays || key === "context") && Array.isArray(val)) { diff --git a/src/util/location.js b/src/util/location.js index 883d4b3daa..38137496c1 100644 --- a/src/util/location.js +++ b/src/util/location.js @@ -26,7 +26,7 @@ export class SourceLocation { export function getLineInfo(input, offset) { for (let line = 1, cur = 0; ;) { lineBreakG.lastIndex = cur; - let match = lineBreakG.exec(input); + const match = lineBreakG.exec(input); if (match && match.index < offset) { ++line; cur = match.index + match[0].length; diff --git a/yarn.lock b/yarn.lock index ae467c1fcc..e3ddfaf709 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1549,9 +1549,9 @@ escope@^3.6.0: esrecurse "^4.1.0" estraverse "^4.1.1" -eslint-config-babel@^3.0.0: - version "3.0.0" - resolved "https://registry.yarnpkg.com/eslint-config-babel/-/eslint-config-babel-3.0.0.tgz#19210e1b46747e7e58d9f477dd00544f5762bf95" +eslint-config-babel@^4.0.0: + version "4.0.1" + resolved "https://registry.yarnpkg.com/eslint-config-babel/-/eslint-config-babel-4.0.1.tgz#79647e62d82aff64872e5013d6b24f8cfe9b503b" eslint-plugin-babel@^4.0.0: version "4.0.0" @@ -1564,8 +1564,8 @@ eslint-plugin-flowtype@^2.20.0: lodash "^4.15.0" eslint@^3.7.1: - version "3.13.0" - resolved "https://registry.yarnpkg.com/eslint/-/eslint-3.13.0.tgz#636925fd163c9babe2e8be7ae43caf518d469577" + version "3.13.1" + resolved "https://registry.yarnpkg.com/eslint/-/eslint-3.13.1.tgz#564d2646b5efded85df96985332edd91a23bff25" dependencies: babel-code-frame "^6.16.0" chalk "^1.1.3" @@ -2507,8 +2507,8 @@ lodash.isarray@^3.0.0: resolved "https://registry.yarnpkg.com/lodash.isarray/-/lodash.isarray-3.0.4.tgz#79e4eb88c36a8122af86f844aa9bcd851b5fbb55" lodash.isequal@^4.4.0: - version "4.4.0" - resolved "https://registry.yarnpkg.com/lodash.isequal/-/lodash.isequal-4.4.0.tgz#6295768e98e14dc15ce8d362ef6340db82852031" + version "4.5.0" + resolved "https://registry.yarnpkg.com/lodash.isequal/-/lodash.isequal-4.5.0.tgz#415c4478f2bcc30120c22ce10ed3226f7d3e18e0" lodash.keys@^3.0.0: version "3.1.2" @@ -3376,8 +3376,8 @@ set-immediate-shim@^1.0.1: resolved "https://registry.yarnpkg.com/set-immediate-shim/-/set-immediate-shim-1.0.1.tgz#4b2b1b27eb808a9f8dcc481a58e5e56f599f3f61" shelljs@^0.7.5: - version "0.7.5" - resolved "https://registry.yarnpkg.com/shelljs/-/shelljs-0.7.5.tgz#2eef7a50a21e1ccf37da00df767ec69e30ad0675" + version "0.7.6" + resolved "https://registry.yarnpkg.com/shelljs/-/shelljs-0.7.6.tgz#379cccfb56b91c8601e4793356eb5382924de9ad" dependencies: glob "^7.0.0" interpret "^1.0.0"