Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[New] : Enforce no unused React elements
- Loading branch information
1 parent
0e9a193
commit a20dea1
Showing
5 changed files
with
144 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Disallow unused React elements (react/no-unused-elements) | ||
|
||
An unused React element indicates a logic error. | ||
|
||
## Rule Details | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```jsx | ||
<div />; | ||
``` | ||
|
||
```js | ||
React.createElement('div'); | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```js | ||
return <div />; | ||
``` | ||
|
||
```js | ||
const partial = <div />; | ||
``` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/** | ||
* @fileoverview Disallow unused React elements | ||
* @author Duncan Beevers | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const Components = require('../util/Components'); | ||
const docsUrl = require('../util/docsUrl'); | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
const NO_UNUSED_ELEMENTS_MESSAGE = 'noUnusedElements'; | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: 'Disallow unused React elements', | ||
category: 'Best Practices', | ||
recommended: false, | ||
url: docsUrl('no-unused-elements') | ||
}, | ||
messages: { | ||
[NO_UNUSED_ELEMENTS_MESSAGE]: 'Unused React element' | ||
}, | ||
schema: [{ | ||
type: 'object', | ||
additionalProperties: false | ||
}] | ||
}, | ||
|
||
create: Components.detect((context, components, utils) => ({ | ||
JSXElement(node) { | ||
if (node.parent.type === 'ExpressionStatement') { | ||
context.report({ | ||
node, | ||
messageId: NO_UNUSED_ELEMENTS_MESSAGE | ||
}); | ||
} | ||
}, | ||
CallExpression(node) { | ||
if (utils.isCreateElement(node) && node.parent.type === 'ExpressionStatement') { | ||
context.report({ | ||
node, | ||
messageId: NO_UNUSED_ELEMENTS_MESSAGE | ||
}); | ||
} | ||
} | ||
}) | ||
) | ||
}; |
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,63 @@ | ||
/** | ||
* @fileoverview Disallow unused React elements | ||
* @author Duncan Beevers | ||
*/ | ||
|
||
'use strict'; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
const RuleTester = require('eslint').RuleTester; | ||
const rule = require('../../../lib/rules/no-unused-elements'); | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Tests | ||
// ------------------------------------------------------------------------------ | ||
|
||
const ruleTester = new RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 2018, | ||
sourceType: 'module', | ||
ecmaFeatures: { | ||
jsx: true | ||
} | ||
} | ||
}); | ||
|
||
const tests = { | ||
valid: [ | ||
{ | ||
code: 'const partial = <div />' | ||
}, | ||
{ | ||
code: '() => <div />' | ||
}, | ||
{ | ||
code: 'const partial = React.createElement(\'div\', {}, \'\')' | ||
} | ||
], | ||
invalid: [ | ||
{ | ||
code: '<div />', | ||
errors: [{ | ||
message: 'Unused React element' | ||
}] | ||
}, | ||
{ | ||
code: 'React.createElement(\'div\', {}, \'\')', | ||
errors: [{ | ||
message: 'Unused React element' | ||
}] | ||
}, | ||
{ | ||
code: 'if (condition) { <div /> }', | ||
errors: [{ | ||
message: 'Unused React element' | ||
}] | ||
} | ||
] | ||
}; | ||
|
||
ruleTester.run('no-unused-elements', rule, tests); |