Skip to content

Commit ac68f93

Browse files
author
Yui T
committed
Addres code review
1 parent e556eaf commit ac68f93

File tree

3 files changed

+39
-32
lines changed

3 files changed

+39
-32
lines changed

src/compiler/checker.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ module ts {
7171
var checker: TypeChecker = {
7272
getProgram: () => program,
7373
getDiagnostics: getDiagnostics,
74-
getDeclarationDiagnostics: getDeclarationDiagnosticsFromSourceFile,
74+
getDeclarationDiagnostics: getDeclarationDiagnostics,
7575
getGlobalDiagnostics: getGlobalDiagnostics,
7676
getNodeCount: () => sum(program.getSourceFiles(), "nodeCount"),
7777
getIdentifierCount: () => sum(program.getSourceFiles(), "identifierCount"),
@@ -7039,10 +7039,10 @@ module ts {
70397039
return getSortedDiagnostics();
70407040
}
70417041

7042-
function getDeclarationDiagnosticsFromSourceFile(targetSourceFile: SourceFile): Diagnostic[] {
7042+
function getDeclarationDiagnostics(targetSourceFile: SourceFile): Diagnostic[] {
70437043
var resolver = createResolver();
7044-
checkProgram();
7045-
return getDeclarationDiagnostics(program, resolver, targetSourceFile);
7044+
checkSourceFile(targetSourceFile);
7045+
return ts.getDeclarationDiagnostics(program, resolver, targetSourceFile);
70467046
}
70477047

70487048
function getGlobalDiagnostics(): Diagnostic[] {

src/compiler/emitter.ts

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ module ts {
1515
getIndent(): number;
1616
}
1717

18+
interface aliasDeclaration {
19+
declaration: ImportDeclaration;
20+
outputPos: number;
21+
indent: number;
22+
asynchronousOutput?: string; // If the output for alias was written asynchronously, the corresponding output
23+
}
24+
1825
var indentStrings: string[] = ["", " "];
1926
export function getIndentString(level: number) {
2027
if (indentStrings[level] === undefined) {
@@ -326,12 +333,7 @@ module ts {
326333

327334
var emitJsDocComments = compilerOptions.removeComments ? function (declaration: Declaration) { } : writeJsDocComments;
328335

329-
var aliasDeclarationEmitInfo: {
330-
declaration: ImportDeclaration;
331-
outputPos: number;
332-
indent: number;
333-
asynchronousOutput?: string; // If the output for alias was written asynchronously, the corresponding output
334-
}[] = [];
336+
var aliasDeclarationEmitInfo: aliasDeclaration[] = [];
335337

336338
var getSymbolVisibilityDiagnosticMessage: (symbolAccesibilityResult: SymbolAccessiblityResult) => {
337339
errorNode: Node;
@@ -1213,26 +1215,29 @@ module ts {
12131215
}
12141216
});
12151217
}
1216-
1217-
// TODO(shkamat): Should we not write any declaration file if any of them can produce error,
1218-
// or should we just not write this file like we are doing now
1219-
if (!reportedDeclarationError) {
1220-
var declarationOutput = referencePathsOutput;
1221-
var synchronousDeclarationOutput = writer.getText();
1222-
// apply additions
1223-
var appliedSyncOutputPos = 0;
1224-
forEach(aliasDeclarationEmitInfo, aliasEmitInfo => {
1225-
if (aliasEmitInfo.asynchronousOutput) {
1226-
declarationOutput += synchronousDeclarationOutput.substring(appliedSyncOutputPos, aliasEmitInfo.outputPos);
1227-
declarationOutput += aliasEmitInfo.asynchronousOutput;
1228-
appliedSyncOutputPos = aliasEmitInfo.outputPos;
1229-
}
1230-
});
1231-
declarationOutput += synchronousDeclarationOutput.substring(appliedSyncOutputPos);
1232-
writeFile(compilerHost, diagnostics, removeFileExtension(jsFilePath) + ".d.ts", declarationOutput, compilerOptions.emitBOM);
1218+
return {
1219+
reportedDeclarationError: reportedDeclarationError,
1220+
aliasDeclarationEmitInfo: aliasDeclarationEmitInfo,
1221+
synchronousDeclarationOutput: writer.getText(),
1222+
declarationOutput: referencePathsOutput,
12331223
}
12341224
}
12351225

1226+
function writeDeclarationToFile(compilerHost: CompilerHost, compilerOptions: CompilerOptions, diagnostics: Diagnostic[], aliasDeclarationEmitInfo: aliasDeclaration[],
1227+
synchronousDeclarationOutput: string, jsFilePath: string, declarationOutput: string) {
1228+
// apply additions
1229+
var appliedSyncOutputPos = 0;
1230+
forEach(aliasDeclarationEmitInfo, aliasEmitInfo => {
1231+
if (aliasEmitInfo.asynchronousOutput) {
1232+
declarationOutput += synchronousDeclarationOutput.substring(appliedSyncOutputPos, aliasEmitInfo.outputPos);
1233+
declarationOutput += aliasEmitInfo.asynchronousOutput;
1234+
appliedSyncOutputPos = aliasEmitInfo.outputPos;
1235+
}
1236+
});
1237+
declarationOutput += synchronousDeclarationOutput.substring(appliedSyncOutputPos);
1238+
writeFile(compilerHost, diagnostics, removeFileExtension(jsFilePath) + ".d.ts", declarationOutput, compilerOptions.emitBOM);
1239+
}
1240+
12361241
export function getDeclarationDiagnostics(program: Program, resolver: EmitResolver, targetSourceFile: SourceFile): Diagnostic[] {
12371242
var diagnostics: Diagnostic[] = [];
12381243
var jsFilePath = getOwnEmitOutputFilePath(targetSourceFile, program, ".js");
@@ -3252,7 +3257,13 @@ module ts {
32523257
function emitFile(jsFilePath: string, sourceFile?: SourceFile) {
32533258
emitJavaScript(jsFilePath, sourceFile);
32543259
if (!hasSemanticErrors && compilerOptions.declaration) {
3255-
emitDeclarations(program, resolver, diagnostics, jsFilePath, sourceFile);
3260+
var emitDeclarationResult = emitDeclarations(program, resolver, diagnostics, jsFilePath, sourceFile);
3261+
// TODO(shkamat): Should we not write any declaration file if any of them can produce error,
3262+
// or should we just not write this file like we are doing now
3263+
if (!emitDeclarationResult.reportedDeclarationError) {
3264+
writeDeclarationToFile(compilerHost, compilerOptions, diagnostics, emitDeclarationResult.aliasDeclarationEmitInfo, emitDeclarationResult.synchronousDeclarationOutput,
3265+
jsFilePath, emitDeclarationResult.declarationOutput);
3266+
}
32563267
}
32573268
}
32583269

src/services/services.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2203,11 +2203,7 @@ module ts {
22032203
var allDiagnostics = checker.getDiagnostics(targetSourceFile);
22042204
if (compilerOptions.declaration) {
22052205
// If '-d' is enabled, check for emitter error. One example of emitter error is export class implements non-export interface
2206-
// Get emitter-diagnostics requires calling TypeChecker.emitFiles so we have to define CompilerHost.writer which does nothing because emitFiles function has side effects defined by CompilerHost.writer
2207-
var savedWriter = writer;
2208-
writer = (filename: string, data: string, writeByteOrderMark: boolean) => { };
22092206
allDiagnostics = allDiagnostics.concat(checker.getDeclarationDiagnostics(targetSourceFile));
2210-
writer = savedWriter;
22112207
}
22122208
return allDiagnostics
22132209
}

0 commit comments

Comments
 (0)