@vtj/parser 0.12.0-alpha.2 → 0.12.1

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/index.cjs CHANGED
@@ -2,9 +2,9 @@
2
2
  * Copyright (c) 2025, VTJ.PRO All rights reserved.
3
3
  * @name @vtj/parser
4
4
  * @author CHC chenhuachun1549@dingtalk.com
5
- * @version 0.12.0-alpha.2
5
+ * @version 0.12.1
6
6
  * @license <a href="https://vtj.pro/license.html">MIT License</a>
7
- */const le="0.12.0-alpha.2";function q(e){const{descriptor:s}=D.parse(e),t=s.template?.content||"",n=(s.scriptSetup||s.script)?.content||"",o=s.styles.map(r=>r.content);return{template:t,script:n,styles:o}}function V(e){return ae.parse(e,{sourceType:"module",plugins:["typescript"]})}function X(e,s){return(A.default||A)(e,s)}function w(e){try{return(M.default||M)(e,{comments:!1,concise:!0,retainLines:!1,jsescOption:{minimal:!0}}).code}catch(s){return console.error("代码生成错误",s),""}}function j(e){return e&&e.type==="JSExpression"}function fe(e){return typeof e=="object"&&e&&e.type==="JSFunction"}function C(e){return j(e)||fe(e)}function me(e){return!!e&&!j(e)&&typeof e!="string"}function de(e){const s=q(e),t=V(s.script),n={};return s.styles&&(n.css=s.styles[0]||""),X(t,{CallExpression(o){const r=o.node.callee?.name;if(r){const c=w(o.node.arguments[0]);c&&(n[r]={type:"JSFunction",value:c})}}}),n}function b(e,s,t){const n=new RegExp(`${s}`,"g"),o=/(\@\_|\$|\.|\,|\w)$/,r=/^\w/;return e.replace(n,(c,i,a)=>{const l=a.substring(0,i),u=a.substring(i+s.length);return o.test(l.trim())||r.test(u.trim())?c:t})}function $(e,s,t){const{context:n={},computed:o=[],libs:r={},members:c=[]}=t||{},i=Array.from(n[s||""]||new Set);if(i)for(const a of i)e=b(e,a,`this.context.${a}`);for(const a of o)e=b(e,a,`this.${a}.value`);for(const[a,l]of Object.entries(r))e=b(e,a,`this.$libs.${l}.${a}`);for(const a of c)e=b(e,a,`this.${a}`);return e=e.replace(/_ctx\./g,"this."),e}function m(e){return{type:"JSExpression",value:/^\{[\w\W]*\}$/.test(e)?`(${e})`:e}}function _(e){return{type:"JSFunction",value:e}}const ye=["beforeCreate","created","beforeMount","mounted","beforeUpdate","updated","beforeUnmount","unmounted","errorCaptured","renderTracked","renderTriggered","activated","deactivated"],he="html,body,base,head,link,meta,style,title,address,article,aside,footer,header,hgroup,h1,h2,h3,h4,h5,h6,nav,section,div,dd,dl,dt,figcaption,figure,picture,hr,img,li,main,ol,p,pre,ul,a,b,abbr,bdi,bdo,br,cite,code,data,dfn,em,i,kbd,mark,q,rp,rt,ruby,s,samp,small,span,strong,sub,sup,time,u,var,wbr,area,audio,map,track,video,embed,object,param,source,canvas,script,noscript,del,ins,caption,col,colgroup,table,thead,tbody,td,th,tr,button,datalist,fieldset,form,input,label,legend,meter,optgroup,option,output,progress,select,textarea,details,dialog,menu,summary,template,blockquote,iframe,tfoot".split(","),ge="component,slot,template".split(",");function ve(e){return he.includes(e)||ge.includes(e)?e:J.upperFirstCamelCase(e)}function Ee(e){return/style lang=\"scss\"/.test(e)}let O=[],S={},U={},B={};function be(e,s,t="",n){return O=[],S={},U=n?.handlers||{},B=n?.styles||{},{nodes:(D.compileTemplate({id:e,filename:s,source:t}).ast?.children||[]).map(i=>N(i)).filter(i=>!!i),slots:O,context:S}}function Te(e){if(e.name==="slot"){let s="default";const t=[];for(const[n,o]of Object.entries(e.props||{}))n==="name"?s=o:t.push(n);O.push({name:s,params:t})}}function we(e){return e.replace(/\s+/g," ").split(";").reduce((t,n)=>{const[o,r]=n.split(":").map(c=>c.trim());return o&&r&&(t[o]=r),t},{})}function Ne(e){const s={};for(const t of e){if(t.type===p.NodeTypes.ATTRIBUTE)if(t.name==="class"){const n=t.value?.content||"",o=/[\w]+_[\w]{5,}/,r=n.match(o)?.[0]||"",c=n.split(" ").filter(a=>a!==r),i=B[`.${r}`];i&&(s.style=i),c.length&&(s.class=c.join(" "))}else if(t.name==="style"){const n=t.value?.content||"";n&&(s.style=we(n))}else s[t.name]=t.value?.content||"";t.type===p.NodeTypes.DIRECTIVE&&t.name==="bind"&&(t.exp?.type===p.NodeTypes.SIMPLE_EXPRESSION&&t.arg?.type===p.NodeTypes.SIMPLE_EXPRESSION&&(s[t.arg.content]=m(`(${t.exp.content})`)),t.exp?.type===p.NodeTypes.COMPOUND_EXPRESSION&&t.arg?.type===p.NodeTypes.SIMPLE_EXPRESSION&&(s[t.arg.content]=m(`(${t.exp.loc.source})`)))}return s}function Se(e,s={}){const t={};for(const n of e)if(n.type===p.NodeTypes.DIRECTIVE&&n.name==="on"&&n.arg?.type===p.NodeTypes.SIMPLE_EXPRESSION){const o=n.modifiers.reduce((u,y)=>(u[y.content]=!0,u),{});let r=n.exp?.loc.source||"";/\)$/.test(r)&&(r=`($event) => ${r}`);const i=new RegExp(`${n.arg.content}_[\\w]{5,}`),a=r.match(i)?.[0]||"",l=s[a];a&&l?t[n.arg.content]={name:n.arg.content,handler:l,modifiers:o}:t[n.arg.content]={name:n.arg.content,handler:_(`(${r})`),modifiers:o}}return t}function xe(e){const s=[];if(e.type===p.NodeTypes.IF){const t=e.codegenNode?.test;t&&s.push({name:"vIf",value:m(t.loc.source)})}if(e.type===p.NodeTypes.FOR&&s.push({name:"vFor",value:m(e.source.loc.source),iterator:{item:e.valueAlias?.loc.source||"item",index:e.keyAlias?.loc.source||"index"}}),e.type===p.NodeTypes.ELEMENT){const t=e.props.filter(i=>i.type===p.NodeTypes.DIRECTIVE),n=t.filter(i=>i.name==="model");n.length&&n.forEach(i=>{s.push({name:"vModel",arg:i.arg?.content,value:m(i.exp?.loc.source||"")})});const o=t.find(i=>i.name==="show");o&&s.push({name:"vShow",value:m(o.exp?.loc.source||"")});const r=t.find(i=>i.name==="bind"&&!i.arg);r&&s.push({name:"vBind",value:m(r.exp?.loc.source||"")});const c=t.find(i=>i.name==="html");c&&s.push({name:"vHtml",value:m(c.exp?.loc.source||"")})}return s}function H(e){let s="";const{name:t,props:n,events:o={}}=e,r=n?.class||"";if(typeof r=="string"){const c=new RegExp(`${t}_([\\w]+)`),i=r.match(c);i&&i[1]&&(s=i[1])}for(const{name:c,handler:i}of Object.values(o)){const a=new RegExp(`${c}_([\\w]+)`),l=i.value.match(a);l&&l[1]&&(s=l[1])}return s||J.uid()}function I(e,s){const t=new Set(s?.id?S[s.id]:[]),n=(e.directives||[]).find(c=>c.name==="vFor");let o=new Set(Array.from(t));if(n){const{item:c="item",index:i="index"}=n.iterator||{};o=new Set([c,i,...Array.from(o)])}const r=e.slot;if(r){const c=typeof r=="string"?[]:r.params||[],i=c.length?c:[`scope_${s?.id}`];o=new Set([...i,...Array.from(o)])}S[e.id]=o}function k(e,s,t){const n={name:ve(e.tag),props:Ne(e.props),events:Se(e.props,U),directives:xe(t||e)};n.id=H(n),I(n,s);const o=ke(n,e.children);return I(n,s),Te(o),o}function N(e,s){if(e.type===p.NodeTypes.ELEMENT)return k(e,s);if(e.type===p.NodeTypes.IF){const t=e.branches[0].children[0];if(t&&t.type===p.NodeTypes.ELEMENT)return k(t,s,e)}if(e.type===p.NodeTypes.FOR){const t=e.children[0];if(t.type===p.NodeTypes.ELEMENT)return k(t,s,e)}return e.type===p.NodeTypes.TEXT_CALL?e.content.type==p.NodeTypes.TEXT?e.content.content:e.content.type===p.NodeTypes.INTERPOLATION?m(e.content.content.loc.source):"":e.type===p.NodeTypes.TEXT?e.content:e.type===p.NodeTypes.INTERPOLATION&&(e.content.type===p.NodeTypes.SIMPLE_EXPRESSION||e.content.type===p.NodeTypes.COMPOUND_EXPRESSION)?m(e.content.loc.source):(e.type===p.NodeTypes.COMPOUND_EXPRESSION&&console.warn("未处理节点",e),console.warn("未处理",e.type),null)}function ke(e,s=[]){const t=[];for(const n of s)if(n.type===p.NodeTypes.ELEMENT&&n.tag==="template"){const o=n.props.find(r=>r.name==="slot");for(const r of n.children){const c=r.type===p.NodeTypes.TEXT||r.type===p.NodeTypes.TEXT_CALL?{name:"span",children:N(r,e)}:N(r,e);c&&(me(c)&&o?.type===p.NodeTypes.DIRECTIVE&&(c.id=H(c),c.slot={name:o.arg?.content||"default",params:o.exp?.identifiers||[]},I(c,e)),t.push(c))}}else{const o=N(n,e);o&&t.push(o)}if(t.length===1){const n=t[0];e.children=typeof n=="string"||j(n)?n:[n]}else e.children=t;return e}function $e(e){const t={imports:Oe(e),emits:[],errors:[]},n=V(e);return X(n,{ExportDefaultDeclaration(o){const r=o.node.declaration;if(r.type!=="CallExpression")return;const c=r.arguments[0];if(c?.type!=="ObjectExpression")return;const i=c.properties.filter(u=>u.type==="ObjectProperty"),a=c.properties.filter(u=>u.type=="ObjectMethod"),l=i.find(u=>u.key.name==="computed");l&&(t.computed=W(l.value),t.watchers=Pe(l.value));for(const u of i)switch(u.key.name){case"name":t.name=u.value.value;break;case"methods":t.handlers=_e(u.value),t.methods=Ce(u.value);break;case"watch":t.watch=Re(u.value,t.watchers);break;case"props":t.props=Le(u.value);break;case"inject":t.inject=De(u.value);break}for(const u of a)switch(u.key.name){case"setup":t.state=je(u.body);break}t.lifeCycles=Ae(a)},CallExpression(o){const r=Fe(o.node);if(r.length)for(let c of r)t.emits?.find(a=>a.name===c.name)||t.emits?.push(c)}}),t}function Oe(e){const s=/import\s+{(.+?)}\s+from\s+['"](.+?)['"]/g,t=[];let n;for(;(n=s.exec(e))!==null;){const o=n[2]==="@element-plus/icons-vue"?"@vtj/icons":n[2];t.push({from:o,imports:n[1].split(",").map(r=>r.trim())})}return t}function Ie(e){for(const s of e.body)if(s.type==="VariableDeclaration"&&s.kind=="const"){const{id:t,init:n}=s.declarations[0];if(t.loc?.identifierName==="state"&&n?.type==="CallExpression"&&n.callee.loc?.identifierName==="reactive")return n.arguments[0]}return null}function je(e){const s=Ie(e);if(!s)return{};const t={};for(const n of s.properties){const{key:o,value:r}=n;if(o.type==="Identifier"){const c=w(r);t[o.name]=m(c)}}return t}function g(e){const{key:s,async:t,params:n,body:o}=e,r=n.map(c=>c.type==="ObjectPattern"?`{${c.properties.map(a=>a.key?.name||a.name).join(",")}}`:c.name).join(", ");if(s.type==="Identifier"){const c=s.name,i=w(o),l=`${t?"async ":""}(${r}) => ${i}`,u=c.startsWith("watcher_");return{name:c,watcher:u,exp:_(l)}}}function W(e){if(!e)return{};const s={};for(const t of e.properties){const n=g(t);n&&!n.watcher&&(s[n.name]=n.exp)}return s}function Ce(e){const s=W(e),t=/\_([\w]{5,})$/,n={};for(const o of Object.keys(s))t.test(o)||(n[o]=s[o]);return n}function _e(e){if(!e)return{};const s={},t=/\_([\w]{5,})$/;for(const n of e.properties){const o=n.key.name;if(t.test(o))try{if(n.body.body[0]?.argument){const r=w(n.body.body[0].argument.callee.object);s[o]=_(r)}else s[o]=g(n)?.exp}catch(r){console.warn(r)}}return s}function Pe(e){if(!e)return{};const s={};for(const t of e.properties){const n=g(t);n&&n.watcher&&(s[n.name]=n.exp)}return s}function T(e,s){return!!G(e,s)}function G(e,s){return e.find(n=>n.key?.name===s)?.value?.value}function F(e,s){const t=e.find(n=>n.key?.name===s);if(t)return g(t)?.exp}function Re(e,s={}){const t=[];for(const n of e.properties){const{key:o,value:r}=n,c=o.value||o.name||"";if(s[c]){const i=r.properties;t.push({source:s[c],deep:T(i,"deep"),immediate:T(i,"immediate"),handler:F(i,"handler")})}else if(n.type==="ObjectMethod"&&t.push({source:{type:"JSFunction",value:`this.${c}`},deep:!1,immediate:!1,handler:g(n)?.exp}),n.type==="ObjectProperty"){const i=r.properties;t.push({source:{type:"JSFunction",value:`this.${c}`},deep:T(i,"deep"),immediate:T(i,"immediate"),handler:F(i,"handler")})}}return t}function Ae(e){const s={};for(const t of e){const n=t.key.name;if(ye.includes(n)){const o=g(t);o&&(s[o.name]=o.exp)}}return s}function Me(e){const s=e.find(t=>t.key?.name==="type");if(s)return s.value.type==="ArrayExpression"?s.value.elements.map(t=>t.name):s.value.name}function K(e){const s=e.find(t=>t.key?.name==="default");if(s)return m(w(s.value))}function Le(e){if(!e)return[];let s=[];return e.type==="ArrayExpression"?s=e.elements.map(t=>t.value):e.type==="ObjectExpression"&&(s=e.properties.map(t=>{const{key:n,value:o}=t,r=o.properties||[];return{name:n.name,required:T(r,"required"),type:Me(r),default:K(r)}})),s}function Fe(e){const s=[];if(e.callee.type==="MemberExpression"&&e.callee.property?.name==="$emit"){const[n,...o]=(e.arguments||[]).map(r=>r.value||r.name);n&&s.push({name:n,params:o})}return s}function De(e){let s=[];return e.type==="ObjectExpression"&&(s=e.properties.map(t=>{const{key:n,value:o}=t,r=o.properties,c=n.name,i=G(r,"from"),a=K(r);return{name:c,from:i||c,default:a}})),s}function Je(e){const s=[],t={},n=[];try{const o=ue.parse(e),r=/^.[\w]+_[\w]{5,}/;for(const c of o.nodes)if(c.type==="rule"){const i={};r.test(c.selector)?(c.nodes.forEach(a=>{a.type==="decl"&&(i[a.prop]=a.value)}),t[c.selector]=i):n.push(c.toString())}}catch(o){s.push(`css解析出错了,错误信息:[ ${o.message} ]
7
+ */const le="0.12.1";function q(e){const{descriptor:s}=D.parse(e),t=s.template?.content||"",n=(s.scriptSetup||s.script)?.content||"",o=s.styles.map(r=>r.content);return{template:t,script:n,styles:o}}function V(e){return ae.parse(e,{sourceType:"module",plugins:["typescript"]})}function X(e,s){return(A.default||A)(e,s)}function w(e){try{return(M.default||M)(e,{comments:!1,concise:!0,retainLines:!1,jsescOption:{minimal:!0}}).code}catch(s){return console.error("代码生成错误",s),""}}function j(e){return e&&e.type==="JSExpression"}function fe(e){return typeof e=="object"&&e&&e.type==="JSFunction"}function C(e){return j(e)||fe(e)}function me(e){return!!e&&!j(e)&&typeof e!="string"}function de(e){const s=q(e),t=V(s.script),n={};return s.styles&&(n.css=s.styles[0]||""),X(t,{CallExpression(o){const r=o.node.callee?.name;if(r){const c=w(o.node.arguments[0]);c&&(n[r]={type:"JSFunction",value:c})}}}),n}function b(e,s,t){const n=new RegExp(`${s}`,"g"),o=/(\@\_|\$|\.|\,|\w)$/,r=/^\w/;return e.replace(n,(c,i,a)=>{const l=a.substring(0,i),u=a.substring(i+s.length);return o.test(l.trim())||r.test(u.trim())?c:t})}function $(e,s,t){const{context:n={},computed:o=[],libs:r={},members:c=[]}=t||{},i=Array.from(n[s||""]||new Set);if(i)for(const a of i)e=b(e,a,`this.context.${a}`);for(const a of o)e=b(e,a,`this.${a}.value`);for(const[a,l]of Object.entries(r))e=b(e,a,`this.$libs.${l}.${a}`);for(const a of c)e=b(e,a,`this.${a}`);return e=e.replace(/_ctx\./g,"this."),e}function m(e){return{type:"JSExpression",value:/^\{[\w\W]*\}$/.test(e)?`(${e})`:e}}function _(e){return{type:"JSFunction",value:e}}const ye=["beforeCreate","created","beforeMount","mounted","beforeUpdate","updated","beforeUnmount","unmounted","errorCaptured","renderTracked","renderTriggered","activated","deactivated"],he="html,body,base,head,link,meta,style,title,address,article,aside,footer,header,hgroup,h1,h2,h3,h4,h5,h6,nav,section,div,dd,dl,dt,figcaption,figure,picture,hr,img,li,main,ol,p,pre,ul,a,b,abbr,bdi,bdo,br,cite,code,data,dfn,em,i,kbd,mark,q,rp,rt,ruby,s,samp,small,span,strong,sub,sup,time,u,var,wbr,area,audio,map,track,video,embed,object,param,source,canvas,script,noscript,del,ins,caption,col,colgroup,table,thead,tbody,td,th,tr,button,datalist,fieldset,form,input,label,legend,meter,optgroup,option,output,progress,select,textarea,details,dialog,menu,summary,template,blockquote,iframe,tfoot".split(","),ge="component,slot,template".split(",");function ve(e){return he.includes(e)||ge.includes(e)?e:J.upperFirstCamelCase(e)}function Ee(e){return/style lang=\"scss\"/.test(e)}let O=[],S={},U={},B={};function be(e,s,t="",n){return O=[],S={},U=n?.handlers||{},B=n?.styles||{},{nodes:(D.compileTemplate({id:e,filename:s,source:t}).ast?.children||[]).map(i=>N(i)).filter(i=>!!i),slots:O,context:S}}function Te(e){if(e.name==="slot"){let s="default";const t=[];for(const[n,o]of Object.entries(e.props||{}))n==="name"?s=o:t.push(n);O.push({name:s,params:t})}}function we(e){return e.replace(/\s+/g," ").split(";").reduce((t,n)=>{const[o,r]=n.split(":").map(c=>c.trim());return o&&r&&(t[o]=r),t},{})}function Ne(e){const s={};for(const t of e){if(t.type===p.NodeTypes.ATTRIBUTE)if(t.name==="class"){const n=t.value?.content||"",o=/[\w]+_[\w]{5,}/,r=n.match(o)?.[0]||"",c=n.split(" ").filter(a=>a!==r),i=B[`.${r}`];i&&(s.style=i),c.length&&(s.class=c.join(" "))}else if(t.name==="style"){const n=t.value?.content||"";n&&(s.style=we(n))}else s[t.name]=t.value?.content||"";t.type===p.NodeTypes.DIRECTIVE&&t.name==="bind"&&(t.exp?.type===p.NodeTypes.SIMPLE_EXPRESSION&&t.arg?.type===p.NodeTypes.SIMPLE_EXPRESSION&&(s[t.arg.content]=m(`(${t.exp.content})`)),t.exp?.type===p.NodeTypes.COMPOUND_EXPRESSION&&t.arg?.type===p.NodeTypes.SIMPLE_EXPRESSION&&(s[t.arg.content]=m(`(${t.exp.loc.source})`)))}return s}function Se(e,s={}){const t={};for(const n of e)if(n.type===p.NodeTypes.DIRECTIVE&&n.name==="on"&&n.arg?.type===p.NodeTypes.SIMPLE_EXPRESSION){const o=n.modifiers.reduce((u,y)=>(u[y.content]=!0,u),{});let r=n.exp?.loc.source||"";/\)$/.test(r)&&(r=`($event) => ${r}`);const i=new RegExp(`${n.arg.content}_[\\w]{5,}`),a=r.match(i)?.[0]||"",l=s[a];a&&l?t[n.arg.content]={name:n.arg.content,handler:l,modifiers:o}:t[n.arg.content]={name:n.arg.content,handler:_(`(${r})`),modifiers:o}}return t}function xe(e){const s=[];if(e.type===p.NodeTypes.IF){const t=e.codegenNode?.test;t&&s.push({name:"vIf",value:m(t.loc.source)})}if(e.type===p.NodeTypes.FOR&&s.push({name:"vFor",value:m(e.source.loc.source),iterator:{item:e.valueAlias?.loc.source||"item",index:e.keyAlias?.loc.source||"index"}}),e.type===p.NodeTypes.ELEMENT){const t=e.props.filter(i=>i.type===p.NodeTypes.DIRECTIVE),n=t.filter(i=>i.name==="model");n.length&&n.forEach(i=>{s.push({name:"vModel",arg:i.arg?.content,value:m(i.exp?.loc.source||"")})});const o=t.find(i=>i.name==="show");o&&s.push({name:"vShow",value:m(o.exp?.loc.source||"")});const r=t.find(i=>i.name==="bind"&&!i.arg);r&&s.push({name:"vBind",value:m(r.exp?.loc.source||"")});const c=t.find(i=>i.name==="html");c&&s.push({name:"vHtml",value:m(c.exp?.loc.source||"")})}return s}function H(e){let s="";const{name:t,props:n,events:o={}}=e,r=n?.class||"";if(typeof r=="string"){const c=new RegExp(`${t}_([\\w]+)`),i=r.match(c);i&&i[1]&&(s=i[1])}for(const{name:c,handler:i}of Object.values(o)){const a=new RegExp(`${c}_([\\w]+)`),l=i.value.match(a);l&&l[1]&&(s=l[1])}return s||J.uid()}function I(e,s){const t=new Set(s?.id?S[s.id]:[]),n=(e.directives||[]).find(c=>c.name==="vFor");let o=new Set(Array.from(t));if(n){const{item:c="item",index:i="index"}=n.iterator||{};o=new Set([c,i,...Array.from(o)])}const r=e.slot;if(r){const c=typeof r=="string"?[]:r.params||[],i=c.length?c:[`scope_${s?.id}`];o=new Set([...i,...Array.from(o)])}S[e.id]=o}function k(e,s,t){const n={name:ve(e.tag),props:Ne(e.props),events:Se(e.props,U),directives:xe(t||e)};n.id=H(n),I(n,s);const o=ke(n,e.children);return I(n,s),Te(o),o}function N(e,s){if(e.type===p.NodeTypes.ELEMENT)return k(e,s);if(e.type===p.NodeTypes.IF){const t=e.branches[0].children[0];if(t&&t.type===p.NodeTypes.ELEMENT)return k(t,s,e)}if(e.type===p.NodeTypes.FOR){const t=e.children[0];if(t.type===p.NodeTypes.ELEMENT)return k(t,s,e)}return e.type===p.NodeTypes.TEXT_CALL?e.content.type==p.NodeTypes.TEXT?e.content.content:e.content.type===p.NodeTypes.INTERPOLATION?m(e.content.content.loc.source):"":e.type===p.NodeTypes.TEXT?e.content:e.type===p.NodeTypes.INTERPOLATION&&(e.content.type===p.NodeTypes.SIMPLE_EXPRESSION||e.content.type===p.NodeTypes.COMPOUND_EXPRESSION)?m(e.content.loc.source):(e.type===p.NodeTypes.COMPOUND_EXPRESSION&&console.warn("未处理节点",e),console.warn("未处理",e.type),null)}function ke(e,s=[]){const t=[];for(const n of s)if(n.type===p.NodeTypes.ELEMENT&&n.tag==="template"){const o=n.props.find(r=>r.name==="slot");for(const r of n.children){const c=r.type===p.NodeTypes.TEXT||r.type===p.NodeTypes.TEXT_CALL?{name:"span",children:N(r,e)}:N(r,e);c&&(me(c)&&o?.type===p.NodeTypes.DIRECTIVE&&(c.id=H(c),c.slot={name:o.arg?.content||"default",params:o.exp?.identifiers||[]},I(c,e)),t.push(c))}}else{const o=N(n,e);o&&t.push(o)}if(t.length===1){const n=t[0];e.children=typeof n=="string"||j(n)?n:[n]}else e.children=t;return e}function $e(e){const t={imports:Oe(e),emits:[],errors:[]},n=V(e);return X(n,{ExportDefaultDeclaration(o){const r=o.node.declaration;if(r.type!=="CallExpression")return;const c=r.arguments[0];if(c?.type!=="ObjectExpression")return;const i=c.properties.filter(u=>u.type==="ObjectProperty"),a=c.properties.filter(u=>u.type=="ObjectMethod"),l=i.find(u=>u.key.name==="computed");l&&(t.computed=W(l.value),t.watchers=Pe(l.value));for(const u of i)switch(u.key.name){case"name":t.name=u.value.value;break;case"methods":t.handlers=_e(u.value),t.methods=Ce(u.value);break;case"watch":t.watch=Re(u.value,t.watchers);break;case"props":t.props=Le(u.value);break;case"inject":t.inject=De(u.value);break}for(const u of a)switch(u.key.name){case"setup":t.state=je(u.body);break}t.lifeCycles=Ae(a)},CallExpression(o){const r=Fe(o.node);if(r.length)for(let c of r)t.emits?.find(a=>a.name===c.name)||t.emits?.push(c)}}),t}function Oe(e){const s=/import\s+{(.+?)}\s+from\s+['"](.+?)['"]/g,t=[];let n;for(;(n=s.exec(e))!==null;){const o=n[2]==="@element-plus/icons-vue"?"@vtj/icons":n[2];t.push({from:o,imports:n[1].split(",").map(r=>r.trim())})}return t}function Ie(e){for(const s of e.body)if(s.type==="VariableDeclaration"&&s.kind=="const"){const{id:t,init:n}=s.declarations[0];if(t.loc?.identifierName==="state"&&n?.type==="CallExpression"&&n.callee.loc?.identifierName==="reactive")return n.arguments[0]}return null}function je(e){const s=Ie(e);if(!s)return{};const t={};for(const n of s.properties){const{key:o,value:r}=n;if(o.type==="Identifier"){const c=w(r);t[o.name]=m(c)}}return t}function g(e){const{key:s,async:t,params:n,body:o}=e,r=n.map(c=>c.type==="ObjectPattern"?`{${c.properties.map(a=>a.key?.name||a.name).join(",")}}`:c.name).join(", ");if(s.type==="Identifier"){const c=s.name,i=w(o),l=`${t?"async ":""}(${r}) => ${i}`,u=c.startsWith("watcher_");return{name:c,watcher:u,exp:_(l)}}}function W(e){if(!e)return{};const s={};for(const t of e.properties){const n=g(t);n&&!n.watcher&&(s[n.name]=n.exp)}return s}function Ce(e){const s=W(e),t=/\_([\w]{5,})$/,n={};for(const o of Object.keys(s))t.test(o)||(n[o]=s[o]);return n}function _e(e){if(!e)return{};const s={},t=/\_([\w]{5,})$/;for(const n of e.properties){const o=n.key.name;if(t.test(o))try{if(n.body.body[0]?.argument){const r=w(n.body.body[0].argument.callee.object);s[o]=_(r)}else s[o]=g(n)?.exp}catch(r){console.warn(r)}}return s}function Pe(e){if(!e)return{};const s={};for(const t of e.properties){const n=g(t);n&&n.watcher&&(s[n.name]=n.exp)}return s}function T(e,s){return!!G(e,s)}function G(e,s){return e.find(n=>n.key?.name===s)?.value?.value}function F(e,s){const t=e.find(n=>n.key?.name===s);if(t)return g(t)?.exp}function Re(e,s={}){const t=[];for(const n of e.properties){const{key:o,value:r}=n,c=o.value||o.name||"";if(s[c]){const i=r.properties;t.push({source:s[c],deep:T(i,"deep"),immediate:T(i,"immediate"),handler:F(i,"handler")})}else if(n.type==="ObjectMethod"&&t.push({source:{type:"JSFunction",value:`this.${c}`},deep:!1,immediate:!1,handler:g(n)?.exp}),n.type==="ObjectProperty"){const i=r.properties;t.push({source:{type:"JSFunction",value:`this.${c}`},deep:T(i,"deep"),immediate:T(i,"immediate"),handler:F(i,"handler")})}}return t}function Ae(e){const s={};for(const t of e){const n=t.key.name;if(ye.includes(n)){const o=g(t);o&&(s[o.name]=o.exp)}}return s}function Me(e){const s=e.find(t=>t.key?.name==="type");if(s)return s.value.type==="ArrayExpression"?s.value.elements.map(t=>t.name):s.value.name}function K(e){const s=e.find(t=>t.key?.name==="default");if(s)return m(w(s.value))}function Le(e){if(!e)return[];let s=[];return e.type==="ArrayExpression"?s=e.elements.map(t=>t.value):e.type==="ObjectExpression"&&(s=e.properties.map(t=>{const{key:n,value:o}=t,r=o.properties||[];return{name:n.name,required:T(r,"required"),type:Me(r),default:K(r)}})),s}function Fe(e){const s=[];if(e.callee.type==="MemberExpression"&&e.callee.property?.name==="$emit"){const[n,...o]=(e.arguments||[]).map(r=>r.value||r.name);n&&s.push({name:n,params:o})}return s}function De(e){let s=[];return e.type==="ObjectExpression"&&(s=e.properties.map(t=>{const{key:n,value:o}=t,r=o.properties,c=n.name,i=G(r,"from"),a=K(r);return{name:c,from:i||c,default:a}})),s}function Je(e){const s=[],t={},n=[];try{const o=ue.parse(e),r=/^.[\w]+_[\w]{5,}/;for(const c of o.nodes)if(c.type==="rule"){const i={};r.test(c.selector)?(c.nodes.forEach(a=>{a.type==="decl"&&(i[a.prop]=a.value)}),t[c.selector]=i):n.push(c.toString())}}catch(o){s.push(`css解析出错了,错误信息:[ ${o.message} ]
8
8
  `)}return{errors:s,styles:t,css:n.join(`
9
9
  `)}}async function qe(e){const s=[],{id:t,name:n,source:o,project:r}=e;if(Ee(o))return s.push("style的lang不能是scss, 请改为css"),Promise.reject(s);const{dependencies:c=[]}=r||{},i=q(o),{styles:a,css:l,errors:u}=Je(i.styles.join(`
10
10
  `)),{state:y,watch:x,lifeCycles:f,computed:h,methods:d,props:Y,emits:z,inject:Q,handlers:Z,imports:ee}=$e(i.script),{nodes:te,slots:se,context:ne}=be(t,n,i.template,{handlers:Z,styles:a}),P={id:t,name:n,inject:Q,props:Y,state:y,watch:x,lifeCycles:f,computed:h,methods:d,slots:se,emits:z,nodes:te,css:l},oe=Object.keys(h||{}),re=["$el","$emit","$nextTick","$parent","$root","$refs","$attrs","$slots","$watch","$props","$options","$forceUpdate","state","$props","props",...Object.keys(d||{})],{libs:ce}=Ue(ee,c),R={context:ne,computed:oe,libs:ce,members:re};return await Ve(P,async v=>{await Xe(v,async E=>{if(C(E)){const ie=await L.tsFormatter(E.value);E.value=$(ie,v.id,R)}})},async v=>{const E=await L.tsFormatter(v.value);v.value=$(E,"",R)}),s.push(...u),s.length?Promise.reject(s):new pe.BlockModel(P).toDsl()}async function Ve(e,s,t){const n=async(f,h)=>{if(await s(f,h),Array.isArray(f.children))for(const d of f.children)await n(d,f)},o=async f=>{if(!f||typeof f!="object")return;if(Array.isArray(f)){for(let d of f)await o(d);return}const h=Object.values(f);for(const d of h)C(d)?await t(d):await o(d)},{state:r,watch:c,computed:i,props:a,dataSources:l,methods:u,lifeCycles:y,inject:x}=e;if(await o({state:r,watch:c,computed:i,props:a,dataSources:l,methods:u,lifeCycles:y,inject:x}),Array.isArray(e.nodes))for(const f of e.nodes)await n(f)}async function Xe(e,s){const t=async n=>{if(!n||typeof n!="object")return;if(Array.isArray(n)){for(let r of n)await s(r),await t(r);return}if(C(n)){await s(n);return}const o=Object.values(n);for(const r of o)await s(r),await t(r)};await t(e)}function Ue(e=[],s=[]){const t={},n=s.reduce((o,r)=>(o[r.package]=r.library,o),{});for(const{from:o,imports:r}of e)r.forEach(c=>{const i=n[o];i&&(t[c]=i)});return{libs:t}}exports.VTJ_PARSER_VERSION=le;exports.parseUniApp=de;exports.parseVue=qe;exports.patchCode=$;exports.replacer=b;
package/dist/index.mjs CHANGED
@@ -11,10 +11,10 @@ import fe from "postcss";
11
11
  * Copyright (c) 2025, VTJ.PRO All rights reserved.
12
12
  * @name @vtj/parser
13
13
  * @author CHC chenhuachun1549@dingtalk.com
14
- * @version 0.12.0-alpha.2
14
+ * @version 0.12.1
15
15
  * @license <a href="https://vtj.pro/license.html">MIT License</a>
16
16
  */
17
- const et = "0.12.0-alpha.2";
17
+ const et = "0.12.1";
18
18
  function D(e) {
19
19
  const { descriptor: n } = ce(e), t = n.template?.content || "", s = (n.scriptSetup || n.script)?.content || "", r = n.styles.map((o) => o.content);
20
20
  return {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vtj/parser",
3
3
  "private": false,
4
- "version": "0.12.0-alpha.2",
4
+ "version": "0.12.1",
5
5
  "type": "module",
6
6
  "keywords": [
7
7
  "低代码引擎",
@@ -27,12 +27,12 @@
27
27
  "@vue/compiler-dom": "~3.5.13",
28
28
  "@vue/compiler-sfc": "~3.5.13",
29
29
  "postcss": "~8.5.0",
30
- "@vtj/base": "~0.12.0-alpha.1",
31
- "@vtj/coder": "~0.12.0-alpha.2",
32
- "@vtj/core": "~0.12.0-alpha.2"
30
+ "@vtj/base": "~0.12.0",
31
+ "@vtj/core": "~0.12.1",
32
+ "@vtj/coder": "~0.12.1"
33
33
  },
34
34
  "devDependencies": {
35
- "@vtj/cli": "~0.12.0-alpha.1"
35
+ "@vtj/cli": "~0.12.0"
36
36
  },
37
37
  "exports": {
38
38
  ".": {
@@ -2,7 +2,7 @@
2
2
  * Copyright (c) 2025, VTJ.PRO All rights reserved.
3
3
  * @name @vtj/parser
4
4
  * @author CHC chenhuachun1549@dingtalk.com
5
- * @version 0.12.0-alpha.1
5
+ * @version 0.12.0
6
6
  * @license <a href="https://vtj.pro/license.html">MIT License</a>
7
7
  */
8
- export declare const version = "0.12.0-alpha.1";
8
+ export declare const version = "0.12.0";