ariya/esprima — pull request #145 by dvdotsenko (2012-12-04 18:37:23+00:00)
Title: SourceMap support for multiple source file names

ESCodeGen now supports generating sourcemaps for AST tree that came from multiple source files.

https://github.com/Constellation/escodegen/pull/61

ESCodeGen looks at the `loc.source` property as defined in the spec https://developer.mozilla.org/en-US/docs/SpiderMonkey/Parser_API

If `loc.source` is defined it uses that entry value as filename for the generation of sourcemap.

With the proposed commit ESPrima now inserts the file name of the source into each `loc` node in parsed AST tree when `options.loc.source` is defined:

var a = ep.parse('var a = 1', {loc:{source:'filenameA.js'}});
var b = ep.parse('var b = 2', {loc:{source:'filenameB.js'}});

// joining the two AST trees
var c = {'type':'Program',body:[].concat(a.body, b.body)};

ec.generate(c);
// output:
'var a = 1;\nvar b = 2;'

ec.generate(c, {sourceMap:true});
// output:
{"version":3,"file":true,"sources":["filenameA.js","filenameB.js"],"names":[],"mappings":"AAAA,GAAA,CAAI,IAAI,CAAR;ACAA,GAAA,CAAI,IAAI,CAAR"}