Skip to content

Commit

Permalink
fix: deepo-import of index files breaks ts3.9 compilers
Browse files Browse the repository at this point in the history
The `typesVersions` directive applies recursively when the TypeScript
compiler searches for the appropriate object to load... When attempting
to resolve a directory, it will first rewrite that directory according
to the typesVersions directive, and then it will rewrite the `index`
sub-path within there again, resulting in two distinct rewrites
happening within the same resolution. This appears to be a TypeScript
bug as reported in microsoft/TypeScript#43133.

The work-around is to include a second rewrite candidate that explicitly
targets the `*/index.d.ts` sub-path, which makes removes the need for a
rewrite when searching down the `index` route.

Causes projen/projen#2570
  • Loading branch information
RomainMuller committed Mar 30, 2023
1 parent a3dc983 commit 40f25ab
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 34 deletions.
1 change: 1 addition & 0 deletions fixtures/@scope/jsii-calc-base/lib/deep/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export class BarrelImportClass {}
2 changes: 2 additions & 0 deletions fixtures/@scope/jsii-calc-base/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ export class StaticConsumer {
return StaticConsumerBase.consume(...args);
}
}

export * as deep from './deep';
12 changes: 11 additions & 1 deletion fixtures/@scope/jsii-calc-base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,20 @@
"typesVersions": {
"<=3.9": {
"*": [
".types-compat/ts3.9/*"
".types-compat/ts3.9/*",
".types-compat/ts3.9/*/index.d.ts"
]
}
},
"exports": {
".": {
"types": "./lib/index.d.ts",
"import": "./lib/index.js",
"require": "./lib/index.js"
},
"./lib/deep": "./lib/deep.js",
"./.warnings.jsii.js": "./.warnings.jsii.js"
},
"dependencies": {
"@scope/jsii-calc-base-of-base": "^2.1.1"
},
Expand Down
4 changes: 4 additions & 0 deletions fixtures/@scope/jsii-calc-lib/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as base from '@scope/jsii-calc-base';
import * as deep from '@scope/jsii-calc-base/lib/deep';
import { Very } from '@scope/jsii-calc-base-of-base';

/**
Expand Down Expand Up @@ -124,6 +125,9 @@ export class BaseFor2647 {

public foo(obj: base.IBaseInterface): void {
obj.bar();

// Just so it's used... no other interest here.
new deep.BarrelImportClass();
}
}

Expand Down
3 changes: 2 additions & 1 deletion fixtures/jsii-calc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"typesVersions": {
"<=3.9": {
"*": [
".types-compat/ts3.9/*"
".types-compat/ts3.9/*",
".types-compat/ts3.9/*/index.d.ts"
]
}
},
Expand Down
3 changes: 2 additions & 1 deletion src/downlevel-dts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ export function emitDownleveledDeclarations({ packageJson, projectRoot, tsc }: P
typesVersions ??= {};
const from = [...(tsc?.outDir != null ? [tsc?.outDir] : []), '*'].join('/');
const to = [...(tsc?.outDir != null ? [tsc?.outDir] : []), TYPES_COMPAT, versionSuffix, '*'].join('/');
typesVersions[`<=${version}`] = { [from]: [to] };
// We put 2 candidate redirects (first match wins), so that it works for nested imports, too (see: https://github.com/microsoft/TypeScript/issues/43133)
typesVersions[`<=${version}`] = { [from]: [to, `${to}/index.d.ts`] };
}

// Compare JSON stringifications, as the order of keys is important here...
Expand Down
90 changes: 59 additions & 31 deletions test/__snapshots__/integration.test.ts.snap

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

Loading

0 comments on commit 40f25ab

Please sign in to comment.