fixed and test edge cases with this scope in harmony imports

This commit is contained in:
Tobias Koppers 2016-02-13 10:00:47 +01:00
parent 84eb9a2741
commit 1cf2cb5fef
7 changed files with 78 additions and 9 deletions

View File

@ -32,6 +32,16 @@ module.exports = AbstractPlugin.create({
this.state.current.addDependency(dep);
return true;
},
"call imported var": function(expr) {
expr = expr.callee;
var name = expr.name;
var settings = this.state.harmonySpecifier["$" + name];
var dep = new HarmonyImportSpecifierDependency(settings[0], settings[1], settings[2], name, expr.range);
dep.call = true;
dep.loc = expr.loc;
this.state.current.addDependency(dep);
return true;
},
"hot accept callback": function(expr, requests) {
var dependencies = requests.filter(function(request) {
return HarmonyModulesHelpers.checkModuleVar(this.state, request);

View File

@ -38,12 +38,15 @@ HarmonyImportSpecifierDependency.Template = function HarmonyImportSpecifierDepen
HarmonyImportSpecifierDependency.Template.prototype.apply = function(dep, source) {
var content;
if(dep.id === "default" && !(dep.importDependency.module.meta && dep.importDependency.module.meta.harmonyModule)) {
content = "(/* harmony import */0," + dep.importedVar + "_default.a)";
content = "/* harmony import */" + dep.importedVar + "_default.a";
} else if(dep.id) {
var used = dep.importDependency.module.isUsed(dep.id);
content = "(/* harmony import */0," + dep.importedVar + "[" + JSON.stringify(used) + "])";
content = "/* harmony import */" + dep.importedVar + "[" + JSON.stringify(used) + "]";
} else {
content = "/* harmony namespace import */ " + dep.importedVar;
}
if(dep.call) {
content = "" + content + ".bind(undefined)";
}
source.replace(dep.range[0], dep.range[1] - 1, content);
};

View File

@ -1,5 +1,3 @@
"use strict";
function returnThis() {
return this;
}

View File

@ -1,12 +1,43 @@
"use strict";
import sd, {sa, sb as SB} from "./strict-abc";
import * as sabc from "./strict-abc";
import d, {a, b as B} from "./abc";
import * as abc from "./abc";
it("should have this undefined on imported functions", function() {
(typeof d()).should.be.eql("undefined");
(typeof a()).should.be.eql("undefined");
(typeof B()).should.be.eql("undefined");
abc.a().should.be.eql(abc);
function x() { throw new Error("should not be executed"); }
it("should have this = undefined on imported strict functions", function() {
(typeof sd()).should.be.eql("undefined");
(typeof sa()).should.be.eql("undefined");
(typeof SB()).should.be.eql("undefined");
x
sabc.sa().should.be.eql(sabc);
});
it("should have this = global on imported non-strict functions", function() {
x
d().should.be.eql(global);
x
a().should.be.eql(global);
x
B().should.be.eql(global);
x
abc.a().should.be.eql(abc);
x
});
import C2, { C } from "./new";
import * as New from "./new";
it("should be possible to use new correctly", function() {
x
new C().should.be.eql({ok: true});
x
new C2().should.be.eql({ok: true});
x
new New.C().should.be.eql({ok: true});
});

View File

@ -0,0 +1,10 @@
function C() {
this.ok = this.pok;
}
C.prototype.pok = true;
export default C;
export {
C
};

View File

@ -0,0 +1,15 @@
"use strict";
function returnThis() {
return this;
}
var sa = returnThis;
var sb = returnThis;
export {
sa,
sb
}
export default returnThis;

View File

@ -82,7 +82,9 @@ it("should be able to import commonjs", function() {
// next line doesn't end with semicolon
x
Thing.should.have.type("function");
x
Thing().should.be.eql("thing");
x
Other.should.be.eql("other");
Thing2.should.have.type("function");