Skip to content

Commit

Permalink
fix(check-tag-names): constructor tag and tagNamePreference (#899)
Browse files Browse the repository at this point in the history
* fix(`check-tag-names`): work with the constructor tag
* test: add invalid test

Co-authored-by: Brett Zamir <brettz9@yahoo.com>
  • Loading branch information
tschaub and brettz9 authored Aug 8, 2022
1 parent 7d21369 commit 4f8d242
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4500,6 +4500,15 @@ function quux (foo) {}
/** @jsxImportSource preact */
/** @jsxRuntime automatic */
// Message: Invalid JSDoc tag name "jsx".

/**
* @constructor
*/
function Test() {
this.works = false;
}
// Settings: {"jsdoc":{"tagNamePreference":{"returns":"return"}}}
// Message: Invalid JSDoc tag (preference). Replace "constructor" JSDoc tag with "class".
````

The following patterns are not considered problems:
Expand Down
2 changes: 1 addition & 1 deletion src/jsdocUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ const getPreferredTagName = (
}),
);

if (name in tagPreferenceFixed) {
if (Object.prototype.hasOwnProperty.call(tagPreferenceFixed, name)) {
return tagPreferenceFixed[name];
}

Expand Down
6 changes: 6 additions & 0 deletions test/jsdocUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ describe('jsdocUtils', () => {
it('returns the primary tag name', () => {
expect(jsdocUtils.getPreferredTagName({}, 'jsdoc', 'return')).to.equal('returns');
});
it('works with the constructor tag', () => {
expect(jsdocUtils.getPreferredTagName({}, 'jsdoc', 'constructor')).to.equal('class');
});
});
it('works with tags that clash with prototype properties', () => {
expect(jsdocUtils.getPreferredTagName({}, 'jsdoc', 'toString')).to.equal('toString');
});
it('returns the primary tag name', () => {
expect(jsdocUtils.getPreferredTagName({}, 'jsdoc', 'returns')).to.equal('returns');
Expand Down
31 changes: 31 additions & 0 deletions test/rules/assertions/checkTagNames.js
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,37 @@ export default {
},
],
},
{
code: `
/**
* @constructor
*/
function Test() {
this.works = false;
}
`,
errors: [
{
line: 3,
message: 'Invalid JSDoc tag (preference). Replace "constructor" JSDoc tag with "class".',
},
],
output: `
/**
* @class
*/
function Test() {
this.works = false;
}
`,
settings: {
jsdoc: {
tagNamePreference: {
returns: 'return',
},
},
},
},
],
valid: [
{
Expand Down

0 comments on commit 4f8d242

Please sign in to comment.