@tslite/printer 0.3.1 → 0.4.0

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 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;
1
+ 'use strict';var core=require('@tslite/core'),capability=require('@tslite/capability'),sanitize=require('@tslite/sanitize');var N=t=>core.VALID_OBJECT_KEY.test(t);function f(t){let r=n=>{if(typeof n=="string")return `"${n}"`;if(typeof n!="object")return String(n);if(n===null)return "null";if(Array.isArray(n))return `[${n.map(r).join(",")}]`;let s=n;if("#expr"in s){let o=s["#expr"],{"#expr":m,...P}=s,B={type:o,...P};return e(B)}return `{ ${Object.entries(s).map(([o,m])=>`${N(o)?o:`"${o}"`}: ${r(m)}`).join(", ")} }`};return r(t)}var u=", ",l=null,e=t=>{if(t==null)return "";if(Array.isArray(t))return t.filter(Boolean).map(e).join(u);if(typeof t!="object")return String(t);if(!t.type)return JSON.stringify(t);if(t.type in S){if(l){let r=capability.nodeViolation(t,l);if(r)throw new core.TSLiteError("node-not-allowed",`'${r}' is not allowed by the active capability profile.`,{type:t.type})}return S[t.type](t)}throw Error("Unknown type: "+t.type)},L=(t,r)=>{t&&typeof t=="object"&&!Array.isArray(t)&&sanitize.assertSafe(t,r.sanitize==="hermetic"?sanitize.HERMETIC_POLICY:void 0);let n=l;l=capability.compileProfile(r);try{return e(t)}finally{l=n;}},c=t=>`(${t})`;function g(t){if(O.has(t.type))return true;if(t.type!=="Literal")return false;let r=t;return !(typeof r.value=="number"||r.bigint!=null)}var O=new Set(["Identifier","ThisExpression","ArrayExpression","MemberExpression","CallExpression","NewExpression","TemplateLiteral"]),y=t=>{if(t.type==="TSAsExpression")return y(t.expression);let r=e(t);return g(t)?r:c(r)},v=t=>`[${e(t.elements)}]`,J=t=>`[${t.elements.map(n=>n?e(n):"").join(", ")}]`,R=t=>{let r=e(t.params),n=t.body.type=="AssignmentExpression"||t.body.type=="BinaryExpression"||t.body.type=="ObjectExpression"||t.body.type=="JsonExpression"?c(e(t.body)):e(t.body);return `${t.async?"async ":""}(${r}) => ${n}`},W=t=>{let r=e(t.left),n=e(t.right);return `${r} ${t.operator} ${n}`},_=t=>"await "+a(t.argument,core.Precedence.Await,false),$=t=>t.type==="TSAsExpression"?$(t.expression):t,U=t=>{let r=$(t);return r.type==="UnaryExpression"||r.type==="AwaitExpression"},q=(t,r)=>{let n=$(r);if(n.type!=="LogicalExpression")return false;let s=n.operator;return t==="??"?s==="||"||s==="&&":t==="||"||t==="&&"?s==="??":false},V=t=>{let r=core.BinaryPrecedence[t.operator]??1/0,n=t.operator==="**",s=a(t.left,r,false,n),p=n&&(U(t.left)||s[0]==="-"||s[0]==="+")?c(s):s,o=a(t.right,r,true,n);return `${p} ${t.operator} ${o}`},M=t=>{let r=t.body.map(e).filter(n=>n&&n!==";").join("; ");return `{ ${r?r+"; ":""}}`},X=t=>t.label?`break ${e(t.label)}`:"break",z=t=>{let r=y(t.callee),n=t.optional?"?.":"";return `${r}${n}(${e(t.arguments)})`},Y=t=>e(t.expression),h=t=>t.type==="BinaryExpression"||t.type==="LogicalExpression"?core.BinaryPrecedence[t.operator]??1/0:t.type==="ConditionalExpression"?core.Precedence.Conditional:t.type==="AssignmentExpression"?core.Precedence.Assignment:t.type==="ArrowFunctionExpression"?core.Precedence.ArrowFunction:t.type==="AwaitExpression"?core.Precedence.Await:t.type==="SequenceExpression"?core.Precedence.Sequence:t.type==="TSAsExpression"?h(t.expression):1/0,a=(t,r,n,s=false)=>{let p=e(t),o=h(t);return (n?s?o<r:o<=r:s?o<=r:o<r)?c(p):p},K=t=>`${a(t.test,core.Precedence.Conditional,false,true)} ? ${e(t.consequent)} : ${e(t.alternate)}`,H=t=>t.label?`continue ${e(t.label)}`:"continue",G=t=>{let r=e(t.expression);return r[0]==="{"||r.startsWith("function")||r.startsWith("class")?c(r):r},Q=t=>";",Z=t=>`do ${e(t.body)} while (${e(t.test)})`,d=t=>`for (${e(t.left)} in ${e(t.right)}) ${e(t.body)}`,tt=t=>`for${t.await?" await":""} (${e(t.left)} of ${e(t.right)}) ${e(t.body)}`,et=t=>{let r=e(t.init),n=e(t.test),s=e(t.update);return `for (${r};${n?` ${n}`:""};${s?` ${s}`:""}) ${e(t.body)}`},rt=t=>{let r=t.params.map(b).join(u);return `${t.async?"async ":""}function ${e(t.id)}(${r}) ${e(t.body)}`},nt=t=>{let r=e(t.params);return `${t.async?"async ":""}function ${e(t.id)}(${r}) ${e(t.body)}`},b=t=>t.name,st=t=>{throw new core.TSLiteError("incomplete-tree","Cannot print a tree with a Placeholder (hole) \u2014 fill the holes first.",{type:t.type})},ot=t=>`if (${e(t.test)}) ${e(t.consequent)}${t.alternate?` else ${e(t.alternate)}`:""}`,it=t=>f(t.body),pt=t=>t.raw?t.raw:t.value===void 0?"undefined":t.value===null?"null":typeof t.value=="string"?JSON.stringify(t.value):typeof t.value=="boolean"?String(t.value):t.bigint?t.bigint+"n":t.regex?`/${t.regex.pattern}/${t.regex.flags}`:(t.value instanceof RegExp,String(t.value)),at=t=>{let r=core.BinaryPrecedence[t.operator]??1/0,n=(s,p)=>q(t.operator,s)?c(e(s)):a(s,r,p);return `${n(t.left,false)} ${t.operator} ${n(t.right,true)}`},ct=t=>{let r=[y(t.object)];return t.optional&&r.push(t.computed?"?.":"?"),r.push(t.computed?"[":"."),r.push(e(t.property)),t.computed&&r.push("]"),r.join("")},lt=t=>`new ${y(t.callee)}(${e(t.arguments)})`,ut=t=>{let r=t.properties.map(e).join(", ");return r.length==0?"{}":`{ ${r} }`},yt=t=>{let r=t.properties.map(e).join(", ");return r.length==0?"{ }":`{ ${r} }`},mt=new Set(["FunctionDeclaration","IfStatement","ForStatement","ForInStatement","ForOfStatement","WhileStatement","TryStatement","SwitchStatement","ClassDeclaration"]),ft=t=>t.body.map(r=>{let n=e(r);return n?mt.has(r.type)?n:n+";":""}).filter(Boolean).join(" "),xt=t=>t.shorthand?e(t.value):`${t.computed?`[${e(t.key)}]`:e(t.key)}: ${e(t.value)}`,$t=t=>`${e(t.left)} = ${e(t.right)}`,St=t=>"return "+e(t.argument),Et=t=>(t.test?`case ${e(t.test)}: `:" default: ")+t.consequent.map(e).join("; "),Tt=t=>`switch (${e(t.discriminant)}) { ${t.cases.map(e).join("; ")}; }`,gt=t=>t.value.raw,ht=t=>"`"+t.quasis.map((r,n)=>e(r)+(r.tail?"":"${"+e(t.expressions[n])+"}")).join("")+"`",bt=t=>"throw "+e(t.argument),wt=t=>{let r=`try ${e(t.block)}`;if(t.handler){let n=t.handler.param;r+=n?` catch (${e(n)}) ${e(t.handler.body)}`:` catch ${e(t.handler.body)}`;}return r+(t.finalizer?` finally ${e(t.finalizer)}`:"")},At=t=>{let r=a(t.argument,core.Precedence.Unary,false),n=t.operator.length>1||(t.operator==="+"||t.operator==="-")&&r[0]===t.operator?" ":"";return t.operator+n+r},It=t=>t.prefix==true?t.operator+e(t.argument):e(t.argument)+t.operator,Pt=t=>`${t.kind} ${t.declarations.map(w).join(", ")}`,w=t=>t.init?`${e(t.id)} = ${e(t.init)}`:e(t.id),Bt=t=>`while (${e(t.test)}) ${e(t.body)}`,jt=t=>{let r=e(t.source);return t.specifiers.length==0?`import ${r}`:t.specifiers[0].type=="ImportNamespaceSpecifier"?`import ${e(t.specifiers[0])} from ${r}`:t.specifiers[0].type=="ImportDefaultSpecifier"?`import ${e(t.specifiers[0])} from ${r}`:`import { ${t.specifiers.filter(s=>s.type==="ImportSpecifier").map(A).join(u)} } from ${r}`},Nt=t=>e(t.local),Ct=t=>`* as ${e(t.local)}`,A=t=>t.imported.name===t.local.name?`${e(t.local)}`:`${e(t.imported)} as ${e(t.local)}`,Dt=t=>t.declaration?`export ${e(t.declaration)}`:`export { ${t.specifiers.map(I).join(u)} }`,Ft=t=>`export default ${e(t.declaration)}`,I=t=>t.local.name===t.exported.name?`${e(t.local)}`:`${e(t.local)} as ${e(t.exported)}`,kt=t=>`...${e(t.argument)}`,Lt=t=>`...${e(t.argument)}`,Ot=()=>"",vt=t=>e(t.expression),Jt=t=>e(core.desugarJSX(t)),Rt=t=>e(core.desugarJSX(t)),S={print:e,JSXElement:Jt,JSXFragment:Rt,ArrayExpression:v,ArrayPattern:J,ArrowFunctionExpression:R,AssignmentExpression:W,AssignmentPattern:$t,AwaitExpression:_,BinaryExpression:V,BlockStatement:M,BreakStatement:X,CallExpression:z,ChainExpression:Y,ConditionalExpression:K,ContinueStatement:H,DoWhileStatement:Z,EmptyStatement:Q,ExpressionStatement:G,ForInStatement:d,ForOfStatement:tt,ForStatement:et,FunctionDeclaration:rt,FunctionExpression:nt,Identifier:b,Placeholder:st,IfStatement:ot,ImportDeclaration:jt,ImportDefaultSpecifier:Nt,ImportNamespaceSpecifier:Ct,ImportSpecifier:A,ExportNamedDeclaration:Dt,ExportDefaultDeclaration:Ft,ExportSpecifier:I,JsonExpression:it,Literal:pt,LogicalExpression:at,MemberExpression:ct,NewExpression:lt,ObjectExpression:ut,ObjectPattern:yt,Program:ft,Property:xt,ReturnStatement:St,SwitchCase:Et,SwitchStatement:Tt,TemplateElement:gt,TemplateLiteral:ht,ThrowStatement:bt,TryStatement:wt,UnaryExpression:At,UpdateExpression:It,VariableDeclaration:Pt,VariableDeclarator:w,WhileStatement:Bt,SpreadElement:kt,RestElement:Lt,TSTypeAliasDeclaration:Ot,TSAsExpression:vt};exports.isPrimaryOperand=g;exports.print=e;exports.printJson=f;exports.printWithProfile=L;
package/dist/index.d.cts CHANGED
@@ -10,6 +10,16 @@ declare const print: (node: PrintInput) => string;
10
10
  * segue emitindo qualquer nó.
11
11
  */
12
12
  declare const printWithProfile: (ast: PrintInput, profile: CapabilityProfile) => string;
13
+ /**
14
+ * Uma expressão é PRIMÁRIA em posição de membro/callee? (isto é: reparsa certo
15
+ * ali, sem parênteses)
16
+ *
17
+ * **Fonte única.** O `@tslite/format` faz a mesma pergunta ao imprimir a callee
18
+ * de uma chamada, e derivá-la duas vezes já custou um defeito: o formatador
19
+ * emitia `() => { … }()`, que o TSLite reparsa mas o **TypeScript recusa** — e a
20
+ * promessa da plataforma é ser um subset PURO de TS.
21
+ */
22
+ declare function isPrimaryOperand(node: BaseNode): boolean;
13
23
 
14
24
  /**
15
25
  * Prints the hybrid JSON form (a value with `#expr` islands) as JS source.
@@ -24,4 +34,4 @@ declare const printWithProfile: (ast: PrintInput, profile: CapabilityProfile) =>
24
34
  */
25
35
  declare function printJson(node: TSLite): string;
26
36
 
27
- export { print, printJson, printWithProfile };
37
+ export { isPrimaryOperand, print, printJson, printWithProfile };
package/dist/index.d.ts CHANGED
@@ -10,6 +10,16 @@ declare const print: (node: PrintInput) => string;
10
10
  * segue emitindo qualquer nó.
11
11
  */
12
12
  declare const printWithProfile: (ast: PrintInput, profile: CapabilityProfile) => string;
13
+ /**
14
+ * Uma expressão é PRIMÁRIA em posição de membro/callee? (isto é: reparsa certo
15
+ * ali, sem parênteses)
16
+ *
17
+ * **Fonte única.** O `@tslite/format` faz a mesma pergunta ao imprimir a callee
18
+ * de uma chamada, e derivá-la duas vezes já custou um defeito: o formatador
19
+ * emitia `() => { … }()`, que o TSLite reparsa mas o **TypeScript recusa** — e a
20
+ * promessa da plataforma é ser um subset PURO de TS.
21
+ */
22
+ declare function isPrimaryOperand(node: BaseNode): boolean;
13
23
 
14
24
  /**
15
25
  * Prints the hybrid JSON form (a value with `#expr` islands) as JS source.
@@ -24,4 +34,4 @@ declare const printWithProfile: (ast: PrintInput, profile: CapabilityProfile) =>
24
34
  */
25
35
  declare function printJson(node: TSLite): string;
26
36
 
27
- export { print, printJson, printWithProfile };
37
+ export { isPrimaryOperand, print, printJson, printWithProfile };
package/dist/index.js CHANGED
@@ -1 +1 @@
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};
1
+ import {Precedence,BinaryPrecedence,TSLiteError,desugarJSX,VALID_OBJECT_KEY}from'@tslite/core';import {compileProfile,nodeViolation}from'@tslite/capability';import {assertSafe,HERMETIC_POLICY}from'@tslite/sanitize';var N=t=>VALID_OBJECT_KEY.test(t);function f(t){let r=n=>{if(typeof n=="string")return `"${n}"`;if(typeof n!="object")return String(n);if(n===null)return "null";if(Array.isArray(n))return `[${n.map(r).join(",")}]`;let s=n;if("#expr"in s){let o=s["#expr"],{"#expr":m,...P}=s,B={type:o,...P};return e(B)}return `{ ${Object.entries(s).map(([o,m])=>`${N(o)?o:`"${o}"`}: ${r(m)}`).join(", ")} }`};return r(t)}var u=", ",l=null,e=t=>{if(t==null)return "";if(Array.isArray(t))return t.filter(Boolean).map(e).join(u);if(typeof t!="object")return String(t);if(!t.type)return JSON.stringify(t);if(t.type in S){if(l){let r=nodeViolation(t,l);if(r)throw new TSLiteError("node-not-allowed",`'${r}' is not allowed by the active capability profile.`,{type:t.type})}return S[t.type](t)}throw Error("Unknown type: "+t.type)},L=(t,r)=>{t&&typeof t=="object"&&!Array.isArray(t)&&assertSafe(t,r.sanitize==="hermetic"?HERMETIC_POLICY:void 0);let n=l;l=compileProfile(r);try{return e(t)}finally{l=n;}},c=t=>`(${t})`;function g(t){if(O.has(t.type))return true;if(t.type!=="Literal")return false;let r=t;return !(typeof r.value=="number"||r.bigint!=null)}var O=new Set(["Identifier","ThisExpression","ArrayExpression","MemberExpression","CallExpression","NewExpression","TemplateLiteral"]),y=t=>{if(t.type==="TSAsExpression")return y(t.expression);let r=e(t);return g(t)?r:c(r)},v=t=>`[${e(t.elements)}]`,J=t=>`[${t.elements.map(n=>n?e(n):"").join(", ")}]`,R=t=>{let r=e(t.params),n=t.body.type=="AssignmentExpression"||t.body.type=="BinaryExpression"||t.body.type=="ObjectExpression"||t.body.type=="JsonExpression"?c(e(t.body)):e(t.body);return `${t.async?"async ":""}(${r}) => ${n}`},W=t=>{let r=e(t.left),n=e(t.right);return `${r} ${t.operator} ${n}`},_=t=>"await "+a(t.argument,Precedence.Await,false),$=t=>t.type==="TSAsExpression"?$(t.expression):t,U=t=>{let r=$(t);return r.type==="UnaryExpression"||r.type==="AwaitExpression"},q=(t,r)=>{let n=$(r);if(n.type!=="LogicalExpression")return false;let s=n.operator;return t==="??"?s==="||"||s==="&&":t==="||"||t==="&&"?s==="??":false},V=t=>{let r=BinaryPrecedence[t.operator]??1/0,n=t.operator==="**",s=a(t.left,r,false,n),p=n&&(U(t.left)||s[0]==="-"||s[0]==="+")?c(s):s,o=a(t.right,r,true,n);return `${p} ${t.operator} ${o}`},M=t=>{let r=t.body.map(e).filter(n=>n&&n!==";").join("; ");return `{ ${r?r+"; ":""}}`},X=t=>t.label?`break ${e(t.label)}`:"break",z=t=>{let r=y(t.callee),n=t.optional?"?.":"";return `${r}${n}(${e(t.arguments)})`},Y=t=>e(t.expression),h=t=>t.type==="BinaryExpression"||t.type==="LogicalExpression"?BinaryPrecedence[t.operator]??1/0:t.type==="ConditionalExpression"?Precedence.Conditional:t.type==="AssignmentExpression"?Precedence.Assignment:t.type==="ArrowFunctionExpression"?Precedence.ArrowFunction:t.type==="AwaitExpression"?Precedence.Await:t.type==="SequenceExpression"?Precedence.Sequence:t.type==="TSAsExpression"?h(t.expression):1/0,a=(t,r,n,s=false)=>{let p=e(t),o=h(t);return (n?s?o<r:o<=r:s?o<=r:o<r)?c(p):p},K=t=>`${a(t.test,Precedence.Conditional,false,true)} ? ${e(t.consequent)} : ${e(t.alternate)}`,H=t=>t.label?`continue ${e(t.label)}`:"continue",G=t=>{let r=e(t.expression);return r[0]==="{"||r.startsWith("function")||r.startsWith("class")?c(r):r},Q=t=>";",Z=t=>`do ${e(t.body)} while (${e(t.test)})`,d=t=>`for (${e(t.left)} in ${e(t.right)}) ${e(t.body)}`,tt=t=>`for${t.await?" await":""} (${e(t.left)} of ${e(t.right)}) ${e(t.body)}`,et=t=>{let r=e(t.init),n=e(t.test),s=e(t.update);return `for (${r};${n?` ${n}`:""};${s?` ${s}`:""}) ${e(t.body)}`},rt=t=>{let r=t.params.map(b).join(u);return `${t.async?"async ":""}function ${e(t.id)}(${r}) ${e(t.body)}`},nt=t=>{let r=e(t.params);return `${t.async?"async ":""}function ${e(t.id)}(${r}) ${e(t.body)}`},b=t=>t.name,st=t=>{throw new TSLiteError("incomplete-tree","Cannot print a tree with a Placeholder (hole) \u2014 fill the holes first.",{type:t.type})},ot=t=>`if (${e(t.test)}) ${e(t.consequent)}${t.alternate?` else ${e(t.alternate)}`:""}`,it=t=>f(t.body),pt=t=>t.raw?t.raw:t.value===void 0?"undefined":t.value===null?"null":typeof t.value=="string"?JSON.stringify(t.value):typeof t.value=="boolean"?String(t.value):t.bigint?t.bigint+"n":t.regex?`/${t.regex.pattern}/${t.regex.flags}`:(t.value instanceof RegExp,String(t.value)),at=t=>{let r=BinaryPrecedence[t.operator]??1/0,n=(s,p)=>q(t.operator,s)?c(e(s)):a(s,r,p);return `${n(t.left,false)} ${t.operator} ${n(t.right,true)}`},ct=t=>{let r=[y(t.object)];return t.optional&&r.push(t.computed?"?.":"?"),r.push(t.computed?"[":"."),r.push(e(t.property)),t.computed&&r.push("]"),r.join("")},lt=t=>`new ${y(t.callee)}(${e(t.arguments)})`,ut=t=>{let r=t.properties.map(e).join(", ");return r.length==0?"{}":`{ ${r} }`},yt=t=>{let r=t.properties.map(e).join(", ");return r.length==0?"{ }":`{ ${r} }`},mt=new Set(["FunctionDeclaration","IfStatement","ForStatement","ForInStatement","ForOfStatement","WhileStatement","TryStatement","SwitchStatement","ClassDeclaration"]),ft=t=>t.body.map(r=>{let n=e(r);return n?mt.has(r.type)?n:n+";":""}).filter(Boolean).join(" "),xt=t=>t.shorthand?e(t.value):`${t.computed?`[${e(t.key)}]`:e(t.key)}: ${e(t.value)}`,$t=t=>`${e(t.left)} = ${e(t.right)}`,St=t=>"return "+e(t.argument),Et=t=>(t.test?`case ${e(t.test)}: `:" default: ")+t.consequent.map(e).join("; "),Tt=t=>`switch (${e(t.discriminant)}) { ${t.cases.map(e).join("; ")}; }`,gt=t=>t.value.raw,ht=t=>"`"+t.quasis.map((r,n)=>e(r)+(r.tail?"":"${"+e(t.expressions[n])+"}")).join("")+"`",bt=t=>"throw "+e(t.argument),wt=t=>{let r=`try ${e(t.block)}`;if(t.handler){let n=t.handler.param;r+=n?` catch (${e(n)}) ${e(t.handler.body)}`:` catch ${e(t.handler.body)}`;}return r+(t.finalizer?` finally ${e(t.finalizer)}`:"")},At=t=>{let r=a(t.argument,Precedence.Unary,false),n=t.operator.length>1||(t.operator==="+"||t.operator==="-")&&r[0]===t.operator?" ":"";return t.operator+n+r},It=t=>t.prefix==true?t.operator+e(t.argument):e(t.argument)+t.operator,Pt=t=>`${t.kind} ${t.declarations.map(w).join(", ")}`,w=t=>t.init?`${e(t.id)} = ${e(t.init)}`:e(t.id),Bt=t=>`while (${e(t.test)}) ${e(t.body)}`,jt=t=>{let r=e(t.source);return t.specifiers.length==0?`import ${r}`:t.specifiers[0].type=="ImportNamespaceSpecifier"?`import ${e(t.specifiers[0])} from ${r}`:t.specifiers[0].type=="ImportDefaultSpecifier"?`import ${e(t.specifiers[0])} from ${r}`:`import { ${t.specifiers.filter(s=>s.type==="ImportSpecifier").map(A).join(u)} } from ${r}`},Nt=t=>e(t.local),Ct=t=>`* as ${e(t.local)}`,A=t=>t.imported.name===t.local.name?`${e(t.local)}`:`${e(t.imported)} as ${e(t.local)}`,Dt=t=>t.declaration?`export ${e(t.declaration)}`:`export { ${t.specifiers.map(I).join(u)} }`,Ft=t=>`export default ${e(t.declaration)}`,I=t=>t.local.name===t.exported.name?`${e(t.local)}`:`${e(t.local)} as ${e(t.exported)}`,kt=t=>`...${e(t.argument)}`,Lt=t=>`...${e(t.argument)}`,Ot=()=>"",vt=t=>e(t.expression),Jt=t=>e(desugarJSX(t)),Rt=t=>e(desugarJSX(t)),S={print:e,JSXElement:Jt,JSXFragment:Rt,ArrayExpression:v,ArrayPattern:J,ArrowFunctionExpression:R,AssignmentExpression:W,AssignmentPattern:$t,AwaitExpression:_,BinaryExpression:V,BlockStatement:M,BreakStatement:X,CallExpression:z,ChainExpression:Y,ConditionalExpression:K,ContinueStatement:H,DoWhileStatement:Z,EmptyStatement:Q,ExpressionStatement:G,ForInStatement:d,ForOfStatement:tt,ForStatement:et,FunctionDeclaration:rt,FunctionExpression:nt,Identifier:b,Placeholder:st,IfStatement:ot,ImportDeclaration:jt,ImportDefaultSpecifier:Nt,ImportNamespaceSpecifier:Ct,ImportSpecifier:A,ExportNamedDeclaration:Dt,ExportDefaultDeclaration:Ft,ExportSpecifier:I,JsonExpression:it,Literal:pt,LogicalExpression:at,MemberExpression:ct,NewExpression:lt,ObjectExpression:ut,ObjectPattern:yt,Program:ft,Property:xt,ReturnStatement:St,SwitchCase:Et,SwitchStatement:Tt,TemplateElement:gt,TemplateLiteral:ht,ThrowStatement:bt,TryStatement:wt,UnaryExpression:At,UpdateExpression:It,VariableDeclaration:Pt,VariableDeclarator:w,WhileStatement:Bt,SpreadElement:kt,RestElement:Lt,TSTypeAliasDeclaration:Ot,TSAsExpression:vt};export{g as isPrimaryOperand,e as print,f as printJson,L as printWithProfile};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tslite/printer",
3
- "version": "0.3.1",
3
+ "version": "0.4.0",
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",
@@ -41,8 +41,8 @@
41
41
  },
42
42
  "dependencies": {
43
43
  "@tslite/capability": "0.2.0",
44
- "@tslite/core": "0.5.0",
45
- "@tslite/sanitize": "0.1.4"
44
+ "@tslite/core": "0.6.0",
45
+ "@tslite/sanitize": "0.1.5"
46
46
  },
47
47
  "scripts": {
48
48
  "build": "tsup",