GithubHelp home page GithubHelp logo

antfu / eslint-ts-patch Goto Github PK

View Code? Open in Web Editor NEW
155.0 3.0 6.0 456 KB

Support loading eslint.config.mjs and eslint.config.ts as flat config files for ESLint.

License: MIT License

JavaScript 70.13% TypeScript 29.87%
eslint eslint-flat-config

eslint-ts-patch's Introduction

eslint-ts-patch

npm version npm downloads bundle JSDocs License

Support loading eslint.config.mjs or eslint.config.ts as flat config file for ESLint.

Configure files will be searched in the following order, the first one found will be used:

  • eslint.config.js
  • eslint.config.mjs
  • eslint.config.cjs
  • eslint.config.ts
  • eslint.config.mts
  • eslint.config.cts

For .js, .cjs, and .mjs files, they will be loaded by Node's native import().

For .ts, .cts, and .mts files, they will be loaded using TypeScript loaders.

Context:

Install

npm i -D eslint-ts-patch eslint@npm:eslint-ts-patch

It should make your eslint CLI work for those config files automatically. If it's still not, you can try switching the CLI to eslint-ts.

TypeScript Loaders

There are multiple solutions to load TypeScript files in Node.js at runtime, and each of them consists of different trade-offs. This patch supports the following loaders powered by importx:

  • default: Auto-detect the loader based on the user's environment.
  • tsx - Use Node's native ESM loader to load TypeScript files.
  • jiti- Transpile TypeScript files and ESM to CJS and execute them at runtime.
    • Pros: Easy to use. No need to install additional dependencies.
    • Cons: Everything is in CJS mode. It does not support top-level-await. It may have inconsistent behavior during ESM/CJS interop.
  • bundle-require - Use esbuild to bundle the eslint.config.ts file, and import the temporary bundle.
    • Pros: Not hacking into Node's internals. ESM and top-level-await are supported.
    • Cons: It writes a temporary file to disk.

Learn more about the loaders in the importx documentation.

To try out different loaders, you can set the ESLINT_TS_PATCH_LOADER environment variable to one of the following values:

ESLINT_TS_PATCH_LOADER=tsx npx eslint
ESLINT_TS_PATCH_LOADER=bundle-require npx eslint

Or you can use magic comments @eslint-ts-patch-loader in your eslint.config.ts file:

// @eslint-ts-patch-loader tsx

Compatibility

Tested with the following tools:

Package Managers

  • npm
  • pnpm
  • yarn

Integrations

  • eslint CLI ✅
  • VSCode ESLint extension ⚠️ (as it's executing your local node_modules/.bin/eslint)

As for VS Code v1.89 (April 2024) the bundled Node is v18.18.2, which is not compatible with tsx loader (requires v18.19.0+) - Which should be fixed with next month's VS Code release. In order for VS Code ESLint works now, you need to update your settings.json with the following config to use your global Node.js:

{
  "eslint.runtime": "node"
}

Versioning

This package proxies all ESLint exports, it should be compatible by aliasing the eslint package. The version of this package is the same as the latest supported ESLint version in addition to a patch number suffix indicating the patches of this package (e.g. 8.55.0-1). It's using ^ relaxed dependency of eslint, so it should work with any newer versions of ESLint.

How it works

As the support of eslint.config.js seems to be quite hard-coded in ESLint, this package proxies all exports of ESLint and installs this register beforehand. The register will swap some internal code of ESLint at runtime to make it work.

Disclaimer

It's only recommended to install this as top-level development dependency (user-aware). For plugin and library authors, it's ok to document the usage of this package for better DX. But we suggest avoiding having this as the dependency of your library or plugin, otherwise, take your own risk.

Troubleshooting

Is the Patch Working

This patch is designed to be as transparent as possible. If you want to verify if it's working, you can add DEBUG="eslint-ts-patch" environment variable to your command to see the debug logs.

➜ DEBUG="eslint-ts-patch" npx eslint -v

  eslint-ts-patch initialized +0ms
  eslint-ts-patch patched lib/eslint/flat-eslint.js +59ms

v8.55.0

Sponsors

License

MIT License © 2023-PRESENT Anthony Fu

eslint-ts-patch's People

Contributors

antfu avatar danielrentz avatar hyoban avatar karlhorky avatar privatenumber 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  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  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  avatar

eslint-ts-patch's Issues

v9.5.0 support

Clear and concise description of the problem

Since ESLint still hasn't introduced native TS config support even as of v9.5.0 this seems necessary.

Suggested solution

It would be helpful to update the deps in this repo (one last time!) to support 9.5.0.

Alternative

No response

Additional context

No response

Validations

Patch with types

Clear and concise description of the problem

If use eslint programmatical, there is no type hint.

image

Suggested solution

I have no good idea for the patched package.

Alternative

No response

Additional context

No response

Validations

Compatibility Issue Between eslint-ts-patch and ESLint v8.57.0

Describe the bug

When following the installation instructions step by step, the default installation resulted in the latest version of ESLint, v8.57.0, being installed. However, it failed to function properly alongside the eslint-ts-patch. Upon installing ESLint v8.56.0 instead, the patch started working as expected. I am unsure of the reason behind this discrepancy, but reverting to ESLint v8.56.0 did resolve the issue.

Could I have missed any crucial instructions or made any mistakes during the installation process? Any insights or assistance would be greatly appreciated. Thank you for your attention to this matter.

Reproduction

System Info

System:
  OS: Windows 11 10.0.22621
  CPU: (12) x64 12th Gen Intel(R) Core(TM) i5-12400
  Memory: 16.20 GB / 31.75 GB
Binaries:
  Node: 20.8.1 - C:\Program Files\nodejs\node.EXE
  npm: 10.1.0 - C:\Program Files\nodejs\npm.CMD
  pnpm: 8.15.4 - C:\Program Files\nodejs\pnpm.CMD
Browsers:
  Edge: Chromium (122.0.2365.59)
  Internet Explorer: 11.0.22621.1

Used Package Manager

pnpm

Validations

  • Follow our Code of Conduct
  • Read the Contributing Guide.
  • Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
  • Check that this is a concrete bug. For Q&A, please open a GitHub Discussion instead.
  • The provided reproduction is a minimal reproducible of the bug.

[email protected] is not supported, and I found this version supported TS config files already

Describe the bug

https://github.com/eslint/eslint/releases/tag/v9.9.0

Reproduction

https://github.com/eslint/eslint/releases/tag/v9.9.0

System Info

System:
    OS: Linux 5.15 Ubuntu 22.04.3 LTS 22.04.3 LTS (Jammy Jellyfish)
    CPU: (12) x64 Intel(R) Core(TM) i7-10710U CPU @ 1.10GHz
    Memory: 12.63 GB / 15.52 GB
    Container: Yes
    Shell: 5.8.1 - /usr/bin/zsh
  Binaries:
    Node: 22.6.0 - ~/.nvm/versions/node/v22.6.0/bin/node
    Yarn: 1.22.10 - /mnt/c/Users/zpr1g/AppData/Roaming/npm/yarn
    npm: 10.8.2 - ~/.nvm/versions/node/v22.6.0/bin/npm
    pnpm: 8.6.10 - /mnt/c/Users/zpr1g/AppData/Roaming/npm/pnpm

Used Package Manager

pnpm

Validations

  • Follow our Code of Conduct
  • Read the Contributing Guide.
  • Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
  • Check that this is a concrete bug. For Q&A, please open a GitHub Discussion instead.
  • The provided reproduction is a minimal reproducible of the bug.

Contributions

  • I am willing to submit a PR to fix this issue
  • I am willing to submit a PR with failing tests (actually just go ahead and do it, thanks!)

v3+ yarn compatibility

Additional context

When installing according to the documentation there will be an error, so you need to select a version of eslint-ts-patch

Example:

npm i -D eslint-ts-patch eslint@npm:[email protected]

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.