Skip to content

Commit 4f8d242

Browse files
tschaubbrettz9
andauthoredAug 8, 2022
fix(check-tag-names): constructor tag and tagNamePreference (#899)
* fix(`check-tag-names`): work with the constructor tag * test: add invalid test Co-authored-by: Brett Zamir <brettz9@yahoo.com>
1 parent 7d21369 commit 4f8d242

File tree

4 files changed

+47
-1
lines changed

4 files changed

+47
-1
lines changed
 

‎README.md

+9
Original file line numberDiff line numberDiff line change
@@ -4500,6 +4500,15 @@ function quux (foo) {}
45004500
/** @jsxImportSource preact */
45014501
/** @jsxRuntime automatic */
45024502
// Message: Invalid JSDoc tag name "jsx".
4503+
4504+
/**
4505+
* @constructor
4506+
*/
4507+
function Test() {
4508+
this.works = false;
4509+
}
4510+
// Settings: {"jsdoc":{"tagNamePreference":{"returns":"return"}}}
4511+
// Message: Invalid JSDoc tag (preference). Replace "constructor" JSDoc tag with "class".
45034512
````
45044513

45054514
The following patterns are not considered problems:

‎src/jsdocUtils.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ const getPreferredTagName = (
396396
}),
397397
);
398398

399-
if (name in tagPreferenceFixed) {
399+
if (Object.prototype.hasOwnProperty.call(tagPreferenceFixed, name)) {
400400
return tagPreferenceFixed[name];
401401
}
402402

‎test/jsdocUtils.js

+6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ describe('jsdocUtils', () => {
1010
it('returns the primary tag name', () => {
1111
expect(jsdocUtils.getPreferredTagName({}, 'jsdoc', 'return')).to.equal('returns');
1212
});
13+
it('works with the constructor tag', () => {
14+
expect(jsdocUtils.getPreferredTagName({}, 'jsdoc', 'constructor')).to.equal('class');
15+
});
16+
});
17+
it('works with tags that clash with prototype properties', () => {
18+
expect(jsdocUtils.getPreferredTagName({}, 'jsdoc', 'toString')).to.equal('toString');
1319
});
1420
it('returns the primary tag name', () => {
1521
expect(jsdocUtils.getPreferredTagName({}, 'jsdoc', 'returns')).to.equal('returns');

‎test/rules/assertions/checkTagNames.js

+31
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,37 @@ export default {
641641
},
642642
],
643643
},
644+
{
645+
code: `
646+
/**
647+
* @constructor
648+
*/
649+
function Test() {
650+
this.works = false;
651+
}
652+
`,
653+
errors: [
654+
{
655+
line: 3,
656+
message: 'Invalid JSDoc tag (preference). Replace "constructor" JSDoc tag with "class".',
657+
},
658+
],
659+
output: `
660+
/**
661+
* @class
662+
*/
663+
function Test() {
664+
this.works = false;
665+
}
666+
`,
667+
settings: {
668+
jsdoc: {
669+
tagNamePreference: {
670+
returns: 'return',
671+
},
672+
},
673+
},
674+
},
644675
],
645676
valid: [
646677
{

0 commit comments

Comments
 (0)
Please sign in to comment.