Skip to content

Instantly share code, notes, and snippets.

@isakb
Created September 21, 2012 22:58
Show Gist options
  • Select an option

  • Save isakb/3764396 to your computer and use it in GitHub Desktop.

Select an option

Save isakb/3764396 to your computer and use it in GitHub Desktop.
Escodegen with comments etc.
{
"author": "Isak <isak@klarna.com>",
"name": "escapism",
"version": "0.0.0",
"repository": {
"url": ""
},
"dependencies": {
"escodegen": "git://github.com/Constellation/escodegen.git#master",
"esprima": "git://github.com/ariya/esprima.git#master"
},
"devDependencies": {},
"optionalDependencies": {},
"engines": {
"node": "*"
}
}
#!/usr/bin/env node
/*jshint node:true*/
"use strict";
var fs = require('fs'),
escodegen = require('escodegen'),
esprima = require('esprima'),
files = process.argv.splice(2);
if (files.length === 0) {
console.log('Usage:');
console.log(' run.js file.js');
process.exit(1);
}
files.forEach(function (filename) {
var content = fs.readFileSync(filename, 'utf-8'),
tree = esprima.parse(content, {
range: true,
tokens: true,
comment: true
});
tree = escodegen.attachComments(tree, tree.comments, tree.tokens);
console.log(escodegen.generate(tree, {
comment: true,
format: {
indent: {
style: ' ',
base: 0,
adjustMultilineComment: false
},
quotes: 'auto',
escapeless: true
}
}));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment