Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
ljqx authored and Unknown committed Jun 23, 2017
1 parent 9bff9ac commit cc2df2f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
16 changes: 9 additions & 7 deletions lib/Parser.js
Expand Up @@ -1069,20 +1069,20 @@ class Parser extends Tapable {
this.walkExpression(argOrThis);
}
const params = functionExpression.params;
const args = options.map(renameArgOrThis, this);
const renameThis = currentThis ? renameArgOrThis.call(this, currentThis) : null;
const args = options.map(renameArgOrThis, this);
this.inScope(params.filter(function(identifier, idx) {
return !args[idx];
}), function() {
if(renameThis) {
this.scope.renames.$this = renameThis;
}
for(let i = 0; i < args.length; i++) {
const param = args[i];
if(!param) continue;
if(!params[i] || params[i].type !== "Identifier") continue;
this.scope.renames["$" + params[i].name] = param;
}
if(renameThis) {
this.scope.renames.$this = renameThis;
}
if(functionExpression.body.type === "BlockStatement") {
this.prewalkStatement(functionExpression.body);
this.walkStatement(functionExpression.body);
Expand Down Expand Up @@ -1140,10 +1140,10 @@ class Parser extends Tapable {
}
if((expr.type === "Identifier" && this.scope.definitions.indexOf(expr.name) === -1) ||
(expr.type === "ThisExpression" && this.scope.renames.$this)) {
if(expr.type === "Identifier") {
exprName.unshift(this.scope.renames["$" + expr.name] || expr.name);
} else {
if(expr.type === "ThisExpression") {
exprName.unshift(this.scope.renames.$this);
} else {
exprName.unshift(this.scope.renames["$" + expr.name] || expr.name);
}
let result = this.applyPluginsBailResult1("expression " + exprName.join("."), expression);
if(result === true)
Expand Down Expand Up @@ -1175,6 +1175,8 @@ class Parser extends Tapable {
renames: Object.create(oldScope.renames)
};

this.scope.renames.$this = undefined;

for(let paramIndex = 0, len = params.length; paramIndex < len; paramIndex++) {
const param = params[paramIndex];

Expand Down
17 changes: 16 additions & 1 deletion test/Parser.test.js
Expand Up @@ -161,7 +161,7 @@ describe("Parser", () => {
abc: ["test"]
}
],
"renaming with IIFE (called)": [
"renaming arguments with IIFE (called)": [
function() {
! function(xyz) {
xyz("test");
Expand All @@ -171,6 +171,15 @@ describe("Parser", () => {
fgh: [""]
}
],
"renaming this's properties with IIFE (called)": [
function() {
! function() {
this.sub;
}.call(ijk);
}, {
ijksub: ["test"]
}
],
};

Object.keys(testCases).forEach((name) => {
Expand All @@ -181,6 +190,7 @@ describe("Parser", () => {

const testParser = new Parser({});
testParser.plugin("can-rename abc", (expr) => true);
testParser.plugin("can-rename ijk", (expr) => true);
testParser.plugin("call abc", (expr) => {
if(!testParser.state.abc) testParser.state.abc = [];
testParser.state.abc.push(testParser.parseString(expr.arguments[0]));
Expand All @@ -206,6 +216,11 @@ describe("Parser", () => {
testParser.state.fghsub.push(testParser.scope.inTry ? "try" : "notry");
return true;
});
testParser.plugin("expression ijk.sub", (expr) => {
if(!testParser.state.ijksub) testParser.state.ijksub = [];
testParser.state.ijksub.push("test");
return true;
});
testParser.plugin("expression memberExpr", (expr) => {
if(!testParser.state.expressions) testParser.state.expressions = [];
testParser.state.expressions.push(expr.name);
Expand Down
10 changes: 9 additions & 1 deletion test/configCases/plugins/provide-plugin/index.js
Expand Up @@ -8,14 +8,22 @@ it("should provide a module for a nested var", function() {
x.should.be.eql("bbbccc");
});

it("should provide a module for a nested var within a IIFE", function() {
it("should provide a module for a nested var within a IIFE's argument", function() {
(function(process) {
(process.env.NODE_ENV).should.be.eql("development");
var x = process.env.NODE_ENV;
x.should.be.eql("development");
}(process));
});

it("should provide a module for a nested var within a IIFE's this", function() {
(function() {
(this.env.NODE_ENV).should.be.eql("development");
var x = this.env.NODE_ENV;
x.should.be.eql("development");
}.call(process));
});

it("should not provide a module for a part of a var", function() {
(typeof bbb).should.be.eql("undefined");
});
Expand Down

0 comments on commit cc2df2f

Please sign in to comment.