Skip to content

Commit ead7e88

Browse files
authored
fix: 'no-missing-context-display-name' correctly handles semicolons in autofix (#1130)
1 parent 03d1f3a commit ead7e88

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ ruleTester.run(RULE_NAME, rule, {
1313
code: tsx`const ctx = createContext();`,
1414
errors: [{ messageId: "noMissingContextDisplayName" }],
1515
output: tsx`
16-
const ctx = createContext()
16+
const ctx = createContext();
1717
ctx.displayName = "ctx";
1818
`,
1919
},
@@ -26,7 +26,7 @@ ruleTester.run(RULE_NAME, rule, {
2626
errors: [{ messageId: "noMissingContextDisplayName" }],
2727
output: tsx`
2828
const ctx1 = createContext();
29-
const ctx2 = createContext()
29+
const ctx2 = createContext();
3030
ctx2.displayName = "ctx2";
3131
ctx1.displayName = "ctx";
3232
`,
@@ -38,7 +38,7 @@ ruleTester.run(RULE_NAME, rule, {
3838
`,
3939
errors: [{ messageId: "noMissingContextDisplayName" }],
4040
output: tsx`
41-
const ctx = createContext()
41+
const ctx = createContext();
4242
ctx.displayName = "ctx";
4343
ctx.displayname = "ctx";
4444
`,

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

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { TSESTree } from "@typescript-eslint/types";
22
import type { RuleListener } from "@typescript-eslint/utils/ts-eslint";
33
import type { CamelCase } from "string-ts";
44
import * as ER from "@eslint-react/core";
5-
import { type RuleContext, type RuleFeature, Selector as SEL } from "@eslint-react/kit";
5+
import { LanguagePreference, type RuleContext, type RuleFeature, Selector as SEL } from "@eslint-react/kit";
66
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
77

88
import { createRule } from "../utils";
@@ -62,12 +62,29 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
6262
return ER.isInstanceIdEqual(context, id, object);
6363
});
6464
if (!hasDisplayNameAssignment) {
65+
const semi = LanguagePreference.defaultLanguagePreference.semicolons === "always"
66+
? ";"
67+
: "";
6568
context.report({
6669
messageId: "noMissingContextDisplayName",
6770
node: id,
68-
fix: id.type === T.Identifier && id.parent === call.parent
69-
? ((fixer) => fixer.insertTextAfter(call, `\n${id.name}.displayName = ${JSON.stringify(id.name)}`))
70-
: null,
71+
fix(fixer) {
72+
if (id.type !== T.Identifier || id.parent !== call.parent) return [];
73+
return fixer.insertTextAfter(
74+
context.sourceCode.getTokenAfter(call) ?? call,
75+
[
76+
"\n",
77+
id.name,
78+
".",
79+
"displayName",
80+
" ",
81+
"=",
82+
" ",
83+
JSON.stringify(id.name),
84+
semi,
85+
].join(""),
86+
);
87+
},
7188
});
7289
}
7390
}

packages/utilities/kit/src/LanguagePreference/LanguagePreference.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ export function make(): LanguagePreference {
2121
};
2222
}
2323

24+
/**
25+
* A default LanguagePreference object.
26+
*/
27+
export const defaultLanguagePreference = make();
28+
2429
export function getFromContext() {
2530
throw new Error("getFromContext is not implemented");
2631
}

0 commit comments

Comments
 (0)