Skip to content

Commit df87725

Browse files
authored
chore: improve jsdoc types and match most files (#2108)
1 parent 71a1254 commit df87725

File tree

13 files changed

+264
-226
lines changed

13 files changed

+264
-226
lines changed

lib/svgo-node.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import os from 'os';
2-
import fs from 'fs';
2+
import fs from 'fs/promises';
33
import path from 'path';
44
import {
55
VERSION,
@@ -35,7 +35,7 @@ const importConfig = async (configFile) => {
3535
*/
3636
const isFile = async (file) => {
3737
try {
38-
const stats = await fs.promises.stat(file);
38+
const stats = await fs.stat(file);
3939
return stats.isFile();
4040
} catch {
4141
return false;

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@
115115
"sax": "^1.4.1"
116116
},
117117
"devDependencies": {
118-
"@eslint/js": "^9.3.0",
118+
"@eslint/js": "^9.25.1",
119119
"@jest/globals": "^29.7.0",
120120
"@rollup/plugin-commonjs": "^25.0.7",
121121
"@rollup/plugin-node-resolve": "^15.2.3",
@@ -125,8 +125,9 @@
125125
"@types/jest": "^29.5.12",
126126
"@types/node": "^20.12.11",
127127
"@types/sax": "^1.2.7",
128+
"@types/tar-stream": "^3.1.3",
128129
"cross-env": "^7.0.3",
129-
"eslint": "^9.3.0",
130+
"eslint": "^9.25.1",
130131
"globals": "^14.0.0",
131132
"jest": "^29.7.0",
132133
"pixelmatch": "^7.1.0",

rollup.config.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,13 @@ export default [
4141
format: 'cjs',
4242
exports: 'named',
4343
},
44-
external: ['os', 'fs', 'url', 'path', ...Object.keys(PKG.dependencies)],
44+
external: [
45+
'os',
46+
'fs/promises',
47+
'url',
48+
'path',
49+
...Object.keys(PKG.dependencies),
50+
],
4551
onwarn(warning) {
4652
throw Error(warning.toString());
4753
},

test/coa/_index.test.js

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,18 @@ const svgFiles = [
1313
path.resolve(__dirname, 'testSvg/test.1.svg'),
1414
];
1515
const tempFolder = 'temp';
16-
const noop = () => {};
1716

17+
/**
18+
* @param {string[]} args
19+
* @returns {Promise<Command>}
20+
*/
1821
function runProgram(args) {
1922
const program = new Command();
2023
svgo(program);
2124
// prevent running process.exit
2225
program.exitOverride(() => {});
2326
// parser skips first two arguments
24-
return program.parseAsync([0, 1, ...args]);
27+
return program.parseAsync(['', '', ...args]);
2528
}
2629

2730
describe('coa', function () {
@@ -34,18 +37,10 @@ describe('coa', function () {
3437
await fs.promises.rm(tempFolder, { force: true, recursive: true });
3538
});
3639

37-
const initialConsoleError = global.console.error;
38-
const initialProcessExit = global.process.exit;
39-
40-
function replaceConsoleError() {
41-
global.process.exit = noop;
42-
}
43-
44-
function restoreConsoleError() {
45-
global.console.error = initialConsoleError;
46-
global.process.exit = initialProcessExit;
47-
}
48-
40+
/**
41+
* @param {string} folderPath
42+
* @returns {number}
43+
*/
4944
function calcFolderSvgWeight(folderPath) {
5045
return fs
5146
.readdirSync(folderPath)
@@ -120,14 +115,9 @@ describe('coa', function () {
120115
});
121116

122117
it('should throw error when stated in input folder does not exist', async () => {
123-
replaceConsoleError();
124-
try {
125-
await expect(
126-
runProgram(['--input', svgFolderPath + 'temp', '--output', tempFolder]),
127-
).rejects.toThrow(/no such file or directory/);
128-
} finally {
129-
restoreConsoleError();
130-
}
118+
await expect(
119+
runProgram(['--input', svgFolderPath + 'temp', '--output', tempFolder]),
120+
).rejects.toThrow(/no such file or directory/);
131121
});
132122

133123
describe('stdout', () => {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/* eslint-disable no-undef */
2+
// @ts-expect-error Testing malformed configuration.
23
export default { plugins };
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
export default { plugins }; // eslint-disable-line no-undef
1+
/* eslint-disable no-undef */
2+
// @ts-expect-error Testing malformed configuration.
3+
export default { plugins };

test/plugins/_index.test.js

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,39 @@
1-
import FS from 'fs';
2-
import PATH from 'path';
1+
import fs from 'fs/promises';
2+
import path from 'path';
33
import { EOL } from 'os';
44
import { fileURLToPath } from 'url';
55
import { optimize } from '../../lib/svgo.js';
66

7-
const regEOL = new RegExp(EOL, 'g');
7+
/**
8+
* @typedef {import('../../lib/svgo.js').PluginConfig} PluginConfig
9+
* @typedef {import('../../lib/svgo.js').CustomPlugin} CustomPlugin
10+
*/
11+
812
const regFilename = /^(.*)\.(\d+)\.svg\.txt$/;
9-
const __dirname = PATH.dirname(fileURLToPath(import.meta.url));
13+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
14+
const files = await fs.readdir(__dirname);
1015

1116
describe('plugins tests', function () {
12-
FS.readdirSync(__dirname).forEach(function (file) {
13-
var match = file.match(regFilename),
14-
index,
15-
name;
17+
for (let file of files) {
18+
const match = file.match(regFilename);
19+
let index;
20+
/** @type {any} */
21+
let name;
1622

1723
if (match) {
1824
name = match[1];
1925
index = match[2];
2026

21-
file = PATH.resolve(__dirname, file);
27+
file = path.resolve(__dirname, file);
2228

23-
it(name + '.' + index, function () {
29+
it(name + '.' + index, () => {
2430
return readFile(file).then(function (data) {
2531
// remove description
2632
const items = normalize(data).split(/\s*===\s*/);
2733
const test = items.length === 2 ? items[1] : items[0];
2834
// extract test case
2935
const [original, should, params] = test.split(/\s*@@@\s*/);
36+
/** @type {Exclude<PluginConfig, CustomPlugin>} */
3037
const plugin = {
3138
name,
3239
params: params ? JSON.parse(params) : {},
@@ -48,18 +55,21 @@ describe('plugins tests', function () {
4855
});
4956
});
5057
}
51-
});
58+
}
5259
});
5360

61+
/**
62+
* @param {string} file
63+
* @returns {string}
64+
*/
5465
function normalize(file) {
55-
return file.trim().replace(regEOL, '\n');
66+
return file.trim().replaceAll(EOL, '\n');
5667
}
5768

69+
/**
70+
* @param {string} file
71+
* @returns {Promise<string>}
72+
*/
5873
function readFile(file) {
59-
return new Promise(function (resolve, reject) {
60-
FS.readFile(file, 'utf8', function (err, data) {
61-
if (err) return reject(err);
62-
resolve(data);
63-
});
64-
});
74+
return fs.readFile(file, 'utf-8');
6575
}

test/plugins/_transforms.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { matrixToTransform } from '../../plugins/_transforms.js';
22

33
/**
4-
* @typedef {import('../../plugins/_transforms').TransformParams} TransformParams
4+
* @typedef {import('../../plugins/_transforms.js').TransformParams} TransformParams
55
*/
66

77
/** @type {TransformParams} */

test/regression-extract.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,13 @@ const extractTarGz = async (url, baseDir) => {
200200
stream.resume();
201201
next();
202202
});
203-
const response = await fetch(url);
204-
await pipeline(response.body, zlib.createGunzip(), extract);
203+
const { body } = await fetch(url);
204+
205+
if (!body) {
206+
throw Error('No body returned when fetching SVGO Test Suite.');
207+
}
208+
209+
await pipeline(body, zlib.createGunzip(), extract);
205210
};
206211

207212
(async () => {

test/svg2js/_index.test.js

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
import FS from 'fs';
2-
import PATH from 'path';
1+
import fs from 'fs';
2+
import path from 'path';
33
import { fileURLToPath } from 'url';
44
import { parseSvg } from '../../lib/parser.js';
55

6-
const __dirname = PATH.dirname(fileURLToPath(import.meta.url));
6+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
77

8-
describe('svg2js', function () {
9-
describe('working svg', function () {
10-
var filepath = PATH.resolve(__dirname, './test.svg'),
11-
root;
8+
describe('svg2js', () => {
9+
describe('working svg', () => {
10+
const filepath = path.resolve(__dirname, './test.svg');
11+
/** @type {any} */
12+
let root;
1213

13-
beforeAll(function (done) {
14-
FS.readFile(filepath, 'utf8', function (err, data) {
14+
beforeAll((done) => {
15+
fs.readFile(filepath, 'utf8', (err, data) => {
1516
if (err) {
1617
throw err;
1718
}
@@ -21,26 +22,26 @@ describe('svg2js', function () {
2122
});
2223
});
2324

24-
describe('root', function () {
25-
it('should exist', function () {
25+
describe('root', () => {
26+
it('should exist', () => {
2627
expect(root).toStrictEqual(expect.anything());
2728
});
2829

29-
it('should be an instance of Object', function () {
30+
it('should be an instance of Object', () => {
3031
expect(root).toBeInstanceOf(Object);
3132
});
3233

33-
it('should have property "children"', function () {
34+
it('should have property "children"', () => {
3435
expect(root).toHaveProperty('children');
3536
});
3637
});
3738

38-
describe('root.children', function () {
39-
it('should be an instance of Array', function () {
39+
describe('root.children', () => {
40+
it('should be an instance of Array', () => {
4041
expect(root.children).toBeInstanceOf(Array);
4142
});
4243

43-
it('should have length 4', function () {
44+
it('should have length 4', () => {
4445
expect(root.children).toHaveLength(4);
4546
});
4647
});
@@ -72,7 +73,7 @@ describe('svg2js', function () {
7273
});
7374
});
7475

75-
describe('name', function () {
76+
describe('name', () => {
7677
it('should have property name: "svg"', function () {
7778
expect(root.children[3]).toStrictEqual(
7879
expect.objectContaining({
@@ -82,22 +83,22 @@ describe('svg2js', function () {
8283
});
8384
});
8485

85-
describe('children', function () {
86-
it('should exist', function () {
86+
describe('children', () => {
87+
it('should exist', () => {
8788
expect(root.children[3].children).toStrictEqual(expect.anything());
8889
});
8990

90-
it('should be an instance of Array', function () {
91+
it('should be an instance of Array', () => {
9192
expect(root.children[3].children).toBeInstanceOf(Array);
9293
});
9394

94-
it('should eventually have length 3', function () {
95+
it('should eventually have length 3', () => {
9596
expect(root.children[3].children).toHaveLength(3);
9697
});
9798
});
9899

99-
describe('text nodes', function () {
100-
it('should contain preserved whitespace', function () {
100+
describe('text nodes', () => {
101+
it('should contain preserved whitespace', () => {
101102
const textNode = root.children[3].children[1].children[0].children[1];
102103
expect(textNode.children[0].value).toBe(' test ');
103104
});

0 commit comments

Comments
 (0)