Scripts for in-place transforms
This commit is contained in:
+9
-1
@@ -17,7 +17,12 @@
|
|||||||
"pretest": "npm run lint",
|
"pretest": "npm run lint",
|
||||||
"test": "npm run karma -- --single-run",
|
"test": "npm run karma -- --single-run",
|
||||||
"debug-server": "node tasks/serve-lib.js",
|
"debug-server": "node tasks/serve-lib.js",
|
||||||
"karma": "node tasks/test.js start test/karma.config.js"
|
"karma": "node tasks/test.js start test/karma.config.js",
|
||||||
|
"transform-src": "jscodeshift --transform transforms/module.js src",
|
||||||
|
"transform-examples": "jscodeshift --transform transforms/module.js examples",
|
||||||
|
"transform-test-spec": "jscodeshift --transform transforms/module.js test/spec",
|
||||||
|
"transform-test-rendering": "jscodeshift --transform transforms/module.js test/rendering",
|
||||||
|
"transform": "npm run transform-src && npm run transform-examples && npm run transform-test-spec && npm run transform-test-rendering && npm run lint -- --fix"
|
||||||
},
|
},
|
||||||
"main": "dist/ol.js",
|
"main": "dist/ol.js",
|
||||||
"repository": {
|
"repository": {
|
||||||
@@ -83,6 +88,9 @@
|
|||||||
},
|
},
|
||||||
"eslintConfig": {
|
"eslintConfig": {
|
||||||
"extends": "openlayers",
|
"extends": "openlayers",
|
||||||
|
"parserOptions": {
|
||||||
|
"sourceType": "module"
|
||||||
|
},
|
||||||
"globals": {
|
"globals": {
|
||||||
"ArrayBuffer": false,
|
"ArrayBuffer": false,
|
||||||
"Float32Array": false,
|
"Float32Array": false,
|
||||||
|
|||||||
+41
-29
@@ -42,6 +42,10 @@ function resolve(fromName, toName) {
|
|||||||
return packageName;
|
return packageName;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (fromParts[0] === 'examples' || fromParts[0] === 'test') {
|
||||||
|
fromParts.unshift('root');
|
||||||
|
toParts.unshift('root', 'src');
|
||||||
|
}
|
||||||
const fromLength = fromParts.length;
|
const fromLength = fromParts.length;
|
||||||
let commonDepth = 1;
|
let commonDepth = 1;
|
||||||
while (commonDepth < fromLength - 2) {
|
while (commonDepth < fromLength - 2) {
|
||||||
@@ -60,6 +64,12 @@ function resolve(fromName, toName) {
|
|||||||
return relative;
|
return relative;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getUnprovided(path) {
|
||||||
|
path = path.replace(/\.(test\.)?js$/, '');
|
||||||
|
const parts = path.split('/');
|
||||||
|
return parts.join('.');
|
||||||
|
}
|
||||||
|
|
||||||
function getGoogExpressionStatement(identifier) {
|
function getGoogExpressionStatement(identifier) {
|
||||||
return {
|
return {
|
||||||
type: 'ExpressionStatement',
|
type: 'ExpressionStatement',
|
||||||
@@ -151,7 +161,7 @@ module.exports = function(info, api) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// remove goog.provide()
|
// remove goog.provide()
|
||||||
let provide;
|
let provide, unprovided;
|
||||||
root.find(j.ExpressionStatement, getGoogExpressionStatement('provide'))
|
root.find(j.ExpressionStatement, getGoogExpressionStatement('provide'))
|
||||||
.forEach(path => {
|
.forEach(path => {
|
||||||
if (provide) {
|
if (provide) {
|
||||||
@@ -163,31 +173,31 @@ module.exports = function(info, api) {
|
|||||||
}
|
}
|
||||||
}).remove();
|
}).remove();
|
||||||
|
|
||||||
if (!provide) {
|
if (provide) {
|
||||||
throw new Error(`No provide found in ${info.path}`);
|
replacements[provide] = rename(provide);
|
||||||
}
|
// replace provide assignment with variable declarator
|
||||||
replacements[provide] = rename(provide);
|
// e.g. `ol.foo.Bar = function() {}` -> `var _ol_foo_Bar_ = function() {}`
|
||||||
|
let declaredProvide = false;
|
||||||
|
root.find(j.ExpressionStatement, getMemberExpressionAssignment(provide))
|
||||||
|
.replaceWith(path => {
|
||||||
|
declaredProvide = true;
|
||||||
|
const statement = j.variableDeclaration('var', [
|
||||||
|
j.variableDeclarator(j.identifier(rename(provide)), path.value.expression.right)
|
||||||
|
]);
|
||||||
|
statement.comments = path.value.comments;
|
||||||
|
return statement;
|
||||||
|
});
|
||||||
|
|
||||||
// replace provide assignment with variable declarator
|
if (!declaredProvide) {
|
||||||
// e.g. `ol.foo.Bar = function() {}` -> `var _ol_foo_Bar_ = function() {}`
|
const body = root.find(j.Program).get('body');
|
||||||
let declaredProvide = false;
|
body.unshift(
|
||||||
root.find(j.ExpressionStatement, getMemberExpressionAssignment(provide))
|
j.variableDeclaration('var', [
|
||||||
.replaceWith(path => {
|
j.variableDeclarator(j.identifier(rename(provide)), j.objectExpression([]))
|
||||||
declaredProvide = true;
|
])
|
||||||
const statement = j.variableDeclaration('var', [
|
);
|
||||||
j.variableDeclarator(j.identifier(rename(provide)), path.value.expression.right)
|
}
|
||||||
]);
|
} else {
|
||||||
statement.comments = path.value.comments;
|
unprovided = getUnprovided(info.path);
|
||||||
return statement;
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!declaredProvide) {
|
|
||||||
const body = root.find(j.Program).get('body');
|
|
||||||
body.unshift(
|
|
||||||
j.variableDeclaration('var', [
|
|
||||||
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'`
|
||||||
@@ -200,7 +210,7 @@ module.exports = function(info, api) {
|
|||||||
}
|
}
|
||||||
const renamed = rename(name);
|
const renamed = rename(name);
|
||||||
replacements[name] = renamed;
|
replacements[name] = renamed;
|
||||||
const resolved = resolve(provide, name);
|
const resolved = resolve(provide || unprovided, 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';
|
||||||
@@ -230,9 +240,11 @@ module.exports = function(info, api) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// add export declaration
|
// add export declaration
|
||||||
root.find(j.Program).get('body').push(
|
if (provide) {
|
||||||
j.exportDefaultDeclaration(j.identifier(rename(provide)))
|
root.find(j.Program).get('body').push(
|
||||||
);
|
j.exportDefaultDeclaration(j.identifier(rename(provide)))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// replace any initial comments
|
// replace any initial comments
|
||||||
root.get().node.comments = comments;
|
root.get().node.comments = comments;
|
||||||
|
|||||||
Reference in New Issue
Block a user