Skip to content

Instantly share code, notes, and snippets.

@romain-h
Last active June 15, 2017 08:26
Show Gist options
  • Save romain-h/832ef38fe8511fea2bda77d98cce4820 to your computer and use it in GitHub Desktop.
Save romain-h/832ef38fe8511fea2bda77d98cce4820 to your computer and use it in GitHub Desktop.
Codemods medium post
const collectGBUsages = (refStore, store) => node =>
node.find(j.MemberExpression, { // XX.XXXX
object: { type: 'MemberExpression' },
})
.filter(path => (
path.parentPath.value.type !== 'AssignmentExpression' &&
isGBNamespaced(path.value) // Check GB.xxx
))
.forEach(path => {
refStore.add(path);
const type = path.value.object.property.name;
const name = path.value.property.name;
if (!store.has(type)) {
store.set(type, new Set());
}
store.set(type, store.get(type).add(name));
});
// Create imports and definitions
const files = [];
const names = [];
store.forEach((imports, type) => {
imports.forEach(imp => {
files.push(j.literal(`src/base/${type}/${kebabCase(imp)}`));
names.push(j.identifier(imp));
});
});
// src/base/collections/dashboards.js
GB.collections.Dashboards = GB.collections.Collection.extend({
model: GB.models.Dashboard,
...
});
// src/base/collections/dashboards.js
GB.collections.Dashboards = GB.collections.Collection.extend({
define([
'src/base/collections/collection',
'src/base/models/dashboard'
], function(Collection, Dashboard) {
return Collection.extend({
model: Dashboard,
...
});
});
root.find(j.ExpressionStatement, {
expression: {
type: 'AssignmentExpression',
operator: '=',
},
})
.filter(path =>
path.value.expression.left &&
isGBNamespaced(path.value.expression.left)
)
.forEach(assignment => {
const body = j.returnStatement(assignment.value.expression.right);
j(assignment)
.replaceWith(
j.expressionStatement(
j.callExpression(
j.identifier('define'),
[
j.arrayExpression(files),
j.functionExpression(null, names, j.blockStatement([body]))
]
)
)
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment