Skip to content

Commit fe5ff39

Browse files
[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>’
1 parent 906bedf commit fe5ff39

File tree

5 files changed

+41
-23
lines changed

5 files changed

+41
-23
lines changed

documentation/sections/nodejs_usage.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ The `compile` method has the signature `compile(code, options)` where `code` is
2020

2121
* `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}`.
2222
* `options.inlineMap`, boolean: if true, output the source map as a base64-encoded string in a comment at the bottom.
23-
* `options.filename`, string: the filename to use for the source map.
23+
* `options.filename`, string: the filename to use for the source map. It can include a path (relative or absolute).
2424
* `options.bare`, boolean: if true, output without the [top-level function safety wrapper](#lexical-scope).
2525
* `options.header`, boolean: if true, output the `Generated by CoffeeScript` header.

lib/coffeescript/sourcemap.js

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/sourcemap.litcoffee

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,18 @@ The starting column in the original source, relative to the previous column.
118118
119119
Produce the canonical JSON object format for a "v3" source map.
120120

121+
sources = if options.sourceFiles
122+
options.sourceFiles
123+
else if options.filename
124+
[options.filename]
125+
else
126+
['<anonymous>']
127+
121128
v3 =
122129
version: 3
123130
file: options.generatedFile or ''
124131
sourceRoot: options.sourceRoot or ''
125-
sources: options.sourceFiles or ['']
132+
sources: sources
126133
names: []
127134
mappings: buffer
128135

test/sourcemap.coffee

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ return if global.testingBrowser
33
SourceMap = require '../src/sourcemap'
44

55
vlqEncodedValues = [
6-
[1, "C"],
7-
[-1, "D"],
8-
[2, "E"],
9-
[-2, "F"],
10-
[0, "A"],
11-
[16, "gB"],
12-
[948, "o7B"]
6+
[1, 'C'],
7+
[-1, 'D'],
8+
[2, 'E'],
9+
[-2, 'F'],
10+
[0, 'A'],
11+
[16, 'gB'],
12+
[948, 'o7B']
1313
]
1414

1515
test "encodeVlq tests", ->
@@ -25,31 +25,41 @@ test "SourceMap tests", ->
2525
map.add [3, 0], [3, 4]
2626

2727
testWithFilenames = map.generate {
28-
sourceRoot: ""
29-
sourceFiles: ["source.coffee"]
30-
generatedFile: "source.js"
28+
sourceRoot: ''
29+
sourceFiles: ['source.coffee']
30+
generatedFile: 'source.js'
3131
}
3232

3333
deepEqual testWithFilenames, {
3434
version: 3
35-
file: "source.js"
36-
sourceRoot: ""
37-
sources: ["source.coffee"]
35+
file: 'source.js'
36+
sourceRoot: ''
37+
sources: ['source.coffee']
3838
names: []
39-
mappings: "AAAA;;IACK,GAAC,CAAG;IAET"
39+
mappings: 'AAAA;;IACK,GAAC,CAAG;IAET'
4040
}
4141

4242
deepEqual map.generate(), {
4343
version: 3
44-
file: ""
45-
sourceRoot: ""
46-
sources: [""]
44+
file: ''
45+
sourceRoot: ''
46+
sources: ['<anonymous>']
4747
names: []
48-
mappings: "AAAA;;IACK,GAAC,CAAG;IAET"
48+
mappings: 'AAAA;;IACK,GAAC,CAAG;IAET'
4949
}
5050

5151
# Look up a generated column - should get back the original source position.
5252
arrayEq map.sourceLocation([2,8]), [1,9]
5353

5454
# Look up a point further along on the same line - should get back the same source position.
5555
arrayEq map.sourceLocation([2,10]), [1,9]
56+
57+
test "#3075: v3 source map fields", ->
58+
{ js, v3SourceMap, sourceMap } = CoffeeScript.compile 'console.log Date.now()',
59+
filename: 'tempus_fugit.coffee'
60+
sourceMap: yes
61+
sourceRoot: './www_root/coffee/'
62+
63+
v3SourceMap = JSON.parse v3SourceMap
64+
arrayEq v3SourceMap.sources, ['tempus_fugit.coffee']
65+
eq v3SourceMap.sourceRoot, './www_root/coffee/'

test/support/helpers.coffee

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ exports.eq = (a, b, msg) ->
2929
"Expected #{reset}#{a}#{red} to equal #{reset}#{b}#{red}"
3030

3131
exports.arrayEq = (a, b, msg) ->
32-
ok arrayEgal(a,b), msg or
32+
ok arrayEgal(a, b), msg or
3333
"Expected #{reset}#{a}#{red} to deep equal #{reset}#{b}#{red}"
3434

3535
exports.eqJS = (input, expectedOutput, msg) ->

0 commit comments

Comments
 (0)