Skip to content

Commit 8d32c71

Browse files
STRd6GeoffreyBooth
andauthored
Don’t patch Error.prepareStackTrace if --enable-source-maps is used. (#5403)
Co-authored-by: Geoffrey Booth <[email protected]>
1 parent 76cf769 commit 8d32c71

File tree

21 files changed

+519
-302
lines changed

21 files changed

+519
-302
lines changed

docs/v2/browser-compiler-legacy/coffeescript.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/v2/browser-compiler-modern/coffeescript.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/v2/index.html

+2-2
Large diffs are not rendered by default.

docs/v2/test.html

+120
Original file line numberDiff line numberDiff line change
@@ -32712,6 +32712,7 @@ <h2>Another heading</h2>
3271232712
<script type="text/x-coffeescript" class="test" id="sourcemap">
3271332713
return if global.testingBrowser
3271432714

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

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

32780+
test "node --enable-source-map built in stack trace mapping", ->
32781+
new Promise (resolve, reject) ->
32782+
proc = fork './test/importing/error.coffee', [
32783+
'--enable-source-maps'
32784+
], stdio: 'pipe'
32785+
32786+
err = ''
32787+
proc.stderr.setEncoding 'utf8'
32788+
proc.stderr.on 'data', (str) -> err += str
32789+
proc.on 'close', ->
32790+
try
32791+
match = err.match /error\.coffee:(\d+):(\d+)/
32792+
throw new Error err unless match
32793+
32794+
[_, line, column] = match
32795+
equal line, 3 # Mapped source line
32796+
equal column, 9 # Mapped source column
32797+
32798+
resolve()
32799+
catch exception
32800+
reject exception
32801+
32802+
if Number(process.versions.node.split('.')[0]) >= 14
32803+
test "NODE_OPTIONS=--enable-source-maps environment variable stack trace mapping", ->
32804+
new Promise (resolve, reject) ->
32805+
proc = fork './test/importing/error.coffee', [],
32806+
env:
32807+
NODE_OPTIONS: '--enable-source-maps'
32808+
stdio: 'pipe'
32809+
32810+
err = ''
32811+
proc.stderr.setEncoding 'utf8'
32812+
proc.stderr.on 'data', (str) -> err += str
32813+
proc.on 'close', ->
32814+
try
32815+
match = err.match /error\.coffee:(\d+):(\d+)/
32816+
throw new Error err unless match
32817+
32818+
[_, line, column] = match
32819+
equal line, 3 # Mapped source line
32820+
equal column, 9 # Mapped source column
32821+
32822+
resolve()
32823+
catch exception
32824+
reject exception
32825+
32826+
test "generate correct stack traces with --enable-source-maps from bin/coffee", ->
32827+
new Promise (resolve, reject) ->
32828+
proc = fork 'test/importing/error.coffee',
32829+
['--enable-source-maps'],
32830+
stdio: 'pipe'
32831+
32832+
err = ""
32833+
proc.stderr.setEncoding 'utf8'
32834+
proc.stderr.on 'data', (str) -> err += str
32835+
proc.on 'close', ->
32836+
try
32837+
match = err.match /error\.coffee:(\d+):(\d+)/
32838+
throw new Error err unless match
32839+
32840+
[_, line, column] = match
32841+
equal line, 3 # Mapped source line
32842+
equal column, 9 # Mapped source column
32843+
32844+
resolve()
32845+
catch exception
32846+
reject exception
32847+
32848+
test "don't change stack traces if another library has patched `Error.prepareStackTrace`", ->
32849+
new Promise (resolve, reject) ->
32850+
# This uses `spawn` rather than the preferred `fork` because `fork` requires
32851+
# loading code in a separate file. The `--eval` here shows exactly what is
32852+
# executing without indirection.
32853+
proc = spawn 'node', [
32854+
'--eval', """
32855+
const patchedPrepareStackTrace = Error.prepareStackTrace = function() {};
32856+
require('./register.js');
32857+
process.stdout.write(Error.prepareStackTrace === patchedPrepareStackTrace ? 'preserved' : 'overwritten');
32858+
"""
32859+
]
32860+
32861+
out = ''
32862+
proc.stdout.setEncoding 'utf8'
32863+
proc.stdout.on 'data', (str) -> out += str
32864+
32865+
proc.on 'close', (status) ->
32866+
try
32867+
equal status, 0
32868+
equal out, 'preserved'
32869+
32870+
resolve()
32871+
catch exception
32872+
reject exception
32873+
32874+
test "requiring 'CoffeeScript' doesn't change `Error.prepareStackTrace`", ->
32875+
new Promise (resolve, reject) ->
32876+
# This uses `spawn` rather than the preferred `fork` because `fork` requires
32877+
# loading code in a separate file. The `--eval` here shows exactly what is
32878+
# executing without indirection.
32879+
proc = spawn 'node', [
32880+
'--eval', """
32881+
require('./lib/coffeescript/coffeescript.js');
32882+
process.stdout.write(Error.prepareStackTrace === undefined ? 'unused' : 'defined');
32883+
"""
32884+
]
32885+
32886+
out = ''
32887+
proc.stdout.setEncoding 'utf8'
32888+
proc.stdout.on 'data', (str) -> out += str
32889+
32890+
proc.on 'close', (status) ->
32891+
try
32892+
equal status, 0
32893+
equal out, 'unused'
32894+
32895+
resolve()
32896+
catch exception
32897+
reject exception
32898+
3277932899
</script>
3278032900
<script type="text/x-coffeescript" class="test" id="strict">
3278132901
# Strict Early Errors

documentation/site/docs.coffee

+10-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ window.gtag 'config', window.GA_TRACKING_ID
1414

1515
# Initialize the CoffeeScript docs interactions
1616
$(document).ready ->
17+
CoffeeScript.patchStackTrace()
18+
1719
# Format dates for the user’s locale, e.g. 'December 24, 2009' or '24 décembre 2009'
1820
$('time').each (index, el) ->
1921
date = el.dateTime or $(el).text()
@@ -105,11 +107,17 @@ $(document).ready ->
105107
if window.localStorage?
106108
window.localStorage.setItem 'tryCoffeeScriptCode', coffee
107109
catch exception
108-
output = CoffeeScript.compile coffee, bare: yes
110+
111+
js = CoffeeScript.compile coffee, bare: yes, inlineMap: true
112+
# Inline map is added to adjust stack trace line numbers but we strip it from the displayed JS for visual clarity.
113+
output = js.replace /(\n\/\/# [^\n]*){2}$/, ""
109114
lastCompilationElapsedTime = Math.max(200, Date.now() - lastCompilationStartTime)
110115
catch exception
111116
output = "#{exception}"
117+
118+
editors[index + 1].js = js
112119
editors[index + 1].setValue output
120+
113121
gtag 'event', 'edit_code',
114122
event_category: 'engagement'
115123
event_label: $textarea.closest('[data-example]').data('example')
@@ -149,7 +157,7 @@ $(document).ready ->
149157
run = $(@).data 'run'
150158
index = $("##{$(@).data('example')}-js").data 'index'
151159
js = if editors[index]?
152-
editors[index].getValue()
160+
editors[index].js
153161
else
154162
$(textareas[index]).val()
155163
js = "#{js}\nalert(#{unescape run});" unless run is yes

lib/coffeescript-browser-compiler-legacy/coffeescript.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/coffeescript-browser-compiler-modern/coffeescript.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)