@tslite/codec 0.2.1 → 0.3.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 printer=require('@tslite/printer'),core=require('@tslite/core'),a=require('@tslite/core/builders');function _interopNamespace(e){if(e&&e.__esModule)return e;var n=Object.create(null);if(e){Object.keys(e).forEach(function(k){if(k!=='default'){var d=Object.getOwnPropertyDescriptor(e,k);Object.defineProperty(n,k,d.get?d:{enumerable:true,get:function(){return e[k]}});}})}n.default=e;return Object.freeze(n)}var a__namespace=/*#__PURE__*/_interopNamespace(a);var c=e=>{if(e.type==="Literal")return e.value;if(e.type==="ArrayExpression")return e.elements.map(c);if(e.type==="ObjectExpression")return b(e);let t=r(e);if(t&&typeof t=="object"&&"type"in t){let{type:s,...l}=t;return {[core.AST_MARKER]:s,...l}}return {[core.AST_MARKER]:"Unknown",value:t}},h=e=>e.type==="Literal"?e.value:printer.print(e);function b(e){let t=e.properties.flatMap(s=>{if(s.type==="SpreadElement"){let i=c(s.argument);if(typeof i=="object"&&i!==null&&!Array.isArray(i)&&!(core.AST_MARKER in i))return Object.entries(i);let y=r(s);return [["...spread",{[core.AST_MARKER]:"SpreadElement",argument:y?.argument}]]}let l=h(s.key);return s.computed?[[`[${l}]`,c(s.value)]]:[[l,c(s.value)]]});return Object.fromEntries(t)}var p=e=>{let t={};for(let s in e)e[s]==false||e[s]==null||(t[s]=e[s]);return t},r=e=>!e||typeof e!="object"?e:Array.isArray(e)?e.map(r):g[e.type]?.(e),g={ArrayExpression:e=>({type:"ArrayExpression",elements:r(e.elements||[])}),ArrayPattern:e=>({type:"ArrayPattern",elements:r(e.elements||[])}),ArrowFunctionExpression:e=>({type:"ArrowFunctionExpression",params:r(e.params||[]),body:r(e.body),...p({async:e.async})}),AssignmentExpression:e=>({type:"AssignmentExpression",operator:e.operator,left:r(e.left),right:r(e.right)}),AssignmentProperty:e=>({type:"Property",key:r(e.key),value:r(e.value),...p({shorthand:e.shorthand})}),AwaitExpression:e=>({type:"AwaitExpression",argument:r(e.argument)}),BinaryExpression:e=>({type:"BinaryExpression",operator:e.operator,left:r(e.left),right:r(e.right)}),BlockStatement:e=>({type:"BlockStatement",body:r(e.body||[])}),BreakStatement:e=>({type:"BreakStatement",...p({label:r(e.label)})}),CallExpression:e=>({type:"CallExpression",callee:r(e.callee),arguments:r(e.arguments||[]),...p({optional:e.optional})}),CatchClause:e=>({type:"CatchClause",param:r(e.param||[]),body:r(e.body)}),ChainExpression:e=>({type:"ChainExpression",expression:r(e.expression)}),ConditionalExpression:e=>({type:"ConditionalExpression",test:r(e.test),consequent:r(e.consequent),alternate:r(e.alternate)}),ContinueStatement:e=>({type:"ContinueStatement",...p({label:r(e.label)})}),DoWhileStatement:e=>({type:"DoWhileStatement",body:r(e.body),test:r(e.test)}),EmptyStatement:()=>({type:"EmptyStatement"}),ExpressionStatement:e=>({type:"ExpressionStatement",expression:r(e.expression)}),ForInStatement:e=>({type:"ForInStatement",left:r(e.left),right:r(e.right),body:r(e.body)}),ForOfStatement:e=>({type:"ForOfStatement",left:r(e.left),right:r(e.right),body:r(e.body),...p({await:e.await})}),ForStatement:e=>({type:"ForStatement",body:r(e.body),...p({init:r(e.init),test:r(e.test),update:r(e.update)})}),FunctionDeclaration:e=>({type:"FunctionDeclaration",id:r(e.id),params:r(e.params||[]),body:r(e.body),...p(core.pick(e,["async"]))}),FunctionExpression:e=>({type:"FunctionExpression",params:r(e.params||[]),body:r(e.body),...p({id:e.id?r(e.id):false,async:e.async})}),Identifier:e=>({type:"Identifier",name:e.name}),IfStatement:e=>({type:"IfStatement",test:r(e.test),consequent:r(e.consequent),alternate:r(e.alternate)}),JsonExpression:e=>({type:"JsonExpression",body:e.body}),Literal:e=>({type:"Literal",value:e.value,raw:e.raw}),LogicalExpression:e=>({type:"LogicalExpression",operator:e.operator,left:r(e.left),right:r(e.right)}),MemberExpression:e=>({type:"MemberExpression",object:r(e.object),property:r(e.property),...p(core.pick(e,["computed","optional"]))}),NewExpression:e=>({type:"NewExpression",callee:r(e.callee),arguments:r(e.arguments||[])}),ObjectExpression:e=>({type:"JsonExpression",body:b(e)}),ObjectPattern:e=>({type:"ObjectPattern",properties:r(e.properties||[])}),Program:e=>({type:"Program",body:r(e.body)}),Property:e=>({type:"Property",key:r(e.key),value:r(e.value),...p(core.pick(e,["kind","shorthand","computed"]))}),ReturnStatement:e=>({type:"ReturnStatement",argument:r(e.argument)}),SwitchCase:e=>({type:"SwitchCase",test:r(e.test),consequent:r(e.consequent)}),SwitchStatement:e=>({type:"SwitchStatement",discriminant:r(e.discriminant),cases:r(e.cases)}),TemplateElement:e=>({type:"TemplateElement",value:e.value,...p({tail:e.tail})}),TemplateLiteral:e=>({type:"TemplateLiteral",quasis:r(e.quasis),expressions:r(e.expressions)}),ThrowStatement:e=>({type:"ThrowStatement",argument:r(e.argument)}),TryStatement:e=>({type:"TryStatement",block:r(e.block),handler:r(e.handler),finalizer:r(e.finalizer)}),UnaryExpression:e=>({type:"UnaryExpression",operator:e.operator,argument:r(e.argument)}),UpdateExpression:e=>({type:"UpdateExpression",operator:e.operator,argument:r(e.argument),...p({prefix:e.prefix})}),VariableDeclaration:e=>({type:"VariableDeclaration",kind:e.kind,declarations:r(e.declarations||[])}),VariableDeclarator:e=>({type:"VariableDeclarator",id:r(e.id),init:r(e.init)}),WhileStatement:e=>({type:"WhileStatement",test:r(e.test),body:r(e.body)}),SpreadElement:e=>({type:"SpreadElement",argument:r(e.argument)}),RestElement:e=>({type:"RestElement",argument:r(e.argument)})};function n(e,t=new WeakMap){if(e==null)return e;if(typeof e!="object")throw new core.TSLiteError("not-an-object","AST node must be an object");if(t.has(e))throw new core.TSLiteError("circular-reference","Circular reference detected in AST",{phase:"ast"});t.set(e,true);try{if(Array.isArray(e))return e.map(l=>n(l,t));let s=e;if(s.type==="JsonExpression")return S(s.body,t);if(typeof s.type=="string"&&E[s.type])return E[s.type](s,t);throw new core.TSLiteError("unknown-node-type",`Unknown node type: ${s.type}`,{type:s.type})}finally{t.delete(e);}}function S(e,t=new WeakMap){if(e===null||typeof e!="object")return a__namespace.literal(e);if(t.has(e))throw new core.TSLiteError("circular-reference","Circular reference detected in JSON",{phase:"json"});t.set(e,true);try{if(Array.isArray(e))return a__namespace.arrayExpression(e.map(i=>S(i,t)));let s=e;if(core.AST_MARKER in s){let i=s[core.AST_MARKER];if(!core.isValidAstEntryType(i))throw core.invalidAstEntryError(i);let y={...s,type:i};return delete y[core.AST_MARKER],k(y,t)}let l=Object.entries(s).map(([i,y])=>a__namespace.property(a__namespace.identifier(i),S(y,t),!1,!1));return a__namespace.objectExpression(l)}finally{t.delete(e);}}function k(e,t){if(typeof e.type!="string")throw new core.TSLiteError("missing-node-type","AST node must have a type property");if(!E[e.type])throw new core.TSLiteError("unknown-node-type",`Unknown AST node type: ${e.type}`,{type:e.type});return E[e.type](e,t)}var E={ArrayExpression:(e,t)=>a__namespace.arrayExpression(o(e.elements,t)),ArrayPattern:(e,t)=>a__namespace.arrayPattern(o(e.elements,t)),ArrowFunctionExpression:(e,t)=>a__namespace.arrowFunctionExpression(o(e.params,t),n(e.body,t),e.async||false,e.generator||false),AssignmentExpression:(e,t)=>a__namespace.assignmentExpression(e.operator,n(e.left,t),n(e.right,t)),AssignmentProperty:(e,t)=>a__namespace.assignmentProperty(n(e.key,t),n(e.value,t),e.shorthand||false),AwaitExpression:(e,t)=>a__namespace.awaitExpression(n(e.argument,t)),BinaryExpression:(e,t)=>a__namespace.binaryExpression(e.operator,n(e.left,t),n(e.right,t)),BlockStatement:(e,t)=>a__namespace.blockStatement(o(e.body,t)),BreakStatement:(e,t)=>a__namespace.breakStatement(e.label?n(e.label,t):null),CallExpression:(e,t)=>a__namespace.callExpression(n(e.callee,t),o(e.arguments,t),e.optional||false),CatchClause:(e,t)=>a__namespace.catchClause(e.param?n(e.param,t):null,n(e.body,t)),ChainExpression:(e,t)=>a__namespace.chainExpression(n(e.expression,t)),ConditionalExpression:(e,t)=>a__namespace.conditionalExpression(n(e.test,t),n(e.consequent,t),n(e.alternate,t)),ContinueStatement:(e,t)=>a__namespace.continueStatement(e.label?n(e.label,t):null),DoWhileStatement:(e,t)=>a__namespace.doWhileStatement(n(e.body,t),n(e.test,t)),EmptyStatement:()=>a__namespace.emptyStatement(),ExpressionStatement:(e,t)=>a__namespace.expressionStatement(n(e.expression,t)),ForInStatement:(e,t)=>a__namespace.forInStatement(n(e.left,t),n(e.right,t),n(e.body,t)),ForOfStatement:(e,t)=>a__namespace.forOfStatement(n(e.left,t),n(e.right,t),n(e.body,t),e.await||false),ForStatement:(e,t)=>a__namespace.forStatement(e.init?n(e.init,t):null,e.test?n(e.test,t):null,e.update?n(e.update,t):null,n(e.body,t)),FunctionDeclaration:(e,t)=>a__namespace.functionDeclaration(n(e.id,t),o(e.params,t),n(e.body,t),e.generator||false,e.async||false),FunctionExpression:(e,t)=>a__namespace.functionExpression(e.id?n(e.id,t):null,o(e.params,t),n(e.body,t),e.async||false,e.generator||false),Identifier:e=>a__namespace.identifier(e.name),IfStatement:(e,t)=>a__namespace.ifStatement(n(e.test,t),n(e.consequent,t),e.alternate?n(e.alternate,t):null),Literal:e=>a__namespace.literal(e.value),LogicalExpression:(e,t)=>a__namespace.logicalExpression(e.operator,n(e.left,t),n(e.right,t)),MemberExpression:(e,t)=>a__namespace.memberExpression(n(e.object,t),n(e.property,t),e.computed||false,e.optional||false),NewExpression:(e,t)=>a__namespace.newExpression(n(e.callee,t),o(e.arguments,t)),ObjectExpression:(e,t)=>a__namespace.objectExpression(o(e.properties,t)),ObjectPattern:(e,t)=>a__namespace.objectPattern(o(e.properties,t)),Program:(e,t)=>a__namespace.program(o(e.body,t)),Property:(e,t)=>a__namespace.property(n(e.key,t),n(e.value,t),e.shorthand||false,e.computed||false),RestElement:(e,t)=>a__namespace.restElement(n(e.argument,t)),ReturnStatement:(e,t)=>a__namespace.returnStatement(e.argument?n(e.argument,t):null),SpreadElement:(e,t)=>a__namespace.spreadElement(n(e.argument,t)),SwitchCase:(e,t)=>a__namespace.switchCase(e.test?n(e.test,t):null,o(e.consequent,t)),SwitchStatement:(e,t)=>a__namespace.switchStatement(n(e.discriminant,t),o(e.cases,t)),TemplateElement:e=>a__namespace.templateElement(e.value,e.tail||false),TemplateLiteral:(e,t)=>a__namespace.templateLiteral(o(e.quasis,t),o(e.expressions,t)),ThrowStatement:(e,t)=>a__namespace.throwStatement(n(e.argument,t)),TryStatement:(e,t)=>a__namespace.tryStatement(n(e.block,t),e.handler?n(e.handler,t):null,e.finalizer?n(e.finalizer,t):null),UnaryExpression:(e,t)=>a__namespace.unaryExpression(e.operator,n(e.argument,t)),UpdateExpression:(e,t)=>a__namespace.updateExpression(e.operator,n(e.argument,t),e.prefix||false),VariableDeclaration:(e,t)=>a__namespace.variableDeclaration(e.kind,o(e.declarations,t)),VariableDeclarator:(e,t)=>a__namespace.variableDeclarator(n(e.id,t),e.init?n(e.init,t):null),WhileStatement:(e,t)=>a__namespace.whileStatement(n(e.test,t),n(e.body,t))};function o(e,t){return e?e.map(s=>s!=null?n(s,t):null):[]}exports.decode=n;exports.encode=r;exports.fromJson=S;exports.objectToJson=b;exports.valueToJson=c;
1
+ 'use strict';var printer=require('@tslite/printer'),core=require('@tslite/core'),a=require('@tslite/core/builders');function _interopNamespace(e){if(e&&e.__esModule)return e;var n=Object.create(null);if(e){Object.keys(e).forEach(function(k){if(k!=='default'){var d=Object.getOwnPropertyDescriptor(e,k);Object.defineProperty(n,k,d.get?d:{enumerable:true,get:function(){return e[k]}});}})}n.default=e;return Object.freeze(n)}var a__namespace=/*#__PURE__*/_interopNamespace(a);var c=e=>{if(e.type==="Literal")return e.value;if(e.type==="ArrayExpression")return e.elements.map(c);if(e.type==="ObjectExpression")return u(e);let t=r(e);if(t&&typeof t=="object"&&"type"in t){let{type:s,...l}=t;return {"#expr":s,...l}}return {"#expr":"Unknown",value:t}},d=e=>e.type==="Literal"?e.value:printer.print(e);function u(e){let t=e.properties.flatMap(s=>{if(s.type==="SpreadElement"){let i=c(s.argument);return typeof i=="object"&&i!==null&&!Array.isArray(i)&&!("#expr"in i)?Object.entries(i):[["...spread",{"#expr":"SpreadElement",argument:r(s)?.argument}]]}let l=d(s.key);return s.computed?[[`[${l}]`,c(s.value)]]:[[l,c(s.value)]]});return Object.fromEntries(t)}var p=e=>{let t={};for(let s in e)e[s]==false||e[s]==null||(t[s]=e[s]);return t},r=e=>!e||typeof e!="object"?e:Array.isArray(e)?e.map(r):f[e.type]?.(e),f={ArrayExpression:e=>({type:"ArrayExpression",elements:r(e.elements||[])}),ArrayPattern:e=>({type:"ArrayPattern",elements:r(e.elements||[])}),ArrowFunctionExpression:e=>({type:"ArrowFunctionExpression",params:r(e.params||[]),body:r(e.body),...p({async:e.async})}),AssignmentExpression:e=>({type:"AssignmentExpression",operator:e.operator,left:r(e.left),right:r(e.right)}),AssignmentProperty:e=>({type:"Property",key:r(e.key),value:r(e.value),...p({shorthand:e.shorthand})}),AwaitExpression:e=>({type:"AwaitExpression",argument:r(e.argument)}),BinaryExpression:e=>({type:"BinaryExpression",operator:e.operator,left:r(e.left),right:r(e.right)}),BlockStatement:e=>({type:"BlockStatement",body:r(e.body||[])}),BreakStatement:e=>({type:"BreakStatement",...p({label:r(e.label)})}),CallExpression:e=>({type:"CallExpression",callee:r(e.callee),arguments:r(e.arguments||[]),...p({optional:e.optional})}),CatchClause:e=>({type:"CatchClause",param:r(e.param||[]),body:r(e.body)}),ChainExpression:e=>({type:"ChainExpression",expression:r(e.expression)}),ConditionalExpression:e=>({type:"ConditionalExpression",test:r(e.test),consequent:r(e.consequent),alternate:r(e.alternate)}),ContinueStatement:e=>({type:"ContinueStatement",...p({label:r(e.label)})}),DoWhileStatement:e=>({type:"DoWhileStatement",body:r(e.body),test:r(e.test)}),EmptyStatement:()=>({type:"EmptyStatement"}),ExpressionStatement:e=>({type:"ExpressionStatement",expression:r(e.expression)}),ForInStatement:e=>({type:"ForInStatement",left:r(e.left),right:r(e.right),body:r(e.body)}),ForOfStatement:e=>({type:"ForOfStatement",left:r(e.left),right:r(e.right),body:r(e.body),...p({await:e.await})}),ForStatement:e=>({type:"ForStatement",body:r(e.body),...p({init:r(e.init),test:r(e.test),update:r(e.update)})}),FunctionDeclaration:e=>({type:"FunctionDeclaration",id:r(e.id),params:r(e.params||[]),body:r(e.body),...p(core.pick(e,["async"]))}),FunctionExpression:e=>({type:"FunctionExpression",params:r(e.params||[]),body:r(e.body),...p({id:e.id?r(e.id):false,async:e.async})}),Identifier:e=>({type:"Identifier",name:e.name}),IfStatement:e=>({type:"IfStatement",test:r(e.test),consequent:r(e.consequent),alternate:r(e.alternate)}),JsonExpression:e=>({type:"JsonExpression",body:e.body}),Literal:e=>({type:"Literal",value:e.value,raw:e.raw}),LogicalExpression:e=>({type:"LogicalExpression",operator:e.operator,left:r(e.left),right:r(e.right)}),MemberExpression:e=>({type:"MemberExpression",object:r(e.object),property:r(e.property),...p(core.pick(e,["computed","optional"]))}),NewExpression:e=>({type:"NewExpression",callee:r(e.callee),arguments:r(e.arguments||[])}),ObjectExpression:e=>({type:"JsonExpression",body:u(e)}),ObjectPattern:e=>({type:"ObjectPattern",properties:r(e.properties||[])}),Program:e=>({type:"Program",body:r(e.body)}),Property:e=>({type:"Property",key:r(e.key),value:r(e.value),...p(core.pick(e,["kind","shorthand","computed"]))}),ReturnStatement:e=>({type:"ReturnStatement",argument:r(e.argument)}),SwitchCase:e=>({type:"SwitchCase",test:r(e.test),consequent:r(e.consequent)}),SwitchStatement:e=>({type:"SwitchStatement",discriminant:r(e.discriminant),cases:r(e.cases)}),TemplateElement:e=>({type:"TemplateElement",value:e.value,...p({tail:e.tail})}),TemplateLiteral:e=>({type:"TemplateLiteral",quasis:r(e.quasis),expressions:r(e.expressions)}),ThrowStatement:e=>({type:"ThrowStatement",argument:r(e.argument)}),TryStatement:e=>({type:"TryStatement",block:r(e.block),handler:r(e.handler),finalizer:r(e.finalizer)}),UnaryExpression:e=>({type:"UnaryExpression",operator:e.operator,argument:r(e.argument)}),UpdateExpression:e=>({type:"UpdateExpression",operator:e.operator,argument:r(e.argument),...p({prefix:e.prefix})}),VariableDeclaration:e=>({type:"VariableDeclaration",kind:e.kind,declarations:r(e.declarations||[])}),VariableDeclarator:e=>({type:"VariableDeclarator",id:r(e.id),init:r(e.init)}),WhileStatement:e=>({type:"WhileStatement",test:r(e.test),body:r(e.body)}),SpreadElement:e=>({type:"SpreadElement",argument:r(e.argument)}),RestElement:e=>({type:"RestElement",argument:r(e.argument)})};function n(e,t=new WeakMap){if(e==null)return e;if(typeof e!="object")throw new core.TSLiteError("not-an-object","AST node must be an object");if(t.has(e))throw new core.TSLiteError("circular-reference","Circular reference detected in AST",{phase:"ast"});t.set(e,true);try{if(Array.isArray(e))return e.map(l=>n(l,t));let s=e;if(s.type==="JsonExpression")return b(s.body,t);if(typeof s.type=="string"&&S[s.type])return S[s.type](s,t);throw new core.TSLiteError("unknown-node-type",`Unknown node type: ${s.type}`,{type:s.type})}finally{t.delete(e);}}function b(e,t=new WeakMap){if(e===null||typeof e!="object")return a__namespace.literal(e);if(t.has(e))throw new core.TSLiteError("circular-reference","Circular reference detected in JSON",{phase:"json"});t.set(e,true);try{if(Array.isArray(e))return a__namespace.arrayExpression(e.map(i=>b(i,t)));let s=e;if("#expr"in s){let i=s["#expr"];if(!core.isValidAstEntryType(i))throw core.invalidAstEntryError(i);let m={...s,type:i};return delete m["#expr"],w(m,t)}let l=Object.entries(s).map(([i,m])=>a__namespace.property(a__namespace.identifier(i),b(m,t),!1,!1));return a__namespace.objectExpression(l)}finally{t.delete(e);}}function w(e,t){if(typeof e.type!="string")throw new core.TSLiteError("missing-node-type","AST node must have a type property");if(!S[e.type])throw new core.TSLiteError("unknown-node-type",`Unknown AST node type: ${e.type}`,{type:e.type});return S[e.type](e,t)}var S={ArrayExpression:(e,t)=>a__namespace.arrayExpression(o(e.elements,t)),ArrayPattern:(e,t)=>a__namespace.arrayPattern(o(e.elements,t)),ArrowFunctionExpression:(e,t)=>a__namespace.arrowFunctionExpression(o(e.params,t),n(e.body,t),e.async||false,e.generator||false),AssignmentExpression:(e,t)=>a__namespace.assignmentExpression(e.operator,n(e.left,t),n(e.right,t)),AssignmentProperty:(e,t)=>a__namespace.assignmentProperty(n(e.key,t),n(e.value,t),e.shorthand||false),AwaitExpression:(e,t)=>a__namespace.awaitExpression(n(e.argument,t)),BinaryExpression:(e,t)=>a__namespace.binaryExpression(e.operator,n(e.left,t),n(e.right,t)),BlockStatement:(e,t)=>a__namespace.blockStatement(o(e.body,t)),BreakStatement:(e,t)=>a__namespace.breakStatement(e.label?n(e.label,t):null),CallExpression:(e,t)=>a__namespace.callExpression(n(e.callee,t),o(e.arguments,t),e.optional||false),CatchClause:(e,t)=>a__namespace.catchClause(e.param?n(e.param,t):null,n(e.body,t)),ChainExpression:(e,t)=>a__namespace.chainExpression(n(e.expression,t)),ConditionalExpression:(e,t)=>a__namespace.conditionalExpression(n(e.test,t),n(e.consequent,t),n(e.alternate,t)),ContinueStatement:(e,t)=>a__namespace.continueStatement(e.label?n(e.label,t):null),DoWhileStatement:(e,t)=>a__namespace.doWhileStatement(n(e.body,t),n(e.test,t)),EmptyStatement:()=>a__namespace.emptyStatement(),ExpressionStatement:(e,t)=>a__namespace.expressionStatement(n(e.expression,t)),ForInStatement:(e,t)=>a__namespace.forInStatement(n(e.left,t),n(e.right,t),n(e.body,t)),ForOfStatement:(e,t)=>a__namespace.forOfStatement(n(e.left,t),n(e.right,t),n(e.body,t),e.await||false),ForStatement:(e,t)=>a__namespace.forStatement(e.init?n(e.init,t):null,e.test?n(e.test,t):null,e.update?n(e.update,t):null,n(e.body,t)),FunctionDeclaration:(e,t)=>a__namespace.functionDeclaration(n(e.id,t),o(e.params,t),n(e.body,t),e.generator||false,e.async||false),FunctionExpression:(e,t)=>a__namespace.functionExpression(e.id?n(e.id,t):null,o(e.params,t),n(e.body,t),e.async||false,e.generator||false),Identifier:e=>a__namespace.identifier(e.name),IfStatement:(e,t)=>a__namespace.ifStatement(n(e.test,t),n(e.consequent,t),e.alternate?n(e.alternate,t):null),Literal:e=>a__namespace.literal(e.value),LogicalExpression:(e,t)=>a__namespace.logicalExpression(e.operator,n(e.left,t),n(e.right,t)),MemberExpression:(e,t)=>a__namespace.memberExpression(n(e.object,t),n(e.property,t),e.computed||false,e.optional||false),NewExpression:(e,t)=>a__namespace.newExpression(n(e.callee,t),o(e.arguments,t)),ObjectExpression:(e,t)=>a__namespace.objectExpression(o(e.properties,t)),ObjectPattern:(e,t)=>a__namespace.objectPattern(o(e.properties,t)),Program:(e,t)=>a__namespace.program(o(e.body,t)),Property:(e,t)=>a__namespace.property(n(e.key,t),n(e.value,t),e.shorthand||false,e.computed||false),RestElement:(e,t)=>a__namespace.restElement(n(e.argument,t)),ReturnStatement:(e,t)=>a__namespace.returnStatement(e.argument?n(e.argument,t):null),SpreadElement:(e,t)=>a__namespace.spreadElement(n(e.argument,t)),SwitchCase:(e,t)=>a__namespace.switchCase(e.test?n(e.test,t):null,o(e.consequent,t)),SwitchStatement:(e,t)=>a__namespace.switchStatement(n(e.discriminant,t),o(e.cases,t)),TemplateElement:e=>a__namespace.templateElement(e.value,e.tail||false),TemplateLiteral:(e,t)=>a__namespace.templateLiteral(o(e.quasis,t),o(e.expressions,t)),ThrowStatement:(e,t)=>a__namespace.throwStatement(n(e.argument,t)),TryStatement:(e,t)=>a__namespace.tryStatement(n(e.block,t),e.handler?n(e.handler,t):null,e.finalizer?n(e.finalizer,t):null),UnaryExpression:(e,t)=>a__namespace.unaryExpression(e.operator,n(e.argument,t)),UpdateExpression:(e,t)=>a__namespace.updateExpression(e.operator,n(e.argument,t),e.prefix||false),VariableDeclaration:(e,t)=>a__namespace.variableDeclaration(e.kind,o(e.declarations,t)),VariableDeclarator:(e,t)=>a__namespace.variableDeclarator(n(e.id,t),e.init?n(e.init,t):null),WhileStatement:(e,t)=>a__namespace.whileStatement(n(e.test,t),n(e.body,t))};function o(e,t){return e?e.map(s=>s!=null?n(s,t):null):[]}exports.decode=n;exports.encode=r;exports.fromJson=b;exports.objectToJson=u;exports.valueToJson=c;
package/dist/index.d.cts CHANGED
@@ -14,30 +14,30 @@ declare const encode: (n: any) => any;
14
14
  /**
15
15
  * decode.ts - Reconstrói AST ESTree a partir de SimpleAST/JSON
16
16
  *
17
- * Fluxo: SimpleAST (JSON com #ast markers) → Full ESTree AST
17
+ * Fluxo: SimpleAST (JSON com #expr markers) → Full ESTree AST
18
18
  *
19
19
  * Este é o inverso do encode():
20
20
  * - encode() compacta AST → JSON (TSLite)
21
21
  * - decode() reconstrói JSON → AST
22
22
  *
23
- * ## Formato #ast
23
+ * ## Formato #expr
24
24
  *
25
- * O marcador `#ast` indica a fronteira JSON → AST. O valor é o tipo do nó:
25
+ * O marcador `#expr` indica a fronteira JSON → AST. O valor é o tipo do nó:
26
26
  *
27
27
  * ```json
28
28
  * {
29
29
  * "name": "João",
30
30
  * "calc": {
31
- * "#ast": "CallExpression",
31
+ * "#expr": "CallExpression",
32
32
  * "callee": { "type": "Identifier", "name": "getValue" },
33
33
  * "arguments": []
34
34
  * }
35
35
  * }
36
36
  * ```
37
37
  *
38
- * - `#ast` marca onde começa o AST (fronteira)
38
+ * - `#expr` marca onde começa o AST (fronteira)
39
39
  * - Dentro do AST, usa `type` normal (ESTree)
40
- * - Só expressões são válidas como `#ast` (coisas que retornam valor)
40
+ * - Só expressões são válidas como `#expr` (coisas que retornam valor)
41
41
  */
42
42
 
43
43
  /** Code dos erros lançados pelo decode do codec. */
@@ -45,7 +45,7 @@ type CodecErrorCode = "not-an-object" | "circular-reference" | "missing-node-typ
45
45
  /**
46
46
  * Reconstrói um nó AST simplificado para AST ESTree completo.
47
47
  *
48
- * @param node - SimpleAST node (pode conter #ast markers)
48
+ * @param node - SimpleAST node (pode conter #expr markers)
49
49
  * @returns Full ESTree AST node
50
50
  *
51
51
  * @example
@@ -54,7 +54,7 @@ type CodecErrorCode = "not-an-object" | "circular-reference" | "missing-node-typ
54
54
  * type: "JsonExpression",
55
55
  * body: {
56
56
  * name: "João",
57
- * calc: { "#ast": "CallExpression", callee: {...}, arguments: [] }
57
+ * calc: { "#expr": "CallExpression", callee: {...}, arguments: [] }
58
58
  * }
59
59
  * };
60
60
  * const fullAst = decode(simpleAst);
@@ -62,10 +62,10 @@ type CodecErrorCode = "not-an-object" | "circular-reference" | "missing-node-typ
62
62
  */
63
63
  declare function decode(node: unknown, visited?: WeakMap<object, boolean>): types.Node;
64
64
  /**
65
- * Converte JSON puro (com possíveis #ast markers) para AST Expression.
65
+ * Converte JSON puro (com possíveis #expr markers) para AST Expression.
66
66
  * Usado internamente para processar o `body` de JsonExpression.
67
67
  *
68
- * @param value - Valor JSON (pode conter #ast markers aninhados)
68
+ * @param value - Valor JSON (pode conter #expr markers aninhados)
69
69
  * @returns AST Expression equivalente
70
70
  */
71
71
  declare function fromJson(value: unknown, visited?: WeakMap<object, boolean>): types.Expression;
@@ -73,15 +73,15 @@ declare function fromJson(value: unknown, visited?: WeakMap<object, boolean>): t
73
73
  /**
74
74
  * json.ts — conversão AST → forma JSON híbrida (encode helpers)
75
75
  *
76
- * Converte nós AST em valores JSON, marcando lógica executável com `#ast`.
76
+ * Converte nós AST em valores JSON, marcando lógica executável com `#expr`.
77
77
  * A direção inversa (JSON → AST) vive em decode.ts; a emissão para código JS
78
78
  * vive em @tslite/printer (printJson).
79
79
  *
80
- * Formato `#ast`: o valor de `#ast` é o tipo do nó; as propriedades ficam no
80
+ * Formato `#expr`: o valor de `#expr` é o tipo do nó; as propriedades ficam no
81
81
  * mesmo nível.
82
82
  *
83
83
  * ```json
84
- * { "#ast": "CallExpression", "callee": { ... }, "arguments": [] }
84
+ * { "#expr": "CallExpression", "callee": { ... }, "arguments": [] }
85
85
  * ```
86
86
  */
87
87
 
@@ -90,12 +90,12 @@ declare function fromJson(value: unknown, visited?: WeakMap<object, boolean>): t
90
90
  * - Literal → valor direto
91
91
  * - ArrayExpression → array JSON
92
92
  * - ObjectExpression → objeto JSON (via objectToJson)
93
- * - Outros → marcador #ast com o nó codificado
93
+ * - Outros → marcador #expr com o nó codificado
94
94
  */
95
95
  declare const valueToJson: (node: any) => any;
96
96
  /**
97
97
  * Converte ObjectExpression AST para objeto JSON.
98
- * Valores estáticos viram JSON direto; valores com lógica recebem `#ast`.
98
+ * Valores estáticos viram JSON direto; valores com lógica recebem `#expr`.
99
99
  */
100
100
  declare function objectToJson(expr: ObjectExpression): any;
101
101
 
package/dist/index.d.ts CHANGED
@@ -14,30 +14,30 @@ declare const encode: (n: any) => any;
14
14
  /**
15
15
  * decode.ts - Reconstrói AST ESTree a partir de SimpleAST/JSON
16
16
  *
17
- * Fluxo: SimpleAST (JSON com #ast markers) → Full ESTree AST
17
+ * Fluxo: SimpleAST (JSON com #expr markers) → Full ESTree AST
18
18
  *
19
19
  * Este é o inverso do encode():
20
20
  * - encode() compacta AST → JSON (TSLite)
21
21
  * - decode() reconstrói JSON → AST
22
22
  *
23
- * ## Formato #ast
23
+ * ## Formato #expr
24
24
  *
25
- * O marcador `#ast` indica a fronteira JSON → AST. O valor é o tipo do nó:
25
+ * O marcador `#expr` indica a fronteira JSON → AST. O valor é o tipo do nó:
26
26
  *
27
27
  * ```json
28
28
  * {
29
29
  * "name": "João",
30
30
  * "calc": {
31
- * "#ast": "CallExpression",
31
+ * "#expr": "CallExpression",
32
32
  * "callee": { "type": "Identifier", "name": "getValue" },
33
33
  * "arguments": []
34
34
  * }
35
35
  * }
36
36
  * ```
37
37
  *
38
- * - `#ast` marca onde começa o AST (fronteira)
38
+ * - `#expr` marca onde começa o AST (fronteira)
39
39
  * - Dentro do AST, usa `type` normal (ESTree)
40
- * - Só expressões são válidas como `#ast` (coisas que retornam valor)
40
+ * - Só expressões são válidas como `#expr` (coisas que retornam valor)
41
41
  */
42
42
 
43
43
  /** Code dos erros lançados pelo decode do codec. */
@@ -45,7 +45,7 @@ type CodecErrorCode = "not-an-object" | "circular-reference" | "missing-node-typ
45
45
  /**
46
46
  * Reconstrói um nó AST simplificado para AST ESTree completo.
47
47
  *
48
- * @param node - SimpleAST node (pode conter #ast markers)
48
+ * @param node - SimpleAST node (pode conter #expr markers)
49
49
  * @returns Full ESTree AST node
50
50
  *
51
51
  * @example
@@ -54,7 +54,7 @@ type CodecErrorCode = "not-an-object" | "circular-reference" | "missing-node-typ
54
54
  * type: "JsonExpression",
55
55
  * body: {
56
56
  * name: "João",
57
- * calc: { "#ast": "CallExpression", callee: {...}, arguments: [] }
57
+ * calc: { "#expr": "CallExpression", callee: {...}, arguments: [] }
58
58
  * }
59
59
  * };
60
60
  * const fullAst = decode(simpleAst);
@@ -62,10 +62,10 @@ type CodecErrorCode = "not-an-object" | "circular-reference" | "missing-node-typ
62
62
  */
63
63
  declare function decode(node: unknown, visited?: WeakMap<object, boolean>): types.Node;
64
64
  /**
65
- * Converte JSON puro (com possíveis #ast markers) para AST Expression.
65
+ * Converte JSON puro (com possíveis #expr markers) para AST Expression.
66
66
  * Usado internamente para processar o `body` de JsonExpression.
67
67
  *
68
- * @param value - Valor JSON (pode conter #ast markers aninhados)
68
+ * @param value - Valor JSON (pode conter #expr markers aninhados)
69
69
  * @returns AST Expression equivalente
70
70
  */
71
71
  declare function fromJson(value: unknown, visited?: WeakMap<object, boolean>): types.Expression;
@@ -73,15 +73,15 @@ declare function fromJson(value: unknown, visited?: WeakMap<object, boolean>): t
73
73
  /**
74
74
  * json.ts — conversão AST → forma JSON híbrida (encode helpers)
75
75
  *
76
- * Converte nós AST em valores JSON, marcando lógica executável com `#ast`.
76
+ * Converte nós AST em valores JSON, marcando lógica executável com `#expr`.
77
77
  * A direção inversa (JSON → AST) vive em decode.ts; a emissão para código JS
78
78
  * vive em @tslite/printer (printJson).
79
79
  *
80
- * Formato `#ast`: o valor de `#ast` é o tipo do nó; as propriedades ficam no
80
+ * Formato `#expr`: o valor de `#expr` é o tipo do nó; as propriedades ficam no
81
81
  * mesmo nível.
82
82
  *
83
83
  * ```json
84
- * { "#ast": "CallExpression", "callee": { ... }, "arguments": [] }
84
+ * { "#expr": "CallExpression", "callee": { ... }, "arguments": [] }
85
85
  * ```
86
86
  */
87
87
 
@@ -90,12 +90,12 @@ declare function fromJson(value: unknown, visited?: WeakMap<object, boolean>): t
90
90
  * - Literal → valor direto
91
91
  * - ArrayExpression → array JSON
92
92
  * - ObjectExpression → objeto JSON (via objectToJson)
93
- * - Outros → marcador #ast com o nó codificado
93
+ * - Outros → marcador #expr com o nó codificado
94
94
  */
95
95
  declare const valueToJson: (node: any) => any;
96
96
  /**
97
97
  * Converte ObjectExpression AST para objeto JSON.
98
- * Valores estáticos viram JSON direto; valores com lógica recebem `#ast`.
98
+ * Valores estáticos viram JSON direto; valores com lógica recebem `#expr`.
99
99
  */
100
100
  declare function objectToJson(expr: ObjectExpression): any;
101
101
 
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- import {print}from'@tslite/printer';import {AST_MARKER,pick,TSLiteError,isValidAstEntryType,invalidAstEntryError}from'@tslite/core';import*as a from'@tslite/core/builders';var c=e=>{if(e.type==="Literal")return e.value;if(e.type==="ArrayExpression")return e.elements.map(c);if(e.type==="ObjectExpression")return b(e);let t=r(e);if(t&&typeof t=="object"&&"type"in t){let{type:s,...l}=t;return {[AST_MARKER]:s,...l}}return {[AST_MARKER]:"Unknown",value:t}},h=e=>e.type==="Literal"?e.value:print(e);function b(e){let t=e.properties.flatMap(s=>{if(s.type==="SpreadElement"){let i=c(s.argument);if(typeof i=="object"&&i!==null&&!Array.isArray(i)&&!(AST_MARKER in i))return Object.entries(i);let y=r(s);return [["...spread",{[AST_MARKER]:"SpreadElement",argument:y?.argument}]]}let l=h(s.key);return s.computed?[[`[${l}]`,c(s.value)]]:[[l,c(s.value)]]});return Object.fromEntries(t)}var p=e=>{let t={};for(let s in e)e[s]==false||e[s]==null||(t[s]=e[s]);return t},r=e=>!e||typeof e!="object"?e:Array.isArray(e)?e.map(r):g[e.type]?.(e),g={ArrayExpression:e=>({type:"ArrayExpression",elements:r(e.elements||[])}),ArrayPattern:e=>({type:"ArrayPattern",elements:r(e.elements||[])}),ArrowFunctionExpression:e=>({type:"ArrowFunctionExpression",params:r(e.params||[]),body:r(e.body),...p({async:e.async})}),AssignmentExpression:e=>({type:"AssignmentExpression",operator:e.operator,left:r(e.left),right:r(e.right)}),AssignmentProperty:e=>({type:"Property",key:r(e.key),value:r(e.value),...p({shorthand:e.shorthand})}),AwaitExpression:e=>({type:"AwaitExpression",argument:r(e.argument)}),BinaryExpression:e=>({type:"BinaryExpression",operator:e.operator,left:r(e.left),right:r(e.right)}),BlockStatement:e=>({type:"BlockStatement",body:r(e.body||[])}),BreakStatement:e=>({type:"BreakStatement",...p({label:r(e.label)})}),CallExpression:e=>({type:"CallExpression",callee:r(e.callee),arguments:r(e.arguments||[]),...p({optional:e.optional})}),CatchClause:e=>({type:"CatchClause",param:r(e.param||[]),body:r(e.body)}),ChainExpression:e=>({type:"ChainExpression",expression:r(e.expression)}),ConditionalExpression:e=>({type:"ConditionalExpression",test:r(e.test),consequent:r(e.consequent),alternate:r(e.alternate)}),ContinueStatement:e=>({type:"ContinueStatement",...p({label:r(e.label)})}),DoWhileStatement:e=>({type:"DoWhileStatement",body:r(e.body),test:r(e.test)}),EmptyStatement:()=>({type:"EmptyStatement"}),ExpressionStatement:e=>({type:"ExpressionStatement",expression:r(e.expression)}),ForInStatement:e=>({type:"ForInStatement",left:r(e.left),right:r(e.right),body:r(e.body)}),ForOfStatement:e=>({type:"ForOfStatement",left:r(e.left),right:r(e.right),body:r(e.body),...p({await:e.await})}),ForStatement:e=>({type:"ForStatement",body:r(e.body),...p({init:r(e.init),test:r(e.test),update:r(e.update)})}),FunctionDeclaration:e=>({type:"FunctionDeclaration",id:r(e.id),params:r(e.params||[]),body:r(e.body),...p(pick(e,["async"]))}),FunctionExpression:e=>({type:"FunctionExpression",params:r(e.params||[]),body:r(e.body),...p({id:e.id?r(e.id):false,async:e.async})}),Identifier:e=>({type:"Identifier",name:e.name}),IfStatement:e=>({type:"IfStatement",test:r(e.test),consequent:r(e.consequent),alternate:r(e.alternate)}),JsonExpression:e=>({type:"JsonExpression",body:e.body}),Literal:e=>({type:"Literal",value:e.value,raw:e.raw}),LogicalExpression:e=>({type:"LogicalExpression",operator:e.operator,left:r(e.left),right:r(e.right)}),MemberExpression:e=>({type:"MemberExpression",object:r(e.object),property:r(e.property),...p(pick(e,["computed","optional"]))}),NewExpression:e=>({type:"NewExpression",callee:r(e.callee),arguments:r(e.arguments||[])}),ObjectExpression:e=>({type:"JsonExpression",body:b(e)}),ObjectPattern:e=>({type:"ObjectPattern",properties:r(e.properties||[])}),Program:e=>({type:"Program",body:r(e.body)}),Property:e=>({type:"Property",key:r(e.key),value:r(e.value),...p(pick(e,["kind","shorthand","computed"]))}),ReturnStatement:e=>({type:"ReturnStatement",argument:r(e.argument)}),SwitchCase:e=>({type:"SwitchCase",test:r(e.test),consequent:r(e.consequent)}),SwitchStatement:e=>({type:"SwitchStatement",discriminant:r(e.discriminant),cases:r(e.cases)}),TemplateElement:e=>({type:"TemplateElement",value:e.value,...p({tail:e.tail})}),TemplateLiteral:e=>({type:"TemplateLiteral",quasis:r(e.quasis),expressions:r(e.expressions)}),ThrowStatement:e=>({type:"ThrowStatement",argument:r(e.argument)}),TryStatement:e=>({type:"TryStatement",block:r(e.block),handler:r(e.handler),finalizer:r(e.finalizer)}),UnaryExpression:e=>({type:"UnaryExpression",operator:e.operator,argument:r(e.argument)}),UpdateExpression:e=>({type:"UpdateExpression",operator:e.operator,argument:r(e.argument),...p({prefix:e.prefix})}),VariableDeclaration:e=>({type:"VariableDeclaration",kind:e.kind,declarations:r(e.declarations||[])}),VariableDeclarator:e=>({type:"VariableDeclarator",id:r(e.id),init:r(e.init)}),WhileStatement:e=>({type:"WhileStatement",test:r(e.test),body:r(e.body)}),SpreadElement:e=>({type:"SpreadElement",argument:r(e.argument)}),RestElement:e=>({type:"RestElement",argument:r(e.argument)})};function n(e,t=new WeakMap){if(e==null)return e;if(typeof e!="object")throw new TSLiteError("not-an-object","AST node must be an object");if(t.has(e))throw new TSLiteError("circular-reference","Circular reference detected in AST",{phase:"ast"});t.set(e,true);try{if(Array.isArray(e))return e.map(l=>n(l,t));let s=e;if(s.type==="JsonExpression")return S(s.body,t);if(typeof s.type=="string"&&E[s.type])return E[s.type](s,t);throw new TSLiteError("unknown-node-type",`Unknown node type: ${s.type}`,{type:s.type})}finally{t.delete(e);}}function S(e,t=new WeakMap){if(e===null||typeof e!="object")return a.literal(e);if(t.has(e))throw new TSLiteError("circular-reference","Circular reference detected in JSON",{phase:"json"});t.set(e,true);try{if(Array.isArray(e))return a.arrayExpression(e.map(i=>S(i,t)));let s=e;if(AST_MARKER in s){let i=s[AST_MARKER];if(!isValidAstEntryType(i))throw invalidAstEntryError(i);let y={...s,type:i};return delete y[AST_MARKER],k(y,t)}let l=Object.entries(s).map(([i,y])=>a.property(a.identifier(i),S(y,t),!1,!1));return a.objectExpression(l)}finally{t.delete(e);}}function k(e,t){if(typeof e.type!="string")throw new TSLiteError("missing-node-type","AST node must have a type property");if(!E[e.type])throw new TSLiteError("unknown-node-type",`Unknown AST node type: ${e.type}`,{type:e.type});return E[e.type](e,t)}var E={ArrayExpression:(e,t)=>a.arrayExpression(o(e.elements,t)),ArrayPattern:(e,t)=>a.arrayPattern(o(e.elements,t)),ArrowFunctionExpression:(e,t)=>a.arrowFunctionExpression(o(e.params,t),n(e.body,t),e.async||false,e.generator||false),AssignmentExpression:(e,t)=>a.assignmentExpression(e.operator,n(e.left,t),n(e.right,t)),AssignmentProperty:(e,t)=>a.assignmentProperty(n(e.key,t),n(e.value,t),e.shorthand||false),AwaitExpression:(e,t)=>a.awaitExpression(n(e.argument,t)),BinaryExpression:(e,t)=>a.binaryExpression(e.operator,n(e.left,t),n(e.right,t)),BlockStatement:(e,t)=>a.blockStatement(o(e.body,t)),BreakStatement:(e,t)=>a.breakStatement(e.label?n(e.label,t):null),CallExpression:(e,t)=>a.callExpression(n(e.callee,t),o(e.arguments,t),e.optional||false),CatchClause:(e,t)=>a.catchClause(e.param?n(e.param,t):null,n(e.body,t)),ChainExpression:(e,t)=>a.chainExpression(n(e.expression,t)),ConditionalExpression:(e,t)=>a.conditionalExpression(n(e.test,t),n(e.consequent,t),n(e.alternate,t)),ContinueStatement:(e,t)=>a.continueStatement(e.label?n(e.label,t):null),DoWhileStatement:(e,t)=>a.doWhileStatement(n(e.body,t),n(e.test,t)),EmptyStatement:()=>a.emptyStatement(),ExpressionStatement:(e,t)=>a.expressionStatement(n(e.expression,t)),ForInStatement:(e,t)=>a.forInStatement(n(e.left,t),n(e.right,t),n(e.body,t)),ForOfStatement:(e,t)=>a.forOfStatement(n(e.left,t),n(e.right,t),n(e.body,t),e.await||false),ForStatement:(e,t)=>a.forStatement(e.init?n(e.init,t):null,e.test?n(e.test,t):null,e.update?n(e.update,t):null,n(e.body,t)),FunctionDeclaration:(e,t)=>a.functionDeclaration(n(e.id,t),o(e.params,t),n(e.body,t),e.generator||false,e.async||false),FunctionExpression:(e,t)=>a.functionExpression(e.id?n(e.id,t):null,o(e.params,t),n(e.body,t),e.async||false,e.generator||false),Identifier:e=>a.identifier(e.name),IfStatement:(e,t)=>a.ifStatement(n(e.test,t),n(e.consequent,t),e.alternate?n(e.alternate,t):null),Literal:e=>a.literal(e.value),LogicalExpression:(e,t)=>a.logicalExpression(e.operator,n(e.left,t),n(e.right,t)),MemberExpression:(e,t)=>a.memberExpression(n(e.object,t),n(e.property,t),e.computed||false,e.optional||false),NewExpression:(e,t)=>a.newExpression(n(e.callee,t),o(e.arguments,t)),ObjectExpression:(e,t)=>a.objectExpression(o(e.properties,t)),ObjectPattern:(e,t)=>a.objectPattern(o(e.properties,t)),Program:(e,t)=>a.program(o(e.body,t)),Property:(e,t)=>a.property(n(e.key,t),n(e.value,t),e.shorthand||false,e.computed||false),RestElement:(e,t)=>a.restElement(n(e.argument,t)),ReturnStatement:(e,t)=>a.returnStatement(e.argument?n(e.argument,t):null),SpreadElement:(e,t)=>a.spreadElement(n(e.argument,t)),SwitchCase:(e,t)=>a.switchCase(e.test?n(e.test,t):null,o(e.consequent,t)),SwitchStatement:(e,t)=>a.switchStatement(n(e.discriminant,t),o(e.cases,t)),TemplateElement:e=>a.templateElement(e.value,e.tail||false),TemplateLiteral:(e,t)=>a.templateLiteral(o(e.quasis,t),o(e.expressions,t)),ThrowStatement:(e,t)=>a.throwStatement(n(e.argument,t)),TryStatement:(e,t)=>a.tryStatement(n(e.block,t),e.handler?n(e.handler,t):null,e.finalizer?n(e.finalizer,t):null),UnaryExpression:(e,t)=>a.unaryExpression(e.operator,n(e.argument,t)),UpdateExpression:(e,t)=>a.updateExpression(e.operator,n(e.argument,t),e.prefix||false),VariableDeclaration:(e,t)=>a.variableDeclaration(e.kind,o(e.declarations,t)),VariableDeclarator:(e,t)=>a.variableDeclarator(n(e.id,t),e.init?n(e.init,t):null),WhileStatement:(e,t)=>a.whileStatement(n(e.test,t),n(e.body,t))};function o(e,t){return e?e.map(s=>s!=null?n(s,t):null):[]}export{n as decode,r as encode,S as fromJson,b as objectToJson,c as valueToJson};
1
+ import {print}from'@tslite/printer';import {pick,TSLiteError,isValidAstEntryType,invalidAstEntryError}from'@tslite/core';import*as a from'@tslite/core/builders';var c=e=>{if(e.type==="Literal")return e.value;if(e.type==="ArrayExpression")return e.elements.map(c);if(e.type==="ObjectExpression")return u(e);let t=r(e);if(t&&typeof t=="object"&&"type"in t){let{type:s,...l}=t;return {"#expr":s,...l}}return {"#expr":"Unknown",value:t}},d=e=>e.type==="Literal"?e.value:print(e);function u(e){let t=e.properties.flatMap(s=>{if(s.type==="SpreadElement"){let i=c(s.argument);return typeof i=="object"&&i!==null&&!Array.isArray(i)&&!("#expr"in i)?Object.entries(i):[["...spread",{"#expr":"SpreadElement",argument:r(s)?.argument}]]}let l=d(s.key);return s.computed?[[`[${l}]`,c(s.value)]]:[[l,c(s.value)]]});return Object.fromEntries(t)}var p=e=>{let t={};for(let s in e)e[s]==false||e[s]==null||(t[s]=e[s]);return t},r=e=>!e||typeof e!="object"?e:Array.isArray(e)?e.map(r):f[e.type]?.(e),f={ArrayExpression:e=>({type:"ArrayExpression",elements:r(e.elements||[])}),ArrayPattern:e=>({type:"ArrayPattern",elements:r(e.elements||[])}),ArrowFunctionExpression:e=>({type:"ArrowFunctionExpression",params:r(e.params||[]),body:r(e.body),...p({async:e.async})}),AssignmentExpression:e=>({type:"AssignmentExpression",operator:e.operator,left:r(e.left),right:r(e.right)}),AssignmentProperty:e=>({type:"Property",key:r(e.key),value:r(e.value),...p({shorthand:e.shorthand})}),AwaitExpression:e=>({type:"AwaitExpression",argument:r(e.argument)}),BinaryExpression:e=>({type:"BinaryExpression",operator:e.operator,left:r(e.left),right:r(e.right)}),BlockStatement:e=>({type:"BlockStatement",body:r(e.body||[])}),BreakStatement:e=>({type:"BreakStatement",...p({label:r(e.label)})}),CallExpression:e=>({type:"CallExpression",callee:r(e.callee),arguments:r(e.arguments||[]),...p({optional:e.optional})}),CatchClause:e=>({type:"CatchClause",param:r(e.param||[]),body:r(e.body)}),ChainExpression:e=>({type:"ChainExpression",expression:r(e.expression)}),ConditionalExpression:e=>({type:"ConditionalExpression",test:r(e.test),consequent:r(e.consequent),alternate:r(e.alternate)}),ContinueStatement:e=>({type:"ContinueStatement",...p({label:r(e.label)})}),DoWhileStatement:e=>({type:"DoWhileStatement",body:r(e.body),test:r(e.test)}),EmptyStatement:()=>({type:"EmptyStatement"}),ExpressionStatement:e=>({type:"ExpressionStatement",expression:r(e.expression)}),ForInStatement:e=>({type:"ForInStatement",left:r(e.left),right:r(e.right),body:r(e.body)}),ForOfStatement:e=>({type:"ForOfStatement",left:r(e.left),right:r(e.right),body:r(e.body),...p({await:e.await})}),ForStatement:e=>({type:"ForStatement",body:r(e.body),...p({init:r(e.init),test:r(e.test),update:r(e.update)})}),FunctionDeclaration:e=>({type:"FunctionDeclaration",id:r(e.id),params:r(e.params||[]),body:r(e.body),...p(pick(e,["async"]))}),FunctionExpression:e=>({type:"FunctionExpression",params:r(e.params||[]),body:r(e.body),...p({id:e.id?r(e.id):false,async:e.async})}),Identifier:e=>({type:"Identifier",name:e.name}),IfStatement:e=>({type:"IfStatement",test:r(e.test),consequent:r(e.consequent),alternate:r(e.alternate)}),JsonExpression:e=>({type:"JsonExpression",body:e.body}),Literal:e=>({type:"Literal",value:e.value,raw:e.raw}),LogicalExpression:e=>({type:"LogicalExpression",operator:e.operator,left:r(e.left),right:r(e.right)}),MemberExpression:e=>({type:"MemberExpression",object:r(e.object),property:r(e.property),...p(pick(e,["computed","optional"]))}),NewExpression:e=>({type:"NewExpression",callee:r(e.callee),arguments:r(e.arguments||[])}),ObjectExpression:e=>({type:"JsonExpression",body:u(e)}),ObjectPattern:e=>({type:"ObjectPattern",properties:r(e.properties||[])}),Program:e=>({type:"Program",body:r(e.body)}),Property:e=>({type:"Property",key:r(e.key),value:r(e.value),...p(pick(e,["kind","shorthand","computed"]))}),ReturnStatement:e=>({type:"ReturnStatement",argument:r(e.argument)}),SwitchCase:e=>({type:"SwitchCase",test:r(e.test),consequent:r(e.consequent)}),SwitchStatement:e=>({type:"SwitchStatement",discriminant:r(e.discriminant),cases:r(e.cases)}),TemplateElement:e=>({type:"TemplateElement",value:e.value,...p({tail:e.tail})}),TemplateLiteral:e=>({type:"TemplateLiteral",quasis:r(e.quasis),expressions:r(e.expressions)}),ThrowStatement:e=>({type:"ThrowStatement",argument:r(e.argument)}),TryStatement:e=>({type:"TryStatement",block:r(e.block),handler:r(e.handler),finalizer:r(e.finalizer)}),UnaryExpression:e=>({type:"UnaryExpression",operator:e.operator,argument:r(e.argument)}),UpdateExpression:e=>({type:"UpdateExpression",operator:e.operator,argument:r(e.argument),...p({prefix:e.prefix})}),VariableDeclaration:e=>({type:"VariableDeclaration",kind:e.kind,declarations:r(e.declarations||[])}),VariableDeclarator:e=>({type:"VariableDeclarator",id:r(e.id),init:r(e.init)}),WhileStatement:e=>({type:"WhileStatement",test:r(e.test),body:r(e.body)}),SpreadElement:e=>({type:"SpreadElement",argument:r(e.argument)}),RestElement:e=>({type:"RestElement",argument:r(e.argument)})};function n(e,t=new WeakMap){if(e==null)return e;if(typeof e!="object")throw new TSLiteError("not-an-object","AST node must be an object");if(t.has(e))throw new TSLiteError("circular-reference","Circular reference detected in AST",{phase:"ast"});t.set(e,true);try{if(Array.isArray(e))return e.map(l=>n(l,t));let s=e;if(s.type==="JsonExpression")return b(s.body,t);if(typeof s.type=="string"&&S[s.type])return S[s.type](s,t);throw new TSLiteError("unknown-node-type",`Unknown node type: ${s.type}`,{type:s.type})}finally{t.delete(e);}}function b(e,t=new WeakMap){if(e===null||typeof e!="object")return a.literal(e);if(t.has(e))throw new TSLiteError("circular-reference","Circular reference detected in JSON",{phase:"json"});t.set(e,true);try{if(Array.isArray(e))return a.arrayExpression(e.map(i=>b(i,t)));let s=e;if("#expr"in s){let i=s["#expr"];if(!isValidAstEntryType(i))throw invalidAstEntryError(i);let m={...s,type:i};return delete m["#expr"],w(m,t)}let l=Object.entries(s).map(([i,m])=>a.property(a.identifier(i),b(m,t),!1,!1));return a.objectExpression(l)}finally{t.delete(e);}}function w(e,t){if(typeof e.type!="string")throw new TSLiteError("missing-node-type","AST node must have a type property");if(!S[e.type])throw new TSLiteError("unknown-node-type",`Unknown AST node type: ${e.type}`,{type:e.type});return S[e.type](e,t)}var S={ArrayExpression:(e,t)=>a.arrayExpression(o(e.elements,t)),ArrayPattern:(e,t)=>a.arrayPattern(o(e.elements,t)),ArrowFunctionExpression:(e,t)=>a.arrowFunctionExpression(o(e.params,t),n(e.body,t),e.async||false,e.generator||false),AssignmentExpression:(e,t)=>a.assignmentExpression(e.operator,n(e.left,t),n(e.right,t)),AssignmentProperty:(e,t)=>a.assignmentProperty(n(e.key,t),n(e.value,t),e.shorthand||false),AwaitExpression:(e,t)=>a.awaitExpression(n(e.argument,t)),BinaryExpression:(e,t)=>a.binaryExpression(e.operator,n(e.left,t),n(e.right,t)),BlockStatement:(e,t)=>a.blockStatement(o(e.body,t)),BreakStatement:(e,t)=>a.breakStatement(e.label?n(e.label,t):null),CallExpression:(e,t)=>a.callExpression(n(e.callee,t),o(e.arguments,t),e.optional||false),CatchClause:(e,t)=>a.catchClause(e.param?n(e.param,t):null,n(e.body,t)),ChainExpression:(e,t)=>a.chainExpression(n(e.expression,t)),ConditionalExpression:(e,t)=>a.conditionalExpression(n(e.test,t),n(e.consequent,t),n(e.alternate,t)),ContinueStatement:(e,t)=>a.continueStatement(e.label?n(e.label,t):null),DoWhileStatement:(e,t)=>a.doWhileStatement(n(e.body,t),n(e.test,t)),EmptyStatement:()=>a.emptyStatement(),ExpressionStatement:(e,t)=>a.expressionStatement(n(e.expression,t)),ForInStatement:(e,t)=>a.forInStatement(n(e.left,t),n(e.right,t),n(e.body,t)),ForOfStatement:(e,t)=>a.forOfStatement(n(e.left,t),n(e.right,t),n(e.body,t),e.await||false),ForStatement:(e,t)=>a.forStatement(e.init?n(e.init,t):null,e.test?n(e.test,t):null,e.update?n(e.update,t):null,n(e.body,t)),FunctionDeclaration:(e,t)=>a.functionDeclaration(n(e.id,t),o(e.params,t),n(e.body,t),e.generator||false,e.async||false),FunctionExpression:(e,t)=>a.functionExpression(e.id?n(e.id,t):null,o(e.params,t),n(e.body,t),e.async||false,e.generator||false),Identifier:e=>a.identifier(e.name),IfStatement:(e,t)=>a.ifStatement(n(e.test,t),n(e.consequent,t),e.alternate?n(e.alternate,t):null),Literal:e=>a.literal(e.value),LogicalExpression:(e,t)=>a.logicalExpression(e.operator,n(e.left,t),n(e.right,t)),MemberExpression:(e,t)=>a.memberExpression(n(e.object,t),n(e.property,t),e.computed||false,e.optional||false),NewExpression:(e,t)=>a.newExpression(n(e.callee,t),o(e.arguments,t)),ObjectExpression:(e,t)=>a.objectExpression(o(e.properties,t)),ObjectPattern:(e,t)=>a.objectPattern(o(e.properties,t)),Program:(e,t)=>a.program(o(e.body,t)),Property:(e,t)=>a.property(n(e.key,t),n(e.value,t),e.shorthand||false,e.computed||false),RestElement:(e,t)=>a.restElement(n(e.argument,t)),ReturnStatement:(e,t)=>a.returnStatement(e.argument?n(e.argument,t):null),SpreadElement:(e,t)=>a.spreadElement(n(e.argument,t)),SwitchCase:(e,t)=>a.switchCase(e.test?n(e.test,t):null,o(e.consequent,t)),SwitchStatement:(e,t)=>a.switchStatement(n(e.discriminant,t),o(e.cases,t)),TemplateElement:e=>a.templateElement(e.value,e.tail||false),TemplateLiteral:(e,t)=>a.templateLiteral(o(e.quasis,t),o(e.expressions,t)),ThrowStatement:(e,t)=>a.throwStatement(n(e.argument,t)),TryStatement:(e,t)=>a.tryStatement(n(e.block,t),e.handler?n(e.handler,t):null,e.finalizer?n(e.finalizer,t):null),UnaryExpression:(e,t)=>a.unaryExpression(e.operator,n(e.argument,t)),UpdateExpression:(e,t)=>a.updateExpression(e.operator,n(e.argument,t),e.prefix||false),VariableDeclaration:(e,t)=>a.variableDeclaration(e.kind,o(e.declarations,t)),VariableDeclarator:(e,t)=>a.variableDeclarator(n(e.id,t),e.init?n(e.init,t):null),WhileStatement:(e,t)=>a.whileStatement(n(e.test,t),n(e.body,t))};function o(e,t){return e?e.map(s=>s!=null?n(s,t):null):[]}export{n as decode,r as encode,b as fromJson,u as objectToJson,c as valueToJson};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tslite/codec",
3
- "version": "0.2.1",
4
- "description": "TSLite codec — serializes AST to hybrid #ast JSON and back (encode/decode)",
3
+ "version": "0.3.0",
4
+ "description": "TSLite codec — serializes AST to hybrid #expr JSON and back (encode/decode)",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
7
7
  "module": "./dist/index.js",
@@ -40,8 +40,8 @@
40
40
  "access": "public"
41
41
  },
42
42
  "dependencies": {
43
- "@tslite/core": "0.3.0",
44
- "@tslite/printer": "0.2.1"
43
+ "@tslite/core": "0.4.0",
44
+ "@tslite/printer": "0.3.0"
45
45
  },
46
46
  "scripts": {
47
47
  "build": "tsup",