forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
/
translate_diagnostics.ts
57 lines (50 loc) · 1.97 KB
/
translate_diagnostics.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {ParseSourceSpan} from '@angular/compiler';
import * as ts from 'typescript';
import {DEFAULT_ERROR_CODE, Diagnostic, SOURCE} from '../transformers/api';
import {GENERATED_FILES} from '../transformers/util';
export interface TypeCheckHost {
parseSourceSpanOf(fileName: string, line: number, character: number): ParseSourceSpan|null;
}
export function translateDiagnostics(host: TypeCheckHost, untranslatedDiagnostics: ts.Diagnostic[]):
{ts: ts.Diagnostic[], ng: Diagnostic[]} {
const ts: ts.Diagnostic[] = [];
const ng: Diagnostic[] = [];
untranslatedDiagnostics.forEach((diagnostic) => {
if (diagnostic.file && diagnostic.start && GENERATED_FILES.test(diagnostic.file.fileName)) {
// We need to filter out diagnostics about unused functions as
// they are in fact referenced by nobody and only serve to surface
// type check errors.
if (diagnostic.code === /* ... is declared but never used */ 6133) {
return;
}
const span = sourceSpanOf(host, diagnostic.file, diagnostic.start);
if (span) {
const fileName = span.start.file.url;
ng.push({
messageText: diagnosticMessageToString(diagnostic.messageText),
category: diagnostic.category, span,
source: SOURCE,
code: DEFAULT_ERROR_CODE
});
}
} else {
ts.push(diagnostic);
}
});
return {ts, ng};
}
function sourceSpanOf(host: TypeCheckHost, source: ts.SourceFile, start: number): ParseSourceSpan|
null {
const {line, character} = ts.getLineAndCharacterOfPosition(source, start);
return host.parseSourceSpanOf(source.fileName, line, character);
}
function diagnosticMessageToString(message: ts.DiagnosticMessageChain | string): string {
return ts.flattenDiagnosticMessageText(message, '\n');
}