Latest Threat ResearchGlassWorm Loader Hits Open VSX via Developer Account Compromise.Details
Socket
Book a DemoInstallSign in
Socket

@putout/plugin-nodejs

Package Overview
Dependencies
Maintainers
1
Versions
125
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@putout/plugin-nodejs

🐊Putout plugin adds ability to transform code to new API of Node.js

Source
npmnpm
Version
4.8.0
Version published
Weekly downloads
15K
21.09%
Maintainers
1
Weekly downloads
 
Created
Source

@putout/plugin-nodejs NPM version

Node.js is an open-source, cross-platform, JavaScript runtime environment.

(c) nodejs.org

🐊Putout plugin adds ability to transform to new Node.js API and apply best practices.

Install

npm i putout @putout/plugin-nodejs -D

Options

{
    "rules": {
        "nodejs/convert-buffer-to-buffer-alloc": "on",
        "nodejs/convert-fs-promises": "on",
        "nodejs/convert-promisify-to-fs-promises": "on",
        "nodejs/convert-dirname-to-url": "on",
        "nodejs/convert-url-to-dirname": "on",
        "nodejs/convert-top-level-return": "on",
        "nodejs/declare-after-require": "on",
        "nodejs/remove-process-exit": "on"
    }
}

Rules

convert-buffer-to-buffer-alloc

According to DEP0005. Check out in 🐊Putout Editor.

❌ Example of incorrect code

const n = 100;
const buf = [];

new Buffer(123);
new Buffer(n);
new Buffer('hello');

new Buffer([]);
new Buffer(buf);

✅ Example of correct code

const n = 100;
const buf = [];

Buffer.alloc(123);
Buffer.alloc(n);
Buffer.from('hello');

Buffer.from([]);
Buffer.from(buf);

convert-fs-promises

Convert fs.promises into form that will be simpler to use and convert to and from ESM.

❌ Example of incorrect code

const {readFile} = require('fs').promises;

✅ Example of correct code

const {readFile} = require('fs/promises');

convert-promisify-to-fs-promises

❌ Example of incorrect code

const fs = require('fs');
const readFile = promisify(fs.readFile);

✅ Example of correct code

const {readFile} = require('fs/promises');

convert-dirname-to-url

Only for ESM.

❌ Example of incorrect code

import {readFile} from 'fs/promises';

const file1 = join(__dirname, '../../package.json');
const file2 = path.join(__dirname, '../../package.json');

✅ Example of correct code

import {readFile} from 'fs/promises';

const file1 = new URL('../../package.json', import.meta.url);
const file2 = new URL('../../package.json', import.meta.url);

convert-url-to-dirname

Only for CommonJS.

❌ Example of incorrect code

const {readFile} = require('fs/promises');
const file = new URL('../../package.json', import.meta.url);

✅ Example of correct code

const {readFile} = require('fs/promises');
const file = join(__dirname, '../../package.json');

remove-process-exit

In most cases process.exit() is called from bin directory, if not - disable this rule using match.

-process.exit();

convert-top-level-return

❌ Example of incorrect code

return;

✅ Example of correct code

process.exit();

declare

Add declarations to built-in node.js modules:

❌ Example of incorrect code

await readFile('hello.txt', 'utf8');

✅ Example of correct code

import {readFile} from 'fs/promises';
await readFile('hello.txt', 'utf8');

declare-after-require

Node.js follows the CommonJS module system, and the builtin require function is the easiest way to include modules that exist in separate files. The basic functionality of require is that it reads a JavaScript file, executes the file, and then proceeds to return the exports object.

(c) Nodejs.org

Check out in 🐊Putout Editor.

❌ Example of incorrect code

const name = 'hello.txt';
const {readFile} = require('fs/promises');

✅ Example of correct code

const {readFile} = require('fs/promises');
const name = 'hello.txt';

License

MIT

Keywords

putout

FAQs

Package last updated on 31 Jul 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts