Skip to content

Commit

Permalink
Convert all SourceMapConsumers to TraceMaps
Browse files Browse the repository at this point in the history
  • Loading branch information
jridgewell committed Feb 9, 2022
1 parent ca611b2 commit 7b0a467
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 97 deletions.
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.3",
"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
2 changes: 1 addition & 1 deletion packages/babel-core/package.json
Expand Up @@ -67,7 +67,7 @@
"devDependencies": {
"@babel/helper-transform-fixture-test-runner": "workspace:^",
"@babel/plugin-transform-modules-commonjs": "workspace:^",
"@jridgewell/trace-mapping": "^0.3.2",
"@jridgewell/trace-mapping": "^0.3.3",
"@types/convert-source-map": "^1.5.1",
"@types/debug": "^4.1.0",
"@types/resolve": "^1.3.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-generator/package.json
Expand Up @@ -26,7 +26,7 @@
"devDependencies": {
"@babel/helper-fixtures": "workspace:^",
"@babel/parser": "workspace:^",
"@jridgewell/trace-mapping": "^0.3.2",
"@jridgewell/trace-mapping": "^0.3.3",
"@types/jsesc": "^2.5.0",
"@types/source-map": "^0.5.0",
"charcodes": "^0.2.0"
Expand Down
Expand Up @@ -18,7 +18,7 @@
"@babel/code-frame": "workspace:^",
"@babel/core": "workspace:^",
"@babel/helper-fixtures": "workspace:^",
"@jridgewell/trace-mapping": "^0.3.2",
"@jridgewell/trace-mapping": "^0.3.3",
"babel-check-duplicated-nodes": "^1.0.0",
"quick-lru": "5.1.0",
"regenerator-runtime": "^0.13.7"
Expand Down
22 changes: 12 additions & 10 deletions yarn.lock
Expand Up @@ -212,6 +212,7 @@ __metadata:
dependencies:
"@babel/core": "workspace:^"
"@babel/helper-fixtures": "workspace:^"
"@jridgewell/trace-mapping": ^0.3.3
"@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 @@ -344,6 +345,7 @@ __metadata:
"@babel/template": "workspace:^"
"@babel/traverse": "workspace:^"
"@babel/types": "workspace:^"
"@jridgewell/trace-mapping": ^0.3.3
"@types/convert-source-map": ^1.5.1
"@types/debug": ^4.1.0
"@types/resolve": ^1.3.2
Expand All @@ -354,7 +356,6 @@ __metadata:
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 @@ -467,6 +468,7 @@ __metadata:
"@babel/helper-fixtures": "workspace:^"
"@babel/parser": "workspace:^"
"@babel/types": "workspace:^"
"@jridgewell/trace-mapping": ^0.3.3
"@types/jsesc": ^2.5.0
"@types/source-map": ^0.5.0
charcodes: ^0.2.0
Expand Down Expand Up @@ -965,7 +967,7 @@ __metadata:
"@babel/code-frame": "workspace:^"
"@babel/core": "workspace:^"
"@babel/helper-fixtures": "workspace:^"
"@jridgewell/trace-mapping": ^0.3.2
"@jridgewell/trace-mapping": ^0.3.3
"@types/jest": ^25.2.2
babel-check-duplicated-nodes: ^1.0.0
quick-lru: 5.1.0
Expand Down Expand Up @@ -4117,7 +4119,7 @@ __metadata:
languageName: node
linkType: hard

"@jridgewell/trace-mapping@npm:^0.3.0, @jridgewell/trace-mapping@npm:^0.3.2":
"@jridgewell/trace-mapping@npm:^0.3.0, @jridgewell/trace-mapping@npm:^0.3.3":
version: 0.3.3
resolution: "@jridgewell/trace-mapping@npm:0.3.3"
dependencies:
Expand Down Expand Up @@ -14173,20 +14175,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

0 comments on commit 7b0a467

Please sign in to comment.