Skip to content

Commit 9d0289d

Browse files
committed
Try to clarify code intent
1 parent bfb1644 commit 9d0289d

File tree

2 files changed

+7
-10
lines changed

2 files changed

+7
-10
lines changed

packages/babel-plugin-htm/index.mjs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,14 @@ export default function htmBabelPlugin({ types: t }, options = {}) {
108108
function propsNode(props) {
109109
return t.isNode(props) ? props : t.objectExpression(
110110
Object.keys(props).map(key => {
111-
const values = props[key];
111+
const values = props[key].map(propValueNode);
112112

113-
let node = propValueNode(values[0]);
114-
let isString = t.isStringLiteral(node);
113+
let node = values[0];
114+
if (values.length > 1 && !t.isStringLiteral(node) && !t.isStringLiteral(values[1])) {
115+
node = t.binaryExpression('+', t.stringLiteral(''), node);
116+
}
115117
values.slice(1).forEach(value => {
116-
const prop = propValueNode(value);
117-
if (!isString && !t.isStringLiteral(prop)) {
118-
node = t.binaryExpression('+', node, t.stringLiteral(''));
119-
isString = true;
120-
}
121-
node = t.binaryExpression('+', node, prop);
118+
node = t.binaryExpression('+', node, value);
122119
});
123120

124121
return t.objectProperty(propertyName(key), node);

test/babel.test.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ describe('htm/babel', () => {
134134
}]
135135
]
136136
}).code
137-
).toBe(`h("a",{b:1+""+2+"3"+4});`);
137+
).toBe(`h("a",{b:""+1+2+"3"+4});`);
138138
});
139139

140140
test('coerces props to strings only when needed', () => {

0 commit comments

Comments
 (0)