Skip to content

Commit

Permalink
refactor(plugins/x): minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Rel1cx committed Feb 2, 2025
1 parent b4c869e commit 7135243
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ export default createRule<[], MessageID>({
},
name: RULE_NAME,
create(context) {
if (!context.sourceCode.text.includes(".Provider")) {
return {};
}
if (!context.sourceCode.text.includes(".Provider")) return {};
const { version } = getSettingsFromContext(context);
if (compare(version, "19.0.0", "<")) {
return {};
Expand All @@ -47,15 +45,15 @@ export default createRule<[], MessageID>({
messageId: "noContextProvider",
node,
fix(fixer) {
const providerName = elementName.replace(/\.Provider$/, "");
const contextName = elementName.replace(/\.Provider$/, "");
const openingElement = node.openingElement;
const closingElement = node.closingElement;
if (closingElement == null) {
return fixer.replaceText(openingElement.name, providerName);
return fixer.replaceText(openingElement.name, contextName);
}
return [
fixer.replaceText(openingElement.name, providerName),
fixer.replaceText(closingElement.name, providerName),
fixer.replaceText(openingElement.name, contextName),
fixer.replaceText(closingElement.name, contextName),
];
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,20 @@ function getFix(node: TSESTree.CallExpression, context: RuleContext): (fixer: Ru
fixer.removeRange([componentNode.range[1], node.range[1]]),
// update component props and ref arguments to match the new signature
...getComponentPropsFixes(
context,
fixer,
componentNode,
node.typeArguments?.params ?? [],
fixer,
context,
),
] as const;
};
}

function getComponentPropsFixes(
context: RuleContext,
fixer: RuleFixer,
node: AST.TSESTreeFunction,
typeArguments: TSESTree.TypeNode[],
fixer: RuleFixer,
context: RuleContext,
) {
const getText = (node: TSESTree.Node) => context.sourceCode.getText(node);
const [arg0, arg1] = node.params;
Expand All @@ -99,7 +99,7 @@ function getComponentPropsFixes(
const fixedArg1Text = match(arg1)
.with(P.nullish, () => "ref")
.with({ type: T.Identifier, name: "ref" }, () => "ref")
.with({ type: T.Identifier, name: P.not("ref") }, (n) => `ref: ${n.name}`)
.with({ type: T.Identifier, name: P.string }, (n) => `ref: ${n.name}`)
.otherwise(() => _);
if (fixedArg0Text == null || fixedArg1Text == null) {
return [];
Expand Down

0 comments on commit 7135243

Please sign in to comment.