Skip to content

Commit

Permalink
fix: fix 100s of (mostly) JSDoc issues found by TypeScript (#2514)
Browse files Browse the repository at this point in the history
  • Loading branch information
wojtekmaj authored Jan 19, 2024
1 parent e817223 commit a663fd8
Show file tree
Hide file tree
Showing 184 changed files with 1,208 additions and 810 deletions.
10 changes: 9 additions & 1 deletion packages/fns/src/asyncCompose.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@

import reverse from './reverse';

/**
* @typedef {Function} AsyncCompose
* @param {any} value
* @param {...any} args
* @returns {any} result
*/

/**
* Performs right-to-left function composition with async functions support
*
* @param {...any} functions
* @param {...Function} fns functions
* @returns {AsyncCompose} composed function
*/
const asyncCompose = (...fns) => async (value, ...args) => {
let result = value;
Expand Down
8 changes: 4 additions & 4 deletions packages/fns/src/capitalize.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/**
* Capitalize first letter of each word
*
* @param {String} string
* @returns {String} capitalized string
* @param {string} value string
* @returns {string} capitalized string
*/
const capitalize = value => {
const capitalize = (value) => {
if (!value) return value;
return value.replace(/(^|\s)\S/g, l => l.toUpperCase());
return value.replace(/(^|\s)\S/g, (l) => l.toUpperCase());
};

export default capitalize;
5 changes: 3 additions & 2 deletions packages/fns/src/castArray.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/**
* Casts value to array
*
* @param {any} value
* @returns {Array} casted value
* @template T
* @param {T|T[]} value value
* @returns {T[]} array
*/
const castArray = value => {
return Array.isArray(value) ? value : [value];
Expand Down
10 changes: 9 additions & 1 deletion packages/fns/src/compose.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@

import reverse from './reverse';

/**
* @typedef {Function} Compose
* @param {any} value
* @param {...any} args
* @returns {any} result
*/

/**
* Performs right-to-left function composition
*
* @param {...any} functions
* @param {...Function} fns functions
* @returns {Compose} composed function
*/
const compose = (...fns) => (value, ...args) => {
let result = value;
Expand Down
12 changes: 8 additions & 4 deletions packages/fns/src/matchPercent.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
const isPercent = value => /((-)?\d+\.?\d*)%/g.exec(value);
/**
* @param {string | number} value
* @returns {RegExpExecArray | null} match
*/
const isPercent = value => /((-)?\d+\.?\d*)%/g.exec(`${value}`);

/**
* Get percentage value of input
*
* @param {String} value
* @returns {Object} percent value (if matches)
* @param {string | number} value
* @returns {{ percent: number, value: number } | null} percent value (if matches)
*/
const matchPercent = value => {
const match = isPercent(value);

if (match) {
const f = parseFloat(match[1], 10);
const f = parseFloat(match[1]);
const percent = f / 100;

return { percent, value: f };
Expand Down
6 changes: 3 additions & 3 deletions packages/fns/src/upperFirst.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/**
* Capitalize first letter of string
*
* @param {String} string
* @returns {String} capitalized string
* @param {string} value string
* @returns {string} capitalized string
*/
const upperFirst = value => {
const upperFirst = (value) => {
if (!value) return value;
return value.charAt(0).toUpperCase() + value.slice(1);
};
Expand Down
19 changes: 10 additions & 9 deletions packages/layout/src/canvas/measureCanvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import isHeightAuto from '../page/isHeightAuto';

const SAFETY_HEIGHT = 10;

const getMax = values => Math.max(-Infinity, ...values);
const getMax = (values) => Math.max(-Infinity, ...values);

/**
* Helper object to predict canvas size
Expand Down Expand Up @@ -88,22 +88,23 @@ const measureCtx = () => {
ctx.linearGradient = nil;
ctx.radialGradient = nil;

ctx.getWidth = () => getMax(points.map(p => p[0]));
ctx.getHeight = () => getMax(points.map(p => p[1]));
ctx.getWidth = () => getMax(points.map((p) => p[0]));
ctx.getHeight = () => getMax(points.map((p) => p[1]));

return ctx;
};

/**
* @typedef {Function} MeasureCanvas
* @returns {{ width: number, height: number }} canvas width and height
*/

/**
* Yoga canvas measure function
*
* @param {Object} page
* @param {Object} node
* @param {Number} width
* @param {Number} widthMode
* @param {Number} height
* @param {Number} heightMode
* @returns {Object} canvas width and height
* @returns {MeasureCanvas} measure canvas
*/
const measureCanvas = (page, node) => () => {
const imageMargin = getMargin(node);
Expand All @@ -124,7 +125,7 @@ const measureCanvas = (page, node) => () => {
const width = ctx.getWidth();
const height = Math.min(pageArea, ctx.getHeight());

return { height, width };
return { width, height };
};

export default measureCanvas;
6 changes: 3 additions & 3 deletions packages/layout/src/image/getRatio.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/**
* Get image ratio
*
* @param {Object} image node
* @returns {Number} image ratio
* @param {Object} node image node
* @returns {number} image ratio
*/
const getRatio = node => {
const getRatio = (node) => {
return node.image?.data ? node.image.width / node.image.height : 1;
};

Expand Down
6 changes: 3 additions & 3 deletions packages/layout/src/image/getSource.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/**
* Get image source
*
* @param {Object} image node
* @returns {String | Object} image src
* @param {Object} node image node
* @returns {string | Object} image src
*/
const getSource = node =>
const getSource = (node) =>
node.props?.src || node.props?.source || node.props?.href;

export default getSource;
19 changes: 12 additions & 7 deletions packages/layout/src/image/measureImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,21 @@ import isHeightAuto from '../page/isHeightAuto';

const SAFETY_HEIGHT = 10;

/**
* @typedef {Function} MeasureImage
* @param {number} width
* @param {number} widthMode
* @param {number} height
* @param {number} heightMode
* @returns {{ width: number, height: number }} image width and height
*/

/**
* Yoga image measure function
*
* @param {Object} page
* @param {Object} node
* @param {Number} width
* @param {Number} widthMode
* @param {Number} height
* @param {Number} heightMode
* @returns {Object} image width and height
* @param {Object} page page
* @param {Object} node node
* @returns {MeasureImage} measure image
*/
const measureImage = (page, node) => (width, widthMode, height, heightMode) => {
const imageRatio = getRatio(node);
Expand Down
2 changes: 1 addition & 1 deletion packages/layout/src/image/resolveSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Also it handles factories and async sources.
*
* @param {string | Object | Function} src
* @returns {object} resolved src
* @returns {Promise<Object>} resolved src
*/
const resolveSource = async src => {
const source = typeof src === 'function' ? await src() : await src;
Expand Down
12 changes: 6 additions & 6 deletions packages/layout/src/node/createInstances.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { castArray } from '@react-pdf/fns';
import { TextInstance } from '@react-pdf/primitives';

const isString = value => typeof value === 'string';
const isString = (value) => typeof value === 'string';

const isNumber = value => typeof value === 'number';
const isNumber = (value) => typeof value === 'number';

const isFragment = value =>
const isFragment = (value) =>
value && value.type === Symbol.for('react.fragment');

/**
* Transforms a react element instance to internal element format.
*
* Can return multiple instances in the case of arrays or fragments.
*
* @param {Object} React element
* @returns {Array} parsed react elements
* @param {Object} element React element
* @returns {Object[]} parsed React elements
*/
const createInstances = element => {
const createInstances = (element) => {
if (!element) return [];

if (isString(element) || isNumber(element)) {
Expand Down
4 changes: 2 additions & 2 deletions packages/layout/src/node/getBorderWidth.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ const getComputedBorder = (yogaNode, edge) =>
* Get Yoga computed border width. Zero otherwise
*
* @param {Object} node
* @return {Object} border widths
* @returns {{ borderTopWidth: number, borderRightWidth: number, borderBottomWidth: number, borderLeftWidth: number }} border widths
*/
const getBorderWidth = node => {
const getBorderWidth = (node) => {
const { yogaNode } = node;

return {
Expand Down
4 changes: 2 additions & 2 deletions packages/layout/src/node/getDimension.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ const DEFAULT_DIMENSION = {
* Get Yoga computed dimensions. Zero otherwise
*
* @param {Object} node
* @return {Object} dimensions
* @returns {{ width: number, height: number }} dimensions
*/
const getDimension = node => {
const getDimension = (node) => {
const { yogaNode } = node;

if (!yogaNode) return DEFAULT_DIMENSION;
Expand Down
4 changes: 2 additions & 2 deletions packages/layout/src/node/getMargin.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ const getComputedMargin = (node, edge) => {
* Get Yoga computed magins. Zero otherwise
*
* @param {Object} node
* @return {Object} margins
* @returns {{ marginTop: number, marginRight: number, marginBottom: number, marginLeft: number }} margins
*/
const getMargin = node => {
const getMargin = (node) => {
const { style, box } = node;

const marginTop =
Expand Down
6 changes: 3 additions & 3 deletions packages/layout/src/node/getOrigin.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { isNil, matchPercent } from '@react-pdf/fns';

const getTransformStyle = s => node =>
const getTransformStyle = (s) => (node) =>
isNil(node.style?.[s]) ? '50%' : node.style?.[s];

/**
* Get node origin
*
* @param {Object} node
* @returns {Object} node origin
* @returns {{ left?: number, top?: number }} node origin
*/
const getOrigin = node => {
const getOrigin = (node) => {
if (!node.box) return {};

const { left, top, width, height } = node.box;
Expand Down
4 changes: 2 additions & 2 deletions packages/layout/src/node/getPadding.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ const getComputedPadding = (node, edge) => {
* Get Yoga computed paddings. Zero otherwise
*
* @param {Object} node
* @return {Object} paddings
* @returns {{ paddingTop: number, paddingRight: number, paddingBottom: number, paddingLeft: number }} paddings
*/
const getPadding = node => {
const getPadding = (node) => {
const { style, box } = node;

const paddingTop =
Expand Down
4 changes: 2 additions & 2 deletions packages/layout/src/node/getPosition.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
* Get Yoga computed position. Zero otherwise
*
* @param {Object} node
* @return {Object} position
* @returns {{ top: number, right: number, bottom: number, left: number }} position
*/
const getPosition = node => {
const getPosition = (node) => {
const { yogaNode } = node;

return {
Expand Down
18 changes: 14 additions & 4 deletions packages/layout/src/node/setAlign.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,23 @@ const ALIGN = {
'space-around': Yoga.ALIGN_SPACE_AROUND,
};

/**
* @typedef {Function} NodeInstanceWrapper
* @param {Object} node node instance
* @returns {Object} node instance
*/

/**
* @typedef {Function} AlignSetter
* @param {string} value align value
* @returns {NodeInstanceWrapper} node instance wrapper
*/

/**
* Set generic align attribute to node's Yoga instance
*
* @param {String} specific align property
* @param {String} align value
* @param {Object} node instance
* @return {Object} node instance
* @param {string} attr specific align property
* @returns {AlignSetter} align setter
*/
const setAlign = attr => value => node => {
const { yogaNode } = node;
Expand Down
4 changes: 2 additions & 2 deletions packages/layout/src/node/setAlignContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import setAlign from './setAlign';
/**
* Set align content attribute to node's Yoga instance
*
* @param {String} align value
* @param {string} align value
* @param {Object} node instance
* @return {Object} node instance
* @returns {Object} node instance
*/
const setAlignContent = setAlign('content');

Expand Down
4 changes: 2 additions & 2 deletions packages/layout/src/node/setAlignItems.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import setAlign from './setAlign';
/**
* Set align items attribute to node's Yoga instance
*
* @param {String} align value
* @param {string} align value
* @param {Object} node instance
* @return {Object} node instance
* @returns {Object} node instance
*/
const setAlignItems = setAlign('items');

Expand Down
4 changes: 2 additions & 2 deletions packages/layout/src/node/setAlignSelf.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import setAlign from './setAlign';
/**
* Set align self attribute to node's Yoga instance
*
* @param {String} align value
* @param {string} align value
* @param {Object} node instance
* @return {Object} node instance
* @returns {Object} node instance
*/
const setAlignSelf = setAlign('self');

Expand Down
Loading

0 comments on commit a663fd8

Please sign in to comment.