estree-toolbox 1.0.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/estree-toolbox.js +1 -0
- package/dist/estree-toolbox.mjs +1 -0
- package/package.json +40 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
((t,e)=>{let i="estree-toolbox";const s={dirname(t){return t===this.sep?t:t.split(this.sep).slice(0,-1).join(this.sep)},resolve(...t){if(!t.length)return this.sep;if(t[0][0]===this.sep)return t.join(this.sep);const e=[...i.split(this.sep),...t.join(this.sep).split(this.sep)],s=[];for(const t of e)".."!==t?"."!==t&&t&&s.push(t):s.pop();return s.join(this.sep)},join(...t){return t.join(this.sep)},sep:"/"},r={};function n(t,e,i){const s=t?.children;!s||i&&s.includes(e)||s.push(e)}function o(t,e,i){return a._load.call({__proto__:null,parentFilename:e?.filename,id:t},t,e,i)}function a(t="",e){this.id=t,this.path=s.dirname(t),this.exports={},n(e,this,!1),this.filename=null,this.loaded=!1,this.children=[]}a.Module=a,a._nodeModulePaths=function(t){const e=[];let i=s.resolve(t);for(;;){e.push(s.join(i,"node_modules"));const t=s.dirname(i);if(t===i)break;i=t}return e},a._cache=Object.create(null),a._extensions=Object.create(null),a._load=function(t,e,i){const o=s.resolve(t).split(s.sep),h=[];for(const t of o)".."!==t?"."!==t&&h.push(t):h.pop();const l=s.resolve(t),p=a._cache[l];if(void 0!==p){if(n(e,p,!0),p.loaded)return p.exports;if(!p[kIsCachedByESMLoader])return getExportsForCircularRequire(p);if(p[kModuleCircularVisited])return getExportsForCircularRequire(p);p[kModuleCircularVisited]=!0}const c=p||new a(l,e);return p||(i&&(r.mainModule=c,c.require.main=r.mainModule,c.id="."),a._cache[l]=c,c.load(l)),c.exports},a.prototype.load=function(t){this.filename??=t,this.paths??=a._nodeModulePaths(s.dirname(t));const e=t.includes(".")?"."+t.split(".").slice(-1)[0]:"";a._extensions[e](this,t),this.loaded=!0},a.prototype.require=function(t){return o(t,this,!1)},Object.assign(a._extensions,{".js":function(e,n){function o(t){return e.require(t)}o.main=r.mainModule,o.extensions=a._extensions,o.cache=a._cache;const h=t[n],l=i;i=s.dirname(n),h(o,e.exports,e),i=l}});const h=()=>o(e,new a(e),!0);"object"==typeof exports?module.exports=h():"function"==typeof define?define("estree-toolbox",[],h):globalThis.estreeToolbox=h()})({"estree-toolbox/index.js"(t,e,i){const s=t("./lib/node-visitor.js"),r=t("./lib/node-transformer.js"),n=t("./lib/code-generator.js"),{Unparser:o,unparse:a}=t("./lib/unparser.js");i.exports={NodeVisitor:s,NodeTransformer:r,BaseCodeGenerator:n,Unparser:o,unparse:a}},"estree-toolbox/lib/code-generator.js"(t,e,i){const s=t("./node-visitor.js");i.exports=class extends s{constructor(t){super(),this._source=[],this._indent=0,this.indentLevel=t?.indent??2}items_view(t,e,i=", "){if(!Array.isArray(e))throw new TypeError(e+" is not a Array.");1===e.length?t.call(this,e[0]):e.forEach((s,r)=>{t.call(this,s),r!==e.length-1&&this.write(i)})}maybe_newline(t=!0){this._source.filter(t=>t.trim()).length&&(t&&this.write(";"),this.write("\n"))}fill(t=""){this.maybe_newline(),this.write(" ".repeat(this._indent*this.indentLevel)+t)}fillBlock(t=!0){t&&this.maybe_newline(!1),this.write(" ".repeat(this._indent*this.indentLevel))}inline(t){const e=this.maybe_newline,i=this.block;this.block=(...t)=>{const r=i.apply(this,t);return t=>{this.maybe_newline=e,this.block=i,r(t),this.maybe_newline=()=>{},this.block=s}};const s=this.block;this.maybe_newline=()=>{},t(),this.maybe_newline=e,this.block=i}write(...t){let e;if(e=t.find(t=>"string"!=typeof t))throw console.error(e),new TypeError(e+" is not a string.");this._source.push(...t)}buffered(t){return t??=[],e=>{const i=this._source;return this._source=t,e(t),this._source=i,t}}block({braces:t}={braces:!0}){return e=>{this.delimit_if("{","}",t)(()=>{const t=this.buffered()(()=>{this._indent++,this.write("\n"),e(),this._indent--});this.write(...t),this.fillBlock()})}}delimit(t,e){return i=>{this.write(t),i(),this.write(e)}}delimit_if(t,e,i){return i?this.delimit(t,e):t=>t()}traverse(t){if(Array.isArray(t))for(const e of t)this.traverse(e);else super.visit(t)}visit(t){return this._source=[],this.traverse(t),this._source.join("")}generic_visit(t){console.warn("Supplementary required "+t.type)}}},"estree-toolbox/lib/node-transformer.js"(t,e,i){const s=t("./node-visitor.js");i.exports=class extends s{generic_visit(t){for(const[e,i]of Object.entries(t))if(Array.isArray(i)){const t=[];for(let e of i){if(e.type){if(e=this.visit(e),!e)continue;if(!e?.type){t.push(...e);continue}}t.push(e)}i.splice(0,i.length,...t)}else if(i?.type){let s=this.visit(i);null==s?delete t[e]:t[e]=s}return t}}},"estree-toolbox/lib/node-visitor.js"(t,e,i){i.exports=class{visit(t){if(!t?.type)throw TypeError(`'${t}' is not a node.`);return(this["visit_"+t.type]??this.generic_visit).call(this,t)}generic_visit(t){for(const[e,i]of Object.entries(t))if(Array.isArray(i))for(const t of i)t?.type&&this.visit(t);else i?.type&&this.visit(i)}}},"estree-toolbox/lib/unparser.js"(t,e,i){const s=t("./code-generator.js");class r{constructor(...t){var e;1!==t.length||(!(e=t[0])||!["object","function"].includes(typeof e)||"number"!=typeof e.length||e.length<0||e.length%1)||(t=t[0]),((t,e,i)=>{for(let s=0;s<t.length;s++){const r=t[s];void 0!==i?e.call(i,r,s):e(r,s)}})(t,(t,e)=>{this[e]=t}),this.length=t.length}has(t){return Array.from(this).includes(t)}}const n=(()=>{let t=0;const e=e=>e?t:t++;return[["MemberExpression",e(!0)],["CallExpression",e(!0)],["NewExpression",e(!0)],["UpdateExpression",e()],["UnaryExpression",e()],[{type:"BinaryExpression",operator:"**"},e()],[{type:"BinaryExpression",operator:new r("*","/","%")},e()],[{type:"BinaryExpression",operator:new r("+","-")},e()],[{type:"BinaryExpression",operator:new r("<<",">>",">>>")},e()],[{type:"BinaryExpression",operator:new r(">","<",">=","<=","in","instanceof")},e()],[{type:"BinaryExpression",operator:new r("==","!=","===","!==")},e()],[{type:"BinaryExpression",operator:"&"},e()],[{type:"BinaryExpression",operator:"^"},e()],[{type:"BinaryExpression",operator:"|"},e()],[{type:"LogicalExpression",operator:"&&"},e()],[{type:"LogicalExpression",operator:"||"},e()],[{type:"CoalesceExpression",operator:"??"},e()],["ConditionalExpression",e()],["AssignmentExpression",e()],["SequenceExpression",e()]]})(),o=(t,e)=>{if("string"==typeof e)return t?.type===e;for(const[i,s]of Object.entries(e))if(s instanceof r){if(!s.has(t[i]))return!1}else if("object"==typeof s&&s){if(!o(t[i],s))return!1}else if(s!==t[i])return!1;return!0},a=(t,e)=>{const i=n.find(([e])=>o(t,e))?.[1],s=n.find(([t])=>o(e,t))?.[1];return i<s};class h extends s{blockNode(t){"BlockStatement"!==t.type?this.block({braces:!1})(()=>{this.traverse(t)}):this.traverse(t)}visit_Program(t){this.traverse(t.body)}visit_VariableDeclaration(t){this.fill(),this.write(t.kind),this.write(" ");for(const e of t.declarations)this.traverse(e)}visit_VariableDeclarator(t){this.traverse(t.id),t.init&&(this.write(" = "),this.traverse(t.init))}visit_Identifier(t){this.write(t.name)}visit_Literal(t){switch(typeof t.value){case"string":this.write(JSON.stringify(t.value));break;case"number":case"boolean":this.write(String(t.value));break;case"bigint":this.write(t.value+"n");break;case"object":t.value instanceof RegExp?this.write(String(t.value)):this.write("null")}}visit_CallExpression(t){this.traverse(t.callee),this.delimit("(",")")(()=>{this.items_view(this.traverse,t.arguments)})}visit_NewExpression(t){this.write("new "),this.traverse(t.callee),this.delimit("(",")")(()=>{this.items_view(this.traverse,t.arguments)})}visit_ArrayExpression(t){this.delimit("[","]")(()=>{this.items_view(this.traverse,t.elements)})}visit_ObjectExpression(t){this.delimit("{","}")(()=>{this.items_view(this.traverse,t.properties)})}visit_Property(t){this.traverse(t.key),this.write(": "),this.traverse(t.value)}visit_ArrowFunctionExpression(t){t.async&&this.write("async "),1===t.params.length&&"Identifier"===t.params[0].type?this.traverse(t.params[0]):this.delimit("(",")")(()=>{this.items_view(this.traverse,t.params)}),this.write(" => "),this.delimit_if("(",")","BlockStatement"!==t.body.type)(()=>{this.traverse(t.body)})}visit_BlockStatement(t){this.block()(()=>{this.traverse(t.body)})}visit_ExpressionStatement(t){this.fill(),this.traverse(t.expression)}visit_AssignmentExpression(t){this.visit_BinaryExpression(t)}visit_BinaryExpression(t){const{left:e,right:i}=t;this.delimit_if("(",")",a(t,e))(()=>{this.traverse(e)}),this.delimit(" "," ")(()=>{this.write(t.operator)}),this.delimit_if("(",")",a(t,i))(()=>{this.traverse(i)})}visit_LogicalExpression(t){this.visit_BinaryExpression(t)}visit_UnaryExpression(t){t.prefix&&(this.write(t.operator),/^\w+$/.test(t.operator)&&this.write(" ")),this.traverse(t.argument),t.prefix||this.write(t.operator)}visit_ConditionalExpression(t){this.delimit_if("(",")",a(t,t.test))(()=>{this.traverse(t.test)}),this.write(" ? "),this.traverse(t.consequent),this.write(" : "),this.delimit_if("(",")",a(t,t.alternate))(()=>{this.traverse(t.alternate)})}visit_MemberExpression(t){this.delimit_if("(",")",a(t,t.object))(()=>{this.traverse(t.object)}),t.optional&&this.write("?."),t.computed?this.delimit("[","]")(()=>{this.traverse(t.property)}):(this.write(t.optional?"?.":"."),this.traverse(t.property))}visit_IfStatement(t){this.fill(),this.write("if "),this.delimit("(",")")(()=>{this.traverse(t.test)}),this.write(" "),this.blockNode(t.consequent),t.alternate&&(this.write("else "),this.blockNode(t.alternate))}visit_SwitchStatement(t){this.fill(),this.write("switch "),this.delimit("(",")")(()=>{this.traverse(t.discriminant)}),this.block()(()=>{this.traverse(t.cases)})}visit_SwitchCase(t){this.fillBlock(),t.test?(this.write("case "),this.delimit("(",")")(()=>{this.traverse(t.test)})):this.write("default "),this.write(":"),this.blockNode(t.consequent)}visit_BreakStatement(t){this.fill(),this.write("break"),t.label&&(this.write(" "),this.traverse(t.label))}visit_ContinueStatement(t){this.fill(),this.write("continue"),t.label&&(this.write(" "),this.traverse(t.label))}visit_ForStatement(t){this.fill(),this.write("for "),this.delimit("(",")")(()=>{let e=this.buffered()(()=>{this.traverse(t.init),this.write(" ; "),this.traverse(t.test),this.write(" ; "),this.traverse(t.update)});this.write(...e)}),this.blockNode(t.body)}visit_UpdateExpression(t){t.prefix&&this.write(t.operator),this.delimit_if("(",")",a(t,t.argument))(()=>{this.traverse(t.argument)}),t.prefix||this.write(t.operator)}visit_ForInStatement(t){this.fill(),this.write("for "),this.delimit("(",")")(()=>{this.inline(()=>{this.traverse(t.left)}),this.write(" in "),this.traverse(t.right)}),this.blockNode(t.body)}visit_EmptyStatement(t){this.fill()}visit_ForOfStatement(t){this.fill(),this.write("for "),t.await&&this.write("await "),this.delimit("(",")")(()=>{this.inline(()=>{this.traverse(t.left)}),this.write(" of "),this.traverse(t.right)}),this.blockNode(t.body)}visit_WhileStatement(t){this.fill(),this.write("while "),this.delimit("(",")")(()=>{this.traverse(t.test)}),this.blockNode(t.body)}visit_DoWhileStatement(t){this.fill(),this.write("do "),this.blockNode(t.body),this.write(" while "),this.delimit("(",")")(()=>{this.traverse(t.test)})}visit_FunctionDeclaration(t){this.fill(),t.async&&this.write("async "),this.write("function"),t.generator&&this.write("*"),this.write(" "),t.id&&this.traverse(t.id),this.delimit("(",")")(()=>{this.items_view(this.traverse,t.params)}),this.write(" "),this.traverse(t.body)}visit_FunctionExpression(t){this.delimit("(",")")(()=>{t.async&&this.write("async "),this.write("function"),t.generator&&this.write("*"),this.write(" "),t.id&&this.traverse(t.id),this.delimit("(",")")(()=>{this.items_view(this.traverse,t.params)}),this.write(" "),this.traverse(t.body)})}visit_AssignmentPattern(t){this.traverse(t.left),this.write(" = "),this.traverse(t.right)}visit_ReturnStatement(t){this.fill("return"),t.argument&&(this.write(" "),this.traverse(t.argument)),this.write(";")}visit_RestElement(t){this.write("..."),this.traverse(t.argument)}visit_SpreadElement(t){this.write("..."),this.traverse(t.argument)}visit_ObjectPattern(t){this.delimit("{","}")(()=>{this.items_view(this.traverse,t.properties)})}visit_ArrayPattern(t){this.delimit("[","]")(()=>{this.items_view(this.traverse,t.elements)})}visit_YieldExpression(t){this.write("yield"),t.delegate&&this.write("*"),t.argument&&(this.write(" "),this.traverse(t.argument))}visit_ThisExpression(t){this.write("this")}visit_ClassDeclaration(t){this.fill("class "),t.id&&this.traverse(t.id),t.superClass&&(this.write(" extends "),this.traverse(t.superClass)),this.block()(()=>{this.traverse(t.body)})}visit_ClassBody(t){this.traverse(t.body)}visit_MethodDefinition(t){switch(this.fill(),t.kind){case"get":this.write("get ");break;case"set":this.write("set ")}const e=t.key;(t=>{t.async&&this.write("async "),t.generator&&this.write("*"),this.traverse(e),this.write(" "),this.delimit("(",")")(()=>{this.items_view(this.traverse,t.params)}),this.write(" "),this.traverse(t.body)})(t.value)}visit_TemplateLiteral(t){const e=[];for(let i=0;i<2*t.quasis.length-1;i++)i%2?e.push(t.expressions[i]):e.push(t.quasis[i]);for(;void 0===e.pop()&&e.length;);this.delimit("`","`")(()=>{for(const t of e)this.delimit_if("${","}","TemplateElement"!==t.type)(()=>{this.traverse(t)})})}visit_TemplateElement(t){this.write(t.value.raw)}visit_Super(t){this.write("super")}visit_ExportNamedDeclaration(t){this.fill("export "),this.delimit_if("{","}",t.specifiers.length)(()=>{this.items_view(this.traverse,t.specifiers)}),t.declaration&&this.inline(()=>{this.traverse(t.declaration)}),t.attributes.length&&(this.write(" with "),this.delimit("{","}")(()=>{this.items_view(this.traverse,t.attributes)}))}visit_ExportSpecifier(t){this.traverse(t.local),t.exported!==t.local&&(this.write(" as "),this.traverse(t.exported))}visit_ExportDefaultDeclaration(t){this.fill("export default "),this.inline(()=>{this.traverse(t.declaration)})}visit_ImportDeclaration(t){this.fill("import ");const e=t.specifiers.filter(t=>!["ImportDefaultSpecifier","ImportNamespaceSpecifier"].includes(t.type));e.length&&this.delimit("{","}")(()=>{this.items_view(this.traverse,e)});const i=t.specifiers.find(t=>"ImportDefaultSpecifier"===t.type);i&&(e.length&&this.write(","),this.traverse(i));const s=t.specifiers.find(t=>"ImportNamespaceSpecifier"===t.type);s&&(e.length&&this.write(","),this.traverse(s)),this.write(" from "),this.traverse(t.source),t.attributes.length&&(this.write(" with "),this.delimit("{","}")(()=>{this.items_view(this.traverse,t.attributes)}))}visit_ImportSpecifier(t){this.traverse(t.local),t.imported!==t.local&&(this.write(" as "),this.traverse(t.imported))}visit_ImportNamespaceSpecifier(t){this.write("* as "),this.traverse(t.local)}visit_ImportDefaultSpecifier(t){this.traverse(t.local)}visit_TryStatement(t){this.fill("try "),this.blockNode(t.block),t.handler&&this.traverse(t.handler),t.finalizer&&(this.write(" finally "),this.blockNode(t.finalizer))}visit_CatchClause(t){this.fill("catch "),this.delimit("(",")")(()=>{this.traverse(t.param)}),this.write(" "),this.blockNode(t.body)}visit_ThrowStatement(t){this.fill("throw "),this.traverse(t.argument)}visit_ChainExpression(t){this.traverse(t.expression)}visit_AwaitExpression(t){this.write("await "),this.traverse(t.argument)}visit_SequenceExpression(t){this.items_view(this.traverse,t.expressions)}}i.exports={Unparser:h,unparse:function(t,e){return new h(e).visit(t)}}}},"./index.js");
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const module=((t,e)=>{let i="estree-toolbox";const s={dirname(t){return t===this.sep?t:t.split(this.sep).slice(0,-1).join(this.sep)},resolve(...t){if(!t.length)return this.sep;if(t[0][0]===this.sep)return t.join(this.sep);const e=[...i.split(this.sep),...t.join(this.sep).split(this.sep)],s=[];for(const t of e)".."!==t?"."!==t&&t&&s.push(t):s.pop();return s.join(this.sep)},join(...t){return t.join(this.sep)},sep:"/"},r={};function n(t,e,i){const s=t?.children;!s||i&&s.includes(e)||s.push(e)}function o(t,e,i){return a._load.call({__proto__:null,parentFilename:e?.filename,id:t},t,e,i)}function a(t="",e){this.id=t,this.path=s.dirname(t),this.exports={},n(e,this,!1),this.filename=null,this.loaded=!1,this.children=[]}a.Module=a,a._nodeModulePaths=function(t){const e=[];let i=s.resolve(t);for(;;){e.push(s.join(i,"node_modules"));const t=s.dirname(i);if(t===i)break;i=t}return e},a._cache=Object.create(null),a._extensions=Object.create(null),a._load=function(t,e,i){const o=s.resolve(t).split(s.sep),h=[];for(const t of o)".."!==t?"."!==t&&h.push(t):h.pop();const l=s.resolve(t),p=a._cache[l];if(void 0!==p){if(n(e,p,!0),p.loaded)return p.exports;if(!p[kIsCachedByESMLoader])return getExportsForCircularRequire(p);if(p[kModuleCircularVisited])return getExportsForCircularRequire(p);p[kModuleCircularVisited]=!0}const c=p||new a(l,e);return p||(i&&(r.mainModule=c,c.require.main=r.mainModule,c.id="."),a._cache[l]=c,c.load(l)),c.exports},a.prototype.load=function(t){this.filename??=t,this.paths??=a._nodeModulePaths(s.dirname(t));const e=t.includes(".")?"."+t.split(".").slice(-1)[0]:"";a._extensions[e](this,t),this.loaded=!0},a.prototype.require=function(t){return o(t,this,!1)},Object.assign(a._extensions,{".js":function(e,n){function o(t){return e.require(t)}o.main=r.mainModule,o.extensions=a._extensions,o.cache=a._cache;const h=t[n],l=i;i=s.dirname(n),h(o,e.exports,e),i=l}});const h=()=>o(e,new a(e),!0);return h()})({"estree-toolbox/index.js"(t,e,i){const s=t("./lib/node-visitor.js"),r=t("./lib/node-transformer.js"),n=t("./lib/code-generator.js"),{Unparser:o,unparse:a}=t("./lib/unparser.js");i.exports={NodeVisitor:s,NodeTransformer:r,BaseCodeGenerator:n,Unparser:o,unparse:a}},"estree-toolbox/lib/code-generator.js"(t,e,i){const s=t("./node-visitor.js");i.exports=class extends s{constructor(t){super(),this._source=[],this._indent=0,this.indentLevel=t?.indent??2}items_view(t,e,i=", "){if(!Array.isArray(e))throw new TypeError(e+" is not a Array.");1===e.length?t.call(this,e[0]):e.forEach((s,r)=>{t.call(this,s),r!==e.length-1&&this.write(i)})}maybe_newline(t=!0){this._source.filter(t=>t.trim()).length&&(t&&this.write(";"),this.write("\n"))}fill(t=""){this.maybe_newline(),this.write(" ".repeat(this._indent*this.indentLevel)+t)}fillBlock(t=!0){t&&this.maybe_newline(!1),this.write(" ".repeat(this._indent*this.indentLevel))}inline(t){const e=this.maybe_newline,i=this.block;this.block=(...t)=>{const r=i.apply(this,t);return t=>{this.maybe_newline=e,this.block=i,r(t),this.maybe_newline=()=>{},this.block=s}};const s=this.block;this.maybe_newline=()=>{},t(),this.maybe_newline=e,this.block=i}write(...t){let e;if(e=t.find(t=>"string"!=typeof t))throw console.error(e),new TypeError(e+" is not a string.");this._source.push(...t)}buffered(t){return t??=[],e=>{const i=this._source;return this._source=t,e(t),this._source=i,t}}block({braces:t}={braces:!0}){return e=>{this.delimit_if("{","}",t)(()=>{const t=this.buffered()(()=>{this._indent++,this.write("\n"),e(),this._indent--});this.write(...t),this.fillBlock()})}}delimit(t,e){return i=>{this.write(t),i(),this.write(e)}}delimit_if(t,e,i){return i?this.delimit(t,e):t=>t()}traverse(t){if(Array.isArray(t))for(const e of t)this.traverse(e);else super.visit(t)}visit(t){return this._source=[],this.traverse(t),this._source.join("")}generic_visit(t){console.warn("Supplementary required "+t.type)}}},"estree-toolbox/lib/node-transformer.js"(t,e,i){const s=t("./node-visitor.js");i.exports=class extends s{generic_visit(t){for(const[e,i]of Object.entries(t))if(Array.isArray(i)){const t=[];for(let e of i){if(e.type){if(e=this.visit(e),!e)continue;if(!e?.type){t.push(...e);continue}}t.push(e)}i.splice(0,i.length,...t)}else if(i?.type){let s=this.visit(i);null==s?delete t[e]:t[e]=s}return t}}},"estree-toolbox/lib/node-visitor.js"(t,e,i){i.exports=class{visit(t){if(!t?.type)throw TypeError(`'${t}' is not a node.`);return(this["visit_"+t.type]??this.generic_visit).call(this,t)}generic_visit(t){for(const[e,i]of Object.entries(t))if(Array.isArray(i))for(const t of i)t?.type&&this.visit(t);else i?.type&&this.visit(i)}}},"estree-toolbox/lib/unparser.js"(t,e,i){const s=t("./code-generator.js");class r{constructor(...t){var e;1!==t.length||(!(e=t[0])||!["object","function"].includes(typeof e)||"number"!=typeof e.length||e.length<0||e.length%1)||(t=t[0]),((t,e,i)=>{for(let s=0;s<t.length;s++){const r=t[s];void 0!==i?e.call(i,r,s):e(r,s)}})(t,(t,e)=>{this[e]=t}),this.length=t.length}has(t){return Array.from(this).includes(t)}}const n=(()=>{let t=0;const e=e=>e?t:t++;return[["MemberExpression",e(!0)],["CallExpression",e(!0)],["NewExpression",e(!0)],["UpdateExpression",e()],["UnaryExpression",e()],[{type:"BinaryExpression",operator:"**"},e()],[{type:"BinaryExpression",operator:new r("*","/","%")},e()],[{type:"BinaryExpression",operator:new r("+","-")},e()],[{type:"BinaryExpression",operator:new r("<<",">>",">>>")},e()],[{type:"BinaryExpression",operator:new r(">","<",">=","<=","in","instanceof")},e()],[{type:"BinaryExpression",operator:new r("==","!=","===","!==")},e()],[{type:"BinaryExpression",operator:"&"},e()],[{type:"BinaryExpression",operator:"^"},e()],[{type:"BinaryExpression",operator:"|"},e()],[{type:"LogicalExpression",operator:"&&"},e()],[{type:"LogicalExpression",operator:"||"},e()],[{type:"CoalesceExpression",operator:"??"},e()],["ConditionalExpression",e()],["AssignmentExpression",e()],["SequenceExpression",e()]]})(),o=(t,e)=>{if("string"==typeof e)return t?.type===e;for(const[i,s]of Object.entries(e))if(s instanceof r){if(!s.has(t[i]))return!1}else if("object"==typeof s&&s){if(!o(t[i],s))return!1}else if(s!==t[i])return!1;return!0},a=(t,e)=>{const i=n.find(([e])=>o(t,e))?.[1],s=n.find(([t])=>o(e,t))?.[1];return i<s};class h extends s{blockNode(t){"BlockStatement"!==t.type?this.block({braces:!1})(()=>{this.traverse(t)}):this.traverse(t)}visit_Program(t){this.traverse(t.body)}visit_VariableDeclaration(t){this.fill(),this.write(t.kind),this.write(" ");for(const e of t.declarations)this.traverse(e)}visit_VariableDeclarator(t){this.traverse(t.id),t.init&&(this.write(" = "),this.traverse(t.init))}visit_Identifier(t){this.write(t.name)}visit_Literal(t){switch(typeof t.value){case"string":this.write(JSON.stringify(t.value));break;case"number":case"boolean":this.write(String(t.value));break;case"bigint":this.write(t.value+"n");break;case"object":t.value instanceof RegExp?this.write(String(t.value)):this.write("null")}}visit_CallExpression(t){this.traverse(t.callee),this.delimit("(",")")(()=>{this.items_view(this.traverse,t.arguments)})}visit_NewExpression(t){this.write("new "),this.traverse(t.callee),this.delimit("(",")")(()=>{this.items_view(this.traverse,t.arguments)})}visit_ArrayExpression(t){this.delimit("[","]")(()=>{this.items_view(this.traverse,t.elements)})}visit_ObjectExpression(t){this.delimit("{","}")(()=>{this.items_view(this.traverse,t.properties)})}visit_Property(t){this.traverse(t.key),this.write(": "),this.traverse(t.value)}visit_ArrowFunctionExpression(t){t.async&&this.write("async "),1===t.params.length&&"Identifier"===t.params[0].type?this.traverse(t.params[0]):this.delimit("(",")")(()=>{this.items_view(this.traverse,t.params)}),this.write(" => "),this.delimit_if("(",")","BlockStatement"!==t.body.type)(()=>{this.traverse(t.body)})}visit_BlockStatement(t){this.block()(()=>{this.traverse(t.body)})}visit_ExpressionStatement(t){this.fill(),this.traverse(t.expression)}visit_AssignmentExpression(t){this.visit_BinaryExpression(t)}visit_BinaryExpression(t){const{left:e,right:i}=t;this.delimit_if("(",")",a(t,e))(()=>{this.traverse(e)}),this.delimit(" "," ")(()=>{this.write(t.operator)}),this.delimit_if("(",")",a(t,i))(()=>{this.traverse(i)})}visit_LogicalExpression(t){this.visit_BinaryExpression(t)}visit_UnaryExpression(t){t.prefix&&(this.write(t.operator),/^\w+$/.test(t.operator)&&this.write(" ")),this.traverse(t.argument),t.prefix||this.write(t.operator)}visit_ConditionalExpression(t){this.delimit_if("(",")",a(t,t.test))(()=>{this.traverse(t.test)}),this.write(" ? "),this.traverse(t.consequent),this.write(" : "),this.delimit_if("(",")",a(t,t.alternate))(()=>{this.traverse(t.alternate)})}visit_MemberExpression(t){this.delimit_if("(",")",a(t,t.object))(()=>{this.traverse(t.object)}),t.optional&&this.write("?."),t.computed?this.delimit("[","]")(()=>{this.traverse(t.property)}):(this.write(t.optional?"?.":"."),this.traverse(t.property))}visit_IfStatement(t){this.fill(),this.write("if "),this.delimit("(",")")(()=>{this.traverse(t.test)}),this.write(" "),this.blockNode(t.consequent),t.alternate&&(this.write("else "),this.blockNode(t.alternate))}visit_SwitchStatement(t){this.fill(),this.write("switch "),this.delimit("(",")")(()=>{this.traverse(t.discriminant)}),this.block()(()=>{this.traverse(t.cases)})}visit_SwitchCase(t){this.fillBlock(),t.test?(this.write("case "),this.delimit("(",")")(()=>{this.traverse(t.test)})):this.write("default "),this.write(":"),this.blockNode(t.consequent)}visit_BreakStatement(t){this.fill(),this.write("break"),t.label&&(this.write(" "),this.traverse(t.label))}visit_ContinueStatement(t){this.fill(),this.write("continue"),t.label&&(this.write(" "),this.traverse(t.label))}visit_ForStatement(t){this.fill(),this.write("for "),this.delimit("(",")")(()=>{let e=this.buffered()(()=>{this.traverse(t.init),this.write(" ; "),this.traverse(t.test),this.write(" ; "),this.traverse(t.update)});this.write(...e)}),this.blockNode(t.body)}visit_UpdateExpression(t){t.prefix&&this.write(t.operator),this.delimit_if("(",")",a(t,t.argument))(()=>{this.traverse(t.argument)}),t.prefix||this.write(t.operator)}visit_ForInStatement(t){this.fill(),this.write("for "),this.delimit("(",")")(()=>{this.inline(()=>{this.traverse(t.left)}),this.write(" in "),this.traverse(t.right)}),this.blockNode(t.body)}visit_EmptyStatement(t){this.fill()}visit_ForOfStatement(t){this.fill(),this.write("for "),t.await&&this.write("await "),this.delimit("(",")")(()=>{this.inline(()=>{this.traverse(t.left)}),this.write(" of "),this.traverse(t.right)}),this.blockNode(t.body)}visit_WhileStatement(t){this.fill(),this.write("while "),this.delimit("(",")")(()=>{this.traverse(t.test)}),this.blockNode(t.body)}visit_DoWhileStatement(t){this.fill(),this.write("do "),this.blockNode(t.body),this.write(" while "),this.delimit("(",")")(()=>{this.traverse(t.test)})}visit_FunctionDeclaration(t){this.fill(),t.async&&this.write("async "),this.write("function"),t.generator&&this.write("*"),this.write(" "),t.id&&this.traverse(t.id),this.delimit("(",")")(()=>{this.items_view(this.traverse,t.params)}),this.write(" "),this.traverse(t.body)}visit_FunctionExpression(t){this.delimit("(",")")(()=>{t.async&&this.write("async "),this.write("function"),t.generator&&this.write("*"),this.write(" "),t.id&&this.traverse(t.id),this.delimit("(",")")(()=>{this.items_view(this.traverse,t.params)}),this.write(" "),this.traverse(t.body)})}visit_AssignmentPattern(t){this.traverse(t.left),this.write(" = "),this.traverse(t.right)}visit_ReturnStatement(t){this.fill("return"),t.argument&&(this.write(" "),this.traverse(t.argument)),this.write(";")}visit_RestElement(t){this.write("..."),this.traverse(t.argument)}visit_SpreadElement(t){this.write("..."),this.traverse(t.argument)}visit_ObjectPattern(t){this.delimit("{","}")(()=>{this.items_view(this.traverse,t.properties)})}visit_ArrayPattern(t){this.delimit("[","]")(()=>{this.items_view(this.traverse,t.elements)})}visit_YieldExpression(t){this.write("yield"),t.delegate&&this.write("*"),t.argument&&(this.write(" "),this.traverse(t.argument))}visit_ThisExpression(t){this.write("this")}visit_ClassDeclaration(t){this.fill("class "),t.id&&this.traverse(t.id),t.superClass&&(this.write(" extends "),this.traverse(t.superClass)),this.block()(()=>{this.traverse(t.body)})}visit_ClassBody(t){this.traverse(t.body)}visit_MethodDefinition(t){switch(this.fill(),t.kind){case"get":this.write("get ");break;case"set":this.write("set ")}const e=t.key;(t=>{t.async&&this.write("async "),t.generator&&this.write("*"),this.traverse(e),this.write(" "),this.delimit("(",")")(()=>{this.items_view(this.traverse,t.params)}),this.write(" "),this.traverse(t.body)})(t.value)}visit_TemplateLiteral(t){const e=[];for(let i=0;i<2*t.quasis.length-1;i++)i%2?e.push(t.expressions[i]):e.push(t.quasis[i]);for(;void 0===e.pop()&&e.length;);this.delimit("`","`")(()=>{for(const t of e)this.delimit_if("${","}","TemplateElement"!==t.type)(()=>{this.traverse(t)})})}visit_TemplateElement(t){this.write(t.value.raw)}visit_Super(t){this.write("super")}visit_ExportNamedDeclaration(t){this.fill("export "),this.delimit_if("{","}",t.specifiers.length)(()=>{this.items_view(this.traverse,t.specifiers)}),t.declaration&&this.inline(()=>{this.traverse(t.declaration)}),t.attributes.length&&(this.write(" with "),this.delimit("{","}")(()=>{this.items_view(this.traverse,t.attributes)}))}visit_ExportSpecifier(t){this.traverse(t.local),t.exported!==t.local&&(this.write(" as "),this.traverse(t.exported))}visit_ExportDefaultDeclaration(t){this.fill("export default "),this.inline(()=>{this.traverse(t.declaration)})}visit_ImportDeclaration(t){this.fill("import ");const e=t.specifiers.filter(t=>!["ImportDefaultSpecifier","ImportNamespaceSpecifier"].includes(t.type));e.length&&this.delimit("{","}")(()=>{this.items_view(this.traverse,e)});const i=t.specifiers.find(t=>"ImportDefaultSpecifier"===t.type);i&&(e.length&&this.write(","),this.traverse(i));const s=t.specifiers.find(t=>"ImportNamespaceSpecifier"===t.type);s&&(e.length&&this.write(","),this.traverse(s)),this.write(" from "),this.traverse(t.source),t.attributes.length&&(this.write(" with "),this.delimit("{","}")(()=>{this.items_view(this.traverse,t.attributes)}))}visit_ImportSpecifier(t){this.traverse(t.local),t.imported!==t.local&&(this.write(" as "),this.traverse(t.imported))}visit_ImportNamespaceSpecifier(t){this.write("* as "),this.traverse(t.local)}visit_ImportDefaultSpecifier(t){this.traverse(t.local)}visit_TryStatement(t){this.fill("try "),this.blockNode(t.block),t.handler&&this.traverse(t.handler),t.finalizer&&(this.write(" finally "),this.blockNode(t.finalizer))}visit_CatchClause(t){this.fill("catch "),this.delimit("(",")")(()=>{this.traverse(t.param)}),this.write(" "),this.blockNode(t.body)}visit_ThrowStatement(t){this.fill("throw "),this.traverse(t.argument)}visit_ChainExpression(t){this.traverse(t.expression)}visit_AwaitExpression(t){this.write("await "),this.traverse(t.argument)}visit_SequenceExpression(t){this.items_view(this.traverse,t.expressions)}}i.exports={Unparser:h,unparse:function(t,e){return new h(e).visit(t)}}}},"./index.js");export default module;export const{NodeVisitor:NodeVisitor,NodeTransformer:NodeTransformer,BaseCodeGenerator:BaseCodeGenerator,Unparser:Unparser,unparse:unparse}=module;
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "estree-toolbox",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A comprehensive toolbox for working with ESTree ASTs: visitors, transformers, and code generators, unparesrs",
|
|
5
|
+
"main": "./dist/estree-toolbox.js",
|
|
6
|
+
"module": "./dist/estree-toolbox.mjs",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"import": "./dist/estree-toolbox.mjs",
|
|
10
|
+
"require": "./dist/estree-toolbox.js",
|
|
11
|
+
"default": "./dist/estree-toolbox.js"
|
|
12
|
+
},
|
|
13
|
+
"./package.json": "./package.json"
|
|
14
|
+
},
|
|
15
|
+
"unpkg": "./dist/estree-toolbox.js",
|
|
16
|
+
"jsdelivr": "./dist/estree-toolbox.js",
|
|
17
|
+
"files": [
|
|
18
|
+
"dist"
|
|
19
|
+
],
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "node build.js",
|
|
22
|
+
"build:watch": "nodemon --watch lib --ext js --exec npm run build",
|
|
23
|
+
"prepublishOnly": "npm run build",
|
|
24
|
+
"prepack": "npm run build",
|
|
25
|
+
"test": "node -e \"const { NodeVisitor } = require('./dist/estree-toolbox.js'); console.log('✅ 压缩版本测试:', typeof NodeVisitor)\""
|
|
26
|
+
},
|
|
27
|
+
"keywords": [
|
|
28
|
+
"estree",
|
|
29
|
+
"ast",
|
|
30
|
+
"visitor",
|
|
31
|
+
"transformer",
|
|
32
|
+
"codegen",
|
|
33
|
+
"unparser",
|
|
34
|
+
"unparse",
|
|
35
|
+
"javascript",
|
|
36
|
+
"code-generator"
|
|
37
|
+
],
|
|
38
|
+
"author": "stevena_star",
|
|
39
|
+
"license": "MIT"
|
|
40
|
+
}
|