Skip to content

Commit

Permalink
Merge pull request #313 from stepankuzmin/skip-unsupported-extensions
Browse files Browse the repository at this point in the history
Fixed issue with unknown extension when using `allowArbitraryExtensions` compiler option
  • Loading branch information
timocov committed Apr 15, 2024
2 parents 0cf1e04 + e6de6ae commit 1a228c4
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/compile-dts.ts
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
@@ -0,0 +1,5 @@
import { TestCaseConfig } from '../../test-cases/test-case-config';

const config: TestCaseConfig = {};

export = config;
@@ -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
@@ -0,0 +1,3 @@
{
"test": 1
}
@@ -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
@@ -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
@@ -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
@@ -0,0 +1,8 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"allowArbitraryExtensions": true,
"esModuleInterop": true,
"resolveJsonModule": true
}
}

0 comments on commit 1a228c4

Please sign in to comment.