|
| 1 | +import { |
| 2 | + ASTUtils, |
| 3 | + AST_NODE_TYPES, |
| 4 | + TSESLint, |
| 5 | +} from "@typescript-eslint/experimental-utils"; |
| 6 | +import { array } from "fp-ts"; |
| 7 | +import { pipe } from "fp-ts/function"; |
| 8 | +import { contextUtils } from "../utils"; |
| 9 | + |
| 10 | +const messages = { |
| 11 | + importNotAllowed: |
| 12 | + "Importing from modules is not allowed, import from 'fp-ts' instead", |
| 13 | + importValuesNotAllowed: |
| 14 | + "Importing values from modules is not allowed, import from 'fp-ts' instead", |
| 15 | + convertImportToIndex: "Import all members from fp-ts", |
| 16 | + convertImportValuesToIndex: "Import values from fp-ts", |
| 17 | +} as const; |
| 18 | +type MessageIds = keyof typeof messages; |
| 19 | + |
| 20 | +type Options = ["always" | "allow-types"]; |
| 21 | + |
| 22 | +export const meta: TSESLint.RuleMetaData<MessageIds> = { |
| 23 | + type: "suggestion", |
| 24 | + fixable: "code", |
| 25 | + schema: [ |
| 26 | + { |
| 27 | + enum: ["always", "allow-types"], |
| 28 | + }, |
| 29 | + ], |
| 30 | + messages, |
| 31 | +}; |
| 32 | + |
| 33 | +export function create( |
| 34 | + context: TSESLint.RuleContext<MessageIds, Options> |
| 35 | +): TSESLint.RuleListener { |
| 36 | + const allowTypes = context.options[0] === "allow-types"; |
| 37 | + const allowedModules = ["function"]; |
| 38 | + |
| 39 | + const { |
| 40 | + addNamedImportIfNeeded, |
| 41 | + isOnlyUsedAsType, |
| 42 | + removeImportDeclaration, |
| 43 | + } = contextUtils(context); |
| 44 | + |
| 45 | + return { |
| 46 | + ImportDeclaration(node) { |
| 47 | + const sourceValue = node.source.value?.toString(); |
| 48 | + if (sourceValue) { |
| 49 | + const forbiddenImportPattern = /^fp-ts\/(.+)/; |
| 50 | + const matches = sourceValue.match(forbiddenImportPattern); |
| 51 | + |
| 52 | + if (matches != null) { |
| 53 | + const matchedModule = matches[1]!.replace("lib/", ""); |
| 54 | + if (allowedModules.includes(matchedModule)) { |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + const importSpecifiers = node.specifiers.filter( |
| 59 | + (importClause) => |
| 60 | + importClause.type === AST_NODE_TYPES.ImportSpecifier |
| 61 | + ); |
| 62 | + |
| 63 | + const nonTypeImports = pipe( |
| 64 | + importSpecifiers, |
| 65 | + array.filter((i) => !isOnlyUsedAsType(i)) |
| 66 | + ); |
| 67 | + |
| 68 | + if (allowTypes && nonTypeImports.length === 0) { |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + if (importSpecifiers.length > 0) { |
| 73 | + context.report({ |
| 74 | + node: node.source, |
| 75 | + messageId: allowTypes |
| 76 | + ? "importValuesNotAllowed" |
| 77 | + : "importNotAllowed", |
| 78 | + suggest: [ |
| 79 | + { |
| 80 | + messageId: allowTypes |
| 81 | + ? "convertImportValuesToIndex" |
| 82 | + : "convertImportToIndex", |
| 83 | + fix(fixer) { |
| 84 | + const indexExport = |
| 85 | + matchedModule.charAt(0).toLowerCase() + |
| 86 | + matchedModule.slice(1); |
| 87 | + |
| 88 | + const referencesFixes = importSpecifiers.flatMap( |
| 89 | + (importSpecifier) => { |
| 90 | + const variable = ASTUtils.findVariable( |
| 91 | + context.getScope(), |
| 92 | + importSpecifier.local.name |
| 93 | + ); |
| 94 | + if (variable) { |
| 95 | + return variable.references |
| 96 | + .filter((ref) => |
| 97 | + allowTypes |
| 98 | + ? ref.identifier.parent?.type !== |
| 99 | + AST_NODE_TYPES.TSTypeReference |
| 100 | + : true |
| 101 | + ) |
| 102 | + .filter( |
| 103 | + (ref) => |
| 104 | + ref.identifier.parent?.type !== |
| 105 | + AST_NODE_TYPES.MemberExpression |
| 106 | + ) |
| 107 | + .map((ref) => |
| 108 | + fixer.insertTextBefore( |
| 109 | + ref.identifier, |
| 110 | + `${indexExport}.` |
| 111 | + ) |
| 112 | + ); |
| 113 | + } else { |
| 114 | + return []; |
| 115 | + } |
| 116 | + } |
| 117 | + ); |
| 118 | + |
| 119 | + const importFixes = |
| 120 | + !allowTypes || |
| 121 | + nonTypeImports.length === importSpecifiers.length |
| 122 | + ? [removeImportDeclaration(node, fixer)] |
| 123 | + : nonTypeImports.map((node) => { |
| 124 | + if ( |
| 125 | + context.getSourceCode().getTokenAfter(node) |
| 126 | + ?.value === "," |
| 127 | + ) { |
| 128 | + return fixer.removeRange([ |
| 129 | + node.range[0], |
| 130 | + node.range[1] + 1, |
| 131 | + ]); |
| 132 | + } else { |
| 133 | + return fixer.remove(node); |
| 134 | + } |
| 135 | + }); |
| 136 | + |
| 137 | + return [ |
| 138 | + ...importFixes, |
| 139 | + ...addNamedImportIfNeeded(indexExport, "fp-ts", fixer), |
| 140 | + ...referencesFixes, |
| 141 | + ]; |
| 142 | + }, |
| 143 | + }, |
| 144 | + ], |
| 145 | + }); |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + }, |
| 150 | + }; |
| 151 | +} |
0 commit comments