Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(jsx-key): update error message and docs #2367

Merged
merged 1 commit into from Aug 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions docs/rules/jsx-key.md
Expand Up @@ -27,10 +27,32 @@ data.map((x, i) => <Hello key={i}>{x}</Hello>);
<Hello key={id} {...{ id, caption }} />
```

## Rule Options

```js
...
"react/jsx-key": [<enabled>, { "checkFragmentShorthand": <boolean> }]
...
```

### `checkFragmentShorthand` (default: `false`)

When `true` the rule will check if usage of the [shorthand fragment syntax][short_syntax] requires a key. This option was added to avoid a breaking change and will be the default in the next major version.

The following patterns are considered warnings:

```jsx
[<></>, <></>, <></>];

data.map(x => <>{x}</>);
```

## When not to use

If you are not using JSX then you can disable this rule.

Also, if you have some prevalent situation where you use arrow functions to
return JSX that will not be held in an iterable, you may want to disable this
rule.

[short_syntax]: https://reactjs.org/docs/fragments.html#short-syntax
7 changes: 5 additions & 2 deletions lib/rules/jsx-key.js
Expand Up @@ -7,6 +7,7 @@

const hasProp = require('jsx-ast-utils/hasProp');
const docsUrl = require('../util/docsUrl');
const pragmaUtil = require('../util/pragma');


// ------------------------------------------------------------------------------
Expand Down Expand Up @@ -40,6 +41,8 @@ module.exports = {
create(context) {
const options = Object.assign({}, defaultOptions, context.options[0]);
const checkFragmentShorthand = options.checkFragmentShorthand;
const reactPragma = pragmaUtil.getFromContext(context);
const fragmentPragma = pragmaUtil.getFragmentFromContext(context);

function checkIteratorElement(node) {
if (node.type === 'JSXElement' && !hasProp(node.openingElement.attributes, 'key')) {
Expand All @@ -50,7 +53,7 @@ module.exports = {
} else if (checkFragmentShorthand && node.type === 'JSXFragment') {
context.report({
node,
message: 'Missing "key" prop for element in iterator. Shorthand fragment syntax does support providing keys'
message: `Missing "key" prop for element in iterator. Shorthand fragment syntax does not support providing keys. Use ${reactPragma}.${fragmentPragma} instead`
});
}
}
Expand Down Expand Up @@ -81,7 +84,7 @@ module.exports = {
if (node.parent.type === 'ArrayExpression') {
context.report({
node,
message: 'Missing "key" prop for element in array. Shorthand fragment syntax does support providing keys'
message: `Missing "key" prop for element in array. Shorthand fragment syntax does not support providing keys. Use ${reactPragma}.${fragmentPragma} instead`
});
}
},
Expand Down
13 changes: 11 additions & 2 deletions tests/lib/rules/jsx-key.js
Expand Up @@ -22,6 +22,13 @@ const parserOptions = {
}
};

const settings = {
react: {
pragma: 'Act',
fragment: 'Frag'
}
};

// ------------------------------------------------------------------------------
// Tests
// ------------------------------------------------------------------------------
Expand Down Expand Up @@ -65,11 +72,13 @@ ruleTester.run('jsx-key', rule, {
code: '[1, 2, 3].map(x => <>{x}</>);',
parser: parsers.BABEL_ESLINT,
options: [{checkFragmentShorthand: true}],
errors: [{message: 'Missing "key" prop for element in iterator. Shorthand fragment syntax does support providing keys'}]
settings,
errors: [{message: 'Missing "key" prop for element in iterator. Shorthand fragment syntax does not support providing keys. Use Act.Frag instead'}]
}, {
code: '[<></>];',
parser: parsers.BABEL_ESLINT,
options: [{checkFragmentShorthand: true}],
errors: [{message: 'Missing "key" prop for element in array. Shorthand fragment syntax does support providing keys'}]
settings,
errors: [{message: 'Missing "key" prop for element in array. Shorthand fragment syntax does not support providing keys. Use Act.Frag instead'}]
}]
});