Skip to content

Commit 91b20b9

Browse files
authored
[fix] error when using combinator incorrectly (#7650)
* error when using combinator incorrectly * add new test case
1 parent 52f5005 commit 91b20b9

File tree

10 files changed

+92
-0
lines changed

10 files changed

+92
-0
lines changed

src/compiler/compile/compiler_errors.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,10 @@ export default {
234234
code: 'css-invalid-global-selector',
235235
message: ':global(...) must contain a single selector'
236236
},
237+
css_invalid_selector: (selector: string) => ({
238+
code: 'css-invalid-selector',
239+
message: `Invalid selector "${selector}"`
240+
}),
237241
duplicate_animation: {
238242
code: 'duplicate-animation',
239243
message: "An element can only have one 'animate' directive"

src/compiler/compile/css/Selector.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ export default class Selector {
145145
}
146146

147147
this.validate_global_with_multiple_selectors(component);
148+
this.validate_invalid_combinator_without_selector(component);
148149
}
149150

150151
validate_global_with_multiple_selectors(component: Component) {
@@ -163,6 +164,17 @@ export default class Selector {
163164
}
164165
}
165166
}
167+
validate_invalid_combinator_without_selector(component: Component) {
168+
for (let i = 0; i < this.blocks.length; i++) {
169+
const block = this.blocks[i];
170+
if (block.combinator && block.selectors.length === 0) {
171+
component.error(this.node, compiler_errors.css_invalid_selector(component.source.slice(this.node.start, this.node.end)));
172+
}
173+
if (!block.combinator && block.selectors.length === 0) {
174+
component.error(this.node, compiler_errors.css_invalid_selector(component.source.slice(this.node.start, this.node.end)));
175+
}
176+
}
177+
}
166178

167179
get_amount_class_specificity_increased() {
168180
let count = 0;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[
2+
{
3+
"code": "css-invalid-selector",
4+
"message": "Invalid selector \"> span\"",
5+
"start": { "line": 10, "column": 1, "character": 88 },
6+
"end": { "line": 10, "column": 7, "character": 94 },
7+
"pos": 88
8+
}
9+
]
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<p><span /></p>
2+
3+
<style>
4+
p > span {
5+
color: red;
6+
}
7+
:has(> span) {
8+
color: red;
9+
}
10+
> span {
11+
color: red;
12+
}
13+
</style>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[
2+
{
3+
"code": "css-invalid-selector",
4+
"message": "Invalid selector \"+ p\"",
5+
"start": { "line": 8, "column": 1, "character": 68 },
6+
"end": { "line": 8, "column": 4, "character": 71 },
7+
"pos": 68
8+
}
9+
]
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<p><span /></p>
2+
<p><span /></p>
3+
4+
<style>
5+
p + p {
6+
color: red;
7+
}
8+
+ p {
9+
color: red;
10+
}
11+
</style>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[
2+
{
3+
"code": "css-invalid-selector",
4+
"message": "Invalid selector \"> span\"",
5+
"start": { "line": 5, "column": 2, "character": 44 },
6+
"end": { "line": 5, "column": 8, "character": 50 },
7+
"pos": 44
8+
}
9+
]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<p><span /></p>
2+
3+
<style>
4+
@media screen {
5+
> span {
6+
color: red;
7+
}
8+
}
9+
</style>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[
2+
{
3+
"code": "css-invalid-selector",
4+
"message": "Invalid selector \"p >\"",
5+
"start": { "line": 4, "column": 1, "character": 26 },
6+
"end": { "line": 4, "column": 4, "character": 29 },
7+
"pos": 26
8+
}
9+
]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<p><span /></p>
2+
3+
<style>
4+
p > {
5+
color: red;
6+
}
7+
</style>

0 commit comments

Comments
 (0)