Skip to content

Commit 5ffd7d4

Browse files
committed
feature: @putout/plugin-nodejs: convert to ESM
1 parent 45792ce commit 5ffd7d4

File tree

65 files changed

+284
-376
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+284
-376
lines changed

packages/plugin-nodejs/.putout.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
{
2+
"rules": {
3+
"nodejs/convert-commonjs-to-esm": "on",
4+
"nodejs/convert-esm-to-commonjs": "off"
5+
},
26
"match": {
37
".filesystem.json": {
48
"eslint/convert-rc-to-flat": "on"

packages/plugin-nodejs/lib/add-node-prefix/index.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
'use strict';
2-
3-
const {isBuiltin} = require('node:module');
4-
const {types, operator} = require('putout');
1+
import {isBuiltin} from 'node:module';
2+
import {types, operator} from 'putout';
53

64
const {
75
setLiteralValue,
@@ -12,11 +10,11 @@ const {isCallExpression} = types;
1210

1311
const REQUIRE = 'require("__a")';
1412

15-
module.exports.report = ({value}) => {
13+
export const report = ({value}) => {
1614
return `Use 'node:${value}' instead of '${value}'`;
1715
};
1816

19-
module.exports.fix = ({path, value}) => {
17+
export const fix = ({path, value}) => {
2018
if (isCallExpression(path)) {
2119
const arg = path.get('arguments.0');
2220
setLiteralValue(arg, `node:${value}`);
@@ -28,7 +26,7 @@ module.exports.fix = ({path, value}) => {
2826
setLiteralValue(source, `node:${value}`);
2927
};
3028

31-
module.exports.traverse = ({push}) => ({
29+
export const traverse = ({push}) => ({
3230
[REQUIRE](path) {
3331
const {__a} = getTemplateValues(path, REQUIRE);
3432
const {value} = __a;

packages/plugin-nodejs/lib/add-node-prefix/index.spec.js

Lines changed: 3 additions & 5 deletions
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-node-prefix', plugin],
97
],
Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1-
'use strict';
2-
3-
const {operator} = require('putout');
4-
const plugin = require('../convert-esm-to-commonjs');
1+
import {operator} from 'putout';
2+
import * as plugin from '../convert-esm-to-commonjs/index.js';
53

64
const {matchFiles} = operator;
7-
8-
module.exports = matchFiles({
5+
const {
6+
report,
7+
fix,
8+
scan,
9+
} = matchFiles({
910
'*.cjs': plugin,
1011
});
12+
13+
export {
14+
report,
15+
fix,
16+
scan,
17+
};

packages/plugin-nodejs/lib/cjs-file/index.spec.js

Lines changed: 3 additions & 5 deletions
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
['cjs-file', plugin],
97
],

packages/plugin-nodejs/lib/convert-buffer-to-buffer-alloc/index.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
'use strict';
1+
import {operator} from 'putout';
22

3-
const {operator} = require('putout');
43
const {compute} = operator;
54

65
const isNumber = (a) => typeof a === 'number';
76

8-
module.exports.report = () => `Use 'Buffer.alloc()' or 'Buffer.from()' instead of 'Buffer()' and 'new Buffer()'`;
7+
export const report = () => `Use 'Buffer.alloc()' or 'Buffer.from()' instead of 'Buffer()' and 'new Buffer()'`;
98

10-
module.exports.match = () => ({
9+
export const match = () => ({
1110
'new Buffer(__a)': (vars, path) => {
1211
const __aPath = path.get('arguments.0');
1312
const [is] = compute(__aPath);
@@ -16,7 +15,7 @@ module.exports.match = () => ({
1615
},
1716
});
1817

19-
module.exports.replace = () => ({
18+
export const replace = () => ({
2019
'new Buffer(__a)': transform,
2120
'new Buffer(__a, __b)': transform,
2221
'Buffer(__a)': transform,

packages/plugin-nodejs/lib/convert-buffer-to-buffer-alloc/index.spec.js

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

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

packages/plugin-nodejs/lib/convert-commonjs-to-esm-commons/index.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
'use strict';
2-
3-
const {template, operator} = require('putout');
1+
import {template, operator} from 'putout';
42

53
const {
64
getPathAfterImports,
@@ -13,23 +11,23 @@ const initCommons = template.ast(`
1311
const require = createRequire(import.meta.url);
1412
`);
1513

16-
module.exports.report = () => '"__filename", "__dirname" and "require" should be declared in ESM';
14+
export const report = () => '"__filename", "__dirname" and "require" should be declared in ESM';
1715

18-
module.exports.fix = ({scope}) => {
16+
export const fix = ({scope}) => {
1917
const programScope = scope.getProgramParent();
2018
const body = programScope.path.get('body');
2119
const afterImportPath = getPathAfterImports(body);
2220

2321
insertBefore(afterImportPath, initCommons);
2422
};
2523

26-
module.exports.include = () => [
24+
export const include = () => [
2725
'__filename',
2826
'__dirname',
2927
'require',
3028
];
3129

32-
module.exports.filter = ({scope}) => {
30+
export const filter = ({scope}) => {
3331
const isDirname = scope.hasBinding('__dirname');
3432
const isFilename = scope.hasBinding('__filename');
3533
const isRequire = scope.hasBinding('require');

packages/plugin-nodejs/lib/convert-commonjs-to-esm-commons/index.spec.js

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

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

packages/plugin-nodejs/lib/convert-commonjs-to-esm-exports/index.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
'use strict';
1+
import {types, operator} from 'putout';
22

3-
const {types, operator} = require('putout');
43
const {
54
exportSpecifier,
65
isIdentifier,
@@ -10,11 +9,11 @@ const {
109

1110
const {replaceWith} = operator;
1211

13-
module.exports.report = () => `Use 'ESM' instead of 'CommonJS'`;
12+
export const report = () => `Use 'ESM' instead of 'CommonJS'`;
1413

15-
module.exports.exclude = () => ['__, __'];
14+
export const exclude = () => ['__, __'];
1615

17-
module.exports.match = () => ({
16+
export const match = () => ({
1817
'module.exports.__a = __b': ({__a, __b}, path) => {
1918
const {name} = __a;
2019

@@ -36,7 +35,7 @@ module.exports.match = () => ({
3635
},
3736
});
3837

39-
module.exports.replace = () => ({
38+
export const replace = () => ({
4039
'module.exports = __a': 'export default __a',
4140
'module.exports.__a = __b': ({__a, __b}, path) => {
4241
const {name} = __a;

0 commit comments

Comments
 (0)