|
| 1 | +/* |
| 2 | + * scss-analyzer-parse.test.ts |
| 3 | + * |
| 4 | + * Tests for the SCSS static-analysis parser wrapper, in particular the |
| 5 | + * text-level workarounds in src/core/sass/analyzer/parse.ts that shield |
| 6 | + * the brittle third-party scss-parser from valid-but-oddly-formatted SCSS. |
| 7 | + * Validates fixes for: |
| 8 | + * https://github.com/quarto-dev/quarto-cli/issues/14687 (colon with no space) |
| 9 | + * https://github.com/quarto-dev/quarto-cli/issues/11703 (double semicolon) |
| 10 | + * |
| 11 | + * Copyright (C) 2020-2026 Posit Software, PBC |
| 12 | + */ |
| 13 | + |
| 14 | +import { unitTest } from "../test.ts"; |
| 15 | +import { assert } from "testing/asserts"; |
| 16 | +import { parse } from "scss-parser"; |
| 17 | +import { makeParserModule } from "../../src/core/sass/analyzer/parse.ts"; |
| 18 | +import { cssVarsBlock } from "../../src/core/sass/add-css-vars.ts"; |
| 19 | + |
| 20 | +const { getSassAst } = makeParserModule(parse); |
| 21 | + |
| 22 | +// SCSS snippets that are valid (Dart Sass compiles them) but used to make |
| 23 | +// the raw scss-parser throw. Each entry documents a shape the workarounds |
| 24 | +// in parse.ts must keep rescuing. |
| 25 | +const parseableSnippets: [string, string][] = [ |
| 26 | + // issue #14687: declaration on the same line as `{`, no space after colon |
| 27 | + [".example {width:100px;}", "same-line declaration, numeric value"], |
| 28 | + [".example { width:100px; }", "same-line declaration, spaces inside braces"], |
| 29 | + [".example {width:$foo;}", "same-line declaration, variable value"], |
| 30 | + [".example {width:100px}", "same-line declaration, missing semicolon"], |
| 31 | + [".example {color:#fff;}", "same-line declaration, hex color value"], |
| 32 | + [".example {width:100%;}", "same-line declaration, percentage value"], |
| 33 | + [".example {margin:0;}", "same-line declaration, bare zero value"], |
| 34 | + [".example {width:.5em;}", "same-line declaration, leading-dot value"], |
| 35 | + [ |
| 36 | + ".example {width:100px;height:200px;}", |
| 37 | + "second declaration follows a semicolon on the same line", |
| 38 | + ], |
| 39 | + ["a:hover {width:100px;}", "pseudo-class selector before a one-line rule"], |
| 40 | + [ |
| 41 | + "@media (min-width:600px) { body { margin:0; } }", |
| 42 | + "one-line media query with colon-no-space declarations", |
| 43 | + ], |
| 44 | + [ |
| 45 | + ".x {background:url(data:image/png;base64,AAAA);}", |
| 46 | + "data: url payload containing colons and semicolons", |
| 47 | + ], |
| 48 | + ['.x {content:"{a:1}";}', "string value containing brace/colon characters"], |
| 49 | + // issue #11703: consecutive semicolons (empty statements) inside a block |
| 50 | + ["a { b: 1;; }", "double semicolon after a declaration"], |
| 51 | + ["a { b: 1; ; }", "semicolons separated by a space"], |
| 52 | + ["a { b: 1;;; }", "triple semicolon"], |
| 53 | + ["a { b: 1;\n;\n}", "semicolons separated by a newline"], |
| 54 | + [ |
| 55 | + ".hanged { max-height: 100% !important;; }", |
| 56 | + "double semicolon after !important (issue #11703 shape)", |
| 57 | + ], |
| 58 | + // shapes that already worked before issue #14687; guard against regressions |
| 59 | + [".example {\n width:100px;\n}", "line-start declaration, no space after colon"], |
| 60 | + [".foo { &:hover {color:red;} }", "nested parent-selector rule on one line"], |
| 61 | + ["$brand: #ff0000;\n.example {width:100px;}", "variable assignment plus one-line rule"], |
| 62 | +]; |
| 63 | + |
| 64 | +unitTest( |
| 65 | + "scss-analyzer - parses valid SCSS shapes that break raw scss-parser (issues #14687, #11703)", |
| 66 | + // deno-lint-ignore require-await |
| 67 | + async () => { |
| 68 | + for (const [snippet, description] of parseableSnippets) { |
| 69 | + try { |
| 70 | + const ast = getSassAst(snippet); |
| 71 | + assert( |
| 72 | + ast.type === "stylesheet", |
| 73 | + `Expected a stylesheet AST for: ${description}`, |
| 74 | + ); |
| 75 | + } catch (e) { |
| 76 | + const message = e instanceof Error ? e.message : String(e); |
| 77 | + throw new Error( |
| 78 | + `getSassAst failed on "${snippet}" (${description}): ${message}`, |
| 79 | + ); |
| 80 | + } |
| 81 | + } |
| 82 | + }, |
| 83 | +); |
| 84 | + |
| 85 | +unitTest( |
| 86 | + "scss-analyzer - cssVarsBlock exports colors despite one-line rules (issue #14687)", |
| 87 | + // deno-lint-ignore require-await |
| 88 | + async () => { |
| 89 | + const scss = [ |
| 90 | + "$brand: #ff0000;", |
| 91 | + "$body-bg: $brand;", |
| 92 | + ".example {width:100px;}", |
| 93 | + ].join("\n"); |
| 94 | + const block = cssVarsBlock(scss); |
| 95 | + assert( |
| 96 | + block.includes("--quarto-scss-export-brand:"), |
| 97 | + `Expected $brand to be exported as a CSS variable, got:\n${block}`, |
| 98 | + ); |
| 99 | + }, |
| 100 | +); |
0 commit comments