Skip to content

Commit 30affee

Browse files
authored
Merge branch 'microsoft:main' into main
2 parents 924c36a + 355b9e0 commit 30affee

File tree

367 files changed

+11514
-2499
lines changed

Some content is hidden

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

367 files changed

+11514
-2499
lines changed

src/compiler/checker.ts

Lines changed: 135 additions & 25 deletions
Large diffs are not rendered by default.

src/compiler/commandLineParser.ts

Lines changed: 124 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ import {
4444
filterMutate,
4545
find,
4646
findIndex,
47-
firstOrUndefinedIterator,
4847
flatten,
4948
forEach,
5049
forEachEntry,
@@ -249,6 +248,8 @@ const libEntries: [string, string][] = [
249248
["esnext.iterator", "lib.esnext.iterator.d.ts"],
250249
["esnext.promise", "lib.esnext.promise.d.ts"],
251250
["esnext.float16", "lib.esnext.float16.d.ts"],
251+
["esnext.error", "lib.esnext.error.d.ts"],
252+
["esnext.sharedmemory", "lib.esnext.sharedmemory.d.ts"],
252253
["decorators", "lib.decorators.d.ts"],
253254
["decorators.legacy", "lib.decorators.legacy.d.ts"],
254255
];
@@ -2789,140 +2790,146 @@ function serializeOptionBaseObject(
27892790
return result;
27902791
}
27912792

2792-
/**
2793-
* Generate a list of the compiler options whose value is not the default.
2794-
* @param options compilerOptions to be evaluated.
2795-
/** @internal */
2796-
export function getCompilerOptionsDiffValue(options: CompilerOptions, newLine: string): string {
2797-
const compilerOptionsMap = getSerializedCompilerOption(options);
2798-
return getOverwrittenDefaultOptions();
2799-
2800-
function makePadding(paddingLength: number): string {
2801-
return Array(paddingLength + 1).join(" ");
2802-
}
2803-
2804-
function getOverwrittenDefaultOptions() {
2805-
const result: string[] = [];
2806-
const tab = makePadding(2);
2807-
commandOptionsWithoutBuild.forEach(cmd => {
2808-
if (!compilerOptionsMap.has(cmd.name)) {
2809-
return;
2810-
}
2811-
2812-
const newValue = compilerOptionsMap.get(cmd.name);
2813-
const defaultValue = getDefaultValueForOption(cmd);
2814-
if (newValue !== defaultValue) {
2815-
result.push(`${tab}${cmd.name}: ${newValue}`);
2816-
}
2817-
else if (hasProperty(defaultInitCompilerOptions, cmd.name)) {
2818-
result.push(`${tab}${cmd.name}: ${defaultValue}`);
2819-
}
2820-
});
2821-
return result.join(newLine) + newLine;
2822-
}
2823-
}
2824-
2825-
/**
2826-
* Get the compiler options to be written into the tsconfig.json.
2827-
* @param options commandlineOptions to be included in the compileOptions.
2828-
*/
2829-
function getSerializedCompilerOption(options: CompilerOptions): Map<string, CompilerOptionsValue> {
2830-
const compilerOptions = extend(options, defaultInitCompilerOptions);
2831-
return serializeCompilerOptions(compilerOptions);
2832-
}
28332793
/**
28342794
* Generate tsconfig configuration when running command line "--init"
28352795
* @param options commandlineOptions to be generated into tsconfig.json
2836-
* @param fileNames array of filenames to be generated into tsconfig.json
2837-
*
28382796
* @internal
28392797
*/
2840-
export function generateTSConfig(options: CompilerOptions, fileNames: readonly string[], newLine: string): string {
2841-
const compilerOptionsMap = getSerializedCompilerOption(options);
2842-
return writeConfigurations();
2798+
export function generateTSConfig(options: CompilerOptions, newLine: string): string {
2799+
type PresetValue = string | number | boolean | (string | number | boolean)[];
2800+
2801+
const tab = " ";
2802+
const result: string[] = [];
2803+
const allSetOptions = Object.keys(options).filter(k => k !== "init" && k !== "help" && k !== "watch");
2804+
2805+
result.push(`{`);
2806+
result.push(`${tab}// ${getLocaleSpecificMessage(Diagnostics.Visit_https_Colon_Slash_Slashaka_ms_Slashtsconfig_to_read_more_about_this_file)}`);
2807+
result.push(`${tab}"compilerOptions": {`);
2808+
2809+
emitHeader(Diagnostics.File_Layout);
2810+
emitOption("rootDir", "./src", "optional");
2811+
emitOption("outDir", "./dist", "optional");
2812+
2813+
newline();
2814+
2815+
emitHeader(Diagnostics.Environment_Settings);
2816+
emitHeader(Diagnostics.See_also_https_Colon_Slash_Slashaka_ms_Slashtsconfig_Slashmodule);
2817+
emitOption("module", ModuleKind.NodeNext);
2818+
emitOption("target", ScriptTarget.ESNext);
2819+
emitOption("types", []);
2820+
if (options.lib) {
2821+
emitOption("lib", options.lib);
2822+
}
2823+
emitHeader(Diagnostics.For_nodejs_Colon);
2824+
result.push(`${tab}${tab}// "lib": ["esnext"],`);
2825+
result.push(`${tab}${tab}// "types": ["node"],`);
2826+
emitHeader(Diagnostics.and_npm_install_D_types_Slashnode);
2827+
2828+
newline();
2829+
2830+
emitHeader(Diagnostics.Other_Outputs);
2831+
emitOption("sourceMap", /*defaultValue*/ true);
2832+
emitOption("declaration", /*defaultValue*/ true);
2833+
emitOption("declarationMap", /*defaultValue*/ true);
2834+
2835+
newline();
2836+
2837+
emitHeader(Diagnostics.Stricter_Typechecking_Options);
2838+
emitOption("noUncheckedIndexedAccess", /*defaultValue*/ true);
2839+
emitOption("exactOptionalPropertyTypes", /*defaultValue*/ true);
2840+
2841+
newline();
2842+
2843+
emitHeader(Diagnostics.Style_Options);
2844+
emitOption("noImplicitReturns", /*defaultValue*/ true, "optional");
2845+
emitOption("noImplicitOverride", /*defaultValue*/ true, "optional");
2846+
emitOption("noUnusedLocals", /*defaultValue*/ true, "optional");
2847+
emitOption("noUnusedParameters", /*defaultValue*/ true, "optional");
2848+
emitOption("noFallthroughCasesInSwitch", /*defaultValue*/ true, "optional");
2849+
emitOption("noPropertyAccessFromIndexSignature", /*defaultValue*/ true, "optional");
2850+
2851+
newline();
2852+
2853+
emitHeader(Diagnostics.Recommended_Options);
2854+
emitOption("strict", /*defaultValue*/ true);
2855+
emitOption("jsx", JsxEmit.ReactJSX);
2856+
emitOption("verbatimModuleSyntax", /*defaultValue*/ true);
2857+
emitOption("isolatedModules", /*defaultValue*/ true);
2858+
emitOption("noUncheckedSideEffectImports", /*defaultValue*/ true);
2859+
emitOption("moduleDetection", ModuleDetectionKind.Force);
2860+
emitOption("skipLibCheck", /*defaultValue*/ true);
2861+
2862+
// Write any user-provided options we haven't already
2863+
if (allSetOptions.length > 0) {
2864+
newline();
2865+
while (allSetOptions.length > 0) {
2866+
emitOption(allSetOptions[0], options[allSetOptions[0]]);
2867+
}
2868+
}
28432869

2844-
function makePadding(paddingLength: number): string {
2845-
return Array(paddingLength + 1).join(" ");
2870+
function newline() {
2871+
result.push("");
28462872
}
28472873

2848-
function isAllowedOptionForOutput({ category, name, isCommandLineOnly }: CommandLineOption): boolean {
2849-
// Skip options which do not have a category or have categories which are more niche
2850-
const categoriesToSkip = [Diagnostics.Command_line_Options, Diagnostics.Editor_Support, Diagnostics.Compiler_Diagnostics, Diagnostics.Backwards_Compatibility, Diagnostics.Watch_and_Build_Modes, Diagnostics.Output_Formatting];
2851-
return !isCommandLineOnly && category !== undefined && (!categoriesToSkip.includes(category) || compilerOptionsMap.has(name));
2874+
function emitHeader(header: DiagnosticMessage) {
2875+
result.push(`${tab}${tab}// ${getLocaleSpecificMessage(header)}`);
28522876
}
28532877

2854-
function writeConfigurations() {
2855-
// Filter applicable options to place in the file
2856-
const categorizedOptions = new Map<DiagnosticMessage, CommandLineOption[]>();
2857-
// Set allowed categories in order
2858-
categorizedOptions.set(Diagnostics.Projects, []);
2859-
categorizedOptions.set(Diagnostics.Language_and_Environment, []);
2860-
categorizedOptions.set(Diagnostics.Modules, []);
2861-
categorizedOptions.set(Diagnostics.JavaScript_Support, []);
2862-
categorizedOptions.set(Diagnostics.Emit, []);
2863-
categorizedOptions.set(Diagnostics.Interop_Constraints, []);
2864-
categorizedOptions.set(Diagnostics.Type_Checking, []);
2865-
categorizedOptions.set(Diagnostics.Completeness, []);
2866-
for (const option of optionDeclarations) {
2867-
if (isAllowedOptionForOutput(option)) {
2868-
let listForCategory = categorizedOptions.get(option.category!);
2869-
if (!listForCategory) categorizedOptions.set(option.category!, listForCategory = []);
2870-
listForCategory.push(option);
2871-
}
2878+
// commented = 'always': Always comment this out, even if it's on commandline
2879+
// commented = 'optional': Comment out unless it's on commandline
2880+
// commented = 'never': Never comment this out
2881+
function emitOption<K extends string & keyof CompilerOptions>(setting: K, defaultValue: CompilerOptions[K], commented: "always" | "optional" | "never" = "never") {
2882+
const existingOptionIndex = allSetOptions.indexOf(setting);
2883+
if (existingOptionIndex >= 0) {
2884+
allSetOptions.splice(existingOptionIndex, 1);
28722885
}
28732886

2874-
// Serialize all options and their descriptions
2875-
let marginLength = 0;
2876-
let seenKnownKeys = 0;
2877-
const entries: { value: string; description?: string; }[] = [];
2878-
categorizedOptions.forEach((options, category) => {
2879-
if (entries.length !== 0) {
2880-
entries.push({ value: "" });
2881-
}
2882-
entries.push({ value: `/* ${getLocaleSpecificMessage(category)} */` });
2883-
for (const option of options) {
2884-
let optionName;
2885-
if (compilerOptionsMap.has(option.name)) {
2886-
optionName = `"${option.name}": ${JSON.stringify(compilerOptionsMap.get(option.name))}${(seenKnownKeys += 1) === compilerOptionsMap.size ? "" : ","}`;
2887-
}
2888-
else {
2889-
optionName = `// "${option.name}": ${JSON.stringify(getDefaultValueForOption(option))},`;
2890-
}
2891-
entries.push({
2892-
value: optionName,
2893-
description: `/* ${option.description && getLocaleSpecificMessage(option.description) || option.name} */`,
2894-
});
2895-
marginLength = Math.max(optionName.length, marginLength);
2896-
}
2897-
});
2898-
2899-
// Write the output
2900-
const tab = makePadding(2);
2901-
const result: string[] = [];
2902-
result.push(`{`);
2903-
result.push(`${tab}"compilerOptions": {`);
2904-
result.push(`${tab}${tab}/* ${getLocaleSpecificMessage(Diagnostics.Visit_https_Colon_Slash_Slashaka_ms_Slashtsconfig_to_read_more_about_this_file)} */`);
2905-
result.push("");
2906-
// Print out each row, aligning all the descriptions on the same column.
2907-
for (const entry of entries) {
2908-
const { value, description = "" } = entry;
2909-
result.push(value && `${tab}${tab}${value}${description && (makePadding(marginLength - value.length + 2) + description)}`);
2887+
let comment: boolean;
2888+
if (commented === "always") {
2889+
comment = true;
29102890
}
2911-
if (fileNames.length) {
2912-
result.push(`${tab}},`);
2913-
result.push(`${tab}"files": [`);
2914-
for (let i = 0; i < fileNames.length; i++) {
2915-
result.push(`${tab}${tab}${JSON.stringify(fileNames[i])}${i === fileNames.length - 1 ? "" : ","}`);
2916-
}
2917-
result.push(`${tab}]`);
2891+
else if (commented === "never") {
2892+
comment = false;
29182893
}
29192894
else {
2920-
result.push(`${tab}}`);
2895+
comment = !hasProperty(options, setting);
29212896
}
2922-
result.push(`}`);
29232897

2924-
return result.join(newLine) + newLine;
2898+
const value = (options[setting] ?? defaultValue) as PresetValue;
2899+
if (comment) {
2900+
result.push(`${tab}${tab}// "${setting}": ${formatValueOrArray(setting, value)},`);
2901+
}
2902+
else {
2903+
result.push(`${tab}${tab}"${setting}": ${formatValueOrArray(setting, value)},`);
2904+
}
29252905
}
2906+
2907+
function formatValueOrArray(settingName: string, value: PresetValue): string {
2908+
const option = optionDeclarations.filter(c => c.name === settingName)[0];
2909+
if (!option) Debug.fail(`No option named ${settingName}?`);
2910+
const map = (option.type instanceof Map) ? option.type : undefined;
2911+
if (isArray(value)) {
2912+
// eslint-disable-next-line local/no-in-operator
2913+
const map = ("element" in option && (option.element.type instanceof Map)) ? option.element.type : undefined;
2914+
return `[${value.map(v => formatSingleValue(v, map)).join(", ")}]`;
2915+
}
2916+
else {
2917+
return formatSingleValue(value, map);
2918+
}
2919+
}
2920+
2921+
function formatSingleValue(value: PresetValue, map: Map<string, string | number> | undefined) {
2922+
if (map) {
2923+
value = getNameOfCompilerOptionValue(value as string | number, map) ?? Debug.fail(`No matching value of ${value}`);
2924+
}
2925+
return JSON.stringify(value);
2926+
}
2927+
2928+
result.push(`${tab}}`);
2929+
result.push(`}`);
2930+
result.push(``);
2931+
2932+
return result.join(newLine);
29262933
}
29272934

29282935
/** @internal */
@@ -4256,25 +4263,3 @@ function getOptionValueWithEmptyStrings(value: any, option: CommandLineOption):
42564263
});
42574264
}
42584265
}
4259-
4260-
function getDefaultValueForOption(option: CommandLineOption): {} {
4261-
switch (option.type) {
4262-
case "number":
4263-
return 1;
4264-
case "boolean":
4265-
return true;
4266-
case "string":
4267-
const defaultValue = option.defaultValueDescription;
4268-
return option.isFilePath ? `./${defaultValue && typeof defaultValue === "string" ? defaultValue : ""}` : "";
4269-
case "list":
4270-
return [];
4271-
case "listOrElement":
4272-
return getDefaultValueForOption(option.element);
4273-
case "object":
4274-
return {};
4275-
default:
4276-
const value = firstOrUndefinedIterator(option.type.keys());
4277-
if (value !== undefined) return value;
4278-
return Debug.fail("Expected 'option.type' to have entries.");
4279-
}
4280-
}

src/compiler/diagnosticMessages.json

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1837,6 +1837,14 @@
18371837
"category": "Error",
18381838
"code": 1544
18391839
},
1840+
"'using' declarations are not allowed in ambient contexts.": {
1841+
"category": "Error",
1842+
"code": 1545
1843+
},
1844+
"'await using' declarations are not allowed in ambient contexts.": {
1845+
"category": "Error",
1846+
"code": 1546
1847+
},
18401848

18411849
"The types of '{0}' are incompatible between these types.": {
18421850
"category": "Error",
@@ -5702,6 +5710,42 @@
57025710
"category": "Message",
57035711
"code": 6283
57045712
},
5713+
"File Layout": {
5714+
"category": "Message",
5715+
"code": 6284
5716+
},
5717+
"Environment Settings": {
5718+
"category": "Message",
5719+
"code": 6285
5720+
},
5721+
"See also https://aka.ms/tsconfig/module": {
5722+
"category": "Message",
5723+
"code": 6286
5724+
},
5725+
"For nodejs:": {
5726+
"category": "Message",
5727+
"code": 6287
5728+
},
5729+
"and npm install -D @types/node": {
5730+
"category": "Message",
5731+
"code": 6290
5732+
},
5733+
"Other Outputs": {
5734+
"category": "Message",
5735+
"code": 6291
5736+
},
5737+
"Stricter Typechecking Options": {
5738+
"category": "Message",
5739+
"code": 6292
5740+
},
5741+
"Style Options": {
5742+
"category": "Message",
5743+
"code": 6293
5744+
},
5745+
"Recommended Options": {
5746+
"category": "Message",
5747+
"code": 6294
5748+
},
57055749

57065750
"Enable project compilation": {
57075751
"category": "Message",
@@ -8451,5 +8495,21 @@
84518495
"String literal import and export names are not supported when the '--module' flag is set to 'es2015' or 'es2020'.": {
84528496
"category": "Error",
84538497
"code": 18057
8498+
},
8499+
"Default imports are not allowed in a deferred import.": {
8500+
"category": "Error",
8501+
"code": 18058
8502+
},
8503+
"Named imports are not allowed in a deferred import.": {
8504+
"category": "Error",
8505+
"code": 18059
8506+
},
8507+
"Deferred imports are only supported when the '--module' flag is set to 'esnext' or 'preserve'.": {
8508+
"category": "Error",
8509+
"code": 18060
8510+
},
8511+
"'{0}' is not a valid meta-property for keyword 'import'. Did you mean 'meta' or 'defer'?": {
8512+
"category": "Error",
8513+
"code": 18061
84548514
}
84558515
}

src/compiler/emitter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3686,8 +3686,8 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
36863686
}
36873687

36883688
function emitImportClause(node: ImportClause) {
3689-
if (node.isTypeOnly) {
3690-
emitTokenWithComment(SyntaxKind.TypeKeyword, node.pos, writeKeyword, node);
3689+
if (node.phaseModifier !== undefined) {
3690+
emitTokenWithComment(node.phaseModifier, node.pos, writeKeyword, node);
36913691
writeSpace();
36923692
}
36933693
emit(node.name);

0 commit comments

Comments
 (0)