diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index b3c10e3c84951..5b0a1d86ff15f 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -67,6 +67,7 @@ import { hasExtension, hasProperty, ImportsNotUsedAsValues, + InitEmit, isArray, isArrayLiteralExpression, isComputedNonLiteralName, @@ -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 = new Map(mapIterator(jsxOptionMap.entries(), ([key, value]: [string, JsxEmit]) => ["" + value, key] as const)); @@ -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", @@ -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))); } @@ -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); @@ -2819,6 +2830,7 @@ function getSerializedCompilerOption(options: CompilerOptions): Map { - 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); } diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 7f686076f1824..1ae702c490896 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -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 }, diff --git a/src/compiler/executeCommandLine.ts b/src/compiler/executeCommandLine.ts index ca166635ac5c1..e6dde1fddf2af 100644 --- a/src/compiler/executeCommandLine.ts +++ b/src/compiler/executeCommandLine.ts @@ -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); } diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 9b5908d71d9bc..7f2314e0bd528 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -7379,7 +7379,7 @@ export interface CompilerOptions { importHelpers?: boolean; /** @deprecated */ importsNotUsedAsValues?: ImportsNotUsedAsValues; - /** @internal */ init?: boolean; + /** @internal */ init?: InitEmit; inlineSourceMap?: boolean; inlineSources?: boolean; isolatedModules?: boolean; @@ -7542,6 +7542,11 @@ export const enum JsxEmit { ReactJSXDev = 5, } +export const enum InitEmit { + Default = 0, + Clean = 1, +} + /** @deprecated */ export const enum ImportsNotUsedAsValues { Remove, diff --git a/tests/baselines/reference/api/typescript.d.ts b/tests/baselines/reference/api/typescript.d.ts index 0ed1d1e839fcd..78b456e670372 100644 --- a/tests/baselines/reference/api/typescript.d.ts +++ b/tests/baselines/reference/api/typescript.d.ts @@ -7145,6 +7145,10 @@ declare namespace ts { ReactJSX = 4, ReactJSXDev = 5, } + enum InitEmit { + Default = 0, + Clean = 1, + } /** @deprecated */ enum ImportsNotUsedAsValues { Remove = 0, diff --git a/tests/baselines/reference/config/initTSConfig/Initialized TSConfig with advanced options/tsconfig.json b/tests/baselines/reference/config/initTSConfig/Initialized TSConfig with advanced options/tsconfig.json index 57d57797e5c65..4f4297bb2adc0 100644 --- a/tests/baselines/reference/config/initTSConfig/Initialized TSConfig with advanced options/tsconfig.json +++ b/tests/baselines/reference/config/initTSConfig/Initialized TSConfig with advanced options/tsconfig.json @@ -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. */ diff --git a/tests/baselines/reference/config/initTSConfig/Initialized TSConfig with boolean value compiler options/tsconfig.json b/tests/baselines/reference/config/initTSConfig/Initialized TSConfig with boolean value compiler options/tsconfig.json index 6f40fa430a101..c9c555d96f35d 100644 --- a/tests/baselines/reference/config/initTSConfig/Initialized TSConfig with boolean value compiler options/tsconfig.json +++ b/tests/baselines/reference/config/initTSConfig/Initialized TSConfig with boolean value compiler options/tsconfig.json @@ -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. */ diff --git a/tests/baselines/reference/config/initTSConfig/Initialized TSConfig with enum value compiler options/tsconfig.json b/tests/baselines/reference/config/initTSConfig/Initialized TSConfig with enum value compiler options/tsconfig.json index c0898eedb6c94..7d3b155cf10fb 100644 --- a/tests/baselines/reference/config/initTSConfig/Initialized TSConfig with enum value compiler options/tsconfig.json +++ b/tests/baselines/reference/config/initTSConfig/Initialized TSConfig with enum value compiler options/tsconfig.json @@ -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. */ @@ -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" + ] } diff --git a/tests/baselines/reference/config/initTSConfig/Initialized TSConfig with files options/tsconfig.json b/tests/baselines/reference/config/initTSConfig/Initialized TSConfig with files options/tsconfig.json index 23354c3f64a09..186a2aaaec41d 100644 --- a/tests/baselines/reference/config/initTSConfig/Initialized TSConfig with files options/tsconfig.json +++ b/tests/baselines/reference/config/initTSConfig/Initialized TSConfig with files options/tsconfig.json @@ -109,7 +109,6 @@ "skipLibCheck": true /* Skip type checking all .d.ts files. */ }, "files": [ - "file0.st", "file1.ts", "file2.ts" ] diff --git a/tests/baselines/reference/config/initTSConfig/Initialized TSConfig with incorrect compiler option value/tsconfig.json b/tests/baselines/reference/config/initTSConfig/Initialized TSConfig with incorrect compiler option value/tsconfig.json index acfcbb5bfad84..a8e6673fc264e 100644 --- a/tests/baselines/reference/config/initTSConfig/Initialized TSConfig with incorrect compiler option value/tsconfig.json +++ b/tests/baselines/reference/config/initTSConfig/Initialized TSConfig with incorrect compiler option value/tsconfig.json @@ -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. */ @@ -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" + ] } diff --git a/tests/baselines/reference/config/initTSConfig/Initialized TSConfig with list compiler options with enum value/tsconfig.json b/tests/baselines/reference/config/initTSConfig/Initialized TSConfig with list compiler options with enum value/tsconfig.json index 4c0ff4bb1cb43..50eb7659829b8 100644 --- a/tests/baselines/reference/config/initTSConfig/Initialized TSConfig with list compiler options with enum value/tsconfig.json +++ b/tests/baselines/reference/config/initTSConfig/Initialized TSConfig with list compiler options with enum value/tsconfig.json @@ -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. */ @@ -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" + ] } diff --git a/tests/baselines/reference/config/initTSConfig/Initialized TSConfig with list compiler options/tsconfig.json b/tests/baselines/reference/config/initTSConfig/Initialized TSConfig with list compiler options/tsconfig.json index c61179ba4d047..cded249c81c0d 100644 --- a/tests/baselines/reference/config/initTSConfig/Initialized TSConfig with list compiler options/tsconfig.json +++ b/tests/baselines/reference/config/initTSConfig/Initialized TSConfig with list compiler options/tsconfig.json @@ -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. */ @@ -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" + ] } diff --git a/tests/baselines/reference/tsc/commandLine/does-not-add-color-when-NO_COLOR-is-set.js b/tests/baselines/reference/tsc/commandLine/does-not-add-color-when-NO_COLOR-is-set.js index 282b543e11298..45ca01b1cd402 100644 --- a/tests/baselines/reference/tsc/commandLine/does-not-add-color-when-NO_COLOR-is-set.js +++ b/tests/baselines/reference/tsc/commandLine/does-not-add-color-when-NO_COLOR-is-set.js @@ -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'. diff --git a/tests/baselines/reference/tsc/commandLine/help-all.js b/tests/baselines/reference/tsc/commandLine/help-all.js index 65d725168ba34..829978b3853e9 100644 --- a/tests/baselines/reference/tsc/commandLine/help-all.js +++ b/tests/baselines/reference/tsc/commandLine/help-all.js @@ -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. diff --git a/tests/baselines/reference/tsc/commandLine/help.js b/tests/baselines/reference/tsc/commandLine/help.js index 731a70f107785..972ecadfac9ac 100644 --- a/tests/baselines/reference/tsc/commandLine/help.js +++ b/tests/baselines/reference/tsc/commandLine/help.js @@ -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'. diff --git a/tests/baselines/reference/tsc/commandLine/show-help-with-ExitStatus.DiagnosticsPresent_OutputsSkipped-when-host-can't-provide-terminal-width.js b/tests/baselines/reference/tsc/commandLine/show-help-with-ExitStatus.DiagnosticsPresent_OutputsSkipped-when-host-can't-provide-terminal-width.js index 77d316babafb1..3be0e07878caa 100644 --- a/tests/baselines/reference/tsc/commandLine/show-help-with-ExitStatus.DiagnosticsPresent_OutputsSkipped-when-host-can't-provide-terminal-width.js +++ b/tests/baselines/reference/tsc/commandLine/show-help-with-ExitStatus.DiagnosticsPresent_OutputsSkipped-when-host-can't-provide-terminal-width.js @@ -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'. diff --git a/tests/baselines/reference/tsc/commandLine/show-help-with-ExitStatus.DiagnosticsPresent_OutputsSkipped.js b/tests/baselines/reference/tsc/commandLine/show-help-with-ExitStatus.DiagnosticsPresent_OutputsSkipped.js index 77d316babafb1..3be0e07878caa 100644 --- a/tests/baselines/reference/tsc/commandLine/show-help-with-ExitStatus.DiagnosticsPresent_OutputsSkipped.js +++ b/tests/baselines/reference/tsc/commandLine/show-help-with-ExitStatus.DiagnosticsPresent_OutputsSkipped.js @@ -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'.