Sort and replace provide and requires in one pass

This commit is contained in:
Tim Schaub
2016-12-18 15:14:25 -07:00
parent 329ec70836
commit ea04523bb9

View File

@@ -89,6 +89,8 @@ module.exports = function(info, api) {
// store any initial comments // store any initial comments
const {comments} = root.find(j.Program).get('body', 0).node; const {comments} = root.find(j.Program).get('body', 0).node;
const replacements = {};
// replace goog.provide() // replace goog.provide()
let provide; let provide;
root.find(j.ExpressionStatement, getGoogExpressionStatement('provide')) root.find(j.ExpressionStatement, getGoogExpressionStatement('provide'))
@@ -105,26 +107,17 @@ module.exports = function(info, api) {
if (!provide) { if (!provide) {
throw new Error(`No provide found in ${info.path}`); throw new Error(`No provide found in ${info.path}`);
} }
replacements[provide] = rename(provide);
// replace all uses of provided name with renamed identifier
if (provide.indexOf('.') > 0) {
root.find(j.MemberExpression, getMemberExpression(provide))
.replaceWith(j.identifier(rename(provide)));
} else {
root.find(j.Identifier, {name: provide})
.replaceWith(j.identifier(rename(provide)));
}
// replace goog.require() // replace goog.require()
const requires = {};
root.find(j.ExpressionStatement, getGoogExpressionStatement('require')) root.find(j.ExpressionStatement, getGoogExpressionStatement('require'))
.replaceWith(path => { .replaceWith(path => {
const name = path.value.expression.arguments[0].value; const name = path.value.expression.arguments[0].value;
if (name in requires) { if (name in replacements) {
throw new Error(`Duplicate require found in ${info.path}: ${name}`); throw new Error(`Duplicate require found in ${info.path}: ${name}`);
} }
const renamed = rename(name); const renamed = rename(name);
requires[name] = renamed; replacements[name] = renamed;
return j.variableDeclaration('var', [ return j.variableDeclaration('var', [
j.variableDeclarator(j.identifier(renamed), j.callExpression( j.variableDeclarator(j.identifier(renamed), j.callExpression(
j.identifier('require'), [j.literal(resolve(provide, name))] j.identifier('require'), [j.literal(resolve(provide, name))]
@@ -132,14 +125,14 @@ module.exports = function(info, api) {
]); ]);
}); });
// replace all uses of required names with renamed identifiers // replace all uses of required or provided names with renamed identifiers
Object.keys(requires).sort().reverse().forEach(name => { Object.keys(replacements).sort().reverse().forEach(name => {
if (name.indexOf('.') > 0) { if (name.indexOf('.') > 0) {
root.find(j.MemberExpression, getMemberExpression(name)) root.find(j.MemberExpression, getMemberExpression(name))
.replaceWith(j.identifier(requires[name])); .replaceWith(j.identifier(replacements[name]));
} else { } else {
root.find(j.Identifier, {name: name}) root.find(j.Identifier, {name: name})
.replaceWith(j.identifier(requires[name])); .replaceWith(j.identifier(replacements[name]));
} }
}); });