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
Auto-imports of declaration files cannot use .ts extension
  • Loading branch information
andrewbranch committed Nov 18, 2022
commit f04f1b5ce2eae4355f4a775de4e6899de910c153
29 changes: 20 additions & 9 deletions src/compiler/moduleSpecifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
ModuleSpecifierResolutionHost, NodeFlags, NodeModulePathParts, normalizePath, Path, pathContainsNodeModules,
pathIsBareSpecifier, pathIsRelative, PropertyAccessExpression, removeFileExtension, removeSuffix, resolvePath,
ScriptKind, some, SourceFile, startsWith, startsWithDirectory, stringContains, StringLiteral, Symbol, SymbolFlags,
toPath, tryGetExtensionFromPath, tryParsePatterns, TypeChecker, UserPreferences, shouldAllowImportingTsExtension, ResolutionMode, ModuleSpecifierEnding, getModuleSpecifierEndingPreference,
toPath, tryGetExtensionFromPath, tryParsePatterns, TypeChecker, UserPreferences, shouldAllowImportingTsExtension, ResolutionMode, ModuleSpecifierEnding, getModuleSpecifierEndingPreference, isDeclarationFileName,
} from "./_namespaces/ts";

// Used by importFixes, getEditsForFileRename, and declaration emit to synthesize import module specifiers.
Expand Down Expand Up @@ -74,10 +74,10 @@ function getPreferences(
if (endsWith(oldImportSpecifier, "/index")) return ModuleSpecifierEnding.Index;
}
return getModuleSpecifierEndingPreference(
importModuleSpecifierEnding,
importingSourceFile.impliedNodeFormat,
compilerOptions,
importingSourceFile);
importModuleSpecifierEnding,
importingSourceFile.impliedNodeFormat,
compilerOptions,
importingSourceFile);
}
}

Expand Down Expand Up @@ -937,10 +937,19 @@ function getPathsRelativeToRootDirs(path: string, rootDirs: readonly string[], g
}

function processEnding(fileName: string, ending: ModuleSpecifierEnding, options: CompilerOptions, host?: ModuleSpecifierResolutionHost): string {
if (fileExtensionIsOneOf(fileName, [Extension.Json, Extension.Mjs, Extension.Cjs])) return fileName;
if (fileExtensionIsOneOf(fileName, [Extension.Json, Extension.Mjs, Extension.Cjs])) {
return fileName;
}

const noExtension = removeFileExtension(fileName);
if (fileName === noExtension) return fileName;
if (fileExtensionIsOneOf(fileName, [Extension.Dmts, Extension.Mts, Extension.Dcts, Extension.Cts])) return noExtension + getJSExtensionForFile(fileName, options);
if (fileName === noExtension) {
return fileName;
}

if (fileExtensionIsOneOf(fileName, [Extension.Dmts, Extension.Mts, Extension.Dcts, Extension.Cts])) {
return noExtension + getJSExtensionForFile(fileName, options);
}

switch (ending) {
case ModuleSpecifierEnding.Minimal:
const withoutIndex = removeSuffix(noExtension, "/index");
Expand All @@ -955,7 +964,9 @@ function processEnding(fileName: string, ending: ModuleSpecifierEnding, options:
case ModuleSpecifierEnding.JsExtension:
return noExtension + getJSExtensionForFile(fileName, options);
case ModuleSpecifierEnding.TsExtension:
return fileName;
// 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;
default:
return Debug.assertNever(ending);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,15 @@ import { Component } from "./Component.tsx";

## From completions

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

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

```ts
import { Component } from "./Component.tsx";
import { fromLocal } from "./local.ts";
Expand Down
22 changes: 22 additions & 0 deletions tests/baselines/reference/autoImportAllowTsExtensions4.baseline.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
```ts
// @Filename: /main.ts
import { Component } from "./local.js";
/*|*/
```

## From completions

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

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

```ts
import { Component, fromLocal } from "./local.js";
fromLocal
```

3 changes: 3 additions & 0 deletions tests/cases/fourslash/autoImportAllowTsExtensions3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
// @Filename: /local.ts
//// export const fromLocal: number;

// @Filename: /decl.d.ts
//// export const fromDecl: number;

// @Filename: /Component.tsx
//// export function Component() { return null; }

Expand Down
20 changes: 20 additions & 0 deletions tests/cases/fourslash/autoImportAllowTsExtensions4.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/// <reference path="fourslash.ts" />

// @moduleResolution: hybrid
// @allowImportingTsExtensions: true
// @noEmit: true

// @Filename: /local.ts
//// export const fromLocal: number;

// @Filename: /decl.d.ts
//// export const fromDecl: number;

// @Filename: /Component.tsx
//// export function Component() { return null; }

// @Filename: /main.ts
//// import { Component } from "./local.js";
//// /**/

verify.baselineAutoImports("");