Skip to content

Commit 23d28dd

Browse files
committed
evaluate stuff for renaming to capture more cases
fixes webpack#208
1 parent a8f80e6 commit 23d28dd

2 files changed

Lines changed: 27 additions & 20 deletions

File tree

lib/Parser.js

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,17 @@ Parser.prototype.initializeEvaluating = function() {
4040
if(expr.operator == "&&") {
4141
var left = this.evaluateExpression(expr.left);
4242
var leftAsBool = left && left.asBool();
43-
if(leftAsBool === false) return new BasicEvaluatedExpression().setBoolean(false).setRange(expr.range);
43+
if(leftAsBool === false) return left.setRange(expr.range);
4444
if(leftAsBool !== true) return;
4545
var right = this.evaluateExpression(expr.right);
46-
var rightAsBool = right && right.asBool();
47-
if(typeof rightAsBool === "boolean")
48-
return new BasicEvaluatedExpression().setBoolean(rightAsBool).setRange(expr.range);
46+
return right.setRange(expr.range);
4947
} else if(expr.operator == "||") {
5048
var left = this.evaluateExpression(expr.left);
5149
var leftAsBool = left && left.asBool();
52-
if(leftAsBool === true) return new BasicEvaluatedExpression().setBoolean(true).setRange(expr.range);
50+
if(leftAsBool === true) return left.setRange(expr.range);
5351
if(leftAsBool !== false) return;
5452
var right = this.evaluateExpression(expr.right);
55-
var rightAsBool = right && right.asBool();
56-
if(typeof rightAsBool === "boolean")
57-
return new BasicEvaluatedExpression().setBoolean(rightAsBool).setRange(expr.range);
53+
return right.setRange(expr.range);
5854
}
5955
});
6056
this.plugin("evaluate BinaryExpression", function(expr) {
@@ -330,6 +326,14 @@ Parser.prototype.initializeEvaluating = function() {
330326
return new BasicEvaluatedExpression().setItems(items).setRange(expr.range);
331327
});
332328
}
329+
330+
Parser.prototype.getRenameIdentifier = function getRenameIdentifier(expr) {
331+
var result = this.evaluateExpression(expr);
332+
if(!result) return;
333+
if(result.isIdentifier()) return result.identifier;
334+
return;
335+
};
336+
333337
Parser.prototype.walkStatements = function walkStatements(statements) {
334338
statements.forEach(function(statement) {
335339
this.walkStatement(statement);
@@ -458,10 +462,10 @@ Parser.prototype.walkVariableDeclarators = function walkVariableDeclarators(decl
458462
declarators.forEach(function(declarator) {
459463
switch(declarator.type) {
460464
case "VariableDeclarator":
461-
var initToIdentifier = declarator.init && declarator.init.type === "Identifier";
462-
if(initToIdentifier && declarator.id.type === "Identifier" && !this.applyPluginsBailResult("rename " + declarator.init.name, declarator.init)) {
465+
var renameIdentifier = declarator.init && this.getRenameIdentifier(declarator.init);
466+
if(renameIdentifier && declarator.id.type === "Identifier" && !this.applyPluginsBailResult("rename " + renameIdentifier, declarator.init)) {
463467
// renaming with "var a = b;"
464-
this.scope.renames["$"+declarator.id.name] = this.scope.renames["$"+declarator.init.name] || declarator.init.name;
468+
this.scope.renames["$"+declarator.id.name] = this.scope.renames["$"+renameIdentifier] || renameIdentifier;
465469
var idx = this.scope.definitions.indexOf(declarator.id.name);
466470
if(idx >= 0) this.scope.definitions.splice(idx, 1);
467471
} else if(declarator.id.type === "Identifier" && !this.applyPluginsBailResult("var " + declarator.id.name, declarator)) {
@@ -536,9 +540,10 @@ Parser.prototype.walkExpression = function walkExpression(expression) {
536540
this.walkExpression(expression.right);
537541
break;
538542
case "AssignmentExpression":
539-
if(expression.left.type === "Identifier" && expression.right.type === "Identifier" && !this.applyPluginsBailResult("rename " + expression.right.name, expression.right)) {
543+
var renameIdentifier = this.getRenameIdentifier(expression.right);
544+
if(expression.left.type === "Identifier" && renameIdentifier && !this.applyPluginsBailResult("rename " + renameIdentifier, expression.right)) {
540545
// renaming "a = b;"
541-
this.scope.renames["$"+expression.left.name] = expression.right.name;
546+
this.scope.renames["$"+expression.left.name] = renameIdentifier;
542547
var idx = this.scope.definitions.indexOf(expression.left.name);
543548
if(idx >= 0) this.scope.definitions.splice(idx, 1);
544549
} else if(expression.left.type === "Identifier") {
@@ -578,15 +583,14 @@ Parser.prototype.walkExpression = function walkExpression(expression) {
578583
function walkIIFE(functionExpression, args) {
579584
var params = functionExpression.params;
580585
var args = args.map(function(arg, idx) {
581-
var result = this.evaluateExpression(arg);
582-
if(result && !result.isIdentifier()) result = undefined;
583-
if(!result) {
586+
var renameIdentifier = this.getRenameIdentifier(arg);
587+
if(!renameIdentifier) {
584588
this.walkExpression(arg);
585589
return;
586-
} else if(this.applyPluginsBailResult("rename " + result.identifier, arg)) {
590+
} else if(this.applyPluginsBailResult("rename " + renameIdentifier, arg)) {
587591
return;
588592
}
589-
return result.identifier;
593+
return renameIdentifier;
590594
}, this);
591595
this.inScope(params.filter(function(identifier, idx) {
592596
return !args[idx];

test/cases/parsing/renaming/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
it("should be able to rename require by var", function() {
22
var cjsRequire; // just to make it difficult
3-
var cjsRequire = require;
3+
var cjsRequire = require, cjsRequire2 = typeof require !== "undefined" && require;
44
cjsRequire("./file").should.be.eql("ok");
5+
cjsRequire2("./file").should.be.eql("ok");
56
});
67

78
it("should be able to rename require by assign", function() {
8-
var cjsRequire;
9+
var cjsRequire, cjsRequire2;
910
(function() {
1011
cjsRequire = require;
12+
cjsRequire2 = typeof require === "function" && require;
1113
cjsRequire("./file").should.be.eql("ok");
14+
cjsRequire2("./file").should.be.eql("ok");
1215
}());
1316
});
1417

0 commit comments

Comments
 (0)