Giter Site home page Giter Site logo

eslint-cjs-to-esm's Introduction

eslint-cjs-to-esm

ESLint wrapper for migration from CJS to ESM.

This tool's main use-case is to migrate from CommonJS (JavaScript/TypeScript) to ES Modules (JavaScript/TypeScript).

This tool is a wrapper of ESLint, and it built-in some rules for migration. So, you can just use this tool without any configuration.

Install

Install with npm:

npm install eslint-cjs-to-esm

Usage

Command Line Arguments are the same as those of ESLint.

npx eslint-cjs-to-esm [ESLint Arguments!]

Run Lint

npx eslint-cjs-to-esm "./src/**/*.{js,ts}"

Fix Errors

npx eslint-cjs-to-esm "./src/**/*.{js,ts}" --fix

Warning You need to start with . for relative path. It is wrapper limitation.
NG: npx eslint-cjs-to-esm "src/**/*.ts"
OK: npx eslint-cjs-to-esm "./src/**/*.ts"

DEBUG:

DEBUG=eslint-cjs-to-esm npx eslint-cjs-to-esm "./src/**/*.{js,ts}"

Rules

This rule set is based on ESLint rules for migrating projects from CommonJS to ESM.

eslint-plugin-file-extension-in-import-ts

eslint-plugin-node

ESLint Plugin Rule Source Description Fixable
node no-extraneous-import ๐Ÿ”— disallow import declarations which import extraneous modules -
node no-sync ๐Ÿ”— disallow synchronous methods -
node file-extension-in-import ๐Ÿ”— enforce the style of file extensions in import declarations Yes

eslint-plugin-import

ESLint Plugin Rule Source Description Fixable
import extensions ๐Ÿ”— Ensure consistent use of file extension within the import path. -
import no-unresolved ๐Ÿ”— Ensure imports point to a file/module that can be resolved. -
import no-useless-path-segments ๐Ÿ”— Prevent unnecessary path segments in import and require statements. Yes
import no-extraneous-dependencies ๐Ÿ”— Forbid the use of extraneous packages. -
import no-commonjs ๐Ÿ”— Report CommonJS require calls and module.exports or exports.*. -

๐Ÿ“ commonjs-to-es-module-codemod helps you to migrate from require/exports(CJS) to import/export(ESM).

eslint-plugin-unicorn

ESLint Plugin Rule Source Description Fixable
unicorn prefer-module ๐Ÿ”— Prefer JavaScript modules (ESM) over CommonJS. Yes
unicorn prefer-node-protocol ๐Ÿ”— Prefer using the node: protocol when importing Node.js builtin modules. Yes
unicorn prefer-top-level-await ๐Ÿ”— Prefer top-level await over top-level promises and async function calls. Suggest

Changelog

See Releases page.

Running tests

Install devDependencies and Run npm test:

npm test

Contributing

Pull requests and stars are always welcome.

For bugs and feature requests, please create an issue.

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

Author

License

MIT ยฉ azu

eslint-cjs-to-esm's People

Contributors

azu avatar renovate[bot] avatar starnayuta avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

eslint-cjs-to-esm's Issues

Not working on Windows 10

I got an error on my Windows 10 computer when I tried to run eslint.

PS C:\path\to\repository> npx eslint-cjs-to-esm "./src/**/*.{js,ts}"
C:\path\to\repository\node_modules\.bin\eslint:2
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
          ^^^^^^^

SyntaxError: missing ) after argument list
    at internalCompileFunction (node:internal/vm:77:18)
    at wrapSafe (node:internal/modules/cjs/loader:1288:20)
    at Module._compile (node:internal/modules/cjs/loader:1340:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
    at Module.load (node:internal/modules/cjs/loader:1207:32)
    at Module._load (node:internal/modules/cjs/loader:1023:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12)
    at node:internal/main/run_main_module:28:49

Node.js v20.11.0

An error is occurring in the execution of the eslint sh file.

https://github.com/azu/eslint-cjs-to-esm/blob/main/eslint-cjs-to-esm.js#L6

I am not familiar with how eslint or npm bin works, but it seems that on Windows bat files are used instead of sh files.

image

Similar Issues:

Specifying tsconfig in the project's .eslintrc.cjs raises parsing errors.

This library references the project's .eslintrc.cjs to run eslint.
Therefore, it may cause errors depending on the project's configuration.

Current behavior

The following command shows that this library runs eslint with reference to the project's .eslintrc.cjs.

npx eslint-cjs-to-esm --print-config  "./src/**/*.{js,ts}"

Some projects may specify tsconfig in parserOptions of .eslintrc.cjs.
https://typescript-eslint.io/linting/typed-linting/#specifying-tsconfigs

module.exports = {
    extends: [
        'eslint:recommended',
        'plugin:@typescript-eslint/recommended-type-checked',
    ],
    plugins: ['@typescript-eslint'],
    parser: '@typescript-eslint/parser',
    parserOptions: {
        project: './tsconfig.json',
    },
    root: true,
};

It causes errors.

PS C:\path\to\repository> npx eslint-cjs-to-esm "./src/index.ts"

C:\path\to\repository\src\index.ts
  0:0  error  Parsing error: Cannot read file 'c:\path\to\repository\node_modules\eslint-cjs-to-esm\tsconfig.json'

โœ– 1 problem (1 error, 0 warnings)

Proposal

The project's .eslintrc.js should be ignored or command line options should be noted in README.md.

npx eslint-cjs-to-esm --no-eslintrc --no-inline-config "./src/**/*.{js,ts}" 
  • --no-eslintrc: Disables use of configuration from .eslintrc.* and package.json files.
  • --no-inline-config: This option prevents inline comments like /*eslint-disable*/ or /*global foo*/ from having any effect.
    • This is to avoid errors caused by comments that disable unloaded rules.
    // eslint-disable-next-line non-existing-rule
    C:\path\to\repository\index.ts
    1:1  error  Definition for rule 'non-existing-rule' was not found  non-existing-rule

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/test.yml
  • actions/checkout v4
  • actions/setup-node v4
npm
package.json
  • @typescript-eslint/parser ^6.21.0
  • eslint-import-resolver-typescript ^3.6.1
  • eslint-plugin-file-extension-in-import-ts ^2.1.0
  • eslint-plugin-import ^2.29.1
  • eslint-plugin-node ^11.1.0
  • eslint-plugin-unicorn ^51.0.1
  • execa ^8.0.1
  • expected-exit-status ^3.1.0
  • npm 10.5.0

  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.