@tslite/printer 0.4.0 → 0.5.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
@@ -1 +1 @@
1
- 'use strict';var core=require('@tslite/core'),capability=require('@tslite/capability'),sanitize=require('@tslite/sanitize');var N=t=>core.VALID_OBJECT_KEY.test(t);function f(t){let r=n=>{if(typeof n=="string")return `"${n}"`;if(typeof n!="object")return String(n);if(n===null)return "null";if(Array.isArray(n))return `[${n.map(r).join(",")}]`;let s=n;if("#expr"in s){let o=s["#expr"],{"#expr":m,...P}=s,B={type:o,...P};return e(B)}return `{ ${Object.entries(s).map(([o,m])=>`${N(o)?o:`"${o}"`}: ${r(m)}`).join(", ")} }`};return r(t)}var u=", ",l=null,e=t=>{if(t==null)return "";if(Array.isArray(t))return t.filter(Boolean).map(e).join(u);if(typeof t!="object")return String(t);if(!t.type)return JSON.stringify(t);if(t.type in S){if(l){let r=capability.nodeViolation(t,l);if(r)throw new core.TSLiteError("node-not-allowed",`'${r}' is not allowed by the active capability profile.`,{type:t.type})}return S[t.type](t)}throw Error("Unknown type: "+t.type)},L=(t,r)=>{t&&typeof t=="object"&&!Array.isArray(t)&&sanitize.assertSafe(t,r.sanitize==="hermetic"?sanitize.HERMETIC_POLICY:void 0);let n=l;l=capability.compileProfile(r);try{return e(t)}finally{l=n;}},c=t=>`(${t})`;function g(t){if(O.has(t.type))return true;if(t.type!=="Literal")return false;let r=t;return !(typeof r.value=="number"||r.bigint!=null)}var O=new Set(["Identifier","ThisExpression","ArrayExpression","MemberExpression","CallExpression","NewExpression","TemplateLiteral"]),y=t=>{if(t.type==="TSAsExpression")return y(t.expression);let r=e(t);return g(t)?r:c(r)},v=t=>`[${e(t.elements)}]`,J=t=>`[${t.elements.map(n=>n?e(n):"").join(", ")}]`,R=t=>{let r=e(t.params),n=t.body.type=="AssignmentExpression"||t.body.type=="BinaryExpression"||t.body.type=="ObjectExpression"||t.body.type=="JsonExpression"?c(e(t.body)):e(t.body);return `${t.async?"async ":""}(${r}) => ${n}`},W=t=>{let r=e(t.left),n=e(t.right);return `${r} ${t.operator} ${n}`},_=t=>"await "+a(t.argument,core.Precedence.Await,false),$=t=>t.type==="TSAsExpression"?$(t.expression):t,U=t=>{let r=$(t);return r.type==="UnaryExpression"||r.type==="AwaitExpression"},q=(t,r)=>{let n=$(r);if(n.type!=="LogicalExpression")return false;let s=n.operator;return t==="??"?s==="||"||s==="&&":t==="||"||t==="&&"?s==="??":false},V=t=>{let r=core.BinaryPrecedence[t.operator]??1/0,n=t.operator==="**",s=a(t.left,r,false,n),p=n&&(U(t.left)||s[0]==="-"||s[0]==="+")?c(s):s,o=a(t.right,r,true,n);return `${p} ${t.operator} ${o}`},M=t=>{let r=t.body.map(e).filter(n=>n&&n!==";").join("; ");return `{ ${r?r+"; ":""}}`},X=t=>t.label?`break ${e(t.label)}`:"break",z=t=>{let r=y(t.callee),n=t.optional?"?.":"";return `${r}${n}(${e(t.arguments)})`},Y=t=>e(t.expression),h=t=>t.type==="BinaryExpression"||t.type==="LogicalExpression"?core.BinaryPrecedence[t.operator]??1/0:t.type==="ConditionalExpression"?core.Precedence.Conditional:t.type==="AssignmentExpression"?core.Precedence.Assignment:t.type==="ArrowFunctionExpression"?core.Precedence.ArrowFunction:t.type==="AwaitExpression"?core.Precedence.Await:t.type==="SequenceExpression"?core.Precedence.Sequence:t.type==="TSAsExpression"?h(t.expression):1/0,a=(t,r,n,s=false)=>{let p=e(t),o=h(t);return (n?s?o<r:o<=r:s?o<=r:o<r)?c(p):p},K=t=>`${a(t.test,core.Precedence.Conditional,false,true)} ? ${e(t.consequent)} : ${e(t.alternate)}`,H=t=>t.label?`continue ${e(t.label)}`:"continue",G=t=>{let r=e(t.expression);return r[0]==="{"||r.startsWith("function")||r.startsWith("class")?c(r):r},Q=t=>";",Z=t=>`do ${e(t.body)} while (${e(t.test)})`,d=t=>`for (${e(t.left)} in ${e(t.right)}) ${e(t.body)}`,tt=t=>`for${t.await?" await":""} (${e(t.left)} of ${e(t.right)}) ${e(t.body)}`,et=t=>{let r=e(t.init),n=e(t.test),s=e(t.update);return `for (${r};${n?` ${n}`:""};${s?` ${s}`:""}) ${e(t.body)}`},rt=t=>{let r=t.params.map(b).join(u);return `${t.async?"async ":""}function ${e(t.id)}(${r}) ${e(t.body)}`},nt=t=>{let r=e(t.params);return `${t.async?"async ":""}function ${e(t.id)}(${r}) ${e(t.body)}`},b=t=>t.name,st=t=>{throw new core.TSLiteError("incomplete-tree","Cannot print a tree with a Placeholder (hole) \u2014 fill the holes first.",{type:t.type})},ot=t=>`if (${e(t.test)}) ${e(t.consequent)}${t.alternate?` else ${e(t.alternate)}`:""}`,it=t=>f(t.body),pt=t=>t.raw?t.raw:t.value===void 0?"undefined":t.value===null?"null":typeof t.value=="string"?JSON.stringify(t.value):typeof t.value=="boolean"?String(t.value):t.bigint?t.bigint+"n":t.regex?`/${t.regex.pattern}/${t.regex.flags}`:(t.value instanceof RegExp,String(t.value)),at=t=>{let r=core.BinaryPrecedence[t.operator]??1/0,n=(s,p)=>q(t.operator,s)?c(e(s)):a(s,r,p);return `${n(t.left,false)} ${t.operator} ${n(t.right,true)}`},ct=t=>{let r=[y(t.object)];return t.optional&&r.push(t.computed?"?.":"?"),r.push(t.computed?"[":"."),r.push(e(t.property)),t.computed&&r.push("]"),r.join("")},lt=t=>`new ${y(t.callee)}(${e(t.arguments)})`,ut=t=>{let r=t.properties.map(e).join(", ");return r.length==0?"{}":`{ ${r} }`},yt=t=>{let r=t.properties.map(e).join(", ");return r.length==0?"{ }":`{ ${r} }`},mt=new Set(["FunctionDeclaration","IfStatement","ForStatement","ForInStatement","ForOfStatement","WhileStatement","TryStatement","SwitchStatement","ClassDeclaration"]),ft=t=>t.body.map(r=>{let n=e(r);return n?mt.has(r.type)?n:n+";":""}).filter(Boolean).join(" "),xt=t=>t.shorthand?e(t.value):`${t.computed?`[${e(t.key)}]`:e(t.key)}: ${e(t.value)}`,$t=t=>`${e(t.left)} = ${e(t.right)}`,St=t=>"return "+e(t.argument),Et=t=>(t.test?`case ${e(t.test)}: `:" default: ")+t.consequent.map(e).join("; "),Tt=t=>`switch (${e(t.discriminant)}) { ${t.cases.map(e).join("; ")}; }`,gt=t=>t.value.raw,ht=t=>"`"+t.quasis.map((r,n)=>e(r)+(r.tail?"":"${"+e(t.expressions[n])+"}")).join("")+"`",bt=t=>"throw "+e(t.argument),wt=t=>{let r=`try ${e(t.block)}`;if(t.handler){let n=t.handler.param;r+=n?` catch (${e(n)}) ${e(t.handler.body)}`:` catch ${e(t.handler.body)}`;}return r+(t.finalizer?` finally ${e(t.finalizer)}`:"")},At=t=>{let r=a(t.argument,core.Precedence.Unary,false),n=t.operator.length>1||(t.operator==="+"||t.operator==="-")&&r[0]===t.operator?" ":"";return t.operator+n+r},It=t=>t.prefix==true?t.operator+e(t.argument):e(t.argument)+t.operator,Pt=t=>`${t.kind} ${t.declarations.map(w).join(", ")}`,w=t=>t.init?`${e(t.id)} = ${e(t.init)}`:e(t.id),Bt=t=>`while (${e(t.test)}) ${e(t.body)}`,jt=t=>{let r=e(t.source);return t.specifiers.length==0?`import ${r}`:t.specifiers[0].type=="ImportNamespaceSpecifier"?`import ${e(t.specifiers[0])} from ${r}`:t.specifiers[0].type=="ImportDefaultSpecifier"?`import ${e(t.specifiers[0])} from ${r}`:`import { ${t.specifiers.filter(s=>s.type==="ImportSpecifier").map(A).join(u)} } from ${r}`},Nt=t=>e(t.local),Ct=t=>`* as ${e(t.local)}`,A=t=>t.imported.name===t.local.name?`${e(t.local)}`:`${e(t.imported)} as ${e(t.local)}`,Dt=t=>t.declaration?`export ${e(t.declaration)}`:`export { ${t.specifiers.map(I).join(u)} }`,Ft=t=>`export default ${e(t.declaration)}`,I=t=>t.local.name===t.exported.name?`${e(t.local)}`:`${e(t.local)} as ${e(t.exported)}`,kt=t=>`...${e(t.argument)}`,Lt=t=>`...${e(t.argument)}`,Ot=()=>"",vt=t=>e(t.expression),Jt=t=>e(core.desugarJSX(t)),Rt=t=>e(core.desugarJSX(t)),S={print:e,JSXElement:Jt,JSXFragment:Rt,ArrayExpression:v,ArrayPattern:J,ArrowFunctionExpression:R,AssignmentExpression:W,AssignmentPattern:$t,AwaitExpression:_,BinaryExpression:V,BlockStatement:M,BreakStatement:X,CallExpression:z,ChainExpression:Y,ConditionalExpression:K,ContinueStatement:H,DoWhileStatement:Z,EmptyStatement:Q,ExpressionStatement:G,ForInStatement:d,ForOfStatement:tt,ForStatement:et,FunctionDeclaration:rt,FunctionExpression:nt,Identifier:b,Placeholder:st,IfStatement:ot,ImportDeclaration:jt,ImportDefaultSpecifier:Nt,ImportNamespaceSpecifier:Ct,ImportSpecifier:A,ExportNamedDeclaration:Dt,ExportDefaultDeclaration:Ft,ExportSpecifier:I,JsonExpression:it,Literal:pt,LogicalExpression:at,MemberExpression:ct,NewExpression:lt,ObjectExpression:ut,ObjectPattern:yt,Program:ft,Property:xt,ReturnStatement:St,SwitchCase:Et,SwitchStatement:Tt,TemplateElement:gt,TemplateLiteral:ht,ThrowStatement:bt,TryStatement:wt,UnaryExpression:At,UpdateExpression:It,VariableDeclaration:Pt,VariableDeclarator:w,WhileStatement:Bt,SpreadElement:kt,RestElement:Lt,TSTypeAliasDeclaration:Ot,TSAsExpression:vt};exports.isPrimaryOperand=g;exports.print=e;exports.printJson=f;exports.printWithProfile=L;
1
+ 'use strict';var core=require('@tslite/core'),capability=require('@tslite/capability'),sanitize=require('@tslite/sanitize');var C=t=>core.VALID_OBJECT_KEY.test(t);function m(t){let r=n=>{if(typeof n=="string")return `"${n}"`;if(typeof n!="object")return String(n);if(n===null)return "null";if(Array.isArray(n))return `[${n.map(r).join(",")}]`;let s=n;if("#expr"in s){let o=s["#expr"],{"#expr":y,...P}=s,B={type:o,...P};return e(B)}return `{ ${Object.entries(s).map(([o,y])=>`${C(o)?o:`"${o}"`}: ${r(y)}`).join(", ")} }`};return r(t)}var u=", ",l=null,e=t=>{if(t==null)return "";if(Array.isArray(t))return t.filter(Boolean).map(e).join(u);if(typeof t!="object")return String(t);if(!t.type)return JSON.stringify(t);if(t.type in S){if(l){let r=capability.nodeViolation(t,l);if(r)throw new core.TSLiteError("node-not-allowed",`'${r}' is not allowed by the active capability profile.`,{type:t.type})}return S[t.type](t)}throw Error("Unknown type: "+t.type)},L=(t,r)=>{t&&typeof t=="object"&&!Array.isArray(t)&&sanitize.assertSafe(t,r.sanitize==="hermetic"?sanitize.HERMETIC_POLICY:void 0);let n=l;l=capability.compileProfile(r);try{return e(t)}finally{l=n;}},c=t=>`(${t})`;function g(t){if(v.has(t.type))return true;if(t.type!=="Literal")return false;let r=t;return !(typeof r.value=="number"||r.bigint!=null)}var v=new Set(["Identifier","ThisExpression","ArrayExpression","MemberExpression","CallExpression","TemplateLiteral"]),x=t=>{if(t.type==="TSAsExpression")return x(t.expression);let r=e(t);return g(t)?r:c(r)},O=t=>`[${e(t.elements)}]`,J=t=>`[${t.elements.map(n=>n?e(n):"").join(", ")}]`,R=t=>{let r=e(t.params),n=e(t.body),s=t.body.type!=="BlockStatement"&&n.startsWith("{"),i=t.body.type=="AssignmentExpression"||t.body.type=="BinaryExpression"||t.body.type=="ObjectExpression"||t.body.type=="JsonExpression"||s?c(n):n;return `${t.async?"async ":""}(${r}) => ${i}`},W=t=>{let r=e(t.left),n=e(t.right);return `${r} ${t.operator} ${n}`},_=t=>"await "+a(t.argument,core.Precedence.Await,false),$=t=>t.type==="TSAsExpression"?$(t.expression):t,U=t=>{let r=$(t);return r.type==="UnaryExpression"||r.type==="AwaitExpression"},q=(t,r)=>{let n=$(r);if(n.type!=="LogicalExpression")return false;let s=n.operator;return t==="??"?s==="||"||s==="&&":t==="||"||t==="&&"?s==="??":false},V=t=>{let r=core.BinaryPrecedence[t.operator]??1/0,n=t.operator==="**",s=a(t.left,r,false,n),i=n&&(U(t.left)||s[0]==="-"||s[0]==="+")?c(s):s,o=a(t.right,r,true,n);return `${i} ${t.operator} ${o}`},M=t=>{let r=t.body.map(e).filter(n=>n&&n!==";").join("; ");return `{ ${r?r+"; ":""}}`},X=t=>t.label?`break ${e(t.label)}`:"break",z=t=>{let r=x(t.callee),n=t.optional?"?.":"";return `${r}${n}(${e(t.arguments)})`},Y=t=>e(t.expression),h=t=>t.type==="BinaryExpression"||t.type==="LogicalExpression"?core.BinaryPrecedence[t.operator]??1/0:t.type==="ConditionalExpression"?core.Precedence.Conditional:t.type==="AssignmentExpression"?core.Precedence.Assignment:t.type==="ArrowFunctionExpression"?core.Precedence.ArrowFunction:t.type==="AwaitExpression"?core.Precedence.Await:t.type==="SequenceExpression"?core.Precedence.Sequence:t.type==="TSAsExpression"?h(t.expression):1/0,a=(t,r,n,s=false)=>{let i=e(t),o=h(t);return (n?s?o<r:o<=r:s?o<=r:o<r)?c(i):i},K=t=>`${a(t.test,core.Precedence.Conditional,false,true)} ? ${e(t.consequent)} : ${e(t.alternate)}`,H=t=>t.label?`continue ${e(t.label)}`:"continue",G=t=>{let r=e(t.expression);return r[0]==="{"||r.startsWith("function")||r.startsWith("class")?c(r):r},Q=t=>";",Z=t=>`do ${e(t.body)} while (${e(t.test)})`,d=t=>`for (${e(t.left)} in ${e(t.right)}) ${e(t.body)}`,tt=t=>`for${t.await?" await":""} (${e(t.left)} of ${e(t.right)}) ${e(t.body)}`,et=t=>{let r=e(t.init),n=e(t.test),s=e(t.update);return `for (${r};${n?` ${n}`:""};${s?` ${s}`:""}) ${e(t.body)}`},rt=t=>{let r=t.params.map(b).join(u);return `${t.async?"async ":""}function ${e(t.id)}(${r}) ${e(t.body)}`},nt=t=>{let r=e(t.params);return `${t.async?"async ":""}function ${e(t.id)}(${r}) ${e(t.body)}`},b=t=>t.name,st=t=>{throw new core.TSLiteError("incomplete-tree","Cannot print a tree with a Placeholder (hole) \u2014 fill the holes first.",{type:t.type})},ot=t=>`if (${e(t.test)}) ${e(t.consequent)}${t.alternate?` else ${e(t.alternate)}`:""}`,it=t=>m(t.body),pt=t=>t.raw?t.raw:t.value===void 0?"undefined":t.value===null?"null":typeof t.value=="string"?JSON.stringify(t.value):typeof t.value=="boolean"?String(t.value):t.bigint?t.bigint+"n":t.regex?`/${t.regex.pattern}/${t.regex.flags}`:(t.value instanceof RegExp,String(t.value)),at=t=>{let r=core.BinaryPrecedence[t.operator]??1/0,n=(s,i)=>q(t.operator,s)?c(e(s)):a(s,r,i);return `${n(t.left,false)} ${t.operator} ${n(t.right,true)}`},ct=t=>{let r=[x(t.object)];return t.optional&&r.push(t.computed?"?.":"?"),r.push(t.computed?"[":"."),r.push(e(t.property)),t.computed&&r.push("]"),r.join("")},lt=t=>{let r=t.properties.map(e).join(", ");return r.length==0?"{}":`{ ${r} }`},ut=t=>{let r=t.properties.map(e).join(", ");return r.length==0?"{ }":`{ ${r} }`},yt=new Set(["FunctionDeclaration","IfStatement","ForStatement","ForInStatement","ForOfStatement","WhileStatement","TryStatement","SwitchStatement","ClassDeclaration"]),mt=t=>t.body.map(r=>{let n=e(r);return n?yt.has(r.type)?n:n+";":""}).filter(Boolean).join(" "),ft=t=>t.shorthand?e(t.value):`${t.computed?`[${e(t.key)}]`:e(t.key)}: ${e(t.value)}`,xt=t=>`${e(t.left)} = ${e(t.right)}`,$t=t=>"return "+e(t.argument),St=t=>(t.test?`case ${e(t.test)}: `:" default: ")+t.consequent.map(e).join("; "),Et=t=>`switch (${e(t.discriminant)}) { ${t.cases.map(e).join("; ")}; }`,Tt=t=>t.value.raw,gt=t=>"`"+t.quasis.map((r,n)=>e(r)+(r.tail?"":"${"+e(t.expressions[n])+"}")).join("")+"`",ht=t=>"throw "+e(t.argument),bt=t=>{let r=`try ${e(t.block)}`;if(t.handler){let n=t.handler.param;r+=n?` catch (${e(n)}) ${e(t.handler.body)}`:` catch ${e(t.handler.body)}`;}return r+(t.finalizer?` finally ${e(t.finalizer)}`:"")},wt=t=>{let r=a(t.argument,core.Precedence.Unary,false),n=t.operator.length>1||(t.operator==="+"||t.operator==="-")&&r[0]===t.operator?" ":"";return t.operator+n+r},At=t=>t.prefix==true?t.operator+e(t.argument):e(t.argument)+t.operator,It=t=>`${t.kind} ${t.declarations.map(w).join(", ")}`,w=t=>t.init?`${e(t.id)} = ${e(t.init)}`:e(t.id),Pt=t=>`while (${e(t.test)}) ${e(t.body)}`,Bt=t=>{let r=e(t.source);return t.specifiers.length==0?`import ${r}`:t.specifiers[0].type=="ImportNamespaceSpecifier"?`import ${e(t.specifiers[0])} from ${r}`:t.specifiers[0].type=="ImportDefaultSpecifier"?`import ${e(t.specifiers[0])} from ${r}`:`import { ${t.specifiers.filter(s=>s.type==="ImportSpecifier").map(A).join(u)} } from ${r}`},jt=t=>e(t.local),Ct=t=>`* as ${e(t.local)}`,A=t=>t.imported.name===t.local.name?`${e(t.local)}`:`${e(t.imported)} as ${e(t.local)}`,Nt=t=>t.declaration?`export ${e(t.declaration)}`:`export { ${t.specifiers.map(I).join(u)} }`,Dt=t=>`export default ${e(t.declaration)}`,I=t=>t.local.name===t.exported.name?`${e(t.local)}`:`${e(t.local)} as ${e(t.exported)}`,Ft=t=>`...${e(t.argument)}`,kt=t=>`...${e(t.argument)}`,Lt=()=>"",vt=t=>e(t.expression),Ot=t=>e(core.desugarJSX(t)),Jt=t=>e(core.desugarJSX(t)),S={print:e,JSXElement:Ot,JSXFragment:Jt,ArrayExpression:O,ArrayPattern:J,ArrowFunctionExpression:R,AssignmentExpression:W,AssignmentPattern:xt,AwaitExpression:_,BinaryExpression:V,BlockStatement:M,BreakStatement:X,CallExpression:z,ChainExpression:Y,ConditionalExpression:K,ContinueStatement:H,DoWhileStatement:Z,EmptyStatement:Q,ExpressionStatement:G,ForInStatement:d,ForOfStatement:tt,ForStatement:et,FunctionDeclaration:rt,FunctionExpression:nt,Identifier:b,Placeholder:st,IfStatement:ot,ImportDeclaration:Bt,ImportDefaultSpecifier:jt,ImportNamespaceSpecifier:Ct,ImportSpecifier:A,ExportNamedDeclaration:Nt,ExportDefaultDeclaration:Dt,ExportSpecifier:I,JsonExpression:it,Literal:pt,LogicalExpression:at,MemberExpression:ct,ObjectExpression:lt,ObjectPattern:ut,Program:mt,Property:ft,ReturnStatement:$t,SwitchCase:St,SwitchStatement:Et,TemplateElement:Tt,TemplateLiteral:gt,ThrowStatement:ht,TryStatement:bt,UnaryExpression:wt,UpdateExpression:At,VariableDeclaration:It,VariableDeclarator:w,WhileStatement:Pt,SpreadElement:Ft,RestElement:kt,TSTypeAliasDeclaration:Lt,TSAsExpression:vt};exports.isPrimaryOperand=g;exports.print=e;exports.printJson=m;exports.printWithProfile=L;
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- import {Precedence,BinaryPrecedence,TSLiteError,desugarJSX,VALID_OBJECT_KEY}from'@tslite/core';import {compileProfile,nodeViolation}from'@tslite/capability';import {assertSafe,HERMETIC_POLICY}from'@tslite/sanitize';var N=t=>VALID_OBJECT_KEY.test(t);function f(t){let r=n=>{if(typeof n=="string")return `"${n}"`;if(typeof n!="object")return String(n);if(n===null)return "null";if(Array.isArray(n))return `[${n.map(r).join(",")}]`;let s=n;if("#expr"in s){let o=s["#expr"],{"#expr":m,...P}=s,B={type:o,...P};return e(B)}return `{ ${Object.entries(s).map(([o,m])=>`${N(o)?o:`"${o}"`}: ${r(m)}`).join(", ")} }`};return r(t)}var u=", ",l=null,e=t=>{if(t==null)return "";if(Array.isArray(t))return t.filter(Boolean).map(e).join(u);if(typeof t!="object")return String(t);if(!t.type)return JSON.stringify(t);if(t.type in S){if(l){let r=nodeViolation(t,l);if(r)throw new TSLiteError("node-not-allowed",`'${r}' is not allowed by the active capability profile.`,{type:t.type})}return S[t.type](t)}throw Error("Unknown type: "+t.type)},L=(t,r)=>{t&&typeof t=="object"&&!Array.isArray(t)&&assertSafe(t,r.sanitize==="hermetic"?HERMETIC_POLICY:void 0);let n=l;l=compileProfile(r);try{return e(t)}finally{l=n;}},c=t=>`(${t})`;function g(t){if(O.has(t.type))return true;if(t.type!=="Literal")return false;let r=t;return !(typeof r.value=="number"||r.bigint!=null)}var O=new Set(["Identifier","ThisExpression","ArrayExpression","MemberExpression","CallExpression","NewExpression","TemplateLiteral"]),y=t=>{if(t.type==="TSAsExpression")return y(t.expression);let r=e(t);return g(t)?r:c(r)},v=t=>`[${e(t.elements)}]`,J=t=>`[${t.elements.map(n=>n?e(n):"").join(", ")}]`,R=t=>{let r=e(t.params),n=t.body.type=="AssignmentExpression"||t.body.type=="BinaryExpression"||t.body.type=="ObjectExpression"||t.body.type=="JsonExpression"?c(e(t.body)):e(t.body);return `${t.async?"async ":""}(${r}) => ${n}`},W=t=>{let r=e(t.left),n=e(t.right);return `${r} ${t.operator} ${n}`},_=t=>"await "+a(t.argument,Precedence.Await,false),$=t=>t.type==="TSAsExpression"?$(t.expression):t,U=t=>{let r=$(t);return r.type==="UnaryExpression"||r.type==="AwaitExpression"},q=(t,r)=>{let n=$(r);if(n.type!=="LogicalExpression")return false;let s=n.operator;return t==="??"?s==="||"||s==="&&":t==="||"||t==="&&"?s==="??":false},V=t=>{let r=BinaryPrecedence[t.operator]??1/0,n=t.operator==="**",s=a(t.left,r,false,n),p=n&&(U(t.left)||s[0]==="-"||s[0]==="+")?c(s):s,o=a(t.right,r,true,n);return `${p} ${t.operator} ${o}`},M=t=>{let r=t.body.map(e).filter(n=>n&&n!==";").join("; ");return `{ ${r?r+"; ":""}}`},X=t=>t.label?`break ${e(t.label)}`:"break",z=t=>{let r=y(t.callee),n=t.optional?"?.":"";return `${r}${n}(${e(t.arguments)})`},Y=t=>e(t.expression),h=t=>t.type==="BinaryExpression"||t.type==="LogicalExpression"?BinaryPrecedence[t.operator]??1/0:t.type==="ConditionalExpression"?Precedence.Conditional:t.type==="AssignmentExpression"?Precedence.Assignment:t.type==="ArrowFunctionExpression"?Precedence.ArrowFunction:t.type==="AwaitExpression"?Precedence.Await:t.type==="SequenceExpression"?Precedence.Sequence:t.type==="TSAsExpression"?h(t.expression):1/0,a=(t,r,n,s=false)=>{let p=e(t),o=h(t);return (n?s?o<r:o<=r:s?o<=r:o<r)?c(p):p},K=t=>`${a(t.test,Precedence.Conditional,false,true)} ? ${e(t.consequent)} : ${e(t.alternate)}`,H=t=>t.label?`continue ${e(t.label)}`:"continue",G=t=>{let r=e(t.expression);return r[0]==="{"||r.startsWith("function")||r.startsWith("class")?c(r):r},Q=t=>";",Z=t=>`do ${e(t.body)} while (${e(t.test)})`,d=t=>`for (${e(t.left)} in ${e(t.right)}) ${e(t.body)}`,tt=t=>`for${t.await?" await":""} (${e(t.left)} of ${e(t.right)}) ${e(t.body)}`,et=t=>{let r=e(t.init),n=e(t.test),s=e(t.update);return `for (${r};${n?` ${n}`:""};${s?` ${s}`:""}) ${e(t.body)}`},rt=t=>{let r=t.params.map(b).join(u);return `${t.async?"async ":""}function ${e(t.id)}(${r}) ${e(t.body)}`},nt=t=>{let r=e(t.params);return `${t.async?"async ":""}function ${e(t.id)}(${r}) ${e(t.body)}`},b=t=>t.name,st=t=>{throw new TSLiteError("incomplete-tree","Cannot print a tree with a Placeholder (hole) \u2014 fill the holes first.",{type:t.type})},ot=t=>`if (${e(t.test)}) ${e(t.consequent)}${t.alternate?` else ${e(t.alternate)}`:""}`,it=t=>f(t.body),pt=t=>t.raw?t.raw:t.value===void 0?"undefined":t.value===null?"null":typeof t.value=="string"?JSON.stringify(t.value):typeof t.value=="boolean"?String(t.value):t.bigint?t.bigint+"n":t.regex?`/${t.regex.pattern}/${t.regex.flags}`:(t.value instanceof RegExp,String(t.value)),at=t=>{let r=BinaryPrecedence[t.operator]??1/0,n=(s,p)=>q(t.operator,s)?c(e(s)):a(s,r,p);return `${n(t.left,false)} ${t.operator} ${n(t.right,true)}`},ct=t=>{let r=[y(t.object)];return t.optional&&r.push(t.computed?"?.":"?"),r.push(t.computed?"[":"."),r.push(e(t.property)),t.computed&&r.push("]"),r.join("")},lt=t=>`new ${y(t.callee)}(${e(t.arguments)})`,ut=t=>{let r=t.properties.map(e).join(", ");return r.length==0?"{}":`{ ${r} }`},yt=t=>{let r=t.properties.map(e).join(", ");return r.length==0?"{ }":`{ ${r} }`},mt=new Set(["FunctionDeclaration","IfStatement","ForStatement","ForInStatement","ForOfStatement","WhileStatement","TryStatement","SwitchStatement","ClassDeclaration"]),ft=t=>t.body.map(r=>{let n=e(r);return n?mt.has(r.type)?n:n+";":""}).filter(Boolean).join(" "),xt=t=>t.shorthand?e(t.value):`${t.computed?`[${e(t.key)}]`:e(t.key)}: ${e(t.value)}`,$t=t=>`${e(t.left)} = ${e(t.right)}`,St=t=>"return "+e(t.argument),Et=t=>(t.test?`case ${e(t.test)}: `:" default: ")+t.consequent.map(e).join("; "),Tt=t=>`switch (${e(t.discriminant)}) { ${t.cases.map(e).join("; ")}; }`,gt=t=>t.value.raw,ht=t=>"`"+t.quasis.map((r,n)=>e(r)+(r.tail?"":"${"+e(t.expressions[n])+"}")).join("")+"`",bt=t=>"throw "+e(t.argument),wt=t=>{let r=`try ${e(t.block)}`;if(t.handler){let n=t.handler.param;r+=n?` catch (${e(n)}) ${e(t.handler.body)}`:` catch ${e(t.handler.body)}`;}return r+(t.finalizer?` finally ${e(t.finalizer)}`:"")},At=t=>{let r=a(t.argument,Precedence.Unary,false),n=t.operator.length>1||(t.operator==="+"||t.operator==="-")&&r[0]===t.operator?" ":"";return t.operator+n+r},It=t=>t.prefix==true?t.operator+e(t.argument):e(t.argument)+t.operator,Pt=t=>`${t.kind} ${t.declarations.map(w).join(", ")}`,w=t=>t.init?`${e(t.id)} = ${e(t.init)}`:e(t.id),Bt=t=>`while (${e(t.test)}) ${e(t.body)}`,jt=t=>{let r=e(t.source);return t.specifiers.length==0?`import ${r}`:t.specifiers[0].type=="ImportNamespaceSpecifier"?`import ${e(t.specifiers[0])} from ${r}`:t.specifiers[0].type=="ImportDefaultSpecifier"?`import ${e(t.specifiers[0])} from ${r}`:`import { ${t.specifiers.filter(s=>s.type==="ImportSpecifier").map(A).join(u)} } from ${r}`},Nt=t=>e(t.local),Ct=t=>`* as ${e(t.local)}`,A=t=>t.imported.name===t.local.name?`${e(t.local)}`:`${e(t.imported)} as ${e(t.local)}`,Dt=t=>t.declaration?`export ${e(t.declaration)}`:`export { ${t.specifiers.map(I).join(u)} }`,Ft=t=>`export default ${e(t.declaration)}`,I=t=>t.local.name===t.exported.name?`${e(t.local)}`:`${e(t.local)} as ${e(t.exported)}`,kt=t=>`...${e(t.argument)}`,Lt=t=>`...${e(t.argument)}`,Ot=()=>"",vt=t=>e(t.expression),Jt=t=>e(desugarJSX(t)),Rt=t=>e(desugarJSX(t)),S={print:e,JSXElement:Jt,JSXFragment:Rt,ArrayExpression:v,ArrayPattern:J,ArrowFunctionExpression:R,AssignmentExpression:W,AssignmentPattern:$t,AwaitExpression:_,BinaryExpression:V,BlockStatement:M,BreakStatement:X,CallExpression:z,ChainExpression:Y,ConditionalExpression:K,ContinueStatement:H,DoWhileStatement:Z,EmptyStatement:Q,ExpressionStatement:G,ForInStatement:d,ForOfStatement:tt,ForStatement:et,FunctionDeclaration:rt,FunctionExpression:nt,Identifier:b,Placeholder:st,IfStatement:ot,ImportDeclaration:jt,ImportDefaultSpecifier:Nt,ImportNamespaceSpecifier:Ct,ImportSpecifier:A,ExportNamedDeclaration:Dt,ExportDefaultDeclaration:Ft,ExportSpecifier:I,JsonExpression:it,Literal:pt,LogicalExpression:at,MemberExpression:ct,NewExpression:lt,ObjectExpression:ut,ObjectPattern:yt,Program:ft,Property:xt,ReturnStatement:St,SwitchCase:Et,SwitchStatement:Tt,TemplateElement:gt,TemplateLiteral:ht,ThrowStatement:bt,TryStatement:wt,UnaryExpression:At,UpdateExpression:It,VariableDeclaration:Pt,VariableDeclarator:w,WhileStatement:Bt,SpreadElement:kt,RestElement:Lt,TSTypeAliasDeclaration:Ot,TSAsExpression:vt};export{g as isPrimaryOperand,e as print,f as printJson,L as printWithProfile};
1
+ import {Precedence,BinaryPrecedence,TSLiteError,desugarJSX,VALID_OBJECT_KEY}from'@tslite/core';import {compileProfile,nodeViolation}from'@tslite/capability';import {assertSafe,HERMETIC_POLICY}from'@tslite/sanitize';var C=t=>VALID_OBJECT_KEY.test(t);function m(t){let r=n=>{if(typeof n=="string")return `"${n}"`;if(typeof n!="object")return String(n);if(n===null)return "null";if(Array.isArray(n))return `[${n.map(r).join(",")}]`;let s=n;if("#expr"in s){let o=s["#expr"],{"#expr":y,...P}=s,B={type:o,...P};return e(B)}return `{ ${Object.entries(s).map(([o,y])=>`${C(o)?o:`"${o}"`}: ${r(y)}`).join(", ")} }`};return r(t)}var u=", ",l=null,e=t=>{if(t==null)return "";if(Array.isArray(t))return t.filter(Boolean).map(e).join(u);if(typeof t!="object")return String(t);if(!t.type)return JSON.stringify(t);if(t.type in S){if(l){let r=nodeViolation(t,l);if(r)throw new TSLiteError("node-not-allowed",`'${r}' is not allowed by the active capability profile.`,{type:t.type})}return S[t.type](t)}throw Error("Unknown type: "+t.type)},L=(t,r)=>{t&&typeof t=="object"&&!Array.isArray(t)&&assertSafe(t,r.sanitize==="hermetic"?HERMETIC_POLICY:void 0);let n=l;l=compileProfile(r);try{return e(t)}finally{l=n;}},c=t=>`(${t})`;function g(t){if(v.has(t.type))return true;if(t.type!=="Literal")return false;let r=t;return !(typeof r.value=="number"||r.bigint!=null)}var v=new Set(["Identifier","ThisExpression","ArrayExpression","MemberExpression","CallExpression","TemplateLiteral"]),x=t=>{if(t.type==="TSAsExpression")return x(t.expression);let r=e(t);return g(t)?r:c(r)},O=t=>`[${e(t.elements)}]`,J=t=>`[${t.elements.map(n=>n?e(n):"").join(", ")}]`,R=t=>{let r=e(t.params),n=e(t.body),s=t.body.type!=="BlockStatement"&&n.startsWith("{"),i=t.body.type=="AssignmentExpression"||t.body.type=="BinaryExpression"||t.body.type=="ObjectExpression"||t.body.type=="JsonExpression"||s?c(n):n;return `${t.async?"async ":""}(${r}) => ${i}`},W=t=>{let r=e(t.left),n=e(t.right);return `${r} ${t.operator} ${n}`},_=t=>"await "+a(t.argument,Precedence.Await,false),$=t=>t.type==="TSAsExpression"?$(t.expression):t,U=t=>{let r=$(t);return r.type==="UnaryExpression"||r.type==="AwaitExpression"},q=(t,r)=>{let n=$(r);if(n.type!=="LogicalExpression")return false;let s=n.operator;return t==="??"?s==="||"||s==="&&":t==="||"||t==="&&"?s==="??":false},V=t=>{let r=BinaryPrecedence[t.operator]??1/0,n=t.operator==="**",s=a(t.left,r,false,n),i=n&&(U(t.left)||s[0]==="-"||s[0]==="+")?c(s):s,o=a(t.right,r,true,n);return `${i} ${t.operator} ${o}`},M=t=>{let r=t.body.map(e).filter(n=>n&&n!==";").join("; ");return `{ ${r?r+"; ":""}}`},X=t=>t.label?`break ${e(t.label)}`:"break",z=t=>{let r=x(t.callee),n=t.optional?"?.":"";return `${r}${n}(${e(t.arguments)})`},Y=t=>e(t.expression),h=t=>t.type==="BinaryExpression"||t.type==="LogicalExpression"?BinaryPrecedence[t.operator]??1/0:t.type==="ConditionalExpression"?Precedence.Conditional:t.type==="AssignmentExpression"?Precedence.Assignment:t.type==="ArrowFunctionExpression"?Precedence.ArrowFunction:t.type==="AwaitExpression"?Precedence.Await:t.type==="SequenceExpression"?Precedence.Sequence:t.type==="TSAsExpression"?h(t.expression):1/0,a=(t,r,n,s=false)=>{let i=e(t),o=h(t);return (n?s?o<r:o<=r:s?o<=r:o<r)?c(i):i},K=t=>`${a(t.test,Precedence.Conditional,false,true)} ? ${e(t.consequent)} : ${e(t.alternate)}`,H=t=>t.label?`continue ${e(t.label)}`:"continue",G=t=>{let r=e(t.expression);return r[0]==="{"||r.startsWith("function")||r.startsWith("class")?c(r):r},Q=t=>";",Z=t=>`do ${e(t.body)} while (${e(t.test)})`,d=t=>`for (${e(t.left)} in ${e(t.right)}) ${e(t.body)}`,tt=t=>`for${t.await?" await":""} (${e(t.left)} of ${e(t.right)}) ${e(t.body)}`,et=t=>{let r=e(t.init),n=e(t.test),s=e(t.update);return `for (${r};${n?` ${n}`:""};${s?` ${s}`:""}) ${e(t.body)}`},rt=t=>{let r=t.params.map(b).join(u);return `${t.async?"async ":""}function ${e(t.id)}(${r}) ${e(t.body)}`},nt=t=>{let r=e(t.params);return `${t.async?"async ":""}function ${e(t.id)}(${r}) ${e(t.body)}`},b=t=>t.name,st=t=>{throw new TSLiteError("incomplete-tree","Cannot print a tree with a Placeholder (hole) \u2014 fill the holes first.",{type:t.type})},ot=t=>`if (${e(t.test)}) ${e(t.consequent)}${t.alternate?` else ${e(t.alternate)}`:""}`,it=t=>m(t.body),pt=t=>t.raw?t.raw:t.value===void 0?"undefined":t.value===null?"null":typeof t.value=="string"?JSON.stringify(t.value):typeof t.value=="boolean"?String(t.value):t.bigint?t.bigint+"n":t.regex?`/${t.regex.pattern}/${t.regex.flags}`:(t.value instanceof RegExp,String(t.value)),at=t=>{let r=BinaryPrecedence[t.operator]??1/0,n=(s,i)=>q(t.operator,s)?c(e(s)):a(s,r,i);return `${n(t.left,false)} ${t.operator} ${n(t.right,true)}`},ct=t=>{let r=[x(t.object)];return t.optional&&r.push(t.computed?"?.":"?"),r.push(t.computed?"[":"."),r.push(e(t.property)),t.computed&&r.push("]"),r.join("")},lt=t=>{let r=t.properties.map(e).join(", ");return r.length==0?"{}":`{ ${r} }`},ut=t=>{let r=t.properties.map(e).join(", ");return r.length==0?"{ }":`{ ${r} }`},yt=new Set(["FunctionDeclaration","IfStatement","ForStatement","ForInStatement","ForOfStatement","WhileStatement","TryStatement","SwitchStatement","ClassDeclaration"]),mt=t=>t.body.map(r=>{let n=e(r);return n?yt.has(r.type)?n:n+";":""}).filter(Boolean).join(" "),ft=t=>t.shorthand?e(t.value):`${t.computed?`[${e(t.key)}]`:e(t.key)}: ${e(t.value)}`,xt=t=>`${e(t.left)} = ${e(t.right)}`,$t=t=>"return "+e(t.argument),St=t=>(t.test?`case ${e(t.test)}: `:" default: ")+t.consequent.map(e).join("; "),Et=t=>`switch (${e(t.discriminant)}) { ${t.cases.map(e).join("; ")}; }`,Tt=t=>t.value.raw,gt=t=>"`"+t.quasis.map((r,n)=>e(r)+(r.tail?"":"${"+e(t.expressions[n])+"}")).join("")+"`",ht=t=>"throw "+e(t.argument),bt=t=>{let r=`try ${e(t.block)}`;if(t.handler){let n=t.handler.param;r+=n?` catch (${e(n)}) ${e(t.handler.body)}`:` catch ${e(t.handler.body)}`;}return r+(t.finalizer?` finally ${e(t.finalizer)}`:"")},wt=t=>{let r=a(t.argument,Precedence.Unary,false),n=t.operator.length>1||(t.operator==="+"||t.operator==="-")&&r[0]===t.operator?" ":"";return t.operator+n+r},At=t=>t.prefix==true?t.operator+e(t.argument):e(t.argument)+t.operator,It=t=>`${t.kind} ${t.declarations.map(w).join(", ")}`,w=t=>t.init?`${e(t.id)} = ${e(t.init)}`:e(t.id),Pt=t=>`while (${e(t.test)}) ${e(t.body)}`,Bt=t=>{let r=e(t.source);return t.specifiers.length==0?`import ${r}`:t.specifiers[0].type=="ImportNamespaceSpecifier"?`import ${e(t.specifiers[0])} from ${r}`:t.specifiers[0].type=="ImportDefaultSpecifier"?`import ${e(t.specifiers[0])} from ${r}`:`import { ${t.specifiers.filter(s=>s.type==="ImportSpecifier").map(A).join(u)} } from ${r}`},jt=t=>e(t.local),Ct=t=>`* as ${e(t.local)}`,A=t=>t.imported.name===t.local.name?`${e(t.local)}`:`${e(t.imported)} as ${e(t.local)}`,Nt=t=>t.declaration?`export ${e(t.declaration)}`:`export { ${t.specifiers.map(I).join(u)} }`,Dt=t=>`export default ${e(t.declaration)}`,I=t=>t.local.name===t.exported.name?`${e(t.local)}`:`${e(t.local)} as ${e(t.exported)}`,Ft=t=>`...${e(t.argument)}`,kt=t=>`...${e(t.argument)}`,Lt=()=>"",vt=t=>e(t.expression),Ot=t=>e(desugarJSX(t)),Jt=t=>e(desugarJSX(t)),S={print:e,JSXElement:Ot,JSXFragment:Jt,ArrayExpression:O,ArrayPattern:J,ArrowFunctionExpression:R,AssignmentExpression:W,AssignmentPattern:xt,AwaitExpression:_,BinaryExpression:V,BlockStatement:M,BreakStatement:X,CallExpression:z,ChainExpression:Y,ConditionalExpression:K,ContinueStatement:H,DoWhileStatement:Z,EmptyStatement:Q,ExpressionStatement:G,ForInStatement:d,ForOfStatement:tt,ForStatement:et,FunctionDeclaration:rt,FunctionExpression:nt,Identifier:b,Placeholder:st,IfStatement:ot,ImportDeclaration:Bt,ImportDefaultSpecifier:jt,ImportNamespaceSpecifier:Ct,ImportSpecifier:A,ExportNamedDeclaration:Nt,ExportDefaultDeclaration:Dt,ExportSpecifier:I,JsonExpression:it,Literal:pt,LogicalExpression:at,MemberExpression:ct,ObjectExpression:lt,ObjectPattern:ut,Program:mt,Property:ft,ReturnStatement:$t,SwitchCase:St,SwitchStatement:Et,TemplateElement:Tt,TemplateLiteral:gt,ThrowStatement:ht,TryStatement:bt,UnaryExpression:wt,UpdateExpression:At,VariableDeclaration:It,VariableDeclarator:w,WhileStatement:Pt,SpreadElement:Ft,RestElement:kt,TSTypeAliasDeclaration:Lt,TSAsExpression:vt};export{g as isPrimaryOperand,e as print,m as printJson,L as printWithProfile};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tslite/printer",
3
- "version": "0.4.0",
3
+ "version": "0.5.1",
4
4
  "description": "TSLite printer — emits JavaScript source from an AST (and from the hybrid #expr JSON form)",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -40,9 +40,9 @@
40
40
  "access": "public"
41
41
  },
42
42
  "dependencies": {
43
- "@tslite/capability": "0.2.0",
44
- "@tslite/core": "0.6.0",
45
- "@tslite/sanitize": "0.1.5"
43
+ "@tslite/capability": "0.4.0",
44
+ "@tslite/core": "0.8.0",
45
+ "@tslite/sanitize": "0.1.7"
46
46
  },
47
47
  "scripts": {
48
48
  "build": "tsup",