Skip to content

Commit

Permalink
chore: freeze plugin arrays (#2028)
Browse files Browse the repository at this point in the history
  • Loading branch information
SethFalco authored Jun 9, 2024
1 parent 1a025da commit a21dae6
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 14 deletions.
6 changes: 3 additions & 3 deletions lib/builtin.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ import * as reusePaths from '../plugins/reusePaths.js';
import * as sortAttrs from '../plugins/sortAttrs.js';
import * as sortDefsChildren from '../plugins/sortDefsChildren.js';

export const builtin = [
export const builtin = Object.freeze([
presetDefault,
addAttributesToSVGElement,
addClassesToSVGElement,
Expand All @@ -70,9 +70,9 @@ export const builtin = [
convertShapeToPath,
convertStyleToAttrs,
convertTransform,
mergeStyles,
inlineStyles,
mergePaths,
mergeStyles,
minifyStyles,
moveElemsAttrsToGroup,
moveGroupAttrsToElems,
Expand Down Expand Up @@ -108,4 +108,4 @@ export const builtin = [
reusePaths,
sortAttrs,
sortDefsChildren,
];
]);
23 changes: 14 additions & 9 deletions lib/svgo.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,26 @@ type BuiltinPlugin<Name, Params> = {
fn: Plugin<Params>;
};

type BuiltinPluginOrPreset<Name, Params> = BuiltinPlugin<Name, Params> & {
/** If the plugin is itself a preset that invokes other plugins. */
isPreset: true | undefined;
/**
* If the plugin is a preset that invokes other plugins, this returns an
* array of the plugins in the preset in the order that they are invoked.
*/
plugins?: Readonly<BuiltinPlugin<unknown, unknown>[]>;
};

/**
* Plugins that are bundled with SVGO. This includes plugin presets, and plugins
* that are not enabled by default.
*/
export declare const builtinPlugins: Array<
{
[Name in keyof PluginsParams]: BuiltinPlugin<Name, PluginsParams[Name]> & {
/** If the plugin is itself a preset that invokes other plugins. */
isPreset: true | undefined;
/**
* If the plugin is a preset that invokes other plugins, this returns an
* array of the plugins in the preset in the order that they are invoked.
*/
plugins?: BuiltinPlugin<unknown, unknown>[];
};
[Name in keyof PluginsParams]: BuiltinPluginOrPreset<
Name,
PluginsParams[Name]
>;
}[keyof PluginsParams]
>;

Expand Down
1 change: 0 additions & 1 deletion lib/svgo/coa.js
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,6 @@ function checkWriteFileError(input, output, data, error) {
/** Show list of available plugins with short description. */
function showAvailablePlugins() {
const list = builtin
.sort((a, b) => a.name.localeCompare(b.name))
.map((plugin) => ` [ ${colors.green(plugin.name)} ] ${plugin.description}`)
.join('\n');
console.log('Currently available plugins:\n' + list);
Expand Down
11 changes: 10 additions & 1 deletion lib/svgo/plugins.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { visit } from '../xast.js';

/**
* @typedef {import('../svgo').BuiltinPlugin<?, ?>} BuiltinPlugin
* @typedef {import('../svgo').BuiltinPluginOrPreset<?, ?>} BuiltinPreset
*/

/**
* Plugins engine.
*
Expand Down Expand Up @@ -31,11 +36,15 @@ export const invokePlugins = (
}
};

/**
* @param {{ name: string, plugins: BuiltinPlugin[] }} arg0
* @returns {BuiltinPreset}
*/
export const createPreset = ({ name, plugins }) => {
return {
name,
isPreset: true,
plugins,
plugins: Object.freeze(plugins),
fn: (ast, params, info) => {
const { floatPrecision, overrides } = params;
const globalOverrides = {};
Expand Down

0 comments on commit a21dae6

Please sign in to comment.