Skip to content

Commit a43e408

Browse files
committed
feature: @putout/engine-runner: get rid of mock-require
1 parent 8cdfb8f commit a43e408

File tree

8 files changed

+56
-58
lines changed

8 files changed

+56
-58
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
const debug = require('debug');
4+
5+
module.exports.createDebug = (namespace) => {
6+
const log = debug(namespace);
7+
8+
return new Proxy(log, {
9+
apply(target, thisArg, args) {
10+
global.__putout_debug(namespace, ...args);
11+
return target(...args);
12+
},
13+
get(target, prop) {
14+
if (global.__putout_debug?.[prop])
15+
return true;
16+
17+
return target[prop];
18+
},
19+
});
20+
};

packages/engine-runner/lib/replacer/index.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,15 @@ const {
1111
setValues,
1212
} = require('@putout/compare');
1313

14-
const debug = require('debug')('putout:runner:replace');
1514
const maybeArray = require('../maybe-array');
16-
1715
const watermark = require('./watermark');
16+
const {createDebug} = require('../debug');
17+
const debug = createDebug('putout:runner:replace');
18+
19+
const log = (from, path) => {
20+
debug.enabled && debug(`${from} -> ${path}\n`);
21+
};
22+
1823
const {
1924
isExpression,
2025
isStatement,
@@ -34,10 +39,6 @@ const PRINT_OPTIONS = {
3439

3540
const isString = (a) => typeof a === 'string';
3641

37-
const log = (from, path) => {
38-
debug.enabled && debug(`${from} -> ${path}\n`);
39-
};
40-
4142
const {keys, entries} = Object;
4243
const {stringify} = JSON;
4344

packages/engine-runner/lib/run-fix.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
'use strict';
22

33
const tryCatch = require('try-catch');
4-
const debug = require('debug')('putout:runner:fix');
5-
const {enabled} = debug;
4+
const {createDebug} = require('./debug');
5+
66
const {stringify} = JSON;
77

88
const isFn = (a) => typeof a === 'function';
99
const getPath = (path) => path.path || path;
10+
const debug = createDebug('putout:runner:fix');
1011

1112
const chooseFixArgs = ({path, pathOptions, options}) => {
1213
if (pathOptions)
@@ -51,7 +52,7 @@ module.exports = (is, fix, {path, pathOptions, rule, position, options}) => {
5152
if (!is)
5253
return;
5354

54-
if (enabled)
55+
if (debug.enabled)
5556
debug(`fix: ${rule}`, position, getPath(path).toString());
5657

5758
validate('fix', fix);

packages/engine-runner/lib/run-fix.spec.js

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
'use strict';
22

3-
const mockRequire = require('mock-require');
4-
53
const {test, stub} = require('supertape');
6-
74
const tryCatch = require('try-catch');
85
const fix = require('./run-fix');
96

10-
const {stopAll, reRequire} = mockRequire;
11-
12-
test('fix: error', (t) => {
7+
test('engine-runner: fix: error', (t) => {
138
const remove = () => {
149
throw Error('hello');
1510
};
@@ -40,9 +35,8 @@ test('fix: error: nested path: debug', (t) => {
4035
const debugFn = stub();
4136

4237
debugFn.enabled = true;
43-
const debug = stub().returns(debugFn);
4438

45-
mockRequire('debug', debug);
39+
global.__putout_debug = stub().returns(debugFn);
4640

4741
const remove = () => {
4842
throw Error('hello');
@@ -60,26 +54,25 @@ test('fix: error: nested path: debug', (t) => {
6054
};
6155

6256
const is = true;
63-
const fix = reRequire('./run-fix');
6457

6558
const [e] = tryCatch(fix, is, fn, {
6659
path,
6760
position,
6861
});
6962

70-
stopAll();
63+
delete global.__putout_debug;
7164

7265
t.equal(e.loc, position);
7366
t.end();
7467
});
7568

7669
test('fix: error: nested path: debug: nested path', (t) => {
70+
const {__putout_debug} = global;
7771
const debugFn = stub();
7872

7973
debugFn.enabled = true;
80-
const debug = stub().returns(debugFn);
8174

82-
mockRequire('debug', debug);
75+
global.__putout_debug = stub().returns(debugFn);
8376

8477
const remove = () => {
8578
throw Error('hello');
@@ -99,14 +92,13 @@ test('fix: error: nested path: debug: nested path', (t) => {
9992
};
10093

10194
const is = true;
102-
const fix = reRequire('./run-fix');
10395

10496
const [e] = tryCatch(fix, is, fn, {
10597
path,
10698
position,
10799
});
108100

109-
stopAll();
101+
global.__putout_debug = __putout_debug;
110102

111103
t.equal(e.loc, position);
112104
t.end();

packages/engine-runner/lib/template/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ const {
1010
} = require('@putout/compare');
1111

1212
const maybeArray = require('../maybe-array');
13-
const debug = require('debug')('putout:runner:template');
13+
const {createDebug} = require('../debug');
14+
const debug = createDebug('putout:runner:template');
1415

1516
const {entries} = Object;
1617
const isFn = (a) => typeof a === 'function';

packages/engine-runner/lib/template/index.spec.js

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@
33
const {test, stub} = require('supertape');
44

55
const putout = require('putout');
6-
const mockRequire = require('mock-require');
7-
86
const {runPlugins} = require('..');
9-
10-
const {reRequire, stopAll} = mockRequire;
7+
const {_log} = require('.');
118

129
test('putout: plugin: traverse: template', (t) => {
1310
const exp = {
@@ -440,28 +437,27 @@ test('putout: plugin: traverse: template: log', (t) => {
440437

441438
test('putout: engine: runner: template: log', (t) => {
442439
const debug = stub();
440+
const {__putout_debug} = global;
443441

444-
mockRequire('debug', stub().returns(debug));
445-
const {_log} = reRequire('.');
442+
global.__putout_debug = stub().returns(debug);
446443

447444
_log();
448-
stopAll();
445+
global.__putout_debug = __putout_debug;
449446

450447
t.notCalled(debug, 'should call debug');
451448
t.end();
452449
});
453450

454451
test('putout: engine: runner: template: log: enabled', (t) => {
452+
const {__putout_debug} = global;
455453
const debug = stub();
456454

457455
debug.enabled = true;
458-
459-
mockRequire('debug', stub().returns(debug));
460-
const {_log} = reRequire('.');
456+
global.__putout_debug = debug;
461457

462458
_log('rule', 'path');
463-
stopAll();
459+
global.__putout_debug = __putout_debug;
464460

465-
t.calledWith(debug, ['rule', 'path'], 'should call debug');
461+
t.calledWith(debug, ['putout:runner:template', 'rule', 'path'], 'should call debug');
466462
t.end();
467463
});

packages/engine-runner/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858
"eslint-plugin-putout": "^26.0.0",
5959
"just-camel-case": "^6.2.0",
6060
"madrun": "^11.0.0",
61-
"mock-require": "^3.0.3",
6261
"montag": "^1.0.0",
6362
"nodemon": "^3.0.1",
6463
"putout": "*",

packages/engine-runner/test/runner.js

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@ const montag = require('montag');
55
const tryCatch = require('try-catch');
66
const {test, stub} = require('supertape');
77
const putout = require('putout');
8-
const mockRequire = require('mock-require');
98
const {loadPlugins} = require('@putout/engine-loader');
109

1110
const {readFixtures} = require('./fixture');
1211
const {runPlugins} = require('..');
1312
const noop = () => {};
14-
const {reRequire, stopAll} = mockRequire;
1513
const {parse} = putout;
1614

1715
const fixture = readFixtures([
@@ -693,13 +691,8 @@ test('putout: runner: debug', (t) => {
693691
const debugFn = stub();
694692

695693
debugFn.enabled = true;
696-
const debug = stub().returns(debugFn);
697694

698-
mockRequire('debug', debug);
699-
reRequire('../lib/run-fix');
700-
701-
const {runPlugins} = reRequire('..');
702-
const putout = reRequire('putout');
695+
global.__putout_debug = stub().returns(debugFn);
703696

704697
const {code} = putout(fixture.debug, {
705698
fix: true,
@@ -711,7 +704,7 @@ test('putout: runner: debug', (t) => {
711704

712705
process.env.DEBUG = DEBUG;
713706

714-
stopAll();
707+
delete process.env.__putout_debug;
715708

716709
t.equal(code, expected);
717710
t.end();
@@ -720,30 +713,25 @@ test('putout: runner: debug', (t) => {
720713
test('putout: runner: debug: replace', (t) => {
721714
const {DEBUG} = process.env;
722715

723-
process.env.DEBUG = 'putout:runner:fix';
724-
725716
const debugFn = stub();
726717

727718
debugFn.enabled = true;
728-
const debug = stub().returns(debugFn);
729-
730-
mockRequire('debug', debug);
731-
reRequire('../lib/replacer/index.js');
719+
global.__putout_debug = debugFn;
732720

733-
const {runPlugins} = reRequire('..');
734-
const putout = reRequire('putout');
721+
process.env.DEBUG = 'putout:runner:fix';
735722

736723
putout('debugger', {
737724
fix: true,
738-
runPlugins,
739725
plugins: ['remove-debugger'],
740726
});
741727

742-
const expected = [`debugger -> ''\n`];
728+
const expected = [
729+
'putout:runner:replace',
730+
`debugger -> ''\n`,
731+
];
743732

744733
process.env.DEBUG = DEBUG;
745-
746-
stopAll();
734+
delete global.__putout_debug;
747735

748736
t.calledWith(debugFn, expected);
749737
t.end();

0 commit comments

Comments
 (0)