babel-parse-wild-code

raw JSON →
2.1.5 verified Sat Apr 25 auth: no javascript

Parse user code files using their project's installed Babel version and config, with automatic fallback. Handles JS, TS, TSX, and DTS files with correct parser plugins based on file extension. Supports Babel 7 only. Current stable version 2.1.5 (June 2023). Key differentiator: resolves ambient Babel config per file directory, works around Babel 7 performance bug where parseAsync redoes loadOptionsAsync work. Falls back to @babel/parser defaults if @babel/core or config is missing. Ships ESM and CJS exports with TypeScript definitions. Maintained by codemodsquad.

error Error: Cannot find module '@babel/core'
cause @babel/core is not installed or not in node_modules path relative to the file being parsed.
fix
Install @babel/core as a dependency in your project: npm install @babel/core --save
error TypeError: (0 , babel-parse-wild-code.parseSync) is not a function
cause Importing the default export instead of named export, or using wrong module system.
fix
Use named import: import { parseSync } from 'babel-parse-wild-code'
error SyntaxError: Unexpected token (1:0)
cause Parsing a file with a syntax that requires a plugin that is not included in the default parser options for that file extension (e.g., JSX in .js file without jsx plugin).
fix
Pass the missing plugin in options: parseSync('file.jsx', { plugins: ['jsx'] })
error Error: @babel/parser: Option 'allowUndeclaredExports' is not registered
cause Using an older version of @babel/parser that does not support certain options that babel-parse-wild-code sets for .d.ts files.
fix
Upgrade @babel/parser to version 7.13.0 or later: npm install @babel/parser@latest
breaking Version 2.0.0 removed default parser plugins for experimental proposals (decorators, doExpressions, exportDefaultFrom, functionBind, pipelineOperator, throwExpressions).
fix Explicitly add these plugins via options.plugins array, or pin to v1.x if you rely on them.
deprecated Babel 6 is not supported; only Babel 7 is supported.
fix Upgrade your project to Babel 7, or use an alternative parser.
gotcha If @babel/core or @babel/parser cannot be resolved from the file's directory, the library falls back to bundled default parser options without any project-specific config.
fix Ensure @babel/core and @babel/parser are installed in the project (not globally) so they can be resolved via require().
gotcha The cache is per-directory and not automatically invalidated on Babel config changes. Clear the cache manually with clearCache() before bulk parsing to avoid stale options.
fix Call clearCache() before each batch of parses, or between batches when config may have changed.
gotcha Options passed to parseSync/parseAsync are merged with resolved options for the file, but plugins are concatenated — duplicates are not removed.
fix If you pass plugins that conflict with resolved plugins, wrap in a function that filters or deduplicates.
gotcha When using getParserSync/getParserAsync with jscodeshift, you must pass `{ tokens: true }` in options to include tokens in the AST.
fix Add `tokens: true` to the options object when calling getParserSync/getParserAsync for use with jscodeshift.
breaking In v2.0.0, the default parser options for .d.ts files changed: disallowAmbiguousJSXLike is now set to true for .d.ts files to conform to TypeScript's parser behavior.
fix If you need the old behavior, set `disallowAmbiguousJSXLike: false` in options.
npm install babel-parse-wild-code
yarn add babel-parse-wild-code
pnpm add babel-parse-wild-code

Demonstrates sync and async parsing with clearCache, custom parser options, and reusable Parser instance for TypeScript files.

import { parseSync, clearCache } from 'babel-parse-wild-code'

// Clear cache before bulk operations
clearCache()

// Parse a TypeScript file synchronously
const ast = parseSync('path/to/file.ts')
console.log(ast.program.body)

// Parse with custom parser options (e.g., tokens for jscodeshift)
const astWithTokens = parseSync('path/to/file.tsx', { tokens: true, plugins: ['jsx'] })

// Async parsing
import { parseAsync } from 'babel-parse-wild-code'
const ast2 = await parseAsync('path/to/file.js')

// Get a reusable Parser instance
import { getParserSync, Parser } from 'babel-parse-wild-code'
const parser: Parser = getParserSync('path/to/file.ts')
const parsed = parser.parse('const x: number = 1')