|
// actually will be just the .d.ts from compiler, plus the following declarations: |
|
export interface CompilerPluginContext { |
|
readonly compilerHost: CompilerHost; |
|
readonly compilerOptions: CompilerOptions; |
|
readonly options: MapLike<any>; // plugin config options |
|
} |
|
|
|
export type CompilerPluginHookResult<T> = PromiseLike<T> | T; |
|
|
|
export interface CompilerPluginModule { |
|
activate?(context: CompilerPluginContext, args: CompilerPluginActivationArgs): |
|
CompilerPluginHookResult<CompilerPluginActivationResult | void>; |
|
|
|
preParse?(context: CompilerPluginContext, args: CompilerPluginPreParseArgs): |
|
CompilerPluginHookResult<CompilerPluginPreParseResult | void>; |
|
|
|
preEmit?(context: CompilerPluginContext, args: CompilerPluginPreEmitArgs): |
|
CompilerPluginHookResult<CompilerPluginPreEmitResult | void>; |
|
|
|
deactivate?(context: CompilerPluginContext): CompilerPluginHookResult<void>; |
|
} |
|
|
|
export interface CompilerPluginResult { |
|
diagnostics?: ReadonlyArray<Diagnostic>; |
|
} |
|
|
|
export interface CompilerPluginActivationArgs { |
|
} |
|
|
|
/** |
|
* An optional result that can be returned from the `CompilerPluginModule.activate` hook. |
|
*/ |
|
export interface CompilerPluginActivationResult extends CompilerPluginResult { |
|
} |
|
|
|
export interface CompilerPluginPreParseArgs { |
|
readonly rootNames: readonly string[]; |
|
readonly projectReferences: readonly ProjectReference[] | undefined; |
|
} |
|
|
|
export interface CompilerPluginPreParseResult extends CompilerPluginResult { |
|
rootNames?: readonly string[]; |
|
projectReferences?: readonly ProjectReference[]; |
|
preprocessors?: readonly TransformerFactory<SourceFile>[]; |
|
} |
|
|
|
export interface CompilerPluginPreEmitArgs { |
|
/** The current `Program`. */ |
|
readonly program: BaseProgram; |
|
/** The `SourceFile` that is about to be emitted, or `undefined` when emitting all outputs. */ |
|
readonly targetSourceFile: SourceFile | undefined; |
|
/** A `CancellationToken` that can be used to abort an operation when running in the language service. */ |
|
readonly cancellationToken: CancellationToken | undefined; |
|
} |
|
|
|
/** |
|
* An optional result that can be returned from the `CompilerPluginModule.preEmit` hook. |
|
*/ |
|
export interface CompilerPluginPreEmitResult extends CompilerPluginResult { |
|
customTransformers?: CustomTransformers; |
|
} |
|
|
|
export interface CompilerPluginDeactivationResult extends CompilerPluginResult { |
|
} |
|
|
|
// NOTE: All factory methods originally on `ts` have been moved to `NodeFactory`, which is accessible |
|
// from the `factory` property of a transformation context. Existing factory methods exist for |
|
// backwards compatibility when using the full TypeScript Language Service or TS Server API, but |
|
// will not be accessible for Compiler Plugins. |
|
export interface NodeFactory { |
|
createNodeArray<T extends Node>(elements?: readonly T[], hasTrailingComma?: boolean): NodeArray<T>; |
|
createNumericLiteral(value: string | number, numericLiteralFlags?: TokenFlags): NumericLiteral; |
|
... |
|
} |
|
|
|
export interface TransformationContext { |
|
// The `NodeFactory` you should use to create nodes. |
|
// NOTE: Preprocessors (returned from the `preParse` hook) and Emit Transformers (returned |
|
// from the `preEmit` hook) will be provided *different* `NodeFactory` instances and should not be conflated. |
|
readonly factory: NodeFactory; |
|
... |
|
} |
|
|
|
export interface BaseProgram { |
|
// essentially the same as `Program`, except the `emit` method is not provided. |
|
... |
|
} |
Also worth noting: The
CompilerHost
in a language service has a very limited lifetime. The host we pass to a plugin should probably be a proxy to a real host, which after the normal synchronous execution of the plugin expires, we poison the methods of and remove the reference to the underlying "real" host, to prevent any methods getting saved off and (potentially accidentally) used asynchronously and potentially poisoning or retaining old programs.