Skip to content

Commit 2a15e9f

Browse files
committed
feature: @putout/plugin-nodejs: declare: timers: add
1 parent 195a5d2 commit 2a15e9f

File tree

9 files changed

+68
-7
lines changed

9 files changed

+68
-7
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -748,11 +748,11 @@ test('putout: operator: declare: couple consts', (t) => {
748748

749749
test('putout: operator: declare: merge', (t) => {
750750
const declarations = {
751-
dirname: `import {dirname} from 'path'`,
751+
dirname: `import {dirname} from 'node:path'`,
752752
};
753753

754754
const source = montag`
755-
const {join} = require('path');
755+
const {join} = require('node:path');
756756
join(dirname('/package.json', 'node_modules'));
757757
`;
758758

@@ -773,7 +773,7 @@ test('putout: operator: declare: merge', (t) => {
773773
const expected = montag`
774774
'use strict';
775775
776-
const {join, dirname: dirname} = require('path');
776+
const {join, dirname: dirname} = require('node:path');
777777
join(dirname('/package.json', 'node_modules'));\n
778778
`;
779779

packages/plugin-nodejs/.nycrc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"**/fixture",
77
"test",
88
".*.*",
9+
"**/scripts",
910
"**/*.config.*"
1011
],
1112
"branches": 100,
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
const {Readable: Readable} = require('stream');
1+
const {Readable: Readable} = require('node:stream');
22
const stream = Readable.from('hello world');
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
'use strict';
22

3-
const {readFileSync: readFileSync} = require('fs');
3+
const {readFileSync: readFileSync} = require('node:fs');
44
readFileSync();

packages/plugin-nodejs/lib/declare/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import path from './modules/path.js';
1010
import fsPromises from './modules/fs-promises.js';
1111
import fs from './modules/fs.js';
1212
import events from './modules/events.js';
13+
import timers from './modules/timers.js';
1314

1415
export const declare = () => ({
1516
...events,
@@ -24,4 +25,5 @@ export const declare = () => ({
2425
...url,
2526
...util,
2627
...child_process,
28+
...timers,
2729
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export default {
2+
scheduler: `import {scheduler} from 'node:timers/promises'`,
3+
setImmediate: `import {setImmediate} from 'node:timers/promises'`,
4+
setInterval: `import {setInterval} from 'node:timers/promises'`,
5+
setTimeout: `import {setTimeout} from 'node:timers/promises'`,
6+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import {createTest} from '@putout/test';
2+
import montag from 'montag';
3+
import * as declare from './index.js';
4+
5+
const test = createTest(import.meta.url, {
6+
plugins: [
7+
['nodejs/declare', declare],
8+
],
9+
});
10+
11+
test('putout: plugin: nodejs: declare: timers: setTimeout', (t) => {
12+
t.transformCode('await setTimeout(1000)', montag`
13+
import {setTimeout} from 'node:timers/promises';
14+
15+
await setTimeout(1000);
16+
17+
`);
18+
t.end();
19+
});

packages/plugin-nodejs/lib/declare/util.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import {createTest} from 'node:@putout/test';
2-
import * as declare from 'node:./index.js';
1+
import {createTest} from '@putout/test';
2+
import * as declare from './index.js';
33

44
const test = createTest(import.meta.url, {
55
plugins: [
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import process from 'node:process';
2+
import {writeFile} from 'node:fs/promises';
3+
4+
const {keys} = Object;
5+
const [name, outputFile] = process.argv.slice(2);
6+
7+
if (!name) {
8+
process.stdout.write('create-declare [name] [output]\n');
9+
process.exit(1);
10+
}
11+
12+
const data = await import(`${name}/promises`);
13+
const result = {};
14+
15+
for (const key of keys(data)) {
16+
if (key === 'promises')
17+
continue;
18+
19+
if (key === 'default')
20+
continue;
21+
22+
result[key] = `import {${key}} from 'node:${name}/promises'`;
23+
}
24+
25+
const output = `export default ${JSON.stringify(result, null, 4) + '\n'}`;
26+
27+
if (outputFile) {
28+
await writeFile(outputFile, output);
29+
process.stdout.write(`✅ ${name}: ${outputFile}: updated seccessfully\n`);
30+
process.exit(0);
31+
}
32+
33+
process.stdout.write(output);

0 commit comments

Comments
 (0)