Skip to content

Commit 09d4f1c

Browse files
authoredJul 17, 2024··
fix: support running inside a CommonJS module
* fix: support running inside a CommonJS module * fix: lint
1 parent 5ea816a commit 09d4f1c

File tree

5 files changed

+37
-9
lines changed

5 files changed

+37
-9
lines changed
 

‎package.json

+12-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,18 @@
1111
"name": "JoshuaKGoldberg",
1212
"email": "npm@joshuakgoldberg.com"
1313
},
14-
"type": "module",
14+
"exports": {
15+
".": {
16+
"import": {
17+
"types": "./lib/index.d.mts",
18+
"default": "./lib/index.mjs"
19+
},
20+
"require": {
21+
"types": "./lib/index.d.ts",
22+
"default": "./lib/index.js"
23+
}
24+
}
25+
},
1526
"main": "./lib/index.js",
1627
"files": [
1728
"lib/",

‎src/index.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
1+
// CJS/ESM 🫠
2+
/* eslint-disable @typescript-eslint/ban-ts-comment, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access */
13
import { Parser } from "prettier";
2-
import babel from "prettier/parser-babel";
3-
import typescript from "prettier/parser-typescript";
4+
import * as babel from "prettier/parser-babel";
5+
import * as typescript from "prettier/parser-typescript";
46

57
import { preprocess } from "./preprocess.js";
68

79
export const parsers = {
810
babel: {
9-
...babel.parsers.babel,
11+
// @ts-ignore
12+
...(babel.default || babel).parsers.babel,
1013
preprocess,
1114
},
1215
typescript: {
13-
...typescript.parsers.typescript,
16+
// @ts-ignore
17+
...(typescript.default || typescript).parsers.typescript,
1418
preprocess,
1519
},
1620
} satisfies Record<string, Parser>;
21+
22+
/* eslint-enable */

‎src/reprintAst.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// CJS/ESM 🫠
2+
/* eslint-disable @typescript-eslint/ban-ts-comment, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/restrict-plus-operands */
13
import generate, { GeneratorOptions } from "@babel/generator";
24

35
import { CollectibleNode } from "./types.js";
@@ -22,7 +24,8 @@ export function reprintAst(code: string, collectedNodes: CollectibleNode[]) {
2224
output += code.slice(lastEnd, collectedNode.start!);
2325

2426
// See https://github.com/prettier/prettier/issues/9114 for a Prettier AST format API.
25-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
27+
28+
// @ts-ignore
2629
output += (generate.default || generate)(collectedNode, printOptions).code;
2730

2831
lastEnd = collectedNode.end!;
@@ -31,3 +34,5 @@ export function reprintAst(code: string, collectedNodes: CollectibleNode[]) {
3134

3235
return output + code.slice(lastEnd);
3336
}
37+
38+
/* eslint-enable */

‎src/traverseAndModifyAst.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// CJS/ESM 🫠
2+
/* eslint-disable @typescript-eslint/ban-ts-comment, @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access */
3+
14
import traverse, { Node, NodePath } from "@babel/traverse";
25

36
import { modifyNodeIfMissingBrackets } from "./modifyNodeIfMissingBrackets.js";
@@ -16,7 +19,7 @@ export function traverseAndModifyAst(ast: Node) {
1619
}
1720
}
1821

19-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- CJS/ESM 🫠
22+
// @ts-ignore
2023
(traverse.default || traverse)(ast, {
2124
DoWhileStatement: collector,
2225
ForInStatement: collector,
@@ -37,8 +40,9 @@ function nodeNotAlreadySeen(node: Node, seenNodes: Set<Node>) {
3740

3841
// All child nodes are marked as seen and removed from collections,
3942
// so that we don't accidentally print overlapping fixes for them later.
40-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- CJS/ESM 🫠
43+
// @ts-ignore
4144
(traverse.default || traverse)(node, {
45+
// @ts-ignore
4246
enter(path) {
4347
seenNodes.add(path.node);
4448
},
@@ -49,3 +53,5 @@ function nodeNotAlreadySeen(node: Node, seenNodes: Set<Node>) {
4953

5054
return true;
5155
}
56+
57+
/* eslint-enable */

‎tsup.config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default defineConfig({
55
clean: true,
66
dts: true,
77
entry: ["src/**/*.ts", "!src/**/*.test.*"],
8-
format: "esm",
8+
format: ["cjs", "esm"],
99
outDir: "lib",
1010
sourcemap: true,
1111
});

0 commit comments

Comments
 (0)
Please sign in to comment.