Skip to content

Commit 5b66ff3

Browse files
authored
Merge pull request #15 from puzzmo-com/ensure-prettier-fns
Ensure prettier fns are fns before calling them
2 parents 25572b1 + b8c70a6 commit 5b66ff3

File tree

5 files changed

+15
-10
lines changed

5 files changed

+15
-10
lines changed

.eslintignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ coverage
33
lib
44
node_modules
55
pnpm-lock.yaml
6-
src/tests/vendor
6+
src/tests/vendor
7+
src/formatDTS.ts

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
### 1.0.2
4+
5+
- Better prettier detection (and fallback) for the generated files, re #14
6+
37
### 1.0
48

59
- No changes

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"format": "prettier \"**/*\" --ignore-unknown",
2222
"format:write": "pnpm format --write",
2323
"jest": "vitest",
24-
"lint": "eslint . --max-warnings 0 --report-unused-disable-directives",
24+
"lint": "eslint . --report-unused-disable-directives",
2525
"lint:knip": "knip",
2626
"lint:md": "markdownlint \"**/*.md\" \".github/**/*.md\" --rules sentences-per-line",
2727
"lint:package": "npmPkgJsonLint .",
@@ -35,7 +35,7 @@
3535
},
3636
"lint-staged": {
3737
"*": "prettier --ignore-unknown --write",
38-
"*.ts": "eslint --fix --max-warnings 0"
38+
"*.ts": "eslint --fix"
3939
},
4040
"dependencies": {
4141
"@mrleebo/prisma-ast": "^0.12.0",

src/formatDTS.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
// https://prettier.io/docs/en/api.html
22

3-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
43
let prettier: any | null = null
54
try {
6-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
75
prettier = await import("prettier")
86
} catch (er) {
97
console.error(er)
108
prettier = null
119
}
1210

1311
export const getPrettierConfig = (path: string): unknown => {
14-
if (!prettier) return {}
12+
if (!prettier?.default?.resolveConfig) return {}
13+
if (typeof prettier.default.resolveConfig !== "function") return {}
1514

16-
// I confirmed that this lookup hits caches in Redwood
17-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
15+
// I confirmed that this lookup hits caches in RedwoodJS
1816
const opts = prettier.default.resolveConfig.sync(path) ?? {}
1917
return opts
2018
}
2119

2220
export const formatDTS = (path: string, content: string, config: unknown): string => {
23-
if (!prettier) return content
21+
if (!prettier?.default?.format) return content
22+
if (typeof prettier.default.format !== "function") return content
23+
2424
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
2525
return prettier.default.format(content, { ...(config as object), filepath: path }) as string
2626
}

tsconfig.eslint.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"extends": "./tsconfig.json",
32
"exclude": ["src/tests/vendor"],
3+
"extends": "./tsconfig.json",
44
"include": [".", "src/tests/**/*"]
55
}

0 commit comments

Comments
 (0)