@tslite/printer 0.5.3 → 0.5.5

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