From 7b0a4672abddc6b834df7fa477763bad47d1edb9 Mon Sep 17 00:00:00 2001 From: Justin Ridgewell Date: Tue, 8 Feb 2022 23:58:03 -0500 Subject: [PATCH] Convert all SourceMapConsumers to TraceMaps --- lib/third-party-libs.js.flow | 74 ------------------- packages/babel-cli/package.json | 1 + packages/babel-cli/src/babel/file.ts | 17 ++--- packages/babel-core/package.json | 2 +- packages/babel-generator/package.json | 2 +- .../package.json | 2 +- yarn.lock | 22 +++--- 7 files changed, 23 insertions(+), 97 deletions(-) diff --git a/lib/third-party-libs.js.flow b/lib/third-party-libs.js.flow index 9ce36c6dc427..c45b2d68c220 100644 --- a/lib/third-party-libs.js.flow +++ b/lib/third-party-libs.js.flow @@ -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; - - 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, diff --git a/packages/babel-cli/package.json b/packages/babel-cli/package.json index b503acb9bf98..9d434a02e7d2 100644 --- a/packages/babel-cli/package.json +++ b/packages/babel-cli/package.json @@ -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", diff --git a/packages/babel-cli/src/babel/file.ts b/packages/babel-cli/src/babel/file.ts index 24f6a77f7951..ac80c9caba42 100644 --- a/packages/babel-cli/src/babel/file.ts +++ b/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"; @@ -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(); - - 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, @@ -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; diff --git a/packages/babel-core/package.json b/packages/babel-core/package.json index 3fd66c0d5a14..3ad8b16458fe 100644 --- a/packages/babel-core/package.json +++ b/packages/babel-core/package.json @@ -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", diff --git a/packages/babel-generator/package.json b/packages/babel-generator/package.json index 02e3a93a618b..25756002d651 100644 --- a/packages/babel-generator/package.json +++ b/packages/babel-generator/package.json @@ -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" diff --git a/packages/babel-helper-transform-fixture-test-runner/package.json b/packages/babel-helper-transform-fixture-test-runner/package.json index 22a7ed4283b9..c3f447a813a7 100644 --- a/packages/babel-helper-transform-fixture-test-runner/package.json +++ b/packages/babel-helper-transform-fixture-test-runner/package.json @@ -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" diff --git a/yarn.lock b/yarn.lock index 0e340a7a583e..4cf92955cd37 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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: @@ -14173,13 +14175,6 @@ 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" @@ -14187,6 +14182,13 @@ fsevents@^1.2.7: 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"