Skip to content

fix(plugins/x): fix 'no-unstable-rules' false positives #896

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
Dec 31, 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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as AST from "@eslint-react/ast";
import { useComponentCollector } from "@eslint-react/core";
import { isReactHookCall, useComponentCollector } from "@eslint-react/core";
import { F, O } from "@eslint-react/eff";
import type { RuleFeature } from "@eslint-react/types";
import * as VAR from "@eslint-react/var";
Expand Down Expand Up @@ -65,9 +65,10 @@ export default createRule<[], MessageID>({
const initialScope = context.sourceCode.getScope(valueExpression);
return O.some(VAR.inspectConstruction(valueExpression, initialScope));
}),
O.filter(({ construction }) => construction._tag !== "None"),
);
for (const { construction, function: [_, fNode] } of O.toArray(constructionEntry)) {
if (construction._tag === "None") continue;
if (isReactHookCall(construction.node)) continue;
constructions.set(fNode, [
...constructions.get(fNode) ?? [],
construction,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ ruleTester.run(RULE_NAME, rule, {
g = new Thing(),
h = <Thing />,
i = Symbol('foo'),
j = unknownFunction()
j = unknownFunction(),
k = window.name
}) {
return null
}
Expand All @@ -152,7 +153,8 @@ ruleTester.run(RULE_NAME, rule, {
g = new Thing(),
h = <Thing />,
i = Symbol('foo'),
j = unknownFunction()
j = unknownFunction(),
k = window.name
}) => {
return null
}
Expand Down Expand Up @@ -212,6 +214,76 @@ ruleTester.run(RULE_NAME, rule, {
return <div>{items}</div>;
}
`,
/* tsx */ `export default function NonComponent({ foo = {} }) {}`,
/* tsx */ `
export default function NonComponent({ foo = {} }) {}
`,
/* tsx */ `
export function DrawerItem(props: Props) {
const { colors, fonts } = useTheme();

const {
href,
icon,
label,
labelStyle,
focused = false,
allowFontScaling,
activeTintColor = colors.primary,
inactiveBackgroundColor = 'transparent',
style,
onPress,
pressColor,
pressOpacity = 1,
testID,
accessibilityLabel,
...rest
} = props;

const { borderRadius = 56 } = StyleSheet.flatten(style || {});
const color = focused ? activeTintColor : inactiveTintColor;
const backgroundColor = focused
? activeBackgroundColor
: inactiveBackgroundColor;

const iconNode = icon ? icon({ size: 24, focused, color }) : null;

return (
<View
collapsable={false}
{...rest}
style={[styles.container, { borderRadius, backgroundColor }, style]}
>
<PlatformPressable
testID={testID}
onPress={onPress}
accessibilityLabel={accessibilityLabel}
accessibilityRole="button"
accessibilityState={{ selected: focused }}
pressColor={pressColor}
pressOpacity={pressOpacity}
hoverEffect={{ color }}
href={href}
>
<View style={[styles.wrapper, { borderRadius }]}>
{iconNode}
<View style={[styles.label, { marginStart: iconNode ? 12 : 0 }]}>
{typeof label === 'string' ? (
<Text
numberOfLines={1}
allowFontScaling={allowFontScaling}
style={[styles.labelText, { color }, fonts.medium, labelStyle]}
>
{label}
</Text>
) : (
label({ color, focused })
)}
</View>
</View>
</PlatformPressable>
</View>
);
}
`,
],
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as AST from "@eslint-react/ast";
import { useComponentCollector } from "@eslint-react/core";
import { isReactHookCall, useComponentCollector } from "@eslint-react/core";
import { O } from "@eslint-react/eff";
import type { RuleFeature } from "@eslint-react/types";
import * as VAR from "@eslint-react/var";
Expand Down Expand Up @@ -71,6 +71,7 @@ export default createRule<[], MessageID>({
VAR.ConstructionHint.StrictCallExpression,
);
if (construction._tag === "None") continue;
if (isReactHookCall(construction.node)) continue;
const forbiddenType = AST.toReadableNodeType(right);
context.report({
messageId: "noUnstableDefaultProps",
Expand Down
62 changes: 29 additions & 33 deletions packages/utilities/var/src/construction.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import * as AST from "@eslint-react/ast";
import { Data, isNullable, isObject, isString, O } from "@eslint-react/eff";
import { Data, F, isNullable, isObject, isString, not, O } from "@eslint-react/eff";
import type { Scope } from "@typescript-eslint/scope-manager";
import { DefinitionType } from "@typescript-eslint/scope-manager";
import type { TSESTree } from "@typescript-eslint/types";
import { AST_NODE_TYPES } from "@typescript-eslint/types";
import { match } from "ts-pattern";

import { getVariableNode } from "./get-variable-node";

export type Construction = Data.TaggedEnum<{
Array: {
node: TSESTree.ArrayExpression;
Expand Down Expand Up @@ -47,7 +48,10 @@ export type Construction = Data.TaggedEnum<{
node: TSESTree.NewExpression;
usage: O.Option<TSESTree.Node>;
};
None: {};
None: {
node: TSESTree.Node;
usage: O.Option<TSESTree.Node>;
};
ObjectExpression: {
node: TSESTree.ObjectExpression;
usage: O.Option<TSESTree.Node>;
Expand Down Expand Up @@ -95,7 +99,7 @@ export function inspectConstruction(
if (hint & ConstructionHint.StrictCallExpression) {
return Construction.CallExpression({ node, usage: O.none() });
}
return Construction.None();
return Construction.None({ node, usage: O.none() });
})
.when(AST.is(AST_NODE_TYPES.NewExpression), (node) => Construction.NewExpression({ node, usage: O.none() }))
.when(
Expand All @@ -108,7 +112,7 @@ export function inspectConstruction(
},
)
.when(AST.is(AST_NODE_TYPES.MemberExpression), (node) => {
if (!("object" in node)) return Construction.None();
if (!("object" in node)) return Construction.None({ node, usage: O.none() });
const object = detect(node.object);
if (object._tag === "None") return object;
return {
Expand All @@ -117,7 +121,7 @@ export function inspectConstruction(
} as const satisfies Construction;
})
.when(AST.is(AST_NODE_TYPES.AssignmentExpression), (node) => {
if (!("right" in node)) return Construction.None();
if (!("right" in node)) return Construction.None({ node, usage: O.none() });
const right = detect(node.right);
if (right._tag === "None") return right;
return Construction.AssignmentExpression({
Expand All @@ -126,7 +130,7 @@ export function inspectConstruction(
});
})
.when(AST.is(AST_NODE_TYPES.AssignmentPattern), (node) => {
if (!("right" in node)) return Construction.None();
if (!("right" in node)) return Construction.None({ node, usage: O.none() });
const right = detect(node.right);
if (right._tag === "None") return right;
return Construction.AssignmentPattern({
Expand All @@ -135,54 +139,46 @@ export function inspectConstruction(
});
})
.when(AST.is(AST_NODE_TYPES.LogicalExpression), (node) => {
if (!("left" in node && "right" in node)) return Construction.None();
if (!("left" in node && "right" in node)) return Construction.None({ node, usage: O.none() });
const left = detect(node.left);
if (left._tag !== "None") return left;
return detect(node.right);
})
.when(AST.is(AST_NODE_TYPES.ConditionalExpression), (node) => {
if (!("consequent" in node && "alternate" in node && !isNullable(node.alternate))) {
return Construction.None();
return Construction.None({ node, usage: O.none() });
}
const consequent = detect(node.consequent);
if (consequent._tag !== "None") return Construction.None();
if (consequent._tag !== "None") return Construction.None({ node, usage: O.none() });
return detect(node.alternate);
})
.when(AST.is(AST_NODE_TYPES.Identifier), (node) => {
if (!("name" in node && isString(node.name))) return Construction.None();
const maybeLatestDef = O.fromNullable(initialScope.set.get(node.name)?.defs.at(-1));
if (O.isNone(maybeLatestDef)) return Construction.None();
const latestDef = maybeLatestDef.value;
if (
latestDef.type !== DefinitionType.Variable
&& latestDef.type !== DefinitionType.FunctionName
) {
return Construction.None();
}
if (latestDef.node.type === AST_NODE_TYPES.FunctionDeclaration) {
return Construction.FunctionDeclaration({
node: latestDef.node,
usage: O.some(node),
});
}
if (!("init" in latestDef.node) || latestDef.node.init === null) {
return Construction.None();
}
return detect(latestDef.node.init);
if (!("name" in node && isString(node.name))) return Construction.None({ node, usage: O.none() });
const construction = F.pipe(
O.fromNullable(initialScope.set.get(node.name)),
O.flatMap(getVariableNode(-1)),
O.map(detect),
O.filter(not(Construction.$is("None"))),
);
if (O.isNone(construction)) return Construction.None({ node, usage: O.none() });
return {
...construction.value,
usage: O.some(node),
} as const;
})
.when(AST.is(AST_NODE_TYPES.Literal), (node) => {
if ("regex" in node) {
return Construction.RegExpLiteral({ node, usage: O.none() });
}
return Construction.None();
return Construction.None({ node, usage: O.none() });
})
.when(AST.isTypeExpression, () => {
if (!("expression" in node) || !isObject(node.expression)) {
return Construction.None();
return Construction.None({ node, usage: O.none() });
}
return detect(node.expression);
})
.otherwise(() => Construction.None());
.otherwise(() => Construction.None({ node, usage: O.none() }));
};
return detect(node);
}
Loading