This repository has been archived by the owner on Aug 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 656
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #377 from matvp91/jsx-key
Added jsx-key lint rule.
- Loading branch information
Showing
6 changed files
with
326 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
176 changes: 176 additions & 0 deletions
176
packages/@romejs/js-compiler/lint/rules/react/jsxKey.test.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
# `jsxKey.test.ts` | ||
|
||
**DO NOT MODIFY**. This file has been autogenerated. Run `rome test packages/@romejs/js-compiler/lint/rules/react/jsxKey.test.ts --update-snapshots` to update. | ||
|
||
## `jsx key` | ||
|
||
### `0` | ||
|
||
``` | ||
unknown:1:11 lint/jsxKey ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
✖ Missing the "key" prop for element in array | ||
const a = [<div />, <div />] | ||
^^^^^^^ | ||
unknown:1:20 lint/jsxKey ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
✖ Missing the "key" prop for element in array | ||
const a = [<div />, <div />] | ||
^^^^^^^ | ||
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
✖ Found 2 problems | ||
``` | ||
|
||
### `0: formatted` | ||
|
||
``` | ||
const a = [<div />, <div />]; | ||
``` | ||
|
||
### `1` | ||
|
||
``` | ||
unknown:1:26 lint/jsxKey ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
✖ Missing the "key" prop for element in iterator | ||
const a = [1, 2].map(x => <div>{x}</div>); | ||
^^^^^^^^^^^^^^ | ||
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
✖ Found 1 problem | ||
``` | ||
|
||
### `1: formatted` | ||
|
||
``` | ||
const a = [1, 2].map((x) => <div>{x}</div>); | ||
``` | ||
|
||
### `2` | ||
|
||
``` | ||
unknown:2:9 lint/jsxKey ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
✖ Missing the "key" prop for element in iterator | ||
1 │ const a = [1, 2].map(x => { | ||
> 2 │ return <div>{x}</div>; | ||
│ ^^^^^^^^^^^^^^ | ||
3 │ }); | ||
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
✖ Found 1 problem | ||
``` | ||
|
||
### `2: formatted` | ||
|
||
``` | ||
const a = [1, 2].map((x) => { | ||
return <div>{x}</div>; | ||
}); | ||
``` | ||
|
||
### `3` | ||
|
||
``` | ||
unknown:2:9 lint/jsxKey ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
✖ Missing the "key" prop for element in iterator | ||
1 │ const a = [1, 2].map(function(x) { | ||
> 2 │ return <div>{x}</div>; | ||
│ ^^^^^^^^^^^^^^ | ||
3 │ }); | ||
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
✖ Found 1 problem | ||
``` | ||
|
||
### `3: formatted` | ||
|
||
``` | ||
const a = [1, 2].map(function(x) { | ||
return <div>{x}</div>; | ||
}); | ||
``` | ||
|
||
### `4` | ||
|
||
``` | ||
✔ No known problems! | ||
``` | ||
|
||
### `4: formatted` | ||
|
||
``` | ||
const a = [<div key="a" />, <div key={'b'} />]; | ||
``` | ||
|
||
### `5` | ||
|
||
``` | ||
✔ No known problems! | ||
``` | ||
|
||
### `5: formatted` | ||
|
||
``` | ||
const a = [1, 2].map((x) => <div key={x}>{x}</div>); | ||
``` | ||
|
||
### `6` | ||
|
||
``` | ||
✔ No known problems! | ||
``` | ||
|
||
### `6: formatted` | ||
|
||
``` | ||
const a = [1, 2].map((x) => { | ||
return <div key={x}>{x}</div>; | ||
}); | ||
``` | ||
|
||
### `7` | ||
|
||
``` | ||
✔ No known problems! | ||
``` | ||
|
||
### `7: formatted` | ||
|
||
``` | ||
const a = [1, 2].map(function(x) { | ||
return <div key={x}>{x}</div>; | ||
}); | ||
``` |
64 changes: 64 additions & 0 deletions
64
packages/@romejs/js-compiler/lint/rules/react/jsxKey.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import {test} from 'rome'; | ||
import {dedent} from '@romejs/string-utils'; | ||
import {testLintMultiple} from '../testHelpers'; | ||
|
||
test( | ||
'jsx key', | ||
async (t) => { | ||
await testLintMultiple( | ||
t, | ||
[ | ||
// INVALID | ||
'const a = [<div />, <div />]', | ||
dedent( | ||
` | ||
const a = [1, 2].map(x => <div>{x}</div>); | ||
`, | ||
), | ||
dedent( | ||
` | ||
const a = [1, 2].map(x => { | ||
return <div>{x}</div>; | ||
}); | ||
`, | ||
), | ||
dedent( | ||
` | ||
const a = [1, 2].map(function(x) { | ||
return <div>{x}</div>; | ||
}); | ||
`, | ||
), | ||
// VALID | ||
'const a = [<div key="a" />, <div key={"b"} />]', | ||
dedent( | ||
` | ||
const a = [1, 2].map(x => <div key={x}>{x}</div>); | ||
`, | ||
), | ||
dedent( | ||
` | ||
const a = [1, 2].map(x => { | ||
return <div key={x}>{x}</div>; | ||
}); | ||
`, | ||
), | ||
dedent( | ||
` | ||
const a = [1, 2].map(function(x) { | ||
return <div key={x}>{x}</div>; | ||
}); | ||
`, | ||
), | ||
], | ||
{category: 'lint/jsxKey'}, | ||
); | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import {Path} from '@romejs/js-compiler'; | ||
import {AnyNode, JSXElement} from '@romejs/js-ast'; | ||
import {descriptions} from '@romejs/diagnostics'; | ||
|
||
function containsKeyAttr(node: JSXElement): boolean { | ||
const ATTR_NAME = 'key'; | ||
return !!node.attributes.find((attr) => | ||
attr.type === 'JSXAttribute' && attr.name.name === ATTR_NAME | ||
); | ||
} | ||
|
||
export default { | ||
name: 'jsxKey', | ||
enter(path: Path): AnyNode { | ||
const {node, context} = path; | ||
|
||
// JSXElement in array literal | ||
if ( | ||
node.type === 'JSXElement' && | ||
!containsKeyAttr(node) && | ||
path.parentPath.node.type === 'ArrayExpression' | ||
) { | ||
context.addNodeDiagnostic(node, descriptions.LINT.REACT_JSX_KEY('array')); | ||
} | ||
|
||
// Array.prototype.map | ||
if ( | ||
node.type === 'CallExpression' && | ||
node.callee.type === 'MemberExpression' && | ||
node.callee.property.value.type === 'Identifier' && | ||
node.callee.property.value.name === 'map' | ||
) { | ||
const fn = node.arguments[0]; | ||
|
||
// Short hand arrow function | ||
if ( | ||
fn.type === 'ArrowFunctionExpression' && | ||
fn.body.type === 'JSXElement' && | ||
!containsKeyAttr(fn.body) | ||
) { | ||
context.addNodeDiagnostic( | ||
fn.body, | ||
descriptions.LINT.REACT_JSX_KEY('iterator'), | ||
); | ||
} | ||
|
||
// Function or arrow function with block statement | ||
if ( | ||
fn && | ||
(fn.type === 'FunctionExpression' || | ||
fn.type === 'ArrowFunctionExpression') && | ||
fn.body.type === 'BlockStatement' | ||
) { | ||
fn.body.body.forEach((statement) => { | ||
if ( | ||
statement.type === 'ReturnStatement' && | ||
statement.argument?.type === 'JSXElement' && | ||
!containsKeyAttr(statement.argument) | ||
) { | ||
context.addNodeDiagnostic( | ||
statement.argument, | ||
descriptions.LINT.REACT_JSX_KEY('iterator'), | ||
); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
return node; | ||
}, | ||
}; |