-
-
Notifications
You must be signed in to change notification settings - Fork 51
fix(no-unused-class-name): detect duplicated class names #1260
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'eslint-plugin-svelte': patch | ||
--- | ||
|
||
fix(no-unused-class-name): fix cannnot detect duplicated class name | ||
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -36,7 +36,10 @@ export default createRule('no-unused-class-name', { | |||||||||||
return {}; | ||||||||||||
} | ||||||||||||
const allowedClassNames = context.options[0]?.allowedClassNames ?? []; | ||||||||||||
const classesUsedInTemplate: Record<string, AST.SourceLocation> = {}; | ||||||||||||
const classesUsedInTemplate: { | ||||||||||||
className: string; | ||||||||||||
loc: AST.SourceLocation; | ||||||||||||
}[] = []; | ||||||||||||
Comment on lines
+39
to
+42
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this better in terms of performance?
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think performance is no changes for now. If data structure is changed, I think code will be complex a little for now. |
||||||||||||
|
||||||||||||
return { | ||||||||||||
SvelteElement(node) { | ||||||||||||
|
@@ -45,7 +48,7 @@ export default createRule('no-unused-class-name', { | |||||||||||
} | ||||||||||||
const classes = node.startTag.attributes.flatMap(findClassesInAttribute); | ||||||||||||
for (const className of classes) { | ||||||||||||
classesUsedInTemplate[className] = node.startTag.loc; | ||||||||||||
classesUsedInTemplate.push({ className, loc: node.startTag.loc }); | ||||||||||||
} | ||||||||||||
}, | ||||||||||||
'Program:exit'() { | ||||||||||||
|
@@ -57,15 +60,16 @@ export default createRule('no-unused-class-name', { | |||||||||||
styleContext.status === 'success' | ||||||||||||
? findClassesInPostCSSNode(styleContext.sourceAst, sourceCode.parserServices) | ||||||||||||
: []; | ||||||||||||
for (const className in classesUsedInTemplate) { | ||||||||||||
|
||||||||||||
for (const { className, loc } of classesUsedInTemplate) { | ||||||||||||
if ( | ||||||||||||
!allowedClassNames.some((allowedClassName: string) => | ||||||||||||
toRegExp(allowedClassName).test(className) | ||||||||||||
) && | ||||||||||||
!classesUsedInStyle.includes(className) | ||||||||||||
) { | ||||||||||||
context.report({ | ||||||||||||
loc: classesUsedInTemplate[className], | ||||||||||||
loc, | ||||||||||||
message: `Unused class "${className}".` | ||||||||||||
}); | ||||||||||||
} | ||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
- message: Unused class "div-class". | ||
line: 1 | ||
column: 1 | ||
suggestions: null | ||
- message: Unused class "div-class". | ||
line: 3 | ||
column: 1 | ||
suggestions: null |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<div class="div-class">Hello</div> | ||
|
||
<span class="div-class">World!</span> |
Uh oh!
There was an error while loading. Please reload this page.