Skip to content

Commit

Permalink
Gate plugins on CLI flag
Browse files Browse the repository at this point in the history
  • Loading branch information
jakebailey committed May 16, 2023
1 parent b808fc2 commit e349801
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,15 @@ export const commonOptionsWithBuild: CommandLineOption[] = [
description: Diagnostics.Set_the_language_of_the_messaging_from_TypeScript_This_does_not_affect_emit,
defaultValueDescription: Diagnostics.Platform_specific
},
{
name: "allowPlugins",
type: "boolean",
affectsSemanticDiagnostics: true,
affectsEmit: true,
affectsBuildInfo: true,
defaultValueDescription: undefined,
isCommandLineOnly: true,
},
];

/** @internal */
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -6236,6 +6236,10 @@
"category": "Error",
"code": 6931
},
"Option '--allowPlugins' must be specified when compiler plugins are present.": {
"category": "Error",
"code": 6932
},

"Variable '{0}' implicitly has an '{1}' type.": {
"category": "Error",
Expand Down
13 changes: 13 additions & 0 deletions src/compiler/pluginUtilities.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { CompilerOptions } from "./types";

/** @internal */
export type Entrypoint = "tsc" | "typescript" | "tsserver" | "tsserverlibrary" | "testRunner";

Expand All @@ -16,3 +18,14 @@ export function getTypeScriptNamespace(): any {
if (currentTsNamespace === undefined) throw new Error("ts namespace unset");
return currentTsNamespace;
}

/** @internal */
export function shouldAllowPlugins(options: CompilerOptions): boolean {
switch (currentEntrypoint) {
case "tsserver":
case "tsserverlibrary":
case "typescript":
return true;
}
return options.allowPlugins ?? false;
}
13 changes: 13 additions & 0 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ import {
setParentRecursive,
setResolvedModule,
setResolvedTypeReferenceDirective,
shouldAllowPlugins,
shouldResolveJsRequire,
skipTrivia,
skipTypeChecking,
Expand Down Expand Up @@ -2712,6 +2713,10 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
}

const compilerOptions = program.getCompilerOptions();
if (!shouldAllowPlugins(compilerOptions)) {
return emptyArray;
}

const customTransformers = mapDefined(compilerOptions.plugins, config => {
if (config.type !== "transformer") return undefined;

Expand Down Expand Up @@ -4444,6 +4449,14 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
});
}

if (options.plugins && !shouldAllowPlugins(options)) {
for (const plugin of options.plugins) {
if (plugin.type === undefined) continue; // Language service plugins. TODO(jakebailey): give these a type, require type, deprecate missing type?
programDiagnostics.add(createCompilerDiagnostic(Diagnostics.Option_allowPlugins_must_be_specified_when_compiler_plugins_are_present));
break;
}
}

// Verify that all the emit files are unique and don't overwrite input files
function verifyEmitFilePath(emitFileName: string | undefined, emitFilesSeen: Set<string>) {
if (emitFileName) {
Expand Down
1 change: 1 addition & 0 deletions src/compiler/tsbuildPublic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ export interface BuildOptions {
/** @internal */ locale?: string;
/** @internal */ generateCpuProfile?: string;
/** @internal */ generateTrace?: string;
allowPlugins?: boolean;

[option: string]: CompilerOptionsValue | undefined;
}
Expand Down
1 change: 1 addition & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7237,6 +7237,7 @@ export interface CompilerOptions {
/** @internal */ showConfig?: boolean;
useDefineForClassFields?: boolean;
customTransformers?: string[];
allowPlugins?: boolean;

[option: string]: CompilerOptionsValue | TsConfigSourceFile | undefined;
}
Expand Down
2 changes: 2 additions & 0 deletions tests/baselines/reference/api/tsclibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3141,6 +3141,7 @@ declare namespace ts {
esModuleInterop?: boolean;
useDefineForClassFields?: boolean;
customTransformers?: string[];
allowPlugins?: boolean;
[option: string]: CompilerOptionsValue | TsConfigSourceFile | undefined;
}
interface WatchOptions {
Expand Down Expand Up @@ -5735,6 +5736,7 @@ declare namespace ts {
sourceMap?: boolean;
inlineSourceMap?: boolean;
traceResolution?: boolean;
allowPlugins?: boolean;
[option: string]: CompilerOptionsValue | undefined;
}
type ReportEmitErrorSummary = (errorCount: number, filesInError: (ReportFileInError | undefined)[]) => void;
Expand Down
2 changes: 2 additions & 0 deletions tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7273,6 +7273,7 @@ declare namespace ts {
esModuleInterop?: boolean;
useDefineForClassFields?: boolean;
customTransformers?: string[];
allowPlugins?: boolean;
[option: string]: CompilerOptionsValue | TsConfigSourceFile | undefined;
}
interface WatchOptions {
Expand Down Expand Up @@ -9870,6 +9871,7 @@ declare namespace ts {
sourceMap?: boolean;
inlineSourceMap?: boolean;
traceResolution?: boolean;
allowPlugins?: boolean;
[option: string]: CompilerOptionsValue | undefined;
}
type ReportEmitErrorSummary = (errorCount: number, filesInError: (ReportFileInError | undefined)[]) => void;
Expand Down
2 changes: 2 additions & 0 deletions tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3226,6 +3226,7 @@ declare namespace ts {
esModuleInterop?: boolean;
useDefineForClassFields?: boolean;
customTransformers?: string[];
allowPlugins?: boolean;
[option: string]: CompilerOptionsValue | TsConfigSourceFile | undefined;
}
interface WatchOptions {
Expand Down Expand Up @@ -5823,6 +5824,7 @@ declare namespace ts {
sourceMap?: boolean;
inlineSourceMap?: boolean;
traceResolution?: boolean;
allowPlugins?: boolean;
[option: string]: CompilerOptionsValue | undefined;
}
type ReportEmitErrorSummary = (errorCount: number, filesInError: (ReportFileInError | undefined)[]) => void;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"allowPlugins": true
}
}

0 comments on commit e349801

Please sign in to comment.