Skip to content

Commit 848bfe7

Browse files
author
Yui T
committed
Update language service to use getDeclarationDiagnostics instead of emitFile to get declaration errors
1 parent cc48fc3 commit 848bfe7

File tree

4 files changed

+33
-1
lines changed

4 files changed

+33
-1
lines changed

src/compiler/checker.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ module ts {
7171
var checker: TypeChecker = {
7272
getProgram: () => program,
7373
getDiagnostics: getDiagnostics,
74+
getDeclarationDiagnostics: getDeclarationDiagnosticsFromSourceFile,
7475
getGlobalDiagnostics: getGlobalDiagnostics,
7576
getNodeCount: () => sum(program.getSourceFiles(), "nodeCount"),
7677
getIdentifierCount: () => sum(program.getSourceFiles(), "identifierCount"),
@@ -7038,6 +7039,29 @@ module ts {
70387039
return getSortedDiagnostics();
70397040
}
70407041

7042+
function getDeclarationDiagnosticsFromSourceFile(targetSourceFile: SourceFile): Diagnostic[] {
7043+
var resolver: EmitResolver = {
7044+
getProgram: () => program,
7045+
getLocalNameOfContainer: getLocalNameOfContainer,
7046+
getExpressionNamePrefix: getExpressionNamePrefix,
7047+
getExportAssignmentName: getExportAssignmentName,
7048+
isReferencedImportDeclaration: isReferencedImportDeclaration,
7049+
getNodeCheckFlags: getNodeCheckFlags,
7050+
getEnumMemberValue: getEnumMemberValue,
7051+
isTopLevelValueImportedViaEntityName: isTopLevelValueImportedViaEntityName,
7052+
hasSemanticErrors: hasSemanticErrors,
7053+
isDeclarationVisible: isDeclarationVisible,
7054+
isImplementationOfOverload: isImplementationOfOverload,
7055+
writeTypeAtLocation: writeTypeAtLocation,
7056+
writeReturnTypeOfSignatureDeclaration: writeReturnTypeOfSignatureDeclaration,
7057+
isSymbolAccessible: isSymbolAccessible,
7058+
isImportDeclarationEntityNameReferenceDeclarationVisible: isImportDeclarationEntityNameReferenceDeclarationVisible,
7059+
getConstantValue: getConstantValue,
7060+
};
7061+
checkProgram();
7062+
return getDeclarationDiagnostics(program, resolver, targetSourceFile);
7063+
}
7064+
70417065
function getGlobalDiagnostics(): Diagnostic[] {
70427066
return filter(getSortedDiagnostics(), d => !d.file);
70437067
}

src/compiler/emitter.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,6 +1233,13 @@ module ts {
12331233
}
12341234
}
12351235

1236+
export function getDeclarationDiagnostics(program: Program, resolver: EmitResolver, targetSourceFile: SourceFile): Diagnostic[] {
1237+
var diagnostics: Diagnostic[] = [];
1238+
var jsFilePath = getOwnEmitOutputFilePath(targetSourceFile, program, ".js");
1239+
emitDeclarations(program, resolver, diagnostics, jsFilePath, targetSourceFile);
1240+
return diagnostics;
1241+
}
1242+
12361243
// targetSourceFile is when users only want one file in entire project to be emitted. This is used in compilerOnSave feature
12371244
export function emitFiles(resolver: EmitResolver, targetSourceFile?: SourceFile): EmitResult {
12381245
var program = resolver.getProgram();

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,7 @@ module ts {
624624
export interface TypeChecker {
625625
getProgram(): Program;
626626
getDiagnostics(sourceFile?: SourceFile): Diagnostic[];
627+
getDeclarationDiagnostics(sourceFile: SourceFile): Diagnostic[];
627628
getGlobalDiagnostics(): Diagnostic[];
628629
getNodeCount(): number;
629630
getIdentifierCount(): number;

src/services/services.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2206,7 +2206,7 @@ module ts {
22062206
// 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
22072207
var savedWriter = writer;
22082208
writer = (filename: string, data: string, writeByteOrderMark: boolean) => { };
2209-
allDiagnostics = allDiagnostics.concat(checker.emitFiles(targetSourceFile).errors);
2209+
allDiagnostics = allDiagnostics.concat(checker.getDeclarationDiagnostics(targetSourceFile));
22102210
writer = savedWriter;
22112211
}
22122212
return allDiagnostics

0 commit comments

Comments
 (0)