Skip to content

Commit

Permalink
Add showConfig tsc flag for debugging configs
Browse files Browse the repository at this point in the history
  • Loading branch information
weswigham committed Sep 25, 2018
1 parent e1fd0ea commit 164000b
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,13 @@ namespace ts {
category: Diagnostics.Command_line_Options,
description: Diagnostics.Stylize_errors_and_messages_using_color_and_context_experimental
},
{
name: "showConfig",
type: "boolean",
category: Diagnostics.Command_line_Options,
isCommandLineOnly: true,
description: Diagnostics.Print_the_final_configuration_instead_of_building
},

// Basic
{
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,10 @@
"category": "Error",
"code": 1349
},
"Print the final configuration instead of building.": {
"category": "Message",
"code": 1350
},

"Duplicate identifier '{0}'.": {
"category": "Error",
Expand Down
1 change: 1 addition & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4443,6 +4443,7 @@ namespace ts {
/*@internal*/ version?: boolean;
/*@internal*/ watch?: boolean;
esModuleInterop?: boolean;
/* @internal */ showConfig?: boolean;

[option: string]: CompilerOptionsValue | TsConfigSourceFile | undefined;
}
Expand Down
91 changes: 91 additions & 0 deletions src/tsc/tsc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,50 @@ namespace ts {
const commandLineOptions = commandLine.options;
if (configFileName) {
const configParseResult = parseConfigFileWithSystem(configFileName, commandLineOptions, sys, reportDiagnostic)!; // TODO: GH#18217
if (commandLineOptions.showConfig) {
const getCanonicalFileName = createGetCanonicalFileName(sys.useCaseSensitiveFileNames);
const files = map(
filter(
configParseResult.fileNames,
!configParseResult.configFileSpecs ? _ => false : matchesSpecs(
configFileName,
configParseResult.configFileSpecs.validatedIncludeSpecs,
configParseResult.configFileSpecs.validatedExcludeSpecs
)
),
f => getRelativePathFromFile(getNormalizedAbsolutePath(configFileName!, sys.getCurrentDirectory()), f, getCanonicalFileName)
);
const config = {
compilerOptions: {
...Object.keys(configParseResult.options).map((key) => {
const value = configParseResult.options[key];
const option = getOptionFromName(key);
if (!option) {
return [key, value] as [keyof CompilerOptions, {} | undefined];
}
return [key, unencodeCompilerOption(value, option, getCanonicalFileName)] as [keyof CompilerOptions, {} | undefined];
}).reduce((prev, cur) => ({...prev, [cur[0]]: cur[1]}), {}),
showConfig: undefined,
configFile: undefined,
configFilePath: undefined,
help: undefined,
init: undefined,
listFiles: undefined,
listEmittedFiles: undefined,
project: undefined,
},
references: map(configParseResult.projectReferences, r => ({...r, path: r.originalPath, originalPath: undefined})),
files: length(files) ? files : undefined,
...(configParseResult.configFileSpecs ? {
include: filterSameAsDefaultInclude(configParseResult.configFileSpecs.validatedIncludeSpecs),
exclude: configParseResult.configFileSpecs.validatedExcludeSpecs
} : {}),
compilerOnSave: !!configParseResult.compileOnSave ? true : undefined
}
// tslint:disable-next-line:no-null
sys.write(JSON.stringify(config, null, 4) + sys.newLine);
return sys.exit(ExitStatus.Success);
}
updateReportDiagnostic(configParseResult.options);
if (isWatchSet(configParseResult.options)) {
reportWatchModeWithoutSysSupport();
Expand All @@ -153,6 +197,53 @@ namespace ts {
}
}

function unencodeCompilerOption(value: any, option: CommandLineOption, getCanonicalFileName: ReturnType<typeof createGetCanonicalFileName>): {} | undefined {
switch (option.type) {
case "object":
case "number":
case "boolean":
return value;
case "string":
if (option.isFilePath) {
return getRelativePathFromFile(sys.getCurrentDirectory(), getNormalizedAbsolutePath(value as string, sys.getCurrentDirectory()), getCanonicalFileName);
}
return value;
case "list":
const elementType = (option as CommandLineOptionOfListType).element;
return isArray(value) ? value.map(v => unencodeCompilerOption(v, elementType, getCanonicalFileName)) : undefined;
default:
return forEachEntry(option.type, (optionEnumValue, optionStringValue) => {
if (optionEnumValue === value) {
return optionStringValue;
}
});
}
}

function filterSameAsDefaultInclude(specs: ReadonlyArray<string> | undefined) {
if (!length(specs)) return undefined;
if (length(specs) !== 1) return specs;
if (specs![0] === "**/*") return undefined;
return specs;
}

function matchesSpecs(path: string, includeSpecs: ReadonlyArray<string> | undefined, excludeSpecs: ReadonlyArray<string> | undefined): (path: string) => boolean {
if (!includeSpecs) return _ => false;
const patterns = getFileMatcherPatterns(path, excludeSpecs, includeSpecs, sys.useCaseSensitiveFileNames, sys.getCurrentDirectory());
const excludeRe = patterns.excludePattern && getRegexFromPattern(patterns.excludePattern, sys.useCaseSensitiveFileNames);
const includeRe = patterns.includeFilePattern && getRegexFromPattern(patterns.includeFilePattern, sys.useCaseSensitiveFileNames);
if (includeRe) {
if (excludeRe) {
return path => includeRe.test(path) && !excludeRe.test(path);
}
return path => includeRe.test(path);
}
if (excludeRe) {
return path => !excludeRe.test(path);
}
return _ => false;
}

function reportWatchModeWithoutSysSupport() {
if (!sys.watchFile || !sys.watchDirectory) {
reportDiagnostic(createCompilerDiagnostic(Diagnostics.The_current_host_does_not_support_the_0_option, "--watch"));
Expand Down

0 comments on commit 164000b

Please sign in to comment.