@xaendar/compiler 0.9.6 → 0.9.7
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/compiler.js +6 -6
- package/package.json +3 -3
package/dist/compiler.js
CHANGED
|
@@ -9,11 +9,11 @@ import{indent as U,slice as Ht}from"@xaendar/common";var C=(f=>(f[f.Element=0]="
|
|
|
9
9
|
${i}(${s.args?.join(", ")}) {`,...U(["const context = new Context(this, parentContext);"])),u&&o.push(U(u)),o.push(...U([...a.children.map((f,N)=>{let _=this._processNode(f,c,N.toString(),p,d??null);if(_){let{code:S,functionsToProcess:H}=_;return t(H),S}return""}).flat(),s.fn.isForBody?"return { context, update };":"return context;"]),"}")}return o.join(`
|
|
10
10
|
`)}catch(r){let o=r,{start:i,end:s}=o.cause;throw new Error(`[Generator] ${o.message}
|
|
11
11
|
----> ${Ht(this._input,i,s)}`)}}_processNode(n,t,r,o,i){let s=this._states[n.type];if(!s)throw new Error(`No transition function for token type ${C[n.type]}`,{cause:n.span});return s(n,t,r,o,i)}};import{slice as Ne,Stack as Qt}from"@xaendar/common";var T=(l=>(l[l.TEXT=0]="TEXT",l[l.TAG_OPEN_NAME=1]="TAG_OPEN_NAME",l[l.TAG_SELF_CLOSE=2]="TAG_SELF_CLOSE",l[l.TAG_OPEN_END=3]="TAG_OPEN_END",l[l.TAG_CLOSE_NAME=4]="TAG_CLOSE_NAME",l[l.ATTRIBUTE=5]="ATTRIBUTE",l[l.ATTRIBUTE_VALUE=6]="ATTRIBUTE_VALUE",l[l.EVENT=7]="EVENT",l[l.EVENT_HANDLER=8]="EVENT_HANDLER",l[l.EVENT_PARAMETER=9]="EVENT_PARAMETER",l[l.INTERPOLATION_LITERAL=10]="INTERPOLATION_LITERAL",l[l.INTERPOLATION_EXPRESSION=11]="INTERPOLATION_EXPRESSION",l[l.IF=12]="IF",l[l.FOR=13]="FOR",l[l.ELSE=14]="ELSE",l[l.ELSE_IF=15]="ELSE_IF",l[l.SWITCH=16]="SWITCH",l[l.CASE=17]="CASE",l[l.DEFAULT=18]="DEFAULT",l[l.CONDITION=19]="CONDITION",l[l.BLOCK_OPEN=20]="BLOCK_OPEN",l[l.BLOCK_CLOSE=21]="BLOCK_CLOSE",l[l.IMPORT=22]="IMPORT",l[l.IMPORT_PATH=23]="IMPORT_PATH",l[l.EOF=24]="EOF",l))(T||{});function We(e,n){let t=!0,r="",o;for(;t;)e.peek()===34?(e.advance(),t=!1,o={state:"tag-body",tokens:[{type:6,parts:[r]}],popState:!0}):(e.advance(),r=`${r}${e.currentChar.value}`);return o}function Ke(e,n){let t=!0,r="",o;for(;t;)switch(e.peek()){case 32:e.advance(),t=!1,o={state:"tag-body",tokens:[{type:5,parts:[r]}]};break;case 62:case 47:t=!1,o={state:"tag-body",tokens:[{type:5,parts:[r]}]};break;case 61:if(!r)throw new Error("Attribute cannot start with '='");if(e.advance(),e.peek()!==34)throw`Attribute value must start with double quotes '"'`;e.advance(),t=!1,o={state:e.peek()===123?"interpolation":"attribute-value",pushState:!0,tokens:[{type:5,parts:[r]}]};break;case 34:case 39:if(!r)throw new Error(`Attribute cannot start with ' or "`);default:e.advance(),r=`${r}${e.currentChar.value}`}return o}function oe(e,n){if(e.skipSpaces(),e.peek()!==40)throw`Expected '(' but got '${String.fromCharCode(e.peek())}'`;e.advance();let t="",r=1;for(;r>0;)switch(e.peek()){case 40:r++,t=he(e,t);break;case 41:if(r--,!r){e.advance();break}t=he(e,t);break;default:t=he(e,t)}return t}function he(e,n){return e.advance(),`${n}${e.currentChar.value}`}function qe(e,n){let t=oe(e,n);return e.skipSpaces(),{state:e.peekMatch("@case")?"flow-control":"flow-control-block",tokens:[{type:19,parts:[t]}],popState:!0}}function Xe(e,n){return{state:"flow-control-block",tokens:[{type:19,parts:[oe(e,n)]}],popState:!0}}function Qe(e,n){let t=!0,r="",o;for(;t;)switch(e.peek()){case 32:throw"No spaces are allowed in event handler name";case 40:if(!r)throw"Event handler cannot be empty";let i="parameter";e.advance(),e.peekMatch(')"')&&(i="tag-body",e.advance(2)),o={state:i,tokens:[{type:8,parts:[r]}]},t=!1;break;default:e.advance(),r=`${r}${e.currentChar.value}`}return o}function je(e,n){let t=!0,r="",o="",i=e.currentChar.index+1,s={state:"tag-body",tokens:[]};for(;t;)switch(e.peek()){case 91:case 93:case 123:case 125:case 34:case 39:case 40:r=Ee(e,r),o?(o===e.currentChar.value||o==="["&&e.currentChar.value==="]"||o==="{"&&e.currentChar.value==="}")&&(o=""):o=e.currentChar.value;break;case 44:if(o)r=Ee(e,r);else{let a=e.currentChar.index+1;s.tokens.push({type:9,parts:[r],span:{start:i,end:a}}),e.advance(),e.skipSpaces(),r="",i=e.currentChar.index+1}break;case 41:if(e.advance(),e.peek()!==34)throw"Event must be included in Double Quotes";if(e.advance(),!o){let a=e.currentChar.index+1;s.tokens.push({type:9,parts:[r],span:{start:i,end:a}}),t=!1;break}r=`${r}${e.currentChar.value}`;break;default:r=Ee(e,r)}return s}function Ee(e,n){return e.advance(),`${n}${e.currentChar.value}`}function ze(e,n){let t=!0,r="",o;for(e.advance();t;)switch(e.peek()){case 32:throw"No spaces are allowed in event name";case 61:if(!r)throw"Event name cannot be empty";if(e.advance(),e.peek()!==34)throw"Event handler must be included in Double Quotes";e.advance(),o={state:"event-handler",tokens:[{type:7,parts:[r]}]},t=!1;break;default:e.advance(),r=`${r}${e.currentChar.value}`}return o}function Ye(e,n){let t;if(e.advance(),e.peekMatch("for "))e.advance(4),t={state:"flow-control-condition",tokens:[{type:13}],pushState:!0};else if(e.peekMatch("if "))e.advance(2),t={state:"flow-control-condition",tokens:[{type:12}],pushState:!0};else if(e.peekMatch("else if "))e.advance(8),t={state:"flow-control-condition",tokens:[{type:15}],pushState:!0};else if(e.peekMatch("else "))e.advance(5),t={state:"flow-control-block",tokens:[{type:14}]};else if(e.peekMatch("switch "))e.advance(7),t={state:"flow-control-condition",tokens:[{type:16}],pushState:!0};else if(e.peekMatch("case "))e.advance(5),t={state:"case-flow-control-condition",tokens:[{type:17}],pushState:!0};else if(e.peekMatch("default "))e.advance(8),t={state:"flow-control-block",tokens:[{type:18}]};else if(e.peekMatch("import "))e.advance(7),t={state:"import"};else throw"Unknown flow-control keyword";return t}function Ze(e,n){if(e.skipSpaces(),e.peek()!==123)throw`Expected '{' but got '${String.fromCharCode(e.peek())}'`;return e.advance(),{state:"text",tokens:[{type:20}],pushState:!0}}function Je(e,n){let t=!0,r="",o,i=!1;if(e.skipSpaces(),!e.peekMatch("from"))throw"Expected 'from' keyword after import list";switch(e.advance(4),e.skipSpaces(),e.advance(),e.currentChar.code){case 39:i=!0;break;case 34:break;default:throw`Import statement must start with ' or ".
|
|
12
|
-
Found character ${e.currentChar.value}`}let s=i?39:34;for(;t;)e.peek()===s?(e.advance(),t=!1,o={state:"text",tokens:[{type:23,parts:[r]}]}):(e.advance(),r=`${r}${e.currentChar.value}`);return o}function tt(e,n){let t=!0,r="",o=e.currentChar.index+1,i={state:"import-path",tokens:[]};if(e.skipSpaces(),e.advance(),e.currentChar.code!==123)throw"Expected { after @import";for(;t;)switch(e.peek()){case 32:e.skipSpaces(),r&&e.peek()!==44&&e.peek()!==125&&(r=`${r}`);break;case 44:et(i,e,r,o),r="",o=e.currentChar.index+1;break;case 125:et(i,e,r,o),t=!1;break;default:e.advance(),r=`${r}${e.currentChar.value}`}return i}function et(e,n,t,r){let o=n.currentChar.index+1;n.advance(),e.tokens.push({type:22,parts:[t],span:{start:r,end:o}})}function rt(e,n){let t=!0,r="",o=1,i;for(;t;)switch(e.peek()){case 123:o++,r=Ce(e,r);break;case 125:if(o--,o===0){e.advance(),r=r.trimEnd();let s=n.history.pop(),a;switch(s){case"attribute":if(e.peek()!==34)throw`Interpolation must end with double quotes '"', found '${String.fromCharCode(e.peek())}'`;e.advance(),a="tag-body";break;case"text":a="text";break;default:throw`Unexpected state '${s}' after interpolation expression`}i={state:a,tokens:[{type:11,parts:[r]}],popState:!0},t=!1}else r=Ce(e,r);break;default:r=Ce(e,r)}return i}function Ce(e,n){return e.advance(1),`${n}${e.currentChar.value}`}function ot(e,n){let t=!0,r="`",o;for(e.advance();t;)switch(e.peek()){case 96:if(r=nt(e,r),e.peek()===125){e.advance();let i=n.history.pop(),s;switch(i){case"attribute":if(e.peek()!==34)throw`Attribute interpolation expression must end with double quotes '"', found '${String.fromCharCode(e.peek())}'`;e.advance(),s="tag-body";break;case"text":s="text";break;default:throw`Unexpected state '${i}' after interpolation literal`}o={state:s,tokens:[{type:10,parts:[r]}],popState:!0},t=!1}else r=`${r}${e.currentChar.value}`;break;default:r=nt(e,r)}return o}function nt(e,n){return e.advance(),`${n}${e.currentChar.value}`}function it(e,n){return e.advance(),e.skipSpaces(),e.peek()===96?{state:"interpolation-literal"}:{state:"interpolation-expression"}}function st(e,n){let t=!0,r;for(;t;)switch(e.peek()){case 64:r={state:"event"},t=!1;break;case 32:e.skipSpaces();break;case 62:case 47:r={state:"tag-open-end"},t=!1;break;default:r={state:"attribute"},t=!1}return r}function at(e,n){let t=!0,r="",o;for(e.advance(2),e.skipSpaces();t;)switch(e.peek()){case 62:if(!r)throw"Tag close name cannot be empty";e.advance(),o={state:"text",tokens:[{type:4,parts:[r]}]},t=!1;break;case 32:throw"Tag close name cannot contain spaces";default:e.advance(),r=`${r}${e.currentChar.value}`}return o}function pt(e,n){let t;if(e.peek()===62)e.advance(),t={state:"text",tokens:[{type:3,parts:[]}]};else{e.advance();let r=e.peek();if(r===62)e.advance(),t={state:"text",tokens:[{type:2,parts:[]}]};else throw`Unexpected character '${String.fromCharCode(r)}' after '/': expected '>' to close self-closing tag`}return t}function ct(e,n){let t=!0,r="",o;for(e.advance(),e.skipSpaces();t;)switch(e.peek()){case 32:case 47:case 62:if(!r)throw"Tag name cannot be empty ";o={state:"tag-body",tokens:[{type:1,parts:[r]}]},t=!1;break;default:e.advance(),r=`${r}${e.currentChar.value}`}return o}function mt(e){return/\S/.test(e)}function lt(e,n){let t=!0,r="",o;for(;t;)switch(e.peek()){case 60:o={state:e.peek(1,{offset:1})===47?"tag-close":"tag-open-name"},t=!1;break;case 123:o={state:"interpolation",pushState:!0},t=!1;break;case 64:o={state:"flow-control"},t=!1;break;case 125:n.history[n.history.length-1]==="flow-control-block"?(e.advance(),o={state:"text",tokens:[{type:21}],popState:!0},t=!1):(e.advance(),r=`${r}${e.currentChar.value}`);break;case 10:case 13:e.advance();break;default:e.advance(),r=`${r}${e.currentChar.value}`}return o.tokens??=mt(r)?[{type:0,parts:[r]}]:void 0,o}import{slice as Xt}from"@xaendar/common";var
|
|
13
|
-
`&&n.push(r+1);return n}};var ae=class extends
|
|
14
|
-
---> ${N}${_}${S}`}}return this._tokens}withTokenSpans(n,t){return n.map(r=>({...r,span:r.span??t}))}};import{slice as lr}from"@xaendar/common";var ce=class extends
|
|
15
|
-
`,"");return{node:o}}function ft(e,n,t){if(!Zt(e))throw Jt(e);if(Yt(e,r=>ft(r,n,t)),t.length)throw t[0].message}function Zt(e){switch(e.kind){case m.StringLiteral:case m.NumericLiteral:case m.BigIntLiteral:case m.TrueKeyword:case m.FalseKeyword:case m.NullKeyword:case m.UndefinedKeyword:case m.Identifier:case m.PropertyAccessExpression:case m.ElementAccessExpression:case m.CallExpression:case m.BinaryExpression:case m.EqualsEqualsToken:case m.EqualsEqualsEqualsToken:case m.ExclamationEqualsToken:case m.ExclamationEqualsEqualsToken:case m.LessThanToken:case m.LessThanEqualsToken:case m.GreaterThanToken:case m.GreaterThanEqualsToken:case m.PlusToken:case m.MinusToken:case m.AsteriskToken:case m.SlashToken:case m.PercentToken:case m.AsteriskAsteriskToken:case m.AmpersandAmpersandToken:case m.BarBarToken:case m.QuestionQuestionToken:case m.QuestionDotToken:case m.QuestionToken:case m.ColonToken:case m.AmpersandToken:case m.BarToken:case m.CaretToken:case m.LessThanLessThanToken:case m.GreaterThanGreaterThanToken:case m.GreaterThanGreaterThanGreaterThanToken:case m.InstanceOfKeyword:case m.InKeyword:case m.PrefixUnaryExpression:case m.PostfixUnaryExpression:case m.TypeOfExpression:case m.VoidExpression:case m.ConditionalExpression:case m.ParenthesizedExpression:case m.TemplateExpression:case m.NoSubstitutionTemplateLiteral:case m.TemplateHead:case m.TemplateMiddle:case m.TemplateTail:case m.TemplateSpan:case m.ArrayLiteralExpression:case m.ObjectLiteralExpression:case m.PropertyAssignment:case m.ShorthandPropertyAssignment:case m.SpreadAssignment:case m.SpreadElement:case m.TaggedTemplateExpression:case m.SyntaxList:return!0;default:return!1}}function Jt(e){switch(e.kind){case m.AwaitExpression:return"'await' is not allowed inside template expressions.";case m.YieldExpression:return"'yield' is not allowed inside template expressions.";case m.NewExpression:return"'new' is not allowed inside template expressions.";case m.ArrowFunction:case m.FunctionExpression:return"Function expressions are not allowed inside template expressions.";case m.BinaryExpression:return er(e.operatorToken.kind)?"Assignments are not allowed inside template expressions. Use @const to declare local template variables instead.":`'${m[e.kind]}' is not allowed inside template expressions.`;default:return`'${m[e.kind]}' is not allowed inside template expressions.`}}function er(e){return e>=m.FirstAssignment&&e<=m.LastAssignment}function Z(e,n,t){let r=t.span.start;return e.advance(),{type:4,expression:R(t.parts[0]).node,span:{start:r,end:e.getCurrentToken().value.span.end}}}function ut(e,n,t){let r=t.span.start;e.advance();let o=t.parts[0],i=e.peek();if(i.type===5||i.type===4||i.type===7)return{type:1,name:o,value:"true",span:{start:r,end:e.getCurrentToken().value.span.end}};if(i.type===11||i.type===10){let s=Z(e,n,i);return{type:1,name:o,value:s,span:{start:r,end:e.getCurrentToken().value.span.end}}}if(i.type!==6)throw`Attribute value missing for ${o} in: ${o}`;return e.advance(),{type:1,name:o,value:i.parts[0],span:{start:r,end:e.getCurrentToken().value.span.end}}}function dt(e,n,t){let r=t.span.start;e.advance();let o=t.parts[0],i=e.peek();if(i.type!==8)throw`Invalid event format for ${o}`;e.advance();let s=e.getCurrentToken().value.span.end,a=new Array;for(;e.peek().type===9;)e.advance(),a.push(R(e.getCurrentToken().value.parts[0]).node),s=e.getCurrentToken().value.span.end;return{type:2,name:o,handler:i.parts[0],parameters:a,span:{start:r,end:s}}}function Tt(e,n,t){e.advance();let r=t.parts[0],o=new Array,i=new Array,s=!0;for(;s;){let p=e.peek();switch(p.type){case 5:o.push(ut(e,n,p));break;case 7:i.push(dt(e,n,p));break;default:s=!1}}let a=e.peek().type;switch(a){case 3:e.advance();break;case 2:return e.advance(),{type:0,tagName:r,attributes:o,events:i,children:[]};default:throw new Error(`Unexpected State ${a}`)}let c=new Array;for(;!tr(e,r);){let p=n();p&&c.push(p)}return e.advance(),{type:0,tagName:r,attributes:o,events:i,children:c}}function tr(e,n){let t=e.peek();switch(t.type){case 24:throw new Error(`Expected closing tag ${n}`);case 4:return t.parts[0]===n;default:return!1}}import{slice as J}from"@xaendar/common";import{createSourceFile as rr,isExpressionStatement as nr,isIdentifier as or,ScriptTarget as ir}from"typescript";function B(e,n){let t=new Array;for(;e.peek().type!==21;){let r=n();r&&t.push(r)}return e.advance(),t}function ht(e,n,t){e.advance();let r=e.peek();if(r.type!==19)throw`Expected CONDITION after FOR, got ${T[r.type]}`;let o=sr(r.parts[0],0);e.advance(2);let i=B(e,n);return{type:8,...o,children:i}}function sr(e,n){let t=pr(e);if(t.length<2)throw'@for requires at least "item of iterable; track expr".';let r=t[0].trim(),o=r.indexOf(" of ");if(o===-1)throw'@for expression must be in the form "item of iterable".';let i=J(r,0,o).trim(),s=J(r,o+4).trim();if(!xt(i))throw`'${i}' is not a valid item alias.`;let a=R(s),c=t[1].trim();if(!c.startsWith("track "))throw'Second section of @for must start with "track".';let p=J(c,6).trim(),u=R(p),d=new Map;if(t.length>=3&&t[2]!==void 0){let f=t[2].trim(),N=n+e.indexOf(t[2]);ar(f,N,d)}return{itemAlias:i,iterableExpression:a.node,iterableSource:s,trackExpression:u.node,trackSource:p,implicitAliases:d}}function ar(e,n,t){let r=e.split(","),o=0,i=new Set(["$index","$last","$first","$even","$odd"]);for(let s=0;s<r.length;s++){let a=r[s],c=a.trim(),p=c.indexOf("=");if(p===-1)throw`Invalid alias declaration '${c}'. Expected '$implicit = alias'.`;o+=a.length+1;let u=J(c,0,p).trim(),d=J(c,p+1).trim();if(!(N=>i.has(N))(d))throw`'${d}' is not a known implicit variable. Known variables: ${[...i].join(", ")}.`;if(o+=a.length+1,!xt(u))throw`'${u}' is not a valid alias identifier.`;if(o+=a.length+1,t.has(d))throw`'${d}' is already aliased in this @for expression.`;t.set(d,u),o+=a.length+1}}function pr(e){let n=new Array,t="",r=0,o;for(let i=0;i<e.length;i++){let s=e[i];if(!(!t&&s===" ")){if(o){t+=s,s===o&&e[i-1]!=="\\"&&(o=null);continue}if(s==='"'||s==="'"||s==="`"){o=s,t+=s;continue}if(s==="("||s==="["||s==="{"){r++,t+=s;continue}if(s===")"||s==="]"||s==="}"){r--,t+=s;continue}if(s===";"&&r===0){n.push(t),t="";continue}t+=s}}return t.trim().length&&n.push(t),n}function xt(e){if(!e.length)return!1;let t=rr("__id.ts",e,ir.ESNext,!1).statements[0];return!!t&&nr(t)&&or(t.expression)&&t.expression.text===e}function Et(e,n,t){return Ct(e,n,t)}function cr(e,n,t){switch(t.type){case 15:return Ct(e,n,t);case 14:let r=t.span.start;e.advance(2);let o=B(e,n);return{type:6,children:o,span:{start:r,end:e.getCurrentToken().value.span.end}}}}function Ct(e,n,t){let r=t.span.start;e.advance();let o=e.peek();if(o.type!==19)throw`Expected CONDITION after ${T[t.type]}, got ${T[o.type]}`;e.advance(2);let i=o.parts[0],s=R(i),a=B(e,n),c=null,p=e.peek();return(p.type===15||p.type===14)&&(c=cr(e,n,p)),{type:t.type===12?5:7,condition:i,conditionNode:s.node,children:a,alternate:c,span:{start:r,end:e.getCurrentToken().value.span.end}}}function kt(e,n,t){let r=new Array;for(;e.peek().type===22;){e.advance();let o=e.getCurrentToken().value.parts[0],i=mr(o);r.push(i)}return e.advance(),{type:11,specifiers:r,path:e.getCurrentToken().value.parts[0]}}function mr(e){let n=e.trim(),t=n.match(/^\*\s+as\s+([A-Za-z_$][\w$]*)$/);if(t)return{imported:"*",local:t[1]};let r=n.match(/^default\s+as\s+([A-Za-z_$][\w$]*)$/);if(r)return{imported:"default",local:r[1]};let o=n.match(/^([A-Za-z_$][\w$]*)(?:\s+as\s+([A-Za-z_$][\w$]*))?$/);if(o)return{imported:o[1],local:o[2]??o[1]};throw`Invalid import specifier "${e}".`}function gt(e,n,t){e.advance();let r=e.peek();if(r.type!==19)throw`Expected CONDITION after SWITCH, got ${T[r.type]}`;let o=R(r.parts[0]).node;e.advance(2);let i=new Array;for(;e.peek().type!==21;){let s=e.peek();switch(s.type){case 17:let a=s.span.start,c=new Array;do{e.advance();let f=e.peek();if(f.type!==19)throw"Expected CONDITION after CASE";c.push(f.parts[0]),e.advance()}while(e.peek().type!==20);e.advance();let p=B(e,n);i.push({type:10,condition:c,children:p,span:{start:a,end:e.getCurrentToken().value.span.end}});break;case 18:let u=s.span.start;e.advance(2);let d=B(e,n);i.push({type:10,condition:null,children:d,span:{start:u,end:e.getCurrentToken().value.span.end}});break}}return e.advance(),{type:9,expression:o,children:i}}function Nt(e,n,t){return e.advance(),{type:3,value:t.parts[0]}}var me=class{constructor(n,t){this._input=n;this._cursor=new ce(this._input,t)}_input;_cursor;_states={0:Nt,11:Z,10:Z,1:Tt,12:Et,13:ht,16:gt,22:kt};parse(){let n=new Array;for(;this._cursor.peek().type!==24;){let t=this.parseNode();t&&n.push(t)}return n}parseNode(){let n=this._cursor.peek();if(n.type===24)return;let t=n.span.start;try{let r=this._states[n.type];if(!r)throw`No transition function for token type ${T[n.type]}`;let o=r(this._cursor,this.parseNode.bind(this),n),i=this._cursor.getCurrentToken().value,s="span"in i?i.span.end:n.span.end;return{...o,span:o.span??{start:t,end:s}}}catch(r){let o=r instanceof Error?r.message:r,i=this._cursor.peek(),s=i.type!==24?i.span.end:void 0;throw`${this._cursor.getPositionFromCharacterIndex(t+1)} ${o}
|
|
12
|
+
Found character ${e.currentChar.value}`}let s=i?39:34;for(;t;)e.peek()===s?(e.advance(),t=!1,o={state:"text",tokens:[{type:23,parts:[r]}]}):(e.advance(),r=`${r}${e.currentChar.value}`);return o}function tt(e,n){let t=!0,r="",o=e.currentChar.index+1,i={state:"import-path",tokens:[]};if(e.skipSpaces(),e.advance(),e.currentChar.code!==123)throw"Expected { after @import";for(;t;)switch(e.peek()){case 32:e.skipSpaces(),r&&e.peek()!==44&&e.peek()!==125&&(r=`${r}`);break;case 44:et(i,e,r,o),r="",o=e.currentChar.index+1;break;case 125:et(i,e,r,o),t=!1;break;default:e.advance(),r=`${r}${e.currentChar.value}`}return i}function et(e,n,t,r){let o=n.currentChar.index+1;n.advance(),e.tokens.push({type:22,parts:[t],span:{start:r,end:o}})}function rt(e,n){let t=!0,r="",o=1,i;for(;t;)switch(e.peek()){case 123:o++,r=Ce(e,r);break;case 125:if(o--,o===0){e.advance(),r=r.trimEnd();let s=n.history.pop(),a;switch(s){case"attribute":if(e.peek()!==34)throw`Interpolation must end with double quotes '"', found '${String.fromCharCode(e.peek())}'`;e.advance(),a="tag-body";break;case"text":a="text";break;default:throw`Unexpected state '${s}' after interpolation expression`}i={state:a,tokens:[{type:11,parts:[r]}],popState:!0},t=!1}else r=Ce(e,r);break;default:r=Ce(e,r)}return i}function Ce(e,n){return e.advance(1),`${n}${e.currentChar.value}`}function ot(e,n){let t=!0,r="`",o;for(e.advance();t;)switch(e.peek()){case 96:if(r=nt(e,r),e.peek()===125){e.advance();let i=n.history.pop(),s;switch(i){case"attribute":if(e.peek()!==34)throw`Attribute interpolation expression must end with double quotes '"', found '${String.fromCharCode(e.peek())}'`;e.advance(),s="tag-body";break;case"text":s="text";break;default:throw`Unexpected state '${i}' after interpolation literal`}o={state:s,tokens:[{type:10,parts:[r]}],popState:!0},t=!1}else r=`${r}${e.currentChar.value}`;break;default:r=nt(e,r)}return o}function nt(e,n){return e.advance(),`${n}${e.currentChar.value}`}function it(e,n){return e.advance(),e.skipSpaces(),e.peek()===96?{state:"interpolation-literal"}:{state:"interpolation-expression"}}function st(e,n){let t=!0,r;for(;t;)switch(e.peek()){case 64:r={state:"event"},t=!1;break;case 32:e.skipSpaces();break;case 62:case 47:r={state:"tag-open-end"},t=!1;break;default:r={state:"attribute"},t=!1}return r}function at(e,n){let t=!0,r="",o;for(e.advance(2),e.skipSpaces();t;)switch(e.peek()){case 62:if(!r)throw"Tag close name cannot be empty";e.advance(),o={state:"text",tokens:[{type:4,parts:[r]}]},t=!1;break;case 32:throw"Tag close name cannot contain spaces";default:e.advance(),r=`${r}${e.currentChar.value}`}return o}function pt(e,n){let t;if(e.peek()===62)e.advance(),t={state:"text",tokens:[{type:3,parts:[]}]};else{e.advance();let r=e.peek();if(r===62)e.advance(),t={state:"text",tokens:[{type:2,parts:[]}]};else throw`Unexpected character '${String.fromCharCode(r)}' after '/': expected '>' to close self-closing tag`}return t}function ct(e,n){let t=!0,r="",o;for(e.advance(),e.skipSpaces();t;)switch(e.peek()){case 32:case 47:case 62:if(!r)throw"Tag name cannot be empty ";o={state:"tag-body",tokens:[{type:1,parts:[r]}]},t=!1;break;default:e.advance(),r=`${r}${e.currentChar.value}`}return o}function mt(e){return/\S/.test(e)}function lt(e,n){let t=!0,r="",o;for(;t;)switch(e.peek()){case 60:o={state:e.peek(1,{offset:1})===47?"tag-close":"tag-open-name"},t=!1;break;case 123:o={state:"interpolation",pushState:!0},t=!1;break;case 64:o={state:"flow-control"},t=!1;break;case 125:n.history[n.history.length-1]==="flow-control-block"?(e.advance(),o={state:"text",tokens:[{type:21}],popState:!0},t=!1):(e.advance(),r=`${r}${e.currentChar.value}`);break;case 10:case 13:e.advance();break;default:e.advance(),r=`${r}${e.currentChar.value}`}return o.tokens??=mt(r)?[{type:0,parts:[r]}]:void 0,o}import{slice as Xt}from"@xaendar/common";var M=class{constructor(n){this.input=n}input;getPositionFromCharacterIndex(n){let t=this.getLineStarts(),r=0,o=t.length-1;for(;r<o;){let a=Math.ceil((r+o)/2);t[a]<=n?r=a:o=a-1}let i=r,s=n-t[i];return`[Ln ${i}, Col ${s}]`}getLineStarts(){let n=[0],t=this.input;for(let r=0;r<t.length;r++)t[r]===`
|
|
13
|
+
`&&n.push(r+1);return n}};var ae=class extends M{_currentChar={code:0,index:-1,value:""};get currentChar(){return this._currentChar}_peekCache=new Map;constructor(n){super(n)}advance(n=1){if(n<1)throw`${n} is not a valid value. Please enter a number equal or greater than 1`;let t=this._currentChar.index+n;if(t>=this.input.length)this._currentChar.code=0,this._currentChar.index=-1,this._currentChar.value="",this.throwEOFError();else{for(let r=this._currentChar.index+1;r<=t;r++)this._peekCache.delete(r);this._currentChar.index=t,this._currentChar.value=this.input[t],this._currentChar.code=this.input.charCodeAt(t)}}peekMatch(n,t){if(typeof n=="string"){let i=this.peek(n.length);for(let s=0;s<n.length;s++)if(i[s]!==n.charCodeAt(s))return!1;return!0}let r=this._currentChar.index+1,o=Xt(this.input,r,r+t);return n.test(o)}peek(n,t){let r=this._peekCache,o=typeof n=="number"?n:1,i=(typeof n=="object"?n:t)?.offset??0;return o===1?this.peekOneChar(this._currentChar.index+i+1,r):this.peekMany(o+i,r)}skipSpaces(){for(;this.peek()===32||this.peek()===10||this.peek()===13;)this.advance()}peekMany(n,t){let r=new Array,o=this._currentChar.index+1;for(let i=o;i<o+n;i++)r.push(this.peekOneChar(i,t));return r}peekOneChar(n,t){if(t.has(n))return t.get(n);n>=this.input.length&&this.throwEOFError();let r=this.input.charCodeAt(n);return t.set(n,r),r}throwEOFError(){throw new Error("",{cause:0})}};var pe=class{constructor(n){this._input=n;this._cursor=new ae(this._input)}_input;_cursor;_state="text";_stack=new Qt;_tokens=new Array;_states={text:lt,"tag-open-name":ct,"tag-body":st,"tag-open-end":pt,"tag-close":at,attribute:Ke,"attribute-value":We,event:ze,"event-handler":Qe,parameter:je,"flow-control":Ye,"flow-control-condition":Xe,"case-flow-control-condition":qe,"flow-control-block":Ze,interpolation:it,"interpolation-expression":rt,"interpolation-literal":ot,import:tt,"import-path":Je};tokenize(){let n=!1,t=this._cursor,r=-1;for(;!n;)try{r=t.currentChar.index+1;let o=this._states[this._state],{state:i,tokens:s,popState:a,pushState:c}=o(t,{history:this._stack.values,tokens:[...this._tokens]});if(s?.length){let p=t.currentChar.index+1;this._tokens.push(...this.withTokenSpans(s,{start:r,end:p}))}c&&this._stack.push(this._state),a&&this._stack.pop(),this._state=i}catch(o){let i=o instanceof Error;if(i&&o.cause===0)n=!0;else{let a=r,c=r,p=!1;for(;!p;){let v=this._input.charCodeAt(c);v===10||v===13||a-c>15&&v===32?(a=c+1,p=!0):c--}let u=t.currentChar.index,d=u<=r?u+2:u+1,f=d;for(c=f,p=!1;!p;){let v=this._input.charCodeAt(c);v===10||v===13||c-f>15&&v===32?(f=c-1,p=!0):c++}let N=Ne(this._input,a,r).replace(/^\s+/,""),_=`\x1B[4m${Ne(this._input,r,d)}\x1B[0m`,S=Ne(this._input,d+1,f).replace(/\s+$/,""),H=i?o.message:o;throw`${t.getPositionFromCharacterIndex(d+1)} - ${H}
|
|
14
|
+
---> ${N}${_}${S}`}}return this._tokens}withTokenSpans(n,t){return n.map(r=>({...r,span:r.span??t}))}};import{slice as lr}from"@xaendar/common";var ce=class extends M{constructor(t,r){super(t);this._tokens=r}_tokens;_currentToken={value:{type:24},index:-1};getCurrentToken(){return this._currentToken}advance(t=1){if(t<1)throw`${t} is not a valid value. Please enter a number equal or greater than 1`;let r=this._currentToken.index+t;r>=this._tokens.length?(this._currentToken.value={type:24},this._currentToken.index=-1):(this._currentToken.index=r,this._currentToken.value=this._tokens[r])}peek(t,r){let o=typeof t=="number"?t:1,i=(typeof t=="object"?t:r)?.offset??0;return o===1?this.peekOneToken(this._currentToken.index+i+1):this.peekMany(o+i)}peekMany(t){let r=new Array,o=this._currentToken.index+1;for(let i=o;i<o+t;i++)r.push(this.peekOneToken(i));return r}peekOneToken(t){return t<this._tokens.length?this._tokens[t]:{type:24}}};import{ScriptTarget as jt,SyntaxKind as m,createSourceFile as zt,forEachChild as Yt}from"typescript";function R(e){let n="const x = ",o=zt("expression.ts",`${n}${e}`,jt.ESNext,!0).statements[0].declarationList.declarations[0].initializer,i=new Array;if(ft(o,n.length,i),i.length)throw i.reduce((s,a)=>`${s}${a.message}
|
|
15
|
+
`,"");return{node:o}}function ft(e,n,t){if(!Zt(e))throw Jt(e);if(Yt(e,r=>ft(r,n,t)),t.length)throw t[0].message}function Zt(e){switch(e.kind){case m.StringLiteral:case m.NumericLiteral:case m.BigIntLiteral:case m.TrueKeyword:case m.FalseKeyword:case m.NullKeyword:case m.UndefinedKeyword:case m.Identifier:case m.PropertyAccessExpression:case m.ElementAccessExpression:case m.CallExpression:case m.BinaryExpression:case m.EqualsEqualsToken:case m.EqualsEqualsEqualsToken:case m.ExclamationEqualsToken:case m.ExclamationEqualsEqualsToken:case m.LessThanToken:case m.LessThanEqualsToken:case m.GreaterThanToken:case m.GreaterThanEqualsToken:case m.PlusToken:case m.MinusToken:case m.AsteriskToken:case m.SlashToken:case m.PercentToken:case m.AsteriskAsteriskToken:case m.AmpersandAmpersandToken:case m.BarBarToken:case m.QuestionQuestionToken:case m.QuestionDotToken:case m.QuestionToken:case m.ColonToken:case m.AmpersandToken:case m.BarToken:case m.CaretToken:case m.LessThanLessThanToken:case m.GreaterThanGreaterThanToken:case m.GreaterThanGreaterThanGreaterThanToken:case m.InstanceOfKeyword:case m.InKeyword:case m.PrefixUnaryExpression:case m.PostfixUnaryExpression:case m.TypeOfExpression:case m.VoidExpression:case m.ConditionalExpression:case m.ParenthesizedExpression:case m.TemplateExpression:case m.NoSubstitutionTemplateLiteral:case m.TemplateHead:case m.TemplateMiddle:case m.TemplateTail:case m.TemplateSpan:case m.ArrayLiteralExpression:case m.ObjectLiteralExpression:case m.PropertyAssignment:case m.ShorthandPropertyAssignment:case m.SpreadAssignment:case m.SpreadElement:case m.TaggedTemplateExpression:case m.SyntaxList:return!0;default:return!1}}function Jt(e){switch(e.kind){case m.AwaitExpression:return"'await' is not allowed inside template expressions.";case m.YieldExpression:return"'yield' is not allowed inside template expressions.";case m.NewExpression:return"'new' is not allowed inside template expressions.";case m.ArrowFunction:case m.FunctionExpression:return"Function expressions are not allowed inside template expressions.";case m.BinaryExpression:return er(e.operatorToken.kind)?"Assignments are not allowed inside template expressions. Use @const to declare local template variables instead.":`'${m[e.kind]}' is not allowed inside template expressions.`;default:return`'${m[e.kind]}' is not allowed inside template expressions.`}}function er(e){return e>=m.FirstAssignment&&e<=m.LastAssignment}function Z(e,n,t){let r=t.span.start;return e.advance(),{type:4,expression:R(t.parts[0]).node,span:{start:r,end:e.getCurrentToken().value.span.end}}}function ut(e,n,t){let r=t.span.start;e.advance();let o=t.parts[0],i=e.peek();if(i.type===5||i.type===4||i.type===7)return{type:1,name:o,value:"true",span:{start:r,end:e.getCurrentToken().value.span.end}};if(i.type===11||i.type===10){let s=Z(e,n,i);return{type:1,name:o,value:s,span:{start:r,end:e.getCurrentToken().value.span.end}}}if(i.type!==6)throw`Attribute value missing for ${o} in: ${o}`;return e.advance(),{type:1,name:o,value:i.parts[0],span:{start:r,end:e.getCurrentToken().value.span.end}}}function dt(e,n,t){let r=t.span.start;e.advance();let o=t.parts[0],i=e.peek();if(i.type!==8)throw`Invalid event format for ${o}`;e.advance();let s=e.getCurrentToken().value.span.end,a=new Array;for(;e.peek().type===9;)e.advance(),a.push(R(e.getCurrentToken().value.parts[0]).node),s=e.getCurrentToken().value.span.end;return{type:2,name:o,handler:i.parts[0],parameters:a,span:{start:r,end:s}}}function Tt(e,n,t){e.advance();let r=t.parts[0],o=new Array,i=new Array,s=!0;for(;s;){let p=e.peek();switch(p.type){case 5:o.push(ut(e,n,p));break;case 7:i.push(dt(e,n,p));break;default:s=!1}}let a=e.peek().type;switch(a){case 3:e.advance();break;case 2:return e.advance(),{type:0,tagName:r,attributes:o,events:i,children:[]};default:throw new Error(`Unexpected State ${a}`)}let c=new Array;for(;!tr(e,r);){let p=n();p&&c.push(p)}return e.advance(),{type:0,tagName:r,attributes:o,events:i,children:c}}function tr(e,n){let t=e.peek();switch(t.type){case 24:throw new Error(`Expected closing tag ${n}`);case 4:return t.parts[0]===n;default:return!1}}import{slice as J}from"@xaendar/common";import{createSourceFile as rr,isExpressionStatement as nr,isIdentifier as or,ScriptTarget as ir}from"typescript";function B(e,n){let t=new Array;for(;e.peek().type!==21;){let r=n();r&&t.push(r)}return e.advance(),t}function ht(e,n,t){e.advance();let r=e.peek();if(r.type!==19)throw`Expected CONDITION after FOR, got ${T[r.type]}`;let o=sr(r.parts[0],0);e.advance(2);let i=B(e,n);return{type:8,...o,children:i}}function sr(e,n){let t=pr(e);if(t.length<2)throw'@for requires at least "item of iterable; track expr".';let r=t[0].trim(),o=r.indexOf(" of ");if(o===-1)throw'@for expression must be in the form "item of iterable".';let i=J(r,0,o).trim(),s=J(r,o+4).trim();if(!xt(i))throw`'${i}' is not a valid item alias.`;let a=R(s),c=t[1].trim();if(!c.startsWith("track "))throw'Second section of @for must start with "track".';let p=J(c,6).trim(),u=R(p),d=new Map;if(t.length>=3&&t[2]!==void 0){let f=t[2].trim(),N=n+e.indexOf(t[2]);ar(f,N,d)}return{itemAlias:i,iterableExpression:a.node,iterableSource:s,trackExpression:u.node,trackSource:p,implicitAliases:d}}function ar(e,n,t){let r=e.split(","),o=0,i=new Set(["$index","$last","$first","$even","$odd"]);for(let s=0;s<r.length;s++){let a=r[s],c=a.trim(),p=c.indexOf("=");if(p===-1)throw`Invalid alias declaration '${c}'. Expected '$implicit = alias'.`;o+=a.length+1;let u=J(c,0,p).trim(),d=J(c,p+1).trim();if(!(N=>i.has(N))(d))throw`'${d}' is not a known implicit variable. Known variables: ${[...i].join(", ")}.`;if(o+=a.length+1,!xt(u))throw`'${u}' is not a valid alias identifier.`;if(o+=a.length+1,t.has(d))throw`'${d}' is already aliased in this @for expression.`;t.set(d,u),o+=a.length+1}}function pr(e){let n=new Array,t="",r=0,o;for(let i=0;i<e.length;i++){let s=e[i];if(!(!t&&s===" ")){if(o){t+=s,s===o&&e[i-1]!=="\\"&&(o=null);continue}if(s==='"'||s==="'"||s==="`"){o=s,t+=s;continue}if(s==="("||s==="["||s==="{"){r++,t+=s;continue}if(s===")"||s==="]"||s==="}"){r--,t+=s;continue}if(s===";"&&r===0){n.push(t),t="";continue}t+=s}}return t.trim().length&&n.push(t),n}function xt(e){if(!e.length)return!1;let t=rr("__id.ts",e,ir.ESNext,!1).statements[0];return!!t&&nr(t)&&or(t.expression)&&t.expression.text===e}function Et(e,n,t){return Ct(e,n,t)}function cr(e,n,t){switch(t.type){case 15:return Ct(e,n,t);case 14:let r=t.span.start;e.advance(2);let o=B(e,n);return{type:6,children:o,span:{start:r,end:e.getCurrentToken().value.span.end}}}}function Ct(e,n,t){let r=t.span.start;e.advance();let o=e.peek();if(o.type!==19)throw`Expected CONDITION after ${T[t.type]}, got ${T[o.type]}`;e.advance(2);let i=o.parts[0],s=R(i),a=B(e,n),c=null,p=e.peek();return(p.type===15||p.type===14)&&(c=cr(e,n,p)),{type:t.type===12?5:7,condition:i,conditionNode:s.node,children:a,alternate:c,span:{start:r,end:e.getCurrentToken().value.span.end}}}function kt(e,n,t){let r=new Array;for(;e.peek().type===22;){e.advance();let o=e.getCurrentToken().value.parts[0],i=mr(o);r.push(i)}return e.advance(),{type:11,specifiers:r,path:e.getCurrentToken().value.parts[0]}}function mr(e){let n=e.trim(),t=n.match(/^\*\s+as\s+([A-Za-z_$][\w$]*)$/);if(t)return{imported:"*",local:t[1]};let r=n.match(/^default\s+as\s+([A-Za-z_$][\w$]*)$/);if(r)return{imported:"default",local:r[1]};let o=n.match(/^([A-Za-z_$][\w$]*)(?:\s+as\s+([A-Za-z_$][\w$]*))?$/);if(o)return{imported:o[1],local:o[2]??o[1]};throw`Invalid import specifier "${e}".`}function gt(e,n,t){e.advance();let r=e.peek();if(r.type!==19)throw`Expected CONDITION after SWITCH, got ${T[r.type]}`;let o=R(r.parts[0]).node;e.advance(2);let i=new Array;for(;e.peek().type!==21;){let s=e.peek();switch(s.type){case 17:let a=s.span.start,c=new Array;do{e.advance();let f=e.peek();if(f.type!==19)throw"Expected CONDITION after CASE";c.push(f.parts[0]),e.advance()}while(e.peek().type!==20);e.advance();let p=B(e,n);i.push({type:10,condition:c,children:p,span:{start:a,end:e.getCurrentToken().value.span.end}});break;case 18:let u=s.span.start;e.advance(2);let d=B(e,n);i.push({type:10,condition:null,children:d,span:{start:u,end:e.getCurrentToken().value.span.end}});break}}return e.advance(),{type:9,expression:o,children:i}}function Nt(e,n,t){return e.advance(),{type:3,value:t.parts[0]}}var me=class{constructor(n,t){this._input=n;this._cursor=new ce(this._input,t)}_input;_cursor;_states={0:Nt,11:Z,10:Z,1:Tt,12:Et,13:ht,16:gt,22:kt};parse(){let n=new Array;for(;this._cursor.peek().type!==24;){let t=this.parseNode();t&&n.push(t)}return n}parseNode(){let n=this._cursor.peek();if(n.type===24)return;let t=n.span.start;try{let r=this._states[n.type];if(!r)throw`No transition function for token type ${T[n.type]}`;let o=r(this._cursor,this.parseNode.bind(this),n),i=this._cursor.getCurrentToken().value,s="span"in i?i.span.end:n.span.end;return{...o,span:o.span??{start:t,end:s}}}catch(r){let o=r instanceof Error?r.message:r,i=this._cursor.peek(),s=i.type!==24?i.span.end:void 0;throw`${this._cursor.getPositionFromCharacterIndex(t+1)} - ${o}
|
|
16
16
|
---> ${lr(this._input,t,s)}`}}};import{slice as wr}from"@xaendar/common";var j=class extends g{_imports=new Array;addImport(n){this._imports.push(n)}getImportBySelector(n){return this._imports.find(t=>t.type==="component"&&t.selectors.includes(n))}};function A(e,n){return{text:e,span:n}}function I(...e){let n="",t=new Array;for(let r of e)if(typeof r=="string")n=`${n}${r}`;else{let o=n.length;n=`${n}${r.text}`,t.push({columnStart:o,columnEnd:n.length,original:r.span})}return t.length?{text:n,mappings:t}:{text:n}}function h(e){return{text:e}}function y(e){return e.map(n=>({text:" ".repeat(2)+n.text,mappings:n.mappings?.map(t=>({...t,columnStart:t.columnStart+2,columnEnd:t.columnEnd+2}))}))}function Lt(e,n,t){let r=new Array;if(hr(e.tagName)){let i=t.getImportBySelector(e.tagName);if(!i)throw new Error(`${e.tagName} selector is not associated to any WebComponent imported in the template`,{cause:e.span});r.push(...fr(e,i,t))}else r.push(...Tr(e,t));let o=e.children;for(let i=0;i<o.length;i++)r.push(...n(o[i],t));return r}function fr(e,n,t){let r=new Array,o=new Set(n.properties.filter(a=>a.required).map(a=>a.alias??a.name)),i=e.attributes;for(let a=0;a<i.length;a++){let{name:c,value:p}=i[a],u=ur(n,c);u&&(o.delete(u.alias??u.name),r.push(h("{"),...y(typeof p=="object"?[I("(",A(x(p.expression,t,{resolver:"root"}),p.span),`) satisfies ${u.type};`)]:[h("let x!: string"),h(`x satisfies ${u.type};`)]),h("}")))}if(o.size)throw new Error(`${e.tagName} is missing the following required properties: ${Array.from(o.values()).join(`
|
|
17
17
|
`)}`,{cause:e.span});let s=e.events;for(let a=0;a<s.length;a++){let{name:c,handler:p,parameters:u}=s[a],d=dr(n,c);if(!d)throw new Error(`Unknown event "${c}" on <${e.tagName}> (${n.className} has no @Event with this name).`,{cause:e.span});let f=new g([],t);f.addUnresolvableIdentifier("$event");let N=u.map(_=>x(_,f,{resolver:"root"})).join(", ");r.push(h("{")),d.type!=="void"&&r.push(...y([h(`let $event!: CustomEvent<${d.type}>;`)])),r.push(...y([I(A(`root.${p}(${N});`,s[a].span))])),r.push(h("}"))}return r}function ur(e,n){return e.properties.find(t=>(t.alias??t.name)===n)}function dr(e,n){return e.events.find(t=>t.name===n)}function Tr(e,n){let t=new Array,r=e.attributes;for(let i=0;i<r.length;i++){let{value:s}=r[i];typeof s!="string"&&t.push(I(A(`${x(s.expression,n,{resolver:"root"})};`,s.span)))}let o=e.events;for(let i=0;i<o.length;i++){let{handler:s,parameters:a}=o[i],c=new g([],n);c.addUnresolvableIdentifier("$event");let p=a.map(u=>x(u,c,{resolver:"root"})).join(", ");t.push(I(A(`root.${s}(${p});`,o[i].span)))}return t}function hr(e){return/^[a-z][a-z0-9._\-]*-[a-z0-9._\-]*$/.test(e)}function At(e,n,t){let r=new j([],t),o=ee(e,"$index"),i=ee(e,"$first"),s=ee(e,"$last"),a=ee(e,"$even"),c=ee(e,"$odd"),p=[o,i,s,a,c,e.itemAlias];for(let d=0;d<p.length;d++)r.addUnresolvableIdentifier(p[d]);let u=new Array;return u.push(I(`for (const ${e.itemAlias} of root.`,A(e.iterableSource,e.span),") {")),u.push(...y([h(`let ${o}!: number;`),h(`let ${i}!: boolean;`),h(`let ${s}!: boolean;`),h(`let ${a}!: boolean;`),h(`let ${c}!: boolean;`),I(A(`${x(e.trackExpression,t,{skipResolution:!0})};`,e.span)),...e.children.flatMap(d=>n(d,r))])),u.push(h("}")),u}function ee(e,n){return e.implicitAliases.get(n)??n}function It(e,n,t){let r=new Array,o=x(e.conditionNode,t,{resolver:"root"});r.push(I("if (",A(o,e.span),") {")),r.push(...y(e.children.flatMap(s=>n(s,t)))),r.push(h("}"));let i=e.alternate;for(;i?.type===7;){let s=x(i.conditionNode,t,{resolver:"root"});r.push(I("else if (",A(s,i.span),") {")),r.push(...y(i.children.flatMap(a=>n(a,t)))),r.push(h("}")),i=i.alternate}return i&&(r.push(h("else {")),r.push(...y(i.children.flatMap(s=>n(s,t)))),r.push(h("}"))),r}function vt(e,n,t){let r=e.path,o=yt(r),i=new Array;for(let s=0;s<e.specifiers.length;s++){let{imported:a,local:c}=e.specifiers[s];switch(a){case"*":i.push(h(`let ${c}!: typeof import('${o}');`));break;case"default":i.push(h(`let ${c}!: typeof import('${o}')['default'];`));break;default:let p=yt(a);i.push(h(`let ${c}!: typeof import('${o}')['${p}'];`));break}}return i}function yt(e){return e.replace(/\\/g,"\\\\").replace(/'/g,"\\'")}function _t(e,n,t){let r=x(e.expression,t,{resolver:"root"}),o=[I("switch (",A(r,e.span),") {")],i=e.children;for(let s=0;s<i.length;s++){let a=i[s],c=a.condition;if(c?.length)for(let p=0;p<c.length;p++)o.push(h(` case ${c[p]}:`));else o.push(h(" default:"));o.push(...y(y([...a.children.flatMap(p=>n(p,t)),h("break;")])))}return o.push(h("}")),o}function Le(e,n,t){return e.type===4?[I(A(`${x(e.expression,t,{resolver:"root"})};`,e.span))]:[]}import{existsSync as Ae}from"fs";import{readFile as xr}from"fs/promises";import{resolve as Er}from"path";import{createSourceFile as Cr,getNameOfDeclaration as St,isArrayLiteralExpression as kr,isCallExpression as te,isClassDeclaration as gr,isDecorator as Nr,isIdentifier as D,isObjectLiteralExpression as bt,isPropertyAccessExpression as Lr,isPropertyAssignment as Ot,isPropertyDeclaration as ye,isStringLiteral as Ie,isTypeReferenceNode as Ar,ScriptTarget as Ir,SyntaxKind as Rt}from"typescript";async function Ft(e,n,t=process.cwd()){try{let r=yr(e,t);if(!r)return;let o=await xr(r,"utf8"),i=Cr(r,o,Ir.Latest,!0),s,a=i.statements;for(let c=0;c<a.length;c++){let p=a[c];if(!s&&gr(p)){let u=p.name?.text;if(u!==n)continue;let d=p.modifiers??[],f=Array.from(d).find(v=>vr(v));if(!f)continue;let N=br(f);if(!N?.length)continue;let _=new Array,S=new Array,H=p.members;for(let v=0;v<H.length;v++){let re=H[v];if(!ye(re))continue;let ve=re.modifiers??[],_e=!1,Se=Array.from(ve).find(b=>{let P=_r(b);return _e=!!P.required,P.decorator});if(Se){let b=St(re),P=b&&D(b)?b.text:void 0;if(P){let l=Rr(P,Se,_e);l&&_.push(l)}continue}let be=Array.from(ve).find(b=>Sr(b));if(be){let b=St(re),P=b&&D(b)?b.text:void 0;if(P){let l=Fr(P,be);l&&S.push(l)}}}s={type:"component",className:u,selectors:N,properties:_,events:S}}}return s}catch(r){console.error(`Failed to extract component metadata from ${e}:`,r);return}}function yr(e,n){if(e.startsWith(".")){let t=Er(n,e);if(Ae(`${t}.ts`))return t+".ts";if(Ae(`${t}/index.ts`))return`${t}/index.ts`;if(Ae(t))return t}}function vr(e){if(e.kind!==Rt.Decorator)return!1;let n=te(e.expression)?e.expression.expression:e.expression;return D(n)&&n.text==="WebComponent"}function _r(e){if(!Nr(e))return{decorator:!1};let n=e.expression;if(!te(n))return{decorator:!1};let t=n.expression;return D(t)&&t.text==="Property"?{decorator:!0}:Lr(t)&&D(t.expression)&&t.expression.text==="Property"&&t.name.text==="required"?{decorator:!0,required:!0}:{decorator:!1}}function Sr(e){if(e.kind!==Rt.Decorator)return!1;let n=te(e.expression)?e.expression.expression:e.expression;return D(n)&&n.text==="Event"}function br(e){try{let n=e.expression;if(!te(n))return[];let t=n.arguments;if(t.length===0)return[];let r=t[0];if(!r||!bt(r))return[];let o=Array.from(r.properties).find(i=>Ot(i)&&D(i.name)&&i.name.text==="selector");return Or(o.initializer)}catch{return[]}}function Or(e){return Ie(e)?[e.text]:kr(e)?e.elements?.filter(n=>Ie(n)).map(n=>n.text):[]}function Rr(e,n,t){let r=n.expression,o={name:e,required:t,type:ye(n.parent)?wt(n.parent.type):"any"};if(te(r)){let i=r.arguments,a=(i.length?i.find(c=>bt(c)):void 0)?.properties?.find(c=>Ot(c)&&D(c.name)&&c.name.text==="alias");a&&Ie(a.initializer)&&(o.alias=a.initializer.text)}return o}function Fr(e,n){let t={name:e,type:"void"};return ye(n.parent)&&(t.type=wt(n.parent.type,!0)),t}function wt(e,n=!1){let t=n?"void":"any";if(!e||!Ar(e))return t;let r=e.typeArguments;return!r||r.length===0?t:r[0].getText()}var le=class{constructor(n,t){this._input=n;this._ast=t}_input;_ast;_context=new j;_states={3:Le,4:Le,0:Lt,5:It,8:At,9:_t,11:vt};async populateImportMetadata(n){let t=this._ast.filter(r=>r.type===11);await Promise.all(t.flatMap(r=>r.specifiers.filter(({imported:o})=>o!=="*").map(async({imported:o,local:i})=>{let s=o==="default"?i:o,a=await Ft(r.path,s,n);a&&this._context.addImport(a)})))}async generate(n){try{await this.populateImportMetadata(n);let t=this._ast.flatMap(o=>this._processNode(o,this._context)),r=[h("function typeCheck() {"),...y(t),h("}")];return{text:r.map(o=>o.text).join(`
|
|
18
|
-
`),mappingTable:this.buildMappingTable(r)}}catch(t){let r,o;if(t instanceof Error){let i=t.cause;o=i&&typeof i=="object"&&"start"in i&&"end"in i?i:void 0,r=t.message}else r=t;throw
|
|
19
|
-
|
|
18
|
+
`),mappingTable:this.buildMappingTable(r)}}catch(t){let r,o;if(t instanceof Error){let i=t.cause;o=i&&typeof i=="object"&&"start"in i&&"end"in i?i:void 0,r=t.message}else r=t;throw o?`${new M(this._input).getPositionFromCharacterIndex(o.start+1)} - ${r}
|
|
19
|
+
---> ${wr(this._input,o.start,o.end)}}`:r}}buildMappingTable(n){let t=new Map;return n.forEach((r,o)=>{r.mappings&&t.set(o,r.mappings)}),t}_processNode=(n,t)=>{let r=this._states[n.type];if(!r)throw new Error(`No transition function for token type ${C[n.type]}`,{cause:n.span});return r(n,this._processNode,t)}};async function wa(e,n,t){let r=new pe(e).tokenize(),o=new me(e,r).parse();return{javascript:new ne(e,o).generate(t),typescript:await new le(e,o).generate(n)}}function Pa(e,n){let t=e.get(n.line);if(t){let r=t.find(o=>n.character>=o.columnStart&&n.character<o.columnEnd);return r?r.original:t.reduce((o,i)=>{let s=Math.min(Math.abs(n.character-i.columnStart),Math.abs(n.character-i.columnEnd)),a=Math.min(Math.abs(n.character-o.columnStart),Math.abs(n.character-o.columnEnd));return s<a?i:o}).original}}export{M as Cursor,wa as compile,Pa as resolveTemplateSpan};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xaendar/compiler",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.7",
|
|
4
4
|
"description": "A library for transpiling Xaendar Templates into JavaScript code",
|
|
5
5
|
"author": "Kaitenjo",
|
|
6
6
|
"license": "MIT",
|
|
@@ -13,8 +13,8 @@
|
|
|
13
13
|
},
|
|
14
14
|
"main": "./dist/compiler.js",
|
|
15
15
|
"dependencies": {
|
|
16
|
-
"@xaendar/common": "0.9.
|
|
17
|
-
"@xaendar/types": "0.9.
|
|
16
|
+
"@xaendar/common": "0.9.7",
|
|
17
|
+
"@xaendar/types": "0.9.7",
|
|
18
18
|
"typescript": "^6.0.3"
|
|
19
19
|
}
|
|
20
20
|
}
|