Skip to content

Commit 08f7834

Browse files
brettz9Copilot
andauthored
feat: strip property when there is a class tag and the context is set (#1414)
* feat: strip property when there is a `class` tag and the context is set; fixes #1376 * Update docs/rules/no-types.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update .README/rules/no-types.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent a083c81 commit 08f7834

File tree

4 files changed

+63
-3
lines changed

4 files changed

+63
-3
lines changed

.README/rules/no-types.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ This rule reports types being used on `@param` or `@returns`.
77
The rule is intended to prevent the indication of types on tags where
88
the type information would be redundant with TypeScript.
99

10+
When `contexts` are supplied, will also strip `@property` when on a
11+
`ClassDeclaration`.
12+
1013
## Fixer
1114

12-
(TODO)
15+
Strips any types that are found.
1316

1417
## Options
1518

docs/rules/no-types.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@ This rule reports types being used on `@param` or `@returns`.
1515
The rule is intended to prevent the indication of types on tags where
1616
the type information would be redundant with TypeScript.
1717

18+
When `contexts` are supplied, will also strip `@property` when on a
19+
`ClassDeclaration`.
20+
1821
<a name="user-content-no-types-fixer"></a>
1922
<a name="no-types-fixer"></a>
2023
## Fixer
2124

22-
(TODO)
25+
Strips any types that are found.
2326

2427
<a name="user-content-no-types-options"></a>
2528
<a name="no-types-options"></a>
@@ -137,6 +140,15 @@ export interface B {
137140
methodB(paramB: string): void
138141
}
139142
// Message: Types are not permitted on @param.
143+
144+
/**
145+
* @class
146+
* @property {object} x
147+
*/
148+
class Example {
149+
x: number;
150+
}
151+
// Message: Types are not permitted on @property in the supplied context.
140152
````
141153

142154

src/rules/noTypes.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const removeType = ({
1111
};
1212

1313
export default iterateJsdoc(({
14+
node,
1415
utils,
1516
}) => {
1617
if (!utils.isIteratingFunction() && !utils.isVirtualFunction()) {
@@ -30,11 +31,26 @@ export default iterateJsdoc(({
3031
});
3132
}
3233
}
34+
35+
if (node?.type === 'ClassDeclaration') {
36+
const propertyTags = utils.getPresentTags([
37+
'prop', 'property',
38+
]);
39+
for (const tag of propertyTags) {
40+
if (tag.type) {
41+
utils.reportJSDoc(`Types are not permitted on @${tag.tag} in the supplied context.`, tag, () => {
42+
for (const source of tag.source) {
43+
removeType(source);
44+
}
45+
});
46+
}
47+
}
48+
}
3349
}, {
3450
contextDefaults: [
3551
'ArrowFunctionExpression', 'FunctionDeclaration', 'FunctionExpression', 'TSDeclareFunction',
3652
// Add this to above defaults
37-
'TSMethodSignature',
53+
'TSMethodSignature', 'ClassDeclaration',
3854
],
3955
meta: {
4056
docs: {

test/rules/assertions/noTypes.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,35 @@ export default /** @type {import('../index.js').TestCases} */ ({
257257
}
258258
`,
259259
},
260+
{
261+
code: `
262+
/**
263+
* @class
264+
* @property {object} x
265+
*/
266+
class Example {
267+
x: number;
268+
}
269+
`,
270+
errors: [
271+
{
272+
line: 4,
273+
message: 'Types are not permitted on @property in the supplied context.',
274+
},
275+
],
276+
languageOptions: {
277+
parser: typescriptEslintParser,
278+
},
279+
output: `
280+
/**
281+
* @class
282+
* @property x
283+
*/
284+
class Example {
285+
x: number;
286+
}
287+
`,
288+
},
260289
],
261290
valid: [
262291
{

0 commit comments

Comments
 (0)