-
-
Save hkdnet/2ee29054cb7115aba688ef91d4fe253a to your computer and use it in GitHub Desktop.
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
class User { | |
} | |
class Something { | |
} | |
User.defineRelationship('Something', { to: 'one' }); |
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 esprima = require('esprima'); | |
var fs = require('fs'); | |
var srcDir = './samples/js/'; | |
var dstDir = './samples/ast/'; | |
fs.readdir(srcDir, function(err, files){ | |
if (err) throw err; | |
files.forEach(function(fileName) { | |
fs.readFile(srcDir + fileName, function(err, code) { | |
if(err) throw err; | |
var ast = esprima.parse(code); | |
var outPath = (dstDir + fileName).replace(/.js/, '.ast.json'); | |
fs.writeFile(outPath, JSON.stringify(ast, null, 2), function(err) { | |
if(err) throw err; | |
console.log(outPath); | |
}); | |
}); | |
}); | |
}); |
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
'use strict'; | |
var ast = require('./samples/ast/relation.ast.json'); | |
var escodegen = require('escodegen'); | |
var estraverse = require('estraverse'); | |
ast = estraverse.replace(ast, { | |
enter: function(node) { | |
if (node.type === 'ExpressionStatement' && node.expression.type === 'Literal') { | |
var m = node.expression.value.match(/(\w+)\s+has\s+(\w+)\s+(\w+)/); | |
if (!m) this.skip(); | |
return { | |
'type': 'ExpressionStatement', | |
'expression': { | |
'type': 'CallExpression', | |
'callee': { | |
'type': 'MemberExpression', | |
'computed': false, | |
'object': { | |
'type': 'Identifier', | |
'name': m[1] | |
}, | |
'property': { | |
'type': 'Identifier', | |
'name': 'defineRelationship' | |
} | |
}, | |
'arguments': [{ | |
'type': 'Literal', | |
'value': m[3], | |
'raw': '\'' + m[3] + '\'' | |
}, { | |
'type': 'ObjectExpression', | |
'properties': [{ | |
'type': 'Property', | |
'key': { | |
'type': 'Identifier', | |
'name': 'to' | |
}, | |
'computed': false, | |
'value': { | |
'type': 'Literal', | |
'value': m[2], | |
'raw': '\'' + m[2] + '\'' | |
}, | |
'kind': 'init', | |
'method': false, | |
'shorthand': false | |
}] | |
}] | |
} | |
}; | |
} | |
} | |
}); | |
var code = escodegen.generate(ast); | |
console.log(code); |
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
class User { | |
} | |
class Something { | |
} | |
"User has one Something"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment