Re-enable the max-len ESLint rule. (#5265)

This commit is contained in:
Logan Smyth
2017-02-04 08:07:15 -08:00
committed by Henry Zhu
parent 4d411ef83e
commit b845f2b69d
63 changed files with 317 additions and 223 deletions

View File

@@ -1,5 +1,3 @@
/* eslint max-len: 0 */
import TraversalContext from "./context";
import * as visitors from "./visitors";
import * as messages from "babel-messages";
@@ -45,7 +43,14 @@ traverse.cheap = function (node, enter) {
return t.traverseFast(node, enter);
};
traverse.node = function (node: Object, opts: Object, scope: Object, state: Object, parentPath: Object, skipKeys?) {
traverse.node = function (
node: Object,
opts: Object,
scope: Object,
state: Object,
parentPath: Object,
skipKeys?
) {
const keys: Array = t.VISITOR_KEYS[node.type];
if (!keys) return;
@@ -74,7 +79,12 @@ function hasBlacklistedType(path, state) {
}
}
traverse.hasType = function (tree: Object, scope: Object, type: Object, blacklistTypes: Array<string>): boolean {
traverse.hasType = function (
tree: Object,
scope: Object,
type: Object,
blacklistTypes: Array<string>
): boolean {
// the node we're searching in is blacklisted
if (includes(blacklistTypes, tree.type)) return false;

View File

@@ -1,12 +1,7 @@
/* eslint indent: 0 */
/* eslint max-len: 0 */
import type NodePath from "./index";
// This file contains Babels metainterpreter that can evaluate static code.
/* eslint eqeqeq: 0 */
const VALID_CALLEES = ["String", "Number", "Math"];
const INVALID_METHODS = ["random"];
@@ -316,7 +311,7 @@ export function evaluate(): { confident: boolean; value: any } {
case ">": return left > right;
case "<=": return left <= right;
case ">=": return left >= right;
case "==": return left == right;
case "==": return left == right; // eslint-disable-line eqeqeq
case "!=": return left != right;
case "===": return left === right;
case "!==": return left !== right;
@@ -335,7 +330,10 @@ export function evaluate(): { confident: boolean; value: any } {
let func;
// Number(1);
if (callee.isIdentifier() && !path.scope.getBinding(callee.node.name, true) && VALID_CALLEES.indexOf(callee.node.name) >= 0) {
if (
callee.isIdentifier() && !path.scope.getBinding(callee.node.name, true) &&
VALID_CALLEES.indexOf(callee.node.name) >= 0
) {
func = global[node.callee.name];
}
@@ -344,7 +342,11 @@ export function evaluate(): { confident: boolean; value: any } {
const property = callee.get("property");
// Math.min(1, 2)
if (object.isIdentifier() && property.isIdentifier() && VALID_CALLEES.indexOf(object.node.name) >= 0 && INVALID_METHODS.indexOf(property.node.name) < 0) {
if (
object.isIdentifier() && property.isIdentifier() &&
VALID_CALLEES.indexOf(object.node.name) >= 0 &&
INVALID_METHODS.indexOf(property.node.name) < 0
) {
context = global[object.node.name];
func = context[property.node.name];
}

View File

@@ -1,5 +1,3 @@
/* eslint max-len: 0 */
import type Hub from "../hub";
import type TraversalContext from "../context";
import * as virtualTypes from "./lib/virtual-types";

View File

@@ -153,7 +153,8 @@ export default class PathHoister {
const attachTo = this.getAttachmentPath();
if (!attachTo) return;
// don't bother hoisting to the same function as this will cause multiple branches to be evaluated more than once leading to a bad optimisation
// don't bother hoisting to the same function as this will cause multiple branches to be
// evaluated more than once leading to a bad optimisation
if (attachTo.getFunctionParent() === this.path.getFunctionParent()) return;
// generate declaration and insert it to our point

View File

@@ -6,28 +6,29 @@
export const hooks = [
function (self, parent) {
let removeParent = false;
const removeParent =
// while (NODE);
// removing the test of a while/switch, we can either just remove it entirely *or* turn the
// `test` into `true` unlikely that the latter will ever be what's wanted so we just remove
// the loop to avoid infinite recursion
(self.key === "test" && (parent.isWhile() || parent.isSwitchCase())) ||
// while (NODE);
// removing the test of a while/switch, we can either just remove it entirely *or* turn the `test` into `true`
// unlikely that the latter will ever be what's wanted so we just remove the loop to avoid infinite recursion
removeParent = removeParent || (self.key === "test" && (parent.isWhile() || parent.isSwitchCase()));
// export NODE;
// just remove a declaration for an export as this is no longer valid
(self.key === "declaration" && parent.isExportDeclaration()) ||
// export NODE;
// just remove a declaration for an export as this is no longer valid
removeParent = removeParent || (self.key === "declaration" && parent.isExportDeclaration());
// label: NODE
// stray labeled statement with no body
(self.key === "body" && parent.isLabeledStatement()) ||
// label: NODE
// stray labeled statement with no body
removeParent = removeParent || (self.key === "body" && parent.isLabeledStatement());
// let NODE;
// remove an entire declaration if there are no declarators left
(self.listKey === "declarations" && parent.isVariableDeclaration() &&
parent.node.declarations.length === 1) ||
// let NODE;
// remove an entire declaration if there are no declarators left
removeParent = removeParent || (self.listKey === "declarations" && parent.isVariableDeclaration() && parent.node.declarations.length === 1);
// NODE;
// remove the entire expression statement if there's no expression
removeParent = removeParent || (self.key === "expression" && parent.isExpressionStatement());
// NODE;
// remove the entire expression statement if there's no expression
(self.key === "expression" && parent.isExpressionStatement());
if (removeParent) {
parent.remove();

View File

@@ -1,4 +1,3 @@
/* eslint max-len: 0 */
// This file contains methods that modify the path/node in some ways.
import { path as pathCache } from "../cache";
@@ -17,7 +16,10 @@ export function insertBefore(nodes) {
if (this.parentPath.isExpressionStatement() || this.parentPath.isLabeledStatement()) {
return this.parentPath.insertBefore(nodes);
} else if (this.isNodeType("Expression") || (this.parentPath.isForStatement() && this.key === "init")) {
} else if (
this.isNodeType("Expression") ||
(this.parentPath.isForStatement() && this.key === "init")
) {
if (this.node) nodes.push(this.node);
this.replaceExpressionWithStatements(nodes);
} else {
@@ -28,7 +30,8 @@ export function insertBefore(nodes) {
if (this.node) nodes.push(this.node);
this._replaceWith(t.blockStatement(nodes));
} else {
throw new Error("We don't know what to do with this node type. We were previously a Statement but we can't fit in here?");
throw new Error("We don't know what to do with this node type. " +
"We were previously a Statement but we can't fit in here?");
}
}
@@ -88,7 +91,8 @@ export function _containerInsertAfter(nodes) {
export function _maybePopFromStatements(nodes) {
const last = nodes[nodes.length - 1];
const isIdentifier = t.isIdentifier(last) || (t.isExpressionStatement(last) && t.isIdentifier(last.expression));
const isIdentifier = t.isIdentifier(last) ||
(t.isExpressionStatement(last) && t.isIdentifier(last.expression));
if (isIdentifier && !this.isCompletionRecord()) {
nodes.pop();
@@ -107,7 +111,10 @@ export function insertAfter(nodes) {
if (this.parentPath.isExpressionStatement() || this.parentPath.isLabeledStatement()) {
return this.parentPath.insertAfter(nodes);
} else if (this.isNodeType("Expression") || (this.parentPath.isForStatement() && this.key === "init")) {
} else if (
this.isNodeType("Expression") ||
(this.parentPath.isForStatement() && this.key === "init")
) {
if (this.node) {
const temp = this.scope.generateDeclaredUidIdentifier();
nodes.unshift(t.expressionStatement(t.assignmentExpression("=", temp, this.node)));
@@ -122,7 +129,8 @@ export function insertAfter(nodes) {
if (this.node) nodes.unshift(this.node);
this._replaceWith(t.blockStatement(nodes));
} else {
throw new Error("We don't know what to do with this node type. We were previously a Statement but we can't fit in here?");
throw new Error("We don't know what to do with this node type. " +
"We were previously a Statement but we can't fit in here?");
}
}

View File

@@ -1,4 +1,3 @@
/* eslint max-len: 0 */
// This file contains methods responsible for replacing a node with another.
import codeFrame from "babel-code-frame";
@@ -114,22 +113,30 @@ export function replaceWith(replacement) {
}
if (Array.isArray(replacement)) {
throw new Error("Don't use `path.replaceWith()` with an array of nodes, use `path.replaceWithMultiple()`");
throw new Error(
"Don't use `path.replaceWith()` with an array of nodes, use `path.replaceWithMultiple()`");
}
if (typeof replacement === "string") {
throw new Error("Don't use `path.replaceWith()` with a source string, use `path.replaceWithSourceString()`");
throw new Error(
"Don't use `path.replaceWith()` with a source string, use `path.replaceWithSourceString()`");
}
if (this.isNodeType("Statement") && t.isExpression(replacement)) {
if (!this.canHaveVariableDeclarationOrExpression() && !this.canSwapBetweenExpressionAndStatement(replacement)) {
if (
!this.canHaveVariableDeclarationOrExpression() &&
!this.canSwapBetweenExpressionAndStatement(replacement)
) {
// replacing a statement with an expression so wrap it in an expression statement
replacement = t.expressionStatement(replacement);
}
}
if (this.isNodeType("Expression") && t.isStatement(replacement)) {
if (!this.canHaveVariableDeclarationOrExpression() && !this.canSwapBetweenExpressionAndStatement(replacement)) {
if (
!this.canHaveVariableDeclarationOrExpression() &&
!this.canSwapBetweenExpressionAndStatement(replacement)
) {
// replacing an expression with a statement so let's explode it
return this.replaceExpressionWithStatements([replacement]);
}

View File

@@ -1,5 +1,3 @@
/* eslint max-len: 0 */
import includes from "lodash/includes";
import repeat from "lodash/repeat";
import Renamer from "./lib/renamer";
@@ -341,13 +339,11 @@ export default class Scope {
// ignore hoisted functions if there's also a local let
if (kind === "hoisted" && local.kind === "let") return;
let duplicate = false;
// don't allow duplicate bindings to exist alongside
if (!duplicate) duplicate = kind === "let" || local.kind === "let" || local.kind === "const" || local.kind === "module";
// don't allow a local of param with a kind of let
if (!duplicate) duplicate = local.kind === "param" && (kind === "let" || kind === "const");
const duplicate =
// don't allow duplicate bindings to exist alongside
kind === "let" || local.kind === "let" || local.kind === "const" || local.kind === "module" ||
// don't allow a local of param with a kind of let
local.kind === "param" && (kind === "let" || kind === "const");
if (duplicate) {
throw this.hub.file.buildCodeFrameError(id, messages.get("scopeDuplicateDeclaration", name), TypeError);

View File

@@ -33,7 +33,8 @@ describe("evaluation", function () {
it("should bail out on recursive evaluation", function () {
assert.strictEqual(
getPath("function fn(a) { var g = a ? 1 : 2, a = g * this.foo; }").get("body.0.body.body.0.declarations.1.init").evaluate().confident,
getPath("function fn(a) { var g = a ? 1 : 2, a = g * this.foo; }")
.get("body.0.body.body.0.declarations.1.init").evaluate().confident,
false
);
});
@@ -54,7 +55,8 @@ describe("evaluation", function () {
it("should deopt when var is redeclared in the same scope", function () {
assert.strictEqual(
getPath("var x = 2; var y = x + 2; { var x = 3 }").get("body.1.declarations.0.init").evaluate().confident,
getPath("var x = 2; var y = x + 2; { var x = 3 }")
.get("body.1.declarations.0.init").evaluate().confident,
false
);
});
@@ -73,7 +75,8 @@ describe("evaluation", function () {
it("it should not deopt let/const inside blocks", function () {
assert.strictEqual(
getPath("let x = 5; { let x = 1; } let y = x + 5").get("body.2.declarations.0.init").evaluate().value,
getPath("let x = 5; { let x = 1; } let y = x + 5")
.get("body.2.declarations.0.init").evaluate().value,
10
);
const constExample = "const d = true; if (d && true || false) { const d = false; d && 5; }";

View File

@@ -36,7 +36,8 @@ describe("inference", function () {
});
it("it should bail when type changes", function () {
const path = getPath("var x = 1; if (foo) x = null;else x = 3; x === 2").get("body")[2].get("expression");
const path = getPath("var x = 1; if (foo) x = null;else x = 3; x === 2")
.get("body")[2].get("expression");
const left = path.get("left");
const right = path.get("right");
@@ -144,7 +145,8 @@ describe("inference", function () {
it("should infer call return type using async generator function", function () {
const path = getPath("(async function * (): string {})()").get("body")[0].get("expression");
const type = path.getTypeAnnotation();
assert.ok(t.isGenericTypeAnnotation(type) && type.id.name === "AsyncIterator", "should be AsyncIterator");
assert.ok(t.isGenericTypeAnnotation(type) && type.id.name === "AsyncIterator",
"should be AsyncIterator");
});
it("should infer number from x/y", function () {
const path = getPath("x/y").get("body")[0].get("expression");

View File

@@ -17,22 +17,29 @@ function getPath(code) {
describe("scope", function () {
describe("binding paths", function () {
it("function declaration id", function () {
assert.ok(getPath("function foo() {}").scope.getBinding("foo").path.type === "FunctionDeclaration");
assert.ok(getPath("function foo() {}")
.scope.getBinding("foo").path.type === "FunctionDeclaration");
});
it("function expression id", function () {
assert.ok(getPath("(function foo() {})").get("body")[0].get("expression").scope.getBinding("foo").path.type === "FunctionExpression");
assert.ok(getPath("(function foo() {})").get("body")[0].get("expression")
.scope.getBinding("foo").path.type === "FunctionExpression");
});
it("function param", function () {
assert.ok(getPath("(function (foo) {})").get("body")[0].get("expression").scope.getBinding("foo").path.type === "Identifier");
assert.ok(getPath("(function (foo) {})").get("body")[0].get("expression")
.scope.getBinding("foo").path.type === "Identifier");
});
it("variable declaration", function () {
assert.ok(getPath("var foo = null;").scope.getBinding("foo").path.type === "VariableDeclarator");
assert.ok(getPath("var { foo } = null;").scope.getBinding("foo").path.type === "VariableDeclarator");
assert.ok(getPath("var [ foo ] = null;").scope.getBinding("foo").path.type === "VariableDeclarator");
assert.ok(getPath("var { bar: [ foo ] } = null;").scope.getBinding("foo").path.type === "VariableDeclarator");
assert.ok(getPath("var foo = null;")
.scope.getBinding("foo").path.type === "VariableDeclarator");
assert.ok(getPath("var { foo } = null;")
.scope.getBinding("foo").path.type === "VariableDeclarator");
assert.ok(getPath("var [ foo ] = null;")
.scope.getBinding("foo").path.type === "VariableDeclarator");
assert.ok(getPath("var { bar: [ foo ] } = null;")
.scope.getBinding("foo").path.type === "VariableDeclarator");
});
it("purity", function () {

View File

@@ -31,7 +31,8 @@ describe("traverse", function () {
it("traverse", function () {
const expect = [
body[0], body[0].declarations[0], body[0].declarations[0].id, body[0].declarations[0].init,
body[1], body[1].expression, body[1].expression.left, body[1].expression.left.object, body[1].expression.left.property, body[1].expression.right
body[1], body[1].expression, body[1].expression.left, body[1].expression.left.object,
body[1].expression.left.property, body[1].expression.right
];
const actual = [];