From 652da8f228329ab6c66b75880c34b6af761dc84b Mon Sep 17 00:00:00 2001 From: Evan Wallace Date: Thu, 27 Jul 2023 15:06:26 -0400 Subject: [PATCH] css: preserve type selector range in ast --- internal/css_ast/css_ast.go | 15 ++++++++------- internal/css_parser/css_nesting.go | 2 +- internal/css_parser/css_parser_selector.go | 8 ++++---- internal/css_printer/css_printer.go | 4 ++-- 4 files changed, 15 insertions(+), 14 deletions(-) diff --git a/internal/css_ast/css_ast.go b/internal/css_ast/css_ast.go index 4cfa02b9a86..922c0b5763d 100644 --- a/internal/css_ast/css_ast.go +++ b/internal/css_ast/css_ast.go @@ -797,7 +797,7 @@ func (sel CompoundSelector) IsInvalidBecauseEmpty() bool { func (sel CompoundSelector) FirstLoc() logger.Loc { var firstLoc ast.Index32 if sel.TypeSelector != nil { - firstLoc = ast.MakeIndex32(uint32(sel.TypeSelector.FirstLoc().Start)) + firstLoc = ast.MakeIndex32(uint32(sel.TypeSelector.Range().Loc.Start)) } else if len(sel.SubclassSelectors) > 0 { firstLoc = ast.MakeIndex32(uint32(sel.SubclassSelectors[0].Loc.Start)) } @@ -828,9 +828,9 @@ func (sel CompoundSelector) Clone() CompoundSelector { } type NameToken struct { - Text string - Loc logger.Loc - Kind css_lexer.T + Text string + Range logger.Range + Kind css_lexer.T } func (a NameToken) Equal(b NameToken) bool { @@ -845,11 +845,12 @@ type NamespacedName struct { Name NameToken } -func (n NamespacedName) FirstLoc() logger.Loc { +func (n NamespacedName) Range() logger.Range { if n.NamespacePrefix != nil { - return n.NamespacePrefix.Loc + loc := n.NamespacePrefix.Range.Loc + return logger.Range{Loc: loc, Len: n.Name.Range.End() - loc.Start} } - return n.Name.Loc + return n.Name.Range } func (n NamespacedName) Clone() NamespacedName { diff --git a/internal/css_parser/css_nesting.go b/internal/css_parser/css_nesting.go index f750b249f18..66dbc6c257b 100644 --- a/internal/css_parser/css_nesting.go +++ b/internal/css_parser/css_nesting.go @@ -393,7 +393,7 @@ func (p *parser) substituteAmpersandsInCompoundSelector( if sel.TypeSelector != nil { p.reportNestingWithGeneratedPseudoClassIs(nestingSelectorLoc) subclassSelectorPrefix = append(subclassSelectorPrefix, css_ast.SubclassSelector{ - Loc: sel.TypeSelector.FirstLoc(), + Loc: sel.TypeSelector.Range().Loc, Data: &css_ast.SSPseudoClassWithSelectorList{ Kind: css_ast.PseudoClassIs, Selectors: []css_ast.ComplexSelector{{Selectors: []css_ast.CompoundSelector{{TypeSelector: sel.TypeSelector}}}}, diff --git a/internal/css_parser/css_parser_selector.go b/internal/css_parser/css_parser_selector.go index ac66a864ef8..49083e4f8f5 100644 --- a/internal/css_parser/css_parser_selector.go +++ b/internal/css_parser/css_parser_selector.go @@ -113,7 +113,7 @@ func mergeCompoundSelectors(target *css_ast.CompoundSelector, source css_ast.Com // // But that just seems so obviously wrong that I'm not going to do that. target.SubclassSelectors = append(target.SubclassSelectors, css_ast.SubclassSelector{ - Loc: source.TypeSelector.FirstLoc(), + Loc: source.TypeSelector.Range().Loc, Data: &css_ast.SSPseudoClassWithSelectorList{ Kind: css_ast.PseudoClassIs, Selectors: []css_ast.ComplexSelector{{Selectors: []css_ast.CompoundSelector{{TypeSelector: source.TypeSelector}}}}, @@ -315,9 +315,9 @@ func (p *parser) parseComplexSelector(opts parseComplexSelectorOpts) (result css func (p *parser) nameToken() css_ast.NameToken { t := p.current() return css_ast.NameToken{ - Kind: t.Kind, - Loc: t.Range.Loc, - Text: p.decoded(), + Kind: t.Kind, + Range: t.Range, + Text: p.decoded(), } } diff --git a/internal/css_printer/css_printer.go b/internal/css_printer/css_printer.go index caeaf2e19d5..c2f4dd05432 100644 --- a/internal/css_printer/css_printer.go +++ b/internal/css_printer/css_printer.go @@ -547,7 +547,7 @@ func (p *printer) printNthIndex(index css_ast.NthIndex) { func (p *printer) printNamespacedName(nsName css_ast.NamespacedName, whitespace trailingWhitespace) { if prefix := nsName.NamespacePrefix; prefix != nil { if p.options.AddSourceMappings { - p.builder.AddSourceMapping(prefix.Loc, "", p.css) + p.builder.AddSourceMapping(prefix.Range.Loc, "", p.css) } switch prefix.Kind { @@ -563,7 +563,7 @@ func (p *printer) printNamespacedName(nsName css_ast.NamespacedName, whitespace } if p.options.AddSourceMappings { - p.builder.AddSourceMapping(nsName.Name.Loc, "", p.css) + p.builder.AddSourceMapping(nsName.Name.Range.Loc, "", p.css) } switch nsName.Name.Kind {