Skip to content

Commit 93e6b1c

Browse files
committed
feat: copy over config properties
Also: - fix: avoid config function rules overwriting config's rules - feat: reexport `getJsdocProcessorPlugin`
1 parent 18d2c0f commit 93e6b1c

File tree

7 files changed

+186
-26
lines changed

7 files changed

+186
-26
lines changed

.README/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export default [
4141
];
4242
```
4343

44-
Or with TypeScript-aware extra rules and settings supplied:
44+
Or with TypeScript-aware extra rules and/or settings supplied:
4545

4646
```js
4747
import {jsdoc} from 'eslint-plugin-jsdoc';
@@ -99,6 +99,10 @@ export default [
9999
];
100100
```
101101

102+
A `plugins` property can also be supplied to merge with the resulting `jsdoc` plugin.
103+
104+
Other config properties such as `files`, `ignores`, etc. are also copied over.
105+
102106
### Flat config (declarative)
103107

104108
```js

.README/processors.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ The approach below works in ESLint 9. For ESLint 7, please see our [`check-examp
99
The approach requires that we first indicate the JavaScript files that will be checked for `@example` tags.
1010

1111
```js
12-
import {getJsdocProcessorPlugin} from 'eslint-plugin-jsdoc/getJsdocProcessorPlugin.js';
12+
import {getJsdocProcessorPlugin} from 'eslint-plugin-jsdoc';
1313

1414
export default [
1515
{

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export default [
6060
];
6161
```
6262

63-
Or with TypeScript-aware extra rules and settings supplied:
63+
Or with TypeScript-aware extra rules and/or settings supplied:
6464

6565
```js
6666
import {jsdoc} from 'eslint-plugin-jsdoc';
@@ -118,6 +118,10 @@ export default [
118118
];
119119
```
120120

121+
A `plugins` property can also be supplied to merge with the resulting `jsdoc` plugin.
122+
123+
Other config properties such as `files`, `ignores`, etc. are also copied over.
124+
121125
<a name="user-content-eslint-plugin-jsdoc-configuration-flat-config-declarative"></a>
122126
<a name="eslint-plugin-jsdoc-configuration-flat-config-declarative"></a>
123127
### Flat config (declarative)

docs/processors.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ The approach below works in ESLint 9. For ESLint 7, please see our [`check-examp
1111
The approach requires that we first indicate the JavaScript files that will be checked for `@example` tags.
1212

1313
```js
14-
import {getJsdocProcessorPlugin} from 'eslint-plugin-jsdoc/getJsdocProcessorPlugin.js';
14+
import {getJsdocProcessorPlugin} from 'eslint-plugin-jsdoc';
1515

1616
export default [
1717
{

src/index-esm.js

Lines changed: 58 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export default index;
1313
/* eslint-disable jsdoc/valid-types -- Bug */
1414
/**
1515
* @type {((
16-
* cfg?: {
16+
* cfg?: import('eslint').Linter.Config & {
1717
* mergeSettings?: boolean,
1818
* config?: `flat/${import('./index-cjs.js').ConfigGroups}${import('./index-cjs.js').ConfigVariants}${import('./index-cjs.js').ErrorLevelVariants}`,
1919
* settings?: Partial<import('./iterateJsdoc.js').Settings>,
@@ -29,19 +29,62 @@ export const jsdoc = function (cfg) {
2929
jsdoc: index,
3030
},
3131
};
32-
if (
33-
cfg?.config
34-
) {
35-
// @ts-expect-error Security check
36-
if (cfg.config === '__proto__') {
37-
throw new TypeError('Disallowed config value');
32+
33+
if (cfg) {
34+
if (cfg.config) {
35+
// @ts-expect-error Security check
36+
if (cfg.config === '__proto__') {
37+
throw new TypeError('Disallowed config value');
38+
}
39+
40+
outputConfig = index.configs[cfg.config];
3841
}
3942

40-
outputConfig = index.configs[cfg.config];
41-
}
43+
if (cfg.rules) {
44+
outputConfig.rules = {
45+
...outputConfig.rules,
46+
...cfg.rules,
47+
};
48+
}
49+
50+
if (cfg.plugins) {
51+
outputConfig.plugins = {
52+
...outputConfig.plugins,
53+
...cfg.plugins,
54+
};
55+
}
56+
57+
if (cfg.name) {
58+
outputConfig.name = cfg.name;
59+
}
60+
61+
if (cfg.basePath) {
62+
outputConfig.basePath = cfg.basePath;
63+
}
64+
65+
if (cfg.files) {
66+
outputConfig.files = cfg.files;
67+
}
68+
69+
if (cfg.ignores) {
70+
outputConfig.ignores = cfg.ignores;
71+
}
4272

43-
if (cfg?.rules) {
44-
outputConfig.rules = cfg.rules;
73+
if (cfg.language) {
74+
outputConfig.language = cfg.language;
75+
}
76+
77+
if (cfg.languageOptions) {
78+
outputConfig.languageOptions = cfg.languageOptions;
79+
}
80+
81+
if (cfg.linterOptions) {
82+
outputConfig.linterOptions = cfg.linterOptions;
83+
}
84+
85+
if (cfg.processor) {
86+
outputConfig.processor = cfg.processor;
87+
}
4588
}
4689

4790
outputConfig.settings = {
@@ -78,3 +121,7 @@ export const jsdoc = function (cfg) {
78121

79122
return outputConfig;
80123
};
124+
125+
export {
126+
getJsdocProcessorPlugin,
127+
} from './getJsdocProcessorPlugin.js';

src/index.js

Lines changed: 58 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ export default index;
536536
/* eslint-disable jsdoc/valid-types -- Bug */
537537
/**
538538
* @type {((
539-
* cfg?: {
539+
* cfg?: import('eslint').Linter.Config & {
540540
* mergeSettings?: boolean,
541541
* config?: `flat/${ConfigGroups}${ConfigVariants}${ErrorLevelVariants}`,
542542
* settings?: Partial<import('./iterateJsdoc.js').Settings>,
@@ -552,19 +552,62 @@ export const jsdoc = function (cfg) {
552552
jsdoc: index,
553553
},
554554
};
555-
if (
556-
cfg?.config
557-
) {
558-
// @ts-expect-error Security check
559-
if (cfg.config === '__proto__') {
560-
throw new TypeError('Disallowed config value');
555+
556+
if (cfg) {
557+
if (cfg.config) {
558+
// @ts-expect-error Security check
559+
if (cfg.config === '__proto__') {
560+
throw new TypeError('Disallowed config value');
561+
}
562+
563+
outputConfig = index.configs[cfg.config];
561564
}
562565

563-
outputConfig = index.configs[cfg.config];
564-
}
566+
if (cfg.rules) {
567+
outputConfig.rules = {
568+
...outputConfig.rules,
569+
...cfg.rules,
570+
};
571+
}
572+
573+
if (cfg.plugins) {
574+
outputConfig.plugins = {
575+
...outputConfig.plugins,
576+
...cfg.plugins,
577+
};
578+
}
579+
580+
if (cfg.name) {
581+
outputConfig.name = cfg.name;
582+
}
583+
584+
if (cfg.basePath) {
585+
outputConfig.basePath = cfg.basePath;
586+
}
587+
588+
if (cfg.files) {
589+
outputConfig.files = cfg.files;
590+
}
565591

566-
if (cfg?.rules) {
567-
outputConfig.rules = cfg.rules;
592+
if (cfg.ignores) {
593+
outputConfig.ignores = cfg.ignores;
594+
}
595+
596+
if (cfg.language) {
597+
outputConfig.language = cfg.language;
598+
}
599+
600+
if (cfg.languageOptions) {
601+
outputConfig.languageOptions = cfg.languageOptions;
602+
}
603+
604+
if (cfg.linterOptions) {
605+
outputConfig.linterOptions = cfg.linterOptions;
606+
}
607+
608+
if (cfg.processor) {
609+
outputConfig.processor = cfg.processor;
610+
}
568611
}
569612

570613
outputConfig.settings = {
@@ -601,3 +644,7 @@ export const jsdoc = function (cfg) {
601644

602645
return outputConfig;
603646
};
647+
648+
export {
649+
getJsdocProcessorPlugin,
650+
} from './getJsdocProcessorPlugin.js';

test/index.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,64 @@ describe('jsdoc()', () => {
1414
});
1515
});
1616

17+
it('Builds config with additional plugin', () => {
18+
const pluginCfg = /** @type {Record<string, import('eslint').ESLint.Plugin>} */ ({
19+
something: {
20+
configs: {
21+
testConfig: {
22+
rules: {
23+
semi: 'off',
24+
},
25+
},
26+
},
27+
},
28+
});
29+
const cfg = jsdoc({
30+
plugins: pluginCfg,
31+
});
32+
expect(cfg.plugins?.jsdoc).to.equal(jsdocDefault);
33+
expect(cfg.plugins?.something).to.equal(pluginCfg.something);
34+
expect(cfg.settings).to.deep.equal({
35+
jsdoc: {},
36+
});
37+
});
38+
39+
it('Builds config reflecting copied properties', () => {
40+
const expected = {
41+
basePath: 'aPath',
42+
files: [
43+
'someFiles',
44+
],
45+
ignores: [
46+
'ignore1', 'dist',
47+
],
48+
language: 'json/jsonc',
49+
languageOptions: {
50+
ecmaVersion: /** @type {const} */ (2_023),
51+
},
52+
linterOptions: {
53+
noInlineConfig: true,
54+
},
55+
name: 'test',
56+
processor: 'abc',
57+
};
58+
59+
const config = structuredClone(expected);
60+
const cfg = jsdoc(config);
61+
expect(cfg.plugins?.jsdoc).to.equal(jsdocDefault);
62+
expect(cfg.settings).to.deep.equal({
63+
jsdoc: {},
64+
});
65+
for (const [
66+
prop,
67+
val,
68+
] of Object.entries(expected)) {
69+
expect(cfg[
70+
/** @type {keyof import('eslint').Linter.Config} */ (prop)
71+
]).to.deep.equal(val);
72+
}
73+
});
74+
1775
it('Builds simple plugins config with rules', () => {
1876
/* eslint-disable jsdoc/valid-types -- Bug */
1977
const rules = /** @type {{[key in keyof import('../src/rules.d.ts').Rules]?: import('eslint').Linter.RuleEntry<import('../src/rules.d.ts').Rules[key]>}} */ ({

0 commit comments

Comments
 (0)