Skip to content

chore: bulk update all jsdoc and jsdoctypes #2114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 14 additions & 32 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,34 +86,28 @@ const config = {
/**
* Convert SVG (XML) string to SVG-as-JS object.
*
* @type {(data: string, from?: string) => XastRoot}
* @param {string} data
* @param {string=} from
* @returns {XastRoot}
*/
export const parseSvg = (data, from) => {
const sax = SAX.parser(config.strict, config);
/**
* @type {XastRoot}
*/
/** @type {XastRoot} */
const root = { type: 'root', children: [] };
/**
* @type {XastParent}
*/
/** @type {XastParent} */
let current = root;
/**
* @type {XastParent[]}
*/
/** @type {XastParent[]} */
const stack = [root];

/**
* @type {(node: XastChild) => void}
* @param {XastChild} node
*/
const pushToContent = (node) => {
current.children.push(node);
};

sax.ondoctype = (doctype) => {
/**
* @type {XastDoctype}
*/
/** @type {XastDoctype} */
const node = {
type: 'doctype',
// TODO parse doctype for name, public and system to match xast
Expand All @@ -135,9 +129,7 @@ export const parseSvg = (data, from) => {
};

sax.onprocessinginstruction = (data) => {
/**
* @type {XastInstruction}
*/
/** @type {XastInstruction} */
const node = {
type: 'instruction',
name: data.name,
Expand All @@ -147,9 +139,7 @@ export const parseSvg = (data, from) => {
};

sax.oncomment = (comment) => {
/**
* @type {XastComment}
*/
/** @type {XastComment} */
const node = {
type: 'comment',
value: comment.trim(),
Expand All @@ -158,9 +148,7 @@ export const parseSvg = (data, from) => {
};

sax.oncdata = (cdata) => {
/**
* @type {XastCdata}
*/
/** @type {XastCdata} */
const node = {
type: 'cdata',
value: cdata,
Expand All @@ -169,9 +157,7 @@ export const parseSvg = (data, from) => {
};

sax.onopentag = (data) => {
/**
* @type {XastElement}
*/
/** @type {XastElement} */
let element = {
type: 'element',
name: data.name,
Expand All @@ -190,18 +176,14 @@ export const parseSvg = (data, from) => {
if (current.type === 'element') {
// prevent trimming of meaningful whitespace inside textual tags
if (textElems.has(current.name)) {
/**
* @type {XastText}
*/
/** @type {XastText} */
const node = {
type: 'text',
value: text,
};
pushToContent(node);
} else if (/\S/.test(text)) {
/**
* @type {XastText}
*/
/** @type {XastText} */
const node = {
type: 'text',
value: text.trim(),
Expand Down
46 changes: 20 additions & 26 deletions lib/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import { removeLeadingZero, toFixed } from './svgo/tools.js';
* @typedef {import('./types.js').PathDataItem} PathDataItem
* @typedef {import('./types.js').PathDataCommand} PathDataCommand
* @typedef {'none' | 'sign' | 'whole' | 'decimal_point' | 'decimal' | 'e' | 'exponent_sign' | 'exponent'} ReadNumberState
*
* @typedef StringifyPathDataOptions
* @property {PathDataItem[]} pathData
* @property {number=} precision
* @property {boolean=} disableSpaceAfterFlags
*/

// Based on https://www.w3.org/TR/SVG11/paths.html#PathDataBNF
Expand Down Expand Up @@ -32,7 +37,8 @@ const argsCountPerCommand = {
};

/**
* @type {(c: string) => c is PathDataCommand}
* @param {string} c
* @returns {c is PathDataCommand}
*/
const isCommand = (c) => {
return c in argsCountPerCommand;
Expand All @@ -59,7 +65,9 @@ const isDigit = (c) => {
};

/**
* @type {(string: string, cursor: number) => [number, ?number]}
* @param {string} string
* @param {number} cursor
* @returns {[number, ?number]}
*/
const readNumber = (string, cursor) => {
let i = cursor;
Expand Down Expand Up @@ -130,13 +138,9 @@ const readNumber = (string, cursor) => {
* @returns {PathDataItem[]}
*/
export const parsePathData = (string) => {
/**
* @type {PathDataItem[]}
*/
/** @type {PathDataItem[]} */
const pathData = [];
/**
* @type {?PathDataCommand}
*/
/** @type {?PathDataCommand} */
let command = null;
let args = /** @type {number[]} */ ([]);
let argsCount = 0;
Expand Down Expand Up @@ -232,10 +236,9 @@ export const parsePathData = (string) => {
};

/**
* @type {(number: number, precision?: number) => {
* roundedStr: string,
* rounded: number
* }}
* @param {number} number
* @param {number=} precision
* @returns {{ roundedStr: string, rounded: number }}
*/
const roundAndStringify = (number, precision) => {
if (precision != null) {
Expand All @@ -252,12 +255,11 @@ const roundAndStringify = (number, precision) => {
* Elliptical arc large-arc and sweep flags are rendered with spaces
* because many non-browser environments are not able to parse such paths
*
* @type {(
* command: string,
* args: number[],
* precision?: number,
* disableSpaceAfterFlags?: boolean
* ) => string}
* @param {string} command
* @param {number[]} args
* @param {number=} precision
* @param {boolean=} disableSpaceAfterFlags
* @returns {string}
*/
const stringifyArgs = (command, args, precision, disableSpaceAfterFlags) => {
let result = '';
Expand Down Expand Up @@ -288,14 +290,6 @@ const stringifyArgs = (command, args, precision, disableSpaceAfterFlags) => {
return result;
};

/**
* @typedef {{
* pathData: PathDataItem[];
* precision?: number;
* disableSpaceAfterFlags?: boolean;
* }} StringifyPathDataOptions
*/

/**
* @param {StringifyPathDataOptions} options
* @returns {string}
Expand Down
8 changes: 2 additions & 6 deletions lib/path.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,7 @@ describe('stringify path data', () => {
).toBe('M.1 1e-7 2 2');
});
it('should configure precision', () => {
/**
* @type {PathDataItem[]}
*/
/** @type {PathDataItem[]} */
const pathData = [
{ command: 'M', args: [0, -1.9876] },
{ command: 'L', args: [0.3, 3.14159265] },
Expand All @@ -177,9 +175,7 @@ describe('stringify path data', () => {
).toBe('M0-2 0 3 0-3 100 200');
});
it('allows to avoid spaces after arc flags', () => {
/**
* @type {PathDataItem[]}
*/
/** @type {PathDataItem[]} */
const pathData = [
{ command: 'M', args: [0, 0] },
{ command: 'A', args: [50, 50, 10, 1, 0, 0.2, 20] },
Expand Down
61 changes: 43 additions & 18 deletions lib/stringifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@ import { textElems } from '../plugins/_collections.js';
* @typedef {import('./types.js').XastCdata} XastCdata
* @typedef {import('./types.js').XastComment} XastComment
* @typedef {import('./types.js').StringifyOptions} StringifyOptions
* @typedef {{
* indent: string,
* textContext: ?XastElement,
* indentLevel: number,
* }} State
* @typedef {Required<StringifyOptions>} Options
*
* @typedef State
* @property {string} indent
* @property {?XastElement} textContext
* @property {number} indentLevel
*/

/**
* @type {(char: string) => string}
* @param {string} char
* @returns {string}
*/
const encodeEntity = (char) => {
return entities[char];
Expand Down Expand Up @@ -65,9 +66,11 @@ const entities = {
};

/**
* convert XAST to SVG string
* Converts XAST to SVG string.
*
* @type {(data: XastRoot, userOptions?: StringifyOptions) => string}
* @param {XastRoot} data
* @param {StringifyOptions=} userOptions
* @returns {string}
*/
export const stringifySvg = (data, userOptions = {}) => {
/** @type {Options} */
Expand Down Expand Up @@ -104,7 +107,10 @@ export const stringifySvg = (data, userOptions = {}) => {
};

/**
* @type {(node: XastParent, config: Options, state: State) => string}
* @param {XastParent} data
* @param {Options} config
* @param {State} state
* @returns {string}
*/
const stringifyNode = (data, config, state) => {
let svg = '';
Expand Down Expand Up @@ -134,9 +140,11 @@ const stringifyNode = (data, config, state) => {
};

/**
* create indent string in accordance with the current node level.
* Create indent string in accordance with the current node level.
*
* @type {(config: Options, state: State) => string}
* @param {Options} config
* @param {State} state
* @returns {string}
*/
const createIndent = (config, state) => {
let indent = '';
Expand All @@ -147,14 +155,18 @@ const createIndent = (config, state) => {
};

/**
* @type {(node: XastDoctype, config: Options) => string}
* @param {XastDoctype} node
* @param {Options} config
* @returns {string}
*/
const stringifyDoctype = (node, config) => {
return config.doctypeStart + node.data.doctype + config.doctypeEnd;
};

/**
* @type {(node: XastInstruction, config: Options) => string}
* @param {XastInstruction} node
* @param {Options} config
* @returns {string}
*/
const stringifyInstruction = (node, config) => {
return (
Expand All @@ -163,14 +175,19 @@ const stringifyInstruction = (node, config) => {
};

/**
* @type {(node: XastComment, config: Options) => string}
* @param {XastComment} node
* @param {Options} config
* @returns {string}
*/
const stringifyComment = (node, config) => {
return config.commentStart + node.value + config.commentEnd;
};

/**
* @type {(node: XastCdata, config: Options, state: State) => string}
* @param {XastCdata} node
* @param {Options} config
* @param {State} state
* @returns {string}
*/
const stringifyCdata = (node, config, state) => {
return (
Expand All @@ -182,7 +199,10 @@ const stringifyCdata = (node, config, state) => {
};

/**
* @type {(node: XastElement, config: Options, state: State) => string}
* @param {XastElement} node
* @param {Options} config
* @param {State} state
* @returns {string}
*/
const stringifyElement = (node, config, state) => {
// empty element and short tag
Expand Down Expand Up @@ -251,7 +271,9 @@ const stringifyElement = (node, config, state) => {
};

/**
* @type {(node: XastElement, config: Options) => string}
* @param {XastElement} node
* @param {Options} config
* @returns {string}
*/
const stringifyAttributes = (node, config) => {
let attrs = '';
Expand All @@ -270,7 +292,10 @@ const stringifyAttributes = (node, config) => {
};

/**
* @type {(node: XastText, config: Options, state: State) => string}
* @param {XastText} node
* @param {Options} config
* @param {State} state
* @returns {string}
*/
const stringifyText = (node, config, state) => {
return (
Expand Down
Loading