Skip to content

Commit 108252b

Browse files
clydinAndrewKushnir
authored andcommitted
refactor(language-service): use type-only TypeScript imports in some files
Standardize on `import type ts from 'typescript'` across several files in the language-service package. This ensures that these files do not have a runtime dependency on a specific version of the TypeScript module, instead relying on the instance provided by the host during initialization. Note: This change is not comprehensive. Several files still require runtime access to the TypeScript module and will require further refactoring to fully decouple the dependency.
1 parent 6550b9d commit 108252b

11 files changed

Lines changed: 43 additions & 43 deletions

File tree

‎packages/language-service/src/codefixes/code_fixes.ts‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import {NgCompiler} from '@angular/compiler-cli/src/ngtsc/core';
10-
import tss from 'typescript';
10+
import type ts from 'typescript';
1111

1212
import {TypeCheckInfo} from '../utils';
1313

@@ -18,7 +18,7 @@ export class CodeFixes {
1818
private fixIdToRegistration = new Map<FixIdForCodeFixesAll, CodeActionMeta>();
1919

2020
constructor(
21-
private readonly tsLS: tss.LanguageService,
21+
private readonly tsLS: ts.LanguageService,
2222
readonly codeActionMetas: CodeActionMeta[],
2323
) {
2424
for (const meta of codeActionMetas) {
@@ -55,11 +55,11 @@ export class CodeFixes {
5555
start: number,
5656
end: number,
5757
errorCodes: readonly number[],
58-
diagnostics: tss.Diagnostic[],
59-
formatOptions: tss.FormatCodeSettings,
60-
preferences: tss.UserPreferences,
61-
): readonly tss.CodeFixAction[] {
62-
const codeActions: tss.CodeFixAction[] = [];
58+
diagnostics: ts.Diagnostic[],
59+
formatOptions: ts.FormatCodeSettings,
60+
preferences: ts.UserPreferences,
61+
): readonly ts.CodeFixAction[] {
62+
const codeActions: ts.CodeFixAction[] = [];
6363
for (const code of errorCodes) {
6464
const metas = this.errorCodeToFixes.get(code);
6565
if (metas === undefined) {
@@ -98,12 +98,12 @@ export class CodeFixes {
9898
*/
9999
getAllCodeActions(
100100
compiler: NgCompiler,
101-
diagnostics: tss.Diagnostic[],
102-
scope: tss.CombinedCodeFixScope,
101+
diagnostics: ts.Diagnostic[],
102+
scope: ts.CombinedCodeFixScope,
103103
fixId: string,
104-
formatOptions: tss.FormatCodeSettings,
105-
preferences: tss.UserPreferences,
106-
): tss.CombinedCodeActions {
104+
formatOptions: ts.FormatCodeSettings,
105+
preferences: ts.UserPreferences,
106+
): ts.CombinedCodeActions {
107107
const meta = this.fixIdToRegistration.get(fixId as FixIdForCodeFixesAll);
108108
if (meta === undefined) {
109109
return {

‎packages/language-service/src/codefixes/fix_invalid_banana_in_box.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import {TmplAstBoundEvent} from '@angular/compiler';
1010
import {ErrorCode, ngErrorCode} from '@angular/compiler-cli/src/ngtsc/diagnostics';
11-
import tss from 'typescript';
11+
import type ts from 'typescript';
1212

1313
import {getTargetAtPosition, TargetNodeKind} from '../template_target';
1414
import {getTypeCheckInfoAtPosition, TypeCheckInfo} from '../utils';
@@ -44,7 +44,7 @@ export const fixInvalidBananaInBoxMeta: CodeActionMeta = {
4444
},
4545
fixIds: [FixIdForCodeFixesAll.FIX_INVALID_BANANA_IN_BOX],
4646
getAllCodeActions({diagnostics, compiler}) {
47-
const fileNameToTextChangesMap = new Map<string, tss.TextChange[]>();
47+
const fileNameToTextChangesMap = new Map<string, ts.TextChange[]>();
4848
for (const diag of diagnostics) {
4949
const fileName = diag.file?.fileName;
5050
if (fileName === undefined) {
@@ -77,7 +77,7 @@ export const fixInvalidBananaInBoxMeta: CodeActionMeta = {
7777
fileTextChanges.push(...textChanges);
7878
}
7979

80-
const fileTextChanges: tss.FileTextChanges[] = [];
80+
const fileTextChanges: ts.FileTextChanges[] = [];
8181
for (const [fileName, textChanges] of fileNameToTextChangesMap) {
8282
fileTextChanges.push({
8383
fileName,
@@ -117,7 +117,7 @@ function getTheBoundEventAtPosition(
117117
/**
118118
* Flip the invalid "box in a banana" `([thing])` to the correct "banana in a box" `[(thing)]`.
119119
*/
120-
function convertBoundEventToTsTextChange(node: TmplAstBoundEvent): readonly tss.TextChange[] {
120+
function convertBoundEventToTsTextChange(node: TmplAstBoundEvent): readonly ts.TextChange[] {
121121
const name = node.name;
122122
const boundSyntax = node.sourceSpan.toString();
123123
const expectedBoundSyntax = boundSyntax.replace(`(${name})`, `[(${name.slice(1, -1)})]`);

‎packages/language-service/src/codefixes/fix_missing_import.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import {ASTWithName, TmplAstElement} from '@angular/compiler';
1010
import {ErrorCode as NgCompilerErrorCode, ngErrorCode} from '@angular/compiler-cli';
1111
import {PotentialDirective, PotentialPipe} from '@angular/compiler-cli/src/ngtsc/typecheck/api';
12-
import ts from 'typescript';
12+
import type ts from 'typescript';
1313

1414
import {getTargetAtPosition, TargetNodeKind} from '../template_target';
1515
import {

‎packages/language-service/src/codefixes/fix_missing_member.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import tss from 'typescript';
9+
import type ts from 'typescript';
1010

1111
import {getTcbNodesOfTemplateAtPosition} from '../template_target';
1212
import {getTypeCheckInfoAtPosition} from '../utils';
@@ -41,7 +41,7 @@ export const missingMemberMeta: CodeActionMeta = {
4141
return [];
4242
}
4343

44-
const codeActions: tss.CodeFixAction[] = [];
44+
const codeActions: ts.CodeFixAction[] = [];
4545
const tcb = tcbNodesInfo.componentTcbNode;
4646
for (const tcbNode of tcbNodesInfo.nodes) {
4747
const tsLsCodeActions = tsLs.getCodeFixesAtPosition(
@@ -75,8 +75,8 @@ export const missingMemberMeta: CodeActionMeta = {
7575
compiler,
7676
diagnostics,
7777
}) {
78-
const changes: tss.FileTextChanges[] = [];
79-
const seen: Set<tss.ClassDeclaration> = new Set();
78+
const changes: ts.FileTextChanges[] = [];
79+
const seen: Set<ts.ClassDeclaration> = new Set();
8080
for (const diag of diagnostics) {
8181
if (!errorCodes.includes(diag.code)) {
8282
continue;

‎packages/language-service/src/codefixes/utils.ts‎

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import {absoluteFrom} from '@angular/compiler-cli';
1010
import {NgCompiler} from '@angular/compiler-cli/src/ngtsc/core';
11-
import tss from 'typescript';
11+
import type ts from 'typescript';
1212

1313
import {TypeCheckInfo} from '../utils';
1414

@@ -26,9 +26,9 @@ export interface CodeActionContext {
2626
start: number;
2727
end: number;
2828
errorCode: number;
29-
formatOptions: tss.FormatCodeSettings;
30-
preferences: tss.UserPreferences;
31-
tsLs: tss.LanguageService;
29+
formatOptions: ts.FormatCodeSettings;
30+
preferences: ts.UserPreferences;
31+
tsLs: ts.LanguageService;
3232
}
3333

3434
/**
@@ -39,38 +39,38 @@ export interface CodeActionContext {
3939
* `scope`, this context will be provided to the `CodeActionMeta` which could handle the `fixId`.
4040
*/
4141
export interface CodeFixAllContext {
42-
scope: tss.CombinedCodeFixScope;
42+
scope: ts.CombinedCodeFixScope;
4343
compiler: NgCompiler;
4444
// https://github.com/microsoft/TypeScript/blob/5c4caafc2a2d0fceb03fce80fb14d3ee4407d918/src/services/types.ts#L781-L785
4545
fixId: string;
46-
formatOptions: tss.FormatCodeSettings;
47-
preferences: tss.UserPreferences;
48-
tsLs: tss.LanguageService;
49-
diagnostics: tss.Diagnostic[];
46+
formatOptions: ts.FormatCodeSettings;
47+
preferences: ts.UserPreferences;
48+
tsLs: ts.LanguageService;
49+
diagnostics: ts.Diagnostic[];
5050
}
5151

5252
export interface CodeActionMeta {
5353
errorCodes: Array<number>;
54-
getCodeActions: (context: CodeActionContext) => readonly tss.CodeFixAction[];
54+
getCodeActions: (context: CodeActionContext) => readonly ts.CodeFixAction[];
5555
fixIds: FixIdForCodeFixesAll[];
56-
getAllCodeActions: (context: CodeFixAllContext) => tss.CombinedCodeActions;
56+
getAllCodeActions: (context: CodeFixAllContext) => ts.CombinedCodeActions;
5757
}
5858

5959
/**
6060
* Convert the span of `textChange` in the TCB to the span of the template.
6161
*/
6262
export function convertFileTextChangeInTcb(
63-
changes: readonly tss.FileTextChanges[],
63+
changes: readonly ts.FileTextChanges[],
6464
compiler: NgCompiler,
65-
): tss.FileTextChanges[] {
65+
): ts.FileTextChanges[] {
6666
const ttc = compiler.getTemplateTypeChecker();
67-
const fileTextChanges: tss.FileTextChanges[] = [];
67+
const fileTextChanges: ts.FileTextChanges[] = [];
6868
for (const fileTextChange of changes) {
6969
if (!ttc.isTrackedTypeCheckFile(absoluteFrom(fileTextChange.fileName))) {
7070
fileTextChanges.push(fileTextChange);
7171
continue;
7272
}
73-
const textChanges: tss.TextChange[] = [];
73+
const textChanges: ts.TextChange[] = [];
7474
let fileName: string | undefined;
7575
const seenTextChangeInTemplate = new Set<string>();
7676
for (const textChange of fileTextChange.textChanges) {
@@ -121,7 +121,7 @@ export function convertFileTextChangeInTcb(
121121
* 'fix all' is only available when there are multiple diagnostics that the code action meta
122122
* indicates it can fix.
123123
*/
124-
export function isFixAllAvailable(meta: CodeActionMeta, diagnostics: tss.Diagnostic[]) {
124+
export function isFixAllAvailable(meta: CodeActionMeta, diagnostics: ts.Diagnostic[]) {
125125
const errorCodes = meta.errorCodes;
126126
let maybeFixableDiagnostics = 0;
127127
for (const diag of diagnostics) {

‎packages/language-service/src/quick_info_built_ins.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
TmplAstForLoopBlockEmpty,
2222
TmplAstNode,
2323
} from '@angular/compiler';
24-
import ts from 'typescript';
24+
import type ts from 'typescript';
2525

2626
import {DisplayInfoKind, SYMBOL_TEXT} from './utils/display_parts';
2727
import {createQuickInfo, getTextSpanOfNode, isWithin, toTextSpan} from './utils';

‎packages/language-service/src/refactorings/convert_to_signal_input/apply_input_refactoring.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import {CompilerOptions} from '@angular/compiler-cli';
1010
import {getFileSystem} from '@angular/compiler-cli/src/ngtsc/file_system';
1111
import {NgCompiler} from '@angular/compiler-cli/src/ngtsc/core';
12-
import ts from 'typescript';
12+
import type ts from 'typescript';
1313
import {
1414
getMessageForClassIncompatibility,
1515
getMessageForFieldIncompatibility,

‎packages/language-service/src/refactorings/convert_to_signal_input/decorators.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import ts from 'typescript';
9+
import type ts from 'typescript';
1010
import {getAngularDecorators} from '@angular/compiler-cli/src/ngtsc/annotations';
1111
import {ReflectionHost} from '@angular/compiler-cli/src/ngtsc/reflection';
1212
import {isDirectiveOrComponent} from '../../utils/decorators';

‎packages/language-service/src/refactorings/convert_to_signal_queries/decorators.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import ts from 'typescript';
9+
import type ts from 'typescript';
1010
import {getAngularDecorators} from '@angular/compiler-cli/src/ngtsc/annotations';
1111
import {ReflectionHost} from '@angular/compiler-cli/src/ngtsc/reflection';
1212
import {isDirectiveOrComponent} from '../../utils/decorators';

‎packages/language-service/src/refactorings/refactoring.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import {NgCompiler} from '@angular/compiler-cli/src/ngtsc/core';
10-
import ts from 'typescript';
10+
import type ts from 'typescript';
1111
import {ApplyRefactoringProgressFn, ApplyRefactoringResult} from '../../api';
1212
import {CompilerOptions} from '@angular/compiler-cli';
1313
import {

0 commit comments

Comments
 (0)