包详细信息

@putout/plugin-esm

coderaiser12.5kMIT4.5.0

🐊Putout plugin improves ability to transform ESM code

putout, putout-plugin, plugin, esm

自述文件

@putout/plugin-esm NPM version

The static import statement is used to import read only live bindings which are exported by another module. The imported bindings are called live bindings because they are updated by the module that exported the binding, but cannot be re-assigned by the importing module.

(c) MDN

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

Install

npm i putout @putout/plugin-esm -D

Rules

File rules

Config

{
    "rules": {
        "esm/add-index-to-import": "on",
        "esm/apply-export-from": "on",
        "esm/declare-imports-first": "on",
        "esm/group-imports-by-source": "on",
        "esm/merge-duplicate-imports": "on",
        "esm/merge-declaration-with-export": "on",
        "esm/remove-quotes-from-import-assertions": "on",
        "esm/remove-empty-export": "on",
        "esm/remove-empty-import": ["on", {
            "ignore": []
        }],
        "esm/sort-imports-by-specifiers": "on",
        "esm/resolve-imported-file": "off",
        "esm/apply-namespace-of-file": "off",
        "esm/inline-export": "off",
        "esm/remove-useless-export-specifiers": "off"
    }
}

Rules

add-index-to-import

ESM doesn't add index.js, so it can be left after @putout/plugin-convert-esm-to-commonjs. Checkout in 🐊Putout Editor.

❌ Example of incorrect code

import insertRust from './insert-rust.js';
import addAction from './add-action.js';

export const rules = {};

✅ Example of correct code

import insertRust from './insert-rust/index.js';
import addAction from './add-action/index.js';

export const rules = {};

apply-export-from

The export declaration is used to export values from a JavaScript module.

(c) MDN

Check out in 🐊Putout Editor.

❌ Example of incorrect code

import * as ns_1 from 'x';

export {
    ns_1 as ns,
};

✅ Example of correct code

export * as ns from 'x';

inline-export

Check out in 🐊Putout Editor.

❌ Example of incorrect code

const stack = [];

function sum(a, b) {
    i32.add(local.get(), local.get());
}

export {
    sum,
    stack,
};

✅ Example of correct code

export const stack = [];

export function sum(a, b) {
    i32.add(local.get(), local.get());
}

remove-useless-export-specifiers

Check out in 🐊Putout Editor.

❌ Example of incorrect code

export const hello = () => 'world';
export const {
-    hello,
}

declare-imports-first

Check out in 🐊Putout Editor. For CommonJS use nodejs/declare-after-require.

❌ Example of incorrect code

const [arg] = process.argv;
import esbuild from 'esbuild';

✅ Example of correct code

import esbuild from 'esbuild';

const [arg] = process.argv;

group-imports-by-source

Group order:

  • ✅ builtins;
  • ✅ external;
  • ✅ hashed;
  • ✅ internal;

Checkout in 🐊Putout Editor.

❌ Example of incorrect code

import fs from 'node:fs';
import {lodash} from 'lodash';
import react from 'react';
import d from '../hello.js';
import ss from '../../bb/ss.js';
import b from './ss.js';
import parse from '#parser';

const c = 5;

✅ Example of correct code

import fs from 'node:fs';
import react from 'react';
import {lodash} from 'lodash';
import parse from '#parser';
import b from './ss.js';
import d from '../hello.js';
import ss from '../../bb/ss.js';

const c = 5;

merge-declaration-with-export

Checkout in 🐊Putout Editor.

❌ Example of incorrect code

const {
    report,
    fix,
    scan,
} = createRemoveFiles(['*.swp', '*.swo']);

export {
    report,
    fix,
    scan,
};
✅ Example of correct code
export const {
    report,
    fix,
    scan,
} = createRemoveFiles(['*.swp', '*.swo']);

merge-duplicate-imports

join

To disable use:

{
    "rules": {
        "esm/merge-duplicate-imports-join": "off"
    }
}
❌ Example of incorrect code
import test from 'supertape';
import {stub} from 'supertape';
✅ Example of correct code
import test, {stub} from 'supertape';

rename

Checkout in 🐊Putout Editor.

To disable use:

{
    "rules": {
        "esm/merge-duplicate-imports-rename": "off"
    }
}
❌ Example of incorrect code
import putout from './putout.js';
import all from './putout.js';
import x from './putout.js';

console.log(all);
console.log(x);
✅ Example of correct code
import putout from './putout.js';

console.log(putout);
console.log(putout);

remove-empty-export

-export {};

remove-empty-import

-import 'abc';

remove-quotes-from-import-assertions

Checkout in 🐊Putout Editor.

❌ Example of incorrect code

import json from './mod.json' with { type: 'json' };

✅ Example of correct code

import json from './mod.json' with { type: 'json' };

sort-imports-by-specifiers

Checkout in 🐊Putout Editor.

❌ Example of incorrect code

import {
    a,
    b,
    c,
    d,
} from 'd';
import a1 from 'a1';

✅ Example of correct code

import a1 from 'a1';
import {
    a,
    b,
    c,
    d,
} from 'd';

convert-assert-to-with

This feature would ideally use the with keyword to denote attributes, but there are existing implementations based on a previous version of the proposal using the assert keyword. Due to potential web compatibility risks, the proposal still includes assert marked as deprecated. Usage of the old syntax is discouraged, and its removal is being investigated.

(c) tc39

Check out in 🐊Putout Editor.

❌ Example of incorrect code

import json from './foo.json' assert { type: 'json' };

import('foo.json', {
    assert: {
        type: 'json',
    },
});

✅ Example of correct code

import json from './foo.json' with { type: 'json' };

import('foo.json', {
    with: {
        type: 'json',
    },
});

File Rules

apply-namespace-import-to-file

The rule fixes:

SyntaxError: The requested module './a.js' does not provide an export named 'default'

Check out in 🐊Putout Editor:

Let's consider file structure:

/
|-- lib/
|  `-- index.js "import a from './a.js';"
|  `-- a.js "export const x = 2;"

In this case index.js can be fixed:

❌ Example of incorrect code

import a from './a.js';

✅ Example of correct code

import * as a from './a.js';

resolve-imported-file

Check out in 🐊Putout Editor:

Let's consider file structure:

/
|-- lib/
|  `-- index.js
|  `-- a.js

In this case index.js can be fixed:

❌ Example of incorrect code

import a from './a';

✅ Example of correct code

import a from './a.js';

License

MIT