Skip to content

Commit 4573d01

Browse files
authored
chore: migrate plugin types to plugin (#2111)
1 parent 0ff7f24 commit 4573d01

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+398
-386
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ You should read our [Plugin Architecture](https://svgo.dev/docs/plugins-api/) do
5555

5656
SVGO plugins can optionally have parameters. These can be consumed by the plugin to tailor the behavior.
5757

58-
As types are managed through TypeScript definition files and JSDocs, you must define the parameter types in [`plugins/plugin-types.d.ts`](https://github.com/svg/svgo/blob/main/plugins/plugins-types.d.ts) for built-in plugins. Then you'll have code completion and type checking as you'd expect while editing the plugin if your code editor supports that.
58+
Parameters must have types declared in a [`@typedef`](https://jsdoc.app/tags-typedef) at the top of the file. For new plugins, you must also append the appropriate type in [`plugins/plugin-types.d.ts`](https://github.com/svg/svgo/blob/main/plugins/plugins-types.d.ts). This way built-in plugins will have code completion and type checking as you'd expect while editing the plugin.
5959

6060
## Documentation
6161

lib/parser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export class SvgoParserError extends Error {
2020
* @param {number} line
2121
* @param {number} column
2222
* @param {string} source
23-
* @param {string|undefined} file
23+
* @param {string | undefined} file
2424
*/
2525
constructor(message, line, column, source, file = undefined) {
2626
super(message);

lib/style.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ export const computeStyle = (stylesheet, node) => {
284284
* if a `.class` or `#id` is included by passing `name=class` or `name=id`
285285
* respectively.
286286
*
287-
* @param {csstree.ListItem<csstree.CssNode>|string} selector
287+
* @param {csstree.ListItem<csstree.CssNode> | string} selector
288288
* @param {string} name
289289
* @param {?string} value
290290
* @param {boolean} traversed

lib/svgo.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function getPlugin(name) {
3535
}
3636

3737
/**
38-
* @param {string|PluginConfig} plugin
38+
* @param {string | PluginConfig} plugin
3939
* @returns {?PluginConfig}
4040
*/
4141
const resolvePluginConfig = (plugin) => {

lib/types.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,9 @@ export type PluginInfo = {
105105
multipassCount: number;
106106
};
107107

108-
export type Plugin<Params> = (
108+
export type Plugin<P = null> = (
109109
root: XastRoot,
110-
params: Params,
110+
params: P,
111111
info: PluginInfo,
112112
) => Visitor | null | void;
113113

plugins/_transforms.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ const getDecompositions = (matrix) => {
183183

184184
/**
185185
* @param {TransformItem} matrix
186-
* @returns {TransformItem[]|undefined}
186+
* @returns {TransformItem[] | undefined}
187187
* @see {@link https://frederic-wang.fr/2013/12/01/decomposition-of-2d-transform-matrices/} Where applicable, variables are named in accordance with this document.
188188
*/
189189
const decomposeQRAB = (matrix) => {
@@ -238,7 +238,7 @@ const decomposeQRAB = (matrix) => {
238238

239239
/**
240240
* @param {TransformItem} matrix
241-
* @returns {TransformItem[]|undefined}
241+
* @returns {TransformItem[] | undefined}
242242
* @see {@link https://frederic-wang.fr/2013/12/01/decomposition-of-2d-transform-matrices/} Where applicable, variables are named in accordance with this document.
243243
*/
244244
const decomposeQRCD = (matrix) => {

plugins/addAttributesToSVGElement.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
/**
2+
* @typedef AddAttributesToSVGElementParams
3+
* @property {string | Record<string, null | string>=} attribute
4+
* @property {Array<string | Record<string, null | string>>=} attributes
5+
*/
6+
17
export const name = 'addAttributesToSVGElement';
28
export const description = 'adds attributes to an outer <svg> element';
39

@@ -45,7 +51,7 @@ plugins: [
4551
*
4652
* @author April Arcus
4753
*
48-
* @type {import('./plugins-types.js').Plugin<'addAttributesToSVGElement'>}
54+
* @type {import('../lib/types.js').Plugin<AddAttributesToSVGElementParams>}
4955
*/
5056
export const fn = (root, params) => {
5157
if (!Array.isArray(params.attributes) && !params.attribute) {

plugins/addClassesToSVGElement.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
/**
2+
* @typedef {import('../lib/types.js').PluginInfo} PluginInfo
3+
* @typedef {import('../lib/types.js').XastElement} XastElement
4+
*
5+
* @typedef AddClassesToSVGElementParams
6+
* @property {string | ((node: XastElement, info: PluginInfo) => string)=} className
7+
* @property {Array<string | ((node: XastElement, info: PluginInfo) => string)>=} classNames
8+
*/
9+
110
export const name = 'addClassesToSVGElement';
211
export const description = 'adds classnames to an outer <svg> element';
312

@@ -47,7 +56,7 @@ plugins: [
4756
*
4857
* @author April Arcus
4958
*
50-
* @type {import('./plugins-types.js').Plugin<'addClassesToSVGElement'>}
59+
* @type {import('../lib/types.js').Plugin<AddClassesToSVGElementParams>}
5160
*/
5261
export const fn = (root, params, info) => {
5362
if (

plugins/cleanupAttrs.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
/**
2+
* @typedef CleanupAttrsParams
3+
* @property {boolean=} newlines
4+
* @property {boolean=} trim
5+
* @property {boolean=} spaces
6+
*/
7+
18
export const name = 'cleanupAttrs';
29
export const description =
310
'cleanups attributes from newlines, trailing and repeating spaces';
@@ -10,7 +17,7 @@ const regSpaces = /\s{2,}/g;
1017
* Cleanup attributes values from newlines, trailing and repeating spaces.
1118
*
1219
* @author Kir Belevich
13-
* @type {import('./plugins-types.js').Plugin<'cleanupAttrs'>}
20+
* @type {import('../lib/types.js').Plugin<CleanupAttrsParams>}
1421
*/
1522
export const fn = (root, params) => {
1623
const { newlines = true, trim = true, spaces = true } = params;

plugins/cleanupEnableBackground.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const regEnableBackground =
1717
* ⬇
1818
* <svg width="100" height="50">
1919
* @author Kir Belevich
20-
* @type {import('./plugins-types.js').Plugin<'cleanupEnableBackground'>}
20+
* @type {import('../lib/types.js').Plugin}
2121
*/
2222
export const fn = (root) => {
2323
let hasFilter = false;

0 commit comments

Comments
 (0)