@putout/plugin-for-of 8.1.0 → 9.0.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.
@@ -1,6 +1,5 @@
1
- 'use strict';
1
+ import {types} from 'putout';
2
2
 
3
- const {types} = require('putout');
4
3
  const {
5
4
  isVariableDeclaration,
6
5
  isIdentifier,
@@ -8,9 +7,9 @@ const {
8
7
  isObjectPattern,
9
8
  } = types;
10
9
 
11
- module.exports.report = () => `Add missing declaration`;
10
+ export const report = () => `Add missing declaration`;
12
11
 
13
- module.exports.match = () => ({
12
+ export const match = () => ({
14
13
  'for (__a of __b) __c': ({__a}, path) => {
15
14
  if (isVariableDeclaration(__a))
16
15
  return false;
@@ -42,6 +41,6 @@ module.exports.match = () => ({
42
41
  },
43
42
  });
44
43
 
45
- module.exports.replace = () => ({
44
+ export const replace = () => ({
46
45
  'for (__a of __b) __c': 'for (const __a of __b) __c',
47
46
  });
@@ -1,10 +1,8 @@
1
- 'use strict';
2
-
3
- const {
1
+ import {
4
2
  template,
5
3
  operator,
6
4
  types,
7
- } = require('putout');
5
+ } from 'putout';
8
6
 
9
7
  const {
10
8
  isIdentifier,
@@ -30,9 +28,9 @@ const forOfEntriesTemplate = template(`
30
28
  const {keys} = Object;
31
29
  const isRoot = (path) => path.isFunction() || path.isProgram();
32
30
 
33
- module.exports.report = () => `Use 'for-of' instead of 'forEach()'`;
31
+ export const report = () => `Use 'for-of' instead of 'forEach()'`;
34
32
 
35
- module.exports.replace = () => ({
33
+ export const replace = () => ({
36
34
  '__.forEach.call(__a, (__b) => __body)': 'for (const __b of __a) __body',
37
35
  '__.forEach(__args)': (vars, path) => {
38
36
  const {params, body} = path.node.arguments[0];
@@ -76,7 +74,7 @@ module.exports.replace = () => ({
76
74
  },
77
75
  });
78
76
 
79
- module.exports.match = () => ({
77
+ export const match = () => ({
80
78
  '__.forEach(__args)': (vars, path) => {
81
79
  const {parentPath} = path;
82
80
 
@@ -1,10 +1,8 @@
1
- 'use strict';
2
-
3
- const {
1
+ import {
4
2
  operator,
5
3
  types,
6
4
  template,
7
- } = require('putout');
5
+ } from 'putout';
8
6
 
9
7
  const {
10
8
  compare,
@@ -13,14 +11,14 @@ const {
13
11
 
14
12
  const {isBlockStatement} = types;
15
13
 
16
- module.exports.report = () => `Use 'for...of' instead of 'for'`;
14
+ export const report = () => `Use 'for...of' instead of 'for'`;
17
15
 
18
16
  const forLoop = 'for (let __i = 0; __i < __e.length; __i++) __c';
19
17
  const getForOfLoop = template(`for (const [INDEX, ITEM] of ITEMS.entries()) BODY`);
20
18
  const assignIterable = (__i) => `const __a = __b[${__i.name}]`;
21
19
  const assignIterableWithName = (__i, __e) => `const __a = ${__e.name}[${__i.name}]`;
22
20
 
23
- module.exports.filter = (path) => {
21
+ export const filter = (path) => {
24
22
  const {node} = path;
25
23
  const {body} = node;
26
24
 
@@ -43,7 +41,7 @@ module.exports.filter = (path) => {
43
41
  return compare(first, assignIterableWithName(__i, __e));
44
42
  };
45
43
 
46
- module.exports.replace = () => ({
44
+ export const replace = () => ({
47
45
  [forLoop]: ({__c, __i}) => {
48
46
  const [node] = __c.body;
49
47
  const {__a, __b} = getTemplateValues(node, assignIterable(__i));
@@ -1,10 +1,8 @@
1
- 'use strict';
2
-
3
- const {
1
+ import {
4
2
  operator,
5
3
  template,
6
4
  types,
7
- } = require('putout');
5
+ } from 'putout';
8
6
 
9
7
  const {
10
8
  compare,
@@ -13,14 +11,14 @@ const {
13
11
 
14
12
  const {isBlockStatement} = types;
15
13
 
16
- module.exports.report = () => `Use 'for...of' instead of 'for'`;
14
+ export const report = () => `Use 'for...of' instead of 'for'`;
17
15
 
18
16
  const forLoopToN = 'for (let __i = 0; __i < __n; __i++) __c';
19
17
  const getForOfLoop = template(`for (const [INDEX, LEFT] of RIGHT.entries()) BODY`);
20
18
  const assignIterable = (__i) => `const __a = __b[${__i.name}]`;
21
19
  const assignN = (__n) => `const ${__n.name} = __e.length`;
22
20
 
23
- module.exports.filter = (path) => {
21
+ export const filter = (path) => {
24
22
  const {node} = path;
25
23
  const prevPath = path.getPrevSibling();
26
24
 
@@ -57,7 +55,7 @@ module.exports.filter = (path) => {
57
55
  return references > 3;
58
56
  };
59
57
 
60
- module.exports.replace = () => ({
58
+ export const replace = () => ({
61
59
  [forLoopToN]: ({__c, __i}) => {
62
60
  const [node] = __c.body;
63
61
  const {__a, __b} = getTemplateValues(node, assignIterable(__i));
@@ -1,15 +1,13 @@
1
- 'use strict';
2
-
3
- const {generate, operator} = require('putout');
1
+ import {generate, operator} from 'putout';
4
2
 
5
3
  const {
6
4
  contains,
7
5
  getTemplateValues,
8
6
  } = operator;
9
7
 
10
- module.exports.report = () => `for-of should be used instead of for-in`;
8
+ export const report = () => `for-of should be used instead of for-in`;
11
9
 
12
- module.exports.match = () => ({
10
+ export const match = () => ({
13
11
  'for (__a in __b) __body': ({__a, __b, __body}) => {
14
12
  const declaration = getTemplateValues(__a, 'var __a');
15
13
 
@@ -22,7 +20,7 @@ module.exports.match = () => ({
22
20
  },
23
21
  });
24
22
 
25
- module.exports.replace = () => ({
23
+ export const replace = () => ({
26
24
  'for (__a in __b) __body': ({__b, __body}) => {
27
25
  const [first] = __body.body;
28
26
  const condition = getTemplateValues(first, 'if (!__b.hasOwnProperty(__a)) __c');
@@ -1,15 +1,13 @@
1
- 'use strict';
2
-
3
- const {generate, operator} = require('putout');
1
+ import {generate, operator} from 'putout';
4
2
 
5
3
  const {
6
4
  contains,
7
5
  getTemplateValues,
8
6
  } = operator;
9
7
 
10
- module.exports.report = () => `for-of should be used instead of for-in`;
8
+ export const report = () => `for-of should be used instead of for-in`;
11
9
 
12
- module.exports.match = () => ({
10
+ export const match = () => ({
13
11
  'for (__a in __b) __body': ({__a, __b, __body}) => {
14
12
  const declaration = getTemplateValues(__a, 'var __a');
15
13
 
@@ -22,7 +20,7 @@ module.exports.match = () => ({
22
20
  },
23
21
  });
24
22
 
25
- module.exports.replace = () => ({
23
+ export const replace = () => ({
26
24
  'for (__a in __b) __body': ({__b, __body}) => {
27
25
  const [first] = __body.body;
28
26
  const condition = getTemplateValues(first, 'if (__b.hasOwnProperty(__a)) __body');
@@ -1,10 +1,8 @@
1
- 'use strict';
2
-
3
- const {
1
+ import {
4
2
  operator,
5
3
  types,
6
4
  template,
7
- } = require('putout');
5
+ } from 'putout';
8
6
 
9
7
  const {
10
8
  compare,
@@ -13,14 +11,14 @@ const {
13
11
 
14
12
  const {isBlockStatement} = types;
15
13
 
16
- module.exports.report = () => `Use 'for...of' instead of 'for'`;
14
+ export const report = () => `Use 'for...of' instead of 'for'`;
17
15
 
18
16
  const forLoop = 'for (let __i = 0; __i < __e.length; __i++) __c';
19
17
  const getForOfLoop = template(`for (const LEFT of RIGHT) BODY`);
20
18
  const assignIterable = (__i) => `const __a = __b[${__i.name}]`;
21
19
  const assignIterableWithName = (__i, __e) => `const __a = ${__e.name}[${__i.name}]`;
22
20
 
23
- module.exports.filter = (path) => {
21
+ export const filter = (path) => {
24
22
  const {node} = path;
25
23
  const {body} = node;
26
24
 
@@ -37,7 +35,7 @@ module.exports.filter = (path) => {
37
35
  return compare(first, assignIterableWithName(__i, __e));
38
36
  };
39
37
 
40
- module.exports.replace = () => ({
38
+ export const replace = () => ({
41
39
  [forLoop]: ({__c, __i}) => {
42
40
  const [node] = __c.body;
43
41
  const {__a, __b} = getTemplateValues(node, assignIterable(__i));
@@ -1,10 +1,8 @@
1
- 'use strict';
2
-
3
- const {
1
+ import {
4
2
  operator,
5
3
  template,
6
4
  types,
7
- } = require('putout');
5
+ } from 'putout';
8
6
 
9
7
  const {
10
8
  compare,
@@ -13,14 +11,14 @@ const {
13
11
 
14
12
  const {isBlockStatement} = types;
15
13
 
16
- module.exports.report = () => `Use 'for...of' instead of 'for'`;
14
+ export const report = () => `Use 'for...of' instead of 'for'`;
17
15
 
18
16
  const forLoopToN = 'for (let __i = 0; __i < __n; __i++) __c';
19
17
  const getForOfLoop = template(`for (const LEFT of RIGHT) BODY`);
20
18
  const assignIterable = (__i) => `const __a = __b[${__i.name}]`;
21
19
  const assignN = (__n) => `const ${__n.name} = __e.length`;
22
20
 
23
- module.exports.filter = (path) => {
21
+ export const filter = (path) => {
24
22
  const {node} = path;
25
23
  const prevPath = path.getPrevSibling();
26
24
 
@@ -46,7 +44,7 @@ module.exports.filter = (path) => {
46
44
  return references <= 3;
47
45
  };
48
46
 
49
- module.exports.replace = () => ({
47
+ export const replace = () => ({
50
48
  [forLoopToN]: ({__c, __i}) => {
51
49
  const [node] = __c.body;
52
50
  const {__a, __b} = getTemplateValues(node, assignIterable(__i));
package/lib/index.js CHANGED
@@ -1,22 +1,20 @@
1
- 'use strict';
1
+ import * as map from './map/index.js';
2
+ import * as forEach from './for-each/index.js';
3
+ import * as forN from './for-n/index.js';
4
+ import * as forLength from './for-length/index.js';
5
+ import * as forEntries from './for-entries/index.js';
6
+ import * as forEntriesN from './for-entries-n/index.js';
7
+ import * as forInNegative from './for-in-negative/index.js';
8
+ import * as forInPositive from './for-in-positive/index.js';
9
+ import * as reduce from './reduce/index.js';
10
+ import * as removeUseless from './remove-useless/index.js';
11
+ import * as removeUselessVariables from './remove-useless-variables/index.js';
12
+ import * as removeUselessArrayFrom from './remove-useless-array-from/index.js';
13
+ import * as removeUnusedVariables from './remove-unused-variables/index.js';
14
+ import * as addMissingDeclaration from './add-missing-declaration/index.js';
15
+ import * as toForN from './to-for-n/index.js';
2
16
 
3
- const map = require('./map');
4
- const forEach = require('./for-each');
5
- const forN = require('./for-n');
6
- const forLength = require('./for-length');
7
- const forEntries = require('./for-entries');
8
- const forEntriesN = require('./for-entries-n');
9
- const forInNegative = require('./for-in-negative');
10
- const forInPositive = require('./for-in-positive');
11
- const reduce = require('./reduce');
12
- const removeUseless = require('./remove-useless');
13
- const removeUselessVariables = require('./remove-useless-variables');
14
- const removeUselessArrayFrom = require('./remove-useless-array-from');
15
- const removeUnusedVariables = require('./remove-unused-variables');
16
- const addMissingDeclaration = require('./add-missing-declaration');
17
- const toForN = require('./to-for-n');
18
-
19
- module.exports.rules = {
17
+ export const rules = {
20
18
  map,
21
19
  'for-each': forEach,
22
20
  'for-n': forN,
package/lib/map/index.js CHANGED
@@ -1,6 +1,5 @@
1
- 'use strict';
1
+ import {types} from 'putout';
2
2
 
3
- const {types} = require('putout');
4
3
  const {
5
4
  isReturnStatement,
6
5
  isBlockStatement,
@@ -9,9 +8,9 @@ const {
9
8
 
10
9
  const tail = (body) => body.at(-1);
11
10
 
12
- module.exports.report = () => `Use 'for...of' instead of map when 'return' absent`;
11
+ export const report = () => `Use 'for...of' instead of map when 'return' absent`;
13
12
 
14
- module.exports.match = () => ({
13
+ export const match = () => ({
15
14
  '__a.map((__b) => __c)': ({__c}, path) => {
16
15
  if (!path.parentPath.isExpressionStatement())
17
16
  return false;
@@ -28,7 +27,7 @@ module.exports.match = () => ({
28
27
  },
29
28
  });
30
29
 
31
- module.exports.replace = () => ({
30
+ export const replace = () => ({
32
31
  '__a.map((__b) => __c)': (vars, path) => {
33
32
  const a = path.get('callee.object');
34
33
  const b = path.get('arguments.0.params.0');
@@ -1,15 +1,14 @@
1
- 'use strict';
1
+ import {types} from 'putout';
2
2
 
3
- const {types} = require('putout');
4
3
  const {
5
4
  isBlockStatement,
6
5
  isArrayExpression,
7
6
  isCallExpression,
8
7
  } = types;
9
8
 
10
- module.exports.report = () => `Use 'for...of' instead of '.reduce()'`;
9
+ export const report = () => `Use 'for...of' instead of '.reduce()'`;
11
10
 
12
- module.exports.match = () => ({
11
+ export const match = () => ({
13
12
  'const __a = __b.reduce((__c, __d) => __e)': ({__b, __e}) => {
14
13
  if (isArrayExpression(__b))
15
14
  return false;
@@ -28,7 +27,7 @@ module.exports.match = () => ({
28
27
  'const __a = __b.reduce((__c, __d) => __e, __f)': ({__e}) => !isBlockStatement(__e),
29
28
  });
30
29
 
31
- module.exports.replace = () => ({
30
+ export const replace = () => ({
32
31
  'const __a = __b.reduce((__c, __d) => __e)': ({__a, __d}, path) => {
33
32
  path.__putout_for_of_reduce = true;
34
33
  rename(path, __d, __a);
@@ -1,6 +1,4 @@
1
- 'use strict';
2
-
3
- const {types, operator} = require('putout');
1
+ import {types, operator} from 'putout';
4
2
 
5
3
  const {
6
4
  isIdentifier,
@@ -10,19 +8,19 @@ const {
10
8
  const {remove} = operator;
11
9
  const getValue = ({value}) => value;
12
10
 
13
- module.exports.report = (path) => {
11
+ export const report = (path) => {
14
12
  const variables = getVariables(path);
15
13
  const names = getNames(variables);
16
14
 
17
15
  return `'${names.join(', ')}' inside 'for...of' loop defined but never used`;
18
16
  };
19
17
 
20
- module.exports.match = () => ({
18
+ export const match = () => ({
21
19
  'for (const __array of __b) __c': matchForOf,
22
20
  'for (const __object of __b) __c': matchForOf,
23
21
  });
24
22
 
25
- module.exports.replace = () => ({
23
+ export const replace = () => ({
26
24
  'for (const __array of __b) __c': replaceForOf,
27
25
  'for (const __object of __b) __c': replaceForOf,
28
26
  });
@@ -1,13 +1,11 @@
1
- 'use strict';
2
-
3
- const {types, operator} = require('putout');
1
+ import {types, operator} from 'putout';
4
2
 
5
3
  const {isIdentifier} = types;
6
4
  const {replaceWith} = operator;
7
5
 
8
- module.exports.report = () => `Avoid useless for-of`;
6
+ export const report = () => `Avoid useless for-of`;
9
7
 
10
- module.exports.match = () => ({
8
+ export const match = () => ({
11
9
  'for (const __identifier__a of __identifier__b) __c': ({__identifier__a}, path) => {
12
10
  const {name} = __identifier__a;
13
11
  const {references} = path.scope.bindings[name];
@@ -28,7 +26,7 @@ module.exports.match = () => ({
28
26
  },
29
27
  });
30
28
 
31
- module.exports.replace = () => ({
29
+ export const replace = () => ({
32
30
  'for (const __identifier__a of __identifier__b) __c': '__c',
33
31
  'for (const __a of __array) __c': ({__a, __c, __array}, path) => {
34
32
  const {elements} = __array;
@@ -1,7 +1,5 @@
1
- 'use strict';
1
+ export const report = () => `'Array.from()' has no sense inside 'for...of'`;
2
2
 
3
- module.exports.report = () => `'Array.from()' has no sense inside 'for...of'`;
4
-
5
- module.exports.replace = () => ({
3
+ export const replace = () => ({
6
4
  'for (const __a of Array.from(__b)) __c': 'for (const __a of __b) __c',
7
5
  });
@@ -1,6 +1,4 @@
1
- 'use strict';
2
-
3
- const {types, operator} = require('putout');
1
+ import {types, operator} from 'putout';
4
2
 
5
3
  const {replaceWith, remove} = operator;
6
4
  const {
@@ -11,14 +9,14 @@ const {
11
9
  const isToManyProperties = (a, {maxProperties}) => a.isObjectPattern() && a.node.properties.length > maxProperties;
12
10
  const isAssignment = (a) => isAssignmentPattern(a.value);
13
11
 
14
- module.exports.report = () => `Use destructuring in head of 'for...of'`;
12
+ export const report = () => `Use destructuring in head of 'for...of'`;
15
13
 
16
- module.exports.fix = ({path, varPath}) => {
14
+ export const fix = ({path, varPath}) => {
17
15
  replaceWith(varPath, path.node.id);
18
16
  remove(path);
19
17
  };
20
18
 
21
- module.exports.traverse = ({push, options}) => ({
19
+ export const traverse = ({push, options}) => ({
22
20
  'for (const __ of __) __'(path) {
23
21
  const leftPath = path.get('left');
24
22
  const varPath = leftPath.get('declarations.0.id');
@@ -1,15 +1,13 @@
1
- 'use strict';
1
+ export const report = () => `Use 'for' instead of 'for...of' with '.entries()' when change index`;
2
2
 
3
- module.exports.report = () => `Use 'for' instead of 'for...of' with '.entries()' when change index`;
4
-
5
- module.exports.match = () => ({
3
+ export const match = () => ({
6
4
  'for (let [__a, __b] of __c.entries())__body': ({__a}, path) => {
7
5
  const binding = path.scope.bindings[__a.name];
8
6
  return binding.constantViolations.length;
9
7
  },
10
8
  });
11
9
 
12
- module.exports.replace = () => ({
10
+ export const replace = () => ({
13
11
  'for (let [__a, __b] of __c.entries())__body': (vars, path) => {
14
12
  const {n} = path.parentPath.scope.bindings;
15
13
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@putout/plugin-for-of",
3
- "version": "8.1.0",
4
- "type": "commonjs",
3
+ "version": "9.0.0",
4
+ "type": "module",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
6
  "description": "🐊Putout plugin adds ability to apply for...of",
7
7
  "homepage": "https://github.com/coderaiser/putout/tree/master/packages/plugin-for-of#readme",
@@ -31,7 +31,7 @@
31
31
  "for-of"
32
32
  ],
33
33
  "devDependencies": {
34
- "@putout/eslint-flat": "^2.0.0",
34
+ "@putout/eslint-flat": "^3.0.0",
35
35
  "@putout/plugin-conditions": "*",
36
36
  "@putout/plugin-convert-const-to-let": "*",
37
37
  "@putout/plugin-remove-console": "*",
@@ -45,17 +45,17 @@
45
45
  "c8": "^10.0.0",
46
46
  "eslint": "^9.0.0",
47
47
  "eslint-plugin-n": "^17.0.0",
48
- "eslint-plugin-putout": "^25.0.1",
49
- "madrun": "^10.0.0",
48
+ "eslint-plugin-putout": "^26.0.0",
49
+ "madrun": "^11.0.0",
50
50
  "montag": "^1.2.1",
51
51
  "nodemon": "^3.0.1"
52
52
  },
53
53
  "peerDependencies": {
54
- "putout": ">=38"
54
+ "putout": ">=39"
55
55
  },
56
56
  "license": "MIT",
57
57
  "engines": {
58
- "node": ">=18"
58
+ "node": ">=20"
59
59
  },
60
60
  "publishConfig": {
61
61
  "access": "public"