Unify eslint/prettier config (#6747)

* Unify eslint/prettier config

Use a prettier config file and correctly configure trailing commas

Enable curly in babylon as in all other packages.

* Add experimental and codemods
This commit is contained in:
Daniel Tschinder 2017-11-06 14:19:59 +01:00 committed by GitHub
parent de72ce6ce7
commit cc66495a95
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 115 additions and 63 deletions

View File

@ -21,9 +21,4 @@ experimental/babel-preset-env/test/debug-fixtures
packages/babel-standalone/babel.js packages/babel-standalone/babel.js
packages/babel-standalone/babel.min.js packages/babel-standalone/babel.min.js
packages/babylon/build packages/babylon/build
packages/babylon/scripts
packages/babylon/test/expressions 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

View File

@ -5,11 +5,28 @@
], ],
"rules": { "rules": {
"curly": ["error", "multi-line"], "curly": ["error", "multi-line"],
"prettier/prettier": ["error", { "trailingComma": "es5" }], "prettier/prettier": "error",
"no-case-declarations": "error" "no-case-declarations": "error"
}, },
"env": { "env": {
"node": true, "node": true
"mocha": 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
}
}
]
} }

16
.prettierrc Normal file
View File

@ -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"
}
}]
}

View File

@ -1,6 +0,0 @@
{
"rules": {
"prettier/prettier": ["error", { "trailingComma": "all" }],
"no-undefined-identifier": 2
}
}

View File

@ -1,3 +1,4 @@
/* eslint-env mocha */
import * as babel from "@babel/core"; import * as babel from "@babel/core";
import { buildExternalHelpers } from "@babel/core"; import { buildExternalHelpers } from "@babel/core";
import getFixtures from "@babel/helper-fixtures"; import getFixtures from "@babel/helper-fixtures";

View File

@ -1,6 +0,0 @@
{
"extends": "../.eslintrc",
"rules": {
"curly": "off"
}
}

View File

@ -14,12 +14,13 @@ const cont = [0x200c, 0x200d].concat(
version + version +
"/Binary_Property/ID_Continue/code-points.js").filter(function(ch) { "/Binary_Property/ID_Continue/code-points.js").filter(function(ch) {
return ch > 0x7f && search(start, ch, last + 1) == -1; return ch > 0x7f && search(start, ch, last + 1) == -1;
}), })
); );
function search(arr, ch, starting) { 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; if (arr[i] === ch) return i;
}
return -1; return -1;
} }
@ -62,10 +63,8 @@ const contData = generate(cont);
console.log('let nonASCIIidentifierStartChars = "' + startData.nonASCII + '";'); console.log('let nonASCIIidentifierStartChars = "' + startData.nonASCII + '";');
console.log('let nonASCIIidentifierChars = "' + contData.nonASCII + '";'); console.log('let nonASCIIidentifierChars = "' + contData.nonASCII + '";');
console.log( console.log(
"const astralIdentifierStartCodes = " + "const astralIdentifierStartCodes = " + JSON.stringify(startData.astral) + ";"
JSON.stringify(startData.astral) +
";",
); );
console.log( console.log(
"const astralIdentifierCodes = " + JSON.stringify(contData.astral) + ";", "const astralIdentifierCodes = " + JSON.stringify(contData.astral) + ";"
); );

View File

@ -61,8 +61,9 @@ export default class ExpressionParser extends LValParser {
const name = key.type === "Identifier" ? key.name : String(key.value); const name = key.type === "Identifier" ? key.name : String(key.value);
if (name === "__proto__") { if (name === "__proto__") {
if (propHash.proto) if (propHash.proto) {
this.raise(key.start, "Redefinition of __proto__ property"); this.raise(key.start, "Redefinition of __proto__ property");
}
propHash.proto = true; propHash.proto = true;
} }
} }
@ -124,8 +125,9 @@ export default class ExpressionParser extends LValParser {
const startLoc = this.state.startLoc; const startLoc = this.state.startLoc;
if (this.match(tt._yield) && this.state.inGenerator) { if (this.match(tt._yield) && this.state.inGenerator) {
let left = this.parseYield(); let left = this.parseYield();
if (afterLeftParse) if (afterLeftParse) {
left = afterLeftParse.call(this, left, startPos, startLoc); left = afterLeftParse.call(this, left, startPos, startLoc);
}
return left; return left;
} }
@ -146,8 +148,9 @@ export default class ExpressionParser extends LValParser {
refShorthandDefaultPos, refShorthandDefaultPos,
refNeedsArrowPos, refNeedsArrowPos,
); );
if (afterLeftParse) if (afterLeftParse) {
left = afterLeftParse.call(this, left, startPos, startLoc); left = afterLeftParse.call(this, left, startPos, startLoc);
}
if (this.state.type.isAssign) { if (this.state.type.isAssign) {
const node = this.startNodeAt(startPos, startLoc); const node = this.startNodeAt(startPos, startLoc);
node.operator = this.state.value; node.operator = this.state.value;
@ -1000,8 +1003,9 @@ export default class ExpressionParser extends LValParser {
(arrowNode = this.parseArrow(arrowNode)) (arrowNode = this.parseArrow(arrowNode))
) { ) {
for (const param of exprList) { for (const param of exprList) {
if (param.extra && param.extra.parenthesized) if (param.extra && param.extra.parenthesized) {
this.unexpected(param.extra.parenStart); this.unexpected(param.extra.parenStart);
}
} }
this.parseArrowExpression(arrowNode, exprList); this.parseArrowExpression(arrowNode, exprList);
@ -1016,8 +1020,9 @@ export default class ExpressionParser extends LValParser {
} }
if (optionalCommaStart) this.unexpected(optionalCommaStart); if (optionalCommaStart) this.unexpected(optionalCommaStart);
if (spreadStart) this.unexpected(spreadStart); if (spreadStart) this.unexpected(spreadStart);
if (refShorthandDefaultPos.start) if (refShorthandDefaultPos.start) {
this.unexpected(refShorthandDefaultPos.start); this.unexpected(refShorthandDefaultPos.start);
}
if (refNeedsArrowPos.start) this.unexpected(refNeedsArrowPos.start); if (refNeedsArrowPos.start) this.unexpected(refNeedsArrowPos.start);
if (exprList.length > 1) { if (exprList.length > 1) {

View File

@ -159,11 +159,12 @@ export default class LValParser extends NodeUtils {
} }
for (let i = 0; i < end; i++) { for (let i = 0; i < end; i++) {
const elt = exprList[i]; const elt = exprList[i];
if (elt && elt.type === "SpreadElement") if (elt && elt.type === "SpreadElement") {
this.raise( this.raise(
elt.start, elt.start,
"The rest element has to be the last element when destructuring", "The rest element has to be the last element when destructuring",
); );
}
if (elt) this.toAssignable(elt, isBinding, contextDescription); if (elt) this.toAssignable(elt, isBinding, contextDescription);
} }
return exprList; return exprList;
@ -352,13 +353,14 @@ export default class LValParser extends NodeUtils {
case "ArrayPattern": case "ArrayPattern":
for (const elem of expr.elements) { for (const elem of expr.elements) {
if (elem) if (elem) {
this.checkLVal( this.checkLVal(
elem, elem,
isBinding, isBinding,
checkClashes, checkClashes,
"array destructuring pattern", "array destructuring pattern",
); );
}
} }
break; break;

View File

@ -133,8 +133,9 @@ export default class StatementParser extends ExpressionParser {
(this.hasPlugin("dynamicImport") && (this.hasPlugin("dynamicImport") &&
this.lookahead().type === tt.parenL) || this.lookahead().type === tt.parenL) ||
(this.hasPlugin("importMeta") && this.lookahead().type === tt.dot) (this.hasPlugin("importMeta") && this.lookahead().type === tt.dot)
) ) {
break; break;
}
if (!this.options.allowImportExportEverywhere && !topLevel) { if (!this.options.allowImportExportEverywhere && !topLevel) {
this.raise( this.raise(
@ -305,8 +306,9 @@ export default class StatementParser extends ExpressionParser {
if (node.label && isBreak) break; if (node.label && isBreak) break;
} }
} }
if (i === this.state.labels.length) if (i === this.state.labels.length) {
this.raise(node.start, "Unsyntactic " + keyword); this.raise(node.start, "Unsyntactic " + keyword);
}
return this.finishNode( return this.finishNode(
node, node,
isBreak ? "BreakStatement" : "ContinueStatement", isBreak ? "BreakStatement" : "ContinueStatement",
@ -449,8 +451,9 @@ export default class StatementParser extends ExpressionParser {
if (isCase) { if (isCase) {
cur.test = this.parseExpression(); cur.test = this.parseExpression();
} else { } else {
if (sawDefault) if (sawDefault) {
this.raise(this.state.lastTokStart, "Multiple default clauses"); this.raise(this.state.lastTokStart, "Multiple default clauses");
}
sawDefault = true; sawDefault = true;
cur.test = null; cur.test = null;
} }
@ -473,8 +476,9 @@ export default class StatementParser extends ExpressionParser {
this.next(); this.next();
if ( if (
lineBreak.test(this.input.slice(this.state.lastTokEnd, this.state.start)) lineBreak.test(this.input.slice(this.state.lastTokEnd, this.state.start))
) ) {
this.raise(this.state.lastTokEnd, "Illegal newline after throw"); this.raise(this.state.lastTokEnd, "Illegal newline after throw");
}
node.argument = this.parseExpression(); node.argument = this.parseExpression();
this.semicolon(); this.semicolon();
return this.finishNode(node, "ThrowStatement"); return this.finishNode(node, "ThrowStatement");
@ -533,8 +537,9 @@ export default class StatementParser extends ExpressionParser {
} }
parseWithStatement(node: N.WithStatement): N.WithStatement { parseWithStatement(node: N.WithStatement): N.WithStatement {
if (this.state.strict) if (this.state.strict) {
this.raise(this.state.start, "'with' in strict mode"); this.raise(this.state.start, "'with' in strict mode");
}
this.next(); this.next();
node.object = this.parseParenExpression(); node.object = this.parseParenExpression();
node.body = this.parseStatement(false); node.body = this.parseStatement(false);

View File

@ -121,8 +121,9 @@ export default (superClass: Class<Parser>): Class<Parser> =>
const name = key.type === "Identifier" ? key.name : String(key.value); const name = key.type === "Identifier" ? key.name : String(key.value);
if (name === "__proto__") { if (name === "__proto__") {
if (propHash.proto) if (propHash.proto) {
this.raise(key.start, "Redefinition of __proto__ property"); this.raise(key.start, "Redefinition of __proto__ property");
}
propHash.proto = true; propHash.proto = true;
} }
} }

View File

@ -184,11 +184,12 @@ export default (superClass: Class<Parser>): Class<Parser> =>
if (this.lookahead().type === tt.dot) { if (this.lookahead().type === tt.dot) {
return this.flowParseDeclareModuleExports(node); return this.flowParseDeclareModuleExports(node);
} else { } else {
if (insideModule) if (insideModule) {
this.unexpected( this.unexpected(
null, null,
"`declare module` cannot be used inside another `declare module`", "`declare module` cannot be used inside another `declare module`",
); );
}
return this.flowParseDeclareModule(node); return this.flowParseDeclareModule(node);
} }
} else if (this.isContextual("type")) { } else if (this.isContextual("type")) {
@ -261,15 +262,17 @@ export default (superClass: Class<Parser>): Class<Parser> =>
"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"; "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 => { body.forEach(bodyElement => {
if (isEsModuleType(bodyElement)) { if (isEsModuleType(bodyElement)) {
if (kind === "CommonJS") if (kind === "CommonJS") {
this.unexpected(bodyElement.start, errorMessage); this.unexpected(bodyElement.start, errorMessage);
}
kind = "ES"; kind = "ES";
} else if (bodyElement.type === "DeclareModuleExports") { } else if (bodyElement.type === "DeclareModuleExports") {
if (hasModuleExport) if (hasModuleExport) {
this.unexpected( this.unexpected(
bodyElement.start, bodyElement.start,
"Duplicate `declare module.exports` statement", "Duplicate `declare module.exports` statement",
); );
}
if (kind === "ES") this.unexpected(bodyElement.start, errorMessage); if (kind === "ES") this.unexpected(bodyElement.start, errorMessage);
kind = "CommonJS"; kind = "CommonJS";
hasModuleExport = true; hasModuleExport = true;
@ -755,8 +758,9 @@ export default (superClass: Class<Parser>): Class<Parser> =>
node.value = this.flowParseObjectTypeMethodish( node.value = this.flowParseObjectTypeMethodish(
this.startNodeAt(node.start, node.loc.start), this.startNodeAt(node.start, node.loc.start),
); );
if (kind === "get" || kind === "set") if (kind === "get" || kind === "set") {
this.flowCheckGetterSetterParamCount(node); this.flowCheckGetterSetterParamCount(node);
}
} else { } else {
if (kind !== "init") this.unexpected(); if (kind !== "init") this.unexpected();
if (this.eat(tt.question)) { if (this.eat(tt.question)) {
@ -1054,8 +1058,9 @@ export default (superClass: Class<Parser>): Class<Parser> =>
case tt.plusMin: case tt.plusMin:
if (this.state.value === "-") { if (this.state.value === "-") {
this.next(); this.next();
if (!this.match(tt.num)) if (!this.match(tt.num)) {
this.unexpected(null, "Unexpected token, expected number"); this.unexpected(null, "Unexpected token, expected number");
}
return this.parseLiteral( return this.parseLiteral(
-this.state.value, -this.state.value,

View File

@ -170,12 +170,14 @@ export default (superClass: Class<Parser>): Class<Parser> =>
if (str[0] === "#") { if (str[0] === "#") {
if (str[1] === "x") { if (str[1] === "x") {
str = str.substr(2); str = str.substr(2);
if (HEX_NUMBER.test(str)) if (HEX_NUMBER.test(str)) {
entity = String.fromCodePoint(parseInt(str, 16)); entity = String.fromCodePoint(parseInt(str, 16));
}
} else { } else {
str = str.substr(1); str = str.substr(1);
if (DECIMAL_NUMBER.test(str)) if (DECIMAL_NUMBER.test(str)) {
entity = String.fromCodePoint(parseInt(str, 10)); entity = String.fromCodePoint(parseInt(str, 10));
}
} }
} else { } else {
entity = XHTMLEntities[str]; entity = XHTMLEntities[str];

View File

@ -1009,10 +1009,12 @@ export default (superClass: Class<Parser>): Class<Parser> =>
node.end = original.end; node.end = original.end;
node.loc.end = original.loc.end; node.loc.end = original.loc.end;
if (original.leadingComments) if (original.leadingComments) {
node.leadingComments = original.leadingComments; node.leadingComments = original.leadingComments;
if (original.trailingComments) }
if (original.trailingComments) {
node.trailingComments = original.trailingComments; node.trailingComments = original.trailingComments;
}
if (original.innerComments) node.innerComments = original.innerComments; if (original.innerComments) node.innerComments = original.innerComments;
return node; return node;

View File

@ -786,8 +786,9 @@ export default class Tokenizer extends LocationParser {
const start = this.state.pos; const start = this.state.pos;
let escaped, inClass; let escaped, inClass;
for (;;) { for (;;) {
if (this.state.pos >= this.input.length) if (this.state.pos >= this.input.length) {
this.raise(start, "Unterminated regular expression"); this.raise(start, "Unterminated regular expression");
}
const ch = this.input.charAt(this.state.pos); const ch = this.input.charAt(this.state.pos);
if (lineBreak.test(ch)) { if (lineBreak.test(ch)) {
this.raise(start, "Unterminated regular expression"); this.raise(start, "Unterminated regular expression");
@ -813,8 +814,9 @@ export default class Tokenizer extends LocationParser {
const mods = this.readWord1(); const mods = this.readWord1();
if (mods) { if (mods) {
const validFlags = /^[gmsiyu]*$/; const validFlags = /^[gmsiyu]*$/;
if (!validFlags.test(mods)) if (!validFlags.test(mods)) {
this.raise(start, "Invalid regular expression flag"); this.raise(start, "Invalid regular expression flag");
}
} }
this.finishToken(tt.regexp, { this.finishToken(tt.regexp, {
@ -886,8 +888,9 @@ export default class Tokenizer extends LocationParser {
if ( if (
this.state.pos === start || this.state.pos === start ||
(len != null && this.state.pos - start !== len) (len != null && this.state.pos - start !== len)
) ) {
return null; return null;
}
return total; return total;
} }
@ -898,8 +901,9 @@ export default class Tokenizer extends LocationParser {
this.state.pos += 2; // 0x this.state.pos += 2; // 0x
const val = this.readInt(radix); const val = this.readInt(radix);
if (val == null) if (val == null) {
this.raise(this.state.start + 2, "Expected number in radix " + radix); this.raise(this.state.start + 2, "Expected number in radix " + radix);
}
if (this.hasPlugin("bigInt")) { if (this.hasPlugin("bigInt")) {
if (this.input.charCodeAt(this.state.pos) === 0x6e) { 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"); this.raise(this.state.pos, "Identifier directly after number");
}
if (isBigInt) { if (isBigInt) {
const str = this.input.slice(start, this.state.pos).replace(/[_n]/g, ""); 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 isFloat = false;
let isBigInt = false; let isBigInt = false;
if (!startsWithDot && this.readInt(10) === null) if (!startsWithDot && this.readInt(10) === null) {
this.raise(start, "Invalid number"); this.raise(start, "Invalid number");
}
if (octal && this.state.pos == start + 1) octal = false; // number === 0 if (octal && this.state.pos == start + 1) octal = false; // number === 0
let next = this.input.charCodeAt(this.state.pos); 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"); this.raise(this.state.pos, "Identifier directly after number");
}
// remove "_" for numeric literal separator, and "n" for BigInts // remove "_" for numeric literal separator, and "n" for BigInts
const str = this.input.slice(start, this.state.pos).replace(/[_n]/g, ""); const str = this.input.slice(start, this.state.pos).replace(/[_n]/g, "");
@ -1022,8 +1029,9 @@ export default class Tokenizer extends LocationParser {
let out = "", let out = "",
chunkStart = ++this.state.pos; chunkStart = ++this.state.pos;
for (;;) { for (;;) {
if (this.state.pos >= this.input.length) if (this.state.pos >= this.input.length) {
this.raise(this.state.start, "Unterminated string constant"); this.raise(this.state.start, "Unterminated string constant");
}
const ch = this.input.charCodeAt(this.state.pos); const ch = this.input.charCodeAt(this.state.pos);
if (ch === quote) break; if (ch === quote) break;
if (ch === 92) { if (ch === 92) {
@ -1033,8 +1041,9 @@ export default class Tokenizer extends LocationParser {
out += this.readEscapedChar(false); out += this.readEscapedChar(false);
chunkStart = this.state.pos; chunkStart = this.state.pos;
} else { } else {
if (isNewLine(ch)) if (isNewLine(ch)) {
this.raise(this.state.start, "Unterminated string constant"); this.raise(this.state.start, "Unterminated string constant");
}
++this.state.pos; ++this.state.pos;
} }
} }
@ -1049,8 +1058,9 @@ export default class Tokenizer extends LocationParser {
chunkStart = this.state.pos, chunkStart = this.state.pos,
containsInvalid = false; containsInvalid = false;
for (;;) { for (;;) {
if (this.state.pos >= this.input.length) if (this.state.pos >= this.input.length) {
this.raise(this.state.start, "Unterminated template"); this.raise(this.state.start, "Unterminated template");
}
const ch = this.input.charCodeAt(this.state.pos); const ch = this.input.charCodeAt(this.state.pos);
if ( if (
ch === 96 || ch === 96 ||

View File

@ -79,10 +79,11 @@ export function isIdentifierStart(code: number): boolean {
if (code < 91) return true; if (code < 91) return true;
if (code < 97) return code === 95; if (code < 97) return code === 95;
if (code < 123) return true; if (code < 123) return true;
if (code <= 0xffff) if (code <= 0xffff) {
return ( return (
code >= 0xaa && nonASCIIidentifierStart.test(String.fromCharCode(code)) code >= 0xaa && nonASCIIidentifierStart.test(String.fromCharCode(code))
); );
}
return isInAstralSet(code, astralIdentifierStartCodes); return isInAstralSet(code, astralIdentifierStartCodes);
} }
@ -95,8 +96,9 @@ export function isIdentifierChar(code: number): boolean {
if (code < 91) return true; if (code < 91) return true;
if (code < 97) return code === 95; if (code < 97) return code === 95;
if (code < 123) return true; if (code < 123) return true;
if (code <= 0xffff) if (code <= 0xffff) {
return code >= 0xaa && nonASCIIidentifier.test(String.fromCharCode(code)); return code >= 0xaa && nonASCIIidentifier.test(String.fromCharCode(code));
}
return ( return (
isInAstralSet(code, astralIdentifierStartCodes) || isInAstralSet(code, astralIdentifierStartCodes) ||
isInAstralSet(code, astralIdentifierCodes) isInAstralSet(code, astralIdentifierCodes)

View File

@ -126,15 +126,17 @@ function misMatch(exp, act) {
if (left !== right) return left + " !== " + right; if (left !== right) return left + " !== " + right;
} else if (Array.isArray(exp)) { } else if (Array.isArray(exp)) {
if (!Array.isArray(act)) return ppJSON(exp) + " != " + ppJSON(act); 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; return "array length mismatch " + exp.length + " != " + act.length;
}
for (let i = 0; i < act.length; ++i) { for (let i = 0; i < act.length; ++i) {
const mis = misMatch(exp[i], act[i]); const mis = misMatch(exp[i], act[i]);
if (mis) return addPath(mis, i); if (mis) return addPath(mis, i);
} }
} else if (!exp || !act || typeof exp != "object" || typeof act != "object") { } 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); return ppJSON(exp) + " !== " + ppJSON(act);
}
} else { } else {
for (const prop in exp) { for (const prop in exp) {
const mis = misMatch(exp[prop], act[prop]); const mis = misMatch(exp[prop], act[prop]);