Skip to content

Commit

Permalink
fix #1947: improve printing of invalid @import
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Jan 20, 2022
1 parent 0a16993 commit c000b61
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
# Changelog

## Unreleased

* Print invalid CSS differently ([#1947](https://github.com/evanw/esbuild/issues/1947))

This changes how esbuild prints nested `@import` statements that are missing a trailing `;`, which is invalid CSS. The result is still partially invalid CSS, but now printed in a better-looking way:

```css
/* Original code */
.bad { @import url("other") }
.red { background: red; }

/* Old output (with --minify) */
.bad{@import url(other) } .red{background: red;}}

/* New output (with --minify) */
.bad{@import"other";}.red{background:red}
```

## 0.14.11

* Fix a bug with enum inlining ([#1903](https://github.com/evanw/esbuild/issues/1903))
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 @@ -665,7 +665,8 @@ func (p *parser) parseAtRule(context atRuleContext) css_ast.Rule {
if path, r, ok := p.expectURLOrString(); ok {
importConditionsStart := p.index
for {
if kind := p.current().Kind; kind == css_lexer.TSemicolon || kind == css_lexer.TOpenBrace || kind == css_lexer.TEndOfFile {
if kind := p.current().Kind; kind == css_lexer.TSemicolon || kind == css_lexer.TOpenBrace ||
kind == css_lexer.TCloseBrace || kind == css_lexer.TEndOfFile {
break
}
p.parseComponentValue()
Expand Down
3 changes: 3 additions & 0 deletions internal/css_parser/css_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,9 @@ func TestAtImport(t *testing.T) {

expectParseError(t, "@import \"foo.css\" {}", "<stdin>: WARNING: Expected \";\"\n")
expectPrinted(t, "@import \"foo\"\na { color: red }\nb { color: blue }", "@import \"foo\" a { color: red }\nb {\n color: blue;\n}\n")

expectParseError(t, "a { @import \"foo.css\" }", "<stdin>: WARNING: Expected \";\"\n")
expectPrinted(t, "a { @import \"foo.css\" }", "a {\n @import \"foo.css\";\n}\n")
}

func TestLegalComment(t *testing.T) {
Expand Down

0 comments on commit c000b61

Please sign in to comment.