-
Notifications
You must be signed in to change notification settings - Fork 10
Add no-unnecessary-components
#207
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,5 @@ | ||
--- | ||
'eslint-plugin-primer-react': major | ||
--- | ||
|
||
[Breaking] Adds `no-unnecessary-components` lint rule and enables it by default. This may raise new (typically autofixable) lint errors in existing codebases. | ||
This file contains hidden or 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,69 @@ | ||
# Disallow unnecessary use of `Box` and `Text` components (no-unnecessary-components) | ||
|
||
🔧 The `--fix` option on the [ESLint CLI](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) can | ||
automatically fix some of the problems reported by this rule. | ||
|
||
## Rule details | ||
|
||
The [`Box`](https://primer.style/components/box) and [`Text`](https://primer.style/components/text) | ||
Primer React components are utilities that exist solely to provide access to `sx` or styled-system | ||
props. | ||
|
||
If these props are not being used, plain HTML element provide better performance, simpler code, | ||
and improved linting support. | ||
|
||
This rule is auto-fixable in nearly all cases. Autofixing respects the presence of an `as` prop. | ||
|
||
👎 Examples of **incorrect** code for this rule: | ||
|
||
```jsx | ||
/* eslint primer-react/no-unnecessary-components: "error" */ | ||
import {Box, Text} from '@primer/react' | ||
|
||
<Box>Content</Box> | ||
<Box style={{padding: '16px'}} className="danger-box">Content</Box> | ||
<Box as="section">Content</Box> | ||
|
||
<Text>Content</Text> | ||
<Text style={{fontSize: '24px'}} className="large-text">Content</Text> | ||
<Text as="p">Content</Text> | ||
``` | ||
|
||
👍 Examples of **correct** code for this rule: | ||
|
||
```jsx | ||
/* eslint primer-react/no-system-props: "error" */ | ||
import {Box, Text} from '@primer/react' | ||
|
||
// Prefer plain HTML elements (autofixable) | ||
<div>Content</div> | ||
<div style={{padding: '16px'}} className="danger-box">Content</div> | ||
<section>Content</section> | ||
|
||
<span>Content</span> | ||
<span style={{fontSize: '24px'}} className="large-text">Content</span> | ||
<p>Content</p> | ||
|
||
// sx props are allowed | ||
<Box sx={{p: 2}}>Content</Box> | ||
<Text sx={{mt: 2}} as="p">Content</Text> | ||
|
||
// styled-system props are allowed | ||
<Box p={2}>Content</Box> | ||
<Text mt={2} as="p">Content</Text> | ||
``` | ||
|
||
```jsx | ||
/* eslint primer-react/no-system-props: ["error", {skipImportCheck: false}] */ | ||
import {Box, Text} from '@primer/brand' | ||
|
||
// Other components with the same name are allowed | ||
<Box>Content</Box> | ||
<Text>Content</Text> | ||
``` | ||
|
||
## Options | ||
|
||
- `skipImportCheck` (default: `false`) | ||
|
||
By default, the rule will only check for incorrect uses of `Box` and `Text` components that are are imported from `@primer/react`. You can disable this behavior (checking all components with these names regardless of import source) by setting `skipImportCheck` to `true`. |
This file contains hidden or 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,7 @@ | ||
// @ts-check | ||
|
||
/** @type {import('jest').Config} **/ | ||
module.exports = { | ||
testEnvironment: 'node', | ||
testMatch: ['**/__tests__/*.test.js'], | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that I've marked this as a major change because the new rule is enabled by default, which may raise new lint errors in existing code.