enable prefer const (#5113)
This commit is contained in:
@@ -7,7 +7,7 @@ export default function ({ types: t }) {
|
||||
*/
|
||||
|
||||
function variableDeclarationHasPattern(node) {
|
||||
for (let declar of (node.declarations: Array)) {
|
||||
for (const declar of (node.declarations: Array)) {
|
||||
if (t.isPattern(declar.id)) {
|
||||
return true;
|
||||
}
|
||||
@@ -20,7 +20,7 @@ export default function ({ types: t }) {
|
||||
*/
|
||||
|
||||
function hasRest(pattern) {
|
||||
for (let elem of (pattern.elements: Array)) {
|
||||
for (const elem of (pattern.elements: Array)) {
|
||||
if (t.isRestElement(elem)) {
|
||||
return true;
|
||||
}
|
||||
@@ -28,7 +28,7 @@ export default function ({ types: t }) {
|
||||
return false;
|
||||
}
|
||||
|
||||
let arrayUnpackVisitor = {
|
||||
const arrayUnpackVisitor = {
|
||||
ReferencedIdentifier(path, state) {
|
||||
if (state.bindings[path.node.name]) {
|
||||
state.deopt = true;
|
||||
@@ -68,7 +68,7 @@ export default function ({ types: t }) {
|
||||
}
|
||||
|
||||
buildVariableDeclaration(id, init) {
|
||||
let declar = t.variableDeclaration("var", [
|
||||
const declar = t.variableDeclaration("var", [
|
||||
t.variableDeclarator(id, init)
|
||||
]);
|
||||
declar._blockHoist = this.blockHoist;
|
||||
@@ -99,9 +99,9 @@ export default function ({ types: t }) {
|
||||
// we need to assign the current value of the assignment to avoid evaluating
|
||||
// it more than once
|
||||
|
||||
let tempValueRef = this.scope.generateUidIdentifierBasedOnNode(valueRef);
|
||||
const tempValueRef = this.scope.generateUidIdentifierBasedOnNode(valueRef);
|
||||
|
||||
let declar = t.variableDeclaration("var", [
|
||||
const declar = t.variableDeclaration("var", [
|
||||
t.variableDeclarator(tempValueRef, valueRef)
|
||||
]);
|
||||
declar._blockHoist = this.blockHoist;
|
||||
@@ -109,15 +109,15 @@ export default function ({ types: t }) {
|
||||
|
||||
//
|
||||
|
||||
let tempConditional = t.conditionalExpression(
|
||||
const tempConditional = t.conditionalExpression(
|
||||
t.binaryExpression("===", tempValueRef, t.identifier("undefined")),
|
||||
pattern.right,
|
||||
tempValueRef
|
||||
);
|
||||
|
||||
let left = pattern.left;
|
||||
const left = pattern.left;
|
||||
if (t.isPattern(left)) {
|
||||
let tempValueDefault = t.expressionStatement(
|
||||
const tempValueDefault = t.expressionStatement(
|
||||
t.assignmentExpression("=", tempValueRef, tempConditional)
|
||||
);
|
||||
tempValueDefault._blockHoist = this.blockHoist;
|
||||
@@ -135,7 +135,7 @@ export default function ({ types: t }) {
|
||||
let keys = [];
|
||||
|
||||
for (let i = 0; i < pattern.properties.length; i++) {
|
||||
let prop = pattern.properties[i];
|
||||
const prop = pattern.properties[i];
|
||||
|
||||
// we've exceeded the index of the spread property to all properties to the
|
||||
// right need to be ignored
|
||||
@@ -153,15 +153,15 @@ export default function ({ types: t }) {
|
||||
|
||||
//
|
||||
|
||||
let value = t.callExpression(this.file.addHelper("objectWithoutProperties"), [objRef, keys]);
|
||||
const value = t.callExpression(this.file.addHelper("objectWithoutProperties"), [objRef, keys]);
|
||||
this.nodes.push(this.buildVariableAssignment(spreadProp.argument, value));
|
||||
}
|
||||
|
||||
pushObjectProperty(prop, propRef) {
|
||||
if (t.isLiteral(prop.key)) prop.computed = true;
|
||||
|
||||
let pattern = prop.value;
|
||||
let objRef = t.memberExpression(propRef, prop.key, prop.computed);
|
||||
const pattern = prop.value;
|
||||
const objRef = t.memberExpression(propRef, prop.key, prop.computed);
|
||||
|
||||
if (t.isPattern(pattern)) {
|
||||
this.push(pattern, objRef);
|
||||
@@ -184,7 +184,7 @@ export default function ({ types: t }) {
|
||||
// only evaluated once
|
||||
|
||||
if (pattern.properties.length > 1 && !this.scope.isStatic(objRef)) {
|
||||
let temp = this.scope.generateUidIdentifierBasedOnNode(objRef);
|
||||
const temp = this.scope.generateUidIdentifierBasedOnNode(objRef);
|
||||
this.nodes.push(this.buildVariableDeclaration(temp, objRef));
|
||||
objRef = temp;
|
||||
}
|
||||
@@ -192,7 +192,7 @@ export default function ({ types: t }) {
|
||||
//
|
||||
|
||||
for (let i = 0; i < pattern.properties.length; i++) {
|
||||
let prop = pattern.properties[i];
|
||||
const prop = pattern.properties[i];
|
||||
if (t.isRestProperty(prop)) {
|
||||
this.pushObjectRest(pattern, objRef, prop, i);
|
||||
} else {
|
||||
@@ -210,7 +210,7 @@ export default function ({ types: t }) {
|
||||
if (pattern.elements.length > arr.elements.length) return;
|
||||
if (pattern.elements.length < arr.elements.length && !hasRest(pattern)) return false;
|
||||
|
||||
for (let elem of (pattern.elements: Array)) {
|
||||
for (const elem of (pattern.elements: Array)) {
|
||||
// deopt on holes
|
||||
if (!elem) return false;
|
||||
|
||||
@@ -218,7 +218,7 @@ export default function ({ types: t }) {
|
||||
if (t.isMemberExpression(elem)) return false;
|
||||
}
|
||||
|
||||
for (let elem of (arr.elements: Array)) {
|
||||
for (const elem of (arr.elements: Array)) {
|
||||
// deopt on spread elements
|
||||
if (t.isSpreadElement(elem)) return false;
|
||||
|
||||
@@ -230,15 +230,15 @@ export default function ({ types: t }) {
|
||||
}
|
||||
|
||||
// deopt on reference to left side identifiers
|
||||
let bindings = t.getBindingIdentifiers(pattern);
|
||||
let state = { deopt: false, bindings };
|
||||
const bindings = t.getBindingIdentifiers(pattern);
|
||||
const state = { deopt: false, bindings };
|
||||
this.scope.traverse(arr, arrayUnpackVisitor, state);
|
||||
return !state.deopt;
|
||||
}
|
||||
|
||||
pushUnpackedArrayPattern(pattern, arr) {
|
||||
for (let i = 0; i < pattern.elements.length; i++) {
|
||||
let elem = pattern.elements[i];
|
||||
const elem = pattern.elements[i];
|
||||
if (t.isRestElement(elem)) {
|
||||
this.push(elem.argument, t.arrayExpression(arr.elements.slice(i)));
|
||||
} else {
|
||||
@@ -264,13 +264,13 @@ export default function ({ types: t }) {
|
||||
// if we have a rest then we need all the elements so don't tell
|
||||
// `scope.toArray` to only get a certain amount
|
||||
|
||||
let count = !hasRest(pattern) && pattern.elements.length;
|
||||
const count = !hasRest(pattern) && pattern.elements.length;
|
||||
|
||||
// so we need to ensure that the `arrayRef` is an array, `scope.toArray` will
|
||||
// return a locally bound identifier if it's been inferred to be an array,
|
||||
// otherwise it'll be a call to a helper that will ensure it's one
|
||||
|
||||
let toArray = this.toArray(arrayRef, count);
|
||||
const toArray = this.toArray(arrayRef, count);
|
||||
|
||||
if (t.isIdentifier(toArray)) {
|
||||
// we've been given an identifier so it must have been inferred to be an
|
||||
@@ -315,7 +315,7 @@ export default function ({ types: t }) {
|
||||
// need to save it to a variable
|
||||
|
||||
if (!t.isArrayExpression(ref) && !t.isMemberExpression(ref)) {
|
||||
let memo = this.scope.maybeGenerateMemoised(ref, true);
|
||||
const memo = this.scope.maybeGenerateMemoised(ref, true);
|
||||
if (memo) {
|
||||
this.nodes.push(this.buildVariableDeclaration(memo, ref));
|
||||
ref = memo;
|
||||
@@ -334,14 +334,14 @@ export default function ({ types: t }) {
|
||||
return {
|
||||
visitor: {
|
||||
ExportNamedDeclaration(path) {
|
||||
let declaration = path.get("declaration");
|
||||
const declaration = path.get("declaration");
|
||||
if (!declaration.isVariableDeclaration()) return;
|
||||
if (!variableDeclarationHasPattern(declaration.node)) return;
|
||||
|
||||
let specifiers = [];
|
||||
const specifiers = [];
|
||||
|
||||
for (let name in path.getOuterBindingIdentifiers(path)) {
|
||||
let id = t.identifier(name);
|
||||
for (const name in path.getOuterBindingIdentifiers(path)) {
|
||||
const id = t.identifier(name);
|
||||
specifiers.push(t.exportSpecifier(id, id));
|
||||
}
|
||||
|
||||
@@ -353,13 +353,13 @@ export default function ({ types: t }) {
|
||||
},
|
||||
|
||||
ForXStatement(path, file) {
|
||||
let { node, scope } = path;
|
||||
let left = node.left;
|
||||
const { node, scope } = path;
|
||||
const left = node.left;
|
||||
|
||||
if (t.isPattern(left)) {
|
||||
// for ({ length: k } in { abc: 3 });
|
||||
|
||||
let temp = scope.generateUidIdentifier("ref");
|
||||
const temp = scope.generateUidIdentifier("ref");
|
||||
|
||||
node.left = t.variableDeclaration("var", [
|
||||
t.variableDeclarator(temp)
|
||||
@@ -376,17 +376,17 @@ export default function ({ types: t }) {
|
||||
|
||||
if (!t.isVariableDeclaration(left)) return;
|
||||
|
||||
let pattern = left.declarations[0].id;
|
||||
const pattern = left.declarations[0].id;
|
||||
if (!t.isPattern(pattern)) return;
|
||||
|
||||
let key = scope.generateUidIdentifier("ref");
|
||||
const key = scope.generateUidIdentifier("ref");
|
||||
node.left = t.variableDeclaration(left.kind, [
|
||||
t.variableDeclarator(key, null)
|
||||
]);
|
||||
|
||||
let nodes = [];
|
||||
const nodes = [];
|
||||
|
||||
let destructuring = new DestructuringTransformer({
|
||||
const destructuring = new DestructuringTransformer({
|
||||
kind: left.kind,
|
||||
file: file,
|
||||
scope: scope,
|
||||
@@ -397,20 +397,20 @@ export default function ({ types: t }) {
|
||||
|
||||
path.ensureBlock();
|
||||
|
||||
let block = node.body;
|
||||
const block = node.body;
|
||||
block.body = nodes.concat(block.body);
|
||||
},
|
||||
|
||||
CatchClause({ node, scope }, file) {
|
||||
let pattern = node.param;
|
||||
const pattern = node.param;
|
||||
if (!t.isPattern(pattern)) return;
|
||||
|
||||
let ref = scope.generateUidIdentifier("ref");
|
||||
const ref = scope.generateUidIdentifier("ref");
|
||||
node.param = ref;
|
||||
|
||||
let nodes = [];
|
||||
const nodes = [];
|
||||
|
||||
let destructuring = new DestructuringTransformer({
|
||||
const destructuring = new DestructuringTransformer({
|
||||
kind: "let",
|
||||
file: file,
|
||||
scope: scope,
|
||||
@@ -422,12 +422,12 @@ export default function ({ types: t }) {
|
||||
},
|
||||
|
||||
AssignmentExpression(path, file) {
|
||||
let { node, scope } = path;
|
||||
const { node, scope } = path;
|
||||
if (!t.isPattern(node.left)) return;
|
||||
|
||||
let nodes = [];
|
||||
const nodes = [];
|
||||
|
||||
let destructuring = new DestructuringTransformer({
|
||||
const destructuring = new DestructuringTransformer({
|
||||
operator: node.operator,
|
||||
file: file,
|
||||
scope: scope,
|
||||
@@ -457,21 +457,21 @@ export default function ({ types: t }) {
|
||||
},
|
||||
|
||||
VariableDeclaration(path, file) {
|
||||
let { node, scope, parent } = path;
|
||||
const { node, scope, parent } = path;
|
||||
if (t.isForXStatement(parent)) return;
|
||||
if (!parent || !path.container) return; // i don't know why this is necessary - TODO
|
||||
if (!variableDeclarationHasPattern(node)) return;
|
||||
|
||||
let nodes = [];
|
||||
const nodes = [];
|
||||
let declar;
|
||||
|
||||
for (let i = 0; i < node.declarations.length; i++) {
|
||||
declar = node.declarations[i];
|
||||
|
||||
let patternId = declar.init;
|
||||
let pattern = declar.id;
|
||||
const patternId = declar.init;
|
||||
const pattern = declar.id;
|
||||
|
||||
let destructuring = new DestructuringTransformer({
|
||||
const destructuring = new DestructuringTransformer({
|
||||
blockHoist: node._blockHoist,
|
||||
nodes: nodes,
|
||||
scope: scope,
|
||||
|
||||
Reference in New Issue
Block a user