|
| 1 | +const path = require('path') |
| 2 | +const BaseChecker = require('../base-checker') |
| 3 | +const { severityDescription } = require('../../doc/utils') |
| 4 | + |
| 5 | +const DEFAULT_SEVERITY = 'warn' |
| 6 | + |
| 7 | +const ruleId = 'duplicated-imports' |
| 8 | +const meta = { |
| 9 | + type: 'miscellaneous', |
| 10 | + |
| 11 | + docs: { |
| 12 | + description: `Check if an import is done twice in the same file and there is no alias`, |
| 13 | + category: 'Style Guide Rules', |
| 14 | + options: [ |
| 15 | + { |
| 16 | + description: severityDescription, |
| 17 | + default: DEFAULT_SEVERITY, |
| 18 | + }, |
| 19 | + ], |
| 20 | + notes: [ |
| 21 | + { |
| 22 | + note: 'Rule reports "(inline) duplicated" if the same object is imported more than once in the same import statement', |
| 23 | + }, |
| 24 | + { |
| 25 | + note: 'Rule reports "(globalSamePath) duplicated" if the same object is imported on another import statement from same location', |
| 26 | + }, |
| 27 | + { |
| 28 | + note: 'Rule reports "(globalDiffPath) duplicated" if the same object is imported on another import statement, from other location, but no alias', |
| 29 | + }, |
| 30 | + { |
| 31 | + note: 'Rule does NOT support this kind of import "import * as Alias from "./filename.sol"', |
| 32 | + }, |
| 33 | + ], |
| 34 | + }, |
| 35 | + |
| 36 | + isDefault: false, |
| 37 | + recommended: false, |
| 38 | + defaultSetup: 'warn', |
| 39 | + fixable: true, |
| 40 | + schema: null, |
| 41 | +} |
| 42 | + |
| 43 | +class DuplicatedImportsChecker extends BaseChecker { |
| 44 | + constructor(reporter) { |
| 45 | + super(reporter, ruleId, meta) |
| 46 | + |
| 47 | + this.imports = [] |
| 48 | + } |
| 49 | + |
| 50 | + ImportDirective(node) { |
| 51 | + const normalizedPath = this.normalizePath(node.path) |
| 52 | + |
| 53 | + const importStatement = { |
| 54 | + path: '', |
| 55 | + objectNames: [], |
| 56 | + } |
| 57 | + |
| 58 | + importStatement.path = normalizedPath |
| 59 | + importStatement.objectNames = node.symbolAliases |
| 60 | + ? node.symbolAliases |
| 61 | + : this.getObjectName(normalizedPath) |
| 62 | + |
| 63 | + this.imports.push(importStatement) |
| 64 | + } |
| 65 | + |
| 66 | + 'SourceUnit:exit'(node) { |
| 67 | + const duplicates = this.findDuplicates(this.imports) |
| 68 | + |
| 69 | + for (let i = 0; i < duplicates.length; i++) { |
| 70 | + this.error(node, `Duplicated Import (${duplicates[i].type}) ${duplicates[i].name}`) |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + getObjectName(normalizedPath) { |
| 75 | + // get file name |
| 76 | + const fileNameWithExtension = path.basename(normalizedPath) |
| 77 | + // Remove extension |
| 78 | + const objectName = fileNameWithExtension.replace('.sol', '') |
| 79 | + return [[objectName, null]] |
| 80 | + } |
| 81 | + |
| 82 | + normalizePath(path) { |
| 83 | + if (path.startsWith('../')) { |
| 84 | + return `./${path}` |
| 85 | + } |
| 86 | + return path |
| 87 | + } |
| 88 | + |
| 89 | + findInlineDuplicates(data) { |
| 90 | + const inlineDuplicates = [] |
| 91 | + |
| 92 | + data.forEach((entry) => { |
| 93 | + const path = entry.path |
| 94 | + // To track object names |
| 95 | + const objectNamesSet = new Set() |
| 96 | + |
| 97 | + entry.objectNames.forEach(([objectName]) => { |
| 98 | + // If object name already been found , it is a duplicated |
| 99 | + if (objectNamesSet.has(objectName)) { |
| 100 | + inlineDuplicates.push({ |
| 101 | + name: objectName, |
| 102 | + type: 'inline', |
| 103 | + paths: [path], |
| 104 | + }) |
| 105 | + } else { |
| 106 | + // If it is not found before, we add it |
| 107 | + objectNamesSet.add(objectName) |
| 108 | + } |
| 109 | + }) |
| 110 | + }) |
| 111 | + |
| 112 | + return inlineDuplicates |
| 113 | + } |
| 114 | + |
| 115 | + finGlobalDuplicatesSamePath(data) { |
| 116 | + const duplicates = [] |
| 117 | + |
| 118 | + // Loop through data |
| 119 | + data.forEach((entry) => { |
| 120 | + const path = entry.path |
| 121 | + |
| 122 | + // Object to track object names on each path |
| 123 | + const objectNamesMap = {} |
| 124 | + |
| 125 | + // Loop through each objectName of current object |
| 126 | + entry.objectNames.forEach(([objectName]) => { |
| 127 | + if (!objectNamesMap[objectName]) { |
| 128 | + objectNamesMap[objectName] = [] |
| 129 | + } |
| 130 | + objectNamesMap[objectName].push(path) |
| 131 | + }) |
| 132 | + |
| 133 | + // Compare this object with the rest to detect duplicates |
| 134 | + data.forEach((otherEntry) => { |
| 135 | + if (otherEntry !== entry) { |
| 136 | + otherEntry.objectNames.forEach(([objectName]) => { |
| 137 | + if ( |
| 138 | + objectNamesMap[objectName] && |
| 139 | + objectNamesMap[objectName].includes(otherEntry.path) |
| 140 | + ) { |
| 141 | + // Add path only if it is not present |
| 142 | + const existingDuplicate = duplicates.find( |
| 143 | + (duplicate) => |
| 144 | + duplicate.name === objectName && |
| 145 | + duplicate.type === 'global' && |
| 146 | + duplicate.paths.includes(entry.path) |
| 147 | + ) |
| 148 | + |
| 149 | + if (!existingDuplicate) { |
| 150 | + duplicates.push({ |
| 151 | + name: objectName, |
| 152 | + type: 'globalSamePath', |
| 153 | + paths: [entry.path], // Just add path once, it is always the same |
| 154 | + }) |
| 155 | + } |
| 156 | + } |
| 157 | + }) |
| 158 | + } |
| 159 | + }) |
| 160 | + }) |
| 161 | + |
| 162 | + return duplicates |
| 163 | + } |
| 164 | + |
| 165 | + finGlobalDuplicatesDiffPathNoAlias(data) { |
| 166 | + const duplicates = [] |
| 167 | + |
| 168 | + // Loop through data |
| 169 | + data.forEach((entry) => { |
| 170 | + // Object to track names on each path |
| 171 | + entry.objectNames.forEach(([objectName, alias]) => { |
| 172 | + // Only compare if there is no alias |
| 173 | + if (!alias) { |
| 174 | + // Go through rest of objects to search for duplicates |
| 175 | + data.forEach((otherEntry) => { |
| 176 | + if (otherEntry !== entry) { |
| 177 | + otherEntry.objectNames.forEach(([otherObjectName, otherAlias]) => { |
| 178 | + // If object name is the same, has no alias and different path |
| 179 | + if ( |
| 180 | + objectName === otherObjectName && |
| 181 | + !otherAlias && |
| 182 | + entry.path !== otherEntry.path |
| 183 | + ) { |
| 184 | + // Check if the name is already in the duplicated array |
| 185 | + const existingDuplicate = duplicates.find( |
| 186 | + (duplicate) => |
| 187 | + duplicate.name === objectName && |
| 188 | + duplicate.type === 'global' && |
| 189 | + duplicate.paths.includes(entry.path) |
| 190 | + ) |
| 191 | + |
| 192 | + // Add new object if doesn't exist |
| 193 | + if (!existingDuplicate) { |
| 194 | + duplicates.push({ |
| 195 | + name: objectName, |
| 196 | + type: 'globalDiffPath', |
| 197 | + paths: [entry.path, otherEntry.path], |
| 198 | + }) |
| 199 | + } |
| 200 | + |
| 201 | + // Add path if already exists |
| 202 | + if (existingDuplicate && !existingDuplicate.paths.includes(otherEntry.path)) { |
| 203 | + existingDuplicate.paths.push(otherEntry.path) |
| 204 | + } |
| 205 | + } |
| 206 | + }) |
| 207 | + } |
| 208 | + }) |
| 209 | + } |
| 210 | + }) |
| 211 | + }) |
| 212 | + |
| 213 | + return duplicates |
| 214 | + } |
| 215 | + |
| 216 | + removeDuplicatedObjects(data) { |
| 217 | + const uniqueData = data.filter((value, index, self) => { |
| 218 | + // Order path arrays to be compared later |
| 219 | + const sortedPaths = value.paths.slice().sort() |
| 220 | + |
| 221 | + return ( |
| 222 | + index === |
| 223 | + self.findIndex( |
| 224 | + (t) => |
| 225 | + t.name === value.name && |
| 226 | + t.type === value.type && |
| 227 | + // Compare ordered arrays of paths |
| 228 | + JSON.stringify(t.paths.slice().sort()) === JSON.stringify(sortedPaths) |
| 229 | + ) |
| 230 | + ) |
| 231 | + }) |
| 232 | + |
| 233 | + return uniqueData |
| 234 | + } |
| 235 | + |
| 236 | + findDuplicates(data) { |
| 237 | + /// @TODO THIS LOGIC CAN BE IMPROVED - Not done due lack of time |
| 238 | + |
| 239 | + const duplicates1 = this.findInlineDuplicates(data) |
| 240 | + |
| 241 | + const duplicates2 = this.finGlobalDuplicatesSamePath(data) |
| 242 | + |
| 243 | + const duplicates3 = this.finGlobalDuplicatesDiffPathNoAlias(data) |
| 244 | + |
| 245 | + const duplicates = this.removeDuplicatedObjects(duplicates1.concat(duplicates2, duplicates3)) |
| 246 | + |
| 247 | + return duplicates |
| 248 | + } |
| 249 | +} |
| 250 | + |
| 251 | +module.exports = DuplicatedImportsChecker |
0 commit comments