Skip to content

Commit 252a3d4

Browse files
committed
Extract findStringArrayProperties() function
1 parent 07d2752 commit 252a3d4

2 files changed

Lines changed: 21 additions & 55 deletions

File tree

lib/__tests__/find-properties.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ describe('findClassNameBindings()', () => {
128128
['classNameBindings: 5', /Unexpected `classNameBindings` value: 5/],
129129
['classNameBindings: foo', /Unexpected `classNameBindings` value: foo/],
130130
['classNameBindings: [42]', /Unexpected `classNameBindings` value: \[42]/],
131+
["classNameBindings: ['a:b:c:d']", /Unexpected `classNameBindings` value: a:b:c:d/],
131132
];
132133

133134
for (let [input, expected] of TESTS) {

lib/transform.js

Lines changed: 20 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -161,79 +161,44 @@ function findElementId(properties) {
161161
return findStringProperty(properties, 'elementId');
162162
}
163163

164-
function findAttributeBindings(properties) {
165-
let attrBindingsProperty = properties.filter(path => isProperty(path, 'attributeBindings'))[0];
166-
if (!attrBindingsProperty) {
167-
return new Map();
164+
function findStringArrayProperties(properties, name) {
165+
let propertyPath = properties.filter(path => isProperty(path, name))[0];
166+
if (!propertyPath) {
167+
return [];
168168
}
169169

170-
let arrayPath = attrBindingsProperty.get('value');
170+
let arrayPath = propertyPath.get('value');
171171
if (arrayPath.value.type !== 'ArrayExpression') {
172-
throw new SilentError(`Unexpected \`attributeBindings\` value: ${j(arrayPath).toSource()}`);
172+
throw new SilentError(`Unexpected \`${name}\` value: ${j(arrayPath).toSource()}`);
173173
}
174174

175-
let elements = arrayPath.get('elements').value;
176-
177-
let attrBindings = new Map();
178-
for (let element of elements) {
175+
return arrayPath.get('elements').value.map(element => {
179176
if (element.type !== 'StringLiteral') {
180-
throw new SilentError(`Unexpected \`attributeBindings\` value: ${j(arrayPath).toSource()}`);
177+
throw new SilentError(`Unexpected \`${name}\` value: ${j(arrayPath).toSource()}`);
181178
}
182179

183-
let [from, to] = element.value.split(':');
180+
return element.value;
181+
});
182+
}
183+
184+
function findAttributeBindings(properties) {
185+
let attrBindings = new Map();
186+
for (let binding of findStringArrayProperties(properties, 'attributeBindings')) {
187+
let [from, to] = binding.split(':');
184188
attrBindings.set(from, to || from);
185189
}
186190

187191
return attrBindings;
188192
}
189193

190194
function findClassNames(properties) {
191-
let classNamesProperty = properties.filter(path => isProperty(path, 'classNames'))[0];
192-
if (!classNamesProperty) {
193-
return [];
194-
}
195-
196-
let arrayPath = classNamesProperty.get('value');
197-
if (arrayPath.value.type !== 'ArrayExpression') {
198-
throw new SilentError(`Unexpected \`classNames\` value: ${j(arrayPath).toSource()}`);
199-
}
200-
201-
let elements = arrayPath.get('elements').value;
202-
203-
let classNames = [];
204-
for (let element of elements) {
205-
if (element.type !== 'StringLiteral') {
206-
throw new SilentError(`Unexpected \`classNames\` value: ${j(arrayPath).toSource()}`);
207-
}
208-
209-
classNames.push(element.value);
210-
}
211-
212-
return classNames;
195+
return findStringArrayProperties(properties, 'classNames');
213196
}
214197

215198
function findClassNameBindings(properties) {
216-
let classNameBindingsProperty = properties.filter(path =>
217-
isProperty(path, 'classNameBindings')
218-
)[0];
219-
if (!classNameBindingsProperty) {
220-
return new Map();
221-
}
222-
223-
let arrayPath = classNameBindingsProperty.get('value');
224-
if (arrayPath.value.type !== 'ArrayExpression') {
225-
throw new SilentError(`Unexpected \`classNameBindings\` value: ${j(arrayPath).toSource()}`);
226-
}
227-
228-
let elements = arrayPath.get('elements').value;
229-
230199
let classNameBindings = new Map();
231-
for (let element of elements) {
232-
if (element.type !== 'StringLiteral') {
233-
throw new SilentError(`Unexpected \`classNameBindings\` value: ${j(arrayPath).toSource()}`);
234-
}
235-
236-
let parts = element.value.split(':');
200+
for (let binding of findStringArrayProperties(properties, 'classNameBindings')) {
201+
let parts = binding.split(':');
237202

238203
if (parts.length === 1) {
239204
classNameBindings.set(parts[0], [stringUtils.dasherize(parts[0]), null]);
@@ -242,7 +207,7 @@ function findClassNameBindings(properties) {
242207
} else if (parts.length === 3) {
243208
classNameBindings.set(parts[0], [parts[1] || null, parts[2]]);
244209
} else {
245-
throw new SilentError(`Unexpected \`classNameBindings\` value: ${j(arrayPath).toSource()}`);
210+
throw new SilentError(`Unexpected \`classNameBindings\` value: ${binding}`);
246211
}
247212
}
248213

0 commit comments

Comments
 (0)