From ef7b29451277e06a79bc24510bf27a6e90b8a176 Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Sun, 12 Jul 2020 17:10:58 +0800 Subject: [PATCH] fix(`check-types`, `no-undefined-types`, `valid-types`): In jsdoc mode, ensure `this` only checked for namepath; in TypeScript or Closure, ensure `this` checked only for type --- src/iterateJsdoc.js | 2 +- src/jsdocUtils.js | 43 ++++++++++++++++++++++++++++++++++++++----- 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/src/iterateJsdoc.js b/src/iterateJsdoc.js index 53a52406e..07830e70d 100644 --- a/src/iterateJsdoc.js +++ b/src/iterateJsdoc.js @@ -272,7 +272,7 @@ const getUtils = ( }; utils.tagMustHaveEitherTypeOrNamePosition = (tagName) => { - return jsdocUtils.tagMustHaveEitherTypeOrNamePosition(tagName); + return jsdocUtils.tagMustHaveEitherTypeOrNamePosition(mode, tagName); }; utils.tagMightHaveEitherTypeOrNamePosition = (tagName) => { diff --git a/src/jsdocUtils.js b/src/jsdocUtils.js index 68c5fffd8..a287a41b8 100644 --- a/src/jsdocUtils.js +++ b/src/jsdocUtils.js @@ -324,9 +324,13 @@ const tagsWithMandatoryTypePosition = new Set([ 'type', ]); -const tagsWithMandatoryTypePositionClosure = new Set([ +const tagsWithMandatoryTypePositionTypeScript = new Set([ ...tagsWithMandatoryTypePosition, 'this', +]); + +const tagsWithMandatoryTypePositionClosure = new Set([ + ...tagsWithMandatoryTypePositionTypeScript, 'define', ]); @@ -445,7 +449,6 @@ const tagsWithOptionalNamePositionBase = new Set([ 'alias', 'augments', 'extends', 'lends', - 'this', // Signature seems to require a "namepath" (and no counter-examples), // though it allows an incomplete namepath ending with connecting symbol @@ -461,6 +464,9 @@ const tagsWithOptionalNamePositionBase = new Set([ // The following do not seem to allow curly brackets in their doc // signature or examples (besides `modifies` and `param`) const tagsWithOptionalNamePosition = new Set([ + // Signature seems to require a "namepath" (and no counter-examples) + // Not used with namepath in Closure/TypeScript, however + 'this', ...namepathDefiningTags, ...tagsWithOptionalNamePositionBase, ]); @@ -500,7 +506,7 @@ const tagsWithMandatoryNamePosition = new Set([ 'typedef', ]); -const tagsWithMandatoryTypeOrNamePosition = new Set([ +const tagsWithMandatoryTypeOrNamePositionBase = new Set([ // "namepath" 'alias', 'augments', 'extends', @@ -508,7 +514,6 @@ const tagsWithMandatoryTypeOrNamePosition = new Set([ 'lends', 'memberof', 'memberof!', 'name', - 'this', 'typedef', 'external', 'host', @@ -517,6 +522,20 @@ const tagsWithMandatoryTypeOrNamePosition = new Set([ 'mixes', ]); +const tagsWithMandatoryTypeOrNamePosition = new Set([ + // namepath + 'this', + ...tagsWithMandatoryTypeOrNamePositionBase, +]); + +const tagsWithMandatoryTypeOrNamePositionTypescript = new Set([ + ...tagsWithMandatoryTypeOrNamePositionBase, +]); + +const tagsWithMandatoryTypeOrNamePositionClosure = new Set([ + ...tagsWithMandatoryTypeOrNamePositionBase, +]); + const isNamepathDefiningTag = (mode, tagName) => { if (mode === 'closure') { return closureNamepathDefiningTags.has(tagName); @@ -533,6 +552,10 @@ const tagMightHaveTypePosition = (mode, tag) => { return tagsWithMandatoryTypePositionClosure.has(tag) || tagsWithOptionalTypePositionClosure.has(tag); } + if (mode === 'typescript') { + return tagsWithMandatoryTypePositionTypeScript.has(tag) || + tagsWithOptionalTypePositionTypescript.has(tag); + } return tagsWithMandatoryTypePosition.has(tag) || tagsWithOptionalTypePosition.has(tag); @@ -542,6 +565,9 @@ const tagMustHaveTypePosition = (mode, tag) => { if (mode === 'closure') { return tagsWithMandatoryTypePositionClosure.has(tag); } + if (mode === 'typescript') { + return tagsWithMandatoryTypePositionTypeScript.has(tag); + } return tagsWithMandatoryTypePosition.has(tag); }; @@ -565,7 +591,14 @@ const tagMightHaveEitherTypeOrNamePosition = (mode, tag) => { return tagMightHaveTypePosition(mode, tag) || tagMightHaveNamePosition(mode, tag); }; -const tagMustHaveEitherTypeOrNamePosition = (tag) => { +const tagMustHaveEitherTypeOrNamePosition = (mode, tag) => { + if (mode === 'closure') { + return tagsWithMandatoryTypeOrNamePositionClosure.has(tag); + } + if (mode === 'typescript') { + return tagsWithMandatoryTypeOrNamePositionTypescript.has(tag); + } + return tagsWithMandatoryTypeOrNamePosition.has(tag); };