mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
Add generateTypesForModule to public API (#28069)
* Add generateTypesForModule to public API * Avoid parameter initializer and update baselines
This commit is contained in:
@@ -329,29 +329,7 @@ namespace FourSlash {
|
||||
});
|
||||
}
|
||||
|
||||
this.formatCodeSettings = {
|
||||
baseIndentSize: 0,
|
||||
indentSize: 4,
|
||||
tabSize: 4,
|
||||
newLineCharacter: "\n",
|
||||
convertTabsToSpaces: true,
|
||||
indentStyle: ts.IndentStyle.Smart,
|
||||
insertSpaceAfterCommaDelimiter: true,
|
||||
insertSpaceAfterSemicolonInForStatements: true,
|
||||
insertSpaceBeforeAndAfterBinaryOperators: true,
|
||||
insertSpaceAfterConstructor: false,
|
||||
insertSpaceAfterKeywordsInControlFlowStatements: true,
|
||||
insertSpaceAfterFunctionKeywordForAnonymousFunctions: false,
|
||||
insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis: false,
|
||||
insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets: false,
|
||||
insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces: true,
|
||||
insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces: false,
|
||||
insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces: false,
|
||||
insertSpaceAfterTypeAssertion: false,
|
||||
placeOpenBraceOnNewLineForFunctions: false,
|
||||
placeOpenBraceOnNewLineForControlBlocks: false,
|
||||
insertSpaceBeforeTypeAnnotation: false
|
||||
};
|
||||
this.formatCodeSettings = ts.testFormatSettings;
|
||||
|
||||
// Open the first file by default
|
||||
this.openFile(0);
|
||||
@@ -3389,8 +3367,8 @@ Actual: ${stringify(fullActual)}`);
|
||||
}
|
||||
|
||||
public generateTypes(examples: ReadonlyArray<FourSlashInterface.GenerateTypesOptions>): void {
|
||||
for (const { name = "example", value, output, outputBaseline } of examples) {
|
||||
const actual = ts.generateTypesForModule(name, value, this.formatCodeSettings);
|
||||
for (const { name = "example", value, global, output, outputBaseline } of examples) {
|
||||
const actual = (global ? ts.generateTypesForGlobal : ts.generateTypesForModule)(name, value, this.formatCodeSettings);
|
||||
if (outputBaseline) {
|
||||
if (actual === undefined) throw ts.Debug.fail();
|
||||
Harness.Baseline.runBaseline(ts.combinePaths("generateTypes", outputBaseline + ts.Extension.Dts), actual);
|
||||
@@ -4536,6 +4514,7 @@ namespace FourSlashInterface {
|
||||
export interface GenerateTypesOptions {
|
||||
readonly name?: string;
|
||||
readonly value: unknown;
|
||||
readonly global?: boolean;
|
||||
// Exactly one of these should be set:
|
||||
readonly output?: string;
|
||||
readonly outputBaseline?: string;
|
||||
@@ -4689,7 +4668,7 @@ namespace FourSlashInterface {
|
||||
}
|
||||
|
||||
public setOption(name: keyof ts.FormatCodeSettings, value: number | string | boolean): void {
|
||||
this.state.formatCodeSettings[name] = value;
|
||||
this.state.formatCodeSettings = { ...this.state.formatCodeSettings, [name]: value };
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -561,7 +561,7 @@ namespace ts.server {
|
||||
this.typingsCache = new TypingsCache(this.typingsInstaller);
|
||||
|
||||
this.hostConfiguration = {
|
||||
formatCodeOptions: getDefaultFormatCodeSettings(this.host),
|
||||
formatCodeOptions: getDefaultFormatCodeSettings(this.host.newLine),
|
||||
preferences: emptyOptions,
|
||||
hostInfo: "Unknown host",
|
||||
extraFileExtensions: []
|
||||
|
||||
@@ -438,7 +438,7 @@ namespace ts.server {
|
||||
setOptions(formatSettings: FormatCodeSettings, preferences: protocol.UserPreferences | undefined): void {
|
||||
if (formatSettings) {
|
||||
if (!this.formatSettings) {
|
||||
this.formatSettings = getDefaultFormatCodeSettings(this.host);
|
||||
this.formatSettings = getDefaultFormatCodeSettings(this.host.newLine);
|
||||
assign(this.formatSettings, formatSettings);
|
||||
}
|
||||
else {
|
||||
|
||||
@@ -56,30 +56,6 @@ namespace ts.server {
|
||||
}
|
||||
}
|
||||
|
||||
export function getDefaultFormatCodeSettings(host: ServerHost): FormatCodeSettings {
|
||||
return {
|
||||
indentSize: 4,
|
||||
tabSize: 4,
|
||||
newLineCharacter: host.newLine || "\n",
|
||||
convertTabsToSpaces: true,
|
||||
indentStyle: IndentStyle.Smart,
|
||||
insertSpaceAfterConstructor: false,
|
||||
insertSpaceAfterCommaDelimiter: true,
|
||||
insertSpaceAfterSemicolonInForStatements: true,
|
||||
insertSpaceBeforeAndAfterBinaryOperators: true,
|
||||
insertSpaceAfterKeywordsInControlFlowStatements: true,
|
||||
insertSpaceAfterFunctionKeywordForAnonymousFunctions: false,
|
||||
insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis: false,
|
||||
insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets: false,
|
||||
insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces: true,
|
||||
insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces: false,
|
||||
insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces: false,
|
||||
insertSpaceBeforeFunctionParenthesis: false,
|
||||
placeOpenBraceOnNewLineForFunctions: false,
|
||||
placeOpenBraceOnNewLineForControlBlocks: false,
|
||||
};
|
||||
}
|
||||
|
||||
export type NormalizedPath = string & { __normalizedPathTag: any };
|
||||
|
||||
export function toNormalizedPath(fileName: string): NormalizedPath {
|
||||
|
||||
@@ -1,14 +1,22 @@
|
||||
/* @internal */
|
||||
namespace ts {
|
||||
export function generateTypesForModule(name: string, moduleValue: unknown, formatSettings: FormatCodeSettings): string {
|
||||
return valueInfoToDeclarationFileText(inspectValue(name, moduleValue), formatSettings);
|
||||
return generateTypesForModuleOrGlobal(name, moduleValue, formatSettings, OutputKind.ExportEquals);
|
||||
}
|
||||
|
||||
export function valueInfoToDeclarationFileText(valueInfo: ValueInfo, formatSettings: FormatCodeSettings): string {
|
||||
return textChanges.getNewFileText(toStatements(valueInfo, OutputKind.ExportEquals), ScriptKind.TS, "\n", formatting.getFormatContext(formatSettings));
|
||||
export function generateTypesForGlobal(name: string, globalValue: unknown, formatSettings: FormatCodeSettings): string {
|
||||
return generateTypesForModuleOrGlobal(name, globalValue, formatSettings, OutputKind.Global);
|
||||
}
|
||||
|
||||
const enum OutputKind { ExportEquals, NamedExport, NamespaceMember }
|
||||
function generateTypesForModuleOrGlobal(name: string, globalValue: unknown, formatSettings: FormatCodeSettings, outputKind: OutputKind.ExportEquals | OutputKind.Global): string {
|
||||
return valueInfoToDeclarationFileText(inspectValue(name, globalValue), formatSettings, outputKind);
|
||||
}
|
||||
|
||||
/* @internal */
|
||||
export function valueInfoToDeclarationFileText(valueInfo: ValueInfo, formatSettings: FormatCodeSettings, outputKind: OutputKind.ExportEquals | OutputKind.Global = OutputKind.ExportEquals): string {
|
||||
return textChanges.getNewFileText(toStatements(valueInfo, outputKind), ScriptKind.TS, formatSettings.newLineCharacter || "\n", formatting.getFormatContext(formatSettings));
|
||||
}
|
||||
|
||||
const enum OutputKind { ExportEquals, NamedExport, NamespaceMember, Global }
|
||||
function toNamespaceMemberStatements(info: ValueInfo): ReadonlyArray<Statement> {
|
||||
return toStatements(info, OutputKind.NamespaceMember);
|
||||
}
|
||||
@@ -18,7 +26,7 @@ namespace ts {
|
||||
if (!isValidIdentifier(name) || isDefault && kind !== OutputKind.NamedExport) return emptyArray;
|
||||
|
||||
const modifiers = isDefault && info.kind === ValueKind.FunctionOrClass ? [createModifier(SyntaxKind.ExportKeyword), createModifier(SyntaxKind.DefaultKeyword)]
|
||||
: kind === OutputKind.ExportEquals ? [createModifier(SyntaxKind.DeclareKeyword)]
|
||||
: kind === OutputKind.Global || kind === OutputKind.ExportEquals ? [createModifier(SyntaxKind.DeclareKeyword)]
|
||||
: kind === OutputKind.NamedExport ? [createModifier(SyntaxKind.ExportKeyword)]
|
||||
: undefined;
|
||||
const exportEquals = () => kind === OutputKind.ExportEquals ? [exportEqualsOrDefault(info.name, /*isExportEquals*/ true)] : emptyArray;
|
||||
@@ -132,11 +140,14 @@ namespace ts {
|
||||
case ValueKind.FunctionOrClass:
|
||||
return createTypeReferenceNode("Function", /*typeArguments*/ undefined); // Normally we create a FunctionDeclaration, but this can happen for a function in an array.
|
||||
case ValueKind.Object:
|
||||
return createTypeLiteralNode(info.members.map(m => createPropertySignature(/*modifiers*/ undefined, m.name, /*questionToken*/ undefined, toType(m), /*initializer*/ undefined)));
|
||||
return createTypeLiteralNode(info.members.map(m => createPropertySignature(/*modifiers*/ undefined, toPropertyName(m.name), /*questionToken*/ undefined, toType(m), /*initializer*/ undefined)));
|
||||
default:
|
||||
return Debug.assertNever(info);
|
||||
}
|
||||
}
|
||||
function toPropertyName(name: string): Identifier | StringLiteral {
|
||||
return isIdentifierText(name, ScriptTarget.ESNext) ? createIdentifier(name) : createStringLiteral(name);
|
||||
}
|
||||
|
||||
// Parses assignments to "this.x" in the constructor into class property declarations
|
||||
function getConstructorFunctionInstanceProperties(fnAst: FunctionOrConstructorNode): ReadonlyArray<PropertyDeclaration> {
|
||||
|
||||
@@ -816,7 +816,7 @@ namespace ts.textChanges {
|
||||
const nonFormattedText = statements.map(s => getNonformattedText(s, oldFile, newLineCharacter).text).join(newLineCharacter);
|
||||
const sourceFile = createSourceFile("any file name", nonFormattedText, ScriptTarget.ESNext, /*setParentNodes*/ true, scriptKind);
|
||||
const changes = formatting.formatDocument(sourceFile, formatContext);
|
||||
return applyChanges(nonFormattedText, changes);
|
||||
return applyChanges(nonFormattedText, changes) + newLineCharacter;
|
||||
}
|
||||
|
||||
function computeNewText(change: Change, sourceFile: SourceFile, newLineCharacter: string, formatContext: formatting.FormatContext, validate: ValidateNonFormattedText | undefined): string {
|
||||
|
||||
+42
-40
@@ -712,49 +712,51 @@ namespace ts {
|
||||
}
|
||||
|
||||
export interface FormatCodeSettings extends EditorSettings {
|
||||
insertSpaceAfterCommaDelimiter?: boolean;
|
||||
insertSpaceAfterSemicolonInForStatements?: boolean;
|
||||
insertSpaceBeforeAndAfterBinaryOperators?: boolean;
|
||||
insertSpaceAfterConstructor?: boolean;
|
||||
insertSpaceAfterKeywordsInControlFlowStatements?: boolean;
|
||||
insertSpaceAfterFunctionKeywordForAnonymousFunctions?: boolean;
|
||||
insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis?: boolean;
|
||||
insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets?: boolean;
|
||||
insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces?: boolean;
|
||||
insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces?: boolean;
|
||||
insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces?: boolean;
|
||||
insertSpaceAfterTypeAssertion?: boolean;
|
||||
insertSpaceBeforeFunctionParenthesis?: boolean;
|
||||
placeOpenBraceOnNewLineForFunctions?: boolean;
|
||||
placeOpenBraceOnNewLineForControlBlocks?: boolean;
|
||||
insertSpaceBeforeTypeAnnotation?: boolean;
|
||||
indentMultiLineObjectLiteralBeginningOnBlankLine?: boolean;
|
||||
readonly insertSpaceAfterCommaDelimiter?: boolean;
|
||||
readonly insertSpaceAfterSemicolonInForStatements?: boolean;
|
||||
readonly insertSpaceBeforeAndAfterBinaryOperators?: boolean;
|
||||
readonly insertSpaceAfterConstructor?: boolean;
|
||||
readonly insertSpaceAfterKeywordsInControlFlowStatements?: boolean;
|
||||
readonly insertSpaceAfterFunctionKeywordForAnonymousFunctions?: boolean;
|
||||
readonly insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis?: boolean;
|
||||
readonly insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets?: boolean;
|
||||
readonly insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces?: boolean;
|
||||
readonly insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces?: boolean;
|
||||
readonly insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces?: boolean;
|
||||
readonly insertSpaceAfterTypeAssertion?: boolean;
|
||||
readonly insertSpaceBeforeFunctionParenthesis?: boolean;
|
||||
readonly placeOpenBraceOnNewLineForFunctions?: boolean;
|
||||
readonly placeOpenBraceOnNewLineForControlBlocks?: boolean;
|
||||
readonly insertSpaceBeforeTypeAnnotation?: boolean;
|
||||
readonly indentMultiLineObjectLiteralBeginningOnBlankLine?: boolean;
|
||||
}
|
||||
|
||||
export function getDefaultFormatCodeSettings(newLineCharacter?: string): FormatCodeSettings {
|
||||
return {
|
||||
indentSize: 4,
|
||||
tabSize: 4,
|
||||
newLineCharacter: newLineCharacter || "\n",
|
||||
convertTabsToSpaces: true,
|
||||
indentStyle: IndentStyle.Smart,
|
||||
insertSpaceAfterConstructor: false,
|
||||
insertSpaceAfterCommaDelimiter: true,
|
||||
insertSpaceAfterSemicolonInForStatements: true,
|
||||
insertSpaceBeforeAndAfterBinaryOperators: true,
|
||||
insertSpaceAfterKeywordsInControlFlowStatements: true,
|
||||
insertSpaceAfterFunctionKeywordForAnonymousFunctions: false,
|
||||
insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis: false,
|
||||
insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets: false,
|
||||
insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces: true,
|
||||
insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces: false,
|
||||
insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces: false,
|
||||
insertSpaceBeforeFunctionParenthesis: false,
|
||||
placeOpenBraceOnNewLineForFunctions: false,
|
||||
placeOpenBraceOnNewLineForControlBlocks: false,
|
||||
};
|
||||
}
|
||||
|
||||
/* @internal */
|
||||
export const testFormatSettings: FormatCodeSettings = {
|
||||
baseIndentSize: 0,
|
||||
indentSize: 4,
|
||||
tabSize: 4,
|
||||
newLineCharacter: "\n",
|
||||
convertTabsToSpaces: true,
|
||||
indentStyle: IndentStyle.Smart,
|
||||
insertSpaceAfterCommaDelimiter: true,
|
||||
insertSpaceAfterSemicolonInForStatements: true,
|
||||
insertSpaceBeforeAndAfterBinaryOperators: true,
|
||||
insertSpaceAfterConstructor: false,
|
||||
insertSpaceAfterKeywordsInControlFlowStatements: true,
|
||||
insertSpaceAfterFunctionKeywordForAnonymousFunctions: false,
|
||||
insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis: false,
|
||||
insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets: false,
|
||||
insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces: true,
|
||||
insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces: false,
|
||||
insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces: false,
|
||||
insertSpaceAfterTypeAssertion: false,
|
||||
placeOpenBraceOnNewLineForFunctions: false,
|
||||
placeOpenBraceOnNewLineForControlBlocks: false,
|
||||
insertSpaceBeforeTypeAnnotation: false
|
||||
};
|
||||
export const testFormatSettings = getDefaultFormatCodeSettings("\n");
|
||||
|
||||
export interface DefinitionInfo extends DocumentSpan {
|
||||
kind: ScriptElementKind;
|
||||
|
||||
@@ -4497,8 +4497,7 @@ namespace ts.projectSystem {
|
||||
const defaultSettings = projectService.getFormatCodeOptions(f1.path as server.NormalizedPath);
|
||||
|
||||
// set global settings
|
||||
const newGlobalSettings1 = clone(defaultSettings);
|
||||
newGlobalSettings1.placeOpenBraceOnNewLineForControlBlocks = !newGlobalSettings1.placeOpenBraceOnNewLineForControlBlocks;
|
||||
const newGlobalSettings1 = { ...defaultSettings, placeOpenBraceOnNewLineForControlBlocks: !defaultSettings.placeOpenBraceOnNewLineForControlBlocks };
|
||||
projectService.setHostConfiguration({ formatOptions: newGlobalSettings1 });
|
||||
|
||||
// get format options for file - should be equal to new global settings
|
||||
@@ -4506,8 +4505,7 @@ namespace ts.projectSystem {
|
||||
assert.deepEqual(s1, newGlobalSettings1, "file settings should be the same with global settings");
|
||||
|
||||
// set per file format options
|
||||
const newPerFileSettings = clone(defaultSettings);
|
||||
newPerFileSettings.insertSpaceAfterCommaDelimiter = !newPerFileSettings.insertSpaceAfterCommaDelimiter;
|
||||
const newPerFileSettings = { ...defaultSettings, insertSpaceAfterCommaDelimiter: !defaultSettings.insertSpaceAfterCommaDelimiter };
|
||||
projectService.setHostConfiguration({ formatOptions: newPerFileSettings, file: f1.path });
|
||||
|
||||
// get format options for file - should be equal to new per-file settings
|
||||
@@ -4515,8 +4513,7 @@ namespace ts.projectSystem {
|
||||
assert.deepEqual(s2, newPerFileSettings, "file settings should be the same with per-file settings");
|
||||
|
||||
// set new global settings - they should not affect ones that were set per-file
|
||||
const newGlobalSettings2 = clone(defaultSettings);
|
||||
newGlobalSettings2.insertSpaceAfterSemicolonInForStatements = !newGlobalSettings2.insertSpaceAfterSemicolonInForStatements;
|
||||
const newGlobalSettings2 = { ...defaultSettings, insertSpaceAfterSemicolonInForStatements: !defaultSettings.insertSpaceAfterSemicolonInForStatements };
|
||||
projectService.setHostConfiguration({ formatOptions: newGlobalSettings2 });
|
||||
|
||||
// get format options for file - should be equal to new per-file settings
|
||||
@@ -6837,7 +6834,7 @@ namespace ts.projectSystem {
|
||||
{
|
||||
start: { line: 0, offset: 0 },
|
||||
end: { line: 0, offset: 0 },
|
||||
newText: "export const a = 0;",
|
||||
newText: "export const a = 0;\n",
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user