From 6b4273194d54bb88b228b3c9436c93100e5e2320 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonio=20P=C3=A9rez=20Barrero?= Date: Wed, 13 Jul 2022 16:26:07 +0200 Subject: [PATCH] [Fix] `display-name`: fix identifying `_` as a capital letter Fixes #3334 --- CHANGELOG.md | 2 ++ lib/util/isFirstLetterCapitalized.js | 2 +- tests/lib/rules/display-name.js | 18 ++++++++++++++++++ tests/util/isFirstLetterCapitalized.js | 2 ++ 4 files changed, 23 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8743fa5eaf..90bc4e80e5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange * [`jsx-key`]: avoid a crash from optional chaining from [#3320][] ([#3327][] @ljharb) * [`jsx-key`]: avoid a crash on a non-array node.body from [#3320][] ([#3328][] @ljharb) * [`display-name`]: fix false positive for assignment of function returning null ([#3331][] @apbarrero) +* [`display-name`]: fix identifying `_` as a capital letter ([#3335][] @apbarrero) ### Changed * [Refactor] [`jsx-indent-props`]: improved readability of the checkNodesIndent function ([#3315][] @caroline223) @@ -23,6 +24,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange * [Docs] `sort-comp`: add class component examples ([#3339][] @maurer2) [#3339]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3339 +[#3335]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3335 [#3331]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3331 [#3328]: https://github.com/jsx-eslint/eslint-plugin-react/issues/3328 [#3327]: https://github.com/jsx-eslint/eslint-plugin-react/issues/3327 diff --git a/lib/util/isFirstLetterCapitalized.js b/lib/util/isFirstLetterCapitalized.js index f487489b65..819edc8860 100644 --- a/lib/util/isFirstLetterCapitalized.js +++ b/lib/util/isFirstLetterCapitalized.js @@ -6,7 +6,7 @@ * @returns {Boolean} True if first letter is capitalized. */ function isFirstLetterCapitalized(word) { - if (!word) { + if (!word || word.charAt(0) === '_') { return false; } const firstLetter = word.charAt(0); diff --git a/tests/lib/rules/display-name.js b/tests/lib/rules/display-name.js index 39bdb59cb5..ba4b9181fe 100644 --- a/tests/lib/rules/display-name.js +++ b/tests/lib/rules/display-name.js @@ -597,6 +597,24 @@ ruleTester.run('display-name', rule, { return f(a); };`, }, + { + // issue #3334 + code: ` + obj._property = (a) => { + if (a == null) return null; + return f(a); + }; + `, + }, + { + // issue #3334 + code: ` + _variable = (a) => { + if (a == null) return null; + return f(a); + }; + `, + }, { // issue #3303 code: ` diff --git a/tests/util/isFirstLetterCapitalized.js b/tests/util/isFirstLetterCapitalized.js index 5ccbfea68e..85bdffd1bc 100644 --- a/tests/util/isFirstLetterCapitalized.js +++ b/tests/util/isFirstLetterCapitalized.js @@ -14,6 +14,8 @@ describe('isFirstLetterCapitalized', () => { it('should return false for uncapitalized string', () => { assert.equal(isFirstLetterCapitalized('isCapitalized'), false); assert.equal(isFirstLetterCapitalized('lowercase'), false); + assert.equal(isFirstLetterCapitalized('_startsWithUnderscore'), false); + assert.equal(isFirstLetterCapitalized('_StartsWithUnderscore'), false); }); it('should return true for capitalized string', () => {