|
| 1 | +import type { TSESTree } from '@typescript-eslint/utils'; |
| 2 | +import { AST_NODE_TYPES } from '@typescript-eslint/utils'; |
| 3 | +import type { Type } from 'typescript'; |
| 4 | + |
| 5 | +import * as util from '../util'; |
| 6 | + |
| 7 | +export type Options = [ |
| 8 | + { |
| 9 | + ignoreIntersections?: boolean; |
| 10 | + ignoreUnions?: boolean; |
| 11 | + }, |
| 12 | +]; |
| 13 | + |
| 14 | +export type MessageIds = 'duplicate'; |
| 15 | + |
| 16 | +const astIgnoreKeys = new Set(['range', 'loc', 'parent']); |
| 17 | + |
| 18 | +const isSameAstNode = (actualNode: unknown, expectedNode: unknown): boolean => { |
| 19 | + if (actualNode === expectedNode) { |
| 20 | + return true; |
| 21 | + } |
| 22 | + if ( |
| 23 | + actualNode && |
| 24 | + expectedNode && |
| 25 | + typeof actualNode === 'object' && |
| 26 | + typeof expectedNode === 'object' |
| 27 | + ) { |
| 28 | + if (Array.isArray(actualNode) && Array.isArray(expectedNode)) { |
| 29 | + if (actualNode.length !== expectedNode.length) { |
| 30 | + return false; |
| 31 | + } |
| 32 | + return !actualNode.some( |
| 33 | + (nodeEle, index) => !isSameAstNode(nodeEle, expectedNode[index]), |
| 34 | + ); |
| 35 | + } |
| 36 | + const actualNodeKeys = Object.keys(actualNode).filter( |
| 37 | + key => !astIgnoreKeys.has(key), |
| 38 | + ); |
| 39 | + const expectedNodeKeys = Object.keys(expectedNode).filter( |
| 40 | + key => !astIgnoreKeys.has(key), |
| 41 | + ); |
| 42 | + if (actualNodeKeys.length !== expectedNodeKeys.length) { |
| 43 | + return false; |
| 44 | + } |
| 45 | + if ( |
| 46 | + actualNodeKeys.some( |
| 47 | + actualNodeKey => |
| 48 | + !Object.prototype.hasOwnProperty.call(expectedNode, actualNodeKey), |
| 49 | + ) |
| 50 | + ) { |
| 51 | + return false; |
| 52 | + } |
| 53 | + if ( |
| 54 | + actualNodeKeys.some( |
| 55 | + actualNodeKey => |
| 56 | + !isSameAstNode( |
| 57 | + actualNode[actualNodeKey as keyof typeof actualNode], |
| 58 | + expectedNode[actualNodeKey as keyof typeof expectedNode], |
| 59 | + ), |
| 60 | + ) |
| 61 | + ) { |
| 62 | + return false; |
| 63 | + } |
| 64 | + return true; |
| 65 | + } |
| 66 | + return false; |
| 67 | +}; |
| 68 | + |
| 69 | +export default util.createRule<Options, MessageIds>({ |
| 70 | + name: 'no-duplicate-type-constituents', |
| 71 | + meta: { |
| 72 | + type: 'suggestion', |
| 73 | + docs: { |
| 74 | + description: |
| 75 | + 'Disallow duplicate constituents of union or intersection types', |
| 76 | + recommended: false, |
| 77 | + requiresTypeChecking: true, |
| 78 | + }, |
| 79 | + fixable: 'code', |
| 80 | + messages: { |
| 81 | + duplicate: '{{type}} type constituent is duplicated with {{previous}}.', |
| 82 | + }, |
| 83 | + schema: [ |
| 84 | + { |
| 85 | + additionalProperties: false, |
| 86 | + type: 'object', |
| 87 | + properties: { |
| 88 | + ignoreIntersections: { |
| 89 | + type: 'boolean', |
| 90 | + }, |
| 91 | + ignoreUnions: { |
| 92 | + type: 'boolean', |
| 93 | + }, |
| 94 | + }, |
| 95 | + }, |
| 96 | + ], |
| 97 | + }, |
| 98 | + defaultOptions: [ |
| 99 | + { |
| 100 | + ignoreIntersections: false, |
| 101 | + ignoreUnions: false, |
| 102 | + }, |
| 103 | + ], |
| 104 | + create(context, [{ ignoreIntersections, ignoreUnions }]) { |
| 105 | + const parserServices = util.getParserServices(context); |
| 106 | + const checker = parserServices.program.getTypeChecker(); |
| 107 | + |
| 108 | + function checkDuplicate( |
| 109 | + node: TSESTree.TSIntersectionType | TSESTree.TSUnionType, |
| 110 | + ): void { |
| 111 | + const cachedTypeMap: Map<Type, TSESTree.TypeNode> = new Map(); |
| 112 | + node.types.reduce<TSESTree.TypeNode[]>( |
| 113 | + (uniqueConstituents, constituentNode) => { |
| 114 | + const duplicatedPreviousConstituentInAst = uniqueConstituents.find( |
| 115 | + ele => isSameAstNode(ele, constituentNode), |
| 116 | + ); |
| 117 | + if (duplicatedPreviousConstituentInAst) { |
| 118 | + reportDuplicate( |
| 119 | + { |
| 120 | + duplicated: constituentNode, |
| 121 | + duplicatePrevious: duplicatedPreviousConstituentInAst, |
| 122 | + }, |
| 123 | + node, |
| 124 | + ); |
| 125 | + return uniqueConstituents; |
| 126 | + } |
| 127 | + const constituentNodeType = checker.getTypeAtLocation( |
| 128 | + parserServices.esTreeNodeToTSNodeMap.get(constituentNode), |
| 129 | + ); |
| 130 | + const duplicatedPreviousConstituentInType = |
| 131 | + cachedTypeMap.get(constituentNodeType); |
| 132 | + if (duplicatedPreviousConstituentInType) { |
| 133 | + reportDuplicate( |
| 134 | + { |
| 135 | + duplicated: constituentNode, |
| 136 | + duplicatePrevious: duplicatedPreviousConstituentInType, |
| 137 | + }, |
| 138 | + node, |
| 139 | + ); |
| 140 | + return uniqueConstituents; |
| 141 | + } |
| 142 | + cachedTypeMap.set(constituentNodeType, constituentNode); |
| 143 | + return [...uniqueConstituents, constituentNode]; |
| 144 | + }, |
| 145 | + [], |
| 146 | + ); |
| 147 | + } |
| 148 | + function reportDuplicate( |
| 149 | + duplicateConstituent: { |
| 150 | + duplicated: TSESTree.TypeNode; |
| 151 | + duplicatePrevious: TSESTree.TypeNode; |
| 152 | + }, |
| 153 | + parentNode: TSESTree.TSIntersectionType | TSESTree.TSUnionType, |
| 154 | + ): void { |
| 155 | + const sourceCode = context.getSourceCode(); |
| 156 | + const beforeTokens = sourceCode.getTokensBefore( |
| 157 | + duplicateConstituent.duplicated, |
| 158 | + { filter: token => token.value === '|' || token.value === '&' }, |
| 159 | + ); |
| 160 | + const beforeUnionOrIntersectionToken = |
| 161 | + beforeTokens[beforeTokens.length - 1]; |
| 162 | + const bracketBeforeTokens = sourceCode.getTokensBetween( |
| 163 | + beforeUnionOrIntersectionToken, |
| 164 | + duplicateConstituent.duplicated, |
| 165 | + ); |
| 166 | + const bracketAfterTokens = sourceCode.getTokensAfter( |
| 167 | + duplicateConstituent.duplicated, |
| 168 | + { count: bracketBeforeTokens.length }, |
| 169 | + ); |
| 170 | + const reportLocation: TSESTree.SourceLocation = { |
| 171 | + start: duplicateConstituent.duplicated.loc.start, |
| 172 | + end: |
| 173 | + bracketAfterTokens.length > 0 |
| 174 | + ? bracketAfterTokens[bracketAfterTokens.length - 1].loc.end |
| 175 | + : duplicateConstituent.duplicated.loc.end, |
| 176 | + }; |
| 177 | + context.report({ |
| 178 | + data: { |
| 179 | + type: |
| 180 | + parentNode.type === AST_NODE_TYPES.TSIntersectionType |
| 181 | + ? 'Intersection' |
| 182 | + : 'Union', |
| 183 | + previous: sourceCode.getText(duplicateConstituent.duplicatePrevious), |
| 184 | + }, |
| 185 | + messageId: 'duplicate', |
| 186 | + node: duplicateConstituent.duplicated, |
| 187 | + loc: reportLocation, |
| 188 | + fix: fixer => { |
| 189 | + return [ |
| 190 | + beforeUnionOrIntersectionToken, |
| 191 | + ...bracketBeforeTokens, |
| 192 | + duplicateConstituent.duplicated, |
| 193 | + ...bracketAfterTokens, |
| 194 | + ].map(token => fixer.remove(token)); |
| 195 | + }, |
| 196 | + }); |
| 197 | + } |
| 198 | + return { |
| 199 | + ...(!ignoreIntersections && { |
| 200 | + TSIntersectionType: checkDuplicate, |
| 201 | + }), |
| 202 | + ...(!ignoreUnions && { |
| 203 | + TSUnionType: checkDuplicate, |
| 204 | + }), |
| 205 | + }; |
| 206 | + }, |
| 207 | +}); |
0 commit comments