From fe5ff39ca22033c2c2691b29c6e12004ab69dce4 Mon Sep 17 00:00:00 2001 From: Geoffrey Booth Date: Fri, 1 Sep 2017 01:06:45 -0700 Subject: [PATCH] [CS2] Fix v3 source map (#4671) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 ‘’ --- documentation/sections/nodejs_usage.md | 2 +- lib/coffeescript/sourcemap.js | 5 +-- src/sourcemap.litcoffee | 9 ++++- test/sourcemap.coffee | 46 ++++++++++++++++---------- test/support/helpers.coffee | 2 +- 5 files changed, 41 insertions(+), 23 deletions(-) diff --git a/documentation/sections/nodejs_usage.md b/documentation/sections/nodejs_usage.md index 544485a2..9d2f7c40 100644 --- a/documentation/sections/nodejs_usage.md +++ b/documentation/sections/nodejs_usage.md @@ -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. diff --git a/lib/coffeescript/sourcemap.js b/lib/coffeescript/sourcemap.js index 7cc668ea..892f69ed 100644 --- a/lib/coffeescript/sourcemap.js +++ b/lib/coffeescript/sourcemap.js @@ -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] : ['']; v3 = { version: 3, file: options.generatedFile || '', sourceRoot: options.sourceRoot || '', - sources: options.sourceFiles || [''], + sources: sources, names: [], mappings: buffer }; diff --git a/src/sourcemap.litcoffee b/src/sourcemap.litcoffee index 432fdad9..596ba671 100644 --- a/src/sourcemap.litcoffee +++ b/src/sourcemap.litcoffee @@ -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 + [''] + v3 = version: 3 file: options.generatedFile or '' sourceRoot: options.sourceRoot or '' - sources: options.sourceFiles or [''] + sources: sources names: [] mappings: buffer diff --git a/test/sourcemap.coffee b/test/sourcemap.coffee index e0ec5a99..155778b3 100644 --- a/test/sourcemap.coffee +++ b/test/sourcemap.coffee @@ -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: [''] 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/' diff --git a/test/support/helpers.coffee b/test/support/helpers.coffee index 7f472778..e715eaee 100644 --- a/test/support/helpers.coffee +++ b/test/support/helpers.coffee @@ -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) ->