From 353a399b3ed9fa192090dd2d1dd27f2adfa79d2b Mon Sep 17 00:00:00 2001 From: Geoffrey Booth Date: Wed, 30 Aug 2017 00:12:03 -0700 Subject: [PATCH 1/5] Per discussion in #3075: if `sourceFiles` is unspecified, but `filename` is, use `filename`; output null instead of an empty string for `sources` or `sourceRoot` --- lib/coffeescript/sourcemap.js | 9 +++++---- src/sourcemap.litcoffee | 13 ++++++++++--- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/lib/coffeescript/sourcemap.js b/lib/coffeescript/sourcemap.js index 7cc668eaa2..3851520ace 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 || [''], + file: options.generatedFile || null, + sourceRoot: options.sourceRoot || null, + sources: sources, names: [], mappings: buffer }; diff --git a/src/sourcemap.litcoffee b/src/sourcemap.litcoffee index 432fdad94a..d13186c8a2 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 [''] + file: options.generatedFile or null + sourceRoot: options.sourceRoot or null + sources: sources names: [] mappings: buffer From 6527f550a791cca2844290e2f51bd322137025da Mon Sep 17 00:00:00 2001 From: Geoffrey Booth Date: Wed, 30 Aug 2017 00:37:50 -0700 Subject: [PATCH 2/5] Update source map tests to reflect that now we return null instead of empty strings; check generated sources array --- test/sourcemap.coffee | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/test/sourcemap.coffee b/test/sourcemap.coffee index e0ec5a99ed..33afcc6d0a 100644 --- a/test/sourcemap.coffee +++ b/test/sourcemap.coffee @@ -33,7 +33,7 @@ test "SourceMap tests", -> deepEqual testWithFilenames, { version: 3 file: "source.js" - sourceRoot: "" + sourceRoot: null sources: ["source.coffee"] names: [] mappings: "AAAA;;IACK,GAAC,CAAG;IAET" @@ -41,9 +41,9 @@ test "SourceMap tests", -> deepEqual map.generate(), { version: 3 - file: "" - sourceRoot: "" - sources: [""] + file: null + sourceRoot: null + sources: [] names: [] mappings: "AAAA;;IACK,GAAC,CAAG;IAET" } @@ -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/' From 6f126e6dadf346bad7d9575e2f56c4ad2c5e442e Mon Sep 17 00:00:00 2001 From: Geoffrey Booth Date: Wed, 30 Aug 2017 00:39:25 -0700 Subject: [PATCH 3/5] Update source map documentation; still leave more obscure options undocumented --- documentation/sections/nodejs_usage.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/documentation/sections/nodejs_usage.md b/documentation/sections/nodejs_usage.md index 544485a2e3..9d2f7c40f3 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. From af833683cf30fbfe88adb758dbaaa87fe2a33879 Mon Sep 17 00:00:00 2001 From: Geoffrey Booth Date: Thu, 31 Aug 2017 19:14:14 -0700 Subject: [PATCH 4/5] =?UTF-8?q?Follow=20the=20TypeScript=20compiler?= =?UTF-8?q?=E2=80=99s=20example=20regarding=20v3SourceMap,=20but=20output?= =?UTF-8?q?=20empty=20strings=20instead=20of=20made-up=20filenames?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/coffeescript/sourcemap.js | 6 +++--- src/sourcemap.litcoffee | 6 +++--- test/sourcemap.coffee | 36 +++++++++++++++++------------------ test/support/helpers.coffee | 2 +- 4 files changed, 25 insertions(+), 25 deletions(-) diff --git a/lib/coffeescript/sourcemap.js b/lib/coffeescript/sourcemap.js index 3851520ace..aba3219864 100644 --- a/lib/coffeescript/sourcemap.js +++ b/lib/coffeescript/sourcemap.js @@ -141,11 +141,11 @@ } } // Produce the canonical JSON object format for a "v3" source map. - sources = options.sourceFiles ? options.sourceFiles : options.filename ? [options.filename] : []; + sources = options.sourceFiles ? options.sourceFiles : options.filename ? [options.filename] : ['']; v3 = { version: 3, - file: options.generatedFile || null, - sourceRoot: options.sourceRoot || null, + file: options.generatedFile || '', + sourceRoot: options.sourceRoot || '', sources: sources, names: [], mappings: buffer diff --git a/src/sourcemap.litcoffee b/src/sourcemap.litcoffee index d13186c8a2..3a8470eb1b 100644 --- a/src/sourcemap.litcoffee +++ b/src/sourcemap.litcoffee @@ -123,12 +123,12 @@ Produce the canonical JSON object format for a "v3" source map. else if options.filename [options.filename] else - [] + [''] v3 = version: 3 - file: options.generatedFile or null - sourceRoot: options.sourceRoot or null + file: options.generatedFile or '' + sourceRoot: options.sourceRoot or '' sources: sources names: [] mappings: buffer diff --git a/test/sourcemap.coffee b/test/sourcemap.coffee index 33afcc6d0a..e511e07693 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: null - 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: null - sourceRoot: null - 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. diff --git a/test/support/helpers.coffee b/test/support/helpers.coffee index 7f47277827..e715eaee1b 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) -> From f3458c16948c18aed68ef74b0210ac71be9eb7ed Mon Sep 17 00:00:00 2001 From: Geoffrey Booth Date: Fri, 1 Sep 2017 00:56:36 -0700 Subject: [PATCH 5/5] =?UTF-8?q?Have=20`sources`=20default=20to=20=E2=80=98?= =?UTF-8?q?=E2=80=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/coffeescript/sourcemap.js | 2 +- src/sourcemap.litcoffee | 2 +- test/sourcemap.coffee | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/coffeescript/sourcemap.js b/lib/coffeescript/sourcemap.js index aba3219864..892f69edfb 100644 --- a/lib/coffeescript/sourcemap.js +++ b/lib/coffeescript/sourcemap.js @@ -141,7 +141,7 @@ } } // Produce the canonical JSON object format for a "v3" source map. - sources = options.sourceFiles ? options.sourceFiles : options.filename ? [options.filename] : ['']; + sources = options.sourceFiles ? options.sourceFiles : options.filename ? [options.filename] : ['']; v3 = { version: 3, file: options.generatedFile || '', diff --git a/src/sourcemap.litcoffee b/src/sourcemap.litcoffee index 3a8470eb1b..596ba67153 100644 --- a/src/sourcemap.litcoffee +++ b/src/sourcemap.litcoffee @@ -123,7 +123,7 @@ Produce the canonical JSON object format for a "v3" source map. else if options.filename [options.filename] else - [''] + [''] v3 = version: 3 diff --git a/test/sourcemap.coffee b/test/sourcemap.coffee index e511e07693..155778b3bf 100644 --- a/test/sourcemap.coffee +++ b/test/sourcemap.coffee @@ -43,7 +43,7 @@ test "SourceMap tests", -> version: 3 file: '' sourceRoot: '' - sources: [''] + sources: [''] names: [] mappings: 'AAAA;;IACK,GAAC,CAAG;IAET' }