Skip to content

Commit a8a53db

Browse files
authored
chore: apply more eslint rules (#2116)
1 parent 97079fb commit a8a53db

37 files changed

+337
-268
lines changed

eslint.config.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ export default [
3434
{
3535
files: ['**/*.js', '**/*.mjs'],
3636
rules: {
37+
'one-var': ['error', 'never'],
38+
curly: 'error',
3739
strict: 'error',
3840
},
3941
},

lib/parser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ export const parseSvg = (data, from) => {
158158

159159
sax.onopentag = (data) => {
160160
/** @type {XastElement} */
161-
let element = {
161+
const element = {
162162
type: 'element',
163163
name: data.name,
164164
attributes: {},

lib/style.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as csstree from 'css-tree';
22
import * as csswhat from 'css-what';
33
import { syntax } from 'csso';
4-
import { visit, matches } from './xast.js';
4+
import { matches, visit } from './xast.js';
55
import {
66
attrsGroups,
77
inheritableAttrs,

lib/svgo-node.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import fs from 'fs/promises';
33
import path from 'path';
44
import {
55
VERSION,
6-
optimize as optimizeAgnostic,
6+
_collections,
77
builtinPlugins,
88
mapNodesToParents,
9+
optimize as optimizeAgnostic,
910
querySelector,
1011
querySelectorAll,
11-
_collections,
1212
} from './svgo.js';
1313

1414
/**

lib/svgo-node.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os from 'os';
22
import path from 'path';
3-
import { optimize, loadConfig } from './svgo-node.js';
3+
import { loadConfig, optimize } from './svgo-node.js';
44

55
const describeLF = os.EOL === '\r\n' ? describe.skip : describe;
66
const describeCRLF = os.EOL === '\r\n' ? describe : describe.skip;

lib/svgo/coa.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import fs from 'fs';
22
import path from 'path';
33
import colors from 'picocolors';
44
import { fileURLToPath } from 'url';
5-
import { encodeSVGDatauri, decodeSVGDatauri } from './tools.js';
5+
import { decodeSVGDatauri, encodeSVGDatauri } from './tools.js';
66
import { loadConfig, optimize } from '../svgo-node.js';
77
import { builtin } from '../builtin.js';
88
import { SvgoParserError } from '../parser.js';
@@ -151,7 +151,7 @@ async function action(args, opts, command) {
151151

152152
if (process?.versions?.node && PKG.engines.node) {
153153
// @ts-expect-error We control this and ensure it is never null.
154-
var nodeVersion = String(PKG.engines.node).match(/\d*(\.\d+)*/)[0];
154+
const nodeVersion = String(PKG.engines.node).match(/\d*(\.\d+)*/)[0];
155155
if (parseFloat(process.versions.node) < parseFloat(nodeVersion)) {
156156
throw Error(
157157
`${PKG.name} requires Node.js version ${nodeVersion} or higher.`,
@@ -224,7 +224,7 @@ async function action(args, opts, command) {
224224
if (input.length && input[0] != '-') {
225225
if (output.length == 1 && checkIsDir(output[0])) {
226226
const dir = output[0];
227-
for (var i = 0; i < input.length; i++) {
227+
for (let i = 0; i < input.length; i++) {
228228
output[i] = checkIsDir(input[i])
229229
? input[i]
230230
: path.resolve(dir, path.basename(input[i]));
@@ -245,7 +245,7 @@ async function action(args, opts, command) {
245245

246246
// --folder
247247
if (opts.folder) {
248-
var outputFolder = (output && output[0]) || opts.folder;
248+
const outputFolder = (output && output[0]) || opts.folder;
249249
await optimizeFolder(config, opts.folder, outputFolder);
250250
}
251251

@@ -254,8 +254,8 @@ async function action(args, opts, command) {
254254
// STDIN
255255
if (input[0] === '-') {
256256
return new Promise((resolve, reject) => {
257-
var data = '',
258-
file = output[0];
257+
let data = '';
258+
const file = output[0];
259259

260260
process.stdin
261261
.on('data', (chunk) => (data += chunk))
@@ -274,7 +274,7 @@ async function action(args, opts, command) {
274274

275275
// --string
276276
} else if (opts.string) {
277-
var data = decodeSVGDatauri(opts.string);
277+
const data = decodeSVGDatauri(opts.string);
278278

279279
return processSVGData(config, null, data, output[0]);
280280
}
@@ -308,7 +308,7 @@ function optimizeFolder(config, dir, output) {
308308
*/
309309
function processDirectory(config, dir, files, output) {
310310
// take only *.svg files, recursively if necessary
311-
var svgFilesDescriptions = getFilesDescriptions(config, dir, files, output);
311+
const svgFilesDescriptions = getFilesDescriptions(config, dir, files, output);
312312

313313
return svgFilesDescriptions.length
314314
? Promise.all(
@@ -396,8 +396,8 @@ function optimizeFile(config, file, output) {
396396
* @return {Promise<any>}
397397
*/
398398
function processSVGData(config, info, data, output, input = undefined) {
399-
var startTime = Date.now(),
400-
prevFileSize = Buffer.byteLength(data, 'utf8');
399+
const startTime = Date.now();
400+
const prevFileSize = Buffer.byteLength(data, 'utf8');
401401

402402
let result;
403403
try {
@@ -413,8 +413,8 @@ function processSVGData(config, info, data, output, input = undefined) {
413413
if (config.datauri) {
414414
result.data = encodeSVGDatauri(result.data, config.datauri);
415415
}
416-
var resultFileSize = Buffer.byteLength(result.data, 'utf8'),
417-
processingTime = Date.now() - startTime;
416+
const resultFileSize = Buffer.byteLength(result.data, 'utf8');
417+
const processingTime = Date.now() - startTime;
418418

419419
return writeOutput(input, output, result.data).then(
420420
function () {

lib/svgo/tools.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const regReferencesBegin = /(\w+)\.[a-zA-Z]/;
2323
* @returns {string}
2424
*/
2525
export const encodeSVGDatauri = (str, type) => {
26-
var prefix = 'data:image/svg+xml';
26+
let prefix = 'data:image/svg+xml';
2727
if (!type || type === 'base64') {
2828
// base64
2929
prefix += ';base64,';
@@ -45,13 +45,15 @@ export const encodeSVGDatauri = (str, type) => {
4545
* @returns {string}
4646
*/
4747
export const decodeSVGDatauri = (str) => {
48-
var regexp = /data:image\/svg\+xml(;charset=[^;,]*)?(;base64)?,(.*)/;
49-
var match = regexp.exec(str);
48+
const regexp = /data:image\/svg\+xml(;charset=[^;,]*)?(;base64)?,(.*)/;
49+
const match = regexp.exec(str);
5050

5151
// plain string
52-
if (!match) return str;
52+
if (!match) {
53+
return str;
54+
}
5355

54-
var data = match[3];
56+
const data = match[3];
5557

5658
if (match[2]) {
5759
// base64
@@ -88,13 +90,17 @@ export const cleanupOutData = (data, params, command) => {
8890
delimiter = ' ';
8991

9092
// no extra space in front of first number
91-
if (i == 0) delimiter = '';
93+
if (i == 0) {
94+
delimiter = '';
95+
}
9296

9397
// no extra space after arc command flags (large-arc and sweep flags)
9498
// a20 60 45 0 1 30 20 → a20 60 45 0130 20
9599
if (params.noSpaceAfterFlags && (command == 'A' || command == 'a')) {
96-
var pos = i % 7;
97-
if (pos == 4 || pos == 5) delimiter = '';
100+
const pos = i % 7;
101+
if (pos == 4 || pos == 5) {
102+
delimiter = '';
103+
}
98104
}
99105

100106
// remove floating-point numbers leading zeros

lib/xast.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import { selectAll, selectOne, is } from 'css-select';
1+
import { is, selectAll, selectOne } from 'css-select';
22
import { createAdapter } from './svgo/css-select-adapter.js';
33

44
/**
55
* @typedef {import('css-select').Options<XastNode & { children?: any }, XastElement>} Options
6+
* @typedef {import('./types.js').Visitor} Visitor
7+
* @typedef {import('./types.js').XastChild} XastChild
68
* @typedef {import('./types.js').XastElement} XastElement
79
* @typedef {import('./types.js').XastNode} XastNode
8-
* @typedef {import('./types.js').XastChild} XastChild
910
* @typedef {import('./types.js').XastParent} XastParent
10-
* @typedef {import('./types.js').Visitor} Visitor
1111
*/
1212

1313
/**

lib/xast.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
import { detachNodeFromParent, visit, visitSkip } from './xast.js';
2+
13
/**
24
* @typedef {import('./types.js').XastElement} XastElement
35
* @typedef {import('./types.js').XastRoot} XastRoot
46
*/
57

6-
import { visit, visitSkip, detachNodeFromParent } from './xast.js';
7-
88
/**
99
* @param {XastElement[]} children
1010
* @returns {XastRoot}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@
133133
"pixelmatch": "^7.1.0",
134134
"playwright": "^1.51.1",
135135
"pngjs": "^7.0.0",
136-
"prettier": "^3.2.5",
136+
"prettier": "^3.5.3",
137137
"rimraf": "^5.0.7",
138138
"rollup": "^4.22.4",
139139
"tar-stream": "^3.1.7",

0 commit comments

Comments
 (0)