forked from import-js/eslint-plugin-import
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This change adds support for ESLint's new Flat config system. It maintains backwards compatibility with eslintrc style configs as well. To achieve this, we're now dynamically creating flat configs on a new `flatConfigs` export. I was a bit on the fence about using this convention, or the other convention that's become prevalent in the community: adding the flat configs directly to the `configs` object, but with a 'flat/' prefix. I like this better, since it's slightly more ergonomic when using it in practice. e.g. `...importX.flatConfigs.recommended` vs `...importX.configs['flat/recommended']`, but i'm open to changing that. Example Usage ```js import importPlugin from 'eslint-plugin-import'; import js from '@eslint/js'; import tsParser from '@typescript-eslint/parser'; export default [ js.configs.recommended, importPlugin.flatConfigs.recommended, importPlugin.flatConfigs.react, importPlugin.flatConfigs.typescript, { files: ['**/*.{js,mjs,cjs,jsx,mjsx,ts,tsx,mtsx}'], languageOptions: { parser: tsParser, ecmaVersion: 'latest', sourceType: 'module', }, ignores: ['eslint.config.js'], rules: { 'no-unused-vars': 'off', 'import/no-dynamic-require': 'warn', 'import/no-nodejs-modules': 'warn', }, }, ]; ``` Note: in order to fill a gap in a future API gap for the `no-unused-module`, this takes advantage of a *proposed* new API on the ESLint context, that currently only exists in a POC state (unreleased). Closes import-js#2556
- Loading branch information
1 parent
dd81308
commit e4ae179
Showing
3 changed files
with
72 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
* This is intended to provide similar capability as the sync api from @nodelib/fs.walk, until `eslint-plugin-import` | ||
* is willing to modernize and update their minimum node version to at least v16. I intentionally made the | ||
* shape of the API (for the part we're using) the same as @nodelib/fs.walk so that that can be swapped in | ||
* when the repo is ready for it. | ||
*/ | ||
|
||
import path from 'path'; | ||
|
||
/** | ||
* Do a comprehensive walk of the provided src directory, and collect all entries. Filter out | ||
* any directories or entries using the optional filter functions. | ||
* @param {string} root - path to the root of the folder we're walking | ||
* @param {{ deepFilter?: ({name: string, path: string, dirent: Dirent}) => boolean, entryFilter?: ({name: string, path: string, dirent: Dirent}) => boolean }} options | ||
* @param {{name: string, path: string, dirent: Dirent}} currentEntry - entry for the current directory we're working in | ||
* @param {{name: string, path: string, dirent: Dirent}[]} existingEntries - list of all entries so far | ||
* @returns {{name: string, path: string, dirent: Dirent}[]} an array of directory entries | ||
*/ | ||
export const walkSync = (root, options, currentEntry, existingEntries) => { | ||
const { readdirSync } = require('node:fs'); | ||
|
||
// Extract the filter functions. Default to evaluating true, if no filter passed in. | ||
const { deepFilter = () => true, entryFilter = () => true } = options; | ||
|
||
let entryList = existingEntries || []; | ||
const currentRelativePath = currentEntry ? currentEntry.path : '.'; | ||
const fullPath = currentEntry ? path.join(root, currentEntry.path) : root; | ||
|
||
const dirents = readdirSync(fullPath, { withFileTypes: true }); | ||
for (const dirent of dirents) { | ||
const entry = { | ||
name: dirent.name, | ||
path: path.join(currentRelativePath, dirent.name), | ||
dirent, | ||
}; | ||
if (dirent.isDirectory() && deepFilter(entry)) { | ||
entryList.push(entry); | ||
entryList = walkSync(root, options, entry, entryList); | ||
} else if (dirent.isFile() && entryFilter(entry)) { | ||
entryList.push(entry); | ||
} | ||
} | ||
|
||
return entryList; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters