Merge pull request #7145 from tschaub/spaceless

Spaceless provides
This commit is contained in:
Tim Schaub
2017-08-17 12:07:06 -04:00
committed by GitHub
2 changed files with 55 additions and 52 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
goog.provide('layer clipping'); goog.provide('ol.test.rendering.layer.Clipping');
goog.require('ol.Map'); goog.require('ol.Map');
goog.require('ol.View'); goog.require('ol.View');
+54 -51
View File
@@ -131,33 +131,36 @@ module.exports = function(info, api) {
// replace `ol.VERSION = ''` with correct version // replace `ol.VERSION = ''` with correct version
root.find(j.ExpressionStatement, getMemberExpressionAssignment('ol.VERSION')) root.find(j.ExpressionStatement, getMemberExpressionAssignment('ol.VERSION'))
.forEach(path => { .forEach(path => {
path.value.expression.right = j.literal('v' + thisPackage.version); path.value.expression.right = j.literal('v' + thisPackage.version);
}); });
const replacements = {}; const replacements = {};
// replace all uses of defines // replace all uses of defines
root.find(j.MemberExpression, defineMemberExpression) root.find(j.MemberExpression, defineMemberExpression)
.filter(path => { .filter(path => {
const node = path.value; const node = path.value;
const name = `${node.object.name}.${node.property.name}`; const name = `${node.object.name}.${node.property.name}`;
return (name in defines) && path.parentPath.value.type !== 'AssignmentExpression'; return (name in defines) && path.parentPath.value.type !== 'AssignmentExpression';
}) })
.replaceWith(path => { .replaceWith(path => {
const name = `${path.value.object.name}.${path.value.property.name}`; const name = `${path.value.object.name}.${path.value.property.name}`;
return j.literal(defines[name]); return j.literal(defines[name]);
}); });
// remove goog.provide() // remove goog.provide()
let provide; let provide;
root.find(j.ExpressionStatement, getGoogExpressionStatement('provide')) root.find(j.ExpressionStatement, getGoogExpressionStatement('provide'))
.forEach(path => { .forEach(path => {
if (provide) { if (provide) {
throw new Error(`Multiple provides in ${info.path}`); throw new Error(`Multiple provides in ${info.path}`);
} }
provide = path.value.expression.arguments[0].value; provide = path.value.expression.arguments[0].value;
}).remove(); if (provide.indexOf(' ') > -1) {
throw new Error(`Space in provide "${provide}" in ${info.path}`);
}
}).remove();
if (!provide) { if (!provide) {
throw new Error(`No provide found in ${info.path}`); throw new Error(`No provide found in ${info.path}`);
@@ -168,48 +171,48 @@ module.exports = function(info, api) {
// e.g. `ol.foo.Bar = function() {}` -> `var _ol_foo_Bar_ = function() {}` // e.g. `ol.foo.Bar = function() {}` -> `var _ol_foo_Bar_ = function() {}`
let declaredProvide = false; let declaredProvide = false;
root.find(j.ExpressionStatement, getMemberExpressionAssignment(provide)) root.find(j.ExpressionStatement, getMemberExpressionAssignment(provide))
.replaceWith(path => { .replaceWith(path => {
declaredProvide = true; declaredProvide = true;
const statement = j.variableDeclaration('var', [ const statement = j.variableDeclaration('var', [
j.variableDeclarator(j.identifier(rename(provide)), path.value.expression.right) j.variableDeclarator(j.identifier(rename(provide)), path.value.expression.right)
]); ]);
statement.comments = path.value.comments; statement.comments = path.value.comments;
return statement; return statement;
}); });
if (!declaredProvide) { if (!declaredProvide) {
const body = root.find(j.Program).get('body'); const body = root.find(j.Program).get('body');
body.unshift( body.unshift(
j.variableDeclaration('var', [ j.variableDeclaration('var', [
j.variableDeclarator(j.identifier(rename(provide)), j.objectExpression([])) j.variableDeclarator(j.identifier(rename(provide)), j.objectExpression([]))
]) ])
); );
} }
// replace `goog.require('foo')` with `import foo from 'foo'` // replace `goog.require('foo')` with `import foo from 'foo'`
const imports = []; const imports = [];
root.find(j.ExpressionStatement, getGoogExpressionStatement('require')) root.find(j.ExpressionStatement, getGoogExpressionStatement('require'))
.forEach(path => { .forEach(path => {
const name = path.value.expression.arguments[0].value; const name = path.value.expression.arguments[0].value;
if (name in replacements) { 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);
replacements[name] = renamed; replacements[name] = renamed;
const resolved = resolve(provide, name); const resolved = resolve(provide, name);
let specifier, source; let specifier, source;
if (Array.isArray(resolved)) { if (Array.isArray(resolved)) {
// import {imported as renamed} from 'source'; // import {imported as renamed} from 'source';
specifier = j.importSpecifier(j.identifier(resolved[1]), j.identifier(renamed)); specifier = j.importSpecifier(j.identifier(resolved[1]), j.identifier(renamed));
source = resolved[0]; source = resolved[0];
} else { } else {
// import renamed from 'source'; // import renamed from 'source';
specifier = j.importDefaultSpecifier(j.identifier(renamed)); specifier = j.importDefaultSpecifier(j.identifier(renamed));
source = resolved; source = resolved;
} }
imports.push(j.importDeclaration([specifier], j.literal(source))); imports.push(j.importDeclaration([specifier], j.literal(source)));
}) })
.remove(); .remove();
const body = root.find(j.Program).get('body'); const body = root.find(j.Program).get('body');
body.unshift.apply(body, imports); body.unshift.apply(body, imports);
@@ -218,16 +221,16 @@ module.exports = function(info, api) {
Object.keys(replacements).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(replacements[name])); .replaceWith(j.identifier(replacements[name]));
} else { } else {
root.find(j.Identifier, {name: name}) root.find(j.Identifier, {name: name})
.replaceWith(j.identifier(replacements[name])); .replaceWith(j.identifier(replacements[name]));
} }
}); });
// add export declaration // add export declaration
root.find(j.Program).get('body').push( root.find(j.Program).get('body').push(
j.exportDefaultDeclaration(j.identifier(rename(provide))) j.exportDefaultDeclaration(j.identifier(rename(provide)))
); );
// replace any initial comments // replace any initial comments