Created
October 18, 2012 16:52
-
-
Save beatak/3913217 to your computer and use it in GitHub Desktop.
testing esprima/escodegen
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
#!/usr/local/bin/node | |
var parse = require('esprima').parse; | |
var generate = require('escodegen').generate; | |
// MULTI ARGS ==================================== | |
var code = "console.log('a', 'b', 'c');"; | |
console.log(code); | |
console.log( '====' ); | |
var ast = parse(code); | |
var args = ast.body[0].expression.arguments; | |
ast.body[0].expression = { | |
type: 'SequenceExpression', | |
expressions: args | |
}; | |
console.log( JSON.stringify( ast, null, 2) ); | |
console.log( '====' ); | |
console.log( generate(ast) ); | |
console.log( '====' ); | |
// SINGLE ARG ==================================== | |
var code = "console.log('a');"; | |
console.log(code); | |
console.log( '====' ); | |
var ast = parse(code); | |
if (2 > ast.body[0].expression.arguments.length) { | |
var args = ast.body[0].expression.arguments; | |
ast.body[0].expression = args[0]; | |
} | |
console.log( JSON.stringify( ast, null, 2) ); | |
console.log( '====' ); | |
console.log( generate(ast) ); | |
console.log( '====' ); | |
// ZERO ARGS ==================================== | |
var code = "console.log();"; | |
console.log(code); | |
console.log( '====' ); | |
var ast = parse(code); | |
if (0 === ast.body[0].expression.arguments.length) { | |
ast.body.splice(0, 1); | |
} | |
console.log( JSON.stringify( ast, null, 2) ); | |
console.log( '====' ); | |
console.log( generate(ast) ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment