Skip to content

Commit 616d44c

Browse files
committed
build: account for object literal syntax when extracting inputs
Fixes that the build was throwing an error when using the object literal syntax for the `inputs` array.
1 parent f260aa0 commit 616d44c

File tree

2 files changed

+41
-29
lines changed

2 files changed

+41
-29
lines changed

tools/dgeni/common/directive-metadata.ts

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,4 @@
1-
import {
2-
ArrayLiteralExpression,
3-
CallExpression,
4-
isCallExpression,
5-
NodeArray,
6-
ObjectLiteralExpression,
7-
PropertyAssignment,
8-
StringLiteral,
9-
SyntaxKind,
10-
getDecorators,
11-
isClassDeclaration,
12-
} from 'typescript';
1+
import ts from 'typescript';
132
import {CategorizedClassDoc} from './dgeni-definitions';
143

154
/**
@@ -30,15 +19,15 @@ import {CategorizedClassDoc} from './dgeni-definitions';
3019
export function getDirectiveMetadata(classDoc: CategorizedClassDoc): Map<string, any> | null {
3120
const declaration = classDoc.symbol.valueDeclaration;
3221
const decorators =
33-
declaration && isClassDeclaration(declaration) ? getDecorators(declaration) : null;
22+
declaration && ts.isClassDeclaration(declaration) ? ts.getDecorators(declaration) : null;
3423

3524
if (!decorators?.length) {
3625
return null;
3726
}
3827

3928
const expression = decorators
40-
.filter(decorator => decorator.expression && isCallExpression(decorator.expression))
41-
.map(decorator => decorator.expression as CallExpression)
29+
.filter(decorator => decorator.expression && ts.isCallExpression(decorator.expression))
30+
.map(decorator => decorator.expression as ts.CallExpression)
4231
.find(
4332
callExpression =>
4433
callExpression.expression.getText() === 'Component' ||
@@ -55,25 +44,41 @@ export function getDirectiveMetadata(classDoc: CategorizedClassDoc): Map<string,
5544
return null;
5645
}
5746

58-
const objectExpression = expression.arguments[0] as ObjectLiteralExpression;
47+
const objectExpression = expression.arguments[0] as ts.ObjectLiteralExpression;
5948
const resultMetadata = new Map<string, any>();
6049

61-
(objectExpression.properties as NodeArray<PropertyAssignment>).forEach(prop => {
50+
(objectExpression.properties as ts.NodeArray<ts.PropertyAssignment>).forEach(prop => {
6251
// Support ArrayLiteralExpression assignments in the directive metadata.
63-
if (prop.initializer.kind === SyntaxKind.ArrayLiteralExpression) {
64-
const arrayData = (prop.initializer as ArrayLiteralExpression).elements.map(
65-
literal => (literal as StringLiteral).text,
66-
);
52+
if (ts.isArrayLiteralExpression(prop.initializer)) {
53+
const arrayData = prop.initializer.elements.map(literal => {
54+
if (ts.isStringLiteralLike(literal)) {
55+
return literal.text;
56+
}
57+
58+
if (ts.isObjectLiteralExpression(literal)) {
59+
return literal.properties.reduce(
60+
(result, prop) => {
61+
if (ts.isPropertyAssignment(prop)) {
62+
result[prop.name.getText()] = ts.isStringLiteralLike(prop.initializer)
63+
? prop.initializer.text
64+
: prop.initializer.getText();
65+
}
66+
67+
return result;
68+
},
69+
{} as Record<string, string>,
70+
);
71+
}
72+
73+
return literal.getText();
74+
});
6775

6876
resultMetadata.set(prop.name.getText(), arrayData);
6977
}
7078

7179
// Support normal StringLiteral and NoSubstitutionTemplateLiteral assignments
72-
if (
73-
prop.initializer.kind === SyntaxKind.StringLiteral ||
74-
prop.initializer.kind === SyntaxKind.NoSubstitutionTemplateLiteral
75-
) {
76-
resultMetadata.set(prop.name.getText(), (prop.initializer as StringLiteral).text);
80+
if (ts.isStringLiteralLike(prop.initializer)) {
81+
resultMetadata.set(prop.name.getText(), prop.initializer.text);
7782
}
7883
});
7984

tools/dgeni/common/property-bindings.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,20 @@ function getBindingPropertyData(
4040
decoratorName: string,
4141
) {
4242
if (metadata) {
43-
const metadataValues: string[] = metadata.get(propertyName) || [];
44-
const foundValue = metadataValues.find(value => value.split(':')[0] === doc.name);
43+
const metadataValues: (string | {name: string; alias?: string})[] =
44+
metadata.get(propertyName) || [];
45+
const foundValue = metadataValues.find(value => {
46+
const name = typeof value === 'string' ? value.split(':')[0] : value.name;
47+
return name === doc.name;
48+
});
4549

4650
if (foundValue) {
4751
return {
4852
name: doc.name,
49-
alias: foundValue.split(':')[1],
53+
alias:
54+
typeof foundValue === 'string'
55+
? foundValue.split(':')[1]
56+
: foundValue.alias || foundValue.name,
5057
};
5158
}
5259
}

0 commit comments

Comments
 (0)