From 4467db503e38b9356517cf6926d11be544ccf4b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=80=E1=85=B5=E1=86=B7=E1=84=89=E1=85=A1=E1=86=BC?= =?UTF-8?q?=E1=84=83=E1=85=AE?= Date: Sat, 16 Mar 2024 12:54:58 +0900 Subject: [PATCH] [Fix] `boolean-prop-naming`: avoid a crash with a non-TSTypeReference type --- CHANGELOG.md | 2 ++ lib/rules/boolean-prop-naming.js | 6 ++++- tests/lib/rules/boolean-prop-naming.js | 33 ++++++++++++++++++++++++-- 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 394add2dee..03815418ab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,10 +13,12 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange * [`no-unknown-property`]: support `popover`, `popovertarget`, `popovertargetaction` attributes ([#3707][] @ljharb) * [`no-unknown-property`]: only match `data-*` attributes containing `-` ([#3713][] @silverwind) * [`checked-requires-onchange-or-readonly`]: correct options that were behaving opposite ([#3715][] @jaesoekjjang) +* [`boolean-prop-naming`]: avoid a crash with a non-TSTypeReference type ([#3718][] @developer-bandi) ### Changed * [`boolean-prop-naming`]: improve error message (@ljharb) +[#3718]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3718 [#3715]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3715 [#3713]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3713 [#3707]: https://github.com/jsx-eslint/eslint-plugin-react/issues/3707 diff --git a/lib/rules/boolean-prop-naming.js b/lib/rules/boolean-prop-naming.js index 9570b04054..173b154354 100644 --- a/lib/rules/boolean-prop-naming.js +++ b/lib/rules/boolean-prop-naming.js @@ -387,7 +387,11 @@ module.exports = { } else if (annotation.type === 'TSTypeReference') { propType = objectTypeAnnotations.get(annotation.typeName.name); } else if (annotation.type === 'TSIntersectionType') { - propType = flatMap(annotation.types, (type) => objectTypeAnnotations.get(type.typeName.name)); + propType = flatMap(annotation.types, (type) => ( + type.type === 'TSTypeReference' + ? objectTypeAnnotations.get(type.typeName.name) + : type + )); } if (propType) { diff --git a/tests/lib/rules/boolean-prop-naming.js b/tests/lib/rules/boolean-prop-naming.js index 393c04a508..2dec7ed70c 100644 --- a/tests/lib/rules/boolean-prop-naming.js +++ b/tests/lib/rules/boolean-prop-naming.js @@ -1312,10 +1312,10 @@ ruleTester.run('boolean-prop-naming', rule, { code: ` type Props = { enabled: boolean - } + }; type BaseProps = { semi: boolean - } + }; const Hello = (props: Props & BaseProps) =>
; `, @@ -1338,5 +1338,34 @@ ruleTester.run('boolean-prop-naming', rule, { }, ], }, + { + code: ` + type Props = { + enabled: boolean + }; + + const Hello = (props: Props & { + semi: boolean + }) =>
; + `, + options: [{ rule: '^(is|has)[A-Z]([A-Za-z0-9]?)+' }], + features: ['ts', 'no-babel', 'no-ts-old'], + errors: [ + { + messageId: 'patternMismatch', + data: { + propName: 'enabled', + pattern: '^(is|has)[A-Z]([A-Za-z0-9]?)+', + }, + }, + { + messageId: 'patternMismatch', + data: { + propName: 'semi', + pattern: '^(is|has)[A-Z]([A-Za-z0-9]?)+', + }, + }, + ], + }, ]), });