Skip to content

Commit 8ed96d6

Browse files
authored
Improve typings: ESM, AcornJsxParser class and tokTypes const (#130)
1 parent de5b1e9 commit 8ed96d6

File tree

1 file changed

+55
-2
lines changed

1 file changed

+55
-2
lines changed

index.d.ts

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,65 @@
1-
import { Parser } from 'acorn'
1+
import * as acorn from 'acorn';
22

3-
declare const jsx: (options?: jsx.Options) => (BaseParser: typeof Parser) => typeof Parser;
3+
interface JsxTokTypes extends AcornTokTypes {
4+
jsxName: acorn.TokenType,
5+
jsxText: acorn.TokenType,
6+
jsxTagEnd: acorn.TokenType,
7+
jsxTagStart: acorn.TokenType
8+
}
9+
10+
declare const jsx: {
11+
tokTypes: JsxTokTypes;
12+
(options?: jsx.Options): (BaseParser: typeof acorn.Parser) => jsx.AcornJsxParserCtor
13+
}
14+
15+
type AcornTokTypes = typeof acorn.tokTypes;
416

517
declare namespace jsx {
18+
19+
type TokTypes = JsxTokTypes
20+
621
interface Options {
722
allowNamespacedObjects?: boolean;
823
allowNamespaces?: boolean;
924
}
25+
26+
interface TokContexts {
27+
tc_oTag: acorn.TokContext,
28+
tc_cTag: acorn.TokContext,
29+
tc_expr: acorn.TokContext
30+
}
31+
32+
// We pick (statics) from acorn rather than plain extending to avoid complaint
33+
// about base constructors needing the same return type (i.e., we return
34+
// `AcornJsxParser` here)
35+
interface AcornJsxParserCtor extends Pick<typeof acorn.Parser, keyof typeof acorn.Parser> {
36+
readonly acornJsx: {
37+
tokTypes: TokTypes;
38+
tokContexts: TokContexts
39+
};
40+
41+
new (options: acorn.Options, input: string, startPos?: number): AcornJsxParser;
42+
}
43+
44+
interface AcornJsxParser extends acorn.Parser {
45+
jsx_readToken(): string;
46+
jsx_readNewLine(normalizeCRLF: boolean): void;
47+
jsx_readString(quote: number): void;
48+
jsx_readEntity(): string;
49+
jsx_readWord(): void;
50+
jsx_parseIdentifier(): acorn.Node;
51+
jsx_parseNamespacedName(): acorn.Node;
52+
jsx_parseElementName(): acorn.Node | string;
53+
jsx_parseAttributeValue(): acorn.Node;
54+
jsx_parseEmptyExpression(): acorn.Node;
55+
jsx_parseExpressionContainer(): acorn.Node;
56+
jsx_parseAttribute(): acorn.Node;
57+
jsx_parseOpeningElementAt(startPos: number, startLoc?: acorn.SourceLocation): acorn.Node;
58+
jsx_parseClosingElementAt(startPos: number, startLoc?: acorn.SourceLocation): acorn.Node;
59+
jsx_parseElementAt(startPos: number, startLoc?: acorn.SourceLocation): acorn.Node;
60+
jsx_parseText(): acorn.Node;
61+
jsx_parseElement(): acorn.Node;
62+
}
1063
}
1164

1265
export = jsx;

0 commit comments

Comments
 (0)