Skip to content

Commit 17f5eb8

Browse files
authored
fix types (#1271)
1 parent cd84810 commit 17f5eb8

File tree

12 files changed

+378
-297
lines changed

12 files changed

+378
-297
lines changed

package-lock.json

Lines changed: 347 additions & 279 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/postcss-cascade-layers/dist/index.cjs

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

plugins/postcss-cascade-layers/dist/index.mjs

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

plugins/postcss-cascade-layers/src/desugar-nested-layers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { someAtRuleInTree } from './some-in-tree';
66

77
export function desugarNestedLayers(root: Container<ChildNode>, model: Model) {
88
while (someAtRuleInTree(root, (node) => {
9-
return node.nodes && someAtRuleInTree(node, (nested) => {
9+
return !!node.nodes && someAtRuleInTree(node, (nested) => {
1010
return isProcessableLayerRule(nested);
1111
});
1212
})) {

plugins/postcss-cascade-layers/src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,11 @@ const creator: PluginCreator<pluginOptions> = (opts?: pluginOptions) => {
165165
return;
166166
}
167167

168-
atRule.replaceWith(atRule.nodes);
168+
if (atRule.nodes) {
169+
atRule.replaceWith(atRule.nodes);
170+
} else {
171+
atRule.remove();
172+
}
169173
});
170174
}
171175

plugins/postcss-cascade-layers/src/sort-root-nodes.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import { isProcessableLayerRule } from './is-processable-layer-rule';
88
// Sort root nodes to apply the preferred order by layer priority for non-selector rules.
99
// Selector rules are adjusted by specificity.
1010
export function sortRootNodes(root: Container, model: Model) {
11+
if (!root.nodes) {
12+
return;
13+
}
14+
1115
// Separate selector rules from other rules
1216
root.walkAtRules((layerRule) => {
1317
if (!isProcessableLayerRule(layerRule)) {
@@ -61,11 +65,11 @@ export function sortRootNodes(root: Container, model: Model) {
6165
withSelectorRules.name = WITH_SELECTORS_LAYER_NAME;
6266

6367
layerRule.replaceWith(withSelectorRules, withoutSelectorRules);
64-
if (withSelectorRules.nodes.length === 0) {
68+
if (!withSelectorRules.nodes?.length) {
6569
withSelectorRules.remove();
6670
}
6771

68-
if (withoutSelectorRules.nodes.length === 0) {
72+
if (!withoutSelectorRules.nodes?.length) {
6973
withoutSelectorRules.remove();
7074
}
7175
});
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
"use strict";var e=require("postcss-value-parser");const t=/^(cross-fade|image|(repeating-)?(conic|linear|radial)-gradient|url|var)$/i;function getImage(n){return!(!n||!n.type)&&("string"===n.type?"url("+e.stringify(n)+")":!("function"!==n.type||!t.test(n.value))&&e.stringify(n))}const n=new Map([["dpcm",2.54],["dpi",1],["dppx",96],["x",96]]);function getMedia(e,t,n){if("boolean"==typeof e)return!1;const r=Math.floor(e/96*100)/100;return t.atRule({name:"media",params:`(-webkit-min-device-pixel-ratio: ${r}), (min-resolution: ${e}dpi)`,source:n.source})}function getMediaDPI(t){if(!t)return!1;if("word"!==t.type)return!1;if(!isNumericNode(t))return!1;const r=e.unit(t.value);if(!r)return!1;const i=n.get(r.unit.toLowerCase());return!!i&&Number(r.number)*i}function isNumericNode(t){if(!t||!t.value)return!1;try{return!1!==e.unit(t.value)}catch(e){return!1}}const handleInvalidation=(e,t,n)=>{if("warn"===e.oninvalid)e.decl.warn(e.result,t,{word:String(n)});else if("throw"===e.oninvalid)throw e.decl.error(t,{word:String(n)})},processImageSet=(t,n,r)=>{const i=n.parent;if(!i)return;const o=new Map,s=n.value;for(let i=0;i<t.length;i++){const{imageSetFunction:l,imageSetOptionNodes:u}=t[i],c=new Map,p=u.length;let d=-1;for(;d<p;){const t=d<0||(a=u[d],"div"===Object(a).type&&","===Object(a).value),i=getImage(u[d+1]),p=getMediaDPI(u[d+2]),f=getMedia(p,r.postcss,n);if(!t)return void handleInvalidation(r,"expected a comma",e.stringify(u));if(!i)return void handleInvalidation(r,"unexpected image",e.stringify(u));if(!f||!p||c.has(p))return void handleInvalidation(r,"unexpected resolution",e.stringify(u));if(c.set(p,f),o.has(p)){const t=o.get(p);t.value=t.value.replace(e.stringify(l),i.trim()),o.set(p,t)}else o.set(p,{atRule:f,value:s.replace(e.stringify(l),i.trim())});d+=3}}var a;for(const{atRule:e,value:t}of o.values()){const r=i.clone().removeAll(),o=n.clone({value:t});r.append(o),e.append(r)}const l=Array.from(o.keys()).sort(((e,t)=>e-t)).map((e=>o.get(e)?.atRule)).filter((e=>!!e));if(!l.length)return;const u=l[0],c=l.slice(1);c.length&&i.after(c);const p=u.nodes[0].nodes[0];n.cloneBefore({value:p.value.trim()}),r.preserve||(n.remove(),i.nodes.length||i.remove())};function hasFallback(e){const t=e.parent;if(!t)return!1;const n=e.prop.toLowerCase(),r=t.index(e);for(let e=0;e<r;e++){const r=t.nodes[e];if("decl"===r.type&&r.prop.toLowerCase()===n)return!0}return!1}const r=/(^|[^\w-])(-webkit-)?image-set\(/i,i=/^(-webkit-)?image-set$/i,creator=t=>{const n=!("preserve"in Object(t))||Boolean(t?.preserve),o="onInvalid"in Object(t)?t?.onInvalid:"ignore";if("oninvalid"in Object(t))throw new Error('"oninvalid" was changed to "onInvalid" to match other plugins with similar options');return{postcssPlugin:"postcss-image-set-function",Declaration(t,{result:s,postcss:a}){const l=t.value;if(!r.test(l))return;if(hasFallback(t))return;let u;try{u=e(l)}catch(e){t.warn(s,`Failed to parse value '${l}' as an image-set function. Leaving the original value intact.`)}if(void 0===u)return;const c=[];u.walk((n=>{if("function"!==n.type)return;if(!i.test(n.value))return;let r=!1;if(e.walk(n.nodes,(e=>{"function"===e.type&&i.test(e.value)&&(r=!0)})),r)return handleInvalidation({decl:t,oninvalid:o,result:s},"nested image-set functions are not allowed",e.stringify(n)),!1;const a=n.nodes.filter((e=>"comment"!==e.type&&"space"!==e.type));c.push({imageSetFunction:n,imageSetOptionNodes:a})})),processImageSet(c,t,{decl:t,oninvalid:o,preserve:n,result:s,postcss:a})}}};creator.postcss=!0,module.exports=creator;
1+
"use strict";var e=require("postcss-value-parser");const t=/^(cross-fade|image|(repeating-)?(conic|linear|radial)-gradient|url|var)$/i;function getImage(n){return!(!n||!n.type)&&("string"===n.type?"url("+e.stringify(n)+")":!("function"!==n.type||!t.test(n.value))&&e.stringify(n))}const n=new Map([["dpcm",2.54],["dpi",1],["dppx",96],["x",96]]);function getMedia(e,t,n){if("boolean"==typeof e)return!1;const r=Math.floor(e/96*100)/100;return t.atRule({name:"media",params:`(-webkit-min-device-pixel-ratio: ${r}), (min-resolution: ${e}dpi)`,source:n.source})}function getMediaDPI(t){if(!t)return!1;if("word"!==t.type)return!1;if(!isNumericNode(t))return!1;const r=e.unit(t.value);if(!r)return!1;const i=n.get(r.unit.toLowerCase());return!!i&&Number(r.number)*i}function isNumericNode(t){if(!t||!t.value)return!1;try{return!1!==e.unit(t.value)}catch(e){return!1}}const handleInvalidation=(e,t,n)=>{if("warn"===e.oninvalid)e.decl.warn(e.result,t,{word:String(n)});else if("throw"===e.oninvalid)throw e.decl.error(t,{word:String(n)})},processImageSet=(t,n,r)=>{const i=n.parent;if(!i)return;const o=new Map,a=n.value;for(let i=0;i<t.length;i++){const{imageSetFunction:l,imageSetOptionNodes:u}=t[i],c=new Map,p=u.length;let f=-1;for(;f<p;){const t=f<0||(s=u[f],"div"===Object(s).type&&","===Object(s).value),i=getImage(u[f+1]),p=getMediaDPI(u[f+2]),d=getMedia(p,r.postcss,n);if(!t)return void handleInvalidation(r,"expected a comma",e.stringify(u));if(!i)return void handleInvalidation(r,"unexpected image",e.stringify(u));if(!d||!p||c.has(p))return void handleInvalidation(r,"unexpected resolution",e.stringify(u));if(c.set(p,d),o.has(p)){const t=o.get(p);t.value=t.value.replace(e.stringify(l),i.trim()),o.set(p,t)}else o.set(p,{atRule:d,value:a.replace(e.stringify(l),i.trim())});f+=3}}var s;for(const{atRule:e,value:t}of o.values()){const r=i.clone().removeAll(),o=n.clone({value:t});r.append(o),e.append(r)}const l=Array.from(o.keys()).sort(((e,t)=>e-t)),u=l.map((e=>o.get(e)?.atRule)).filter((e=>!!e));if(!u.length)return;const c=o.get(l[0])?.value;if(!c)return;const p=u.slice(1);p.length&&i.after(p),n.cloneBefore({value:c.trim()}),r.preserve||(n.remove(),i.nodes?.length||i.remove())};function hasFallback(e){const t=e.parent;if(!t)return!1;const n=e.prop.toLowerCase(),r=t.index(e);for(let e=0;e<r;e++){const r=t.nodes[e];if("decl"===r.type&&r.prop.toLowerCase()===n)return!0}return!1}const r=/(^|[^\w-])(-webkit-)?image-set\(/i,i=/^(-webkit-)?image-set$/i,creator=t=>{const n=!("preserve"in Object(t))||Boolean(t?.preserve),o="onInvalid"in Object(t)?t?.onInvalid:"ignore";if("oninvalid"in Object(t))throw new Error('"oninvalid" was changed to "onInvalid" to match other plugins with similar options');return{postcssPlugin:"postcss-image-set-function",Declaration(t,{result:a,postcss:s}){const l=t.value;if(!r.test(l))return;if(hasFallback(t))return;let u;try{u=e(l)}catch(e){t.warn(a,`Failed to parse value '${l}' as an image-set function. Leaving the original value intact.`)}if(void 0===u)return;const c=[];u.walk((n=>{if("function"!==n.type)return;if(!i.test(n.value))return;let r=!1;if(e.walk(n.nodes,(e=>{"function"===e.type&&i.test(e.value)&&(r=!0)})),r)return handleInvalidation({decl:t,oninvalid:o,result:a},"nested image-set functions are not allowed",e.stringify(n)),!1;const s=n.nodes.filter((e=>"comment"!==e.type&&"space"!==e.type));c.push({imageSetFunction:n,imageSetOptionNodes:s})})),processImageSet(c,t,{decl:t,oninvalid:o,preserve:n,result:a,postcss:s})}}};creator.postcss=!0,module.exports=creator;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
import e from"postcss-value-parser";const t=/^(cross-fade|image|(repeating-)?(conic|linear|radial)-gradient|url|var)$/i;function getImage(n){return!(!n||!n.type)&&("string"===n.type?"url("+e.stringify(n)+")":!("function"!==n.type||!t.test(n.value))&&e.stringify(n))}const n=new Map([["dpcm",2.54],["dpi",1],["dppx",96],["x",96]]);function getMedia(e,t,n){if("boolean"==typeof e)return!1;const r=Math.floor(e/96*100)/100;return t.atRule({name:"media",params:`(-webkit-min-device-pixel-ratio: ${r}), (min-resolution: ${e}dpi)`,source:n.source})}function getMediaDPI(t){if(!t)return!1;if("word"!==t.type)return!1;if(!isNumericNode(t))return!1;const r=e.unit(t.value);if(!r)return!1;const i=n.get(r.unit.toLowerCase());return!!i&&Number(r.number)*i}function isNumericNode(t){if(!t||!t.value)return!1;try{return!1!==e.unit(t.value)}catch(e){return!1}}const handleInvalidation=(e,t,n)=>{if("warn"===e.oninvalid)e.decl.warn(e.result,t,{word:String(n)});else if("throw"===e.oninvalid)throw e.decl.error(t,{word:String(n)})},processImageSet=(t,n,r)=>{const i=n.parent;if(!i)return;const o=new Map,a=n.value;for(let i=0;i<t.length;i++){const{imageSetFunction:l,imageSetOptionNodes:u}=t[i],c=new Map,p=u.length;let f=-1;for(;f<p;){const t=f<0||(s=u[f],"div"===Object(s).type&&","===Object(s).value),i=getImage(u[f+1]),p=getMediaDPI(u[f+2]),d=getMedia(p,r.postcss,n);if(!t)return void handleInvalidation(r,"expected a comma",e.stringify(u));if(!i)return void handleInvalidation(r,"unexpected image",e.stringify(u));if(!d||!p||c.has(p))return void handleInvalidation(r,"unexpected resolution",e.stringify(u));if(c.set(p,d),o.has(p)){const t=o.get(p);t.value=t.value.replace(e.stringify(l),i.trim()),o.set(p,t)}else o.set(p,{atRule:d,value:a.replace(e.stringify(l),i.trim())});f+=3}}var s;for(const{atRule:e,value:t}of o.values()){const r=i.clone().removeAll(),o=n.clone({value:t});r.append(o),e.append(r)}const l=Array.from(o.keys()).sort(((e,t)=>e-t)).map((e=>o.get(e)?.atRule)).filter((e=>!!e));if(!l.length)return;const u=l[0],c=l.slice(1);c.length&&i.after(c);const p=u.nodes[0].nodes[0];n.cloneBefore({value:p.value.trim()}),r.preserve||(n.remove(),i.nodes.length||i.remove())};function hasFallback(e){const t=e.parent;if(!t)return!1;const n=e.prop.toLowerCase(),r=t.index(e);for(let e=0;e<r;e++){const r=t.nodes[e];if("decl"===r.type&&r.prop.toLowerCase()===n)return!0}return!1}const r=/(^|[^\w-])(-webkit-)?image-set\(/i,i=/^(-webkit-)?image-set$/i,creator=t=>{const n=!("preserve"in Object(t))||Boolean(t?.preserve),o="onInvalid"in Object(t)?t?.onInvalid:"ignore";if("oninvalid"in Object(t))throw new Error('"oninvalid" was changed to "onInvalid" to match other plugins with similar options');return{postcssPlugin:"postcss-image-set-function",Declaration(t,{result:a,postcss:s}){const l=t.value;if(!r.test(l))return;if(hasFallback(t))return;let u;try{u=e(l)}catch(e){t.warn(a,`Failed to parse value '${l}' as an image-set function. Leaving the original value intact.`)}if(void 0===u)return;const c=[];u.walk((n=>{if("function"!==n.type)return;if(!i.test(n.value))return;let r=!1;if(e.walk(n.nodes,(e=>{"function"===e.type&&i.test(e.value)&&(r=!0)})),r)return handleInvalidation({decl:t,oninvalid:o,result:a},"nested image-set functions are not allowed",e.stringify(n)),!1;const s=n.nodes.filter((e=>"comment"!==e.type&&"space"!==e.type));c.push({imageSetFunction:n,imageSetOptionNodes:s})})),processImageSet(c,t,{decl:t,oninvalid:o,preserve:n,result:a,postcss:s})}}};creator.postcss=!0;export{creator as default};
1+
import e from"postcss-value-parser";const t=/^(cross-fade|image|(repeating-)?(conic|linear|radial)-gradient|url|var)$/i;function getImage(n){return!(!n||!n.type)&&("string"===n.type?"url("+e.stringify(n)+")":!("function"!==n.type||!t.test(n.value))&&e.stringify(n))}const n=new Map([["dpcm",2.54],["dpi",1],["dppx",96],["x",96]]);function getMedia(e,t,n){if("boolean"==typeof e)return!1;const r=Math.floor(e/96*100)/100;return t.atRule({name:"media",params:`(-webkit-min-device-pixel-ratio: ${r}), (min-resolution: ${e}dpi)`,source:n.source})}function getMediaDPI(t){if(!t)return!1;if("word"!==t.type)return!1;if(!isNumericNode(t))return!1;const r=e.unit(t.value);if(!r)return!1;const i=n.get(r.unit.toLowerCase());return!!i&&Number(r.number)*i}function isNumericNode(t){if(!t||!t.value)return!1;try{return!1!==e.unit(t.value)}catch(e){return!1}}const handleInvalidation=(e,t,n)=>{if("warn"===e.oninvalid)e.decl.warn(e.result,t,{word:String(n)});else if("throw"===e.oninvalid)throw e.decl.error(t,{word:String(n)})},processImageSet=(t,n,r)=>{const i=n.parent;if(!i)return;const o=new Map,a=n.value;for(let i=0;i<t.length;i++){const{imageSetFunction:l,imageSetOptionNodes:u}=t[i],c=new Map,p=u.length;let f=-1;for(;f<p;){const t=f<0||(s=u[f],"div"===Object(s).type&&","===Object(s).value),i=getImage(u[f+1]),p=getMediaDPI(u[f+2]),d=getMedia(p,r.postcss,n);if(!t)return void handleInvalidation(r,"expected a comma",e.stringify(u));if(!i)return void handleInvalidation(r,"unexpected image",e.stringify(u));if(!d||!p||c.has(p))return void handleInvalidation(r,"unexpected resolution",e.stringify(u));if(c.set(p,d),o.has(p)){const t=o.get(p);t.value=t.value.replace(e.stringify(l),i.trim()),o.set(p,t)}else o.set(p,{atRule:d,value:a.replace(e.stringify(l),i.trim())});f+=3}}var s;for(const{atRule:e,value:t}of o.values()){const r=i.clone().removeAll(),o=n.clone({value:t});r.append(o),e.append(r)}const l=Array.from(o.keys()).sort(((e,t)=>e-t)),u=l.map((e=>o.get(e)?.atRule)).filter((e=>!!e));if(!u.length)return;const c=o.get(l[0])?.value;if(!c)return;const p=u.slice(1);p.length&&i.after(p),n.cloneBefore({value:c.trim()}),r.preserve||(n.remove(),i.nodes?.length||i.remove())};function hasFallback(e){const t=e.parent;if(!t)return!1;const n=e.prop.toLowerCase(),r=t.index(e);for(let e=0;e<r;e++){const r=t.nodes[e];if("decl"===r.type&&r.prop.toLowerCase()===n)return!0}return!1}const r=/(^|[^\w-])(-webkit-)?image-set\(/i,i=/^(-webkit-)?image-set$/i,creator=t=>{const n=!("preserve"in Object(t))||Boolean(t?.preserve),o="onInvalid"in Object(t)?t?.onInvalid:"ignore";if("oninvalid"in Object(t))throw new Error('"oninvalid" was changed to "onInvalid" to match other plugins with similar options');return{postcssPlugin:"postcss-image-set-function",Declaration(t,{result:a,postcss:s}){const l=t.value;if(!r.test(l))return;if(hasFallback(t))return;let u;try{u=e(l)}catch(e){t.warn(a,`Failed to parse value '${l}' as an image-set function. Leaving the original value intact.`)}if(void 0===u)return;const c=[];u.walk((n=>{if("function"!==n.type)return;if(!i.test(n.value))return;let r=!1;if(e.walk(n.nodes,(e=>{"function"===e.type&&i.test(e.value)&&(r=!0)})),r)return handleInvalidation({decl:t,oninvalid:o,result:a},"nested image-set functions are not allowed",e.stringify(n)),!1;const s=n.nodes.filter((e=>"comment"!==e.type&&"space"!==e.type));c.push({imageSetFunction:n,imageSetOptionNodes:s})})),processImageSet(c,t,{decl:t,oninvalid:o,preserve:n,result:a,postcss:s})}}};creator.postcss=!0;export{creator as default};

plugins/postcss-image-set-function/src/lib/process-image-set.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { getComma } from './get-comma';
33
import { getImage } from './get-image';
44
import { getMedia, getMediaDPI } from './get-media';
55
import { handleInvalidation } from './handle-invalidation';
6-
import type { AtRule, Container, Declaration, Result, Postcss } from 'postcss';
6+
import type { AtRule, Declaration, Result, Postcss } from 'postcss';
77
import type { Node } from 'postcss-value-parser';
88

99
type imageSetFunction = {
@@ -78,30 +78,35 @@ export const processImageSet = (imageSetFunctions: Array<imageSetFunction>, decl
7878
atRule.append(parentClone);
7979
}
8080

81-
const medias: Array<AtRule> = Array.from(mediasByDpr.keys())
82-
.sort((a, b) => a - b)
81+
const mediaSizes = Array.from(mediasByDpr.keys())
82+
.sort((a, b) => a - b);
83+
84+
const medias: Array<AtRule> = mediaSizes
8385
.map(params => mediasByDpr.get(params)?.atRule)
8486
.filter((x) => !!x) as Array<AtRule>;
8587

8688
if (!medias.length) {
8789
return;
8890
}
8991

90-
const smallestMedia = medias[0];
92+
const smallestValue = mediasByDpr.get(mediaSizes[0])?.value;
93+
if (!smallestValue) {
94+
return;
95+
}
96+
9197
const mediasWithoutSmallest = medias.slice(1);
9298

9399
if (mediasWithoutSmallest.length) {
94100
parent.after(mediasWithoutSmallest);
95101
}
96102

97-
const firstDecl = (smallestMedia.nodes[0] as Container).nodes[0] as Declaration;
98-
decl.cloneBefore({ value: firstDecl.value.trim() });
103+
decl.cloneBefore({ value: smallestValue.trim() });
99104

100105
if (!opts.preserve) {
101106
decl.remove();
102107

103108
// and then conditionally remove its parent
104-
if (!parent.nodes.length) {
109+
if (!parent.nodes?.length) {
105110
parent.remove();
106111
}
107112
}

0 commit comments

Comments
 (0)