Skip to content

Commit 7b16cf8

Browse files
committed
feature: @putout/plugin-operator-declare: nodejs/convert-esm-to-commonjs: improve support
1 parent d6b771e commit 7b16cf8

File tree

6 files changed

+78
-2
lines changed

6 files changed

+78
-2
lines changed

packages/operator-declare/lib/declare.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const {template} = require('@putout/engine-parser');
44
const {isESM, insertAfter} = require('@putout/operate');
5+
56
const {compare} = require('@putout/compare');
67

78
const {types} = require('@putout/babel');
@@ -143,8 +144,12 @@ function insert(node, bodyPath) {
143144
if (isVariableDeclaration(node) && isImportDeclaration(insertionPath) || isRequire(insertionPath))
144145
return insertAfter(insertionPath, node);
145146

146-
if (isVariableDeclaration(node))
147+
if (isVariableDeclaration(node)) {
148+
if (isRequire(first))
149+
return first.insertAfter(node);
150+
147151
return first.insertBefore(node);
152+
}
148153

149154
if (!insertionPath)
150155
return first.insertBefore(node);

packages/operator-declare/lib/declare.spec.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const test = require('supertape');
44
const putout = require('putout');
55
const montag = require('montag');
6+
const nodejs = require('@putout/plugin-nodejs');
67

78
const {declare} = require('./declare.js');
89

@@ -636,8 +637,8 @@ test('putout: operator: declare: require', (t) => {
636637
});
637638

638639
const expected = montag`
639-
const process = require('process');
640640
const fs = require('fs');
641+
const process = require('process');
641642
const a = 5;
642643
643644
async function hello() {
@@ -1047,3 +1048,34 @@ test('putout: operator: declare: under require', (t) => {
10471048
t.equal(code, expected);
10481049
t.end();
10491050
});
1051+
1052+
test('putout: operator: declare: insert before require', (t) => {
1053+
const declarations = {
1054+
operator: `import {operator} from 'putout'`,
1055+
findFile: `const {findFile} = operator`,
1056+
};
1057+
1058+
const source = montag`
1059+
import {operator} from 'putout';
1060+
const a = 5;
1061+
findFile();
1062+
`;
1063+
1064+
const {code} = putout(source, {
1065+
plugins: [
1066+
['convert-esm-to-commonjs', nodejs.rules['convert-esm-to-commonjs']],
1067+
['declare', declare(declarations)],
1068+
],
1069+
});
1070+
1071+
const expected = montag`
1072+
const {operator: operator} = require('putout');
1073+
const {findFile} = operator;
1074+
const a = 5;
1075+
1076+
findFile();\n
1077+
`;
1078+
1079+
t.equal(code, expected);
1080+
t.end();
1081+
});

packages/operator-declare/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
],
3838
"devDependencies": {
3939
"@putout/eslint-flat": "^2.0.0",
40+
"@putout/plugin-nodejs": "*",
4041
"@putout/test": "^12.0.0",
4142
"c8": "^10.0.0",
4243
"eslint": "^9.0.0",

packages/plugin-declare-before-reference/test/declare-before-reference.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,11 @@ test('plugin-nodejs: group-require-by-id: no report after transform: group-requi
123123
});
124124
t.end();
125125
});
126+
127+
test('plugin-declare-before-reference: transform: convert-traverse-to-scan', (t) => {
128+
t.transform('convert-traverse-to-scan', {
129+
'convert-esm-to-commonjs': nodejs.rules['convert-esm-to-commonjs'],
130+
'putout/declare': putout.rules.declare,
131+
});
132+
t.end();
133+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const {operator: operator} = require('putout');
2+
const {findFile} = operator;
3+
const {removeFile} = operator;
4+
const FS = '__putout_processor_filesystem(__object)';
5+
6+
module.exports.report = () => `Remove vim swap file`;
7+
8+
module.exports.fix = (filePath) => {
9+
removeFile(filePath);
10+
};
11+
12+
module.exports.traverse = ({push}) => ({
13+
[FS](path) {
14+
findFile(path, '*.swp').map(push);
15+
},
16+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const FS = '__putout_processor_filesystem(__object)';
2+
3+
export const report = () => `Remove vim swap file`;
4+
5+
export const fix = (filePath) => {
6+
removeFile(filePath);
7+
};
8+
9+
export const traverse = ({push}) => ({
10+
[FS](path) {
11+
findFile(path, '*.swp').map(push);
12+
}
13+
});
14+

0 commit comments

Comments
 (0)