Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/tsc init clean #60407

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
30 changes: 23 additions & 7 deletions src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import {
hasExtension,
hasProperty,
ImportsNotUsedAsValues,
InitEmit,
isArray,
isArrayLiteralExpression,
isComputedNonLiteralName,
Expand Down Expand Up @@ -138,6 +139,11 @@ const jsxOptionMap = new Map(Object.entries({
"react-jsxdev": JsxEmit.ReactJSXDev,
}));

const initOptionMap = new Map(Object.entries({
default: InitEmit.Default,
clean: InitEmit.Clean,
}));

/** @internal */
export const inverseJsxOptionMap: Map<string, string> = new Map(mapIterator(jsxOptionMap.entries(), ([key, value]: [string, JsxEmit]) => ["" + value, key] as const));

Expand Down Expand Up @@ -634,11 +640,11 @@ const commandOptionsWithoutBuild: CommandLineOption[] = [
},
{
name: "init",
type: "boolean",
type: initOptionMap,
showInSimplifiedHelpView: true,
category: Diagnostics.Command_line_Options,
description: Diagnostics.Initializes_a_TypeScript_project_and_creates_a_tsconfig_json_file,
defaultValueDescription: false,
description: Diagnostics.Initializes_a_TypeScript_project_and_creates_a_tsconfig_json_file_Use_init_clean_to_skip_comments,
defaultValueDescription: undefined,
},
{
name: "project",
Expand Down Expand Up @@ -2011,7 +2017,7 @@ function parseOptionValue(
}
else {
// Check to see if no argument was provided (e.g. "--locale" is the last command-line argument).
if (!args[i] && opt.type !== "boolean") {
if (!args[i] && opt.type !== "boolean" && opt.type !== initOptionMap) {
errors.push(createCompilerDiagnostic(diagnostics.optionTypeMismatchDiagnostic, opt.name, getCompilerOptionValueTypeString(opt)));
}

Expand Down Expand Up @@ -2044,6 +2050,11 @@ function parseOptionValue(
case "listOrElement":
Debug.fail("listOrElement not supported here");
break;
case initOptionMap:
const initArg = args[i] ? args[i] : "default";
options[opt.name] = parseCustomTypeOption(opt, initArg, errors);
i++;
break;
// If not a primitive, the possible types are specified in what is effectively a map of options.
default:
options[opt.name] = parseCustomTypeOption(opt as CommandLineOptionOfCustomType, args[i], errors);
Expand Down Expand Up @@ -2819,6 +2830,7 @@ function getSerializedCompilerOption(options: CompilerOptions): Map<string, Comp
*/
export function generateTSConfig(options: CompilerOptions, fileNames: readonly string[], newLine: string): string {
const compilerOptionsMap = getSerializedCompilerOption(options);
const isClean = options.init === InitEmit.Clean;
return writeConfigurations();

function makePadding(paddingLength: number): string {
Expand Down Expand Up @@ -2856,21 +2868,25 @@ export function generateTSConfig(options: CompilerOptions, fileNames: readonly s
let seenKnownKeys = 0;
const entries: { value: string; description?: string; }[] = [];
categorizedOptions.forEach((options, category) => {
if (entries.length !== 0) {
if (entries.length !== 0 && !isClean) {
entries.push({ value: "" });
}
entries.push({ value: `/* ${getLocaleSpecificMessage(category)} */` });
// Header comments
if (!isClean) {
entries.push({ value: `/* ${getLocaleSpecificMessage(category)} */` });
}
for (const option of options) {
let optionName;
if (compilerOptionsMap.has(option.name)) {
optionName = `"${option.name}": ${JSON.stringify(compilerOptionsMap.get(option.name))}${(seenKnownKeys += 1) === compilerOptionsMap.size ? "" : ","}`;
}
else {
if (isClean) continue;
optionName = `// "${option.name}": ${JSON.stringify(getDefaultValueForOption(option))},`;
}
entries.push({
value: optionName,
description: `/* ${option.description && getLocaleSpecificMessage(option.description) || option.name} */`,
description: isClean ? `/* ${option.description && getLocaleSpecificMessage(option.description) || option.name} */` : "",
});
marginLength = Math.max(optionName.length, marginLength);
}
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -4904,7 +4904,7 @@
"category": "Message",
"code": 6066
},
"Initializes a TypeScript project and creates a tsconfig.json file.": {
"Initializes a TypeScript project and creates a tsconfig.json file. Use '--init clean' to skip comments.": {
"category": "Message",
"code": 6070
},
Expand Down
3 changes: 1 addition & 2 deletions src/compiler/executeCommandLine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -572,8 +572,7 @@ function executeCommandLineWorker(
commandLine.errors.forEach(reportDiagnostic);
return sys.exit(ExitStatus.DiagnosticsPresent_OutputsSkipped);
}

if (commandLine.options.init) {
if (commandLine.options.init !== undefined) {
writeConfigFile(sys, reportDiagnostic, commandLine.options, commandLine.fileNames);
return sys.exit(ExitStatus.Success);
}
Expand Down
7 changes: 6 additions & 1 deletion src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7379,7 +7379,7 @@ export interface CompilerOptions {
importHelpers?: boolean;
/** @deprecated */
importsNotUsedAsValues?: ImportsNotUsedAsValues;
/** @internal */ init?: boolean;
/** @internal */ init?: InitEmit;
inlineSourceMap?: boolean;
inlineSources?: boolean;
isolatedModules?: boolean;
Expand Down Expand Up @@ -7542,6 +7542,11 @@ export const enum JsxEmit {
ReactJSXDev = 5,
}

export const enum InitEmit {
Default = 0,
Clean = 1,
}

/** @deprecated */
export const enum ImportsNotUsedAsValues {
Remove,
Expand Down
4 changes: 4 additions & 0 deletions tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7145,6 +7145,10 @@ declare namespace ts {
ReactJSX = 4,
ReactJSXDev = 5,
}
enum InitEmit {
Default = 0,
Clean = 1,
}
/** @deprecated */
enum ImportsNotUsedAsValues {
Remove = 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */

/* Emit */
"declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
// "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
// "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */
// "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */
// "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
"noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */
// "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */
// "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */
// "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
// "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */

/* Language and Environment */
"target": "es5", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
"target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
"jsx": "react", /* Specify what JSX code is generated. */
// "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */
Expand Down Expand Up @@ -107,5 +107,8 @@
/* Completeness */
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
"skipLibCheck": true /* Skip type checking all .d.ts files. */
}
},
"files": [
"es5"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@
"skipLibCheck": true /* Skip type checking all .d.ts files. */
},
"files": [
"file0.st",
"file1.ts",
"file2.ts"
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

/* Language and Environment */
"target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
"lib": ["es5","es2015.promise"], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
// "jsx": "preserve", /* Specify what JSX code is generated. */
// "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
Expand Down Expand Up @@ -107,5 +107,8 @@
/* Completeness */
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
"skipLibCheck": true /* Skip type checking all .d.ts files. */
}
},
"files": [
"nonExistLib,es5,es2015.promise"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

/* Language and Environment */
"target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
"lib": ["es5","es2015.core"], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
// "jsx": "preserve", /* Specify what JSX code is generated. */
// "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
Expand Down Expand Up @@ -107,5 +107,8 @@
/* Completeness */
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
"skipLibCheck": true /* Skip type checking all .d.ts files. */
}
},
"files": [
"es5,es2015.core"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
// "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */
"types": ["jquery","mocha"], /* Specify type package names to be included without being referenced in a source file. */
// "types": [], /* Specify type package names to be included without being referenced in a source file. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
// "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
// "allowImportingTsExtensions": true, /* Allow imports to include TypeScript file extensions. Requires '--moduleResolution bundler' and either '--noEmit' or '--emitDeclarationOnly' to be set. */
Expand Down Expand Up @@ -107,5 +107,8 @@
/* Completeness */
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
"skipLibCheck": true /* Skip type checking all .d.ts files. */
}
},
"files": [
"jquery,mocha"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Show all compiler options.
Print the compiler's version.

--init
Initializes a TypeScript project and creates a tsconfig.json file.
Initializes a TypeScript project and creates a tsconfig.json file. Use '--init clean' to skip comments.

--project, -p
Compile the project given the path to its configuration file, or to a folder with a 'tsconfig.json'.
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/tsc/commandLine/help-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Print this message.


--init
Initializes a TypeScript project and creates a tsconfig.json file.
Initializes a TypeScript project and creates a tsconfig.json file. Use '--init clean' to skip comments.

--listFilesOnly
Print names of files that are part of the compilation and then stop processing.
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/tsc/commandLine/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Show all compiler options.
Print the compiler's version.

--init
Initializes a TypeScript project and creates a tsconfig.json file.
Initializes a TypeScript project and creates a tsconfig.json file. Use '--init clean' to skip comments.

--project, -p
Compile the project given the path to its configuration file, or to a folder with a 'tsconfig.json'.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Show all compiler options.
Print the compiler's version.

--init
Initializes a TypeScript project and creates a tsconfig.json file.
Initializes a TypeScript project and creates a tsconfig.json file. Use '--init clean' to skip comments.

--project, -p
Compile the project given the path to its configuration file, or to a folder with a 'tsconfig.json'.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Show all compiler options.
Print the compiler's version.

--init
Initializes a TypeScript project and creates a tsconfig.json file.
Initializes a TypeScript project and creates a tsconfig.json file. Use '--init clean' to skip comments.

--project, -p
Compile the project given the path to its configuration file, or to a folder with a 'tsconfig.json'.
Expand Down
Loading