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

+4
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

+5-7
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

+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-node-prefix', plugin],
97
],
+13-6
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

+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
['cjs-file', plugin],
97
],

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

+4-5
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

+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 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

+5-7
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

+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 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

+5-6
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;

packages/plugin-nodejs/lib/convert-commonjs-to-esm-exports/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 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/exports', convert],
97
],

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

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
'use strict';
2-
3-
const {
1+
import {
42
types,
53
operator,
64
template,
7-
} = require('putout');
5+
} from 'putout';
6+
import justCamelCase from 'just-camel-case';
87

9-
const justCamelCase = require('just-camel-case');
108
const {
119
awaitExpression,
1210
identifier,
@@ -23,7 +21,7 @@ const {replaceWith, insertBefore} = operator;
2321

2422
const camelCase = (a) => justCamelCase(a.replace('@', ''));
2523

26-
module.exports.report = () => `Use 'ESM' instead of 'CommonJS'`;
24+
export const report = () => `Use 'ESM' instead of 'CommonJS'`;
2725

2826
const __B = 'declarations.0.init.arguments.0';
2927

@@ -38,7 +36,7 @@ const createImport = ({name, source}) => {
3836
const createFnDeclaration = template('const NAME1 = FN(NAME2)');
3937
const isPackage = ({value}) => /package(\.json)?$/.test(value);
4038

41-
module.exports.match = () => ({
39+
export const match = () => ({
4240
'const __a = require(__b)': ({__b}, path) => {
4341
// exclude jsons while not supported
4442
if (/\.json/.test(__b.value))
@@ -64,9 +62,9 @@ module.exports.match = () => ({
6462
'const __a = require("__b").__c(__args)': checkCall,
6563
});
6664

67-
module.exports.replace = () => ({
65+
export const replace = () => ({
6866
'const __a = require(".")': 'import __a from "./index.js"',
69-
'const __a = require(__b).default': 'import __a from "__b"',
67+
'const __a = require("__b").default': 'import __a from "__b"',
7068
'const __a = require(__b).__c': `{
7169
const {__c} = require(__b);
7270
const __a = __c;

packages/plugin-nodejs/lib/convert-commonjs-to-esm-require/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 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/require', convert],
97
],

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

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
'use strict';
1+
import * as convertCommonjsToEsmExports from './convert-commonjs-to-esm-exports/index.js';
2+
import * as convertCommonjsToEsmCommons from './convert-commonjs-to-esm-commons/index.js';
3+
import * as convertCommonjsToEsmRequire from './convert-commonjs-to-esm-require/index.js';
24

3-
const convertCommonjsToEsmExports = require('./convert-commonjs-to-esm-exports');
4-
const convertCommonjsToEsmCommons = require('./convert-commonjs-to-esm-commons');
5-
const convertCommonjsToEsmRequire = require('./convert-commonjs-to-esm-require');
6-
7-
module.exports.rules = {
5+
export const rules = {
86
'convert-commonjs-to-esm-exports': convertCommonjsToEsmExports,
97
'convert-commonjs-to-esm-common': convertCommonjsToEsmCommons,
108
'convert-commonjs-to-esm-require': convertCommonjsToEsmRequire,
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 {isESM} = operator;
54

6-
module.exports.report = () => `Use 'import.meta.url' instead of '__dirname'`;
5+
export const report = () => `Use 'import.meta.url' instead of '__dirname'`;
76

8-
module.exports.filter = isESM;
7+
export const filter = isESM;
98

10-
module.exports.replace = () => ({
9+
export const replace = () => ({
1110
'join(__dirname, __a)': 'new URL(__a, import.meta.url).pathname',
1211
'path.join(__dirname, __a)': 'new URL(__a, import.meta.url).pathname',
1312
});

packages/plugin-nodejs/lib/convert-dirname-to-url/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 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-dirname-to-url', convert],
97
],

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

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

3-
const {types} = require('putout');
43
const {isImportDefaultSpecifier} = types;
54

65
const BODY = '{__body}';
@@ -9,9 +8,9 @@ const GEN_FN = `function* __a(__args) ${BODY}`;
98
const ASYNC_FN = `async function __a(__args) ${BODY}`;
109
const CLASS = `class __a ${BODY}`;
1110

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

14-
module.exports.replace = () => ({
13+
export const replace = () => ({
1514
'export default __a': 'module.exports = __a',
1615
[`export ${CLASS}`]: `module.exports.__a = ${CLASS}`,
1716
[`export ${FN}`]: `module.epxorts.__a = ${FN}`,

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

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
'use strict';
2-
3-
const {createTest} = require('@putout/test');
4-
const putout = require('@putout/plugin-putout');
5-
const convert = require('.');
6-
1+
import {fileURLToPath} from 'node:url';
2+
import {dirname} from 'node:path';
3+
import {createRequire} from 'node:module';
4+
import {createTest} from '@putout/test';
5+
import * as putout from '@putout/plugin-putout';
6+
import * as convert from './index.js';
7+
8+
const __filename = fileURLToPath(import.meta.url);
9+
const __dirname = dirname(__filename);
10+
const require = createRequire(import.meta.url);
711
const nodejsDeclare = require('..').rules.declare;
812

9-
const test = createTest(__dirname, {
13+
const test = createTest(import.meta.url, {
1014
plugins: [
1115
['nodejs/convert-esm-to-commonjs', convert],
1216
],
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
'use strict';
1+
export const report = () => `Use 'module.exports' instead of 'exports'`;
22

3-
module.exports.report = () => `Use 'module.exports' instead of 'exports'`;
4-
5-
module.exports.replace = () => ({
3+
export const replace = () => ({
64
'exports.__a': 'module.exports.__a',
75
});

packages/plugin-nodejs/lib/convert-exports-to-module-exports/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
['convert-exports-to-module-exports', plugin],
97
],

0 commit comments

Comments
 (0)