Skip to content

Commit

Permalink
Don’t patch Error.prepareStackTrace if --enable-source-maps is us…
Browse files Browse the repository at this point in the history
…ed. (#5403)

Co-authored-by: Geoffrey Booth <[email protected]>
  • Loading branch information
STRd6 and GeoffreyBooth authored Apr 19, 2022
1 parent 76cf769 commit 8d32c71
Show file tree
Hide file tree
Showing 21 changed files with 519 additions and 302 deletions.
2 changes: 1 addition & 1 deletion docs/v2/browser-compiler-legacy/coffeescript.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/v2/browser-compiler-modern/coffeescript.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/v2/index.html

Large diffs are not rendered by default.

120 changes: 120 additions & 0 deletions docs/v2/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -32712,6 +32712,7 @@ <h2>Another heading</h2>
<script type="text/x-coffeescript" class="test" id="sourcemap">
return if global.testingBrowser

{spawn, fork} = require('child_process')
SourceMap = require '../src/sourcemap'

vlqEncodedValues = [
Expand Down Expand Up @@ -32776,6 +32777,125 @@ <h2>Another heading</h2>
arrayEq v3SourceMap.sources, ['tempus_fugit.coffee']
eq v3SourceMap.sourceRoot, './www_root/coffee/'

test "node --enable-source-map built in stack trace mapping", ->
new Promise (resolve, reject) ->
proc = fork './test/importing/error.coffee', [
'--enable-source-maps'
], stdio: 'pipe'

err = ''
proc.stderr.setEncoding 'utf8'
proc.stderr.on 'data', (str) -> err += str
proc.on 'close', ->
try
match = err.match /error\.coffee:(\d+):(\d+)/
throw new Error err unless match

[_, line, column] = match
equal line, 3 # Mapped source line
equal column, 9 # Mapped source column

resolve()
catch exception
reject exception

if Number(process.versions.node.split('.')[0]) >= 14
test "NODE_OPTIONS=--enable-source-maps environment variable stack trace mapping", ->
new Promise (resolve, reject) ->
proc = fork './test/importing/error.coffee', [],
env:
NODE_OPTIONS: '--enable-source-maps'
stdio: 'pipe'

err = ''
proc.stderr.setEncoding 'utf8'
proc.stderr.on 'data', (str) -> err += str
proc.on 'close', ->
try
match = err.match /error\.coffee:(\d+):(\d+)/
throw new Error err unless match

[_, line, column] = match
equal line, 3 # Mapped source line
equal column, 9 # Mapped source column

resolve()
catch exception
reject exception

test "generate correct stack traces with --enable-source-maps from bin/coffee", ->
new Promise (resolve, reject) ->
proc = fork 'test/importing/error.coffee',
['--enable-source-maps'],
stdio: 'pipe'

err = ""
proc.stderr.setEncoding 'utf8'
proc.stderr.on 'data', (str) -> err += str
proc.on 'close', ->
try
match = err.match /error\.coffee:(\d+):(\d+)/
throw new Error err unless match

[_, line, column] = match
equal line, 3 # Mapped source line
equal column, 9 # Mapped source column

resolve()
catch exception
reject exception

test "don't change stack traces if another library has patched `Error.prepareStackTrace`", ->
new Promise (resolve, reject) ->
# This uses `spawn` rather than the preferred `fork` because `fork` requires
# loading code in a separate file. The `--eval` here shows exactly what is
# executing without indirection.
proc = spawn 'node', [
'--eval', """
const patchedPrepareStackTrace = Error.prepareStackTrace = function() {};
require('./register.js');
process.stdout.write(Error.prepareStackTrace === patchedPrepareStackTrace ? 'preserved' : 'overwritten');
"""
]

out = ''
proc.stdout.setEncoding 'utf8'
proc.stdout.on 'data', (str) -> out += str

proc.on 'close', (status) ->
try
equal status, 0
equal out, 'preserved'

resolve()
catch exception
reject exception

test "requiring 'CoffeeScript' doesn't change `Error.prepareStackTrace`", ->
new Promise (resolve, reject) ->
# This uses `spawn` rather than the preferred `fork` because `fork` requires
# loading code in a separate file. The `--eval` here shows exactly what is
# executing without indirection.
proc = spawn 'node', [
'--eval', """
require('./lib/coffeescript/coffeescript.js');
process.stdout.write(Error.prepareStackTrace === undefined ? 'unused' : 'defined');
"""
]

out = ''
proc.stdout.setEncoding 'utf8'
proc.stdout.on 'data', (str) -> out += str

proc.on 'close', (status) ->
try
equal status, 0
equal out, 'unused'

resolve()
catch exception
reject exception

</script>
<script type="text/x-coffeescript" class="test" id="strict">
# Strict Early Errors
Expand Down
12 changes: 10 additions & 2 deletions documentation/site/docs.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ window.gtag 'config', window.GA_TRACKING_ID

# Initialize the CoffeeScript docs interactions
$(document).ready ->
CoffeeScript.patchStackTrace()

# Format dates for the user’s locale, e.g. 'December 24, 2009' or '24 décembre 2009'
$('time').each (index, el) ->
date = el.dateTime or $(el).text()
Expand Down Expand Up @@ -105,11 +107,17 @@ $(document).ready ->
if window.localStorage?
window.localStorage.setItem 'tryCoffeeScriptCode', coffee
catch exception
output = CoffeeScript.compile coffee, bare: yes

js = CoffeeScript.compile coffee, bare: yes, inlineMap: true
# Inline map is added to adjust stack trace line numbers but we strip it from the displayed JS for visual clarity.
output = js.replace /(\n\/\/# [^\n]*){2}$/, ""
lastCompilationElapsedTime = Math.max(200, Date.now() - lastCompilationStartTime)
catch exception
output = "#{exception}"

editors[index + 1].js = js
editors[index + 1].setValue output

gtag 'event', 'edit_code',
event_category: 'engagement'
event_label: $textarea.closest('[data-example]').data('example')
Expand Down Expand Up @@ -149,7 +157,7 @@ $(document).ready ->
run = $(@).data 'run'
index = $("##{$(@).data('example')}-js").data 'index'
js = if editors[index]?
editors[index].getValue()
editors[index].js
else
$(textareas[index]).val()
js = "#{js}\nalert(#{unescape run});" unless run is yes
Expand Down
2 changes: 1 addition & 1 deletion lib/coffeescript-browser-compiler-legacy/coffeescript.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/coffeescript-browser-compiler-modern/coffeescript.js

Large diffs are not rendered by default.

Loading

0 comments on commit 8d32c71

Please sign in to comment.