Skip to content

Commit bfb25a8

Browse files
committed
feature: @putout/plugin-for-of: convert to ESM
1 parent d466671 commit bfb25a8

File tree

33 files changed

+145
-207
lines changed

33 files changed

+145
-207
lines changed

packages/plugin-for-of/lib/add-missing-declaration/index.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
'use strict';
1+
import {types} from 'putout';
22

3-
const {types} = require('putout');
43
const {
54
isVariableDeclaration,
65
isIdentifier,
76
isArrayPattern,
87
isObjectPattern,
98
} = types;
109

11-
module.exports.report = () => `Add missing declaration`;
10+
export const report = () => `Add missing declaration`;
1211

13-
module.exports.match = () => ({
12+
export const match = () => ({
1413
'for (__a of __b) __c': ({__a}, path) => {
1514
if (isVariableDeclaration(__a))
1615
return false;
@@ -42,6 +41,6 @@ module.exports.match = () => ({
4241
},
4342
});
4443

45-
module.exports.replace = () => ({
44+
export const replace = () => ({
4645
'for (__a of __b) __c': 'for (const __a of __b) __c',
4746
});

packages/plugin-for-of/lib/add-missing-declaration/index.spec.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
'use strict';
1+
import {createTest} from '@putout/test';
2+
import * as plugin from './index.js';
23

3-
const {createTest} = require('@putout/test');
4-
const plugin = require('.');
5-
6-
const test = createTest(__dirname, {
4+
const test = createTest(import.meta.url, {
75
plugins: [
86
['add-missing-declaration', plugin],
97
],

packages/plugin-for-of/lib/for-each/index.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
'use strict';
2-
3-
const {
1+
import {
42
template,
53
operator,
64
types,
7-
} = require('putout');
5+
} from 'putout';
86

97
const {
108
isIdentifier,
@@ -30,9 +28,9 @@ const forOfEntriesTemplate = template(`
3028
const {keys} = Object;
3129
const isRoot = (path) => path.isFunction() || path.isProgram();
3230

33-
module.exports.report = () => `Use 'for-of' instead of 'forEach()'`;
31+
export const report = () => `Use 'for-of' instead of 'forEach()'`;
3432

35-
module.exports.replace = () => ({
33+
export const replace = () => ({
3634
'__.forEach.call(__a, (__b) => __body)': 'for (const __b of __a) __body',
3735
'__.forEach(__args)': (vars, path) => {
3836
const {params, body} = path.node.arguments[0];
@@ -76,7 +74,7 @@ module.exports.replace = () => ({
7674
},
7775
});
7876

79-
module.exports.match = () => ({
77+
export const match = () => ({
8078
'__.forEach(__args)': (vars, path) => {
8179
const {parentPath} = path;
8280

packages/plugin-for-of/lib/for-each/index.spec.js

+9-10
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
'use strict';
2-
3-
const {createTest} = require('@putout/test');
4-
5-
const convertConstToLet = require('@putout/plugin-convert-const-to-let');
6-
const removeUselessContinue = require('@putout/plugin-remove-useless-continue');
7-
const plugin = require('.');
8-
9-
const removeUselessVariables = require('../remove-useless-variables');
1+
import {createRequire} from 'node:module';
2+
import {createTest} from '@putout/test';
3+
import * as convertConstToLet from '@putout/plugin-convert-const-to-let';
4+
import * as removeUselessContinue from '@putout/plugin-remove-useless-continue';
5+
import * as plugin from './index.js';
6+
import * as removeUselessVariables from '../remove-useless-variables/index.js';
7+
8+
const require = createRequire(import.meta.url);
109
const convertComparisonToBoolean = require('@putout/plugin-conditions').rules['convert-comparison-to-boolean'];
1110

12-
const test = createTest(__dirname, {
11+
const test = createTest(import.meta.url, {
1312
plugins: [
1413
['for-of/for-each', plugin],
1514
],

packages/plugin-for-of/lib/for-entries-n/index.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
'use strict';
2-
3-
const {
1+
import {
42
operator,
53
template,
64
types,
7-
} = require('putout');
5+
} from 'putout';
86

97
const {
108
compare,
@@ -13,14 +11,14 @@ const {
1311

1412
const {isBlockStatement} = types;
1513

16-
module.exports.report = () => `Use 'for...of' instead of 'for'`;
14+
export const report = () => `Use 'for...of' instead of 'for'`;
1715

1816
const forLoopToN = 'for (let __i = 0; __i < __n; __i++) __c';
1917
const getForOfLoop = template(`for (const [INDEX, LEFT] of RIGHT.entries()) BODY`);
2018
const assignIterable = (__i) => `const __a = __b[${__i.name}]`;
2119
const assignN = (__n) => `const ${__n.name} = __e.length`;
2220

23-
module.exports.filter = (path) => {
21+
export const filter = (path) => {
2422
const {node} = path;
2523
const prevPath = path.getPrevSibling();
2624

@@ -57,7 +55,7 @@ module.exports.filter = (path) => {
5755
return references > 3;
5856
};
5957

60-
module.exports.replace = () => ({
58+
export const replace = () => ({
6159
[forLoopToN]: ({__c, __i}) => {
6260
const [node] = __c.body;
6361
const {__a, __b} = getTemplateValues(node, assignIterable(__i));

packages/plugin-for-of/lib/for-entries-n/index.spec.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
'use strict';
1+
import montag from 'montag';
2+
import {createTest} from '@putout/test';
3+
import * as entriesN from './index.js';
24

3-
const montag = require('montag');
4-
const {createTest} = require('@putout/test');
5-
const entriesN = require('./index.js');
6-
7-
const test = createTest(__dirname, {
5+
const test = createTest(import.meta.url, {
86
plugins: [
97
['convert-for-to-for-of/entries-n', entriesN],
108
],

packages/plugin-for-of/lib/for-entries/index.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
'use strict';
2-
3-
const {
1+
import {
42
operator,
53
types,
64
template,
7-
} = require('putout');
5+
} from 'putout';
86

97
const {
108
compare,
@@ -13,14 +11,14 @@ const {
1311

1412
const {isBlockStatement} = types;
1513

16-
module.exports.report = () => `Use 'for...of' instead of 'for'`;
14+
export const report = () => `Use 'for...of' instead of 'for'`;
1715

1816
const forLoop = 'for (let __i = 0; __i < __e.length; __i++) __c';
1917
const getForOfLoop = template(`for (const [INDEX, ITEM] of ITEMS.entries()) BODY`);
2018
const assignIterable = (__i) => `const __a = __b[${__i.name}]`;
2119
const assignIterableWithName = (__i, __e) => `const __a = ${__e.name}[${__i.name}]`;
2220

23-
module.exports.filter = (path) => {
21+
export const filter = (path) => {
2422
const {node} = path;
2523
const {body} = node;
2624

@@ -43,7 +41,7 @@ module.exports.filter = (path) => {
4341
return compare(first, assignIterableWithName(__i, __e));
4442
};
4543

46-
module.exports.replace = () => ({
44+
export const replace = () => ({
4745
[forLoop]: ({__c, __i}) => {
4846
const [node] = __c.body;
4947
const {__a, __b} = getTemplateValues(node, assignIterable(__i));

packages/plugin-for-of/lib/for-entries/index.spec.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
'use strict';
1+
import removeUnusedVariables from '@putout/plugin-remove-unused-variables';
2+
import {createTest} from '@putout/test';
3+
import * as forEntries from './index.js';
24

3-
const removeUnusedVariables = require('@putout/plugin-remove-unused-variables');
4-
const {createTest} = require('@putout/test');
5-
const forEntries = require('.');
6-
7-
const test = createTest(__dirname, {
5+
const test = createTest(import.meta.url, {
86
plugins: [
97
['for-of/for-entries', forEntries],
108
],

packages/plugin-for-of/lib/for-in-negative/index.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
'use strict';
2-
3-
const {generate, operator} = require('putout');
1+
import {generate, operator} from 'putout';
42

53
const {
64
contains,
75
getTemplateValues,
86
} = operator;
97

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`;
119

12-
module.exports.match = () => ({
10+
export const match = () => ({
1311
'for (__a in __b) __body': ({__a, __b, __body}) => {
1412
const declaration = getTemplateValues(__a, 'var __a');
1513

@@ -22,7 +20,7 @@ module.exports.match = () => ({
2220
},
2321
});
2422

25-
module.exports.replace = () => ({
23+
export const replace = () => ({
2624
'for (__a in __b) __body': ({__b, __body}) => {
2725
const [first] = __body.body;
2826
const condition = getTemplateValues(first, 'if (!__b.hasOwnProperty(__a)) __c');

packages/plugin-for-of/lib/for-in-negative/index.spec.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
'use strict';
1+
import {createTest} from '@putout/test';
2+
import * as negative from './index.js';
23

3-
const {createTest} = require('@putout/test');
4-
const negative = require('.');
5-
6-
const test = createTest(__dirname, {
4+
const test = createTest(import.meta.url, {
75
plugins: [
86
['convert-for-in-to-for-of/negative', negative],
97
],

packages/plugin-for-of/lib/for-in-positive/index.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
'use strict';
2-
3-
const {generate, operator} = require('putout');
1+
import {generate, operator} from 'putout';
42

53
const {
64
contains,
75
getTemplateValues,
86
} = operator;
97

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`;
119

12-
module.exports.match = () => ({
10+
export const match = () => ({
1311
'for (__a in __b) __body': ({__a, __b, __body}) => {
1412
const declaration = getTemplateValues(__a, 'var __a');
1513

@@ -22,7 +20,7 @@ module.exports.match = () => ({
2220
},
2321
});
2422

25-
module.exports.replace = () => ({
23+
export const replace = () => ({
2624
'for (__a in __b) __body': ({__b, __body}) => {
2725
const [first] = __body.body;
2826
const condition = getTemplateValues(first, 'if (__b.hasOwnProperty(__a)) __body');

packages/plugin-for-of/lib/for-in-positive/index.spec.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
'use strict';
1+
import {createTest} from '@putout/test';
2+
import * as convertForInToForOf from './index.js';
23

3-
const {createTest} = require('@putout/test');
4-
const convertForInToForOf = require('.');
5-
6-
const test = createTest(__dirname, {
4+
const test = createTest(import.meta.url, {
75
plugins: [
86
['convert-for-in-to-for-of', convertForInToForOf],
97
],

packages/plugin-for-of/lib/for-length/index.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
'use strict';
2-
3-
const {
1+
import {
42
operator,
53
types,
64
template,
7-
} = require('putout');
5+
} from 'putout';
86

97
const {
108
compare,
@@ -13,14 +11,14 @@ const {
1311

1412
const {isBlockStatement} = types;
1513

16-
module.exports.report = () => `Use 'for...of' instead of 'for'`;
14+
export const report = () => `Use 'for...of' instead of 'for'`;
1715

1816
const forLoop = 'for (let __i = 0; __i < __e.length; __i++) __c';
1917
const getForOfLoop = template(`for (const LEFT of RIGHT) BODY`);
2018
const assignIterable = (__i) => `const __a = __b[${__i.name}]`;
2119
const assignIterableWithName = (__i, __e) => `const __a = ${__e.name}[${__i.name}]`;
2220

23-
module.exports.filter = (path) => {
21+
export const filter = (path) => {
2422
const {node} = path;
2523
const {body} = node;
2624

@@ -37,7 +35,7 @@ module.exports.filter = (path) => {
3735
return compare(first, assignIterableWithName(__i, __e));
3836
};
3937

40-
module.exports.replace = () => ({
38+
export const replace = () => ({
4139
[forLoop]: ({__c, __i}) => {
4240
const [node] = __c.body;
4341
const {__a, __b} = getTemplateValues(node, assignIterable(__i));

packages/plugin-for-of/lib/for-length/index.spec.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
'use strict';
1+
import removeUnusedVariables from '@putout/plugin-remove-unused-variables';
2+
import {createTest} from '@putout/test';
3+
import * as convertForToForOf from './index.js';
24

3-
const removeUnusedVariables = require('@putout/plugin-remove-unused-variables');
4-
const {createTest} = require('@putout/test');
5-
const convertForToForOf = require('./index.js');
6-
7-
const test = createTest(__dirname, {
5+
const test = createTest(import.meta.url, {
86
plugins: [
97
['for-of/length', convertForToForOf],
108
],

packages/plugin-for-of/lib/for-n/index.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
'use strict';
2-
3-
const {
1+
import {
42
operator,
53
template,
64
types,
7-
} = require('putout');
5+
} from 'putout';
86

97
const {
108
compare,
@@ -13,14 +11,14 @@ const {
1311

1412
const {isBlockStatement} = types;
1513

16-
module.exports.report = () => `Use 'for...of' instead of 'for'`;
14+
export const report = () => `Use 'for...of' instead of 'for'`;
1715

1816
const forLoopToN = 'for (let __i = 0; __i < __n; __i++) __c';
1917
const getForOfLoop = template(`for (const LEFT of RIGHT) BODY`);
2018
const assignIterable = (__i) => `const __a = __b[${__i.name}]`;
2119
const assignN = (__n) => `const ${__n.name} = __e.length`;
2220

23-
module.exports.filter = (path) => {
21+
export const filter = (path) => {
2422
const {node} = path;
2523
const prevPath = path.getPrevSibling();
2624

@@ -46,7 +44,7 @@ module.exports.filter = (path) => {
4644
return references <= 3;
4745
};
4846

49-
module.exports.replace = () => ({
47+
export const replace = () => ({
5048
[forLoopToN]: ({__c, __i}) => {
5149
const [node] = __c.body;
5250
const {__a, __b} = getTemplateValues(node, assignIterable(__i));

packages/plugin-for-of/lib/for-n/index.spec.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
'use strict';
1+
import {createRequire} from 'node:module';
2+
import {createTest} from '@putout/test';
3+
import * as forN from './index.js';
24

3-
const {createTest} = require('@putout/test');
4-
const forN = require('.');
5+
const require = createRequire(import.meta.url);
56

6-
const test = createTest(__dirname, {
7+
const test = createTest(import.meta.url, {
78
plugins: [
89
['for-of/n', forN],
910
],

0 commit comments

Comments
 (0)