Skip to content

Commit

Permalink
Merge pull request #5260 from ljqx/iife-nested-this-properties
Browse files Browse the repository at this point in the history
[Parser] support rename `this` in `.call(this)` inside arbitrary level `.call` for IIFE
  • Loading branch information
sokra committed Jul 11, 2017
2 parents a6c8362 + 12a1352 commit 5a64498
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/Parser.js
Expand Up @@ -266,6 +266,14 @@ class Parser extends Tapable {
return this.applyPluginsBailResult1("evaluate defined Identifier " + name, expr);
}
});
this.plugin("evaluate ThisExpression", function(expr) {
const name = this.scope.renames.$this;
if(name) {
const result = this.applyPluginsBailResult1("evaluate Identifier " + name, expr);
if(result) return result;
return new BasicEvaluatedExpression().setIdentifier(name).setRange(expr.range);
}
});
this.plugin("evaluate MemberExpression", function(expression) {
let expr = expression;
let exprName = [];
Expand Down
11 changes: 11 additions & 0 deletions test/Parser.test.js
Expand Up @@ -180,6 +180,17 @@ describe("Parser", () => {
ijksub: ["test"]
}
],
"renaming this's properties with nested IIFE (called)": [
function() {
! function() {
! function() {
this.sub;
}.call(this);
}.call(ijk);
}, {
ijksub: ["test"]
}
],
};

Object.keys(testCases).forEach((name) => {
Expand Down
10 changes: 10 additions & 0 deletions test/configCases/plugins/provide-plugin/index.js
Expand Up @@ -24,6 +24,16 @@ it("should provide a module for a nested var within a IIFE's this", function() {
}.call(process));
});

it("should provide a module for a nested var within a nested IIFE's this", function() {
(function() {
(function() {
(this.env.NODE_ENV).should.be.eql("development");
var x = this.env.NODE_ENV;
x.should.be.eql("development");
}.call(this));
}.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 5a64498

Please sign in to comment.