Registry / serialization / acorn-typescript

acorn-typescript

JSON →
library1.4.13jsnpmunverified

acorn-typescript is an experimental alternative plugin for Acorn, a small, fast JavaScript parser. It extends Acorn to parse TypeScript scripts into a TypeScript AST, aiming for faster performance compared to other TypeScript parsers. The current stable version is 1.4.13, and it exhibits an active release cadence, frequently addressing bug fixes related to various TypeScript syntax features like optional class properties, generic types, arrow functions, and decorators. Key differentiators include its integration as an Acorn plugin, allowing existing Acorn users to add TypeScript parsing capabilities, and its specific focus on optimizing parsing speed. It supports standard TypeScript syntax, decorators, and JSX/TSX. It requires `acorn` version `>=8.9.0` as a peer dependency.

npm install acorn-typescript
INSTALL
IMPORT
SIG · ACORN-TYPESCRIPT
A
acorn-typescript
serializationjavascriptv1.4.13
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

acorn
import * as acorn from 'acorn'
const acorn = require('acorn')
Primarily used in ESM contexts. While Acorn itself supports CJS, this plugin's usage examples are ESM.
tsPlugin
import tsPlugin from 'acorn-typescript'
import { tsPlugin } from 'acorn-typescript'
This is a default export, not a named export.
Parser.extend
acorn.Parser.extend(tsPlugin())
acorn.extend(tsPlugin())
The `extend` method is a static method on the `Parser` class, not directly on the `acorn` module.

Demonstrates how to extend Acorn's parser with the `acorn-typescript` plugin to parse TypeScript code, including basic syntax and ambient contexts using the `dts` option, ensuring `locations: true` is set.

import * as acorn from 'acorn'; import tsPlugin from 'acorn-typescript'; // Example 1: Basic TypeScript parsing const code1 = ` const a: number = 1; type MyType = string; export { a, type MyType as RenamedType }; class MyClass { accessor b = 'hello'; } `; const ast1 = acorn.Parser.extend(tsPlugin()).parse(code1, { sourceType: 'module', ecmaVersion: 'latest', locations: true // Required for acorn-typescript }); console.log('Parsed AST 1 (basic):', JSON.stringify(ast1.body[0], null, 2)); // Example 2: Parsing within a TypeScript ambient context (.d.ts files) const code2 = ` declare module 'my-module' { interface MyInterface { name: string; id: number; } } `; const ast2 = acorn.Parser.extend(tsPlugin({ dts: true })).parse(code2, { sourceType: 'module', ecmaVersion: 'latest', locations: true // Still required }); console.log('Parsed AST 2 (dts context):', JSON.stringify(ast2.body[0], null, 2));
Debug
Known issues
gotchaThe `locations: true` option is mandatory when using `acorn-typescript`. Failing to enable it will result in parsing errors or unexpected AST structures.
fix
Always include `{ locations: true }` in the options object passed to `acorn.parse()` or `acorn.Parser.extend(...).parse()`.
affects: >=1.0.0
breakingStarting from `v1.3.7`, `acorn-typescript` added explicit validation for `locations` when using `static parse` or `static parseExpression`. While the requirement existed before, this version likely makes the omission an explicit error or warning during parsing.
fix
Ensure `locations: true` is always provided in the parser options. Refer to the `quickstart` example for correct usage.
affects: >=1.3.7
gotchaThis package is a plugin for Acorn. It requires `acorn` as a peer dependency, specifically `acorn@>=8.9.0`. Ensure a compatible version of `acorn` is installed in your project to avoid runtime errors.
fix
Install a compatible version of `acorn` alongside `acorn-typescript`: `npm install acorn@^8.9.0` or `yarn add acorn@^8.9.0`.
affects: >=1.0.0
gotchaWhen parsing TypeScript declaration files (`.d.ts`) or code within `declare module` blocks, you must enable the `dts: true` option in the plugin configuration to handle ambient context-specific syntax correctly.
fix
Pass `{ dts: true }` to the plugin function: `acorn.Parser.extend(tsPlugin({ dts: true }))`.
affects: >=1.0.0
Errors
Common errors & fixes
Error: The 'locations' option must be enabled for acorn-typescript.
`locations: true` was not included in the parser options.
fix
Add `{ locations: true }` to the options object passed to the `parse` method.
TypeError: Cannot read properties of undefined (reading 'extend')
The `acorn` package might not be installed, or an incompatible version is present, preventing the `Parser.extend` method from being accessed correctly.
fix
Ensure `acorn@^8.9.0` is installed: `npm install acorn@^8.9.0`.
SyntaxError: Unexpected token (some_typescript_keyword)
Attempting to parse TypeScript specific syntax (e.g., `type`, `interface`, decorators) without correctly extending Acorn with `acorn-typescript` or with incorrect parser options.
fix
Verify that `acorn.Parser.extend(tsPlugin())` is correctly applied to your parser instance and that `ecmaVersion: 'latest'` and `sourceType: 'module'` are set if applicable.
TypeError: acorn_typescript__WEBPACK_IMPORTED_MODULE_1___default is not a function
Attempted to import the `acorn-typescript` plugin incorrectly, typically using `import { tsPlugin } from 'acorn-typescript'` instead of a default import.
fix
Use a default import: `import tsPlugin from 'acorn-typescript'`.
Upgrade
Version history
1.4.13latest on npm
Audit
Dependencies
acornrequiredCore parser library that this package extends as a plugin.
Agent activity
51 hits · last 30 days
node
44
OpenAI (training)
1
Resources
acorn-typescript — npm install acorn-typescript · libregistry