Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
@putout/engine-runner
Advanced tools
Run πPutout plugins.
npm i @putout/engine-runner
There is a couple plugin types supported by πPutout:
All of them supports subset of JavaScript π¦PutoutScript described in @putout/compare
.
They goes from simplest to hardest. Let's start from Replacer.
Replacer converts code in declarative way. Simplest possible form, no need to use fix
. Just from
and to
parts
according to template variables syntax
.
Simplest replace example:
module.exports.report = () => 'any message here';
module.exports.replace = () => ({
'const a = 1': 'const b = 1',
});
// optional
module.exports.filter = (path) => {
return true;
};
// optional
module.exports.match = () => ({
'const __a = 1': ({__a}) => {
return true;
},
});
// optional
module.exports.exclude = () => [
`const hello = 'world'`,
'ArrowFunctionExpression',
];
Simplest remove example:
module.exports.report = () => 'debugger should not be used';
module.exports.replace = () => ({
debugger: '',
});
Templates:
module.exports.report = () => 'any message here';
module.exports.replace = () => ({
'var __a = 1': 'const __a = 1',
});
A couple variables example:
module.exports.report = () => 'any message here';
module.exports.replace = () => ({
'const __a = __b': 'const __b = __a',
});
You can pass a function as object value for more soficticated processing.
Remove node:
module.exports.report = () => 'any message here';
module.exports.replace = () => ({
'for (const __a of __b) __c': ({__a, __b, __c}, path) => {
// remove node
return '';
},
});
Update node:
module.exports.report = () => 'any message here';
module.exports.replace = () => ({
'for (const __a of __array) __c': ({__a, __array, __c}, path) => {
// update __array elements count
path.node.right.elements = [];
return path;
},
});
Update node using template variables:
module.exports.report = () => 'any message here';
module.exports.replace = () => ({
'for (const __a of __array) __c': ({__a, __array, __c}, path) => {
// update the whole node using template string
return 'for (const x of y) z';
},
});
includer
is the most preferable format of a plugin, simplest to use (after replacer
)
module.exports.report = () => 'debugger statement should not be used';
module.exports.fix = (path) => {
path.remove();
};
module.exports.include = () => [
'debugger',
];
// optional
module.exports.exclude = () => {
};
// optional
module.exports.filter = (path) => {
return true;
};
include
and exclude
returns an array of @babel/types, or code blocks:
const __ = 'hello';
Where __
can be any node. All this possible with help of @putout/compare. Templates format supported in traverse
and find
plugins as well.
Traverse plugins
gives you more power to filter
and fix
nodes you need.
module.exports.report = () => 'debugger statement should not be used';
module.exports.fix = (path, {options}) => {
path.remove();
};
module.exports.traverse = ({push}) => ({
'debugger'(path) {
push(path);
},
});
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.
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);
},
});
};
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 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
;uplist
;Let's talk about each of them.
listStore
To save things as a list use listStore
.
Let's suppose you have such code:
debugger;
const hello = '';
debugger;
const world = '';
Let's process it!
module.exports.traverse = ({listStore}) => ({
'debugger'(path) {
listStore('x');
},
Program: {
exit() {
console.log(listStore());
// returns
['x', 'x'];
// for code
'debugger; debugger';
},
},
});
store
When you need key-value
use store
:
module.exports.traverse = ({push, store}) => ({
'debugger'(path) {
store('hello', 'world');
push(path);
},
Program: {
exit() {
store();
// returns
['world'];
store.entries();
// returns
[['hello', 'world']];
store('hello');
// returns
'world';
},
},
});
upstore
When you need to update already saved values, use upstore
:
module.exports.traverse = ({push, store}) => ({
TSTypeAliasDeclaration(path) {
if (path.parentPath.isExportNamedDeclaration())
return;
store(path.node.id.name, {
path,
});
},
ObjectProperty(path) {
const {value} = path.node;
const {name} = value;
store(name, {
used: true,
});
},
Program: {
exit() {
for (const {path, used} of store()) {
if (used)
continue;
push(path);
}
},
},
});
uplist
When you need to update named arrays:
module.exports.traverse = ({uplist, push}) => ({
'const __object = __a.__b': (fullPath) => {
const {__a, __b} = getTemplateValues(fullPath, 'const __object = __a.__b');
const path = fullPath.get('declarations.0.init');
const {uid} = path.scope;
if (isIdentifier(__a) || isCallExpression(__a)) {
const {code} = generate(__a);
const id = `${uid}-${code}`;
return uplist(id, path);
}
},
'Program': {
exit: () => {
for (const items of uplist()) {
if (items.length < 2)
continue;
const index = items.length - 1;
const path = items[index];
push({
path,
items,
});
}
},
},
});
To see logs, use:
DEBUG=putout:runner:*
DEBUG=putout:runner:fix
DEBUG=putout:runner:find
DEBUG=putout:runner:template
DEBUG=putout:runner:replace
MIT
FAQs
Run πPutout plugins
The npm package @putout/engine-runner receives a total of 12,312 weekly downloads. As such, @putout/engine-runner popularity was classified as popular.
We found that @putout/engine-runner demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago.Β It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.