Created
February 12, 2015 04:36
-
-
Save ichernev/c631ae7532013414a908 to your computer and use it in GitHub Desktop.
Helper script using recast.js to convert moment files to es6
This file contains 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
var recast = require('recast'); | |
var fs = require('fs'); | |
var types = recast.types; | |
var n = types.namedTypes; | |
var b = recast.types.builders; | |
var locale = 'en'; | |
function doIt(locale) { | |
console.log("processing", locale); | |
var ast = recast.parse(fs.readFileSync('test/locale/' + locale + '.js', {encoding: 'utf8'})); | |
var newTests = []; | |
function toKey(node) { | |
return n.Identifier.check(node) ? node.name : | |
n.Literal.check(node) ? node.value : null; | |
} | |
types.visit(ast, { | |
visitAssignmentExpression: function (path) { | |
var that = this; | |
this.traverse(path); | |
if (n.MemberExpression.check(path.node.left) && | |
n.Identifier.check(path.node.left.object) && | |
path.node.left.object.name === "exports" && | |
n.ObjectExpression.check(path.node.right)) { | |
path.node.right.properties.forEach(function (prop) { | |
var key = that.toKey(prop.key); | |
if (key === "tearDown" || key === "setUp") { | |
return; | |
} | |
if (key === null) { | |
console.log("WTF got null key", prop.key); | |
return; | |
} | |
newTests.push(b.expressionStatement( | |
b.callExpression( | |
b.identifier("test"), | |
[b.literal(key), that.transformTest(prop.value)] | |
) | |
)); | |
}); | |
} | |
}, | |
toKey: function (node) { | |
return n.Identifier.check(node) ? node.name : | |
n.Literal.check(node) ? node.value : null; | |
}, | |
transformTest: function (fnNode) { | |
types.visit(fnNode, { | |
visitExpressionStatement: function (path) { | |
this.traverse(path); | |
if (n.CallExpression.check(path.node.expression) && | |
n.MemberExpression.check(path.node.expression.callee) && | |
n.Identifier.check(path.node.expression.callee.object) && | |
path.node.expression.callee.object.name === "test") { | |
if (n.Identifier.check(path.node.expression.callee.property) && | |
path.node.expression.callee.property.name === "done") { | |
// console.log('remove test.done()'); | |
path.prune(); | |
return; | |
} | |
if (n.Identifier.check(path.node.expression.callee.property) && | |
path.node.expression.callee.property.name === "expect") { | |
console.log('remove test.expect()'); | |
path.prune(); | |
return | |
} | |
var prop = path.node.expression.callee.property; | |
if (n.Identifier.check(prop)) { | |
switch (prop.name) { | |
case "ok": | |
// console.log("replaced test.ok with assert.ok"); | |
path.get("expression", "callee").replace( | |
b.memberExpression( | |
b.identifier("assert"), | |
b.identifier("ok"), | |
false | |
) | |
); | |
break; | |
case "equal": | |
// console.log("replaced test.ok with assert.equal"); | |
path.get("expression", "callee").replace( | |
b.memberExpression( | |
b.identifier("assert"), | |
b.identifier("equal"), | |
false | |
) | |
); | |
break; | |
} | |
} else { | |
console.log("I don't know what is test.", prop); | |
} | |
} | |
} | |
}); | |
fnNode.params = [b.identifier("assert")]; | |
return fnNode; | |
} | |
}); | |
var out = b.program([ | |
b.importDeclaration( | |
[ | |
b.importSpecifier(b.identifier("localeModule")), | |
b.importSpecifier(b.identifier("test")) | |
], | |
b.moduleSpecifier("../qunit") | |
), | |
b.importDeclaration( | |
[ | |
b.importSpecifier(b.identifier("moment")), | |
], | |
b.moduleSpecifier("../../moment") | |
), | |
b.expressionStatement( | |
b.callExpression( | |
b.identifier("localeModule"), | |
[b.literal(locale)] | |
) | |
) | |
].concat(newTests)); | |
fs.writeFileSync("src/test/locale/" + locale + ".js", recast.print(out).code); | |
} | |
doIt("he"); | |
// "hi hr hu hy-am id is it ja ka km ko lb lt lv mk ml mr ms-my my nb ne nl nn pl pt-br pt ro ru sk sl sq sr-cyrl sr sv ta th tl-ph tr tzm-latn tzm uk uz vi zh-cn zh-tw".split(" ").forEach(doIt); | |
// "af ar-ma ar-sa ar-tn ar az be bg bn bo br bs ca cs cv cy da de-at de el en-au en-ca en-gb en eo es et eu fa fi fo fr-ca fr fy gl he hi hr hu hy-am id is it ja ka km ko lb lt lv mk ml mr ms-my my nb ne nl nn pl pt-br pt ro ru sk sl sq sr-cyrl sr sv ta th tl-ph tr tzm-latn tzm uk uz vi zh-cn zh-tw".split(" ").forEach(doIt); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment