Skip to content

Commit

Permalink
feat(no-undefined-types): allow structuredTags types to be auto-d…
Browse files Browse the repository at this point in the history
…efined
  • Loading branch information
brettz9 committed Feb 23, 2021
1 parent 9dad3e0 commit b7198e4
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .README/rules/no-undefined-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ the `valid-types` rule to report parsing errors.
If you define your own tags, you can use `settings.jsdoc.structuredTags`
to indicate that a tag's `name` is "namepath-defining" (and should prevent
reporting on use of that namepath elsewhere) and/or that a tag's `type` is
`false` (and should not be checked for types).
`false` (and should not be checked for types). If the `type` is an array, that
array's items will be considered as defined for the purposes of that tag.

#### Options

Expand Down
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7446,7 +7446,8 @@ the `valid-types` rule to report parsing errors.
If you define your own tags, you can use `settings.jsdoc.structuredTags`
to indicate that a tag's `name` is "namepath-defining" (and should prevent
reporting on use of that namepath elsewhere) and/or that a tag's `type` is
`false` (and should not be checked for types).
`false` (and should not be checked for types). If the `type` is an array, that
array's items will be considered as defined for the purposes of that tag.
<a name="eslint-plugin-jsdoc-rules-no-undefined-types-options-16"></a>
#### Options
Expand Down Expand Up @@ -7618,6 +7619,13 @@ function quux () {}
// Settings: {"jsdoc":{"structuredTags":{"aCustomTag":{"type":true}}}}
// Message: The type 'SomeType' is undefined.
/**
* @aCustomTag {SomeType}
*/
function quux () {}
// Settings: {"jsdoc":{"structuredTags":{"aCustomTag":{"type":["aType","anotherType"]}}}}
// Message: The type 'SomeType' is undefined.
/**
* @namepathDefiner SomeType
*/
Expand Down Expand Up @@ -7964,6 +7972,12 @@ exports.resolve1 = function resolve1(value) {
function quux () {}
// Settings: {"jsdoc":{"structuredTags":{"aCustomTag":{"type":false}}}}
/**
* @aCustomTag {SomeType}
*/
function quux () {}
// Settings: {"jsdoc":{"structuredTags":{"aCustomTag":{"type":["aType","SomeType"]}}}}
/**
* @namepathDefiner SomeType
*/
Expand Down
7 changes: 5 additions & 2 deletions src/rules/noUndefinedTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default iterateJsdoc(({
const {definedTypes = []} = context.options[0] || {};

let definedPreferredTypes = [];
const {preferredTypes, mode} = settings;
const {preferredTypes, structuredTags, mode} = settings;
if (Object.keys(preferredTypes).length) {
definedPreferredTypes = Object.values(preferredTypes).map((preferredType) => {
if (typeof preferredType === 'string') {
Expand Down Expand Up @@ -154,7 +154,10 @@ export default iterateJsdoc(({

traverse(parsedType, ({type, name}) => {
if (type === 'NAME') {
if (!allDefinedTypes.has(name)) {
const structuredTypes = structuredTags[tag.tag]?.type;
if (!allDefinedTypes.has(name) &&
(!Array.isArray(structuredTypes) || !structuredTypes.includes(name))
) {
report(`The type '${name}' is undefined.`, null, tag);
} else if (!extraTypes.includes(name)) {
context.markVariableAsUsed(name);
Expand Down
40 changes: 40 additions & 0 deletions test/rules/assertions/noUndefinedTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,29 @@ export default {
},
},
},
{
code: `
/**
* @aCustomTag {SomeType}
*/
function quux () {}
`,
errors: [
{
line: 3,
message: 'The type \'SomeType\' is undefined.',
},
],
settings: {
jsdoc: {
structuredTags: {
aCustomTag: {
type: ['aType', 'anotherType'],
},
},
},
},
},
{
code: `
/**
Expand Down Expand Up @@ -919,6 +942,23 @@ export default {
},
},
},
{
code: `
/**
* @aCustomTag {SomeType}
*/
function quux () {}
`,
settings: {
jsdoc: {
structuredTags: {
aCustomTag: {
type: ['aType', 'SomeType'],
},
},
},
},
},
{
code: `
/**
Expand Down

0 comments on commit b7198e4

Please sign in to comment.