-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
There are a few edge cases that I decided to punt on for now. I tried to note as many as I could think of in the documentation and in code comments. Fixes #2.
- Loading branch information
Showing
8 changed files
with
544 additions
and
2 deletions.
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,52 @@ | ||
# Disallow unused styles | ||
|
||
## Rule Details | ||
|
||
The following patterns are considered warnings: | ||
|
||
``` jsx | ||
function MyComponent({ styles }) { | ||
return ( | ||
<div {...css(styles.foo)}> | ||
Foo | ||
</div> | ||
); | ||
} | ||
|
||
export default withStyles(() => ({ | ||
foo: { | ||
backgroundColor: 'red', | ||
}, | ||
|
||
bar: { // <--- this style is not used | ||
backgroundColor: 'green', | ||
}, | ||
}))(MyComponent); | ||
``` | ||
|
||
The following patterns are not warnings: | ||
|
||
``` jsx | ||
function MyComponent({ styles }) { | ||
return ( | ||
<div {...css(styles.foo)}> | ||
Foo | ||
</div> | ||
); | ||
} | ||
|
||
export default withStyles(() => ({ | ||
foo: { | ||
backgroundColor: 'red', | ||
}, | ||
}))(MyComponent); | ||
``` | ||
|
||
## Known limitations | ||
|
||
- Will not detect styles defined by computed properties. | ||
- Will not detect styles defined by object spread. | ||
- Will not detect styles used by Literal reference (e.g. `styles['foo']` or | ||
`props['styles'].foo`). | ||
- Will not handle files that contain multiple styled components very well. | ||
- Will not handle `styles` prop that has been renamed to something else. |
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
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,143 @@ | ||
'use strict'; | ||
|
||
const has = require('has'); | ||
|
||
const findImportCSSFromWithStylesImportDeclaration = require('../util/findImportCSSFromWithStylesImportDeclaration'); | ||
const findRequireCSSFromWithStylesCallExpression = require('../util/findRequireCSSFromWithStylesCallExpression'); | ||
|
||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: 'Require that all styles that are defined are also referenced in the same file', | ||
recommended: false, // TODO add to recommended for next major release | ||
}, | ||
|
||
schema: [], | ||
}, | ||
|
||
create: function rule(context) { | ||
// If `css()` is imported by this file, we want to store what it is imported | ||
// as in this variable so we can verify where it is used. | ||
let cssFromWithStylesName; | ||
|
||
const usedStyles = {}; | ||
const definedStyles = {}; | ||
|
||
function isCSSFound() { | ||
return !!cssFromWithStylesName; | ||
} | ||
|
||
return { | ||
CallExpression(node) { | ||
const found = findRequireCSSFromWithStylesCallExpression(node); | ||
if (found !== null) { | ||
cssFromWithStylesName = found; | ||
} | ||
|
||
// foo() | ||
if (!isCSSFound()) { | ||
// We are not importing `css` from withStyles, so we have no work to | ||
// do here. | ||
return; | ||
} | ||
|
||
if (node.callee.name === 'withStyles') { | ||
const styles = node.arguments[0]; | ||
|
||
if (styles && styles.type === 'ArrowFunctionExpression') { | ||
const body = styles.body; | ||
|
||
let stylesObj; | ||
if (body.type === 'ObjectExpression') { | ||
stylesObj = body; | ||
} else if (body.type === 'BlockStatement') { | ||
body.body.forEach((bodyNode) => { | ||
if ( | ||
bodyNode.type === 'ReturnStatement' && | ||
bodyNode.argument.type === 'ObjectExpression' | ||
) { | ||
stylesObj = bodyNode.argument; | ||
} | ||
}); | ||
} | ||
|
||
if (stylesObj) { | ||
stylesObj.properties.forEach((property) => { | ||
if (property.computed) { | ||
// Skip over computed properties for now. | ||
// e.g. `{ [foo]: { ... } }` | ||
// TODO handle this better? | ||
return; | ||
} | ||
|
||
if (property.type === 'ExperimentalSpreadProperty') { | ||
// Skip over object spread for now. | ||
// e.g. `{ ...foo }` | ||
// TODO handle this better? | ||
return; | ||
} | ||
|
||
definedStyles[property.key.name] = property; | ||
}); | ||
} | ||
} | ||
|
||
// Now we know all of the defined styles and used styles, so we can | ||
// see if there are any defined styles that are not used. | ||
Object.keys(definedStyles).forEach((definedStyleKey) => { | ||
if (!has(usedStyles, definedStyleKey)) { | ||
context.report( | ||
definedStyles[definedStyleKey], | ||
`Style \`${definedStyleKey}\` is unused` | ||
); | ||
} | ||
}); | ||
} | ||
}, | ||
|
||
MemberExpression(node) { | ||
if (!isCSSFound()) { | ||
// We are not importing `css` from withStyles, so we have no work to | ||
// do here. | ||
return; | ||
} | ||
|
||
if (node.object.type === 'Identifier' && node.object.name === 'styles') { | ||
usedStyles[node.property.name] = true; | ||
return; | ||
} | ||
|
||
if (node.property.type === 'Identifier' && node.property.name === 'styles') { | ||
const parent = node.parent; | ||
|
||
if (node.object.object && node.object.object.type !== 'ThisExpression') { | ||
return; | ||
} | ||
|
||
if (parent.object.type === 'Identifier' && parent.object.name !== 'props') { | ||
return; | ||
} | ||
|
||
if (parent.parent.type === 'MemberExpression') { | ||
return; | ||
} | ||
|
||
usedStyles[parent.property.name] = true; | ||
} | ||
}, | ||
|
||
ImportDeclaration(node) { | ||
if (isCSSFound()) { | ||
// We've already found it, so there is no more work to do. | ||
return; | ||
} | ||
|
||
const found = findImportCSSFromWithStylesImportDeclaration(node); | ||
if (found !== null) { | ||
cssFromWithStylesName = found; | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
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
Oops, something went wrong.