Skip to content

Commit

Permalink
Unify eslint/prettier config (babel#6747)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
danez authored Nov 6, 2017
1 parent de72ce6 commit cc66495
Show file tree
Hide file tree
Showing 17 changed files with 115 additions and 63 deletions.
5 changes: 0 additions & 5 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
25 changes: 21 additions & 4 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
]
}
16 changes: 16 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -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"
}
}]
}
6 changes: 0 additions & 6 deletions packages/.eslintrc

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-env mocha */
import * as babel from "@babel/core";
import { buildExternalHelpers } from "@babel/core";
import getFixtures from "@babel/helper-fixtures";
Expand Down
6 changes: 0 additions & 6 deletions packages/babylon/.eslintrc

This file was deleted.

11 changes: 5 additions & 6 deletions packages/babylon/scripts/generate-identifier-regex.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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) + ";"
);
15 changes: 10 additions & 5 deletions packages/babylon/src/parser/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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) {
Expand Down
6 changes: 4 additions & 2 deletions packages/babylon/src/parser/lval.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down
15 changes: 10 additions & 5 deletions packages/babylon/src/parser/statement.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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;
}
Expand All @@ -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");
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion packages/babylon/src/plugins/estree.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,9 @@ export default (superClass: Class<Parser>): Class<Parser> =>
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;
}
}
Expand Down
15 changes: 10 additions & 5 deletions packages/babylon/src/plugins/flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,12 @@ export default (superClass: Class<Parser>): Class<Parser> =>
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")) {
Expand Down Expand Up @@ -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";
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;
Expand Down Expand Up @@ -755,8 +758,9 @@ export default (superClass: Class<Parser>): Class<Parser> =>
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)) {
Expand Down Expand Up @@ -1054,8 +1058,9 @@ export default (superClass: Class<Parser>): Class<Parser> =>
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,
Expand Down
6 changes: 4 additions & 2 deletions packages/babylon/src/plugins/jsx/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,14 @@ export default (superClass: Class<Parser>): Class<Parser> =>
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];
Expand Down
6 changes: 4 additions & 2 deletions packages/babylon/src/plugins/typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -1009,10 +1009,12 @@ export default (superClass: Class<Parser>): Class<Parser> =>
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;
Expand Down
Loading

0 comments on commit cc66495

Please sign in to comment.