@sveltejs/acorn-typescript
TypeScript icon, indicating that this package has built-in type declarations

1.0.5 • Public • Published

acorn-typescript

License Chat

This is a plugin for Acorn - a tiny, fast JavaScript parser, written completely in JavaScript.

It was created as an experimental alternative, faster TypeScript parser. It will help you to parse TypeScript using Acorn.

Usage

To get started, import the plugin and use Acorn's extension mechanism to register it. You have to enable options.locations while using acorn-typescript.

import * as acorn from 'acorn';
import tsPlugin from 'acorn-typescript';

const node = acorn.Parser.extend(tsPlugin()).parse(
	`
const a = 1
type A = number
export {
  a,
  type A as B
}
`,
	{
		sourceType: 'module',
		ecmaVersion: 'latest',
		locations: true
	}
);

If you want to enable parsing within a TypeScript ambient context, where certain syntax have different rules (like .d.ts files and inside declare module blocks):

import * as acorn from 'acorn';
import tsPlugin from 'acorn-typescript';

const node = acorn.Parser.extend(tsPlugin({ dts: true })).parse(
	`
const a = 1
type A = number
export {
  a,
  type A as B
}
`,
	{
		sourceType: 'module',
		ecmaVersion: 'latest',
		locations: true
	}
);

SUPPORTED

  • Typescript normal syntax
  • Support to parse TypeScript Decorators
  • Support to parse JSX & TSX

CHANGELOG

click

Acknowledgments

We want to thank TyrealHu for his original work on this project. He maintained acorn-typescript until early 2024.

Readme

Keywords

none

Package Sidebar

Install

npm i @sveltejs/acorn-typescript

Weekly Downloads

382,076

Version

1.0.5

License

MIT

Unpacked Size

197 kB

Total Files

5

Last publish

Collaborators

  • rich_harris
  • svelte-admin
  • conduitry
  • dominik_g