@tslite/printer 0.5.1 → 0.5.2
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 +1 -1
- package/dist/index.d.cts +28 -1
- package/dist/index.d.ts +28 -1
- package/dist/index.js +1 -1
- package/package.json +4 -4
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
'use strict';var core=require('@tslite/core'),capability=require('@tslite/capability'),sanitize=require('@tslite/sanitize');var
|
|
1
|
+
'use strict';var core=require('@tslite/core'),capability=require('@tslite/capability'),sanitize=require('@tslite/sanitize');var F=t=>core.VALID_OBJECT_KEY.test(t);function y(t){let e=n=>{if(typeof n=="string")return `"${n}"`;if(typeof n!="object")return String(n);if(n===null)return "null";if(Array.isArray(n))return `[${n.map(e).join(",")}]`;let s=n;if("#expr"in s){let i=s["#expr"],{"#expr":E,...N}=s,D={type:i,...N};return r(D)}return `{ ${Object.entries(s).map(([i,E])=>`${F(i)?i:`"${i}"`}: ${e(E)}`).join(", ")} }`};return e(t)}var u=", ",l=null,r=t=>{if(t==null)return "";if(Array.isArray(t))return t.filter(Boolean).map(r).join(u);if(typeof t!="object")return String(t);if(!t.type)return JSON.stringify(t);if(t.type in T){if(l){let e=capability.nodeViolation(t,l);if(e)throw new core.TSLiteError("node-not-allowed",`'${e}' is not allowed by the active capability profile.`,{type:t.type})}return T[t.type](t)}throw Error("Unknown type: "+t.type)},R=(t,e)=>{t&&typeof t=="object"&&!Array.isArray(t)&&sanitize.assertSafe(t,e.sanitize==="hermetic"?sanitize.HERMETIC_POLICY:void 0);let n=l;l=capability.compileProfile(e);try{return r(t)}finally{l=n;}},c=t=>`(${t})`;function b(t){if(W.has(t.type))return true;if(t.type!=="Literal")return false;let e=t;return !(typeof e.value=="number"||e.bigint!=null)}var W=new Set(["Identifier","ThisExpression","ArrayExpression","MemberExpression","CallExpression","TemplateLiteral"]),f=t=>{if(t.type==="TSAsExpression")return f(t.expression);let e=r(t);return b(t)?e:c(e)},_=t=>`[${r(t.elements)}]`,U=t=>`[${t.elements.map(n=>n?r(n):"").join(", ")}]`,q=t=>{let e=r(t.params),n=r(t.body),s=t.body.type!=="BlockStatement"&&n.startsWith("{"),o=t.body.type=="AssignmentExpression"||t.body.type=="BinaryExpression"||t.body.type=="ObjectExpression"||t.body.type=="JsonExpression"||s?c(n):n;return `${t.async?"async ":""}(${e}) => ${o}`},V=t=>{let e=r(t.left),n=r(t.right);return `${e} ${t.operator} ${n}`},M=t=>"await "+p(t.argument,core.Precedence.Await,false),x=t=>t.type==="TSAsExpression"?x(t.expression):t,w=t=>{let e=x(t);return e.type==="UnaryExpression"||e.type==="AwaitExpression"},A=(t,e)=>{let n=x(e);if(n.type!=="LogicalExpression")return false;let s=n.operator;return t==="??"?s==="||"||s==="&&":t==="||"||t==="&&"?s==="??":false},X=t=>{let e=core.BinaryPrecedence[t.operator]??1/0,n=t.operator==="**",s=p(t.left,e,false,n),o=n&&(w(t.left)||s[0]==="-"||s[0]==="+")?c(s):s,i=p(t.right,e,true,n);return `${o} ${t.operator} ${i}`},K=t=>{let e=t.body.map(S).filter(Boolean).join(" ");return `{ ${e?e+" ":""}}`},z=t=>t.label?`break ${r(t.label)}`:"break",Y=t=>{let e=f(t.callee),n=t.optional?"?.":"";return `${e}${n}(${r(t.arguments)})`},H=t=>r(t.expression),$=t=>t.type==="BinaryExpression"||t.type==="LogicalExpression"?core.BinaryPrecedence[t.operator]??1/0:t.type==="ConditionalExpression"?core.Precedence.Conditional:t.type==="AssignmentExpression"?core.Precedence.Assignment:t.type==="ArrowFunctionExpression"?core.Precedence.ArrowFunction:t.type==="AwaitExpression"?core.Precedence.Await:t.type==="SequenceExpression"?core.Precedence.Sequence:t.type==="TSAsExpression"?$(t.expression):1/0,B=(t,e,n,s=false)=>{let o=$(t);return n?s?o<e:o<=e:s?o<=e:o<e},p=(t,e,n,s=false)=>{let o=r(t);return B(t,e,n,s)?c(o):o},G=t=>`${p(t.test,core.Precedence.Conditional,false,true)} ? ${r(t.consequent)} : ${r(t.alternate)}`,Q=t=>t.label?`continue ${r(t.label)}`:"continue",Z=t=>{let e=r(t.expression);return e[0]==="{"||e.startsWith("function")||e.startsWith("class")?c(e):e},d=t=>";",tt=t=>`do ${r(t.body)} while (${r(t.test)})`,et=t=>`for (${r(t.left)} in ${r(t.right)}) ${r(t.body)}`,rt=t=>`for${t.await?" await":""} (${r(t.left)} of ${r(t.right)}) ${r(t.body)}`,nt=t=>{let e=r(t.init),n=r(t.test),s=r(t.update);return `for (${e};${n?` ${n}`:""};${s?` ${s}`:""}) ${r(t.body)}`},st=t=>{let e=t.params.map(P).join(u);return `${t.async?"async ":""}function ${r(t.id)}(${e}) ${r(t.body)}`},ot=t=>{let e=r(t.params);return `${t.async?"async ":""}function ${r(t.id)}(${e}) ${r(t.body)}`},P=t=>t.name,it=t=>{throw new core.TSLiteError("incomplete-tree","Cannot print a tree with a Placeholder (hole) \u2014 fill the holes first.",{type:t.type})},at=t=>`if (${r(t.test)}) ${r(t.consequent)}${t.alternate?` else ${r(t.alternate)}`:""}`,pt=t=>y(t.body),ct=t=>t.raw?t.raw:t.value===void 0?"undefined":t.value===null?"null":typeof t.value=="string"?JSON.stringify(t.value):typeof t.value=="boolean"?String(t.value):t.bigint?t.bigint+"n":t.regex?`/${t.regex.pattern}/${t.regex.flags}`:(t.value instanceof RegExp,String(t.value)),lt=t=>{let e=core.BinaryPrecedence[t.operator]??1/0,n=(s,o)=>A(t.operator,s)?c(r(s)):p(s,e,o);return `${n(t.left,false)} ${t.operator} ${n(t.right,true)}`},ut=t=>{let e=[f(t.object)];return t.optional&&e.push(t.computed?"?.":"?"),e.push(t.computed?"[":"."),e.push(r(t.property)),t.computed&&e.push("]"),e.join("")},yt=t=>{let e=t.properties.map(r).join(", ");return e.length==0?"{}":`{ ${e} }`},mt=t=>{let e=t.properties.map(r).join(", ");return e.length==0?"{ }":`{ ${e} }`},ft=new Set(["FunctionDeclaration","IfStatement","ForStatement","ForInStatement","ForOfStatement","WhileStatement","TryStatement","SwitchStatement","ClassDeclaration","BlockStatement"]),S=t=>{let e=r(t);return !e||e===";"?"":ft.has(t.type)?e:e+";"},xt=t=>t.body.map(S).filter(Boolean).join(" "),$t=t=>t.shorthand?r(t.value):`${t.computed?`[${r(t.key)}]`:r(t.key)}: ${r(t.value)}`,St=t=>`${r(t.left)} = ${r(t.right)}`,Et=t=>"return "+r(t.argument),Tt=t=>{let e=t.test?`case ${r(t.test)}:`:"default:",n=t.consequent.map(S).filter(Boolean).join(" ");return n?`${e} ${n}`:e},gt=t=>`switch (${r(t.discriminant)}) { ${t.cases.map(r).join(" ")} }`,ht=t=>t.value.raw,bt=t=>"`"+t.quasis.map((e,n)=>r(e)+(e.tail?"":"${"+r(t.expressions[n])+"}")).join("")+"`",wt=t=>"throw "+r(t.argument),At=t=>{let e=`try ${r(t.block)}`;if(t.handler){let n=t.handler.param;e+=n?` catch (${r(n)}) ${r(t.handler.body)}`:` catch ${r(t.handler.body)}`;}return e+(t.finalizer?` finally ${r(t.finalizer)}`:"")},Bt=t=>{let e=p(t.argument,core.Precedence.Unary,false),n=t.operator.length>1||(t.operator==="+"||t.operator==="-")&&e[0]===t.operator?" ":"";return t.operator+n+e},Pt=t=>t.prefix==true?t.operator+r(t.argument):r(t.argument)+t.operator,It=t=>`${t.kind} ${t.declarations.map(I).join(", ")}`,I=t=>t.init?`${r(t.id)} = ${r(t.init)}`:r(t.id),jt=t=>`while (${r(t.test)}) ${r(t.body)}`,Ct=t=>{if(t.importKind==="type")return "";let e=r(t.source);if(t.specifiers.length==0)return `import ${e}`;let n=t.specifiers.find(i=>i.type==="ImportDefaultSpecifier"||i.type==="ImportNamespaceSpecifier"),s=t.specifiers.filter(i=>i.type==="ImportSpecifier"&&i.importKind!=="type");if(!n&&s.length===0)return "";let o=[];return n&&o.push(r(n)),s.length&&o.push(`{ ${s.map(j).join(u)} }`),`import ${o.join(u)} from ${e}`},Nt=t=>r(t.local),Dt=t=>`* as ${r(t.local)}`,j=t=>t.imported.name===t.local.name?`${r(t.local)}`:`${r(t.imported)} as ${r(t.local)}`,kt=t=>{if(t.declaration){let n=r(t.declaration);return n?`export ${n}`:""}return `export { ${t.specifiers.map(C).join(u)} }`},Ft=t=>`export default ${r(t.declaration)}`,C=t=>t.local.name===t.exported.name?`${r(t.local)}`:`${r(t.local)} as ${r(t.exported)}`,Lt=t=>`...${r(t.argument)}`,vt=t=>`...${r(t.argument)}`,Ot=()=>"",Jt=t=>r(t.expression),Rt=t=>r(core.desugarJSX(t)),Wt=t=>r(core.desugarJSX(t)),T={print:r,JSXElement:Rt,JSXFragment:Wt,ArrayExpression:_,ArrayPattern:U,ArrowFunctionExpression:q,AssignmentExpression:V,AssignmentPattern:St,AwaitExpression:M,BinaryExpression:X,BlockStatement:K,BreakStatement:z,CallExpression:Y,ChainExpression:H,ConditionalExpression:G,ContinueStatement:Q,DoWhileStatement:tt,EmptyStatement:d,ExpressionStatement:Z,ForInStatement:et,ForOfStatement:rt,ForStatement:nt,FunctionDeclaration:st,FunctionExpression:ot,Identifier:P,Placeholder:it,IfStatement:at,ImportDeclaration:Ct,ImportDefaultSpecifier:Nt,ImportNamespaceSpecifier:Dt,ImportSpecifier:j,ExportNamedDeclaration:kt,ExportDefaultDeclaration:Ft,ExportSpecifier:C,JsonExpression:pt,Literal:ct,LogicalExpression:lt,MemberExpression:ut,ObjectExpression:yt,ObjectPattern:mt,Program:xt,Property:$t,ReturnStatement:Et,SwitchCase:Tt,SwitchStatement:gt,TemplateElement:ht,TemplateLiteral:bt,ThrowStatement:wt,TryStatement:At,UnaryExpression:Bt,UpdateExpression:Pt,VariableDeclaration:It,VariableDeclarator:I,WhileStatement:jt,SpreadElement:Lt,RestElement:vt,TSTypeAliasDeclaration:Ot,TSAsExpression:Jt};exports.getExpressionPrecedence=$;exports.isPrimaryOperand=b;exports.mixesCoalesce=A;exports.needsParens=B;exports.print=r;exports.printJson=y;exports.printWithProfile=R;exports.unaryLike=w;
|
package/dist/index.d.cts
CHANGED
|
@@ -20,6 +20,33 @@ declare const printWithProfile: (ast: PrintInput, profile: CapabilityProfile) =>
|
|
|
20
20
|
* promessa da plataforma é ser um subset PURO de TS.
|
|
21
21
|
*/
|
|
22
22
|
declare function isPrimaryOperand(node: BaseNode): boolean;
|
|
23
|
+
/** Operando que começa com unário/`await` (à esquerda de `**` exige parênteses). */
|
|
24
|
+
declare const unaryLike: (node: BaseNode) => boolean;
|
|
25
|
+
declare const mixesCoalesce: (parentOp: string, child: BaseNode) => boolean;
|
|
26
|
+
/**
|
|
27
|
+
* Returns the operator precedence of an expression node.
|
|
28
|
+
* Higher value = binds tighter. Returns Infinity for atoms (Identifier, Literal, etc).
|
|
29
|
+
*/
|
|
30
|
+
/** A precedência efetiva de uma expressão (erasure de `as`); `Infinity` = primária. */
|
|
31
|
+
declare const getExpressionPrecedence: (node: BaseNode) => number;
|
|
32
|
+
/**
|
|
33
|
+
* Wraps a child node in parentheses when its precedence is lower than
|
|
34
|
+
* the parent's, respecting left/right associativity rules.
|
|
35
|
+
*
|
|
36
|
+
* For left-associative operators (most):
|
|
37
|
+
* - left child: parens when childPrec < parentPrec
|
|
38
|
+
* - right child: parens when childPrec <= parentPrec
|
|
39
|
+
*
|
|
40
|
+
* For right-associative operators (**):
|
|
41
|
+
* - left child: parens when childPrec <= parentPrec
|
|
42
|
+
* - right child: parens when childPrec < parentPrec
|
|
43
|
+
*/
|
|
44
|
+
/**
|
|
45
|
+
* A DECISÃO de parentizar um operando pela precedência — exportada para quem
|
|
46
|
+
* imprime por outra via (o `@tslite/emit` renderiza `Doc`, não texto) tomar a
|
|
47
|
+
* mesma decisão, em vez de manter uma segunda máquina de precedência.
|
|
48
|
+
*/
|
|
49
|
+
declare const needsParens: (node: BaseNode, parentPrec: number, isRight: boolean, rightAssociative?: boolean) => boolean;
|
|
23
50
|
|
|
24
51
|
/**
|
|
25
52
|
* Prints the hybrid JSON form (a value with `#expr` islands) as JS source.
|
|
@@ -34,4 +61,4 @@ declare function isPrimaryOperand(node: BaseNode): boolean;
|
|
|
34
61
|
*/
|
|
35
62
|
declare function printJson(node: TSLite): string;
|
|
36
63
|
|
|
37
|
-
export { isPrimaryOperand, print, printJson, printWithProfile };
|
|
64
|
+
export { getExpressionPrecedence, isPrimaryOperand, mixesCoalesce, needsParens, print, printJson, printWithProfile, unaryLike };
|
package/dist/index.d.ts
CHANGED
|
@@ -20,6 +20,33 @@ declare const printWithProfile: (ast: PrintInput, profile: CapabilityProfile) =>
|
|
|
20
20
|
* promessa da plataforma é ser um subset PURO de TS.
|
|
21
21
|
*/
|
|
22
22
|
declare function isPrimaryOperand(node: BaseNode): boolean;
|
|
23
|
+
/** Operando que começa com unário/`await` (à esquerda de `**` exige parênteses). */
|
|
24
|
+
declare const unaryLike: (node: BaseNode) => boolean;
|
|
25
|
+
declare const mixesCoalesce: (parentOp: string, child: BaseNode) => boolean;
|
|
26
|
+
/**
|
|
27
|
+
* Returns the operator precedence of an expression node.
|
|
28
|
+
* Higher value = binds tighter. Returns Infinity for atoms (Identifier, Literal, etc).
|
|
29
|
+
*/
|
|
30
|
+
/** A precedência efetiva de uma expressão (erasure de `as`); `Infinity` = primária. */
|
|
31
|
+
declare const getExpressionPrecedence: (node: BaseNode) => number;
|
|
32
|
+
/**
|
|
33
|
+
* Wraps a child node in parentheses when its precedence is lower than
|
|
34
|
+
* the parent's, respecting left/right associativity rules.
|
|
35
|
+
*
|
|
36
|
+
* For left-associative operators (most):
|
|
37
|
+
* - left child: parens when childPrec < parentPrec
|
|
38
|
+
* - right child: parens when childPrec <= parentPrec
|
|
39
|
+
*
|
|
40
|
+
* For right-associative operators (**):
|
|
41
|
+
* - left child: parens when childPrec <= parentPrec
|
|
42
|
+
* - right child: parens when childPrec < parentPrec
|
|
43
|
+
*/
|
|
44
|
+
/**
|
|
45
|
+
* A DECISÃO de parentizar um operando pela precedência — exportada para quem
|
|
46
|
+
* imprime por outra via (o `@tslite/emit` renderiza `Doc`, não texto) tomar a
|
|
47
|
+
* mesma decisão, em vez de manter uma segunda máquina de precedência.
|
|
48
|
+
*/
|
|
49
|
+
declare const needsParens: (node: BaseNode, parentPrec: number, isRight: boolean, rightAssociative?: boolean) => boolean;
|
|
23
50
|
|
|
24
51
|
/**
|
|
25
52
|
* Prints the hybrid JSON form (a value with `#expr` islands) as JS source.
|
|
@@ -34,4 +61,4 @@ declare function isPrimaryOperand(node: BaseNode): boolean;
|
|
|
34
61
|
*/
|
|
35
62
|
declare function printJson(node: TSLite): string;
|
|
36
63
|
|
|
37
|
-
export { isPrimaryOperand, print, printJson, printWithProfile };
|
|
64
|
+
export { getExpressionPrecedence, isPrimaryOperand, mixesCoalesce, needsParens, print, printJson, printWithProfile, unaryLike };
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import {Precedence,
|
|
1
|
+
import {BinaryPrecedence,Precedence,TSLiteError,desugarJSX,VALID_OBJECT_KEY}from'@tslite/core';import {compileProfile,nodeViolation}from'@tslite/capability';import {assertSafe,HERMETIC_POLICY}from'@tslite/sanitize';var F=t=>VALID_OBJECT_KEY.test(t);function y(t){let e=n=>{if(typeof n=="string")return `"${n}"`;if(typeof n!="object")return String(n);if(n===null)return "null";if(Array.isArray(n))return `[${n.map(e).join(",")}]`;let s=n;if("#expr"in s){let i=s["#expr"],{"#expr":E,...N}=s,D={type:i,...N};return r(D)}return `{ ${Object.entries(s).map(([i,E])=>`${F(i)?i:`"${i}"`}: ${e(E)}`).join(", ")} }`};return e(t)}var u=", ",l=null,r=t=>{if(t==null)return "";if(Array.isArray(t))return t.filter(Boolean).map(r).join(u);if(typeof t!="object")return String(t);if(!t.type)return JSON.stringify(t);if(t.type in T){if(l){let e=nodeViolation(t,l);if(e)throw new TSLiteError("node-not-allowed",`'${e}' is not allowed by the active capability profile.`,{type:t.type})}return T[t.type](t)}throw Error("Unknown type: "+t.type)},R=(t,e)=>{t&&typeof t=="object"&&!Array.isArray(t)&&assertSafe(t,e.sanitize==="hermetic"?HERMETIC_POLICY:void 0);let n=l;l=compileProfile(e);try{return r(t)}finally{l=n;}},c=t=>`(${t})`;function b(t){if(W.has(t.type))return true;if(t.type!=="Literal")return false;let e=t;return !(typeof e.value=="number"||e.bigint!=null)}var W=new Set(["Identifier","ThisExpression","ArrayExpression","MemberExpression","CallExpression","TemplateLiteral"]),f=t=>{if(t.type==="TSAsExpression")return f(t.expression);let e=r(t);return b(t)?e:c(e)},_=t=>`[${r(t.elements)}]`,U=t=>`[${t.elements.map(n=>n?r(n):"").join(", ")}]`,q=t=>{let e=r(t.params),n=r(t.body),s=t.body.type!=="BlockStatement"&&n.startsWith("{"),o=t.body.type=="AssignmentExpression"||t.body.type=="BinaryExpression"||t.body.type=="ObjectExpression"||t.body.type=="JsonExpression"||s?c(n):n;return `${t.async?"async ":""}(${e}) => ${o}`},V=t=>{let e=r(t.left),n=r(t.right);return `${e} ${t.operator} ${n}`},M=t=>"await "+p(t.argument,Precedence.Await,false),x=t=>t.type==="TSAsExpression"?x(t.expression):t,w=t=>{let e=x(t);return e.type==="UnaryExpression"||e.type==="AwaitExpression"},A=(t,e)=>{let n=x(e);if(n.type!=="LogicalExpression")return false;let s=n.operator;return t==="??"?s==="||"||s==="&&":t==="||"||t==="&&"?s==="??":false},X=t=>{let e=BinaryPrecedence[t.operator]??1/0,n=t.operator==="**",s=p(t.left,e,false,n),o=n&&(w(t.left)||s[0]==="-"||s[0]==="+")?c(s):s,i=p(t.right,e,true,n);return `${o} ${t.operator} ${i}`},K=t=>{let e=t.body.map(S).filter(Boolean).join(" ");return `{ ${e?e+" ":""}}`},z=t=>t.label?`break ${r(t.label)}`:"break",Y=t=>{let e=f(t.callee),n=t.optional?"?.":"";return `${e}${n}(${r(t.arguments)})`},H=t=>r(t.expression),$=t=>t.type==="BinaryExpression"||t.type==="LogicalExpression"?BinaryPrecedence[t.operator]??1/0:t.type==="ConditionalExpression"?Precedence.Conditional:t.type==="AssignmentExpression"?Precedence.Assignment:t.type==="ArrowFunctionExpression"?Precedence.ArrowFunction:t.type==="AwaitExpression"?Precedence.Await:t.type==="SequenceExpression"?Precedence.Sequence:t.type==="TSAsExpression"?$(t.expression):1/0,B=(t,e,n,s=false)=>{let o=$(t);return n?s?o<e:o<=e:s?o<=e:o<e},p=(t,e,n,s=false)=>{let o=r(t);return B(t,e,n,s)?c(o):o},G=t=>`${p(t.test,Precedence.Conditional,false,true)} ? ${r(t.consequent)} : ${r(t.alternate)}`,Q=t=>t.label?`continue ${r(t.label)}`:"continue",Z=t=>{let e=r(t.expression);return e[0]==="{"||e.startsWith("function")||e.startsWith("class")?c(e):e},d=t=>";",tt=t=>`do ${r(t.body)} while (${r(t.test)})`,et=t=>`for (${r(t.left)} in ${r(t.right)}) ${r(t.body)}`,rt=t=>`for${t.await?" await":""} (${r(t.left)} of ${r(t.right)}) ${r(t.body)}`,nt=t=>{let e=r(t.init),n=r(t.test),s=r(t.update);return `for (${e};${n?` ${n}`:""};${s?` ${s}`:""}) ${r(t.body)}`},st=t=>{let e=t.params.map(P).join(u);return `${t.async?"async ":""}function ${r(t.id)}(${e}) ${r(t.body)}`},ot=t=>{let e=r(t.params);return `${t.async?"async ":""}function ${r(t.id)}(${e}) ${r(t.body)}`},P=t=>t.name,it=t=>{throw new TSLiteError("incomplete-tree","Cannot print a tree with a Placeholder (hole) \u2014 fill the holes first.",{type:t.type})},at=t=>`if (${r(t.test)}) ${r(t.consequent)}${t.alternate?` else ${r(t.alternate)}`:""}`,pt=t=>y(t.body),ct=t=>t.raw?t.raw:t.value===void 0?"undefined":t.value===null?"null":typeof t.value=="string"?JSON.stringify(t.value):typeof t.value=="boolean"?String(t.value):t.bigint?t.bigint+"n":t.regex?`/${t.regex.pattern}/${t.regex.flags}`:(t.value instanceof RegExp,String(t.value)),lt=t=>{let e=BinaryPrecedence[t.operator]??1/0,n=(s,o)=>A(t.operator,s)?c(r(s)):p(s,e,o);return `${n(t.left,false)} ${t.operator} ${n(t.right,true)}`},ut=t=>{let e=[f(t.object)];return t.optional&&e.push(t.computed?"?.":"?"),e.push(t.computed?"[":"."),e.push(r(t.property)),t.computed&&e.push("]"),e.join("")},yt=t=>{let e=t.properties.map(r).join(", ");return e.length==0?"{}":`{ ${e} }`},mt=t=>{let e=t.properties.map(r).join(", ");return e.length==0?"{ }":`{ ${e} }`},ft=new Set(["FunctionDeclaration","IfStatement","ForStatement","ForInStatement","ForOfStatement","WhileStatement","TryStatement","SwitchStatement","ClassDeclaration","BlockStatement"]),S=t=>{let e=r(t);return !e||e===";"?"":ft.has(t.type)?e:e+";"},xt=t=>t.body.map(S).filter(Boolean).join(" "),$t=t=>t.shorthand?r(t.value):`${t.computed?`[${r(t.key)}]`:r(t.key)}: ${r(t.value)}`,St=t=>`${r(t.left)} = ${r(t.right)}`,Et=t=>"return "+r(t.argument),Tt=t=>{let e=t.test?`case ${r(t.test)}:`:"default:",n=t.consequent.map(S).filter(Boolean).join(" ");return n?`${e} ${n}`:e},gt=t=>`switch (${r(t.discriminant)}) { ${t.cases.map(r).join(" ")} }`,ht=t=>t.value.raw,bt=t=>"`"+t.quasis.map((e,n)=>r(e)+(e.tail?"":"${"+r(t.expressions[n])+"}")).join("")+"`",wt=t=>"throw "+r(t.argument),At=t=>{let e=`try ${r(t.block)}`;if(t.handler){let n=t.handler.param;e+=n?` catch (${r(n)}) ${r(t.handler.body)}`:` catch ${r(t.handler.body)}`;}return e+(t.finalizer?` finally ${r(t.finalizer)}`:"")},Bt=t=>{let e=p(t.argument,Precedence.Unary,false),n=t.operator.length>1||(t.operator==="+"||t.operator==="-")&&e[0]===t.operator?" ":"";return t.operator+n+e},Pt=t=>t.prefix==true?t.operator+r(t.argument):r(t.argument)+t.operator,It=t=>`${t.kind} ${t.declarations.map(I).join(", ")}`,I=t=>t.init?`${r(t.id)} = ${r(t.init)}`:r(t.id),jt=t=>`while (${r(t.test)}) ${r(t.body)}`,Ct=t=>{if(t.importKind==="type")return "";let e=r(t.source);if(t.specifiers.length==0)return `import ${e}`;let n=t.specifiers.find(i=>i.type==="ImportDefaultSpecifier"||i.type==="ImportNamespaceSpecifier"),s=t.specifiers.filter(i=>i.type==="ImportSpecifier"&&i.importKind!=="type");if(!n&&s.length===0)return "";let o=[];return n&&o.push(r(n)),s.length&&o.push(`{ ${s.map(j).join(u)} }`),`import ${o.join(u)} from ${e}`},Nt=t=>r(t.local),Dt=t=>`* as ${r(t.local)}`,j=t=>t.imported.name===t.local.name?`${r(t.local)}`:`${r(t.imported)} as ${r(t.local)}`,kt=t=>{if(t.declaration){let n=r(t.declaration);return n?`export ${n}`:""}return `export { ${t.specifiers.map(C).join(u)} }`},Ft=t=>`export default ${r(t.declaration)}`,C=t=>t.local.name===t.exported.name?`${r(t.local)}`:`${r(t.local)} as ${r(t.exported)}`,Lt=t=>`...${r(t.argument)}`,vt=t=>`...${r(t.argument)}`,Ot=()=>"",Jt=t=>r(t.expression),Rt=t=>r(desugarJSX(t)),Wt=t=>r(desugarJSX(t)),T={print:r,JSXElement:Rt,JSXFragment:Wt,ArrayExpression:_,ArrayPattern:U,ArrowFunctionExpression:q,AssignmentExpression:V,AssignmentPattern:St,AwaitExpression:M,BinaryExpression:X,BlockStatement:K,BreakStatement:z,CallExpression:Y,ChainExpression:H,ConditionalExpression:G,ContinueStatement:Q,DoWhileStatement:tt,EmptyStatement:d,ExpressionStatement:Z,ForInStatement:et,ForOfStatement:rt,ForStatement:nt,FunctionDeclaration:st,FunctionExpression:ot,Identifier:P,Placeholder:it,IfStatement:at,ImportDeclaration:Ct,ImportDefaultSpecifier:Nt,ImportNamespaceSpecifier:Dt,ImportSpecifier:j,ExportNamedDeclaration:kt,ExportDefaultDeclaration:Ft,ExportSpecifier:C,JsonExpression:pt,Literal:ct,LogicalExpression:lt,MemberExpression:ut,ObjectExpression:yt,ObjectPattern:mt,Program:xt,Property:$t,ReturnStatement:Et,SwitchCase:Tt,SwitchStatement:gt,TemplateElement:ht,TemplateLiteral:bt,ThrowStatement:wt,TryStatement:At,UnaryExpression:Bt,UpdateExpression:Pt,VariableDeclaration:It,VariableDeclarator:I,WhileStatement:jt,SpreadElement:Lt,RestElement:vt,TSTypeAliasDeclaration:Ot,TSAsExpression:Jt};export{$ as getExpressionPrecedence,b as isPrimaryOperand,A as mixesCoalesce,B as needsParens,r as print,y as printJson,R as printWithProfile,w as unaryLike};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tslite/printer",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.2",
|
|
4
4
|
"description": "TSLite printer — emits JavaScript source from an AST (and from the hybrid #expr JSON form)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -40,9 +40,9 @@
|
|
|
40
40
|
"access": "public"
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"@tslite/capability": "0.4.
|
|
44
|
-
"@tslite/
|
|
45
|
-
"@tslite/
|
|
43
|
+
"@tslite/capability": "0.4.1",
|
|
44
|
+
"@tslite/sanitize": "0.1.8",
|
|
45
|
+
"@tslite/core": "0.8.1"
|
|
46
46
|
},
|
|
47
47
|
"scripts": {
|
|
48
48
|
"build": "tsup",
|