From 4f8d242f39136722e6386d78e093620ab41fd91c Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Mon, 8 Aug 2022 15:16:02 -0600 Subject: [PATCH] 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 --- README.md | 9 ++++++++ src/jsdocUtils.js | 2 +- test/jsdocUtils.js | 6 +++++ test/rules/assertions/checkTagNames.js | 31 ++++++++++++++++++++++++++ 4 files changed, 47 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3c27af924..f2f88bd03 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/src/jsdocUtils.js b/src/jsdocUtils.js index c7d791a92..ba9e5aec2 100644 --- a/src/jsdocUtils.js +++ b/src/jsdocUtils.js @@ -396,7 +396,7 @@ const getPreferredTagName = ( }), ); - if (name in tagPreferenceFixed) { + if (Object.prototype.hasOwnProperty.call(tagPreferenceFixed, name)) { return tagPreferenceFixed[name]; } diff --git a/test/jsdocUtils.js b/test/jsdocUtils.js index d97aa3b14..86c6fbd11 100644 --- a/test/jsdocUtils.js +++ b/test/jsdocUtils.js @@ -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'); diff --git a/test/rules/assertions/checkTagNames.js b/test/rules/assertions/checkTagNames.js index c709ac050..0cb06fb25 100644 --- a/test/rules/assertions/checkTagNames.js +++ b/test/rules/assertions/checkTagNames.js @@ -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: [ {