Skip to content

Commit

Permalink
Rename API to importPlugin (#50554)
Browse files Browse the repository at this point in the history
* Rename API to importPlugin

* Make it internal too
  • Loading branch information
sheetalkamat committed Aug 31, 2022
1 parent 19defbf commit d293e72
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/server/editorServices.ts
Expand Up @@ -4068,7 +4068,7 @@ namespace ts.server {

/*@internal*/
requestEnablePlugin(project: Project, pluginConfigEntry: PluginImport, searchPaths: string[], pluginConfigOverrides: Map<any> | 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;
}
Expand All @@ -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);
Expand Down
11 changes: 5 additions & 6 deletions src/server/project.ts
Expand Up @@ -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 };
Expand Down Expand Up @@ -1607,7 +1607,7 @@ namespace ts.server {
protected enableGlobalPlugins(options: CompilerOptions, pluginConfigOverrides: Map<any> | 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;
}
Expand Down Expand Up @@ -1658,7 +1658,7 @@ namespace ts.server {
*/
/*@internal*/
async beginEnablePluginAsync(pluginConfigEntry: PluginImport, searchPaths: string[], pluginConfigOverrides: Map<any> | undefined): Promise<BeginEnablePluginResult> {
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);
Expand Down Expand Up @@ -2522,8 +2522,7 @@ namespace ts.server {
/*@internal*/
enablePluginsWithOptions(options: CompilerOptions, pluginConfigOverrides: ESMap<string, any> | 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;
}
Expand Down
3 changes: 2 additions & 1 deletion src/server/types.ts
Expand Up @@ -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<ModuleImportResult>;
/*@internal*/
importPlugin?(root: string, moduleName: string): Promise<ModuleImportResult>;
}
}
24 changes: 12 additions & 12 deletions src/testRunner/unittests/tsserver/webServer.ts
Expand Up @@ -29,7 +29,7 @@ namespace ts.projectSystem {
}
}

function setup(logLevel: server.LogLevel | undefined, options?: Partial<server.StartSessionOptions>, importServicePlugin?: server.ServerHost["importServicePlugin"]) {
function setup(logLevel: server.LogLevel | undefined, options?: Partial<server.StartSessionOptions>, importPlugin?: server.ServerHost["importPlugin"]) {
const host = createServerHost([libFile], { windowsStyleRoot: "c:/" });
const messages: any[] = [];
const webHost: server.WebHost = {
Expand All @@ -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 };
Expand Down Expand Up @@ -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<server.ModuleImportResult> => {
const importPlugin = async (_root: string, _moduleName: string): Promise<server.ModuleImportResult> => {
await Promise.resolve(); // simulate at least a single turn delay
pluginModuleInstantiated = true;
return {
Expand All @@ -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: "" } });
Expand Down Expand Up @@ -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<server.ModuleImportResult> => {
const importPlugin = async (_root: string, moduleName: string): Promise<server.ModuleImportResult> => {
log.push(`request import ${moduleName}`);
const promise = moduleName === "plugin-a" ? pluginADeferred.promise : pluginBDeferred.promise;
await promise;
Expand All @@ -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: "" } });
Expand All @@ -241,15 +241,15 @@ namespace ts.projectSystem {
});

it("sends projectsUpdatedInBackground event", async () => {
const importServicePlugin = async (_root: string, _moduleName: string): Promise<server.ModuleImportResult> => {
const importPlugin = async (_root: string, _moduleName: string): Promise<server.ModuleImportResult> => {
await Promise.resolve(); // simulate at least a single turn delay
return {
module: (() => ({ create: info => info.languageService })) as server.PluginModuleFactory,
error: undefined
};
};

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: "" } });
Expand All @@ -270,7 +270,7 @@ namespace ts.projectSystem {
const pluginAShouldLoad = Utils.defer();
const pluginAExternalFilesRequested = Utils.defer();

const importServicePlugin = async (_root: string, _moduleName: string): Promise<server.ModuleImportResult> => {
const importPlugin = async (_root: string, _moduleName: string): Promise<server.ModuleImportResult> => {
// wait until the initial external files are requested from the project service.
await pluginAShouldLoad.promise;

Expand All @@ -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: "" } });
Expand Down Expand Up @@ -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<server.ModuleImportResult> => {
const importPlugin = async (_root: string, _moduleName: string): Promise<server.ModuleImportResult> => {
// mark that the plugin has started loading
pluginALoaded.resolve();

Expand All @@ -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: "" } });
Expand Down
2 changes: 1 addition & 1 deletion src/webServer/webServer.ts
Expand Up @@ -162,7 +162,7 @@ namespace ts.server {
clearImmediate: handle => clearTimeout(handle),
/* eslint-enable no-restricted-globals */

importServicePlugin: async (initialDir: string, moduleName: string): Promise<ModuleImportResult> => {
importPlugin: async (initialDir: string, moduleName: string): Promise<ModuleImportResult> => {
const packageRoot = combinePaths(initialDir, moduleName);

let packageJson: any | undefined;
Expand Down
1 change: 0 additions & 1 deletion tests/baselines/reference/api/tsserverlibrary.d.ts
Expand Up @@ -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<ModuleImportResult>;
}
}
declare namespace ts.server {
Expand Down

0 comments on commit d293e72

Please sign in to comment.