Skip to content

Commit

Permalink
feat(check-types): add skipRootChecking option to `preferredTypes…
Browse files Browse the repository at this point in the history
…` setting; fixes #863
  • Loading branch information
brettz9 committed Nov 2, 2022
1 parent 691a414 commit e5da5bb
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .README/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,8 @@ The format of the configuration is as follows:
- a string type to be preferred in its place (and which `fix` mode
can replace)
- `false` (for forbidding the type)
- an optional key `skipRootChecking` (for `check-types`) to allow for this
type in the context of a root (i.e., a parent object of some child type)

Note that the preferred types indicated as targets in
`settings.jsdoc.preferredTypes` map will be assumed to be defined by
Expand Down
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,8 @@ The format of the configuration is as follows:
- a string type to be preferred in its place (and which `fix` mode
can replace)
- `false` (for forbidding the type)
- an optional key `skipRootChecking` (for `check-types`) to allow for this
type in the context of a root (i.e., a parent object of some child type)

Note that the preferred types indicated as targets in
`settings.jsdoc.preferredTypes` map will be assumed to be defined by
Expand Down Expand Up @@ -5737,6 +5739,15 @@ function abc(param) {
return 'abc';
}
// Message: Invalid JSDoc @param "param" type "Object"; prefer: "object".

/**
* @param {object} root
* @param {number} root.a
* @param {object} b
*/
function a () {}
// Settings: {"jsdoc":{"preferredTypes":{"object":{"skipRootChecking":true}}}}
// Message: Invalid JSDoc @param "b" type "object".
````

The following patterns are not considered problems:
Expand Down Expand Up @@ -6055,6 +6066,13 @@ function foo(spec) {
}

foo()

/**
* @param {object} root
* @param {number} root.a
*/
function a () {}
// Settings: {"jsdoc":{"preferredTypes":{"object":{"message":"Won't see this message","skipRootChecking":true}}}}
````


Expand Down
34 changes: 23 additions & 11 deletions src/rules/checkTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,13 +237,15 @@ export default iterateJsdoc(({
* @param {string} type
* @param {string} value
* @param {string} tagName
* @param {string} nameInTag
* @param {number} idx
* @param {string} property
* @param {import('jsdoc-type-pratt-parser/dist/src/index.d.ts').NonTerminalResult} node
* @param {import('jsdoc-type-pratt-parser/dist/src/index.d.ts').NonTerminalResult} parentNode
* @param {string[]} invalidTypes
* @returns {void}
*/
const getInvalidTypes = (type, value, tagName, property, node, parentNode, invalidTypes) => {
const getInvalidTypes = (type, value, tagName, nameInTag, idx, property, node, parentNode, invalidTypes) => {
let typeNodeName = type === 'JsdocTypeAny' ? '*' : value;

const [
Expand All @@ -267,13 +269,17 @@ export default iterateJsdoc(({
invalidTypes.push([
typeNodeName, preferred,
]);
} else if (typeof preferredSetting === 'object') {
preferred = preferredSetting?.replacement;
invalidTypes.push([
typeNodeName,
preferred,
preferredSetting?.message,
]);
} else if (preferredSetting && typeof preferredSetting === 'object') {
const nextItem = preferredSetting.skipRootChecking && jsdocTagsWithPossibleType[idx + 1];

if (!nextItem || !nextItem.name.startsWith(`${nameInTag}.`)) {
preferred = preferredSetting.replacement;
invalidTypes.push([
typeNodeName,
preferred,
preferredSetting.message,
]);
}
} else {
utils.reportSettings(
'Invalid `settings.jsdoc.preferredTypes`. Values must be falsy, a string, or an object.',
Expand Down Expand Up @@ -306,7 +312,10 @@ export default iterateJsdoc(({
}
};

for (const jsdocTag of jsdocTagsWithPossibleType) {
for (const [
idx,
jsdocTag,
] of jsdocTagsWithPossibleType.entries()) {
const invalidTypes = [];
let typeAst;

Expand All @@ -316,7 +325,10 @@ export default iterateJsdoc(({
continue;
}

const tagName = jsdocTag.tag;
const {
tag: tagName,
name: nameInTag,
} = jsdocTag;

traverse(typeAst, (node, parentNode, property) => {
const {
Expand All @@ -329,7 +341,7 @@ export default iterateJsdoc(({
return;
}

getInvalidTypes(type, value, tagName, property, node, parentNode, invalidTypes);
getInvalidTypes(type, value, tagName, nameInTag, idx, property, node, parentNode, invalidTypes);
});

if (invalidTypes.length) {
Expand Down
44 changes: 44 additions & 0 deletions test/rules/assertions/checkTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2396,6 +2396,31 @@ export default {
}
`,
},
{
code: `
/**
* @param {object} root
* @param {number} root.a
* @param {object} b
*/
function a () {}
`,
errors: [
{
line: 5,
message: 'Invalid JSDoc @param "b" type "object".',
},
],
settings: {
jsdoc: {
preferredTypes: {
object: {
skipRootChecking: true,
},
},
},
},
},
],
valid: [
{
Expand Down Expand Up @@ -3057,5 +3082,24 @@ export default {
foo()
`,
},
{
code: `
/**
* @param {object} root
* @param {number} root.a
*/
function a () {}
`,
settings: {
jsdoc: {
preferredTypes: {
object: {
message: 'Won\'t see this message',
skipRootChecking: true,
},
},
},
},
},
],
};

0 comments on commit e5da5bb

Please sign in to comment.