Skip to content

Commit

Permalink
fix: Plugins returning TransformerBasePlugin not supported (fixes #113)
Browse files Browse the repository at this point in the history
  • Loading branch information
nonara committed Jul 20, 2023
1 parent 0bba677 commit 12ee3a2
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 112 deletions.
3 changes: 2 additions & 1 deletion projects/core/shared/plugin-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ export interface PluginConfig {
}

export type TransformerList = Required<ts.CustomTransformers>;
export type TransformerPlugin = TransformerBasePlugin | ts.TransformerFactory<ts.SourceFile>;
export type TransformerPlugin = TransformerBasePlugin | TsTransformerFactory;
export type TsTransformerFactory = ts.TransformerFactory<ts.SourceFile>

export type PluginFactory =
LSPattern | ProgramPattern | ConfigPattern | CompilerOptionsPattern | TypeCheckerPattern | RawPattern;
Expand Down
41 changes: 29 additions & 12 deletions projects/patch/src/plugin/plugin-creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,32 @@ namespace tsp {

if (!transform) throw new TsPatchError('Not a valid config entry: "transform" key not found');

let transformerFn: PluginFactory;
const transformerKind = after ? 'after' : afterDeclarations ? 'afterDeclarations' : 'before';

let pluginFactoryResult: TransformerPlugin;
switch (config.type) {
case 'ls':
if (!ls) throw new TsPatchError(`Plugin ${transform} needs a LanguageService`);
transformerFn = (factory as LSPattern)(ls, cleanConfig);
pluginFactoryResult = (factory as LSPattern)(ls, cleanConfig);
break;

case 'config':
transformerFn = (factory as ConfigPattern)(cleanConfig);
pluginFactoryResult = (factory as ConfigPattern)(cleanConfig);
break;

case 'compilerOptions':
transformerFn = (factory as CompilerOptionsPattern)(program.getCompilerOptions(), cleanConfig);
pluginFactoryResult = (factory as CompilerOptionsPattern)(program.getCompilerOptions(), cleanConfig);
break;

case 'checker':
transformerFn = (factory as TypeCheckerPattern)(program.getTypeChecker(), cleanConfig);
pluginFactoryResult = (factory as TypeCheckerPattern)(program.getTypeChecker(), cleanConfig);
break;

case undefined:
case 'program':
const { addDiagnostic, removeDiagnostic, diagnostics } = diagnosticExtrasFactory(program);

transformerFn = (factory as ProgramPattern)(program, cleanConfig, {
pluginFactoryResult = (factory as ProgramPattern)(program, cleanConfig, {
ts: <any>ts,
addDiagnostic,
removeDiagnostic,
Expand All @@ -64,20 +66,35 @@ namespace tsp {
break;

case 'raw':
transformerFn = (ctx: tsShim.TransformationContext) => (factory as RawPattern)(ctx, program, cleanConfig);
pluginFactoryResult = (ctx: tsShim.TransformationContext) => (factory as RawPattern)(ctx, program, cleanConfig);
break;

default:
throw new TsPatchError(`Invalid plugin type found in tsconfig.json: '${config.type}'`);
}

/* Handle result */
let transformerFactory: TsTransformerFactory | undefined;
switch (typeof pluginFactoryResult) {
case 'function':
transformerFactory = pluginFactoryResult;
break;
case 'object':
transformerFactory = pluginFactoryResult[transformerKind];
break;
}

if (!transformerFactory || typeof transformerFactory !== 'function')
throw new TsPatchError(
`Invalid plugin entry point! Expected a transformer factory function or an object with a '${transformerKind}' property`
);

/* Wrap w/ register */
const wrapper = wrapTransformer(transformerFn, registerConfig, true);
const wrapper = wrapTransformer(transformerFactory, registerConfig, true);

const res: TransformerBasePlugin =
after ? ({ after: wrapper }) :
afterDeclarations ? ({ afterDeclarations: wrapper as tsShim.TransformerFactory<tsShim.SourceFile | tsShim.Bundle> }) :
{ before: wrapper };
const res: TransformerBasePlugin = {
[transformerKind]: wrapper
};

return res;
}
Expand Down
3 changes: 2 additions & 1 deletion projects/patch/src/types/plugin-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ declare namespace tsp {
transformProgram?: boolean;
}
export type TransformerList = Required<ts.CustomTransformers>;
export type TransformerPlugin = TransformerBasePlugin | ts.TransformerFactory<ts.SourceFile>;
export type TransformerPlugin = TransformerBasePlugin | TsTransformerFactory;
export type TsTransformerFactory = ts.TransformerFactory<ts.SourceFile>;
export type PluginFactory = LSPattern | ProgramPattern | ConfigPattern | CompilerOptionsPattern | TypeCheckerPattern | RawPattern;
export interface TransformerBasePlugin {
before?: ts.TransformerFactory<ts.SourceFile>;
Expand Down

0 comments on commit 12ee3a2

Please sign in to comment.