Skip to content

Commit 08482f2

Browse files
atscottalxhub
authored andcommitted
fix(language-service): Retain correct language service when ts.Project reloads (#51912)
When the `ts.Project` creates the language service plugin (in this case, the Angular Language Service), it sets the project's language service to the new language service returned by the plugin create: https://sourcegraph.com/github.com/microsoft/TypeScript@b12af0fa2bbd4b015e59adcfb49988cea7f919a1/-/blob/src/server/project.ts?L2035-2044 The project may be reloaded in response to various events, such as a change to the tsconfig file, which then recreates the plugin. When this happens, the language service that gets passed to the plugin `create` function will not be the typescript language service, but rather the previous instance of the new language service returned by the last call to `create`. This commit ensures that subsequent calls to `create` for the `NgLanguageService` plugin for a project after the first call are able to retrieve and hold on to the _TypeScript_ language service. fixes angular/vscode-ng-language-service#1923 PR Close #51912
1 parent 422a3db commit 08482f2

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

packages/language-service/api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export interface NgLanguageService extends ts.LanguageService {
5858
getComponentLocationsForTemplate(fileName: string): GetComponentLocationsForTemplateResponse;
5959
getTemplateLocationForComponent(fileName: string, position: number):
6060
GetTemplateLocationForComponentResponse;
61+
getTypescriptLanguageService(): ts.LanguageService;
6162
}
6263

6364
export function isNgLanguageService(ls: ts.LanguageService|

packages/language-service/src/ts_plugin.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@
88

99
import ts from 'typescript/lib/tsserverlibrary';
1010

11-
import {GetComponentLocationsForTemplateResponse, GetTcbResponse, GetTemplateLocationForComponentResponse, NgLanguageService} from '../api';
11+
import {GetComponentLocationsForTemplateResponse, GetTcbResponse, GetTemplateLocationForComponentResponse, isNgLanguageService, NgLanguageService} from '../api';
1212

1313
import {LanguageService} from './language_service';
1414

1515
export function create(info: ts.server.PluginCreateInfo): NgLanguageService {
16-
const {project, languageService: tsLS, config} = info;
16+
const {project, languageService, config} = info;
17+
const tsLS = isNgLanguageService(languageService) ?
18+
languageService.getTypescriptLanguageService() :
19+
languageService;
1720
const angularOnly = config?.angularOnly === true;
1821

1922
const ngLS = new LanguageService(project, tsLS, config);
@@ -194,6 +197,9 @@ export function create(info: ts.server.PluginCreateInfo): NgLanguageService {
194197
}
195198
}
196199

200+
function getTypescriptLanguageService() {
201+
return tsLS;
202+
}
197203

198204
return {
199205
...tsLS,
@@ -214,6 +220,7 @@ export function create(info: ts.server.PluginCreateInfo): NgLanguageService {
214220
getTemplateLocationForComponent,
215221
getCodeFixesAtPosition,
216222
getCombinedCodeFix,
223+
getTypescriptLanguageService,
217224
};
218225
}
219226

0 commit comments

Comments
 (0)