From 8ed96d6ddec2065204ba07d924bb2e7bca539ea6 Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Tue, 15 Nov 2022 00:40:32 +0800 Subject: [PATCH] Improve typings: ESM, `AcornJsxParser` class and `tokTypes` const (#130) --- index.d.ts | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 55 insertions(+), 2 deletions(-) diff --git a/index.d.ts b/index.d.ts index f37b1df..66ef62f 100644 --- a/index.d.ts +++ b/index.d.ts @@ -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 { + 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;