diff --git a/.eslintignore b/.eslintignore index b40ed3c607ad..c4f3acc6e0a1 100644 --- a/.eslintignore +++ b/.eslintignore @@ -21,9 +21,4 @@ experimental/babel-preset-env/test/debug-fixtures packages/babel-standalone/babel.js packages/babel-standalone/babel.min.js packages/babylon/build -packages/babylon/scripts packages/babylon/test/expressions - -# Prettier tries to insert trailing commas in function calls, which Node.js -# doesn't natively support. This causes an error when loading the Gulp tasks. -packages/babel-standalone/src/gulpTasks.js diff --git a/.eslintrc b/.eslintrc index 709eff25503a..5b787848b191 100644 --- a/.eslintrc +++ b/.eslintrc @@ -5,11 +5,28 @@ ], "rules": { "curly": ["error", "multi-line"], - "prettier/prettier": ["error", { "trailingComma": "es5" }], + "prettier/prettier": "error", "no-case-declarations": "error" }, "env": { - "node": true, - "mocha": true - } + "node": true + }, + "overrides": [ + { + "files": [ + "packages/*/src/**/*.js", + "experimental/*/src/**/*.js", + "codemods/*/src/**/*.js" + ], + "rules": { + "no-undefined-identifier": "error" + } + }, + { + "files": [ "packages/*/test/**/*.js", "test/**/*.js" ], + "env": { + "mocha": true + } + } + ] } diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 000000000000..91c663d6bc4f --- /dev/null +++ b/.prettierrc @@ -0,0 +1,16 @@ +{ + "trailingComma": "es5", + "overrides": [{ + "files": [ + "**/experimental/*/src/**/*.js", + "**/experimental/*/test/**/*.js", + "**/codemods/*/src/**/*.js", + "**/codemods/*/test/**/*.js", + "**/packages/*/src/**/*.js", + "**/packages/*/test/**/*.js" + ], + "options": { + "trailingComma": "all" + } + }] +} diff --git a/packages/.eslintrc b/packages/.eslintrc deleted file mode 100644 index 06311000fa11..000000000000 --- a/packages/.eslintrc +++ /dev/null @@ -1,6 +0,0 @@ -{ - "rules": { - "prettier/prettier": ["error", { "trailingComma": "all" }], - "no-undefined-identifier": 2 - } -} diff --git a/packages/babel-helper-transform-fixture-test-runner/src/index.js b/packages/babel-helper-transform-fixture-test-runner/src/index.js index ea6aa1f498a8..19f5e4e50295 100644 --- a/packages/babel-helper-transform-fixture-test-runner/src/index.js +++ b/packages/babel-helper-transform-fixture-test-runner/src/index.js @@ -1,3 +1,4 @@ +/* eslint-env mocha */ import * as babel from "@babel/core"; import { buildExternalHelpers } from "@babel/core"; import getFixtures from "@babel/helper-fixtures"; diff --git a/packages/babylon/.eslintrc b/packages/babylon/.eslintrc deleted file mode 100644 index 622e276bdc5d..000000000000 --- a/packages/babylon/.eslintrc +++ /dev/null @@ -1,6 +0,0 @@ -{ - "extends": "../.eslintrc", - "rules": { - "curly": "off" - } -} diff --git a/packages/babylon/scripts/generate-identifier-regex.js b/packages/babylon/scripts/generate-identifier-regex.js index 95641eafe0e0..2d5ccdca6a12 100644 --- a/packages/babylon/scripts/generate-identifier-regex.js +++ b/packages/babylon/scripts/generate-identifier-regex.js @@ -14,12 +14,13 @@ const cont = [0x200c, 0x200d].concat( version + "/Binary_Property/ID_Continue/code-points.js").filter(function(ch) { return ch > 0x7f && search(start, ch, last + 1) == -1; - }), + }) ); function search(arr, ch, starting) { - for (let i = starting; arr[i] <= ch && i < arr.length; last = i++) + for (let i = starting; arr[i] <= ch && i < arr.length; last = i++) { if (arr[i] === ch) return i; + } return -1; } @@ -62,10 +63,8 @@ const contData = generate(cont); console.log('let nonASCIIidentifierStartChars = "' + startData.nonASCII + '";'); console.log('let nonASCIIidentifierChars = "' + contData.nonASCII + '";'); console.log( - "const astralIdentifierStartCodes = " + - JSON.stringify(startData.astral) + - ";", + "const astralIdentifierStartCodes = " + JSON.stringify(startData.astral) + ";" ); console.log( - "const astralIdentifierCodes = " + JSON.stringify(contData.astral) + ";", + "const astralIdentifierCodes = " + JSON.stringify(contData.astral) + ";" ); diff --git a/packages/babylon/src/parser/expression.js b/packages/babylon/src/parser/expression.js index 6213bcc1f8a1..28357735cb03 100644 --- a/packages/babylon/src/parser/expression.js +++ b/packages/babylon/src/parser/expression.js @@ -61,8 +61,9 @@ export default class ExpressionParser extends LValParser { const name = key.type === "Identifier" ? key.name : String(key.value); if (name === "__proto__") { - if (propHash.proto) + if (propHash.proto) { this.raise(key.start, "Redefinition of __proto__ property"); + } propHash.proto = true; } } @@ -124,8 +125,9 @@ export default class ExpressionParser extends LValParser { const startLoc = this.state.startLoc; if (this.match(tt._yield) && this.state.inGenerator) { let left = this.parseYield(); - if (afterLeftParse) + if (afterLeftParse) { left = afterLeftParse.call(this, left, startPos, startLoc); + } return left; } @@ -146,8 +148,9 @@ export default class ExpressionParser extends LValParser { refShorthandDefaultPos, refNeedsArrowPos, ); - if (afterLeftParse) + if (afterLeftParse) { left = afterLeftParse.call(this, left, startPos, startLoc); + } if (this.state.type.isAssign) { const node = this.startNodeAt(startPos, startLoc); node.operator = this.state.value; @@ -1000,8 +1003,9 @@ export default class ExpressionParser extends LValParser { (arrowNode = this.parseArrow(arrowNode)) ) { for (const param of exprList) { - if (param.extra && param.extra.parenthesized) + if (param.extra && param.extra.parenthesized) { this.unexpected(param.extra.parenStart); + } } this.parseArrowExpression(arrowNode, exprList); @@ -1016,8 +1020,9 @@ export default class ExpressionParser extends LValParser { } if (optionalCommaStart) this.unexpected(optionalCommaStart); if (spreadStart) this.unexpected(spreadStart); - if (refShorthandDefaultPos.start) + if (refShorthandDefaultPos.start) { this.unexpected(refShorthandDefaultPos.start); + } if (refNeedsArrowPos.start) this.unexpected(refNeedsArrowPos.start); if (exprList.length > 1) { diff --git a/packages/babylon/src/parser/lval.js b/packages/babylon/src/parser/lval.js index e703d979393d..5dabd72541fe 100644 --- a/packages/babylon/src/parser/lval.js +++ b/packages/babylon/src/parser/lval.js @@ -159,11 +159,12 @@ export default class LValParser extends NodeUtils { } for (let i = 0; i < end; i++) { const elt = exprList[i]; - if (elt && elt.type === "SpreadElement") + if (elt && elt.type === "SpreadElement") { this.raise( elt.start, "The rest element has to be the last element when destructuring", ); + } if (elt) this.toAssignable(elt, isBinding, contextDescription); } return exprList; @@ -352,13 +353,14 @@ export default class LValParser extends NodeUtils { case "ArrayPattern": for (const elem of expr.elements) { - if (elem) + if (elem) { this.checkLVal( elem, isBinding, checkClashes, "array destructuring pattern", ); + } } break; diff --git a/packages/babylon/src/parser/statement.js b/packages/babylon/src/parser/statement.js index 1147eea62ba7..d655345099bf 100644 --- a/packages/babylon/src/parser/statement.js +++ b/packages/babylon/src/parser/statement.js @@ -133,8 +133,9 @@ export default class StatementParser extends ExpressionParser { (this.hasPlugin("dynamicImport") && this.lookahead().type === tt.parenL) || (this.hasPlugin("importMeta") && this.lookahead().type === tt.dot) - ) + ) { break; + } if (!this.options.allowImportExportEverywhere && !topLevel) { this.raise( @@ -305,8 +306,9 @@ export default class StatementParser extends ExpressionParser { if (node.label && isBreak) break; } } - if (i === this.state.labels.length) + if (i === this.state.labels.length) { this.raise(node.start, "Unsyntactic " + keyword); + } return this.finishNode( node, isBreak ? "BreakStatement" : "ContinueStatement", @@ -449,8 +451,9 @@ export default class StatementParser extends ExpressionParser { if (isCase) { cur.test = this.parseExpression(); } else { - if (sawDefault) + if (sawDefault) { this.raise(this.state.lastTokStart, "Multiple default clauses"); + } sawDefault = true; cur.test = null; } @@ -473,8 +476,9 @@ export default class StatementParser extends ExpressionParser { this.next(); if ( lineBreak.test(this.input.slice(this.state.lastTokEnd, this.state.start)) - ) + ) { this.raise(this.state.lastTokEnd, "Illegal newline after throw"); + } node.argument = this.parseExpression(); this.semicolon(); return this.finishNode(node, "ThrowStatement"); @@ -533,8 +537,9 @@ export default class StatementParser extends ExpressionParser { } parseWithStatement(node: N.WithStatement): N.WithStatement { - if (this.state.strict) + if (this.state.strict) { this.raise(this.state.start, "'with' in strict mode"); + } this.next(); node.object = this.parseParenExpression(); node.body = this.parseStatement(false); diff --git a/packages/babylon/src/plugins/estree.js b/packages/babylon/src/plugins/estree.js index 8d1b15115407..70d5908c3811 100644 --- a/packages/babylon/src/plugins/estree.js +++ b/packages/babylon/src/plugins/estree.js @@ -121,8 +121,9 @@ export default (superClass: Class): Class => const name = key.type === "Identifier" ? key.name : String(key.value); if (name === "__proto__") { - if (propHash.proto) + if (propHash.proto) { this.raise(key.start, "Redefinition of __proto__ property"); + } propHash.proto = true; } } diff --git a/packages/babylon/src/plugins/flow.js b/packages/babylon/src/plugins/flow.js index 91c7a521ddb1..e26bd4301a79 100644 --- a/packages/babylon/src/plugins/flow.js +++ b/packages/babylon/src/plugins/flow.js @@ -184,11 +184,12 @@ export default (superClass: Class): Class => if (this.lookahead().type === tt.dot) { return this.flowParseDeclareModuleExports(node); } else { - if (insideModule) + if (insideModule) { this.unexpected( null, "`declare module` cannot be used inside another `declare module`", ); + } return this.flowParseDeclareModule(node); } } else if (this.isContextual("type")) { @@ -261,15 +262,17 @@ export default (superClass: Class): Class => "Found both `declare module.exports` and `declare export` in the same module. Modules can only have 1 since they are either an ES module or they are a CommonJS module"; body.forEach(bodyElement => { if (isEsModuleType(bodyElement)) { - if (kind === "CommonJS") + if (kind === "CommonJS") { this.unexpected(bodyElement.start, errorMessage); + } kind = "ES"; } else if (bodyElement.type === "DeclareModuleExports") { - if (hasModuleExport) + if (hasModuleExport) { this.unexpected( bodyElement.start, "Duplicate `declare module.exports` statement", ); + } if (kind === "ES") this.unexpected(bodyElement.start, errorMessage); kind = "CommonJS"; hasModuleExport = true; @@ -755,8 +758,9 @@ export default (superClass: Class): Class => node.value = this.flowParseObjectTypeMethodish( this.startNodeAt(node.start, node.loc.start), ); - if (kind === "get" || kind === "set") + if (kind === "get" || kind === "set") { this.flowCheckGetterSetterParamCount(node); + } } else { if (kind !== "init") this.unexpected(); if (this.eat(tt.question)) { @@ -1054,8 +1058,9 @@ export default (superClass: Class): Class => case tt.plusMin: if (this.state.value === "-") { this.next(); - if (!this.match(tt.num)) + if (!this.match(tt.num)) { this.unexpected(null, "Unexpected token, expected number"); + } return this.parseLiteral( -this.state.value, diff --git a/packages/babylon/src/plugins/jsx/index.js b/packages/babylon/src/plugins/jsx/index.js index 199aa652a355..9478bec17c3b 100644 --- a/packages/babylon/src/plugins/jsx/index.js +++ b/packages/babylon/src/plugins/jsx/index.js @@ -170,12 +170,14 @@ export default (superClass: Class): Class => if (str[0] === "#") { if (str[1] === "x") { str = str.substr(2); - if (HEX_NUMBER.test(str)) + if (HEX_NUMBER.test(str)) { entity = String.fromCodePoint(parseInt(str, 16)); + } } else { str = str.substr(1); - if (DECIMAL_NUMBER.test(str)) + if (DECIMAL_NUMBER.test(str)) { entity = String.fromCodePoint(parseInt(str, 10)); + } } } else { entity = XHTMLEntities[str]; diff --git a/packages/babylon/src/plugins/typescript.js b/packages/babylon/src/plugins/typescript.js index e1d13420c72e..c3b4735341f4 100644 --- a/packages/babylon/src/plugins/typescript.js +++ b/packages/babylon/src/plugins/typescript.js @@ -1009,10 +1009,12 @@ export default (superClass: Class): Class => node.end = original.end; node.loc.end = original.loc.end; - if (original.leadingComments) + if (original.leadingComments) { node.leadingComments = original.leadingComments; - if (original.trailingComments) + } + if (original.trailingComments) { node.trailingComments = original.trailingComments; + } if (original.innerComments) node.innerComments = original.innerComments; return node; diff --git a/packages/babylon/src/tokenizer/index.js b/packages/babylon/src/tokenizer/index.js index 26d003094fe4..b08448f7f63c 100644 --- a/packages/babylon/src/tokenizer/index.js +++ b/packages/babylon/src/tokenizer/index.js @@ -786,8 +786,9 @@ export default class Tokenizer extends LocationParser { const start = this.state.pos; let escaped, inClass; for (;;) { - if (this.state.pos >= this.input.length) + if (this.state.pos >= this.input.length) { this.raise(start, "Unterminated regular expression"); + } const ch = this.input.charAt(this.state.pos); if (lineBreak.test(ch)) { this.raise(start, "Unterminated regular expression"); @@ -813,8 +814,9 @@ export default class Tokenizer extends LocationParser { const mods = this.readWord1(); if (mods) { const validFlags = /^[gmsiyu]*$/; - if (!validFlags.test(mods)) + if (!validFlags.test(mods)) { this.raise(start, "Invalid regular expression flag"); + } } this.finishToken(tt.regexp, { @@ -886,8 +888,9 @@ export default class Tokenizer extends LocationParser { if ( this.state.pos === start || (len != null && this.state.pos - start !== len) - ) + ) { return null; + } return total; } @@ -898,8 +901,9 @@ export default class Tokenizer extends LocationParser { this.state.pos += 2; // 0x const val = this.readInt(radix); - if (val == null) + if (val == null) { this.raise(this.state.start + 2, "Expected number in radix " + radix); + } if (this.hasPlugin("bigInt")) { if (this.input.charCodeAt(this.state.pos) === 0x6e) { @@ -909,8 +913,9 @@ export default class Tokenizer extends LocationParser { } } - if (isIdentifierStart(this.fullCharCodeAtPos())) + if (isIdentifierStart(this.fullCharCodeAtPos())) { this.raise(this.state.pos, "Identifier directly after number"); + } if (isBigInt) { const str = this.input.slice(start, this.state.pos).replace(/[_n]/g, ""); @@ -929,8 +934,9 @@ export default class Tokenizer extends LocationParser { let isFloat = false; let isBigInt = false; - if (!startsWithDot && this.readInt(10) === null) + if (!startsWithDot && this.readInt(10) === null) { this.raise(start, "Invalid number"); + } if (octal && this.state.pos == start + 1) octal = false; // number === 0 let next = this.input.charCodeAt(this.state.pos); @@ -961,8 +967,9 @@ export default class Tokenizer extends LocationParser { } } - if (isIdentifierStart(this.fullCharCodeAtPos())) + if (isIdentifierStart(this.fullCharCodeAtPos())) { this.raise(this.state.pos, "Identifier directly after number"); + } // remove "_" for numeric literal separator, and "n" for BigInts const str = this.input.slice(start, this.state.pos).replace(/[_n]/g, ""); @@ -1022,8 +1029,9 @@ export default class Tokenizer extends LocationParser { let out = "", chunkStart = ++this.state.pos; for (;;) { - if (this.state.pos >= this.input.length) + if (this.state.pos >= this.input.length) { this.raise(this.state.start, "Unterminated string constant"); + } const ch = this.input.charCodeAt(this.state.pos); if (ch === quote) break; if (ch === 92) { @@ -1033,8 +1041,9 @@ export default class Tokenizer extends LocationParser { out += this.readEscapedChar(false); chunkStart = this.state.pos; } else { - if (isNewLine(ch)) + if (isNewLine(ch)) { this.raise(this.state.start, "Unterminated string constant"); + } ++this.state.pos; } } @@ -1049,8 +1058,9 @@ export default class Tokenizer extends LocationParser { chunkStart = this.state.pos, containsInvalid = false; for (;;) { - if (this.state.pos >= this.input.length) + if (this.state.pos >= this.input.length) { this.raise(this.state.start, "Unterminated template"); + } const ch = this.input.charCodeAt(this.state.pos); if ( ch === 96 || diff --git a/packages/babylon/src/util/identifier.js b/packages/babylon/src/util/identifier.js index c938eb9d5e3e..775fa57e88de 100644 --- a/packages/babylon/src/util/identifier.js +++ b/packages/babylon/src/util/identifier.js @@ -79,10 +79,11 @@ export function isIdentifierStart(code: number): boolean { if (code < 91) return true; if (code < 97) return code === 95; if (code < 123) return true; - if (code <= 0xffff) + if (code <= 0xffff) { return ( code >= 0xaa && nonASCIIidentifierStart.test(String.fromCharCode(code)) ); + } return isInAstralSet(code, astralIdentifierStartCodes); } @@ -95,8 +96,9 @@ export function isIdentifierChar(code: number): boolean { if (code < 91) return true; if (code < 97) return code === 95; if (code < 123) return true; - if (code <= 0xffff) + if (code <= 0xffff) { return code >= 0xaa && nonASCIIidentifier.test(String.fromCharCode(code)); + } return ( isInAstralSet(code, astralIdentifierStartCodes) || isInAstralSet(code, astralIdentifierCodes) diff --git a/packages/babylon/test/helpers/runFixtureTests.js b/packages/babylon/test/helpers/runFixtureTests.js index a38a117b8192..c7774bf3cff5 100644 --- a/packages/babylon/test/helpers/runFixtureTests.js +++ b/packages/babylon/test/helpers/runFixtureTests.js @@ -126,15 +126,17 @@ function misMatch(exp, act) { if (left !== right) return left + " !== " + right; } else if (Array.isArray(exp)) { if (!Array.isArray(act)) return ppJSON(exp) + " != " + ppJSON(act); - if (act.length != exp.length) + if (act.length != exp.length) { return "array length mismatch " + exp.length + " != " + act.length; + } for (let i = 0; i < act.length; ++i) { const mis = misMatch(exp[i], act[i]); if (mis) return addPath(mis, i); } } else if (!exp || !act || typeof exp != "object" || typeof act != "object") { - if (exp !== act && typeof exp != "function") + if (exp !== act && typeof exp != "function") { return ppJSON(exp) + " !== " + ppJSON(act); + } } else { for (const prop in exp) { const mis = misMatch(exp[prop], act[prop]);