-
Notifications
You must be signed in to change notification settings - Fork 9
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
feat: add rule no inline styles #224
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bb3b4bf
feat(rrc): add new rule noInlineStyles
Whomy09 d0af7eb
feat(rrd): add doc no inline styles
Whomy09 a7ca358
[autofix.ci] apply automated fixes
autofix-ci[bot] 1837e90
chore: add reportNoInlineStyles in rulesReport
Whomy09 5df5f3f
chore: resolve conflicts and add reportNoInlineStyles in rules report
Whomy09 2559413
chore: resolve formatting conflicts
Whomy09 e248345
chore: add from in the checkNoInlineStyles
Whomy09 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,43 @@ | ||
# No Inline Styles | ||
|
||
Checks if in an element within the template of an SFC uses a inline style | ||
|
||
## ❓ Why it's good to follow this rule? | ||
|
||
**Maintainability:** External CSS makes updates easier and keeps the codebase clean. | ||
**Performance:** Reduces HTML file size and leverages browser caching. | ||
**Consistency:** Ensures a uniform style across the entire website or application. | ||
|
||
## 😱 Examples of code for which this rule will throw a warning | ||
|
||
::: warning | ||
The following code contains inline `styles` which are not recommended because they increase the html code and do not take advantage of browser caching | ||
::: | ||
|
||
```vue | ||
<template> | ||
<div style="background-color: #ff0000;"> | ||
<!-- ... --> | ||
</div> | ||
</template> | ||
``` | ||
|
||
## 🤩 How to fix it? | ||
|
||
::: tip | ||
Using `style` with the `scoped` attribute and creating the necessary classes for the style you want to apply can be a good option. | ||
::: | ||
|
||
```vue | ||
<template> | ||
<div class="bg-primary"> | ||
<!-- ... --> | ||
</div> | ||
</template> | ||
|
||
<style scoped> | ||
.bg-primary { | ||
background-color: #ff0000; | ||
} | ||
</style> | ||
``` |
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,57 @@ | ||
import { beforeEach, describe, expect, it } from 'vitest' | ||
import type { SFCTemplateBlock } from '@vue/compiler-sfc' | ||
import { BG_RESET, BG_WARN, TEXT_INFO, TEXT_RESET, TEXT_WARN } from '../asceeCodes' | ||
import { checkNoInlineStyles, reportNoInlineStyles, resetNoInlineStyles } from './noInlineStyles' | ||
|
||
describe('checkNoInlineStyles', () => { | ||
beforeEach(() => { | ||
resetNoInlineStyles() | ||
}) | ||
|
||
it('should not report files with inline styles', () => { | ||
const template = { | ||
content: ` | ||
<template> | ||
<div>Hello World!</div> | ||
<div> | ||
<span>Hi!</span> | ||
</div> | ||
</template> | ||
`, | ||
} as SFCTemplateBlock | ||
const fileName = 'noInlineStyles.vue' | ||
checkNoInlineStyles(template, fileName) | ||
expect(reportNoInlineStyles().length).toBe(0) | ||
expect(reportNoInlineStyles()).toStrictEqual([]) | ||
}) | ||
|
||
it('should report files with inline styles', () => { | ||
const template = { | ||
content: ` | ||
<template> | ||
<div style="background: #fff;">Hello World!</div> | ||
<div> | ||
<span style="color: red;">Hi!</span> | ||
</div> | ||
</template> | ||
`, | ||
} as SFCTemplateBlock | ||
const fileName = 'noInlineStyles-problem.vue' | ||
checkNoInlineStyles(template, fileName) | ||
expect(reportNoInlineStyles().length).toBe(2) | ||
expect(reportNoInlineStyles()).toStrictEqual([ | ||
{ | ||
file: fileName, | ||
rule: `${TEXT_INFO}rrd ~ no Inline Styles${TEXT_RESET}`, | ||
description: `👉 ${TEXT_WARN}Avoid using inline styles. Consider moving the styles to a CSS or SCSS file, or use a Vue scoped style.${TEXT_RESET}`, | ||
message: `line #2 ${BG_WARN}Found inline style: style="background: #fff;"${BG_RESET} 🚨`, | ||
}, | ||
{ | ||
file: fileName, | ||
rule: `${TEXT_INFO}rrd ~ no Inline Styles${TEXT_RESET}`, | ||
description: `👉 ${TEXT_WARN}Avoid using inline styles. Consider moving the styles to a CSS or SCSS file, or use a Vue scoped style.${TEXT_RESET}`, | ||
message: `line #4 ${BG_WARN}Found inline style: style="color: red;"${BG_RESET} 🚨`, | ||
}, | ||
]) | ||
}) | ||
}) |
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,44 @@ | ||
import type { SFCTemplateBlock } from '@vue/compiler-sfc' | ||
import getLineNumber from '../getLineNumber' | ||
import type { FileCheckResult, Offense } from '../../types' | ||
import { BG_RESET, BG_WARN, TEXT_INFO, TEXT_RESET, TEXT_WARN } from '../asceeCodes' | ||
|
||
const results: FileCheckResult[] = [] | ||
|
||
const checkNoInlineStyles = (template: SFCTemplateBlock | null, filePath: string) => { | ||
if (!template) { | ||
return | ||
} | ||
|
||
const regex = /style\s*=\s*['"][^'"]*['"]/g | ||
|
||
const matches = [...template.content.matchAll(regex)] | ||
|
||
matches?.forEach((match) => { | ||
const lineNumber = getLineNumber(template.content.trim(), match[0]) | ||
results.push({ | ||
filePath, | ||
message: `line #${lineNumber} ${BG_WARN}Found inline style: ${match[0]}${BG_RESET}`, | ||
}) | ||
}) | ||
} | ||
|
||
const reportNoInlineStyles = () => { | ||
const offenses: Offense[] = [] | ||
|
||
if (results.length > 0) { | ||
results.forEach((result) => { | ||
offenses.push({ | ||
file: result.filePath, | ||
rule: `${TEXT_INFO}rrd ~ no Inline Styles${TEXT_RESET}`, | ||
description: `👉 ${TEXT_WARN}Avoid using inline styles. Consider moving the styles to a CSS or SCSS file, or use a Vue scoped style.${TEXT_RESET}`, | ||
message: `${result.message} 🚨`, | ||
}) | ||
}) | ||
} | ||
return offenses | ||
} | ||
|
||
const resetNoInlineStyles = () => (results.length = 0) | ||
|
||
export { checkNoInlineStyles, reportNoInlineStyles, resetNoInlineStyles } |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please add the
from
parameter here otherwise if the same pattern found multiple times, it will report the same line number for them