Skip to content

Commit

Permalink
Drop full, perItem and perItemReverse plugin types
Browse files Browse the repository at this point in the history
  • Loading branch information
TrySound committed Oct 2, 2022
1 parent acf1032 commit de5d682
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 56 deletions.
1 change: 0 additions & 1 deletion lib/svgo.test.js
Expand Up @@ -368,7 +368,6 @@ test('multipass option should trigger plugins multiple times', () => {
* @type {Plugin<void>}
*/
const testPlugin = {
type: 'visitor',
name: 'testPlugin',
fn: (_root, _params, info) => {
list.push(info.multipassCount);
Expand Down
53 changes: 4 additions & 49 deletions lib/svgo/plugins.js
Expand Up @@ -20,62 +20,17 @@ const invokePlugins = (ast, info, plugins, overrides, globalOverrides) => {
}
const params = { ...plugin.params, ...globalOverrides, ...override };

if (plugin.type === 'perItem') {
ast = perItem(ast, info, plugin, params);
}
if (plugin.type === 'perItemReverse') {
ast = perItem(ast, info, plugin, params, true);
}
if (plugin.type === 'full') {
if (plugin.active) {
ast = plugin.fn(ast, params, info);
}
}
if (plugin.type === 'visitor') {
if (plugin.active) {
const visitor = plugin.fn(ast, params, info);
if (visitor != null) {
visit(ast, visitor);
}
if (plugin.active) {
const visitor = plugin.fn(ast, params, info);
if (visitor != null) {
visit(ast, visitor);
}
}
}
return ast;
};
exports.invokePlugins = invokePlugins;

/**
* Direct or reverse per-item loop.
*
* @param {Object} data input data
* @param {Object} info extra information
* @param {Array} plugins plugins list to process
* @param {boolean} [reverse] reverse pass?
* @return {Object} output data
*/
function perItem(data, info, plugin, params, reverse) {
function monkeys(items) {
items.children = items.children.filter(function (item) {
// reverse pass
if (reverse && item.children) {
monkeys(item);
}
// main filter
let kept = true;
if (plugin.active) {
kept = plugin.fn(item, params, info) !== false;
}
// direct pass
if (!reverse && item.children) {
monkeys(item);
}
return kept;
});
return items;
}
return monkeys(data);
}

const createPreset = ({ name, plugins }) => {
return {
name,
Expand Down
6 changes: 0 additions & 6 deletions plugins-api.md
Expand Up @@ -9,7 +9,6 @@ Basic plugin looks like this

```js
export const myPlugin = {
type: 'visitor',
name: 'pluginName',
fn: () => {
// do nothing
Expand All @@ -33,7 +32,6 @@ Visitor pattern allows to access all nodes in direct order from the root to the

```js
const myPlugin = {
type: 'visitor',
name: 'pluginName',
fn: () => {
return {
Expand Down Expand Up @@ -74,7 +72,6 @@ All nodes except root also have an access to parentNode

```js
const myPlugin = {
type: 'visitor',
name: 'pluginName',
fn: () => {
return {
Expand All @@ -97,7 +94,6 @@ To remove node from parent create new array with children

```js
const myPlugin = {
type: 'visitor',
name: 'pluginName',
fn: () => {
return {
Expand All @@ -115,7 +111,6 @@ Find all circles with specific attribute

```js
const myPlugin = {
type: 'visitor',
name: 'pluginName',
fn: () => {
return {
Expand All @@ -135,7 +130,6 @@ Collect all elements and analyze later

```js
const myPlugin = {
type: 'visitor',
name: 'pluginName',
fn: () => {
const elements = []
Expand Down

0 comments on commit de5d682

Please sign in to comment.