Skip to content

Commit

Permalink
css: preserve type selector range in ast
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Aug 6, 2023
1 parent dd2f047 commit 652da8f
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 14 deletions.
15 changes: 8 additions & 7 deletions internal/css_ast/css_ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion internal/css_parser/css_nesting.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}}}},
Expand Down
8 changes: 4 additions & 4 deletions internal/css_parser/css_parser_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}}}},
Expand Down Expand Up @@ -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(),
}
}

Expand Down
4 changes: 2 additions & 2 deletions internal/css_printer/css_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit 652da8f

Please sign in to comment.