From 7dbdd9f271a27a8b370ca0f5053f5c51a98dc671 Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Mon, 28 Mar 2022 17:21:10 +0800 Subject: [PATCH] fix(`check-types`): proper use of optional chaining; fixes #861 --- README.md | 12 +++++++++ src/rules/checkTypes.js | 4 +-- test/rules/assertions/checkTypes.js | 40 +++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a86e0923e..545d55e6a 100644 --- a/README.md +++ b/README.md @@ -5576,6 +5576,18 @@ function quux (foo) { } // Settings: {"jsdoc":{"mode":"typescript"}} // Message: Invalid JSDoc @param "foo" type "object"; prefer: "Object<>". + +/** + * + * @param {Object} param + * @return {Object | String} + */ +function abc(param) { + if (param.a) + return {}; + return 'abc'; +} +// Message: Invalid JSDoc @param "param" type "Object"; prefer: "object". ```` The following patterns are not considered problems: diff --git a/src/rules/checkTypes.js b/src/rules/checkTypes.js index 04483bb31..3a0a931fd 100644 --- a/src/rules/checkTypes.js +++ b/src/rules/checkTypes.js @@ -206,8 +206,8 @@ export default iterateJsdoc(({ // `unifyParentAndChildTypeChecks`) and we don't want // `object<>` given TypeScript issue https://github.com/microsoft/TypeScript/issues/20555 parentNode?.elements.length && ( - parentNode?.left.type === 'JsdocTypeName' && - parentNode?.left.value === 'Object' + parentNode?.left?.type === 'JsdocTypeName' && + parentNode?.left?.value === 'Object' ) ) ) { diff --git a/test/rules/assertions/checkTypes.js b/test/rules/assertions/checkTypes.js index e2503809e..05d393464 100644 --- a/test/rules/assertions/checkTypes.js +++ b/test/rules/assertions/checkTypes.js @@ -2356,6 +2356,46 @@ export default { }, }, }, + { + code: ` + /** + * + * @param {Object} param + * @return {Object | String} + */ + function abc(param) { + if (param.a) + return {}; + return 'abc'; + } + `, + errors: [ + { + line: 4, + message: 'Invalid JSDoc @param "param" type "Object"; prefer: "object".', + }, + { + line: 5, + message: 'Invalid JSDoc @return type "Object"; prefer: "object".', + }, + { + line: 5, + message: 'Invalid JSDoc @return type "String"; prefer: "string".', + }, + ], + output: ` + /** + * + * @param {object} param + * @return {Object | String} + */ + function abc(param) { + if (param.a) + return {}; + return 'abc'; + } + `, + }, ], valid: [ {