{"id":16752,"library":"acorn-loose","title":"Acorn Loose Parser","description":"acorn-loose is an error-tolerant ECMAScript parser, currently at version 8.5.2, designed to produce an Abstract Syntax Tree (AST) conforming to the ESTree specification even when faced with syntactically invalid JavaScript code. It's part of the broader Acorn project, sharing the same tokenizer as the strict acorn parser, and updates are typically released in tandem with its parent project, following an active but unstated release cadence. Its primary differentiation from acorn is its robust recovery mechanism, which attempts to make sense of malformed input, sometimes by treating whitespace as significant or by inserting placeholder \"dummy\" nodes, identifiable by the name \"✖\", where it cannot resolve the syntax. This makes it invaluable for tasks like linting, code analysis, or language services where full parsing is required despite minor errors. However, it's generally recommended to first attempt parsing with the strict acorn parser and only fall back to acorn-loose if syntax errors are encountered, as its error-recovery can occasionally mis-parse valid but unusually indented code, leading to potentially unexpected AST structures.","status":"active","version":"8.5.2","language":"javascript","source_language":"en","source_url":"https://github.com/acornjs/acorn","tags":["javascript","typescript"],"install":[{"cmd":"npm install acorn-loose","lang":"bash","label":"npm"},{"cmd":"yarn add acorn-loose","lang":"bash","label":"yarn"},{"cmd":"pnpm add acorn-loose","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Used for its tokenizer and is the recommended strict parser to try first before falling back to acorn-loose.","package":"acorn","optional":false}],"imports":[{"note":"For ESM environments, prefer named import. CommonJS users can access `parse` as a method on the default export (`acornLoose.parse`).","wrong":"const acornLoose = require('acorn-loose'); const ast = acornLoose.parse(...)","symbol":"parse","correct":"import { parse } from 'acorn-loose'"},{"note":"Use the named export `LooseParser` to extend its functionality with plugins via `LooseParser.extend()`. Available in both ESM and CJS.","wrong":"const { Parser } = require('acorn-loose')","symbol":"LooseParser","correct":"import { LooseParser } from 'acorn-loose'"},{"note":"A utility function to check if an AST node was inserted as a placeholder by the error-tolerant parser. Available as a named export in ESM and CJS.","wrong":"acornLoose.isDummy(node)","symbol":"isDummy","correct":"import { isDummy } from 'acorn-loose'"}],"quickstart":{"code":"import { parse, isDummy } from 'acorn-loose';\nimport { parse as strictParse } from 'acorn'; // Recommended peer dependency for strict parsing\nimport { generate } from 'astring'; // A common AST utility for code generation\n\nconst invalidCode = `\n  function example(arg1, arg2,) { // Trailing comma in params is okay in ES2017+\n    if (true { // Missing parenthesis and closing brace\n      return \"hello\";\n  `;\n\nlet ast;\ntry {\n  // Always attempt strict parsing first for valid code\n  strictParse(invalidCode, { ecmaVersion: 2020, sourceType: 'module' });\n} catch (e: any) {\n  console.log('Strict parser failed as expected:', e.message.split('\\n')[0]);\n  // Fallback to acorn-loose for error-tolerant parsing of malformed code\n  ast = parse(invalidCode, { ecmaVersion: 2020, sourceType: 'module' });\n  console.log('AST generated by acorn-loose (first few nodes):', JSON.stringify(ast.body.slice(0, 2), null, 2));\n\n  let dummyCount = 0;\n  // Simple traversal to find dummy nodes (a dedicated AST walker like acorn.walk is recommended for full traversal)\n  function traverse(node: any) {\n    if (node && typeof node === 'object') {\n      if (isDummy(node)) {\n        dummyCount++;\n      }\n      for (const key in node) {\n        // Avoid circular references and properties that may not contain nodes (e.g., 'parent')\n        if (Object.prototype.hasOwnProperty.call(node, key) && key !== 'parent' && typeof node[key] === 'object') {\n          traverse(node[key]);\n        }\n      }\n    }\n  }\n  traverse(ast);\n  console.log(`Found ${dummyCount} dummy nodes in the AST.`);\n  console.log('Attempting to regenerate code (may be malformed due to error recovery):\\n', generate(ast));\n}\n","lang":"typescript","description":"Demonstrates parsing invalid JavaScript code with `acorn-loose` after a strict parse failure, identifying dummy nodes, and regenerating potentially malformed code using `astring`."},"warnings":[{"fix":"Always attempt to parse with the strict `acorn` parser first for known valid code, and only fall back to `acorn-loose` for error recovery when a `SyntaxError` is encountered.","message":"acorn-loose may mis-parse valid but weirdly indented files due to its error-recovery mechanisms treating whitespace as significant in some contexts. This can lead to an incorrect AST for otherwise valid code.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Utilize the `isDummy(node)` utility function to identify and gracefully handle or skip these placeholder nodes during AST traversal or processing to prevent unexpected errors.","message":"The parser inserts 'dummy' identifier nodes with the name \"✖\" as placeholders for syntactically incomprehensible parts of the input. Downstream tools must be designed to explicitly handle or skip these placeholder nodes.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Ensure that your installed versions of `acorn` and `acorn-loose` are compatible. Ideally, install both as peer dependencies or use versions released in close proximity to avoid conflicts.","message":"acorn-loose relies on the `acorn` package for its tokenizer. Mismatched major versions between `acorn` and `acorn-loose` could lead to unexpected parsing behavior or runtime errors.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Before accessing properties, validate nodes using `isDummy(node)` or implement more robust checks for node existence and expected structure. Be prepared for malformed subtrees resulting from error recovery.","cause":"A downstream AST processing tool or custom traversal logic expects a fully valid ESTree structure but encounters dummy nodes or partially formed nodes generated by `acorn-loose`'s error recovery, which lack expected properties.","error":"TypeError: Cannot read properties of undefined (reading 'type') (or similar property access error)"},{"fix":"Implement a fallback mechanism: first try parsing with `acorn.parse()`, and if it throws a `SyntaxError`, then re-attempt parsing the same code with `acorn-loose.parse()`.","cause":"Attempting to parse malformed JavaScript code directly with the strict `acorn` parser (or another strict parser) instead of leveraging the error-tolerant `acorn-loose`.","error":"SyntaxError: Unexpected token (X:Y)"},{"fix":"Ensure `acorn-loose` and `acorn` are installed (`npm install acorn-loose acorn`). Verify your `tsconfig.json` (for TypeScript) or `package.json` `type` field and adjust import statements accordingly (ESM `import` or CommonJS `require`).","cause":"The package (or its dependency) is not installed, or there's a mismatch in import syntax (e.g., using CommonJS `require` in an ESM context or vice-versa without proper tooling/configuration).","error":"ModuleNotFoundError: Cannot find module 'acorn-loose' or 'acorn'"}],"ecosystem":"npm","meta_description":null}