Last active
June 15, 2017 08:26
-
-
Save romain-h/832ef38fe8511fea2bda77d98cce4820 to your computer and use it in GitHub Desktop.
Codemods medium post
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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)); | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// src/base/collections/dashboards.js | |
GB.collections.Dashboards = GB.collections.Collection.extend({ | |
model: GB.models.Dashboard, | |
... | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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, | |
... | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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