Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ npm run css-audit -- v5.5/**/* --recommended
- Places where `display: none` is used
- `typography`
- A collection of information about various typography-related properties
- `repeated-rules`
- Detects duplicate `property: value` pairs across different selectors
- Example: both `.foo` and `.bar` using `border: 1px solid red;`

## Technical details

Expand Down
25 changes: 25 additions & 0 deletions src/audits/repeated-rules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module.exports = {
name: "repeated-rules",
description: "Detect repeated CSS property/value pairs across selectors",

run: (astRoot, result) => {
const seen = new Map();
const repeated = [];

// Walk through all rules in the CSS AST
astRoot.walkDecls((decl) => {
const rule = `${decl.prop}: ${decl.value}`;
if (seen.has(rule)) {
repeated.push(rule);
} else {
seen.set(rule, decl.parent.selector);
}
});

if (repeated.length > 0) {
result.warn(
`Repeated rules found: ${[...new Set(repeated)].join(", ")}`
);
}
},
};
4 changes: 4 additions & 0 deletions src/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ const runAudits = ( cssFiles ) => {
if ( getArg( '--alphas' ) ) {
audits.push( require( './audits/alphas' )( cssFiles ) );
}
if ( runAll || getArg( '--repeated-rules' ) ) {
audits.push( require( './audits/repeated-rules' )( cssFiles ) );
}


const propertyValues = getArg( '--property-values' );
const isPropertyValuesArray =
Expand Down