Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
ab7cb27
Add compiler option to enable declaration sourcemaps
weswigham Feb 15, 2018
91cc19c
Transparent goto definition for sourcemapped declaration files
weswigham Feb 17, 2018
c291828
Post-rebase touchups
weswigham Mar 16, 2018
2b26a27
Rename API methods
weswigham Mar 16, 2018
ec44da5
Fix lints
weswigham Mar 16, 2018
ffb7f85
Fix typo in name XD
weswigham Mar 16, 2018
589ac28
Log sourcemap decode errors
weswigham Mar 16, 2018
5584a2d
Share the cache more, but also invalidate it more
weswigham Mar 16, 2018
238ba98
Remove todo
weswigham Mar 16, 2018
ff158cf
Enable mapping on go to implementation as well
weswigham Mar 17, 2018
b9f6149
Allow fourslash to test declaration maps mroe easily
weswigham Mar 19, 2018
53f72de
more test
weswigham Mar 19, 2018
29c732e
Merge branch 'master' into decl-maps
weswigham Mar 19, 2018
6070108
Handle sourceRoot
weswigham Mar 19, 2018
0a63553
Add tests documenting current behavior with other sourcemapping flags
weswigham Mar 19, 2018
d8480b2
Ignore inline options for declaration file maps, simplify dispatch in…
weswigham Mar 20, 2018
ca88d5e
Change program diagnostic
weswigham Mar 20, 2018
2b231d7
Fix nit
weswigham Mar 20, 2018
fed6132
Use charCodeAt
weswigham Mar 20, 2018
509e4f3
Rename internal methods + veriables
weswigham Mar 20, 2018
0c44ba1
Avoid filter
weswigham Mar 20, 2018
c8620e3
span -> position
weswigham Mar 20, 2018
5687e10
Use character codes
weswigham Mar 20, 2018
e64e73d
Dont parse our sourcemap names until we need to start using them
weswigham Mar 20, 2018
47e701a
zero-index parsed positions
weswigham Mar 20, 2018
6a467ba
Handle sourceMappingURL comments, including base64 encoded ones
weswigham Mar 20, 2018
f2c8d3f
Unittest b64 decoder, make mroe robust to handle unicode properly
weswigham Mar 20, 2018
bd9cccf
Fix lint
weswigham Mar 20, 2018
c7a5296
declarationMaps -> declarationMap
weswigham Mar 20, 2018
f6e81f2
Merge branch 'master' into decl-maps
weswigham Mar 24, 2018
d0a5e28
Even more feedback
weswigham Mar 24, 2018
943dc12
USE Mroe lenient combined regexp
weswigham Mar 26, 2018
265cc1a
only match base64 characters
weswigham Mar 26, 2018
e34a6bd
Fix nit
weswigham Mar 26, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Ignore inline options for declaration file maps, simplify dispatch in…
… emitter
  • Loading branch information
weswigham committed Mar 20, 2018
commit d8480b2515da9996a17b4496b1b3cf398d363924
61 changes: 25 additions & 36 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ namespace ts {
}

function getSourceMapFilePath(jsFilePath: string, options: CompilerOptions) {
return options.sourceMap ? jsFilePath + ".map" : undefined;
return (options.sourceMap && !options.inlineSourceMap) ? jsFilePath + ".map" : undefined;
}

// JavaScript files are always LanguageVariant.JSX, as JSX syntax is allowed in .js files also.
Expand All @@ -85,13 +85,6 @@ namespace ts {
return Extension.Js;
}

const enum SourceMapEmitKind {
None,
File,
Inline,
DeclarationFile
}

/*@internal*/
// targetSourceFile is when users only want one file in entire project to be emitted. This is used in compileOnSave feature
export function emitFiles(resolver: EmitResolver, host: EmitHost, targetSourceFile: SourceFile, emitOnlyDtsFiles?: boolean, transformers?: TransformerFactory<SourceFile>[]): EmitResult {
Expand All @@ -103,6 +96,13 @@ namespace ts {
const newLine = host.getNewLine();
const writer = createTextWriter(newLine);
const sourceMap = createSourceMapWriter(host, writer);
const declarationSourceMap = createSourceMapWriter(host, writer, {
sourceMap: compilerOptions.declarationMaps,
sourceRoot: compilerOptions.sourceRoot,
mapRoot: compilerOptions.mapRoot,
extendedDiagnostics: compilerOptions.extendedDiagnostics,
// Explicitly do not passthru either `inline` option
});

let currentSourceFile: SourceFile;
let bundledHelpers: Map<boolean>;
Expand Down Expand Up @@ -171,8 +171,7 @@ namespace ts {
onSetSourceFile: setSourceFile,
});

const sourcemapKind = compilerOptions.inlineSourceMap ? SourceMapEmitKind.Inline : compilerOptions.sourceMap ? SourceMapEmitKind.File : SourceMapEmitKind.None;
printSourceFileOrBundle(jsFilePath, sourceMapFilePath, isSourceFile(sourceFileOrBundle) ? transform.transformed[0] : createBundle(transform.transformed), printer, sourcemapKind);
printSourceFileOrBundle(jsFilePath, sourceMapFilePath, isSourceFile(sourceFileOrBundle) ? transform.transformed[0] : createBundle(transform.transformed), printer, sourceMap);

// Clean up emit nodes on parse tree
transform.dispose();
Expand All @@ -197,10 +196,10 @@ namespace ts {
hasGlobalName: resolver.hasGlobalName,

// sourcemap hooks
onEmitSourceMapOfNode: sourceMap.emitNodeWithSourceMap,
onEmitSourceMapOfToken: sourceMap.emitTokenWithSourceMap,
onEmitSourceMapOfPosition: sourceMap.emitPos,
onSetSourceFile: setSourceFile,
onEmitSourceMapOfNode: declarationSourceMap.emitNodeWithSourceMap,
onEmitSourceMapOfToken: declarationSourceMap.emitTokenWithSourceMap,
onEmitSourceMapOfPosition: declarationSourceMap.emitPos,
onSetSourceFile: setSourceFileDeclarationMaps,

// transform hooks
onEmitNode: declarationTransform.emitNodeWithNotification,
Expand All @@ -209,21 +208,16 @@ namespace ts {
const declBlocked = (!!declarationTransform.diagnostics && !!declarationTransform.diagnostics.length) || !!host.isEmitBlocked(declarationFilePath) || !!compilerOptions.noEmit;
emitSkipped = emitSkipped || declBlocked;
if (!declBlocked || emitOnlyDtsFiles) {
printSourceFileOrBundle(declarationFilePath, declarationMapPath, declarationTransform.transformed[0], declarationPrinter, getAreDeclarationMapsEnabled(compilerOptions) ? SourceMapEmitKind.DeclarationFile : SourceMapEmitKind.None);
printSourceFileOrBundle(declarationFilePath, declarationMapPath, declarationTransform.transformed[0], declarationPrinter, declarationSourceMap);
}
declarationTransform.dispose();
}

function printSourceFileOrBundle(jsFilePath: string, sourceMapFilePath: string | undefined, sourceFileOrBundle: SourceFile | Bundle, printer: Printer, sourcemapKind: SourceMapEmitKind) {
function printSourceFileOrBundle(jsFilePath: string, sourceMapFilePath: string | undefined, sourceFileOrBundle: SourceFile | Bundle, printer: Printer, mapRecorder: SourceMapWriter) {
const bundle = sourceFileOrBundle.kind === SyntaxKind.Bundle ? sourceFileOrBundle : undefined;
const sourceFile = sourceFileOrBundle.kind === SyntaxKind.SourceFile ? sourceFileOrBundle : undefined;
const sourceFiles = bundle ? bundle.sourceFiles : [sourceFile];
if (sourcemapKind !== SourceMapEmitKind.None) {
sourceMap.initialize(jsFilePath, sourceMapFilePath, sourceFileOrBundle);
}
else {
sourceMap.setState(/*disabled*/ true);
}
mapRecorder.initialize(jsFilePath, sourceMapFilePath || "", sourceFileOrBundle, sourceMapDataList);

if (bundle) {
bundledHelpers = createMap<boolean>();
Expand All @@ -237,31 +231,21 @@ namespace ts {

writer.writeLine();

const sourceMappingURL = sourceMap.getSourceMappingURL();
const sourceMappingURL = mapRecorder.getSourceMappingURL();
if (sourceMappingURL) {
writer.write(`//# ${"sourceMappingURL"}=${sourceMappingURL}`); // Sometimes tools can sometimes see this line as a source mapping url comment
}

// Write the source map
if (sourceMapFilePath && (sourcemapKind === SourceMapEmitKind.File || sourcemapKind === SourceMapEmitKind.DeclarationFile)) {
writeFile(host, emitterDiagnostics, sourceMapFilePath, sourceMap.getText(), /*writeByteOrderMark*/ false, sourceFiles);
}

// Record source map data for the test harness.
if (sourcemapKind !== SourceMapEmitKind.None) {
sourceMapDataList.push(sourceMap.getSourceMapData());
if (sourceMapFilePath) {
writeFile(host, emitterDiagnostics, sourceMapFilePath, mapRecorder.getText(), /*writeByteOrderMark*/ false, sourceFiles);
}

// Write the output file
writeFile(host, emitterDiagnostics, jsFilePath, writer.getText(), compilerOptions.emitBOM, sourceFiles);

// Reset state
if (sourcemapKind !== SourceMapEmitKind.None) {
sourceMap.reset();
}
else {
sourceMap.setState(/*disabled*/ false);
}
mapRecorder.reset();
writer.clear();

currentSourceFile = undefined;
Expand All @@ -274,6 +258,11 @@ namespace ts {
sourceMap.setSourceFile(node);
}

function setSourceFileDeclarationMaps(node: SourceFile) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function name isn't very clear.

currentSourceFile = node;
declarationSourceMap.setSourceFile(node);
}

function emitHelpers(node: Node, writeLines: (text: string) => void) {
let helpersEmitted = false;
const bundle = node.kind === SyntaxKind.Bundle ? <Bundle>node : undefined;
Expand Down
42 changes: 20 additions & 22 deletions src/compiler/sourcemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace ts {
* @param sourceMapFilePath The path to the output source map file.
* @param sourceFileOrBundle The input source file or bundle for the program.
*/
initialize(filePath: string, sourceMapFilePath: string, sourceFileOrBundle: SourceFile | Bundle): void;
initialize(filePath: string, sourceMapFilePath: string, sourceFileOrBundle: SourceFile | Bundle, sourceMapOutput?: SourceMapData[]): void;

/**
* Reset the SourceMapWriter to an empty state.
Expand Down Expand Up @@ -62,16 +62,6 @@ namespace ts {
* Gets the SourceMappingURL for the source map.
*/
getSourceMappingURL(): string;

/**
* Gets test data for source maps.
*/
getSourceMapData(): SourceMapData;

/**
* @returns the previous disabled state
*/
setState(disabled: boolean): boolean;
}

// Used for initialize lastEncodedSourceMapSpan and reset lastEncodedSourceMapSpan when updateLastEncodedAndRecordedSpans
Expand All @@ -83,8 +73,16 @@ namespace ts {
sourceIndex: 0
};

export function createSourceMapWriter(host: EmitHost, writer: EmitTextWriter): SourceMapWriter {
const compilerOptions = host.getCompilerOptions();
export interface SourceMapOptions {
sourceMap?: boolean;
inlineSourceMap?: boolean;
inlineSources?: boolean;
sourceRoot?: string;
mapRoot?: string;
extendedDiagnostics?: boolean;
}

export function createSourceMapWriter(host: EmitHost, writer: EmitTextWriter, compilerOptions: SourceMapOptions = host.getCompilerOptions()): SourceMapWriter {
const extendedDiagnostics = compilerOptions.extendedDiagnostics;
let currentSource: SourceMapSource;
let currentSourceText: string;
Expand All @@ -100,27 +98,20 @@ namespace ts {

// Source map data
let sourceMapData: SourceMapData;
let sourceMapDataList: SourceMapData[];
let disabled: boolean = !(compilerOptions.sourceMap || compilerOptions.inlineSourceMap);

return {
initialize,
reset,
getSourceMapData: () => sourceMapData,
setSourceFile,
emitPos,
emitNodeWithSourceMap,
emitTokenWithSourceMap,
getText,
getSourceMappingURL,
setState,
};

function setState(state: boolean) {
const last = disabled;
disabled = state;
return last;
}

/**
* Skips trivia such as comments and white-space that can optionally overriden by the source map source
*/
Expand All @@ -135,14 +126,15 @@ namespace ts {
* @param sourceMapFilePath The path to the output source map file.
* @param sourceFileOrBundle The input source file or bundle for the program.
*/
function initialize(filePath: string, sourceMapFilePath: string, sourceFileOrBundle: SourceFile | Bundle) {
function initialize(filePath: string, sourceMapFilePath: string, sourceFileOrBundle: SourceFile | Bundle, outputSourceMapDataList?: SourceMapData[]) {
if (disabled) {
return;
}

if (sourceMapData) {
reset();
}
sourceMapDataList = outputSourceMapDataList;

currentSource = undefined;
currentSourceText = undefined;
Expand Down Expand Up @@ -211,13 +203,19 @@ namespace ts {
return;
}

// Record source map data for the test harness.
if (sourceMapDataList) {
sourceMapDataList.push(sourceMapData);
}

currentSource = undefined;
sourceMapDir = undefined;
sourceMapSourceIndex = undefined;
lastRecordedSourceMapSpan = undefined;
lastEncodedSourceMapSpan = undefined;
lastEncodedNameIndex = undefined;
sourceMapData = undefined;
sourceMapDataList = undefined;
}

// Encoding for sourcemap span
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2818,7 +2818,7 @@ namespace ts {

export interface EmitFileNames {
jsFilePath: string;
sourceMapFilePath: string;
sourceMapFilePath: string | undefined;
declarationFilePath: string | undefined;
declarationMapPath: string | undefined;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ export declare class Foo {
export interface SomeType {
member: number;
}
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3Rlc3RzL2Nhc2VzL2ZvdXJzbGFzaC9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQTtJQUNJLE1BQU0sRUFBRSxNQUFNLENBQUM7SUFDZixVQUFVLENBQUMsUUFBUSxFQUFFLFFBQVEsR0FBRyxJQUFJO0lBQ3BDLFdBQVc7Ozs7Ozs7Q0FNZDtBQUVELE1BQU0sV0FBVyxRQUFRO0lBQ3JCLE1BQU0sRUFBRSxNQUFNLENBQUM7Q0FDbEIifQ==
//# sourceMappingURL=index.d.ts.map
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var Foo = /** @class */ (function () {
}());
exports.Foo = Foo;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90ZXN0cy9jYXNlcy9mb3Vyc2xhc2gvaW5kZXgudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7QUFBQTtJQUFBO0lBU0EsQ0FBQztJQVBHLHdCQUFVLEdBQVYsVUFBVyxRQUFrQixJQUFTLENBQUM7SUFDdkMseUJBQVcsR0FBWDtRQUNJLElBQUksSUFBSSxDQUFDLE1BQU0sRUFBRSxHQUFHLEdBQUcsRUFBRTtZQUNyQixPQUFPLEVBQUMsQ0FBQyxFQUFFLEVBQUUsRUFBQyxDQUFDO1NBQ2xCO1FBQ0QsT0FBTyxFQUFDLENBQUMsRUFBRSxLQUFLLEVBQUMsQ0FBQztJQUN0QixDQUFDO0lBQ0wsVUFBQztBQUFELENBQUMsQUFURCxJQVNDO0FBVFksa0JBQUciLCJzb3VyY2VzQ29udGVudCI6WyJleHBvcnQgY2xhc3MgRm9vIHtcbiAgICBtZW1iZXI6IHN0cmluZztcbiAgICBtZXRob2ROYW1lKHByb3BOYW1lOiBTb21lVHlwZSk6IHZvaWQge31cbiAgICBvdGhlck1ldGhvZCgpIHtcbiAgICAgICAgaWYgKE1hdGgucmFuZG9tKCkgPiAwLjUpIHtcbiAgICAgICAgICAgIHJldHVybiB7eDogNDJ9O1xuICAgICAgICB9XG4gICAgICAgIHJldHVybiB7eTogXCJ5ZXNcIn07XG4gICAgfVxufVxuXG5leHBvcnQgaW50ZXJmYWNlIFNvbWVUeXBlIHtcbiAgICBtZW1iZXI6IG51bWJlcjtcbn0iXX0=FileName : ./dist/index.d.ts.map
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../tests/cases/fourslash/index.ts"],"names":[],"mappings":"AAAA;IACI,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,CAAC,QAAQ,EAAE,QAAQ,GAAG,IAAI;IACpC,WAAW;;;;;;;CAMd;AAED,MAAM,WAAW,QAAQ;IACrB,MAAM,EAAE,MAAM,CAAC;CAClB","sourcesContent":["export class Foo {\n member: string;\n methodName(propName: SomeType): void {}\n otherMethod() {\n if (Math.random() > 0.5) {\n return {x: 42};\n }\n return {y: \"yes\"};\n }\n}\n\nexport interface SomeType {\n member: number;\n}"]}FileName : ./dist/index.d.ts
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../tests/cases/fourslash/index.ts"],"names":[],"mappings":"AAAA;IACI,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,CAAC,QAAQ,EAAE,QAAQ,GAAG,IAAI;IACpC,WAAW;;;;;;;CAMd;AAED,MAAM,WAAW,QAAQ;IACrB,MAAM,EAAE,MAAM,CAAC;CAClB"}FileName : ./dist/index.d.ts
export declare class Foo {
member: string;
methodName(propName: SomeType): void;
Expand All @@ -31,4 +31,4 @@ export declare class Foo {
export interface SomeType {
member: number;
}
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3Rlc3RzL2Nhc2VzL2ZvdXJzbGFzaC9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQTtJQUNJLE1BQU0sRUFBRSxNQUFNLENBQUM7SUFDZixVQUFVLENBQUMsUUFBUSxFQUFFLFFBQVEsR0FBRyxJQUFJO0lBQ3BDLFdBQVc7Ozs7Ozs7Q0FNZDtBQUVELE1BQU0sV0FBVyxRQUFRO0lBQ3JCLE1BQU0sRUFBRSxNQUFNLENBQUM7Q0FDbEIiLCJzb3VyY2VzQ29udGVudCI6WyJleHBvcnQgY2xhc3MgRm9vIHtcbiAgICBtZW1iZXI6IHN0cmluZztcbiAgICBtZXRob2ROYW1lKHByb3BOYW1lOiBTb21lVHlwZSk6IHZvaWQge31cbiAgICBvdGhlck1ldGhvZCgpIHtcbiAgICAgICAgaWYgKE1hdGgucmFuZG9tKCkgPiAwLjUpIHtcbiAgICAgICAgICAgIHJldHVybiB7eDogNDJ9O1xuICAgICAgICB9XG4gICAgICAgIHJldHVybiB7eTogXCJ5ZXNcIn07XG4gICAgfVxufVxuXG5leHBvcnQgaW50ZXJmYWNlIFNvbWVUeXBlIHtcbiAgICBtZW1iZXI6IG51bWJlcjtcbn0iXX0=
//# sourceMappingURL=index.d.ts.map