Skip to content

Commit 41fc675

Browse files
committed
Gate plugins on CLI flag
1 parent f610b44 commit 41fc675

File tree

10 files changed

+52
-0
lines changed

10 files changed

+52
-0
lines changed

Diff for: 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 */

Diff for: src/compiler/diagnosticMessages.json

+4
Original file line numberDiff line numberDiff line change
@@ -6236,6 +6236,10 @@
62366236
"category": "Error",
62376237
"code": 6931
62386238
},
6239+
"Option '--allowPlugins' must be specified when compiler plugins are present.": {
6240+
"category": "Error",
6241+
"code": 6932
6242+
},
62396243

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

Diff for: 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+
}

Diff for: src/compiler/program.ts

+13
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ import {
294294
setParentRecursive,
295295
setResolvedModule,
296296
setResolvedTypeReferenceDirective,
297+
shouldAllowPlugins,
297298
shouldResolveJsRequire,
298299
skipTrivia,
299300
skipTypeChecking,
@@ -2712,6 +2713,10 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
27122713
}
27132714

27142715
const compilerOptions = program.getCompilerOptions();
2716+
if (!shouldAllowPlugins(compilerOptions)) {
2717+
return emptyArray;
2718+
}
2719+
27152720
const customTransformers = mapDefined(compilerOptions.plugins, config => {
27162721
if (config.type !== "transformer") return undefined;
27172722

@@ -4444,6 +4449,14 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
44444449
});
44454450
}
44464451

4452+
if (options.plugins && !shouldAllowPlugins(options)) {
4453+
for (const plugin of options.plugins) {
4454+
if (plugin.type === undefined) continue; // Language service plugins. TODO(jakebailey): give these a type, require type, deprecate missing type?
4455+
programDiagnostics.add(createCompilerDiagnostic(Diagnostics.Option_allowPlugins_must_be_specified_when_compiler_plugins_are_present));
4456+
break;
4457+
}
4458+
}
4459+
44474460
// Verify that all the emit files are unique and don't overwrite input files
44484461
function verifyEmitFilePath(emitFileName: string | undefined, emitFilesSeen: Set<string>) {
44494462
if (emitFileName) {

Diff for: 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
}

Diff for: src/compiler/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -7234,6 +7234,7 @@ export interface CompilerOptions {
72347234
/** @internal */ showConfig?: boolean;
72357235
useDefineForClassFields?: boolean;
72367236
customTransformers?: string[];
7237+
allowPlugins?: boolean;
72377238

72387239
[option: string]: CompilerOptionsValue | TsConfigSourceFile | undefined;
72397240
}

Diff for: tests/baselines/reference/api/tsclibrary.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -3141,6 +3141,7 @@ declare namespace ts {
31413141
esModuleInterop?: boolean;
31423142
useDefineForClassFields?: boolean;
31433143
customTransformers?: string[];
3144+
allowPlugins?: boolean;
31443145
[option: string]: CompilerOptionsValue | TsConfigSourceFile | undefined;
31453146
}
31463147
interface WatchOptions {
@@ -5735,6 +5736,7 @@ declare namespace ts {
57355736
sourceMap?: boolean;
57365737
inlineSourceMap?: boolean;
57375738
traceResolution?: boolean;
5739+
allowPlugins?: boolean;
57385740
[option: string]: CompilerOptionsValue | undefined;
57395741
}
57405742
type ReportEmitErrorSummary = (errorCount: number, filesInError: (ReportFileInError | undefined)[]) => void;

Diff for: tests/baselines/reference/api/tsserverlibrary.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -7273,6 +7273,7 @@ declare namespace ts {
72737273
esModuleInterop?: boolean;
72747274
useDefineForClassFields?: boolean;
72757275
customTransformers?: string[];
7276+
allowPlugins?: boolean;
72767277
[option: string]: CompilerOptionsValue | TsConfigSourceFile | undefined;
72777278
}
72787279
interface WatchOptions {
@@ -9870,6 +9871,7 @@ declare namespace ts {
98709871
sourceMap?: boolean;
98719872
inlineSourceMap?: boolean;
98729873
traceResolution?: boolean;
9874+
allowPlugins?: boolean;
98739875
[option: string]: CompilerOptionsValue | undefined;
98749876
}
98759877
type ReportEmitErrorSummary = (errorCount: number, filesInError: (ReportFileInError | undefined)[]) => void;

Diff for: tests/baselines/reference/api/typescript.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -3226,6 +3226,7 @@ declare namespace ts {
32263226
esModuleInterop?: boolean;
32273227
useDefineForClassFields?: boolean;
32283228
customTransformers?: string[];
3229+
allowPlugins?: boolean;
32293230
[option: string]: CompilerOptionsValue | TsConfigSourceFile | undefined;
32303231
}
32313232
interface WatchOptions {
@@ -5823,6 +5824,7 @@ declare namespace ts {
58235824
sourceMap?: boolean;
58245825
inlineSourceMap?: boolean;
58255826
traceResolution?: boolean;
5827+
allowPlugins?: boolean;
58265828
[option: string]: CompilerOptionsValue | undefined;
58275829
}
58285830
type ReportEmitErrorSummary = (errorCount: number, filesInError: (ReportFileInError | undefined)[]) => void;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"compilerOptions": {
3+
"allowPlugins": true
4+
}
5+
}

0 commit comments

Comments
 (0)