@putout/plugin-variables 1.1.0 → 1.3.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.
@@ -0,0 +1,40 @@
1
+ import {operator} from 'putout';
2
+ import getVars from './get-vars/index.js';
3
+ import transform from './transform.js';
4
+ import getUnused from './get-unused.js';
5
+
6
+ const {
7
+ replaceWith,
8
+ compare,
9
+ remove,
10
+ } = operator;
11
+
12
+ export const report = ({name}) => `'${name}' is defined but never used`;
13
+
14
+ export const fix = ({path}) => {
15
+ if (compare(path, 'const __a = __b = __c'))
16
+ return replaceWith(path.parentPath, path.node.init);
17
+
18
+ if (isOneImport(path))
19
+ return remove(path.parentPath);
20
+
21
+ remove(path);
22
+ };
23
+
24
+ export const find = (ast, {traverse}) => {
25
+ const vars = getVars(ast, {
26
+ setPath: true,
27
+ traverse,
28
+ });
29
+
30
+ const transformed = transform(vars);
31
+
32
+ return getUnused(transformed);
33
+ };
34
+
35
+ function isOneImport({parentPath}) {
36
+ if (!parentPath.isImportDeclaration())
37
+ return false;
38
+
39
+ return parentPath.node.specifiers.length === 1;
40
+ }
@@ -0,0 +1,18 @@
1
+ export default (items) => {
2
+ const result = [];
3
+
4
+ for (const item of items) {
5
+ const entries = Object.entries(item);
6
+
7
+ for (const entry of entries) {
8
+ result.push(transform(entry));
9
+ }
10
+ }
11
+
12
+ return result;
13
+ };
14
+
15
+ const transform = ([name, value]) => ({
16
+ name,
17
+ ...value,
18
+ });
@@ -3,7 +3,7 @@ import {operator, types} from 'putout';
3
3
  const {remove, rename} = operator;
4
4
  const {isIdentifier} = types;
5
5
 
6
- export const report = ({idName}) => `Useless variable declaration with name "${idName}"`;
6
+ export const report = ({idName}) => `Avoid useless variable declaration with name '${idName}'`;
7
7
 
8
8
  export const fix = ({path, bindingPath, initName, idName}) => {
9
9
  rename(bindingPath, initName, idName);
@@ -0,0 +1,67 @@
1
+ import {types, operator} from 'putout';
2
+
3
+ const {
4
+ isForStatement,
5
+ variableDeclaration,
6
+ } = types;
7
+
8
+ const {
9
+ replaceWithMultiple,
10
+ isKeyword,
11
+ } = operator;
12
+
13
+ export const report = () => 'Variables should be declared separately';
14
+
15
+ export const fix = (path) => {
16
+ const {node} = path;
17
+ const varNodes = getVarNodes(node);
18
+
19
+ replaceWithMultiple(path, varNodes);
20
+ };
21
+
22
+ export const traverse = ({push}) => ({
23
+ VariableDeclaration(path) {
24
+ const {
25
+ node,
26
+ parent,
27
+ parentPath,
28
+ } = path;
29
+
30
+ const {declarations} = node;
31
+
32
+ if (parentPath.isExportDeclaration())
33
+ return;
34
+
35
+ if (declarations.length === 1)
36
+ return;
37
+
38
+ for (const declaration of declarations) {
39
+ const {name} = declaration.id;
40
+
41
+ if (isKeyword(name))
42
+ return;
43
+ }
44
+
45
+ const init = node;
46
+
47
+ if (isForStatement(parent, {init}))
48
+ return;
49
+
50
+ push(path);
51
+ },
52
+ });
53
+
54
+ function getVarNodes({kind, declarations}) {
55
+ const result = [];
56
+
57
+ for (const declaration of declarations) {
58
+ const declarations = [declaration];
59
+
60
+ result.push(variableDeclaration(
61
+ kind,
62
+ declarations,
63
+ ));
64
+ }
65
+
66
+ return result;
67
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-variables",
3
- "version": "1.1.0",
3
+ "version": "1.3.0",
4
4
  "type": "module",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
6
  "description": "🐊Putout plugin adds ability to find and remove useless",
@@ -40,6 +40,8 @@
40
40
  "@putout/plugin-for-of": "*",
41
41
  "@putout/plugin-maybe": "*",
42
42
  "@putout/plugin-minify": "*",
43
+ "@putout/plugin-nodejs": "*",
44
+ "@putout/plugin-putout": "*",
43
45
  "@putout/plugin-remove-useless-array-from": "*",
44
46
  "@putout/plugin-reuse-duplicate-init": "*",
45
47
  "@putout/test": "^14.0.0",
@@ -47,8 +49,11 @@
47
49
  "eslint": "v10.0.0-alpha.0",
48
50
  "eslint-plugin-n": "^17.0.0",
49
51
  "eslint-plugin-putout": "^29.0.0",
52
+ "just-camel-case": "^6.2.0",
50
53
  "madrun": "^11.0.0",
51
- "nodemon": "^3.0.1"
54
+ "nodemon": "^3.0.1",
55
+ "supertape": "^11.3.1",
56
+ "try-catch": "^3.0.1"
52
57
  },
53
58
  "peerDependencies": {
54
59
  "putout": ">=41"