@putout/engine-runner
Advanced tools
Comparing version 12.4.1 to 12.5.0
@@ -27,5 +27,3 @@ 'use strict'; | ||
pluginsTraverse, | ||
} = splitPlugins(plugins, { | ||
template, | ||
}); | ||
} = splitPlugins(plugins); | ||
@@ -134,3 +132,3 @@ for (let i = 0; i < fixCount; i++) { | ||
function splitPlugins(plugins, {template}) { | ||
function splitPlugins(plugins) { | ||
const pluginsFind = []; | ||
@@ -153,5 +151,3 @@ const pluginsTraverse = []; | ||
if (plugin.replace) { | ||
pluginsTraverse.push(include(replace(item, { | ||
template, | ||
}))); | ||
pluginsTraverse.push(include(replace(item))); | ||
continue; | ||
@@ -158,0 +154,0 @@ } |
'use strict'; | ||
const isString = (a) => typeof a === 'string'; | ||
const {template} = require('@putout/engine-parser'); | ||
@@ -31,9 +33,2 @@ const { | ||
const validateTemplateValues = (a, b) => { | ||
for (const key of keys(a)) { | ||
if (!b[key]) | ||
throw Error(`☝️ Looks like template values not linked: ${stringify(keys(b))} -> ${stringify(keys(a))}`); | ||
} | ||
}; | ||
module.exports = ({rule, plugin, msg, options}) => { | ||
@@ -155,7 +150,17 @@ const { | ||
if (isObj(toStr)) | ||
if (isObj(toStr) && toStr.type) | ||
return toStr; | ||
if (!isString(toStr)) | ||
throw Error(`☝️ Looks like you passed 'replace' value with a wrong type. Allowed: 'string', 'node' and 'path'. Received: '${typeof toStr}' with value '${toStr}'.`); | ||
return template.ast.fresh(toStr); | ||
} | ||
const validateTemplateValues = (a, b) => { | ||
for (const key of keys(a)) { | ||
if (!b[key]) | ||
throw Error(`☝️ Looks like template values not linked: ${stringify(keys(b))} -> ${stringify(keys(a))}`); | ||
} | ||
}; | ||
{ | ||
"name": "@putout/engine-runner", | ||
"version": "12.4.1", | ||
"version": "12.5.0", | ||
"type": "commonjs", | ||
@@ -5,0 +5,0 @@ "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)", |
134
README.md
@@ -16,2 +16,11 @@ # @putout/engine-runner [![NPM version][NPMIMGURL]][NPMURL] | ||
There is a couple plugin types supported by 🐊`Putout`: | ||
- ✅[`Replacer`](#replacer) | ||
- ✅[`Includer`](#includer) | ||
- ✅[`Traverser`](#traverser) | ||
- ✅[`Finder`](#finder) | ||
They goes from simplest to hardest. Let's start from `Replacer`. | ||
### Replacer | ||
@@ -175,12 +184,68 @@ | ||
#### Storages | ||
### Finder | ||
Storages is preferred way of keeping data 🐊`Putout`, `traverse` init function called only once, and any other way | ||
of handling variables will most likely will lead to bugs. | ||
`Find plugins` gives you all the control over traversing, but it's the slowest format. | ||
Because `traversers` not merged in contrast with other plugin formats. | ||
##### listStore | ||
```js | ||
module.exports.report = () => 'debugger statement should not be used'; | ||
To keep things and save them as a list during traverse in a safe way `listStore` can be used for code: | ||
module.exports.fix = (path) => { | ||
path.remove(); | ||
}; | ||
module.exports.find = (ast, {push, traverse}) => { | ||
traverse(ast, { | ||
'debugger'(path) { | ||
push(path); | ||
}, | ||
}); | ||
}; | ||
``` | ||
## Example | ||
```js | ||
const {runPlugins} = require('@putout/engine-runner'); | ||
const {parse} = require('@putout/engin-parser'); | ||
const plugins = [{ | ||
rule: "remove-debugger", | ||
msg: "", // optional | ||
options: {}, // optional | ||
plugin: { | ||
include: () => ['debugger'], | ||
fix: (path) => path.remove(), | ||
report: () => `debugger should not be used`, | ||
}, | ||
}]; | ||
const ast = parse('const m = "hi"; debugger'); | ||
const places = runPlugins({ | ||
ast, | ||
shebang: false, // default | ||
fix: true, // default | ||
fixCount: 1, // default | ||
plugins, | ||
parser: 'babel', // default | ||
}); | ||
``` | ||
## Stores | ||
Stores is preferred way of keeping 🐊`Putout` data, `traverse` init function called only once, and any other way | ||
of handling variables will most likely will lead to bugs. There is 3 store types: | ||
- ✅`listStore`; | ||
- ✅`store`; | ||
- ✅`upstore`; | ||
Let's talk about each of them. | ||
### `listStore` | ||
To save things as a list use `listStore`. | ||
Let's suppose you have such code: | ||
```js | ||
debugger; | ||
@@ -192,2 +257,4 @@ const hello = ''; | ||
Let's process it! | ||
```js | ||
@@ -211,4 +278,6 @@ module.exports.traverse = ({listStore}) => ({ | ||
When you need `key-value` storage `store` can be used. | ||
### `store` | ||
When you need `key-value` use `store`: | ||
```js | ||
@@ -239,5 +308,5 @@ module.exports.traverse = ({push, store}) => ({ | ||
#### Upstore | ||
### `upstore` | ||
When you need to update already saved values, use `upstore` | ||
When you need to update already saved values, use `upstore`: | ||
@@ -277,51 +346,2 @@ ```js | ||
### Finder | ||
`Find plugins` gives you all the control over traversing, but it's the slowest format. | ||
Because `traversers` not merged in contrast with other plugin formats. | ||
```js | ||
module.exports.report = () => 'debugger statement should not be used'; | ||
module.exports.fix = (path) => { | ||
path.remove(); | ||
}; | ||
module.exports.find = (ast, {push, traverse}) => { | ||
traverse(ast, { | ||
'debugger'(path) { | ||
push(path); | ||
}, | ||
}); | ||
}; | ||
``` | ||
## Example | ||
```js | ||
const {runPlugins} = require('@putout/engine-runner'); | ||
const {parse} = require('@putout/engin-parser'); | ||
const plugins = [{ | ||
rule: "remove-debugger", | ||
msg: "", // optional | ||
options: {}, // optional | ||
plugin: { | ||
include: () => ['debugger'], | ||
fix: (path) => path.remove(), | ||
report: () => `debugger should not be used`, | ||
}, | ||
}]; | ||
const ast = parse('const m = "hi"; debugger'); | ||
const places = runPlugins({ | ||
ast, | ||
shebang: false, // default | ||
fix: true, // default | ||
fixCount: 1, // default | ||
plugins, | ||
parser: 'babel', // default | ||
}); | ||
``` | ||
## Logs | ||
@@ -328,0 +348,0 @@ |
29599
354
706