Skip to content

Commit 4166dfc

Browse files
clydindylhunn
authored andcommitted
fix(language-service): prevent underlying TS Service from handling template files (#55003)
When the `angularOnly` option is disabled, the underlying TypeScript language service was previously used for all files including `.html` template files. This can result in incorrect responses including a large amount of invalid diagnostics for a template file. Checks have now been added to only use the Angular Language service for template files which can properly handle template files. Additionally, the check for a TypeScript file name has now been expanded to encompass all current file extension forms. PR Close #55003
1 parent 311f552 commit 4166dfc

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

packages/language-service/src/ts_plugin.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import ts from 'typescript';
1111
import {GetComponentLocationsForTemplateResponse, GetTcbResponse, GetTemplateLocationForComponentResponse, isNgLanguageService, NgLanguageService} from '../api';
1212

1313
import {LanguageService} from './language_service';
14+
import {isTypeScriptFile} from './utils';
1415

1516
export function create(info: ts.server.PluginCreateInfo): NgLanguageService {
1617
const {project, languageService, config} = info;
@@ -23,15 +24,15 @@ export function create(info: ts.server.PluginCreateInfo): NgLanguageService {
2324

2425
function getSemanticDiagnostics(fileName: string): ts.Diagnostic[] {
2526
const diagnostics: ts.Diagnostic[] = [];
26-
if (!angularOnly) {
27+
if (!angularOnly && isTypeScriptFile(fileName)) {
2728
diagnostics.push(...tsLS.getSemanticDiagnostics(fileName));
2829
}
2930
diagnostics.push(...ngLS.getSemanticDiagnostics(fileName));
3031
return diagnostics;
3132
}
3233

3334
function getQuickInfoAtPosition(fileName: string, position: number): ts.QuickInfo|undefined {
34-
if (angularOnly) {
35+
if (angularOnly || !isTypeScriptFile(fileName)) {
3536
return ngLS.getQuickInfoAtPosition(fileName, position);
3637
} else {
3738
// If TS could answer the query, then return that result. Otherwise, return from Angular LS.
@@ -42,7 +43,7 @@ export function create(info: ts.server.PluginCreateInfo): NgLanguageService {
4243

4344
function getTypeDefinitionAtPosition(
4445
fileName: string, position: number): readonly ts.DefinitionInfo[]|undefined {
45-
if (angularOnly) {
46+
if (angularOnly || !isTypeScriptFile(fileName)) {
4647
return ngLS.getTypeDefinitionAtPosition(fileName, position);
4748
} else {
4849
// If TS could answer the query, then return that result. Otherwise, return from Angular LS.
@@ -53,7 +54,7 @@ export function create(info: ts.server.PluginCreateInfo): NgLanguageService {
5354

5455
function getDefinitionAndBoundSpan(
5556
fileName: string, position: number): ts.DefinitionInfoAndBoundSpan|undefined {
56-
if (angularOnly) {
57+
if (angularOnly || !isTypeScriptFile(fileName)) {
5758
return ngLS.getDefinitionAndBoundSpan(fileName, position);
5859
} else {
5960
// If TS could answer the query, then return that result. Otherwise, return from Angular LS.
@@ -85,7 +86,7 @@ export function create(info: ts.server.PluginCreateInfo): NgLanguageService {
8586
function getCompletionsAtPosition(
8687
fileName: string, position: number,
8788
options: ts.GetCompletionsAtPositionOptions): ts.WithMetadata<ts.CompletionInfo>|undefined {
88-
if (angularOnly) {
89+
if (angularOnly || !isTypeScriptFile(fileName)) {
8990
return ngLS.getCompletionsAtPosition(fileName, position, options);
9091
} else {
9192
// If TS could answer the query, then return that result. Otherwise, return from Angular LS.
@@ -99,7 +100,7 @@ export function create(info: ts.server.PluginCreateInfo): NgLanguageService {
99100
formatOptions: ts.FormatCodeOptions|ts.FormatCodeSettings|undefined, source: string|undefined,
100101
preferences: ts.UserPreferences|undefined,
101102
data: ts.CompletionEntryData|undefined): ts.CompletionEntryDetails|undefined {
102-
if (angularOnly) {
103+
if (angularOnly || !isTypeScriptFile(fileName)) {
103104
return ngLS.getCompletionEntryDetails(
104105
fileName, position, entryName, formatOptions, preferences, data);
105106
} else {
@@ -114,7 +115,7 @@ export function create(info: ts.server.PluginCreateInfo): NgLanguageService {
114115
function getCompletionEntrySymbol(
115116
fileName: string, position: number, name: string, source: string|undefined): ts.Symbol|
116117
undefined {
117-
if (angularOnly) {
118+
if (angularOnly || !isTypeScriptFile(fileName)) {
118119
return ngLS.getCompletionEntrySymbol(fileName, position, name);
119120
} else {
120121
// If TS could answer the query, then return that result. Otherwise, return from Angular LS.
@@ -137,7 +138,7 @@ export function create(info: ts.server.PluginCreateInfo): NgLanguageService {
137138
function getSignatureHelpItems(
138139
fileName: string, position: number,
139140
options: ts.SignatureHelpItemsOptions): ts.SignatureHelpItems|undefined {
140-
if (angularOnly) {
141+
if (angularOnly || !isTypeScriptFile(fileName)) {
141142
return ngLS.getSignatureHelpItems(fileName, position, options);
142143
} else {
143144
return tsLS.getSignatureHelpItems(fileName, position, options) ??
@@ -146,7 +147,7 @@ export function create(info: ts.server.PluginCreateInfo): NgLanguageService {
146147
}
147148

148149
function getOutliningSpans(fileName: string): ts.OutliningSpan[] {
149-
if (angularOnly) {
150+
if (angularOnly || !isTypeScriptFile(fileName)) {
150151
return ngLS.getOutliningSpans(fileName);
151152
} else {
152153
return tsLS.getOutliningSpans(fileName) ?? ngLS.getOutliningSpans(fileName);
@@ -178,7 +179,7 @@ export function create(info: ts.server.PluginCreateInfo): NgLanguageService {
178179
fileName: string, start: number, end: number, errorCodes: readonly number[],
179180
formatOptions: ts.FormatCodeSettings,
180181
preferences: ts.UserPreferences): readonly ts.CodeFixAction[] {
181-
if (angularOnly) {
182+
if (angularOnly || !isTypeScriptFile(fileName)) {
182183
return ngLS.getCodeFixesAtPosition(
183184
fileName, start, end, errorCodes, formatOptions, preferences);
184185
} else {

packages/language-service/src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ export function flatMap<T, R>(items: T[]|readonly T[], f: (item: T) => R[] | rea
336336
}
337337

338338
export function isTypeScriptFile(fileName: string): boolean {
339-
return fileName.endsWith('.ts');
339+
return /\.[cm]?tsx?$/i.test(fileName);
340340
}
341341

342342
export function isExternalTemplate(fileName: string): boolean {

0 commit comments

Comments
 (0)