-
Notifications
You must be signed in to change notification settings - Fork 100
feat(yarn-plugin-eslint): Yarn plugin with integrated ESLint command #3577
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
tido64
wants to merge
1
commit into
main
Choose a base branch
from
tido/yarn/plugin-eslint
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,5 @@ | ||
--- | ||
"@rnx-kit/yarn-plugin-eslint": minor | ||
--- | ||
|
||
Introduce a Yarn plugin with integrated ESLint and opinionated defaults |
This file contains hidden or 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,15 @@ | ||
diff --git a/lib/config/config-loader.js b/lib/config/config-loader.js | ||
index 845cd0c92861353481d3369bb09e034e90f269ea..2eaaaade1e6b4949f1e38cee1c884f70b491b2d2 100644 | ||
--- a/lib/config/config-loader.js | ||
+++ b/lib/config/config-loader.js | ||
@@ -158,7 +158,9 @@ async function loadConfigFile(filePath, allowTS) { | ||
* the require cache only if the file has been changed. | ||
*/ | ||
if (importedConfigFileModificationTime.get(filePath) !== mtime) { | ||
- delete require.cache[filePath]; | ||
+ if (require.cache) { | ||
+ delete require.cache[filePath]; | ||
+ } | ||
} | ||
|
||
const isTS = isFileTS(filePath); |
This file contains hidden or 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,29 @@ | ||
# @rnx-kit/yarn-plugin-eslint | ||
|
||
[](https://github.com/microsoft/rnx-kit/actions/workflows/build.yml) | ||
[](https://www.npmjs.com/package/@rnx-kit/yarn-plugin-eslint) | ||
|
||
🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧 | ||
|
||
### THIS TOOL IS EXPERIMENTAL — USE WITH CAUTION | ||
|
||
🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧 | ||
|
||
This is a Yarn plugin that integrates ESLint and opinionated (but overridable) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reminder to add information about how it can be overridden and what the opinionated defaults are. |
||
defaults, allowing you to run ESLint without having to install/configure it | ||
separately in every workspace. | ||
|
||
## Installation | ||
|
||
The plugin needs to be installed via `yarn plugin install` command. This needs | ||
to reference the produced bundle out of the `lib` folder. | ||
|
||
```sh | ||
yarn plugin import ./path/to/@rnx-kit/yarn-plugin-eslint/lib/index.js | ||
``` | ||
|
||
## Usage | ||
|
||
```sh | ||
yarn rnx-lint | ||
``` |
This file contains hidden or 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,47 @@ | ||
{ | ||
"name": "@rnx-kit/yarn-plugin-eslint", | ||
"version": "0.0.1", | ||
"description": "EXPERIMENTAL - USE WITH CAUTION - yarn-plugin-eslint", | ||
"homepage": "https://github.com/microsoft/rnx-kit/tree/main/incubator/yarn-plugin-eslint#readme", | ||
"license": "MIT", | ||
"author": { | ||
"name": "Microsoft Open Source", | ||
"email": "[email protected]" | ||
}, | ||
"files": [ | ||
"lib/index.js" | ||
], | ||
"main": "lib/index.js", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/microsoft/rnx-kit", | ||
"directory": "incubator/yarn-plugin-eslint" | ||
}, | ||
"engines": { | ||
"node": ">=18.19" | ||
}, | ||
"scripts": { | ||
"build": "rnx-kit-scripts build", | ||
"bundle": "rnx-kit-scripts bundle --platform yarn --minify", | ||
"format": "rnx-kit-scripts format" | ||
}, | ||
"peerDependencies": { | ||
"jiti": "*" | ||
}, | ||
"peerDependenciesMeta": { | ||
"jiti": { | ||
"optional": true | ||
} | ||
}, | ||
"devDependencies": { | ||
"@eslint/compat": "^1.2.8", | ||
"@rnx-kit/eslint-plugin": "*", | ||
"@rnx-kit/scripts": "*", | ||
"@rnx-kit/tsconfig": "*", | ||
"@yarnpkg/cli": "^4.6.0", | ||
"@yarnpkg/core": "^4.0.0", | ||
"clipanion": "^4.0.0-rc.4", | ||
"eslint-formatter-pretty": "^6.0.1" | ||
}, | ||
"experimental": true | ||
} |
This file contains hidden or 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,131 @@ | ||
import { includeIgnoreFile } from "@eslint/compat"; | ||
import { BaseCommand } from "@yarnpkg/cli"; | ||
import { Configuration, Project } from "@yarnpkg/core"; | ||
import { Option } from "clipanion"; | ||
import type { Linter } from "eslint"; | ||
import { ESLint } from "eslint"; | ||
import { spawnSync } from "node:child_process"; | ||
import * as fs from "node:fs"; | ||
import * as path from "node:path"; | ||
|
||
type LinterConfigs = Linter.Config<Linter.RulesRecord>[]; | ||
|
||
export class Lint extends BaseCommand { | ||
static override paths = [["rnx-lint"]]; | ||
|
||
fix = Option.Boolean("--fix", false, { | ||
description: "Automatically fix problems", | ||
}); | ||
|
||
patterns = Option.Rest({ name: "file.js" }); | ||
|
||
async execute(): Promise<number | void> { | ||
const cwd = this.context.cwd; | ||
const configuration = await Configuration.find(cwd, this.context.plugins); | ||
const { project } = await Project.find(configuration, cwd); | ||
|
||
const [overrideConfigFile, overrideConfig] = await this.loadConfig(cwd); | ||
|
||
const eslint = new ESLint({ | ||
cwd, | ||
ignorePatterns: this.ignorePatterns(project), | ||
warnIgnored: false, | ||
overrideConfigFile, | ||
overrideConfig, | ||
fix: this.fix, | ||
}); | ||
|
||
const results = await eslint.lintFiles(this.filePatterns()); | ||
await ESLint.outputFixes(results); | ||
|
||
const formatter = await this.loadFormatter(); | ||
const output = formatter.format(results); | ||
|
||
if (output) { | ||
if (ESLint.getErrorResults(results).length > 0) { | ||
console.error(output); | ||
return 1; | ||
} | ||
|
||
console.log(output); | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
private filePatterns() { | ||
if (this.patterns.length > 0) { | ||
return this.patterns; | ||
} | ||
|
||
const args = [ | ||
"ls-files", | ||
"*.cjs", | ||
"*.js", | ||
"*.jsx", | ||
"*.mjs", | ||
"*.ts", | ||
"*.tsx", | ||
]; | ||
const { stdout } = spawnSync("git", args); | ||
return stdout.toString().trim().split("\n"); | ||
} | ||
|
||
private ignorePatterns(project: Project): string[] { | ||
const patterns: string[] = []; | ||
|
||
const locations = [project.cwd, this.context.cwd]; | ||
for (const location of locations) { | ||
const gitignore = path.join(location, ".gitignore"); | ||
if (fs.existsSync(gitignore)) { | ||
const { ignores } = includeIgnoreFile(gitignore); | ||
if (ignores) { | ||
patterns.push(...ignores); | ||
} | ||
} | ||
} | ||
|
||
return patterns; | ||
} | ||
|
||
private async loadConfig( | ||
cwd: string | ||
): Promise<[boolean | string, LinterConfigs | undefined]> { | ||
const eslintConfigPath = path.join(cwd, "eslint.config.js"); | ||
const overrideConfigFile = fs.existsSync(eslintConfigPath) | ||
? eslintConfigPath | ||
: true; | ||
|
||
if (overrideConfigFile !== true) { | ||
return [eslintConfigPath, undefined]; | ||
} | ||
|
||
const rnx = await import("@rnx-kit/eslint-plugin"); | ||
const config = [ | ||
...rnx.configs.strict, | ||
...rnx.configs.stylistic, | ||
{ | ||
rules: { | ||
"@typescript-eslint/consistent-type-definitions": ["error", "type"], | ||
}, | ||
}, | ||
]; | ||
|
||
return [true, config]; | ||
} | ||
|
||
/** | ||
* ESLint will try to dynamically import a formatter and fail. We bundle our | ||
* own formatter to bypass this. | ||
*/ | ||
private async loadFormatter() { | ||
const { default: format } = await import("eslint-formatter-pretty"); | ||
return { format }; | ||
} | ||
} | ||
|
||
// eslint-disable-next-line no-restricted-exports | ||
export default { | ||
name: "@rnx-kit/yarn-plugin-eslint", | ||
commands: [Lint], | ||
}; |
This file contains hidden or 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,8 @@ | ||
{ | ||
"extends": "@rnx-kit/tsconfig/tsconfig.json", | ||
"compilerOptions": { | ||
"target": "ES2021", | ||
"noEmit": true | ||
}, | ||
"include": ["src"] | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs to be upstreamed.