Skip to content

Commit

Permalink
preliminary support for bool: [needs help] (#347)
Browse files Browse the repository at this point in the history
* preliminary support for ``bool:`

* make code smaller

* value of attribute should be empty when adding it to the dom

* inline bool: attribute into the template when possible

* fix prop name

* undo space test
  • Loading branch information
titoBouzout authored and ryansolid committed Sep 9, 2024
1 parent f9a00f7 commit a21a043
Show file tree
Hide file tree
Showing 11 changed files with 404 additions and 49 deletions.
55 changes: 55 additions & 0 deletions packages/babel-plugin-jsx-dom-expressions/src/dom/element.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,13 @@ export function setAttr(path, elem, name, value, { isSVG, dynamic, prevId, isCE,
return t.assignmentExpression("=", t.memberExpression(elem, t.identifier("data")), value);
}

if(namespace === 'bool') {
return t.callExpression(
registerImportMethod(path, "setBoolAttribute", getRendererConfig(path, "dom").moduleName),
[elem, t.stringLiteral(name), value]
);
}

const isChildProp = ChildProperties.has(name);
const isProp = Properties.has(name);
const alias = getPropAlias(name, tagName.toUpperCase());
Expand Down Expand Up @@ -714,6 +721,54 @@ function transformAttributes(path, results) {
isCE,
tagName
});
} else if(key.slice(0, 5) === 'bool:'){

// inline it on the template when possible
let content = value;

if (t.isJSXExpressionContainer(content)) content = content.expression;

function addBoolAttribute() {
results.template += `${needsSpacing ? " " : ""}${key.slice(5)}`;
needsSpacing = true;
}

switch (content.type) {
case "StringLiteral": {
if (content.value.length && content.value !== "0") {
addBoolAttribute();
}
return;
}
case "NullLiteral": {
return;
}
case "BooleanLiteral": {
if (content.value) {
addBoolAttribute();
}
return;
}
case "Identifier": {
if (content.name === "undefined") {
return;
}
break;
}
}

// when not possible to inline it in the template
results.exprs.push(
t.expressionStatement(
setAttr(
attribute,
elem,
key,
t.isJSXExpressionContainer(value) ? value.expression : value,
{ isSVG, isCE, tagName },
),
),
);
} else {
results.exprs.push(
t.expressionStatement(
Expand Down
5 changes: 3 additions & 2 deletions packages/babel-plugin-jsx-dom-expressions/src/shared/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ export const reservedNameSpaces = new Set([
"style",
"use",
"prop",
"attr"
"attr",
"bool"
]);

export const nonSpreadNameSpaces = new Set(["class", "style", "use", "prop", "attr"]);
export const nonSpreadNameSpaces = new Set(["class", "style", "use", "prop", "attr", "bool"]);

export function getConfig(path) {
return path.hub.file.metadata.config;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,15 +203,42 @@ const template41 = (
</select>
);

const template42 = <img src="" />;
const template43 = <div><img src=""/></div>;

const template44 = <img src="" loading="lazy"/>;
const template45 = <div><img src="" loading="lazy"/></div>;

const template46 = <iframe src=""></iframe>;
const template47 = <div><iframe src=""></iframe></div>;

const template48 = <iframe src="" loading="lazy"></iframe>;
const template49 = <div><iframe src="" loading="lazy"></iframe></div>;

// bool:
function boolTest(){return true}
const boolTestBinding = false
const boolTestObjBinding = {value:false}

const template42 = <div bool:quack="">empty string</div>;
const template43 = <div bool:quack={""}>js empty</div>;
const template44 = <div bool:quack="hola">hola</div>;
const template45 = <div bool:quack={"hola js"}>"hola js"</div>;
const template46 = <div bool:quack={true}>true</div>;
const template47 = <div bool:quack={false}>false</div>;
const template48 = <div bool:quack={1}>1</div>;
const template49 = <div bool:quack={0}>0</div>;
const template50 = <div bool:quack={"1"}>"1"</div>;
const template51 = <div bool:quack={"0"}>"0"</div>;
const template52 = <div bool:quack={undefined}>undefined</div>;
const template53 = <div bool:quack={null}>null</div>;
const template54 = <div bool:quack={boolTest()}>boolTest()</div>;
const template55 = <div bool:quack={boolTest}>boolTest</div>;
const template56 = <div bool:quack={boolTestBinding}>boolTestBinding</div>;
const template57 = <div bool:quack={boolTestObjBinding.value}>boolTestObjBinding.value</div>;
const template58 = <div bool:quack={()=>false}>fn</div>;

const template59 = <div before bool:quack="true">should have space before</div>;
const template60 = <div before bool:quack="true" after>should have space before/after</div>;
const template61 = <div bool:quack="true" after>should have space before/after</div>;
// this crash it for some reason- */ const template62 = <div bool:quack>really empty</div>;

const template63 = <img src="" />;
const template64 = <div><img src=""/></div>;

const template65 = <img src="" loading="lazy"/>;
const template66 = <div><img src="" loading="lazy"/></div>;

const template67 = <iframe src=""></iframe>;
const template68 = <div><iframe src=""></iframe></div>;

const template69 = <iframe src="" loading="lazy"></iframe>;
const template70 = <div><iframe src="" loading="lazy"></iframe></div>;
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { template as _$template } from "r-dom";
import { delegateEvents as _$delegateEvents } from "r-dom";
import { setBoolAttribute as _$setBoolAttribute } from "r-dom";
import { insert as _$insert } from "r-dom";
import { memo as _$memo } from "r-dom";
import { addEventListener as _$addEventListener } from "r-dom";
Expand Down Expand Up @@ -33,14 +34,34 @@ var _tmpl$ = /*#__PURE__*/ _$template(`<div id=main><h1 class=base id=my-h1><a h
_tmpl$18 = /*#__PURE__*/ _$template(`<button>`),
_tmpl$19 = /*#__PURE__*/ _$template(`<input value=10>`),
_tmpl$20 = /*#__PURE__*/ _$template(`<select><option>Red</option><option>Blue`),
_tmpl$21 = /*#__PURE__*/ _$template(`<img src="">`),
_tmpl$22 = /*#__PURE__*/ _$template(`<div><img src="">`),
_tmpl$23 = /*#__PURE__*/ _$template(`<img src=""loading=lazy>`, true, false),
_tmpl$24 = /*#__PURE__*/ _$template(`<div><img src=""loading=lazy>`, true, false),
_tmpl$25 = /*#__PURE__*/ _$template(`<iframe src="">`),
_tmpl$26 = /*#__PURE__*/ _$template(`<div><iframe src="">`),
_tmpl$27 = /*#__PURE__*/ _$template(`<iframe src=""loading=lazy>`, true, false),
_tmpl$28 = /*#__PURE__*/ _$template(`<div><iframe src=""loading=lazy>`, true, false);
_tmpl$21 = /*#__PURE__*/ _$template(`<div>empty string`),
_tmpl$22 = /*#__PURE__*/ _$template(`<div>js empty`),
_tmpl$23 = /*#__PURE__*/ _$template(`<div quack>hola`),
_tmpl$24 = /*#__PURE__*/ _$template(`<div quack>"hola js"`),
_tmpl$25 = /*#__PURE__*/ _$template(`<div quack>true`),
_tmpl$26 = /*#__PURE__*/ _$template(`<div>false`),
_tmpl$27 = /*#__PURE__*/ _$template(`<div quack>1`),
_tmpl$28 = /*#__PURE__*/ _$template(`<div>0`),
_tmpl$29 = /*#__PURE__*/ _$template(`<div quack>"1"`),
_tmpl$30 = /*#__PURE__*/ _$template(`<div>"0"`),
_tmpl$31 = /*#__PURE__*/ _$template(`<div>undefined`),
_tmpl$32 = /*#__PURE__*/ _$template(`<div>null`),
_tmpl$33 = /*#__PURE__*/ _$template(`<div>boolTest()`),
_tmpl$34 = /*#__PURE__*/ _$template(`<div>boolTest`),
_tmpl$35 = /*#__PURE__*/ _$template(`<div>boolTestBinding`),
_tmpl$36 = /*#__PURE__*/ _$template(`<div>boolTestObjBinding.value`),
_tmpl$37 = /*#__PURE__*/ _$template(`<div>fn`),
_tmpl$38 = /*#__PURE__*/ _$template(`<div before quack>should have space before`),
_tmpl$39 = /*#__PURE__*/ _$template(`<div before quack after>should have space before/after`),
_tmpl$40 = /*#__PURE__*/ _$template(`<div quack after>should have space before/after`),
_tmpl$41 = /*#__PURE__*/ _$template(`<img src="">`),
_tmpl$42 = /*#__PURE__*/ _$template(`<div><img src="">`),
_tmpl$43 = /*#__PURE__*/ _$template(`<img src=""loading=lazy>`, true, false),
_tmpl$44 = /*#__PURE__*/ _$template(`<div><img src=""loading=lazy>`, true, false),
_tmpl$45 = /*#__PURE__*/ _$template(`<iframe src="">`),
_tmpl$46 = /*#__PURE__*/ _$template(`<div><iframe src="">`),
_tmpl$47 = /*#__PURE__*/ _$template(`<iframe src=""loading=lazy>`, true, false),
_tmpl$48 = /*#__PURE__*/ _$template(`<div><iframe src=""loading=lazy>`, true, false);
import * as styles from "./styles.module.css";
const selected = true;
let id = "my-h1";
Expand Down Expand Up @@ -434,6 +455,15 @@ const template41 = (() => {
_$effect(() => (_el$57.value = state.color));
return _el$57;
})();

// bool:
function boolTest() {
return true;
}
const boolTestBinding = false;
const boolTestObjBinding = {
value: false
};
const template42 = _tmpl$21();
const template43 = _tmpl$22();
const template44 = _tmpl$23();
Expand All @@ -442,4 +472,46 @@ const template46 = _tmpl$25();
const template47 = _tmpl$26();
const template48 = _tmpl$27();
const template49 = _tmpl$28();
const template50 = _tmpl$29();
const template51 = _tmpl$30();
const template52 = _tmpl$31();
const template53 = _tmpl$32();
const template54 = (() => {
var _el$72 = _tmpl$33();
_$effect(() => _$setBoolAttribute(_el$72, "quack", boolTest()));
return _el$72;
})();
const template55 = (() => {
var _el$73 = _tmpl$34();
_$setBoolAttribute(_el$73, "quack", boolTest);
return _el$73;
})();
const template56 = (() => {
var _el$74 = _tmpl$35();
_$setBoolAttribute(_el$74, "quack", boolTestBinding);
return _el$74;
})();
const template57 = (() => {
var _el$75 = _tmpl$36();
_$effect(() => _$setBoolAttribute(_el$75, "quack", boolTestObjBinding.value));
return _el$75;
})();
const template58 = (() => {
var _el$76 = _tmpl$37();
_$setBoolAttribute(_el$76, "quack", () => false);
return _el$76;
})();
const template59 = _tmpl$38();
const template60 = _tmpl$39();
const template61 = _tmpl$40();
// this crash it for some reason- */ const template62 = <div bool:quack>really empty</div>;

const template63 = _tmpl$41();
const template64 = _tmpl$42();
const template65 = _tmpl$43();
const template66 = _tmpl$44();
const template67 = _tmpl$45();
const template68 = _tmpl$46();
const template69 = _tmpl$47();
const template70 = _tmpl$48();
_$delegateEvents(["click", "input"]);
Original file line number Diff line number Diff line change
@@ -1,26 +1,54 @@
const template = (
<my-element some-attr={name} notProp={data} attr:my-attr={data} prop:someProp={data} />
);

const template2 = (
<my-element
some-attr={state.name}
notProp={state.data}
attr:my-attr={state.data}
prop:someProp={state.data}
/>
);

const template3 = (
<my-element>
<header slot="head">Title</header>
</my-element>
);

const template4 = (
<>
<slot name="head"></slot>
</>
);

const template5 = <a is="my-element" />;
const template = (
<my-element some-attr={name} notProp={data} attr:my-attr={data} prop:someProp={data} />
);

const template2 = (
<my-element
some-attr={state.name}
notProp={state.data}
attr:my-attr={state.data}
prop:someProp={state.data}
/>
);

const template3 = (
<my-element>
<header slot="head">Title</header>
</my-element>
);

const template4 = (
<>
<slot name="head"></slot>
</>
);

const template5 = <a is="my-element" />;

// bool:
function boolTest(){return true}
const boolTestBinding = false
const boolTestObjBinding = {value:false}

const template42 = <my-el bool:quack="">empty string</my-el>;
const template43 = <my-el bool:quack={""}>js empty</my-el>;
const template44 = <my-el bool:quack="hola">hola</my-el>;
const template45 = <my-el bool:quack={"hola js"}>"hola js"</my-el>;
const template46 = <my-el bool:quack={true}>true</my-el>;
const template47 = <my-el bool:quack={false}>false</my-el>;
const template48 = <my-el bool:quack={1}>1</my-el>;
const template49 = <my-el bool:quack={0}>0</my-el>;
const template50 = <my-el bool:quack={"1"}>"1"</my-el>;
const template51 = <my-el bool:quack={"0"}>"0"</my-el>;
const template52 = <my-el bool:quack={undefined}>undefined</my-el>;
const template53 = <my-el bool:quack={null}>null</my-el>;
const template54 = <my-el bool:quack={boolTest()}>boolTest()</my-el>;
const template55 = <my-el bool:quack={boolTest}>boolTest</my-el>;
const template56 = <my-el bool:quack={boolTestBinding}>boolTestBinding</my-el>;
const template57 = <my-el bool:quack={boolTestObjBinding.value}>boolTestObjBinding.value</my-el>;
const template58 = <my-el bool:quack={()=>false}>fn</my-el>;

const template59 = <my-el before bool:quack="true">should have space before</my-el>;
const template60 = <my-el before bool:quack="true" after>should have space before/after</my-el>;
const template61 = <my-el bool:quack="true" after>should have space before/after</my-el>;
// this crash it for some reason- */ const template62 = <div bool:quack>really empty</div>;
Loading

0 comments on commit a21a043

Please sign in to comment.