Skip to content

Commit

Permalink
feat: migrate from source-map to @jridgewell/trace-mapping (#12692)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB authored Apr 20, 2022
1 parent 5183c15 commit 58af3c1
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 41 deletions.
2 changes: 1 addition & 1 deletion packages/jest-reporters/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
Expand Down
13 changes: 4 additions & 9 deletions packages/jest-reporters/src/CoverageReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@

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';
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,
Expand All @@ -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<RawSourceMap, 'version'> {
version: number;
file?: string;
}

const FAIL_COLOR = chalk.bold.red;
const RUNNING_TEST_COLOR = chalk.bold.dim;

Expand Down Expand Up @@ -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 &&
Expand All @@ -456,7 +450,8 @@ export default class CoverageReporter extends BaseReporter {
originalSource: fileTransform.originalCode,
source: fileTransform.code,
sourceMap: {
sourcemap: {file: res.url, ...sourcemapContent},
// remove `as any` after https://github.com/istanbuljs/v8-to-istanbul/pull/186 is released
sourcemap: {file: res.url, ...sourcemapContent} as any,
},
}
: {source: fs.readFileSync(res.url, 'utf8')},
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-source-map/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
43 changes: 28 additions & 15 deletions packages/jest-source-map/src/__tests__/getCallsite.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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<string, number>) {
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');
});
});
11 changes: 5 additions & 6 deletions packages/jest-source-map/src/getCallsite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof consumer.originalPositionFor> | null = null;
let position: ReturnType<typeof originalPositionFor> | null = null;

function getPosition() {
if (!position) {
position = consumer.originalPositionFor({
position = originalPositionFor(tracer, {
column: getColumnNumber.call(callsite) || -1,
line: getLineNumber.call(callsite) || -1,
});
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-transform/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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": {
Expand Down
6 changes: 3 additions & 3 deletions packages/jest-transform/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,8 +26,8 @@ export interface Options
isInternalModule?: boolean;
}

// This is fixed in [email protected], but we can't upgrade yet since it's async
interface FixedRawSourceMap extends Omit<RawSourceMap, 'version'> {
// `babel` and `@jridgewell/trace-mapping` disagrees - `number` vs `3`
interface FixedRawSourceMap extends Omit<EncodedSourceMap, 'version'> {
version: number;
}

Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 58af3c1

Please sign in to comment.