Skip to content

Commit 482e7a7

Browse files
committed
feature: @putout/engine-runner: add error reason when throw during traverse
1 parent bf5481e commit 482e7a7

File tree

3 files changed

+79
-3
lines changed

3 files changed

+79
-3
lines changed

packages/engine-runner/lib/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const declare = require('./declare');
1313
const scanner = require('./scanner');
1414
const template = require('./template');
1515
const {createProgress} = require('./progress');
16+
const {tryThrowWithReason} = require('./try-throw-with-reason');
1617

1718
const {getPath, getPosition} = require('./get-position');
1819

@@ -80,7 +81,7 @@ function runWithMerge({ast, fix, shebang, template, pluginsTraverse, merge, trav
8081
template,
8182
});
8283

83-
traverse(ast, visitor);
84+
tryThrowWithReason(traverse, ast, visitor);
8485

8586
const places = [];
8687

@@ -107,7 +108,7 @@ function runWithoutMerge({ast, fix, shebang, template, pluginsFind}) {
107108

108109
const {report, find} = plugin;
109110

110-
const items = superFind({
111+
const items = tryThrowWithReason(superFind, {
111112
rule,
112113
find,
113114
ast,
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
3+
const tryCatch = require('try-catch');
4+
5+
module.exports.tryThrowWithReason = (fn, ...args) => {
6+
const [error, result] = tryCatch(fn, ...args);
7+
8+
if (error) {
9+
error.reason = 'traverse';
10+
throw error;
11+
}
12+
13+
return result;
14+
};

packages/engine-runner/test/runner.js

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const {loadPlugins} = require('@putout/engine-loader');
1010

1111
const {readFixtures} = require('./fixture');
1212
const {runPlugins} = require('..');
13-
13+
const noop = () => {};
1414
const {reRequire, stopAll} = mockRequire;
1515
const {parse} = putout;
1616

@@ -1036,3 +1036,64 @@ test('putout: runner: runPlugins: override traverse', (t) => {
10361036
t.calledWith(traverse, expected);
10371037
t.end();
10381038
});
1039+
1040+
test('putout: runner: runPlugins: find: reason', (t) => {
1041+
const duplicator = {
1042+
report: () => '',
1043+
find: (ast, {traverse}) => {
1044+
traverse(ast, {
1045+
DebuggerStatement: () => {
1046+
throw Error('x');
1047+
},
1048+
});
1049+
},
1050+
};
1051+
1052+
const code = 'debugger';
1053+
const ast = parse(code);
1054+
1055+
const plugins = loadPlugins({
1056+
pluginNames: [
1057+
['duplicator', duplicator],
1058+
],
1059+
});
1060+
1061+
const [error] = tryCatch(runPlugins, {
1062+
ast,
1063+
fix: false,
1064+
plugins,
1065+
});
1066+
1067+
t.equal(error.reason, 'traverse');
1068+
t.end();
1069+
});
1070+
1071+
test('putout: runner: runPlugins: traverse: reason', (t) => {
1072+
const duplicator = {
1073+
report: () => '',
1074+
fix: noop,
1075+
traverse: () => ({
1076+
DebuggerStatement: () => {
1077+
throw Error('x');
1078+
},
1079+
}),
1080+
};
1081+
1082+
const code = 'debugger';
1083+
const ast = parse(code);
1084+
1085+
const plugins = loadPlugins({
1086+
pluginNames: [
1087+
['duplicator', duplicator],
1088+
],
1089+
});
1090+
1091+
const [error] = tryCatch(runPlugins, {
1092+
ast,
1093+
fix: false,
1094+
plugins,
1095+
});
1096+
1097+
t.equal(error.reason, 'traverse');
1098+
t.end();
1099+
});

0 commit comments

Comments
 (0)