Skip to content

Commit

Permalink
refactor(compiler-cli): support running JIT transforms as part of tsi…
Browse files Browse the repository at this point in the history
…ckle emit

When running the JIT transforms in 1P w/ tsickle, tsickle will
transform source files before our custom transforms can run. This is
also impacting the Ivy transform and hence we use `ts.getOriginalNode`
in various places to inspect the source AST for detecting Angular.

For the JIT transform we need to do a similar change so that the
transform could run in 1P.
  • Loading branch information
devversion committed Jun 19, 2024
1 parent 0bd55a6 commit ab676f6
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,12 @@ function createTransformVisitor(
): ts.Visitor<ts.Node, ts.Node> {
const visitor: ts.Visitor<ts.Node, ts.Node> = (node: ts.Node): ts.Node => {
if (ts.isClassDeclaration(node) && node.name !== undefined) {
const originalNode = ts.getOriginalNode(node, ts.isClassDeclaration);
// Note: Attempt to detect the `angularDecorator` on the original node of the class.
// That is because e.g. Tsickle or other transforms might have transformed the node
// already to transform decorators.
const angularDecorator = host
.getDecoratorsOfDeclaration(node)
.getDecoratorsOfDeclaration(originalNode)
?.find((d) => decoratorsWithInputs.some((name) => isAngularDecorator(d, name, isCore)));

if (angularDecorator !== undefined) {
Expand Down
53 changes: 51 additions & 2 deletions packages/compiler-cli/test/initializer_api_transforms_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ describe('initializer API metadata transform', () => {
host = new MockCompilerHost(context);
});

function transform(contents: string, postDownlevelDecoratorsTransform = false) {
function transform(
contents: string,
postDownlevelDecoratorsTransform = false,
customPreTransform?: ts.TransformerFactory<ts.SourceFile>,
) {
context.writeFile(TEST_FILE_INPUT, contents);

const program = ts.createProgram(
Expand All @@ -59,7 +63,10 @@ describe('initializer API metadata transform', () => {
const reflectionHost = new TypeScriptReflectionHost(typeChecker);
const importTracker = new ImportedSymbolsTracker();
const transformers: ts.CustomTransformers = {
before: [getInitializerApiJitTransform(reflectionHost, importTracker, /* isCore */ false)],
before: [
...(customPreTransform ? [customPreTransform] : []),
getInitializerApiJitTransform(reflectionHost, importTracker, /* isCore */ false),
],
};

if (postDownlevelDecoratorsTransform) {
Expand Down Expand Up @@ -269,6 +276,48 @@ describe('initializer API metadata transform', () => {
`),
);
});

it('should migrate if the class decorator is downleveled in advance', () => {
const fakeDownlevelPreTransform = (ctx: ts.TransformationContext) => {
return (sf: ts.SourceFile) => {
const visitor = (node: ts.Node) => {
if (ts.isClassDeclaration(node)) {
return ctx.factory.updateClassDeclaration(
node,
[],
node.name,
node.typeParameters,
node.heritageClauses,
node.members,
);
}
return node;
};
return ts.visitEachChild(sf, visitor, ctx);
};
};

const result = transform(
`
import {input, Directive} from '@angular/core';
@Directive({})
class MyDir {
someInput = input(0);
}
`,
false,
fakeDownlevelPreTransform,
);

expect(result).toContain(
omitLeadingWhitespace(`
__decorate([
i0.Input({ isSignal: true, alias: "someInput", required: false, transform: undefined })
], MyDir.prototype, "someInput", void 0);
`),
);
});
});

describe('model()', () => {
Expand Down

0 comments on commit ab676f6

Please sign in to comment.