UNPKG

grunt-zuckrig-closure

Version:

Reduce a verbose syntax for Google Closure Compiler to be more Pythonic/Rubistic.

49 lines (38 loc) 927 B
var Parser, esprima; esprima = require('esprima'); /** Inspirated by https://github.com/steida/coffee2closure */ Parser = (function() { function Parser(source) { this.source = source; this.parsed = null; this.tokens = null; } Parser.prototype.parse = function() { this.parsed = esprima.parse(this.source, { comment: true, tokens: true, range: true, loc: true }); return this._prepare_tokens(); }; Parser.prototype._prepare_tokens = function() { this.tokens = this.parsed.tokens.concat(this.parsed.comments); return this._sort_tokens(this.tokens); }; Parser.prototype._sort_tokens = function() { return this.tokens.sort(function(a, b) { if (a.range[0] > b.range[0]) { return 1; } if (a.range[0] < b.range[0]) { return -1; } return 0; }); }; return Parser; })(); module.exports = Parser;