Skip to content

Commit ecafbe4

Browse files
authored
Merge pull request webpack#3852 from webpack/bugfix/import-position
make harmony order stable
2 parents a22b00e + 6ae2b07 commit ecafbe4

11 files changed

Lines changed: 56 additions & 19 deletions

File tree

lib/Parser.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -578,21 +578,21 @@ Parser.prototype.walkExportNamedDeclaration = function walkExportNamedDeclaratio
578578
var pos = this.scope.definitions.length;
579579
this.walkStatement(statement.declaration);
580580
var newDefs = this.scope.definitions.slice(pos);
581-
newDefs.reverse().forEach(function(def) {
582-
this.applyPluginsBailResult("export specifier", statement, def, def);
581+
newDefs.reverse().forEach(function(def, idx) {
582+
this.applyPluginsBailResult("export specifier", statement, def, def, idx);
583583
}, this);
584584
}
585585
}
586586
}
587587
if(statement.specifiers) {
588-
statement.specifiers.forEach(function(specifier) {
588+
statement.specifiers.forEach(function(specifier, idx) {
589589
switch(specifier.type) {
590590
case "ExportSpecifier":
591591
var name = specifier.exported.name;
592592
if(source)
593-
this.applyPluginsBailResult("export import specifier", statement, source, specifier.local.name, name);
593+
this.applyPluginsBailResult("export import specifier", statement, source, specifier.local.name, name, idx);
594594
else
595-
this.applyPluginsBailResult("export specifier", statement, specifier.local.name, name);
595+
this.applyPluginsBailResult("export specifier", statement, specifier.local.name, name, idx);
596596
break;
597597
}
598598
}, this);

lib/compareLocations.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ module.exports = function compareLocations(a, b) {
1717
if(typeof b === "string") {
1818
return -1;
1919
} else if(typeof b === "object") {
20-
if(a.start) a = a.start;
21-
if(b.start) b = b.start;
22-
if(a.line < b.line) return -1;
23-
if(a.line > b.line) return 1;
24-
if(a.column < b.column) return -1;
25-
if(a.column > b.column) return 1;
20+
var aa = a.start ? a.start : a;
21+
var bb = b.start ? b.start : b;
22+
if(aa.line < bb.line) return -1;
23+
if(aa.line > bb.line) return 1;
24+
if(aa.column < bb.column) return -1;
25+
if(aa.column > bb.column) return 1;
26+
if(aa.index < bb.index) return -1;
27+
if(aa.index > bb.index) return 1;
2628
if(a.index < b.index) return -1;
2729
if(a.index > b.index) return 1;
2830
return 0;

lib/dependencies/HarmonyExportDependencyParserPlugin.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ const HarmonyModulesHelpers = require("./HarmonyModulesHelpers");
1515
function makeHarmonyModule(module, loc) {
1616
if(!module.meta.harmonyModule) {
1717
const dep = new HarmonyCompatiblilityDependency(module);
18-
dep.loc = loc;
18+
dep.loc = Object.create(loc);
19+
dep.loc.index = -2;
1920
module.addDependency(dep);
2021
module.meta.harmonyModule = true;
2122
module.strict = true;
@@ -27,27 +28,30 @@ module.exports = class HarmonyExportDependencyParserPlugin {
2728
parser.plugin("export", statement => {
2829
const dep = new HarmonyExportHeaderDependency(statement.declaration && statement.declaration.range, statement.range);
2930
makeHarmonyModule(parser.state.module, statement.loc);
30-
dep.loc = statement.loc;
31+
dep.loc = Object.create(statement.loc);
32+
dep.loc.index = -1;
3133
parser.state.current.addDependency(dep);
3234
return true;
3335
});
3436
parser.plugin("export import", (statement, source) => {
3537
const dep = new HarmonyImportDependency(source, HarmonyModulesHelpers.getNewModuleVar(parser.state, source), statement.range);
3638
makeHarmonyModule(parser.state.module, statement.loc);
37-
dep.loc = statement.loc;
39+
dep.loc = Object.create(statement.loc);
40+
dep.loc.index = -1;
3841
parser.state.current.addDependency(dep);
3942
parser.state.lastHarmoryImport = dep;
4043
return true;
4144
});
4245
parser.plugin("export expression", (statement, expr) => {
4346
const dep = new HarmonyExportExpressionDependency(parser.state.module, expr.range, statement.range);
44-
dep.loc = statement.loc;
47+
dep.loc = Object.create(statement.loc);
48+
dep.loc.index = -1;
4549
parser.state.current.addDependency(dep);
4650
parser.state.module.strict = true;
4751
return true;
4852
});
4953
parser.plugin("export declaration", statement => {});
50-
parser.plugin("export specifier", (statement, id, name) => {
54+
parser.plugin("export specifier", (statement, id, name, idx) => {
5155
const rename = parser.scope.renames[`$${id}`];
5256
let dep;
5357
if(rename === "imported var") {
@@ -58,13 +62,15 @@ module.exports = class HarmonyExportDependencyParserPlugin {
5862
const hoisted = statement.declaration && isHoistedStatement(statement.declaration);
5963
dep = new HarmonyExportSpecifierDependency(parser.state.module, id, name, !immutable || hoisted ? -0.5 : (statement.range[1] + 0.5), immutable);
6064
}
61-
dep.loc = statement.loc;
65+
dep.loc = Object.create(statement.loc);
66+
dep.loc.index = idx;
6267
parser.state.current.addDependency(dep);
6368
return true;
6469
});
65-
parser.plugin("export import specifier", (statement, source, id, name) => {
70+
parser.plugin("export import specifier", (statement, source, id, name, idx) => {
6671
const dep = new HarmonyExportImportedSpecifierDependency(parser.state.module, parser.state.lastHarmoryImport, HarmonyModulesHelpers.getModuleVar(parser.state, source), id, name);
67-
dep.loc = statement.loc;
72+
dep.loc = Object.create(statement.loc);
73+
dep.loc.index = idx;
6874
parser.state.current.addDependency(dep);
6975
return true;
7076
});

test/cases/parsing/issue-3769/a.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const thing = 123;

test/cases/parsing/issue-3769/b.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 123;

test/cases/parsing/issue-3769/c.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 123;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
exports.__esModule = true;
2+
exports.test = 123;

test/cases/parsing/issue-3769/d.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 123;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exports.test = "test";
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
it("should generate valid code", function() {
2+
require("./module").myTest.should.be.eql("test");
3+
});

0 commit comments

Comments
 (0)