Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit eadf44d

Browse filesBrowse files
author
Andy
authored
Add generateTypesForModule to public API (microsoft#28069)
* Add generateTypesForModule to public API * Avoid parameter initializer and update baselines
1 parent 996fb78 commit eadf44d
Copy full SHA for eadf44d

39 files changed

+238-186Lines changed: 238 additions & 186 deletions
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/harness/fourslash.ts‎

Copy file name to clipboardExpand all lines: src/harness/fourslash.ts
+5-26Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -329,29 +329,7 @@ namespace FourSlash {
329329
});
330330
}
331331

332-
this.formatCodeSettings = {
333-
baseIndentSize: 0,
334-
indentSize: 4,
335-
tabSize: 4,
336-
newLineCharacter: "\n",
337-
convertTabsToSpaces: true,
338-
indentStyle: ts.IndentStyle.Smart,
339-
insertSpaceAfterCommaDelimiter: true,
340-
insertSpaceAfterSemicolonInForStatements: true,
341-
insertSpaceBeforeAndAfterBinaryOperators: true,
342-
insertSpaceAfterConstructor: false,
343-
insertSpaceAfterKeywordsInControlFlowStatements: true,
344-
insertSpaceAfterFunctionKeywordForAnonymousFunctions: false,
345-
insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis: false,
346-
insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets: false,
347-
insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces: true,
348-
insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces: false,
349-
insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces: false,
350-
insertSpaceAfterTypeAssertion: false,
351-
placeOpenBraceOnNewLineForFunctions: false,
352-
placeOpenBraceOnNewLineForControlBlocks: false,
353-
insertSpaceBeforeTypeAnnotation: false
354-
};
332+
this.formatCodeSettings = ts.testFormatSettings;
355333

356334
// Open the first file by default
357335
this.openFile(0);
@@ -3389,8 +3367,8 @@ Actual: ${stringify(fullActual)}`);
33893367
}
33903368

33913369
public generateTypes(examples: ReadonlyArray<FourSlashInterface.GenerateTypesOptions>): void {
3392-
for (const { name = "example", value, output, outputBaseline } of examples) {
3393-
const actual = ts.generateTypesForModule(name, value, this.formatCodeSettings);
3370+
for (const { name = "example", value, global, output, outputBaseline } of examples) {
3371+
const actual = (global ? ts.generateTypesForGlobal : ts.generateTypesForModule)(name, value, this.formatCodeSettings);
33943372
if (outputBaseline) {
33953373
if (actual === undefined) throw ts.Debug.fail();
33963374
Harness.Baseline.runBaseline(ts.combinePaths("generateTypes", outputBaseline + ts.Extension.Dts), actual);
@@ -4536,6 +4514,7 @@ namespace FourSlashInterface {
45364514
export interface GenerateTypesOptions {
45374515
readonly name?: string;
45384516
readonly value: unknown;
4517+
readonly global?: boolean;
45394518
// Exactly one of these should be set:
45404519
readonly output?: string;
45414520
readonly outputBaseline?: string;
@@ -4689,7 +4668,7 @@ namespace FourSlashInterface {
46894668
}
46904669

46914670
public setOption(name: keyof ts.FormatCodeSettings, value: number | string | boolean): void {
4692-
this.state.formatCodeSettings[name] = value;
4671+
this.state.formatCodeSettings = { ...this.state.formatCodeSettings, [name]: value };
46934672
}
46944673
}
46954674

Collapse file

‎src/server/editorServices.ts‎

Copy file name to clipboardExpand all lines: src/server/editorServices.ts
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ namespace ts.server {
561561
this.typingsCache = new TypingsCache(this.typingsInstaller);
562562

563563
this.hostConfiguration = {
564-
formatCodeOptions: getDefaultFormatCodeSettings(this.host),
564+
formatCodeOptions: getDefaultFormatCodeSettings(this.host.newLine),
565565
preferences: emptyOptions,
566566
hostInfo: "Unknown host",
567567
extraFileExtensions: []
Collapse file

‎src/server/scriptInfo.ts‎

Copy file name to clipboardExpand all lines: src/server/scriptInfo.ts
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ namespace ts.server {
438438
setOptions(formatSettings: FormatCodeSettings, preferences: protocol.UserPreferences | undefined): void {
439439
if (formatSettings) {
440440
if (!this.formatSettings) {
441-
this.formatSettings = getDefaultFormatCodeSettings(this.host);
441+
this.formatSettings = getDefaultFormatCodeSettings(this.host.newLine);
442442
assign(this.formatSettings, formatSettings);
443443
}
444444
else {
Collapse file

‎src/server/utilities.ts‎

Copy file name to clipboardExpand all lines: src/server/utilities.ts
-24Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -56,30 +56,6 @@ namespace ts.server {
5656
}
5757
}
5858

59-
export function getDefaultFormatCodeSettings(host: ServerHost): FormatCodeSettings {
60-
return {
61-
indentSize: 4,
62-
tabSize: 4,
63-
newLineCharacter: host.newLine || "\n",
64-
convertTabsToSpaces: true,
65-
indentStyle: IndentStyle.Smart,
66-
insertSpaceAfterConstructor: false,
67-
insertSpaceAfterCommaDelimiter: true,
68-
insertSpaceAfterSemicolonInForStatements: true,
69-
insertSpaceBeforeAndAfterBinaryOperators: true,
70-
insertSpaceAfterKeywordsInControlFlowStatements: true,
71-
insertSpaceAfterFunctionKeywordForAnonymousFunctions: false,
72-
insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis: false,
73-
insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets: false,
74-
insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces: true,
75-
insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces: false,
76-
insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces: false,
77-
insertSpaceBeforeFunctionParenthesis: false,
78-
placeOpenBraceOnNewLineForFunctions: false,
79-
placeOpenBraceOnNewLineForControlBlocks: false,
80-
};
81-
}
82-
8359
export type NormalizedPath = string & { __normalizedPathTag: any };
8460

8561
export function toNormalizedPath(fileName: string): NormalizedPath {
Collapse file

‎src/services/codefixes/generateTypes.ts‎

Copy file name to clipboardExpand all lines: src/services/codefixes/generateTypes.ts
+18-7Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
1-
/* @internal */
21
namespace ts {
32
export function generateTypesForModule(name: string, moduleValue: unknown, formatSettings: FormatCodeSettings): string {
4-
return valueInfoToDeclarationFileText(inspectValue(name, moduleValue), formatSettings);
3+
return generateTypesForModuleOrGlobal(name, moduleValue, formatSettings, OutputKind.ExportEquals);
54
}
65

7-
export function valueInfoToDeclarationFileText(valueInfo: ValueInfo, formatSettings: FormatCodeSettings): string {
8-
return textChanges.getNewFileText(toStatements(valueInfo, OutputKind.ExportEquals), ScriptKind.TS, "\n", formatting.getFormatContext(formatSettings));
6+
export function generateTypesForGlobal(name: string, globalValue: unknown, formatSettings: FormatCodeSettings): string {
7+
return generateTypesForModuleOrGlobal(name, globalValue, formatSettings, OutputKind.Global);
98
}
109

11-
const enum OutputKind { ExportEquals, NamedExport, NamespaceMember }
10+
function generateTypesForModuleOrGlobal(name: string, globalValue: unknown, formatSettings: FormatCodeSettings, outputKind: OutputKind.ExportEquals | OutputKind.Global): string {
11+
return valueInfoToDeclarationFileText(inspectValue(name, globalValue), formatSettings, outputKind);
12+
}
13+
14+
/* @internal */
15+
export function valueInfoToDeclarationFileText(valueInfo: ValueInfo, formatSettings: FormatCodeSettings, outputKind: OutputKind.ExportEquals | OutputKind.Global = OutputKind.ExportEquals): string {
16+
return textChanges.getNewFileText(toStatements(valueInfo, outputKind), ScriptKind.TS, formatSettings.newLineCharacter || "\n", formatting.getFormatContext(formatSettings));
17+
}
18+
19+
const enum OutputKind { ExportEquals, NamedExport, NamespaceMember, Global }
1220
function toNamespaceMemberStatements(info: ValueInfo): ReadonlyArray<Statement> {
1321
return toStatements(info, OutputKind.NamespaceMember);
1422
}
@@ -18,7 +26,7 @@ namespace ts {
1826
if (!isValidIdentifier(name) || isDefault && kind !== OutputKind.NamedExport) return emptyArray;
1927

2028
const modifiers = isDefault && info.kind === ValueKind.FunctionOrClass ? [createModifier(SyntaxKind.ExportKeyword), createModifier(SyntaxKind.DefaultKeyword)]
21-
: kind === OutputKind.ExportEquals ? [createModifier(SyntaxKind.DeclareKeyword)]
29+
: kind === OutputKind.Global || kind === OutputKind.ExportEquals ? [createModifier(SyntaxKind.DeclareKeyword)]
2230
: kind === OutputKind.NamedExport ? [createModifier(SyntaxKind.ExportKeyword)]
2331
: undefined;
2432
const exportEquals = () => kind === OutputKind.ExportEquals ? [exportEqualsOrDefault(info.name, /*isExportEquals*/ true)] : emptyArray;
@@ -132,11 +140,14 @@ namespace ts {
132140
case ValueKind.FunctionOrClass:
133141
return createTypeReferenceNode("Function", /*typeArguments*/ undefined); // Normally we create a FunctionDeclaration, but this can happen for a function in an array.
134142
case ValueKind.Object:
135-
return createTypeLiteralNode(info.members.map(m => createPropertySignature(/*modifiers*/ undefined, m.name, /*questionToken*/ undefined, toType(m), /*initializer*/ undefined)));
143+
return createTypeLiteralNode(info.members.map(m => createPropertySignature(/*modifiers*/ undefined, toPropertyName(m.name), /*questionToken*/ undefined, toType(m), /*initializer*/ undefined)));
136144
default:
137145
return Debug.assertNever(info);
138146
}
139147
}
148+
function toPropertyName(name: string): Identifier | StringLiteral {
149+
return isIdentifierText(name, ScriptTarget.ESNext) ? createIdentifier(name) : createStringLiteral(name);
150+
}
140151

141152
// Parses assignments to "this.x" in the constructor into class property declarations
142153
function getConstructorFunctionInstanceProperties(fnAst: FunctionOrConstructorNode): ReadonlyArray<PropertyDeclaration> {
Collapse file

‎src/services/textChanges.ts‎

Copy file name to clipboardExpand all lines: src/services/textChanges.ts
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,7 @@ namespace ts.textChanges {
816816
const nonFormattedText = statements.map(s => getNonformattedText(s, oldFile, newLineCharacter).text).join(newLineCharacter);
817817
const sourceFile = createSourceFile("any file name", nonFormattedText, ScriptTarget.ESNext, /*setParentNodes*/ true, scriptKind);
818818
const changes = formatting.formatDocument(sourceFile, formatContext);
819-
return applyChanges(nonFormattedText, changes);
819+
return applyChanges(nonFormattedText, changes) + newLineCharacter;
820820
}
821821

822822
function computeNewText(change: Change, sourceFile: SourceFile, newLineCharacter: string, formatContext: formatting.FormatContext, validate: ValidateNonFormattedText | undefined): string {
Collapse file

‎src/services/types.ts‎

Copy file name to clipboardExpand all lines: src/services/types.ts
+42-40Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -712,49 +712,51 @@ namespace ts {
712712
}
713713

714714
export interface FormatCodeSettings extends EditorSettings {
715-
insertSpaceAfterCommaDelimiter?: boolean;
716-
insertSpaceAfterSemicolonInForStatements?: boolean;
717-
insertSpaceBeforeAndAfterBinaryOperators?: boolean;
718-
insertSpaceAfterConstructor?: boolean;
719-
insertSpaceAfterKeywordsInControlFlowStatements?: boolean;
720-
insertSpaceAfterFunctionKeywordForAnonymousFunctions?: boolean;
721-
insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis?: boolean;
722-
insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets?: boolean;
723-
insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces?: boolean;
724-
insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces?: boolean;
725-
insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces?: boolean;
726-
insertSpaceAfterTypeAssertion?: boolean;
727-
insertSpaceBeforeFunctionParenthesis?: boolean;
728-
placeOpenBraceOnNewLineForFunctions?: boolean;
729-
placeOpenBraceOnNewLineForControlBlocks?: boolean;
730-
insertSpaceBeforeTypeAnnotation?: boolean;
731-
indentMultiLineObjectLiteralBeginningOnBlankLine?: boolean;
715+
readonly insertSpaceAfterCommaDelimiter?: boolean;
716+
readonly insertSpaceAfterSemicolonInForStatements?: boolean;
717+
readonly insertSpaceBeforeAndAfterBinaryOperators?: boolean;
718+
readonly insertSpaceAfterConstructor?: boolean;
719+
readonly insertSpaceAfterKeywordsInControlFlowStatements?: boolean;
720+
readonly insertSpaceAfterFunctionKeywordForAnonymousFunctions?: boolean;
721+
readonly insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis?: boolean;
722+
readonly insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets?: boolean;
723+
readonly insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces?: boolean;
724+
readonly insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces?: boolean;
725+
readonly insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces?: boolean;
726+
readonly insertSpaceAfterTypeAssertion?: boolean;
727+
readonly insertSpaceBeforeFunctionParenthesis?: boolean;
728+
readonly placeOpenBraceOnNewLineForFunctions?: boolean;
729+
readonly placeOpenBraceOnNewLineForControlBlocks?: boolean;
730+
readonly insertSpaceBeforeTypeAnnotation?: boolean;
731+
readonly indentMultiLineObjectLiteralBeginningOnBlankLine?: boolean;
732+
}
733+
734+
export function getDefaultFormatCodeSettings(newLineCharacter?: string): FormatCodeSettings {
735+
return {
736+
indentSize: 4,
737+
tabSize: 4,
738+
newLineCharacter: newLineCharacter || "\n",
739+
convertTabsToSpaces: true,
740+
indentStyle: IndentStyle.Smart,
741+
insertSpaceAfterConstructor: false,
742+
insertSpaceAfterCommaDelimiter: true,
743+
insertSpaceAfterSemicolonInForStatements: true,
744+
insertSpaceBeforeAndAfterBinaryOperators: true,
745+
insertSpaceAfterKeywordsInControlFlowStatements: true,
746+
insertSpaceAfterFunctionKeywordForAnonymousFunctions: false,
747+
insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis: false,
748+
insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets: false,
749+
insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces: true,
750+
insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces: false,
751+
insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces: false,
752+
insertSpaceBeforeFunctionParenthesis: false,
753+
placeOpenBraceOnNewLineForFunctions: false,
754+
placeOpenBraceOnNewLineForControlBlocks: false,
755+
};
732756
}
733757

734758
/* @internal */
735-
export const testFormatSettings: FormatCodeSettings = {
736-
baseIndentSize: 0,
737-
indentSize: 4,
738-
tabSize: 4,
739-
newLineCharacter: "\n",
740-
convertTabsToSpaces: true,
741-
indentStyle: IndentStyle.Smart,
742-
insertSpaceAfterCommaDelimiter: true,
743-
insertSpaceAfterSemicolonInForStatements: true,
744-
insertSpaceBeforeAndAfterBinaryOperators: true,
745-
insertSpaceAfterConstructor: false,
746-
insertSpaceAfterKeywordsInControlFlowStatements: true,
747-
insertSpaceAfterFunctionKeywordForAnonymousFunctions: false,
748-
insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis: false,
749-
insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets: false,
750-
insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces: true,
751-
insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces: false,
752-
insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces: false,
753-
insertSpaceAfterTypeAssertion: false,
754-
placeOpenBraceOnNewLineForFunctions: false,
755-
placeOpenBraceOnNewLineForControlBlocks: false,
756-
insertSpaceBeforeTypeAnnotation: false
757-
};
759+
export const testFormatSettings = getDefaultFormatCodeSettings("\n");
758760

759761
export interface DefinitionInfo extends DocumentSpan {
760762
kind: ScriptElementKind;
Collapse file

‎src/testRunner/unittests/tsserverProjectSystem.ts‎

Copy file name to clipboardExpand all lines: src/testRunner/unittests/tsserverProjectSystem.ts
+4-7Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4497,26 +4497,23 @@ namespace ts.projectSystem {
44974497
const defaultSettings = projectService.getFormatCodeOptions(f1.path as server.NormalizedPath);
44984498

44994499
// set global settings
4500-
const newGlobalSettings1 = clone(defaultSettings);
4501-
newGlobalSettings1.placeOpenBraceOnNewLineForControlBlocks = !newGlobalSettings1.placeOpenBraceOnNewLineForControlBlocks;
4500+
const newGlobalSettings1 = { ...defaultSettings, placeOpenBraceOnNewLineForControlBlocks: !defaultSettings.placeOpenBraceOnNewLineForControlBlocks };
45024501
projectService.setHostConfiguration({ formatOptions: newGlobalSettings1 });
45034502

45044503
// get format options for file - should be equal to new global settings
45054504
const s1 = projectService.getFormatCodeOptions(server.toNormalizedPath(f1.path));
45064505
assert.deepEqual(s1, newGlobalSettings1, "file settings should be the same with global settings");
45074506

45084507
// set per file format options
4509-
const newPerFileSettings = clone(defaultSettings);
4510-
newPerFileSettings.insertSpaceAfterCommaDelimiter = !newPerFileSettings.insertSpaceAfterCommaDelimiter;
4508+
const newPerFileSettings = { ...defaultSettings, insertSpaceAfterCommaDelimiter: !defaultSettings.insertSpaceAfterCommaDelimiter };
45114509
projectService.setHostConfiguration({ formatOptions: newPerFileSettings, file: f1.path });
45124510

45134511
// get format options for file - should be equal to new per-file settings
45144512
const s2 = projectService.getFormatCodeOptions(server.toNormalizedPath(f1.path));
45154513
assert.deepEqual(s2, newPerFileSettings, "file settings should be the same with per-file settings");
45164514

45174515
// set new global settings - they should not affect ones that were set per-file
4518-
const newGlobalSettings2 = clone(defaultSettings);
4519-
newGlobalSettings2.insertSpaceAfterSemicolonInForStatements = !newGlobalSettings2.insertSpaceAfterSemicolonInForStatements;
4516+
const newGlobalSettings2 = { ...defaultSettings, insertSpaceAfterSemicolonInForStatements: !defaultSettings.insertSpaceAfterSemicolonInForStatements };
45204517
projectService.setHostConfiguration({ formatOptions: newGlobalSettings2 });
45214518

45224519
// get format options for file - should be equal to new per-file settings
@@ -6837,7 +6834,7 @@ namespace ts.projectSystem {
68376834
{
68386835
start: { line: 0, offset: 0 },
68396836
end: { line: 0, offset: 0 },
6840-
newText: "export const a = 0;",
6837+
newText: "export const a = 0;\n",
68416838
},
68426839
],
68436840
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.