Skip to content

Commit

Permalink
Return empty source file for foreign files (resolves #623)
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed May 7, 2024
1 parent 96f91df commit dc2f508
Show file tree
Hide file tree
Showing 15 changed files with 51 additions and 3 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "other",
"version": "1.0.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "@fixtures/module-resolution-non-std-absolute",
"version": "1.0.0",
"workspaces": ["other", "self"]
}
Empty file.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import 'other/absolute.svg';
import 'other/absolute.css';
import 'self/absolute.svg';
import 'self/absolute.css';
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "self",
"version": "1.0.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"compilerOptions": {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@ import Icon from './icon.svg';

import './global.css';

// By exception, .css files will be resolved because `.css` is not added to virtual path extensions, since `.css.ts`
// files on disk are common (in contrast to e.g. `.svg.ts`; binaries like `.png.ts` can be safely ignored).
import 'styles/base.css';
2 changes: 1 addition & 1 deletion packages/knip/src/typescript/SourceFileManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class SourceFileManager {
if (this.sourceFileCache.has(filePath)) return this.sourceFileCache.get(filePath);
const ext = extname(filePath);
const compiler = this.syncCompilers.get(ext);
if (FOREIGN_FILE_EXTENSIONS.has(ext) && !compiler) return undefined;
if (FOREIGN_FILE_EXTENSIONS.has(ext) && !compiler) return this.createSourceFile(filePath, '');
if (this.isSkipLibs && isInNodeModules(filePath)) {
if (isDeclarationFileExtension(ext)) return undefined;
return this.createSourceFile(filePath, '');
Expand Down
4 changes: 4 additions & 0 deletions packages/knip/src/typescript/resolveModuleNames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ export function createCustomModuleResolver(
});
}

/**
* - Virtual files have built-in or custom compiler, return as JS
* - Foreign files have path resolved verbatim (file manager will return empty source file)
*/
function resolveModuleName(name: string, containingFile: string): ts.ResolvedModuleFull | undefined {
const sanitizedSpecifier = sanitizeSpecifier(name);

Expand Down
24 changes: 24 additions & 0 deletions packages/knip/test/module-resolution-non-std-absolute.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { test } from 'bun:test';
import assert from 'node:assert/strict';
import { main } from '../src/index.js';
import { resolve } from '../src/util/path.js';
import baseArguments from './helpers/baseArguments.js';
import baseCounters from './helpers/baseCounters.js';

const cwd = resolve('fixtures/module-resolution-non-std-absolute');

test('Resolve non-standard absolute specifiers', async () => {
const { issues, counters } = await main({
...baseArguments,
cwd,
});

assert(issues.unlisted['self/index.ts']['other']);

assert.deepEqual(counters, {
...baseCounters,
unlisted: 1,
processed: 5,
total: 5,
});
});

0 comments on commit dc2f508

Please sign in to comment.