Skip to content

Commit

Permalink
feature: @putout/engine-runner: improve plugins validation (#218)
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed Sep 10, 2024
1 parent 060ded6 commit 58eea70
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 8 deletions.
8 changes: 1 addition & 7 deletions packages/engine-runner/lib/include.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

const log = require('debug')('putout:runner:include');
const maybeArray = require('./maybe-array');
const {validate} = require('./validate');

const {stringify} = JSON;
const stub = () => [];
const good = () => true;
const isFn = (a) => typeof a === 'function';

module.exports = ({rule, plugin, msg, options}) => {
const {
Expand Down Expand Up @@ -64,8 +63,3 @@ const getTraverse = (include, filter, rule) => ({push, options}) => {

return result;
};

function validate(name, fn) {
if (!isFn(fn))
throw Error(`☝️ Looks like '${name}' is not a 'function' but '${typeof fn}' with value: '${stringify(fn)}'. More on using Includer: https://git.io/JqcMn`);
}
5 changes: 4 additions & 1 deletion packages/engine-runner/lib/merge-visitors.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const {generate} = require('@putout/engine-parser');
const runFix = require('./run-fix');
const {getPosition} = require('./get-position');
const maybeArray = require('./maybe-array');
const {validate} = require('./validate');

const {
listStore,
Expand All @@ -23,8 +24,10 @@ const {assign} = Object;
const parse = (name, plugin, options) => {
const list = [];

if (plugin[name])
if (plugin[name]) {
validate(name, plugin[name]);
list.push(...maybeArray(plugin[name]()));
}

if (options[name])
list.push(...maybeArray(options[name]));
Expand Down
9 changes: 9 additions & 0 deletions packages/engine-runner/lib/validate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

const isFn = (a) => typeof a === 'function';
const {stringify} = JSON;

module.exports.validate = (name, fn) => {
if (!isFn(fn))
throw Error(`☝️ Looks like '${name}' is not a 'function' but '${typeof fn}' with value: '${stringify(fn)}'. More on using Includer: https://git.io/JqcMn`);
};
38 changes: 38 additions & 0 deletions packages/engine-runner/test/include.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,41 @@ test('putout: runner: include: no fix', (t) => {
t.equal(error.message, expected);
t.end();
});

test('putout: runner: include: not function, with traverse', (t) => {
const source = `import type { Query } from 'assets/core_api/types/query'`;
const unyield = {
report: () => `Use 'return' instead of 'yield'`,
include: ['YieldExpression'],
fix: (path) => {
path.replaceWith(path.node.argument); // I want to just have the argument, not return it, I had this wrong earlier
},
traverse: ({push}) => ({
YieldExpression(path) {
push(path);
},
Program: {
exit(path) {
path.traverse({
Function(path) {
push(path);
},
});
},
},
}),
};

const [error] = tryCatch(putout, source, {
isTS: true,
fix: true,
plugins: [{
unyield,
}],
});

const expected = `☝️ Looks like 'include' is not a 'function' but 'object' with value: '["YieldExpression"]'. More on using Includer: https://git.io/JqcMn`;

t.equal(error.message, expected);
t.end();
});

0 comments on commit 58eea70

Please sign in to comment.