Skip to content

Commit f77cf2a

Browse files
JoshuaKGoldbergbrettz9
authored andcommitted
Added enableFixer option
1 parent 7763b99 commit f77cf2a

File tree

4 files changed

+69
-12
lines changed

4 files changed

+69
-12
lines changed

.README/rules/check-tag-names.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,10 @@ The format is as follows:
187187
}
188188
```
189189

190+
##### `enableFixer`
191+
192+
Set to `false` to disable auto-removal of types that are redundant with the [`typed` option](#typed).
193+
190194
#### `jsxTags`
191195

192196
If this is set to `true`, all of the following tags used to control JSX output are allowed:

README.md

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4486,6 +4486,12 @@ The format is as follows:
44864486
}
44874487
```
44884488

4489+
<a name="user-content-eslint-plugin-jsdoc-rules-check-tag-names-options-6-enablefixer-2"></a>
4490+
<a name="eslint-plugin-jsdoc-rules-check-tag-names-options-6-enablefixer-2"></a>
4491+
##### <code>enableFixer</code>
4492+
4493+
Set to `false` to disable auto-removal of types that are redundant with the [`typed` option](#user-content-typed).
4494+
44894495
<a name="user-content-eslint-plugin-jsdoc-rules-check-tag-names-jsxtags"></a>
44904496
<a name="eslint-plugin-jsdoc-rules-check-tag-names-jsxtags"></a>
44914497
#### <code>jsxTags</code>
@@ -4575,6 +4581,10 @@ The following patterns are considered problems:
45754581
// "jsdoc/check-tag-names": ["error"|"warn", {"typed":true}]
45764582
// Message: '@type' is redundant when using a type system.
45774583

4584+
/** @type {string} */let a;
4585+
// "jsdoc/check-tag-names": ["error"|"warn", {"enableFixer":false,"typed":true}]
4586+
// Message: '@type' is redundant when using a type system.
4587+
45784588
/** @type {string} */ let a;
45794589
// "jsdoc/check-tag-names": ["error"|"warn", {"typed":true}]
45804590
// Message: '@type' is redundant when using a type system.
@@ -4607,6 +4617,11 @@ let a;
46074617
// "jsdoc/check-tag-names": ["error"|"warn", {"typed":true}]
46084618
// Message: '@abstract' is redundant outside of ambient (`declare`/`.d.ts`) contexts when using a type system.
46094619

4620+
/** @abstract */
4621+
let a;
4622+
// "jsdoc/check-tag-names": ["error"|"warn", {"enableFixer":false,"typed":true}]
4623+
// Message: '@abstract' is redundant outside of ambient (`declare`/`.d.ts`) contexts when using a type system.
4624+
46104625
const a = {
46114626
/** @abstract */
46124627
b: true,
@@ -12769,8 +12784,8 @@ A value indicating whether getters should be checked. Defaults to `false`.
1276912784

1277012785
A value indicating whether setters should be checked. Defaults to `false`.
1277112786

12772-
<a name="user-content-eslint-plugin-jsdoc-rules-require-example-options-25-enablefixer-2"></a>
12773-
<a name="eslint-plugin-jsdoc-rules-require-example-options-25-enablefixer-2"></a>
12787+
<a name="user-content-eslint-plugin-jsdoc-rules-require-example-options-25-enablefixer-3"></a>
12788+
<a name="eslint-plugin-jsdoc-rules-require-example-options-25-enablefixer-3"></a>
1277412789
##### <code>enableFixer</code>
1277512790

1277612791
A boolean on whether to enable the fixer (which adds an empty `@example` block).
@@ -13708,8 +13723,8 @@ setters should be checked but only when there is no getter. This may be useful
1370813723
if one only wishes documentation on one of the two accessors. Defaults to
1370913724
`false`.
1371013725

13711-
<a name="user-content-eslint-plugin-jsdoc-rules-require-jsdoc-options-28-enablefixer-3"></a>
13712-
<a name="eslint-plugin-jsdoc-rules-require-jsdoc-options-28-enablefixer-3"></a>
13726+
<a name="user-content-eslint-plugin-jsdoc-rules-require-jsdoc-options-28-enablefixer-4"></a>
13727+
<a name="eslint-plugin-jsdoc-rules-require-jsdoc-options-28-enablefixer-4"></a>
1371313728
##### <code>enableFixer</code>
1371413729

1371513730
A boolean on whether to enable the fixer (which adds an empty jsdoc block).
@@ -16159,8 +16174,8 @@ function signature, it may appear that there is an actual property named
1615916174

1616016175
An options object accepts the following optional properties:
1616116176

16162-
<a name="user-content-eslint-plugin-jsdoc-rules-require-param-options-32-enablefixer-4"></a>
16163-
<a name="eslint-plugin-jsdoc-rules-require-param-options-32-enablefixer-4"></a>
16177+
<a name="user-content-eslint-plugin-jsdoc-rules-require-param-options-32-enablefixer-5"></a>
16178+
<a name="eslint-plugin-jsdoc-rules-require-param-options-32-enablefixer-5"></a>
1616416179
##### <code>enableFixer</code>
1616516180

1616616181
Whether to enable the fixer. Defaults to `true`.

src/rules/checkTagNames.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ export default iterateJsdoc(({
7272
}) => {
7373
const {
7474
definedTags = [],
75+
enableFixer = true,
7576
jsxTags,
7677
typed,
7778
} = context.options[0] || {};
@@ -133,8 +134,8 @@ export default iterateJsdoc(({
133134
return true;
134135
};
135136

136-
const reportWithTypeRemovalFixer = (message, jsdocTag, tagIndex, additionalTagChanges) => {
137-
utils.reportJSDoc(message, jsdocTag, () => {
137+
const reportWithTagRemovalFixer = (message, jsdocTag, tagIndex, additionalTagChanges) => {
138+
utils.reportJSDoc(message, jsdocTag, enableFixer ? () => {
138139
if (jsdocTag.description.trim()) {
139140
utils.changeTag(jsdocTag, {
140141
postType: '',
@@ -146,25 +147,25 @@ export default iterateJsdoc(({
146147
removeEmptyBlock: true,
147148
});
148149
}
149-
}, true);
150+
} : null, true);
150151
};
151152

152153
const checkTagForTypedValidity = (jsdocTag, tagIndex) => {
153154
if (typedTagsAlwaysUnnecessary.has(jsdocTag.tag)) {
154-
reportWithTypeRemovalFixer(`'@${jsdocTag.tag}' is redundant when using a type system.`, jsdocTag, tagIndex, {
155+
reportWithTagRemovalFixer(`'@${jsdocTag.tag}' is redundant when using a type system.`, jsdocTag, tagIndex, {
155156
postTag: '',
156157
tag: '',
157158
});
158159
return true;
159160
}
160161

161162
if (tagIsRedundantWhenTyped(jsdocTag)) {
162-
reportWithTypeRemovalFixer(`'@${jsdocTag.tag}' is redundant outside of ambient (\`declare\`/\`.d.ts\`) contexts when using a type system.`, jsdocTag, tagIndex);
163+
reportWithTagRemovalFixer(`'@${jsdocTag.tag}' is redundant outside of ambient (\`declare\`/\`.d.ts\`) contexts when using a type system.`, jsdocTag, tagIndex);
163164
return true;
164165
}
165166

166167
if (typedTagsNeedingName.has(jsdocTag.tag) && !jsdocTag.name) {
167-
reportWithTypeRemovalFixer(`'@${jsdocTag.tag}' without a name is redundant when using a type system.`, jsdocTag, tagIndex);
168+
reportWithTagRemovalFixer(`'@${jsdocTag.tag}' without a name is redundant when using a type system.`, jsdocTag, tagIndex);
168169
return true;
169170
}
170171

@@ -244,6 +245,9 @@ export default iterateJsdoc(({
244245
},
245246
type: 'array',
246247
},
248+
enableFixer: {
249+
type: 'boolean',
250+
},
247251
jsxTags: {
248252
type: 'boolean',
249253
},

test/rules/assertions/checkTagNames.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,22 @@ export default {
4141
output: `let a;
4242
`,
4343
},
44+
{
45+
code: `/** @type {string} */let a;
46+
`,
47+
errors: [
48+
{
49+
line: 1,
50+
message: '\'@type\' is redundant when using a type system.',
51+
},
52+
],
53+
options: [
54+
{
55+
enableFixer: false,
56+
typed: true,
57+
},
58+
],
59+
},
4460
{
4561
code: `/** @type {string} */ let a;
4662
`,
@@ -163,6 +179,24 @@ export default {
163179
let a;
164180
`,
165181
},
182+
{
183+
code: `
184+
/** @abstract */
185+
let a;
186+
`,
187+
errors: [
188+
{
189+
line: 2,
190+
message: '\'@abstract\' is redundant outside of ambient (`declare`/`.d.ts`) contexts when using a type system.',
191+
},
192+
],
193+
options: [
194+
{
195+
enableFixer: false,
196+
typed: true,
197+
},
198+
],
199+
},
166200
{
167201
code: `
168202
const a = {

0 commit comments

Comments
 (0)