Skip to content

Commit

Permalink
[Fix] jsx-key: avoid a crash with optional chaining
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Aug 28, 2022
1 parent c57ccd1 commit 0771bc1
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange

## Unreleased

### Fixed
* [`jsx-key`]: avoid a crash with optional chaining ([#3371][], @ljharb)

## [7.31.1] - 2022.08.26

### Fixed
* [`jsx-key`]: fix detecting missing key in `Array.from`'s mapping function ([#3369][] @sjarva)
* [`jsx-no-leaked-render`]: coerce strategy now allows a ternary ([#3370][], @sjarva)

[7.31.1]: https://github.com/jsx-eslint/eslint-plugin-react/compare/v7.31.0...v7.31.1
[#3371]: https://github.com/jsx-eslint/eslint-plugin-react/issues/3371
[#3370]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3370
[#3369]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3369

Expand Down
5 changes: 2 additions & 3 deletions lib/rules/jsx-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,8 @@ module.exports = {
CallExpression[callee.type="OptionalMemberExpression"][callee.property.name="map"],\
OptionalCallExpression[callee.type="MemberExpression"][callee.property.name="map"],\
OptionalCallExpression[callee.type="OptionalMemberExpression"][callee.property.name="map"]'(node) {
const fn = node.arguments[0];
if (!astUtil.isFunctionLikeExpression(fn)) {
const fn = node.arguments.length > 0 && node.arguments[0];
if (!fn || !astUtil.isFunctionLikeExpression(fn)) {
return;
}

Expand All @@ -239,7 +239,6 @@ module.exports = {
// Array.from
'CallExpression[callee.type="MemberExpression"][callee.property.name="from"]'(node) {
const fn = node.arguments.length > 1 && node.arguments[1];

if (!astUtil.isFunctionLikeExpression(fn)) {
return;
}
Expand Down
6 changes: 5 additions & 1 deletion tests/helpers/parsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ function minEcmaVersion(features, parserOptions) {
const minEcmaVersionForFeatures = {
'class fields': 2022,
'optional chaining': 2020,
'nullish coalescing': 2020,
};
const result = Math.max.apply(
Math,
Expand Down Expand Up @@ -135,7 +136,10 @@ const parsers = {
|| (features.has('fragment') && semver.satisfies(version, '< 5'));

const skipBabel = features.has('no-babel');
const skipOldBabel = skipBabel || features.has('no-babel-old') || semver.satisfies(version, '>= 8');
const skipOldBabel = skipBabel
|| features.has('no-babel-old')
|| features.has('optional chaining')
|| semver.satisfies(version, '>= 8');
const skipNewBabel = skipBabel
|| features.has('no-babel-new')
|| !semver.satisfies(version, '^7.5.0') // require('@babel/eslint-parser/package.json').peerDependencies.eslint
Expand Down
16 changes: 13 additions & 3 deletions tests/lib/rules/jsx-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ ruleTester.run('jsx-key', rule, {
import './ResourceVideo.sass';
import VimeoVideoPlayInModal from '../vimeoVideoPlayInModal/VimeoVideoPlayInModal';
type Props = {
videoUrl: string;
videoTitle: string;
Expand All @@ -115,7 +115,7 @@ ruleTester.run('jsx-key', rule, {
</div>
);
};
export default ResourceVideo;
`,
features: ['types'],
Expand All @@ -125,7 +125,7 @@ ruleTester.run('jsx-key', rule, {
// testrule.jsx
const trackLink = () => {};
const getAnalyticsUiElement = () => {};
const onTextButtonClick = (e, item) => trackLink([, getAnalyticsUiElement(item), item.name], e);
`,
},
Expand Down Expand Up @@ -153,6 +153,16 @@ ruleTester.run('jsx-key', rule, {
`,
features: ['optional chaining'],
},
{
code: `
const baz = foo?.bar?.()?.[1] ?? 'qux';
qux()?.map()
const directiveRanges = comments?.map(tryParseTSDirective)
`,
features: ['optional chaining', 'nullish coalescing'],
},
]),
invalid: parsers.all([
{
Expand Down

0 comments on commit 0771bc1

Please sign in to comment.