@tslite/printer 0.2.1 → 0.3.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 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;
1
+ 'use strict';var core=require('@tslite/core'),capability=require('@tslite/capability'),sanitize=require('@tslite/sanitize');var j=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 i=s["#expr"],{"#expr":m,...A}=s,I={type:i,...A};return e(I)}return `{ ${Object.entries(s).map(([i,m])=>`${j(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 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)},k=(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})`,L=new Set(["Identifier","ThisExpression","ArrayExpression","MemberExpression","CallExpression","NewExpression","TemplateLiteral"]),y=t=>{if(t.type==="TSAsExpression")return y(t.expression);let r=e(t);if(L.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)},v=t=>`[${e(t.elements)}]`,J=t=>`[${t.elements.map(n=>n?e(n):"").join(", ")}]`,O=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}`},R=t=>{let r=e(t.left),n=e(t.right);return `${r} ${t.operator} ${n}`},W=t=>"await "+c(t.argument,core.Precedence.Await,false),$=t=>t.type==="TSAsExpression"?$(t.expression):t,_=t=>{let r=$(t);return r.type==="UnaryExpression"||r.type==="AwaitExpression"},U=(t,r)=>{let n=$(r);if(n.type!=="LogicalExpression")return false;let s=n.operator;return t==="??"?s==="||"||s==="&&":t==="||"||t==="&&"?s==="??":false},q=t=>{let r=core.BinaryPrecedence[t.operator]??1/0,n=t.operator==="**",s=c(t.left,r,false,n),p=n&&(_(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+"; ":""}}`},M=t=>t.label?`break ${e(t.label)}`:"break",X=t=>{let r=y(t.callee),n=t.optional?"?.":"";return `${r}${n}(${e(t.arguments)})`},z=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},Y=t=>`${c(t.test,core.Precedence.Conditional,false,true)} ? ${e(t.consequent)} : ${e(t.alternate)}`,K=t=>t.label?`continue ${e(t.label)}`:"continue",H=t=>{let r=e(t.expression);return r[0]==="{"||r.startsWith("function")||r.startsWith("class")?a(r):r},G=t=>";",Q=t=>`do ${e(t.body)} while (${e(t.test)})`,Z=t=>`for (${e(t.left)} in ${e(t.right)}) ${e(t.body)}`,d=t=>`for${t.await?" await":""} (${e(t.left)} of ${e(t.right)}) ${e(t.body)}`,tt=t=>{let r=e(t.init),n=e(t.test),s=e(t.update);return `for (${r};${n?` ${n}`:""};${s?` ${s}`:""}) ${e(t.body)}`},et=t=>{let r=t.params.map(g).join(u);return `${t.async?"async ":""}function ${e(t.id)}(${r}) ${e(t.body)}`},rt=t=>{let r=e(t.params);return `${t.async?"async ":""}function ${e(t.id)}(${r}) ${e(t.body)}`},g=t=>t.name,nt=t=>`if (${e(t.test)}) ${e(t.consequent)}${t.alternate?` else ${e(t.alternate)}`:""}`,st=t=>f(t.body),it=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)),ot=t=>{let r=core.BinaryPrecedence[t.operator]??1/0,n=(s,p)=>U(t.operator,s)?a(e(s)):c(s,r,p);return `${n(t.left,false)} ${t.operator} ${n(t.right,true)}`},pt=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("")},at=t=>`new ${y(t.callee)}(${e(t.arguments)})`,ct=t=>{let r=t.properties.map(e).join(", ");return r.length==0?"{}":`{ ${r} }`},lt=t=>{let r=t.properties.map(e).join(", ");return r.length==0?"{ }":`{ ${r} }`},ut=new Set(["FunctionDeclaration","IfStatement","ForStatement","ForInStatement","ForOfStatement","WhileStatement","TryStatement","SwitchStatement","ClassDeclaration"]),yt=t=>t.body.map(r=>{let n=e(r);return n?ut.has(r.type)?n:n+";":""}).filter(Boolean).join(" "),mt=t=>t.shorthand?e(t.value):`${t.computed?`[${e(t.key)}]`:e(t.key)}: ${e(t.value)}`,ft=t=>`${e(t.left)} = ${e(t.right)}`,xt=t=>"return "+e(t.argument),$t=t=>(t.test?`case ${e(t.test)}: `:" default: ")+t.consequent.map(e).join("; "),St=t=>`switch (${e(t.discriminant)}) { ${t.cases.map(e).join("; ")}; }`,Et=t=>t.value.raw,Tt=t=>"`"+t.quasis.map((r,n)=>e(r)+(r.tail?"":"${"+e(t.expressions[n])+"}")).join("")+"`",gt=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)}`:"")},ht=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},wt=t=>t.prefix==true?t.operator+e(t.argument):e(t.argument)+t.operator,At=t=>`${t.kind} ${t.declarations.map(b).join(", ")}`,b=t=>t.init?`${e(t.id)} = ${e(t.init)}`:e(t.id),It=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(h).join(u)} } from ${r}`},jt=t=>e(t.local),Nt=t=>`* as ${e(t.local)}`,h=t=>t.imported.name===t.local.name?`${e(t.local)}`:`${e(t.imported)} as ${e(t.local)}`,Pt=t=>t.declaration?`export ${e(t.declaration)}`:`export { ${t.specifiers.map(w).join(u)} }`,Ct=t=>`export default ${e(t.declaration)}`,w=t=>t.local.name===t.exported.name?`${e(t.local)}`:`${e(t.local)} as ${e(t.exported)}`,Dt=t=>`...${e(t.argument)}`,Ft=t=>`...${e(t.argument)}`,kt=()=>"",Lt=t=>e(t.expression),vt=t=>e(core.desugarJSX(t)),Jt=t=>e(core.desugarJSX(t)),S={print:e,JSXElement:vt,JSXFragment:Jt,ArrayExpression:v,ArrayPattern:J,ArrowFunctionExpression:O,AssignmentExpression:R,AssignmentPattern:ft,AwaitExpression:W,BinaryExpression:q,BlockStatement:V,BreakStatement:M,CallExpression:X,ChainExpression:z,ConditionalExpression:Y,ContinueStatement:K,DoWhileStatement:Q,EmptyStatement:G,ExpressionStatement:H,ForInStatement:Z,ForOfStatement:d,ForStatement:tt,FunctionDeclaration:et,FunctionExpression:rt,Identifier:g,IfStatement:nt,ImportDeclaration:Bt,ImportDefaultSpecifier:jt,ImportNamespaceSpecifier:Nt,ImportSpecifier:h,ExportNamedDeclaration:Pt,ExportDefaultDeclaration:Ct,ExportSpecifier:w,JsonExpression:st,Literal:it,LogicalExpression:ot,MemberExpression:pt,NewExpression:at,ObjectExpression:ct,ObjectPattern:lt,Program:yt,Property:mt,ReturnStatement:xt,SwitchCase:$t,SwitchStatement:St,TemplateElement:Et,TemplateLiteral:Tt,ThrowStatement:gt,TryStatement:bt,UnaryExpression:ht,UpdateExpression:wt,VariableDeclaration:At,VariableDeclarator:b,WhileStatement:It,SpreadElement:Dt,RestElement:Ft,TSTypeAliasDeclaration:kt,TSAsExpression:Lt};exports.print=e;exports.printJson=f;exports.printWithProfile=k;
package/dist/index.d.cts CHANGED
@@ -12,13 +12,13 @@ declare const print: (node: PrintInput) => string;
12
12
  declare const printWithProfile: (ast: PrintInput, profile: CapabilityProfile) => string;
13
13
 
14
14
  /**
15
- * Prints the hybrid JSON form (a value with `#ast` islands) as JS source.
15
+ * Prints the hybrid JSON form (a value with `#expr` islands) as JS source.
16
16
  *
17
- * Static data is emitted as an object/array literal; each `#ast` island is
18
- * handed back to `print()` — the same `#ast` boundary the type layer walks.
17
+ * Static data is emitted as an object/array literal; each `#expr` island is
18
+ * handed back to `print()` — the same `#expr` boundary the type layer walks.
19
19
  *
20
20
  * ```ts
21
- * printJson({ name: "John", init: { "#ast": "CallExpression", ... } })
21
+ * printJson({ name: "John", init: { "#expr": "CallExpression", ... } })
22
22
  * // '{ name: "John", init: setup() }'
23
23
  * ```
24
24
  */
package/dist/index.d.ts CHANGED
@@ -12,13 +12,13 @@ declare const print: (node: PrintInput) => string;
12
12
  declare const printWithProfile: (ast: PrintInput, profile: CapabilityProfile) => string;
13
13
 
14
14
  /**
15
- * Prints the hybrid JSON form (a value with `#ast` islands) as JS source.
15
+ * Prints the hybrid JSON form (a value with `#expr` islands) as JS source.
16
16
  *
17
- * Static data is emitted as an object/array literal; each `#ast` island is
18
- * handed back to `print()` — the same `#ast` boundary the type layer walks.
17
+ * Static data is emitted as an object/array literal; each `#expr` island is
18
+ * handed back to `print()` — the same `#expr` boundary the type layer walks.
19
19
  *
20
20
  * ```ts
21
- * printJson({ name: "John", init: { "#ast": "CallExpression", ... } })
21
+ * printJson({ name: "John", init: { "#expr": "CallExpression", ... } })
22
22
  * // '{ name: "John", init: setup() }'
23
23
  * ```
24
24
  */
package/dist/index.js CHANGED
@@ -1 +1 @@
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};
1
+ import {Precedence,BinaryPrecedence,desugarJSX,TSLiteError,VALID_OBJECT_KEY}from'@tslite/core';import {compileProfile,nodeViolation}from'@tslite/capability';import {assertSafe,HERMETIC_POLICY}from'@tslite/sanitize';var j=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 i=s["#expr"],{"#expr":m,...A}=s,I={type:i,...A};return e(I)}return `{ ${Object.entries(s).map(([i,m])=>`${j(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 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)},k=(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})`,L=new Set(["Identifier","ThisExpression","ArrayExpression","MemberExpression","CallExpression","NewExpression","TemplateLiteral"]),y=t=>{if(t.type==="TSAsExpression")return y(t.expression);let r=e(t);if(L.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)},v=t=>`[${e(t.elements)}]`,J=t=>`[${t.elements.map(n=>n?e(n):"").join(", ")}]`,O=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}`},R=t=>{let r=e(t.left),n=e(t.right);return `${r} ${t.operator} ${n}`},W=t=>"await "+c(t.argument,Precedence.Await,false),$=t=>t.type==="TSAsExpression"?$(t.expression):t,_=t=>{let r=$(t);return r.type==="UnaryExpression"||r.type==="AwaitExpression"},U=(t,r)=>{let n=$(r);if(n.type!=="LogicalExpression")return false;let s=n.operator;return t==="??"?s==="||"||s==="&&":t==="||"||t==="&&"?s==="??":false},q=t=>{let r=BinaryPrecedence[t.operator]??1/0,n=t.operator==="**",s=c(t.left,r,false,n),p=n&&(_(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+"; ":""}}`},M=t=>t.label?`break ${e(t.label)}`:"break",X=t=>{let r=y(t.callee),n=t.optional?"?.":"";return `${r}${n}(${e(t.arguments)})`},z=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},Y=t=>`${c(t.test,Precedence.Conditional,false,true)} ? ${e(t.consequent)} : ${e(t.alternate)}`,K=t=>t.label?`continue ${e(t.label)}`:"continue",H=t=>{let r=e(t.expression);return r[0]==="{"||r.startsWith("function")||r.startsWith("class")?a(r):r},G=t=>";",Q=t=>`do ${e(t.body)} while (${e(t.test)})`,Z=t=>`for (${e(t.left)} in ${e(t.right)}) ${e(t.body)}`,d=t=>`for${t.await?" await":""} (${e(t.left)} of ${e(t.right)}) ${e(t.body)}`,tt=t=>{let r=e(t.init),n=e(t.test),s=e(t.update);return `for (${r};${n?` ${n}`:""};${s?` ${s}`:""}) ${e(t.body)}`},et=t=>{let r=t.params.map(g).join(u);return `${t.async?"async ":""}function ${e(t.id)}(${r}) ${e(t.body)}`},rt=t=>{let r=e(t.params);return `${t.async?"async ":""}function ${e(t.id)}(${r}) ${e(t.body)}`},g=t=>t.name,nt=t=>`if (${e(t.test)}) ${e(t.consequent)}${t.alternate?` else ${e(t.alternate)}`:""}`,st=t=>f(t.body),it=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)),ot=t=>{let r=BinaryPrecedence[t.operator]??1/0,n=(s,p)=>U(t.operator,s)?a(e(s)):c(s,r,p);return `${n(t.left,false)} ${t.operator} ${n(t.right,true)}`},pt=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("")},at=t=>`new ${y(t.callee)}(${e(t.arguments)})`,ct=t=>{let r=t.properties.map(e).join(", ");return r.length==0?"{}":`{ ${r} }`},lt=t=>{let r=t.properties.map(e).join(", ");return r.length==0?"{ }":`{ ${r} }`},ut=new Set(["FunctionDeclaration","IfStatement","ForStatement","ForInStatement","ForOfStatement","WhileStatement","TryStatement","SwitchStatement","ClassDeclaration"]),yt=t=>t.body.map(r=>{let n=e(r);return n?ut.has(r.type)?n:n+";":""}).filter(Boolean).join(" "),mt=t=>t.shorthand?e(t.value):`${t.computed?`[${e(t.key)}]`:e(t.key)}: ${e(t.value)}`,ft=t=>`${e(t.left)} = ${e(t.right)}`,xt=t=>"return "+e(t.argument),$t=t=>(t.test?`case ${e(t.test)}: `:" default: ")+t.consequent.map(e).join("; "),St=t=>`switch (${e(t.discriminant)}) { ${t.cases.map(e).join("; ")}; }`,Et=t=>t.value.raw,Tt=t=>"`"+t.quasis.map((r,n)=>e(r)+(r.tail?"":"${"+e(t.expressions[n])+"}")).join("")+"`",gt=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)}`:"")},ht=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},wt=t=>t.prefix==true?t.operator+e(t.argument):e(t.argument)+t.operator,At=t=>`${t.kind} ${t.declarations.map(b).join(", ")}`,b=t=>t.init?`${e(t.id)} = ${e(t.init)}`:e(t.id),It=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(h).join(u)} } from ${r}`},jt=t=>e(t.local),Nt=t=>`* as ${e(t.local)}`,h=t=>t.imported.name===t.local.name?`${e(t.local)}`:`${e(t.imported)} as ${e(t.local)}`,Pt=t=>t.declaration?`export ${e(t.declaration)}`:`export { ${t.specifiers.map(w).join(u)} }`,Ct=t=>`export default ${e(t.declaration)}`,w=t=>t.local.name===t.exported.name?`${e(t.local)}`:`${e(t.local)} as ${e(t.exported)}`,Dt=t=>`...${e(t.argument)}`,Ft=t=>`...${e(t.argument)}`,kt=()=>"",Lt=t=>e(t.expression),vt=t=>e(desugarJSX(t)),Jt=t=>e(desugarJSX(t)),S={print:e,JSXElement:vt,JSXFragment:Jt,ArrayExpression:v,ArrayPattern:J,ArrowFunctionExpression:O,AssignmentExpression:R,AssignmentPattern:ft,AwaitExpression:W,BinaryExpression:q,BlockStatement:V,BreakStatement:M,CallExpression:X,ChainExpression:z,ConditionalExpression:Y,ContinueStatement:K,DoWhileStatement:Q,EmptyStatement:G,ExpressionStatement:H,ForInStatement:Z,ForOfStatement:d,ForStatement:tt,FunctionDeclaration:et,FunctionExpression:rt,Identifier:g,IfStatement:nt,ImportDeclaration:Bt,ImportDefaultSpecifier:jt,ImportNamespaceSpecifier:Nt,ImportSpecifier:h,ExportNamedDeclaration:Pt,ExportDefaultDeclaration:Ct,ExportSpecifier:w,JsonExpression:st,Literal:it,LogicalExpression:ot,MemberExpression:pt,NewExpression:at,ObjectExpression:ct,ObjectPattern:lt,Program:yt,Property:mt,ReturnStatement:xt,SwitchCase:$t,SwitchStatement:St,TemplateElement:Et,TemplateLiteral:Tt,ThrowStatement:gt,TryStatement:bt,UnaryExpression:ht,UpdateExpression:wt,VariableDeclaration:At,VariableDeclarator:b,WhileStatement:It,SpreadElement:Dt,RestElement:Ft,TSTypeAliasDeclaration:kt,TSAsExpression:Lt};export{e as print,f as printJson,k as printWithProfile};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tslite/printer",
3
- "version": "0.2.1",
4
- "description": "TSLite printer — emits JavaScript source from an AST (and from the hybrid #ast JSON form)",
3
+ "version": "0.3.1",
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",
7
7
  "module": "./dist/index.js",
@@ -41,8 +41,8 @@
41
41
  },
42
42
  "dependencies": {
43
43
  "@tslite/capability": "0.2.0",
44
- "@tslite/sanitize": "0.1.2",
45
- "@tslite/core": "0.3.0"
44
+ "@tslite/core": "0.5.0",
45
+ "@tslite/sanitize": "0.1.4"
46
46
  },
47
47
  "scripts": {
48
48
  "build": "tsup",