Skip to content

Commit

Permalink
Simplify identity source map generator
Browse files Browse the repository at this point in the history
  • Loading branch information
aleclarson committed Jun 18, 2018
1 parent 4f9ffde commit be52a14
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 20 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@
"commander": "^2.12.2",
"lines-and-columns": "^1.1.6",
"mz": "^2.7.0",
"pirates": "^3.0.2",
"source-map": "^0.7.3"
"pirates": "^3.0.2"
},
"engines": {
"node": ">=8"
Expand Down
31 changes: 19 additions & 12 deletions src/computeSourceMap.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import sourceMap, {RawSourceMap} from "source-map";
import {SourceMapOptions} from "./index";

export interface RawSourceMap {
version: number;
file: string;
sources: Array<string>;
sourceRoot?: string;
sourcesContent?: Array<string>;
mappings: string;
names: Array<string>;
}

/**
* Generate a simple source map indicating that each line maps directly to the original line.
*/
Expand All @@ -9,19 +18,17 @@ export default function computeSourceMap(
filePath: string,
{compiledFilename}: SourceMapOptions,
): RawSourceMap {
const mapGenerator = new sourceMap.SourceMapGenerator({file: compiledFilename});
let numLines = 1;
let mappings = "AAAA";
for (let i = 0; i < code.length; i++) {
if (code[i] === "\n") {
numLines++;
mappings += ";AACA";
}
}
for (let line = 1; line <= numLines; line++) {
mapGenerator.addMapping({
source: filePath,
generated: {line, column: 0},
original: {line, column: 0},
});
}
return mapGenerator.toJSON();
return {
version: 3,
file: compiledFilename || "",
sources: [filePath],
mappings,
names: [],
};
}
3 changes: 1 addition & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {RawSourceMap} from "source-map";
import CJSImportProcessor from "./CJSImportProcessor";
import computeSourceMap from "./computeSourceMap";
import computeSourceMap, {RawSourceMap} from "./computeSourceMap";
import identifyShadowedGlobals from "./identifyShadowedGlobals";
import NameManager from "./NameManager";
import {parse} from "./parser";
Expand Down
4 changes: 0 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2665,10 +2665,6 @@ source-map@^0.6.1:
version "0.6.1"
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263"

source-map@^0.7.3:
version "0.7.3"
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.7.3.tgz#5302f8169031735226544092e64981f751750383"

spawn-wrap@^1.4.2:
version "1.4.2"
resolved "https://registry.yarnpkg.com/spawn-wrap/-/spawn-wrap-1.4.2.tgz#cff58e73a8224617b6561abdc32586ea0c82248c"
Expand Down

0 comments on commit be52a14

Please sign in to comment.