diff --git a/package.json b/package.json index 9c725a2fa..2b1022a4b 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,7 @@ "@glimmer/syntax": "0.84.3", "@iarna/toml": "2.2.5", "@prettier/is-es5-identifier-name": "0.2.0", - "@prettier/parse-srcset": "3.0.0", + "@prettier/parse-srcset": "3.1.0", "@typescript-eslint/typescript-estree": "6.2.1", "@typescript-eslint/visitor-keys": "6.2.1", "acorn": "8.10.0", diff --git a/src/language-html/embed/srcset.js b/src/language-html/embed/srcset.js index bf30adef5..36f0c9354 100644 --- a/src/language-html/embed/srcset.js +++ b/src/language-html/embed/srcset.js @@ -3,6 +3,8 @@ import { ifBreak, join, line } from "../../document/builders.js"; import { getUnescapedAttributeValue } from "../utils/index.js"; import { printExpand } from "./utils.js"; +/** @typedef {import("../../document/builders.js").Doc} Doc */ + function printSrcset(path /*, options*/) { if ( path.node.fullName === "srcset" && @@ -12,19 +14,20 @@ function printSrcset(path /*, options*/) { } } +const SRCSET_UNITS = { width: "w", height: "h", density: "x" }; +const SRCSET_TYPES = Object.keys(SRCSET_UNITS); function printSrcsetValue(value) { const srcset = parseSrcset(value); + const types = SRCSET_TYPES.filter((type) => + srcset.some((candidate) => Object.hasOwn(candidate, type)), + ); - const hasW = srcset.some(({ width }) => width); - const hasH = srcset.some(({ height }) => height); - const hasX = srcset.some(({ density }) => density); - - if (hasW + hasH + hasX > 1) { + if (types.length > 1) { throw new Error("Mixed descriptor in srcset is not supported"); } - const key = hasW ? "width" : hasH ? "height" : "density"; - const unit = hasW ? "w" : hasH ? "h" : "x"; + const [key] = types; + const unit = SRCSET_UNITS[key]; const urls = srcset.map((src) => src.source.value); const maxUrlLength = Math.max(...urls.map((url) => url.length)); @@ -42,6 +45,7 @@ function printSrcsetValue(value) { join( [",", line], urls.map((url, index) => { + /** @type {Doc[]} */ const parts = [url]; const descriptor = descriptors[index]; diff --git a/yarn.lock b/yarn.lock index 15272660d..f391a88b7 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1557,10 +1557,10 @@ __metadata: languageName: node linkType: hard -"@prettier/parse-srcset@npm:3.0.0": - version: 3.0.0 - resolution: "@prettier/parse-srcset@npm:3.0.0" - checksum: 18f988f2a1d2f6ea3f2dc3357587a3e7c64f3cbb68b8f25e8f6de390c7d42af5babc6651a621b45901bb35f62c8d72c364ad8c06802e729e1470672bce334094 +"@prettier/parse-srcset@npm:3.1.0": + version: 3.1.0 + resolution: "@prettier/parse-srcset@npm:3.1.0" + checksum: c3394d20609a3da0c205551cf5406746fa7e51060b0adaa5cb343815e78262a16801a2cb2c273964c51842d7a95043c57d86b1a03e17988c0594432d7585f412 languageName: node linkType: hard @@ -7003,7 +7003,7 @@ __metadata: "@glimmer/syntax": 0.84.3 "@iarna/toml": 2.2.5 "@prettier/is-es5-identifier-name": 0.2.0 - "@prettier/parse-srcset": 3.0.0 + "@prettier/parse-srcset": 3.1.0 "@types/estree": 1.0.1 "@types/file-entry-cache": 5.0.2 "@types/find-cache-dir": 3.2.1