@putout/plugin-for-of 1.1.0 → 1.2.1

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.
@@ -1,11 +1,17 @@
1
1
  'use strict';
2
2
 
3
- const {types} = require('putout');
3
+ const {
4
+ types,
5
+ operator,
6
+ } = require('putout');
7
+
4
8
  const {
5
9
  isIdentifier,
6
10
  isObjectProperty,
11
+ isObjectPattern,
7
12
  } = types;
8
13
 
14
+ const {remove} = operator;
9
15
  const getValue = ({value}) => value;
10
16
 
11
17
  module.exports.report = (path) => {
@@ -29,9 +35,6 @@ function match(path) {
29
35
  const idPath = path.get('left.declarations.0.id');
30
36
  const elements = getElements(idPath);
31
37
 
32
- if (!isAllIdentifiers(elements))
33
- return false;
34
-
35
38
  if (isAllReferenced(idPath, elements))
36
39
  return false;
37
40
 
@@ -42,7 +45,7 @@ function replace(path) {
42
45
  const variables = getVariables(path);
43
46
 
44
47
  for (const currentPath of variables) {
45
- currentPath.remove();
48
+ remove(currentPath);
46
49
  }
47
50
 
48
51
  return path;
@@ -61,15 +64,6 @@ function getVariables(path) {
61
64
  return result;
62
65
  }
63
66
 
64
- function isAllIdentifiers(array) {
65
- const realArray = array.filter(Boolean);
66
-
67
- const {length} = realArray;
68
- const {length: realLength} = realArray.filter(isIdentifier);
69
-
70
- return length === realLength;
71
- }
72
-
73
67
  function isReferenced({scope}, path) {
74
68
  const {bindings} = scope;
75
69
 
@@ -80,6 +74,10 @@ function isReferenced({scope}, path) {
80
74
  return !current || current.referenced;
81
75
  }
82
76
 
77
+ if (path.isObjectPattern()) {
78
+ return true;
79
+ }
80
+
83
81
  const {node} = path;
84
82
 
85
83
  if (!node)
@@ -90,14 +88,30 @@ function isReferenced({scope}, path) {
90
88
  return bindings[name].referenced;
91
89
  }
92
90
 
93
- function isAllReferenced(path, array) {
91
+ function isAllReferenced(path, nodes) {
94
92
  const {bindings} = path.scope;
95
93
 
96
- for (const {name} of array) {
97
- const current = bindings[name];
94
+ for (const node of nodes) {
95
+ if (isIdentifier(node)) {
96
+ const {name} = node;
97
+ const current = bindings[name];
98
+
99
+ if (!current.referenced)
100
+ return false;
101
+
102
+ continue;
103
+ }
98
104
 
99
- if (!current.referenced)
100
- return false;
105
+ if (isObjectPattern(node)) {
106
+ for (const {key} of node.properties) {
107
+ const current = bindings[key.name];
108
+
109
+ if (!current.referenced)
110
+ return false;
111
+ }
112
+
113
+ continue;
114
+ }
101
115
  }
102
116
 
103
117
  return true;
@@ -118,7 +132,6 @@ function getElements({node}) {
118
132
  function getPathElements(path) {
119
133
  if (path.isObjectPattern())
120
134
  return path.get('properties');
121
-
122
135
  return path.get('elements');
123
136
  }
124
137
 
@@ -136,4 +149,3 @@ function getNames(paths) {
136
149
 
137
150
  return result;
138
151
  }
139
-
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-for-of",
3
- "version": "1.1.0",
3
+ "version": "1.2.1",
4
4
  "type": "commonjs",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
6
  "description": "🐊Putout plugin adds ability to apply for...of",