enable prefer const (#5113)

This commit is contained in:
Henry Zhu
2017-01-14 09:48:52 -05:00
committed by GitHub
parent 982850731e
commit 672adba9a1
177 changed files with 1862 additions and 1863 deletions

View File

@@ -3,7 +3,7 @@ import nameFunction from "babel-helper-function-name";
import template from "babel-template";
export default function ({ types: t }) {
let findBareSupers = {
const findBareSupers = {
Super(path) {
if (path.parentPath.isCallExpression({ callee: path.node })) {
this.push(path.parentPath);
@@ -11,7 +11,7 @@ export default function ({ types: t }) {
}
};
let referenceVisitor = {
const referenceVisitor = {
ReferencedIdentifier(path) {
if (this.scope.hasOwnBinding(path.node.name)) {
this.collision = true;
@@ -45,12 +45,12 @@ export default function ({ types: t }) {
visitor: {
Class(path, state) {
const buildClassProperty = state.opts.spec ? buildClassPropertySpec : buildClassPropertyNonSpec;
let isDerived = !!path.node.superClass;
const isDerived = !!path.node.superClass;
let constructor;
let props = [];
let body = path.get("body");
const props = [];
const body = path.get("body");
for (let path of body.get("body")) {
for (const path of body.get("body")) {
if (path.isClassProperty()) {
props.push(path);
} else if (path.isClassMethod({ kind: "constructor" })) {
@@ -60,7 +60,7 @@ export default function ({ types: t }) {
if (!props.length) return;
let nodes = [];
const nodes = [];
let ref;
if (path.isClassExpression() || !path.node.id) {
@@ -72,15 +72,15 @@ export default function ({ types: t }) {
let instanceBody = [];
for (let prop of props) {
let propNode = prop.node;
for (const prop of props) {
const propNode = prop.node;
if (propNode.decorators && propNode.decorators.length > 0) continue;
// In non-spec mode, all properties without values are ignored.
// In spec mode, *static* properties without values are still defined (see below).
if (!state.opts.spec && !propNode.value) continue;
let isStatic = propNode.static;
const isStatic = propNode.static;
if (isStatic) {
nodes.push(buildClassProperty(ref, propNode));
@@ -92,7 +92,7 @@ export default function ({ types: t }) {
if (instanceBody.length) {
if (!constructor) {
let newConstructor = t.classMethod("constructor", t.identifier("constructor"), [], t.blockStatement([]));
const newConstructor = t.classMethod("constructor", t.identifier("constructor"), [], t.blockStatement([]));
if (isDerived) {
newConstructor.params = [t.restElement(t.identifier("args"))];
newConstructor.body.body.push(
@@ -107,18 +107,18 @@ export default function ({ types: t }) {
[constructor] = body.unshiftContainer("body", newConstructor);
}
let collisionState = {
const collisionState = {
collision: false,
scope: constructor.scope
};
for (let prop of props) {
for (const prop of props) {
prop.traverse(referenceVisitor, collisionState);
if (collisionState.collision) break;
}
if (collisionState.collision) {
let initialisePropsRef = path.scope.generateUidIdentifier("initialiseProps");
const initialisePropsRef = path.scope.generateUidIdentifier("initialiseProps");
nodes.push(t.variableDeclaration("var", [
t.variableDeclarator(
@@ -137,9 +137,9 @@ export default function ({ types: t }) {
//
if (isDerived) {
let bareSupers = [];
const bareSupers = [];
constructor.traverse(findBareSupers, bareSupers);
for (let bareSuper of bareSupers) {
for (const bareSuper of bareSupers) {
bareSuper.insertAfter(instanceBody);
}
} else {
@@ -147,7 +147,7 @@ export default function ({ types: t }) {
}
}
for (let prop of props) {
for (const prop of props) {
prop.remove();
}
@@ -169,11 +169,11 @@ export default function ({ types: t }) {
path.insertAfter(nodes);
},
ArrowFunctionExpression(path) {
let classExp = path.get("body");
const classExp = path.get("body");
if (!classExp.isClassExpression()) return;
let body = classExp.get("body");
let members = body.get("body");
const body = classExp.get("body");
const members = body.get("body");
if (members.some((member) => member.isClassProperty())) {
path.ensureBlock();
}