Skip to content

Commit

Permalink
css: set shouldLowerNesting flag unconditionally
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Jul 19, 2023
1 parent 09d69e4 commit 7fcbdb8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
19 changes: 9 additions & 10 deletions internal/css_parser/css_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ loop:
}

// Lower CSS nesting if it's not supported (but only at the top level)
if context.isTopLevel && p.shouldLowerNesting {
if p.shouldLowerNesting && p.options.unsupportedCSSFeatures.Has(compat.Nesting) && context.isTopLevel {
rules = p.lowerNestingInRule(rule, rules)
} else {
rules = append(rules, rule)
Expand Down Expand Up @@ -447,7 +447,7 @@ loop:
}

// Lower CSS nesting if it's not supported (but only at the top level)
if context.isTopLevel && p.shouldLowerNesting {
if p.shouldLowerNesting && p.options.unsupportedCSSFeatures.Has(compat.Nesting) && context.isTopLevel {
rules = p.lowerNestingInRule(rule, rules)
} else {
rules = append(rules, rule)
Expand Down Expand Up @@ -505,6 +505,7 @@ func (p *parser) parseListOfDeclarations(opts listOfDeclarationsOpts) (list []cs

case css_lexer.TAtKeyword:
if p.inSelectorSubtree > 0 {
p.shouldLowerNesting = true
p.reportUseOfNesting(p.current().Range, false)
}
list = append(list, p.parseAtRule(atRuleContext{
Expand All @@ -523,6 +524,7 @@ func (p *parser) parseListOfDeclarations(opts listOfDeclarationsOpts) (list []cs
css_lexer.TDelimPlus,
css_lexer.TDelimGreaterThan,
css_lexer.TDelimTilde:
p.shouldLowerNesting = true
p.reportUseOfNesting(p.current().Range, false)
list = append(list, p.parseSelectorRuleFrom(p.index, false, parseSelectorOpts{isDeclarationContext: true}))
foundNesting = true
Expand Down Expand Up @@ -1474,15 +1476,12 @@ func (p *parser) expectValidLayerNameIdent() (string, bool) {
}

func (p *parser) reportUseOfNesting(r logger.Range, didWarnAlready bool) {
if p.options.unsupportedCSSFeatures.Has(compat.Nesting) {
p.shouldLowerNesting = true
if p.options.unsupportedCSSFeatures.Has(compat.IsPseudoClass) && !didWarnAlready {
text := "CSS nesting syntax is not supported in the configured target environment"
if p.options.originalTargetEnv != "" {
text = fmt.Sprintf("%s (%s)", text, p.options.originalTargetEnv)
}
p.log.AddID(logger.MsgID_CSS_UnsupportedCSSNesting, logger.Warning, &p.tracker, r, text)
if p.options.unsupportedCSSFeatures.Has(compat.Nesting) && p.options.unsupportedCSSFeatures.Has(compat.IsPseudoClass) && !didWarnAlready {
text := "CSS nesting syntax is not supported in the configured target environment"
if p.options.originalTargetEnv != "" {
text = fmt.Sprintf("%s (%s)", text, p.options.originalTargetEnv)
}
p.log.AddID(logger.MsgID_CSS_UnsupportedCSSNesting, logger.Warning, &p.tracker, r, text)
}
}

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 @@ -4,7 +4,6 @@ import (
"fmt"

"github.com/evanw/esbuild/internal/ast"
"github.com/evanw/esbuild/internal/compat"
"github.com/evanw/esbuild/internal/css_ast"
"github.com/evanw/esbuild/internal/css_lexer"
"github.com/evanw/esbuild/internal/logger"
Expand Down Expand Up @@ -211,9 +210,7 @@ func (p *parser) flattenLocalAndGlobalSelectors(list []css_ast.ComplexSelector,
})

// Make sure we report that nesting is present so that it can be lowered
if p.options.unsupportedCSSFeatures.Has(compat.Nesting) {
p.shouldLowerNesting = true
}
p.shouldLowerNesting = true
}

sel.Selectors = selectors
Expand Down Expand Up @@ -264,6 +261,7 @@ func (p *parser) parseComplexSelector(opts parseComplexSelectorOpts) (result css
r := p.current().Range
combinator := p.parseCombinator()
if combinator.Byte != 0 {
p.shouldLowerNesting = true
p.reportUseOfNesting(r, opts.isDeclarationContext)
p.eat(css_lexer.TWhitespace)
}
Expand Down Expand Up @@ -325,6 +323,7 @@ func (p *parser) parseCompoundSelector(opts parseComplexSelectorOpts) (sel css_a
// This is an extension: https://drafts.csswg.org/css-nesting-1/
hasLeadingNestingSelector := p.peek(css_lexer.TDelimAmpersand)
if hasLeadingNestingSelector {
p.shouldLowerNesting = true
p.reportUseOfNesting(p.current().Range, opts.isDeclarationContext)
sel.NestingSelectorLoc = ast.MakeIndex32(uint32(startLoc.Start))
p.advance()
Expand Down Expand Up @@ -439,6 +438,7 @@ subclassSelectors:

case css_lexer.TDelimAmpersand:
// This is an extension: https://drafts.csswg.org/css-nesting-1/
p.shouldLowerNesting = true
p.reportUseOfNesting(subclassToken.Range, sel.HasNestingSelector())
sel.NestingSelectorLoc = ast.MakeIndex32(uint32(subclassToken.Range.Loc.Start))
p.advance()
Expand Down

0 comments on commit 7fcbdb8

Please sign in to comment.