-
-
Notifications
You must be signed in to change notification settings - Fork 50
Respect style tag type attribute when formatting #670
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
Open
bminer
wants to merge
2
commits into
prettier:main
Choose a base branch
from
bminer:fix/style-type-parser
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+133
−1
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import type { BuiltInParserName } from 'prettier'; | ||
| import type { AttributeToken } from 'pug-lexer'; | ||
|
|
||
| const wrappingQuotesRe: RegExp = /(^(["'`]))|((["'`])$)/g; | ||
|
|
||
| // Matches style types to the required parser for them | ||
| // Note: Types not listed here (e.g. `sass` or `stylus`) are left unformatted, | ||
| // because there is no builtin prettier parser for them | ||
| const styleTypeToParserMap: Map<string, BuiltInParserName> = new Map([ | ||
| ['css', 'css'], | ||
| ['text/css', 'css'], | ||
| ['less', 'less'], | ||
| ['text/less', 'less'], | ||
| ['scss', 'scss'], | ||
| ['text/scss', 'scss'], | ||
| ]); | ||
|
|
||
| /** | ||
| * Decides which parser to format style contents with. | ||
| * | ||
| * @param typeAttrToken Type token of the style tag. | ||
| * @returns Parser name to parse contents with. | ||
| */ | ||
| export function getStyleParserName( | ||
| typeAttrToken?: AttributeToken, | ||
| ): BuiltInParserName | undefined { | ||
| // Omission means CSS | ||
| if (!typeAttrToken) { | ||
| return 'css'; | ||
| } | ||
|
|
||
| const typeRaw: string | boolean = typeAttrToken.val; | ||
| // If it's not a string, best not do anything | ||
| if (typeof typeRaw !== 'string') { | ||
| return; | ||
| } | ||
|
|
||
| const type: string = typeRaw.replaceAll(wrappingQuotesRe, '').toLowerCase(); | ||
|
|
||
| // Empty type is equivalent to omission | ||
| if (!type) { | ||
| return 'css'; | ||
| } | ||
|
|
||
| return styleTypeToParserMap.get(type); | ||
| } | ||
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,41 @@ | ||
| //- Stays unchanged, because no loaded plugin provides a parser for sass | ||
| style(type="sass"). | ||
| :host | ||
| display: inline | ||
|
|
||
| //- Formatted with the builtin scss parser | ||
| style(type="text/scss"). | ||
| :host { | ||
| .a { | ||
| display: inline; | ||
| } | ||
| } | ||
|
|
||
| //- Formatted with the builtin less parser | ||
| style(type="less"). | ||
| @color: red; | ||
| .a { | ||
| color: @color; | ||
| } | ||
|
|
||
| //- Formatted with the builtin css parser | ||
| style(type="text/css"). | ||
| :host { | ||
| display: inline; | ||
| } | ||
|
|
||
| //- Without a type, formatted with the builtin css parser | ||
| style. | ||
| :host { | ||
| display: inline; | ||
| } | ||
|
|
||
| //- An empty type is formatted with the builtin css parser | ||
| style(type=""). | ||
| :host { | ||
| display: inline; | ||
| } | ||
|
|
||
| //- Stays unchanged, because a type without a value is not a string | ||
| style(type). | ||
| :host { display:inline } |
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,11 @@ | ||
| import { compareFiles } from 'tests/common'; | ||
| import { describe, expect, it } from 'vitest'; | ||
|
|
||
| describe('Issues', () => { | ||
| describe('issue #532', () => { | ||
| it('should format style tags based on their type attribute', async () => { | ||
| const { actual, expected } = await compareFiles(import.meta.url); | ||
| expect(actual).toBe(expected); | ||
| }); | ||
| }); | ||
| }); |
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,31 @@ | ||
| //- Stays unchanged, because no loaded plugin provides a parser for sass | ||
| style(type="sass"). | ||
| :host | ||
| display: inline | ||
|
|
||
| //- Formatted with the builtin scss parser | ||
| style(type='text/scss'). | ||
| :host { | ||
| .a { display:inline } | ||
| } | ||
|
|
||
| //- Formatted with the builtin less parser | ||
| style(type="less"). | ||
| @color: red; | ||
| .a { color:@color } | ||
|
|
||
| //- Formatted with the builtin css parser | ||
| style(type="text/css"). | ||
| :host { display:inline } | ||
|
|
||
| //- Without a type, formatted with the builtin css parser | ||
| style. | ||
| :host { display:inline } | ||
|
|
||
| //- An empty type is formatted with the builtin css parser | ||
| style(type=""). | ||
| :host { display:inline } | ||
|
|
||
| //- Stays unchanged, because a type without a value is not a string | ||
| style(type). | ||
| :host { display:inline } |
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.
issue (if-minor): line 35 and 42 are both uncovered, do you think we can add test cases for them?
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.
Should be all set. Let me know what you think!
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.
I'm a bit confused myself, but the Coverage Report -> File Coverage ->
src/utils/script-mime-types.tsnow shows42, 49👀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.
No idea. I think those lines have coverage. I'll investigate more tomorrow. Thanks!