Skip to content

Commit 7cf7c12

Browse files
committed
chore(eslint): remove unused console
1 parent 1c24d5c commit 7cf7c12

File tree

1 file changed

+95
-99
lines changed

1 file changed

+95
-99
lines changed

src/rules/no-wildcard-imports.js

Lines changed: 95 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -239,133 +239,129 @@ module.exports = {
239239
create(context) {
240240
return {
241241
ImportDeclaration(node) {
242-
try {
243-
if (!node.source.value.startsWith('@primer/react/lib-esm')) {
244-
return
245-
}
242+
if (!node.source.value.startsWith('@primer/react/lib-esm')) {
243+
return
244+
}
245+
246+
const wildcardImportMigrations = wildcardImports.get(node.source.value)
247+
if (!wildcardImportMigrations) {
248+
context.report({
249+
node,
250+
messageId: 'unknownWildcardImport',
251+
})
252+
return
253+
}
254+
255+
/**
256+
* Maps entrypoint to array of changes. This tuple contains the new
257+
* imported name from the entrypoint along with the existing local name
258+
* @type {Map<string, Array<[string, string]>>}
259+
*/
260+
const changes = new Map()
246261

247-
const wildcardImportMigrations = wildcardImports.get(node.source.value)
248-
if (!wildcardImportMigrations) {
262+
for (const specifier of node.specifiers) {
263+
const migration = wildcardImportMigrations.find(migration => {
264+
if (specifier.type === 'ImportDefaultSpecifier') {
265+
return migration.name === 'default'
266+
}
267+
return specifier.imported.name === migration.name
268+
})
269+
270+
// If we do not have a migration, we should report an error even if we
271+
// cannot autofix it
272+
if (!migration) {
249273
context.report({
250274
node,
251275
messageId: 'unknownWildcardImport',
252276
})
253277
return
254278
}
255279

256-
/**
257-
* Maps entrypoint to array of changes. This tuple contains the new
258-
* imported name from the entrypoint along with the existing local name
259-
* @type {Map<string, Array<[string, string]>>}
260-
*/
261-
const changes = new Map()
262-
263-
for (const specifier of node.specifiers) {
264-
const migration = wildcardImportMigrations.find(migration => {
265-
if (specifier.type === 'ImportDefaultSpecifier') {
266-
return migration.name === 'default'
267-
}
268-
return specifier.imported.name === migration.name
269-
})
270-
271-
// If we do not have a migration, we should report an error even if we
272-
// cannot autofix it
273-
if (!migration) {
274-
context.report({
275-
node,
276-
messageId: 'unknownWildcardImport',
277-
})
278-
return
279-
}
280-
281-
if (!changes.has(migration.from)) {
282-
changes.set(migration.from, [])
283-
}
284-
285-
if (migration.as) {
286-
changes.get(migration.from).push([migration.as, migration.as, migration.type])
287-
} else {
288-
changes.get(migration.from).push([migration.name, specifier.local.name, migration.type])
289-
}
280+
if (!changes.has(migration.from)) {
281+
changes.set(migration.from, [])
290282
}
291283

292-
if (changes.length === 0) {
293-
return
284+
if (migration.as) {
285+
changes.get(migration.from).push([migration.as, migration.as, migration.type])
286+
} else {
287+
changes.get(migration.from).push([migration.name, specifier.local.name, migration.type])
294288
}
289+
}
295290

296-
context.report({
297-
node,
298-
messageId: 'wildcardMigration',
299-
data: {
300-
wildcardEntrypoint: node.source.value,
301-
},
302-
*fix(fixer) {
303-
for (const [entrypoint, importSpecifiers] of changes) {
304-
const typeSpecifiers = importSpecifiers.filter(([, , type]) => {
305-
return type === 'type'
291+
if (changes.length === 0) {
292+
return
293+
}
294+
295+
context.report({
296+
node,
297+
messageId: 'wildcardMigration',
298+
data: {
299+
wildcardEntrypoint: node.source.value,
300+
},
301+
*fix(fixer) {
302+
for (const [entrypoint, importSpecifiers] of changes) {
303+
const typeSpecifiers = importSpecifiers.filter(([, , type]) => {
304+
return type === 'type'
305+
})
306+
307+
// If all imports are type imports, emit emit as `import type {specifier} from '...'`
308+
if (typeSpecifiers.length === importSpecifiers.length) {
309+
const namedSpecifiers = typeSpecifiers.filter(([imported]) => {
310+
return imported !== 'default'
311+
})
312+
const defaultSpecifier = typeSpecifiers.find(([imported]) => {
313+
return imported === 'default'
306314
})
307315

308-
// If all imports are type imports, emit emit as `import type {specifier} from '...'`
309-
if (typeSpecifiers.length === importSpecifiers.length) {
310-
const namedSpecifiers = typeSpecifiers.filter(([imported]) => {
311-
return imported !== 'default'
312-
})
313-
const defaultSpecifier = typeSpecifiers.find(([imported]) => {
314-
return imported === 'default'
316+
if (namedSpecifiers.length > 0 && !defaultSpecifier) {
317+
const specifiers = namedSpecifiers.map(([imported, local]) => {
318+
if (imported !== local) {
319+
return `${imported} as ${local}`
320+
}
321+
return imported
315322
})
323+
yield fixer.replaceText(node, `import type {${specifiers.join(', ')}} from '${entrypoint}'`)
324+
} else if (namedSpecifiers.length > 0 && defaultSpecifier) {
325+
yield fixer.replaceText(
326+
node,
327+
`import type ${defaultSpecifier[1]}, ${specifiers.join(', ')} from '${entrypoint}'`,
328+
)
329+
} else if (defaultSpecifier && namedSpecifiers.length === 0) {
330+
yield fixer.replaceText(node, `import type ${defaultSpecifier[1]} from '${entrypoint}'`)
331+
}
316332

317-
if (namedSpecifiers.length > 0 && !defaultSpecifier) {
318-
const specifiers = namedSpecifiers.map(([imported, local]) => {
319-
if (imported !== local) {
320-
return `${imported} as ${local}`
321-
}
322-
return imported
323-
})
324-
yield fixer.replaceText(node, `import type {${specifiers.join(', ')}} from '${entrypoint}'`)
325-
} else if (namedSpecifiers.length > 0 && defaultSpecifier) {
326-
yield fixer.replaceText(
327-
node,
328-
`import type ${defaultSpecifier[1]}, ${specifiers.join(', ')} from '${entrypoint}'`,
329-
)
330-
} else if (defaultSpecifier && namedSpecifiers.length === 0) {
331-
yield fixer.replaceText(node, `import type ${defaultSpecifier[1]} from '${entrypoint}'`)
332-
}
333+
return
334+
}
333335

334-
return
335-
}
336+
// Otherwise, we have a mix of type and value imports to emit
337+
const valueSpecifiers = importSpecifiers.filter(([, , type]) => {
338+
return type !== 'type'
339+
})
336340

337-
// Otherwise, we have a mix of type and value imports to emit
338-
const valueSpecifiers = importSpecifiers.filter(([, , type]) => {
339-
return type !== 'type'
340-
})
341+
if (valueSpecifiers.length === 0) {
342+
return
343+
}
341344

342-
if (valueSpecifiers.length === 0) {
343-
return
345+
const specifiers = valueSpecifiers.map(([imported, local]) => {
346+
if (imported !== local) {
347+
return `${imported} as ${local}`
344348
}
349+
return imported
350+
})
351+
yield fixer.replaceText(node, `import {${specifiers.join(', ')}} from '${entrypoint}'`)
345352

353+
if (typeSpecifiers.length > 0) {
346354
const specifiers = valueSpecifiers.map(([imported, local]) => {
347355
if (imported !== local) {
348356
return `${imported} as ${local}`
349357
}
350358
return imported
351359
})
352-
yield fixer.replaceText(node, `import {${specifiers.join(', ')}} from '${entrypoint}'`)
353-
354-
if (typeSpecifiers.length > 0) {
355-
const specifiers = valueSpecifiers.map(([imported, local]) => {
356-
if (imported !== local) {
357-
return `${imported} as ${local}`
358-
}
359-
return imported
360-
})
361-
yield fixer.insertTextAfter(node, `import type {${specifiers.join(', ')}} from '${entrypoint}'`)
362-
}
360+
yield fixer.insertTextAfter(node, `import type {${specifiers.join(', ')}} from '${entrypoint}'`)
363361
}
364-
},
365-
})
366-
} catch (error) {
367-
console.log(error)
368-
}
362+
}
363+
},
364+
})
369365
},
370366
}
371367
},

0 commit comments

Comments
 (0)