Skip to content

Commit d8c6afd

Browse files
7nik7nik
and
7nik
authored
fix: emit error on wrong placement of the :global block selector (#15794)
Co-authored-by: 7nik <[email protected]>
1 parent 6fe5977 commit d8c6afd

File tree

7 files changed

+42
-1
lines changed

7 files changed

+42
-1
lines changed

Diff for: .changeset/pretty-beers-care.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: emit error on wrong placement of the `:global` block selector

Diff for: documentation/docs/98-reference/.generated/compile-errors.md

+6
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,12 @@ A `:global` selector cannot modify an existing selector
274274
A `:global` selector can only be modified if it is a descendant of other selectors
275275
```
276276

277+
### css_global_block_invalid_placement
278+
279+
```
280+
A `:global` selector cannot be inside a pseudoclass
281+
```
282+
277283
### css_global_invalid_placement
278284

279285
```

Diff for: packages/svelte/messages/compile-errors/style.md

+4
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ x y {
5050

5151
> A `:global` selector can only be modified if it is a descendant of other selectors
5252
53+
## css_global_block_invalid_placement
54+
55+
> A `:global` selector cannot be inside a pseudoclass
56+
5357
## css_global_invalid_placement
5458

5559
> `:global(...)` can be at the start or end of a selector sequence, but not in the middle

Diff for: packages/svelte/src/compiler/errors.js

+9
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,15 @@ export function css_global_block_invalid_modifier_start(node) {
581581
e(node, 'css_global_block_invalid_modifier_start', `A \`:global\` selector can only be modified if it is a descendant of other selectors\nhttps://svelte.dev/e/css_global_block_invalid_modifier_start`);
582582
}
583583

584+
/**
585+
* A `:global` selector cannot be inside a pseudoclass
586+
* @param {null | number | NodeLike} node
587+
* @returns {never}
588+
*/
589+
export function css_global_block_invalid_placement(node) {
590+
e(node, 'css_global_block_invalid_placement', `A \`:global\` selector cannot be inside a pseudoclass\nhttps://svelte.dev/e/css_global_block_invalid_placement`);
591+
}
592+
584593
/**
585594
* `:global(...)` can be at the start or end of a selector sequence, but not in the middle
586595
* @param {null | number | NodeLike} node

Diff for: packages/svelte/src/compiler/phases/2-analyze/css/css-analyze.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,12 @@ const css_visitors = {
6868
const global = node.children.find(is_global);
6969

7070
if (global) {
71-
const idx = node.children.indexOf(global);
71+
const is_nested = context.path.at(-2)?.type === 'PseudoClassSelector';
72+
if (is_nested && !global.selectors[0].args) {
73+
e.css_global_block_invalid_placement(global.selectors[0]);
74+
}
7275

76+
const idx = node.children.indexOf(global);
7377
if (global.selectors[0].args !== null && idx !== 0 && idx !== node.children.length - 1) {
7478
// ensure `:global(...)` is not used in the middle of a selector (but multiple `global(...)` in sequence are ok)
7579
for (let i = idx + 1; i < node.children.length; i++) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { test } from '../../test';
2+
3+
export default test({
4+
error: {
5+
code: 'css_global_block_invalid_placement',
6+
message: 'A `:global` selector cannot be inside a pseudoclass',
7+
position: [28, 35]
8+
}
9+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<style>
2+
/* invalid */
3+
:is(:global) { color: red }
4+
</style>

0 commit comments

Comments
 (0)