From 29a5a90111204e3792b59667dde6f7725aff2f39 Mon Sep 17 00:00:00 2001 From: Daniel Trevino <23410540+danieltre23@users.noreply.github.com> Date: Tue, 27 Jul 2021 21:07:03 +0000 Subject: [PATCH] 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 --- packages/compiler-cli/src/ngtsc/testing/src/utils.ts | 9 +++++++++ .../compiler-cli/test/ngtsc/template_typecheck_spec.ts | 7 +------ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/packages/compiler-cli/src/ngtsc/testing/src/utils.ts b/packages/compiler-cli/src/ngtsc/testing/src/utils.ts index 13fe87a560..fe72f5e67f 100644 --- a/packages/compiler-cli/src/ngtsc/testing/src/utils.ts +++ b/packages/compiler-cli/src/ngtsc/testing/src/utils.ts @@ -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); +} diff --git a/packages/compiler-cli/test/ngtsc/template_typecheck_spec.ts b/packages/compiler-cli/test/ngtsc/template_typecheck_spec.ts index 1cc1497288..a6c5d44954 100644 --- a/packages/compiler-cli/test/ngtsc/template_typecheck_spec.ts +++ b/packages/compiler-cli/test/ngtsc/template_typecheck_spec.ts @@ -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!); -}