refactor(compiler-cli): move `getSourceCodeForDiagnostic` to utils (#42984)

Export `getSourceCodeForDiagnostic` from `ngtsc/testing` to make it
available for other packages. This will help confirm that the source
code is correct in other tests.

Refs #42966

PR Close #42984
This commit is contained in:
Daniel Trevino 2021-07-27 21:07:03 +00:00 committed by Andrew Kushnir
parent 29e2bc7d91
commit 29a5a90111
2 changed files with 10 additions and 6 deletions

View File

@ -138,3 +138,12 @@ function bindingNameEquals(node: ts.BindingName, name: string): boolean {
}
return false;
}
export function getSourceCodeForDiagnostic(diag: ts.Diagnostic): string {
if (diag.file === undefined || diag.start === undefined || diag.length === undefined) {
throw new Error(
`Unable to get source code for diagnostic. Provided diagnostic instance doesn't contain "file", "start" and/or "length" properties.`);
}
const text = diag.file.text;
return text.substr(diag.start, diag.length);
}

View File

@ -11,7 +11,7 @@ import * as ts from 'typescript';
import {ErrorCode, ngErrorCode} from '../../src/ngtsc/diagnostics';
import {absoluteFrom as _, getFileSystem, getSourceFileOrError} from '../../src/ngtsc/file_system';
import {runInEachFileSystem} from '../../src/ngtsc/file_system/testing';
import {expectCompleteReuse, loadStandardTestFiles} from '../../src/ngtsc/testing';
import {expectCompleteReuse, getSourceCodeForDiagnostic, loadStandardTestFiles} from '../../src/ngtsc/testing';
import {NgtscTestEnvironment} from './env';
@ -2506,8 +2506,3 @@ export declare class AnimationEvent {
});
});
});
function getSourceCodeForDiagnostic(diag: ts.Diagnostic): string {
const text = diag.file!.text;
return text.substr(diag.start!, diag.length!);
}