Skip to content

Commit

Permalink
css: source map tests can now be more accurate
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Jul 17, 2023
1 parent feea007 commit 9ff3860
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 44 deletions.
4 changes: 2 additions & 2 deletions internal/bundler_tests/snapshots/snapshots_loader.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ TestEmptyLoaderCSS
"version": 3,
"sources": ["entry.css"],
"sourcesContent": ["\n\t\t\t\t@import 'a.empty';\n\t\t\t\ta { background: url(b.empty) }\n\t\t\t"],
"mappings": ";AAEI;AAAA,EAAI,YAAY;AAAa;",
"mappings": ";AAEI;AAAI,cAAY;AAAa;",
"names": []
}

Expand Down Expand Up @@ -58,7 +58,7 @@ a {
"imports": [],
"exports": [],
"inputs": {},
"bytes": 208
"bytes": 203
},
"entry.css": {
"imports": [
Expand Down
59 changes: 25 additions & 34 deletions internal/css_printer/css_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,28 @@ func (p *printer) printRule(rule css_ast.Rule, indent int32, omitTrailingSemicol
p.printNewlinePastLineLimit(indent)
}

if p.options.AddSourceMappings {
shouldPrintMapping := true
if indent == 0 || p.options.MinifyWhitespace {
switch rule.Data.(type) {
case *css_ast.RSelector, *css_ast.RQualified, *css_ast.RBadDeclaration:
// These rules will begin with a potentially more accurate mapping. We
// shouldn't print a mapping here if there's no indent in between this
// mapping and the rule.
shouldPrintMapping = false
}
}
if shouldPrintMapping {
p.builder.AddSourceMapping(rule.Loc, "", p.css)
}
}

if !p.options.MinifyWhitespace {
p.printIndent(indent)
}

switch r := rule.Data.(type) {
case *css_ast.RAtCharset:
if p.options.AddSourceMappings {
p.builder.AddSourceMapping(rule.Loc, "", p.css)
}

// It's not valid to remove the space in between these two tokens
p.print("@charset ")

Expand All @@ -145,9 +157,6 @@ func (p *printer) printRule(rule css_ast.Rule, indent int32, omitTrailingSemicol
p.print(";")

case *css_ast.RAtImport:
if p.options.AddSourceMappings {
p.builder.AddSourceMapping(rule.Loc, "", p.css)
}
if p.options.MinifyWhitespace {
p.print("@import")
} else {
Expand All @@ -159,9 +168,6 @@ func (p *printer) printRule(rule css_ast.Rule, indent int32, omitTrailingSemicol
p.print(";")

case *css_ast.RAtKeyframes:
if p.options.AddSourceMappings {
p.builder.AddSourceMapping(rule.Loc, "", p.css)
}
p.print("@")
p.printIdent(r.AtToken, identNormal, mayNeedWhitespaceAfter)
p.print(" ")
Expand All @@ -176,12 +182,12 @@ func (p *printer) printRule(rule css_ast.Rule, indent int32, omitTrailingSemicol
}
indent++
for _, block := range r.Blocks {
if !p.options.MinifyWhitespace {
p.printIndent(indent)
}
if p.options.AddSourceMappings {
p.builder.AddSourceMapping(block.Loc, "", p.css)
}
if !p.options.MinifyWhitespace {
p.printIndent(indent)
}
for i, sel := range block.Selectors {
if i > 0 {
if p.options.MinifyWhitespace {
Expand All @@ -201,18 +207,15 @@ func (p *printer) printRule(rule css_ast.Rule, indent int32, omitTrailingSemicol
}
}
indent--
if !p.options.MinifyWhitespace {
p.printIndent(indent)
}
if p.options.AddSourceMappings && r.CloseBraceLoc.Start != 0 {
p.builder.AddSourceMapping(r.CloseBraceLoc, "", p.css)
}
if !p.options.MinifyWhitespace {
p.printIndent(indent)
}
p.print("}")

case *css_ast.RKnownAt:
if p.options.AddSourceMappings {
p.builder.AddSourceMapping(rule.Loc, "", p.css)
}
p.print("@")
whitespace := mayNeedWhitespaceAfter
if len(r.Prelude) == 0 {
Expand All @@ -233,9 +236,6 @@ func (p *printer) printRule(rule css_ast.Rule, indent int32, omitTrailingSemicol
}

case *css_ast.RUnknownAt:
if p.options.AddSourceMappings {
p.builder.AddSourceMapping(rule.Loc, "", p.css)
}
p.print("@")
whitespace := mayNeedWhitespaceAfter
if len(r.Prelude) == 0 {
Expand Down Expand Up @@ -270,9 +270,6 @@ func (p *printer) printRule(rule css_ast.Rule, indent int32, omitTrailingSemicol
p.printRuleBlock(r.Rules, indent, r.CloseBraceLoc)

case *css_ast.RDeclaration:
if p.options.AddSourceMappings {
p.builder.AddSourceMapping(rule.Loc, "", p.css)
}
p.printIdent(r.KeyText, identNormal, canDiscardWhitespaceAfter)
p.print(":")
hasWhitespaceAfter := p.printTokens(r.Value, printTokensOpts{
Expand All @@ -296,15 +293,9 @@ func (p *printer) printRule(rule css_ast.Rule, indent int32, omitTrailingSemicol
}

case *css_ast.RComment:
if p.options.AddSourceMappings {
p.builder.AddSourceMapping(rule.Loc, "", p.css)
}
p.printIndentedComment(indent, r.Text)

case *css_ast.RAtLayer:
if p.options.AddSourceMappings {
p.builder.AddSourceMapping(rule.Loc, "", p.css)
}
p.print("@layer")
for i, parts := range r.Names {
if i == 0 {
Expand Down Expand Up @@ -367,12 +358,12 @@ func (p *printer) printRuleBlock(rules []css_ast.Rule, indent int32, closeBraceL
p.printRule(decl, indent+1, omitTrailingSemicolon)
}

if !p.options.MinifyWhitespace {
p.printIndent(indent)
}
if p.options.AddSourceMappings && closeBraceLoc.Start != 0 {
p.builder.AddSourceMapping(closeBraceLoc, "", p.css)
}
if !p.options.MinifyWhitespace {
p.printIndent(indent)
}
p.print("}")
}

Expand Down
2 changes: 1 addition & 1 deletion scripts/js-api-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1576,7 +1576,7 @@ body {
},
},
[makePath(output + '.map')]: {
bytes: 335,
bytes: 325,
exports: [],
imports: [],
inputs: {},
Expand Down
7 changes: 0 additions & 7 deletions scripts/verify-source-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -507,13 +507,6 @@ async function check(kind, testCase, toSearch, { ext, flags, entryPoints, crlf,
const inLastLine = inLines[inLines.length - 1]
let inColumn = inLastLine.length

if (ext === 'css') {
const outMatch = /\s*content:\s*$/.exec(outLastLine)
const inMatch = /\bcontent:\s*$/.exec(inLastLine)
if (outMatch) outColumn -= outMatch[0].length
if (inMatch) inColumn -= inMatch[0].length
}

const expected = JSON.stringify({ source, line: inLine, column: inColumn })
const observed = JSON.stringify({ source, line, column })
recordCheck(expected === observed, `expected original position: ${expected}, observed original position: ${observed}`)
Expand Down

0 comments on commit 9ff3860

Please sign in to comment.