@putout/plugin-nodejs 4.5.0 → 4.6.2

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.
@@ -75,13 +75,13 @@ module.exports.traverse = ({push, listStore}) => ({
75
75
  if (!lastRequire.node.loc)
76
76
  return;
77
77
 
78
- const lastRequireLine = lastRequire.node.loc.start.line;
78
+ const lastRequireLine = lastRequire.node.loc.end.line;
79
79
 
80
80
  for (const path of constPaths) {
81
81
  const {loc} = path.node;
82
82
  const line = !loc ? 0 : loc.start.line;
83
83
 
84
- if (line < lastRequireLine) {
84
+ if (line < lastRequireLine && lastRequireLine < getReferenceLine(path, constPaths)) {
85
85
  push({
86
86
  path,
87
87
  firstRequire,
@@ -92,3 +92,34 @@ module.exports.traverse = ({push, listStore}) => ({
92
92
  },
93
93
  },
94
94
  });
95
+
96
+ function getName(path) {
97
+ const idPath = path.get('declarations.0.id');
98
+
99
+ if (idPath.isIdentifier())
100
+ return idPath.node.name;
101
+
102
+ if (idPath.isObjectPattern() && idPath.node.properties.length)
103
+ return idPath.node.properties[0].key.name;
104
+
105
+ return null;
106
+ }
107
+
108
+ function getReferenceLine(path, constPaths) {
109
+ const name = getName(path);
110
+ const binding = path.scope.bindings[name];
111
+
112
+ if (!binding)
113
+ return -1;
114
+
115
+ const {referencePaths} = binding;
116
+
117
+ if (!referencePaths.length)
118
+ return Infinity;
119
+
120
+ if (compareAny(referencePaths[0], constPaths))
121
+ return Infinity;
122
+
123
+ return referencePaths[0].node.loc.start.line;
124
+ }
125
+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-nodejs",
3
- "version": "4.5.0",
3
+ "version": "4.6.2",
4
4
  "type": "commonjs",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
6
  "description": "🐊Putout plugin adds ability to transform code to new API of Node.js",