Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issue with unknown extension when using allowArbitraryExtensions compiler option #313

Merged
merged 3 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/compile-dts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface CompileDtsResult {
rootFilesRemapping: Map<string, string>;
}

const declarationExtsRemapping: Record<string, ts.Extension> = {
const declarationExtsRemapping: Partial<Record<string, ts.Extension>> = {
[ts.Extension.Js]: ts.Extension.Js,
[ts.Extension.Jsx]: ts.Extension.Jsx,
[ts.Extension.Json]: ts.Extension.Json,
Expand Down Expand Up @@ -65,6 +65,10 @@ export function compileDts(rootFiles: readonly string[], preferredConfigPath?: s
const resolvedModule = ts.resolveModuleName(moduleLiteral.text, containingFile, compilerOptions, host, moduleResolutionCache).resolvedModule;
if (resolvedModule && !resolvedModule.isExternalLibraryImport) {
const newExt = declarationExtsRemapping[resolvedModule.extension];
if (newExt === undefined) {
verboseLog(`Skipping module ${resolvedModule.resolvedFileName} because it has unsupported extension "${resolvedModule.extension}"`);
return { resolvedModule };
}

// eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison
if (newExt !== resolvedModule.extension) {
Expand Down
5 changes: 5 additions & 0 deletions tests/e2e/test-cases/allow-arbitrary-extensions/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { TestCaseConfig } from '../../test-cases/test-case-config';

const config: TestCaseConfig = {};

export = config;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare const hello: Record<any, any>;

export default hello;
3 changes: 3 additions & 0 deletions tests/e2e/test-cases/allow-arbitrary-extensions/hello.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"test": 1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require('../run-test-case').runTestCase(__dirname);
3 changes: 3 additions & 0 deletions tests/e2e/test-cases/allow-arbitrary-extensions/input.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import hello from './hello.json';

export function test(): typeof hello;
4 changes: 4 additions & 0 deletions tests/e2e/test-cases/allow-arbitrary-extensions/output.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare const hello: Record<any, any>;
export function test(): typeof hello;

export {};
8 changes: 8 additions & 0 deletions tests/e2e/test-cases/allow-arbitrary-extensions/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"allowArbitraryExtensions": true,
"esModuleInterop": true,
"resolveJsonModule": true
}
}