-
-
Notifications
You must be signed in to change notification settings - Fork 23
Added support for style selector parsing #619
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
baseballyama
merged 6 commits into
sveltejs:main
from
marekdedic:selector-node-loc-fixing
Dec 31, 2024
+2,793
−7
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
33d298b
test(style-node-loc): added node types to tests
marekdedic 5953dba
test(selector-parsing): added tests
marekdedic ba98577
feat(selector-parsing): added helper function to parse style selectors
marekdedic 652e002
test(selector-node-loc-fixing): added tests
marekdedic 0c66ec6
feat(selector-parsing): added helper function to convert style select…
marekdedic 243f4c4
fix(styles): fixed missing types on parser services
marekdedic 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"svelte-eslint-parser": minor | ||
--- | ||
|
||
feat: added support for style selector parsing |
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
25 changes: 25 additions & 0 deletions
25
tests/fixtures/parser/selector-parsing/simple-css-input.svelte
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,25 @@ | ||
<script> | ||
let a = 10 | ||
</script> | ||
|
||
<span class="myClass">Hello!</span> | ||
|
||
<b>{a}</b> | ||
|
||
<style> | ||
.myClass { | ||
color: red; | ||
} | ||
b { | ||
font-size: xx-large; | ||
} | ||
a:active, | ||
a::before, | ||
b + a, | ||
b + .myClass, | ||
a[data-key="value"] { | ||
color: blue; | ||
} | ||
</style> |
494 changes: 494 additions & 0 deletions
494
tests/fixtures/parser/selector-parsing/simple-css-output.json
Large diffs are not rendered by default.
Oops, something went wrong.
20 changes: 20 additions & 0 deletions
20
tests/fixtures/parser/selector-parsing/simple-postcss-input.svelte
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,20 @@ | ||
<div class="container"> | ||
<div class="div-class">Hello</div> | ||
|
||
<span class="span-class">World!</span> | ||
</div> | ||
|
||
<style lang="postcss"> | ||
body { | ||
colour: white; | ||
background-colour: grey; | ||
} | ||
a:active, | ||
a::before, | ||
b + a, | ||
b + .myClass, | ||
a[data-key="value"] { | ||
color: blue; | ||
} | ||
</style> |
433 changes: 433 additions & 0 deletions
433
tests/fixtures/parser/selector-parsing/simple-postcss-output.json
Large diffs are not rendered by default.
Oops, something went wrong.
26 changes: 26 additions & 0 deletions
26
tests/fixtures/parser/selector-parsing/simple-scss-input.svelte
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,26 @@ | ||
<div class="container"> | ||
<div class="div-class">Hello</div> | ||
|
||
<span class="span-class">World!</span> | ||
</div> | ||
|
||
<style lang="scss"> | ||
.container { | ||
.div-class { | ||
// This is an inline comment | ||
color: red; | ||
} | ||
.span-class { | ||
font-weight: bold; | ||
} | ||
a:active, | ||
a::before, | ||
b + a, | ||
b + .myClass, | ||
a[data-key="value"] { | ||
color: blue; | ||
} | ||
} | ||
</style> |
554 changes: 554 additions & 0 deletions
554
tests/fixtures/parser/selector-parsing/simple-scss-output.json
Large diffs are not rendered by default.
Oops, something went wrong.
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
25 changes: 25 additions & 0 deletions
25
tests/fixtures/parser/style-selector-location-converter/simple-css-input.svelte
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,25 @@ | ||
<script> | ||
let a = 10 | ||
</script> | ||
|
||
<span class="myClass">Hello!</span> | ||
|
||
<b>{a}</b> | ||
|
||
<style> | ||
.myClass { | ||
color: red; | ||
} | ||
b { | ||
font-size: xx-large; | ||
} | ||
a:active, | ||
a::before, | ||
b + a, | ||
b + .myClass, | ||
a[data-key="value"] { | ||
color: blue; | ||
} | ||
</style> |
281 changes: 281 additions & 0 deletions
281
tests/fixtures/parser/style-selector-location-converter/simple-css-output.json
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,281 @@ | ||
[ | ||
[ | ||
[ | ||
"selector", | ||
{ | ||
"start": { | ||
"line": 10, | ||
"column": 2 | ||
}, | ||
"end": { | ||
"line": 10, | ||
"column": 10 | ||
} | ||
} | ||
], | ||
[ | ||
"class", | ||
{ | ||
"start": { | ||
"line": 10, | ||
"column": 2 | ||
}, | ||
"end": { | ||
"line": 10, | ||
"column": 10 | ||
} | ||
} | ||
] | ||
], | ||
[ | ||
[ | ||
"selector", | ||
{ | ||
"start": { | ||
"line": 14, | ||
"column": 2 | ||
}, | ||
"end": { | ||
"line": 14, | ||
"column": 3 | ||
} | ||
} | ||
], | ||
[ | ||
"tag", | ||
{ | ||
"start": { | ||
"line": 14, | ||
"column": 2 | ||
}, | ||
"end": { | ||
"line": 14, | ||
"column": 3 | ||
} | ||
} | ||
] | ||
], | ||
[ | ||
[ | ||
"selector", | ||
{ | ||
"start": { | ||
"line": 18, | ||
"column": 2 | ||
}, | ||
"end": { | ||
"line": 18, | ||
"column": 10 | ||
} | ||
} | ||
], | ||
[ | ||
"tag", | ||
{ | ||
"start": { | ||
"line": 18, | ||
"column": 2 | ||
}, | ||
"end": { | ||
"line": 18, | ||
"column": 3 | ||
} | ||
} | ||
], | ||
[ | ||
"pseudo", | ||
{ | ||
"start": { | ||
"line": 18, | ||
"column": 3 | ||
}, | ||
"end": { | ||
"line": 18, | ||
"column": 10 | ||
} | ||
} | ||
], | ||
[ | ||
"selector", | ||
{ | ||
"start": { | ||
"line": 19, | ||
"column": -1 | ||
}, | ||
"end": { | ||
"line": 19, | ||
"column": 11 | ||
} | ||
} | ||
], | ||
[ | ||
"tag", | ||
{ | ||
"start": { | ||
"line": 19, | ||
"column": 2 | ||
}, | ||
"end": { | ||
"line": 19, | ||
"column": 3 | ||
} | ||
} | ||
], | ||
[ | ||
"pseudo", | ||
{ | ||
"start": { | ||
"line": 19, | ||
"column": 3 | ||
}, | ||
"end": { | ||
"line": 19, | ||
"column": 11 | ||
} | ||
} | ||
], | ||
[ | ||
"selector", | ||
{ | ||
"start": { | ||
"line": 20, | ||
"column": -1 | ||
}, | ||
"end": { | ||
"line": 20, | ||
"column": 7 | ||
} | ||
} | ||
], | ||
[ | ||
"tag", | ||
{ | ||
"start": { | ||
"line": 20, | ||
"column": 2 | ||
}, | ||
"end": { | ||
"line": 20, | ||
"column": 3 | ||
} | ||
} | ||
], | ||
[ | ||
"combinator", | ||
{ | ||
"start": { | ||
"line": 20, | ||
"column": 4 | ||
}, | ||
"end": { | ||
"line": 20, | ||
"column": 5 | ||
} | ||
} | ||
], | ||
[ | ||
"tag", | ||
{ | ||
"start": { | ||
"line": 20, | ||
"column": 6 | ||
}, | ||
"end": { | ||
"line": 20, | ||
"column": 7 | ||
} | ||
} | ||
], | ||
[ | ||
"selector", | ||
{ | ||
"start": { | ||
"line": 21, | ||
"column": -1 | ||
}, | ||
"end": { | ||
"line": 21, | ||
"column": 14 | ||
} | ||
} | ||
], | ||
[ | ||
"tag", | ||
{ | ||
"start": { | ||
"line": 21, | ||
"column": 2 | ||
}, | ||
"end": { | ||
"line": 21, | ||
"column": 3 | ||
} | ||
} | ||
], | ||
[ | ||
"combinator", | ||
{ | ||
"start": { | ||
"line": 21, | ||
"column": 4 | ||
}, | ||
"end": { | ||
"line": 21, | ||
"column": 5 | ||
} | ||
} | ||
], | ||
[ | ||
"class", | ||
{ | ||
"start": { | ||
"line": 21, | ||
"column": 6 | ||
}, | ||
"end": { | ||
"line": 21, | ||
"column": 14 | ||
} | ||
} | ||
], | ||
[ | ||
"selector", | ||
{ | ||
"start": { | ||
"line": 22, | ||
"column": -1 | ||
}, | ||
"end": { | ||
"line": 22, | ||
"column": 21 | ||
} | ||
} | ||
], | ||
[ | ||
"tag", | ||
{ | ||
"start": { | ||
"line": 22, | ||
"column": 2 | ||
}, | ||
"end": { | ||
"line": 22, | ||
"column": 3 | ||
} | ||
} | ||
], | ||
[ | ||
"attribute", | ||
{ | ||
"start": { | ||
"line": 22, | ||
"column": 3 | ||
}, | ||
"end": { | ||
"line": 22, | ||
"column": 21 | ||
} | ||
} | ||
] | ||
] | ||
] |
20 changes: 20 additions & 0 deletions
20
tests/fixtures/parser/style-selector-location-converter/simple-postcss-input.svelte
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,20 @@ | ||
<div class="container"> | ||
<div class="div-class">Hello</div> | ||
|
||
<span class="span-class">World!</span> | ||
</div> | ||
|
||
<style lang="postcss"> | ||
body { | ||
colour: white; | ||
background-colour: grey; | ||
} | ||
a:active, | ||
a::before, | ||
b + a, | ||
b + .myClass, | ||
a[data-key="value"] { | ||
color: blue; | ||
} | ||
</style> |
253 changes: 253 additions & 0 deletions
253
tests/fixtures/parser/style-selector-location-converter/simple-postcss-output.json
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,253 @@ | ||
[ | ||
[ | ||
[ | ||
"selector", | ||
{ | ||
"start": { | ||
"line": 8, | ||
"column": 2 | ||
}, | ||
"end": { | ||
"line": 8, | ||
"column": 6 | ||
} | ||
} | ||
], | ||
[ | ||
"tag", | ||
{ | ||
"start": { | ||
"line": 8, | ||
"column": 2 | ||
}, | ||
"end": { | ||
"line": 8, | ||
"column": 6 | ||
} | ||
} | ||
] | ||
], | ||
[ | ||
[ | ||
"selector", | ||
{ | ||
"start": { | ||
"line": 13, | ||
"column": 2 | ||
}, | ||
"end": { | ||
"line": 13, | ||
"column": 10 | ||
} | ||
} | ||
], | ||
[ | ||
"tag", | ||
{ | ||
"start": { | ||
"line": 13, | ||
"column": 2 | ||
}, | ||
"end": { | ||
"line": 13, | ||
"column": 3 | ||
} | ||
} | ||
], | ||
[ | ||
"pseudo", | ||
{ | ||
"start": { | ||
"line": 13, | ||
"column": 3 | ||
}, | ||
"end": { | ||
"line": 13, | ||
"column": 10 | ||
} | ||
} | ||
], | ||
[ | ||
"selector", | ||
{ | ||
"start": { | ||
"line": 14, | ||
"column": -1 | ||
}, | ||
"end": { | ||
"line": 14, | ||
"column": 11 | ||
} | ||
} | ||
], | ||
[ | ||
"tag", | ||
{ | ||
"start": { | ||
"line": 14, | ||
"column": 2 | ||
}, | ||
"end": { | ||
"line": 14, | ||
"column": 3 | ||
} | ||
} | ||
], | ||
[ | ||
"pseudo", | ||
{ | ||
"start": { | ||
"line": 14, | ||
"column": 3 | ||
}, | ||
"end": { | ||
"line": 14, | ||
"column": 11 | ||
} | ||
} | ||
], | ||
[ | ||
"selector", | ||
{ | ||
"start": { | ||
"line": 15, | ||
"column": -1 | ||
}, | ||
"end": { | ||
"line": 15, | ||
"column": 7 | ||
} | ||
} | ||
], | ||
[ | ||
"tag", | ||
{ | ||
"start": { | ||
"line": 15, | ||
"column": 2 | ||
}, | ||
"end": { | ||
"line": 15, | ||
"column": 3 | ||
} | ||
} | ||
], | ||
[ | ||
"combinator", | ||
{ | ||
"start": { | ||
"line": 15, | ||
"column": 4 | ||
}, | ||
"end": { | ||
"line": 15, | ||
"column": 5 | ||
} | ||
} | ||
], | ||
[ | ||
"tag", | ||
{ | ||
"start": { | ||
"line": 15, | ||
"column": 6 | ||
}, | ||
"end": { | ||
"line": 15, | ||
"column": 7 | ||
} | ||
} | ||
], | ||
[ | ||
"selector", | ||
{ | ||
"start": { | ||
"line": 16, | ||
"column": -1 | ||
}, | ||
"end": { | ||
"line": 16, | ||
"column": 14 | ||
} | ||
} | ||
], | ||
[ | ||
"tag", | ||
{ | ||
"start": { | ||
"line": 16, | ||
"column": 2 | ||
}, | ||
"end": { | ||
"line": 16, | ||
"column": 3 | ||
} | ||
} | ||
], | ||
[ | ||
"combinator", | ||
{ | ||
"start": { | ||
"line": 16, | ||
"column": 4 | ||
}, | ||
"end": { | ||
"line": 16, | ||
"column": 5 | ||
} | ||
} | ||
], | ||
[ | ||
"class", | ||
{ | ||
"start": { | ||
"line": 16, | ||
"column": 6 | ||
}, | ||
"end": { | ||
"line": 16, | ||
"column": 14 | ||
} | ||
} | ||
], | ||
[ | ||
"selector", | ||
{ | ||
"start": { | ||
"line": 17, | ||
"column": -1 | ||
}, | ||
"end": { | ||
"line": 17, | ||
"column": 21 | ||
} | ||
} | ||
], | ||
[ | ||
"tag", | ||
{ | ||
"start": { | ||
"line": 17, | ||
"column": 2 | ||
}, | ||
"end": { | ||
"line": 17, | ||
"column": 3 | ||
} | ||
} | ||
], | ||
[ | ||
"attribute", | ||
{ | ||
"start": { | ||
"line": 17, | ||
"column": 3 | ||
}, | ||
"end": { | ||
"line": 17, | ||
"column": 21 | ||
} | ||
} | ||
] | ||
] | ||
] |
26 changes: 26 additions & 0 deletions
26
tests/fixtures/parser/style-selector-location-converter/simple-scss-input.svelte
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,26 @@ | ||
<div class="container"> | ||
<div class="div-class">Hello</div> | ||
|
||
<span class="span-class">World!</span> | ||
</div> | ||
|
||
<style lang="scss"> | ||
.container { | ||
.div-class { | ||
// This is an inline comment | ||
color: red; | ||
} | ||
.span-class { | ||
font-weight: bold; | ||
} | ||
a:active, | ||
a::before, | ||
b + a, | ||
b + .myClass, | ||
a[data-key="value"] { | ||
color: blue; | ||
} | ||
} | ||
</style> |
309 changes: 309 additions & 0 deletions
309
tests/fixtures/parser/style-selector-location-converter/simple-scss-output.json
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,309 @@ | ||
[ | ||
[ | ||
[ | ||
"selector", | ||
{ | ||
"start": { | ||
"line": 8, | ||
"column": 2 | ||
}, | ||
"end": { | ||
"line": 8, | ||
"column": 12 | ||
} | ||
} | ||
], | ||
[ | ||
"class", | ||
{ | ||
"start": { | ||
"line": 8, | ||
"column": 2 | ||
}, | ||
"end": { | ||
"line": 8, | ||
"column": 12 | ||
} | ||
} | ||
] | ||
], | ||
[ | ||
[ | ||
"selector", | ||
{ | ||
"start": { | ||
"line": 9, | ||
"column": 4 | ||
}, | ||
"end": { | ||
"line": 9, | ||
"column": 14 | ||
} | ||
} | ||
], | ||
[ | ||
"class", | ||
{ | ||
"start": { | ||
"line": 9, | ||
"column": 4 | ||
}, | ||
"end": { | ||
"line": 9, | ||
"column": 14 | ||
} | ||
} | ||
] | ||
], | ||
[ | ||
[ | ||
"selector", | ||
{ | ||
"start": { | ||
"line": 14, | ||
"column": 4 | ||
}, | ||
"end": { | ||
"line": 14, | ||
"column": 15 | ||
} | ||
} | ||
], | ||
[ | ||
"class", | ||
{ | ||
"start": { | ||
"line": 14, | ||
"column": 4 | ||
}, | ||
"end": { | ||
"line": 14, | ||
"column": 15 | ||
} | ||
} | ||
] | ||
], | ||
[ | ||
[ | ||
"selector", | ||
{ | ||
"start": { | ||
"line": 18, | ||
"column": 4 | ||
}, | ||
"end": { | ||
"line": 18, | ||
"column": 12 | ||
} | ||
} | ||
], | ||
[ | ||
"tag", | ||
{ | ||
"start": { | ||
"line": 18, | ||
"column": 4 | ||
}, | ||
"end": { | ||
"line": 18, | ||
"column": 5 | ||
} | ||
} | ||
], | ||
[ | ||
"pseudo", | ||
{ | ||
"start": { | ||
"line": 18, | ||
"column": 5 | ||
}, | ||
"end": { | ||
"line": 18, | ||
"column": 12 | ||
} | ||
} | ||
], | ||
[ | ||
"selector", | ||
{ | ||
"start": { | ||
"line": 19, | ||
"column": -1 | ||
}, | ||
"end": { | ||
"line": 19, | ||
"column": 13 | ||
} | ||
} | ||
], | ||
[ | ||
"tag", | ||
{ | ||
"start": { | ||
"line": 19, | ||
"column": 4 | ||
}, | ||
"end": { | ||
"line": 19, | ||
"column": 5 | ||
} | ||
} | ||
], | ||
[ | ||
"pseudo", | ||
{ | ||
"start": { | ||
"line": 19, | ||
"column": 5 | ||
}, | ||
"end": { | ||
"line": 19, | ||
"column": 13 | ||
} | ||
} | ||
], | ||
[ | ||
"selector", | ||
{ | ||
"start": { | ||
"line": 20, | ||
"column": -1 | ||
}, | ||
"end": { | ||
"line": 20, | ||
"column": 9 | ||
} | ||
} | ||
], | ||
[ | ||
"tag", | ||
{ | ||
"start": { | ||
"line": 20, | ||
"column": 4 | ||
}, | ||
"end": { | ||
"line": 20, | ||
"column": 5 | ||
} | ||
} | ||
], | ||
[ | ||
"combinator", | ||
{ | ||
"start": { | ||
"line": 20, | ||
"column": 6 | ||
}, | ||
"end": { | ||
"line": 20, | ||
"column": 7 | ||
} | ||
} | ||
], | ||
[ | ||
"tag", | ||
{ | ||
"start": { | ||
"line": 20, | ||
"column": 8 | ||
}, | ||
"end": { | ||
"line": 20, | ||
"column": 9 | ||
} | ||
} | ||
], | ||
[ | ||
"selector", | ||
{ | ||
"start": { | ||
"line": 21, | ||
"column": -1 | ||
}, | ||
"end": { | ||
"line": 21, | ||
"column": 16 | ||
} | ||
} | ||
], | ||
[ | ||
"tag", | ||
{ | ||
"start": { | ||
"line": 21, | ||
"column": 4 | ||
}, | ||
"end": { | ||
"line": 21, | ||
"column": 5 | ||
} | ||
} | ||
], | ||
[ | ||
"combinator", | ||
{ | ||
"start": { | ||
"line": 21, | ||
"column": 6 | ||
}, | ||
"end": { | ||
"line": 21, | ||
"column": 7 | ||
} | ||
} | ||
], | ||
[ | ||
"class", | ||
{ | ||
"start": { | ||
"line": 21, | ||
"column": 8 | ||
}, | ||
"end": { | ||
"line": 21, | ||
"column": 16 | ||
} | ||
} | ||
], | ||
[ | ||
"selector", | ||
{ | ||
"start": { | ||
"line": 22, | ||
"column": -1 | ||
}, | ||
"end": { | ||
"line": 22, | ||
"column": 23 | ||
} | ||
} | ||
], | ||
[ | ||
"tag", | ||
{ | ||
"start": { | ||
"line": 22, | ||
"column": 4 | ||
}, | ||
"end": { | ||
"line": 22, | ||
"column": 5 | ||
} | ||
} | ||
], | ||
[ | ||
"attribute", | ||
{ | ||
"start": { | ||
"line": 22, | ||
"column": 5 | ||
}, | ||
"end": { | ||
"line": 22, | ||
"column": 23 | ||
} | ||
} | ||
] | ||
] | ||
] |
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,56 @@ | ||
import assert from "assert"; | ||
import fs from "fs"; | ||
import path from "path"; | ||
import type { Node } from "postcss"; | ||
import type { Root as SelectorRoot } from "postcss-selector-parser"; | ||
|
||
import { parseForESLint } from "../../../src/index.js"; | ||
import { | ||
generateParserOptions, | ||
listupFixtures, | ||
selectorAstToJson, | ||
} from "./test-utils.js"; | ||
|
||
const dirname = path.dirname(new URL(import.meta.url).pathname); | ||
const SELECTOR_PARSING_FIXTURE_ROOT = path.resolve( | ||
dirname, | ||
"../../fixtures/parser/selector-parsing", | ||
); | ||
|
||
function parse(code: string, filePath: string, config: any) { | ||
return parseForESLint(code, generateParserOptions({ filePath }, config)); | ||
} | ||
|
||
describe("Check for AST.", () => { | ||
for (const { | ||
input, | ||
inputFileName, | ||
outputFileName, | ||
config, | ||
meetRequirements, | ||
} of listupFixtures(SELECTOR_PARSING_FIXTURE_ROOT)) { | ||
if (!meetRequirements("parse")) { | ||
continue; | ||
} | ||
describe(inputFileName, () => { | ||
let services: any; | ||
|
||
it("most to generate the expected selector AST.", () => { | ||
services = parse(input, inputFileName, config).services; | ||
if (!meetRequirements("test")) { | ||
return; | ||
} | ||
const styleContext = services.getStyleContext(); | ||
assert.strictEqual(styleContext.status, "success"); | ||
const selectorASTs: SelectorRoot[] = []; | ||
styleContext.sourceAst.walk((node: Node) => { | ||
if (node.type === "rule") { | ||
selectorASTs.push(services.getStyleSelectorAST(node)); | ||
} | ||
}); | ||
const output = fs.readFileSync(outputFileName, "utf8"); | ||
assert.strictEqual(`${selectorAstToJson(selectorASTs)}\n`, output); | ||
}); | ||
}); | ||
} | ||
}); |
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
25 changes: 25 additions & 0 deletions
25
tests/src/parser/style-selector-location-converter-utils.ts
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,25 @@ | ||
import type { AnyNode, Root } from "postcss"; | ||
import type { Node as SelectorNode } from "postcss-selector-parser"; | ||
|
||
import type { SourceLocation } from "../../../src/ast/common.js"; | ||
|
||
export function extractSelectorLocations( | ||
services: Record<string, any>, | ||
styleAST: Root, | ||
): [string, Partial<SourceLocation>][][] { | ||
const locations: [string, Partial<SourceLocation>][][] = []; | ||
styleAST.walk((node: AnyNode) => { | ||
if (node.type === "rule") { | ||
const selectorAst = services.getStyleSelectorAST(node); | ||
const selectorLocations: [string, Partial<SourceLocation>][] = []; | ||
selectorAst.walk((selectorNode: SelectorNode) => { | ||
selectorLocations.push([ | ||
selectorNode.type, | ||
services.styleSelectorNodeLoc(selectorNode, node), | ||
]); | ||
}); | ||
locations.push(selectorLocations); | ||
} | ||
}); | ||
return locations; | ||
} |
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,49 @@ | ||
import assert from "assert"; | ||
import fs from "fs"; | ||
import path from "path"; | ||
|
||
import { parseForESLint } from "../../../src/index.js"; | ||
import { extractSelectorLocations } from "./style-selector-location-converter-utils.js"; | ||
import { generateParserOptions, listupFixtures } from "./test-utils.js"; | ||
|
||
const dirname = path.dirname(new URL(import.meta.url).pathname); | ||
const SELECTOR_CONVERTER_FIXTURE_ROOT = path.resolve( | ||
dirname, | ||
"../../fixtures/parser/style-selector-location-converter", | ||
); | ||
|
||
function parse(code: string, filePath: string, config: any) { | ||
return parseForESLint(code, generateParserOptions({ filePath }, config)); | ||
} | ||
|
||
describe("Check for AST.", () => { | ||
for (const { | ||
input, | ||
inputFileName, | ||
outputFileName, | ||
config, | ||
meetRequirements, | ||
} of listupFixtures(SELECTOR_CONVERTER_FIXTURE_ROOT)) { | ||
describe(inputFileName, () => { | ||
let services: any; | ||
|
||
it("most to generate the expected style context.", () => { | ||
services = parse(input, inputFileName, config).services; | ||
if (!meetRequirements("test")) { | ||
return; | ||
} | ||
const styleContext = services.getStyleContext(); | ||
assert.strictEqual(styleContext.status, "success"); | ||
const locations = extractSelectorLocations( | ||
services, | ||
styleContext.sourceAst, | ||
); | ||
const output = fs.readFileSync(outputFileName, "utf8"); | ||
assert.strictEqual( | ||
`${JSON.stringify(locations, undefined, 2)}\n`, | ||
output, | ||
); | ||
}); | ||
}); | ||
} | ||
}); |
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
Unchanged files with check annotations Beta
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
(key as any).parent = sAttr; | ||
ctx.scriptLet.addObjectShorthandProperty(attribute.key, sAttr, (es) => { | ||
if ( | ||
// FIXME: Older parsers may use the same node. In that case, do not replace. | ||
// We will drop support for ESLint v7 in the next major version and remove this branch. | ||
es.key !== es.value | ||
) { |
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
import type {} from "svelte"; // FIXME: Workaround to get type information for "svelte/compiler" | ||
import { parse } from "svelte/compiler"; | ||
import type * as Compiler from "./svelte-ast-types-for-v5.js"; | ||
import type * as SvAST from "./svelte-ast-types.js"; |
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.
Why don't you do this in
fixSelectorNodeLocation
? I thought theloc
information could always be handled in ESLint style because this program runs on ESLint.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.
Yeah, I'm following the same approach we agreed on in #340 (comment) - the TL;DR is that PostCSS AST locations are different than ESLint AST locations - one of them starts counting columns from 0 and other from 1, one of them has the end point at the last character while the other has it point at the first character after the end... So we agreed we would recalculate the positions to be relative to the whole file instead of being relative to the selector, but we would keep the PostCSS semantics in the PostCSS AST. Only when converting from PostCSS AST to an ESLint
SourceLocation
would we convert this to the ESLint way of doing things.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.
Of course this is debatable (there is no universally "right" solution IMO), but I'd strongly suggest doing the selector parsing in the same way as the style node parsing. So if we were to change this one, we'd need to change the other one too.
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.
Thank you for explanation! OK. So let's merge this PR now!