Skip to content

Commit 086e267

Browse files
committed
Validate annotations being send to getAnnotationValue, omit empty field attributes
1 parent 5f55915 commit 086e267

1 file changed

Lines changed: 39 additions & 27 deletions

File tree

src/bscPlugin/fileProviders/ComponentStatementProvider.ts

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,24 @@ export class ComponentStatementProvider {
5656

5757
//declare interface field
5858
} else if (isFieldStatement(member) && member.accessModifier?.text.toLowerCase() === 'public') {
59-
return `<field
60-
id="${member.name.text}"
61-
type="${member.typeExpression.getName()}"
62-
alias="${this.getAnnotationValue(member.annotations?.filter(x => x.name.toLowerCase() === 'alias'))}"
63-
onChange="${this.getAnnotationValue(member.annotations?.filter(x => x.name.toLowerCase() === 'onchange'))}"
64-
alwaysNotify="${this.getAnnotationValue(member.annotations?.filter(x => x.name.toLowerCase() === 'alwaysnotify')) === 'true'}"
65-
/>`;
59+
let fieldAttributes = {
60+
id: member.name.text,
61+
type: member.typeExpression.getName(),
62+
alias: this.getAnnotationValue(member.annotations?.filter(x => x.name.toLowerCase() === 'alias')),
63+
onChange: this.getAnnotationValue(member.annotations?.filter(x => x.name.toLowerCase() === 'onchange')),
64+
alwaysNotify: this.getAnnotationValue(member.annotations?.filter(x => x.name.toLowerCase() === 'alwaysnotify')) === 'true' ? 'true' : ''
65+
};
66+
67+
let field = '<field';
68+
Object.keys(fieldAttributes).forEach(attribute => {
69+
let value = fieldAttributes[attribute];
70+
// Only add a attribute if the value is not empty.
71+
if (value !== '') {
72+
field += ` ${attribute}="${fieldAttributes[attribute]}"`;
73+
}
74+
});
75+
field += ' />';
76+
return field;
6677
} else {
6778
return '';
6879
}
@@ -71,7 +82,8 @@ export class ComponentStatementProvider {
7182
let componentChildren = '';
7283
const template = statement.annotations?.find(x => x.name.toLowerCase() === 'template');
7384
if (template) {
74-
componentChildren = `<children>${this.getAnnotationValue([template])}</children>`;
85+
// TODO: Better strip of component and children elements.
86+
componentChildren = `<children>${this.getAnnotationValue([template]).replaceAll('<component>', '').replaceAll('</component>', '').replaceAll('<children>', '').replaceAll('</children>', '')}</children>`;
7587
}
7688

7789
xmlFile.parse(undent`
@@ -90,27 +102,27 @@ export class ComponentStatementProvider {
90102

91103
private getAnnotationValue(annotations: any) {
92104
let response = [];
93-
annotations.forEach(a => {
94-
let args = a?.call?.args[0];
95-
if (isVariableExpression(args) || isDottedGetExpression(args)) {
96-
response.push(args.name.text);
97-
} else if (isLiteralExpression(args)) {
98-
let values = args?.token?.text.replaceAll('\"', '').replaceAll(' ', '').split(',');
99-
response = response.concat(values);
100-
} else if (isTemplateStringExpression(args)) {
101-
let textOutput = '';
102-
args.quasis[0]?.expressions?.forEach((a: { token: { text: string } }) => {
103-
if (!a.token.text.includes('component>') && !a.token.text.includes('children>')) {
105+
if (annotations !== undefined) {
106+
annotations.forEach(a => {
107+
let args = a?.call?.args[0];
108+
if (isVariableExpression(args) || isDottedGetExpression(args)) {
109+
response.push(args.name.text);
110+
} else if (isLiteralExpression(args)) {
111+
let values = args?.token?.text.replaceAll('\"', '').replaceAll(' ', '').split(',');
112+
response = response.concat(values);
113+
} else if (isTemplateStringExpression(args)) {
114+
let textOutput = '';
115+
args.quasis[0]?.expressions?.forEach((a: { token: { text: string } }) => {
104116
textOutput += a.token.text;
105-
}
106-
});
107-
response.push(textOutput);
108-
}
109-
});
117+
});
118+
response.push(textOutput);
119+
}
120+
});
110121

111-
response = response.filter((item, index) => {
112-
return response.indexOf(item) === index;
113-
});
122+
response = response.filter((item, index) => {
123+
return response.indexOf(item) === index;
124+
});
125+
}
114126
return response.join(', ');
115127
}
116128

0 commit comments

Comments
 (0)