Skip to content

Commit 49954bc

Browse files
authored
chores: various clean ups regardin ESM and TypeScript (#2117)
1 parent a8a53db commit 49954bc

22 files changed

+102
-101
lines changed

lib/path.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/**
2+
* @fileoverview Based on https://www.w3.org/TR/SVG11/paths.html#PathDataBNF.
3+
*/
4+
15
import { removeLeadingZero, toFixed } from './svgo/tools.js';
26

37
/**
@@ -6,13 +10,11 @@ import { removeLeadingZero, toFixed } from './svgo/tools.js';
610
* @typedef {'none' | 'sign' | 'whole' | 'decimal_point' | 'decimal' | 'e' | 'exponent_sign' | 'exponent'} ReadNumberState
711
*
812
* @typedef StringifyPathDataOptions
9-
* @property {PathDataItem[]} pathData
13+
* @property {ReadonlyArray<PathDataItem>} pathData
1014
* @property {number=} precision
1115
* @property {boolean=} disableSpaceAfterFlags
1216
*/
1317

14-
// Based on https://www.w3.org/TR/SVG11/paths.html#PathDataBNF
15-
1618
const argsCountPerCommand = {
1719
M: 2,
1820
m: 2,
@@ -257,7 +259,7 @@ const roundAndStringify = (number, precision) => {
257259
* because many non-browser environments are not able to parse such paths
258260
*
259261
* @param {string} command
260-
* @param {number[]} args
262+
* @param {ReadonlyArray<number>} args
261263
* @param {number=} precision
262264
* @param {boolean=} disableSpaceAfterFlags
263265
* @returns {string}

lib/path.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ describe('stringify path data', () => {
154154
).toBe('M.1 1e-7 2 2');
155155
});
156156
it('should configure precision', () => {
157-
/** @type {PathDataItem[]} */
157+
/** @type {ReadonlyArray<PathDataItem>} */
158158
const pathData = [
159159
{ command: 'M', args: [0, -1.9876] },
160160
{ command: 'L', args: [0.3, 3.14159265] },
@@ -175,7 +175,7 @@ describe('stringify path data', () => {
175175
).toBe('M0-2 0 3 0-3 100 200');
176176
});
177177
it('allows to avoid spaces after arc flags', () => {
178-
/** @type {PathDataItem[]} */
178+
/** @type {ReadonlyArray<PathDataItem>} */
179179
const pathData = [
180180
{ command: 'M', args: [0, 0] },
181181
{ command: 'A', args: [50, 50, 10, 1, 0, 0.2, 20] },

lib/svgo.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ export type BuiltinPluginOrPreset<Name, Params> = BuiltinPlugin<
4444
* If the plugin is a preset that invokes other plugins, this returns an
4545
* array of the plugins in the preset in the order that they are invoked.
4646
*/
47-
plugins?: Readonly<BuiltinPlugin<string, Object>[]>;
47+
plugins?: ReadonlyArray<BuiltinPlugin<string, Object>>;
4848
};
4949

5050
/**
5151
* Plugins that are bundled with SVGO. This includes plugin presets, and plugins
5252
* that are not enabled by default.
5353
*/
54-
export declare const builtinPlugins: Array<
54+
export declare const builtinPlugins: ReadonlyArray<
5555
{
5656
[Name in keyof PluginsParams]: BuiltinPluginOrPreset<
5757
Name,

lib/svgo/coa.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { SvgoParserError } from '../parser.js';
1313

1414
const __dirname = path.dirname(fileURLToPath(import.meta.url));
1515
const pkgPath = path.join(__dirname, '../../package.json');
16-
const PKG = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'));
16+
const PKG = JSON.parse(await fs.promises.readFile(pkgPath, 'utf-8'));
1717

1818
/**
1919
* Synchronously check if path is a directory. Tolerant to errors like ENOENT.
@@ -89,7 +89,7 @@ export default function makeProgram(program) {
8989
}
9090

9191
/**
92-
* @param {string[]} args
92+
* @param {ReadonlyArray<string>} args
9393
* @param {any} opts
9494
* @param {Command} command
9595
* @returns
@@ -302,7 +302,7 @@ function optimizeFolder(config, dir, output) {
302302
*
303303
* @param {any} config options
304304
* @param {string} dir input directory
305-
* @param {string[]} files list of file names in the directory
305+
* @param {ReadonlyArray<string>} files list of file names in the directory
306306
* @param {string} output output directory
307307
* @return {Promise<any>}
308308
*/
@@ -330,7 +330,7 @@ function processDirectory(config, dir, files, output) {
330330
*
331331
* @param {any} config options
332332
* @param {string} dir input directory
333-
* @param {string[]} files list of file names in the directory
333+
* @param {ReadonlyArray<string>} files list of file names in the directory
334334
* @param {string} output output directory
335335
* @return {any[]}
336336
*/
@@ -373,7 +373,7 @@ function getFilesDescriptions(config, dir, files, output) {
373373
/**
374374
* Read SVG file and pass to processing.
375375
*
376-
* @param {Object} config options
376+
* @param {any} config options
377377
* @param {string} file
378378
* @param {string} output
379379
* @return {Promise<any>}
@@ -445,13 +445,13 @@ function processSVGData(config, info, data, output, input = undefined) {
445445
* @param {string} data data to write
446446
* @return {Promise<void>}
447447
*/
448-
function writeOutput(input, output, data) {
448+
async function writeOutput(input, output, data) {
449449
if (output == '-') {
450450
process.stdout.write(data);
451451
return Promise.resolve();
452452
}
453453

454-
fs.mkdirSync(path.dirname(output), { recursive: true });
454+
await fs.promises.mkdir(path.dirname(output), { recursive: true });
455455

456456
return fs.promises
457457
.writeFile(output, data, 'utf8')

lib/svgo/plugins.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import { visit } from '../xast.js';
1212
* @module plugins
1313
*
1414
* @param {XastNode} ast Input AST.
15-
* @param {Object} info Extra information.
16-
* @param {Array<any>} plugins Plugins property from config.
15+
* @param {any} info Extra information.
16+
* @param {ReadonlyArray<any>} plugins Plugins property from config.
1717
* @param {any} overrides
1818
* @param {any} globalOverrides
1919
*/
@@ -39,7 +39,7 @@ export const invokePlugins = (
3939
};
4040

4141
/**
42-
* @param {{ name: string, plugins: BuiltinPlugin[] }} arg0
42+
* @param {{ name: string, plugins: ReadonlyArray<BuiltinPlugin> }} arg0
4343
* @returns {BuiltinPreset}
4444
*/
4545
export const createPreset = ({ name, plugins }) => {

lib/svgo/tools.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export const decodeSVGDatauri = (str) => {
7474
* @example
7575
* [0, -1, .5, .5] → "0-1 .5.5"
7676
*
77-
* @param {number[]} data
77+
* @param {ReadonlyArray<number>} data
7878
* @param {CleanupOutDataParams} params
7979
* @param {PathDataCommand=} command
8080
* @returns {string}

package.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
"type": "opencollective",
5151
"url": "https://opencollective.com/svgo"
5252
},
53-
"main": "./lib/svgo-node.js",
5453
"bin": "./bin/svgo.js",
5554
"types": "./lib/svgo-node.d.ts",
5655
"exports": {
@@ -82,7 +81,7 @@
8281
"!**/*.test.js"
8382
],
8483
"engines": {
85-
"node": ">=16.0.0"
84+
"node": ">=16"
8685
},
8786
"scripts": {
8887
"build": "node scripts/sync-version.js && rollup -c",
@@ -120,10 +119,10 @@
120119
"@rollup/plugin-commonjs": "^26.0.3",
121120
"@rollup/plugin-node-resolve": "^15.2.3",
122121
"@rollup/plugin-terser": "^0.4.4",
123-
"@types/css-tree": "^2.3.7",
122+
"@types/css-tree": "^2.3.10",
124123
"@types/csso": "^5.0.4",
125-
"@types/jest": "^29.5.12",
126-
"@types/node": "^20.12.11",
124+
"@types/jest": "^29.5.14",
125+
"@types/node": "^22.15.3",
127126
"@types/sax": "^1.2.7",
128127
"@types/tar-stream": "^3.1.3",
129128
"cross-env": "^7.0.3",

plugins/_path.js

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export const path2js = (path) => {
5656
/**
5757
* Convert relative Path data to absolute.
5858
*
59-
* @param {PathDataItem[]} data
59+
* @param {ReadonlyArray<PathDataItem>} data
6060
* @returns {PathDataItem[]}
6161
*/
6262
const convertRelativeToAbsolute = (data) => {
@@ -189,7 +189,7 @@ const convertRelativeToAbsolute = (data) => {
189189
* Convert path array to string.
190190
*
191191
* @param {XastElement} path
192-
* @param {PathDataItem[]} data
192+
* @param {ReadonlyArray<PathDataItem>} data
193193
* @param {Js2PathParams} params
194194
*/
195195
export const js2path = function (path, data, params) {
@@ -223,7 +223,7 @@ export const js2path = function (path, data, params) {
223223

224224
/**
225225
* @param {number[]} dest
226-
* @param {number[]} source
226+
* @param {ReadonlyArray<number>} source
227227
* @returns {number[]}
228228
*/
229229
function set(dest, source) {
@@ -237,8 +237,8 @@ function set(dest, source) {
237237
* collision using Gilbert-Johnson-Keerthi distance algorithm
238238
* https://web.archive.org/web/20180822200027/http://entropyinteractive.com/2011/04/gjk-algorithm/
239239
*
240-
* @param {PathDataItem[]} path1
241-
* @param {PathDataItem[]} path2
240+
* @param {ReadonlyArray<PathDataItem>} path1
241+
* @param {ReadonlyArray<PathDataItem>} path2
242242
* @returns {boolean}
243243
*/
244244
export const intersects = function (path1, path2) {
@@ -310,7 +310,7 @@ export const intersects = function (path1, path2) {
310310
/**
311311
* @param {Point} a
312312
* @param {Point} b
313-
* @param {number[]} direction
313+
* @param {ReadonlyArray<number>} direction
314314
* @returns {number[]}
315315
*/
316316
function getSupport(a, b, direction) {
@@ -324,7 +324,7 @@ export const intersects = function (path1, path2) {
324324
* we find the farthest point.
325325
*
326326
* @param {Point} polygon
327-
* @param {number[]} direction
327+
* @param {ReadonlyArray<number>} direction
328328
* @returns {number[]}
329329
*/
330330
function supportPoint(polygon, direction) {
@@ -408,34 +408,34 @@ function processSimplex(simplex, direction) {
408408
}
409409

410410
/**
411-
* @param {number[]} v
411+
* @param {ReadonlyArray<number>} v
412412
* @returns {number[]}
413413
*/
414414
function minus(v) {
415415
return [-v[0], -v[1]];
416416
}
417417

418418
/**
419-
* @param {number[]} v1
420-
* @param {number[]} v2
419+
* @param {ReadonlyArray<number>} v1
420+
* @param {ReadonlyArray<number>} v2
421421
* @returns {number[]}
422422
*/
423423
function sub(v1, v2) {
424424
return [v1[0] - v2[0], v1[1] - v2[1]];
425425
}
426426

427427
/**
428-
* @param {number[]} v1
429-
* @param {number[]} v2
428+
* @param {ReadonlyArray<number>} v1
429+
* @param {ReadonlyArray<number>} v2
430430
* @returns {number}
431431
*/
432432
function dot(v1, v2) {
433433
return v1[0] * v2[0] + v1[1] * v2[1];
434434
}
435435

436436
/**
437-
* @param {number[]} v
438-
* @param {number[]} from
437+
* @param {ReadonlyArray<number>} v
438+
* @param {ReadonlyArray<number>} from
439439
* @returns {number[]}
440440
*/
441441
function orth(v, from) {
@@ -444,7 +444,7 @@ function orth(v, from) {
444444
}
445445

446446
/**
447-
* @param {PathDataItem[]} pathData
447+
* @param {ReadonlyArray<PathDataItem>} pathData
448448
* @returns {Points}
449449
*/
450450
function gatherPoints(pathData) {
@@ -699,9 +699,9 @@ function convexHull(points) {
699699
}
700700

701701
/**
702-
* @param {number[]} o
703-
* @param {number[]} a
704-
* @param {number[]} b
702+
* @param {ReadonlyArray<number>} o
703+
* @param {ReadonlyArray<number>} a
704+
* @param {ReadonlyArray<number>} b
705705
* @returns {number}
706706
*/
707707
function cross(o, a, b) {
@@ -721,7 +721,7 @@ function cross(o, a, b) {
721721
* @param {number} sweep_flag
722722
* @param {number} x2
723723
* @param {number} y2
724-
* @param {number[]} recursive
724+
* @param {ReadonlyArray<number>} recursive
725725
* @returns {number[]}
726726
*/
727727
const a2c = (

plugins/_transforms.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export const transform2js = (transformString) => {
7474
/**
7575
* Multiply transforms into one.
7676
*
77-
* @param {TransformItem[]} transforms
77+
* @param {ReadonlyArray<TransformItem>} transforms
7878
* @returns {TransformItem}
7979
*/
8080
export const transformsMultiply = (transforms) => {
@@ -344,8 +344,8 @@ const isIdentityTransform = (t) => {
344344

345345
/**
346346
* Optimize matrix of simple transforms.
347-
* @param {TransformItem[]} roundedTransforms
348-
* @param {TransformItem[]} rawTransforms
347+
* @param {ReadonlyArray<TransformItem>} roundedTransforms
348+
* @param {ReadonlyArray<TransformItem>} rawTransforms
349349
* @returns {TransformItem[]}
350350
*/
351351
const optimize = (roundedTransforms, rawTransforms) => {
@@ -443,7 +443,7 @@ const optimize = (roundedTransforms, rawTransforms) => {
443443
};
444444

445445
/**
446-
* @param {number[]} data
446+
* @param {ReadonlyArray<number>} data
447447
* @returns {TransformItem}
448448
*/
449449
const createScaleTransform = (data) => {
@@ -546,7 +546,7 @@ const transformToMatrix = (transform) => {
546546
*
547547
* @param {[number, number]} cursor
548548
* @param {number[]} arc
549-
* @param {number[]} transform
549+
* @param {ReadonlyArray<number>} transform
550550
* @returns {number[]}
551551
*/
552552
export const transformArc = (cursor, arc, transform) => {
@@ -608,8 +608,8 @@ export const transformArc = (cursor, arc, transform) => {
608608
/**
609609
* Multiply transformation matrices.
610610
*
611-
* @param {number[]} a
612-
* @param {number[]} b
611+
* @param {ReadonlyArray<number>} a
612+
* @param {ReadonlyArray<number>} b
613613
* @returns {number[]}
614614
*/
615615
const multiplyTransformMatrices = (a, b) => {
@@ -702,7 +702,7 @@ const transformRound = (data, params) => {
702702
/**
703703
* Rounds numbers in array.
704704
*
705-
* @param {number[]} data
705+
* @param {ReadonlyArray<number>} data
706706
* @returns {number[]}
707707
*/
708708
const round = (data) => {
@@ -739,7 +739,7 @@ const smartRound = (precision, data) => {
739739
/**
740740
* Convert transforms JS representation to string.
741741
*
742-
* @param {TransformItem[]} transformJS
742+
* @param {ReadonlyArray<TransformItem>} transformJS
743743
* @param {TransformParams} params
744744
* @returns {string}
745745
*/

0 commit comments

Comments
 (0)