Skip to content

Commit 664ed17

Browse files
Allow only package names as plugin names (#42713)
* Allow only package names as plugin names * Remove extra argument following merge from master branch. * kipped -> Skipped Co-authored-by: Sheetal Nandi <[email protected]>
1 parent fe2899c commit 664ed17

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

src/server/project.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1581,6 +1581,10 @@ namespace ts.server {
15811581

15821582
protected enablePlugin(pluginConfigEntry: PluginImport, searchPaths: string[], pluginConfigOverrides: Map<any> | undefined) {
15831583
this.projectService.logger.info(`Enabling plugin ${pluginConfigEntry.name} from candidate paths: ${searchPaths.join(",")}`);
1584+
if (parsePackageName(pluginConfigEntry.name).rest) {
1585+
this.projectService.logger.info(`Skipped loading plugin ${pluginConfigEntry.name} because only package name is allowed plugin name`);
1586+
return;
1587+
}
15841588

15851589
const log = (message: string) => this.projectService.logger.info(message);
15861590
let errorLogs: string[] | undefined;

src/testRunner/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@
184184
"unittests/tsserver/openFile.ts",
185185
"unittests/tsserver/packageJsonInfo.ts",
186186
"unittests/tsserver/partialSemanticServer.ts",
187+
"unittests/tsserver/plugins.ts",
187188
"unittests/tsserver/projectErrors.ts",
188189
"unittests/tsserver/projectReferenceCompileOnSave.ts",
189190
"unittests/tsserver/projectReferenceErrors.ts",
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
namespace ts.projectSystem {
2+
describe("unittests:: tsserver:: plugins loading", () => {
3+
function createHostWithPlugin(files: readonly File[]) {
4+
const host = createServerHost(files);
5+
const pluginsLoaded: string[] = [];
6+
host.require = (_initialPath, moduleName) => {
7+
pluginsLoaded.push(moduleName);
8+
return {
9+
module: () => ({
10+
create(info: server.PluginCreateInfo) {
11+
return Harness.LanguageService.makeDefaultProxy(info);
12+
}
13+
}),
14+
error: undefined
15+
};
16+
};
17+
return { host, pluginsLoaded };
18+
}
19+
20+
it("With local plugins", () => {
21+
const expectedToLoad = ["@myscoped/plugin", "unscopedPlugin"];
22+
const notToLoad = ["../myPlugin", "myPlugin/../malicious"];
23+
const aTs: File = { path: "/a.ts", content: `class c { prop = "hello"; foo() { return this.prop; } }` };
24+
const tsconfig: File = {
25+
path: "/tsconfig.json",
26+
content: JSON.stringify({
27+
compilerOptions: { plugins: [...expectedToLoad, ...notToLoad].map(name => ({ name })) }
28+
})
29+
};
30+
const { host, pluginsLoaded } = createHostWithPlugin([aTs, tsconfig, libFile]);
31+
const service = createProjectService(host);
32+
service.openClientFile(aTs.path);
33+
assert.deepEqual(pluginsLoaded, expectedToLoad);
34+
});
35+
36+
it("With global plugins", () => {
37+
const expectedToLoad = ["@myscoped/plugin", "unscopedPlugin"];
38+
const notToLoad = ["../myPlugin", "myPlugin/../malicious"];
39+
const aTs: File = { path: "/a.ts", content: `class c { prop = "hello"; foo() { return this.prop; } }` };
40+
const tsconfig: File = {
41+
path: "/tsconfig.json",
42+
content: "{}"
43+
};
44+
const { host, pluginsLoaded } = createHostWithPlugin([aTs, tsconfig, libFile]);
45+
const service = createProjectService(host, { globalPlugins: [...expectedToLoad, ...notToLoad] });
46+
service.openClientFile(aTs.path);
47+
assert.deepEqual(pluginsLoaded, expectedToLoad);
48+
});
49+
});
50+
}

0 commit comments

Comments
 (0)