Skip to content

feat(shared): add version detection logic; fix(plugins/x): 'no-leaked-conditi… #864

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

Merged
merged 1 commit into from
Nov 22, 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
1 change: 1 addition & 0 deletions packages/plugins/eslint-plugin-react-x/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"@typescript-eslint/type-utils": "^8.15.0",
"@typescript-eslint/types": "^8.15.0",
"@typescript-eslint/utils": "^8.15.0",
"compare-versions": "^6.1.1",
"is-immutable-type": "5.0.0",
"ts-pattern": "^5.5.0"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ ruleTesterWithTypes.run(RULE_NAME, rule, {
errors: [
{ messageId: "noLeakedConditionalRendering" },
],
settings: {
"react-x": {
version: "17.0.0",
},
},
},
{
code: /* tsx */ `
Expand All @@ -25,6 +30,28 @@ ruleTesterWithTypes.run(RULE_NAME, rule, {
errors: [
{ messageId: "noLeakedConditionalRendering" },
],
settings: {
"react-x": {
version: "17.0.0",
},
},
},
{
code: /* tsx */ `
/// <reference types="react" />
/// <reference types="react-dom" />

const anyString = Math.random() > 0.5 ? "" : "foo";
const a = <>{anyString && <Something />}</>;
`,
errors: [
{ messageId: "noLeakedConditionalRendering" },
],
settings: {
"react-x": {
version: "17.0.0",
},
},
},
{
code: /* tsx */ `
Expand Down Expand Up @@ -683,5 +710,33 @@ ruleTesterWithTypes.run(RULE_NAME, rule, {
);
};
`,
{
code: /* tsx */ `
/// <reference types="react" />
/// <reference types="react-dom" />

const someString = "";
const a = <>{someString && <Something />}</>;
`,
settings: {
"react-x": {
version: "18.0.0",
},
},
},
{
code: /* tsx */ `
/// <reference types="react" />
/// <reference types="react-dom" />

const anyString = Math.random() > 0.5 ? "" : "foo";
const a = <>{anyString && <Something />}</>;
`,
settings: {
"react-x": {
version: "18.0.0",
},
},
},
],
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as AST from "@eslint-react/ast";
import { decodeSettings, normalizeSettings } from "@eslint-react/shared";
import { F, O } from "@eslint-react/tools";
import * as VAR from "@eslint-react/var";
import type { Variable } from "@typescript-eslint/scope-manager";
Expand All @@ -8,6 +9,7 @@ import { AST_NODE_TYPES } from "@typescript-eslint/types";
import { ESLintUtils } from "@typescript-eslint/utils";
import { getStaticValue } from "@typescript-eslint/utils/ast-utils";
import type { ReportDescriptor } from "@typescript-eslint/utils/ts-eslint";
import { compare } from "compare-versions";
import type { CamelCase } from "string-ts";
import { isFalseLiteralType, isTrueLiteralType, isTypeFlagSet, unionTypeParts } from "ts-api-utils";
import { isMatching, match, P } from "ts-pattern";
Expand Down Expand Up @@ -48,20 +50,6 @@ type VariantType =
| "truthy string";
/* eslint-enable perfectionist/sort-union-types */

// Allowed left node type variants
const allowedVariants = [
"any",
"boolean",
"nullish",
"object",
"string",
"falsy boolean",
"truthy bigint",
"truthy boolean",
"truthy number",
"truthy string",
] as const satisfies VariantType[];

// #endregion

// #region Helpers
Expand Down Expand Up @@ -223,6 +211,25 @@ export default createRule<[], MessageID>({
name: RULE_NAME,
create(context) {
if (!context.sourceCode.text.includes("&&") && !context.sourceCode.text.includes("?")) return {};

const { version } = normalizeSettings(decodeSettings(context.settings));

// Allowed left node type variants
const allowedVariants = [
"any",
"boolean",
"nullish",
"object",
"falsy boolean",
"truthy bigint",
"truthy boolean",
"truthy number",
"truthy string",
...compare(version, "18.0.0", "<")
? []
: ["string", "falsy string"] as const,
] as const satisfies VariantType[];

const services = ESLintUtils.getParserServices(context, false);
function getReportDescriptor(node: TSESTree.Expression): O.Option<ReportDescriptor<MessageID>> {
return match<typeof node, O.Option<ReportDescriptor<MessageID>>>(node)
Expand Down
4 changes: 3 additions & 1 deletion packages/shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@
"dependencies": {
"@eslint-react/tools": "workspace:*",
"@typescript-eslint/utils": "^8.15.0",
"picomatch": "^4.0.2"
"local-pkg": "^0.5.1",
"picomatch": "^4.0.2",
"ts-pattern": "^5.5.0"
},
"devDependencies": {
"@types/picomatch": "^3.0.1",
Expand Down
1 change: 1 addition & 0 deletions packages/shared/src/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,5 @@ export type ESLintSettings = InferOutput<typeof ESLintSettingsSchema>;
export interface ESLintReactSettingsNormalized extends ESLintReactSettings {
additionalComponents: CustomComponentNormalized[];
components: Map<string, string>;
version: string;
}
5 changes: 5 additions & 0 deletions packages/shared/src/settings.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Data, 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";

Expand Down Expand Up @@ -72,6 +74,9 @@ export const normalizeSettings = memoize((settings: ESLintReactSettings) => {
if (!/^[\w-]+$/u.test(name)) return acc;
return acc.set(name, as);
}, new Map<string, string>()),
version: match(settings.version)
.with(P.union(P.nullish, "", "detect"), () => getPackageInfoSync("react")?.version)
.otherwise(F.identity) ?? "18.3.1",
});
}, { isEqual: shallowEqual });

Expand Down
52 changes: 33 additions & 19 deletions pnpm-lock.yaml

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