Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert all SourceMapConsumers to TraceMaps #14253

Merged
merged 5 commits into from Feb 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
74 changes: 0 additions & 74 deletions lib/third-party-libs.js.flow
Expand Up @@ -63,83 +63,9 @@ declare module "source-map" {
};

declare module.exports: {
SourceMapConsumer: typeof SourceMapConsumer,
SourceMapGenerator: typeof SourceMapGenerator,
}

declare class SourceMapConsumer {
static GENERATED_ORDER: 1;
static ORIGINAL_ORDER: 2;

file: string | null;
sourceRoot: string | null;
sources: Array<string>;

constructor(?SourceMap): this;

computeColumnSpans(): string;
originalPositionFor({
line: number,
column: number,
}): {|
source: string,
line: number,
column: number,
name: string | null
|} | {|
source: null,
line: null,
column: null,
name: null
|};

generatedPositionFor({
source: string,
line: number,
column: number,
}): {|
line: number,
column: number,
lastColumn: number | null | void,
|} | {|
line: null,
column: null,
lastColumn: null | void,
|};

allGeneratedPositionsFor({
source: string,
line: number,
column: number,
}): Array<{|
line: number,
column: number,
lastColumn: number,
|}>;

sourceContentFor(string, boolean | void): string | null;

eachMapping(
({|
generatedLine: number,
generatedColumn: number,
source: string,
originalLine: number,
originalColumn: number,
name: string | null,
|} | {|
generatedLine: number,
generatedColumn: number,
source: null,
originalLine: null,
originalColumn: null,
name: null,
|}) => mixed,
context: mixed,
order: ?(1 | 2),
): void;
}

declare class SourceMapGenerator {
constructor(?{
file?: string | null,
Expand Down
1 change: 1 addition & 0 deletions packages/babel-cli/package.json
Expand Up @@ -24,6 +24,7 @@
"compiler"
],
"dependencies": {
"@jridgewell/trace-mapping": "^0.3.4",
"commander": "^4.0.1",
"convert-source-map": "^1.1.0",
"fs-readdir-recursive": "^1.1.0",
Expand Down
17 changes: 7 additions & 10 deletions packages/babel-cli/src/babel/file.ts
@@ -1,4 +1,5 @@
import convertSourceMap from "convert-source-map";
import { TraceMap, eachMapping } from "@jridgewell/trace-mapping";
import sourceMap from "source-map";
import slash from "slash";
import path from "path";
Expand Down Expand Up @@ -35,12 +36,9 @@ export default async function ({
code += result.code + "\n";

if (result.map) {
const consumer = new sourceMap.SourceMapConsumer(result.map);
const sources = new Set<string>();

consumer.eachMapping(function (mapping) {
if (mapping.source != null) sources.add(mapping.source);
const consumer = new TraceMap(result.map);

eachMapping(consumer, mapping => {
map.addMapping({
generated: {
line: mapping.generatedLine + offset,
Expand All @@ -57,11 +55,10 @@ export default async function ({
});
});

sources.forEach(source => {
const content = consumer.sourceContentFor(source, true);
if (content !== null) {
map.setSourceContent(source, content);
}
const { resolvedSources, sourcesContent } = consumer;
sourcesContent?.forEach((content, i) => {
if (content === null) return;
map.setSourceContent(resolvedSources[i], content);
});

offset = code.split("\n").length - 1;
Expand Down
5 changes: 2 additions & 3 deletions packages/babel-core/package.json
Expand Up @@ -67,12 +67,11 @@
"devDependencies": {
"@babel/helper-transform-fixture-test-runner": "workspace:^",
"@babel/plugin-transform-modules-commonjs": "workspace:^",
"@jridgewell/trace-mapping": "^0.3.4",
"@types/convert-source-map": "^1.5.1",
"@types/debug": "^4.1.0",
"@types/resolve": "^1.3.2",
"@types/semver": "^5.4.0",
"@types/source-map": "^0.5.0",
"source-map": "0.6.1"
"@types/semver": "^5.4.0"
},
"conditions": {
"BABEL_8_BREAKING": [
Expand Down
6 changes: 3 additions & 3 deletions packages/babel-core/test/api.js
@@ -1,5 +1,5 @@
import babel from "../lib/index.js";
import sourceMap from "source-map";
import { TraceMap, originalPositionFor } from "@jridgewell/trace-mapping";
import path from "path";
import generator from "@babel/generator";
import { fileURLToPath } from "url";
Expand Down Expand Up @@ -532,10 +532,10 @@ describe("api", function () {
].join("\n"),
).toBe(result.code);

const consumer = new sourceMap.SourceMapConsumer(result.map);
const consumer = new TraceMap(result.map);

expect(
consumer.originalPositionFor({
originalPositionFor(consumer, {
line: 7,
column: 4,
}),
Expand Down
1 change: 1 addition & 0 deletions packages/babel-generator/package.json
Expand Up @@ -26,6 +26,7 @@
"devDependencies": {
"@babel/helper-fixtures": "workspace:^",
"@babel/parser": "workspace:^",
"@jridgewell/trace-mapping": "^0.3.4",
"@types/jsesc": "^2.5.0",
"@types/source-map": "^0.5.0",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

generator does, because it uses SourceMapGenerator. trace-mapping only offers a Consumer API.

"charcodes": "^0.2.0"
Expand Down
10 changes: 5 additions & 5 deletions packages/babel-generator/test/index.js
Expand Up @@ -3,7 +3,7 @@ import * as t from "@babel/types";
import fs from "fs";
import path from "path";
import fixtures from "@babel/helper-fixtures";
import sourcemap from "source-map";
import { TraceMap, originalPositionFor } from "@jridgewell/trace-mapping";
import { fileURLToPath } from "url";

import _Printer from "../lib/printer.js";
Expand Down Expand Up @@ -295,8 +295,8 @@ describe("generation", function () {
code,
);

const consumer = new sourcemap.SourceMapConsumer(generated.map);
const loc = consumer.originalPositionFor({ line: 2, column: 1 });
const consumer = new TraceMap(generated.map);
const loc = originalPositionFor(consumer, { line: 2, column: 1 });
expect(loc).toMatchObject({
column: 0,
line: 2,
Expand All @@ -316,8 +316,8 @@ describe("generation", function () {
code,
);

const consumer = new sourcemap.SourceMapConsumer(generated.map);
const loc = consumer.originalPositionFor({ line: 2, column: 1 });
const consumer = new TraceMap(generated.map);
const loc = originalPositionFor(consumer, { line: 2, column: 1 });
expect(loc).toMatchObject({
column: 0,
line: 2,
Expand Down
Expand Up @@ -18,10 +18,10 @@
"@babel/code-frame": "workspace:^",
"@babel/core": "workspace:^",
"@babel/helper-fixtures": "workspace:^",
"@jridgewell/trace-mapping": "^0.3.4",
"babel-check-duplicated-nodes": "^1.0.0",
"quick-lru": "5.1.0",
"regenerator-runtime": "^0.13.7",
"source-map": "^0.5.0"
"regenerator-runtime": "^0.13.7"
},
"devDependencies": {
"@types/jest": "^25.2.2"
Expand Down
Expand Up @@ -5,8 +5,8 @@ import {
default as getFixtures,
resolveOptionPluginOrPreset,
} from "@babel/helper-fixtures";
import sourceMap from "source-map";
import { codeFrameColumns } from "@babel/code-frame";
import { TraceMap, originalPositionFor } from "@jridgewell/trace-mapping";
import * as helpers from "./helpers";
import assert from "assert";
import fs from "fs";
Expand Down Expand Up @@ -346,12 +346,12 @@ function run(task) {
}

if (task.sourceMappings) {
const consumer = new sourceMap.SourceMapConsumer(result.map);
const consumer = new TraceMap(result.map);

task.sourceMappings.forEach(function (mapping) {
const actual = mapping.original;

const expected = consumer.originalPositionFor(mapping.generated);
const expected = originalPositionFor(consumer, mapping.generated);
expect({ line: expected.line, column: expected.column }).toEqual(actual);
});
}
Expand Down
29 changes: 15 additions & 14 deletions yarn.lock
Expand Up @@ -202,6 +202,7 @@ __metadata:
dependencies:
"@babel/core": "workspace:^"
"@babel/helper-fixtures": "workspace:^"
"@jridgewell/trace-mapping": ^0.3.4
"@nicolo-ribaudo/chokidar-2": "condition:BABEL_8_BREAKING ? : 2.1.8-no-fsevents.3"
chokidar: ^3.4.0
commander: ^4.0.1
Expand Down Expand Up @@ -334,17 +335,16 @@ __metadata:
"@babel/template": "workspace:^"
"@babel/traverse": "workspace:^"
"@babel/types": "workspace:^"
"@jridgewell/trace-mapping": ^0.3.4
"@types/convert-source-map": ^1.5.1
"@types/debug": ^4.1.0
"@types/resolve": ^1.3.2
"@types/semver": ^5.4.0
"@types/source-map": ^0.5.0
convert-source-map: ^1.7.0
debug: ^4.1.0
gensync: ^1.0.0-beta.2
json5: ^2.1.2
semver: "condition:BABEL_8_BREAKING ? ^7.3.4 : ^6.3.0"
source-map: 0.6.1
languageName: unknown
linkType: soft

Expand Down Expand Up @@ -457,6 +457,7 @@ __metadata:
"@babel/helper-fixtures": "workspace:^"
"@babel/parser": "workspace:^"
"@babel/types": "workspace:^"
"@jridgewell/trace-mapping": ^0.3.4
"@types/jsesc": ^2.5.0
"@types/source-map": ^0.5.0
charcodes: ^0.2.0
Expand Down Expand Up @@ -955,11 +956,11 @@ __metadata:
"@babel/code-frame": "workspace:^"
"@babel/core": "workspace:^"
"@babel/helper-fixtures": "workspace:^"
"@jridgewell/trace-mapping": ^0.3.4
"@types/jest": ^25.2.2
babel-check-duplicated-nodes: ^1.0.0
quick-lru: 5.1.0
regenerator-runtime: ^0.13.7
source-map: ^0.5.0
languageName: unknown
linkType: soft

Expand Down Expand Up @@ -4097,13 +4098,13 @@ __metadata:
languageName: node
linkType: hard

"@jridgewell/trace-mapping@npm:^0.3.0":
version: 0.3.2
resolution: "@jridgewell/trace-mapping@npm:0.3.2"
"@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:
"@jridgewell/resolve-uri": ^3.0.3
"@jridgewell/sourcemap-codec": ^1.4.10
checksum: b58be6b4133cbcb20bfd28c9ca843b8db9efa0bf1d7e0e9e26b2228dace94ad53161c996ab1d762d7c3955dfc398a7734e7b84a2493ae36b451f232234fbb257
checksum: ab8bce84bbbc8c34f3ba8325ed926f8f2d3098983c10442a80c55764c4eb6e47d5b92d8ff20a0dd868c3e76a3535651fd8a0138182c290dbfc8396195685c37b
languageName: node
linkType: hard

Expand Down Expand Up @@ -14153,20 +14154,20 @@ fsevents@^1.2.7:
languageName: node
linkType: hard

"source-map@npm:0.6.1, source-map@npm:^0.6.0, source-map@npm:^0.6.1, source-map@npm:~0.6.1":
version: 0.6.1
resolution: "source-map@npm:0.6.1"
checksum: 59ce8640cf3f3124f64ac289012c2b8bd377c238e316fb323ea22fbfe83da07d81e000071d7242cad7a23cd91c7de98e4df8830ec3f133cb6133a5f6e9f67bc2
languageName: node
linkType: hard

"source-map@npm:^0.5.0, source-map@npm:^0.5.3, source-map@npm:^0.5.6, source-map@npm:~0.5.1, source-map@npm:~0.5.3":
version: 0.5.7
resolution: "source-map@npm:0.5.7"
checksum: 5dc2043b93d2f194142c7f38f74a24670cd7a0063acdaf4bf01d2964b402257ae843c2a8fa822ad5b71013b5fcafa55af7421383da919752f22ff488bc553f4d
languageName: node
linkType: hard

"source-map@npm:^0.6.0, source-map@npm:^0.6.1, source-map@npm:~0.6.1":
version: 0.6.1
resolution: "source-map@npm:0.6.1"
checksum: 59ce8640cf3f3124f64ac289012c2b8bd377c238e316fb323ea22fbfe83da07d81e000071d7242cad7a23cd91c7de98e4df8830ec3f133cb6133a5f6e9f67bc2
languageName: node
linkType: hard

"source-map@npm:^0.7.3, source-map@npm:~0.7.2":
version: 0.7.3
resolution: "source-map@npm:0.7.3"
Expand Down