Skip to content

Commit 384c191

Browse files
authored
feat: add autofix for no-missing-context-display-name (#1128)
1 parent 3ff9649 commit 384c191

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

packages/plugins/eslint-plugin-react-x/src/rules/no-missing-context-display-name.spec.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ ruleTester.run(RULE_NAME, rule, {
1212
{
1313
code: tsx`const ctx = createContext();`,
1414
errors: [{ messageId: "noMissingContextDisplayName" }],
15+
output: tsx`
16+
const ctx = createContext()
17+
ctx.displayName = "ctx";
18+
`,
1519
},
1620
{
1721
code: tsx`
@@ -20,13 +24,24 @@ ruleTester.run(RULE_NAME, rule, {
2024
ctx1.displayName = "ctx";
2125
`,
2226
errors: [{ messageId: "noMissingContextDisplayName" }],
27+
output: tsx`
28+
const ctx1 = createContext();
29+
const ctx2 = createContext()
30+
ctx2.displayName = "ctx2";
31+
ctx1.displayName = "ctx";
32+
`,
2333
},
2434
{
2535
code: tsx`
2636
const ctx = createContext();
2737
ctx.displayname = "ctx";
2838
`,
2939
errors: [{ messageId: "noMissingContextDisplayName" }],
40+
output: tsx`
41+
const ctx = createContext()
42+
ctx.displayName = "ctx";
43+
ctx.displayname = "ctx";
44+
`,
3045
},
3146
{
3247
code: tsx`
@@ -35,6 +50,37 @@ ruleTester.run(RULE_NAME, rule, {
3550
`,
3651
errors: [{ messageId: "noMissingContextDisplayName" }],
3752
},
53+
{
54+
// this doesn't make sense, it's just to test the autofixer
55+
code: tsx`
56+
const [nonsense] = createContext();
57+
const { invalid } = createContext();
58+
`,
59+
errors: [
60+
{ messageId: "noMissingContextDisplayName" },
61+
{ messageId: "noMissingContextDisplayName" },
62+
],
63+
},
64+
{
65+
// not autofixable
66+
code: tsx`
67+
const contexts = { a: createContext(), b: createContext() };
68+
`,
69+
errors: [
70+
{ messageId: "noMissingContextDisplayName" },
71+
{ messageId: "noMissingContextDisplayName" },
72+
],
73+
},
74+
{
75+
// not autofixable
76+
code: tsx`
77+
const [a, b] = [createContext(), createContext()];
78+
`,
79+
errors: [
80+
{ messageId: "noMissingContextDisplayName" },
81+
{ messageId: "noMissingContextDisplayName" },
82+
],
83+
},
3884
],
3985
valid: [
4086
...allFunctions,

packages/plugins/eslint-plugin-react-x/src/rules/no-missing-context-display-name.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export default createRule<[], MessageID>({
2020
description: "Enforces that all contexts have a `displayName` which can be used in devtools.",
2121
[Symbol.for("rule_features")]: RULE_FEATURES,
2222
},
23+
fixable: "code",
2324
messages: {
2425
noMissingContextDisplayName: "Add missing 'displayName' for context.",
2526
},
@@ -62,6 +63,9 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
6263
context.report({
6364
messageId: "noMissingContextDisplayName",
6465
node: id,
66+
fix: id.type === T.Identifier && id.parent === call.parent
67+
? ((fixer) => fixer.insertTextAfter(call, `\n${id.name}.displayName = ${JSON.stringify(id.name)}`))
68+
: null,
6569
});
6670
}
6771
}

0 commit comments

Comments
 (0)