Skip to content

Commit

Permalink
fix #2892: css parser bug with @-rules without {
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Feb 6, 2023
1 parent d8a86ca commit 0d7c6f1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

## Unreleased

* Fix a CSS parser crash on invalid CSS ([#2892](https://github.com/evanw/esbuild/issues/2892))

Previously the following invalid CSS caused esbuild's parser to crash:

```css
@media screen
```

The crash was caused by trying to construct a helpful error message assuming that there was an opening `{` token, which is not the case here. This release fixes the crash.

* Inline TypeScript enums that are referenced before their declaration

Previously esbuild inlined enums within a TypeScript file from top to bottom, which meant that references to TypeScript enum members were only inlined within the same file if they came after the enum declaration. With this release, esbuild will now inline enums even when they are referenced before they are declared:
Expand Down
3 changes: 2 additions & 1 deletion internal/css_parser/css_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ func (p *parser) expectWithMatchingLoc(kind css_lexer.T, matchingLoc logger.Loc)
// Have a nice error message for forgetting a trailing semicolon or colon
text = fmt.Sprintf("Expected %s", expected)
t = p.at(p.index - 1)
} else if (kind == css_lexer.TCloseBrace || kind == css_lexer.TCloseBracket || kind == css_lexer.TCloseParen) && matchingLoc.Start != -1 {
} else if (kind == css_lexer.TCloseBrace || kind == css_lexer.TCloseBracket || kind == css_lexer.TCloseParen) &&
matchingLoc.Start != -1 && int(matchingLoc.Start)+1 <= len(p.source.Contents) {
// Have a nice error message for forgetting a closing brace/bracket/parenthesis
c := p.source.Contents[matchingLoc.Start : matchingLoc.Start+1]
text = fmt.Sprintf("Expected %s to go with %q", expected, c)
Expand Down
6 changes: 6 additions & 0 deletions internal/css_parser/css_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,12 @@ func TestAtRule(t *testing.T) {
"@-moz-document url-prefix() {\n h1 {\n color: green;\n }\n}\n")

expectPrinted(t, "@media foo { bar }", "@media foo {\n bar {\n }\n}\n")
expectParseError(t, "@media foo { bar }", "<stdin>: WARNING: Unexpected \"}\"\n")

expectParseError(t, "@media foo { bar {}", "<stdin>: WARNING: Expected \"}\" to go with \"{\"\n<stdin>: NOTE: The unbalanced \"{\" is here:\n")
expectParseError(t, "@media foo {", "<stdin>: WARNING: Expected \"}\" to go with \"{\"\n<stdin>: NOTE: The unbalanced \"{\" is here:\n")
expectParseError(t, "@media foo", "<stdin>: WARNING: Expected \"{\" but found end of file\n")
expectParseError(t, "@media", "<stdin>: WARNING: Expected \"{\" but found end of file\n")

// https://www.w3.org/TR/css-page-3/#syntax-page-selector
expectPrinted(t, `
Expand Down

0 comments on commit 0d7c6f1

Please sign in to comment.