|
| 1 | +import type { Rule } from "eslint"; |
| 2 | +import type * as ESTree from "estree"; |
| 3 | + |
| 4 | +interface StyleRuleOptions { |
| 5 | + syntax: "default" | "namespace"; |
| 6 | +} |
| 7 | + |
| 8 | +const styleRule: Rule.RuleModule = { |
| 9 | + meta: { |
| 10 | + type: "suggestion", |
| 11 | + fixable: "code", |
| 12 | + docs: { |
| 13 | + description: [ |
| 14 | + "Enforces React import style across your code.", |
| 15 | + "Can be customized to use default or namespace import.", |
| 16 | + "By default converts exports using namespace import", |
| 17 | + ].join(" "), |
| 18 | + }, |
| 19 | + messages: { |
| 20 | + wrongImport: "You should import React using {{syntax}} import syntax", |
| 21 | + multipleImport: |
| 22 | + "React was already imported. This import should be removed when using {{syntax}} import", |
| 23 | + }, |
| 24 | + schema: [ |
| 25 | + { |
| 26 | + type: "object", |
| 27 | + properties: { |
| 28 | + syntax: { enum: ["default", "namespace"] }, |
| 29 | + }, |
| 30 | + additionalProperties: false, |
| 31 | + }, |
| 32 | + ], |
| 33 | + }, |
| 34 | + create(context) { |
| 35 | + const [{ syntax = "namespace" } = {}] = context.options as [ |
| 36 | + StyleRuleOptions? |
| 37 | + ]; |
| 38 | + |
| 39 | + const invalidImportTypes: Array<string> = [ |
| 40 | + "ImportSpecifier", |
| 41 | + /** |
| 42 | + * Based on {@link StyleRuleOptions.syntax} we integrate the array with the unwanted import syntax: |
| 43 | + * If syntax is `default` we should exclude `namespace` imports and vice-versa. |
| 44 | + */ |
| 45 | + syntax === "default" |
| 46 | + ? "ImportNamespaceSpecifier" |
| 47 | + : "ImportDefaultSpecifier", |
| 48 | + ]; |
| 49 | + |
| 50 | + /** |
| 51 | + * Variable used to store all react imports not following the required syntax |
| 52 | + */ |
| 53 | + const reactInvalidImports: Array<ESTree.ImportDeclaration> = []; |
| 54 | + |
| 55 | + return { |
| 56 | + ImportDeclaration: (node) => { |
| 57 | + const { source, specifiers } = node; |
| 58 | + |
| 59 | + /** @todo might change selector to something like ImportDeclaration[source.value="react"] */ |
| 60 | + if (source.value !== "react") return; |
| 61 | + |
| 62 | + if (specifiers.some((it) => invalidImportTypes.includes(it.type))) { |
| 63 | + reactInvalidImports.push(node); |
| 64 | + } |
| 65 | + }, |
| 66 | + |
| 67 | + "Program:exit": () => { |
| 68 | + /** Check if there is at least one invalid import */ |
| 69 | + if (!reactInvalidImports.length) return; |
| 70 | + |
| 71 | + const [firstImport, ...otherReactImports] = reactInvalidImports; |
| 72 | + |
| 73 | + /** Replace the first import with the right import based on options */ |
| 74 | + context.report({ |
| 75 | + messageId: "wrongImport", |
| 76 | + data: { syntax }, |
| 77 | + loc: { ...firstImport.loc! }, |
| 78 | + fix(fixer) { |
| 79 | + /** Cycle all imports to understand if it should become a import type */ |
| 80 | + const importType = reactInvalidImports.every( |
| 81 | + (importNode) => |
| 82 | + "importKind" in importNode && importNode.importKind === "type" |
| 83 | + ) |
| 84 | + ? "type" |
| 85 | + : ""; |
| 86 | + |
| 87 | + const correctImportSyntax = |
| 88 | + syntax === "default" ? "React" : "* as React"; |
| 89 | + |
| 90 | + /** @todo maybe there is a more smart method working directly on AST node to do this */ |
| 91 | + const newImport = `import ${importType} ${correctImportSyntax} from 'react';`; |
| 92 | + |
| 93 | + return fixer.replaceText( |
| 94 | + firstImport, |
| 95 | + newImport.replace(/\s+/g, " ") |
| 96 | + ); |
| 97 | + }, |
| 98 | + }); |
| 99 | + |
| 100 | + /** All additional imports can be removed */ |
| 101 | + for (const reactImportNode of otherReactImports) { |
| 102 | + context.report({ |
| 103 | + messageId: "multipleImport", |
| 104 | + data: { syntax }, |
| 105 | + loc: { ...reactImportNode.loc! }, |
| 106 | + fix(fixer) { |
| 107 | + return fixer.remove(reactImportNode); |
| 108 | + }, |
| 109 | + }); |
| 110 | + } |
| 111 | + }, |
| 112 | + }; |
| 113 | + }, |
| 114 | +}; |
| 115 | + |
| 116 | +export default styleRule; |
0 commit comments