Skip to content

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
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/spotty-cars-call.md
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
15 changes: 15 additions & 0 deletions .yarn/patches/eslint-npm-9.17.0-75805166d6.patch
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];
+ }
}
Comment on lines +8 to +13
Copy link
Member Author

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.


const isTS = isFileTS(filePath);
29 changes: 29 additions & 0 deletions incubator/yarn-plugin-eslint/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# @rnx-kit/yarn-plugin-eslint

[![Build](https://github.com/microsoft/rnx-kit/actions/workflows/build.yml/badge.svg)](https://github.com/microsoft/rnx-kit/actions/workflows/build.yml)
[![npm version](https://img.shields.io/npm/v/@rnx-kit/yarn-plugin-eslint)](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)
Copy link
Contributor

Choose a reason for hiding this comment

The 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
```
47 changes: 47 additions & 0 deletions incubator/yarn-plugin-eslint/package.json
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
}
131 changes: 131 additions & 0 deletions incubator/yarn-plugin-eslint/src/index.ts
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],
};
8 changes: 8 additions & 0 deletions incubator/yarn-plugin-eslint/tsconfig.json
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"]
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"@react-native-community/cli-types": "^15.0.0",
"@rnx-kit/react-native-host": "workspace:*",
"@vue/compiler-sfc": "link:./incubator/ignore",
"eslint": "patch:eslint@npm%3A9.17.0#~/.yarn/patches/eslint-npm-9.17.0-75805166d6.patch",
"react-native-macos/@react-native/assets-registry": "^0.76.0",
"react-native-macos/@react-native/codegen": "^0.76.0",
"react-native-macos/@react-native/community-cli-plugin": "^0.76.0",
Expand Down
1 change: 1 addition & 0 deletions packages/eslint-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"eslint": ">=8.57.0"
},
"devDependencies": {
"@eslint/js": "^9.0.0",
"@microsoft/eslint-plugin-sdl": "^1.0.0",
"@rnx-kit/scripts": "*",
"@rnx-kit/tsconfig": "*",
Expand Down
4 changes: 2 additions & 2 deletions packages/eslint-plugin/src/rules/no-export-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

/**
* @typedef {import("@typescript-eslint/types/dist/index").TSESTree.Node} Node
* @typedef {import("eslint").Linter.FlatConfig} FlatConfig
* @typedef {import("eslint").Linter.Config} Config
* @typedef {import("eslint").Rule.RuleContext} ESLintRuleContext
* @typedef {import("eslint").Rule.ReportFixer} ESLintReportFixer
* @typedef {{ exports: string[], types: string[] }} NamedExports
Expand All @@ -16,7 +16,7 @@
* maxDepth: number;
* };
* filename: string;
* languageOptions: FlatConfig["languageOptions"];
* languageOptions: Config["languageOptions"];
* parserOptions: ESLintRuleContext["parserOptions"];
* parserPath: ESLintRuleContext["parserPath"];
* sourceCode: ESLintRuleContext["sourceCode"];
Expand Down
1 change: 1 addition & 0 deletions test-repos/rnx-kit-workspaces.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"@rnx-kit/tsconfig": "packages/tsconfig",
"@rnx-kit/typescript-service": "packages/typescript-service",
"@rnx-kit/yarn-plugin-dynamic-extensions": "incubator/yarn-plugin-dynamic-extensions",
"@rnx-kit/yarn-plugin-eslint": "incubator/yarn-plugin-eslint",
"@rnx-kit/yarn-plugin-external-workspaces": "incubator/yarn-plugin-external-workspaces"
}
}
Expand Down
Loading
Loading