Skip to content

Commit

Permalink
Look up importing module ourselves, don't defer to `ts.LS.findReferen…
Browse files Browse the repository at this point in the history
…ces` (fixes #229)
  • Loading branch information
webpro committed Sep 4, 2023
1 parent 36214cb commit ca95b21
Show file tree
Hide file tree
Showing 14 changed files with 48 additions and 5 deletions.
2 changes: 2 additions & 0 deletions fixtures/re-exports-deep/1-entry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { something } from './2-re-export-star';
something;
1 change: 1 addition & 0 deletions fixtures/re-exports-deep/2-re-export-star.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './3-re-export-named';
1 change: 1 addition & 0 deletions fixtures/re-exports-deep/3-re-export-named.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { something } from './4-re-export-star.js';
1 change: 1 addition & 0 deletions fixtures/re-exports-deep/4-re-export-star.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './5-re-export-named';
1 change: 1 addition & 0 deletions fixtures/re-exports-deep/5-re-export-named.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { something } from './6-re-export-star';
1 change: 1 addition & 0 deletions fixtures/re-exports-deep/6-re-export-star.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './7-my-module';
1 change: 1 addition & 0 deletions fixtures/re-exports-deep/7-my-module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const something = {};
11 changes: 11 additions & 0 deletions fixtures/re-exports-deep/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "@fixtures/re-exports-deep",
"knip": {
"entry": [
"1-entry.ts"
],
"project": [
"*.ts"
]
}
}
2 changes: 2 additions & 0 deletions fixtures/re-exports/1-entry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { something } from './2-re-export-star.js';
something;
1 change: 1 addition & 0 deletions fixtures/re-exports/2-re-export-star.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './3-re-export-named.js';
1 change: 1 addition & 0 deletions fixtures/re-exports/3-re-export-named.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { something } from './4-my-module';
1 change: 1 addition & 0 deletions fixtures/re-exports/4-my-module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const something = {};
11 changes: 11 additions & 0 deletions fixtures/re-exports/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "@fixtures/re-exports",
"knip": {
"entry": [
"1-entry.ts"
],
"project": [
"*.ts"
]
}
}
18 changes: 13 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,14 @@ export const main = async (unresolvedConfiguration: CommandLineOptions) => {

collector.addFileCounts({ processed: analyzedFiles.size, unused: unusedFiles.length });

const isSymbolImported = (symbol: string, importingModule?: ImportedModule): boolean => {
if (!importingModule) return false;
if (importingModule.symbols.has(symbol)) return true;
const { isReExport, isReExportedBy } = importingModule;
const hasSymbol = (file: string) => isSymbolImported(symbol, importedSymbols.get(file));
return isReExport ? Array.from(isReExportedBy).some(hasSymbol) : false;
};

const isExportedInEntryFile = (importedModule?: ImportedModule): boolean => {
if (!importedModule) return false;
const { isReExport, isReExportedBy } = importedModule;
Expand All @@ -354,7 +362,7 @@ export const main = async (unresolvedConfiguration: CommandLineOptions) => {
// Bail out when in entry file (unless --include-entry-exports)
if (!isIncludeEntryExports && principal.entryPaths.has(filePath)) continue;

const importedModule = importedSymbols.get(filePath);
const importingModule = importedSymbols.get(filePath);

for (const [symbol, exportedItem] of exportItems.entries()) {
const jsDocTags = principal.getJSDocTags(exportedItem);
Expand All @@ -365,9 +373,9 @@ export const main = async (unresolvedConfiguration: CommandLineOptions) => {
// Skip exports tagged `@internal` in --production --ignore-internal mode
if (isIgnoreInternal && jsDocTags.includes('@internal')) continue;

if (importedModule?.symbols.has(symbol)) {
if (importingModule && isSymbolImported(symbol, importingModule)) {
// Skip members of classes/enums that are eventually exported by entry files
if (importedModule.isReExport && isExportedInEntryFile(importedModule)) continue;
if (importingModule.isReExport && isExportedInEntryFile(importingModule)) continue;

if (report.enumMembers && exportedItem.type === 'enum' && exportedItem.members) {
if (isProduction) continue;
Expand All @@ -386,8 +394,8 @@ export const main = async (unresolvedConfiguration: CommandLineOptions) => {
continue;
}

const isStar = Boolean(importedModule?.isStar);
const isReExportedByEntryFile = !isIncludeEntryExports && isStar && isExportedInEntryFile(importedModule);
const isStar = Boolean(importingModule?.isStar);
const isReExportedByEntryFile = !isIncludeEntryExports && isStar && isExportedInEntryFile(importingModule);

if (!isReExportedByEntryFile && !isExportedItemReferenced(exportedItem, filePath)) {
if (['enum', 'type', 'interface'].includes(exportedItem.type)) {
Expand Down

0 comments on commit ca95b21

Please sign in to comment.