Skip to content

Commit

Permalink
css: only show each :is warning once
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Jul 19, 2023
1 parent 47d4f89 commit afd73d1
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
9 changes: 8 additions & 1 deletion internal/bundler_tests/bundler_css_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,9 @@ func TestCSSNestingOldBrowser(t *testing.T) {
"/two-type-selectors.css": `a { .c b& { color: red; } }`,
"/two-parent-selectors.css": `a b { .c & { color: red; } }`,

// Make sure this only generates one warning (even though it generates ":is" three times)
"/only-one-warning.css": `.a, .b .c, .d { & > & { color: red; } }`,

"/nested-@layer.css": `a { @layer base { color: red; } }`,
"/nested-@media.css": `a { @media screen { color: red; } }`,
"/nested-ampersand-twice.css": `a { &, & { color: red; } }`,
Expand Down Expand Up @@ -970,6 +973,8 @@ func TestCSSNestingOldBrowser(t *testing.T) {
"/two-type-selectors.css",
"/two-parent-selectors.css",

"/only-one-warning.css",

"/nested-@layer.css",
"/nested-@media.css",
"/nested-ampersand-twice.css",
Expand Down Expand Up @@ -1012,7 +1017,9 @@ func TestCSSNestingOldBrowser(t *testing.T) {
UnsupportedCSSFeatures: compat.Nesting | compat.IsPseudoClass,
OriginalTargetEnv: "chrome10",
},
expectedScanLog: `two-parent-selectors.css: WARNING: Transforming this CSS nesting syntax is not supported in the configured target environment (chrome10)
expectedScanLog: `only-one-warning.css: WARNING: Transforming this CSS nesting syntax is not supported in the configured target environment (chrome10)
NOTE: The nesting transform for this case must generate an ":is(...)" but the configured target environment does not support the ":is" pseudo-class.
two-parent-selectors.css: WARNING: Transforming this CSS nesting syntax is not supported in the configured target environment (chrome10)
NOTE: The nesting transform for this case must generate an ":is(...)" but the configured target environment does not support the ":is" pseudo-class.
two-type-selectors.css: WARNING: Transforming this CSS nesting syntax is not supported in the configured target environment (chrome10)
NOTE: The nesting transform for this case must generate an ":is(...)" but the configured target environment does not support the ":is" pseudo-class.
Expand Down
14 changes: 14 additions & 0 deletions internal/bundler_tests/snapshots/snapshots_css.txt
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,20 @@ TestCSSNestingOldBrowser
color: red;
}

---------- /out/only-one-warning.css ----------
/* only-one-warning.css */
.a > .a,
.a > :is(.b .c),
.a > .d,
.b .c > .a,
.b .c > :is(.b .c),
.b .c > .d,
.d > .a,
.d > :is(.b .c),
.d > .d {
color: red;
}

---------- /out/nested-@layer.css ----------
/* nested-@layer.css */
@layer base {
Expand Down
16 changes: 13 additions & 3 deletions internal/css_parser/css_nesting.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ func (p *parser) substituteAmpersandsInCompoundSelector(
} else {
// ".foo .bar { :hover & {} }" => ":hover :is(.foo .bar) {}"
// ".foo .bar { > &:hover {} }" => ".foo .bar > :is(.foo .bar):hover {}"
p.reportNestingWithGeneratedPseudoClassIs(logger.Range{Loc: nestingSelectorLoc, Len: 1})
p.reportNestingWithGeneratedPseudoClassIs(nestingSelectorLoc)
single = css_ast.CompoundSelector{
SubclassSelectors: []css_ast.SubclassSelector{{
Loc: nestingSelectorLoc,
Expand All @@ -391,7 +391,7 @@ func (p *parser) substituteAmpersandsInCompoundSelector(
// Insert the type selector
if single.TypeSelector != nil {
if sel.TypeSelector != nil {
p.reportNestingWithGeneratedPseudoClassIs(logger.Range{Loc: nestingSelectorLoc, Len: 1})
p.reportNestingWithGeneratedPseudoClassIs(nestingSelectorLoc)
subclassSelectorPrefix = append(subclassSelectorPrefix, css_ast.SubclassSelector{
Loc: sel.TypeSelector.FirstLoc(),
Data: &css_ast.SSPseudoClassWithSelectorList{
Expand Down Expand Up @@ -465,12 +465,22 @@ func (p *parser) multipleComplexSelectorsToSingleComplexSelector(selectors []css
}
}

func (p *parser) reportNestingWithGeneratedPseudoClassIs(r logger.Range) {
func (p *parser) reportNestingWithGeneratedPseudoClassIs(nestingSelectorLoc logger.Loc) {
if p.options.unsupportedCSSFeatures.Has(compat.IsPseudoClass) {
_, didWarn := p.nestingWarnings[nestingSelectorLoc]
if didWarn {
// Only warn at each location once
return
}
if p.nestingWarnings == nil {
p.nestingWarnings = make(map[logger.Loc]struct{})
}
p.nestingWarnings[nestingSelectorLoc] = struct{}{}
text := "Transforming this CSS nesting syntax is not supported in the configured target environment"
if p.options.originalTargetEnv != "" {
text = fmt.Sprintf("%s (%s)", text, p.options.originalTargetEnv)
}
r := logger.Range{Loc: nestingSelectorLoc, Len: 1}
p.log.AddIDWithNotes(logger.MsgID_CSS_UnsupportedCSSNesting, logger.Warning, &p.tracker, r, text, []logger.MsgData{{
Text: "The nesting transform for this case must generate an \":is(...)\" but the configured target environment does not support the \":is\" pseudo-class."}})
}
Expand Down
1 change: 1 addition & 0 deletions internal/css_parser/css_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type parser struct {
symbols []ast.Symbol
localSymbolMap map[string]ast.Ref
globalSymbolMap map[string]ast.Ref
nestingWarnings map[logger.Loc]struct{}
tracker logger.LineColumnTracker
index int
end int
Expand Down

0 comments on commit afd73d1

Please sign in to comment.