Skip to content

Commit ec96635

Browse files
committed
rename shorthand properties correctly
fixes webpack#5027
1 parent 7871b6b commit ec96635

File tree

7 files changed

+104
-1
lines changed

7 files changed

+104
-1
lines changed

lib/optimize/ConcatenatedModule.js

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,49 @@ function reduceSet(a, b) {
8383
return a;
8484
}
8585

86+
function getPathInAst(ast, node) {
87+
if(ast === node) {
88+
return [];
89+
}
90+
const nr = node.range;
91+
var i;
92+
if(Array.isArray(ast)) {
93+
for(i = 0; i < ast.length; i++) {
94+
const enterResult = enterNode(ast[i]);
95+
if(typeof enterResult !== "undefined")
96+
return enterResult;
97+
}
98+
} else if(ast && typeof ast === "object") {
99+
const keys = Object.keys(ast);
100+
for(i = 0; i < keys.length; i++) {
101+
const value = ast[keys[i]];
102+
if(Array.isArray(value)) {
103+
const pathResult = getPathInAst(value, node);
104+
if(typeof pathResult !== "undefined")
105+
return pathResult;
106+
} else if(value && typeof value === "object") {
107+
const enterResult = enterNode(value);
108+
if(typeof enterResult !== "undefined")
109+
return enterResult;
110+
}
111+
}
112+
}
113+
114+
function enterNode(n) {
115+
const r = n.range;
116+
if(r) {
117+
if(r[0] <= nr[0] && r[1] >= nr[1]) {
118+
const path = getPathInAst(n, node);
119+
if(path) {
120+
path.push(n);
121+
return path;
122+
}
123+
}
124+
}
125+
return undefined;
126+
}
127+
}
128+
86129
class ConcatenatedModule extends Module {
87130
constructor(rootModule, modules) {
88131
super();
@@ -190,6 +233,7 @@ class ConcatenatedModule extends Module {
190233
return {
191234
module: m,
192235
index: idx,
236+
ast: undefined,
193237
source: undefined,
194238
globalScope: undefined,
195239
moduleScope: undefined,
@@ -259,6 +303,7 @@ class ConcatenatedModule extends Module {
259303
const globalScope = scopeManager.acquire(ast);
260304
const moduleScope = globalScope.childScopes[0];
261305
const resultSource = new ReplaceSource(source);
306+
info.ast = ast;
262307
info.source = resultSource;
263308
info.globalScope = globalScope;
264309
info.moduleScope = moduleScope;
@@ -298,7 +343,12 @@ class ConcatenatedModule extends Module {
298343
const allIdentifiers = new Set(references.map(r => r.identifier).concat(variable.identifiers));
299344
for(const identifier of allIdentifiers) {
300345
const r = identifier.range;
301-
source.replace(r[0], r[1] - 1, newName);
346+
const path = getPathInAst(info.ast, identifier);
347+
if(path && path.length > 1 && path[1].type === "Property" && path[1].shorthand) {
348+
source.insert(r[1], `: ${newName}`);
349+
} else {
350+
source.replace(r[0], r[1] - 1, newName);
351+
}
302352
}
303353
} else {
304354
allUsedNames.add(name);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export var test = "test1";
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
var [ test ] = [ "test2" ];
2+
export { test }
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
var { test } = { test: "test3" };
2+
export { test }
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
var {o:[{ test }]} = {o:[{ test: "test4" }]};
2+
export { test }
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import m from "./module";
2+
3+
it("should apply shorthand properties correctly when renaming", function() {
4+
m.should.be.eql({
5+
obj: {
6+
test: "test1",
7+
test2: "test2",
8+
test3: "test3",
9+
test4: "test4"
10+
},
11+
nested: {
12+
array: [{
13+
test: "test1",
14+
test2: "test2",
15+
test3: "test3",
16+
test4: "test4"
17+
}]
18+
},
19+
test: "test1",
20+
test2: "test2",
21+
test3: "test3",
22+
test4: "test4",
23+
f: ["test2", "test2", "test3"]
24+
})
25+
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { test } from './file1';
2+
import { test as test2 } from './file2';
3+
import { test as test3 } from './file3';
4+
import { test as test4 } from './file4';
5+
6+
var obj = { test, test2, test3, test4 };
7+
var nested = { array: [ { test, test2, test3, test4 }]};
8+
9+
function f(test = test2, { test2: t2 } = { test2 }, { t3 = test3 } = {}) {
10+
return [test, t2, t3];
11+
}
12+
13+
export default {
14+
obj,
15+
nested,
16+
test,
17+
test2,
18+
test3,
19+
test4,
20+
f: f()
21+
};

0 commit comments

Comments
 (0)