Skip to content

Commit 54b1f9f

Browse files
committed
feature: @putout/plugin-esm: migrate to ESM
1 parent 91efff3 commit 54b1f9f

File tree

27 files changed

+105
-149
lines changed

27 files changed

+105
-149
lines changed

packages/plugin-esm/lib/apply-export-from/index.js

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

53
const {
64
compare,
@@ -10,11 +8,11 @@ const {
108

119
const {exportNamespaceSpecifier} = types;
1210

13-
module.exports.report = () => `Use 'export *' instead of 'import/export'`;
11+
export const report = () => `Use 'export *' instead of 'import/export'`;
1412

1513
const IMPORT = 'import * as __a from "__b"';
1614

17-
module.exports.fix = ({path, current}) => {
15+
export const fix = ({path, current}) => {
1816
const {exported} = current.node.specifiers[0];
1917

2018
current.node.source = path.node.source;
@@ -29,7 +27,7 @@ module.exports.fix = ({path, current}) => {
2927
remove(path);
3028
};
3129

32-
module.exports.traverse = ({push}) => ({
30+
export const traverse = ({push}) => ({
3331
[IMPORT]: (path) => {
3432
const {__a} = getTemplateValues(path, IMPORT);
3533
const {name} = __a;

packages/plugin-esm/lib/apply-export-from/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
['apply-export-from', plugin],
97
],

packages/plugin-esm/lib/convert-assert-to-with/index.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
'use strict';
2-
31
const setImportType = (path, type) => {
42
path.node.options.properties[0].key.name = type;
53
};
64

7-
module.exports.report = () => `Use 'with' instead of 'assert'`;
5+
export const report = () => `Use 'with' instead of 'assert'`;
86

9-
module.exports.include = () => [
7+
export const include = () => [
108
'ImportDeclaration',
119
'import(__a, {assert: {type: "json"}})',
1210
];
1311

14-
module.exports.fix = (path) => {
12+
export const fix = (path) => {
1513
if (path.isImportDeclaration()) {
1614
delete path.node.extra.deprecatedAssertSyntax;
1715
return;
@@ -20,7 +18,7 @@ module.exports.fix = (path) => {
2018
setImportType(path, 'with');
2119
};
2220

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

2624
if (path.isImportDeclaration())

packages/plugin-esm/lib/convert-assert-to-with/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
['convert-assert-to-with', plugin],
97
],

packages/plugin-esm/lib/declare-imports-first/index.js

Lines changed: 4 additions & 5 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
isImportDeclaration,
65
isExportDeclaration,
@@ -9,9 +8,9 @@ const {
98
const {replaceWith} = operator;
109
const isESMNode = (a) => isImportDeclaration(a) || isExportDeclaration(a);
1110

12-
module.exports.report = () => `Declare imports first`;
11+
export const report = () => `Declare imports first`;
1312

14-
module.exports.fix = ({path, importPath}) => {
13+
export const fix = ({path, importPath}) => {
1514
let prev = path;
1615
let preventInfiniteLoop = 500;
1716

@@ -29,7 +28,7 @@ module.exports.fix = ({path, importPath}) => {
2928
}
3029
};
3130

32-
module.exports.traverse = ({push, pathStore}) => ({
31+
export const traverse = ({push, pathStore}) => ({
3332
ImportDeclaration: (path) => {
3433
pathStore(path);
3534
},

packages/plugin-esm/lib/declare-imports-first/index.spec.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1-
'use strict';
2-
3-
const {operator} = require('putout');
4-
const {createTest} = require('@putout/test');
5-
6-
const nodejs = require('@putout/plugin-nodejs');
7-
const plugin = require('.');
1+
import {operator} from 'putout';
2+
import {createTest} from '@putout/test';
3+
import * as nodejs from '@putout/plugin-nodejs';
4+
import * as plugin from './index.js';
85

96
const convertEsmToCommonjs = nodejs.rules['convert-esm-to-commonjs'];
107

118
const {remove} = operator;
129

13-
const test = createTest(__dirname, {
10+
const test = createTest(import.meta.url, {
1411
plugins: [
1512
['declare-imports-first', plugin],
1613
],

packages/plugin-esm/lib/group-imports-by-source/index.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
'use strict';
1+
import {isDeepStrictEqual} from 'node:util';
2+
import {types, operator} from 'putout';
23

3-
const {isDeepStrictEqual} = require('node:util');
4-
const {types, operator} = require('putout');
54
const {isImportDeclaration} = types;
65

76
const {
87
replaceWithMultiple,
98
remove,
109
} = operator;
1110

12-
module.exports.report = () => `Group imports by source: 'builtins', 'external', 'hashed', 'internal'`;
11+
export const report = () => `Group imports by source: 'builtins', 'external', 'hashed', 'internal'`;
1312

14-
module.exports.fix = ({grouped}) => {
13+
export const fix = ({grouped}) => {
1514
const [first, ...others] = grouped;
1615
const nodes = [first.node];
1716

@@ -24,7 +23,7 @@ module.exports.fix = ({grouped}) => {
2423
replaceWithMultiple(first, nodes);
2524
};
2625

27-
module.exports.traverse = ({pathStore, push}) => ({
26+
export const traverse = ({pathStore, push}) => ({
2827
ImportDeclaration: pathStore,
2928
Program: {
3029
exit(path) {

packages/plugin-esm/lib/group-imports-by-source/index.spec.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
'use strict';
1+
import {createTest} from '@putout/test';
2+
import * as nodejs from '@putout/plugin-nodejs';
3+
import * as plugin from './index.js';
4+
import * as sortImportsBySpecifiers from '../sort-imports-by-specifiers/index.js';
25

3-
const {createTest} = require('@putout/test');
4-
5-
const nodejs = require('@putout/plugin-nodejs');
6-
const plugin = require('..');
7-
8-
const sortImportsBySpecifiers = require('../sort-imports-by-specifiers');
96
const convertEsmToCommonJS = nodejs.rules['convert-esm-to-commonjs'];
107

11-
const test = createTest(__dirname, {
8+
const test = createTest(import.meta.url, {
129
plugins: [
1310
['group-imports-by-source', plugin],
1411
],

0 commit comments

Comments
 (0)