https://github.com/tyrealhu/acorn-typescript
Alternative, TypeScript parser
https://github.com/tyrealhu/acorn-typescript
acorn ast babel browser javascript nodejs parse parser typescript
Last synced: 12 months ago
JSON representation
Alternative, TypeScript parser
- Host: GitHub
- URL: https://github.com/tyrealhu/acorn-typescript
- Owner: TyrealHu
- License: mit
- Created: 2022-10-23T06:52:45.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2024-06-28T19:30:25.000Z (almost 2 years ago)
- Last Synced: 2025-06-09T10:09:10.221Z (about 1 year ago)
- Topics: acorn, ast, babel, browser, javascript, nodejs, parse, parser, typescript
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/acorn-typescript?activeTab=readme
- Size: 1.18 MB
- Stars: 146
- Watchers: 7
- Forks: 15
- Open Issues: 17
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# acorn-typescript
[](https://www.npmjs.com/package/acorn-typescript)[](https://codecov.io/gh/TyrealHu/acorn-typescript)
---
This is plugin for [Acorn](http://marijnhaverbeke.nl/acorn/) - a tiny, fast JavaScript parser, written completely in JavaScript.
It was created as an experimental alternative, faster [TypeScript](https://www.typescriptlang.org/) parser. It will help you to parse
typescript script into typeScript AST.
## Usage
Requiring this module provides you with an Acorn plugin that you can use like this:
```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](https://www.typescriptlang.org/docs/handbook/declaration-files/introduction.html)).
```typescript
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
})
```
## Notice
- You have to enable options.locations while using acorn-typescript
```ts
acorn.parse(input, {
sourceType: 'module',
ecmaVersion: 'latest',
// here
locations: true
})
```
## SUPPORTED
- Typescript normal syntax
- Support to parse TypeScript [Decorators](https://www.typescriptlang.org/docs/handbook/decorators.html)
- Support to parse JSX & TSX
## CHANGELOG
[click](./CHANGELOG.md)
## RoadMap
- support import-assertions
## License
[MIT](https://couto.mit-license.org/)