From ad5d19bcfc7fdf2e4b9228786b6b87fedbfc10b5 Mon Sep 17 00:00:00 2001 From: tanhauhau Date: Wed, 1 Mar 2023 09:10:56 +0800 Subject: [PATCH] fix code review --- src/compiler/compile/compiler_errors.ts | 4 ++ src/compiler/compile/css/Selector.ts | 38 +++++++++---------- .../errors.json | 2 +- .../input.svelte | 1 + 4 files changed, 24 insertions(+), 21 deletions(-) diff --git a/src/compiler/compile/compiler_errors.ts b/src/compiler/compile/compiler_errors.ts index b0c836681c..5733ccaed7 100644 --- a/src/compiler/compile/compiler_errors.ts +++ b/src/compiler/compile/compiler_errors.ts @@ -234,6 +234,10 @@ export default { code: 'css-invalid-global-selector', message: ':global(...) must contain a single selector' }, + css_invalid_global_selector_position: { + code: 'css-invalid-global-selector-position', + message: ':global(...) which not at the start of selector sequence should not contain type or universal selector' + }, css_invalid_selector: (selector: string) => ({ code: 'css-invalid-selector', message: `Invalid selector "${selector}"` diff --git a/src/compiler/compile/css/Selector.ts b/src/compiler/compile/css/Selector.ts index 9673c77f0e..1508d26c25 100644 --- a/src/compiler/compile/css/Selector.ts +++ b/src/compiler/compile/css/Selector.ts @@ -130,8 +130,6 @@ export default class Selector { } validate(component: Component) { - this.blocks.some((block) => block.validateGlobalCompoundSelector(component)); - let start = 0; let end = this.blocks.length; @@ -150,6 +148,7 @@ export default class Selector { } this.validate_global_with_multiple_selectors(component); + this.validate_global_compound_selector(component); this.validate_invalid_combinator_without_selector(component); } @@ -181,6 +180,23 @@ export default class Selector { } } + validate_global_compound_selector(component: Component) { + for (const block of this.blocks) { + for (let index = 0; index < block.selectors.length; index++) { + const selector = block.selectors[index]; + if (selector.type === 'PseudoClassSelector' && + selector.name === 'global' && + index !== 0 && + selector.children && + selector.children.length > 0 && + !/[.:#]/.test(selector.children[0].value) + ) { + component.error(selector, compiler_errors.css_invalid_global_selector_position); + } + } + } + } + get_amount_class_specificity_increased() { let count = 0; for (const block of this.blocks) { @@ -640,24 +656,6 @@ class Block { this.selectors.every((selector) => selector.type === 'PseudoClassSelector' || selector.type === 'PseudoElementSelector') ); } - - validateGlobalCompoundSelector(component: Component) { - this.selectors.some((selector, index) => { - if (selector.type === 'PseudoClassSelector' && - selector.name === 'global' && - index !== 0 && - selector.children && - selector.children.length > 0 && - selector.children[0].value[0] !== '.' - ) { - component.error(selector, { - code: 'css-invalid-global', - message: ':global(...) which not at the start of selector sequence should not contain type or universal selector' - }); - return false; - } - }); - } } function group_selectors(selector: CssNode) { diff --git a/test/validator/samples/css-invalid-global-placement-4/errors.json b/test/validator/samples/css-invalid-global-placement-4/errors.json index 883d15ccd4..f72bfe00f5 100644 --- a/test/validator/samples/css-invalid-global-placement-4/errors.json +++ b/test/validator/samples/css-invalid-global-placement-4/errors.json @@ -1,5 +1,5 @@ [{ - "code": "css-invalid-global", + "code": "css-invalid-global-selector-position", "message": ":global(...) which not at the start of selector sequence should not contain type or universal selector", "start": { "line": 2, diff --git a/test/validator/samples/css-invalid-global-placement-4/input.svelte b/test/validator/samples/css-invalid-global-placement-4/input.svelte index 42135a6163..93645baab2 100644 --- a/test/validator/samples/css-invalid-global-placement-4/input.svelte +++ b/test/validator/samples/css-invalid-global-placement-4/input.svelte @@ -2,4 +2,5 @@ .foo:global(div) { color: red; } + \ No newline at end of file