From 849a980189e3c5ab85c5200d47ce5e9cab0be6c1 Mon Sep 17 00:00:00 2001 From: Tito Bouzout Date: Thu, 28 Nov 2024 10:25:13 -0300 Subject: [PATCH] handle literal boolean in attributes, `` becomes ``, `` becomes `` --- .../src/dom/element.js | 20 +- .../attributeExpressions/code.js | 7 +- .../attributeExpressions/output.js | 207 ++++++++++-------- .../attributeExpressions/output.js | 75 +++---- .../attributeExpressions/output.js | 8 +- 5 files changed, 173 insertions(+), 144 deletions(-) diff --git a/packages/babel-plugin-jsx-dom-expressions/src/dom/element.js b/packages/babel-plugin-jsx-dom-expressions/src/dom/element.js index 4528e8b4..01a0a066 100644 --- a/packages/babel-plugin-jsx-dom-expressions/src/dom/element.js +++ b/packages/babel-plugin-jsx-dom-expressions/src/dom/element.js @@ -487,7 +487,12 @@ function transformAttributes(path, results) { if ( t.isJSXExpressionContainer(value) && (reservedNameSpace || - !(t.isStringLiteral(value.expression) || t.isNumericLiteral(value.expression))) + !( + t.isStringLiteral(value.expression) || + t.isNumericLiteral(value.expression) || + // remove `!isCE` when custom elements start defaulting to attributes + (t.isBooleanLiteral(value.expression) && !isCE) + )) ) { if (key === "ref") { // Normalize expressions for non-null and type-as @@ -804,6 +809,19 @@ function transformAttributes(path, results) { return; } if (t.isJSXExpressionContainer(value)) value = value.expression; + + // boolean as ``, not as `` + // `` becomes `` + // `` becomes `` + if (t.isBooleanLiteral(value)) { + if (value.value === true) { + results.template += `${needsSpacing ? " " : ""}${key}`; + needsSpacing = true; + } + return; + } + + // properties key = Aliases[key] || key; if (value && ChildProperties.has(key)) { results.exprs.push( diff --git a/packages/babel-plugin-jsx-dom-expressions/test/__dom_fixtures__/attributeExpressions/code.js b/packages/babel-plugin-jsx-dom-expressions/test/__dom_fixtures__/attributeExpressions/code.js index 0a46668f..3b841442 100644 --- a/packages/babel-plugin-jsx-dom-expressions/test/__dom_fixtures__/attributeExpressions/code.js +++ b/packages/babel-plugin-jsx-dom-expressions/test/__dom_fixtures__/attributeExpressions/code.js @@ -254,4 +254,9 @@ const template73 =
; const template74 =
const template75 =
-const template76 =
\ No newline at end of file +const template76 =
+ +const template77 =
+const template78 =
+const template79 =
+const template80 =
\ No newline at end of file diff --git a/packages/babel-plugin-jsx-dom-expressions/test/__dom_fixtures__/attributeExpressions/output.js b/packages/babel-plugin-jsx-dom-expressions/test/__dom_fixtures__/attributeExpressions/output.js index ea25c8cb..71902448 100644 --- a/packages/babel-plugin-jsx-dom-expressions/test/__dom_fixtures__/attributeExpressions/output.js +++ b/packages/babel-plugin-jsx-dom-expressions/test/__dom_fixtures__/attributeExpressions/output.js @@ -17,52 +17,55 @@ var _tmpl$ = /*#__PURE__*/ _$template(`

`), _tmpl$4 = /*#__PURE__*/ _$template(`
`), _tmpl$5 = /*#__PURE__*/ _$template(`
`), - _tmpl$6 = /*#__PURE__*/ _$template(``), - _tmpl$7 = /*#__PURE__*/ _$template(`
\`$\``), - _tmpl$8 = /*#__PURE__*/ _$template(`