{"id":12150,"library":"to-ast","title":"JavaScript Object to AST Converter","description":"The `to-ast` package, currently at version 1.0.0, provides a utility for converting standard JavaScript objects and primitive values into a Mozilla Parser API-compatible Abstract Syntax Tree (AST) representation. This library is specifically designed to facilitate the generation of JavaScript source code from data structures, often used in conjunction with tools like `escodegen`. It supports a wide range of built-in types including primitives (numbers, strings, booleans, undefined, null), functions, arrays, buffers, dates, errors, regular expressions, and object literals. A key differentiator is its ability to handle custom types by respecting a `toAST` method on the object's prototype, allowing for custom AST representations. Given its singular purpose and stable version, its release cadence is likely low, operating more as a specialized, maintenance-mode utility.","status":"maintenance","version":"1.0.0","language":"javascript","source_language":"en","source_url":"git://github.com/devongovett/to-ast","tags":["javascript","ast","object","syntax","escodegen","esprima"],"install":[{"cmd":"npm install to-ast","lang":"bash","label":"npm"},{"cmd":"yarn add to-ast","lang":"bash","label":"yarn"},{"cmd":"pnpm add to-ast","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The package is CommonJS-only in v1.0.0. Use 'require' for correct module import.","wrong":"import toAST from 'to-ast';","symbol":"toAST","correct":"const toAST = require('to-ast');"}],"quickstart":{"code":"const toAST = require('to-ast');\nconst escodegen = require('escodegen'); // Often used in conjunction with to-ast\n\n// Convert a number literal to its AST representation\nconst numberAst = toAST(2);\nconsole.log('AST for 2:', JSON.stringify(numberAst, null, 2));\n// Expected output: {\"type\":\"Literal\",\"value\":2}\n\n// Generate source code from the AST\nconst numberSource = escodegen.generate(numberAst);\nconsole.log('Source for 2:', numberSource);\n// Expected output: 2\n\n// Demonstrate custom type conversion using a 'toAST' method\nfunction Person(name) {\n  this.name = name;\n}\n\nPerson.prototype.toAST = function() {\n  return {\n    type: 'NewExpression',\n    callee: { type: 'Identifier', name: 'Person' },\n    arguments: [{ type: 'Literal', value: this.name }]\n  };\n};\n\nconst personInstance = new Person('Devon');\nconst personAst = toAST(personInstance);\nconsole.log('AST for Person:', JSON.stringify(personAst, null, 2));\n/* Expected output:\n{\n  \"type\": \"NewExpression\",\n  \"callee\": {\n    \"type\": \"Identifier\",\n    \"name\": \"Person\"\n  },\n  \"arguments\": [\n    {\n      \"type\": \"Literal\",\n      \"value\": \"Devon\"\n    }\n  ]\n}\n*/\n\nconst personSource = escodegen.generate(personAst);\nconsole.log('Source for Person:', personSource);\n// Expected output: new Person('Devon')","lang":"javascript","description":"Demonstrates converting primitive and custom JavaScript objects into ASTs, and then generating source code using escodegen."},"warnings":[{"fix":"Use 'const toAST = require('to-ast');' for module import.","message":"This package is a CommonJS module and does not natively support ESM 'import' syntax in v1.0.0. Attempting to 'import' it will result in a module resolution error.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Verify compatibility with target AST tools or consider using an AST adapter if necessary.","message":"The AST generated by 'to-ast' strictly adheres to the Mozilla Parser API. While compatible with 'escodegen', other AST transformation tools might require normalization or a different AST format.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Change your import statement to `const toAST = require('to-ast');`.","cause":"Attempting to import 'to-ast' using ESM 'import' syntax, which is not supported by this CommonJS-only package.","error":"TypeError: toAST is not a function"},{"fix":"Install and use a separate code generation library like `escodegen` to convert the AST output from `to-ast` into executable JavaScript code. Example: `const escodegen = require('escodegen'); escodegen.generate(yourAst);`","cause":"Mistaking `to-ast` for a code generator. `to-ast` only produces Abstract Syntax Trees (ASTs), it does not generate source code directly.","error":"escodegen.generate is not a function"}],"ecosystem":"npm"}