@putout/plugin-tape 3.5.0 → 3.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -18,6 +18,9 @@ npm i @putout/plugin-tape -D
18
18
  ```json
19
19
  {
20
20
  "rules": {
21
+ "tape/apply-stub-operator": "on",
22
+ "tape/apply-with-name": "on",
23
+ "tape/sync-with-name": "on",
21
24
  "tape/switch-expected-with-result": "on",
22
25
  "tape/convert-tape-to-supertape": "on",
23
26
  "tape/convert-throws-to-try-catch": "on",
@@ -26,13 +29,14 @@ npm i @putout/plugin-tape -D
26
29
  "tape/convert-called-with-to-called-with-no-args": "on",
27
30
  "tape/convert-called-with-no-args-to-called-with": "on",
28
31
  "tape/expand-try-catch-arguments": "on",
29
- "tape/apply-stub-operator": "on",
30
32
  "tape/convert-emitter-to-promise": "on",
31
33
  "tape/convert-ok-to-match": "on",
32
34
  "tape/convert-ok-to-called-with": "on",
33
35
  "tape/convert-match-regexp-to-string": "on",
34
36
  "tape/declare-stub": "on",
35
- "tape/declare-t": "on"
37
+ "tape/declare-t": "on",
38
+ "tape/remove-default-messages": "on",
39
+ "tape/remove-useless-not-called-args": "on"
36
40
  }
37
41
  }
38
42
  ```
@@ -314,6 +318,58 @@ test('some message', (t) => {
314
318
  });
315
319
  ```
316
320
 
321
+ ## apply-with-name
322
+
323
+ ### ❌ Incorrect code example
324
+
325
+ ```js
326
+ test('should call init before show', (t) => {
327
+ const init = stub();
328
+ const show = stub();
329
+
330
+ t.calledInOrder([init, show]);
331
+ t.end();
332
+ });
333
+ ```
334
+
335
+ ### ✅ Correct code example
336
+
337
+ ```js
338
+ test('should call init before show', (t) => {
339
+ const init = stub().withName('init');
340
+ const show = stub().withName('show');
341
+
342
+ t.calledInOrder([init, show]);
343
+ t.end();
344
+ });
345
+ ```
346
+
347
+ ## sync-with-name
348
+
349
+ ### ❌ Incorrect code example
350
+
351
+ ```js
352
+ test('should call init before show', (t) => {
353
+ const init = stub().withName('show');
354
+ const show = stub().withName('show');
355
+
356
+ t.calledInOrder([init, show]);
357
+ t.end();
358
+ });
359
+ ```
360
+
361
+ ### ✅ Correct code example
362
+
363
+ ```js
364
+ test('should call init before show', (t) => {
365
+ const init = stub().withName('init');
366
+ const show = stub().withName('show');
367
+
368
+ t.calledInOrder([init, show]);
369
+ t.end();
370
+ });
371
+ ```
372
+
317
373
  ## declare-stub
318
374
 
319
375
  ### ❌ Incorrect code example
@@ -445,6 +501,20 @@ t.equal(result, expected, 'should equal');
445
501
  t.equal(result, expected);
446
502
  ```
447
503
 
504
+ ## remove-useless-not-called-args
505
+
506
+ ### ❌ Incorrect code example
507
+
508
+ ```js
509
+ t.notCalled(fn, []);
510
+ ```
511
+
512
+ ### ✅ Correct code example
513
+
514
+ ```js
515
+ t.notCalled(fn);
516
+ ```
517
+
448
518
  ## License
449
519
 
450
520
  MIT
@@ -0,0 +1,85 @@
1
+ 'use strict';
2
+
3
+ const {
4
+ template,
5
+ operator,
6
+ } = require('putout');
7
+
8
+ const {replaceWith} = operator;
9
+
10
+ module.exports.report = () => `'stub().withName()' should be used`;
11
+
12
+ module.exports.match = () => ({
13
+ 't.calledBefore(__a, __b)': checkStubs,
14
+ 't.calledAfter(__a, __b)': checkStubs,
15
+ 't.calledInOrder(__array)': checkStubsArray,
16
+ });
17
+
18
+ module.exports.replace = () => ({
19
+ 't.calledBefore(__a, __b)': applyWithName,
20
+ 't.calledAfter(__a, __b)': applyWithName,
21
+ 't.calledInOrder(__array)': applyWithNameToArray,
22
+ });
23
+
24
+ function checkStubs({__a, __b}, path) {
25
+ const elements = [__a, __b];
26
+ const __array = {
27
+ elements,
28
+ };
29
+ return checkStubsArray({__array}, path);
30
+ }
31
+
32
+ function applyWithName({__a, __b}, path) {
33
+ applyWithNameToNode(__a, path);
34
+ applyWithNameToNode(__b, path);
35
+ return path;
36
+ }
37
+
38
+ function checkStubsArray({__array}, path) {
39
+ for (const stub of __array.elements) {
40
+ if (checkStub(stub, path))
41
+ return true;
42
+ }
43
+
44
+ return false;
45
+ }
46
+
47
+ function applyWithNameToArray({__array}, path) {
48
+ for (const stub of __array.elements) {
49
+ applyWithNameToNode(stub, path);
50
+ }
51
+
52
+ return path;
53
+ }
54
+
55
+ function checkStub(node, path) {
56
+ const {bindings} = path.scope;
57
+ const {name} = node;
58
+ const binding = bindings[name];
59
+
60
+ if (!binding)
61
+ return false;
62
+
63
+ const initPath = binding.path.get('init');
64
+ const calleePath = initPath.get('callee');
65
+
66
+ return !calleePath.isMemberExpression();
67
+ }
68
+
69
+ function applyWithNameToNode(node, path) {
70
+ const {bindings} = path.scope;
71
+ const {name} = node;
72
+ const binding = bindings[name];
73
+
74
+ if (!binding)
75
+ return;
76
+
77
+ const initPath = binding.path.get('init');
78
+ const calleePath = initPath.get('callee');
79
+
80
+ if (calleePath.isMemberExpression())
81
+ return;
82
+
83
+ replaceWith(initPath, template.ast(`stub().withName('${name}')`));
84
+ }
85
+
package/lib/index.js CHANGED
@@ -5,7 +5,8 @@ const getRule = (a) => ({
5
5
  });
6
6
 
7
7
  module.exports.rules = {
8
- ...getRule('switch-expected-with-result'),
8
+ ...getRule('apply-with-name'),
9
+ ...getRule('sync-with-name'),
9
10
  ...getRule('convert-tape-to-supertape'),
10
11
  ...getRule('convert-throws-to-try-catch'),
11
12
  ...getRule('convert-does-not-throw-to-try-catch'),
@@ -19,9 +20,11 @@ module.exports.rules = {
19
20
  ...getRule('convert-equal-to-not-ok'),
20
21
  ...getRule('convert-equal-to-ok'),
21
22
  ...getRule('expand-try-catch-arguments'),
22
- ...getRule('apply-stub-operator'),
23
23
  ...getRule('declare-stub'),
24
24
  ...getRule('declare-t'),
25
25
  ...getRule('remove-default-messages'),
26
+ ...getRule('remove-useless-not-called-args'),
27
+ ...getRule('switch-expected-with-result'),
28
+ ...getRule('apply-stub-operator'),
26
29
  };
27
30
 
@@ -0,0 +1,8 @@
1
+ 'use strict';
2
+
3
+ module.exports.report = () => 'Remove useless "notCalled" args';
4
+
5
+ module.exports.replace = () => ({
6
+ 't.notCalled(__a, __array)': 't.notCalled(__a)',
7
+ });
8
+
@@ -0,0 +1,14 @@
1
+ 'use strict';
2
+
3
+ module.exports.report = () => `'stub().withName(name)' should synced with variable name`;
4
+
5
+ module.exports.match = () => ({
6
+ 'const __a = stub().withName(__b)': ({__a, __b}) => __b.value !== __a.name,
7
+ });
8
+
9
+ module.exports.replace = () => ({
10
+ 'const __a = stub().withName(__b)': ({__a, __b}, path) => {
11
+ __b.value = __a.name;
12
+ return path;
13
+ },
14
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-tape",
3
- "version": "3.5.0",
3
+ "version": "3.8.0",
4
4
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
5
5
  "description": "putout plugin helps with tests",
6
6
  "homepage": "http://github.com/coderaiser/putout",
@@ -39,6 +39,7 @@
39
39
  "eslint-plugin-putout": "^10.0.0",
40
40
  "lerna": "^4.0.0",
41
41
  "madrun": "^8.0.1",
42
+ "montag": "^1.2.1",
42
43
  "nodemon": "^2.0.1"
43
44
  },
44
45
  "peerDependencies": {