Skip to content

Commit

Permalink
feat(plugin): use local ts binary for meta printer
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Jul 5, 2023
1 parent e5f5fdf commit 1fc18f6
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 42 deletions.
18 changes: 14 additions & 4 deletions lib/compiler/plugins/plugin-metadata-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ export class PluginMetadataGenerator {
private readonly pluginMetadataPrinter = new PluginMetadataPrinter();
private readonly typeCheckerHost = new TypeCheckerHost();
private readonly typescriptLoader = new TypeScriptBinaryLoader();
private readonly tsBinary: typeof ts;

constructor() {
this.tsBinary = this.typescriptLoader.load();
}

generate(options: PluginMetadataGenerateOptions) {
const {
Expand Down Expand Up @@ -145,9 +150,14 @@ export class PluginMetadataGenerator {
...visitor.typeImports,
};
});
this.pluginMetadataPrinter.print(collectedMetadata, typeImports, {
outputDir,
filename,
});
this.pluginMetadataPrinter.print(
collectedMetadata,
typeImports,
{
outputDir,
filename,
},
this.tsBinary,
);
}
}
88 changes: 50 additions & 38 deletions lib/compiler/plugins/plugin-metadata-printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,46 +24,50 @@ export class PluginMetadataPrinter {
metadata: ComposedPluginMeta,
typeImports: Record<string, string>,
options: PluginMetadataPrintOptions,
tsBinary: typeof ts,
) {
const objectLiteralExpr = ts.factory.createObjectLiteralExpression(
const objectLiteralExpr = tsBinary.factory.createObjectLiteralExpression(
Object.keys(metadata).map((key) =>
this.recursivelyCreatePropertyAssignment(
key,
metadata[key] as unknown as Array<
[ts.CallExpression, DeepPluginMeta]
>,
tsBinary,
),
),
);

const exportAssignment = ts.factory.createExportAssignment(
const exportAssignment = tsBinary.factory.createExportAssignment(
undefined,
undefined,
ts.factory.createArrowFunction(
[ts.factory.createToken(ts.SyntaxKind.AsyncKeyword)],
tsBinary.factory.createArrowFunction(
[tsBinary.factory.createToken(tsBinary.SyntaxKind.AsyncKeyword)],
undefined,
[],
undefined,
ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken),
ts.factory.createBlock(
tsBinary.factory.createToken(
tsBinary.SyntaxKind.EqualsGreaterThanToken,
),
tsBinary.factory.createBlock(
[
this.createTypeImportVariableStatement(typeImports),
ts.factory.createReturnStatement(objectLiteralExpr),
this.createTypeImportVariableStatement(typeImports, tsBinary),
tsBinary.factory.createReturnStatement(objectLiteralExpr),
],
true,
),
),
);

const printer = ts.createPrinter({
newLine: ts.NewLineKind.LineFeed,
const printer = tsBinary.createPrinter({
newLine: tsBinary.NewLineKind.LineFeed,
});
const resultFile = ts.createSourceFile(
const resultFile = tsBinary.createSourceFile(
'file.ts',
'',
ts.ScriptTarget.Latest,
tsBinary.ScriptTarget.Latest,
/*setParentNodes*/ false,
ts.ScriptKind.TS,
tsBinary.ScriptKind.TS,
);

const filename = join(
Expand All @@ -75,7 +79,7 @@ export class PluginMetadataPrinter {
filename,
eslintPrefix +
printer.printNode(
ts.EmitHint.Unspecified,
tsBinary.EmitHint.Unspecified,
exportAssignment,
resultFile,
),
Expand All @@ -85,15 +89,16 @@ export class PluginMetadataPrinter {
private recursivelyCreatePropertyAssignment(
identifier: string,
meta: DeepPluginMeta | Array<[ts.CallExpression, DeepPluginMeta]>,
tsBinary: typeof ts,
): ts.PropertyAssignment {
if (Array.isArray(meta)) {
return ts.factory.createPropertyAssignment(
ts.factory.createStringLiteral(identifier),
ts.factory.createArrayLiteralExpression(
return tsBinary.factory.createPropertyAssignment(
tsBinary.factory.createStringLiteral(identifier),
tsBinary.factory.createArrayLiteralExpression(
meta.map(([importExpr, meta]) =>
ts.factory.createArrayLiteralExpression([
tsBinary.factory.createArrayLiteralExpression([
importExpr,
ts.factory.createObjectLiteralExpression(
tsBinary.factory.createObjectLiteralExpression(
Object.keys(meta).map((key) =>
this.recursivelyCreatePropertyAssignment(
key,
Expand All @@ -102,6 +107,7 @@ export class PluginMetadataPrinter {
[key: string]: DeepPluginMeta;
}
)[key],
tsBinary,
),
),
),
Expand All @@ -110,11 +116,11 @@ export class PluginMetadataPrinter {
),
);
}
return ts.factory.createPropertyAssignment(
ts.factory.createStringLiteral(identifier),
ts.isObjectLiteralExpression(meta as unknown as ts.Node)
return tsBinary.factory.createPropertyAssignment(
tsBinary.factory.createStringLiteral(identifier),
tsBinary.isObjectLiteralExpression(meta as unknown as ts.Node)
? (meta as ts.ObjectLiteralExpression)
: ts.factory.createObjectLiteralExpression(
: tsBinary.factory.createObjectLiteralExpression(
Object.keys(meta).map((key) =>
this.recursivelyCreatePropertyAssignment(
key,
Expand All @@ -123,6 +129,7 @@ export class PluginMetadataPrinter {
[key: string]: DeepPluginMeta;
}
)[key],
tsBinary,
),
),
),
Expand All @@ -131,37 +138,42 @@ export class PluginMetadataPrinter {

private createTypeImportVariableStatement(
typeImports: Record<string, string>,
tsBinary: typeof ts,
): ts.Statement {
return ts.factory.createVariableStatement(
return tsBinary.factory.createVariableStatement(
undefined,
ts.factory.createVariableDeclarationList(
tsBinary.factory.createVariableDeclarationList(
[
ts.factory.createVariableDeclaration(
ts.factory.createIdentifier(TYPE_IMPORT_VARIABLE_NAME),
tsBinary.factory.createVariableDeclaration(
tsBinary.factory.createIdentifier(TYPE_IMPORT_VARIABLE_NAME),
undefined,
undefined,
ts.factory.createObjectLiteralExpression(
tsBinary.factory.createObjectLiteralExpression(
Object.keys(typeImports).map((ti) =>
this.createPropertyAssignment(ti, typeImports[ti]),
this.createPropertyAssignment(ti, typeImports[ti], tsBinary),
),
true,
),
),
],
ts.NodeFlags.Const |
ts.NodeFlags.AwaitContext |
ts.NodeFlags.ContextFlags |
ts.NodeFlags.TypeExcludesFlags,
tsBinary.NodeFlags.Const |
tsBinary.NodeFlags.AwaitContext |
tsBinary.NodeFlags.ContextFlags |
tsBinary.NodeFlags.TypeExcludesFlags,
),
);
}

private createPropertyAssignment(identifier: string, target: string) {
return ts.factory.createPropertyAssignment(
ts.factory.createComputedPropertyName(
ts.factory.createStringLiteral(identifier),
private createPropertyAssignment(
identifier: string,
target: string,
tsBinary: typeof ts,
) {
return tsBinary.factory.createPropertyAssignment(
tsBinary.factory.createComputedPropertyName(
tsBinary.factory.createStringLiteral(identifier),
),
ts.factory.createIdentifier(target),
tsBinary.factory.createIdentifier(target),
);
}
}

0 comments on commit 1fc18f6

Please sign in to comment.