|
1 | | -import type { RuleContext, RuleFeature } from "@eslint-react/kit"; |
2 | 1 | import type { TSESTree } from "@typescript-eslint/types"; |
3 | 2 | import type { RuleListener } from "@typescript-eslint/utils/ts-eslint"; |
4 | 3 | import type { CamelCase } from "string-ts"; |
| 4 | +import { type RuleContext, type RuleFeature } from "@eslint-react/kit"; |
5 | 5 | import { getSettingsFromContext } from "@eslint-react/shared"; |
6 | 6 |
|
7 | 7 | import { createRule } from "../utils"; |
@@ -43,26 +43,28 @@ export function create(context: RuleContext<MessageID, []>): RuleListener { |
43 | 43 | messageId: "preferReactNamespaceImport", |
44 | 44 | node: hasOtherSpecifiers ? node : node.parent, |
45 | 45 | data: { importSource }, |
46 | | - // TODO: Use `settings.languagePreference` to determine what code style to use in the fixer (e.g. single or double quotes) |
47 | 46 | fix(fixer) { |
| 47 | + const importDeclarationText = context.sourceCode.getText(node.parent); |
| 48 | + const semi = importDeclarationText.endsWith(";") ? ";" : ""; |
48 | 49 | const quote = node.parent.source.raw.at(0) ?? "'"; |
49 | 50 | const isTypeImport = node.parent.importKind === "type"; |
50 | 51 | const importStringPrefix = `import${isTypeImport ? " type" : ""}`; |
51 | 52 | const importSourceQuoted = `${quote}${importSource}${quote}`; |
52 | | - const sourceCode = context.sourceCode.getText(node.parent); |
53 | | - const semiColon = sourceCode.endsWith(";") ? ";" : ""; |
54 | 53 | if (!hasOtherSpecifiers) { |
55 | 54 | return fixer.replaceText( |
56 | 55 | node.parent, |
57 | | - `${importStringPrefix} * as ${node.local.name} from ${importSourceQuoted}${semiColon}`, |
| 56 | + `${importStringPrefix} * as ${node.local.name} from ${importSourceQuoted}${semi}`, |
58 | 57 | ); |
59 | 58 | } |
60 | | - |
| 59 | + // dprint-ignore |
61 | 60 | // remove the default specifier and prepend the namespace import specifier |
62 | | - const specifiers = sourceCode.slice(sourceCode.indexOf("{"), sourceCode.indexOf("}") + 1); |
| 61 | + const specifiers = importDeclarationText.slice(importDeclarationText.indexOf("{"), importDeclarationText.indexOf("}") + 1); |
63 | 62 | return fixer.replaceText( |
64 | 63 | node.parent, |
65 | | - `${importStringPrefix} * as ${node.local.name} from ${importSourceQuoted}${semiColon}\n${importStringPrefix} ${specifiers} from ${importSourceQuoted}${semiColon}`, |
| 64 | + [ |
| 65 | + `${importStringPrefix} * as ${node.local.name} from ${importSourceQuoted}${semi}`, |
| 66 | + `${importStringPrefix} ${specifiers} from ${importSourceQuoted}${semi}`, |
| 67 | + ].join("\n"), |
66 | 68 | ); |
67 | 69 | }, |
68 | 70 | }); |
|
0 commit comments