Skip to content

Commit 866d709

Browse files
committed
fix: ensure types work for both CJS and ESM
1 parent 7d5f14d commit 866d709

File tree

3 files changed

+21
-18
lines changed

3 files changed

+21
-18
lines changed

index.d.cts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import * as jsx from './index.js';
2+
3+
export = jsx;

index.d.ts

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,32 @@ interface JsxTokTypes extends AcornTokTypes {
1818

1919
type AcornTokTypes = typeof acorn.tokTypes;
2020

21-
declare function jsx(options?: jsx.Options): (BaseParser: typeof acorn.Parser) => jsx.AcornJsxParserCtor;
21+
type TokTypes = JsxTokTypes
2222

23-
declare namespace jsx {
24-
type TokTypes = JsxTokTypes
25-
26-
interface Options {
23+
interface Options {
2724
allowNamespacedObjects?: boolean;
2825
allowNamespaces?: boolean;
29-
}
26+
}
3027

31-
interface TokContexts {
28+
interface TokContexts {
3229
tc_oTag: TokContext,
3330
tc_cTag: TokContext,
3431
tc_expr: TokContext
35-
}
32+
}
3633

37-
// We pick (statics) from acorn rather than plain extending to avoid complaint
38-
// about base constructors needing the same return type (i.e., we return
39-
// `AcornJsxParser` here)
40-
interface AcornJsxParserCtor extends Pick<typeof acorn.Parser, keyof typeof acorn.Parser> {
34+
// We pick (statics) from acorn rather than plain extending to avoid complaint
35+
// about base constructors needing the same return type (i.e., we return
36+
// `AcornJsxParser` here)
37+
interface AcornJsxParserCtor extends Pick<typeof acorn.Parser, keyof typeof acorn.Parser> {
4138
readonly acornJsx: {
42-
tokTypes: TokTypes;
43-
tokContexts: TokContexts
39+
tokTypes: TokTypes;
40+
tokContexts: TokContexts
4441
};
4542

4643
new (options: acorn.Options, input: string, startPos?: number): AcornJsxParser;
47-
}
44+
}
4845

49-
interface AcornJsxParser extends acorn.Parser {
46+
interface AcornJsxParser extends acorn.Parser {
5047
jsx_readToken(): string;
5148
jsx_readNewLine(normalizeCRLF: boolean): void;
5249
jsx_readString(quote: number): void;
@@ -64,7 +61,6 @@ declare namespace jsx {
6461
jsx_parseElementAt(startPos: number, startLoc?: acorn.SourceLocation): acorn.Node;
6562
jsx_parseText(): acorn.Node;
6663
jsx_parseElement(): acorn.Node;
67-
}
6864
}
6965

70-
export = jsx;
66+
export default function jsx(options?: Options): (BaseParser: typeof acorn.Parser) => AcornJsxParserCtor;

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
"type": "module",
77
"main": "index.cjs.js",
88
"exports": {
9+
"types": {
10+
"import": "./index.d.ts",
11+
"require": "./index.d.cts"
12+
},
913
"import": "./index.js",
1014
"require": "./index.cjs"
1115
},

0 commit comments

Comments
 (0)