-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Progress toward #223 This is a first pass that's meant to be simple, flexible, and fairly unopinionated. Notably, it does NOT yet compute mappings within each line, and instead just maps each line to the corresponding line in the source. I'll implement smarter mappings in the future, possibly as an option if it ends up hurting performance. A quick benchmark shows that enabling source maps increases total running time by about 25%, even with this simple approach. Hopefully that can be optimized in the future. Another possible to-do item is for Sucrase to accept a source map which it then modifies. However, this can hopefully be handled by other tools that compose source maps. There are also various tweaks that may make this more convenient, depending on use case. * Optionally include the original source code in `sourcesContent` in the source map. * Optionally append a sourceMappingURL comment to the end of the source code. * Optionally return the source map as a string instead of an object. * Better control over the sourceRoot and file paths specified in the source map. All of these use cases can be satisfied with just one or two lines of code after running Sucrase, though, so none seem super critical. Some docs of other tools emitting source maps, which I used for inspiration: https://babeljs.io/docs/en/babel-core https://www.typescriptlang.org/docs/handbook/compiler-options.html
- Loading branch information
1 parent
61613d3
commit 70cf9c3
Showing
7 changed files
with
1,177 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ self-build | |
dist | ||
example-runner/example-repos | ||
integrations/gulp-plugin/dist | ||
.nyc_output |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import sourceMap, {RawSourceMap} from "source-map"; | ||
|
||
/** | ||
* Generate a simple source map indicating that each line maps directly to the original line. | ||
*/ | ||
export default function computeSourceMap(code: string, filePath: string): RawSourceMap { | ||
const mapGenerator = new sourceMap.SourceMapGenerator({file: filePath}); | ||
let numLines = 1; | ||
for (let i = 0; i < code.length; i++) { | ||
if (code[i] === "\n") { | ||
numLines++; | ||
} | ||
} | ||
for (let line = 1; line <= numLines; line++) { | ||
mapGenerator.addMapping({ | ||
source: filePath, | ||
generated: {line, column: 0}, | ||
original: {line, column: 0}, | ||
}); | ||
} | ||
return mapGenerator.toJSON(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import * as assert from "assert"; | ||
import {transform} from "../src"; | ||
|
||
describe("source maps", () => { | ||
it("generates a simple line-based source map", () => { | ||
const result = transform( | ||
`\ | ||
import a from "./a"; | ||
const x: number = 1; | ||
console.log(x + 1); | ||
`, | ||
{transforms: ["imports", "typescript"], computeSourceMap: true, filePath: "test.ts"}, | ||
); | ||
assert.deepEqual(result.sourceMap, { | ||
version: 3, | ||
sources: ["test.ts"], | ||
names: [], | ||
mappings: "AAAA;AACA;AACA;AACA", | ||
file: "test.ts", | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.