Skip to content

Commit 4aaa755

Browse files
abhayagarwal-devGeoffreyBoothCopilot
authored
fix: Node 22 compatibility for source maps and import assertions (#5503)
Co-authored-by: Geoffrey Booth <[email protected]> Co-authored-by: Copilot <[email protected]>
1 parent 817c39a commit 4aaa755

11 files changed

Lines changed: 62 additions & 18 deletions

File tree

‎.github/workflows/continuous-integration.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
strategy:
1212
matrix:
1313
operating-system: [ubuntu-latest, macos-latest, windows-latest]
14-
node-version: [12.x, 14.x, 16.x, 18.x, 20.x]
14+
node-version: [16.x, 18.x, 20.x, 22.x, 24.x, 26.x]
1515

1616
steps:
1717
- uses: actions/checkout@v3

‎Cakefile‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ runTests = (CoffeeScript) ->
477477
skipUnless '/foo.bar/s.test("foo\tbar")', ['regex_dotall.coffee']
478478
skipUnless '1_2_3', ['numeric_literal_separators.coffee']
479479
skipUnless '1n', ['numbers_bigint.coffee']
480-
skipUnless 'async () => { await import(\'data:application/json,{"foo":"bar"}\', { assert: { type: "json" } }) }', ['import_assertions.coffee']
480+
skipUnless 'async () => { await import(\'data:application/json,{"foo":"bar"}\', { with: { type: "json" } }) }', ['import_assertions.coffee']
481481
files = fs.readdirSync('test').filter (filename) ->
482482
filename not in testFilesToSkip
483483

@@ -519,7 +519,17 @@ task 'test:browser', 'run the test suite against the modern browser compiler in
519519

520520
server.listen 8080, ->
521521
puppeteer = require 'puppeteer'
522-
browser = await puppeteer.launch()
522+
launchOptions = {}
523+
if process.env.CI
524+
if process.platform is 'linux'
525+
launchOptions.args = ['--no-sandbox', '--disable-setuid-sandbox']
526+
launchOptions.executablePath = '/usr/bin/google-chrome'
527+
else if process.platform is 'darwin' and process.arch is 'arm64'
528+
launchOptions.executablePath = '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'
529+
else if process.platform is 'win32'
530+
launchOptions.executablePath = 'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe'
531+
532+
browser = await puppeteer.launch launchOptions
523533
page = await browser.newPage()
524534
result = ""
525535

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

Lines changed: 1 addition & 1 deletion
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‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎docs/v2/test.html‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25716,9 +25716,15 @@ <h1>CoffeeScript Test Suite</h1>
2571625716
</script>
2571725717
<script type="text/x-coffeescript" class="test" id="import_assertions">
2571825718
# This file is running in CommonJS (in Node) or as a classic Script (in the browser tests) so it can use import() within an async function, but not at the top level; and we can’t use static import.
25719+
importJson = (url) ->
25720+
try
25721+
await import(url, { with: { type: 'json' } })
25722+
catch
25723+
await import(url, { assert: { type: 'json' } })
25724+
2571925725
test "dynamic import assertion", ->
2572025726
try
25721-
{ default: secret } = await import('data:application/json,{"ofLife":42}', { assert: { type: 'json' } })
25727+
{ default: secret } = await importJson('data:application/json,{"ofLife":42}')
2572225728
eq secret.ofLife, 42
2572325729
catch exception
2572425730
# This parses on Node 16.14.x but throws an error because JSON modules aren’t unflagged there yet; remove this try/catch once the unflagging of `--experimental-json-modules` is backported (see https://github.com/nodejs/node/pull/41736#issuecomment-1086738670)
@@ -25729,7 +25735,7 @@ <h1>CoffeeScript Test Suite</h1>
2572925735
assert = 1
2573025736

2573125737
try
25732-
{ default: assert } = await import('data:application/json,{"thatIAm":42}', { assert: { type: 'json' } })
25738+
{ default: assert } = await importJson('data:application/json,{"thatIAm":42}')
2573325739
eq assert.thatIAm, 42
2573425740
catch exception
2573525741
# This parses on Node 16.14.x but throws an error because JSON modules aren’t unflagged there yet; remove this try/catch once the unflagging of `--experimental-json-modules` is backported (see https://github.com/nodejs/node/pull/41736#issuecomment-1086738670)
@@ -32915,10 +32921,13 @@ <h2>Another heading</h2>
3291532921
# This uses `spawn` rather than the preferred `fork` because `fork` requires
3291632922
# loading code in a separate file. The `--eval` here shows exactly what is
3291732923
# executing without indirection.
32924+
# Note: Node 12+ may pre-set Error.prepareStackTrace internally; we check that
32925+
# requiring coffeescript.js leaves it *unchanged*, not that it stays undefined.
3291832926
proc = spawn 'node', [
3291932927
'--eval', """
32928+
var before = Error.prepareStackTrace;
3292032929
require('./lib/coffeescript/coffeescript.js');
32921-
process.stdout.write(Error.prepareStackTrace === undefined ? 'unused' : 'defined');
32930+
process.stdout.write(Error.prepareStackTrace === before ? 'unchanged' : 'changed');
3292232931
"""
3292332932
]
3292432933

@@ -32929,12 +32938,13 @@ <h2>Another heading</h2>
3292932938
proc.on 'close', (status) ->
3293032939
try
3293132940
equal status, 0
32932-
equal out, 'unused'
32941+
equal out, 'unchanged'
3293332942

3293432943
resolve()
3293532944
catch exception
3293632945
reject exception
3293732946

32947+
3293832948
</script>
3293932949
<script type="text/x-coffeescript" class="test" id="strict">
3294032950
# Strict Early Errors

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

Lines changed: 1 addition & 1 deletion
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‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎lib/coffeescript/register.js‎

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

‎src/register.coffee‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,14 @@ nodeSourceMapsSupportEnabled = process? and (
1111
process.env.NODE_OPTIONS?.includes('--enable-source-maps')
1212
)
1313

14-
unless Error.prepareStackTrace or nodeSourceMapsSupportEnabled
14+
# Node 22+ sets its own internal `Error.prepareStackTrace` (named
15+
# `ErrorPrepareStackTrace`) before any user code runs. Treat that built-in
16+
# handler the same as "not set" so CoffeeScript can still install its own
17+
# source-map translator.
18+
isNodeInternalHandler = Error.prepareStackTrace and
19+
/^\s*function ErrorPrepareStackTrace/.test Error.prepareStackTrace.toString()
20+
21+
unless (Error.prepareStackTrace and not isNodeInternalHandler) or nodeSourceMapsSupportEnabled
1522
cacheSourceMaps = true
1623
patchStackTrace()
1724

@@ -71,3 +78,4 @@ if child_process
7178
# Utility function to find the `options` object attached to the topmost module.
7279
getRootModule = (module) ->
7380
if module.parent then getRootModule module.parent else module
81+

‎test/import_assertions.coffee‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
# This file is running in CommonJS (in Node) or as a classic Script (in the browser tests) so it can use import() within an async function, but not at the top level; and we can’t use static import.
2+
importJson = (url) ->
3+
try
4+
await import(url, { with: { type: 'json' } })
5+
catch
6+
await import(url, { assert: { type: 'json' } })
7+
28
test "dynamic import assertion", ->
39
try
4-
{ default: secret } = await import('data:application/json,{"ofLife":42}', { assert: { type: 'json' } })
10+
{ default: secret } = await importJson('data:application/json,{"ofLife":42}')
511
eq secret.ofLife, 42
612
catch exception
713
# This parses on Node 16.14.x but throws an error because JSON modules aren’t unflagged there yet; remove this try/catch once the unflagging of `--experimental-json-modules` is backported (see https://github.com/nodejs/node/pull/41736#issuecomment-1086738670)
@@ -12,7 +18,7 @@ test "assert keyword", ->
1218
assert = 1
1319

1420
try
15-
{ default: assert } = await import('data:application/json,{"thatIAm":42}', { assert: { type: 'json' } })
21+
{ default: assert } = await importJson('data:application/json,{"thatIAm":42}')
1622
eq assert.thatIAm, 42
1723
catch exception
1824
# This parses on Node 16.14.x but throws an error because JSON modules aren’t unflagged there yet; remove this try/catch once the unflagging of `--experimental-json-modules` is backported (see https://github.com/nodejs/node/pull/41736#issuecomment-1086738670)

0 commit comments

Comments
 (0)