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