Skip to content
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

refactor(shared): replace 'local-pkg' package with node built-in api #881

Merged
merged 2 commits into from
Dec 17, 2024
Merged
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
8 changes: 8 additions & 0 deletions packages/plugins/eslint-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ export default [

![used-by](https://eslint-react.xyz/used_by.png)

### Also available in

- [`AndreaPontrandolfo/sheriff`](https://github.com/AndreaPontrandolfo/sheriff) - A comprehensive and opinionated Typescript-first ESLint configuration.
- [`antfu/eslint-config`](https://github.com/antfu/eslint-config) - Anthony's ESLint config preset.
- [`christopher-buss/roblox-ts-eslint-config`](https://github.com/christopher-buss/roblox-ts-eslint-config) - iSentinel's ESLint config preset for Roblox-TS.
- [`eslint-config-sukka`](https://github.com/SukkaW/eslint-config-sukka) - Sukka's ESLint config preset.
- [`RightCapitalHQ/frontend-style-guide`](https://github.com/RightCapitalHQ/frontend-style-guide) - RightCapital's frontend style guide.

_Data collected from GitHub dependents network, if there are any mismatch or outdated information, feel free to [open issue](https://github.com/Rel1cx/eslint-react/issues/new?assignees=&labels=type%3A+documentation&projects=&template=docs_report.md&title=%5Bdocs%5D+) or pull request._

Find more on [GitHub Dependents](https://github.com/Rel1cx/eslint-react/network/dependents).
Expand Down
1 change: 1 addition & 0 deletions packages/shared/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@
## Functions

- [defineSettings](functions/defineSettings.md)
- [getReactVersion](functions/getReactVersion.md)
23 changes: 23 additions & 0 deletions packages/shared/docs/functions/getReactVersion.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[**@eslint-react/shared**](../README.md)

***

[@eslint-react/shared](../README.md) / getReactVersion

# Function: getReactVersion()

> **getReactVersion**(`at`, `fallback`): `string`

## Parameters

### at

`string` = `...`

### fallback

`string` = `"19.0.0"`

## Returns

`string`
1 change: 0 additions & 1 deletion packages/shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
"dependencies": {
"@eslint-react/tools": "workspace:*",
"@typescript-eslint/utils": "^8.18.1",
"local-pkg": "^0.5.1",
"picomatch": "^4.0.2",
"ts-pattern": "^5.6.0"
},
Expand Down
15 changes: 15 additions & 0 deletions packages/shared/src/get-react-version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* eslint-disable @typescript-eslint/no-unsafe-return */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable import-x/no-extraneous-dependencies */
import module from "node:module";

export function getReactVersion(at = import.meta.url, fallback = "19.0.0"): string {
const require = module.createRequire(at);
try {
return require("react").version ?? fallback;
} catch {
// eslint-disable-next-line no-console
console.warn(`Failed to detect React version. Falling back to ${fallback}.`);
return fallback;
}
}
1 change: 1 addition & 0 deletions packages/shared/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from "./constants";
export * from "./create-rule";
export * from "./get-react-version";
export * from "./schemas";
export * from "./settings";
4 changes: 2 additions & 2 deletions packages/shared/src/settings.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { F } from "@eslint-react/tools";
import { shallowEqual } from "fast-equals";
import { getPackageInfoSync } from "local-pkg";
import memoize from "micro-memoize";
import pm from "picomatch";
import { match, P } from "ts-pattern";
import type { PartialDeep } from "type-fest";
import { parse } from "valibot";

import { getReactVersion } from "./get-react-version";
import type { ESLintReactSettings, ESLintReactSettingsNormalized } from "./schemas";
import { ESLintSettingsSchema } from "./schemas";

Expand Down Expand Up @@ -82,7 +82,7 @@ export const normalizeSettings = memoize((settings: ESLintReactSettings): ESLint
return acc.set(name, as);
}, new Map<string, string>()),
version: match(settings.version)
.with(P.union(P.nullish, "", "detect"), () => getPackageInfoSync("react")?.version ?? "19.0.0")
.with(P.union(P.nullish, "", "detect"), () => getReactVersion())
.otherwise(F.identity),
};
}, { isEqual: shallowEqual });
Expand Down
5 changes: 0 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading