Skip to content

Commit

Permalink
Improve typings: ESM, AcornJsxParser class and tokTypes const (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
brettz9 authored Nov 14, 2022
1 parent de5b1e9 commit 8ed96d6
Showing 1 changed file with 55 additions and 2 deletions.
57 changes: 55 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,65 @@
import { Parser } from 'acorn'
import * as acorn from 'acorn';

declare const jsx: (options?: jsx.Options) => (BaseParser: typeof Parser) => typeof Parser;
interface JsxTokTypes extends AcornTokTypes {
jsxName: acorn.TokenType,
jsxText: acorn.TokenType,
jsxTagEnd: acorn.TokenType,
jsxTagStart: acorn.TokenType
}

declare const jsx: {
tokTypes: JsxTokTypes;
(options?: jsx.Options): (BaseParser: typeof acorn.Parser) => jsx.AcornJsxParserCtor
}

type AcornTokTypes = typeof acorn.tokTypes;

declare namespace jsx {

type TokTypes = JsxTokTypes

interface Options {
allowNamespacedObjects?: boolean;
allowNamespaces?: boolean;
}

interface TokContexts {
tc_oTag: acorn.TokContext,
tc_cTag: acorn.TokContext,
tc_expr: acorn.TokContext
}

// We pick (statics) from acorn rather than plain extending to avoid complaint
// about base constructors needing the same return type (i.e., we return
// `AcornJsxParser` here)
interface AcornJsxParserCtor extends Pick<typeof acorn.Parser, keyof typeof acorn.Parser> {
readonly acornJsx: {
tokTypes: TokTypes;
tokContexts: TokContexts
};

new (options: acorn.Options, input: string, startPos?: number): AcornJsxParser;
}

interface AcornJsxParser extends acorn.Parser {
jsx_readToken(): string;
jsx_readNewLine(normalizeCRLF: boolean): void;
jsx_readString(quote: number): void;
jsx_readEntity(): string;
jsx_readWord(): void;
jsx_parseIdentifier(): acorn.Node;
jsx_parseNamespacedName(): acorn.Node;
jsx_parseElementName(): acorn.Node | string;
jsx_parseAttributeValue(): acorn.Node;
jsx_parseEmptyExpression(): acorn.Node;
jsx_parseExpressionContainer(): acorn.Node;
jsx_parseAttribute(): acorn.Node;
jsx_parseOpeningElementAt(startPos: number, startLoc?: acorn.SourceLocation): acorn.Node;
jsx_parseClosingElementAt(startPos: number, startLoc?: acorn.SourceLocation): acorn.Node;
jsx_parseElementAt(startPos: number, startLoc?: acorn.SourceLocation): acorn.Node;
jsx_parseText(): acorn.Node;
jsx_parseElement(): acorn.Node;
}
}

export = jsx;

0 comments on commit 8ed96d6

Please sign in to comment.