@knighted/jsx 1.13.1 → 1.13.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/react/react-jsx.cjs +18 -1
- package/dist/cjs/runtime/shared.cjs +7 -2
- package/dist/cjs/runtime/shared.d.cts +4 -1
- package/dist/lite/debug/index.js +7 -7
- package/dist/lite/index.js +7 -7
- package/dist/lite/node/debug/index.js +7 -7
- package/dist/lite/node/index.js +8 -8
- package/dist/lite/node/react/index.js +7 -7
- package/dist/lite/react/index.js +7 -7
- package/dist/react/react-jsx.js +18 -1
- package/dist/runtime/shared.d.ts +4 -1
- package/dist/runtime/shared.js +7 -2
- package/package.json +1 -1
|
@@ -102,6 +102,21 @@ const evaluateReactJsxChildren = (children, ctx) => {
|
|
|
102
102
|
const createReactElement = (type, props, children) => {
|
|
103
103
|
return (0, react_1.createElement)(type, props, ...children);
|
|
104
104
|
};
|
|
105
|
+
const reactMemoSymbol = Symbol.for('react.memo');
|
|
106
|
+
const reactForwardRefSymbol = Symbol.for('react.forward_ref');
|
|
107
|
+
const reactLazySymbol = Symbol.for('react.lazy');
|
|
108
|
+
const isReactTagBindingValue = (value) => {
|
|
109
|
+
if (typeof value === 'function') {
|
|
110
|
+
return true;
|
|
111
|
+
}
|
|
112
|
+
if (typeof value !== 'object' || value === null) {
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
const candidate = value;
|
|
116
|
+
return (candidate.$$typeof === reactMemoSymbol ||
|
|
117
|
+
candidate.$$typeof === reactForwardRefSymbol ||
|
|
118
|
+
candidate.$$typeof === reactLazySymbol);
|
|
119
|
+
};
|
|
105
120
|
const evaluateReactJsxElement = (element, ctx) => {
|
|
106
121
|
const opening = element.openingElement;
|
|
107
122
|
const tagName = (0, shared_js_1.getIdentifierName)(opening.name);
|
|
@@ -124,7 +139,9 @@ const evaluateReactJsxNode = (node, ctx) => {
|
|
|
124
139
|
return evaluateReactJsxElement(node, ctx);
|
|
125
140
|
};
|
|
126
141
|
const reactJsx = (templates, ...values) => {
|
|
127
|
-
const build = (0, shared_js_1.buildTemplate)(templates, values
|
|
142
|
+
const build = (0, shared_js_1.buildTemplate)(templates, values, {
|
|
143
|
+
isTagNameBindingValue: isReactTagBindingValue,
|
|
144
|
+
});
|
|
128
145
|
const result = (0, oxc_parser_1.parseSync)('inline.jsx', build.source, shared_js_1.parserOptions);
|
|
129
146
|
if (result.errors.length > 0) {
|
|
130
147
|
throw new Error((0, shared_js_1.formatTaggedTemplateParserError)('reactJsx', templates, build.diagnostics, result.errors[0]));
|
|
@@ -170,7 +170,7 @@ const ensureBinding = (value, bindings, bindingLookup) => {
|
|
|
170
170
|
return binding;
|
|
171
171
|
};
|
|
172
172
|
exports.ensureBinding = ensureBinding;
|
|
173
|
-
const buildTemplate = (strings, values) => {
|
|
173
|
+
const buildTemplate = (strings, values, options) => {
|
|
174
174
|
const raw = strings.raw ?? strings;
|
|
175
175
|
const placeholders = new Map();
|
|
176
176
|
const bindings = [];
|
|
@@ -179,19 +179,24 @@ const buildTemplate = (strings, values) => {
|
|
|
179
179
|
const templateId = invocationCounter++;
|
|
180
180
|
let placeholderIndex = 0;
|
|
181
181
|
const expressionRanges = [];
|
|
182
|
+
const isTagNameBindingValue = options?.isTagNameBindingValue ??
|
|
183
|
+
((value) => typeof value === 'function');
|
|
182
184
|
for (let idx = 0; idx < values.length; idx++) {
|
|
183
185
|
const chunk = raw[idx] ?? '';
|
|
184
186
|
const nextChunk = raw[idx + 1] ?? '';
|
|
185
187
|
const value = values[idx];
|
|
186
188
|
const isTagNamePosition = OPEN_TAG_RE.test(chunk) || CLOSE_TAG_RE.test(chunk);
|
|
187
189
|
let insertion;
|
|
188
|
-
if (isTagNamePosition &&
|
|
190
|
+
if (isTagNamePosition && isTagNameBindingValue(value)) {
|
|
189
191
|
const binding = (0, exports.ensureBinding)(value, bindings, bindingLookup);
|
|
190
192
|
insertion = binding.name;
|
|
191
193
|
}
|
|
192
194
|
else if (isTagNamePosition && typeof value === 'string') {
|
|
193
195
|
insertion = value;
|
|
194
196
|
}
|
|
197
|
+
else if (isTagNamePosition) {
|
|
198
|
+
throw new Error('Invalid tag interpolation value. Expected a component, class, or string tag name.');
|
|
199
|
+
}
|
|
195
200
|
else {
|
|
196
201
|
const placeholder = `${exports.PLACEHOLDER_PREFIX}${templateId}_${placeholderIndex++}__`;
|
|
197
202
|
placeholders.set(placeholder, value);
|
|
@@ -22,6 +22,9 @@ export type TemplateBuildResult<TComponent extends TemplateComponent> = {
|
|
|
22
22
|
bindings: BindingEntry<TComponent>[];
|
|
23
23
|
diagnostics: TemplateDiagnostics;
|
|
24
24
|
};
|
|
25
|
+
export type TemplateBuildOptions<TComponent extends TemplateComponent> = {
|
|
26
|
+
isTagNameBindingValue?: (value: unknown) => value is TComponent;
|
|
27
|
+
};
|
|
25
28
|
export type TemplateContext<TComponent extends TemplateComponent> = {
|
|
26
29
|
source: string;
|
|
27
30
|
placeholders: Map<string, unknown>;
|
|
@@ -40,4 +43,4 @@ export declare const collectPlaceholderNames: <TComponent extends TemplateCompon
|
|
|
40
43
|
export declare const evaluateExpression: <TComponent extends TemplateComponent>(expression: Expression | JSXElement | JSXFragment, ctx: TemplateContext<TComponent>, evaluateJsxNode: (node: JSXElement | JSXFragment) => unknown) => unknown;
|
|
41
44
|
export declare const sanitizeIdentifier: (value: string) => string;
|
|
42
45
|
export declare const ensureBinding: <TComponent extends TemplateComponent>(value: TComponent, bindings: BindingEntry<TComponent>[], bindingLookup: Map<TComponent, BindingEntry<TComponent>>) => BindingEntry<TComponent>;
|
|
43
|
-
export declare const buildTemplate: <TComponent extends TemplateComponent>(strings: TemplateStringsArray, values: unknown[]) => TemplateBuildResult<TComponent>;
|
|
46
|
+
export declare const buildTemplate: <TComponent extends TemplateComponent>(strings: TemplateStringsArray, values: unknown[], options?: TemplateBuildOptions<TComponent>) => TemplateBuildResult<TComponent>;
|
package/dist/lite/debug/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {parseSync}from'oxc-parser';import {find,svg,html}from'property-information';var
|
|
2
|
-
`),a=
|
|
3
|
-
`)},
|
|
1
|
+
import {parseSync}from'oxc-parser';import {find,svg,html}from'property-information';var C=null,L=e=>{C=e;},ue=e=>{C?.warnLowercaseEventProp?.(e);},de=e=>{C?.ensureValidDangerouslySetInnerHTML?.(e);},I=e=>{let{getIdentifierName:t,evaluateExpressionWithNamespace:n}=e;return (r,s,o)=>{let a={},c=(i,p)=>{i==="dangerouslySetInnerHTML"&&de(p),a[i]=p;};return r.forEach(i=>{if(i.type==="JSXSpreadAttribute"){let l=n(i.argument,s,o);l&&typeof l=="object"&&!Array.isArray(l)&&Object.assign(a,l);return}let p=t(i.name);if(ue(p),!i.value){c(p,true);return}if(i.value.type==="Literal"){c(p,i.value.value);return}if(i.value.type==="JSXExpressionContainer"){if(i.value.expression.type==="JSXEmptyExpression")return;c(p,n(i.value.expression,s,o));}}),a}};var B=null,W=e=>{B=e;},_="Capture",H=e=>e.endsWith(_)?{eventName:e.slice(0,-_.length),capture:true}:{eventName:e,capture:false},V=e=>{if(!e.startsWith("on"))return null;if(e.startsWith("on:")){let r=e.slice(3);if(!r)return null;let s=H(r);return s.eventName?s:null}let t=e.slice(2);if(!t)return null;let n=H(t);return n.eventName?{eventName:n.eventName.toLowerCase(),capture:n.capture}:null},U=e=>!e||typeof e!="object"?false:"handleEvent"in e&&typeof e.handleEvent=="function",ge=e=>{if(!e||typeof e!="object"||!("handler"in e))return false;let t=e.handler;return typeof t=="function"?true:U(t)},fe=(e,t)=>{B?.onInvalidHandler?.(e,t);},z=(e,t)=>{if(typeof t=="function"||U(t))return {listener:t};if(!ge(t))return fe(e,t),null;let n=t,r=n.options?{...n.options}:void 0,s=(o,a)=>{a!=null&&(r||(r={}),r[o]=a);};return s("capture",n.capture),s("once",n.once),s("passive",n.passive),s("signal",n.signal??void 0),{listener:n.handler,options:r}};var xe="@knighted/jsx",K=e=>`${xe}: ${e}`,v=()=>typeof process<"u"&&process.env?.KNIGHTED_JSX_DEBUG==="1",k=e=>{if(e===null)return "null";if(e===void 0)return "undefined";if(typeof e=="function")return e.name?`function ${e.name}`:"function";if(Array.isArray(e))return "array";if(typeof e=="object"){let t=e.constructor;return t&&typeof t.name=="string"&&t.name&&t.name!=="Object"?`${t.name} instance`:"object"}return typeof e},b=e=>new Error(K(e)),G=(e,t=false)=>{!t&&!v()||typeof console<"u"&&typeof console.warn=="function"&&console.warn(K(e));};var Ee=e=>e>="a"&&e<="z",S="env",A=()=>S==="always"||S==="env"&&v(),ye=()=>S==="always",he={warnLowercaseEventProp(e){if(!A()||!e.startsWith("on")||e.startsWith("on:")||e.length<3)return;let t=e[2]??"";if(!Ee(t))return;let n=`${e.slice(0,2)}${t.toUpperCase()}${e.slice(3)}`;G(`Use camelCase DOM event props when targeting runtime jsx templates. Received "${e}"; did you mean "${n}"?`,ye());},ensureValidDangerouslySetInnerHTML(e){if(!A())return;if(!e||typeof e!="object"||Array.isArray(e))throw b("dangerouslySetInnerHTML expects an object with a string __html field.");let t=e.__html;if(typeof t!="string")throw b(`dangerouslySetInnerHTML.__html must be a string but received ${k(t)}.`)}},be={onInvalidHandler(e,t){if(A())throw b(`The "${e}" prop expects a function, EventListenerObject, or descriptor ({ handler }) but received ${k(t)}.`)}},Z=e=>{S=e?.mode,L(he),W(be);};var q=e=>{let t=e.replace(/\r/g,"").replace(/\n\s+/g," "),n=e.match(/^\s*/)?.[0]??"",r=e.match(/\s*$/)?.[0]??"",s=/\n/.test(n),o=/\n/.test(r),a=t;return s&&(a=a.replace(/^\s+/,"")),o&&(a=a.replace(/\s+$/,"")),a.length===0||a.trim().length===0?null:a};var Se="oxc-parser",Je=e=>{let t=e.raw??e,n=t[0]??"",r=[];for(let s=0;s<t.length-1;s++){let o="${expr#"+s+"}",a=n.length;n+=o;let c=n.length;r.push({index:s,templateStart:a,templateEnd:c,label:o}),n+=t[s+1]??"";}return {source:n,spans:r}},Te=(e,t)=>{let n=new Map;return t.forEach(r=>{n.set(r.index,r);}),e.expressionRanges.map(r=>{let s=n.get(r.index);if(!s)return null;let o=Math.max(0,r.sourceEnd-r.sourceStart),a=Math.max(0,s.templateEnd-s.templateStart);return {sourceStart:r.sourceStart,sourceEnd:r.sourceEnd,templateStart:s.templateStart,templateEnd:s.templateEnd,delta:a-o}}).filter(r=>!!r).sort((r,s)=>r.sourceStart-s.sourceStart)},we=(e,t,n)=>{if(!Number.isFinite(e)||e<=0)return 0;let r=0;for(let o of t){if(e<o.sourceStart)break;if(e<o.sourceEnd){let a=Math.max(0,e-o.sourceStart),c=Math.max(0,o.templateEnd-o.templateStart);if(c===0)return o.templateStart;let i=Math.min(a,Math.max(0,c-1));return o.templateStart+i}r+=o.delta;}let s=e+r;return s<=0?0:s>=n?n:s},Q=(e,t)=>{let n=Math.max(0,Math.min(t,e.length)),r=1,s=1;for(let o=0;o<n;o++){if(e.charCodeAt(o)===10){r++,s=1;continue}s++;}return {line:r,column:s}},Ce=e=>{let t=[],n=0;return e.forEach((r,s)=>{t.push(n),n+=r.length,s<e.length-1&&(n+=1);}),t},ve=(e,t,n,r,s,o,a)=>{let c=n+t.length,i=Math.max(r,n),p=Math.min(s,c);if(p>i){let l=Math.max(0,i-n),d=Math.max(1,p-i);return " ".repeat(l)+"^".repeat(d)}if(t.length===0&&e>=o&&e<=a)return "^";if(e===o){let l=Math.max(0,r-n);return " ".repeat(Math.min(l,t.length))+"^"}return ""},ke=(e,t,n,r,s)=>{if(!e.length)return "";let o=e.split(`
|
|
2
|
+
`),a=Ce(o),c=Math.max(1,r-1),i=Math.min(o.length,s+1),p=String(i).length,l=[];for(let d=c;d<=i;d++){let m=o[d-1]??"",u=String(d).padStart(p," ");l.push(`${u} | ${m}`);let g=ve(d,m,a[d-1]??0,t,n,r,s);g&&l.push(`${" ".repeat(p)} | ${g}`);}return l.join(`
|
|
3
|
+
`)},R=(e,t,n,r,s)=>{let o=Se,a=`[${o}] ${r.message}`,c=r.labels?.[0];if(!c)return a;let{source:i,spans:p}=Je(t),l=Te(n,p),d=h=>we(typeof h=="number"?h:0,l,i.length),m=d(c.start),u=d(c.end);u<=m&&(u=Math.min(i.length,m+1));let g=Q(i,m),x=Q(i,Math.max(m,u-1)),E=ke(i,m,u,g.line,x.line),f=`[${o}] ${r.message}`;return f+=`
|
|
4
4
|
--> ${e} template:${g.line}:${g.column}`,c.message&&(f+=`
|
|
5
|
-
${c.message}`),
|
|
6
|
-
${
|
|
7
|
-
${r.helpMessage}`),f};var
|
|
8
|
-
export{
|
|
5
|
+
${c.message}`),E&&(f+=`
|
|
6
|
+
${E}`),r.helpMessage&&(f+=`
|
|
7
|
+
${r.helpMessage}`),f};var Ae=/<\s*$/,Re=/<\/\s*$/,ee="__KX_EXPR__",Y=new RegExp(`${ee}\\d+_\\d+__`,"g"),De=0;var te={lang:"jsx",sourceType:"module",range:true,preserveParens:true},ne=e=>{for(let t of e.body)if(t.type==="ExpressionStatement"){let n=t.expression;if(n.type==="JSXElement"||n.type==="JSXFragment")return n}throw new Error("The jsx template must contain a single JSX element or fragment.")},J=e=>{switch(e.type){case "JSXIdentifier":return e.name;case "JSXNamespacedName":return `${e.namespace.name}:${e.name.name}`;case "JSXMemberExpression":return `${J(e.object)}.${e.property.name}`;default:return ""}},D=(e,t)=>{if(!e||typeof e!="object")return;let n=e;typeof n.type=="string"&&(t(n),Object.values(n).forEach(r=>{if(r){if(Array.isArray(r)){r.forEach(s=>D(s,t));return}typeof r=="object"&&D(r,t);}}));},re=(e,t)=>{let n=q(e);if(!n)return [];let r=[];Y.lastIndex=0;let s=0,o;for(;o=Y.exec(n);){let c=o.index,i=n.slice(s,c);i&&r.push(i);let p=o[0];t.has(p)?r.push(t.get(p)):r.push(p),s=c+p.length;}let a=n.slice(s);return a&&r.push(a),r},Ne=(e,t)=>{let n=new Set;return D(e,r=>{r.type==="Identifier"&&t.placeholders.has(r.name)&&n.add(r.name);}),Array.from(n)},oe=(e,t,n)=>{if(e.type==="JSXElement"||e.type==="JSXFragment")return n(e);if(!("range"in e)||!e.range)throw new Error("Unable to evaluate expression: missing source range information.");let[r,s]=e.range,o=t.source.slice(r,s),a=Ne(e,t);try{let c=new Function(...a,`"use strict"; return (${o});`),i=a.map(p=>t.placeholders.get(p));return c(...i)}catch(c){throw new Error(`Failed to evaluate expression ${o}: ${c.message}`,{cause:c})}},Pe=e=>{let t=e.replace(/[^a-zA-Z0-9_$]/g,"");return t?/[A-Za-z_$]/.test(t[0])?t:`Component${t}`:"Component"},je=(e,t,n)=>{let r=n.get(e);if(r)return r;let s=e.displayName||e.name||`Component${t.length}`,o=Pe(s??""),a=o,c=1;for(;t.some(p=>p.name===a);)a=`${o}${c++}`;let i={name:a,value:e};return t.push(i),n.set(e,i),i},se=(e,t,n)=>{let r=e.raw??e,s=new Map,o=[],a=new Map,c=r[0]??"",i=De++,p=0,l=[],d=(m=>typeof m=="function");for(let m=0;m<t.length;m++){let u=r[m]??"",g=r[m+1]??"",x=t[m],E=Ae.test(u)||Re.test(u),f;if(E&&d(x))f=je(x,o,a).name;else if(E&&typeof x=="string")f=x;else {if(E)throw new Error("Invalid tag interpolation value. Expected a component, class, or string tag name.");{let w=`${ee}${i}_${p++}__`;s.set(w,x),f=w;}}let h=c.length;c+=f;let F=c.length;l.push({index:m,sourceStart:h,sourceEnd:F}),c+=g;}return {source:c,placeholders:s,bindings:o,diagnostics:{expressionRanges:l}}};var N=Symbol.for("@knighted/jsx::Fragment"),T="__jsxNs",Xe=(e,t)=>t.length>0?t:Object.prototype.hasOwnProperty.call(e,"children")?[e.children]:[],Me=(e,t)=>{let n={...e};return t.length===1?n.children=t[0]:t.length>1?n.children=t:delete n.children,n},$e=e=>{if(!Object.prototype.hasOwnProperty.call(e,T))return;let t=e[T];if(delete e[T],t==="svg"||t===null)return t;throw new Error(`${T} must be "svg" or null when provided.`)},ae=({ensureDomAvailable:e,appendChildValue:t,setDomProp:n,isPromiseLike:r})=>{function s(o,a,...c){e();let i=a?{...a}:{},p=Xe(i,c);if(o===N){let u=document.createDocumentFragment();return p.forEach(g=>t(u,g)),u}if(typeof o=="function"){let u=o(Me(i,p));if(r(u))throw new Error("Async jsx components are not supported.");return u}if(typeof o!="string")throw new Error(`Unsupported jsx createElement type: ${String(o)}`);delete i.children;let d=$e(i)??(o==="svg"?"svg":null),m=d==="svg"?document.createElementNS("http://www.w3.org/2000/svg",o):document.createElement(o);return Object.entries(i).forEach(([u,g])=>{u!=="key"&&n(m,u,g,d);}),p.forEach(u=>t(m,u)),m}return s};var pe=()=>{if(typeof document>"u"||typeof document.createElement!="function")throw new Error("The jsx template tag requires a DOM-like environment (document missing).")},_e=e=>typeof Node>"u"?false:e instanceof Node||e instanceof DocumentFragment,He=e=>!e||typeof e=="string"?false:typeof e[Symbol.iterator]=="function",M=e=>!e||typeof e!="object"&&typeof e!="function"?false:typeof e.then=="function",Be={xlink:"http://www.w3.org/1999/xlink",xml:"http://www.w3.org/XML/1998/namespace",xmlns:"http://www.w3.org/2000/xmlns/"},ce=e=>{if(!(!e||e==="html"||e==="svg"))return Be[e]},We=e=>e==="svg"?svg:html,P=(e,t,n)=>{let r=ce(t.space),s=String(n);if(r){e.setAttributeNS(r,t.attribute,s);return}e.setAttribute(t.attribute,s);},ie=(e,t)=>{let n=ce(t.space);if(n){e.removeAttributeNS(n,t.attribute);return}e.removeAttribute(t.attribute);},j=(e,t)=>Array.isArray(e)?e.filter(Boolean).join(t):e,Ve=(e,t,n)=>t==="svg"||!n.property||n.property.includes(":")?false:n.property in e,le=(e,t,n,r)=>{if(n==null)return;if(t==="dangerouslySetInnerHTML"&&typeof n=="object"&&n&&"__html"in n){e.innerHTML=String(n.__html??"");return}if(t==="ref"){if(typeof n=="function"){n(e);return}if(n&&typeof n=="object"){n.current=e;return}}if(t==="style"&&typeof n=="object"&&n!==null){let p=n,l=e.style;if(!l)return;let d=l;Object.entries(p).forEach(([m,u])=>{if(u!=null){if(m.startsWith("--")){l.setProperty(m,String(u));return}d[m]=u;}});return}let s=V(t);if(s){let p=z(t,n);if(p){let l=p.options?{...p.options}:void 0;s.capture&&(l?l.capture=true:l={capture:true}),e.addEventListener(s.eventName,p.listener,l);return}}let o=find(We(r),t),a=e,c=Ve(e,r,o);if(o.mustUseProperty){let p=o.boolean?!!n:n;a[o.property]=p;return}if(o.boolean){let p=!!n;c&&(a[o.property]=p),p?P(e,o,""):ie(e,o);return}let i=n;if(o.spaceSeparated?i=j(n," "):o.commaSeparated?i=j(n,","):o.commaOrSpaceSeparated&&(i=j(n," ")),o.booleanish&&typeof i=="boolean"&&(i=i?"true":"false"),o.overloadedBoolean){if(i===false){ie(e,o);return}if(i===true){P(e,o,"");return}}if(c){a[o.property]=i;return}i!==false&&P(e,o,i);},y=(e,t)=>{if(t!=null&&typeof t!="boolean"){if(M(t))throw new Error("Async values are not supported inside jsx template results.");if(Array.isArray(t)){t.forEach(n=>y(e,n));return}if(He(t)){for(let n of t)y(e,n);return}if(_e(t)){e.appendChild(t);return}e.appendChild(document.createTextNode(String(t)));}},X=(e,t,n)=>oe(e,t,r=>O(r,t,n)),me=I({getIdentifierName:J,evaluateExpressionWithNamespace:X}),Ue=(e,t,n,r)=>{let s=me(t,n,r);Object.entries(s).forEach(([o,a])=>{if(o!=="key"){if(o==="children"){y(e,a);return}le(e,o,a,r);}});},$=(e,t,n)=>{let r=[];return e.forEach(s=>{switch(s.type){case "JSXText":{re(s.value,t.placeholders).forEach(a=>{r.push(a);});break}case "JSXExpressionContainer":{if(s.expression.type==="JSXEmptyExpression")break;r.push(X(s.expression,t,n));break}case "JSXSpreadChild":{let o=X(s.expression,t,n);o!=null&&r.push(o);break}case "JSXElement":case "JSXFragment":{r.push(O(s,t,n));break}}}),r},ze=(e,t,n,r)=>{let s=me(e.openingElement.attributes,t,r),o=$(e.children,t,r);o.length===1?s.children=o[0]:o.length>1&&(s.children=o);let a=n(s);if(M(a))throw new Error("Async jsx components are not supported.");return a},Ke=(e,t,n)=>{let r=e.openingElement,s=J(r.name),o=t.components.get(s);if(o)return ze(e,t,o,n);if(/[A-Z]/.test(s[0]??""))throw new Error(`Unknown component "${s}". Did you interpolate it with the template literal?`);let a=s==="svg"?"svg":n,c=s==="foreignObject"?null:a,i=a==="svg"?document.createElementNS("http://www.w3.org/2000/svg",s):document.createElement(s);return Ue(i,r.attributes,t,a),$(e.children,t,c).forEach(l=>y(i,l)),i},O=(e,t,n)=>{if(e.type==="JSXFragment"){let r=document.createDocumentFragment();return $(e.children,t,n).forEach(o=>y(r,o)),r}return Ke(e,t,n)},Ge=ae({ensureDomAvailable:pe,appendChildValue:y,setDomProp:le,isPromiseLike:M}),Ze=(e,...t)=>{pe();let n=se(e,t),r=parseSync("inline.jsx",n.source,te);if(r.errors.length>0)throw new Error(R("jsx",e,n.diagnostics,r.errors[0]));let s=ne(r.program),o={source:n.source,placeholders:n.placeholders,components:new Map(n.bindings.map(a=>[a.name,a.value]))};return O(s,o,null)},qe=Object.assign(Ze,{createElement:Ge,Fragment:N});Z({mode:"always"});
|
|
8
|
+
export{qe as jsx};
|
package/dist/lite/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {parseSync}from'oxc-parser';import {find,svg,html}from'property-information';var
|
|
2
|
-
`),a=
|
|
3
|
-
`)},
|
|
1
|
+
import {parseSync}from'oxc-parser';import {find,svg,html}from'property-information';var j=e=>{let t=e.replace(/\r/g,"").replace(/\n\s+/g," "),n=e.match(/^\s*/)?.[0]??"",r=e.match(/\s*$/)?.[0]??"",s=/\n/.test(n),o=/\n/.test(r),a=t;return s&&(a=a.replace(/^\s+/,"")),o&&(a=a.replace(/\s+$/,"")),a.length===0||a.trim().length===0?null:a};var te="oxc-parser",ne=e=>{let t=e.raw??e,n=t[0]??"",r=[];for(let s=0;s<t.length-1;s++){let o="${expr#"+s+"}",a=n.length;n+=o;let l=n.length;r.push({index:s,templateStart:a,templateEnd:l,label:o}),n+=t[s+1]??"";}return {source:n,spans:r}},re=(e,t)=>{let n=new Map;return t.forEach(r=>{n.set(r.index,r);}),e.expressionRanges.map(r=>{let s=n.get(r.index);if(!s)return null;let o=Math.max(0,r.sourceEnd-r.sourceStart),a=Math.max(0,s.templateEnd-s.templateStart);return {sourceStart:r.sourceStart,sourceEnd:r.sourceEnd,templateStart:s.templateStart,templateEnd:s.templateEnd,delta:a-o}}).filter(r=>!!r).sort((r,s)=>r.sourceStart-s.sourceStart)},oe=(e,t,n)=>{if(!Number.isFinite(e)||e<=0)return 0;let r=0;for(let o of t){if(e<o.sourceStart)break;if(e<o.sourceEnd){let a=Math.max(0,e-o.sourceStart),l=Math.max(0,o.templateEnd-o.templateStart);if(l===0)return o.templateStart;let i=Math.min(a,Math.max(0,l-1));return o.templateStart+i}r+=o.delta;}let s=e+r;return s<=0?0:s>=n?n:s},D=(e,t)=>{let n=Math.max(0,Math.min(t,e.length)),r=1,s=1;for(let o=0;o<n;o++){if(e.charCodeAt(o)===10){r++,s=1;continue}s++;}return {line:r,column:s}},se=e=>{let t=[],n=0;return e.forEach((r,s)=>{t.push(n),n+=r.length,s<e.length-1&&(n+=1);}),t},ae=(e,t,n,r,s,o,a)=>{let l=n+t.length,i=Math.max(r,n),p=Math.min(s,l);if(p>i){let c=Math.max(0,i-n),d=Math.max(1,p-i);return " ".repeat(c)+"^".repeat(d)}if(t.length===0&&e>=o&&e<=a)return "^";if(e===o){let c=Math.max(0,r-n);return " ".repeat(Math.min(c,t.length))+"^"}return ""},ie=(e,t,n,r,s)=>{if(!e.length)return "";let o=e.split(`
|
|
2
|
+
`),a=se(o),l=Math.max(1,r-1),i=Math.min(o.length,s+1),p=String(i).length,c=[];for(let d=l;d<=i;d++){let m=o[d-1]??"",u=String(d).padStart(p," ");c.push(`${u} | ${m}`);let g=ae(d,m,a[d-1]??0,t,n,r,s);g&&c.push(`${" ".repeat(p)} | ${g}`);}return c.join(`
|
|
3
|
+
`)},T=(e,t,n,r,s)=>{let o=te,a=`[${o}] ${r.message}`,l=r.labels?.[0];if(!l)return a;let{source:i,spans:p}=ne(t),c=re(n,p),d=h=>oe(typeof h=="number"?h:0,c,i.length),m=d(l.start),u=d(l.end);u<=m&&(u=Math.min(i.length,m+1));let g=D(i,m),x=D(i,Math.max(m,u-1)),E=ie(i,m,u,g.line,x.line),f=`[${o}] ${r.message}`;return f+=`
|
|
4
4
|
--> ${e} template:${g.line}:${g.column}`,l.message&&(f+=`
|
|
5
|
-
${l.message}`),
|
|
6
|
-
${
|
|
7
|
-
${r.helpMessage}`),f};var
|
|
8
|
-
export{
|
|
5
|
+
${l.message}`),E&&(f+=`
|
|
6
|
+
${E}`),r.helpMessage&&(f+=`
|
|
7
|
+
${r.helpMessage}`),f};var pe=/<\s*$/,le=/<\/\s*$/,O="__KX_EXPR__",F=new RegExp(`${O}\\d+_\\d+__`,"g"),ce=0;var M={lang:"jsx",sourceType:"module",range:true,preserveParens:true},L=e=>{for(let t of e.body)if(t.type==="ExpressionStatement"){let n=t.expression;if(n.type==="JSXElement"||n.type==="JSXFragment")return n}throw new Error("The jsx template must contain a single JSX element or fragment.")},b=e=>{switch(e.type){case "JSXIdentifier":return e.name;case "JSXNamespacedName":return `${e.namespace.name}:${e.name.name}`;case "JSXMemberExpression":return `${b(e.object)}.${e.property.name}`;default:return ""}},w=(e,t)=>{if(!e||typeof e!="object")return;let n=e;typeof n.type=="string"&&(t(n),Object.values(n).forEach(r=>{if(r){if(Array.isArray(r)){r.forEach(s=>w(s,t));return}typeof r=="object"&&w(r,t);}}));},I=(e,t)=>{let n=j(e);if(!n)return [];let r=[];F.lastIndex=0;let s=0,o;for(;o=F.exec(n);){let l=o.index,i=n.slice(s,l);i&&r.push(i);let p=o[0];t.has(p)?r.push(t.get(p)):r.push(p),s=l+p.length;}let a=n.slice(s);return a&&r.push(a),r},me=(e,t)=>{let n=new Set;return w(e,r=>{r.type==="Identifier"&&t.placeholders.has(r.name)&&n.add(r.name);}),Array.from(n)},$=(e,t,n)=>{if(e.type==="JSXElement"||e.type==="JSXFragment")return n(e);if(!("range"in e)||!e.range)throw new Error("Unable to evaluate expression: missing source range information.");let[r,s]=e.range,o=t.source.slice(r,s),a=me(e,t);try{let l=new Function(...a,`"use strict"; return (${o});`),i=a.map(p=>t.placeholders.get(p));return l(...i)}catch(l){throw new Error(`Failed to evaluate expression ${o}: ${l.message}`,{cause:l})}},ue=e=>{let t=e.replace(/[^a-zA-Z0-9_$]/g,"");return t?/[A-Za-z_$]/.test(t[0])?t:`Component${t}`:"Component"},de=(e,t,n)=>{let r=n.get(e);if(r)return r;let s=e.displayName||e.name||`Component${t.length}`,o=ue(s??""),a=o,l=1;for(;t.some(p=>p.name===a);)a=`${o}${l++}`;let i={name:a,value:e};return t.push(i),n.set(e,i),i},_=(e,t,n)=>{let r=e.raw??e,s=new Map,o=[],a=new Map,l=r[0]??"",i=ce++,p=0,c=[],d=(m=>typeof m=="function");for(let m=0;m<t.length;m++){let u=r[m]??"",g=r[m+1]??"",x=t[m],E=pe.test(u)||le.test(u),f;if(E&&d(x))f=de(x,o,a).name;else if(E&&typeof x=="string")f=x;else {if(E)throw new Error("Invalid tag interpolation value. Expected a component, class, or string tag name.");{let J=`${O}${i}_${p++}__`;s.set(J,x),f=J;}}let h=l.length;l+=f;let X=l.length;c.push({index:m,sourceStart:h,sourceEnd:X}),l+=g;}return {source:l,placeholders:s,bindings:o,diagnostics:{expressionRanges:c}}};var B=e=>{let{getIdentifierName:t,evaluateExpressionWithNamespace:n}=e;return (r,s,o)=>{let a={},l=(i,p)=>{a[i]=p;};return r.forEach(i=>{if(i.type==="JSXSpreadAttribute"){let c=n(i.argument,s,o);c&&typeof c=="object"&&!Array.isArray(c)&&Object.assign(a,c);return}let p=t(i.name);if(!i.value){l(p,true);return}if(i.value.type==="Literal"){l(p,i.value.value);return}if(i.value.type==="JSXExpressionContainer"){if(i.value.expression.type==="JSXEmptyExpression")return;l(p,n(i.value.expression,s,o));}}),a}};var W="Capture",V=e=>e.endsWith(W)?{eventName:e.slice(0,-W.length),capture:true}:{eventName:e,capture:false},z=e=>{if(!e.startsWith("on"))return null;if(e.startsWith("on:")){let r=e.slice(3);if(!r)return null;let s=V(r);return s.eventName?s:null}let t=e.slice(2);if(!t)return null;let n=V(t);return n.eventName?{eventName:n.eventName.toLowerCase(),capture:n.capture}:null},U=e=>!e||typeof e!="object"?false:"handleEvent"in e&&typeof e.handleEvent=="function",Ee=e=>{if(!e||typeof e!="object"||!("handler"in e))return false;let t=e.handler;return typeof t=="function"?true:U(t)},K=(e,t)=>{if(typeof t=="function"||U(t))return {listener:t};if(!Ee(t))return null;let n=t,r=n.options?{...n.options}:void 0,s=(o,a)=>{a!=null&&(r||(r={}),r[o]=a);};return s("capture",n.capture),s("once",n.once),s("passive",n.passive),s("signal",n.signal??void 0),{listener:n.handler,options:r}};var C=Symbol.for("@knighted/jsx::Fragment"),S="__jsxNs",he=(e,t)=>t.length>0?t:Object.prototype.hasOwnProperty.call(e,"children")?[e.children]:[],be=(e,t)=>{let n={...e};return t.length===1?n.children=t[0]:t.length>1?n.children=t:delete n.children,n},Se=e=>{if(!Object.prototype.hasOwnProperty.call(e,S))return;let t=e[S];if(delete e[S],t==="svg"||t===null)return t;throw new Error(`${S} must be "svg" or null when provided.`)},Z=({ensureDomAvailable:e,appendChildValue:t,setDomProp:n,isPromiseLike:r})=>{function s(o,a,...l){e();let i=a?{...a}:{},p=he(i,l);if(o===C){let u=document.createDocumentFragment();return p.forEach(g=>t(u,g)),u}if(typeof o=="function"){let u=o(be(i,p));if(r(u))throw new Error("Async jsx components are not supported.");return u}if(typeof o!="string")throw new Error(`Unsupported jsx createElement type: ${String(o)}`);delete i.children;let d=Se(i)??(o==="svg"?"svg":null),m=d==="svg"?document.createElementNS("http://www.w3.org/2000/svg",o):document.createElement(o);return Object.entries(i).forEach(([u,g])=>{u!=="key"&&n(m,u,g,d);}),p.forEach(u=>t(m,u)),m}return s};var G=()=>{if(typeof document>"u"||typeof document.createElement!="function")throw new Error("The jsx template tag requires a DOM-like environment (document missing).")},ke=e=>typeof Node>"u"?false:e instanceof Node||e instanceof DocumentFragment,ve=e=>!e||typeof e=="string"?false:typeof e[Symbol.iterator]=="function",A=e=>!e||typeof e!="object"&&typeof e!="function"?false:typeof e.then=="function",Re={xlink:"http://www.w3.org/1999/xlink",xml:"http://www.w3.org/XML/1998/namespace",xmlns:"http://www.w3.org/2000/xmlns/"},Q=e=>{if(!(!e||e==="html"||e==="svg"))return Re[e]},Ae=e=>e==="svg"?svg:html,k=(e,t,n)=>{let r=Q(t.space),s=String(n);if(r){e.setAttributeNS(r,t.attribute,s);return}e.setAttribute(t.attribute,s);},q=(e,t)=>{let n=Q(t.space);if(n){e.removeAttributeNS(n,t.attribute);return}e.removeAttribute(t.attribute);},v=(e,t)=>Array.isArray(e)?e.filter(Boolean).join(t):e,Ne=(e,t,n)=>t==="svg"||!n.property||n.property.includes(":")?false:n.property in e,Y=(e,t,n,r)=>{if(n==null)return;if(t==="dangerouslySetInnerHTML"&&typeof n=="object"&&n&&"__html"in n){e.innerHTML=String(n.__html??"");return}if(t==="ref"){if(typeof n=="function"){n(e);return}if(n&&typeof n=="object"){n.current=e;return}}if(t==="style"&&typeof n=="object"&&n!==null){let p=n,c=e.style;if(!c)return;let d=c;Object.entries(p).forEach(([m,u])=>{if(u!=null){if(m.startsWith("--")){c.setProperty(m,String(u));return}d[m]=u;}});return}let s=z(t);if(s){let p=K(t,n);if(p){let c=p.options?{...p.options}:void 0;s.capture&&(c?c.capture=true:c={capture:true}),e.addEventListener(s.eventName,p.listener,c);return}}let o=find(Ae(r),t),a=e,l=Ne(e,r,o);if(o.mustUseProperty){let p=o.boolean?!!n:n;a[o.property]=p;return}if(o.boolean){let p=!!n;l&&(a[o.property]=p),p?k(e,o,""):q(e,o);return}let i=n;if(o.spaceSeparated?i=v(n," "):o.commaSeparated?i=v(n,","):o.commaOrSpaceSeparated&&(i=v(n," ")),o.booleanish&&typeof i=="boolean"&&(i=i?"true":"false"),o.overloadedBoolean){if(i===false){q(e,o);return}if(i===true){k(e,o,"");return}}if(l){a[o.property]=i;return}i!==false&&k(e,o,i);},y=(e,t)=>{if(t!=null&&typeof t!="boolean"){if(A(t))throw new Error("Async values are not supported inside jsx template results.");if(Array.isArray(t)){t.forEach(n=>y(e,n));return}if(ve(t)){for(let n of t)y(e,n);return}if(ke(t)){e.appendChild(t);return}e.appendChild(document.createTextNode(String(t)));}},R=(e,t,n)=>$(e,t,r=>P(r,t,n)),ee=B({getIdentifierName:b,evaluateExpressionWithNamespace:R}),Pe=(e,t,n,r)=>{let s=ee(t,n,r);Object.entries(s).forEach(([o,a])=>{if(o!=="key"){if(o==="children"){y(e,a);return}Y(e,o,a,r);}});},N=(e,t,n)=>{let r=[];return e.forEach(s=>{switch(s.type){case "JSXText":{I(s.value,t.placeholders).forEach(a=>{r.push(a);});break}case "JSXExpressionContainer":{if(s.expression.type==="JSXEmptyExpression")break;r.push(R(s.expression,t,n));break}case "JSXSpreadChild":{let o=R(s.expression,t,n);o!=null&&r.push(o);break}case "JSXElement":case "JSXFragment":{r.push(P(s,t,n));break}}}),r},Xe=(e,t,n,r)=>{let s=ee(e.openingElement.attributes,t,r),o=N(e.children,t,r);o.length===1?s.children=o[0]:o.length>1&&(s.children=o);let a=n(s);if(A(a))throw new Error("Async jsx components are not supported.");return a},je=(e,t,n)=>{let r=e.openingElement,s=b(r.name),o=t.components.get(s);if(o)return Xe(e,t,o,n);if(/[A-Z]/.test(s[0]??""))throw new Error(`Unknown component "${s}". Did you interpolate it with the template literal?`);let a=s==="svg"?"svg":n,l=s==="foreignObject"?null:a,i=a==="svg"?document.createElementNS("http://www.w3.org/2000/svg",s):document.createElement(s);return Pe(i,r.attributes,t,a),N(e.children,t,l).forEach(c=>y(i,c)),i},P=(e,t,n)=>{if(e.type==="JSXFragment"){let r=document.createDocumentFragment();return N(e.children,t,n).forEach(o=>y(r,o)),r}return je(e,t,n)},De=Z({ensureDomAvailable:G,appendChildValue:y,setDomProp:Y,isPromiseLike:A}),Fe=(e,...t)=>{G();let n=_(e,t),r=parseSync("inline.jsx",n.source,M);if(r.errors.length>0)throw new Error(T("jsx",e,n.diagnostics,r.errors[0]));let s=L(r.program),o={source:n.source,placeholders:n.placeholders,components:new Map(n.bindings.map(a=>[a.name,a.value]))};return P(s,o,null)},Oe=Object.assign(Fe,{createElement:De,Fragment:C});
|
|
8
|
+
export{Oe as jsx};
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {createRequire}from'module';import {parseSync}from'oxc-parser';import {find,svg,html}from'property-information';var
|
|
2
|
-
`),a=
|
|
3
|
-
`)},A=(e,t,n,r,s)=>{let o=
|
|
1
|
+
import {createRequire}from'module';import {parseSync}from'oxc-parser';import {find,svg,html}from'property-information';var C=null,F=e=>{C=e;},ye=e=>{C?.warnLowercaseEventProp?.(e);},be=e=>{C?.ensureValidDangerouslySetInnerHTML?.(e);},H=e=>{let{getIdentifierName:t,evaluateExpressionWithNamespace:n}=e;return (r,s,o)=>{let a={},c=(i,p)=>{i==="dangerouslySetInnerHTML"&&be(p),a[i]=p;};return r.forEach(i=>{if(i.type==="JSXSpreadAttribute"){let l=n(i.argument,s,o);l&&typeof l=="object"&&!Array.isArray(l)&&Object.assign(a,l);return}let p=t(i.name);if(ye(p),!i.value){c(p,true);return}if(i.value.type==="Literal"){c(p,i.value.value);return}if(i.value.type==="JSXExpressionContainer"){if(i.value.expression.type==="JSXEmptyExpression")return;c(p,n(i.value.expression,s,o));}}),a}};var V=null,U=e=>{V=e;},W="Capture",B=e=>e.endsWith(W)?{eventName:e.slice(0,-W.length),capture:true}:{eventName:e,capture:false},z=e=>{if(!e.startsWith("on"))return null;if(e.startsWith("on:")){let r=e.slice(3);if(!r)return null;let s=B(r);return s.eventName?s:null}let t=e.slice(2);if(!t)return null;let n=B(t);return n.eventName?{eventName:n.eventName.toLowerCase(),capture:n.capture}:null},q=e=>!e||typeof e!="object"?false:"handleEvent"in e&&typeof e.handleEvent=="function",Se=e=>{if(!e||typeof e!="object"||!("handler"in e))return false;let t=e.handler;return typeof t=="function"?true:q(t)},we=(e,t)=>{V?.onInvalidHandler?.(e,t);},G=(e,t)=>{if(typeof t=="function"||q(t))return {listener:t};if(!Se(t))return we(e,t),null;let n=t,r=n.options?{...n.options}:void 0,s=(o,a)=>{a!=null&&(r||(r={}),r[o]=a);};return s("capture",n.capture),s("once",n.once),s("passive",n.passive),s("signal",n.signal??void 0),{listener:n.handler,options:r}};var Je="@knighted/jsx",K=e=>`${Je}: ${e}`,k=()=>typeof process<"u"&&process.env?.KNIGHTED_JSX_DEBUG==="1",v=e=>{if(e===null)return "null";if(e===void 0)return "undefined";if(typeof e=="function")return e.name?`function ${e.name}`:"function";if(Array.isArray(e))return "array";if(typeof e=="object"){let t=e.constructor;return t&&typeof t.name=="string"&&t.name&&t.name!=="Object"?`${t.name} instance`:"object"}return typeof e},b=e=>new Error(K(e)),Z=(e,t=false)=>{!t&&!k()||typeof console<"u"&&typeof console.warn=="function"&&console.warn(K(e));};var Te=e=>e>="a"&&e<="z",S="env",D=()=>S==="always"||S==="env"&&k(),Ce=()=>S==="always",ke={warnLowercaseEventProp(e){if(!D()||!e.startsWith("on")||e.startsWith("on:")||e.length<3)return;let t=e[2]??"";if(!Te(t))return;let n=`${e.slice(0,2)}${t.toUpperCase()}${e.slice(3)}`;Z(`Use camelCase DOM event props when targeting runtime jsx templates. Received "${e}"; did you mean "${n}"?`,Ce());},ensureValidDangerouslySetInnerHTML(e){if(!D())return;if(!e||typeof e!="object"||Array.isArray(e))throw b("dangerouslySetInnerHTML expects an object with a string __html field.");let t=e.__html;if(typeof t!="string")throw b(`dangerouslySetInnerHTML.__html must be a string but received ${v(t)}.`)}},ve={onInvalidHandler(e,t){if(D())throw b(`The "${e}" prop expects a function, EventListenerObject, or descriptor ({ handler }) but received ${v(t)}.`)}},Y=e=>{S=e?.mode,F(ke),U(ve);};var Re=createRequire(import.meta.url),ee=()=>Re,te="<!doctype html><html><body></body></html>",Ae=["window","self","document","HTMLElement","Element","Node","DocumentFragment","customElements","Text","Comment","MutationObserver","navigator"],Pe=()=>typeof document<"u"&&typeof document.createElement=="function",je=e=>{let t=globalThis,n=e;Ae.forEach(r=>{t[r]===void 0&&n[r]!==void 0&&(t[r]=n[r]);});},R=()=>{let{parseHTML:e}=ee()("linkedom"),{window:t}=e(te);return t},N=()=>{let{JSDOM:e}=ee()("jsdom"),{window:t}=new e(te);return t},Me=()=>{let e=typeof process<"u"&&process.env?.KNIGHTED_JSX_NODE_SHIM?process.env.KNIGHTED_JSX_NODE_SHIM.toLowerCase():void 0;return e==="linkedom"||e==="jsdom"?e:"auto"},Xe=()=>{let e=Me();return e==="linkedom"?[R,N]:e==="jsdom"?[N,R]:[R,N]},Le=()=>{let e=[];for(let n of Xe())try{return n()}catch(r){e.push(r);}let t='Unable to bootstrap a DOM-like environment. Install "linkedom" or "jsdom" (both optional peer dependencies) or set KNIGHTED_JSX_NODE_SHIM to pick one explicitly.';throw new AggregateError(e,t)},Q=false,ne=()=>{if(Pe()||Q)return;let e=Le();je(e),Q=true;};var re=e=>{let t=e.replace(/\r/g,"").replace(/\n\s+/g," "),n=e.match(/^\s*/)?.[0]??"",r=e.match(/\s*$/)?.[0]??"",s=/\n/.test(n),o=/\n/.test(r),a=t;return s&&(a=a.replace(/^\s+/,"")),o&&(a=a.replace(/\s+$/,"")),a.length===0||a.trim().length===0?null:a};var Oe="oxc-parser",Ie=e=>{let t=e.raw??e,n=t[0]??"",r=[];for(let s=0;s<t.length-1;s++){let o="${expr#"+s+"}",a=n.length;n+=o;let c=n.length;r.push({index:s,templateStart:a,templateEnd:c,label:o}),n+=t[s+1]??"";}return {source:n,spans:r}},_e=(e,t)=>{let n=new Map;return t.forEach(r=>{n.set(r.index,r);}),e.expressionRanges.map(r=>{let s=n.get(r.index);if(!s)return null;let o=Math.max(0,r.sourceEnd-r.sourceStart),a=Math.max(0,s.templateEnd-s.templateStart);return {sourceStart:r.sourceStart,sourceEnd:r.sourceEnd,templateStart:s.templateStart,templateEnd:s.templateEnd,delta:a-o}}).filter(r=>!!r).sort((r,s)=>r.sourceStart-s.sourceStart)},$e=(e,t,n)=>{if(!Number.isFinite(e)||e<=0)return 0;let r=0;for(let o of t){if(e<o.sourceStart)break;if(e<o.sourceEnd){let a=Math.max(0,e-o.sourceStart),c=Math.max(0,o.templateEnd-o.templateStart);if(c===0)return o.templateStart;let i=Math.min(a,Math.max(0,c-1));return o.templateStart+i}r+=o.delta;}let s=e+r;return s<=0?0:s>=n?n:s},oe=(e,t)=>{let n=Math.max(0,Math.min(t,e.length)),r=1,s=1;for(let o=0;o<n;o++){if(e.charCodeAt(o)===10){r++,s=1;continue}s++;}return {line:r,column:s}},Fe=e=>{let t=[],n=0;return e.forEach((r,s)=>{t.push(n),n+=r.length,s<e.length-1&&(n+=1);}),t},He=(e,t,n,r,s,o,a)=>{let c=n+t.length,i=Math.max(r,n),p=Math.min(s,c);if(p>i){let l=Math.max(0,i-n),d=Math.max(1,p-i);return " ".repeat(l)+"^".repeat(d)}if(t.length===0&&e>=o&&e<=a)return "^";if(e===o){let l=Math.max(0,r-n);return " ".repeat(Math.min(l,t.length))+"^"}return ""},We=(e,t,n,r,s)=>{if(!e.length)return "";let o=e.split(`
|
|
2
|
+
`),a=Fe(o),c=Math.max(1,r-1),i=Math.min(o.length,s+1),p=String(i).length,l=[];for(let d=c;d<=i;d++){let m=o[d-1]??"",u=String(d).padStart(p," ");l.push(`${u} | ${m}`);let g=He(d,m,a[d-1]??0,t,n,r,s);g&&l.push(`${" ".repeat(p)} | ${g}`);}return l.join(`
|
|
3
|
+
`)},A=(e,t,n,r,s)=>{let o=Oe,a=`[${o}] ${r.message}`,c=r.labels?.[0];if(!c)return a;let{source:i,spans:p}=Ie(t),l=_e(n,p),d=y=>$e(typeof y=="number"?y:0,l,i.length),m=d(c.start),u=d(c.end);u<=m&&(u=Math.min(i.length,m+1));let g=oe(i,m),x=oe(i,Math.max(m,u-1)),E=We(i,m,u,g.line,x.line),f=`[${o}] ${r.message}`;return f+=`
|
|
4
4
|
--> ${e} template:${g.line}:${g.column}`,c.message&&(f+=`
|
|
5
|
-
${c.message}`),
|
|
6
|
-
${
|
|
7
|
-
${r.helpMessage}`),f};var
|
|
8
|
-
export{
|
|
5
|
+
${c.message}`),E&&(f+=`
|
|
6
|
+
${E}`),r.helpMessage&&(f+=`
|
|
7
|
+
${r.helpMessage}`),f};var Be=/<\s*$/,Ve=/<\/\s*$/,ae="__KX_EXPR__",se=new RegExp(`${ae}\\d+_\\d+__`,"g"),Ue=0;var ie={lang:"jsx",sourceType:"module",range:true,preserveParens:true},pe=e=>{for(let t of e.body)if(t.type==="ExpressionStatement"){let n=t.expression;if(n.type==="JSXElement"||n.type==="JSXFragment")return n}throw new Error("The jsx template must contain a single JSX element or fragment.")},w=e=>{switch(e.type){case "JSXIdentifier":return e.name;case "JSXNamespacedName":return `${e.namespace.name}:${e.name.name}`;case "JSXMemberExpression":return `${w(e.object)}.${e.property.name}`;default:return ""}},P=(e,t)=>{if(!e||typeof e!="object")return;let n=e;typeof n.type=="string"&&(t(n),Object.values(n).forEach(r=>{if(r){if(Array.isArray(r)){r.forEach(s=>P(s,t));return}typeof r=="object"&&P(r,t);}}));},ce=(e,t)=>{let n=re(e);if(!n)return [];let r=[];se.lastIndex=0;let s=0,o;for(;o=se.exec(n);){let c=o.index,i=n.slice(s,c);i&&r.push(i);let p=o[0];t.has(p)?r.push(t.get(p)):r.push(p),s=c+p.length;}let a=n.slice(s);return a&&r.push(a),r},ze=(e,t)=>{let n=new Set;return P(e,r=>{r.type==="Identifier"&&t.placeholders.has(r.name)&&n.add(r.name);}),Array.from(n)},le=(e,t,n)=>{if(e.type==="JSXElement"||e.type==="JSXFragment")return n(e);if(!("range"in e)||!e.range)throw new Error("Unable to evaluate expression: missing source range information.");let[r,s]=e.range,o=t.source.slice(r,s),a=ze(e,t);try{let c=new Function(...a,`"use strict"; return (${o});`),i=a.map(p=>t.placeholders.get(p));return c(...i)}catch(c){throw new Error(`Failed to evaluate expression ${o}: ${c.message}`,{cause:c})}},qe=e=>{let t=e.replace(/[^a-zA-Z0-9_$]/g,"");return t?/[A-Za-z_$]/.test(t[0])?t:`Component${t}`:"Component"},Ge=(e,t,n)=>{let r=n.get(e);if(r)return r;let s=e.displayName||e.name||`Component${t.length}`,o=qe(s??""),a=o,c=1;for(;t.some(p=>p.name===a);)a=`${o}${c++}`;let i={name:a,value:e};return t.push(i),n.set(e,i),i},me=(e,t,n)=>{let r=e.raw??e,s=new Map,o=[],a=new Map,c=r[0]??"",i=Ue++,p=0,l=[],d=(m=>typeof m=="function");for(let m=0;m<t.length;m++){let u=r[m]??"",g=r[m+1]??"",x=t[m],E=Be.test(u)||Ve.test(u),f;if(E&&d(x))f=Ge(x,o,a).name;else if(E&&typeof x=="string")f=x;else {if(E)throw new Error("Invalid tag interpolation value. Expected a component, class, or string tag name.");{let T=`${ae}${i}_${p++}__`;s.set(T,x),f=T;}}let y=c.length;c+=f;let $=c.length;l.push({index:m,sourceStart:y,sourceEnd:$}),c+=g;}return {source:c,placeholders:s,bindings:o,diagnostics:{expressionRanges:l}}};var j=Symbol.for("@knighted/jsx::Fragment"),J="__jsxNs",Ke=(e,t)=>t.length>0?t:Object.prototype.hasOwnProperty.call(e,"children")?[e.children]:[],Ze=(e,t)=>{let n={...e};return t.length===1?n.children=t[0]:t.length>1?n.children=t:delete n.children,n},Ye=e=>{if(!Object.prototype.hasOwnProperty.call(e,J))return;let t=e[J];if(delete e[J],t==="svg"||t===null)return t;throw new Error(`${J} must be "svg" or null when provided.`)},ue=({ensureDomAvailable:e,appendChildValue:t,setDomProp:n,isPromiseLike:r})=>{function s(o,a,...c){e();let i=a?{...a}:{},p=Ke(i,c);if(o===j){let u=document.createDocumentFragment();return p.forEach(g=>t(u,g)),u}if(typeof o=="function"){let u=o(Ze(i,p));if(r(u))throw new Error("Async jsx components are not supported.");return u}if(typeof o!="string")throw new Error(`Unsupported jsx createElement type: ${String(o)}`);delete i.children;let d=Ye(i)??(o==="svg"?"svg":null),m=d==="svg"?document.createElementNS("http://www.w3.org/2000/svg",o):document.createElement(o);return Object.entries(i).forEach(([u,g])=>{u!=="key"&&n(m,u,g,d);}),p.forEach(u=>t(m,u)),m}return s};var ge=()=>{if(typeof document>"u"||typeof document.createElement!="function")throw new Error("The jsx template tag requires a DOM-like environment (document missing).")},rt=e=>typeof Node>"u"?false:e instanceof Node||e instanceof DocumentFragment,ot=e=>!e||typeof e=="string"?false:typeof e[Symbol.iterator]=="function",O=e=>!e||typeof e!="object"&&typeof e!="function"?false:typeof e.then=="function",st={xlink:"http://www.w3.org/1999/xlink",xml:"http://www.w3.org/XML/1998/namespace",xmlns:"http://www.w3.org/2000/xmlns/"},fe=e=>{if(!(!e||e==="html"||e==="svg"))return st[e]},at=e=>e==="svg"?svg:html,M=(e,t,n)=>{let r=fe(t.space),s=String(n);if(r){e.setAttributeNS(r,t.attribute,s);return}e.setAttribute(t.attribute,s);},de=(e,t)=>{let n=fe(t.space);if(n){e.removeAttributeNS(n,t.attribute);return}e.removeAttribute(t.attribute);},X=(e,t)=>Array.isArray(e)?e.filter(Boolean).join(t):e,it=(e,t,n)=>t==="svg"||!n.property||n.property.includes(":")?false:n.property in e,xe=(e,t,n,r)=>{if(n==null)return;if(t==="dangerouslySetInnerHTML"&&typeof n=="object"&&n&&"__html"in n){e.innerHTML=String(n.__html??"");return}if(t==="ref"){if(typeof n=="function"){n(e);return}if(n&&typeof n=="object"){n.current=e;return}}if(t==="style"&&typeof n=="object"&&n!==null){let p=n,l=e.style;if(!l)return;let d=l;Object.entries(p).forEach(([m,u])=>{if(u!=null){if(m.startsWith("--")){l.setProperty(m,String(u));return}d[m]=u;}});return}let s=z(t);if(s){let p=G(t,n);if(p){let l=p.options?{...p.options}:void 0;s.capture&&(l?l.capture=true:l={capture:true}),e.addEventListener(s.eventName,p.listener,l);return}}let o=find(at(r),t),a=e,c=it(e,r,o);if(o.mustUseProperty){let p=o.boolean?!!n:n;a[o.property]=p;return}if(o.boolean){let p=!!n;c&&(a[o.property]=p),p?M(e,o,""):de(e,o);return}let i=n;if(o.spaceSeparated?i=X(n," "):o.commaSeparated?i=X(n,","):o.commaOrSpaceSeparated&&(i=X(n," ")),o.booleanish&&typeof i=="boolean"&&(i=i?"true":"false"),o.overloadedBoolean){if(i===false){de(e,o);return}if(i===true){M(e,o,"");return}}if(c){a[o.property]=i;return}i!==false&&M(e,o,i);},h=(e,t)=>{if(t!=null&&typeof t!="boolean"){if(O(t))throw new Error("Async values are not supported inside jsx template results.");if(Array.isArray(t)){t.forEach(n=>h(e,n));return}if(ot(t)){for(let n of t)h(e,n);return}if(rt(t)){e.appendChild(t);return}e.appendChild(document.createTextNode(String(t)));}},L=(e,t,n)=>le(e,t,r=>_(r,t,n)),Ee=H({getIdentifierName:w,evaluateExpressionWithNamespace:L}),pt=(e,t,n,r)=>{let s=Ee(t,n,r);Object.entries(s).forEach(([o,a])=>{if(o!=="key"){if(o==="children"){h(e,a);return}xe(e,o,a,r);}});},I=(e,t,n)=>{let r=[];return e.forEach(s=>{switch(s.type){case "JSXText":{ce(s.value,t.placeholders).forEach(a=>{r.push(a);});break}case "JSXExpressionContainer":{if(s.expression.type==="JSXEmptyExpression")break;r.push(L(s.expression,t,n));break}case "JSXSpreadChild":{let o=L(s.expression,t,n);o!=null&&r.push(o);break}case "JSXElement":case "JSXFragment":{r.push(_(s,t,n));break}}}),r},ct=(e,t,n,r)=>{let s=Ee(e.openingElement.attributes,t,r),o=I(e.children,t,r);o.length===1?s.children=o[0]:o.length>1&&(s.children=o);let a=n(s);if(O(a))throw new Error("Async jsx components are not supported.");return a},lt=(e,t,n)=>{let r=e.openingElement,s=w(r.name),o=t.components.get(s);if(o)return ct(e,t,o,n);if(/[A-Z]/.test(s[0]??""))throw new Error(`Unknown component "${s}". Did you interpolate it with the template literal?`);let a=s==="svg"?"svg":n,c=s==="foreignObject"?null:a,i=a==="svg"?document.createElementNS("http://www.w3.org/2000/svg",s):document.createElement(s);return pt(i,r.attributes,t,a),I(e.children,t,c).forEach(l=>h(i,l)),i},_=(e,t,n)=>{if(e.type==="JSXFragment"){let r=document.createDocumentFragment();return I(e.children,t,n).forEach(o=>h(r,o)),r}return lt(e,t,n)},mt=ue({ensureDomAvailable:ge,appendChildValue:h,setDomProp:xe,isPromiseLike:O}),ut=(e,...t)=>{ge();let n=me(e,t),r=parseSync("inline.jsx",n.source,ie);if(r.errors.length>0)throw new Error(A("jsx",e,n.diagnostics,r.errors[0]));let s=pe(r.program),o={source:n.source,placeholders:n.placeholders,components:new Map(n.bindings.map(a=>[a.name,a.value]))};return _(s,o,null)},he=Object.assign(ut,{createElement:mt,Fragment:j});Y({mode:"always"});ne();var It=he;
|
|
8
|
+
export{It as jsx};
|
package/dist/lite/node/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {createRequire}from'module';import {parseSync}from'oxc-parser';import {find,svg,html}from'property-information';var
|
|
2
|
-
`),a=
|
|
3
|
-
`)},
|
|
4
|
-
--> ${e} template:${
|
|
5
|
-
${l.message}`),
|
|
6
|
-
${
|
|
7
|
-
${r.helpMessage}`),
|
|
8
|
-
export{
|
|
1
|
+
import {createRequire}from'module';import {parseSync}from'oxc-parser';import {find,svg,html}from'property-information';var le=createRequire(import.meta.url),L=()=>le,O="<!doctype html><html><body></body></html>",me=["window","self","document","HTMLElement","Element","Node","DocumentFragment","customElements","Text","Comment","MutationObserver","navigator"],ue=()=>typeof document<"u"&&typeof document.createElement=="function",de=e=>{let t=globalThis,n=e;me.forEach(r=>{t[r]===void 0&&n[r]!==void 0&&(t[r]=n[r]);});},J=()=>{let{parseHTML:e}=L()("linkedom"),{window:t}=e(O);return t},w=()=>{let{JSDOM:e}=L()("jsdom"),{window:t}=new e(O);return t},ge=()=>{let e=typeof process<"u"&&process.env?.KNIGHTED_JSX_NODE_SHIM?process.env.KNIGHTED_JSX_NODE_SHIM.toLowerCase():void 0;return e==="linkedom"||e==="jsdom"?e:"auto"},fe=()=>{let e=ge();return e==="linkedom"?[J,w]:e==="jsdom"?[w,J]:[J,w]},xe=()=>{let e=[];for(let n of fe())try{return n()}catch(r){e.push(r);}let t='Unable to bootstrap a DOM-like environment. Install "linkedom" or "jsdom" (both optional peer dependencies) or set KNIGHTED_JSX_NODE_SHIM to pick one explicitly.';throw new AggregateError(e,t)},M=false,F=()=>{if(ue()||M)return;let e=xe();de(e),M=true;};var I=e=>{let t=e.replace(/\r/g,"").replace(/\n\s+/g," "),n=e.match(/^\s*/)?.[0]??"",r=e.match(/\s*$/)?.[0]??"",s=/\n/.test(n),o=/\n/.test(r),a=t;return s&&(a=a.replace(/^\s+/,"")),o&&(a=a.replace(/\s+$/,"")),a.length===0||a.trim().length===0?null:a};var Ee="oxc-parser",he=e=>{let t=e.raw??e,n=t[0]??"",r=[];for(let s=0;s<t.length-1;s++){let o="${expr#"+s+"}",a=n.length;n+=o;let l=n.length;r.push({index:s,templateStart:a,templateEnd:l,label:o}),n+=t[s+1]??"";}return {source:n,spans:r}},ye=(e,t)=>{let n=new Map;return t.forEach(r=>{n.set(r.index,r);}),e.expressionRanges.map(r=>{let s=n.get(r.index);if(!s)return null;let o=Math.max(0,r.sourceEnd-r.sourceStart),a=Math.max(0,s.templateEnd-s.templateStart);return {sourceStart:r.sourceStart,sourceEnd:r.sourceEnd,templateStart:s.templateStart,templateEnd:s.templateEnd,delta:a-o}}).filter(r=>!!r).sort((r,s)=>r.sourceStart-s.sourceStart)},be=(e,t,n)=>{if(!Number.isFinite(e)||e<=0)return 0;let r=0;for(let o of t){if(e<o.sourceStart)break;if(e<o.sourceEnd){let a=Math.max(0,e-o.sourceStart),l=Math.max(0,o.templateEnd-o.templateStart);if(l===0)return o.templateStart;let i=Math.min(a,Math.max(0,l-1));return o.templateStart+i}r+=o.delta;}let s=e+r;return s<=0?0:s>=n?n:s},_=(e,t)=>{let n=Math.max(0,Math.min(t,e.length)),r=1,s=1;for(let o=0;o<n;o++){if(e.charCodeAt(o)===10){r++,s=1;continue}s++;}return {line:r,column:s}},Se=e=>{let t=[],n=0;return e.forEach((r,s)=>{t.push(n),n+=r.length,s<e.length-1&&(n+=1);}),t},Te=(e,t,n,r,s,o,a)=>{let l=n+t.length,i=Math.max(r,n),p=Math.min(s,l);if(p>i){let c=Math.max(0,i-n),d=Math.max(1,p-i);return " ".repeat(c)+"^".repeat(d)}if(t.length===0&&e>=o&&e<=a)return "^";if(e===o){let c=Math.max(0,r-n);return " ".repeat(Math.min(c,t.length))+"^"}return ""},Je=(e,t,n,r,s)=>{if(!e.length)return "";let o=e.split(`
|
|
2
|
+
`),a=Se(o),l=Math.max(1,r-1),i=Math.min(o.length,s+1),p=String(i).length,c=[];for(let d=l;d<=i;d++){let m=o[d-1]??"",u=String(d).padStart(p," ");c.push(`${u} | ${m}`);let g=Te(d,m,a[d-1]??0,t,n,r,s);g&&c.push(`${" ".repeat(p)} | ${g}`);}return c.join(`
|
|
3
|
+
`)},C=(e,t,n,r,s)=>{let o=Ee,a=`[${o}] ${r.message}`,l=r.labels?.[0];if(!l)return a;let{source:i,spans:p}=he(t),c=ye(n,p),d=y=>be(typeof y=="number"?y:0,c,i.length),m=d(l.start),u=d(l.end);u<=m&&(u=Math.min(i.length,m+1));let g=_(i,m),x=_(i,Math.max(m,u-1)),E=Je(i,m,u,g.line,x.line),f=`[${o}] ${r.message}`;return f+=`
|
|
4
|
+
--> ${e} template:${g.line}:${g.column}`,l.message&&(f+=`
|
|
5
|
+
${l.message}`),E&&(f+=`
|
|
6
|
+
${E}`),r.helpMessage&&(f+=`
|
|
7
|
+
${r.helpMessage}`),f};var we=/<\s*$/,Ce=/<\/\s*$/,H="__KX_EXPR__",$=new RegExp(`${H}\\d+_\\d+__`,"g"),ke=0;var B={lang:"jsx",sourceType:"module",range:true,preserveParens:true},W=e=>{for(let t of e.body)if(t.type==="ExpressionStatement"){let n=t.expression;if(n.type==="JSXElement"||n.type==="JSXFragment")return n}throw new Error("The jsx template must contain a single JSX element or fragment.")},b=e=>{switch(e.type){case "JSXIdentifier":return e.name;case "JSXNamespacedName":return `${e.namespace.name}:${e.name.name}`;case "JSXMemberExpression":return `${b(e.object)}.${e.property.name}`;default:return ""}},k=(e,t)=>{if(!e||typeof e!="object")return;let n=e;typeof n.type=="string"&&(t(n),Object.values(n).forEach(r=>{if(r){if(Array.isArray(r)){r.forEach(s=>k(s,t));return}typeof r=="object"&&k(r,t);}}));},V=(e,t)=>{let n=I(e);if(!n)return [];let r=[];$.lastIndex=0;let s=0,o;for(;o=$.exec(n);){let l=o.index,i=n.slice(s,l);i&&r.push(i);let p=o[0];t.has(p)?r.push(t.get(p)):r.push(p),s=l+p.length;}let a=n.slice(s);return a&&r.push(a),r},ve=(e,t)=>{let n=new Set;return k(e,r=>{r.type==="Identifier"&&t.placeholders.has(r.name)&&n.add(r.name);}),Array.from(n)},z=(e,t,n)=>{if(e.type==="JSXElement"||e.type==="JSXFragment")return n(e);if(!("range"in e)||!e.range)throw new Error("Unable to evaluate expression: missing source range information.");let[r,s]=e.range,o=t.source.slice(r,s),a=ve(e,t);try{let l=new Function(...a,`"use strict"; return (${o});`),i=a.map(p=>t.placeholders.get(p));return l(...i)}catch(l){throw new Error(`Failed to evaluate expression ${o}: ${l.message}`,{cause:l})}},Ne=e=>{let t=e.replace(/[^a-zA-Z0-9_$]/g,"");return t?/[A-Za-z_$]/.test(t[0])?t:`Component${t}`:"Component"},Re=(e,t,n)=>{let r=n.get(e);if(r)return r;let s=e.displayName||e.name||`Component${t.length}`,o=Ne(s??""),a=o,l=1;for(;t.some(p=>p.name===a);)a=`${o}${l++}`;let i={name:a,value:e};return t.push(i),n.set(e,i),i},U=(e,t,n)=>{let r=e.raw??e,s=new Map,o=[],a=new Map,l=r[0]??"",i=ke++,p=0,c=[],d=(m=>typeof m=="function");for(let m=0;m<t.length;m++){let u=r[m]??"",g=r[m+1]??"",x=t[m],E=we.test(u)||Ce.test(u),f;if(E&&d(x))f=Re(x,o,a).name;else if(E&&typeof x=="string")f=x;else {if(E)throw new Error("Invalid tag interpolation value. Expected a component, class, or string tag name.");{let T=`${H}${i}_${p++}__`;s.set(T,x),f=T;}}let y=l.length;l+=f;let X=l.length;c.push({index:m,sourceStart:y,sourceEnd:X}),l+=g;}return {source:l,placeholders:s,bindings:o,diagnostics:{expressionRanges:c}}};var K=e=>{let{getIdentifierName:t,evaluateExpressionWithNamespace:n}=e;return (r,s,o)=>{let a={},l=(i,p)=>{a[i]=p;};return r.forEach(i=>{if(i.type==="JSXSpreadAttribute"){let c=n(i.argument,s,o);c&&typeof c=="object"&&!Array.isArray(c)&&Object.assign(a,c);return}let p=t(i.name);if(!i.value){l(p,true);return}if(i.value.type==="Literal"){l(p,i.value.value);return}if(i.value.type==="JSXExpressionContainer"){if(i.value.expression.type==="JSXEmptyExpression")return;l(p,n(i.value.expression,s,o));}}),a}};var G="Capture",Z=e=>e.endsWith(G)?{eventName:e.slice(0,-G.length),capture:true}:{eventName:e,capture:false},Y=e=>{if(!e.startsWith("on"))return null;if(e.startsWith("on:")){let r=e.slice(3);if(!r)return null;let s=Z(r);return s.eventName?s:null}let t=e.slice(2);if(!t)return null;let n=Z(t);return n.eventName?{eventName:n.eventName.toLowerCase(),capture:n.capture}:null},Q=e=>!e||typeof e!="object"?false:"handleEvent"in e&&typeof e.handleEvent=="function",De=e=>{if(!e||typeof e!="object"||!("handler"in e))return false;let t=e.handler;return typeof t=="function"?true:Q(t)},ee=(e,t)=>{if(typeof t=="function"||Q(t))return {listener:t};if(!De(t))return null;let n=t,r=n.options?{...n.options}:void 0,s=(o,a)=>{a!=null&&(r||(r={}),r[o]=a);};return s("capture",n.capture),s("once",n.once),s("passive",n.passive),s("signal",n.signal??void 0),{listener:n.handler,options:r}};var v=Symbol.for("@knighted/jsx::Fragment"),S="__jsxNs",Me=(e,t)=>t.length>0?t:Object.prototype.hasOwnProperty.call(e,"children")?[e.children]:[],Le=(e,t)=>{let n={...e};return t.length===1?n.children=t[0]:t.length>1?n.children=t:delete n.children,n},Oe=e=>{if(!Object.prototype.hasOwnProperty.call(e,S))return;let t=e[S];if(delete e[S],t==="svg"||t===null)return t;throw new Error(`${S} must be "svg" or null when provided.`)},te=({ensureDomAvailable:e,appendChildValue:t,setDomProp:n,isPromiseLike:r})=>{function s(o,a,...l){e();let i=a?{...a}:{},p=Me(i,l);if(o===v){let u=document.createDocumentFragment();return p.forEach(g=>t(u,g)),u}if(typeof o=="function"){let u=o(Le(i,p));if(r(u))throw new Error("Async jsx components are not supported.");return u}if(typeof o!="string")throw new Error(`Unsupported jsx createElement type: ${String(o)}`);delete i.children;let d=Oe(i)??(o==="svg"?"svg":null),m=d==="svg"?document.createElementNS("http://www.w3.org/2000/svg",o):document.createElement(o);return Object.entries(i).forEach(([u,g])=>{u!=="key"&&n(m,u,g,d);}),p.forEach(u=>t(m,u)),m}return s};var re=()=>{if(typeof document>"u"||typeof document.createElement!="function")throw new Error("The jsx template tag requires a DOM-like environment (document missing).")},He=e=>typeof Node>"u"?false:e instanceof Node||e instanceof DocumentFragment,Be=e=>!e||typeof e=="string"?false:typeof e[Symbol.iterator]=="function",P=e=>!e||typeof e!="object"&&typeof e!="function"?false:typeof e.then=="function",We={xlink:"http://www.w3.org/1999/xlink",xml:"http://www.w3.org/XML/1998/namespace",xmlns:"http://www.w3.org/2000/xmlns/"},oe=e=>{if(!(!e||e==="html"||e==="svg"))return We[e]},Ve=e=>e==="svg"?svg:html,N=(e,t,n)=>{let r=oe(t.space),s=String(n);if(r){e.setAttributeNS(r,t.attribute,s);return}e.setAttribute(t.attribute,s);},ne=(e,t)=>{let n=oe(t.space);if(n){e.removeAttributeNS(n,t.attribute);return}e.removeAttribute(t.attribute);},R=(e,t)=>Array.isArray(e)?e.filter(Boolean).join(t):e,ze=(e,t,n)=>t==="svg"||!n.property||n.property.includes(":")?false:n.property in e,se=(e,t,n,r)=>{if(n==null)return;if(t==="dangerouslySetInnerHTML"&&typeof n=="object"&&n&&"__html"in n){e.innerHTML=String(n.__html??"");return}if(t==="ref"){if(typeof n=="function"){n(e);return}if(n&&typeof n=="object"){n.current=e;return}}if(t==="style"&&typeof n=="object"&&n!==null){let p=n,c=e.style;if(!c)return;let d=c;Object.entries(p).forEach(([m,u])=>{if(u!=null){if(m.startsWith("--")){c.setProperty(m,String(u));return}d[m]=u;}});return}let s=Y(t);if(s){let p=ee(t,n);if(p){let c=p.options?{...p.options}:void 0;s.capture&&(c?c.capture=true:c={capture:true}),e.addEventListener(s.eventName,p.listener,c);return}}let o=find(Ve(r),t),a=e,l=ze(e,r,o);if(o.mustUseProperty){let p=o.boolean?!!n:n;a[o.property]=p;return}if(o.boolean){let p=!!n;l&&(a[o.property]=p),p?N(e,o,""):ne(e,o);return}let i=n;if(o.spaceSeparated?i=R(n," "):o.commaSeparated?i=R(n,","):o.commaOrSpaceSeparated&&(i=R(n," ")),o.booleanish&&typeof i=="boolean"&&(i=i?"true":"false"),o.overloadedBoolean){if(i===false){ne(e,o);return}if(i===true){N(e,o,"");return}}if(l){a[o.property]=i;return}i!==false&&N(e,o,i);},h=(e,t)=>{if(t!=null&&typeof t!="boolean"){if(P(t))throw new Error("Async values are not supported inside jsx template results.");if(Array.isArray(t)){t.forEach(n=>h(e,n));return}if(Be(t)){for(let n of t)h(e,n);return}if(He(t)){e.appendChild(t);return}e.appendChild(document.createTextNode(String(t)));}},A=(e,t,n)=>z(e,t,r=>D(r,t,n)),ae=K({getIdentifierName:b,evaluateExpressionWithNamespace:A}),Ue=(e,t,n,r)=>{let s=ae(t,n,r);Object.entries(s).forEach(([o,a])=>{if(o!=="key"){if(o==="children"){h(e,a);return}se(e,o,a,r);}});},j=(e,t,n)=>{let r=[];return e.forEach(s=>{switch(s.type){case "JSXText":{V(s.value,t.placeholders).forEach(a=>{r.push(a);});break}case "JSXExpressionContainer":{if(s.expression.type==="JSXEmptyExpression")break;r.push(A(s.expression,t,n));break}case "JSXSpreadChild":{let o=A(s.expression,t,n);o!=null&&r.push(o);break}case "JSXElement":case "JSXFragment":{r.push(D(s,t,n));break}}}),r},qe=(e,t,n,r)=>{let s=ae(e.openingElement.attributes,t,r),o=j(e.children,t,r);o.length===1?s.children=o[0]:o.length>1&&(s.children=o);let a=n(s);if(P(a))throw new Error("Async jsx components are not supported.");return a},Ke=(e,t,n)=>{let r=e.openingElement,s=b(r.name),o=t.components.get(s);if(o)return qe(e,t,o,n);if(/[A-Z]/.test(s[0]??""))throw new Error(`Unknown component "${s}". Did you interpolate it with the template literal?`);let a=s==="svg"?"svg":n,l=s==="foreignObject"?null:a,i=a==="svg"?document.createElementNS("http://www.w3.org/2000/svg",s):document.createElement(s);return Ue(i,r.attributes,t,a),j(e.children,t,l).forEach(c=>h(i,c)),i},D=(e,t,n)=>{if(e.type==="JSXFragment"){let r=document.createDocumentFragment();return j(e.children,t,n).forEach(o=>h(r,o)),r}return Ke(e,t,n)},Ge=te({ensureDomAvailable:re,appendChildValue:h,setDomProp:se,isPromiseLike:P}),Ze=(e,...t)=>{re();let n=U(e,t),r=parseSync("inline.jsx",n.source,B);if(r.errors.length>0)throw new Error(C("jsx",e,n.diagnostics,r.errors[0]));let s=W(r.program),o={source:n.source,placeholders:n.placeholders,components:new Map(n.bindings.map(a=>[a.name,a.value]))};return D(s,o,null)},ie=Object.assign(Ze,{createElement:Ge,Fragment:v});F();var Et=ie;
|
|
8
|
+
export{Et as jsx};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {parseSync}from'oxc-parser';import {createElement,Fragment}from'react';var
|
|
2
|
-
`),a=
|
|
3
|
-
`)},J=(e,r,
|
|
4
|
-
--> ${e} template:${
|
|
5
|
-
${p.message}`),
|
|
6
|
-
${
|
|
7
|
-
${t.helpMessage}`),
|
|
1
|
+
import {parseSync}from'oxc-parser';import {createElement,Fragment}from'react';var X=e=>{let n=e.replace(/\r/g,"").replace(/\n\s+/g," "),r=e.match(/^\s*/)?.[0]??"",t=e.match(/\s*$/)?.[0]??"",o=/\n/.test(r),s=/\n/.test(t),a=n;return o&&(a=a.replace(/^\s+/,"")),s&&(a=a.replace(/\s+$/,"")),a.length===0||a.trim().length===0?null:a};var j="oxc-parser",L=e=>{let n=e.raw??e,r=n[0]??"",t=[];for(let o=0;o<n.length-1;o++){let s="${expr#"+o+"}",a=r.length;r+=s;let p=r.length;t.push({index:o,templateStart:a,templateEnd:p,label:s}),r+=n[o+1]??"";}return {source:r,spans:t}},D=(e,n)=>{let r=new Map;return n.forEach(t=>{r.set(t.index,t);}),e.expressionRanges.map(t=>{let o=r.get(t.index);if(!o)return null;let s=Math.max(0,t.sourceEnd-t.sourceStart),a=Math.max(0,o.templateEnd-o.templateStart);return {sourceStart:t.sourceStart,sourceEnd:t.sourceEnd,templateStart:o.templateStart,templateEnd:o.templateEnd,delta:a-s}}).filter(t=>!!t).sort((t,o)=>t.sourceStart-o.sourceStart)},v=(e,n,r)=>{if(!Number.isFinite(e)||e<=0)return 0;let t=0;for(let s of n){if(e<s.sourceStart)break;if(e<s.sourceEnd){let a=Math.max(0,e-s.sourceStart),p=Math.max(0,s.templateEnd-s.templateStart);if(p===0)return s.templateStart;let i=Math.min(a,Math.max(0,p-1));return s.templateStart+i}t+=s.delta;}let o=e+t;return o<=0?0:o>=r?r:o},$=(e,n)=>{let r=Math.max(0,Math.min(n,e.length)),t=1,o=1;for(let s=0;s<r;s++){if(e.charCodeAt(s)===10){t++,o=1;continue}o++;}return {line:t,column:o}},z=e=>{let n=[],r=0;return e.forEach((t,o)=>{n.push(r),r+=t.length,o<e.length-1&&(r+=1);}),n},V=(e,n,r,t,o,s,a)=>{let p=r+n.length,i=Math.max(t,r),c=Math.min(o,p);if(c>i){let u=Math.max(0,i-r),l=Math.max(1,c-i);return " ".repeat(u)+"^".repeat(l)}if(n.length===0&&e>=s&&e<=a)return "^";if(e===s){let u=Math.max(0,t-r);return " ".repeat(Math.min(u,n.length))+"^"}return ""},W=(e,n,r,t,o)=>{if(!e.length)return "";let s=e.split(`
|
|
2
|
+
`),a=z(s),p=Math.max(1,t-1),i=Math.min(s.length,o+1),c=String(i).length,u=[];for(let l=p;l<=i;l++){let m=s[l-1]??"",d=String(l).padStart(c," ");u.push(`${d} | ${m}`);let x=V(l,m,a[l-1]??0,n,r,t,o);x&&u.push(`${" ".repeat(c)} | ${x}`);}return u.join(`
|
|
3
|
+
`)},J=(e,n,r,t,o)=>{let s=j,a=`[${s}] ${t.message}`,p=t.labels?.[0];if(!p)return a;let{source:i,spans:c}=L(n),u=D(r,c),l=h=>v(typeof h=="number"?h:0,u,i.length),m=l(p.start),d=l(p.end);d<=m&&(d=Math.min(i.length,m+1));let x=$(i,m),f=$(i,Math.max(m,d-1)),y=W(i,m,d,x.line,f.line),g=`[${s}] ${t.message}`;return g+=`
|
|
4
|
+
--> ${e} template:${x.line}:${x.column}`,p.message&&(g+=`
|
|
5
|
+
${p.message}`),y&&(g+=`
|
|
6
|
+
${y}`),t.helpMessage&&(g+=`
|
|
7
|
+
${t.helpMessage}`),g};var H=/<\s*$/,U=/<\/\s*$/,A="__KX_EXPR__",k=new RegExp(`${A}\\d+_\\d+__`,"g"),Z=0;var M={lang:"jsx",sourceType:"module",range:true,preserveParens:true},N=e=>{for(let n of e.body)if(n.type==="ExpressionStatement"){let r=n.expression;if(r.type==="JSXElement"||r.type==="JSXFragment")return r}throw new Error("The jsx template must contain a single JSX element or fragment.")},T=e=>{switch(e.type){case "JSXIdentifier":return e.name;case "JSXNamespacedName":return `${e.namespace.name}:${e.name.name}`;case "JSXMemberExpression":return `${T(e.object)}.${e.property.name}`;default:return ""}},b=(e,n)=>{if(!e||typeof e!="object")return;let r=e;typeof r.type=="string"&&(n(r),Object.values(r).forEach(t=>{if(t){if(Array.isArray(t)){t.forEach(o=>b(o,n));return}typeof t=="object"&&b(t,n);}}));},F=(e,n)=>{let r=X(e);if(!r)return [];let t=[];k.lastIndex=0;let o=0,s;for(;s=k.exec(r);){let p=s.index,i=r.slice(o,p);i&&t.push(i);let c=s[0];n.has(c)?t.push(n.get(c)):t.push(c),o=p+c.length;}let a=r.slice(o);return a&&t.push(a),t},G=(e,n)=>{let r=new Set;return b(e,t=>{t.type==="Identifier"&&n.placeholders.has(t.name)&&r.add(t.name);}),Array.from(r)},I=(e,n,r)=>{if(e.type==="JSXElement"||e.type==="JSXFragment")return r(e);if(!("range"in e)||!e.range)throw new Error("Unable to evaluate expression: missing source range information.");let[t,o]=e.range,s=n.source.slice(t,o),a=G(e,n);try{let p=new Function(...a,`"use strict"; return (${s});`),i=a.map(c=>n.placeholders.get(c));return p(...i)}catch(p){throw new Error(`Failed to evaluate expression ${s}: ${p.message}`,{cause:p})}},K=e=>{let n=e.replace(/[^a-zA-Z0-9_$]/g,"");return n?/[A-Za-z_$]/.test(n[0])?n:`Component${n}`:"Component"},q=(e,n,r)=>{let t=r.get(e);if(t)return t;let o=e.displayName||e.name||`Component${n.length}`,s=K(o??""),a=s,p=1;for(;n.some(c=>c.name===a);)a=`${s}${p++}`;let i={name:a,value:e};return n.push(i),r.set(e,i),i},P=(e,n,r)=>{let t=e.raw??e,o=new Map,s=[],a=new Map,p=t[0]??"",i=Z++,c=0,u=[],l=r?.isTagNameBindingValue??(m=>typeof m=="function");for(let m=0;m<n.length;m++){let d=t[m]??"",x=t[m+1]??"",f=n[m],y=H.test(d)||U.test(d),g;if(y&&l(f))g=q(f,s,a).name;else if(y&&typeof f=="string")g=f;else {if(y)throw new Error("Invalid tag interpolation value. Expected a component, class, or string tag name.");{let C=`${A}${i}_${c++}__`;o.set(C,f),g=C;}}let h=p.length;p+=g;let w=p.length;u.push({index:m,sourceStart:h,sourceEnd:w}),p+=x;}return {source:p,placeholders:o,bindings:s,diagnostics:{expressionRanges:u}}};var ee=e=>!e||typeof e=="string"?false:typeof e[Symbol.iterator]=="function",te=e=>!e||typeof e!="object"&&typeof e!="function"?false:typeof e.then=="function",E=(e,n)=>{if(n!=null&&typeof n!="boolean"){if(te(n))throw new Error("Async values are not supported inside reactJsx template results.");if(Array.isArray(n)){n.forEach(r=>E(e,r));return}if(ee(n)){for(let r of n)E(e,r);return}e.push(n);}},S=(e,n)=>I(e,n,r=>R(r,n)),ne=(e,n)=>{let r={};return e.forEach(t=>{if(t.type==="JSXSpreadAttribute"){let s=S(t.argument,n);s&&typeof s=="object"&&!Array.isArray(s)&&Object.assign(r,s);return}let o=T(t.name);if(!t.value){r[o]=true;return}if(t.value.type==="Literal"){r[o]=t.value.value;return}if(t.value.type==="JSXExpressionContainer"){if(t.value.expression.type==="JSXEmptyExpression")return;r[o]=S(t.value.expression,n);}}),r},B=(e,n)=>{let r=[];return e.forEach(t=>{switch(t.type){case "JSXText":{F(t.value,n.placeholders).forEach(s=>E(r,s));break}case "JSXExpressionContainer":{if(t.expression.type==="JSXEmptyExpression")break;E(r,S(t.expression,n));break}case "JSXSpreadChild":{let o=S(t.expression,n);o!=null&&E(r,o);break}case "JSXElement":case "JSXFragment":{r.push(R(t,n));break}}}),r},O=(e,n,r)=>createElement(e,n,...r),re=Symbol.for("react.memo"),oe=Symbol.for("react.forward_ref"),se=Symbol.for("react.lazy"),ae=e=>{if(typeof e=="function")return true;if(typeof e!="object"||e===null)return false;let n=e;return n.$$typeof===re||n.$$typeof===oe||n.$$typeof===se},pe=(e,n)=>{let r=e.openingElement,t=T(r.name),o=n.components.get(t),s=ne(r.attributes,n),a=B(e.children,n);if(o)return O(o,s,a);if(/[A-Z]/.test(t[0]??""))throw new Error(`Unknown component "${t}". Did you interpolate it with the template literal?`);return O(t,s,a)},R=(e,n)=>{if(e.type==="JSXFragment"){let r=B(e.children,n);return createElement(Fragment,null,...r)}return pe(e,n)},ie=(e,...n)=>{let r=P(e,n,{isTagNameBindingValue:ae}),t=parseSync("inline.jsx",r.source,M);if(t.errors.length>0)throw new Error(J("reactJsx",e,r.diagnostics,t.errors[0]));let o=N(t.program),s={source:r.source,placeholders:r.placeholders,components:new Map(r.bindings.map(a=>[a.name,a.value]))};return R(o,s)};export{ie as reactJsx};
|
package/dist/lite/react/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {parseSync}from'oxc-parser';import {createElement,Fragment}from'react';var
|
|
2
|
-
`),a=
|
|
3
|
-
`)},
|
|
4
|
-
--> ${e} template:${
|
|
5
|
-
${p.message}`),
|
|
6
|
-
${
|
|
7
|
-
${t.helpMessage}`),
|
|
1
|
+
import {parseSync}from'oxc-parser';import {createElement,Fragment}from'react';var X=e=>{let n=e.replace(/\r/g,"").replace(/\n\s+/g," "),r=e.match(/^\s*/)?.[0]??"",t=e.match(/\s*$/)?.[0]??"",o=/\n/.test(r),s=/\n/.test(t),a=n;return o&&(a=a.replace(/^\s+/,"")),s&&(a=a.replace(/\s+$/,"")),a.length===0||a.trim().length===0?null:a};var j="oxc-parser",D=e=>{let n=e.raw??e,r=n[0]??"",t=[];for(let o=0;o<n.length-1;o++){let s="${expr#"+o+"}",a=r.length;r+=s;let p=r.length;t.push({index:o,templateStart:a,templateEnd:p,label:s}),r+=n[o+1]??"";}return {source:r,spans:t}},L=(e,n)=>{let r=new Map;return n.forEach(t=>{r.set(t.index,t);}),e.expressionRanges.map(t=>{let o=r.get(t.index);if(!o)return null;let s=Math.max(0,t.sourceEnd-t.sourceStart),a=Math.max(0,o.templateEnd-o.templateStart);return {sourceStart:t.sourceStart,sourceEnd:t.sourceEnd,templateStart:o.templateStart,templateEnd:o.templateEnd,delta:a-s}}).filter(t=>!!t).sort((t,o)=>t.sourceStart-o.sourceStart)},v=(e,n,r)=>{if(!Number.isFinite(e)||e<=0)return 0;let t=0;for(let s of n){if(e<s.sourceStart)break;if(e<s.sourceEnd){let a=Math.max(0,e-s.sourceStart),p=Math.max(0,s.templateEnd-s.templateStart);if(p===0)return s.templateStart;let i=Math.min(a,Math.max(0,p-1));return s.templateStart+i}t+=s.delta;}let o=e+t;return o<=0?0:o>=r?r:o},$=(e,n)=>{let r=Math.max(0,Math.min(n,e.length)),t=1,o=1;for(let s=0;s<r;s++){if(e.charCodeAt(s)===10){t++,o=1;continue}o++;}return {line:t,column:o}},z=e=>{let n=[],r=0;return e.forEach((t,o)=>{n.push(r),r+=t.length,o<e.length-1&&(r+=1);}),n},V=(e,n,r,t,o,s,a)=>{let p=r+n.length,i=Math.max(t,r),c=Math.min(o,p);if(c>i){let u=Math.max(0,i-r),l=Math.max(1,c-i);return " ".repeat(u)+"^".repeat(l)}if(n.length===0&&e>=s&&e<=a)return "^";if(e===s){let u=Math.max(0,t-r);return " ".repeat(Math.min(u,n.length))+"^"}return ""},W=(e,n,r,t,o)=>{if(!e.length)return "";let s=e.split(`
|
|
2
|
+
`),a=z(s),p=Math.max(1,t-1),i=Math.min(s.length,o+1),c=String(i).length,u=[];for(let l=p;l<=i;l++){let m=s[l-1]??"",x=String(l).padStart(c," ");u.push(`${x} | ${m}`);let d=V(l,m,a[l-1]??0,n,r,t,o);d&&u.push(`${" ".repeat(c)} | ${d}`);}return u.join(`
|
|
3
|
+
`)},C=(e,n,r,t,o)=>{let s=j,a=`[${s}] ${t.message}`,p=t.labels?.[0];if(!p)return a;let{source:i,spans:c}=D(n),u=L(r,c),l=h=>v(typeof h=="number"?h:0,u,i.length),m=l(p.start),x=l(p.end);x<=m&&(x=Math.min(i.length,m+1));let d=$(i,m),f=$(i,Math.max(m,x-1)),y=W(i,m,x,d.line,f.line),g=`[${s}] ${t.message}`;return g+=`
|
|
4
|
+
--> ${e} template:${d.line}:${d.column}`,p.message&&(g+=`
|
|
5
|
+
${p.message}`),y&&(g+=`
|
|
6
|
+
${y}`),t.helpMessage&&(g+=`
|
|
7
|
+
${t.helpMessage}`),g};var H=/<\s*$/,U=/<\/\s*$/,A="__KX_EXPR__",k=new RegExp(`${A}\\d+_\\d+__`,"g"),Z=0;var M={lang:"jsx",sourceType:"module",range:true,preserveParens:true},N=e=>{for(let n of e.body)if(n.type==="ExpressionStatement"){let r=n.expression;if(r.type==="JSXElement"||r.type==="JSXFragment")return r}throw new Error("The jsx template must contain a single JSX element or fragment.")},T=e=>{switch(e.type){case "JSXIdentifier":return e.name;case "JSXNamespacedName":return `${e.namespace.name}:${e.name.name}`;case "JSXMemberExpression":return `${T(e.object)}.${e.property.name}`;default:return ""}},R=(e,n)=>{if(!e||typeof e!="object")return;let r=e;typeof r.type=="string"&&(n(r),Object.values(r).forEach(t=>{if(t){if(Array.isArray(t)){t.forEach(o=>R(o,n));return}typeof t=="object"&&R(t,n);}}));},F=(e,n)=>{let r=X(e);if(!r)return [];let t=[];k.lastIndex=0;let o=0,s;for(;s=k.exec(r);){let p=s.index,i=r.slice(o,p);i&&t.push(i);let c=s[0];n.has(c)?t.push(n.get(c)):t.push(c),o=p+c.length;}let a=r.slice(o);return a&&t.push(a),t},G=(e,n)=>{let r=new Set;return R(e,t=>{t.type==="Identifier"&&n.placeholders.has(t.name)&&r.add(t.name);}),Array.from(r)},I=(e,n,r)=>{if(e.type==="JSXElement"||e.type==="JSXFragment")return r(e);if(!("range"in e)||!e.range)throw new Error("Unable to evaluate expression: missing source range information.");let[t,o]=e.range,s=n.source.slice(t,o),a=G(e,n);try{let p=new Function(...a,`"use strict"; return (${s});`),i=a.map(c=>n.placeholders.get(c));return p(...i)}catch(p){throw new Error(`Failed to evaluate expression ${s}: ${p.message}`,{cause:p})}},K=e=>{let n=e.replace(/[^a-zA-Z0-9_$]/g,"");return n?/[A-Za-z_$]/.test(n[0])?n:`Component${n}`:"Component"},q=(e,n,r)=>{let t=r.get(e);if(t)return t;let o=e.displayName||e.name||`Component${n.length}`,s=K(o??""),a=s,p=1;for(;n.some(c=>c.name===a);)a=`${s}${p++}`;let i={name:a,value:e};return n.push(i),r.set(e,i),i},P=(e,n,r)=>{let t=e.raw??e,o=new Map,s=[],a=new Map,p=t[0]??"",i=Z++,c=0,u=[],l=r?.isTagNameBindingValue??(m=>typeof m=="function");for(let m=0;m<n.length;m++){let x=t[m]??"",d=t[m+1]??"",f=n[m],y=H.test(x)||U.test(x),g;if(y&&l(f))g=q(f,s,a).name;else if(y&&typeof f=="string")g=f;else {if(y)throw new Error("Invalid tag interpolation value. Expected a component, class, or string tag name.");{let J=`${A}${i}_${c++}__`;o.set(J,f),g=J;}}let h=p.length;p+=g;let w=p.length;u.push({index:m,sourceStart:h,sourceEnd:w}),p+=d;}return {source:p,placeholders:o,bindings:s,diagnostics:{expressionRanges:u}}};var ee=e=>!e||typeof e=="string"?false:typeof e[Symbol.iterator]=="function",te=e=>!e||typeof e!="object"&&typeof e!="function"?false:typeof e.then=="function",E=(e,n)=>{if(n!=null&&typeof n!="boolean"){if(te(n))throw new Error("Async values are not supported inside reactJsx template results.");if(Array.isArray(n)){n.forEach(r=>E(e,r));return}if(ee(n)){for(let r of n)E(e,r);return}e.push(n);}},S=(e,n)=>I(e,n,r=>b(r,n)),ne=(e,n)=>{let r={};return e.forEach(t=>{if(t.type==="JSXSpreadAttribute"){let s=S(t.argument,n);s&&typeof s=="object"&&!Array.isArray(s)&&Object.assign(r,s);return}let o=T(t.name);if(!t.value){r[o]=true;return}if(t.value.type==="Literal"){r[o]=t.value.value;return}if(t.value.type==="JSXExpressionContainer"){if(t.value.expression.type==="JSXEmptyExpression")return;r[o]=S(t.value.expression,n);}}),r},B=(e,n)=>{let r=[];return e.forEach(t=>{switch(t.type){case "JSXText":{F(t.value,n.placeholders).forEach(s=>E(r,s));break}case "JSXExpressionContainer":{if(t.expression.type==="JSXEmptyExpression")break;E(r,S(t.expression,n));break}case "JSXSpreadChild":{let o=S(t.expression,n);o!=null&&E(r,o);break}case "JSXElement":case "JSXFragment":{r.push(b(t,n));break}}}),r},O=(e,n,r)=>createElement(e,n,...r),re=Symbol.for("react.memo"),oe=Symbol.for("react.forward_ref"),se=Symbol.for("react.lazy"),ae=e=>{if(typeof e=="function")return true;if(typeof e!="object"||e===null)return false;let n=e;return n.$$typeof===re||n.$$typeof===oe||n.$$typeof===se},pe=(e,n)=>{let r=e.openingElement,t=T(r.name),o=n.components.get(t),s=ne(r.attributes,n),a=B(e.children,n);if(o)return O(o,s,a);if(/[A-Z]/.test(t[0]??""))throw new Error(`Unknown component "${t}". Did you interpolate it with the template literal?`);return O(t,s,a)},b=(e,n)=>{if(e.type==="JSXFragment"){let r=B(e.children,n);return createElement(Fragment,null,...r)}return pe(e,n)},ie=(e,...n)=>{let r=P(e,n,{isTagNameBindingValue:ae}),t=parseSync("inline.jsx",r.source,M);if(t.errors.length>0)throw new Error(C("reactJsx",e,r.diagnostics,t.errors[0]));let o=N(t.program),s={source:r.source,placeholders:r.placeholders,components:new Map(r.bindings.map(a=>[a.name,a.value]))};return b(o,s)};export{ie as reactJsx};
|
package/dist/react/react-jsx.js
CHANGED
|
@@ -99,6 +99,21 @@ const evaluateReactJsxChildren = (children, ctx) => {
|
|
|
99
99
|
const createReactElement = (type, props, children) => {
|
|
100
100
|
return createElement(type, props, ...children);
|
|
101
101
|
};
|
|
102
|
+
const reactMemoSymbol = Symbol.for('react.memo');
|
|
103
|
+
const reactForwardRefSymbol = Symbol.for('react.forward_ref');
|
|
104
|
+
const reactLazySymbol = Symbol.for('react.lazy');
|
|
105
|
+
const isReactTagBindingValue = (value) => {
|
|
106
|
+
if (typeof value === 'function') {
|
|
107
|
+
return true;
|
|
108
|
+
}
|
|
109
|
+
if (typeof value !== 'object' || value === null) {
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
const candidate = value;
|
|
113
|
+
return (candidate.$$typeof === reactMemoSymbol ||
|
|
114
|
+
candidate.$$typeof === reactForwardRefSymbol ||
|
|
115
|
+
candidate.$$typeof === reactLazySymbol);
|
|
116
|
+
};
|
|
102
117
|
const evaluateReactJsxElement = (element, ctx) => {
|
|
103
118
|
const opening = element.openingElement;
|
|
104
119
|
const tagName = getIdentifierName(opening.name);
|
|
@@ -121,7 +136,9 @@ const evaluateReactJsxNode = (node, ctx) => {
|
|
|
121
136
|
return evaluateReactJsxElement(node, ctx);
|
|
122
137
|
};
|
|
123
138
|
export const reactJsx = (templates, ...values) => {
|
|
124
|
-
const build = buildTemplate(templates, values
|
|
139
|
+
const build = buildTemplate(templates, values, {
|
|
140
|
+
isTagNameBindingValue: isReactTagBindingValue,
|
|
141
|
+
});
|
|
125
142
|
const result = parseSync('inline.jsx', build.source, parserOptions);
|
|
126
143
|
if (result.errors.length > 0) {
|
|
127
144
|
throw new Error(formatTaggedTemplateParserError('reactJsx', templates, build.diagnostics, result.errors[0]));
|
package/dist/runtime/shared.d.ts
CHANGED
|
@@ -22,6 +22,9 @@ export type TemplateBuildResult<TComponent extends TemplateComponent> = {
|
|
|
22
22
|
bindings: BindingEntry<TComponent>[];
|
|
23
23
|
diagnostics: TemplateDiagnostics;
|
|
24
24
|
};
|
|
25
|
+
export type TemplateBuildOptions<TComponent extends TemplateComponent> = {
|
|
26
|
+
isTagNameBindingValue?: (value: unknown) => value is TComponent;
|
|
27
|
+
};
|
|
25
28
|
export type TemplateContext<TComponent extends TemplateComponent> = {
|
|
26
29
|
source: string;
|
|
27
30
|
placeholders: Map<string, unknown>;
|
|
@@ -40,4 +43,4 @@ export declare const collectPlaceholderNames: <TComponent extends TemplateCompon
|
|
|
40
43
|
export declare const evaluateExpression: <TComponent extends TemplateComponent>(expression: Expression | JSXElement | JSXFragment, ctx: TemplateContext<TComponent>, evaluateJsxNode: (node: JSXElement | JSXFragment) => unknown) => unknown;
|
|
41
44
|
export declare const sanitizeIdentifier: (value: string) => string;
|
|
42
45
|
export declare const ensureBinding: <TComponent extends TemplateComponent>(value: TComponent, bindings: BindingEntry<TComponent>[], bindingLookup: Map<TComponent, BindingEntry<TComponent>>) => BindingEntry<TComponent>;
|
|
43
|
-
export declare const buildTemplate: <TComponent extends TemplateComponent>(strings: TemplateStringsArray, values: unknown[]) => TemplateBuildResult<TComponent>;
|
|
46
|
+
export declare const buildTemplate: <TComponent extends TemplateComponent>(strings: TemplateStringsArray, values: unknown[], options?: TemplateBuildOptions<TComponent>) => TemplateBuildResult<TComponent>;
|
package/dist/runtime/shared.js
CHANGED
|
@@ -157,7 +157,7 @@ export const ensureBinding = (value, bindings, bindingLookup) => {
|
|
|
157
157
|
bindingLookup.set(value, binding);
|
|
158
158
|
return binding;
|
|
159
159
|
};
|
|
160
|
-
export const buildTemplate = (strings, values) => {
|
|
160
|
+
export const buildTemplate = (strings, values, options) => {
|
|
161
161
|
const raw = strings.raw ?? strings;
|
|
162
162
|
const placeholders = new Map();
|
|
163
163
|
const bindings = [];
|
|
@@ -166,19 +166,24 @@ export const buildTemplate = (strings, values) => {
|
|
|
166
166
|
const templateId = invocationCounter++;
|
|
167
167
|
let placeholderIndex = 0;
|
|
168
168
|
const expressionRanges = [];
|
|
169
|
+
const isTagNameBindingValue = options?.isTagNameBindingValue ??
|
|
170
|
+
((value) => typeof value === 'function');
|
|
169
171
|
for (let idx = 0; idx < values.length; idx++) {
|
|
170
172
|
const chunk = raw[idx] ?? '';
|
|
171
173
|
const nextChunk = raw[idx + 1] ?? '';
|
|
172
174
|
const value = values[idx];
|
|
173
175
|
const isTagNamePosition = OPEN_TAG_RE.test(chunk) || CLOSE_TAG_RE.test(chunk);
|
|
174
176
|
let insertion;
|
|
175
|
-
if (isTagNamePosition &&
|
|
177
|
+
if (isTagNamePosition && isTagNameBindingValue(value)) {
|
|
176
178
|
const binding = ensureBinding(value, bindings, bindingLookup);
|
|
177
179
|
insertion = binding.name;
|
|
178
180
|
}
|
|
179
181
|
else if (isTagNamePosition && typeof value === 'string') {
|
|
180
182
|
insertion = value;
|
|
181
183
|
}
|
|
184
|
+
else if (isTagNamePosition) {
|
|
185
|
+
throw new Error('Invalid tag interpolation value. Expected a component, class, or string tag name.');
|
|
186
|
+
}
|
|
182
187
|
else {
|
|
183
188
|
const placeholder = `${PLACEHOLDER_PREFIX}${templateId}_${placeholderIndex++}__`;
|
|
184
189
|
placeholders.set(placeholder, value);
|