-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #294 from 0Miles/dev/beta
Add(ESLint): Class collision check
- Loading branch information
Showing
9 changed files
with
243 additions
and
79 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<!-- eslint-disable @master/css/class-order --> | ||
<div class="m:10 m:40 m:50 m:60 m:20 m:30:hover m:40@dark"> | ||
... | ||
</div> | ||
|
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,105 @@ | ||
|
||
const astUtil = require('../utils/ast') | ||
const defineVisitors = require('../utils/define-visitors') | ||
const resolveContext = require('../utils/resolve-context') | ||
const { createValidRules } = require('@master/css-validator') | ||
const CssTree = require('css-tree') | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: 'Avoid declaring the identical CSS property repeatedly', | ||
category: 'Stylistic Issues', | ||
recommended: false, | ||
url: 'https://beta.css.master.co/docs/code-linting#avoid-declaring-the-identical-css-property-repeatedly', | ||
}, | ||
messages: { | ||
collisionClass: '{{message}}', | ||
}, | ||
fixable: 'code' | ||
}, | ||
create: function (context) { | ||
const { options, settings, config } = resolveContext(context) | ||
const visitNode = (node, arg = null) => { | ||
astUtil.parseNodeRecursive( | ||
node, | ||
arg, | ||
(classNames, node, originalClassNamesValue, start, end) => { | ||
const sourceCode = context.getSourceCode() | ||
const sourceCodeLines = sourceCode.lines | ||
const nodeStartLine = node.loc.start.line | ||
const nodeEndLine = node.loc.end.line | ||
|
||
const parsedRules = classNames | ||
.map(x => createValidRules(x, { config })) | ||
.map(rules => { | ||
if (rules.length) { | ||
const ruleAst = CssTree.parse(rules[0].text, { parseValue: false }) | ||
const ruleStyles = [] | ||
CssTree.walk(ruleAst, (cssNode) => { | ||
if (cssNode.type === "Declaration") { | ||
ruleStyles.push({ | ||
key: cssNode.property, | ||
value: cssNode.value.value | ||
}) | ||
} | ||
}) | ||
|
||
return { | ||
selector: Object.values(rules[0].vendorSuffixSelectors ?? {})?.[0]?.[0], | ||
mediaToken: rules[0].media?.token, | ||
styles: ruleStyles | ||
} | ||
} | ||
return null | ||
}) | ||
|
||
for (let i = 0; i < classNames.length ; i++) { | ||
const className = classNames[i] | ||
const parsedRule = parsedRules[i] | ||
const conflicts = [] | ||
|
||
if (parsedRule && parsedRule.styles.length === 1) { | ||
for (let j = 0; j < classNames.length; j++) { | ||
const compareClassName = classNames[j] | ||
const compareRule = parsedRules[j] | ||
if (i !== j && compareRule && compareRule.styles.length === 1 | ||
&& parsedRule.selector == compareRule.selector | ||
&& parsedRule.mediaToken == compareRule.mediaToken | ||
&& parsedRule.styles[0].key == compareRule.styles[0].key | ||
) { | ||
conflicts.push(compareClassName) | ||
} | ||
} | ||
|
||
if (conflicts.length > 0) { | ||
const conflictClassNamesMsg = conflicts.map(x => `\`${x}\``).join(' and ') | ||
let fixClassNames = originalClassNamesValue | ||
for (const conflictClassName of conflicts){ | ||
const regexSafe = conflictClassName.replace(/(\\|\.|\(|\)|\[|\]|\{|\}|\+|\*|\?|\^|\$|\||\/)/g, '\\$1') | ||
fixClassNames = fixClassNames.replace(new RegExp(`\\s+${regexSafe}|${regexSafe}\\s+`), '') | ||
} | ||
|
||
context.report({ | ||
loc: astUtil.findLoc(className, sourceCodeLines, nodeStartLine, nodeEndLine), | ||
messageId: 'collisionClass', | ||
data: { | ||
message: `\`${className}\` applies the same CSS declarations as ${conflictClassNamesMsg}. | ||
`, | ||
}, | ||
fix: function (fixer) { | ||
return fixer.replaceTextRange([start, end], fixClassNames) | ||
} | ||
}) | ||
} | ||
} | ||
} | ||
}, | ||
false, | ||
false, | ||
settings.ignoredKeys | ||
) | ||
} | ||
return defineVisitors({ context, options, settings, config }, visitNode) | ||
}, | ||
} |
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
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,27 @@ | ||
|
||
const rule = require('../lib/rules/class-collision') | ||
const RuleTester = require('eslint').RuleTester | ||
|
||
new RuleTester({ | ||
parserOptions: { | ||
ecmaFeatures: { | ||
jsx: true, | ||
}, | ||
} | ||
}).run('collision', rule, { | ||
valid: [ | ||
{ | ||
code: `<div class="m:10 m:30:hover m:40@dark">Simple, basic</div>`, | ||
} | ||
], | ||
invalid: [ | ||
{ | ||
code: `<div class="m:10 m:20 m:30:hover m:40@dark">collision</div>`, | ||
output: `<div class="m:10 m:30:hover m:40@dark">collision</div>`, | ||
errors: [ | ||
{ messageId: 'collisionClass' }, | ||
{ messageId: 'collisionClass' } | ||
] | ||
}, | ||
], | ||
}) |
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.