From 42fd333a68caea34583495f5d99fae16e51ff70c Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Tue, 19 Apr 2022 16:03:05 +0200 Subject: [PATCH 1/3] feat: migrate from `source-map` to `@jridgewell/trace-mapping` --- packages/jest-reporters/package.json | 2 +- .../jest-reporters/src/CoverageReporter.ts | 12 ++---- packages/jest-source-map/package.json | 4 +- .../src/__tests__/getCallsite.test.ts | 43 ++++++++++++------- packages/jest-source-map/src/getCallsite.ts | 11 +++-- packages/jest-transform/package.json | 2 +- packages/jest-transform/src/types.ts | 9 +--- yarn.lock | 8 ++-- 8 files changed, 46 insertions(+), 45 deletions(-) diff --git a/packages/jest-reporters/package.json b/packages/jest-reporters/package.json index a79b91382150..82fe47a62706 100644 --- a/packages/jest-reporters/package.json +++ b/packages/jest-reporters/package.json @@ -17,6 +17,7 @@ "@jest/test-result": "^28.0.0-alpha.9", "@jest/transform": "^28.0.0-alpha.9", "@jest/types": "^28.0.0-alpha.9", + "@jridgewell/trace-mapping": "^0.3.4", "@types/node": "*", "chalk": "^4.0.0", "collect-v8-coverage": "^1.0.0", @@ -31,7 +32,6 @@ "jest-util": "^28.0.0-alpha.9", "jest-worker": "^28.0.0-alpha.9", "slash": "^3.0.0", - "source-map": "^0.6.1", "string-length": "^4.0.1", "terminal-link": "^2.0.0", "v8-to-istanbul": "^8.1.0" diff --git a/packages/jest-reporters/src/CoverageReporter.ts b/packages/jest-reporters/src/CoverageReporter.ts index 2aa808d97e75..5095555242f5 100644 --- a/packages/jest-reporters/src/CoverageReporter.ts +++ b/packages/jest-reporters/src/CoverageReporter.ts @@ -7,6 +7,7 @@ import * as path from 'path'; import {mergeProcessCovs} from '@bcoe/v8-coverage'; +import type {EncodedSourceMap} from '@jridgewell/trace-mapping'; import chalk = require('chalk'); import glob = require('glob'); import * as fs from 'graceful-fs'; @@ -14,7 +15,6 @@ import istanbulCoverage = require('istanbul-lib-coverage'); import istanbulReport = require('istanbul-lib-report'); import libSourceMaps = require('istanbul-lib-source-maps'); import istanbulReports = require('istanbul-reports'); -import type {RawSourceMap} from 'source-map'; import v8toIstanbul = require('v8-to-istanbul'); import type { AggregatedResult, @@ -31,12 +31,6 @@ import BaseReporter from './BaseReporter'; import getWatermarks from './getWatermarks'; import type {CoverageWorker, ReporterContext} from './types'; -// This is fixed in a newer versions of source-map, but our dependencies are still stuck on old versions -interface FixedRawSourceMap extends Omit { - version: number; - file?: string; -} - const FAIL_COLOR = chalk.bold.red; const RUNNING_TEST_COLOR = chalk.bold.dim; @@ -437,7 +431,7 @@ export default class CoverageReporter extends BaseReporter { mergedCoverages.result.map(async res => { const fileTransform = fileTransforms.get(res.url); - let sourcemapContent: FixedRawSourceMap | undefined = undefined; + let sourcemapContent: EncodedSourceMap | undefined = undefined; if ( fileTransform?.sourceMapPath && @@ -456,7 +450,7 @@ export default class CoverageReporter extends BaseReporter { originalSource: fileTransform.originalCode, source: fileTransform.code, sourceMap: { - sourcemap: {file: res.url, ...sourcemapContent}, + sourcemap: {file: res.url, ...sourcemapContent} as any, }, } : {source: fs.readFileSync(res.url, 'utf8')}, diff --git a/packages/jest-source-map/package.json b/packages/jest-source-map/package.json index b57ea028f33b..477f6ebe8667 100644 --- a/packages/jest-source-map/package.json +++ b/packages/jest-source-map/package.json @@ -17,9 +17,9 @@ "./package.json": "./package.json" }, "dependencies": { + "@jridgewell/trace-mapping": "^0.3.4", "callsites": "^3.0.0", - "graceful-fs": "^4.2.9", - "source-map": "^0.6.1" + "graceful-fs": "^4.2.9" }, "devDependencies": { "@types/graceful-fs": "^4.1.3" diff --git a/packages/jest-source-map/src/__tests__/getCallsite.test.ts b/packages/jest-source-map/src/__tests__/getCallsite.test.ts index 00f265f9b2d9..0482b12f7323 100644 --- a/packages/jest-source-map/src/__tests__/getCallsite.test.ts +++ b/packages/jest-source-map/src/__tests__/getCallsite.test.ts @@ -5,11 +5,19 @@ * LICENSE file in the root directory of this source tree. */ +import {originalPositionFor} from '@jridgewell/trace-mapping'; import * as fs from 'graceful-fs'; -import SourceMap from 'source-map'; import getCallsite from '../getCallsite'; jest.mock('graceful-fs'); +jest.mock('@jridgewell/trace-mapping', () => { + const actual = jest.requireActual('@jridgewell/trace-mapping'); + + return { + ...actual, + originalPositionFor: jest.fn(actual.originalPositionFor), + }; +}); describe('getCallsite', () => { test('without source map', () => { @@ -35,30 +43,35 @@ describe('getCallsite', () => { }); test('reads source map file to determine line and column', () => { - (fs.readFileSync as jest.Mock).mockImplementation(() => 'file data'); + (fs.readFileSync as jest.Mock).mockImplementation(() => + JSON.stringify({ + file: 'file.js', + mappings: 'AAAA,OAAO,MAAM,KAAK,GAAG,QAAd', + names: [], + sources: ['file.js'], + sourcesContent: ["export const hello = 'foobar';\\n"], + version: 3, + }), + ); const sourceMapColumn = 1; const sourceMapLine = 2; - SourceMap.SourceMapConsumer = class { - originalPositionFor(params: Record) { - expect(params).toMatchObject({ - column: expect.any(Number), - line: expect.any(Number), - }); - - return { - column: sourceMapColumn, - line: sourceMapLine, - }; - } - }; + jest.mocked(originalPositionFor).mockImplementation(() => ({ + column: sourceMapColumn, + line: sourceMapLine, + })); const site = getCallsite(0, new Map([[__filename, 'mockedSourceMapFile']])); expect(site.getFileName()).toEqual(__filename); expect(site.getColumnNumber()).toEqual(sourceMapColumn); expect(site.getLineNumber()).toEqual(sourceMapLine); + expect(originalPositionFor).toHaveBeenCalledTimes(1); + expect(originalPositionFor).toHaveBeenCalledWith(expect.anything(), { + column: expect.any(Number), + line: expect.any(Number), + }); expect(fs.readFileSync).toHaveBeenCalledWith('mockedSourceMapFile', 'utf8'); }); }); diff --git a/packages/jest-source-map/src/getCallsite.ts b/packages/jest-source-map/src/getCallsite.ts index 44d4420fb201..930e5c1eb5bb 100644 --- a/packages/jest-source-map/src/getCallsite.ts +++ b/packages/jest-source-map/src/getCallsite.ts @@ -5,23 +5,23 @@ * LICENSE file in the root directory of this source tree. */ +import {TraceMap, originalPositionFor} from '@jridgewell/trace-mapping'; import callsites = require('callsites'); import {readFileSync} from 'graceful-fs'; -import {SourceMapConsumer} from 'source-map'; import type {SourceMapRegistry} from './types'; // Copied from https://github.com/rexxars/sourcemap-decorate-callsites/blob/5b9735a156964973a75dc62fd2c7f0c1975458e8/lib/index.js#L113-L158 const addSourceMapConsumer = ( callsite: callsites.CallSite, - consumer: SourceMapConsumer, + tracer: TraceMap, ) => { const getLineNumber = callsite.getLineNumber; const getColumnNumber = callsite.getColumnNumber; - let position: ReturnType | null = null; + let position: ReturnType | null = null; function getPosition() { if (!position) { - position = consumer.originalPositionFor({ + position = originalPositionFor(tracer, { column: getColumnNumber.call(callsite) || -1, line: getLineNumber.call(callsite) || -1, }); @@ -57,8 +57,7 @@ export default function getCallsite( if (sourceMapFileName) { try { const sourceMap = readFileSync(sourceMapFileName, 'utf8'); - // @ts-expect-error: Not allowed to pass string - addSourceMapConsumer(stack, new SourceMapConsumer(sourceMap)); + addSourceMapConsumer(stack, new TraceMap(sourceMap)); } catch { // ignore } diff --git a/packages/jest-transform/package.json b/packages/jest-transform/package.json index fc42a0eabb8b..ebb34e5f963b 100644 --- a/packages/jest-transform/package.json +++ b/packages/jest-transform/package.json @@ -19,6 +19,7 @@ "dependencies": { "@babel/core": "^7.11.6", "@jest/types": "^28.0.0-alpha.9", + "@jridgewell/trace-mapping": "^0.3.4", "babel-plugin-istanbul": "^6.1.1", "chalk": "^4.0.0", "convert-source-map": "^1.4.0", @@ -30,7 +31,6 @@ "micromatch": "^4.0.4", "pirates": "^4.0.4", "slash": "^3.0.0", - "source-map": "^0.6.1", "write-file-atomic": "^4.0.1" }, "devDependencies": { diff --git a/packages/jest-transform/src/types.ts b/packages/jest-transform/src/types.ts index 543583477a6a..73759eec608f 100644 --- a/packages/jest-transform/src/types.ts +++ b/packages/jest-transform/src/types.ts @@ -5,7 +5,7 @@ * LICENSE file in the root directory of this source tree. */ -import type {RawSourceMap} from 'source-map'; +import type {EncodedSourceMap} from '@jridgewell/trace-mapping'; import type {Config, TransformTypes} from '@jest/types'; export interface ShouldInstrumentOptions @@ -26,14 +26,9 @@ export interface Options isInternalModule?: boolean; } -// This is fixed in source-map@0.7.x, but we can't upgrade yet since it's async -interface FixedRawSourceMap extends Omit { - version: number; -} - export type TransformedSource = { code: string; - map?: FixedRawSourceMap | string | null; + map?: EncodedSourceMap | string | null; }; export type TransformResult = TransformTypes.TransformResult; diff --git a/yarn.lock b/yarn.lock index 875b2c9d58df..428a3a0a9ec5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2758,6 +2758,7 @@ __metadata: "@jest/test-utils": ^28.0.0-alpha.9 "@jest/transform": ^28.0.0-alpha.9 "@jest/types": ^28.0.0-alpha.9 + "@jridgewell/trace-mapping": ^0.3.4 "@types/exit": ^0.1.30 "@types/glob": ^7.1.1 "@types/graceful-fs": ^4.1.3 @@ -2783,7 +2784,6 @@ __metadata: jest-worker: ^28.0.0-alpha.9 mock-fs: ^5.1.2 slash: ^3.0.0 - source-map: ^0.6.1 string-length: ^4.0.1 strip-ansi: ^6.0.0 terminal-link: ^2.0.0 @@ -2808,10 +2808,10 @@ __metadata: version: 0.0.0-use.local resolution: "@jest/source-map@workspace:packages/jest-source-map" dependencies: + "@jridgewell/trace-mapping": ^0.3.4 "@types/graceful-fs": ^4.1.3 callsites: ^3.0.0 graceful-fs: ^4.2.9 - source-map: ^0.6.1 languageName: unknown linkType: soft @@ -2872,6 +2872,7 @@ __metadata: "@babel/core": ^7.11.6 "@jest/test-utils": ^28.0.0-alpha.9 "@jest/types": ^28.0.0-alpha.9 + "@jridgewell/trace-mapping": ^0.3.4 "@types/babel__core": ^7.1.14 "@types/convert-source-map": ^1.5.1 "@types/fast-json-stable-stringify": ^2.0.0 @@ -2890,7 +2891,6 @@ __metadata: micromatch: ^4.0.4 pirates: ^4.0.4 slash: ^3.0.0 - source-map: ^0.6.1 write-file-atomic: ^4.0.1 languageName: unknown linkType: soft @@ -2950,7 +2950,7 @@ __metadata: languageName: node linkType: hard -"@jridgewell/trace-mapping@npm:^0.3.0": +"@jridgewell/trace-mapping@npm:^0.3.0, @jridgewell/trace-mapping@npm:^0.3.4": version: 0.3.4 resolution: "@jridgewell/trace-mapping@npm:0.3.4" dependencies: From efe1e5ed59e67b1e1e5172d7fa539b1641b00fbd Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Tue, 19 Apr 2022 16:27:14 +0200 Subject: [PATCH 2/3] type fix, maybe --- packages/jest-transform/src/types.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/jest-transform/src/types.ts b/packages/jest-transform/src/types.ts index 73759eec608f..79afdb3fd45d 100644 --- a/packages/jest-transform/src/types.ts +++ b/packages/jest-transform/src/types.ts @@ -26,9 +26,14 @@ export interface Options isInternalModule?: boolean; } +// `babel` and `@jridgewell/trace-mapping` disagrees +interface FixedRawSourceMap extends Omit { + version: number; +} + export type TransformedSource = { code: string; - map?: EncodedSourceMap | string | null; + map?: FixedRawSourceMap | string | null; }; export type TransformResult = TransformTypes.TransformResult; From dc7d26c11aca9c6653a3baa7545c4937966f0ea9 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Wed, 20 Apr 2022 06:54:28 +0200 Subject: [PATCH 3/3] Apply suggestions from code review --- packages/jest-reporters/src/CoverageReporter.ts | 1 + packages/jest-transform/src/types.ts | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/jest-reporters/src/CoverageReporter.ts b/packages/jest-reporters/src/CoverageReporter.ts index 5095555242f5..dc4d87daa6c1 100644 --- a/packages/jest-reporters/src/CoverageReporter.ts +++ b/packages/jest-reporters/src/CoverageReporter.ts @@ -450,6 +450,7 @@ export default class CoverageReporter extends BaseReporter { originalSource: fileTransform.originalCode, source: fileTransform.code, sourceMap: { + // remove `as any` after https://github.com/istanbuljs/v8-to-istanbul/pull/186 is released sourcemap: {file: res.url, ...sourcemapContent} as any, }, } diff --git a/packages/jest-transform/src/types.ts b/packages/jest-transform/src/types.ts index 79afdb3fd45d..27308b5403f8 100644 --- a/packages/jest-transform/src/types.ts +++ b/packages/jest-transform/src/types.ts @@ -26,7 +26,7 @@ export interface Options isInternalModule?: boolean; } -// `babel` and `@jridgewell/trace-mapping` disagrees +// `babel` and `@jridgewell/trace-mapping` disagrees - `number` vs `3` interface FixedRawSourceMap extends Omit { version: number; }