|
// 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. |
|
... |
|
} |
I'd generally prefer to be more declarative, specifying something akin to VS Code's activation events in the plugin's package.json, and possibly plugin dependencies (which would need to be added to both the
"dependencies"
map as well as the plugin's configuration in package.json).