Skip to content

Commit 9261da0

Browse files
authored
fix(typescript): attempt to fix types export (#1483)
1 parent e433f7d commit 9261da0

File tree

5 files changed

+732
-10
lines changed

5 files changed

+732
-10
lines changed

package.json

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
},
2020
"description": "JSDoc linting rules for ESLint.",
2121
"devDependencies": {
22+
"@arethetypeswrong/cli": "^0.18.2",
2223
"@babel/cli": "^7.28.3",
2324
"@babel/core": "^7.28.4",
2425
"@babel/eslint-parser": "^7.28.4",
@@ -83,19 +84,24 @@
8384
"types": "./dist/index.d.ts",
8485
"exports": {
8586
".": {
86-
"types": "./dist/index.d.ts",
87-
"import": "./src/index.js",
88-
"require": "./dist/index-cjs.cjs"
87+
"import": {
88+
"types": "./dist/index.d.ts",
89+
"default": "./src/index.js"
90+
},
91+
"require": {
92+
"types": "./dist/index-cjs.d.ts",
93+
"default": "./dist/index-cjs.cjs"
94+
}
8995
},
9096
"./getJsdocProcessorPlugin.js": {
9197
"types": "./dist/getJsdocProcessorPlugin.d.ts",
92-
"import": "./dist/getJsdocProcessorPlugin.cjs",
93-
"require": "./src/getJsdocProcessorPlugin.js"
98+
"import": "./src/getJsdocProcessorPlugin.js",
99+
"require": "./dist/getJsdocProcessorPlugin.cjs"
94100
},
95101
"./iterateJsdoc.js": {
96102
"types": "./dist/iterateJsdoc.d.ts",
97-
"import": "./dist/iterateJsdoc.cjs",
98-
"require": "./src/iterateJsdoc.js"
103+
"import": "./src/iterateJsdoc.js",
104+
"require": "./dist/iterateJsdoc.cjs"
99105
}
100106
},
101107
"name": "eslint-plugin-jsdoc",
@@ -137,7 +143,8 @@
137143
"scripts": {
138144
"tsc": "tsc",
139145
"tsc-build": "tsc -p tsconfig-prod.json",
140-
"build": "rimraf ./dist && NODE_ENV=production babel ./src --out-file-extension .cjs --out-dir ./dist --copy-files --source-maps --ignore ./src/bin/*.js --no-copy-ignored && replace 'require\\(\"\\.(.*?)\\.[^.]*?\"\\)' 'require(\".$1.cjs\")' 'dist' -r --include=\"*.cjs\" && pnpm tsc-build",
146+
"build": "node ./src/bin/buildEntryFileForTS.js && rimraf ./dist && NODE_ENV=production babel ./src --out-file-extension .cjs --out-dir ./dist --copy-files --source-maps --ignore ./src/bin/*.js --no-copy-ignored && replace 'require\\(\"\\.(.*?)\\.[^.]*?\"\\)' 'require(\".$1.cjs\")' 'dist' -r --include=\"*.cjs\" && pnpm tsc-build",
147+
"attw": "attw --pack .",
141148
"check-docs": "node ./src/bin/generateDocs.js --check",
142149
"create-docs": "pnpm run create-options && node ./src/bin/generateDocs.js",
143150
"create-rule": "node ./src/bin/generateRule.js",

pnpm-lock.yaml

Lines changed: 97 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/bin/buildEntryFileForTS.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import {
2+
readFile,
3+
writeFile,
4+
} from 'node:fs/promises';
5+
6+
const {
7+
dirname,
8+
} = import.meta;
9+
const dataToEmbed = await readFile(dirname + '/../index-cjs.js', 'utf8');
10+
const templateContent = await readFile(dirname + '/../index-esm.js', 'utf8');
11+
12+
const finalContent = '/* AUTO-GENERATED BY build SCRIPT */\n' + templateContent.replace(
13+
/\/\/ BEGIN REPLACE[\s\S]*\/\/ END REPLACE\n/v,
14+
`${dataToEmbed}`,
15+
);
16+
await writeFile(dirname + '/../index.js', finalContent);

src/index-esm.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/* eslint-disable perfectionist/sort-imports -- For auto-generate; Do not remove */
2+
import {
3+
merge,
4+
} from 'object-deep-merge';
5+
6+
// BEGIN REPLACE
7+
import index from './index-cjs.js';
8+
9+
// eslint-disable-next-line unicorn/prefer-export-from --- Reusing `index`
10+
export default index;
11+
// END REPLACE
12+
13+
/* eslint-disable jsdoc/valid-types -- Bug */
14+
/**
15+
* @type {((
16+
* cfg?: {
17+
* mergeSettings?: boolean,
18+
* config?: `flat/${import('./index-cjs.js').ConfigGroups}${import('./index-cjs.js').ConfigVariants}${import('./index-cjs.js').ErrorLevelVariants}`,
19+
* settings?: Partial<import('./iterateJsdoc.js').Settings>
20+
* }
21+
* ) => import('eslint').Linter.Config)}
22+
*/
23+
/* eslint-enable jsdoc/valid-types -- Bug */
24+
export const jsdoc = function (cfg) {
25+
/** @type {import('eslint').Linter.Config} */
26+
let outputConfig = {
27+
plugins: {
28+
jsdoc: index,
29+
},
30+
};
31+
if (
32+
cfg?.config
33+
) {
34+
// @ts-expect-error Security check
35+
if (cfg.config === '__proto__') {
36+
throw new TypeError('Disallowed config value');
37+
}
38+
39+
outputConfig = index.configs[cfg.config];
40+
}
41+
42+
outputConfig.settings = {
43+
jsdoc: cfg?.mergeSettings === false ?
44+
cfg.settings :
45+
merge(
46+
{},
47+
cfg?.settings ?? {},
48+
cfg?.config?.includes('recommended') ?
49+
{
50+
// We may need to drop these for "typescript" (non-"flavor") configs,
51+
// if support is later added: https://www.typescriptlang.org/docs/handbook/jsdoc-supported-types.html
52+
structuredTags: {
53+
next: {
54+
required: [
55+
'type',
56+
],
57+
},
58+
throws: {
59+
required: [
60+
'type',
61+
],
62+
},
63+
yields: {
64+
required: [
65+
'type',
66+
],
67+
},
68+
},
69+
} :
70+
{},
71+
),
72+
};
73+
74+
return outputConfig;
75+
};

0 commit comments

Comments
 (0)