Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
86dd5da
Add options
andrewbranch Oct 20, 2022
8846f31
Add customConditions option
andrewbranch Oct 21, 2022
9bc0f07
Add first tests
andrewbranch Oct 24, 2022
4545afb
CJS constructs are not allowed
andrewbranch Oct 24, 2022
e82e61d
Add another test
andrewbranch Oct 24, 2022
038783d
Fix extension adding/replacing priority
andrewbranch Oct 25, 2022
870d807
Merge branch 'module-resolution/ts-extensions' into module-resolution…
andrewbranch Nov 8, 2022
a482180
Merge branch 'module-resolution/ts-extensions' into module-resolution…
andrewbranch Nov 15, 2022
89bcd01
Update test to reflect the choice not to block on unrecognized extens…
andrewbranch Nov 16, 2022
6074661
Add auto-imports and string completions tests
andrewbranch Nov 17, 2022
9e03db2
Merge branch 'module-resolution/ts-extensions' into module-resolution…
andrewbranch Nov 18, 2022
c4320a2
Comment test
andrewbranch Nov 18, 2022
f04f1b5
Auto-imports of declaration files cannot use .ts extension
andrewbranch Nov 18, 2022
29039de
Have declaration file auto imports default to extensionless instead
andrewbranch Nov 18, 2022
e83ca2a
Merge branch 'module-resolution/ts-extensions' into module-resolution…
andrewbranch Nov 18, 2022
f98730a
Add test for custom conditions
andrewbranch Nov 18, 2022
5a01dd6
Fix indentation
andrewbranch Nov 18, 2022
4dd6d01
Add baseline showing resolvePackageJsonImports/Exports compatibility
andrewbranch Nov 18, 2022
81b9e70
Merge branch 'module-resolution/ts-extensions' into module-resolution…
andrewbranch Nov 28, 2022
f6fa5f3
Fix test and prevent CJS require from resolving
andrewbranch Nov 28, 2022
aeb23be
Update unit test baselines
andrewbranch Nov 28, 2022
e0c0375
Merge branch 'module-resolution/ts-extensions' into module-resolution…
andrewbranch Dec 5, 2022
f9414aa
Fix bad merge conflict resolution
andrewbranch Dec 5, 2022
dadc1a8
Make resolvedUsingTsExtension optional
andrewbranch Dec 5, 2022
5089aab
Merge branch 'module-resolution/ts-extensions' into module-resolution…
andrewbranch Dec 5, 2022
a88b8ad
Update missed baselines
andrewbranch Dec 6, 2022
224bb10
Merge branch 'module-resolution/ts-extensions' into module-resolution…
andrewbranch Dec 6, 2022
b2e283c
Revert now-unnecessary API implementation changes
andrewbranch Dec 6, 2022
30afa86
Clean up
andrewbranch Dec 9, 2022
0baca5b
Merge branch 'module-resolution/ts-extensions' into module-resolution…
andrewbranch Dec 9, 2022
6e763d9
Update baselines to es5 emit
andrewbranch Dec 9, 2022
22fbb44
Merge branch 'module-resolution/ts-extensions' into module-resolution…
andrewbranch Dec 13, 2022
d4a3b3c
Rename to `bundler`
andrewbranch Dec 13, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Have declaration file auto imports default to extensionless instead
  • Loading branch information
andrewbranch committed Nov 18, 2022
commit 29039dec4e8eb101e09070a33d6fe2b45111b248
31 changes: 19 additions & 12 deletions src/compiler/moduleSpecifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function getPreferences(
}
switch (preferredEnding) {
case ModuleSpecifierEnding.JsExtension: return [ModuleSpecifierEnding.JsExtension, ModuleSpecifierEnding.Minimal, ModuleSpecifierEnding.Index];
case ModuleSpecifierEnding.TsExtension: return [ModuleSpecifierEnding.TsExtension, ModuleSpecifierEnding.TsExtension, ModuleSpecifierEnding.Minimal, ModuleSpecifierEnding.Index];
case ModuleSpecifierEnding.TsExtension: return [ModuleSpecifierEnding.TsExtension, ModuleSpecifierEnding.Minimal, ModuleSpecifierEnding.JsExtension, ModuleSpecifierEnding.Index];
case ModuleSpecifierEnding.Index: return [ModuleSpecifierEnding.Index, ModuleSpecifierEnding.Minimal, ModuleSpecifierEnding.JsExtension];
case ModuleSpecifierEnding.Minimal: return [ModuleSpecifierEnding.Minimal, ModuleSpecifierEnding.Index, ModuleSpecifierEnding.JsExtension];
default: Debug.assertNever(preferredEnding);
Expand Down Expand Up @@ -367,7 +367,7 @@ function getLocalModuleSpecifier(moduleFileName: string, info: Info, compilerOpt
return fromPaths;
}

const maybeNonRelative = fromPaths === undefined && baseUrl !== undefined ? processEnding(relativeToBaseUrl, ending, compilerOptions) : fromPaths;
const maybeNonRelative = fromPaths === undefined && baseUrl !== undefined ? processEnding(relativeToBaseUrl, allowedEndings, compilerOptions) : fromPaths;
if (!maybeNonRelative) {
return relativePath;
}
Expand Down Expand Up @@ -655,7 +655,7 @@ function tryGetModuleNameFromPaths(relativeToBaseUrl: string, paths: MapLike<rea
// sorted among the others for a particular value of `importModuleSpecifierEnding`.
const candidates: { ending: ModuleSpecifierEnding | undefined, value: string }[] = allowedEndings.map(ending => ({
ending,
value: processEnding(relativeToBaseUrl, ending, compilerOptions)
value: processEnding(relativeToBaseUrl, [ending], compilerOptions)
}));
if (tryGetExtensionFromPath(pattern)) {
candidates.push({ ending: undefined, value: relativeToBaseUrl });
Expand Down Expand Up @@ -692,7 +692,7 @@ function tryGetModuleNameFromPaths(relativeToBaseUrl: string, paths: MapLike<rea
// `ModuleSpecifierEnding.Index` result, which should already be in the list of candidates if `Minimal` was. (Note: the assumption here is
// that every module resolution mode that supports dropping extensions also supports dropping `/index`. Like literally
// everything else in this file, this logic needs to be updated if that's not true in some future module resolution mode.)
return ending !== ModuleSpecifierEnding.Minimal || value === processEnding(relativeToBaseUrl, ending, compilerOptions, host);
return ending !== ModuleSpecifierEnding.Minimal || value === processEnding(relativeToBaseUrl, [ending], compilerOptions, host);
}
}

Expand Down Expand Up @@ -767,7 +767,7 @@ function tryGetModuleNameFromExports(options: CompilerOptions, targetFilePath: s
return undefined;
}

function tryGetModuleNameFromRootDirs(rootDirs: readonly string[], moduleFileName: string, sourceDirectory: string, getCanonicalFileName: (file: string) => string, ending: ModuleSpecifierEnding, compilerOptions: CompilerOptions): string | undefined {
function tryGetModuleNameFromRootDirs(rootDirs: readonly string[], moduleFileName: string, sourceDirectory: string, getCanonicalFileName: (file: string) => string, allowedEndings: readonly ModuleSpecifierEnding[], compilerOptions: CompilerOptions): string | undefined {
const normalizedTargetPaths = getPathsRelativeToRootDirs(moduleFileName, rootDirs, getCanonicalFileName);
if (normalizedTargetPaths === undefined) {
return undefined;
Expand All @@ -781,7 +781,7 @@ function tryGetModuleNameFromRootDirs(rootDirs: readonly string[], moduleFileNam
if (!shortest) {
return undefined;
}
return processEnding(shortest, ending, compilerOptions);
return processEnding(shortest, allowedEndings, compilerOptions);
}

function tryGetModuleNameAsNodeModule({ path, isRedirect }: ModulePath, { getCanonicalFileName, sourceDirectory }: Info, importingSourceFile: SourceFile, host: ModuleSpecifierResolutionHost, options: CompilerOptions, userPreferences: UserPreferences, packageNameOnly?: boolean, overrideMode?: ResolutionMode): string | undefined {
Expand Down Expand Up @@ -823,7 +823,7 @@ function tryGetModuleNameAsNodeModule({ path, isRedirect }: ModulePath, { getCan
// try with next level of directory
packageRootIndex = path.indexOf(directorySeparator, packageRootIndex + 1);
if (packageRootIndex === -1) {
moduleSpecifier = processEnding(moduleFileName, allowedEndings[0], options, host);
moduleSpecifier = processEnding(moduleFileName, allowedEndings, options, host);
break;
}
}
Expand Down Expand Up @@ -936,7 +936,7 @@ function getPathsRelativeToRootDirs(path: string, rootDirs: readonly string[], g
});
}

function processEnding(fileName: string, ending: ModuleSpecifierEnding, options: CompilerOptions, host?: ModuleSpecifierResolutionHost): string {
function processEnding(fileName: string, allowedEndings: readonly ModuleSpecifierEnding[], options: CompilerOptions, host?: ModuleSpecifierResolutionHost): string {
if (fileExtensionIsOneOf(fileName, [Extension.Json, Extension.Mjs, Extension.Cjs])) {
return fileName;
}
Expand All @@ -950,7 +950,7 @@ function processEnding(fileName: string, ending: ModuleSpecifierEnding, options:
return noExtension + getJSExtensionForFile(fileName, options);
}

switch (ending) {
switch (allowedEndings[0]) {
case ModuleSpecifierEnding.Minimal:
const withoutIndex = removeSuffix(noExtension, "/index");
if (host && withoutIndex !== noExtension && tryGetAnyFileFromPath(host, withoutIndex)) {
Expand All @@ -965,10 +965,17 @@ function processEnding(fileName: string, ending: ModuleSpecifierEnding, options:
return noExtension + getJSExtensionForFile(fileName, options);
case ModuleSpecifierEnding.TsExtension:
// For now, we don't know if this import is going to be type-only, which means we don't
// know if a .d.ts extension is valid, so use the .js extension.
return isDeclarationFileName(fileName) ? noExtension + getJSExtensionForFile(fileName, options) : fileName;
// know if a .d.ts extension is valid, so use no extension or a .js extension
if (isDeclarationFileName(fileName)) {
const extensionlessPriority = allowedEndings.findIndex(e => e === ModuleSpecifierEnding.Minimal || e === ModuleSpecifierEnding.Index);
const jsPriority = allowedEndings.indexOf(ModuleSpecifierEnding.JsExtension);
return extensionlessPriority !== -1 && extensionlessPriority < jsPriority
? noExtension
: noExtension + getJSExtensionForFile(fileName, options);
}
return fileName;
default:
return Debug.assertNever(ending);
return Debug.assertNever(allowedEndings[0]);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import { Component } from "./Component.tsx";

## From completions

- `fromDecl` from `"./decl.js"`
- `fromDecl` from `"./decl"`
- `fromLocal` from `"./local.ts"`

```ts
import { Component } from "./Component.tsx";
import { fromDecl } from "./decl.js";
import { fromDecl } from "./decl";
fromDecl
```

Expand Down