-
Notifications
You must be signed in to change notification settings - Fork 10
add enforce-css-module-default-import rule #266
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
2 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': minor | ||
--- | ||
|
||
Add enforce-css-module-default-import rule |
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,53 @@ | ||
# Enforce CSS Module Default Import (enforce-css-module-default-import) | ||
|
||
CSS Modules should import only the default import. Avoid named imports which can often conflict with other variables (including the function name of the React component) when importing css modules. | ||
|
||
Additionally, for consistency among the codebase the default import should be consistently named. This rule allows enforcing the name of the default import, which by default expects it to be named either `classes` or be suffixed `Classes`. | ||
|
||
## Rule details | ||
|
||
This rule disallows the use of any CSS Module property that does not match the desired casing. | ||
|
||
👎 Examples of **incorrect** code for this rule: | ||
|
||
```jsx | ||
/* eslint primer-react/enforce-css-module-default-import: "error" */ | ||
import {Button} from '@primer/react' | ||
import {Button as ButtonClass} from './some.module.css' // oops! Conflict! | ||
``` | ||
|
||
```jsx | ||
/* eslint primer-react/enforce-css-module-default-import: ["error",{enforceName:"^classes$"}] */ | ||
import {Button} from '@primer/react' | ||
import classnames from './some.module.css' // This default import should match /^classes$/ | ||
``` | ||
|
||
👍 Examples of **correct** code for this rule: | ||
|
||
```jsx | ||
/* eslint primer-react/enforce-css-module-default-import: "error" */ | ||
import {Button} from '@primer/react' | ||
import classes from './some.module.css' | ||
``` | ||
|
||
```jsx | ||
/* eslint primer-react/enforce-css-module-default-import: ["error",{enforceName:"^classes$"}] */ | ||
import {Button} from '@primer/react' | ||
import classes from './some.module.css' | ||
``` | ||
|
||
```jsx | ||
/* eslint primer-react/enforce-css-module-default-import: ["error",{enforceName:"(^classes$|Classes$)"}] */ | ||
import {Button} from '@primer/react' | ||
import classes from './some.module.css' | ||
import sharedClasses from './shared.module.css' | ||
import utilClasses from './util.module.css' | ||
``` | ||
|
||
## Options | ||
|
||
- `enforceName` (default: `.*`) | ||
|
||
By default, the `enforce-css-module-default-import` rule will allow any name for the default export, | ||
however in the _recommended_ ruleset this is set to `(^classes$|Classes$)` meaning that the default | ||
export name must either be exactly `classes` or end in `Classes`, for example `sharedClasses`. |
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
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
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,88 @@ | ||
const rule = require('../enforce-css-module-default-import') | ||
const {RuleTester} = require('eslint') | ||
|
||
const ruleTester = new RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 'latest', | ||
sourceType: 'module', | ||
ecmaFeatures: { | ||
jsx: true, | ||
}, | ||
}, | ||
}) | ||
|
||
ruleTester.run('enforce-css-module-default-import', rule, { | ||
valid: [ | ||
'import classes from "a.module.css";', | ||
'import {default as classes} from "a.module.css";', | ||
'import staticClasses from "a.module.css";', | ||
'import fooClasses from "a.module.css";', | ||
'import whatever from "a.js";', | ||
'import whatever from "a.module.js";', | ||
'import whatever from "a.css";', | ||
'import {whatever} from "a.js";', | ||
'import {whatever} from "a.css";', | ||
{ | ||
code: 'import classes from "a.module.css";', | ||
options: [{enforceName: '(?:^classes$|Classes$)'}], | ||
}, | ||
{ | ||
code: 'import sharedClasses from "a.module.css";', | ||
options: [{enforceName: '(?:^classes$|Classes$)'}], | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
code: 'import {foo} from "a.module.css";', | ||
errors: [ | ||
{ | ||
messageId: 'notDefault', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'import _, {foo} from "a.module.css";', | ||
errors: [ | ||
{ | ||
messageId: 'notDefault', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'import foobar from "a.module.css";', | ||
options: [{enforceName: '(?:^classes$|Classes$)'}], | ||
errors: [ | ||
{ | ||
messageId: 'badName', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'import someclasses from "a.module.css";', | ||
options: [{enforceName: '(?:^classes$|Classes$)'}], | ||
errors: [ | ||
{ | ||
messageId: 'badName', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'import {default as foobar} from "a.module.css";', | ||
options: [{enforceName: '(?:^classes$|Classes$)'}], | ||
errors: [ | ||
{ | ||
messageId: 'badName', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'import {default as someclasses} from "a.module.css";', | ||
options: [{enforceName: '(?:^classes$|Classes$)'}], | ||
errors: [ | ||
{ | ||
messageId: 'badName', | ||
}, | ||
], | ||
}, | ||
], | ||
}) |
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,62 @@ | ||
module.exports = { | ||
meta: { | ||
type: 'suggestion', | ||
fixable: 'code', | ||
schema: [ | ||
{ | ||
properties: { | ||
enforceName: { | ||
type: 'string', | ||
}, | ||
}, | ||
}, | ||
], | ||
messages: { | ||
badName: 'This default import should match {{enforceName}}', | ||
notDefault: 'Class modules should only import the default object.', | ||
noDefault: 'Class modules should always import default.', | ||
}, | ||
}, | ||
create(context) { | ||
const enforceName = new RegExp(context.options[0]?.enforceName || '.*') | ||
return { | ||
['ImportDeclaration>Literal[value=/.module.css$/]']: function (node) { | ||
node = node.parent | ||
const defaultSpecifier = node.specifiers.find(spec => spec.type === 'ImportDefaultSpecifier') | ||
const otherSpecifiers = node.specifiers.filter(spec => spec.type !== 'ImportDefaultSpecifier') | ||
const asDefaultSpecifier = otherSpecifiers.find(spec => spec.imported?.name === 'default') | ||
if (!node.specifiers.length) { | ||
context.report({ | ||
node, | ||
messageId: 'noDefault', | ||
}) | ||
} else if (otherSpecifiers.length === 1 && asDefaultSpecifier) { | ||
if (!enforceName.test(asDefaultSpecifier.local.name)) { | ||
context.report({ | ||
node, | ||
messageId: 'badName', | ||
data: {enforceName}, | ||
}) | ||
} | ||
} else if (otherSpecifiers.length) { | ||
context.report({ | ||
node, | ||
messageId: 'notDefault', | ||
}) | ||
} else if (!defaultSpecifier) { | ||
context.report({ | ||
node, | ||
messageId: 'noDefault', | ||
}) | ||
} | ||
if (defaultSpecifier && !enforceName.test(defaultSpecifier.local.name)) { | ||
context.report({ | ||
node, | ||
messageId: 'badName', | ||
data: {enforceName}, | ||
}) | ||
} | ||
}, | ||
} | ||
}, | ||
} |
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.
.js
→.test.js
: #326 🤝