Skip to content

Commit

Permalink
extend enforce-css-module-identifier-casing to support destructured i…
Browse files Browse the repository at this point in the history
…mports (#265)
  • Loading branch information
keithamus authored Oct 24, 2024
1 parent 83f29f3 commit 650bbca
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
10 changes: 10 additions & 0 deletions src/rules/__tests__/enforce-css-module-identifier-casing.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ ruleTester.run('enforce-css-module-identifier-casing', rule, {
'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]}`}/> }',
'import {Foo} from "a.module.css"; function Bar() { return <Box className={Foo}/> }',
],
invalid: [
{
Expand All @@ -30,6 +31,15 @@ ruleTester.run('enforce-css-module-identifier-casing', rule, {
},
],
},
{
code: 'import {foo} from "a.module.css"; function Bar() { return <Box className={foo}/> }',
errors: [
{
messageId: 'pascal',
data: {name: 'foo'},
},
],
},
{
code: 'import classes from "a.module.css"; function Foo() { return <Box className={clsx(classes.foo)}/> }',
errors: [
Expand Down
14 changes: 12 additions & 2 deletions src/rules/enforce-css-module-identifier-casing.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ module.exports = {
create(context) {
const casing = context.options[0]?.casing || 'pascal'
return {
['JSXAttribute[name.name="className"] JSXExpressionContainer>Identifier']: function (node) {
if (!identifierIsCSSModuleBinding(node, context)) return
if (!casingMatches(node.name || '', casing)) {
context.report({
node,
messageId: casing,
data: {name: node.name},
})
}
},
['JSXAttribute[name.name="className"] JSXExpressionContainer MemberExpression[object.type="Identifier"]']:
function (node) {
if (!identifierIsCSSModuleBinding(node.object, context)) return
Expand All @@ -44,8 +54,8 @@ module.exports = {
})
}
} else if (node.computed) {
const ref = context
.getScope()
const ref = context.sourceCode
.getScope(node)
.references.find(reference => reference.identifier.name === node.property.name)
const def = ref.resolved?.defs?.[0]
if (def?.node?.init?.type === 'Literal') {
Expand Down
4 changes: 2 additions & 2 deletions src/utils/css-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ function importBindingIsFromCSSModuleImport(node) {

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)) {
const ref = context.sourceCode.getScope(node).references.find(reference => reference.identifier.name === node.name)
if (ref && ref.resolved?.defs?.some(importBindingIsFromCSSModuleImport)) {
return true
}
return false
Expand Down

0 comments on commit 650bbca

Please sign in to comment.