Skip to content

Commit

Permalink
css: can now calculate ranges of selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Aug 6, 2023
1 parent 6108301 commit 4e6dcbb
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
21 changes: 13 additions & 8 deletions internal/css_ast/css_ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -794,17 +794,22 @@ func (sel CompoundSelector) IsInvalidBecauseEmpty() bool {
return !sel.HasNestingSelector() && sel.TypeSelector == nil && len(sel.SubclassSelectors) == 0
}

func (sel CompoundSelector) FirstLoc() logger.Loc {
var firstLoc ast.Index32
func (sel CompoundSelector) Range() (r logger.Range) {
if sel.Combinator.Byte != 0 {
r = logger.Range{Loc: sel.Combinator.Loc, Len: 1}
}
if sel.TypeSelector != nil {
firstLoc = ast.MakeIndex32(uint32(sel.TypeSelector.Range().Loc.Start))
} else if len(sel.SubclassSelectors) > 0 {
firstLoc = ast.MakeIndex32(uint32(sel.SubclassSelectors[0].Range.Loc.Start))
r.ExpandBy(sel.TypeSelector.Range())
}
if firstLoc.IsValid() && (!sel.NestingSelectorLoc.IsValid() || firstLoc.GetIndex() < sel.NestingSelectorLoc.GetIndex()) {
return logger.Loc{Start: int32(firstLoc.GetIndex())}
if sel.NestingSelectorLoc.IsValid() {
r.ExpandBy(logger.Range{Loc: logger.Loc{Start: int32(sel.NestingSelectorLoc.GetIndex())}, Len: 1})
}
return logger.Loc{Start: int32(sel.NestingSelectorLoc.GetIndex())}
if len(sel.SubclassSelectors) > 0 {
for _, ss := range sel.SubclassSelectors {
r.ExpandBy(ss.Range)
}
}
return
}

func (sel CompoundSelector) Clone() CompoundSelector {
Expand Down
3 changes: 2 additions & 1 deletion internal/css_parser/css_parser_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func (p *parser) parseSelectorList(opts parseSelectorOpts) (list []css_ast.Compl
fmt.Sprintf("Unexpected \",\" inside %q", kind),
[]logger.MsgData{{Text: fmt.Sprintf("Different CSS tools behave differently in this case, so esbuild doesn't allow it. "+
"Either remove this comma or split this selector up into multiple comma-separated %q selectors instead.", kind)}})
return
}
} else {
skip:
Expand Down Expand Up @@ -208,7 +209,7 @@ func (p *parser) flattenLocalAndGlobalSelectors(list []css_ast.ComplexSelector,
if len(selectors) == 0 {
// Treat a bare ":global" or ":local" as a bare "&" nesting selector
selectors = append(selectors, css_ast.CompoundSelector{
NestingSelectorLoc: ast.MakeIndex32(uint32(sel.Selectors[0].FirstLoc().Start)),
NestingSelectorLoc: ast.MakeIndex32(uint32(sel.Selectors[0].Range().Loc.Start)),
WasEmptyFromLocalOrGlobal: true,
})

Expand Down
15 changes: 15 additions & 0 deletions internal/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,21 @@ func (r Range) End() int32 {
return r.Loc.Start + r.Len
}

func (a *Range) ExpandBy(b Range) {
if a.Len == 0 {
*a = b
} else {
end := a.End()
if n := b.End(); n > end {
end = n
}
if b.Loc.Start < a.Loc.Start {
a.Loc.Start = b.Loc.Start
}
a.Len = end - a.Loc.Start
}
}

type Span struct {
Text string
Range Range
Expand Down

0 comments on commit 4e6dcbb

Please sign in to comment.