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 hoistVariables from "babel-helper-hoist-variables";
import template from "babel-template";
let buildTemplate = template(`
const buildTemplate = template(`
SYSTEM_REGISTER(MODULE_NAME, [SOURCES], function (EXPORT_IDENTIFIER, CONTEXT_IDENTIFIER) {
"use strict";
BEFORE_BODY;
@@ -16,7 +16,7 @@ let buildTemplate = template(`
});
`);
let buildExportAll = template(`
const buildExportAll = template(`
for (var KEY in TARGET) {
if (KEY !== "default" && KEY !== "__esModule") EXPORT_OBJ[KEY] = TARGET[KEY];
}
@@ -26,22 +26,22 @@ let buildExportAll = template(`
const TYPE_IMPORT = "Import";
export default function ({ types: t }) {
let IGNORE_REASSIGNMENT_SYMBOL = Symbol();
const IGNORE_REASSIGNMENT_SYMBOL = Symbol();
let reassignmentVisitor = {
const reassignmentVisitor = {
"AssignmentExpression|UpdateExpression"(path) {
if (path.node[IGNORE_REASSIGNMENT_SYMBOL]) return;
path.node[IGNORE_REASSIGNMENT_SYMBOL] = true;
let arg = path.get(path.isAssignmentExpression() ? "left" : "argument");
const arg = path.get(path.isAssignmentExpression() ? "left" : "argument");
if (!arg.isIdentifier()) return;
let name = arg.node.name;
const name = arg.node.name;
// redeclared in this scope
if (this.scope.getBinding(name) !== path.scope.getBinding(name)) return;
let exportedNames = this.exports[name];
const exportedNames = this.exports[name];
if (!exportedNames) return;
let node = path.node;
@@ -59,7 +59,7 @@ export default function ({ types: t }) {
isPostUpdateExpression = false;
}
for (let exportedName of exportedNames) {
for (const exportedName of exportedNames) {
node = this.buildCall(exportedName, node).expression;
}
@@ -75,7 +75,7 @@ export default function ({ types: t }) {
CallExpression(path, state) {
if (path.node.callee.type === TYPE_IMPORT) {
let contextIdent = state.contextIdent;
const contextIdent = state.contextIdent;
path.replaceWith(t.callExpression(t.memberExpression(contextIdent, t.identifier("import")), path.node.arguments));
}
},
@@ -91,17 +91,17 @@ export default function ({ types: t }) {
state.contextIdent = path.scope.generateUidIdentifier("context");
},
exit(path, state) {
let exportIdent = path.scope.generateUidIdentifier("export");
let contextIdent = state.contextIdent;
const exportIdent = path.scope.generateUidIdentifier("export");
const contextIdent = state.contextIdent;
let exportNames = Object.create(null);
let modules = [];
const exportNames = Object.create(null);
const modules = [];
let beforeBody = [];
let setters = [];
let sources = [];
let variableIds = [];
let removedPaths = [];
const setters = [];
const sources = [];
const variableIds = [];
const removedPaths = [];
function addExportName(key, val) {
exportNames[key] = exportNames[key] || [];
@@ -127,7 +127,7 @@ export default function ({ types: t }) {
);
}
let body: Array<Object> = path.get("body");
const body: Array<Object> = path.get("body");
let canHoist = true;
for (let path of body) {
@@ -138,14 +138,14 @@ export default function ({ types: t }) {
}
}
for (let path of body) {
for (const path of body) {
if (canHoist && path.isFunctionDeclaration()) {
beforeBody.push(path.node);
removedPaths.push(path);
} else if (path.isImportDeclaration()) {
let source = path.node.source.value;
const source = path.node.source.value;
pushModule(source, "imports", path.node.specifiers);
for (let name in path.getBindingIdentifiers()) {
for (const name in path.getBindingIdentifiers()) {
path.scope.removeBinding(name);
variableIds.push(t.identifier(name));
}
@@ -154,10 +154,10 @@ export default function ({ types: t }) {
pushModule(path.node.source.value, "exports", path.node);
path.remove();
} else if (path.isExportDefaultDeclaration()) {
let declar = path.get("declaration");
const declar = path.get("declaration");
if (declar.isClassDeclaration() || declar.isFunctionDeclaration()) {
let id = declar.node.id;
let nodes = [];
const id = declar.node.id;
const nodes = [];
if (id) {
nodes.push(declar.node);
@@ -177,16 +177,16 @@ export default function ({ types: t }) {
path.replaceWith(buildExportCall("default", declar.node));
}
} else if (path.isExportNamedDeclaration()) {
let declar = path.get("declaration");
const declar = path.get("declaration");
if (declar.node) {
path.replaceWith(declar);
let nodes = [];
const nodes = [];
let bindingIdentifiers;
if (path.isFunction()) {
let node = declar.node;
let name = node.id.name;
const node = declar.node;
const name = node.id.name;
if (canHoist) {
addExportName(name, name);
beforeBody.push(node);
@@ -198,21 +198,21 @@ export default function ({ types: t }) {
} else {
bindingIdentifiers = declar.getBindingIdentifiers();
}
for (let name in bindingIdentifiers) {
for (const name in bindingIdentifiers) {
addExportName(name, name);
nodes.push(buildExportCall(name, t.identifier(name)));
}
path.insertAfter(nodes);
} else {
let specifiers = path.node.specifiers;
const specifiers = path.node.specifiers;
if (specifiers && specifiers.length) {
if (path.node.source) {
pushModule(path.node.source.value, "exports", specifiers);
path.remove();
} else {
let nodes = [];
const nodes = [];
for (let specifier of specifiers) {
for (const specifier of specifiers) {
nodes.push(buildExportCall(specifier.exported.name, specifier.local));
addExportName(specifier.local.name, specifier.exported.name);
}
@@ -225,8 +225,8 @@ export default function ({ types: t }) {
}
modules.forEach(function (specifiers) {
let setterBody = [];
let target = path.scope.generateUidIdentifier(specifiers.key);
const setterBody = [];
const target = path.scope.generateUidIdentifier(specifiers.key);
for (let specifier of specifiers.imports) {
if (t.isImportNamespaceSpecifier(specifier)) {
@@ -241,13 +241,13 @@ export default function ({ types: t }) {
}
if (specifiers.exports.length) {
let exportObjRef = path.scope.generateUidIdentifier("exportObj");
const exportObjRef = path.scope.generateUidIdentifier("exportObj");
setterBody.push(t.variableDeclaration("var", [
t.variableDeclarator(exportObjRef, t.objectExpression([]))
]));
for (let node of specifiers.exports) {
for (const node of specifiers.exports) {
if (t.isExportAllDeclaration(node)) {
setterBody.push(buildExportAll({
KEY: path.scope.generateUidIdentifier("key"),
@@ -288,7 +288,7 @@ export default function ({ types: t }) {
scope: path.scope
});
for (let path of removedPaths) {
for (const path of removedPaths) {
path.remove();
}