-
Notifications
You must be signed in to change notification settings - Fork 6.5k
feat: introduce stylelint plugin #7644
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
47ccf97
feat: introduce stylelint plugin
canerakdas e6242d1
Update packages/ui-components/stylelint.plugin.mjs
canerakdas 94af7ca
refactor: review suggestions
canerakdas db51994
chore: resolve conflict
canerakdas 981f1ad
chore(docs): align Tailwind usage with official documentation in docs…
canerakdas 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -21,7 +21,7 @@ | |
|
|
||
| & img { | ||
| @apply h-auto | ||
| w-24 | ||
| md:w-48; | ||
| w-24 | ||
| md:w-48; | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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
22 changes: 18 additions & 4 deletions
22
packages/ui-components/Common/Breadcrumbs/BreadcrumbItem/index.module.css
This file contains hidden or 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 |
|---|---|---|
| @@ -1,27 +1,41 @@ | ||
| @reference "../../../styles/index.css"; | ||
|
|
||
| .item { | ||
| @apply flex max-w-fit items-center gap-5 truncate text-sm font-medium; | ||
| @apply flex | ||
| max-w-fit | ||
| items-center | ||
| gap-5 | ||
| truncate | ||
| text-sm | ||
| font-medium; | ||
|
|
||
| &:last-child { | ||
| @apply w-full; | ||
| } | ||
|
|
||
| a { | ||
| @apply shrink grow; | ||
| @apply shrink | ||
| grow; | ||
| } | ||
|
|
||
| &, | ||
| > a, | ||
| > a:hover { | ||
| @apply text-neutral-800 motion-safe:transition-colors dark:text-neutral-200; | ||
| @apply text-neutral-800 | ||
| motion-safe:transition-colors | ||
| dark:text-neutral-200; | ||
| } | ||
|
|
||
| &.visuallyHidden { | ||
| @apply hidden; | ||
| } | ||
|
|
||
| .separator { | ||
| @apply size-4 max-w-fit shrink-0 grow text-neutral-600 dark:text-neutral-400; | ||
| @apply size-4 | ||
| max-w-fit | ||
| shrink-0 | ||
| grow | ||
| text-neutral-600 | ||
| dark:text-neutral-400; | ||
| } | ||
| } |
15 changes: 12 additions & 3 deletions
15
packages/ui-components/Common/Breadcrumbs/BreadcrumbLink/index.module.css
This file contains hidden or 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 |
|---|---|---|
| @@ -1,13 +1,22 @@ | ||
| @reference "../../../styles/index.css"; | ||
|
|
||
| .link { | ||
| @apply max-w-fit truncate; | ||
| @apply max-w-fit | ||
| truncate; | ||
|
|
||
| &.active { | ||
| @apply rounded bg-green-600 px-2 py-1 font-semibold text-white motion-safe:transition-colors dark:text-white; | ||
| @apply rounded | ||
| bg-green-600 | ||
| px-2 | ||
| py-1 | ||
| font-semibold | ||
| text-white | ||
| motion-safe:transition-colors | ||
| dark:text-white; | ||
|
|
||
| &:hover { | ||
| @apply bg-green-700 text-white; | ||
| @apply bg-green-700 | ||
| text-white; | ||
| } | ||
| } | ||
| } |
6 changes: 5 additions & 1 deletion
6
packages/ui-components/Common/Breadcrumbs/BreadcrumbRoot/index.module.css
This file contains hidden or 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 |
|---|---|---|
| @@ -1,5 +1,9 @@ | ||
| @reference "../../../styles/index.css"; | ||
|
|
||
| .list { | ||
| @apply xs:w-full flex w-screen gap-5 px-6; | ||
| @apply xs:w-full | ||
| flex | ||
| w-screen | ||
| gap-5 | ||
| px-6; | ||
| } |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -35,7 +35,7 @@ | |
|
|
||
| .tabSecondaryLabel { | ||
| @apply text-green-800 | ||
| dark:text-green-600; | ||
| dark:text-green-600; | ||
| } | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or 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,75 @@ | ||
| import { | ||
| indentClassNames, | ||
| cleanClassNames, | ||
| applyIndentation, | ||
| } from '../utils.mjs'; | ||
|
|
||
| describe('cleanClassNames', () => { | ||
| it('removes newlines and extra spaces', () => { | ||
| const input = ' text-xl \r font-bold \n\n text-center '; | ||
| const expected = 'text-xl font-bold text-center'; | ||
|
|
||
| expect(cleanClassNames(input)).toBe(expected); | ||
| }); | ||
|
|
||
| it('trims leading and trailing spaces', () => { | ||
| const input = ' mt-4 '; | ||
| const expected = 'mt-4'; | ||
|
|
||
| expect(cleanClassNames(input)).toBe(expected); | ||
| }); | ||
|
|
||
| it('returns empty string when input is empty', () => { | ||
| expect(cleanClassNames('')).toBe(''); | ||
| }); | ||
| }); | ||
|
|
||
| describe('applyIndentation', () => { | ||
| it('adds indentation to each class name', () => { | ||
| const input = 'text-xl font-bold text-center'; | ||
| const indent = ' '; | ||
| const expected = ['text-xl', ' font-bold', ' text-center'].join('\n'); | ||
|
|
||
| expect(applyIndentation(input, indent)).toBe(expected); | ||
| }); | ||
| }); | ||
|
|
||
| describe('indentClassNames', () => { | ||
| it('returns null if rule is missing required properties', () => { | ||
| expect(indentClassNames(null)).toBeNull(); | ||
| expect(indentClassNames({})).toBeNull(); | ||
| expect(indentClassNames({ params: 'foo' })).toBeNull(); | ||
| expect(indentClassNames({ params: 'foo', raws: {} })).toBeNull(); | ||
| }); | ||
|
|
||
| it('cleans and indents class names correctly', () => { | ||
| const rule = { | ||
| params: ' text-xl \n font-bold \n text-center ', | ||
| raws: { | ||
| before: ' ', | ||
| }, | ||
| }; | ||
|
|
||
| const result = indentClassNames({ ...rule }); | ||
| const expectedParams = ['text-xl', ' font-bold', ' text-center'].join( | ||
| '\n' | ||
| ); | ||
|
|
||
| expect(result).toEqual({ | ||
| ...rule, | ||
| params: expectedParams, | ||
| }); | ||
| }); | ||
|
|
||
| it('returns the same rule object with modified params', () => { | ||
| const rule = { | ||
| params: 'p-4 mb-2', | ||
| raws: { before: ' \n' }, | ||
| }; | ||
|
|
||
| const result = indentClassNames(rule); | ||
|
|
||
| expect(result).toBe(rule); | ||
| expect(result.params).toBe('p-4\n mb-2'); | ||
| }); | ||
| }); |
This file contains hidden or 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,64 @@ | ||
| import stylelint from 'stylelint'; | ||
|
|
||
| import { indentClassNames } from './utils.mjs'; | ||
|
|
||
| const { | ||
| createPlugin, | ||
| utils: { report, ruleMessages }, | ||
| } = stylelint; | ||
|
|
||
| const name = 'nodejs/one-mixin-per-line'; | ||
|
|
||
| const messages = ruleMessages(name, { | ||
| className: tokens => | ||
| `Each line should contain only one Tailwind token; "${tokens}"`, | ||
|
canerakdas marked this conversation as resolved.
Outdated
|
||
| spacing: tokens => | ||
| `Each class used with @apply should be indented by 2 spaces on a new line; ${tokens}`, | ||
|
canerakdas marked this conversation as resolved.
Outdated
|
||
| }); | ||
|
|
||
| const meta = { | ||
| url: 'https://github.com/nodejs/nodejs.org/blob/main/COLLABORATOR_GUIDE.md#styling-a-component', | ||
| fixable: true, | ||
| }; | ||
|
|
||
| /** @type {import('stylelint').Rule} */ | ||
| const rule = () => (root, result) => { | ||
| root.walkAtRules('apply', rule => { | ||
| if (!rule.params.includes(' ')) { | ||
| // If there are no spaces in the params, we don't need to check anything | ||
| return; | ||
| } | ||
|
|
||
| // Since we use 2 spaces for indentation, each line in the apply rule | ||
| // should be indented with two extra whitespaces. | ||
| const classes = rule.params.split(`${rule.raws.before} `); | ||
| const classNames = classes.join(''); | ||
|
|
||
| // Check if the params contain any spaces | ||
| if (classNames.includes(' ')) { | ||
| if (classNames.includes('\n')) { | ||
| return report({ | ||
| ruleName: name, | ||
| result: result, | ||
| message: messages.spacing(JSON.stringify(rule.params)), | ||
| node: rule, | ||
| fix: indentClassNames(rule), | ||
| }); | ||
| } | ||
|
|
||
| return report({ | ||
| ruleName: name, | ||
| result, | ||
| message: messages.className(rule.params), | ||
| node: rule, | ||
| fix: indentClassNames(rule), | ||
| }); | ||
| } | ||
| }); | ||
| }; | ||
|
|
||
| rule.ruleName = name; | ||
| rule.messages = messages; | ||
| rule.meta = meta; | ||
|
|
||
| export default createPlugin(name, rule); | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.