Replace define assignment with variable declaration

This commit is contained in:
Tim Schaub
2016-12-25 17:46:50 -07:00
parent 91c1857d83
commit b73ab8914a
2 changed files with 27 additions and 22 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "ol", "name": "ol",
"version": "3.21.0-beta.5", "version": "3.21.0-beta.6",
"description": "OpenLayers", "description": "OpenLayers",
"main": "index.js", "main": "index.js",
"module": "index.js", "module": "index.js",
+26 -21
View File
@@ -72,20 +72,23 @@ function getGoogExpressionStatement(identifier) {
}; };
} }
const defineAssignment = { const defineStatement = {
type: 'AssignmentExpression', type: 'ExpressionStatement',
left: { expression: {
type: 'MemberExpression', type: 'AssignmentExpression',
object: { left: {
type: 'Identifier', type: 'MemberExpression',
name: 'ol' object: {
type: 'Identifier',
name: 'ol'
},
property: {
type: 'Identifier'
}
}, },
property: { right: {
type: 'Identifier' type: 'Literal'
} }
},
right: {
type: 'Literal'
} }
}; };
@@ -132,19 +135,21 @@ module.exports = function(info, api) {
const replacements = {}; const replacements = {};
// replace assignments for boolean defines (e.g. ol.FOO = true -> window.OL_FOO = true) // replace assignments for boolean defines (e.g. ol.FOO = true -> window.OL_FOO = true)
root.find(j.AssignmentExpression, defineAssignment) root.find(j.ExpressionStatement, defineStatement)
.filter(path => { .filter(path => {
const node = path.value; const expression = path.value.expression;
const defineName = `${node.left.object.name}.${node.left.property.name}`; const defineName = `${expression.left.object.name}.${expression.left.property.name}`;
return defineName in defineLookup; return defineName in defineLookup;
}) })
.replaceWith(path => { .replaceWith(path => {
const node = path.value; const expression = path.value.expression;
const defineName = `${node.left.object.name}.${node.left.property.name}`; const defineName = `${expression.left.object.name}.${expression.left.property.name}`;
return j.assignmentExpression( const comments = path.value.comments;
'=', const statement = j.variableDeclaration('var', [
j.memberExpression(j.identifier('window'), j.identifier(renameDefine(defineName))), j.variableDeclarator(j.identifier(renameDefine(defineName)), j.literal(expression.right.value))
j.literal(node.right.value)); ]);
statement.comments = comments;
return statement;
}); });
// replace all uses of boolean defines with renamed define // replace all uses of boolean defines with renamed define