| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 0b3fbc8 commit fb2ccb1
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1822,7 +1822,8 @@ function wrapSafe(filename, content, cjsModuleInstance, format) { | |||
| 1822 | 1822 | */ | |
| 1823 | 1823 | Module.prototype._compile = function(content, filename, format) { | |
| 1824 | 1824 | if (format === 'commonjs-typescript' || format === 'module-typescript' || format === 'typescript') { | |
| 1825 | - content = stripTypeScriptModuleTypes(content, filename); | ||
| 1825 | + this[kURL] ??= convertCJSFilenameToURL(filename); | ||
| 1826 | + content = stripTypeScriptModuleTypes(content, filename, this[kURL]); | ||
| 1826 | 1827 | switch (format) { | |
| 1827 | 1828 | case 'commonjs-typescript': { | |
| 1828 | 1829 | format = 'commonjs'; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -175,9 +175,10 @@ function getCachedCodeType(mode, sourceMap) { | |||
| 175 | 175 | * It is used by internal loaders. | |
| 176 | 176 | * @param {string} source TypeScript code to parse. | |
| 177 | 177 | * @param {string} filename The filename of the source code. | |
| 178 | + * @param {string} [sourceURL] The source URL of the source code. If not specified, use filename. | ||
| 178 | 179 | * @returns {TransformOutput} The stripped TypeScript code. | |
| 179 | 180 | */ | |
| 180 | - function stripTypeScriptModuleTypes(source, filename) { | ||
| 181 | + function stripTypeScriptModuleTypes(source, filename, sourceURL) { | ||
| 181 | 182 | assert(typeof source === 'string'); | |
| 182 | 183 | if (isUnderNodeModules(filename)) { | |
| 183 | 184 | throw new ERR_UNSUPPORTED_NODE_MODULES_TYPE_STRIPPING(filename); | |
@@ -203,7 +204,7 @@ function stripTypeScriptModuleTypes(source, filename) { | |||
| 203 | 204 | const options = { | |
| 204 | 205 | mode, | |
| 205 | 206 | sourceMap, | |
| 206 | - filename, | ||
| 207 | + filename: sourceURL ?? filename, | ||
| 207 | 208 | }; | |
| 208 | 209 | ||
| 209 | 210 | const transpiled = processTypeScriptCode(source, options); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,23 +1,61 @@ | |||
| 1 | + // Verifies that type-stripped TypeScript reports a file: URL as its script | ||
| 2 | + // sourceURL to the inspector across all module loading paths, hinting that it | ||
| 3 | + // is a generated source. | ||
| 1 | 4 | 'use strict'; | |
| 2 | 5 | ||
| 3 | 6 | const common = require('../common'); | |
| 7 | + const { describe, it } = require('node:test'); | ||
| 4 | 8 | common.skipIfInspectorDisabled(); | |
| 5 | 9 | if (!process.config.variables.node_use_amaro) common.skip('Requires Amaro'); | |
| 6 | 10 | ||
| 7 | 11 | const { NodeInstance } = require('../common/inspector-helper.js'); | |
| 8 | 12 | const fixtures = require('../common/fixtures'); | |
| 9 | 13 | const assert = require('assert'); | |
| 10 | - const { pathToFileURL } = require('url'); | ||
| 11 | 14 | ||
| 12 | - const scriptPath = fixtures.path('typescript/ts/test-typescript.ts'); | ||
| 13 | - const scriptURL = pathToFileURL(scriptPath); | ||
| 14 | - | ||
| 15 | - async function runTest() { | ||
| 16 | - const child = new NodeInstance( | ||
| 17 | - ['--inspect-brk=0'], | ||
| 18 | - undefined, | ||
| 19 | - scriptPath); | ||
| 15 | + const scenarios = [ | ||
| 16 | + { | ||
| 17 | + // CommonJS: a .ts entry point. | ||
| 18 | + entry: 'typescript/ts/test-typescript.ts', | ||
| 19 | + expected: [ | ||
| 20 | + fixtures.fileURL('typescript/ts/test-typescript.ts').href, | ||
| 21 | + ], | ||
| 22 | + }, | ||
| 23 | + { | ||
| 24 | + // CommonJS: a .ts entry that require()s a .cts dependency. | ||
| 25 | + entry: 'typescript/ts/test-require-cts.ts', | ||
| 26 | + expected: [ | ||
| 27 | + fixtures.fileURL('typescript/ts/test-require-cts.ts').href, | ||
| 28 | + fixtures.fileURL('typescript/cts/test-cts-export-foo.cts').href, | ||
| 29 | + ], | ||
| 30 | + }, | ||
| 31 | + { | ||
| 32 | + // CommonJS: a .ts entry that require()s a .mts dependency. | ||
| 33 | + entry: 'typescript/ts/test-require-mts.ts', | ||
| 34 | + expected: [ | ||
| 35 | + fixtures.fileURL('typescript/ts/test-require-mts.ts').href, | ||
| 36 | + fixtures.fileURL('typescript/mts/test-mts-export-foo.mts').href, | ||
| 37 | + ], | ||
| 38 | + }, | ||
| 39 | + { | ||
| 40 | + // ESM: a .mts entry that imports a .ts dependency. | ||
| 41 | + entry: 'typescript/mts/test-import-ts-file.mts', | ||
| 42 | + expected: [ | ||
| 43 | + fixtures.fileURL('typescript/mts/test-import-ts-file.mts').href, | ||
| 44 | + fixtures.fileURL('typescript/mts/test-module-export.ts').href, | ||
| 45 | + ], | ||
| 46 | + }, | ||
| 47 | + { | ||
| 48 | + // ESM: a .mts entry that imports a .cts dependency. | ||
| 49 | + entry: 'typescript/mts/test-import-commonjs.mts', | ||
| 50 | + expected: [ | ||
| 51 | + fixtures.fileURL('typescript/mts/test-import-commonjs.mts').href, | ||
| 52 | + fixtures.fileURL('typescript/cts/test-cts-export-foo.cts').href, | ||
| 53 | + ], | ||
| 54 | + }, | ||
| 55 | + ]; | ||
| 20 | 56 | ||
| 57 | + async function collectParsedScripts(scriptPath) { | ||
| 58 | + const child = new NodeInstance(['--inspect-brk=0'], undefined, scriptPath); | ||
| 21 | 59 | const session = await child.connectInspectorSession(); | |
| 22 | 60 | ||
| 23 | 61 | await session.send({ method: 'NodeRuntime.enable' }); | |
@@ -29,18 +67,39 @@ async function runTest() { | |||
| 29 | 67 | ]); | |
| 30 | 68 | await session.send({ method: 'NodeRuntime.disable' }); | |
| 31 | 69 | ||
| 32 | - const scriptParsed = await session.waitForNotification((notification) => { | ||
| 33 | - if (notification.method !== 'Debugger.scriptParsed') return false; | ||
| 34 | - | ||
| 35 | - return notification.params.url === scriptPath || notification.params.url === scriptURL.href; | ||
| 70 | + // Collect every parsed script while resuming through the break on start and | ||
| 71 | + // any later pauses, until the main execution context is destroyed. | ||
| 72 | + const scripts = new Map(); | ||
| 73 | + await session.waitForNotification((notification) => { | ||
| 74 | + if (notification.method === 'Debugger.scriptParsed') { | ||
| 75 | + const { url } = notification.params; | ||
| 76 | + if (!url.startsWith('node:')) { | ||
| 77 | + scripts.set(url, notification.params); | ||
| 78 | + } | ||
| 79 | + } | ||
| 80 | + if (notification.method === 'Debugger.paused') { | ||
| 81 | + session.send({ method: 'Debugger.resume' }); | ||
| 82 | + } | ||
| 83 | + return notification.method === 'Runtime.executionContextDestroyed' && | ||
| 84 | + notification.params.executionContextId === 1; | ||
| 36 | 85 | }); | |
| 37 | - // Verify that the script has a sourceURL, hinting that it is a generated source. | ||
| 38 | - assert(scriptParsed.params.hasSourceURL || common.isInsideDirWithUnusualChars); | ||
| 39 | - | ||
| 40 | - await session.waitForPauseOnStart(); | ||
| 41 | - await session.runToCompletion(); | ||
| 86 | + await session.waitForDisconnect(); | ||
| 42 | 87 | ||
| 43 | 88 | assert.strictEqual((await child.expectShutdown()).exitCode, 0); | |
| 89 | + return scripts; | ||
| 44 | 90 | } | |
| 45 | 91 | ||
| 46 | - runTest().then(common.mustCall()); | ||
| 92 | + describe('type stripping source URLs', { concurrency: !process.env.TEST_PARALLEL }, () => { | ||
| 93 | + for (const { entry, expected } of scenarios) { | ||
| 94 | + it(entry, async () => { | ||
| 95 | + const scripts = await collectParsedScripts(fixtures.path(entry)); | ||
| 96 | + for (const href of expected) { | ||
| 97 | + const params = scripts.get(href); | ||
| 98 | + assert(params, | ||
| 99 | + `expected ${entry} to report ${href} to the inspector, ` + | ||
| 100 | + `got:\n- ${[...scripts.keys()].join('\n- ')}`); | ||
| 101 | + assert(params.hasSourceURL); | ||
| 102 | + } | ||
| 103 | + }); | ||
| 104 | + } | ||
| 105 | + }); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments