fix(compiler-cli): bypass static resolving of the component's changeDetection field in local compilation mode (#51848)

Currently the field changeDetection undergoes some static analysis to check if it is `ChangeDetectionStrategy` enum. Such static check fails in local compilation mode in g3 as the symbol cannot be resolved. So in local compilation mode we bypass such resolving and just write the expression as is into the component definition.

PR Close #51848
This commit is contained in:
Payam Valadkhan
2023-09-20 18:34:19 -04:00
committed by Dylan Hunn
parent dcaad169ec
commit 377a7abfda
6 changed files with 53 additions and 13 deletions
@@ -235,8 +235,14 @@ export class ComponentDecoratorHandler implements
const encapsulation: number =
resolveEnumValue(this.evaluator, component, 'encapsulation', 'ViewEncapsulation') ??
ViewEncapsulation.Emulated;
const changeDetection: number|null =
resolveEnumValue(this.evaluator, component, 'changeDetection', 'ChangeDetectionStrategy');
let changeDetection: number|Expression|null = null;
if (this.compilationMode !== CompilationMode.LOCAL) {
changeDetection =
resolveEnumValue(this.evaluator, component, 'changeDetection', 'ChangeDetectionStrategy');
} else if (component.has('changeDetection')) {
changeDetection = new WrappedNodeExpr(component.get('changeDetection')!);
}
let animations: Expression|null = null;
let animationTriggerNames: AnimationTriggerNames|null = null;
@@ -459,6 +465,7 @@ export class ComponentDecoratorHandler implements
ngContentSelectors: template.ngContentSelectors,
},
encapsulation,
changeDetection,
interpolation: template.interpolationConfig ?? DEFAULT_INTERPOLATION_CONFIG,
styles,
@@ -494,9 +501,7 @@ export class ComponentDecoratorHandler implements
},
diagnostics,
};
if (changeDetection !== null) {
output.analysis!.meta.changeDetection = changeDetection;
}
return output;
}
@@ -345,6 +345,27 @@ runInEachFileSystem(() => {
});
});
describe('component fields', () => {
it('should place the changeDetection as it is into the component def', () => {
env.write('test.ts', `
import {Component} from '@angular/core';
import {SomeWeirdThing} from 'some-where';
@Component({
changeDetection: SomeWeirdThing,
template: '<span>Hello world!</span>',
})
export class MainComponent {
}
`);
env.driveMain();
const jsContents = env.getContents('test.js');
expect(jsContents).toContain('changeDetection: SomeWeirdThing');
});
});
describe('constructor injection', () => {
it('should include injector types with all possible import/injection styles into component factory',
() => {