[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:
parent
906bedf93a
commit
fe5ff39ca2
|
@ -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.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.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.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.
|
* `options.header`, boolean: if true, output the `Generated by CoffeeScript` header.
|
||||||
|
|
|
@ -93,7 +93,7 @@
|
||||||
// map. Also, `options.sourceFiles` and `options.generatedFile` may be passed to
|
// map. Also, `options.sourceFiles` and `options.generatedFile` may be passed to
|
||||||
// set "sources" and "file", respectively.
|
// set "sources" and "file", respectively.
|
||||||
generate(options = {}, code = null) {
|
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;
|
writingline = 0;
|
||||||
lastColumn = 0;
|
lastColumn = 0;
|
||||||
lastSourceLine = 0;
|
lastSourceLine = 0;
|
||||||
|
@ -141,11 +141,12 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Produce the canonical JSON object format for a "v3" source map.
|
// Produce the canonical JSON object format for a "v3" source map.
|
||||||
|
sources = options.sourceFiles ? options.sourceFiles : options.filename ? [options.filename] : ['<anonymous>'];
|
||||||
v3 = {
|
v3 = {
|
||||||
version: 3,
|
version: 3,
|
||||||
file: options.generatedFile || '',
|
file: options.generatedFile || '',
|
||||||
sourceRoot: options.sourceRoot || '',
|
sourceRoot: options.sourceRoot || '',
|
||||||
sources: options.sourceFiles || [''],
|
sources: sources,
|
||||||
names: [],
|
names: [],
|
||||||
mappings: buffer
|
mappings: buffer
|
||||||
};
|
};
|
||||||
|
|
|
@ -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.
|
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 =
|
v3 =
|
||||||
version: 3
|
version: 3
|
||||||
file: options.generatedFile or ''
|
file: options.generatedFile or ''
|
||||||
sourceRoot: options.sourceRoot or ''
|
sourceRoot: options.sourceRoot or ''
|
||||||
sources: options.sourceFiles or ['']
|
sources: sources
|
||||||
names: []
|
names: []
|
||||||
mappings: buffer
|
mappings: buffer
|
||||||
|
|
||||||
|
|
|
@ -3,13 +3,13 @@ return if global.testingBrowser
|
||||||
SourceMap = require '../src/sourcemap'
|
SourceMap = require '../src/sourcemap'
|
||||||
|
|
||||||
vlqEncodedValues = [
|
vlqEncodedValues = [
|
||||||
[1, "C"],
|
[1, 'C'],
|
||||||
[-1, "D"],
|
[-1, 'D'],
|
||||||
[2, "E"],
|
[2, 'E'],
|
||||||
[-2, "F"],
|
[-2, 'F'],
|
||||||
[0, "A"],
|
[0, 'A'],
|
||||||
[16, "gB"],
|
[16, 'gB'],
|
||||||
[948, "o7B"]
|
[948, 'o7B']
|
||||||
]
|
]
|
||||||
|
|
||||||
test "encodeVlq tests", ->
|
test "encodeVlq tests", ->
|
||||||
|
@ -25,27 +25,27 @@ test "SourceMap tests", ->
|
||||||
map.add [3, 0], [3, 4]
|
map.add [3, 0], [3, 4]
|
||||||
|
|
||||||
testWithFilenames = map.generate {
|
testWithFilenames = map.generate {
|
||||||
sourceRoot: ""
|
sourceRoot: ''
|
||||||
sourceFiles: ["source.coffee"]
|
sourceFiles: ['source.coffee']
|
||||||
generatedFile: "source.js"
|
generatedFile: 'source.js'
|
||||||
}
|
}
|
||||||
|
|
||||||
deepEqual testWithFilenames, {
|
deepEqual testWithFilenames, {
|
||||||
version: 3
|
version: 3
|
||||||
file: "source.js"
|
file: 'source.js'
|
||||||
sourceRoot: ""
|
sourceRoot: ''
|
||||||
sources: ["source.coffee"]
|
sources: ['source.coffee']
|
||||||
names: []
|
names: []
|
||||||
mappings: "AAAA;;IACK,GAAC,CAAG;IAET"
|
mappings: 'AAAA;;IACK,GAAC,CAAG;IAET'
|
||||||
}
|
}
|
||||||
|
|
||||||
deepEqual map.generate(), {
|
deepEqual map.generate(), {
|
||||||
version: 3
|
version: 3
|
||||||
file: ""
|
file: ''
|
||||||
sourceRoot: ""
|
sourceRoot: ''
|
||||||
sources: [""]
|
sources: ['<anonymous>']
|
||||||
names: []
|
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.
|
# 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.
|
# Look up a point further along on the same line - should get back the same source position.
|
||||||
arrayEq map.sourceLocation([2,10]), [1,9]
|
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/'
|
||||||
|
|
|
@ -29,7 +29,7 @@ exports.eq = (a, b, msg) ->
|
||||||
"Expected #{reset}#{a}#{red} to equal #{reset}#{b}#{red}"
|
"Expected #{reset}#{a}#{red} to equal #{reset}#{b}#{red}"
|
||||||
|
|
||||||
exports.arrayEq = (a, b, msg) ->
|
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}"
|
"Expected #{reset}#{a}#{red} to deep equal #{reset}#{b}#{red}"
|
||||||
|
|
||||||
exports.eqJS = (input, expectedOutput, msg) ->
|
exports.eqJS = (input, expectedOutput, msg) ->
|
||||||
|
|
Loading…
Reference in New Issue