Skip to content

Commit d2fd643

Browse files
committed
initial implementation of path mapping based module resolution
1 parent bd84b84 commit d2fd643

File tree

90 files changed

+3391
-49
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+3391
-49
lines changed

src/compiler/commandLineParser.ts

+55-26
Original file line numberDiff line numberDiff line change
@@ -250,10 +250,12 @@ namespace ts {
250250
name: "moduleResolution",
251251
type: {
252252
"node": ModuleResolutionKind.NodeJs,
253-
"classic": ModuleResolutionKind.Classic
253+
"classic": ModuleResolutionKind.Classic,
254+
// name is lowercased so we can still use hasProperty(userValue.toLower()) to check if user has entered the right value
255+
"baseurl": ModuleResolutionKind.BaseUrl,
254256
},
255257
description: Diagnostics.Specifies_module_resolution_strategy_Colon_node_Node_js_or_classic_TypeScript_pre_1_6,
256-
error: Diagnostics.Argument_for_moduleResolution_option_must_be_node_or_classic,
258+
error: Diagnostics.Argument_for_moduleResolution_option_must_be_node_classic_or_baseUrl,
257259
},
258260
{
259261
name: "allowUnusedLabels",
@@ -279,6 +281,26 @@ namespace ts {
279281
name: "forceConsistentCasingInFileNames",
280282
type: "boolean",
281283
description: Diagnostics.Disallow_inconsistently_cased_references_to_the_same_file
284+
},
285+
{
286+
name: "baseUrl",
287+
type: "string",
288+
isFilePath: true,
289+
description: Diagnostics.Base_directory_to_resolve_relative_module_names
290+
},
291+
{
292+
// this option can only be specified in tsconfig.json
293+
// use type = object to copy the value as-is
294+
name: "paths",
295+
type: "object",
296+
isTSConfigOnly: true
297+
},
298+
{
299+
// this option can only be specified in tsconfig.json
300+
// use type = object to copy the value as-is
301+
name: "rootDirs",
302+
type: "object",
303+
isTSConfigOnly: true
282304
}
283305
];
284306

@@ -339,31 +361,36 @@ namespace ts {
339361
if (hasProperty(optionNameMap, s)) {
340362
const opt = optionNameMap[s];
341363

342-
// Check to see if no argument was provided (e.g. "--locale" is the last command-line argument).
343-
if (!args[i] && opt.type !== "boolean") {
344-
errors.push(createCompilerDiagnostic(Diagnostics.Compiler_option_0_expects_an_argument, opt.name));
364+
if (opt.isTSConfigOnly) {
365+
errors.push(createCompilerDiagnostic(Diagnostics.Option_0_can_only_be_specified_in_tsconfig_json_file, opt.name));
345366
}
367+
else {
368+
// Check to see if no argument was provided (e.g. "--locale" is the last command-line argument).
369+
if (!args[i] && opt.type !== "boolean") {
370+
errors.push(createCompilerDiagnostic(Diagnostics.Compiler_option_0_expects_an_argument, opt.name));
371+
}
346372

347-
switch (opt.type) {
348-
case "number":
349-
options[opt.name] = parseInt(args[i++]);
350-
break;
351-
case "boolean":
352-
options[opt.name] = true;
353-
break;
354-
case "string":
355-
options[opt.name] = args[i++] || "";
356-
break;
357-
// If not a primitive, the possible types are specified in what is effectively a map of options.
358-
default:
359-
let map = <Map<number>>opt.type;
360-
let key = (args[i++] || "").toLowerCase();
361-
if (hasProperty(map, key)) {
362-
options[opt.name] = map[key];
363-
}
364-
else {
365-
errors.push(createCompilerDiagnostic((<CommandLineOptionOfCustomType>opt).error));
366-
}
373+
switch (opt.type) {
374+
case "number":
375+
options[opt.name] = parseInt(args[i++]);
376+
break;
377+
case "boolean":
378+
options[opt.name] = true;
379+
break;
380+
case "string":
381+
options[opt.name] = args[i++] || "";
382+
break;
383+
// If not a primitive, the possible types are specified in what is effectively a map of options.
384+
default:
385+
let map = <Map<number>>opt.type;
386+
let key = (args[i++] || "").toLowerCase();
387+
if (hasProperty(map, key)) {
388+
options[opt.name] = map[key];
389+
}
390+
else {
391+
errors.push(createCompilerDiagnostic((<CommandLineOptionOfCustomType>opt).error));
392+
}
393+
}
367394
}
368395
}
369396
else {
@@ -466,7 +493,6 @@ namespace ts {
466493
return output;
467494
}
468495

469-
470496
/**
471497
* Parse the contents of a config file (tsconfig.json).
472498
* @param json The contents of the config file to parse
@@ -477,6 +503,9 @@ namespace ts {
477503
export function parseJsonConfigFileContent(json: any, host: ParseConfigHost, basePath: string): ParsedCommandLine {
478504
const { options, errors } = convertCompilerOptionsFromJson(json["compilerOptions"], basePath);
479505

506+
// set basePath as inferredBaseUrl so baseUrl module resolution strategy can still work even if user have not specified baseUrl explicity
507+
options.inferredBaseUrl = basePath;
508+
480509
return {
481510
options,
482511
fileNames: getFileNames(),

src/compiler/diagnosticMessages.json

+29-2
Original file line numberDiff line numberDiff line change
@@ -2060,7 +2060,26 @@
20602060
"category": "Error",
20612061
"code": 5054
20622062
},
2063-
2063+
"Option '{0}' can only be used when option 'moduleResolution' is 'baseUrl'.": {
2064+
"category": "Error",
2065+
"code": 5055
2066+
},
2067+
"Pattern '{0}' can have at most one '*' character": {
2068+
"category": "Error",
2069+
"code": 5056
2070+
},
2071+
"Substitution '{0}' in pattern '{1}' in can have at most one '*' character": {
2072+
"category": "Error",
2073+
"code": 5057
2074+
},
2075+
"'moduleResolution' kind 'baseUrl' cannot be used without specifying '--baseUrl' option.": {
2076+
"category": "Error",
2077+
"code": 5058
2078+
},
2079+
"Module resolution kind cannot be determined automatically. Please specify module resolution explicitly via 'moduleResolution' option.": {
2080+
"category": "Error",
2081+
"code": 5059
2082+
},
20642083
"Concatenate and emit output to single file.": {
20652084
"category": "Message",
20662085
"code": 6001
@@ -2253,10 +2272,14 @@
22532272
"category": "Error",
22542273
"code": 6062
22552274
},
2256-
"Argument for '--moduleResolution' option must be 'node' or 'classic'.": {
2275+
"Argument for '--moduleResolution' option must be 'node', 'classic' or 'baseUrl'.": {
22572276
"category": "Error",
22582277
"code": 6063
22592278
},
2279+
"Option '{0}' can only be specified in 'tsconfig.json' file.": {
2280+
"category": "Error",
2281+
"code": 6064
2282+
},
22602283

22612284
"Enables experimental support for ES7 decorators.": {
22622285
"category": "Message",
@@ -2322,6 +2345,10 @@
23222345
"category": "Error",
23232346
"code": 6082
23242347
},
2348+
"Base directory to resolve relative module names.": {
2349+
"category": "Message",
2350+
"code": 6083
2351+
},
23252352
"Variable '{0}' implicitly has an '{1}' type.": {
23262353
"category": "Error",
23272354
"code": 7005

0 commit comments

Comments
 (0)