Skip to content

Commit 127076a

Browse files
committed
feat(app): Directive composition API for directives and components
fix #1340
1 parent 22a22ce commit 127076a

29 files changed

+251
-24
lines changed

src-refactored/core/use-cases/scan-files.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ describe('Use-cases - Should scan folders', () => {
66
it('should find files', async () => {
77
const testFolderpath = 'test/fixtures/todomvc-ng2';
88
const files = await ScanFiles.scan(testFolderpath);
9-
expect(files.length).equal(70);
9+
expect(files.length).equal(73);
1010
});
1111
});

src/app/compiler/angular/deps/component-dep.factory.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export class ComponentDepFactory {
3434
template: this.helper.getComponentTemplate(props, srcFile),
3535
templateUrl: this.helper.getComponentTemplateUrl(props, srcFile),
3636
viewProviders: this.helper.getComponentViewProviders(props, srcFile),
37+
hostDirectives: [...this.helper.getComponentHostDirectives(props)],
3738
inputsClass: IO.inputs,
3839
outputsClass: IO.outputs,
3940
propertiesClass: IO.properties,
@@ -124,6 +125,7 @@ export interface IComponentDep extends IDep {
124125
entryComponents: Array<any>;
125126

126127
hostBindings: Array<any>;
128+
hostDirectives: Array<any>;
127129
hostListeners: Array<any>;
128130

129131
description: string;

src/app/compiler/angular/deps/directive-dep.factory.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export class DirectiveDepFactory {
2222
selector: this.helper.getComponentSelector(props, srcFile),
2323
providers: this.helper.getComponentProviders(props, srcFile),
2424
exportAs: this.helper.getComponentExportAs(props, srcFile),
25+
hostDirectives: [...this.helper.getComponentHostDirectives(props)],
2526

2627
standalone: this.helper.getComponentStandalone(props, srcFile) ? true : false,
2728

@@ -38,6 +39,7 @@ export class DirectiveDepFactory {
3839
methodsClass: IO.methods,
3940
exampleUrls: this.helper.getComponentExampleUrls(srcFile.getText())
4041
};
42+
4143
if (Configuration.mainData.disableLifeCycleHooks) {
4244
directiveDeps.methodsClass = cleanLifecycleHooksFromMethods(directiveDeps.methodsClass);
4345
}
@@ -79,6 +81,7 @@ export interface IDirectiveDep extends IDep {
7981
deprecationMessage: string;
8082

8183
hostBindings: any;
84+
hostDirectives: any;
8285
hostListeners: any;
8386

8487
propertiesClass: any;

src/app/compiler/angular/deps/helpers/class-helper.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -737,16 +737,16 @@ export class ClassHelper {
737737
} else if (outputDecorator && outputDecorator.length > 0) {
738738
outputs.push(this.visitOutput(member, outputDecorator[0], sourceFile));
739739
} else if (parsedHostBindings && parsedHostBindings.length > 0) {
740-
let k = 0,
741-
lenHB = parsedHostBindings.length;
740+
let k = 0;
741+
const lenHB = parsedHostBindings.length;
742742
for (k; k < lenHB; k++) {
743743
hostBindings.push(
744744
this.visitInputAndHostBinding(member, parsedHostBindings[k], sourceFile)
745745
);
746746
}
747747
} else if (parsedHostListeners && parsedHostListeners.length > 0) {
748-
let l = 0,
749-
lenHL = parsedHostListeners.length;
748+
let l = 0;
749+
const lenHL = parsedHostListeners.length;
750750
for (l; l < lenHL; l++) {
751751
hostListeners.push(
752752
this.visitHostListener(member, parsedHostListeners[l], sourceFile)

src/app/compiler/angular/deps/helpers/component-helper.ts

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ts } from 'ts-morph';
1+
import { SyntaxKind, ts } from 'ts-morph';
22
import { detectIndent } from '../../../../../utils';
33
import { ClassHelper } from './class-helper';
44
import { IParseDeepIdentifierResult, SymbolHelper } from './symbol-helper';
@@ -44,6 +44,77 @@ export class ComponentHelper {
4444
return this.symbolHelper.getSymbolDeps(props, 'exportAs', srcFile).pop();
4545
}
4646

47+
public getComponentHostDirectives(
48+
props: ReadonlyArray<ts.ObjectLiteralElementLike>
49+
): Array<any> {
50+
const hostDirectiveSymbolParsed = this.symbolHelper.getSymbolDepsRaw(
51+
props,
52+
'hostDirectives'
53+
);
54+
let hostDirectiveSymbol = null;
55+
56+
if (hostDirectiveSymbolParsed.length > 0) {
57+
hostDirectiveSymbol = hostDirectiveSymbolParsed.pop();
58+
}
59+
60+
const result = [];
61+
62+
if (
63+
hostDirectiveSymbol &&
64+
hostDirectiveSymbol.initializer &&
65+
hostDirectiveSymbol.initializer.elements &&
66+
hostDirectiveSymbol.initializer.elements.length > 0
67+
) {
68+
hostDirectiveSymbol.initializer.elements.forEach(element => {
69+
if (element.kind === SyntaxKind.Identifier) {
70+
result.push({
71+
name: element.escapedText
72+
});
73+
} else if (
74+
element.kind === SyntaxKind.ObjectLiteralExpression &&
75+
element.properties &&
76+
element.properties.length > 0
77+
) {
78+
const parsedDirective: any = {
79+
name: '',
80+
inputs: [],
81+
outputs: []
82+
};
83+
84+
element.properties.forEach(property => {
85+
if (property.name.escapedText === 'directive') {
86+
parsedDirective.name = property.initializer.escapedText;
87+
} else if (property.name.escapedText === 'inputs') {
88+
if (
89+
property.initializer &&
90+
property.initializer.elements &&
91+
property.initializer.elements.length > 0
92+
) {
93+
property.initializer.elements.forEach(propertyElement => {
94+
parsedDirective.inputs.push(propertyElement.text);
95+
});
96+
}
97+
} else if (property.name.escapedText === 'outputs') {
98+
if (
99+
property.initializer &&
100+
property.initializer.elements &&
101+
property.initializer.elements.length > 0
102+
) {
103+
property.initializer.elements.forEach(propertyElement => {
104+
parsedDirective.outputs.push(propertyElement.text);
105+
});
106+
}
107+
}
108+
});
109+
110+
result.push(parsedDirective);
111+
}
112+
});
113+
}
114+
115+
return result;
116+
}
117+
47118
public getComponentHost(
48119
props: ReadonlyArray<ts.ObjectLiteralElementLike>
49120
): Map<string, string> {

src/locales/bg-BG.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export const TRANSLATION_BG_BG = {
3838
guard: 'Guard',
3939
guards: 'Guards',
4040
hostbindings: 'HostBindings',
41+
hostdirectives: 'HostDirectives',
4142
hostlisteners: 'HostListeners',
4243
'html-element': 'HTML-елемент',
4344
'html-element-with-directive': 'HTML-елемент с директива',

src/locales/de-DE.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export const TRANSLATION_DE_DE = {
3838
guard: 'Guard',
3939
guards: 'Guards',
4040
hostbindings: 'HostBindings',
41+
hostdirectives: 'HostDirectives',
4142
hostlisteners: 'HostListeners',
4243
'html-element': 'Html Element',
4344
'html-element-with-directive': 'Html-Element mit Direktive',

src/locales/en-US.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export const TRANSLATION_EN_US = {
3838
guard: 'Guard',
3939
guards: 'Guards',
4040
hostbindings: 'HostBindings',
41+
hostdirectives: 'HostDirectives',
4142
hostlisteners: 'HostListeners',
4243
'html-element': 'Html element',
4344
'html-element-with-directive': 'Html element with directive',

src/locales/es-ES.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export const TRANSLATION_ES_ES = {
3838
guard: 'Guardia',
3939
guards: 'Guardias',
4040
hostbindings: 'Fijaciones de Host',
41+
hostdirectives: 'HostDirectives',
4142
hostlisteners: 'Escuchadores de Host',
4243
'html-element': 'Elemento Html',
4344
'html-element-with-directive': 'Elemento Html con directiva',

src/locales/fr-FR.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export const TRANSLATION_FR_FR = {
3838
guard: 'Garde',
3939
guards: 'Gardes',
4040
hostbindings: 'HostBindings',
41+
hostdirectives: 'HostDirectives',
4142
hostlisteners: 'HostListeners',
4243
'html-element': 'Elément Html',
4344
'html-element-with-directive': 'Elément Html avec une directive',

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy