Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
e9f0a1a
WIP
andrewbranch Jul 28, 2022
5f9cf9d
Add extension error back unless noEmit is set
andrewbranch Jul 29, 2022
2421cf5
Add non-relative tests
andrewbranch Jul 29, 2022
660026e
Add error for importing from declaration file
andrewbranch Jul 29, 2022
2daa136
Merge branch 'main' into module-resolution/minimal
andrewbranch Sep 14, 2022
f49a368
Update unit test
andrewbranch Sep 14, 2022
af03675
Add explicit flag for importing from .ts extensions
andrewbranch Sep 14, 2022
496ef99
Add module specifier resolution changes
andrewbranch Sep 15, 2022
458c125
Add auto-import tests
andrewbranch Sep 19, 2022
7d64928
Disallow relative imports into node_modules
andrewbranch Sep 26, 2022
0249e0a
Ensure auto-imports don’t suggest ./node_modules;
andrewbranch Sep 26, 2022
6242ee3
Test a non-portable declaration emit issue
andrewbranch Sep 26, 2022
6cc0e18
Test auto-importing TSX file
andrewbranch Sep 26, 2022
240533a
Update path completions
andrewbranch Sep 26, 2022
eb9a4bb
Merge branch 'main' into module-resolution/minimal
andrewbranch Sep 26, 2022
619843e
Fix lint due to merge
andrewbranch Sep 27, 2022
2ceb4bc
Remove minimal-specific stuff
andrewbranch Oct 13, 2022
aad7fe7
Remove minimal tests
andrewbranch Oct 13, 2022
7fd3e72
Update unit tests
andrewbranch Oct 13, 2022
7cca6f3
Merge branch 'main' into module-resolution/ts-extensions
andrewbranch Oct 14, 2022
ea12012
Merge branch 'main' into module-resolution/ts-extensions
andrewbranch Oct 25, 2022
8655cf9
Merge branch 'main' into module-resolution/ts-extensions
andrewbranch Nov 7, 2022
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
c55c454
Merge branch 'main' into module-resolution/ts-extensions
andrewbranch Nov 8, 2022
870d807
Merge branch 'module-resolution/ts-extensions' into module-resolution…
andrewbranch Nov 8, 2022
6e1989e
Merge branch 'main' into module-resolution/ts-extensions
andrewbranch Nov 15, 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
fa188b1
Revamp string completions ending preferences
andrewbranch Nov 18, 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
c6b963b
Merge branch 'main' into module-resolution/ts-extensions
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
7225671
Merge branch 'main' into module-resolution/ts-extensions
andrewbranch Nov 28, 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
3b1d554
Merge branch 'main' into module-resolution/ts-extensions
andrewbranch Dec 5, 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
5909643
Merge branch 'main' into module-resolution/ts-extensions
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
c3575de
Merge branch 'main' into module-resolution/ts-extensions
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
c4ba4a5
Merge branch 'main' into module-resolution/ts-extensions
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
4e73a46
Merge branch 'main' into module-resolution/ts-extensions
andrewbranch Dec 13, 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
Add first tests
  • Loading branch information
andrewbranch committed Nov 7, 2022
commit 9bc0f07fd2585253d04531ed81f5b1d0991cce08
32 changes: 19 additions & 13 deletions src/compiler/moduleNameResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ namespace ts {
if (resolutionMode === ModuleKind.ESNext && (getEmitModuleResolutionKind(options) === ModuleResolutionKind.Node16 || getEmitModuleResolutionKind(options) === ModuleResolutionKind.NodeNext)) {
features |= NodeResolutionFeatures.EsmMode;
}
const conditions = features & NodeResolutionFeatures.Exports ? features & NodeResolutionFeatures.EsmMode ? ["node", "import", "types"] : ["node", "require", "types"] : [];
const conditions = features & NodeResolutionFeatures.Exports ? getConditions(options, !!(features & NodeResolutionFeatures.EsmMode)) : [];
const diagnostics: Diagnostic[] = [];
const moduleResolutionState: ModuleResolutionState = {
compilerOptions: options,
Expand Down Expand Up @@ -508,10 +508,12 @@ namespace ts {
return features;
}

function getConditions(options: CompilerOptions, esmMode: boolean | undefined) {
function getConditions(options: CompilerOptions, esmMode?: boolean) {
// conditions are only used by the node16/nodenext/hybrid resolvers - there's no priority order in the list,
// it's essentially a set (priority is determined by object insertion order in the object we look at).
const conditions = esmMode ? ["node", "import"] : ["node", "require"];
const conditions = esmMode || getEmitModuleResolutionKind(options) === ModuleResolutionKind.Hybrid
? ["node", "import"]
: ["node", "require"];
if (!options.noDtsResolution) {
conditions.push("types");
}
Expand Down Expand Up @@ -1546,7 +1548,7 @@ namespace ts {
}
}
// esm mode relative imports shouldn't do any directory lookups (either inside `package.json`
// files or implicit `index.js`es). This is a notable depature from cjs norms, where `./foo/pkg`
// files or implicit `index.js`es). This is a notable departure from cjs norms, where `./foo/pkg`
// could have been redirected by `./foo/pkg/package.json` to an arbitrary location!
if (!(state.features & NodeResolutionFeatures.EsmMode)) {
return loadNodeModuleFromDirectory(extensions, candidate, onlyRecordFailures, state, considerPackageJson);
Expand Down Expand Up @@ -1774,25 +1776,29 @@ namespace ts {
let entrypoints: string[] | undefined;
const extensions = resolveJs ? Extensions.JavaScript : Extensions.TypeScript;
const features = getNodeResolutionFeatures(options);
const requireState = getTemporaryModuleResolutionState(cache?.getPackageJsonInfoCache(), host, options);
requireState.conditions = ["node", "require", "types"];
requireState.requestContainingDirectory = packageJsonInfo.packageDirectory;
const requireResolution = loadNodeModuleFromDirectoryWorker(
const loadPackageJsonMainState = getTemporaryModuleResolutionState(cache?.getPackageJsonInfoCache(), host, options);
loadPackageJsonMainState.conditions = getConditions(options);
loadPackageJsonMainState.requestContainingDirectory = packageJsonInfo.packageDirectory;
const mainResolution = loadNodeModuleFromDirectoryWorker(
extensions,
packageJsonInfo.packageDirectory,
/*onlyRecordFailures*/ false,
requireState,
loadPackageJsonMainState,
packageJsonInfo.contents.packageJsonContent,
packageJsonInfo.contents.versionPaths);
entrypoints = append(entrypoints, requireResolution?.path);
entrypoints = append(entrypoints, mainResolution?.path);

if (features & NodeResolutionFeatures.Exports && packageJsonInfo.contents.packageJsonContent.exports) {
for (const conditions of [["node", "import", "types"], ["node", "require", "types"]]) {
const exportState = { ...requireState, failedLookupLocations: [], conditions };
const conditionSets = deduplicate(
[getConditions(options, /*esmMode*/ true), getConditions(options, /*esmMode*/ false)],
arrayIsEqualTo
);
for (const conditions of conditionSets) {
const loadPackageJsonExportsState = { ...loadPackageJsonMainState, failedLookupLocations: [], conditions };
const exportResolutions = loadEntrypointsFromExportMap(
packageJsonInfo,
packageJsonInfo.contents.packageJsonContent.exports,
exportState,
loadPackageJsonExportsState,
extensions);
if (exportResolutions) {
for (const resolution of exportResolutions) {
Expand Down
11 changes: 6 additions & 5 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6441,11 +6441,12 @@ namespace ts {
}

export function getAllowSyntheticDefaultImports(compilerOptions: CompilerOptions) {
const moduleKind = getEmitModuleKind(compilerOptions);
return compilerOptions.allowSyntheticDefaultImports !== undefined
? compilerOptions.allowSyntheticDefaultImports
: getESModuleInterop(compilerOptions) ||
moduleKind === ModuleKind.System;
if (compilerOptions.allowSyntheticDefaultImports !== undefined) {
return compilerOptions.allowSyntheticDefaultImports;
}
return getESModuleInterop(compilerOptions)
|| getEmitModuleKind(compilerOptions) === ModuleKind.System
|| getEmitModuleResolutionKind(compilerOptions) === ModuleResolutionKind.Hybrid;
}

export function moduleResolutionSupportsPackageJsonExportsAndImports(moduleResolution: ModuleResolutionKind): boolean {
Expand Down
41 changes: 41 additions & 0 deletions tests/baselines/reference/hybridRelative1.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
error TS5070: Option '--resolveJsonModule' cannot be specified without 'node' module resolution strategy.
/main.ts(4,16): error TS5097: An import path can only end with a '.ts' extension when 'allowImportingTsExtensions' is enabled.
/main.ts(7,16): error TS2307: Cannot find module './redirect/index' or its corresponding type declarations.


!!! error TS5070: Option '--resolveJsonModule' cannot be specified without 'node' module resolution strategy.
==== /dir/index.ts (0 errors) ====
export const x = 0;

==== /redirect/package.json (0 errors) ====
{ "main": "../foo" }

==== /foo/index.ts (0 errors) ====
export const y = 0;

==== /types/esm.d.ts (0 errors) ====
declare const _: string;
export default _;

==== /types/cjs.d.ts (0 errors) ====
declare const _: string;
export = _;

==== /main.ts (2 errors) ====
import { x } from "./dir";
import {} from "./dir/index";
import {} from "./dir/index.js";
import {} from "./dir/index.ts";
~~~~~~~~~~~~~~~~
!!! error TS5097: An import path can only end with a '.ts' extension when 'allowImportingTsExtensions' is enabled.

import { y } from "./redirect";
import {} from "./redirect/index";
~~~~~~~~~~~~~~~~~~
!!! error TS2307: Cannot find module './redirect/index' or its corresponding type declarations.

import a from "./types/esm";
import * as esm from "./types/esm";
import b from "./types/cjs";
import * as cjs from "./types/cjs";

47 changes: 47 additions & 0 deletions tests/baselines/reference/hybridRelative1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//// [tests/cases/conformance/moduleResolution/hybrid/hybridRelative1.ts] ////

//// [index.ts]
export const x = 0;

//// [package.json]
{ "main": "../foo" }

//// [index.ts]
export const y = 0;

//// [esm.d.ts]
declare const _: string;
export default _;

//// [cjs.d.ts]
declare const _: string;
export = _;

//// [main.ts]
import { x } from "./dir";
import {} from "./dir/index";
import {} from "./dir/index.js";
import {} from "./dir/index.ts";

import { y } from "./redirect";
import {} from "./redirect/index";

import a from "./types/esm";
import * as esm from "./types/esm";
import b from "./types/cjs";
import * as cjs from "./types/cjs";


//// [index.js]
"use strict";
exports.__esModule = true;
exports.x = void 0;
exports.x = 0;
//// [index.js]
"use strict";
exports.__esModule = true;
exports.y = void 0;
exports.y = 0;
//// [main.js]
"use strict";
exports.__esModule = true;
47 changes: 47 additions & 0 deletions tests/baselines/reference/hybridRelative1.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
=== /dir/index.ts ===
export const x = 0;
>x : Symbol(x, Decl(index.ts, 0, 12))

=== /foo/index.ts ===
export const y = 0;
>y : Symbol(y, Decl(index.ts, 0, 12))

=== /types/esm.d.ts ===
declare const _: string;
>_ : Symbol(_, Decl(esm.d.ts, 0, 13))

export default _;
>_ : Symbol(_, Decl(esm.d.ts, 0, 13))

=== /types/cjs.d.ts ===
declare const _: string;
>_ : Symbol(_, Decl(cjs.d.ts, 0, 13))

export = _;
>_ : Symbol(_, Decl(cjs.d.ts, 0, 13))

=== /main.ts ===
import { x } from "./dir";
>x : Symbol(x, Decl(main.ts, 0, 8))

import {} from "./dir/index";
import {} from "./dir/index.js";
import {} from "./dir/index.ts";

import { y } from "./redirect";
>y : Symbol(y, Decl(main.ts, 5, 8))

import {} from "./redirect/index";

import a from "./types/esm";
>a : Symbol(a, Decl(main.ts, 8, 6))

import * as esm from "./types/esm";
>esm : Symbol(esm, Decl(main.ts, 9, 6))

import b from "./types/cjs";
>b : Symbol(b, Decl(main.ts, 10, 6))

import * as cjs from "./types/cjs";
>cjs : Symbol(cjs, Decl(main.ts, 11, 6))

80 changes: 80 additions & 0 deletions tests/baselines/reference/hybridRelative1.trace.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
[
"======== Resolving module './dir' from '/main.ts'. ========",
"Explicitly specified module resolution kind: 'Hybrid'.",
"Loading module as file / folder, candidate module location '/dir', target file type 'TypeScript'.",
"File '/dir.ts' does not exist.",
"File '/dir.tsx' does not exist.",
"File '/dir.d.ts' does not exist.",
"File '/dir/package.json' does not exist.",
"File '/dir/index.ts' exist - use it as a name resolution result.",
"======== Module name './dir' was successfully resolved to '/dir/index.ts'. ========",
"======== Resolving module './dir/index' from '/main.ts'. ========",
"Explicitly specified module resolution kind: 'Hybrid'.",
"Loading module as file / folder, candidate module location '/dir/index', target file type 'TypeScript'.",
"File '/dir/index.ts' exist - use it as a name resolution result.",
"======== Module name './dir/index' was successfully resolved to '/dir/index.ts'. ========",
"======== Resolving module './dir/index.js' from '/main.ts'. ========",
"Explicitly specified module resolution kind: 'Hybrid'.",
"Loading module as file / folder, candidate module location '/dir/index.js', target file type 'TypeScript'.",
"File '/dir/index.js.ts' does not exist.",
"File '/dir/index.js.tsx' does not exist.",
"File '/dir/index.js.d.ts' does not exist.",
"File name '/dir/index.js' has a '.js' extension - stripping it.",
"File '/dir/index.ts' exist - use it as a name resolution result.",
"======== Module name './dir/index.js' was successfully resolved to '/dir/index.ts'. ========",
"======== Resolving module './dir/index.ts' from '/main.ts'. ========",
"Explicitly specified module resolution kind: 'Hybrid'.",
"Loading module as file / folder, candidate module location '/dir/index.ts', target file type 'TypeScript'.",
"File '/dir/index.ts.ts' does not exist.",
"File '/dir/index.ts.tsx' does not exist.",
"File '/dir/index.ts.d.ts' does not exist.",
"File name '/dir/index.ts' has a '.ts' extension - stripping it.",
"File '/dir/index.ts' exist - use it as a name resolution result.",
"======== Module name './dir/index.ts' was successfully resolved to '/dir/index.ts'. ========",
"======== Resolving module './redirect' from '/main.ts'. ========",
"Explicitly specified module resolution kind: 'Hybrid'.",
"Loading module as file / folder, candidate module location '/redirect', target file type 'TypeScript'.",
"File '/redirect.ts' does not exist.",
"File '/redirect.tsx' does not exist.",
"File '/redirect.d.ts' does not exist.",
"Found 'package.json' at '/redirect/package.json'.",
"'package.json' does not have a 'typesVersions' field.",
"'package.json' does not have a 'typings' field.",
"'package.json' does not have a 'types' field.",
"'package.json' has 'main' field '../foo' that references '/foo'.",
"File '/foo' does not exist.",
"Loading module as file / folder, candidate module location '/foo', target file type 'TypeScript'.",
"File '/foo.ts' does not exist.",
"File '/foo.tsx' does not exist.",
"File '/foo.d.ts' does not exist.",
"File '/foo/index.ts' exist - use it as a name resolution result.",
"======== Module name './redirect' was successfully resolved to '/foo/index.ts'. ========",
"======== Resolving module './redirect/index' from '/main.ts'. ========",
"Explicitly specified module resolution kind: 'Hybrid'.",
"Loading module as file / folder, candidate module location '/redirect/index', target file type 'TypeScript'.",
"File '/redirect/index.ts' does not exist.",
"File '/redirect/index.tsx' does not exist.",
"File '/redirect/index.d.ts' does not exist.",
"Directory '/redirect/index' does not exist, skipping all lookups in it.",
"Loading module as file / folder, candidate module location '/redirect/index', target file type 'JavaScript'.",
"File '/redirect/index.js' does not exist.",
"File '/redirect/index.jsx' does not exist.",
"Directory '/redirect/index' does not exist, skipping all lookups in it.",
"Loading module as file / folder, candidate module location '/redirect/index', target file type 'Json'.",
"Directory '/redirect/index' does not exist, skipping all lookups in it.",
"======== Module name './redirect/index' was not resolved. ========",
"======== Resolving module './types/esm' from '/main.ts'. ========",
"Explicitly specified module resolution kind: 'Hybrid'.",
"Loading module as file / folder, candidate module location '/types/esm', target file type 'TypeScript'.",
"File '/types/esm.ts' does not exist.",
"File '/types/esm.tsx' does not exist.",
"File '/types/esm.d.ts' exist - use it as a name resolution result.",
"======== Module name './types/esm' was successfully resolved to '/types/esm.d.ts'. ========",
"======== Resolving module './types/cjs' from '/main.ts'. ========",
"Explicitly specified module resolution kind: 'Hybrid'.",
"Loading module as file / folder, candidate module location '/types/cjs', target file type 'TypeScript'.",
"File '/types/cjs.ts' does not exist.",
"File '/types/cjs.tsx' does not exist.",
"File '/types/cjs.d.ts' exist - use it as a name resolution result.",
"======== Module name './types/cjs' was successfully resolved to '/types/cjs.d.ts'. ========"
]
49 changes: 49 additions & 0 deletions tests/baselines/reference/hybridRelative1.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
=== /dir/index.ts ===
export const x = 0;
>x : 0
>0 : 0

=== /foo/index.ts ===
export const y = 0;
>y : 0
>0 : 0

=== /types/esm.d.ts ===
declare const _: string;
>_ : string

export default _;
>_ : string

=== /types/cjs.d.ts ===
declare const _: string;
>_ : string

export = _;
>_ : string

=== /main.ts ===
import { x } from "./dir";
>x : 0

import {} from "./dir/index";
import {} from "./dir/index.js";
import {} from "./dir/index.ts";

import { y } from "./redirect";
>y : 0

import {} from "./redirect/index";

import a from "./types/esm";
>a : string

import * as esm from "./types/esm";
>esm : typeof esm

import b from "./types/cjs";
>b : string

import * as cjs from "./types/cjs";
>cjs : string

33 changes: 33 additions & 0 deletions tests/cases/conformance/moduleResolution/hybrid/hybridRelative1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// @moduleResolution: hybrid
// @traceResolution: true

// @Filename: /dir/index.ts
export const x = 0;

// @Filename: /redirect/package.json
{ "main": "../foo" }

// @Filename: /foo/index.ts
export const y = 0;

// @Filename: /types/esm.d.ts
declare const _: string;
export default _;

// @Filename: /types/cjs.d.ts
declare const _: string;
export = _;

// @Filename: /main.ts
import { x } from "./dir";
import {} from "./dir/index";
import {} from "./dir/index.js";
import {} from "./dir/index.ts";

import { y } from "./redirect";
import {} from "./redirect/index";

import a from "./types/esm";
import * as esm from "./types/esm";
import b from "./types/cjs";
import * as cjs from "./types/cjs";