[CS2] Fix v3 source map (#4671)

* Per discussion in #3075: if `sourceFiles` is unspecified, but `filename` is, use `filename`; output null instead of an empty string for `sources` or `sourceRoot`

* Update source map tests to reflect that now we return null instead of empty strings; check generated sources array

* Update source map documentation; still leave more obscure options undocumented

* Follow the TypeScript compiler’s example regarding v3SourceMap, but output empty strings instead of made-up filenames

* Have `sources` default to ‘<anonymous>’
This commit is contained in:
Geoffrey Booth 2017-09-01 01:06:45 -07:00 committed by GitHub
parent 906bedf93a
commit fe5ff39ca2
5 changed files with 41 additions and 23 deletions

View File

@ -20,6 +20,6 @@ The `compile` method has the signature `compile(code, options)` where `code` is
* `options.sourceMap`, boolean: if true, a source map will be generated; and instead of returning a string, `compile` will return an object of the form `{js, v3SourceMap, sourceMap}`.
* `options.inlineMap`, boolean: if true, output the source map as a base64-encoded string in a comment at the bottom.
* `options.filename`, string: the filename to use for the source map.
* `options.filename`, string: the filename to use for the source map. It can include a path (relative or absolute).
* `options.bare`, boolean: if true, output without the [top-level function safety wrapper](#lexical-scope).
* `options.header`, boolean: if true, output the `Generated by CoffeeScript` header.

View File

@ -93,7 +93,7 @@
// map. Also, `options.sourceFiles` and `options.generatedFile` may be passed to
// set "sources" and "file", respectively.
generate(options = {}, code = null) {
var buffer, i, j, lastColumn, lastSourceColumn, lastSourceLine, len, len1, lineMap, lineNumber, mapping, needComma, ref, ref1, v3, writingline;
var buffer, i, j, lastColumn, lastSourceColumn, lastSourceLine, len, len1, lineMap, lineNumber, mapping, needComma, ref, ref1, sources, v3, writingline;
writingline = 0;
lastColumn = 0;
lastSourceLine = 0;
@ -141,11 +141,12 @@
}
}
// Produce the canonical JSON object format for a "v3" source map.
sources = options.sourceFiles ? options.sourceFiles : options.filename ? [options.filename] : ['<anonymous>'];
v3 = {
version: 3,
file: options.generatedFile || '',
sourceRoot: options.sourceRoot || '',
sources: options.sourceFiles || [''],
sources: sources,
names: [],
mappings: buffer
};

View File

@ -118,11 +118,18 @@ The starting column in the original source, relative to the previous column.
Produce the canonical JSON object format for a "v3" source map.
sources = if options.sourceFiles
options.sourceFiles
else if options.filename
[options.filename]
else
['<anonymous>']
v3 =
version: 3
file: options.generatedFile or ''
sourceRoot: options.sourceRoot or ''
sources: options.sourceFiles or ['']
sources: sources
names: []
mappings: buffer

View File

@ -3,13 +3,13 @@ return if global.testingBrowser
SourceMap = require '../src/sourcemap'
vlqEncodedValues = [
[1, "C"],
[-1, "D"],
[2, "E"],
[-2, "F"],
[0, "A"],
[16, "gB"],
[948, "o7B"]
[1, 'C'],
[-1, 'D'],
[2, 'E'],
[-2, 'F'],
[0, 'A'],
[16, 'gB'],
[948, 'o7B']
]
test "encodeVlq tests", ->
@ -25,27 +25,27 @@ test "SourceMap tests", ->
map.add [3, 0], [3, 4]
testWithFilenames = map.generate {
sourceRoot: ""
sourceFiles: ["source.coffee"]
generatedFile: "source.js"
sourceRoot: ''
sourceFiles: ['source.coffee']
generatedFile: 'source.js'
}
deepEqual testWithFilenames, {
version: 3
file: "source.js"
sourceRoot: ""
sources: ["source.coffee"]
file: 'source.js'
sourceRoot: ''
sources: ['source.coffee']
names: []
mappings: "AAAA;;IACK,GAAC,CAAG;IAET"
mappings: 'AAAA;;IACK,GAAC,CAAG;IAET'
}
deepEqual map.generate(), {
version: 3
file: ""
sourceRoot: ""
sources: [""]
file: ''
sourceRoot: ''
sources: ['<anonymous>']
names: []
mappings: "AAAA;;IACK,GAAC,CAAG;IAET"
mappings: 'AAAA;;IACK,GAAC,CAAG;IAET'
}
# Look up a generated column - should get back the original source position.
@ -53,3 +53,13 @@ test "SourceMap tests", ->
# Look up a point further along on the same line - should get back the same source position.
arrayEq map.sourceLocation([2,10]), [1,9]
test "#3075: v3 source map fields", ->
{ js, v3SourceMap, sourceMap } = CoffeeScript.compile 'console.log Date.now()',
filename: 'tempus_fugit.coffee'
sourceMap: yes
sourceRoot: './www_root/coffee/'
v3SourceMap = JSON.parse v3SourceMap
arrayEq v3SourceMap.sources, ['tempus_fugit.coffee']
eq v3SourceMap.sourceRoot, './www_root/coffee/'

View File

@ -29,7 +29,7 @@ exports.eq = (a, b, msg) ->
"Expected #{reset}#{a}#{red} to equal #{reset}#{b}#{red}"
exports.arrayEq = (a, b, msg) ->
ok arrayEgal(a,b), msg or
ok arrayEgal(a, b), msg or
"Expected #{reset}#{a}#{red} to deep equal #{reset}#{b}#{red}"
exports.eqJS = (input, expectedOutput, msg) ->