Skip to content

Commit

Permalink
Bump @prettier/parse-srcset to v3 (#14665)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Apr 5, 2023
1 parent 78b0a6b commit e4349ca
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"@glimmer/syntax": "0.84.3",
"@iarna/toml": "2.2.5",
"@prettier/is-es5-identifier-name": "0.1.0",
"@prettier/parse-srcset": "2.0.2",
"@prettier/parse-srcset": "3.0.0",
"@typescript-eslint/typescript-estree": "5.55.0",
"@typescript-eslint/visitor-keys": "5.55.0",
"acorn": "8.8.2",
Expand Down
22 changes: 10 additions & 12 deletions src/language-html/embed/srcset.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,28 @@ function printSrcset(path /*, options*/) {
function printSrcsetValue(value) {
const srcset = parseSrcset(value);

const hasW = srcset.some(({ w }) => w);
const hasH = srcset.some(({ h }) => h);
const hasX = srcset.some(({ d }) => d);
const hasW = srcset.some(({ width }) => width);
const hasH = srcset.some(({ height }) => height);
const hasX = srcset.some(({ density }) => density);

if (hasW + hasH + hasX > 1) {
throw new Error("Mixed descriptor in srcset is not supported");
}

const key = hasW ? "w" : hasH ? "h" : "d";
const key = hasW ? "width" : hasH ? "height" : "density";
const unit = hasW ? "w" : hasH ? "h" : "x";

const getMax = (values) => Math.max(...values);
const urls = srcset.map((src) => src.source.value);
const maxUrlLength = Math.max(...urls.map((url) => url.length));

const urls = srcset.map((src) => src.url);
const maxUrlLength = getMax(urls.map((url) => url.length));

const descriptors = srcset
.map((src) => src[key])
.map((descriptor) => (descriptor ? descriptor.toString() : ""));
const descriptors = srcset.map((src) =>
src[key] ? String(src[key].value) : ""
);
const descriptorLeftLengths = descriptors.map((descriptor) => {
const index = descriptor.indexOf(".");
return index === -1 ? descriptor.length : index;
});
const maxDescriptorLeftLength = getMax(descriptorLeftLengths);
const maxDescriptorLeftLength = Math.max(...descriptorLeftLengths);

return printExpand(
join(
Expand Down
10 changes: 10 additions & 0 deletions tests/format/html/srcset/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ srcset="
"
alt=""/>
<img src="a"
srcset=",,,"/>
<img src="a"
srcset=" "/>
=====================================output=====================================
<img
src="a"
Expand All @@ -37,5 +43,9 @@ srcset="
alt=""
/>
<img src="a" srcset=",,," />
<img src="a" srcset=" " />
================================================================================
`;
6 changes: 6 additions & 0 deletions tests/format/html/srcset/invalid.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,9 @@
should-not-format ,, should-not-format 0q,,,
"
alt=""/>

<img src="a"
srcset=",,,"/>

<img src="a"
srcset=" "/>
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1547,10 +1547,10 @@ __metadata:
languageName: node
linkType: hard

"@prettier/parse-srcset@npm:2.0.2":
version: 2.0.2
resolution: "@prettier/parse-srcset@npm:2.0.2"
checksum: defcb9e122b2bba256aed1703442c78bfbc884924c2a5ea5342fc301b10557c9ece8fe02400223c22de485ba5e8fe679610c4b8992abb19f2e260acabf67ef8e
"@prettier/parse-srcset@npm:3.0.0":
version: 3.0.0
resolution: "@prettier/parse-srcset@npm:3.0.0"
checksum: 18f988f2a1d2f6ea3f2dc3357587a3e7c64f3cbb68b8f25e8f6de390c7d42af5babc6651a621b45901bb35f62c8d72c364ad8c06802e729e1470672bce334094
languageName: node
linkType: hard

Expand Down Expand Up @@ -6721,7 +6721,7 @@ __metadata:
"@glimmer/syntax": 0.84.3
"@iarna/toml": 2.2.5
"@prettier/is-es5-identifier-name": 0.1.0
"@prettier/parse-srcset": 2.0.2
"@prettier/parse-srcset": 3.0.0
"@types/estree": 1.0.0
"@types/file-entry-cache": 5.0.2
"@types/find-cache-dir": 3.2.1
Expand Down

0 comments on commit e4349ca

Please sign in to comment.