@putout/plugin-tape 9.2.0 → 9.4.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
@@ -20,6 +20,7 @@ npm i @putout/plugin-tape -D
20
20
  ```json
21
21
  {
22
22
  "rules": {
23
+ "tape/apply-stub": "on",
23
24
  "tape/apply-stub-operator": "on",
24
25
  "tape/apply-with-name": "on",
25
26
  "tape/add-t-end": "on",
@@ -333,6 +334,32 @@ test('copymitter', async (t) => {
333
334
  });
334
335
  ```
335
336
 
337
+ ## apply-stub
338
+
339
+ Apply [stub](https://github.com/cloudcmd/stub) functions created. Look how it works in [Putout Editor](https://putout.cloudcmd.io/#/gist/d098a8c8d03a072a7f3981e3cfb1d7af/f1542bfa39513ac625b2a9cd9243cc06dee286c0).
340
+
341
+ ### ❌ Example of incorrect code
342
+
343
+ ```js
344
+ const a = async () => true;
345
+ const b = async () => {};
346
+
347
+ const c = async () => throw Error('hello');
348
+ const d = async () => {
349
+ throw Error('hello');
350
+ };
351
+ ```
352
+
353
+ ### ✅ Example of correct code
354
+
355
+ ```js
356
+ const a = stub().resolves(true);
357
+ const b = stub().resolves();
358
+
359
+ const c = stub().rejects(Error('hello'));
360
+ const d = stub().rejects(Error('hello'));
361
+ ```
362
+
336
363
  ## apply-stub-operator
337
364
 
338
365
  ### ❌ Example of incorrect code
@@ -0,0 +1,54 @@
1
+ 'use strict';
2
+
3
+ const {
4
+ template,
5
+ types,
6
+ } = require('putout');
7
+
8
+ const {
9
+ isUnaryExpression,
10
+ isThrowStatement,
11
+ isBlockStatement,
12
+ } = types;
13
+
14
+ const REJECTS = template('stub().rejects(A)');
15
+
16
+ module.exports.report = () => `Use 'stub()' instead of creating a function`;
17
+
18
+ module.exports.match = () => ({
19
+ 'async () => __a': ({__a}, path) => {
20
+ if (!path.parentPath.isVariableDeclarator())
21
+ return false;
22
+
23
+ if (!isBlockStatement(__a))
24
+ return true;
25
+
26
+ if (!__a.body.length)
27
+ return true;
28
+
29
+ return isThrowStatement(__a.body[0]);
30
+ },
31
+ });
32
+
33
+ module.exports.replace = () => ({
34
+ 'async () => __a': ({__a}) => {
35
+ if (isUnaryExpression(__a, {operator: 'throw'})) {
36
+ return REJECTS({
37
+ A: __a.argument,
38
+ });
39
+ }
40
+
41
+ if (isBlockStatement(__a) && !__a.body.length) {
42
+ return 'stub().resolves()';
43
+ }
44
+
45
+ if (isBlockStatement(__a) && isThrowStatement(__a.body[0])) {
46
+ return REJECTS({
47
+ A: __a.body[0].argument,
48
+ });
49
+ }
50
+
51
+ return 'stub().resolves(__a)';
52
+ },
53
+ });
54
+
@@ -57,7 +57,17 @@ function checkStub(node, path) {
57
57
  const {name} = node;
58
58
  const binding = bindings[name];
59
59
 
60
- return binding;
60
+ if (!binding)
61
+ return false;
62
+
63
+ const initPath = binding.path.get('init');
64
+ const calleePath = initPath.get('callee');
65
+ const str = initPath.toString();
66
+
67
+ if (calleePath.isMemberExpression() && str.includes('withName'))
68
+ return false;
69
+
70
+ return true;
61
71
  }
62
72
 
63
73
  function applyWithNameToNode(node, path) {
@@ -60,9 +60,10 @@ function replace({__a}, path) {
60
60
 
61
61
  const arg = path.get('arguments.1').node;
62
62
  arg.async = true;
63
- const {body} = arg.body;
64
63
 
64
+ const {body} = arg.body;
65
65
  const n = body.length;
66
+
66
67
  for (let i = 0; i < n; i++) {
67
68
  let nodes = [];
68
69
 
package/lib/index.js CHANGED
@@ -5,6 +5,7 @@ const getRule = (a) => ({
5
5
  });
6
6
 
7
7
  module.exports.rules = {
8
+ ...getRule('apply-stub'),
8
9
  ...getRule('apply-with-name'),
9
10
  ...getRule('add-await-to-re-import'),
10
11
  ...getRule('sync-with-name'),
@@ -11,12 +11,15 @@ const {
11
11
  TEST_ASYNC_SKIP,
12
12
  } = require('../test-signatures');
13
13
 
14
- const {traverse} = operator;
14
+ const {
15
+ traverse,
16
+ remove,
17
+ } = operator;
15
18
 
16
19
  module.exports.report = () => `Remove 'stopAll()' when you not calling 'reImport()'`;
17
20
 
18
21
  module.exports.fix = (path) => {
19
- path.remove();
22
+ remove(path);
20
23
  };
21
24
 
22
25
  module.exports.traverse = ({push}) => ({
@@ -2,12 +2,15 @@
2
2
 
3
3
  const {operator} = require('putout');
4
4
 
5
- const {traverse} = operator;
5
+ const {
6
+ traverse,
7
+ remove,
8
+ } = operator;
6
9
 
7
10
  module.exports.report = () => `Avoid useless 't.end()'`;
8
11
 
9
12
  module.exports.fix = (path) => {
10
- path.remove();
13
+ remove(path);
11
14
  };
12
15
 
13
16
  const TEST = 'test("__a", (t) => __body)';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-tape",
3
- "version": "9.2.0",
3
+ "version": "9.4.0",
4
4
  "type": "commonjs",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
6
  "description": "putout plugin helps with tests",