Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(transformers): Add beforeDeclarations transformers #58879

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/compiler/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,11 @@ function getScriptTransformers(compilerOptions: CompilerOptions, customTransform

function getDeclarationTransformers(customTransformers?: CustomTransformers) {
const transformers: TransformerFactory<SourceFile | Bundle>[] = [];

addRange(transformers, customTransformers && map(customTransformers.beforeDeclarations, wrapDeclarationTransformerFactory));

transformers.push(transformDeclarations);

addRange(transformers, customTransformers && map(customTransformers.afterDeclarations, wrapDeclarationTransformerFactory));
return transformers;
}
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4867,6 +4867,8 @@ export interface CustomTransformers {
before?: (TransformerFactory<SourceFile> | CustomTransformerFactory)[];
/** Custom transformers to evaluate after built-in .js transformations. */
after?: (TransformerFactory<SourceFile> | CustomTransformerFactory)[];
/** Custom transformers to evaluate before built-in .d.ts transformations. */
beforeDeclarations?: (TransformerFactory<Bundle | SourceFile> | CustomTransformerFactory)[];
/** Custom transformers to evaluate after built-in .d.ts transformations. */
afterDeclarations?: (TransformerFactory<Bundle | SourceFile> | CustomTransformerFactory)[];
}
Expand Down
35 changes: 35 additions & 0 deletions src/testRunner/unittests/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,22 @@ describe("unittests:: TransformAPI", () => {
return (file: T) => file;
}

function transformInitializerToDefaultJSDoc<T extends ts.SourceFile | ts.Bundle>(context: ts.TransformationContext) {
return (file: T) => {
if (!ts.isSourceFile(file)) {
return file;
}
const visitor: ts.Visitor = (node: ts.Node): ts.VisitResult<ts.Node> => {
if (ts.isPropertyDeclaration(node) && node.initializer) {
return ts.addSyntheticLeadingComment(node, /*kind*/ ts.SyntaxKind.MultiLineCommentTrivia, /*text*/ `* @default \`${node.initializer.getText()}\``, /*hasTrailingNewLine*/ true);
}
return ts.visitEachChild(node, visitor, context);
};

return ts.visitNode(file, visitor) as T;
};
}

function replaceIdentifiersNamedOldNameWithNewName2(context: ts.TransformationContext) {
const visitor = (node: ts.Node): ts.Node => {
if (ts.isIdentifier(node) && node.text === "oldName") {
Expand Down Expand Up @@ -393,6 +409,25 @@ describe("unittests:: TransformAPI", () => {
});
});

testBaseline("transformDeclarationFileBefore", () => {
return baselineDeclarationTransform(
`
export class Test {
public foo: string = "bar"
}
`,
{
transformers: {
beforeDeclarations: [transformInitializerToDefaultJSDoc],
},
compilerOptions: {
newLine: ts.NewLineKind.CarriageReturnLineFeed,
declaration: true,
},
},
);
});

// https://github.com/microsoft/TypeScript/issues/33295
testBaseline("transformParameterProperty", () => {
return transpileModule("", {
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 @@ -6058,6 +6058,8 @@ declare namespace ts {
before?: (TransformerFactory<SourceFile> | CustomTransformerFactory)[];
/** Custom transformers to evaluate after built-in .js transformations. */
after?: (TransformerFactory<SourceFile> | CustomTransformerFactory)[];
/** Custom transformers to evaluate before built-in .d.ts transformations. */
beforeDeclarations?: (TransformerFactory<Bundle | SourceFile> | CustomTransformerFactory)[];
/** Custom transformers to evaluate after built-in .d.ts transformations. */
afterDeclarations?: (TransformerFactory<Bundle | SourceFile> | CustomTransformerFactory)[];
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export declare class Test {
/** @default `"bar"`*/
foo: string;
}