Skip to content

Commit 05b1015

Browse files
committed
Gate plugins on CLI flag
1 parent 749882e commit 05b1015

File tree

6 files changed

+41
-0
lines changed

6 files changed

+41
-0
lines changed

src/compiler/commandLineParser.ts

+9
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,15 @@ export const commonOptionsWithBuild: CommandLineOption[] = [
500500
description: Diagnostics.Set_the_language_of_the_messaging_from_TypeScript_This_does_not_affect_emit,
501501
defaultValueDescription: Diagnostics.Platform_specific
502502
},
503+
{
504+
name: "allowPlugins",
505+
type: "boolean",
506+
affectsSemanticDiagnostics: true,
507+
affectsEmit: true,
508+
affectsBuildInfo: true,
509+
defaultValueDescription: undefined,
510+
isCommandLineOnly: true,
511+
},
503512
];
504513

505514
/** @internal */

src/compiler/diagnosticMessages.json

+4
Original file line numberDiff line numberDiff line change
@@ -6232,6 +6232,10 @@
62326232
"category": "Error",
62336233
"code": 6931
62346234
},
6235+
"Option '--allowPlugins' must be specified when compiler plugins are present.": {
6236+
"category": "Error",
6237+
"code": 6932
6238+
},
62356239

62366240
"Variable '{0}' implicitly has an '{1}' type.": {
62376241
"category": "Error",

src/compiler/pluginUtilities.ts

+13
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { CompilerOptions } from "./types";
2+
13
/** @internal */
24
export type Entrypoint = "tsc" | "typescript" | "tsserver" | "tsserverlibrary" | "testRunner";
35

@@ -16,3 +18,14 @@ export function getTypeScriptNamespace(): any {
1618
if (currentTsNamespace === undefined) throw new Error("ts namespace unset");
1719
return currentTsNamespace;
1820
}
21+
22+
/** @internal */
23+
export function shouldAllowPlugins(options: CompilerOptions): boolean {
24+
switch (currentEntrypoint) {
25+
case "tsserver":
26+
case "tsserverlibrary":
27+
case "typescript":
28+
return true;
29+
}
30+
return options.allowPlugins ?? false;
31+
}

src/compiler/program.ts

+13
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ import {
293293
setParentRecursive,
294294
setResolvedModule,
295295
setResolvedTypeReferenceDirective,
296+
shouldAllowPlugins,
296297
shouldResolveJsRequire,
297298
skipTrivia,
298299
skipTypeChecking,
@@ -2680,6 +2681,10 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
26802681
}
26812682

26822683
const compilerOptions = program.getCompilerOptions();
2684+
if (!shouldAllowPlugins(compilerOptions)) {
2685+
return emptyArray;
2686+
}
2687+
26832688
const customTransformers = mapDefined(compilerOptions.plugins, config => {
26842689
if (config.type !== "transformer") return undefined;
26852690

@@ -4412,6 +4417,14 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
44124417
});
44134418
}
44144419

4420+
if (options.plugins && !shouldAllowPlugins(options)) {
4421+
for (const plugin of options.plugins) {
4422+
if (plugin.type === undefined) continue; // Language service plugins. TODO(jakebailey): give these a type, require type, deprecate missing type?
4423+
programDiagnostics.add(createCompilerDiagnostic(Diagnostics.Option_allowPlugins_must_be_specified_when_compiler_plugins_are_present));
4424+
break;
4425+
}
4426+
}
4427+
44154428
// Verify that all the emit files are unique and don't overwrite input files
44164429
function verifyEmitFilePath(emitFileName: string | undefined, emitFilesSeen: Set<string>) {
44174430
if (emitFileName) {

src/compiler/tsbuildPublic.ts

+1
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ export interface BuildOptions {
162162
/** @internal */ locale?: string;
163163
/** @internal */ generateCpuProfile?: string;
164164
/** @internal */ generateTrace?: string;
165+
allowPlugins?: boolean;
165166

166167
[option: string]: CompilerOptionsValue | undefined;
167168
}

src/compiler/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -7219,6 +7219,7 @@ export interface CompilerOptions {
72197219
/** @internal */ showConfig?: boolean;
72207220
useDefineForClassFields?: boolean;
72217221
customTransformers?: string[];
7222+
allowPlugins?: boolean;
72227223

72237224
[option: string]: CompilerOptionsValue | TsConfigSourceFile | undefined;
72247225
}

0 commit comments

Comments
 (0)