c885178d5f
Reworks the compiler to output the factories for directives, components and pipes under a new static field called `ngFactoryFn`, instead of the usual `factory` property in their respective defs. This should eventually allow us to inject any kind of decorated class (e.g. a pipe). **Note:** these changes are the first part of the refactor and they don't include injectables. I decided to leave injectables for a follow-up PR, because there's some more cases we need to handle when it comes to their factories. Furthermore, directives, components and pipes make up most of the compiler output tests that need to be refactored and it'll make follow-up PRs easier to review if the tests are cleaned up now. This is part of the larger refactor for FW-1468. PR Close #31953
70 lines
2.0 KiB
TypeScript
70 lines
2.0 KiB
TypeScript
/**
|
|
* @license
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
*
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
* found in the LICENSE file at https://angular.io/license
|
|
*/
|
|
|
|
import {MockDirectory, setup} from '@angular/compiler/test/aot/test_util';
|
|
import {compile, expectEmit} from './mock_compile';
|
|
|
|
describe('compiler compliance: dependency injection', () => {
|
|
const angularFiles = setup({
|
|
compileAngular: false,
|
|
compileFakeCore: true,
|
|
compileAnimations: false,
|
|
});
|
|
|
|
it('should create factory methods', () => {
|
|
const files = {
|
|
app: {
|
|
'spec.ts': `
|
|
import {Component, NgModule, Injectable, Attribute, Host, SkipSelf, Self, Optional} from '@angular/core';
|
|
|
|
@Injectable()
|
|
export class MyService {}
|
|
|
|
@Component({
|
|
selector: 'my-component',
|
|
template: \`\`
|
|
})
|
|
export class MyComponent {
|
|
constructor(
|
|
@Attribute('name') name:string,
|
|
s1: MyService,
|
|
@Host() s2: MyService,
|
|
@Self() s4: MyService,
|
|
@SkipSelf() s3: MyService,
|
|
@Optional() s5: MyService,
|
|
@Self() @Optional() s6: MyService,
|
|
) {}
|
|
}
|
|
|
|
@NgModule({declarations: [MyComponent], providers: [MyService]})
|
|
export class MyModule {}
|
|
`
|
|
}
|
|
};
|
|
|
|
const factory = `
|
|
MyComponent.ngFactoryDef = function MyComponent_Factory(t) {
|
|
return new (t || MyComponent)(
|
|
$r3$.ɵɵinjectAttribute('name'),
|
|
$r3$.ɵɵdirectiveInject(MyService),
|
|
$r3$.ɵɵdirectiveInject(MyService, 1),
|
|
$r3$.ɵɵdirectiveInject(MyService, 2),
|
|
$r3$.ɵɵdirectiveInject(MyService, 4),
|
|
$r3$.ɵɵdirectiveInject(MyService, 8),
|
|
$r3$.ɵɵdirectiveInject(MyService, 10)
|
|
);
|
|
}`;
|
|
|
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
expectEmit(result.source, factory, 'Incorrect factory');
|
|
});
|
|
|
|
});
|