Skip to content

Commit 1fc7d34

Browse files
committed
[Fix] display-name, prop-types: when checking for a capitalized name, ignore underscores entirely
Fixes #3560
1 parent 492c34c commit 1fc7d34

File tree

4 files changed

+59
-4
lines changed

4 files changed

+59
-4
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
1616
* [`no-unknown-property`]: allow `onLoad` on `source` (@ljharb)
1717
* [`jsx-first-prop-new-line`]: ensure autofix preserves generics in component name ([#3546][] @ljharb)
1818
* [`no-unknown-property`]: allow `fill` prop on `<symbol>` ([#3555][] @stefanprobst)
19+
* [`display-name`], [`prop-types`]: when checking for a capitalized name, ignore underscores entirely ([#3560][] @ljharb)
1920

21+
[#3560]: https://github.com/jsx-eslint/eslint-plugin-react/issues/3560
2022
[#3555]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3555
2123
[#3548]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3548
2224
[#3546]: https://github.com/jsx-eslint/eslint-plugin-react/issues/3546

lib/util/isFirstLetterCapitalized.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
* @returns {Boolean} True if first letter is capitalized.
77
*/
88
function isFirstLetterCapitalized(word) {
9-
if (!word || word.charAt(0) === '_') {
9+
if (!word) {
1010
return false;
1111
}
12-
const firstLetter = word.charAt(0);
12+
const firstLetter = word.replace(/^_+/, '').charAt(0);
1313
return firstLetter.toUpperCase() === firstLetter;
1414
}
1515

tests/lib/rules/prop-types.js

+49
Original file line numberDiff line numberDiff line change
@@ -8199,6 +8199,55 @@ ruleTester.run('prop-types', rule, {
81998199
data: { name: 'foo' },
82008200
},
82018201
],
8202+
},
8203+
{
8204+
code: `
8205+
function _EventsList({ prop_ }) {
8206+
return (
8207+
<div>
8208+
{prop_.events.map((event) => (
8209+
<Event key={event.id} eventId={event.id} />
8210+
))}
8211+
</div>
8212+
);
8213+
}
8214+
8215+
function $EventsList({ prop$ }) {
8216+
return (
8217+
<div>
8218+
{prop$.events.map((event) => (
8219+
<Event key={event.id} eventId={event.id} />
8220+
))}
8221+
</div>
8222+
);
8223+
}
8224+
`,
8225+
errors: [
8226+
{
8227+
messageId: 'missingPropType',
8228+
data: { name: 'prop_' },
8229+
},
8230+
{
8231+
messageId: 'missingPropType',
8232+
data: { name: 'prop_.events' },
8233+
},
8234+
{
8235+
messageId: 'missingPropType',
8236+
data: { name: 'prop_.events.map' },
8237+
},
8238+
{
8239+
messageId: 'missingPropType',
8240+
data: { name: 'prop$' },
8241+
},
8242+
{
8243+
messageId: 'missingPropType',
8244+
data: { name: 'prop$.events' },
8245+
},
8246+
{
8247+
messageId: 'missingPropType',
8248+
data: { name: 'prop$.events.map' },
8249+
},
8250+
],
82028251
}
82038252
)),
82048253
});

tests/util/isFirstLetterCapitalized.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,15 @@ describe('isFirstLetterCapitalized', () => {
1515
assert.equal(isFirstLetterCapitalized('isCapitalized'), false);
1616
assert.equal(isFirstLetterCapitalized('lowercase'), false);
1717
assert.equal(isFirstLetterCapitalized('_startsWithUnderscore'), false);
18-
assert.equal(isFirstLetterCapitalized('_StartsWithUnderscore'), false);
18+
assert.equal(isFirstLetterCapitalized('__startsWithUnderscore'), false);
1919
});
2020

21-
it('should return true for capitalized string', () => {
21+
it('should return true for capitalized string, with or without leading underscores', () => {
2222
assert.equal(isFirstLetterCapitalized('IsCapitalized'), true);
23+
assert.equal(isFirstLetterCapitalized('_IsCapitalized'), true);
24+
assert.equal(isFirstLetterCapitalized('__IsCapitalized'), true);
2325
assert.equal(isFirstLetterCapitalized('UPPERCASE'), true);
26+
assert.equal(isFirstLetterCapitalized('_UPPERCASE'), true);
27+
assert.equal(isFirstLetterCapitalized('__UPPERCASE'), true);
2428
});
2529
});

0 commit comments

Comments
 (0)