From d293e723a20cdba258238397097e9a4e409bc41f Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Wed, 31 Aug 2022 14:21:56 -0700 Subject: [PATCH] Rename API to importPlugin (#50554) * Rename API to importPlugin * Make it internal too --- src/server/editorServices.ts | 4 ++-- src/server/project.ts | 11 ++++----- src/server/types.ts | 3 ++- .../unittests/tsserver/webServer.ts | 24 +++++++++---------- src/webServer/webServer.ts | 2 +- .../reference/api/tsserverlibrary.d.ts | 1 - 6 files changed, 22 insertions(+), 23 deletions(-) diff --git a/src/server/editorServices.ts b/src/server/editorServices.ts index c6d1513c0771f..3ab9f02741cc1 100644 --- a/src/server/editorServices.ts +++ b/src/server/editorServices.ts @@ -4068,7 +4068,7 @@ namespace ts.server { /*@internal*/ requestEnablePlugin(project: Project, pluginConfigEntry: PluginImport, searchPaths: string[], pluginConfigOverrides: Map | undefined) { - if (!this.host.importServicePlugin && !this.host.require) { + if (!this.host.importPlugin && !this.host.require) { this.logger.info("Plugins were requested but not running in environment that supports 'require'. Nothing will be loaded"); return; } @@ -4080,7 +4080,7 @@ namespace ts.server { } // If the host supports dynamic import, begin enabling the plugin asynchronously. - if (this.host.importServicePlugin) { + if (this.host.importPlugin) { const importPromise = project.beginEnablePluginAsync(pluginConfigEntry, searchPaths, pluginConfigOverrides); this.pendingPluginEnablements ??= new Map(); let promises = this.pendingPluginEnablements.get(project); diff --git a/src/server/project.ts b/src/server/project.ts index 28dbeb66303dc..43b1c7128e005 100644 --- a/src/server/project.ts +++ b/src/server/project.ts @@ -256,12 +256,12 @@ namespace ts.server { /*@internal*/ public static async importServicePluginAsync(moduleName: string, initialDir: string, host: ServerHost, log: (message: string) => void, logErrors?: (message: string) => void): Promise<{} | undefined> { - Debug.assertIsDefined(host.importServicePlugin); + Debug.assertIsDefined(host.importPlugin); const resolvedPath = combinePaths(initialDir, "node_modules"); log(`Dynamically importing ${moduleName} from ${initialDir} (resolved to ${resolvedPath})`); let result: ModuleImportResult; try { - result = await host.importServicePlugin(resolvedPath, moduleName); + result = await host.importPlugin(resolvedPath, moduleName); } catch (e) { result = { module: undefined, error: e }; @@ -1607,7 +1607,7 @@ namespace ts.server { protected enableGlobalPlugins(options: CompilerOptions, pluginConfigOverrides: Map | undefined): void { const host = this.projectService.host; - if (!host.require && !host.importServicePlugin) { + if (!host.require && !host.importPlugin) { this.projectService.logger.info("Plugins were requested but not running in environment that supports 'require'. Nothing will be loaded"); return; } @@ -1658,7 +1658,7 @@ namespace ts.server { */ /*@internal*/ async beginEnablePluginAsync(pluginConfigEntry: PluginImport, searchPaths: string[], pluginConfigOverrides: Map | undefined): Promise { - Debug.assertIsDefined(this.projectService.host.importServicePlugin); + Debug.assertIsDefined(this.projectService.host.importPlugin); let errorLogs: string[] | undefined; const log = (message: string) => this.projectService.logger.info(message); @@ -2522,8 +2522,7 @@ namespace ts.server { /*@internal*/ enablePluginsWithOptions(options: CompilerOptions, pluginConfigOverrides: ESMap | undefined): void { const host = this.projectService.host; - - if (!host.require && !host.importServicePlugin) { + if (!host.require && !host.importPlugin) { this.projectService.logger.info("Plugins were requested but not running in environment that supports 'require'. Nothing will be loaded"); return; } diff --git a/src/server/types.ts b/src/server/types.ts index b19879a94920f..2aa26f1231314 100644 --- a/src/server/types.ts +++ b/src/server/types.ts @@ -20,6 +20,7 @@ declare namespace ts.server { gc?(): void; trace?(s: string): void; require?(initialPath: string, moduleName: string): ModuleImportResult; - importServicePlugin?(root: string, moduleName: string): Promise; + /*@internal*/ + importPlugin?(root: string, moduleName: string): Promise; } } diff --git a/src/testRunner/unittests/tsserver/webServer.ts b/src/testRunner/unittests/tsserver/webServer.ts index ab78d26308af2..7a940abc68c58 100644 --- a/src/testRunner/unittests/tsserver/webServer.ts +++ b/src/testRunner/unittests/tsserver/webServer.ts @@ -29,7 +29,7 @@ namespace ts.projectSystem { } } - function setup(logLevel: server.LogLevel | undefined, options?: Partial, importServicePlugin?: server.ServerHost["importServicePlugin"]) { + function setup(logLevel: server.LogLevel | undefined, options?: Partial, importPlugin?: server.ServerHost["importPlugin"]) { const host = createServerHost([libFile], { windowsStyleRoot: "c:/" }); const messages: any[] = []; const webHost: server.WebHost = { @@ -38,7 +38,7 @@ namespace ts.projectSystem { writeMessage: s => messages.push(s), }; const webSys = server.createWebSystem(webHost, emptyArray, () => host.getExecutingFilePath()); - webSys.importServicePlugin = importServicePlugin; + webSys.importPlugin = importPlugin; const logger = logLevel !== undefined ? new server.MainProcessLogger(logLevel, webHost) : nullLogger(); const session = new TestWorkerSession(webSys, webHost, { serverMode: LanguageServiceMode.PartialSemantic, ...options }, logger); return { getMessages: () => messages, clearMessages: () => messages.length = 0, session }; @@ -161,7 +161,7 @@ namespace ts.projectSystem { it("plugins are not loaded immediately", async () => { let pluginModuleInstantiated = false; let pluginInvoked = false; - const importServicePlugin = async (_root: string, _moduleName: string): Promise => { + const importPlugin = async (_root: string, _moduleName: string): Promise => { await Promise.resolve(); // simulate at least a single turn delay pluginModuleInstantiated = true; return { @@ -173,7 +173,7 @@ namespace ts.projectSystem { }; }; - const { session } = setup(/*logLevel*/ undefined, { globalPlugins: ["plugin-a"] }, importServicePlugin); + const { session } = setup(/*logLevel*/ undefined, { globalPlugins: ["plugin-a"] }, importPlugin); const projectService = session.getProjectService(); session.executeCommand({ seq: 1, type: "request", command: protocol.CommandTypes.Open, arguments: { file: "^memfs:/foo.ts", content: "" } }); @@ -201,7 +201,7 @@ namespace ts.projectSystem { const pluginADeferred = Utils.defer(); const pluginBDeferred = Utils.defer(); const log: string[] = []; - const importServicePlugin = async (_root: string, moduleName: string): Promise => { + const importPlugin = async (_root: string, moduleName: string): Promise => { log.push(`request import ${moduleName}`); const promise = moduleName === "plugin-a" ? pluginADeferred.promise : pluginBDeferred.promise; await promise; @@ -215,7 +215,7 @@ namespace ts.projectSystem { }; }; - const { session } = setup(/*logLevel*/ undefined, { globalPlugins: ["plugin-a", "plugin-b"] }, importServicePlugin); + const { session } = setup(/*logLevel*/ undefined, { globalPlugins: ["plugin-a", "plugin-b"] }, importPlugin); const projectService = session.getProjectService(); session.executeCommand({ seq: 1, type: "request", command: protocol.CommandTypes.Open, arguments: { file: "^memfs:/foo.ts", content: "" } }); @@ -241,7 +241,7 @@ namespace ts.projectSystem { }); it("sends projectsUpdatedInBackground event", async () => { - const importServicePlugin = async (_root: string, _moduleName: string): Promise => { + const importPlugin = async (_root: string, _moduleName: string): Promise => { await Promise.resolve(); // simulate at least a single turn delay return { module: (() => ({ create: info => info.languageService })) as server.PluginModuleFactory, @@ -249,7 +249,7 @@ namespace ts.projectSystem { }; }; - const { session, getMessages } = setup(/*logLevel*/ undefined, { globalPlugins: ["plugin-a"] }, importServicePlugin); + const { session, getMessages } = setup(/*logLevel*/ undefined, { globalPlugins: ["plugin-a"] }, importPlugin); const projectService = session.getProjectService(); session.executeCommand({ seq: 1, type: "request", command: protocol.CommandTypes.Open, arguments: { file: "^memfs:/foo.ts", content: "" } }); @@ -270,7 +270,7 @@ namespace ts.projectSystem { const pluginAShouldLoad = Utils.defer(); const pluginAExternalFilesRequested = Utils.defer(); - const importServicePlugin = async (_root: string, _moduleName: string): Promise => { + const importPlugin = async (_root: string, _moduleName: string): Promise => { // wait until the initial external files are requested from the project service. await pluginAShouldLoad.promise; @@ -287,7 +287,7 @@ namespace ts.projectSystem { }; }; - const { session } = setup(/*logLevel*/ undefined, { globalPlugins: ["plugin-a"] }, importServicePlugin); + const { session } = setup(/*logLevel*/ undefined, { globalPlugins: ["plugin-a"] }, importPlugin); const projectService = session.getProjectService(); session.executeCommand({ seq: 1, type: "request", command: protocol.CommandTypes.Open, arguments: { file: "^memfs:/foo.ts", content: "" } }); @@ -316,7 +316,7 @@ namespace ts.projectSystem { it("project is closed before plugins are loaded", async () => { const pluginALoaded = Utils.defer(); const projectClosed = Utils.defer(); - const importServicePlugin = async (_root: string, _moduleName: string): Promise => { + const importPlugin = async (_root: string, _moduleName: string): Promise => { // mark that the plugin has started loading pluginALoaded.resolve(); @@ -328,7 +328,7 @@ namespace ts.projectSystem { }; }; - const { session, getMessages } = setup(/*logLevel*/ undefined, { globalPlugins: ["plugin-a"] }, importServicePlugin); + const { session, getMessages } = setup(/*logLevel*/ undefined, { globalPlugins: ["plugin-a"] }, importPlugin); const projectService = session.getProjectService(); session.executeCommand({ seq: 1, type: "request", command: protocol.CommandTypes.Open, arguments: { file: "^memfs:/foo.ts", content: "" } }); diff --git a/src/webServer/webServer.ts b/src/webServer/webServer.ts index 0cad87dd97b67..c989c00557ba4 100644 --- a/src/webServer/webServer.ts +++ b/src/webServer/webServer.ts @@ -162,7 +162,7 @@ namespace ts.server { clearImmediate: handle => clearTimeout(handle), /* eslint-enable no-restricted-globals */ - importServicePlugin: async (initialDir: string, moduleName: string): Promise => { + importPlugin: async (initialDir: string, moduleName: string): Promise => { const packageRoot = combinePaths(initialDir, moduleName); let packageJson: any | undefined; diff --git a/tests/baselines/reference/api/tsserverlibrary.d.ts b/tests/baselines/reference/api/tsserverlibrary.d.ts index 5a0a1e2782c5d..2544ef8fc6884 100644 --- a/tests/baselines/reference/api/tsserverlibrary.d.ts +++ b/tests/baselines/reference/api/tsserverlibrary.d.ts @@ -7044,7 +7044,6 @@ declare namespace ts.server { gc?(): void; trace?(s: string): void; require?(initialPath: string, moduleName: string): ModuleImportResult; - importServicePlugin?(root: string, moduleName: string): Promise; } } declare namespace ts.server {