Skip to content

Commit

Permalink
Merge pull request #34495 from microsoft/forceDts
Browse files Browse the repository at this point in the history
Add forceDtsEmit flag to getEmitOutput
  • Loading branch information
sheetalkamat authored Oct 16, 2019
2 parents f24db4c + 850d16e commit aaadb17
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 10 deletions.
4 changes: 3 additions & 1 deletion src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,9 @@ namespace ts {
declarationFilePath: string | undefined,
declarationMapPath: string | undefined,
relativeToBuildInfo: (path: string) => string) {
if (!sourceFileOrBundle || !declarationFilePath) {
if (!sourceFileOrBundle) return;
if (!declarationFilePath) {
if (emitOnlyDtsFiles || compilerOptions.emitDeclarationOnly) emitSkipped = true;
return;
}
const sourceFiles = isSourceFile(sourceFileOrBundle) ? [sourceFileOrBundle] : sourceFileOrBundle.sourceFiles;
Expand Down
4 changes: 2 additions & 2 deletions src/services/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1638,12 +1638,12 @@ namespace ts {
return NavigateTo.getNavigateToItems(sourceFiles, program.getTypeChecker(), cancellationToken, searchValue, maxResultCount, excludeDtsFiles);
}

function getEmitOutput(fileName: string, emitOnlyDtsFiles = false) {
function getEmitOutput(fileName: string, emitOnlyDtsFiles?: boolean, forceDtsEmit?: boolean) {
synchronizeHostData();

const sourceFile = getValidSourceFile(fileName);
const customTransformers = host.getCustomTransformers && host.getCustomTransformers();
return getFileEmitOutput(program, sourceFile, emitOnlyDtsFiles, cancellationToken, customTransformers);
return getFileEmitOutput(program, sourceFile, !!emitOnlyDtsFiles, cancellationToken, customTransformers, forceDtsEmit);
}

// Signature help
Expand Down
2 changes: 1 addition & 1 deletion src/services/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ namespace ts {
organizeImports(scope: OrganizeImportsScope, formatOptions: FormatCodeSettings, preferences: UserPreferences | undefined): readonly FileTextChanges[];
getEditsForFileRename(oldFilePath: string, newFilePath: string, formatOptions: FormatCodeSettings, preferences: UserPreferences | undefined): readonly FileTextChanges[];

getEmitOutput(fileName: string, emitOnlyDtsFiles?: boolean): EmitOutput;
getEmitOutput(fileName: string, emitOnlyDtsFiles?: boolean, forceDtsEmit?: boolean): EmitOutput;

getProgram(): Program | undefined;

Expand Down
44 changes: 40 additions & 4 deletions src/testRunner/unittests/services/languageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ class Carousel<T> extends Vue {
"vue-class-component.d.ts": `import Vue from "./vue";
export function Component(x: Config): any;`
};
// Regression test for GH #18245 - bug in single line comment writer caused a debug assertion when attempting
// to write an alias to a module's default export was referrenced across files and had no default export
it("should be able to create a language service which can respond to deinition requests without throwing", () => {
const languageService = createLanguageService({

function createLanguageService() {
return ts.createLanguageService({
getCompilationSettings() {
return {};
},
Expand All @@ -39,8 +38,45 @@ export function Component(x: Config): any;`
return getDefaultLibFilePath(options);
},
});
}
// Regression test for GH #18245 - bug in single line comment writer caused a debug assertion when attempting
// to write an alias to a module's default export was referrenced across files and had no default export
it("should be able to create a language service which can respond to deinition requests without throwing", () => {
const languageService = createLanguageService();
const definitions = languageService.getDefinitionAtPosition("foo.ts", 160); // 160 is the latter `vueTemplateHtml` position
expect(definitions).to.exist; // eslint-disable-line no-unused-expressions
});

it("getEmitOutput on language service has way to force dts emit", () => {
const languageService = createLanguageService();
assert.deepEqual(
languageService.getEmitOutput(
"foo.ts",
/*emitOnlyDtsFiles*/ true
),
{
emitSkipped: true,
outputFiles: emptyArray,
exportedModulesFromDeclarationEmit: undefined
}
);

assert.deepEqual(
languageService.getEmitOutput(
"foo.ts",
/*emitOnlyDtsFiles*/ true,
/*forceDtsEmit*/ true
),
{
emitSkipped: false,
outputFiles: [{
name: "foo.d.ts",
text: "export {};\r\n",
writeByteOrderMark: false
}],
exportedModulesFromDeclarationEmit: undefined
}
);
});
});
}
2 changes: 1 addition & 1 deletion tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5073,7 +5073,7 @@ declare namespace ts {
getEditsForRefactor(fileName: string, formatOptions: FormatCodeSettings, positionOrRange: number | TextRange, refactorName: string, actionName: string, preferences: UserPreferences | undefined): RefactorEditInfo | undefined;
organizeImports(scope: OrganizeImportsScope, formatOptions: FormatCodeSettings, preferences: UserPreferences | undefined): readonly FileTextChanges[];
getEditsForFileRename(oldFilePath: string, newFilePath: string, formatOptions: FormatCodeSettings, preferences: UserPreferences | undefined): readonly FileTextChanges[];
getEmitOutput(fileName: string, emitOnlyDtsFiles?: boolean): EmitOutput;
getEmitOutput(fileName: string, emitOnlyDtsFiles?: boolean, forceDtsEmit?: boolean): EmitOutput;
getProgram(): Program | undefined;
dispose(): void;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5073,7 +5073,7 @@ declare namespace ts {
getEditsForRefactor(fileName: string, formatOptions: FormatCodeSettings, positionOrRange: number | TextRange, refactorName: string, actionName: string, preferences: UserPreferences | undefined): RefactorEditInfo | undefined;
organizeImports(scope: OrganizeImportsScope, formatOptions: FormatCodeSettings, preferences: UserPreferences | undefined): readonly FileTextChanges[];
getEditsForFileRename(oldFilePath: string, newFilePath: string, formatOptions: FormatCodeSettings, preferences: UserPreferences | undefined): readonly FileTextChanges[];
getEmitOutput(fileName: string, emitOnlyDtsFiles?: boolean): EmitOutput;
getEmitOutput(fileName: string, emitOnlyDtsFiles?: boolean, forceDtsEmit?: boolean): EmitOutput;
getProgram(): Program | undefined;
dispose(): void;
}
Expand Down

0 comments on commit aaadb17

Please sign in to comment.