Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(check-tag-names): work with the constructor tag #899

Merged
merged 2 commits into from
Aug 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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');
Copy link
Contributor Author

@tschaub tschaub Aug 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without the src change, this test fails with expected [Function Object] to equal 'class'.

});
});
it('works with tags that clash with prototype properties', () => {
expect(jsdocUtils.getPreferredTagName({}, 'jsdoc', 'toString')).to.equal('toString');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without the src change, this test fails with expected [Function 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