包详细信息

@sveltejs/acorn-typescript

sveltejs1.6mMIT1.0.5

Acorn plugin that parses TypeScript

自述文件

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.

更新日志

Changelog

1.0.5

Patch Changes

  • fix: allow type casts during reassignments (57b6335)

1.0.4

Patch Changes

  • fix: ensure scope is entered correctly (1697562)

1.0.3

Patch Changes

  • fix: allow override modifier in class that extends another class (cd3843e)

  • fix: avoid validating arrow function parameters too early (f184d66)

  • fix: handle class/function identifier names inside template literal (8819aa1)

  • fix: typeParameters->typeArguments in some places to align with TSESTree spec (bc39dcb)

  • fix: correct end position, remove obsolete index (f35b64e)

1.0.2

Patch Changes

  • fix: support const modifier in generic type parameters (0c11e2b)

  • fix: allow declaration merging (35e1a3b)

  • fix: support type annotation on rest parameters in arrow function (847d787)

  • fix: support non-null expressions in more places (6267f0c)

1.0.1

Patch Changes

  • fix: parse type assertions (#1)

1.0.0 / acorn-typescript

This project started as a fork of https://github.com/TyrealHu/acorn-typescript

Version 1.0 of @sveltejs/acorn-typescript has some breaking changes compared to the original acorn-typescript project:

  • Only named export (i.e. you have to do import { tsPlugin } from '@sveltejs/acorn-typescript';, a default export is no longer provided)
  • ESM only (no CJS build)
  • JSX parsing is disabled by default now (you can turn it back on by passing { jsx: true })
  • allowSatisfies option was removed, satisfies operator is always parsed now
  • index on loc was removed
  • typeParameters is now typeArguments in some places (like TSTypeReference) to align with the TSESTree spec

Changelog of the project this originated from: https://github.com/TyrealHu/acorn-typescript/CHANGELOG.md