Skip to content

Commit

Permalink
Add to UNUSED_EXTERNAL_IMPORT warning information about the origin …
Browse files Browse the repository at this point in the history
…of the problem (#4054)

* feat: add sources to warn

* fix: tests

* fix: semicolon

* refactoring

* style: Add semicolons
  • Loading branch information
cawa-93 committed Apr 27, 2021
1 parent a4a3b8a commit 8c59abe
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
17 changes: 15 additions & 2 deletions src/ExternalModule.ts
Expand Up @@ -110,11 +110,24 @@ export default class ExternalModule {
.map(name => `'${name}'`)
.join(', ')} and '${unused.slice(-1)}' are`;

const importersSet = new Set<string>();
for (const name of unused) {
const {importers, dynamicImporters} = this.declarations[name].module;

if (Array.isArray(importers)) importers.forEach(v => importersSet.add(v));
if (Array.isArray(dynamicImporters)) dynamicImporters.forEach(v => importersSet.add(v));
}

const importersArray = Array.from(importersSet);

const importerList = ' in' + importersArray.map(s => `\n\t${s};`);

this.options.onwarn({
code: 'UNUSED_EXTERNAL_IMPORT',
message: `${names} imported from external module '${this.id}' but never used`,
message: `${names} imported from external module '${this.id}' but never used${importerList}`,
names: unused,
source: this.id
source: this.id,
sources: importersArray
});
}
}
7 changes: 6 additions & 1 deletion test/function/samples/unused-import/_config.js
@@ -1,3 +1,5 @@
const path = require('path');

module.exports = {
description: 'warns on unused imports ([#595])',
options: {
Expand All @@ -13,7 +15,10 @@ module.exports = {
code: 'UNUSED_EXTERNAL_IMPORT',
source: 'external',
names: ['notused', 'neverused'],
message: `'notused' and 'neverused' are imported from external module 'external' but never used`
message: `'notused' and 'neverused' are imported from external module 'external' but never used in\n\t${path.resolve(__dirname, './main.js')};`,
sources: [
path.resolve(__dirname, './main.js')
]
}
]
};

0 comments on commit 8c59abe

Please sign in to comment.