buntis
Version:
A 100% compliant, self-hosted typescript parser that emits an ESTree-compatible abstract syntax tree
176 lines (144 loc) • 6.81 kB
Markdown
<h1 align="center">Buntis</h1>
<p align="center"> A 100% compliant, self-hosted Typescript parser that emits an ESTree-compatible abstract syntax tree</p>
<p align="center">
<a href="https://www.npmjs.com/package/buntis"><img src="https://img.shields.io/npm/v/buntis.svg?style=flat-square" alt="Azure Pipelines"/></a>
<a href="https://lgtm.com/projects/g/buntis/buntis/context:javascript"><img src="https://img.shields.io/lgtm/grade/javascript/g/buntis/buntis.svg?logo=lgtm&logoWidth=18" alt="GitHub license" /></a>
<a href="https://lgtm.com/projects/g/buntis/buntis/alerts"><img src="https://img.shields.io/lgtm/alerts/g/buntis/buntis.svg?logo=lgtm&logoWidth=18" alt="Total alerts" /></a>
<a href="https://circleci.com/gh/buntis/buntis"><img src="https://circleci.com/gh/buntis/buntis.svg?style=svg" alt="Circle" /></a>
<a href="https://github.com/buntis/buntis/blob/master/LICENSE.md"><img src="https://img.shields.io/github/license/buntis/buntis.svg" alt="Circle" /></a>
</p>
<br>
## Features
* Conforms to the standard ECMAScript® 2020 (ECMA-262 10th Edition) language specification
* Conforms to the latest Typescript language specification
* Support TC39 proposals via option (*wip*)
* Support for additional ECMAScript features for Web Browsers
* JSX / TSX support via option
* Optionally track syntactic node locations (*wip*)
* Emits an ESTree-compatible abstract syntax tree
* No backtracking
* Low memory usage
* Very well tested (~16 000 unit tests with full code coverage)
* Lightweight - ~85 KB minified
## Installation
```sh
npm install buntis --save-dev
```
## API
Buntis generates `AST` according to [ESTree AST format](https://github.com/estree/estree), and can be used to perform [syntactic analysis](https://en.wikipedia.org/wiki/Parsing) (parsing) of a `Javascript` or `Typescript` program, and with `ES2015` and later a JavaScript program can be either [a script or a module](https://tc39.github.io/ecma262/index.html#sec-ecmascript-language-scripts-and-modules). The same methods are used when parsing in `Typescript` mode - `parseTSModule` and `parseTSScript`.
The `parse` methods exposed by Buntis takes an optional `options` object which allows you to specify whether to parse in [`script`](https://tc39.github.io/ecma262/#sec-parse-script) mode (*the default*) or in [`module`](https://tc39.github.io/ecma262/#sec-parsemodule) mode or `Typescript` mode.
This is the available options:
```ts
{
// Enabled directives
directives: false;
// Disable web compability
disableWebCompat: false;
// Enable React JSX parsing when set to `true`
jsx: false
// The flag to enable stage 3 support (ESNext)
next: false;
// The flag to enable line/column location information to each node
loc: false;
// The flag to attach raw property to each literal and identifier node
raw: false;
// The flag to allow return in the global scope
globalReturn: false;
// The flag to enable implied strict mode
impliedStrict: false;
// Enable comment extraction. Accepts **only** a function
onComment: function() {}
// Enable Typescript parsing
ts: false
// Adds a source attribute in every node’s loc object when the locations option is `true`
source: false;
}
```
Example usage:
```ts
import { parseTSScript } from './buntis';
parseTSScript('const f = function<T>(x?: T): T {}');
```
This will return when serialized in json:
```ts
{
"type": "Program",
"sourceType": "script",
"body": [
{
"type": "VariableDeclaration",
"kind": "const",
"declarations": [
{
"type": "VariableDeclarator",
"init": {
"type": "FunctionExpression",
"params": [
{
"type": "Identifier",
"name": "x",
"optional": true,
"typeAnnotation": {
"type": "TypeAnnotation",
"typeAnnotation": {
"type": "TypeReference",
"typeName": {
"type": "Identifier",
"name": "T"
},
"typeParameters": []
}
}
}
],
"body": {
"type": "BlockStatement",
"body": []
},
"async": false,
"generator": false,
"id": null,
"typeParameters": {
"type": "TypeParameterDeclaration",
"params": [
{
"type": "TypeParameter",
"name": {
"type": "Identifier",
"name": "T",
"optional": 0,
"typeAnnotation": []
},
"constraint": null,
"default": null
}
]
},
"returnType": {
"type": "TypeAnnotation",
"typeAnnotation": {
"type": "TypeReference",
"typeName": {
"type": "Identifier",
"name": "T",
"optional": false,
"typeAnnotation": []
},
"typeParameters": []
}
},
"declare": false
},
"id": {
"type": "Identifier",
"name": "f",
"optional": false,
"typeAnnotation": []
},
"definite": false
}
]
}
]
}
```