Skip to content

Commit

Permalink
Merge pull request #7761 from loganfsmyth/bad-map-merge
Browse files Browse the repository at this point in the history
Reimplement input sourcemap merging using range matching instead of closest-position matching
  • Loading branch information
loganfsmyth committed Apr 25, 2018
2 parents 0112479 + cfb386f commit 138d609
Show file tree
Hide file tree
Showing 13 changed files with 769 additions and 55 deletions.
106 changes: 104 additions & 2 deletions lib/third-party-libs.js.flow
Expand Up @@ -26,22 +26,124 @@ declare module "lodash/defaults" {

declare module "lodash/clone" {
declare export default <T>(obj: T) => T;
}
}

declare module "lodash/merge" {
declare export default <T: Object>(T, Object) => T;
}

declare module "convert-source-map" {
declare module "source-map" {
declare export type SourceMap = {
version: 3,
file: ?string,
sourceRoot: ?string,
sources: [?string],
sourcesContent: [?string],
names: [?string],
mappings: string,
};

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,
sourceRoot?: string | null,
skipValidation?: boolean | null,
}): this;

addMapping({
generated: {
line: number,
column: number,
}
}): void;

setSourceContent(string, string): void;

toJSON(): SourceMap;
}
}

declare module "convert-source-map" {
import type { SourceMap } from "source-map";

declare class Converter {
toJSON(): string;
toBase64(): string;
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 1 addition & 44 deletions packages/babel-core/src/transformation/file/generate.js
Expand Up @@ -2,10 +2,10 @@

import type { PluginPasses } from "../../config";
import convertSourceMap, { type SourceMap } from "convert-source-map";
import sourceMap from "source-map";
import generate from "@babel/generator";

import type File from "./file";
import mergeSourceMap from "./merge-map";

export default function generateCode(
pluginPasses: PluginPasses,
Expand Down Expand Up @@ -72,46 +72,3 @@ export default function generateCode(

return { outputCode, outputMap };
}

function mergeSourceMap(inputMap: SourceMap, map: SourceMap): SourceMap {
const inputMapConsumer = new sourceMap.SourceMapConsumer(inputMap);
const outputMapConsumer = new sourceMap.SourceMapConsumer(map);

const mergedGenerator = new sourceMap.SourceMapGenerator({
file: inputMapConsumer.file,
sourceRoot: inputMapConsumer.sourceRoot,
});

// This assumes the output map always has a single source, since Babel always compiles a
// single source file to a single output file.
const source = outputMapConsumer.sources[0];

inputMapConsumer.eachMapping(function(mapping) {
const generatedPosition = outputMapConsumer.generatedPositionFor({
line: mapping.generatedLine,
column: mapping.generatedColumn,
source: source,
});
if (generatedPosition.column != null) {
mergedGenerator.addMapping({
source: mapping.source,

original:
mapping.source == null
? null
: {
line: mapping.originalLine,
column: mapping.originalColumn,
},

generated: generatedPosition,

name: mapping.name,
});
}
});

const mergedMap = mergedGenerator.toJSON();
inputMap.mappings = mergedMap.mappings;
return inputMap;
}

0 comments on commit 138d609

Please sign in to comment.