Skip to content

Commit

Permalink
Merge pull request webpack#5058 from webpack/bugfix/renaming-shorthand
Browse files Browse the repository at this point in the history
rename shorthand properties correctly
  • Loading branch information
sokra authored Jun 13, 2017
2 parents 7871b6b + 588db26 commit c29e526
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 1 deletion.
52 changes: 51 additions & 1 deletion lib/optimize/ConcatenatedModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,49 @@ function reduceSet(a, b) {
return a;
}

function getPathInAst(ast, node) {
if(ast === node) {
return [];
}
const nr = node.range;
var i;
if(Array.isArray(ast)) {
for(i = 0; i < ast.length; i++) {
const enterResult = enterNode(ast[i]);
if(typeof enterResult !== "undefined")
return enterResult;
}
} else if(ast && typeof ast === "object") {
const keys = Object.keys(ast);
for(i = 0; i < keys.length; i++) {
const value = ast[keys[i]];
if(Array.isArray(value)) {
const pathResult = getPathInAst(value, node);
if(typeof pathResult !== "undefined")
return pathResult;
} else if(value && typeof value === "object") {
const enterResult = enterNode(value);
if(typeof enterResult !== "undefined")
return enterResult;
}
}
}

function enterNode(n) {
const r = n.range;
if(r) {
if(r[0] <= nr[0] && r[1] >= nr[1]) {
const path = getPathInAst(n, node);
if(path) {
path.push(n);
return path;
}
}
}
return undefined;
}
}

class ConcatenatedModule extends Module {
constructor(rootModule, modules) {
super();
Expand Down Expand Up @@ -190,6 +233,7 @@ class ConcatenatedModule extends Module {
return {
module: m,
index: idx,
ast: undefined,
source: undefined,
globalScope: undefined,
moduleScope: undefined,
Expand Down Expand Up @@ -259,6 +303,7 @@ class ConcatenatedModule extends Module {
const globalScope = scopeManager.acquire(ast);
const moduleScope = globalScope.childScopes[0];
const resultSource = new ReplaceSource(source);
info.ast = ast;
info.source = resultSource;
info.globalScope = globalScope;
info.moduleScope = moduleScope;
Expand Down Expand Up @@ -298,7 +343,12 @@ class ConcatenatedModule extends Module {
const allIdentifiers = new Set(references.map(r => r.identifier).concat(variable.identifiers));
for(const identifier of allIdentifiers) {
const r = identifier.range;
source.replace(r[0], r[1] - 1, newName);
const path = getPathInAst(info.ast, identifier);
if(path && path.length > 1 && path[1].type === "Property" && path[1].shorthand) {
source.insert(r[1], `: ${newName}`);
} else {
source.replace(r[0], r[1] - 1, newName);
}
}
} else {
allUsedNames.add(name);
Expand Down
1 change: 1 addition & 0 deletions test/cases/scope-hoisting/renaming-shorthand-5027/file1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export var test = "test1";
2 changes: 2 additions & 0 deletions test/cases/scope-hoisting/renaming-shorthand-5027/file2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
var [ test ] = [ "test2" ];
export { test }
2 changes: 2 additions & 0 deletions test/cases/scope-hoisting/renaming-shorthand-5027/file3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
var { test } = { test: "test3" };
export { test }
2 changes: 2 additions & 0 deletions test/cases/scope-hoisting/renaming-shorthand-5027/file4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
var {o:[{ test }]} = {o:[{ test: "test4" }]};
export { test }
25 changes: 25 additions & 0 deletions test/cases/scope-hoisting/renaming-shorthand-5027/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import m from "./module";

it("should apply shorthand properties correctly when renaming", function() {
m.should.be.eql({
obj: {
test: "test1",
test2: "test2",
test3: "test3",
test4: "test4"
},
nested: {
array: [{
test: "test1",
test2: "test2",
test3: "test3",
test4: "test4"
}]
},
test: "test1",
test2: "test2",
test3: "test3",
test4: "test4",
f: ["test2", "test2", "test3"]
})
});
21 changes: 21 additions & 0 deletions test/cases/scope-hoisting/renaming-shorthand-5027/module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { test } from './file1';
import { test as test2 } from './file2';
import { test as test3 } from './file3';
import { test as test4 } from './file4';

var obj = { test, test2, test3, test4 };
var nested = { array: [ { test, test2, test3, test4 }]};

function f(test = test2, { test2: t2 } = { test2 }, { t3 = test3 } = {}) {
return [test, t2, t3];
}

export default {
obj,
nested,
test,
test2,
test3,
test4,
f: f()
};
12 changes: 12 additions & 0 deletions test/cases/scope-hoisting/renaming-shorthand-5027/test.filter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var supportsES6 = require("../../../helpers/supportsES6");
var supportDefaultAssignment = require("../../../helpers/supportDefaultAssignment");
var supportsObjectDestructuring = require("../../../helpers/supportsObjectDestructuring");
var supportsIteratorDestructuring = require("../../../helpers/supportsIteratorDestructuring");

module.exports = function(config) {
return !config.minimize &&
supportsES6() &&
supportDefaultAssignment() &&
supportsObjectDestructuring() &&
supportsIteratorDestructuring();
};

0 comments on commit c29e526

Please sign in to comment.