Skip to content

Commit

Permalink
microsoft#48190 on top of v4.3.5
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeagle authored and gregmagolan committed Mar 16, 2022
1 parent 1294ee8 commit 49bbdea
Show file tree
Hide file tree
Showing 6 changed files with 599 additions and 455 deletions.
973 changes: 522 additions & 451 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"@types/mkdirp": "latest",
"@types/mocha": "latest",
"@types/ms": "latest",
"@types/node": "latest",
"@types/node": "14.x.x",
"@types/node-fetch": "^2.3.4",
"@types/q": "latest",
"@types/source-map-support": "latest",
Expand All @@ -61,7 +61,7 @@
"browser-resolve": "^1.11.2",
"browserify": "latest",
"chai": "latest",
"chalk": "latest",
"chalk": "4.1.1",
"convert-source-map": "latest",
"del": "5.1.0",
"diff": "^4.0.2",
Expand Down Expand Up @@ -94,7 +94,7 @@
"remove-internal": "^2.9.2",
"source-map-support": "latest",
"through2": "latest",
"typescript": "^4.2.3",
"typescript": "4.3.5",
"vinyl": "latest",
"vinyl-sourcemaps-apply": "latest",
"xml2js": "^0.4.19"
Expand Down
7 changes: 7 additions & 0 deletions src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,13 @@ namespace ts {
category: Diagnostics.Module_Resolution_Options,
description: Diagnostics.List_of_folders_to_include_type_definitions_from
},
{
name: "resolveFromOutDir",
type: "boolean",
affectsModuleResolution: true,
category: Diagnostics.Module_Resolution_Options,
description: Diagnostics.Allow_resolving_files_relative_to_the_output_directory
},
{
name: "types",
type: "list",
Expand Down
16 changes: 16 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -4331,6 +4331,10 @@
"category": "Message",
"code": 6107
},
"'resolveFromOutDir' option is set, using it to resolve relative module name '{0}'.": {
"category": "Message",
"code": 16107
},
"Longest matching prefix for '{0}' is '{1}'.": {
"category": "Message",
"code": 6108
Expand All @@ -4339,6 +4343,10 @@
"category": "Message",
"code": 6109
},
"Loading '{0}' from the out dir '{1}', candidate location '{2}'.": {
"category": "Message",
"code": 16109
},
"Trying other entries in 'rootDirs'.": {
"category": "Message",
"code": 6110
Expand All @@ -4347,6 +4355,10 @@
"category": "Message",
"code": 6111
},
"Module resolution using 'outDir' has failed.": {
"category": "Message",
"code": 16111
},
"Do not emit 'use strict' directives in module output.": {
"category": "Message",
"code": 6112
Expand Down Expand Up @@ -5086,6 +5098,10 @@
"category": "Message",
"code": 6505
},
"Allow resolving files relative to the output directory.": {
"category": "Message",
"code": 6719
},

"Include 'undefined' in index signature results": {
"category": "Message",
Expand Down
51 changes: 50 additions & 1 deletion src/compiler/moduleNameResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -878,12 +878,13 @@ namespace ts {
type ResolutionKindSpecificLoader = (extensions: Extensions, candidate: string, onlyRecordFailures: boolean, state: ModuleResolutionState) => Resolved | undefined;

/**
* Any module resolution kind can be augmented with optional settings: 'baseUrl', 'paths' and 'rootDirs' - they are used to
* Any module resolution kind can be augmented with optional settings: 'baseUrl', 'resolveFromOutDir', 'paths' and 'rootDirs' - they are used to
* mitigate differences between design time structure of the project and its runtime counterpart so the same import name
* can be resolved successfully by TypeScript compiler and runtime module loader.
* If these settings are set then loading procedure will try to use them to resolve module name and it can of failure it will
* fallback to standard resolution routine.
*
* 'resolveFromOutDir': TODO document the semantics
* - baseUrl - this setting controls how non-relative module names are resolved. If this setting is specified then non-relative
* names will be resolved relative to baseUrl: i.e. if baseUrl is '/a/b' then candidate location to resolve module name 'c/d' will
* be '/a/b/c/d'
Expand Down Expand Up @@ -940,6 +941,9 @@ namespace ts {
function tryLoadModuleUsingOptionalResolutionSettings(extensions: Extensions, moduleName: string, containingDirectory: string, loader: ResolutionKindSpecificLoader,
state: ModuleResolutionState): Resolved | undefined {

const resolvedFromOutDir = tryLoadModuleUsingOutDirIfEligible(extensions, moduleName, containingDirectory, loader, state);
if (resolvedFromOutDir) return resolvedFromOutDir;

const resolved = tryLoadModuleUsingPathsIfEligible(extensions, moduleName, loader, state);
if (resolved) return resolved.value;

Expand All @@ -965,6 +969,51 @@ namespace ts {
}
}

function tryLoadModuleUsingOutDirIfEligible(extensions: Extensions, moduleName: string, containingDirectory: string, loader: ResolutionKindSpecificLoader, state: ModuleResolutionState): Resolved | undefined {
const { baseUrl, resolveFromOutDir, outDir, rootDir } = state.compilerOptions;
if (!resolveFromOutDir) {
return undefined;
}
if (!outDir) {
return undefined;
}
if (state.traceEnabled) {
trace(state.host, Diagnostics.resolveFromOutDir_option_is_set_using_it_to_resolve_relative_module_name_0, moduleName);
}

// COMMENT FOR REVIEWER: Is there a more robust way to determine the base directory here?
var baseDirectory = baseUrl;
if (!baseDirectory && state.host.getCurrentDirectory) {
baseDirectory = state.host.getCurrentDirectory();
}
if (!baseDirectory) {
return undefined;
}

// COMMENT FOR REVIEWER: I've seen rootDir be relative path and and absolute path so
// handling both cases here to come up with an absolute normalizedPrefix path
var normalizedPrefix = rootDir && ts.startsWith(rootDir, ts.directorySeparator) ?
ts.normalizePath(rootDir) :
ts.normalizePath(ts.combinePaths(baseDirectory, rootDir));
var candidate = ts.normalizePath(ts.combinePaths(containingDirectory, moduleName));

// COMMENT FOR REVIEWER: No ts.relativePath() function that I could find. Is there one
// somewhere that I'm not aware of?
var suffix = require("path").relative(normalizedPrefix, candidate);
candidate = ts.normalizePath(ts.combinePaths(baseDirectory, outDir, suffix))
if (state.traceEnabled) {
trace(state.host, Diagnostics.Loading_0_from_the_out_dir_1_candidate_location_2, suffix, outDir, candidate);
}
const resolvedFileName = loader(extensions, candidate, !directoryProbablyExists(containingDirectory, state.host), state);
if (resolvedFileName) {
return resolvedFileName;
}
if (state.traceEnabled) {
trace(state.host, Diagnostics.Module_resolution_using_outDir_has_failed);
}
return undefined;
}

function tryLoadModuleUsingRootDirs(extensions: Extensions, moduleName: string, containingDirectory: string, loader: ResolutionKindSpecificLoader,
state: ModuleResolutionState): Resolved | undefined {

Expand Down
1 change: 1 addition & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5982,6 +5982,7 @@ namespace ts {
project?: string;
/* @internal */ pretty?: boolean;
reactNamespace?: string;
resolveFromOutDir?: boolean;
jsxFactory?: string;
jsxFragmentFactory?: string;
jsxImportSource?: string;
Expand Down

0 comments on commit 49bbdea

Please sign in to comment.