From 1c1c76c35d99b4f7d17728ad37eb8ed9448a259f Mon Sep 17 00:00:00 2001 From: Louis Ye Date: Wed, 16 Jun 2021 11:21:12 -0400 Subject: [PATCH 1/8] fix: Add debugging information for sourcemapper --- src/agent/debuglet.ts | 3 +- src/agent/io/sourcemapper.ts | 43 +++++++++++++++------ test/test-circular.ts | 2 +- test/test-duplicate-expressions.ts | 2 +- test/test-duplicate-nested-expressions.ts | 2 +- test/test-evaluated-expressions.ts | 2 +- test/test-expression-side-effect.ts | 2 +- test/test-fat-arrow.ts | 2 +- test/test-max-data-size.ts | 2 +- test/test-sourcemapper.ts | 46 +++++++++++++++++++---- test/test-this-context.ts | 2 +- test/test-try-catch.ts | 2 +- test/test-v8debugapi.ts | 6 +-- 13 files changed, 84 insertions(+), 32 deletions(-) diff --git a/src/agent/debuglet.ts b/src/agent/debuglet.ts index 691b58fe..da3ca1ec 100644 --- a/src/agent/debuglet.ts +++ b/src/agent/debuglet.ts @@ -396,12 +396,13 @@ export class Debuglet extends EventEmitter { let mapper; try { - mapper = await SourceMapper.create(findResults.mapFiles); + mapper = await SourceMapper.create(findResults.mapFiles, that.logger); } catch (err3) { that.logger.error('Error processing the sourcemaps.', err3); that.emit('initError', err3); return; } + that.v8debug = debugapi.create( that.logger, that.config, diff --git a/src/agent/io/sourcemapper.ts b/src/agent/io/sourcemapper.ts index df58aec4..92319656 100644 --- a/src/agent/io/sourcemapper.ts +++ b/src/agent/io/sourcemapper.ts @@ -18,6 +18,7 @@ import * as path from 'path'; import {promisify} from 'util'; import * as sourceMap from 'source-map'; +import {Logger} from '../config'; import {findScriptsFuzzy} from '../util/utils'; const CONCURRENCY = 10; @@ -167,17 +168,10 @@ async function processSourcemap( } export class SourceMapper { + /** Maps each original source path to the corresponding source map info. */ infoMap: Map; - /** - * @param {Array.} sourcemapPaths An array of paths to .map sourcemap - * files that should be processed. The paths should be relative to the - * current process's current working directory - * @param {Logger} logger A logger that reports errors that occurred while - * processing the given sourcemap files - * @constructor - */ - constructor() { + constructor(readonly logger: Logger) { this.infoMap = new Map(); } @@ -211,6 +205,8 @@ export class SourceMapper { Array.from(this.infoMap.keys()) ); + this.logger.debug(`sourcemapper fuzzy matches: ${matches}`); + if (matches.length === 1) { return this.infoMap.get(matches[0]) as MapInfoInput; } @@ -246,6 +242,8 @@ export class SourceMapper { colNumber: number, entry: MapInfoInput ): MapInfoOutput | null { + this.logger.debug(`sourcemapper inputPath: ${inputPath}`); + inputPath = path.normalize(inputPath); const relPath = path @@ -273,6 +271,8 @@ export class SourceMapper { column: colNumber, // to be zero-based }; + this.logger.debug(`sourcemapper sourcePos: ${JSON.stringify(sourcePos)}`); + const allPos = entry.mapConsumer.allGeneratedPositionsFor(sourcePos); /* * Based on testing, it appears that the following code is needed to @@ -290,6 +290,8 @@ export class SourceMapper { }) : entry.mapConsumer.generatedPositionFor(sourcePos); + this.logger.debug(`sourcemapper mappedPos: ${JSON.stringify(mappedPos)}`); + return { file: entry.outputFile, line: (mappedPos.line ?? 0) - 1, // convert the one-based line numbers returned @@ -305,11 +307,29 @@ export class SourceMapper { // output }; } + + /** Prints the debugging information of the source mapper to the logger. */ + debug() { + this.logger.debug('Printing source mapper debugging information ...'); + for (const [key, value] of this.infoMap) { + this.logger.debug(` source ${key}:`); + this.logger.debug(` outputFile: ${value.outputFile}`); + this.logger.debug(` mapFile: ${value.mapFile}`); + this.logger.debug(` sources: ${value.sources}`); + } + } } -export async function create(sourcemapPaths: string[]): Promise { +/** + * @param {Array.} sourcemapPaths An array of paths to .map sourcemap + * files that should be processed. The paths should be relative to the + * current process's current working directory + * @param {Logger} logger A logger that reports errors that occurred while + * processing the given sourcemap files + */ +export async function create(sourcemapPaths: string[], logger: Logger): Promise { const limit = pLimit(CONCURRENCY); - const mapper = new SourceMapper(); + const mapper = new SourceMapper(logger); const promises = sourcemapPaths.map(path => limit(() => processSourcemap(mapper.infoMap, path)) ); @@ -320,5 +340,6 @@ export async function create(sourcemapPaths: string[]): Promise { 'An error occurred while processing the sourcemap files' + err ); } + mapper.debug(); return mapper; } diff --git a/test/test-circular.ts b/test/test-circular.ts index a6bcb93f..2717cacc 100644 --- a/test/test-circular.ts +++ b/test/test-circular.ts @@ -71,7 +71,7 @@ maybeDescribe(__filename, () => { assert.strictEqual(fileStats.errors().size, 0); const jsStats = fileStats.selectStats(/.js$/); const mapFiles = fileStats.selectFiles(/.map$/, process.cwd()); - const mapper = await SourceMapper.create(mapFiles); + const mapper = await SourceMapper.create(mapFiles, logger); api = debugapi.create( logger, config, diff --git a/test/test-duplicate-expressions.ts b/test/test-duplicate-expressions.ts index 7423473d..a88aa2b2 100644 --- a/test/test-duplicate-expressions.ts +++ b/test/test-duplicate-expressions.ts @@ -63,7 +63,7 @@ describe(__filename, () => { assert.strictEqual(fileStats.errors().size, 0); const jsStats = fileStats.selectStats(/.js$/); const mapFiles = fileStats.selectFiles(/.map$/, process.cwd()); - const mapper = await SourceMapper.create(mapFiles); + const mapper = await SourceMapper.create(mapFiles, logger); // TODO: Handle the case when mapper is undefined // TODO: Handle the case when v8debugapi.create returns null api = debugapi.create( diff --git a/test/test-duplicate-nested-expressions.ts b/test/test-duplicate-nested-expressions.ts index 978475df..99f20906 100644 --- a/test/test-duplicate-nested-expressions.ts +++ b/test/test-duplicate-nested-expressions.ts @@ -58,7 +58,7 @@ describe(__filename, () => { assert.strictEqual(fileStats.errors().size, 0); const jsStats = fileStats.selectStats(/.js$/); const mapFiles = fileStats.selectFiles(/.map$/, process.cwd()); - const mapper = await SourceMapper.create(mapFiles); + const mapper = await SourceMapper.create(mapFiles, logger); // TODO: Handle the case when mapper is undefined // TODO: Handle the case when v8debugapi.create returns null api = debugapi.create( diff --git a/test/test-evaluated-expressions.ts b/test/test-evaluated-expressions.ts index 40b8e96e..b6fe07ee 100644 --- a/test/test-evaluated-expressions.ts +++ b/test/test-evaluated-expressions.ts @@ -41,7 +41,7 @@ describe('debugger provides useful information', () => { scanner.scan(config.workingDirectory, /\.js$/).then(async fileStats => { const jsStats = fileStats.selectStats(/\.js$/); const mapFiles = fileStats.selectFiles(/\.map$/, process.cwd()); - const mapper = await SourceMapper.create(mapFiles); + const mapper = await SourceMapper.create(mapFiles, logger); assert(mapper); api = debugapi.create(logger, config, jsStats, mapper!); done(); diff --git a/test/test-expression-side-effect.ts b/test/test-expression-side-effect.ts index efb0ffb2..7c6abe3b 100644 --- a/test/test-expression-side-effect.ts +++ b/test/test-expression-side-effect.ts @@ -43,7 +43,7 @@ describe('evaluating expressions', () => { scanner.scan(config.workingDirectory, /\.js$/).then(async fileStats => { const jsStats = fileStats.selectStats(/\.js$/); const mapFiles = fileStats.selectFiles(/\.map$/, process.cwd()); - const mapper = await SourceMapper.create(mapFiles); + const mapper = await SourceMapper.create(mapFiles, logger); assert(mapper); api = debugapi.create(logger, config, jsStats, mapper!); done(); diff --git a/test/test-fat-arrow.ts b/test/test-fat-arrow.ts index 3e97e074..c8786406 100644 --- a/test/test-fat-arrow.ts +++ b/test/test-fat-arrow.ts @@ -60,7 +60,7 @@ describe(__filename, () => { const jsStats = fileStats.selectStats(/.js$/); const mapFiles = fileStats.selectFiles(/.map$/, process.cwd()); // TODO: Determine if the err parameter should be used. - const mapper = await SourceMapper.create(mapFiles); + const mapper = await SourceMapper.create(mapFiles, logger); // TODO: Handle the case when mapper is undefined // TODO: Handle the case when v8debugapi.create returns null api = debugapi.create( diff --git a/test/test-max-data-size.ts b/test/test-max-data-size.ts index 2d105ba1..f622930d 100644 --- a/test/test-max-data-size.ts +++ b/test/test-max-data-size.ts @@ -47,7 +47,7 @@ describe('maxDataSize', () => { assert.strictEqual(fileStats.errors().size, 0); const jsStats = fileStats.selectStats(/.js$/); const mapFiles = fileStats.selectFiles(/.map$/, process.cwd()); - const mapper = await SourceMapper.create(mapFiles); + const mapper = await SourceMapper.create(mapFiles, logger); // TODO: Handle the case when mapper is undefined // TODO: Handle the case when v8debugapi.create returns null api = debugapi.create( diff --git a/test/test-sourcemapper.ts b/test/test-sourcemapper.ts index 0fdf738d..cf800a69 100644 --- a/test/test-sourcemapper.ts +++ b/test/test-sourcemapper.ts @@ -20,6 +20,8 @@ import * as path from 'path'; import * as sm from '../src/agent/io/sourcemapper'; +import {MockLogger} from './mock-logger'; + const BASE_PATH = path.join(__dirname, 'fixtures', 'sourcemapper'); const QUICK_MILLISECONDS = 300; @@ -52,16 +54,28 @@ function testTool( const outputFilePath = path.join(BASE_PATH, relativeOutputFilePath); describe('sourcemapper for tool ' + tool, () => { + const logger = new MockLogger(); let sourcemapper: sm.SourceMapper; - it('for tool ' + tool, async () => { - const start = Date.now(); - sourcemapper = await sm.create([mapFilePath]); - assert( - Date.now() - start < QUICK_MILLISECONDS, - 'should create the SourceMapper quickly' - ); - }); + it('for tool ' + tool + ' sourcemapper should be created correctly', + async () => { + const start = Date.now(); + sourcemapper = await sm.create([mapFilePath], logger); + assert( + Date.now() - start < QUICK_MILLISECONDS, + 'should create the SourceMapper quickly'); + + // Verify if the debugging information is correctly printed. + assert.notStrictEqual( + logger.debugs[0].args[0].indexOf('debugging information ...'), -1); + assert.notStrictEqual(logger.debugs[1].args[0].indexOf('source '), -1); + assert.notStrictEqual( + logger.debugs[2].args[0].indexOf('outputFile'), -1); + assert.notStrictEqual(logger.debugs[3].args[0].indexOf('mapFile'), -1); + assert.notStrictEqual(logger.debugs[4].args[0].indexOf('sources'), -1); + assert.strictEqual( + logger.debugs.length, sourcemapper.infoMap.size * 4 + 1); + }); it( 'for tool ' + @@ -125,6 +139,22 @@ function testTool( 0, mapInfoInput! ); + + // Verify if the debugging information is correctly printed. + const debugsLength = logger.debugs.length; + assert.notStrictEqual( + logger.debugs[debugsLength - 3].args[0].indexOf( + 'sourcemapper inputPath:'), + -1); + assert.notStrictEqual( + logger.debugs[debugsLength - 2].args[0].indexOf( + 'sourcemapper sourcePos: {'), + -1); + assert.notStrictEqual( + logger.debugs[debugsLength - 1].args[0].indexOf( + 'sourcemapper mappedPos: {'), + -1); + assert.notStrictEqual( info, null, diff --git a/test/test-this-context.ts b/test/test-this-context.ts index ea49e2b0..a9991352 100644 --- a/test/test-this-context.ts +++ b/test/test-this-context.ts @@ -58,7 +58,7 @@ describe(__filename, () => { assert.strictEqual(fileStats.errors().size, 0); const jsStats = fileStats.selectStats(/.js$/); const mapFiles = fileStats.selectFiles(/.map$/, process.cwd()); - const mapper = await SourceMapper.create(mapFiles); + const mapper = await SourceMapper.create(mapFiles, logger); // TODO: Handle the case when mapper is undefined // TODO: Handle the case when v8debugapi.create returns null api = debugapi.create( diff --git a/test/test-try-catch.ts b/test/test-try-catch.ts index 053ffbd8..d17495d5 100644 --- a/test/test-try-catch.ts +++ b/test/test-try-catch.ts @@ -57,7 +57,7 @@ describe(__filename, () => { assert.strictEqual(fileStats.errors().size, 0); const jsStats = fileStats.selectStats(/.js$/); const mapFiles = fileStats.selectFiles(/.map$/, process.cwd()); - const mapper = await SourceMapper.create(mapFiles); + const mapper = await SourceMapper.create(mapFiles, logger); // TODO: Handle the case when mapper is undefined // TODO: Handle the case when v8debugapi.create returns null api = debugapi.create( diff --git a/test/test-v8debugapi.ts b/test/test-v8debugapi.ts index db771b42..47c26e09 100644 --- a/test/test-v8debugapi.ts +++ b/test/test-v8debugapi.ts @@ -178,7 +178,7 @@ describe('debugapi selection', () => { assert.strictEqual(fileStats.errors().size, 0); const jsStats = fileStats.selectStats(/.js$/); const mapFiles = fileStats.selectFiles(/.js.map$/, process.cwd()); - const mapper = await SourceMapper.create(mapFiles); + const mapper = await SourceMapper.create(mapFiles, logger); // TODO(dominickramer): Handle the case when mapper is undefined. // TODO(dominickramer): Handle the case when v8debugapi.create // returns null @@ -222,7 +222,7 @@ describeFn('debugapi selection on Node >=10', () => { assert.strictEqual(fileStats.errors().size, 0); const jsStats = fileStats.selectStats(/.js$/); const mapFiles = fileStats.selectFiles(/.js.map$/, process.cwd()); - const mapper = await SourceMapper.create(mapFiles); + const mapper = await SourceMapper.create(mapFiles, logger); assert(mapper); api = debugapi.create(logger, config, jsStats, mapper!); // eslint-disable-next-line @typescript-eslint/no-var-requires @@ -250,7 +250,7 @@ describe('v8debugapi', () => { assert.strictEqual(fileStats.errors().size, 0); const jsStats = fileStats.selectStats(/.js$|.jsz$/); const mapFiles = fileStats.selectFiles(/.js.map$/, process.cwd()); - const mapper = await SourceMapper.create(mapFiles); + const mapper = await SourceMapper.create(mapFiles, logger); // TODO(dominickramer): Handle the case when mapper is undefined. // TODO(dominickramer): Handle the case when v8debugapi.create From 9f1165ed3092a2a8d0a54377d63b967c8d7ab624 Mon Sep 17 00:00:00 2001 From: Louis Ye Date: Wed, 16 Jun 2021 11:42:28 -0400 Subject: [PATCH 2/8] Fix lint issue --- src/agent/io/sourcemapper.ts | 5 ++- test/test-sourcemapper.ts | 69 ++++++++++++++++++++++-------------- 2 files changed, 46 insertions(+), 28 deletions(-) diff --git a/src/agent/io/sourcemapper.ts b/src/agent/io/sourcemapper.ts index 92319656..3be5316a 100644 --- a/src/agent/io/sourcemapper.ts +++ b/src/agent/io/sourcemapper.ts @@ -327,7 +327,10 @@ export class SourceMapper { * @param {Logger} logger A logger that reports errors that occurred while * processing the given sourcemap files */ -export async function create(sourcemapPaths: string[], logger: Logger): Promise { +export async function create( + sourcemapPaths: string[], + logger: Logger +): Promise { const limit = pLimit(CONCURRENCY); const mapper = new SourceMapper(logger); const promises = sourcemapPaths.map(path => diff --git a/test/test-sourcemapper.ts b/test/test-sourcemapper.ts index cf800a69..56e517cd 100644 --- a/test/test-sourcemapper.ts +++ b/test/test-sourcemapper.ts @@ -57,25 +57,34 @@ function testTool( const logger = new MockLogger(); let sourcemapper: sm.SourceMapper; - it('for tool ' + tool + ' sourcemapper should be created correctly', - async () => { - const start = Date.now(); - sourcemapper = await sm.create([mapFilePath], logger); - assert( - Date.now() - start < QUICK_MILLISECONDS, - 'should create the SourceMapper quickly'); + it( + 'for tool ' + tool + ' sourcemapper should be created correctly', + async () => { + const start = Date.now(); + sourcemapper = await sm.create([mapFilePath], logger); + assert( + Date.now() - start < QUICK_MILLISECONDS, + 'should create the SourceMapper quickly' + ); - // Verify if the debugging information is correctly printed. - assert.notStrictEqual( - logger.debugs[0].args[0].indexOf('debugging information ...'), -1); - assert.notStrictEqual(logger.debugs[1].args[0].indexOf('source '), -1); - assert.notStrictEqual( - logger.debugs[2].args[0].indexOf('outputFile'), -1); - assert.notStrictEqual(logger.debugs[3].args[0].indexOf('mapFile'), -1); - assert.notStrictEqual(logger.debugs[4].args[0].indexOf('sources'), -1); - assert.strictEqual( - logger.debugs.length, sourcemapper.infoMap.size * 4 + 1); - }); + // Verify if the debugging information is correctly printed. + assert.notStrictEqual( + logger.debugs[0].args[0].indexOf('debugging information ...'), + -1 + ); + assert.notStrictEqual(logger.debugs[1].args[0].indexOf('source '), -1); + assert.notStrictEqual( + logger.debugs[2].args[0].indexOf('outputFile'), + -1 + ); + assert.notStrictEqual(logger.debugs[3].args[0].indexOf('mapFile'), -1); + assert.notStrictEqual(logger.debugs[4].args[0].indexOf('sources'), -1); + assert.strictEqual( + logger.debugs.length, + sourcemapper.infoMap.size * 4 + 1 + ); + } + ); it( 'for tool ' + @@ -143,17 +152,23 @@ function testTool( // Verify if the debugging information is correctly printed. const debugsLength = logger.debugs.length; assert.notStrictEqual( - logger.debugs[debugsLength - 3].args[0].indexOf( - 'sourcemapper inputPath:'), - -1); + logger.debugs[debugsLength - 3].args[0].indexOf( + 'sourcemapper inputPath:' + ), + -1 + ); assert.notStrictEqual( - logger.debugs[debugsLength - 2].args[0].indexOf( - 'sourcemapper sourcePos: {'), - -1); + logger.debugs[debugsLength - 2].args[0].indexOf( + 'sourcemapper sourcePos: {' + ), + -1 + ); assert.notStrictEqual( - logger.debugs[debugsLength - 1].args[0].indexOf( - 'sourcemapper mappedPos: {'), - -1); + logger.debugs[debugsLength - 1].args[0].indexOf( + 'sourcemapper mappedPos: {' + ), + -1 + ); assert.notStrictEqual( info, From 425979729ec81c81f6e9d73fe9333b3d822a62b4 Mon Sep 17 00:00:00 2001 From: Louis Ye Date: Wed, 16 Jun 2021 17:41:27 -0400 Subject: [PATCH 3/8] fix: source mapping original path instead of user-provided input --- src/agent/io/sourcemapper.ts | 20 ++++++----- src/agent/v8/inspector-debugapi.ts | 1 - src/agent/v8/legacy-debugapi.ts | 1 - test/test-sourcemapper.ts | 55 ++++++++++++++++-------------- 4 files changed, 41 insertions(+), 36 deletions(-) diff --git a/src/agent/io/sourcemapper.ts b/src/agent/io/sourcemapper.ts index 3be5316a..24048077 100644 --- a/src/agent/io/sourcemapper.ts +++ b/src/agent/io/sourcemapper.ts @@ -35,6 +35,10 @@ export interface MapInfoInput { // process's working directory). outputFile: string; + // The path of the original source file. In the example above, this will be + // either "src/index1.ts" or "src/index2.ts". + inputFile: string; + // The source map's path (relative to the process's working directory). For // the same example above, this field is "dist/index.js.map". mapFile: string; @@ -158,8 +162,10 @@ async function processSourcemap( } for (const src of normalizedSourcesRelToProc) { - infoMap.set(path.normalize(src), { + const inputFile = path.normalize(src); + infoMap.set(inputFile, { outputFile: outputPath, + inputFile, mapFile: mapPath, mapConsumer: consumer, sources: sourcesRelToSrcmap, @@ -218,13 +224,12 @@ export class SourceMapper { } /** - * @param {string} inputPath The path to an input file that could possibly - * be the input to a transpilation process. The path should be relative to - * the process's current working directory * @param {number} The line number in the input file where the line number is * zero-based. * @param {number} (Optional) The column number in the line of the file * specified where the column number is zero-based. + * @param {string} The entry of the source map info in this.infoMap. + * * @return {Object} The object returned has a "file" attribute for the * path of the output file associated with the given input file (where the * path is relative to the process's current working directory), @@ -237,17 +242,14 @@ export class SourceMapper { * with it then null is returned. */ getMapInfoOutput( - inputPath: string, lineNumber: number, colNumber: number, entry: MapInfoInput ): MapInfoOutput | null { - this.logger.debug(`sourcemapper inputPath: ${inputPath}`); - - inputPath = path.normalize(inputPath); + this.logger.debug(`sourcemapper entry.inputFile: ${entry.inputFile}`); const relPath = path - .relative(path.dirname(entry.mapFile), inputPath) + .relative(path.dirname(entry.mapFile), entry.inputFile) .replace(/\\/g, '/'); /** diff --git a/src/agent/v8/inspector-debugapi.ts b/src/agent/v8/inspector-debugapi.ts index 8e1b4807..e6c8a1ed 100644 --- a/src/agent/v8/inspector-debugapi.ts +++ b/src/agent/v8/inspector-debugapi.ts @@ -198,7 +198,6 @@ export class InspectorDebugApi implements debugapi.DebugApi { const line = breakpoint.location.line; const column = 0; const mapInfo = this.sourcemapper.getMapInfoOutput( - baseScriptPath, line, column, mapInfoInput diff --git a/src/agent/v8/legacy-debugapi.ts b/src/agent/v8/legacy-debugapi.ts index a7eebbf5..c3965f9b 100644 --- a/src/agent/v8/legacy-debugapi.ts +++ b/src/agent/v8/legacy-debugapi.ts @@ -145,7 +145,6 @@ export class V8DebugApi implements debugapi.DebugApi { const line = breakpoint.location.line; const column = 0; const mapInfo = this.sourcemapper.getMapInfoOutput( - baseScriptPath, line, column, mapInfoInput diff --git a/test/test-sourcemapper.ts b/test/test-sourcemapper.ts index 56e517cd..a510bc68 100644 --- a/test/test-sourcemapper.ts +++ b/test/test-sourcemapper.ts @@ -28,11 +28,11 @@ const QUICK_MILLISECONDS = 300; /** * @param {string} tool The name of the tool that was used to generate the * given sourcemap data - * @param {string} relativeMapFilePath The path to the sourcemap file of a + * @param {string} mapFilePath The path to the sourcemap file of a * transpilation to test - * @param {string} relativeInputFilePath The path to the input file that was + * @param {string} inputFilePath The path to the input file that was * transpiled to generate the specified sourcemap file - * @param {string} relativeOutputFilePath The path to the output file that was + * @param {string} outputFilePath The path to the output file that was * generated during the transpilation process that constructed the * specified sourcemap file * @param {Array.>} inToOutLineNums An array of arrays @@ -44,15 +44,11 @@ const QUICK_MILLISECONDS = 300; */ function testTool( tool: string, - relativeMapFilePath: string, - relativeInputFilePath: string, - relativeOutputFilePath: string, + mapFilePath: string, + inputFilePath: string, + outputFilePath: string, inToOutLineNums: number[][] ) { - const mapFilePath = path.join(BASE_PATH, relativeMapFilePath); - const inputFilePath = path.join(BASE_PATH, relativeInputFilePath); - const outputFilePath = path.join(BASE_PATH, relativeOutputFilePath); - describe('sourcemapper for tool ' + tool, () => { const logger = new MockLogger(); let sourcemapper: sm.SourceMapper; @@ -112,7 +108,7 @@ function testTool( ); const movedPath = path.join( '/some/other/base/dir/', - relativeInputFilePath + inputFilePath ); assert.notStrictEqual( sourcemapper.getMapInfoInput(inputFilePath), @@ -143,7 +139,6 @@ function testTool( const mapInfoInput = sourcemapper.getMapInfoInput(inputFilePath); assert.notEqual(mapInfoInput, null); const info = sourcemapper.getMapInfoOutput( - inputFilePath, inputLine, 0, mapInfoInput! @@ -153,7 +148,7 @@ function testTool( const debugsLength = logger.debugs.length; assert.notStrictEqual( logger.debugs[debugsLength - 3].args[0].indexOf( - 'sourcemapper inputPath:' + 'sourcemapper entry.inputFile:' ), -1 ); @@ -200,9 +195,9 @@ function testTool( testTool( 'Babel', - path.join('babel', 'out.js.map'), - path.join('babel', 'in.js'), - path.join('babel', 'out.js'), + path.join(BASE_PATH, path.join('babel', 'out.js.map')), + path.join(BASE_PATH, path.join('babel', 'in.js')), + path.join(BASE_PATH, path.join('babel', 'out.js')), [ [1, 14], [2, 15], @@ -259,9 +254,9 @@ testTool( testTool( 'Typescript', - path.join('typescript', 'out.js.map'), - path.join('typescript', 'in.ts'), - path.join('typescript', 'out.js'), + path.join(BASE_PATH, path.join('typescript', 'out.js.map')), + path.join(BASE_PATH, path.join('typescript', 'in.ts')), + path.join(BASE_PATH, path.join('typescript', 'out.js')), [ [1, 5], [2, 6], @@ -279,11 +274,21 @@ testTool( ] ); +testTool( + 'Typescript with sub path', + path.join(BASE_PATH, path.join('typescript', 'out.js.map')), + 'in.ts', + path.join(BASE_PATH, path.join('typescript', 'out.js')), + [ + [1, 5], + ] +); + testTool( 'Coffeescript', - path.join('coffeescript', 'in.js.map'), - path.join('coffeescript', 'in.coffee'), - path.join('coffeescript', 'in.js'), + path.join(BASE_PATH, path.join('coffeescript', 'in.js.map')), + path.join(BASE_PATH, path.join('coffeescript', 'in.coffee')), + path.join(BASE_PATH, path.join('coffeescript', 'in.js')), [ [1, 1], [2, 7], @@ -305,9 +310,9 @@ testTool( testTool( 'Webpack with Typescript', - path.join('webpack-ts', 'out.js.map'), - path.join('webpack-ts', 'in.ts_'), - path.join('webpack-ts', 'out.js'), + path.join(BASE_PATH, path.join('webpack-ts', 'out.js.map')), + path.join(BASE_PATH, path.join('webpack-ts', 'in.ts_')), + path.join(BASE_PATH, path.join('webpack-ts', 'out.js')), [ [3, 93], [4, 94], From c25b71c159a66c8fb9b29abdb153efbd656792eb Mon Sep 17 00:00:00 2001 From: Louis Ye Date: Thu, 17 Jun 2021 10:21:13 -0400 Subject: [PATCH 4/8] Fix lint issue --- test/test-sourcemapper.ts | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/test/test-sourcemapper.ts b/test/test-sourcemapper.ts index a510bc68..96fe6084 100644 --- a/test/test-sourcemapper.ts +++ b/test/test-sourcemapper.ts @@ -106,10 +106,7 @@ function testTool( sourcemapper.getMapInfoInput(inputFilePath), null ); - const movedPath = path.join( - '/some/other/base/dir/', - inputFilePath - ); + const movedPath = path.join('/some/other/base/dir/', inputFilePath); assert.notStrictEqual( sourcemapper.getMapInfoInput(inputFilePath), null, @@ -138,11 +135,7 @@ function testTool( const testLineMapping = (inputLine: number, expectedOutputLine: number) => { const mapInfoInput = sourcemapper.getMapInfoInput(inputFilePath); assert.notEqual(mapInfoInput, null); - const info = sourcemapper.getMapInfoOutput( - inputLine, - 0, - mapInfoInput! - ); + const info = sourcemapper.getMapInfoOutput(inputLine, 0, mapInfoInput!); // Verify if the debugging information is correctly printed. const debugsLength = logger.debugs.length; @@ -279,9 +272,7 @@ testTool( path.join(BASE_PATH, path.join('typescript', 'out.js.map')), 'in.ts', path.join(BASE_PATH, path.join('typescript', 'out.js')), - [ - [1, 5], - ] + [[1, 5]] ); testTool( From 919e039c4ad605ac3f94e6c8b0985614cacebfde Mon Sep 17 00:00:00 2001 From: Louis Ye Date: Mon, 21 Jun 2021 17:25:19 -0400 Subject: [PATCH 5/8] Add test case for more coverage in sourcemapper --- test/test-sourcemapper.ts | 49 ++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 26 deletions(-) diff --git a/test/test-sourcemapper.ts b/test/test-sourcemapper.ts index 25bb9b40..3ba12898 100644 --- a/test/test-sourcemapper.ts +++ b/test/test-sourcemapper.ts @@ -180,37 +180,34 @@ function testTool( } ); + it( + 'for tool ' + + tool + + ' it can get mapping info output when input file does not exist in the source map', + done => { + const mapInfoInput = sourcemapper.getMapInfoInput(inputFilePath); + assert.notEqual(mapInfoInput, null); + + const mapInfoOutput = sourcemapper.getMapInfoOutput(10, 0, { + outputFile: mapInfoInput!.outputFile, + inputFile: 'some/file/not/in/sourcemap', + mapFile: mapInfoInput!.mapFile, + mapConsumer: mapInfoInput!.mapConsumer, + sources: mapInfoInput!.sources, + }); + + assert.notEqual(mapInfoOutput, null); + assert.strictEqual(mapInfoOutput!.file, mapInfoInput!.outputFile); + assert.strictEqual(mapInfoOutput!.line, -1); + done(); + } + ); + const testLineMapping = (inputLine: number, expectedOutputLine: number) => { const mapInfoInput = sourcemapper.getMapInfoInput(inputFilePath); assert.notEqual(mapInfoInput, null); const info = sourcemapper.getMapInfoOutput(inputLine, 0, mapInfoInput!); - // Verify if the debugging information is correctly printed. - const debugsLength = logger.debugs.length; - assert.notStrictEqual( - logger.debugs[debugsLength - 3].args[0].indexOf( - 'sourcemapper entry.inputFile:' - ), - -1 - ); - assert.notStrictEqual( - logger.debugs[debugsLength - 2].args[0].indexOf( - 'sourcemapper sourcePos: {' - ), - -1 - ); - assert.notStrictEqual( - logger.debugs[debugsLength - 1].args[0].indexOf( - 'sourcemapper mappedPos: {' - ), - -1 - ); - - assert.notStrictEqual( - info, - null, - 'The mapping info for file ' + inputFilePath + ' must be non-null' - ); assert.strictEqual(info!.file, outputFilePath); assert.strictEqual( info!.line, From c6d911c939b259aea7e7a7405468ae183a0ceac5 Mon Sep 17 00:00:00 2001 From: Louis Ye Date: Mon, 21 Jun 2021 17:27:04 -0400 Subject: [PATCH 6/8] Move comments in test-sourcemmapper to the correct place --- test/test-sourcemapper.ts | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/test/test-sourcemapper.ts b/test/test-sourcemapper.ts index 3ba12898..b9a9f6b0 100644 --- a/test/test-sourcemapper.ts +++ b/test/test-sourcemapper.ts @@ -25,24 +25,6 @@ import {MockLogger} from './mock-logger'; const BASE_PATH = path.join(__dirname, 'fixtures', 'sourcemapper'); const QUICK_MILLISECONDS = 300; -/** - * @param {string} tool The name of the tool that was used to generate the - * given sourcemap data - * @param {string} mapFilePath The path to the sourcemap file of a - * transpilation to test - * @param {string} inputFilePath The path to the input file that was - * transpiled to generate the specified sourcemap file - * @param {string} outputFilePath The path to the output file that was - * generated during the transpilation process that constructed the - * specified sourcemap file - * @param {Array.>} inToOutLineNums An array of arrays - * where each element in the array is a pair of numbers. The first number - * in the pair is the line number from the input file and the second number - * in the pair is the expected line number in the corresponding output file - * - * Note: The line numbers are zero-based - */ - describe('sourcemapper debug info', () => { const logger = new MockLogger(); const mapFilePath = path.join( @@ -110,6 +92,23 @@ describe('sourcemapper debug info', () => { }); }); +/** + * @param {string} tool The name of the tool that was used to generate the + * given sourcemap data + * @param {string} mapFilePath The path to the sourcemap file of a + * transpilation to test + * @param {string} inputFilePath The path to the input file that was + * transpiled to generate the specified sourcemap file + * @param {string} outputFilePath The path to the output file that was + * generated during the transpilation process that constructed the + * specified sourcemap file + * @param {Array.>} inToOutLineNums An array of arrays + * where each element in the array is a pair of numbers. The first number + * in the pair is the line number from the input file and the second number + * in the pair is the expected line number in the corresponding output file + * + * Note: The line numbers are zero-based + */ function testTool( tool: string, mapFilePath: string, From 67ecb9ace5091f6c2fed7750210df1d350aae4cc Mon Sep 17 00:00:00 2001 From: Louis Ye Date: Wed, 23 Jun 2021 17:22:44 -0400 Subject: [PATCH 7/8] Update comments about the entry of getMapInfoOutput --- src/agent/io/sourcemapper.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/agent/io/sourcemapper.ts b/src/agent/io/sourcemapper.ts index 1b4bcc82..cd85aef0 100644 --- a/src/agent/io/sourcemapper.ts +++ b/src/agent/io/sourcemapper.ts @@ -229,7 +229,8 @@ export class SourceMapper { * zero-based. * @param {number} (Optional) The column number in the line of the file * specified where the column number is zero-based. - * @param {string} The entry of the source map info in this.infoMap. + * @param {string} The entry of the source map info in the sourceMapper. Such + * an entry is supposed to get got by the getMapInfoInput method. * * @return {Object} The object returned has a "file" attribute for the * path of the output file associated with the given input file (where the From b9179361dae69c6a91ff0d924274568a96dced4f Mon Sep 17 00:00:00 2001 From: Louis Ye Date: Wed, 23 Jun 2021 17:23:35 -0400 Subject: [PATCH 8/8] Fix typo --- src/agent/io/sourcemapper.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/agent/io/sourcemapper.ts b/src/agent/io/sourcemapper.ts index cd85aef0..9c244f1d 100644 --- a/src/agent/io/sourcemapper.ts +++ b/src/agent/io/sourcemapper.ts @@ -230,7 +230,7 @@ export class SourceMapper { * @param {number} (Optional) The column number in the line of the file * specified where the column number is zero-based. * @param {string} The entry of the source map info in the sourceMapper. Such - * an entry is supposed to get got by the getMapInfoInput method. + * an entry is supposed to be got by the getMapInfoInput method. * * @return {Object} The object returned has a "file" attribute for the * path of the output file associated with the given input file (where the