-
Notifications
You must be signed in to change notification settings - Fork 10
add enforce-css-module-identifier-casing rule #258
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
keithamus
merged 4 commits into
main
from
add-enforce-css-module-identifier-casing-rule
Oct 24, 2024
Merged
Changes from all commits
Commits
Show all changes
4 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-identifier-casing 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,39 @@ | ||
# Enforce CSS Module Identifier Casing (enforce-css-module-identifier-casing) | ||
|
||
CSS Modules should expose class names written in PascalCase. | ||
|
||
## 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-identifier-casing: "error" */ | ||
import {Button} from '@primer/react' | ||
import classes from './some.module.css' | ||
|
||
<Button className={classes.button} /> | ||
<Button className={classes['button']} /> | ||
<Button className={clsx(classes.button)} /> | ||
|
||
let ButtonClass = "button" | ||
<Button className={clsx(classes[ButtonClass])} /> | ||
``` | ||
|
||
👍 Examples of **correct** code for this rule: | ||
|
||
```jsx | ||
/* eslint primer-react/enforce-css-module-identifier-casing: "error" */ | ||
import {Button} from '@primer/react' | ||
import classes from './some.module.css' | ||
;<Button className={classes.Button} /> | ||
``` | ||
|
||
## Options | ||
|
||
- `casing` (default: `'pascal'`) | ||
|
||
By default, the `enforce-css-module-identifier-casing` rule will check for identifiers matching PascalCase. | ||
Changing this to `'camel'` will instead enforce camelCasing rules. Changing this to `'kebab'` will instead | ||
enforce kebab-casing rules. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
99 changes: 99 additions & 0 deletions
99
src/rules/__tests__/enforce-css-module-identifier-casing.test.js
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,99 @@ | ||
const rule = require('../enforce-css-module-identifier-casing') | ||
const {RuleTester} = require('eslint') | ||
|
||
const ruleTester = new RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 'latest', | ||
sourceType: 'module', | ||
ecmaFeatures: { | ||
jsx: true, | ||
}, | ||
}, | ||
}) | ||
|
||
ruleTester.run('enforce-css-module-identifier-casing', rule, { | ||
valid: [ | ||
'import classes from "a.module.css"; function Foo() { return <Box className={classes.Foo}/> }', | ||
'import classes from "a.module.css"; function Foo() { return <Box className={clsx(classes.Foo)}/> }', | ||
'import classes from "a.module.css"; function Foo() { return <Box className={clsx(className, classes.Foo)}/> }', | ||
'import classes from "a.module.css"; function Foo() { return <Box className={`${classes.Foo}`}/> }', | ||
'import classes from "a.module.css"; function Foo() { return <Box className={`${classes["Foo"]}`}/> }', | ||
'import classes from "a.module.css"; let x = "Foo"; function Foo() { return <Box className={`${classes[x]}`}/> }', | ||
], | ||
invalid: [ | ||
{ | ||
code: 'import classes from "a.module.css"; function Foo() { return <Box className={classes.foo}/> }', | ||
errors: [ | ||
{ | ||
messageId: 'pascal', | ||
data: {name: 'foo'}, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'import classes from "a.module.css"; function Foo() { return <Box className={clsx(classes.foo)}/> }', | ||
errors: [ | ||
{ | ||
messageId: 'pascal', | ||
data: {name: 'foo'}, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'import classes from "a.module.css"; function Foo() { return <Box className={clsx(className, classes.foo)}/> }', | ||
errors: [ | ||
{ | ||
messageId: 'pascal', | ||
data: {name: 'foo'}, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'import classes from "a.module.css"; function Foo() { return <Box className={`${classes.foo}`}/> }', | ||
errors: [ | ||
{ | ||
messageId: 'pascal', | ||
data: {name: 'foo'}, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'import classes from "a.module.css"; function Foo() { return <Box className={classes["foo"]}/> }', | ||
errors: [ | ||
{ | ||
messageId: 'pascal', | ||
data: {name: 'foo'}, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'import classes from "a.module.css"; function Foo() { return <Box className={classes.Foo}/> }', | ||
options: [{casing: 'camel'}], | ||
errors: [ | ||
{ | ||
messageId: 'camel', | ||
data: {name: 'Foo'}, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'import classes from "a.module.css"; let FooClass = "foo"; function Foo() { return <Box className={classes[FooClass]}/> }', | ||
errors: [ | ||
{ | ||
messageId: 'pascal', | ||
data: {name: 'foo'}, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'import classes from "a.module.css"; function Foo() { return <Box className={classes[x]}/> }', | ||
options: [{casing: 'camel'}], | ||
errors: [ | ||
{ | ||
messageId: 'bad', | ||
data: {type: 'Identifier'}, | ||
}, | ||
], | ||
}, | ||
], | ||
}) |
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,76 @@ | ||
const {availableCasings, casingMatches} = require('../utils/casing-matches') | ||
const {identifierIsCSSModuleBinding} = require('../utils/css-modules') | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'suggestion', | ||
fixable: 'code', | ||
schema: [ | ||
{ | ||
properties: { | ||
casing: { | ||
enum: availableCasings, | ||
}, | ||
}, | ||
}, | ||
], | ||
messages: { | ||
bad: 'Class names should be in a recognisable case, and either an identifier or literal, saw: {{ type }}', | ||
camel: 'Class names should be camelCase in both CSS and JS, saw: {{ name }}', | ||
pascal: 'Class names should be PascalCase in both CSS and JS, saw: {{ name }}', | ||
kebab: 'Class names should be kebab-case in both CSS and JS, saw: {{ name }}', | ||
}, | ||
}, | ||
create(context) { | ||
const casing = context.options[0]?.casing || 'pascal' | ||
return { | ||
['JSXAttribute[name.name="className"] JSXExpressionContainer MemberExpression[object.type="Identifier"]']: | ||
function (node) { | ||
if (!identifierIsCSSModuleBinding(node.object, context)) return | ||
if (!node.computed && node.property?.type === 'Identifier') { | ||
if (!casingMatches(node.property.name || '', casing)) { | ||
context.report({ | ||
node: node.property, | ||
messageId: casing, | ||
data: {name: node.property.name}, | ||
}) | ||
} | ||
} else if (node.property?.type === 'Literal') { | ||
if (!casingMatches(node.property.value || '', casing)) { | ||
context.report({ | ||
node: node.property, | ||
messageId: casing, | ||
data: {name: node.property.value}, | ||
}) | ||
} | ||
} else if (node.computed) { | ||
const ref = context | ||
.getScope() | ||
.references.find(reference => reference.identifier.name === node.property.name) | ||
const def = ref.resolved?.defs?.[0] | ||
if (def?.node?.init?.type === 'Literal') { | ||
if (!casingMatches(def.node.init.value || '', casing)) { | ||
context.report({ | ||
node: node.property, | ||
messageId: casing, | ||
data: {name: def.node.init.value}, | ||
}) | ||
} | ||
} else { | ||
context.report({ | ||
node: node.property, | ||
messageId: 'bad', | ||
data: {type: node.property.type}, | ||
}) | ||
} | ||
} else { | ||
context.report({ | ||
node: node.property, | ||
messageId: 'bad', | ||
data: {type: node.property.type}, | ||
}) | ||
} | ||
}, | ||
} | ||
}, | ||
} |
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,19 @@ | ||
const camelReg = /^[a-z]+(?:[A-Z0-9][a-z0-9]+)*?$/ | ||
const pascalReg = /^(?:[A-Z0-9][a-z0-9]+)+?$/ | ||
|
||
const kebabReg = /^[a-z]+(?:-[a-z0-9]+)*?$/ | ||
|
||
function casingMatches(name, type) { | ||
switch (type) { | ||
case 'camel': | ||
return camelReg.test(name) | ||
|
||
case 'pascal': | ||
return pascalReg.test(name) | ||
|
||
case 'kebab': | ||
return kebabReg.test(name) | ||
default: | ||
throw new Error(`Invalid case type ${type}`) | ||
} | ||
} | ||
exports.casingMatches = casingMatches | ||
|
||
exports.availableCasings = ['camel', 'pascal', 'kebab'] |
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,15 @@ | ||
function importBindingIsFromCSSModuleImport(node) { | ||
return node.type === 'ImportBinding' && node.parent?.source?.value?.endsWith('.module.css') | ||
} | ||
|
||
function identifierIsCSSModuleBinding(node, context) { | ||
if (node.type !== 'Identifier') return false | ||
const ref = context.getScope().references.find(reference => reference.identifier.name === node.name) | ||
if (ref.resolved?.defs?.some(importBindingIsFromCSSModuleImport)) { | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
exports.importBindingIsFromCSSModuleImport = importBindingIsFromCSSModuleImport | ||
exports.identifierIsCSSModuleBinding = identifierIsCSSModuleBinding |
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.
Uh oh!
There was an error while loading. Please reload this page.