Skip to content

Commit

Permalink
fix #3197: css nesting warning in selector subtree
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Jun 26, 2023
1 parent b677152 commit 8a15942
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

This fixes a regression in version 0.18.4 where combining `--minify-syntax` with `--keep-names` could cause expressions with side effects after a function declaration to be considered side-effect free for tree shaking purposes. The reason was because `--keep-names` generates an expression statement containing a call to a helper function after the function declaration with a special flag that makes the function call able to be tree shaken, and then `--minify-syntax` could potentially merge that expression statement with following expressions without clearing the flag. This release fixes the bug by clearing the flag when merging expression statements together.

* Fix an incorrect warning about CSS nesting ([#3197](https://github.com/evanw/esbuild/issues/3197))

A warning is currently generated when transforming nested CSS to a browser that doesn't support `:is()` because transformed nested CSS may need to use that feature to represent nesting. This was previously always triggered when an at-rule was encountered in a declaration context. Typically the only case you would encounter this is when using CSS nesting within a selector rule. However, there is a case where that's not true: when using a margin at-rule such as `@top-left` within `@page`. This release avoids incorrectly generating a warning in this case by checking that the at-rule is within a selector rule before generating a warning.

## 0.18.9

* Fix `await using` declarations inside `async` generator functions
Expand Down
5 changes: 5 additions & 0 deletions internal/bundler_tests/bundler_css_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,9 @@ func TestCSSNestingOldBrowser(t *testing.T) {
"/media-hash.css": `@media screen { #id { color: red; } }`,
"/media-plus.css": `@media screen { + b { color: red; } }`,
"/media-tilde.css": `@media screen { ~ b { color: red; } }`,

// See: https://github.com/evanw/esbuild/issues/3197
"/page-no-warning.css": `@page { @top-left { background: red } }`,
},
entryPaths: []string{
"/nested-@layer.css",
Expand Down Expand Up @@ -768,6 +771,8 @@ func TestCSSNestingOldBrowser(t *testing.T) {
"/media-hash.css",
"/media-plus.css",
"/media-tilde.css",

"/page-no-warning.css",
},
options: config.Options{
Mode: config.ModeBundle,
Expand Down
8 changes: 8 additions & 0 deletions internal/bundler_tests/snapshots/snapshots_css.txt
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,14 @@ a,
}
}

---------- /out/page-no-warning.css ----------
/* page-no-warning.css */
@page {
@top-left {
background: red;
}
}

================================================================================
TestDataURLImportURLInCSS
---------- /out/entry.css ----------
Expand Down
7 changes: 6 additions & 1 deletion 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 {
index int
end int
legalCommentIndex int
inSelectorSubtree int
prevError logger.Loc
options Options
shouldLowerNesting bool
Expand Down Expand Up @@ -405,7 +406,9 @@ func (p *parser) parseListOfDeclarations(opts listOfDeclarationsOpts) (list []cs
return

case css_lexer.TAtKeyword:
p.reportUseOfNesting(p.current().Range, false)
if p.inSelectorSubtree > 0 {
p.reportUseOfNesting(p.current().Range, false)
}
list = append(list, p.parseAtRule(atRuleContext{
isDeclarationList: true,
canInlineNoOpNesting: opts.canInlineNoOpNesting,
Expand Down Expand Up @@ -1725,9 +1728,11 @@ func (p *parser) parseSelectorRuleFrom(preludeStart int, isTopLevel bool, opts p
selector := css_ast.RSelector{Selectors: list}
matchingLoc := p.current().Range.Loc
if p.expect(css_lexer.TOpenBrace) {
p.inSelectorSubtree++
selector.Rules = p.parseListOfDeclarations(listOfDeclarationsOpts{
canInlineNoOpNesting: canInlineNoOpNesting,
})
p.inSelectorSubtree--
p.expectWithMatchingLoc(css_lexer.TCloseBrace, matchingLoc)
return css_ast.Rule{Loc: p.tokens[preludeStart].Range.Loc, Data: &selector}
}
Expand Down

0 comments on commit 8a15942

Please sign in to comment.