diff --git a/package.json b/package.json index 55acc40268d..159788e697f 100644 --- a/package.json +++ b/package.json @@ -75,6 +75,7 @@ "gulp-typescript": "latest", "istanbul": "latest", "jake": "latest", + "lodash": "4.17.10", "merge2": "latest", "minimist": "latest", "mkdirp": "latest", diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index f4fc99406e0..f4bc6325092 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -601,6 +601,8 @@ namespace ts { FunctionFacts = FunctionStrictFacts | EQUndefined | EQNull | EQUndefinedOrNull | Falsy, UndefinedFacts = TypeofNEString | TypeofNENumber | TypeofNEBoolean | TypeofNESymbol | TypeofNEObject | TypeofNEFunction | TypeofNEHostObject | EQUndefined | EQUndefinedOrNull | NENull | Falsy, NullFacts = TypeofEQObject | TypeofNEString | TypeofNENumber | TypeofNEBoolean | TypeofNESymbol | TypeofNEFunction | TypeofNEHostObject | EQNull | EQUndefinedOrNull | NEUndefined | Falsy, + EmptyObjectStrictFacts = All & ~(EQUndefined | EQNull | EQUndefinedOrNull), + EmptyObjectFacts = All, } const typeofEQFacts = createMapFromTemplate({ @@ -11836,8 +11838,12 @@ namespace ts { const simplified = getSimplifiedType((target).type); const constraint = simplified !== (target).type ? simplified : getConstraintOfType((target).type); if (constraint) { - if (result = isRelatedTo(source, getIndexType(constraint, (target as IndexType).stringsOnly), reportErrors)) { - return result; + // We require Ternary.True here such that circular constraints don't cause + // false positives. For example, given 'T extends { [K in keyof T]: string }', + // 'keyof T' has itself as its constraint and produces a Ternary.Maybe when + // related to other types. + if (isRelatedTo(source, getIndexType(constraint, (target as IndexType).stringsOnly), reportErrors) === Ternary.True) { + return Ternary.True; } } } @@ -14241,9 +14247,11 @@ namespace ts { (type === falseType || type === regularFalseType) ? TypeFacts.FalseFacts : TypeFacts.TrueFacts; } if (flags & TypeFlags.Object) { - return isFunctionObjectType(type) ? - strictNullChecks ? TypeFacts.FunctionStrictFacts : TypeFacts.FunctionFacts : - strictNullChecks ? TypeFacts.ObjectStrictFacts : TypeFacts.ObjectFacts; + return getObjectFlags(type) & ObjectFlags.Anonymous && isEmptyObjectType(type) ? + strictNullChecks ? TypeFacts.EmptyObjectStrictFacts : TypeFacts.EmptyObjectFacts : + isFunctionObjectType(type) ? + strictNullChecks ? TypeFacts.FunctionStrictFacts : TypeFacts.FunctionFacts : + strictNullChecks ? TypeFacts.ObjectStrictFacts : TypeFacts.ObjectFacts; } if (flags & (TypeFlags.Void | TypeFlags.Undefined)) { return TypeFacts.UndefinedFacts; @@ -15163,23 +15171,24 @@ namespace ts { return getTypeWithFacts(assumeTrue ? mapType(type, narrowTypeForTypeof) : type, facts); function narrowTypeForTypeof(type: Type) { - if (assumeTrue && !(type.flags & TypeFlags.Union)) { - if (type.flags & TypeFlags.Unknown && literal.text === "object") { - return getUnionType([nonPrimitiveType, nullType]); + if (type.flags & TypeFlags.Unknown && literal.text === "object") { + return getUnionType([nonPrimitiveType, nullType]); + } + // We narrow a non-union type to an exact primitive type if the non-union type + // is a supertype of that primitive type. For example, type 'any' can be narrowed + // to one of the primitive types. + const targetType = literal.text === "function" ? globalFunctionType : typeofTypesByName.get(literal.text); + if (targetType) { + if (isTypeSubtypeOf(type, targetType)) { + return type; } - // We narrow a non-union type to an exact primitive type if the non-union type - // is a supertype of that primitive type. For example, type 'any' can be narrowed - // to one of the primitive types. - const targetType = literal.text === "function" ? globalFunctionType : typeofTypesByName.get(literal.text); - if (targetType) { - if (isTypeSubtypeOf(targetType, type)) { - return isTypeAny(type) ? targetType : getIntersectionType([type, targetType]); // Intersection to handle `string` being a subtype of `keyof T` - } - if (type.flags & TypeFlags.Instantiable) { - const constraint = getBaseConstraintOfType(type) || anyType; - if (isTypeSubtypeOf(targetType, constraint)) { - return getIntersectionType([type, targetType]); - } + if (isTypeSubtypeOf(targetType, type)) { + return targetType; + } + if (type.flags & TypeFlags.Instantiable) { + const constraint = getBaseConstraintOfType(type) || anyType; + if (isTypeSubtypeOf(targetType, constraint)) { + return getIntersectionType([type, targetType]); } } } diff --git a/src/compiler/core.ts b/src/compiler/core.ts index 45b9351759c..22fc19bcc8a 100644 --- a/src/compiler/core.ts +++ b/src/compiler/core.ts @@ -1409,9 +1409,12 @@ namespace ts { /** * Tests whether a value is string */ - export function isString(text: any): text is string { + export function isString(text: unknown): text is string { return typeof text === "string"; } + export function isNumber(x: unknown): x is number { + return typeof x === "number"; + } export function tryCast(value: TIn | undefined, test: (value: TIn) => value is TOut): TOut | undefined; export function tryCast(value: T, test: (value: T) => boolean): T | undefined; @@ -1534,6 +1537,7 @@ namespace ts { * Every function should be assignable to this, but this should not be assignable to every function. */ export type AnyFunction = (...args: never[]) => void; + export type AnyConstructor = new (...args: unknown[]) => unknown; export namespace Debug { export let currentAssertionLevel = AssertionLevel.None; @@ -2125,4 +2129,8 @@ namespace ts { deleted(oldItems[oldIndex++]); } } + + export function fill(length: number, cb: (index: number) => T): T[] { + return new Array(length).fill(0).map((_, i) => cb(i)); + } } diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index eb83d48ae75..706a3552765 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -4671,5 +4671,13 @@ "Convert all to async functions": { "category": "Message", "code": 95066 + }, + "Generate types for '{0}'": { + "category": "Message", + "code": 95067 + }, + "Generate types for all packages without types": { + "category": "Message", + "code": 95068 } -} +} \ No newline at end of file diff --git a/src/compiler/factory.ts b/src/compiler/factory.ts index 984e6d59b2e..4b2855790a9 100644 --- a/src/compiler/factory.ts +++ b/src/compiler/factory.ts @@ -235,7 +235,7 @@ namespace ts { // Modifiers - export function createModifier(kind: T) { + export function createModifier(kind: T): Token { return createToken(kind); } diff --git a/src/compiler/inspectValue.ts b/src/compiler/inspectValue.ts new file mode 100644 index 00000000000..86b45b65031 --- /dev/null +++ b/src/compiler/inspectValue.ts @@ -0,0 +1,159 @@ +/* @internal */ +namespace ts { + export interface InspectValueOptions { + readonly fileNameToRequire: string; + } + + export const enum ValueKind { Const, Array, FunctionOrClass, Object } + export interface ValueInfoBase { + readonly name: string; + } + export type ValueInfo = ValueInfoSimple | ValueInfoArray | ValueInfoFunctionOrClass | ValueInfoObject; + export interface ValueInfoSimple extends ValueInfoBase { + readonly kind: ValueKind.Const; + readonly typeName: string; + readonly comment?: string | undefined; + } + export interface ValueInfoFunctionOrClass extends ValueInfoBase { + readonly kind: ValueKind.FunctionOrClass; + readonly source: string | number; // For a native function, this is the length. + readonly prototypeMembers: ReadonlyArray; + readonly namespaceMembers: ReadonlyArray; + } + export interface ValueInfoArray extends ValueInfoBase { + readonly kind: ValueKind.Array; + readonly inner: ValueInfo; + } + export interface ValueInfoObject extends ValueInfoBase { + readonly kind: ValueKind.Object; + readonly members: ReadonlyArray; + } + + export function inspectModule(fileNameToRequire: string): ValueInfo { + return inspectValue(removeFileExtension(getBaseFileName(fileNameToRequire)), tryRequire(fileNameToRequire)); + } + + export function inspectValue(name: string, value: unknown): ValueInfo { + return getValueInfo(name, value, getRecurser()); + } + + type Recurser = (obj: unknown, name: string, cbOk: () => T, cbFail: (isCircularReference: boolean, keyStack: ReadonlyArray) => T) => T; + function getRecurser(): Recurser { + const seen = new Set(); + const nameStack: string[] = []; + return (obj, name, cbOk, cbFail) => { + if (seen.has(obj) || nameStack.length > 4) { + return cbFail(seen.has(obj), nameStack); + } + + seen.add(obj); + nameStack.push(name); + const res = cbOk(); + nameStack.pop(); + seen.delete(obj); + return res; + }; + } + + function getValueInfo(name: string, value: unknown, recurser: Recurser): ValueInfo { + return recurser(value, name, + (): ValueInfo => { + if (typeof value === "function") return getFunctionOrClassInfo(value as AnyFunction, name, recurser); + if (typeof value === "object") { + const builtin = getBuiltinType(name, value as object, recurser); + if (builtin !== undefined) return builtin; + const entries = getEntriesOfObject(value as object); + return { kind: ValueKind.Object, name, members: flatMap(entries, ({ key, value }) => getValueInfo(key, value, recurser)) }; + } + return { kind: ValueKind.Const, name, typeName: isNullOrUndefined(value) ? "any" : typeof value }; + }, + (isCircularReference, keyStack) => anyValue(name, ` ${isCircularReference ? "Circular reference" : "Too-deep object hierarchy"} from ${keyStack.join(".")}`)); + } + + function getFunctionOrClassInfo(fn: AnyFunction, name: string, recurser: Recurser): ValueInfoFunctionOrClass { + const prototypeMembers = getPrototypeMembers(fn, recurser); + const namespaceMembers = flatMap(getEntriesOfObject(fn), ({ key, value }) => getValueInfo(key, value, recurser)); + const toString = cast(Function.prototype.toString.call(fn), isString); + const source = stringContains(toString, "{ [native code] }") ? getFunctionLength(fn) : toString; + return { kind: ValueKind.FunctionOrClass, name, source, namespaceMembers, prototypeMembers }; + } + + const builtins: () => ReadonlyMap = memoize(() => { + const map = createMap(); + for (const { key, value } of getEntriesOfObject(global)) { + if (typeof value === "function" && typeof value.prototype === "object" && value !== Object) { + map.set(key, value as AnyConstructor); + } + } + return map; + }); + function getBuiltinType(name: string, value: object, recurser: Recurser): ValueInfo | undefined { + return isArray(value) + ? { name, kind: ValueKind.Array, inner: value.length && getValueInfo("element", first(value), recurser) || anyValue(name) } + : forEachEntry(builtins(), (builtin, builtinName): ValueInfo | undefined => + value instanceof builtin ? { kind: ValueKind.Const, name, typeName: builtinName } : undefined); + } + + function getPrototypeMembers(fn: AnyFunction, recurser: Recurser): ReadonlyArray { + const prototype = fn.prototype as unknown; + // tslint:disable-next-line no-unnecessary-type-assertion (TODO: update LKG and it will really be unnecessary) + return typeof prototype !== "object" || prototype === null ? emptyArray : mapDefined(getEntriesOfObject(prototype as object), ({ key, value }) => + key === "constructor" ? undefined : getValueInfo(key, value, recurser)); + } + + const ignoredProperties: ReadonlySet = new Set(["arguments", "caller", "constructor", "eval", "super_"]); + const reservedFunctionProperties: ReadonlySet = new Set(Object.getOwnPropertyNames(noop)); + interface ObjectEntry { readonly key: string; readonly value: unknown; } + function getEntriesOfObject(obj: object): ReadonlyArray { + const seen = createMap(); + const entries: ObjectEntry[] = []; + let chain = obj; + while (!isNullOrUndefined(chain) && chain !== Object.prototype && chain !== Function.prototype) { + for (const key of Object.getOwnPropertyNames(chain)) { + if (!isJsPrivate(key) && + !ignoredProperties.has(key) && + (typeof obj !== "function" || !reservedFunctionProperties.has(key)) && + // Don't add property from a higher prototype if it already exists in a lower one + addToSeen(seen, key)) { + const value = safeGetPropertyOfObject(chain, key); + // Don't repeat "toString" that matches signature from Object.prototype + if (!(key === "toString" && typeof value === "function" && value.length === 0)) { + entries.push({ key, value }); + } + } + } + chain = Object.getPrototypeOf(chain); + } + return entries.sort((e1, e2) => compareStringsCaseSensitive(e1.key, e2.key)); + } + + function getFunctionLength(fn: AnyFunction): number { + return tryCast(safeGetPropertyOfObject(fn, "length"), isNumber) || 0; + } + + function safeGetPropertyOfObject(obj: object, key: string): unknown { + const desc = Object.getOwnPropertyDescriptor(obj, key); + return desc && desc.value; + } + + function isNullOrUndefined(value: unknown): value is null | undefined { + return value == null; // tslint:disable-line + } + + function anyValue(name: string, comment?: string): ValueInfo { + return { kind: ValueKind.Const, name, typeName: "any", comment }; + } + + export function isJsPrivate(name: string): boolean { + return name.startsWith("_"); + } + + function tryRequire(fileNameToRequire: string): unknown { + try { + return require(fileNameToRequire); + } + catch { + return undefined; + } + } +} diff --git a/src/compiler/moduleNameResolver.ts b/src/compiler/moduleNameResolver.ts index 335e47286b7..1d912faa116 100644 --- a/src/compiler/moduleNameResolver.ts +++ b/src/compiler/moduleNameResolver.ts @@ -779,14 +779,23 @@ namespace ts { */ /* @internal */ export function resolveJSModule(moduleName: string, initialDir: string, host: ModuleResolutionHost): string { - const { resolvedModule, failedLookupLocations } = - nodeModuleNameResolverWorker(moduleName, initialDir, { moduleResolution: ModuleResolutionKind.NodeJs, allowJs: true }, host, /*cache*/ undefined, /*jsOnly*/ true); + const { resolvedModule, failedLookupLocations } = tryResolveJSModuleWorker(moduleName, initialDir, host); if (!resolvedModule) { throw new Error(`Could not resolve JS module '${moduleName}' starting at '${initialDir}'. Looked in: ${failedLookupLocations.join(", ")}`); } return resolvedModule.resolvedFileName; } + /* @internal */ + export function tryResolveJSModule(moduleName: string, initialDir: string, host: ModuleResolutionHost): string | undefined { + const { resolvedModule } = tryResolveJSModuleWorker(moduleName, initialDir, host); + return resolvedModule && resolvedModule.resolvedFileName; + } + + function tryResolveJSModuleWorker(moduleName: string, initialDir: string, host: ModuleResolutionHost): ResolvedModuleWithFailedLookupLocations { + return nodeModuleNameResolverWorker(moduleName, initialDir, { moduleResolution: ModuleResolutionKind.NodeJs, allowJs: true }, host, /*cache*/ undefined, /*jsOnly*/ true); + } + export function nodeModuleNameResolver(moduleName: string, containingFile: string, compilerOptions: CompilerOptions, host: ModuleResolutionHost, cache?: ModuleResolutionCache): ResolvedModuleWithFailedLookupLocations { return nodeModuleNameResolverWorker(moduleName, getDirectoryPath(containingFile), compilerOptions, host, cache, /*jsOnly*/ false); } diff --git a/src/compiler/program.ts b/src/compiler/program.ts index d0605bd55d9..44eae36d654 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -207,7 +207,7 @@ namespace ts { ...program.getSemanticDiagnostics(sourceFile, cancellationToken) ]; - if (program.getCompilerOptions().declaration) { + if (getEmitDeclarations(program.getCompilerOptions())) { addRange(diagnostics, program.getDeclarationDiagnostics(sourceFile, cancellationToken)); } @@ -817,9 +817,9 @@ namespace ts { // If a rootDir is specified use it as the commonSourceDirectory commonSourceDirectory = getNormalizedAbsolutePath(options.rootDir, currentDirectory); } - else if (options.composite) { + else if (options.composite && options.configFilePath) { // Project compilations never infer their root from the input source paths - commonSourceDirectory = getDirectoryPath(normalizeSlashes(options.configFilePath!)); // TODO: GH#18217 + commonSourceDirectory = getDirectoryPath(normalizeSlashes(options.configFilePath)); checkSourceFilesBelongToPath(emittedFiles, commonSourceDirectory); } else { @@ -1392,7 +1392,7 @@ namespace ts { ...program.getSemanticDiagnostics(sourceFile, cancellationToken) ]; - if (diagnostics.length === 0 && program.getCompilerOptions().declaration) { + if (diagnostics.length === 0 && getEmitDeclarations(program.getCompilerOptions())) { declarationDiagnostics = program.getDeclarationDiagnostics(/*sourceFile*/ undefined, cancellationToken); } @@ -2410,8 +2410,8 @@ namespace ts { } if (options.isolatedModules) { - if (options.declaration) { - createDiagnosticForOptionName(Diagnostics.Option_0_cannot_be_specified_with_option_1, "declaration", "isolatedModules"); + if (getEmitDeclarations(options)) { + createDiagnosticForOptionName(Diagnostics.Option_0_cannot_be_specified_with_option_1, getEmitDeclarationOptionName(options), "isolatedModules"); } if (options.noEmitOnError) { @@ -2535,8 +2535,8 @@ namespace ts { } if (options.declarationDir) { - if (!options.declaration) { - createDiagnosticForOptionName(Diagnostics.Option_0_cannot_be_specified_without_specifying_option_1, "declarationDir", "declaration"); + if (!getEmitDeclarations(options)) { + createDiagnosticForOptionName(Diagnostics.Option_0_cannot_be_specified_without_specifying_option_1_or_option_2, "declarationDir", "declaration", "composite"); } if (options.out || options.outFile) { createDiagnosticForOptionName(Diagnostics.Option_0_cannot_be_specified_with_option_1, "declarationDir", options.out ? "out" : "outFile"); @@ -2544,7 +2544,7 @@ namespace ts { } if (options.declarationMap && !getEmitDeclarations(options)) { - createDiagnosticForOptionName(Diagnostics.Option_0_cannot_be_specified_without_specifying_option_1, "declarationMap", "declaration"); + createDiagnosticForOptionName(Diagnostics.Option_0_cannot_be_specified_without_specifying_option_1_or_option_2, "declarationMap", "declaration", "composite"); } if (options.lib && options.noLib) { @@ -2612,8 +2612,8 @@ namespace ts { } } - if (!options.noEmit && options.allowJs && options.declaration) { - createDiagnosticForOptionName(Diagnostics.Option_0_cannot_be_specified_with_option_1, "allowJs", "declaration"); + if (!options.noEmit && options.allowJs && getEmitDeclarations(options)) { + createDiagnosticForOptionName(Diagnostics.Option_0_cannot_be_specified_with_option_1, "allowJs", getEmitDeclarationOptionName(options)); } if (options.checkJs && !options.allowJs) { @@ -2621,8 +2621,8 @@ namespace ts { } if (options.emitDeclarationOnly) { - if (!options.declaration) { - createDiagnosticForOptionName(Diagnostics.Option_0_cannot_be_specified_without_specifying_option_1, "emitDeclarationOnly", "declaration"); + if (!getEmitDeclarations(options)) { + createDiagnosticForOptionName(Diagnostics.Option_0_cannot_be_specified_without_specifying_option_1_or_option_2, "emitDeclarationOnly", "declaration", "composite"); } if (options.noEmit) { @@ -2880,6 +2880,9 @@ namespace ts { return resolveConfigFileProjectName(passedInRef.path); } + function getEmitDeclarationOptionName(options: CompilerOptions) { + return options.declaration ? "declaration" : "composite"; + } /* @internal */ /** * Returns a DiagnosticMessage if we won't include a resolved module due to its extension. diff --git a/src/compiler/tsconfig.json b/src/compiler/tsconfig.json index 2d3cbcf54fe..4822f1dc06a 100644 --- a/src/compiler/tsconfig.json +++ b/src/compiler/tsconfig.json @@ -52,6 +52,7 @@ "resolutionCache.ts", "moduleSpecifiers.ts", "watch.ts", - "tsbuild.ts" + "tsbuild.ts", + "inspectValue.ts", ] } diff --git a/src/harness/fourslash.ts b/src/harness/fourslash.ts index fb1ed9fcb1b..c06e95339f1 100644 --- a/src/harness/fourslash.ts +++ b/src/harness/fourslash.ts @@ -2502,9 +2502,7 @@ Actual: ${stringify(fullActual)}`); const { changes, commands } = this.languageService.getCombinedCodeFix({ type: "file", fileName: this.activeFile.fileName }, fixId, this.formatCodeSettings, ts.emptyOptions); assert.deepEqual | undefined>(commands, expectedCommands); - assert(changes.every(c => c.fileName === this.activeFile.fileName), "TODO: support testing codefixes that touch multiple files"); - this.applyChanges(changes); - this.verifyCurrentFileContent(newFileContent); + this.verifyNewContent({ newFileContent }, changes); } /** @@ -3389,6 +3387,19 @@ Actual: ${stringify(fullActual)}`); private getApplicableRefactorsWorker(positionOrRange: number | ts.TextRange, fileName: string, preferences = ts.emptyOptions): ReadonlyArray { return this.languageService.getApplicableRefactors(fileName, positionOrRange, preferences) || ts.emptyArray; } + + public generateTypes(examples: ReadonlyArray): void { + for (const { name = "example", value, output, outputBaseline } of examples) { + const actual = 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); + } + else { + assert.equal(actual, output, `generateTypes output for ${name} does not match`); + } + } + } } function updateTextRangeForTextChanges({ pos, end }: ts.TextRange, textChanges: ReadonlyArray): ts.TextRange { @@ -3442,7 +3453,7 @@ Actual: ${stringify(fullActual)}`); // Parse out the files and their metadata const testData = parseTestData(absoluteBasePath, content, absoluteFileName); const state = new TestState(absoluteBasePath, testType, testData); - const output = ts.transpileModule(content, { reportDiagnostics: true }); + const output = ts.transpileModule(content, { reportDiagnostics: true, compilerOptions: { target: ts.ScriptTarget.ES2015 } }); if (output.diagnostics!.length > 0) { throw new Error(`Syntax error in ${absoluteBasePath}: ${output.diagnostics![0].messageText}`); } @@ -4512,6 +4523,18 @@ namespace FourSlashInterface { public noMoveToNewFile(): void { this.state.noMoveToNewFile(); } + + public generateTypes(...options: GenerateTypesOptions[]): void { + this.state.generateTypes(options); + } + } + + export interface GenerateTypesOptions { + readonly name?: string; + readonly value: unknown; + // Exactly one of these should be set: + readonly output?: string; + readonly outputBaseline?: string; } export class Edit { @@ -4901,7 +4924,7 @@ namespace FourSlashInterface { export interface VerifyCodeFixAllOptions { fixId: string; fixAllDescription: string; - newFileContent: string; + newFileContent: NewFileContent; commands: ReadonlyArray<{}>; } diff --git a/src/jsTyping/shared.ts b/src/jsTyping/shared.ts index 54b34193d06..78e9fe051b7 100644 --- a/src/jsTyping/shared.ts +++ b/src/jsTyping/shared.ts @@ -4,6 +4,7 @@ namespace ts.server { export const ActionSet: ActionSet = "action::set"; export const ActionInvalidate: ActionInvalidate = "action::invalidate"; export const ActionPackageInstalled: ActionPackageInstalled = "action::packageInstalled"; + export const ActionValueInspected: ActionValueInspected = "action::valueInspected"; export const EventTypesRegistry: EventTypesRegistry = "event::typesRegistry"; export const EventBeginInstallTypes: EventBeginInstallTypes = "event::beginInstallTypes"; export const EventEndInstallTypes: EventEndInstallTypes = "event::endInstallTypes"; diff --git a/src/jsTyping/types.ts b/src/jsTyping/types.ts index 4e00c2eb6f5..408d30a6511 100644 --- a/src/jsTyping/types.ts +++ b/src/jsTyping/types.ts @@ -2,6 +2,7 @@ declare namespace ts.server { export type ActionSet = "action::set"; export type ActionInvalidate = "action::invalidate"; export type ActionPackageInstalled = "action::packageInstalled"; + export type ActionValueInspected = "action::valueInspected"; export type EventTypesRegistry = "event::typesRegistry"; export type EventBeginInstallTypes = "event::beginInstallTypes"; export type EventEndInstallTypes = "event::endInstallTypes"; @@ -12,7 +13,7 @@ declare namespace ts.server { } export interface TypingInstallerResponse { - readonly kind: ActionSet | ActionInvalidate | EventTypesRegistry | ActionPackageInstalled | EventBeginInstallTypes | EventEndInstallTypes | EventInitializationFailed; + readonly kind: ActionSet | ActionInvalidate | EventTypesRegistry | ActionPackageInstalled | ActionValueInspected | EventBeginInstallTypes | EventEndInstallTypes | EventInitializationFailed; } export interface TypingInstallerRequestWithProjectName { @@ -20,7 +21,7 @@ declare namespace ts.server { } /* @internal */ - export type TypingInstallerRequestUnion = DiscoverTypings | CloseProject | TypesRegistryRequest | InstallPackageRequest; + export type TypingInstallerRequestUnion = DiscoverTypings | CloseProject | TypesRegistryRequest | InstallPackageRequest | InspectValueRequest; export interface DiscoverTypings extends TypingInstallerRequestWithProjectName { readonly fileNames: string[]; @@ -47,6 +48,12 @@ declare namespace ts.server { readonly projectRootPath: Path; } + /* @internal */ + export interface InspectValueRequest { + readonly kind: "inspectValue"; + readonly options: InspectValueOptions; + } + /* @internal */ export interface TypesRegistryResponse extends TypingInstallerResponse { readonly kind: EventTypesRegistry; @@ -59,6 +66,12 @@ declare namespace ts.server { readonly message: string; } + /* @internal */ + export interface InspectValueResponse { + readonly kind: ActionValueInspected; + readonly result: ValueInfo; + } + export interface InitializationFailedResponse extends TypingInstallerResponse { readonly kind: EventInitializationFailed; readonly message: string; @@ -106,5 +119,5 @@ declare namespace ts.server { } /* @internal */ - export type TypingInstallerResponseUnion = SetTypings | InvalidateCachedTypings | TypesRegistryResponse | PackageInstalledResponse | InstallTypes | InitializationFailedResponse; + export type TypingInstallerResponseUnion = SetTypings | InvalidateCachedTypings | TypesRegistryResponse | PackageInstalledResponse | InspectValueResponse | InstallTypes | InitializationFailedResponse; } diff --git a/src/server/project.ts b/src/server/project.ts index 3acb99dacd6..e0297c640b8 100644 --- a/src/server/project.ts +++ b/src/server/project.ts @@ -254,6 +254,11 @@ namespace ts.server { installPackage(options: InstallPackageOptions): Promise { return this.typingsCache.installPackage({ ...options, projectName: this.projectName, projectRootPath: this.toPath(this.currentDirectory) }); } + /* @internal */ + inspectValue(options: InspectValueOptions): Promise { + return this.typingsCache.inspectValue(options); + } + private get typingsCache(): TypingsCache { return this.projectService.typingsCache; } @@ -352,6 +357,10 @@ namespace ts.server { return this.projectService.host.readFile(fileName); } + writeFile(fileName: string, content: string): void { + return this.projectService.host.writeFile(fileName, content); + } + fileExists(file: string): boolean { // As an optimization, don't hit the disks for files we already know don't exist // (because we're watching for their creation). diff --git a/src/server/session.ts b/src/server/session.ts index 977a0bd186d..688bccb935c 100644 --- a/src/server/session.ts +++ b/src/server/session.ts @@ -1829,8 +1829,8 @@ namespace ts.server { private applyCodeActionCommand(args: protocol.ApplyCodeActionCommandRequestArgs): {} { const commands = args.command as CodeActionCommand | CodeActionCommand[]; // They should be sending back the command we sent them. for (const command of toArray(commands)) { - const { project } = this.getFileAndProject(command); - project.getLanguageService().applyCodeActionCommand(command).then( + const { file, project } = this.getFileAndProject(command); + project.getLanguageService().applyCodeActionCommand(command, this.getFormatOptions(file)).then( _result => { /* TODO: GH#20447 report success message? */ }, _error => { /* TODO: GH#20447 report errors */ }); } diff --git a/src/server/typingsCache.ts b/src/server/typingsCache.ts index 12b2303cfd3..64a117bbe25 100644 --- a/src/server/typingsCache.ts +++ b/src/server/typingsCache.ts @@ -8,6 +8,8 @@ namespace ts.server { export interface ITypingsInstaller { isKnownTypesPackageName(name: string): boolean; installPackage(options: InstallPackageOptionsWithProject): Promise; + /* @internal */ + inspectValue(options: InspectValueOptions): Promise; enqueueInstallTypingsRequest(p: Project, typeAcquisition: TypeAcquisition, unresolvedImports: SortedReadonlyArray | undefined): void; attach(projectService: ProjectService): void; onProjectClosed(p: Project): void; @@ -18,6 +20,7 @@ namespace ts.server { isKnownTypesPackageName: returnFalse, // Should never be called because we never provide a types registry. installPackage: notImplemented, + inspectValue: notImplemented, enqueueInstallTypingsRequest: noop, attach: noop, onProjectClosed: noop, @@ -95,6 +98,10 @@ namespace ts.server { return this.installer.installPackage(options); } + inspectValue(options: InspectValueOptions): Promise { + return this.installer.inspectValue(options); + } + enqueueInstallTypingsForProject(project: Project, unresolvedImports: SortedReadonlyArray | undefined, forceRefresh: boolean) { const typeAcquisition = project.getTypeAcquisition(); diff --git a/src/services/codefixes/convertToAsyncFunction.ts b/src/services/codefixes/convertToAsyncFunction.ts index fc045381f5f..01aaaae65ca 100644 --- a/src/services/codefixes/convertToAsyncFunction.ts +++ b/src/services/codefixes/convertToAsyncFunction.ts @@ -14,17 +14,10 @@ namespace ts.codefix { getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, err) => convertToAsyncFunction(changes, err.file, err.start, context.program.getTypeChecker(), context)), }); - - /* - custom type to encapsulate information for variable declarations synthesized in the refactor - numberOfUsesOriginal - number of times the variable should be assigned in the refactor - numberOfUsesSynthesized - count of how many times the variable has been assigned so far - At the end of the refactor, numberOfUsesOriginal should === numberOfUsesSynthesized - */ interface SynthIdentifier { identifier: Identifier; types: Type[]; - numberOfAssignmentsOriginal: number; + numberOfAssignmentsOriginal: number; // number of times the variable should be assigned in the refactor } interface SymbolAndIdentifier { @@ -179,9 +172,9 @@ namespace ts.codefix { // if the identifier refers to a function we want to add the new synthesized variable for the declaration (ex. blob in let blob = res(arg)) // Note - the choice of the last call signature is arbitrary - if (lastCallSignature && lastCallSignature.parameters.length && !synthNamesMap.has(symbolIdString)) { - const firstParameter = lastCallSignature.parameters[0]; - const ident = isParameter(firstParameter.valueDeclaration) && tryCast(firstParameter.valueDeclaration.name, isIdentifier) || createOptimisticUniqueName("result"); + if (lastCallSignature && !isFunctionLikeDeclaration(node.parent) && !synthNamesMap.has(symbolIdString)) { + const firstParameter = firstOrUndefined(lastCallSignature.parameters); + const ident = firstParameter && isParameter(firstParameter.valueDeclaration) && tryCast(firstParameter.valueDeclaration.name, isIdentifier) || createOptimisticUniqueName("result"); const synthName = getNewNameIfConflict(ident, collidingSymbolMap); synthNamesMap.set(symbolIdString, synthName); allVarNames.push({ identifier: synthName.identifier, symbol }); @@ -234,9 +227,7 @@ namespace ts.codefix { if (renameInfo) { const type = checker.getTypeAtLocation(node); - if (type) { - originalType.set(getNodeId(clone).toString(), type); - } + originalType.set(getNodeId(clone).toString(), type); } } @@ -320,7 +311,7 @@ namespace ts.codefix { const tryBlock = createBlock(transformExpression(node.expression, transformer, node, prevArgName)); const transformationBody = getTransformationBody(func, prevArgName, argName, node, transformer); - const catchArg = argName.identifier.text.length > 0 ? argName.identifier.text : "e"; + const catchArg = argName ? argName.identifier.text : "e"; const catchClause = createCatchClause(catchArg, createBlock(transformationBody)); /* @@ -362,7 +353,7 @@ namespace ts.codefix { const transformationBody2 = getTransformationBody(rej, prevArgName, argNameRej, node, transformer); - const catchArg = argNameRej.identifier.text.length > 0 ? argNameRej.identifier.text : "e"; + const catchArg = argNameRej ? argNameRej.identifier.text : "e"; const catchClause = createCatchClause(catchArg, createBlock(transformationBody2)); return [createTry(tryBlock, catchClause, /* finallyBlock */ undefined) as Statement]; @@ -379,21 +370,25 @@ namespace ts.codefix { function transformPromiseCall(node: Expression, transformer: Transformer, prevArgName?: SynthIdentifier): Statement[] { const shouldReturn = transformer.setOfExpressionsToReturn.get(getNodeId(node).toString()); // the identifier is empty when the handler (.then()) ignores the argument - In this situation we do not need to save the result of the promise returning call - const hasPrevArgName = prevArgName && prevArgName.identifier.text.length > 0; const originalNodeParent = node.original ? node.original.parent : node.parent; - if (hasPrevArgName && !shouldReturn && (!originalNodeParent || isPropertyAccessExpression(originalNodeParent))) { - return createVariableDeclarationOrAssignment(prevArgName!, createAwait(node), transformer).concat(); // hack to make the types match + if (prevArgName && !shouldReturn && (!originalNodeParent || isPropertyAccessExpression(originalNodeParent))) { + return createTransformedStatement(prevArgName, createAwait(node), transformer); } - else if (!hasPrevArgName && !shouldReturn && (!originalNodeParent || isPropertyAccessExpression(originalNodeParent))) { + else if (!prevArgName && !shouldReturn && (!originalNodeParent || isPropertyAccessExpression(originalNodeParent))) { return [createStatement(createAwait(node))]; } return [createReturn(getSynthesizedDeepClone(node))]; } - function createVariableDeclarationOrAssignment(prevArgName: SynthIdentifier, rightHandSide: Expression, transformer: Transformer): NodeArray { + function createTransformedStatement(prevArgName: SynthIdentifier | undefined, rightHandSide: Expression, transformer: Transformer): MutableNodeArray { + if (!prevArgName || prevArgName.identifier.text.length === 0) { + // if there's no argName to assign to, there still might be side effects + return createNodeArray([createStatement(rightHandSide)]); + } if (prevArgName.types.length < prevArgName.numberOfAssignmentsOriginal) { + // if the variable has already been declared, we don't need "let" or "const" return createNodeArray([createStatement(createAssignment(getSynthesizedDeepClone(prevArgName.identifier), rightHandSide))]); } @@ -402,31 +397,36 @@ namespace ts.codefix { } // should be kept up to date with isFixablePromiseArgument in suggestionDiagnostics.ts - function getTransformationBody(func: Expression, prevArgName: SynthIdentifier | undefined, argName: SynthIdentifier, parent: CallExpression, transformer: Transformer): NodeArray { + function getTransformationBody(func: Expression, prevArgName: SynthIdentifier | undefined, argName: SynthIdentifier | undefined, parent: CallExpression, transformer: Transformer): NodeArray { - const hasPrevArgName = prevArgName && prevArgName.identifier.text.length > 0; - const hasArgName = argName && argName.identifier.text.length > 0; const shouldReturn = transformer.setOfExpressionsToReturn.get(getNodeId(parent).toString()); switch (func.kind) { case SyntaxKind.NullKeyword: // do not produce a transformed statement for a null argument break; - case SyntaxKind.Identifier: - // identifier includes undefined - if (!hasArgName) break; + case SyntaxKind.Identifier: // identifier includes undefined + if (!argName) { + // undefined was argument passed to promise handler + break; + } - const synthCall = createCall(getSynthesizedDeepClone(func) as Identifier, /*typeArguments*/ undefined, [argName.identifier]); + const synthCall = createCall(getSynthesizedDeepClone(func) as Identifier, /*typeArguments*/ undefined, argName ? [argName.identifier] : []); if (shouldReturn) { return createNodeArray([createReturn(synthCall)]); } - if (!hasPrevArgName) break; - - const type = transformer.originalTypeMap.get(getNodeId(func).toString()); - const callSignatures = type && transformer.checker.getSignaturesOfType(type, SignatureKind.Call); - const returnType = callSignatures && callSignatures[0].getReturnType(); - const varDeclOrAssignment = createVariableDeclarationOrAssignment(prevArgName!, createAwait(synthCall), transformer); - prevArgName!.types.push(returnType!); + const type = transformer.originalTypeMap.get(getNodeId(func).toString()) || transformer.checker.getTypeAtLocation(func); + const callSignatures = transformer.checker.getSignaturesOfType(type, SignatureKind.Call); + if (!callSignatures.length) { + // if identifier in handler has no call signatures, it's invalid + codeActionSucceeded = false; + break; + } + const returnType = callSignatures[0].getReturnType(); + const varDeclOrAssignment = createTransformedStatement(prevArgName, createAwait(synthCall), transformer); + if (prevArgName) { + prevArgName.types.push(returnType); + } return varDeclOrAssignment; case SyntaxKind.FunctionExpression: @@ -451,7 +451,7 @@ namespace ts.codefix { } return shouldReturn ? getSynthesizedDeepClones(createNodeArray(refactoredStmts)) : - removeReturns(createNodeArray(refactoredStmts), prevArgName!.identifier, transformer.constIdentifiers, seenReturnStatement); + removeReturns(createNodeArray(refactoredStmts), prevArgName!.identifier, transformer, seenReturnStatement); } else { const innerRetStmts = getReturnStatementsWithPromiseHandlers(createReturn(funcBody)); @@ -461,12 +461,16 @@ namespace ts.codefix { return createNodeArray(innerCbBody); } - if (hasPrevArgName && !shouldReturn) { + if (!shouldReturn) { const type = transformer.checker.getTypeAtLocation(func); const returnType = getLastCallSignature(type, transformer.checker)!.getReturnType(); - const varDeclOrAssignment = createVariableDeclarationOrAssignment(prevArgName!, getSynthesizedDeepClone(funcBody), transformer); - prevArgName!.types.push(returnType); - return varDeclOrAssignment; + const rightHandSide = getSynthesizedDeepClone(funcBody); + const possiblyAwaitedRightHandSide = !!transformer.checker.getPromisedTypeOfPromise(returnType) ? createAwait(rightHandSide) : rightHandSide; + const transformedStatement = createTransformedStatement(prevArgName, possiblyAwaitedRightHandSide, transformer); + if (prevArgName) { + prevArgName.types.push(returnType); + } + return transformedStatement; } else { return createNodeArray([createReturn(getSynthesizedDeepClone(funcBody))]); @@ -474,7 +478,7 @@ namespace ts.codefix { } } default: - // We've found a transformation body we don't know how to handle, so the refactoring should no-op to avoid deleting code. + // If no cases apply, we've found a transformation body we don't know how to handle, so the refactoring should no-op to avoid deleting code. codeActionSucceeded = false; break; } @@ -487,13 +491,14 @@ namespace ts.codefix { } - function removeReturns(stmts: NodeArray, prevArgName: Identifier, constIdentifiers: Identifier[], seenReturnStatement: boolean): NodeArray { + function removeReturns(stmts: NodeArray, prevArgName: Identifier, transformer: Transformer, seenReturnStatement: boolean): NodeArray { const ret: Statement[] = []; for (const stmt of stmts) { if (isReturnStatement(stmt)) { if (stmt.expression) { + const possiblyAwaitedExpression = isPromiseReturningExpression(stmt.expression, transformer.checker) ? createAwait(stmt.expression) : stmt.expression; ret.push(createVariableStatement(/*modifiers*/ undefined, - (createVariableDeclarationList([createVariableDeclaration(prevArgName, /*type*/ undefined, stmt.expression)], getFlagOfIdentifier(prevArgName, constIdentifiers))))); + (createVariableDeclarationList([createVariableDeclaration(prevArgName, /*type*/ undefined, possiblyAwaitedExpression)], getFlagOfIdentifier(prevArgName, transformer.constIdentifiers))))); } } else { @@ -504,7 +509,7 @@ namespace ts.codefix { // if block has no return statement, need to define prevArgName as undefined to prevent undeclared variables if (!seenReturnStatement) { ret.push(createVariableStatement(/*modifiers*/ undefined, - (createVariableDeclarationList([createVariableDeclaration(prevArgName, /*type*/ undefined, createIdentifier("undefined"))], getFlagOfIdentifier(prevArgName, constIdentifiers))))); + (createVariableDeclarationList([createVariableDeclaration(prevArgName, /*type*/ undefined, createIdentifier("undefined"))], getFlagOfIdentifier(prevArgName, transformer.constIdentifiers))))); } return createNodeArray(ret); @@ -531,7 +536,7 @@ namespace ts.codefix { return innerCbBody; } - function getArgName(funcNode: Node, transformer: Transformer): SynthIdentifier { + function getArgName(funcNode: Expression, transformer: Transformer): SynthIdentifier | undefined { const numberOfAssignmentsOriginal = 0; const types: Type[] = []; @@ -541,20 +546,21 @@ namespace ts.codefix { if (isFunctionLikeDeclaration(funcNode)) { if (funcNode.parameters.length > 0) { const param = funcNode.parameters[0].name as Identifier; - name = getMapEntryIfExists(param); + name = getMapEntryOrDefault(param); } } else if (isIdentifier(funcNode)) { - name = getMapEntryIfExists(funcNode); + name = getMapEntryOrDefault(funcNode); } - if (!name || name.identifier === undefined || name.identifier.text === "_" || name.identifier.text === "undefined") { - return { identifier: createIdentifier(""), types, numberOfAssignmentsOriginal }; + // return undefined argName when arg is null or undefined + if (!name || name.identifier.text === "undefined") { + return undefined; } return name; - function getMapEntryIfExists(identifier: Identifier): SynthIdentifier { + function getMapEntryOrDefault(identifier: Identifier): SynthIdentifier { const originalNode = getOriginalNode(identifier); const symbol = getSymbol(originalNode); diff --git a/src/services/codefixes/fixCannotFindModule.ts b/src/services/codefixes/fixCannotFindModule.ts index 6822cae7911..1c7070c74bf 100644 --- a/src/services/codefixes/fixCannotFindModule.ts +++ b/src/services/codefixes/fixCannotFindModule.ts @@ -1,6 +1,9 @@ /* @internal */ namespace ts.codefix { - const fixId = "fixCannotFindModule"; + const fixName = "fixCannotFindModule"; + const fixIdInstallTypesPackage = "installTypesPackage"; + const fixIdGenerateTypes = "generateTypes"; + const errorCodeCannotFindModule = Diagnostics.Cannot_find_module_0.code; const errorCodes = [ errorCodeCannotFindModule, @@ -10,26 +13,141 @@ namespace ts.codefix { errorCodes, getCodeActions: context => { const { host, sourceFile, span: { start } } = context; - const packageName = getTypesPackageNameToInstall(host, sourceFile, start, context.errorCode); - return packageName === undefined ? [] - : [createCodeFixAction(fixId, /*changes*/ [], [Diagnostics.Install_0, packageName], fixId, Diagnostics.Install_all_missing_types_packages, getCommand(sourceFile.fileName, packageName))]; + const packageName = tryGetImportedPackageName(sourceFile, start); + if (packageName === undefined) return undefined; + const typesPackageName = getTypesPackageNameToInstall(packageName, host, context.errorCode); + return typesPackageName === undefined + ? singleElementArray(tryGetGenerateTypesAction(context, packageName)) + : [createCodeFixAction(fixName, /*changes*/ [], [Diagnostics.Install_0, typesPackageName], fixIdInstallTypesPackage, Diagnostics.Install_all_missing_types_packages, getInstallCommand(sourceFile.fileName, typesPackageName))]; + }, + fixIds: [fixIdInstallTypesPackage, fixIdGenerateTypes], + getAllCodeActions: context => { + let savedTypesDir: string | null | undefined = null; // tslint:disable-line no-null-keyword + return codeFixAll(context, errorCodes, (changes, diag, commands) => { + const packageName = tryGetImportedPackageName(diag.file, diag.start); + if (packageName === undefined) return undefined; + switch (context.fixId) { + case fixIdInstallTypesPackage: { + const pkg = getTypesPackageNameToInstall(packageName, context.host, diag.code); + if (pkg) { + commands.push(getInstallCommand(diag.file.fileName, pkg)); + } + break; + } + case fixIdGenerateTypes: { + const typesDir = savedTypesDir !== null ? savedTypesDir : savedTypesDir = getOrCreateTypesDirectory(changes, context); + const command = typesDir === undefined ? undefined : tryGenerateTypes(typesDir, packageName, context); + if (command) commands.push(command); + break; + } + default: + Debug.fail(`Bad fixId: ${context.fixId}`); + } + }); }, - fixIds: [fixId], - getAllCodeActions: context => codeFixAll(context, errorCodes, (_, diag, commands) => { - const pkg = getTypesPackageNameToInstall(context.host, diag.file, diag.start, diag.code); - if (pkg) { - commands.push(getCommand(diag.file.fileName, pkg)); - } - }), }); - function getCommand(fileName: string, packageName: string): InstallPackageAction { + function tryGetGenerateTypesAction(context: CodeFixContextBase, packageName: string): CodeFixAction | undefined { + let command: GenerateTypesAction | undefined; + const changes = textChanges.ChangeTracker.with(context, t => { + const typesDir = getOrCreateTypesDirectory(t, context); + command = typesDir === undefined ? undefined : tryGenerateTypes(typesDir, packageName, context); + }); + return command && createCodeFixAction(fixName, changes, [Diagnostics.Generate_types_for_0, packageName], fixIdGenerateTypes, Diagnostics.Generate_types_for_all_packages_without_types, command); + } + + function tryGenerateTypes(typesDir: string, packageName: string, context: CodeFixContextBase): GenerateTypesAction | undefined { + const file = context.sourceFile.fileName; + const fileToGenerateTypesFor = tryResolveJSModule(packageName, getDirectoryPath(file), context.host as ModuleResolutionHost); // TODO: GH#18217 + if (fileToGenerateTypesFor === undefined) return undefined; + + const outputFileName = resolvePath(getDirectoryPath(context.program.getCompilerOptions().configFile!.fileName), typesDir, packageName + ".d.ts"); + if (context.host.fileExists!(outputFileName)) return undefined; + return { type: "generate types", file, fileToGenerateTypesFor, outputFileName }; + } + + // If no types directory exists yet, adds it to tsconfig.json + function getOrCreateTypesDirectory(changes: textChanges.ChangeTracker, context: CodeFixContextBase): string | undefined { + const { configFile } = context.program.getCompilerOptions(); + if (!configFile) return undefined; + + const tsconfigObjectLiteral = getTsConfigObjectLiteralExpression(configFile); + if (!tsconfigObjectLiteral) return undefined; + + const compilerOptionsProperty = findProperty(tsconfigObjectLiteral, "compilerOptions"); + if (!compilerOptionsProperty) { + const newCompilerOptions = createObjectLiteral([makeDefaultBaseUrl(), makeDefaultPaths()]); + changes.insertNodeAtObjectStart(configFile, tsconfigObjectLiteral, createJsonPropertyAssignment("compilerOptions", newCompilerOptions)); + return defaultTypesDirectoryName; + } + + const compilerOptions = compilerOptionsProperty.initializer; + if (!isObjectLiteralExpression(compilerOptions)) return defaultTypesDirectoryName; + + const baseUrl = getOrAddBaseUrl(changes, configFile, compilerOptions); + const typesDirectoryFromPathMapping = getOrAddPathMapping(changes, configFile, compilerOptions); + return combinePaths(baseUrl, typesDirectoryFromPathMapping); + } + + const defaultBaseUrl = "."; + function makeDefaultBaseUrl(): PropertyAssignment { + return createJsonPropertyAssignment("baseUrl", createStringLiteral(defaultBaseUrl)); + } + function getOrAddBaseUrl(changes: textChanges.ChangeTracker, tsconfig: TsConfigSourceFile, compilerOptions: ObjectLiteralExpression): string { + const baseUrlProp = findProperty(compilerOptions, "baseUrl"); + if (baseUrlProp) { + return isStringLiteral(baseUrlProp.initializer) ? baseUrlProp.initializer.text : defaultBaseUrl; + } + else { + changes.insertNodeAtObjectStart(tsconfig, compilerOptions, makeDefaultBaseUrl()); + return defaultBaseUrl; + } + } + + const defaultTypesDirectoryName = "types"; + function makeDefaultPathMapping(): PropertyAssignment { + return createJsonPropertyAssignment("*", createArrayLiteral([createStringLiteral(`${defaultTypesDirectoryName}/*`)])); + } + function makeDefaultPaths(): PropertyAssignment { + return createJsonPropertyAssignment("paths", createObjectLiteral([makeDefaultPathMapping()])); + } + function getOrAddPathMapping(changes: textChanges.ChangeTracker, tsconfig: TsConfigSourceFile, compilerOptions: ObjectLiteralExpression) { + const paths = findProperty(compilerOptions, "paths"); + if (!paths || !isObjectLiteralExpression(paths.initializer)) { + changes.insertNodeAtObjectStart(tsconfig, compilerOptions, makeDefaultPaths()); + return defaultTypesDirectoryName; + } + + // Look for an existing path mapping. Should look like `"*": "foo/*"`. + const existing = firstDefined(paths.initializer.properties, prop => + isPropertyAssignment(prop) && isStringLiteral(prop.name) && prop.name.text === "*" && isArrayLiteralExpression(prop.initializer) + ? firstDefined(prop.initializer.elements, value => isStringLiteral(value) ? tryRemoveSuffix(value.text, "/*") : undefined) + : undefined); + if (existing) return existing; + + changes.insertNodeAtObjectStart(tsconfig, paths.initializer, makeDefaultPathMapping()); + return defaultTypesDirectoryName; + } + + function createJsonPropertyAssignment(name: string, initializer: Expression) { + return createPropertyAssignment(createStringLiteral(name), initializer); + } + + function findProperty(obj: ObjectLiteralExpression, name: string): PropertyAssignment | undefined { + return find(obj.properties, (p): p is PropertyAssignment => isPropertyAssignment(p) && !!p.name && isStringLiteral(p.name) && p.name.text === name); + } + + function getInstallCommand(fileName: string, packageName: string): InstallPackageAction { return { type: "install package", file: fileName, packageName }; } - function getTypesPackageNameToInstall(host: LanguageServiceHost, sourceFile: SourceFile, pos: number, diagCode: number): string | undefined { + function tryGetImportedPackageName(sourceFile: SourceFile, pos: number): string | undefined { const moduleName = cast(getTokenAtPosition(sourceFile, pos), isStringLiteral).text; const { packageName } = parsePackageName(moduleName); + return isExternalModuleNameRelative(packageName) ? undefined : packageName; + } + + function getTypesPackageNameToInstall(packageName: string, host: LanguageServiceHost, diagCode: number): string | undefined { return diagCode === errorCodeCannotFindModule ? (JsTyping.nodeCoreModules.has(packageName) ? "@types/node" : undefined) : (host.isKnownTypesPackageName!(packageName) ? getTypesPackageName(packageName) : undefined); // TODO: GH#18217 diff --git a/src/services/codefixes/generateTypes.ts b/src/services/codefixes/generateTypes.ts new file mode 100644 index 00000000000..6af72da562d --- /dev/null +++ b/src/services/codefixes/generateTypes.ts @@ -0,0 +1,227 @@ +/* @internal */ +namespace ts { + export function generateTypesForModule(name: string, moduleValue: unknown, formatSettings: FormatCodeSettings): string { + return valueInfoToDeclarationFileText(inspectValue(name, moduleValue), formatSettings); + } + + export function valueInfoToDeclarationFileText(valueInfo: ValueInfo, formatSettings: FormatCodeSettings): string { + return textChanges.getNewFileText(toStatements(valueInfo, OutputKind.ExportEquals), ScriptKind.TS, "\n", formatting.getFormatContext(formatSettings)); + } + + const enum OutputKind { ExportEquals, NamedExport, NamespaceMember } + function toNamespaceMemberStatements(info: ValueInfo): ReadonlyArray { + return toStatements(info, OutputKind.NamespaceMember); + } + function toStatements(info: ValueInfo, kind: OutputKind): ReadonlyArray { + const isDefault = info.name === InternalSymbolName.Default; + const name = isDefault ? "_default" : info.name; + 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.NamedExport ? [createModifier(SyntaxKind.ExportKeyword)] + : undefined; + const exportEquals = () => kind === OutputKind.ExportEquals ? [exportEqualsOrDefault(info.name, /*isExportEquals*/ true)] : emptyArray; + const exportDefault = () => isDefault ? [exportEqualsOrDefault("_default", /*isExportEquals*/ false)] : emptyArray; + + switch (info.kind) { + case ValueKind.FunctionOrClass: + return [...exportEquals(), ...functionOrClassToStatements(modifiers, name, info)]; + case ValueKind.Object: + const { members } = info; + if (kind === OutputKind.ExportEquals) { + return flatMap(members, v => toStatements(v, OutputKind.NamedExport)); + } + if (members.some(m => m.kind === ValueKind.FunctionOrClass)) { + // If some member is a function, use a namespace so it gets a FunctionDeclaration or ClassDeclaration. + return [...exportDefault(), createNamespace(modifiers, name, flatMap(members, toNamespaceMemberStatements))]; + } + // falls through + case ValueKind.Const: + case ValueKind.Array: { + const comment = info.kind === ValueKind.Const ? info.comment : undefined; + const constVar = createVariableStatement(modifiers, createVariableDeclarationList([createVariableDeclaration(name, toType(info))], NodeFlags.Const)); + return [...exportEquals(), ...exportDefault(), addComment(constVar, comment)]; + } + default: + return Debug.assertNever(info); + } + } + function exportEqualsOrDefault(name: string, isExportEquals: boolean): ExportAssignment { + return createExportAssignment(/*decorators*/ undefined, /*modifiers*/ undefined, isExportEquals, createIdentifier(name)); + } + + function functionOrClassToStatements(modifiers: Modifiers, name: string, { source, prototypeMembers, namespaceMembers }: ValueInfoFunctionOrClass): ReadonlyArray { + const fnAst = parseClassOrFunctionBody(source); + const { parameters, returnType } = fnAst === undefined ? { parameters: emptyArray, returnType: anyType() } : getParametersAndReturnType(fnAst); + const instanceProperties = typeof fnAst === "object" ? getConstructorFunctionInstanceProperties(fnAst) : emptyArray; + + const classStaticMembers: ClassElement[] | undefined = + instanceProperties.length !== 0 || prototypeMembers.length !== 0 || fnAst === undefined || typeof fnAst !== "number" && fnAst.kind === SyntaxKind.Constructor ? [] : undefined; + + const namespaceStatements = flatMap(namespaceMembers, info => { + if (!isValidIdentifier(info.name)) return undefined; + if (classStaticMembers) { + switch (info.kind) { + case ValueKind.Object: + if (info.members.some(m => m.kind === ValueKind.FunctionOrClass)) { + break; + } + // falls through + case ValueKind.Array: + case ValueKind.Const: + classStaticMembers.push( + addComment( + createProperty(/*decorators*/ undefined, [createModifier(SyntaxKind.StaticKeyword)], info.name, /*questionOrExclamationToken*/ undefined, toType(info), /*initializer*/ undefined), + info.kind === ValueKind.Const ? info.comment : undefined)); + return undefined; + case ValueKind.FunctionOrClass: + if (!info.namespaceMembers.length) { // Else, can't merge a static method with a namespace. Must make it a function on the namespace. + const sig = tryGetMethod(info, [createModifier(SyntaxKind.StaticKeyword)]); + if (sig) { + classStaticMembers.push(sig); + return undefined; + } + } + } + } + return toStatements(info, OutputKind.NamespaceMember); + }); + + const decl = classStaticMembers + ? createClassDeclaration( + /*decorators*/ undefined, + modifiers, + name, + /*typeParameters*/ undefined, + /*heritageClauses*/ undefined, + [ + ...classStaticMembers, + ...(parameters.length ? [createConstructor(/*decorators*/ undefined, /*modifiers*/ undefined, parameters, /*body*/ undefined)] : emptyArray), + ...instanceProperties, + // ignore non-functions on the prototype + ...mapDefined(prototypeMembers, info => info.kind === ValueKind.FunctionOrClass ? tryGetMethod(info) : undefined), + ]) + : createFunctionDeclaration(/*decorators*/ undefined, modifiers, /*asteriskToken*/ undefined, name, /*typeParameters*/ undefined, parameters, returnType, /*body*/ undefined); + return [decl, ...(namespaceStatements.length === 0 ? emptyArray : [createNamespace(modifiers && modifiers.map(m => getSynthesizedDeepClone(m)), name, namespaceStatements)])]; + } + + function tryGetMethod({ name, source }: ValueInfoFunctionOrClass, modifiers?: Modifiers): MethodDeclaration | undefined { + if (!isValidIdentifier(name)) return undefined; + const fnAst = parseClassOrFunctionBody(source); + if (fnAst === undefined || (typeof fnAst !== "number" && fnAst.kind === SyntaxKind.Constructor)) return undefined; + const sig = getParametersAndReturnType(fnAst); + return sig && createMethod( + /*decorators*/ undefined, + modifiers, + /*asteriskToken*/ undefined, + name, + /*questionToken*/ undefined, + /*typeParameters*/ undefined, + sig.parameters, + sig.returnType, + /*body*/ undefined); + } + + function toType(info: ValueInfo): TypeNode { + switch (info.kind) { + case ValueKind.Const: + return createTypeReferenceNode(info.typeName, /*typeArguments*/ undefined); + case ValueKind.Array: + return createArrayTypeNode(toType(info.inner)); + 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))); + default: + return Debug.assertNever(info); + } + } + + // Parses assignments to "this.x" in the constructor into class property declarations + function getConstructorFunctionInstanceProperties(fnAst: FunctionOrConstructorNode): ReadonlyArray { + const members: PropertyDeclaration[] = []; + forEachOwnNodeOfFunction(fnAst, node => { + if (isAssignmentExpression(node, /*excludeCompoundAssignment*/ true) && + isPropertyAccessExpression(node.left) && node.left.expression.kind === SyntaxKind.ThisKeyword) { + const name = node.left.name.text; + if (!isJsPrivate(name)) members.push(createProperty(/*decorators*/ undefined, /*modifiers*/ undefined, name, /*questionOrExclamationToken*/ undefined, anyType(), /*initializer*/ undefined)); + } + }); + return members; + } + + interface ParametersAndReturnType { readonly parameters: ReadonlyArray; readonly returnType: TypeNode; } + function getParametersAndReturnType(fnAst: FunctionOrConstructor): ParametersAndReturnType { + if (typeof fnAst === "number") { + return { parameters: fill(fnAst, i => makeParameter(`p${i}`, anyType())), returnType: anyType() }; + } + let usedArguments = false, hasReturn = false; + forEachOwnNodeOfFunction(fnAst, node => { + usedArguments = usedArguments || isIdentifier(node) && node.text === "arguments"; + hasReturn = hasReturn || isReturnStatement(node) && !!node.expression && node.expression.kind !== SyntaxKind.VoidExpression; + }); + const parameters = [ + ...fnAst.parameters.map(p => makeParameter(`${p.name.getText()}`, inferParameterType(fnAst, p))), + ...(usedArguments ? [makeRestParameter()] : emptyArray), + ]; + return { parameters, returnType: hasReturn ? anyType() : createKeywordTypeNode(SyntaxKind.VoidKeyword) }; + } + function makeParameter(name: string, type: TypeNode): ParameterDeclaration { + return createParameter(/*decorators*/ undefined, /*modifiers*/ undefined, /*dotDotDotToken*/ undefined, name, /*questionToken*/ undefined, type); + } + function makeRestParameter(): ParameterDeclaration { + return createParameter(/*decorators*/ undefined, /*modifiers*/ undefined, createToken(SyntaxKind.DotDotDotToken), "args", /*questionToken*/ undefined, createArrayTypeNode(anyType())); + } + + type FunctionOrConstructorNode = FunctionExpression | ArrowFunction | ConstructorDeclaration | MethodDeclaration; + type FunctionOrConstructor = FunctionOrConstructorNode | number; // number is for native function + /** Returns 'undefined' for class with no declared constructor */ + function parseClassOrFunctionBody(source: string | number): FunctionOrConstructor | undefined { + if (typeof source === "number") return source; + const classOrFunction = tryCast(parseExpression(source), (node): node is FunctionExpression | ArrowFunction | ClassExpression => isFunctionExpression(node) || isArrowFunction(node) || isClassExpression(node)); + return classOrFunction + ? isClassExpression(classOrFunction) ? find(classOrFunction.members, isConstructorDeclaration) : classOrFunction + // If that didn't parse, it's a method `m() {}`. Parse again inside of an object literal. + : cast(first(cast(parseExpression(`{ ${source} }`), isObjectLiteralExpression).properties), isMethodDeclaration); + } + + function parseExpression(expr: string): Expression { + const text = `const _ = ${expr}`; + const srcFile = createSourceFile("test.ts", text, ScriptTarget.Latest, /*setParentNodes*/ true); + return first(cast(first(srcFile.statements), isVariableStatement).declarationList.declarations).initializer!; + } + + function inferParameterType(_fn: FunctionOrConstructor, _param: ParameterDeclaration): TypeNode { + // TODO: Inspect function body for clues (see inferFromUsage.ts) + return anyType(); + } + + // Descends through all nodes in a function, but not in nested functions. + function forEachOwnNodeOfFunction(fnAst: FunctionOrConstructorNode, cb: (node: Node) => void) { + fnAst.body!.forEachChild(function recur(node) { + cb(node); + if (!isFunctionLike(node)) node.forEachChild(recur); + }); + } + + function isValidIdentifier(name: string): boolean { + const keyword = stringToToken(name); + return !(keyword && isNonContextualKeyword(keyword)) && isIdentifierText(name, ScriptTarget.ESNext); + } + + type Modifiers = ReadonlyArray | undefined; + + function addComment(node: T, comment: string | undefined): T { + if (comment !== undefined) addSyntheticLeadingComment(node, SyntaxKind.SingleLineCommentTrivia, comment); + return node; + } + + function anyType(): KeywordTypeNode { + return createKeywordTypeNode(SyntaxKind.AnyKeyword); + } + + function createNamespace(modifiers: Modifiers, name: string, statements: ReadonlyArray): NamespaceDeclaration { + return createModuleDeclaration(/*decorators*/ undefined, modifiers, createIdentifier(name), createModuleBlock(statements), NodeFlags.Namespace) as NamespaceDeclaration; + } +} diff --git a/src/services/importTracker.ts b/src/services/importTracker.ts index 86bee92eaec..9872b378403 100644 --- a/src/services/importTracker.ts +++ b/src/services/importTracker.ts @@ -385,7 +385,7 @@ namespace ts.FindAllReferences { } /** Iterates over all statements at the top level or in module declarations. Returns the first truthy result. */ - function forEachPossibleImportOrExportStatement(sourceFileLike: SourceFileLike, action: (statement: Statement) => T): T | undefined { + function forEachPossibleImportOrExportStatement(sourceFileLike: SourceFileLike, action: (statement: Statement) => T) { return forEach(sourceFileLike.kind === SyntaxKind.SourceFile ? sourceFileLike.statements : sourceFileLike.body!.statements, statement => // TODO: GH#18217 action(statement) || (isAmbientModuleDeclaration(statement) && forEach(statement.body && statement.body.statements, action))); } diff --git a/src/services/services.ts b/src/services/services.ts index c4318c03c69..542cc869873 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -1383,7 +1383,7 @@ namespace ts { // Therefore only get diagnostics for given file. const semanticDiagnostics = program.getSemanticDiagnostics(targetSourceFile, cancellationToken); - if (!program.getCompilerOptions().declaration) { + if (!getEmitDeclarations(program.getCompilerOptions())) { return semanticDiagnostics.slice(); } @@ -1809,25 +1809,36 @@ namespace ts { return ts.getEditsForFileRename(getProgram()!, oldFilePath, newFilePath, host, formatting.getFormatContext(formatOptions), preferences, sourceMapper); } - function applyCodeActionCommand(action: CodeActionCommand): Promise; - function applyCodeActionCommand(action: CodeActionCommand[]): Promise; - function applyCodeActionCommand(action: CodeActionCommand | CodeActionCommand[]): Promise; + function applyCodeActionCommand(action: CodeActionCommand, formatSettings?: FormatCodeSettings): Promise; + function applyCodeActionCommand(action: CodeActionCommand[], formatSettings?: FormatCodeSettings): Promise; + function applyCodeActionCommand(action: CodeActionCommand | CodeActionCommand[], formatSettings?: FormatCodeSettings): Promise; function applyCodeActionCommand(fileName: Path, action: CodeActionCommand): Promise; function applyCodeActionCommand(fileName: Path, action: CodeActionCommand[]): Promise; - function applyCodeActionCommand(fileName: Path | CodeActionCommand | CodeActionCommand[], actionOrUndefined?: CodeActionCommand | CodeActionCommand[]): Promise { - const action = typeof fileName === "string" ? actionOrUndefined! : fileName as CodeActionCommand[]; - return isArray(action) ? Promise.all(action.map(applySingleCodeActionCommand)) : applySingleCodeActionCommand(action); + function applyCodeActionCommand(fileName: Path | CodeActionCommand | CodeActionCommand[], actionOrFormatSettingsOrUndefined?: CodeActionCommand | CodeActionCommand[] | FormatCodeSettings): Promise { + const action = typeof fileName === "string" ? actionOrFormatSettingsOrUndefined as CodeActionCommand | CodeActionCommand[] : fileName as CodeActionCommand[]; + const formatSettings = typeof fileName !== "string" ? actionOrFormatSettingsOrUndefined as FormatCodeSettings : undefined; + return isArray(action) ? Promise.all(action.map(a => applySingleCodeActionCommand(a, formatSettings))) : applySingleCodeActionCommand(action, formatSettings); } - function applySingleCodeActionCommand(action: CodeActionCommand): Promise { + function applySingleCodeActionCommand(action: CodeActionCommand, formatSettings: FormatCodeSettings | undefined): Promise { + const getPath = (path: string): Path => toPath(path, currentDirectory, getCanonicalFileName); switch (action.type) { case "install package": return host.installPackage - ? host.installPackage({ fileName: toPath(action.file, currentDirectory, getCanonicalFileName), packageName: action.packageName }) + ? host.installPackage({ fileName: getPath(action.file), packageName: action.packageName }) : Promise.reject("Host does not implement `installPackage`"); + case "generate types": { + const { fileToGenerateTypesFor, outputFileName } = action; + if (!host.inspectValue) return Promise.reject("Host does not implement `installPackage`"); + const valueInfoPromise = host.inspectValue({ fileNameToRequire: fileToGenerateTypesFor }); + return valueInfoPromise.then(valueInfo => { + const fullOut = getPath(outputFileName); + host.writeFile!(fullOut, valueInfoToDeclarationFileText(valueInfo, formatSettings || testFormatSettings)); // TODO: GH#18217 + return { successMessage: `Wrote types to '${fullOut}'` }; + }); + } default: - return Debug.fail(); - // TODO: Debug.assertNever(action); will only work if there is more than one type. + return Debug.assertNever(action); } } diff --git a/src/services/textChanges.ts b/src/services/textChanges.ts index 99b94c2cc86..756d2799a28 100644 --- a/src/services/textChanges.ts +++ b/src/services/textChanges.ts @@ -211,8 +211,8 @@ namespace ts.textChanges { export class ChangeTracker { private readonly changes: Change[] = []; - private readonly newFiles: { readonly oldFile: SourceFile, readonly fileName: string, readonly statements: ReadonlyArray }[] = []; - private readonly classesWithNodesInsertedAtStart = createMap(); // Set implemented as Map + private readonly newFiles: { readonly oldFile: SourceFile | undefined, readonly fileName: string, readonly statements: ReadonlyArray }[] = []; + private readonly classesWithNodesInsertedAtStart = createMap<{ readonly node: ClassDeclaration | InterfaceDeclaration | ObjectLiteralExpression, readonly sourceFile: SourceFile }>(); // Set implemented as Map private readonly deletedNodes: { readonly sourceFile: SourceFile, readonly node: Node | NodeArray }[] = []; public static fromContext(context: TextChangesContext): ChangeTracker { @@ -410,25 +410,33 @@ namespace ts.textChanges { } public insertNodeAtClassStart(sourceFile: SourceFile, cls: ClassLikeDeclaration | InterfaceDeclaration, newElement: ClassElement): void { + this.insertNodeAtStartWorker(sourceFile, cls, newElement); + } + public insertNodeAtObjectStart(sourceFile: SourceFile, obj: ObjectLiteralExpression, newElement: ObjectLiteralElementLike): void { + this.insertNodeAtStartWorker(sourceFile, obj, newElement); + } + + private insertNodeAtStartWorker(sourceFile: SourceFile, cls: ClassLikeDeclaration | InterfaceDeclaration | ObjectLiteralExpression, newElement: ClassElement | ObjectLiteralElementLike): void { const clsStart = cls.getStart(sourceFile); const indentation = formatting.SmartIndenter.findFirstNonWhitespaceColumn(getLineStartPositionForPosition(clsStart, sourceFile), clsStart, sourceFile, this.formatContext.options) + this.formatContext.options.indentSize!; - this.insertNodeAt(sourceFile, cls.members.pos, newElement, { indentation, ...this.getInsertNodeAtClassStartPrefixSuffix(sourceFile, cls) }); + this.insertNodeAt(sourceFile, getMembersOrProperties(cls).pos, newElement, { indentation, ...this.getInsertNodeAtStartPrefixSuffix(sourceFile, cls) }); } - private getInsertNodeAtClassStartPrefixSuffix(sourceFile: SourceFile, cls: ClassLikeDeclaration | InterfaceDeclaration): { prefix: string, suffix: string } { - if (cls.members.length === 0) { - if (addToSeen(this.classesWithNodesInsertedAtStart, getNodeId(cls), cls)) { + private getInsertNodeAtStartPrefixSuffix(sourceFile: SourceFile, cls: ClassLikeDeclaration | InterfaceDeclaration | ObjectLiteralExpression): { prefix: string, suffix: string } { + const comma = isObjectLiteralExpression(cls) ? "," : ""; + if (getMembersOrProperties(cls).length === 0) { + if (addToSeen(this.classesWithNodesInsertedAtStart, getNodeId(cls), { node: cls, sourceFile })) { // For `class C {\n}`, don't add the trailing "\n" - const shouldSuffix = (positionsAreOnSameLine as any)(...getClassBraceEnds(cls, sourceFile), sourceFile); // TODO: GH#4130 remove 'as any' - return { prefix: this.newLineCharacter, suffix: shouldSuffix ? this.newLineCharacter : "" }; + const shouldSuffix = (positionsAreOnSameLine as any)(...getClassOrObjectBraceEnds(cls, sourceFile), sourceFile); // TODO: GH#4130 remove 'as any' + return { prefix: this.newLineCharacter, suffix: comma + (shouldSuffix ? this.newLineCharacter : "") }; } else { - return { prefix: "", suffix: this.newLineCharacter }; + return { prefix: "", suffix: comma + this.newLineCharacter }; } } else { - return { prefix: this.newLineCharacter, suffix: "" }; + return { prefix: this.newLineCharacter, suffix: comma }; } } @@ -647,9 +655,8 @@ namespace ts.textChanges { } private finishClassesWithNodesInsertedAtStart(): void { - this.classesWithNodesInsertedAtStart.forEach(cls => { - const sourceFile = cls.getSourceFile(); - const [openBraceEnd, closeBraceEnd] = getClassBraceEnds(cls, sourceFile); + this.classesWithNodesInsertedAtStart.forEach(({ node, sourceFile }) => { + const [openBraceEnd, closeBraceEnd] = getClassOrObjectBraceEnds(node, sourceFile); // For `class C { }` remove the whitespace inside the braces. if (positionsAreOnSameLine(openBraceEnd, closeBraceEnd, sourceFile) && openBraceEnd !== closeBraceEnd - 1) { this.deleteRange(sourceFile, createRange(openBraceEnd, closeBraceEnd - 1)); @@ -698,7 +705,7 @@ namespace ts.textChanges { return changes; } - public createNewFile(oldFile: SourceFile, fileName: string, statements: ReadonlyArray) { + public createNewFile(oldFile: SourceFile | undefined, fileName: string, statements: ReadonlyArray) { this.newFiles.push({ oldFile, fileName, statements }); } } @@ -708,12 +715,19 @@ namespace ts.textChanges { return skipTrivia(sourceFile.text, getAdjustedStartPosition(sourceFile, node, {}, Position.FullStart), /*stopAfterLineBreak*/ false, /*stopAtComments*/ true); } - function getClassBraceEnds(cls: ClassLikeDeclaration | InterfaceDeclaration, sourceFile: SourceFile): [number, number] { + function getClassOrObjectBraceEnds(cls: ClassLikeDeclaration | InterfaceDeclaration | ObjectLiteralExpression, sourceFile: SourceFile): [number, number] { return [findChildOfKind(cls, SyntaxKind.OpenBraceToken, sourceFile)!.end, findChildOfKind(cls, SyntaxKind.CloseBraceToken, sourceFile)!.end]; } + function getMembersOrProperties(cls: ClassLikeDeclaration | InterfaceDeclaration | ObjectLiteralExpression): NodeArray { + return isObjectLiteralExpression(cls) ? cls.properties : cls.members; + } export type ValidateNonFormattedText = (node: Node, text: string) => void; + export function getNewFileText(statements: ReadonlyArray, scriptKind: ScriptKind, newLineCharacter: string, formatContext: formatting.FormatContext): string { + return changesToText.newFileChangesWorker(/*oldFile*/ undefined, scriptKind, statements, newLineCharacter, formatContext); + } + namespace changesToText { export function getTextChangesFromChanges(changes: ReadonlyArray, newLineCharacter: string, formatContext: formatting.FormatContext, validate: ValidateNonFormattedText | undefined): FileTextChanges[] { return group(changes, c => c.sourceFile.path).map(changesInFile => { @@ -732,13 +746,17 @@ namespace ts.textChanges { }); } - export function newFileChanges(oldFile: SourceFile, fileName: string, statements: ReadonlyArray, newLineCharacter: string, formatContext: formatting.FormatContext): FileTextChanges { + export function newFileChanges(oldFile: SourceFile | undefined, fileName: string, statements: ReadonlyArray, newLineCharacter: string, formatContext: formatting.FormatContext): FileTextChanges { + const text = newFileChangesWorker(oldFile, getScriptKindFromFileName(fileName), statements, newLineCharacter, formatContext); + return { fileName, textChanges: [createTextChange(createTextSpan(0, 0), text)], isNewFile: true }; + } + + export function newFileChangesWorker(oldFile: SourceFile | undefined, scriptKind: ScriptKind, statements: ReadonlyArray, newLineCharacter: string, formatContext: formatting.FormatContext): string { // TODO: this emits the file, parses it back, then formats it that -- may be a less roundabout way to do this const nonFormattedText = statements.map(s => getNonformattedText(s, oldFile, newLineCharacter).text).join(newLineCharacter); - const sourceFile = createSourceFile(fileName, nonFormattedText, ScriptTarget.ESNext, /*setParentNodes*/ true); + const sourceFile = createSourceFile("any file name", nonFormattedText, ScriptTarget.ESNext, /*setParentNodes*/ true, scriptKind); const changes = formatting.formatDocument(sourceFile, formatContext); - const text = applyChanges(nonFormattedText, changes); - return { fileName, textChanges: [createTextChange(createTextSpan(0, 0), text)], isNewFile: true }; + return applyChanges(nonFormattedText, changes); } function computeNewText(change: Change, sourceFile: SourceFile, newLineCharacter: string, formatContext: formatting.FormatContext, validate: ValidateNonFormattedText | undefined): string { diff --git a/src/services/transpile.ts b/src/services/transpile.ts index 70ca5b64314..030d2273b62 100644 --- a/src/services/transpile.ts +++ b/src/services/transpile.ts @@ -48,6 +48,7 @@ namespace ts { options.paths = undefined; options.rootDirs = undefined; options.declaration = undefined; + options.composite = undefined; options.declarationDir = undefined; options.out = undefined; options.outFile = undefined; diff --git a/src/services/tsconfig.json b/src/services/tsconfig.json index fee6da0f8a3..6f718b43730 100644 --- a/src/services/tsconfig.json +++ b/src/services/tsconfig.json @@ -69,6 +69,7 @@ "codefixes/inferFromUsage.ts", "codefixes/fixInvalidImportSyntax.ts", "codefixes/fixStrictClassInitialization.ts", + "codefixes/generateTypes.ts", "codefixes/requireInTs.ts", "codefixes/useDefaultImport.ts", "codefixes/fixAddModuleReferTypeMissingTypeof.ts", @@ -82,6 +83,6 @@ "services.ts", "breakpoints.ts", "transform.ts", - "shims.ts" + "shims.ts", ] } diff --git a/src/services/types.ts b/src/services/types.ts index 08a09d7f912..dbf79cea783 100644 --- a/src/services/types.ts +++ b/src/services/types.ts @@ -231,6 +231,8 @@ namespace ts { isKnownTypesPackageName?(name: string): boolean; installPackage?(options: InstallPackageOptions): Promise; + /* @internal */ inspectValue?(options: InspectValueOptions): Promise; + writeFile?(fileName: string, content: string): void; } /* @internal */ @@ -331,9 +333,9 @@ namespace ts { getCodeFixesAtPosition(fileName: string, start: number, end: number, errorCodes: ReadonlyArray, formatOptions: FormatCodeSettings, preferences: UserPreferences): ReadonlyArray; getCombinedCodeFix(scope: CombinedCodeFixScope, fixId: {}, formatOptions: FormatCodeSettings, preferences: UserPreferences): CombinedCodeActions; - applyCodeActionCommand(action: CodeActionCommand): Promise; - applyCodeActionCommand(action: CodeActionCommand[]): Promise; - applyCodeActionCommand(action: CodeActionCommand | CodeActionCommand[]): Promise; + applyCodeActionCommand(action: CodeActionCommand, formatSettings?: FormatCodeSettings): Promise; + applyCodeActionCommand(action: CodeActionCommand[], formatSettings?: FormatCodeSettings): Promise; + applyCodeActionCommand(action: CodeActionCommand | CodeActionCommand[], formatSettings?: FormatCodeSettings): Promise; /** @deprecated `fileName` will be ignored */ applyCodeActionCommand(fileName: string, action: CodeActionCommand): Promise; /** @deprecated `fileName` will be ignored */ @@ -526,12 +528,22 @@ namespace ts { // Publicly, this type is just `{}`. Internally it is a union of all the actions we use. // See `commands?: {}[]` in protocol.ts - export type CodeActionCommand = InstallPackageAction; + export type CodeActionCommand = InstallPackageAction | GenerateTypesAction; export interface InstallPackageAction { - /* @internal */ file: string; - /* @internal */ type: "install package"; - /* @internal */ packageName: string; + /* @internal */ readonly type: "install package"; + /* @internal */ readonly file: string; + /* @internal */ readonly packageName: string; + } + + export interface GenerateTypesAction extends GenerateTypesOptions { + /* @internal */ readonly type: "generate types"; + } + + export interface GenerateTypesOptions { + readonly file: string; // File that was importing fileToGenerateTypesFor; used for formatting options. + readonly fileToGenerateTypesFor: string; + readonly outputFileName: string; } /** @@ -717,6 +729,31 @@ namespace ts { indentMultiLineObjectLiteralBeginningOnBlankLine?: boolean; } + /* @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 interface DefinitionInfo extends DocumentSpan { kind: ScriptElementKind; name: string; diff --git a/src/testRunner/unittests/convertToAsyncFunction.ts b/src/testRunner/unittests/convertToAsyncFunction.ts index b58c698effd..c1a08b3319c 100644 --- a/src/testRunner/unittests/convertToAsyncFunction.ts +++ b/src/testRunner/unittests/convertToAsyncFunction.ts @@ -292,7 +292,7 @@ interface Array {}` cancellationToken: { throwIfCancellationRequested: noop, isCancellationRequested: returnFalse }, preferences: emptyOptions, host: notImplementedHost, - formatContext: formatting.getFormatContext(testFormatOptions) + formatContext: formatting.getFormatContext(testFormatSettings) }; const diagnostics = languageService.getSuggestionDiagnostics(f.path); @@ -416,6 +416,20 @@ function [#|f|](): Promise { return fetch('https://typescriptlang.org').then( () => console.log("done") ); }` ); + _testConvertToAsyncFunction("convertToAsyncFunction_IgnoreArgs3", ` +function [#|f|](): Promise { + return fetch('https://typescriptlang.org').then( () => console.log("almost done") ).then( () => console.log("done") ); +}` + ); + _testConvertToAsyncFunction("convertToAsyncFunction_IgnoreArgs4", ` +function [#|f|]() { + return fetch('https://typescriptlang.org').then(res); +} +function res(){ + console.log("done"); +}` + ); + _testConvertToAsyncFunction("convertToAsyncFunction_Method", ` class Parser { [#|f|]():Promise { @@ -473,6 +487,12 @@ function [#|f|]():Promise { function [#|f|]() { return fetch('https://typescriptlang.org').then(undefined, rejection => console.log("rejected:", rejection)); } +` + ); + _testConvertToAsyncFunction("convertToAsyncFunction_NoCatchHandler", ` +function [#|f|]() { + return fetch('https://typescriptlang.org').then(x => x.statusText).catch(undefined); +} ` ); _testConvertToAsyncFunctionFailed("convertToAsyncFunction_NoSuggestion", ` @@ -817,7 +837,7 @@ function [#|f|]() { ); _testConvertToAsyncFunction("convertToAsyncFunction_Scope1", ` function [#|f|]() { - var var1:Promise, var2; + var var1: Response, var2; return fetch('https://typescriptlang.org').then( _ => Promise.resolve().then( res => { var2 = "test"; @@ -1183,6 +1203,45 @@ function [#|f|]() { } `); + _testConvertToAsyncFunction("convertToAsyncFunction_runEffectfulContinuation", ` +function [#|f|]() { + return fetch('https://typescriptlang.org').then(res).then(_ => console.log("done")); +} +function res(result) { + return Promise.resolve().then(x => console.log(result)); +} +`); + + _testConvertToAsyncFunction("convertToAsyncFunction_callbackReturnsPromise", ` +function [#|f|]() { + return fetch('https://typescriptlang.org').then(s => Promise.resolve(s.statusText.length)).then(x => console.log(x + 5)); +} +`); + + _testConvertToAsyncFunction("convertToAsyncFunction_callbackReturnsPromiseInBlock", ` +function [#|f|]() { + return fetch('https://typescriptlang.org').then(s => { return Promise.resolve(s.statusText.length) }).then(x => x + 5); +} +`); + + _testConvertToAsyncFunction("convertToAsyncFunction_callbackReturnsFixablePromise", ` +function [#|f|]() { + return fetch('https://typescriptlang.org').then(s => Promise.resolve(s.statusText).then(st => st.length)).then(x => console.log(x + 5)); +} +`); + + _testConvertToAsyncFunction("convertToAsyncFunction_callbackReturnsPromiseLastInChain", ` +function [#|f|]() { + return fetch('https://typescriptlang.org').then(s => Promise.resolve(s.statusText.length)); +} +`); + + +_testConvertToAsyncFunction("convertToAsyncFunction_nestedPromises", ` +function [#|f|]() { + return fetch('https://typescriptlang.org').then(x => Promise.resolve(3).then(y => Promise.resolve(x.statusText.length + y))); +} +`); }); function _testConvertToAsyncFunction(caption: string, text: string) { diff --git a/src/testRunner/unittests/extractTestHelpers.ts b/src/testRunner/unittests/extractTestHelpers.ts index 6e46c1304ab..6a74a637e92 100644 --- a/src/testRunner/unittests/extractTestHelpers.ts +++ b/src/testRunner/unittests/extractTestHelpers.ts @@ -64,27 +64,6 @@ namespace ts { } export const newLineCharacter = "\n"; - export const testFormatOptions: FormatCodeSettings = { - indentSize: 4, - tabSize: 4, - newLineCharacter, - 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 const notImplementedHost: LanguageServiceHost = { getCompilationSettings: notImplemented, @@ -123,7 +102,7 @@ namespace ts { startPosition: selectionRange.pos, endPosition: selectionRange.end, host: notImplementedHost, - formatContext: formatting.getFormatContext(testFormatOptions), + formatContext: formatting.getFormatContext(testFormatSettings), preferences: emptyOptions, }; const rangeToExtract = refactor.extractSymbol.getRangeToExtract(sourceFile, createTextSpanFromRange(selectionRange)); @@ -185,7 +164,7 @@ namespace ts { startPosition: selectionRange.pos, endPosition: selectionRange.end, host: notImplementedHost, - formatContext: formatting.getFormatContext(testFormatOptions), + formatContext: formatting.getFormatContext(testFormatSettings), preferences: emptyOptions, }; const rangeToExtract = refactor.extractSymbol.getRangeToExtract(sourceFile, createTextSpanFromRange(selectionRange)); diff --git a/src/testRunner/unittests/organizeImports.ts b/src/testRunner/unittests/organizeImports.ts index b4f54afa21b..355292c6d89 100644 --- a/src/testRunner/unittests/organizeImports.ts +++ b/src/testRunner/unittests/organizeImports.ts @@ -270,7 +270,7 @@ export const Other = 1; content: "function F() { }", }; const languageService = makeLanguageService(testFile); - const changes = languageService.organizeImports({ type: "file", fileName: testFile.path }, testFormatOptions, emptyOptions); + const changes = languageService.organizeImports({ type: "file", fileName: testFile.path }, testFormatSettings, emptyOptions); assert.isEmpty(changes); }); @@ -741,7 +741,7 @@ export * from "lib"; function runBaseline(baselinePath: string, testFile: TestFSWithWatch.File, ...otherFiles: TestFSWithWatch.File[]) { const { path: testPath, content: testContent } = testFile; const languageService = makeLanguageService(testFile, ...otherFiles); - const changes = languageService.organizeImports({ type: "file", fileName: testPath }, testFormatOptions, emptyOptions); + const changes = languageService.organizeImports({ type: "file", fileName: testPath }, testFormatSettings, emptyOptions); assert.equal(changes.length, 1); assert.equal(changes[0].fileName, testPath); diff --git a/src/testRunner/unittests/textChanges.ts b/src/testRunner/unittests/textChanges.ts index 699f668b552..164073207b3 100644 --- a/src/testRunner/unittests/textChanges.ts +++ b/src/testRunner/unittests/textChanges.ts @@ -20,7 +20,7 @@ namespace ts { const newLineCharacter = getNewLineCharacter(printerOptions); function getRuleProvider(placeOpenBraceOnNewLineForFunctions: boolean): formatting.FormatContext { - return formatting.getFormatContext(placeOpenBraceOnNewLineForFunctions ? { ...testFormatOptions, placeOpenBraceOnNewLineForFunctions: true } : testFormatOptions); + return formatting.getFormatContext(placeOpenBraceOnNewLineForFunctions ? { ...testFormatSettings, placeOpenBraceOnNewLineForFunctions: true } : testFormatSettings); } // validate that positions that were recovered from the printed text actually match positions that will be created if the same text is parsed. diff --git a/src/testRunner/unittests/tsserverProjectSystem.ts b/src/testRunner/unittests/tsserverProjectSystem.ts index ba3df56b7f2..639cd657e0d 100644 --- a/src/testRunner/unittests/tsserverProjectSystem.ts +++ b/src/testRunner/unittests/tsserverProjectSystem.ts @@ -85,6 +85,7 @@ namespace ts.projectSystem { isKnownTypesPackageName = notImplemented; installPackage = notImplemented; + inspectValue = notImplemented; executePendingCommands() { const actionsToRun = this.postExecActions; @@ -9092,7 +9093,7 @@ export const x = 10;` Debug.assert(!!project.resolveModuleNames); - const edits = project.getLanguageService().getEditsForFileRename("/old.ts", "/new.ts", testFormatOptions, emptyOptions); + const edits = project.getLanguageService().getEditsForFileRename("/old.ts", "/new.ts", testFormatSettings, emptyOptions); assert.deepEqual>(edits, [{ fileName: "/user.ts", textChanges: [{ diff --git a/src/tsserver/server.ts b/src/tsserver/server.ts index 951b158c152..8084d42e1c5 100644 --- a/src/tsserver/server.ts +++ b/src/tsserver/server.ts @@ -231,7 +231,8 @@ namespace ts.server { // buffer, but we have yet to find a way to retrieve that value. private static readonly maxActiveRequestCount = 10; private static readonly requestDelayMillis = 100; - private packageInstalledPromise: { resolve(value: ApplyCodeActionCommandResult): void, reject(reason: any): void } | undefined; + private packageInstalledPromise: { resolve(value: ApplyCodeActionCommandResult): void, reject(reason: unknown): void } | undefined; + private inspectValuePromise: { resolve(value: ValueInfo): void } | undefined; constructor( private readonly telemetryEnabled: boolean, @@ -261,14 +262,19 @@ namespace ts.server { } installPackage(options: InstallPackageOptionsWithProject): Promise { - const rq: InstallPackageRequest = { kind: "installPackage", ...options }; - this.send(rq); + this.send({ kind: "installPackage", ...options }); Debug.assert(this.packageInstalledPromise === undefined); - return new Promise((resolve, reject) => { + return new Promise((resolve, reject) => { this.packageInstalledPromise = { resolve, reject }; }); } + inspectValue(options: InspectValueOptions): Promise { + this.send({ kind: "inspectValue", options }); + Debug.assert(this.inspectValuePromise === undefined); + return new Promise(resolve => { this.inspectValuePromise = { resolve }; }); + } + attach(projectService: ProjectService) { this.projectService = projectService; if (this.logger.hasLevel(LogLevel.requestTime)) { @@ -320,7 +326,7 @@ namespace ts.server { this.send({ projectName: p.getProjectName(), kind: "closeProject" }); } - private send(rq: TypingInstallerRequestUnion): void { + private send(rq: T): void { this.installer.send(rq); } @@ -353,7 +359,7 @@ namespace ts.server { } } - private handleMessage(response: TypesRegistryResponse | PackageInstalledResponse | SetTypings | InvalidateCachedTypings | BeginInstallTypes | EndInstallTypes | InitializationFailedResponse) { + private handleMessage(response: TypesRegistryResponse | PackageInstalledResponse | InspectValueResponse | SetTypings | InvalidateCachedTypings | BeginInstallTypes | EndInstallTypes | InitializationFailedResponse) { if (this.logger.hasLevel(LogLevel.verbose)) { this.logger.info(`Received response:${stringifyIndented(response)}`); } @@ -378,6 +384,10 @@ namespace ts.server { this.event(response, "setTypings"); break; } + case ActionValueInspected: + this.inspectValuePromise!.resolve(response.result); + this.inspectValuePromise = undefined; + break; case EventInitializationFailed: { const body: protocol.TypesInstallerInitializationFailedEventBody = { diff --git a/src/typingsInstaller/nodeTypingsInstaller.ts b/src/typingsInstaller/nodeTypingsInstaller.ts index d2c7269f9bf..00ee8b4fa19 100644 --- a/src/typingsInstaller/nodeTypingsInstaller.ts +++ b/src/typingsInstaller/nodeTypingsInstaller.ts @@ -164,6 +164,11 @@ namespace ts.server.typingsInstaller { } break; } + case "inspectValue": { + const response: InspectValueResponse = { kind: ActionValueInspected, result: inspectModule(req.options.fileNameToRequire) }; + this.sendResponse(response); + break; + } default: Debug.assertNever(req); } diff --git a/tests/baselines/reference/api/tsserverlibrary.d.ts b/tests/baselines/reference/api/tsserverlibrary.d.ts index e9b896e9d17..21ee605120a 100644 --- a/tests/baselines/reference/api/tsserverlibrary.d.ts +++ b/tests/baselines/reference/api/tsserverlibrary.d.ts @@ -4445,6 +4445,7 @@ declare namespace ts.server { type ActionSet = "action::set"; type ActionInvalidate = "action::invalidate"; type ActionPackageInstalled = "action::packageInstalled"; + type ActionValueInspected = "action::valueInspected"; type EventTypesRegistry = "event::typesRegistry"; type EventBeginInstallTypes = "event::beginInstallTypes"; type EventEndInstallTypes = "event::endInstallTypes"; @@ -4453,7 +4454,7 @@ declare namespace ts.server { " __sortedArrayBrand": any; } interface TypingInstallerResponse { - readonly kind: ActionSet | ActionInvalidate | EventTypesRegistry | ActionPackageInstalled | EventBeginInstallTypes | EventEndInstallTypes | EventInitializationFailed; + readonly kind: ActionSet | ActionInvalidate | EventTypesRegistry | ActionPackageInstalled | ActionValueInspected | EventBeginInstallTypes | EventEndInstallTypes | EventInitializationFailed; } interface TypingInstallerRequestWithProjectName { readonly projectName: string; @@ -4661,6 +4662,7 @@ declare namespace ts { getCustomTransformers?(): CustomTransformers | undefined; isKnownTypesPackageName?(name: string): boolean; installPackage?(options: InstallPackageOptions): Promise; + writeFile?(fileName: string, content: string): void; } interface LanguageService { cleanupSemanticCache(): void; @@ -4718,9 +4720,9 @@ declare namespace ts { toLineColumnOffset?(fileName: string, position: number): LineAndCharacter; getCodeFixesAtPosition(fileName: string, start: number, end: number, errorCodes: ReadonlyArray, formatOptions: FormatCodeSettings, preferences: UserPreferences): ReadonlyArray; getCombinedCodeFix(scope: CombinedCodeFixScope, fixId: {}, formatOptions: FormatCodeSettings, preferences: UserPreferences): CombinedCodeActions; - applyCodeActionCommand(action: CodeActionCommand): Promise; - applyCodeActionCommand(action: CodeActionCommand[]): Promise; - applyCodeActionCommand(action: CodeActionCommand | CodeActionCommand[]): Promise; + applyCodeActionCommand(action: CodeActionCommand, formatSettings?: FormatCodeSettings): Promise; + applyCodeActionCommand(action: CodeActionCommand[], formatSettings?: FormatCodeSettings): Promise; + applyCodeActionCommand(action: CodeActionCommand | CodeActionCommand[], formatSettings?: FormatCodeSettings): Promise; /** @deprecated `fileName` will be ignored */ applyCodeActionCommand(fileName: string, action: CodeActionCommand): Promise; /** @deprecated `fileName` will be ignored */ @@ -4882,9 +4884,16 @@ declare namespace ts { changes: ReadonlyArray; commands?: ReadonlyArray; } - type CodeActionCommand = InstallPackageAction; + type CodeActionCommand = InstallPackageAction | GenerateTypesAction; interface InstallPackageAction { } + interface GenerateTypesAction extends GenerateTypesOptions { + } + interface GenerateTypesOptions { + readonly file: string; + readonly fileToGenerateTypesFor: string; + readonly outputFileName: string; + } /** * A set of one or more available refactoring actions, grouped under a parent refactoring. */ @@ -8102,6 +8111,7 @@ declare namespace ts.server { useCaseSensitiveFileNames(): boolean; readDirectory(path: string, extensions?: ReadonlyArray, exclude?: ReadonlyArray, include?: ReadonlyArray, depth?: number): string[]; readFile(fileName: string): string | undefined; + writeFile(fileName: string, content: string): void; fileExists(file: string): boolean; resolveModuleNames(moduleNames: string[], containingFile: string, reusedNames?: string[]): ResolvedModuleFull[]; getResolvedModuleWithFailedLookupLocationsFromCache(moduleName: string, containingFile: string): ResolvedModuleWithFailedLookupLocations | undefined; diff --git a/tests/baselines/reference/api/typescript.d.ts b/tests/baselines/reference/api/typescript.d.ts index cdc5017209a..4b01738783d 100644 --- a/tests/baselines/reference/api/typescript.d.ts +++ b/tests/baselines/reference/api/typescript.d.ts @@ -4445,6 +4445,7 @@ declare namespace ts.server { type ActionSet = "action::set"; type ActionInvalidate = "action::invalidate"; type ActionPackageInstalled = "action::packageInstalled"; + type ActionValueInspected = "action::valueInspected"; type EventTypesRegistry = "event::typesRegistry"; type EventBeginInstallTypes = "event::beginInstallTypes"; type EventEndInstallTypes = "event::endInstallTypes"; @@ -4453,7 +4454,7 @@ declare namespace ts.server { " __sortedArrayBrand": any; } interface TypingInstallerResponse { - readonly kind: ActionSet | ActionInvalidate | EventTypesRegistry | ActionPackageInstalled | EventBeginInstallTypes | EventEndInstallTypes | EventInitializationFailed; + readonly kind: ActionSet | ActionInvalidate | EventTypesRegistry | ActionPackageInstalled | ActionValueInspected | EventBeginInstallTypes | EventEndInstallTypes | EventInitializationFailed; } interface TypingInstallerRequestWithProjectName { readonly projectName: string; @@ -4661,6 +4662,7 @@ declare namespace ts { getCustomTransformers?(): CustomTransformers | undefined; isKnownTypesPackageName?(name: string): boolean; installPackage?(options: InstallPackageOptions): Promise; + writeFile?(fileName: string, content: string): void; } interface LanguageService { cleanupSemanticCache(): void; @@ -4718,9 +4720,9 @@ declare namespace ts { toLineColumnOffset?(fileName: string, position: number): LineAndCharacter; getCodeFixesAtPosition(fileName: string, start: number, end: number, errorCodes: ReadonlyArray, formatOptions: FormatCodeSettings, preferences: UserPreferences): ReadonlyArray; getCombinedCodeFix(scope: CombinedCodeFixScope, fixId: {}, formatOptions: FormatCodeSettings, preferences: UserPreferences): CombinedCodeActions; - applyCodeActionCommand(action: CodeActionCommand): Promise; - applyCodeActionCommand(action: CodeActionCommand[]): Promise; - applyCodeActionCommand(action: CodeActionCommand | CodeActionCommand[]): Promise; + applyCodeActionCommand(action: CodeActionCommand, formatSettings?: FormatCodeSettings): Promise; + applyCodeActionCommand(action: CodeActionCommand[], formatSettings?: FormatCodeSettings): Promise; + applyCodeActionCommand(action: CodeActionCommand | CodeActionCommand[], formatSettings?: FormatCodeSettings): Promise; /** @deprecated `fileName` will be ignored */ applyCodeActionCommand(fileName: string, action: CodeActionCommand): Promise; /** @deprecated `fileName` will be ignored */ @@ -4882,9 +4884,16 @@ declare namespace ts { changes: ReadonlyArray; commands?: ReadonlyArray; } - type CodeActionCommand = InstallPackageAction; + type CodeActionCommand = InstallPackageAction | GenerateTypesAction; interface InstallPackageAction { } + interface GenerateTypesAction extends GenerateTypesOptions { + } + interface GenerateTypesOptions { + readonly file: string; + readonly fileToGenerateTypesFor: string; + readonly outputFileName: string; + } /** * A set of one or more available refactoring actions, grouped under a parent refactoring. */ diff --git a/tests/baselines/reference/controlFlowTruthiness.js b/tests/baselines/reference/controlFlowTruthiness.js index b57befbd641..a841619ace5 100644 --- a/tests/baselines/reference/controlFlowTruthiness.js +++ b/tests/baselines/reference/controlFlowTruthiness.js @@ -67,7 +67,33 @@ function f6() { y; // string | undefined } } - + +function f7(x: {}) { + if (x) { + x; // {} + } + else { + x; // {} + } +} + +function f8(x: T) { + if (x) { + x; // {} + } + else { + x; // {} + } +} + +function f9(x: T) { + if (x) { + x; // {} + } + else { + x; // never + } +} //// [controlFlowTruthiness.js] function f1() { @@ -131,3 +157,27 @@ function f6() { y; // string | undefined } } +function f7(x) { + if (x) { + x; // {} + } + else { + x; // {} + } +} +function f8(x) { + if (x) { + x; // {} + } + else { + x; // {} + } +} +function f9(x) { + if (x) { + x; // {} + } + else { + x; // never + } +} diff --git a/tests/baselines/reference/controlFlowTruthiness.symbols b/tests/baselines/reference/controlFlowTruthiness.symbols index 72dd304f810..098a81e5164 100644 --- a/tests/baselines/reference/controlFlowTruthiness.symbols +++ b/tests/baselines/reference/controlFlowTruthiness.symbols @@ -140,3 +140,54 @@ function f6() { } } +function f7(x: {}) { +>f7 : Symbol(f7, Decl(controlFlowTruthiness.ts, 67, 1)) +>x : Symbol(x, Decl(controlFlowTruthiness.ts, 69, 12)) + + if (x) { +>x : Symbol(x, Decl(controlFlowTruthiness.ts, 69, 12)) + + x; // {} +>x : Symbol(x, Decl(controlFlowTruthiness.ts, 69, 12)) + } + else { + x; // {} +>x : Symbol(x, Decl(controlFlowTruthiness.ts, 69, 12)) + } +} + +function f8(x: T) { +>f8 : Symbol(f8, Decl(controlFlowTruthiness.ts, 76, 1)) +>T : Symbol(T, Decl(controlFlowTruthiness.ts, 78, 12)) +>x : Symbol(x, Decl(controlFlowTruthiness.ts, 78, 15)) +>T : Symbol(T, Decl(controlFlowTruthiness.ts, 78, 12)) + + if (x) { +>x : Symbol(x, Decl(controlFlowTruthiness.ts, 78, 15)) + + x; // {} +>x : Symbol(x, Decl(controlFlowTruthiness.ts, 78, 15)) + } + else { + x; // {} +>x : Symbol(x, Decl(controlFlowTruthiness.ts, 78, 15)) + } +} + +function f9(x: T) { +>f9 : Symbol(f9, Decl(controlFlowTruthiness.ts, 85, 1)) +>T : Symbol(T, Decl(controlFlowTruthiness.ts, 87, 12)) +>x : Symbol(x, Decl(controlFlowTruthiness.ts, 87, 30)) +>T : Symbol(T, Decl(controlFlowTruthiness.ts, 87, 12)) + + if (x) { +>x : Symbol(x, Decl(controlFlowTruthiness.ts, 87, 30)) + + x; // {} +>x : Symbol(x, Decl(controlFlowTruthiness.ts, 87, 30)) + } + else { + x; // never +>x : Symbol(x, Decl(controlFlowTruthiness.ts, 87, 30)) + } +} diff --git a/tests/baselines/reference/controlFlowTruthiness.types b/tests/baselines/reference/controlFlowTruthiness.types index 225f6d0e7e2..454d094f406 100644 --- a/tests/baselines/reference/controlFlowTruthiness.types +++ b/tests/baselines/reference/controlFlowTruthiness.types @@ -157,3 +157,50 @@ function f6() { } } +function f7(x: {}) { +>f7 : (x: {}) => void +>x : {} + + if (x) { +>x : {} + + x; // {} +>x : {} + } + else { + x; // {} +>x : {} + } +} + +function f8(x: T) { +>f8 : (x: T) => void +>x : T + + if (x) { +>x : T + + x; // {} +>x : T + } + else { + x; // {} +>x : T + } +} + +function f9(x: T) { +>f9 : (x: T) => void +>x : T + + if (x) { +>x : T + + x; // {} +>x : T + } + else { + x; // never +>x : never + } +} diff --git a/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_IgnoreArgs1.ts b/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_IgnoreArgs1.ts index 2cb2ef5b32d..b3cef31ef8b 100644 --- a/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_IgnoreArgs1.ts +++ b/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_IgnoreArgs1.ts @@ -6,6 +6,6 @@ function /*[#|*/f/*|]*/(): Promise { // ==ASYNC FUNCTION::Convert to async function== async function f(): Promise { - await fetch('https://typescriptlang.org'); + const _ = await fetch('https://typescriptlang.org'); console.log("done"); } \ No newline at end of file diff --git a/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_IgnoreArgs3.ts b/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_IgnoreArgs3.ts new file mode 100644 index 00000000000..60cc408a8f5 --- /dev/null +++ b/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_IgnoreArgs3.ts @@ -0,0 +1,12 @@ +// ==ORIGINAL== + +function /*[#|*/f/*|]*/(): Promise { + return fetch('https://typescriptlang.org').then( () => console.log("almost done") ).then( () => console.log("done") ); +} +// ==ASYNC FUNCTION::Convert to async function== + +async function f(): Promise { + await fetch('https://typescriptlang.org'); + console.log("almost done"); + return console.log("done"); +} \ No newline at end of file diff --git a/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_IgnoreArgs4.js b/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_IgnoreArgs4.js new file mode 100644 index 00000000000..a8cbdff4451 --- /dev/null +++ b/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_IgnoreArgs4.js @@ -0,0 +1,17 @@ +// ==ORIGINAL== + +function /*[#|*/f/*|]*/() { + return fetch('https://typescriptlang.org').then(res); +} +function res(){ + console.log("done"); +} +// ==ASYNC FUNCTION::Convert to async function== + +async function f() { + const result = await fetch('https://typescriptlang.org'); + return res(result); +} +function res(){ + console.log("done"); +} \ No newline at end of file diff --git a/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_IgnoreArgs4.ts b/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_IgnoreArgs4.ts new file mode 100644 index 00000000000..a8cbdff4451 --- /dev/null +++ b/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_IgnoreArgs4.ts @@ -0,0 +1,17 @@ +// ==ORIGINAL== + +function /*[#|*/f/*|]*/() { + return fetch('https://typescriptlang.org').then(res); +} +function res(){ + console.log("done"); +} +// ==ASYNC FUNCTION::Convert to async function== + +async function f() { + const result = await fetch('https://typescriptlang.org'); + return res(result); +} +function res(){ + console.log("done"); +} \ No newline at end of file diff --git a/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_NoCatchHandler.js b/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_NoCatchHandler.js new file mode 100644 index 00000000000..25061a4dfb5 --- /dev/null +++ b/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_NoCatchHandler.js @@ -0,0 +1,15 @@ +// ==ORIGINAL== + +function /*[#|*/f/*|]*/() { + return fetch('https://typescriptlang.org').then(x => x.statusText).catch(undefined); +} + +// ==ASYNC FUNCTION::Convert to async function== + +async function f() { + try { + const x = await fetch('https://typescriptlang.org'); + return x.statusText; + } + catch (e) { } +} diff --git a/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_NoCatchHandler.ts b/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_NoCatchHandler.ts new file mode 100644 index 00000000000..25061a4dfb5 --- /dev/null +++ b/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_NoCatchHandler.ts @@ -0,0 +1,15 @@ +// ==ORIGINAL== + +function /*[#|*/f/*|]*/() { + return fetch('https://typescriptlang.org').then(x => x.statusText).catch(undefined); +} + +// ==ASYNC FUNCTION::Convert to async function== + +async function f() { + try { + const x = await fetch('https://typescriptlang.org'); + return x.statusText; + } + catch (e) { } +} diff --git a/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_Scope1.ts b/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_Scope1.ts index c9b9d40b3e3..ab7a707ddd0 100644 --- a/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_Scope1.ts +++ b/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_Scope1.ts @@ -1,7 +1,7 @@ // ==ORIGINAL== function /*[#|*/f/*|]*/() { - var var1:Promise, var2; + var var1: Response, var2; return fetch('https://typescriptlang.org').then( _ => Promise.resolve().then( res => { var2 = "test"; @@ -18,11 +18,11 @@ function /*[#|*/f/*|]*/() { // ==ASYNC FUNCTION::Convert to async function== async function f() { - var var1:Promise, var2; - await fetch('https://typescriptlang.org'); + var var1: Response, var2; + const _ = await fetch('https://typescriptlang.org'); const res = await Promise.resolve(); var2 = "test"; - const res_1 = fetch("https://microsoft.com"); + const res_1 = await fetch("https://microsoft.com"); const response = var1 === res_1; return res(response); } diff --git a/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_callbackReturnsFixablePromise.js b/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_callbackReturnsFixablePromise.js new file mode 100644 index 00000000000..09989a79fb5 --- /dev/null +++ b/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_callbackReturnsFixablePromise.js @@ -0,0 +1,14 @@ +// ==ORIGINAL== + +function /*[#|*/f/*|]*/() { + return fetch('https://typescriptlang.org').then(s => Promise.resolve(s.statusText).then(st => st.length)).then(x => console.log(x + 5)); +} + +// ==ASYNC FUNCTION::Convert to async function== + +async function f() { + const s = await fetch('https://typescriptlang.org'); + const st = await Promise.resolve(s.statusText); + const x = st.length; + return console.log(x + 5); +} diff --git a/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_callbackReturnsFixablePromise.ts b/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_callbackReturnsFixablePromise.ts new file mode 100644 index 00000000000..09989a79fb5 --- /dev/null +++ b/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_callbackReturnsFixablePromise.ts @@ -0,0 +1,14 @@ +// ==ORIGINAL== + +function /*[#|*/f/*|]*/() { + return fetch('https://typescriptlang.org').then(s => Promise.resolve(s.statusText).then(st => st.length)).then(x => console.log(x + 5)); +} + +// ==ASYNC FUNCTION::Convert to async function== + +async function f() { + const s = await fetch('https://typescriptlang.org'); + const st = await Promise.resolve(s.statusText); + const x = st.length; + return console.log(x + 5); +} diff --git a/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_callbackReturnsPromise.js b/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_callbackReturnsPromise.js new file mode 100644 index 00000000000..31ca7652f7c --- /dev/null +++ b/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_callbackReturnsPromise.js @@ -0,0 +1,13 @@ +// ==ORIGINAL== + +function /*[#|*/f/*|]*/() { + return fetch('https://typescriptlang.org').then(s => Promise.resolve(s.statusText.length)).then(x => console.log(x + 5)); +} + +// ==ASYNC FUNCTION::Convert to async function== + +async function f() { + const s = await fetch('https://typescriptlang.org'); + const x = await Promise.resolve(s.statusText.length); + return console.log(x + 5); +} diff --git a/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_callbackReturnsPromise.ts b/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_callbackReturnsPromise.ts new file mode 100644 index 00000000000..31ca7652f7c --- /dev/null +++ b/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_callbackReturnsPromise.ts @@ -0,0 +1,13 @@ +// ==ORIGINAL== + +function /*[#|*/f/*|]*/() { + return fetch('https://typescriptlang.org').then(s => Promise.resolve(s.statusText.length)).then(x => console.log(x + 5)); +} + +// ==ASYNC FUNCTION::Convert to async function== + +async function f() { + const s = await fetch('https://typescriptlang.org'); + const x = await Promise.resolve(s.statusText.length); + return console.log(x + 5); +} diff --git a/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_callbackReturnsPromiseInBlock.js b/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_callbackReturnsPromiseInBlock.js new file mode 100644 index 00000000000..bde99bcc384 --- /dev/null +++ b/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_callbackReturnsPromiseInBlock.js @@ -0,0 +1,13 @@ +// ==ORIGINAL== + +function /*[#|*/f/*|]*/() { + return fetch('https://typescriptlang.org').then(s => { return Promise.resolve(s.statusText.length) }).then(x => x + 5); +} + +// ==ASYNC FUNCTION::Convert to async function== + +async function f() { + const s = await fetch('https://typescriptlang.org'); + const x = await Promise.resolve(s.statusText.length); + return x + 5; +} diff --git a/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_callbackReturnsPromiseInBlock.ts b/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_callbackReturnsPromiseInBlock.ts new file mode 100644 index 00000000000..bde99bcc384 --- /dev/null +++ b/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_callbackReturnsPromiseInBlock.ts @@ -0,0 +1,13 @@ +// ==ORIGINAL== + +function /*[#|*/f/*|]*/() { + return fetch('https://typescriptlang.org').then(s => { return Promise.resolve(s.statusText.length) }).then(x => x + 5); +} + +// ==ASYNC FUNCTION::Convert to async function== + +async function f() { + const s = await fetch('https://typescriptlang.org'); + const x = await Promise.resolve(s.statusText.length); + return x + 5; +} diff --git a/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_callbackReturnsPromiseLastInChain.js b/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_callbackReturnsPromiseLastInChain.js new file mode 100644 index 00000000000..09f98a62649 --- /dev/null +++ b/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_callbackReturnsPromiseLastInChain.js @@ -0,0 +1,12 @@ +// ==ORIGINAL== + +function /*[#|*/f/*|]*/() { + return fetch('https://typescriptlang.org').then(s => Promise.resolve(s.statusText.length)); +} + +// ==ASYNC FUNCTION::Convert to async function== + +async function f() { + const s = await fetch('https://typescriptlang.org'); + return Promise.resolve(s.statusText.length); +} diff --git a/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_callbackReturnsPromiseLastInChain.ts b/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_callbackReturnsPromiseLastInChain.ts new file mode 100644 index 00000000000..09f98a62649 --- /dev/null +++ b/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_callbackReturnsPromiseLastInChain.ts @@ -0,0 +1,12 @@ +// ==ORIGINAL== + +function /*[#|*/f/*|]*/() { + return fetch('https://typescriptlang.org').then(s => Promise.resolve(s.statusText.length)); +} + +// ==ASYNC FUNCTION::Convert to async function== + +async function f() { + const s = await fetch('https://typescriptlang.org'); + return Promise.resolve(s.statusText.length); +} diff --git a/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_nestedPromises.js b/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_nestedPromises.js new file mode 100644 index 00000000000..3ce5c421c3d --- /dev/null +++ b/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_nestedPromises.js @@ -0,0 +1,13 @@ +// ==ORIGINAL== + +function /*[#|*/f/*|]*/() { + return fetch('https://typescriptlang.org').then(x => Promise.resolve(3).then(y => Promise.resolve(x.statusText.length + y))); +} + +// ==ASYNC FUNCTION::Convert to async function== + +async function f() { + const x = await fetch('https://typescriptlang.org'); + const y = await Promise.resolve(3); + return Promise.resolve(x.statusText.length + y); +} diff --git a/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_nestedPromises.ts b/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_nestedPromises.ts new file mode 100644 index 00000000000..3ce5c421c3d --- /dev/null +++ b/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_nestedPromises.ts @@ -0,0 +1,13 @@ +// ==ORIGINAL== + +function /*[#|*/f/*|]*/() { + return fetch('https://typescriptlang.org').then(x => Promise.resolve(3).then(y => Promise.resolve(x.statusText.length + y))); +} + +// ==ASYNC FUNCTION::Convert to async function== + +async function f() { + const x = await fetch('https://typescriptlang.org'); + const y = await Promise.resolve(3); + return Promise.resolve(x.statusText.length + y); +} diff --git a/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_runEffectfulContinuation.js b/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_runEffectfulContinuation.js new file mode 100644 index 00000000000..4753c67cdae --- /dev/null +++ b/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_runEffectfulContinuation.js @@ -0,0 +1,19 @@ +// ==ORIGINAL== + +function /*[#|*/f/*|]*/() { + return fetch('https://typescriptlang.org').then(res).then(_ => console.log("done")); +} +function res(result) { + return Promise.resolve().then(x => console.log(result)); +} + +// ==ASYNC FUNCTION::Convert to async function== + +async function f() { + const result = await fetch('https://typescriptlang.org'); + const _ = await res(result); + return console.log("done"); +} +function res(result) { + return Promise.resolve().then(x => console.log(result)); +} diff --git a/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_runEffectfulContinuation.ts b/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_runEffectfulContinuation.ts new file mode 100644 index 00000000000..4753c67cdae --- /dev/null +++ b/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_runEffectfulContinuation.ts @@ -0,0 +1,19 @@ +// ==ORIGINAL== + +function /*[#|*/f/*|]*/() { + return fetch('https://typescriptlang.org').then(res).then(_ => console.log("done")); +} +function res(result) { + return Promise.resolve().then(x => console.log(result)); +} + +// ==ASYNC FUNCTION::Convert to async function== + +async function f() { + const result = await fetch('https://typescriptlang.org'); + const _ = await res(result); + return console.log("done"); +} +function res(result) { + return Promise.resolve().then(x => console.log(result)); +} diff --git a/tests/baselines/reference/declFileEmitDeclarationOnlyError1.errors.txt b/tests/baselines/reference/declFileEmitDeclarationOnlyError1.errors.txt index 62447a4a911..968b2a09b00 100644 --- a/tests/baselines/reference/declFileEmitDeclarationOnlyError1.errors.txt +++ b/tests/baselines/reference/declFileEmitDeclarationOnlyError1.errors.txt @@ -1,7 +1,7 @@ -error TS5052: Option 'emitDeclarationOnly' cannot be specified without specifying option 'declaration'. +error TS5069: Option 'emitDeclarationOnly' cannot be specified without specifying option 'declaration' or option 'composite'. -!!! error TS5052: Option 'emitDeclarationOnly' cannot be specified without specifying option 'declaration'. +!!! error TS5069: Option 'emitDeclarationOnly' cannot be specified without specifying option 'declaration' or option 'composite'. ==== tests/cases/compiler/hello.ts (0 errors) ==== var hello = "yo!"; \ No newline at end of file diff --git a/tests/baselines/reference/declFileEmitDeclarationOnlyError2.errors.txt b/tests/baselines/reference/declFileEmitDeclarationOnlyError2.errors.txt index 37be20aa132..0b1ea1409c5 100644 --- a/tests/baselines/reference/declFileEmitDeclarationOnlyError2.errors.txt +++ b/tests/baselines/reference/declFileEmitDeclarationOnlyError2.errors.txt @@ -1,9 +1,9 @@ -error TS5052: Option 'emitDeclarationOnly' cannot be specified without specifying option 'declaration'. error TS5053: Option 'emitDeclarationOnly' cannot be specified with option 'noEmit'. +error TS5069: Option 'emitDeclarationOnly' cannot be specified without specifying option 'declaration' or option 'composite'. -!!! error TS5052: Option 'emitDeclarationOnly' cannot be specified without specifying option 'declaration'. !!! error TS5053: Option 'emitDeclarationOnly' cannot be specified with option 'noEmit'. +!!! error TS5069: Option 'emitDeclarationOnly' cannot be specified without specifying option 'declaration' or option 'composite'. ==== tests/cases/compiler/hello.ts (0 errors) ==== var hello = "yo!"; \ No newline at end of file diff --git a/tests/baselines/reference/declarationEmitToDeclarationDirWithCompositeOption.js b/tests/baselines/reference/declarationEmitToDeclarationDirWithCompositeOption.js new file mode 100644 index 00000000000..f048e6aceea --- /dev/null +++ b/tests/baselines/reference/declarationEmitToDeclarationDirWithCompositeOption.js @@ -0,0 +1,17 @@ +//// [test.ts] +interface Foo { + x: number; +} +export default Foo; + + +//// [test.js] +"use strict"; +exports.__esModule = true; + + +//// [test.d.ts] +interface Foo { + x: number; +} +export default Foo; diff --git a/tests/baselines/reference/declarationEmitToDeclarationDirWithCompositeOption.symbols b/tests/baselines/reference/declarationEmitToDeclarationDirWithCompositeOption.symbols new file mode 100644 index 00000000000..74f9fba394b --- /dev/null +++ b/tests/baselines/reference/declarationEmitToDeclarationDirWithCompositeOption.symbols @@ -0,0 +1,10 @@ +=== /foo/test.ts === +interface Foo { +>Foo : Symbol(Foo, Decl(test.ts, 0, 0)) + + x: number; +>x : Symbol(Foo.x, Decl(test.ts, 0, 15)) +} +export default Foo; +>Foo : Symbol(Foo, Decl(test.ts, 0, 0)) + diff --git a/tests/baselines/reference/declarationEmitToDeclarationDirWithCompositeOption.types b/tests/baselines/reference/declarationEmitToDeclarationDirWithCompositeOption.types new file mode 100644 index 00000000000..489a84bb9f1 --- /dev/null +++ b/tests/baselines/reference/declarationEmitToDeclarationDirWithCompositeOption.types @@ -0,0 +1,8 @@ +=== /foo/test.ts === +interface Foo { + x: number; +>x : number +} +export default Foo; +>Foo : Foo + diff --git a/tests/baselines/reference/declarationEmitToDeclarationDirWithDeclarationOption.js b/tests/baselines/reference/declarationEmitToDeclarationDirWithDeclarationOption.js new file mode 100644 index 00000000000..f048e6aceea --- /dev/null +++ b/tests/baselines/reference/declarationEmitToDeclarationDirWithDeclarationOption.js @@ -0,0 +1,17 @@ +//// [test.ts] +interface Foo { + x: number; +} +export default Foo; + + +//// [test.js] +"use strict"; +exports.__esModule = true; + + +//// [test.d.ts] +interface Foo { + x: number; +} +export default Foo; diff --git a/tests/baselines/reference/declarationEmitToDeclarationDirWithDeclarationOption.symbols b/tests/baselines/reference/declarationEmitToDeclarationDirWithDeclarationOption.symbols new file mode 100644 index 00000000000..74f9fba394b --- /dev/null +++ b/tests/baselines/reference/declarationEmitToDeclarationDirWithDeclarationOption.symbols @@ -0,0 +1,10 @@ +=== /foo/test.ts === +interface Foo { +>Foo : Symbol(Foo, Decl(test.ts, 0, 0)) + + x: number; +>x : Symbol(Foo.x, Decl(test.ts, 0, 15)) +} +export default Foo; +>Foo : Symbol(Foo, Decl(test.ts, 0, 0)) + diff --git a/tests/baselines/reference/declarationEmitToDeclarationDirWithDeclarationOption.types b/tests/baselines/reference/declarationEmitToDeclarationDirWithDeclarationOption.types new file mode 100644 index 00000000000..489a84bb9f1 --- /dev/null +++ b/tests/baselines/reference/declarationEmitToDeclarationDirWithDeclarationOption.types @@ -0,0 +1,8 @@ +=== /foo/test.ts === +interface Foo { + x: number; +>x : number +} +export default Foo; +>Foo : Foo + diff --git a/tests/baselines/reference/declarationEmitToDeclarationDirWithoutCompositeAndDeclarationOptions.errors.txt b/tests/baselines/reference/declarationEmitToDeclarationDirWithoutCompositeAndDeclarationOptions.errors.txt new file mode 100644 index 00000000000..63ba6281732 --- /dev/null +++ b/tests/baselines/reference/declarationEmitToDeclarationDirWithoutCompositeAndDeclarationOptions.errors.txt @@ -0,0 +1,16 @@ +/foo/tsconfig.json(2,26): error TS5069: Option 'declarationDir' cannot be specified without specifying option 'declaration' or option 'composite'. + + +==== /foo/tsconfig.json (1 errors) ==== + { + "compilerOptions": { "declarationDir": "out" } + ~~~~~~~~~~~~~~~~ +!!! error TS5069: Option 'declarationDir' cannot be specified without specifying option 'declaration' or option 'composite'. + } + +==== /foo/test.ts (0 errors) ==== + interface Foo { + x: number; + } + export default Foo; + \ No newline at end of file diff --git a/tests/baselines/reference/declarationEmitToDeclarationDirWithoutCompositeAndDeclarationOptions.js b/tests/baselines/reference/declarationEmitToDeclarationDirWithoutCompositeAndDeclarationOptions.js new file mode 100644 index 00000000000..48235293467 --- /dev/null +++ b/tests/baselines/reference/declarationEmitToDeclarationDirWithoutCompositeAndDeclarationOptions.js @@ -0,0 +1,10 @@ +//// [test.ts] +interface Foo { + x: number; +} +export default Foo; + + +//// [test.js] +"use strict"; +exports.__esModule = true; diff --git a/tests/baselines/reference/declarationEmitToDeclarationDirWithoutCompositeAndDeclarationOptions.symbols b/tests/baselines/reference/declarationEmitToDeclarationDirWithoutCompositeAndDeclarationOptions.symbols new file mode 100644 index 00000000000..74f9fba394b --- /dev/null +++ b/tests/baselines/reference/declarationEmitToDeclarationDirWithoutCompositeAndDeclarationOptions.symbols @@ -0,0 +1,10 @@ +=== /foo/test.ts === +interface Foo { +>Foo : Symbol(Foo, Decl(test.ts, 0, 0)) + + x: number; +>x : Symbol(Foo.x, Decl(test.ts, 0, 15)) +} +export default Foo; +>Foo : Symbol(Foo, Decl(test.ts, 0, 0)) + diff --git a/tests/baselines/reference/declarationEmitToDeclarationDirWithoutCompositeAndDeclarationOptions.types b/tests/baselines/reference/declarationEmitToDeclarationDirWithoutCompositeAndDeclarationOptions.types new file mode 100644 index 00000000000..489a84bb9f1 --- /dev/null +++ b/tests/baselines/reference/declarationEmitToDeclarationDirWithoutCompositeAndDeclarationOptions.types @@ -0,0 +1,8 @@ +=== /foo/test.ts === +interface Foo { + x: number; +>x : number +} +export default Foo; +>Foo : Foo + diff --git a/tests/baselines/reference/declarationMapsWithoutDeclaration.errors.txt b/tests/baselines/reference/declarationMapsWithoutDeclaration.errors.txt index c432f445130..8574b7d3388 100644 --- a/tests/baselines/reference/declarationMapsWithoutDeclaration.errors.txt +++ b/tests/baselines/reference/declarationMapsWithoutDeclaration.errors.txt @@ -1,7 +1,7 @@ -error TS5052: Option 'declarationMap' cannot be specified without specifying option 'declaration'. +error TS5069: Option 'declarationMap' cannot be specified without specifying option 'declaration' or option 'composite'. -!!! error TS5052: Option 'declarationMap' cannot be specified without specifying option 'declaration'. +!!! error TS5069: Option 'declarationMap' cannot be specified without specifying option 'declaration' or option 'composite'. ==== tests/cases/compiler/declarationMapsWithoutDeclaration.ts (0 errors) ==== module m2 { export interface connectModule { diff --git a/tests/baselines/reference/generateTypes/global.d.ts b/tests/baselines/reference/generateTypes/global.d.ts new file mode 100644 index 00000000000..a49c80258ae --- /dev/null +++ b/tests/baselines/reference/generateTypes/global.d.ts @@ -0,0 +1,236 @@ +export class Array { + static from(p0: any): any; + static isArray(p0: any): any; + static of(): any; + concat(p0: any): any; + copyWithin(p0: any, p1: any): any; + entries(): any; + every(p0: any): any; + fill(p0: any): any; + filter(p0: any): any; + find(p0: any): any; + findIndex(p0: any): any; + forEach(p0: any): any; + includes(p0: any): any; + indexOf(p0: any): any; + join(p0: any): any; + keys(): any; + lastIndexOf(p0: any): any; + map(p0: any): any; + pop(): any; + push(p0: any): any; + reduce(p0: any): any; + reduceRight(p0: any): any; + reverse(): any; + shift(): any; + slice(p0: any, p1: any): any; + some(p0: any): any; + sort(p0: any): any; + splice(p0: any, p1: any): any; + toLocaleString(): any; + unshift(p0: any): any; +} +export class Boolean { + constructor(p0: any); + valueOf(): any; +} +export class Date { + static UTC(p0: any, p1: any, p2: any, p3: any, p4: any, p5: any, p6: any): any; + static now(): any; + static parse(p0: any): any; + constructor(p0: any, p1: any, p2: any, p3: any, p4: any, p5: any, p6: any); + getDate(): any; + getDay(): any; + getFullYear(): any; + getHours(): any; + getMilliseconds(): any; + getMinutes(): any; + getMonth(): any; + getSeconds(): any; + getTime(): any; + getTimezoneOffset(): any; + getUTCDate(): any; + getUTCDay(): any; + getUTCFullYear(): any; + getUTCHours(): any; + getUTCMilliseconds(): any; + getUTCMinutes(): any; + getUTCMonth(): any; + getUTCSeconds(): any; + getYear(): any; + setDate(p0: any): any; + setFullYear(p0: any, p1: any, p2: any): any; + setHours(p0: any, p1: any, p2: any, p3: any): any; + setMilliseconds(p0: any): any; + setMinutes(p0: any, p1: any, p2: any): any; + setMonth(p0: any, p1: any): any; + setSeconds(p0: any, p1: any): any; + setTime(p0: any): any; + setUTCDate(p0: any): any; + setUTCFullYear(p0: any, p1: any, p2: any): any; + setUTCHours(p0: any, p1: any, p2: any, p3: any): any; + setUTCMilliseconds(p0: any): any; + setUTCMinutes(p0: any, p1: any, p2: any): any; + setUTCMonth(p0: any, p1: any): any; + setUTCSeconds(p0: any, p1: any): any; + setYear(p0: any): any; + toDateString(): any; + toGMTString(): any; + toISOString(): any; + toJSON(p0: any): any; + toLocaleDateString(): any; + toLocaleString(): any; + toLocaleTimeString(): any; + toTimeString(): any; + toUTCString(): any; + valueOf(): any; +} +export namespace Math { + const E: number; + const LN10: number; + const LN2: number; + const LOG10E: number; + const LOG2E: number; + const PI: number; + const SQRT1_2: number; + const SQRT2: number; + function abs(p0: any): any; + function acos(p0: any): any; + function acosh(p0: any): any; + function asin(p0: any): any; + function asinh(p0: any): any; + function atan(p0: any): any; + function atan2(p0: any, p1: any): any; + function atanh(p0: any): any; + function cbrt(p0: any): any; + function ceil(p0: any): any; + function clz32(p0: any): any; + function cos(p0: any): any; + function cosh(p0: any): any; + function exp(p0: any): any; + function expm1(p0: any): any; + function floor(p0: any): any; + function fround(p0: any): any; + function hypot(p0: any, p1: any): any; + function imul(p0: any, p1: any): any; + function log(p0: any): any; + function log10(p0: any): any; + function log1p(p0: any): any; + function log2(p0: any): any; + function max(p0: any, p1: any): any; + function min(p0: any, p1: any): any; + function pow(p0: any, p1: any): any; + function random(): any; + function round(p0: any): any; + function sign(p0: any): any; + function sin(p0: any): any; + function sinh(p0: any): any; + function sqrt(p0: any): any; + function tan(p0: any): any; + function tanh(p0: any): any; + function trunc(p0: any): any; +} +export class Number { + static EPSILON: number; + static MAX_SAFE_INTEGER: number; + static MAX_VALUE: number; + static MIN_SAFE_INTEGER: number; + static MIN_VALUE: number; + static NEGATIVE_INFINITY: number; + static NaN: number; + static POSITIVE_INFINITY: number; + static isFinite(p0: any): any; + static isInteger(p0: any): any; + static isNaN(p0: any): any; + static isSafeInteger(p0: any): any; + static parseFloat(p0: any): any; + static parseInt(p0: any, p1: any): any; + constructor(p0: any); + toExponential(p0: any): any; + toFixed(p0: any): any; + toLocaleString(): any; + toPrecision(p0: any): any; + toString(p0: any): any; + valueOf(): any; +} +export class RegExp { + static $1: any; + static $2: any; + static $3: any; + static $4: any; + static $5: any; + static $6: any; + static $7: any; + static $8: any; + static $9: any; + static $_: any; + static input: any; + static lastMatch: any; + static lastParen: any; + static leftContext: any; + static rightContext: any; + constructor(p0: any, p1: any); + compile(p0: any, p1: any): any; + exec(p0: any): any; + test(p0: any): any; +} +export class String { + static fromCharCode(p0: any): any; + static fromCodePoint(p0: any): any; + static raw(p0: any): any; + anchor(p0: any): any; + big(): any; + blink(): any; + bold(): any; + charAt(p0: any): any; + charCodeAt(p0: any): any; + codePointAt(p0: any): any; + concat(p0: any): any; + endsWith(p0: any): any; + fixed(): any; + fontcolor(p0: any): any; + fontsize(p0: any): any; + includes(p0: any): any; + indexOf(p0: any): any; + italics(): any; + lastIndexOf(p0: any): any; + link(p0: any): any; + localeCompare(p0: any): any; + match(p0: any): any; + normalize(): any; + repeat(p0: any): any; + replace(p0: any, p1: any): any; + search(p0: any): any; + slice(p0: any, p1: any): any; + small(): any; + split(p0: any, p1: any): any; + startsWith(p0: any): any; + strike(): any; + sub(): any; + substr(p0: any, p1: any): any; + substring(p0: any, p1: any): any; + sup(): any; + toLocaleLowerCase(): any; + toLocaleUpperCase(): any; + toLowerCase(): any; + toUpperCase(): any; + trim(): any; + trimLeft(): any; + trimRight(): any; + valueOf(): any; +} +export class Symbol { + static hasInstance: symbol; + static isConcatSpreadable: symbol; + static iterator: symbol; + static keyFor(p0: any): any; + static match: symbol; + static replace: symbol; + static search: symbol; + static species: symbol; + static split: symbol; + static toPrimitive: symbol; + static toStringTag: symbol; + static unscopables: symbol; + valueOf(): any; +} \ No newline at end of file diff --git a/tests/baselines/reference/generateTypes/lodash.d.ts b/tests/baselines/reference/generateTypes/lodash.d.ts new file mode 100644 index 00000000000..6354df710db --- /dev/null +++ b/tests/baselines/reference/generateTypes/lodash.d.ts @@ -0,0 +1,668 @@ +export = example; +declare class example { + static VERSION: string; + static add(value: any, other: any): any; + static after(n: any, func: any): any; + static ary(func: any, n: any, guard: any): any; + static assign(...args: any[]): any; + static assignIn(...args: any[]): any; + static assignInWith(...args: any[]): any; + static assignWith(...args: any[]): any; + static at(...args: any[]): any; + static attempt(...args: any[]): any; + static before(n: any, func: any): any; + static bindAll(...args: any[]): any; + static camelCase(string: any): any; + static capitalize(string: any): any; + static castArray(...args: any[]): any; + static ceil(number: any, precision: any): any; + static chain(value: any): any; + static chunk(array: any, size: any, guard: any): any; + static clamp(number: any, lower: any, upper: any): any; + static clone(value: any): any; + static cloneDeep(value: any): any; + static cloneDeepWith(value: any, customizer: any): any; + static cloneWith(value: any, customizer: any): any; + static compact(array: any): any; + static concat(...args: any[]): any; + static cond(pairs: any): any; + static conforms(source: any): any; + static conformsTo(object: any, source: any): any; + static constant(value: any): any; + static countBy(collection: any, iteratee: any): any; + static create(prototype: any, properties: any): any; + static debounce(func: any, wait: any, options: any): any; + static deburr(string: any): any; + static defaultTo(value: any, defaultValue: any): any; + static defaults(...args: any[]): any; + static defaultsDeep(...args: any[]): any; + static defer(...args: any[]): any; + static delay(...args: any[]): any; + static difference(...args: any[]): any; + static differenceBy(...args: any[]): any; + static differenceWith(...args: any[]): any; + static divide(value: any, other: any): any; + static drop(array: any, n: any, guard: any): any; + static dropRight(array: any, n: any, guard: any): any; + static dropRightWhile(array: any, predicate: any): any; + static dropWhile(array: any, predicate: any): any; + static each(collection: any, iteratee: any): any; + static eachRight(collection: any, iteratee: any): any; + static endsWith(string: any, target: any, position: any): any; + static entries(object: any): any; + static entriesIn(object: any): any; + static eq(value: any, other: any): any; + static escape(string: any): any; + static escapeRegExp(string: any): any; + static every(collection: any, predicate: any, guard: any): any; + static extend(...args: any[]): any; + static extendWith(...args: any[]): any; + static fill(array: any, value: any, start: any, end: any): any; + static filter(collection: any, predicate: any): any; + static find(collection: any, predicate: any, fromIndex: any): any; + static findIndex(array: any, predicate: any, fromIndex: any): any; + static findKey(object: any, predicate: any): any; + static findLast(collection: any, predicate: any, fromIndex: any): any; + static findLastIndex(array: any, predicate: any, fromIndex: any): any; + static findLastKey(object: any, predicate: any): any; + static first(array: any): any; + static flatMap(collection: any, iteratee: any): any; + static flatMapDeep(collection: any, iteratee: any): any; + static flatMapDepth(collection: any, iteratee: any, depth: any): any; + static flatten(array: any): any; + static flattenDeep(array: any): any; + static flattenDepth(array: any, depth: any): any; + static flip(func: any): any; + static floor(number: any, precision: any): any; + static flow(...args: any[]): any; + static flowRight(...args: any[]): any; + static forEach(collection: any, iteratee: any): any; + static forEachRight(collection: any, iteratee: any): any; + static forIn(object: any, iteratee: any): any; + static forInRight(object: any, iteratee: any): any; + static forOwn(object: any, iteratee: any): any; + static forOwnRight(object: any, iteratee: any): any; + static fromPairs(pairs: any): any; + static functions(object: any): any; + static functionsIn(object: any): any; + static get(object: any, path: any, defaultValue: any): any; + static groupBy(collection: any, iteratee: any): any; + static gt(value: any, other: any): any; + static gte(value: any, other: any): any; + static has(object: any, path: any): any; + static hasIn(object: any, path: any): any; + static head(array: any): any; + static identity(value: any): any; + static inRange(number: any, start: any, end: any): any; + static includes(collection: any, value: any, fromIndex: any, guard: any): any; + static indexOf(array: any, value: any, fromIndex: any): any; + static initial(array: any): any; + static intersection(...args: any[]): any; + static intersectionBy(...args: any[]): any; + static intersectionWith(...args: any[]): any; + static invert(object: any, iteratee: any): any; + static invertBy(object: any, iteratee: any): any; + static invoke(...args: any[]): any; + static invokeMap(...args: any[]): any; + static isArguments(value: any): any; + static isArray(p0: any): any; + static isArrayBuffer(value: any): any; + static isArrayLike(value: any): any; + static isArrayLikeObject(value: any): any; + static isBoolean(value: any): any; + static isBuffer(b: any): any; + static isDate(value: any): any; + static isElement(value: any): any; + static isEmpty(value: any): any; + static isEqual(value: any, other: any): any; + static isEqualWith(value: any, other: any, customizer: any): any; + static isError(value: any): any; + static isFinite(value: any): any; + static isFunction(value: any): any; + static isInteger(value: any): any; + static isLength(value: any): any; + static isMap(value: any): any; + static isMatch(object: any, source: any): any; + static isMatchWith(object: any, source: any, customizer: any): any; + static isNaN(value: any): any; + static isNative(value: any): any; + static isNil(value: any): any; + static isNull(value: any): any; + static isNumber(value: any): any; + static isObject(value: any): any; + static isObjectLike(value: any): any; + static isPlainObject(value: any): any; + static isRegExp(value: any): any; + static isSafeInteger(value: any): any; + static isSet(value: any): any; + static isString(value: any): any; + static isSymbol(value: any): any; + static isTypedArray(value: any): any; + static isUndefined(value: any): any; + static isWeakMap(value: any): any; + static isWeakSet(value: any): any; + static iteratee(func: any): any; + static join(array: any, separator: any): any; + static kebabCase(string: any): any; + static keyBy(collection: any, iteratee: any): any; + static keys(object: any): any; + static keysIn(object: any): any; + static last(array: any): any; + static lastIndexOf(array: any, value: any, fromIndex: any): any; + static lowerCase(string: any): any; + static lowerFirst(string: any): any; + static lt(value: any, other: any): any; + static lte(value: any, other: any): any; + static map(collection: any, iteratee: any): any; + static mapKeys(object: any, iteratee: any): any; + static mapValues(object: any, iteratee: any): any; + static matches(source: any): any; + static matchesProperty(path: any, srcValue: any): any; + static max(array: any): any; + static maxBy(array: any, iteratee: any): any; + static mean(array: any): any; + static meanBy(array: any, iteratee: any): any; + static merge(...args: any[]): any; + static mergeWith(...args: any[]): any; + static method(...args: any[]): any; + static methodOf(...args: any[]): any; + static min(array: any): any; + static minBy(array: any, iteratee: any): any; + static mixin(object: any, source: any, options: any): any; + static multiply(value: any, other: any): any; + static negate(predicate: any): any; + static noConflict(): any; + static noop(): void; + static now(): any; + static nth(array: any, n: any): any; + static nthArg(n: any): any; + static omit(...args: any[]): any; + static omitBy(object: any, predicate: any): any; + static once(func: any): any; + static orderBy(collection: any, iteratees: any, orders: any, guard: any): any; + static over(...args: any[]): any; + static overArgs(...args: any[]): any; + static overEvery(...args: any[]): any; + static overSome(...args: any[]): any; + static pad(string: any, length: any, chars: any): any; + static padEnd(string: any, length: any, chars: any): any; + static padStart(string: any, length: any, chars: any): any; + static parseInt(string: any, radix: any, guard: any): any; + static partition(collection: any, iteratee: any): any; + static pick(...args: any[]): any; + static pickBy(object: any, predicate: any): any; + static property(path: any): any; + static propertyOf(object: any): any; + static pull(...args: any[]): any; + static pullAll(array: any, values: any): any; + static pullAllBy(array: any, values: any, iteratee: any): any; + static pullAllWith(array: any, values: any, comparator: any): any; + static pullAt(...args: any[]): any; + static random(lower: any, upper: any, floating: any): any; + static range(start: any, end: any, step: any): any; + static rangeRight(start: any, end: any, step: any): any; + static rearg(...args: any[]): any; + static reduce(collection: any, iteratee: any, accumulator: any, ...args: any[]): any; + static reduceRight(collection: any, iteratee: any, accumulator: any, ...args: any[]): any; + static reject(collection: any, predicate: any): any; + static remove(array: any, predicate: any): any; + static repeat(string: any, n: any, guard: any): any; + static replace(...args: any[]): any; + static rest(func: any, start: any): any; + static result(object: any, path: any, defaultValue: any): any; + static reverse(array: any): any; + static round(number: any, precision: any): any; + static runInContext(context: any): any; + static sample(collection: any): any; + static sampleSize(collection: any, n: any, guard: any): any; + static set(object: any, path: any, value: any): any; + static setWith(object: any, path: any, value: any, customizer: any): any; + static shuffle(collection: any): any; + static size(collection: any): any; + static slice(array: any, start: any, end: any): any; + static snakeCase(string: any): any; + static some(collection: any, predicate: any, guard: any): any; + static sortBy(...args: any[]): any; + static sortedIndex(array: any, value: any): any; + static sortedIndexBy(array: any, value: any, iteratee: any): any; + static sortedIndexOf(array: any, value: any): any; + static sortedLastIndex(array: any, value: any): any; + static sortedLastIndexBy(array: any, value: any, iteratee: any): any; + static sortedLastIndexOf(array: any, value: any): any; + static sortedUniq(array: any): any; + static sortedUniqBy(array: any, iteratee: any): any; + static split(string: any, separator: any, limit: any): any; + static spread(func: any, start: any): any; + static startCase(string: any): any; + static startsWith(string: any, target: any, position: any): any; + static stubArray(): any; + static stubFalse(): any; + static stubObject(): any; + static stubString(): any; + static stubTrue(): any; + static subtract(value: any, other: any): any; + static sum(array: any): any; + static sumBy(array: any, iteratee: any): any; + static tail(array: any): any; + static take(array: any, n: any, guard: any): any; + static takeRight(array: any, n: any, guard: any): any; + static takeRightWhile(array: any, predicate: any): any; + static takeWhile(array: any, predicate: any): any; + static tap(value: any, interceptor: any): any; + static template(string: any, options: any, guard: any): any; + static templateSettings: { + escape: RegExp; + evaluate: RegExp; + imports: {}; + interpolate: RegExp; + variable: string; + }; + static throttle(func: any, wait: any, options: any): any; + static thru(value: any, interceptor: any): any; + static times(n: any, iteratee: any): any; + static toArray(value: any): any; + static toFinite(value: any): any; + static toInteger(value: any): any; + static toLength(value: any): any; + static toLower(value: any): any; + static toNumber(value: any): any; + static toPairs(object: any): any; + static toPairsIn(object: any): any; + static toPath(value: any): any; + static toPlainObject(value: any): any; + static toSafeInteger(value: any): any; + static toString(value: any): any; + static toUpper(value: any): any; + static transform(object: any, iteratee: any, accumulator: any): any; + static trim(string: any, chars: any, guard: any): any; + static trimEnd(string: any, chars: any, guard: any): any; + static trimStart(string: any, chars: any, guard: any): any; + static truncate(string: any, options: any): any; + static unary(func: any): any; + static unescape(string: any): any; + static union(...args: any[]): any; + static unionBy(...args: any[]): any; + static unionWith(...args: any[]): any; + static uniq(array: any): any; + static uniqBy(array: any, iteratee: any): any; + static uniqWith(array: any, comparator: any): any; + static uniqueId(prefix: any): any; + static unset(object: any, path: any): any; + static unzip(array: any): any; + static unzipWith(array: any, iteratee: any): any; + static update(object: any, path: any, updater: any): any; + static updateWith(object: any, path: any, updater: any, customizer: any): any; + static upperCase(string: any): any; + static upperFirst(string: any): any; + static values(object: any): any; + static valuesIn(object: any): any; + static without(...args: any[]): any; + static words(string: any, pattern: any, guard: any): any; + static wrap(value: any, wrapper: any): any; + static xor(...args: any[]): any; + static xorBy(...args: any[]): any; + static xorWith(...args: any[]): any; + static zip(...args: any[]): any; + static zipObject(props: any, values: any): any; + static zipObjectDeep(props: any, values: any): any; + static zipWith(...args: any[]): any; + constructor(value: any); + add(...args: any[]): any; + after(...args: any[]): any; + ary(...args: any[]): any; + assign(...args: any[]): any; + assignIn(...args: any[]): any; + assignInWith(...args: any[]): any; + assignWith(...args: any[]): any; + at(...args: any[]): any; + attempt(...args: any[]): any; + before(...args: any[]): any; + bind(...args: any[]): any; + bindAll(...args: any[]): any; + bindKey(...args: any[]): any; + camelCase(...args: any[]): any; + capitalize(...args: any[]): any; + castArray(...args: any[]): any; + ceil(...args: any[]): any; + chain(): any; + chunk(...args: any[]): any; + clamp(...args: any[]): any; + clone(...args: any[]): any; + cloneDeep(...args: any[]): any; + cloneDeepWith(...args: any[]): any; + cloneWith(...args: any[]): any; + commit(): any; + compact(...args: any[]): any; + concat(...args: any[]): any; + cond(...args: any[]): any; + conforms(...args: any[]): any; + conformsTo(...args: any[]): any; + constant(...args: any[]): any; + countBy(...args: any[]): any; + create(...args: any[]): any; + curry(...args: any[]): any; + curryRight(...args: any[]): any; + debounce(...args: any[]): any; + deburr(...args: any[]): any; + defaultTo(...args: any[]): any; + defaults(...args: any[]): any; + defaultsDeep(...args: any[]): any; + defer(...args: any[]): any; + delay(...args: any[]): any; + difference(...args: any[]): any; + differenceBy(...args: any[]): any; + differenceWith(...args: any[]): any; + divide(...args: any[]): any; + drop(...args: any[]): any; + dropRight(...args: any[]): any; + dropRightWhile(...args: any[]): any; + dropWhile(...args: any[]): any; + each(...args: any[]): any; + eachRight(...args: any[]): any; + endsWith(...args: any[]): any; + entries(...args: any[]): any; + entriesIn(...args: any[]): any; + eq(...args: any[]): any; + escape(...args: any[]): any; + escapeRegExp(...args: any[]): any; + every(...args: any[]): any; + extend(...args: any[]): any; + extendWith(...args: any[]): any; + fill(...args: any[]): any; + filter(...args: any[]): any; + find(...args: any[]): any; + findIndex(...args: any[]): any; + findKey(...args: any[]): any; + findLast(...args: any[]): any; + findLastIndex(...args: any[]): any; + findLastKey(...args: any[]): any; + first(...args: any[]): any; + flatMap(...args: any[]): any; + flatMapDeep(...args: any[]): any; + flatMapDepth(...args: any[]): any; + flatten(...args: any[]): any; + flattenDeep(...args: any[]): any; + flattenDepth(...args: any[]): any; + flip(...args: any[]): any; + floor(...args: any[]): any; + flow(...args: any[]): any; + flowRight(...args: any[]): any; + forEach(...args: any[]): any; + forEachRight(...args: any[]): any; + forIn(...args: any[]): any; + forInRight(...args: any[]): any; + forOwn(...args: any[]): any; + forOwnRight(...args: any[]): any; + fromPairs(...args: any[]): any; + functions(...args: any[]): any; + functionsIn(...args: any[]): any; + get(...args: any[]): any; + groupBy(...args: any[]): any; + gt(...args: any[]): any; + gte(...args: any[]): any; + has(...args: any[]): any; + hasIn(...args: any[]): any; + head(...args: any[]): any; + identity(...args: any[]): any; + inRange(...args: any[]): any; + includes(...args: any[]): any; + indexOf(...args: any[]): any; + initial(...args: any[]): any; + intersection(...args: any[]): any; + intersectionBy(...args: any[]): any; + intersectionWith(...args: any[]): any; + invert(...args: any[]): any; + invertBy(...args: any[]): any; + invoke(...args: any[]): any; + invokeMap(...args: any[]): any; + isArguments(...args: any[]): any; + isArray(...args: any[]): any; + isArrayBuffer(...args: any[]): any; + isArrayLike(...args: any[]): any; + isArrayLikeObject(...args: any[]): any; + isBoolean(...args: any[]): any; + isBuffer(...args: any[]): any; + isDate(...args: any[]): any; + isElement(...args: any[]): any; + isEmpty(...args: any[]): any; + isEqual(...args: any[]): any; + isEqualWith(...args: any[]): any; + isError(...args: any[]): any; + isFinite(...args: any[]): any; + isFunction(...args: any[]): any; + isInteger(...args: any[]): any; + isLength(...args: any[]): any; + isMap(...args: any[]): any; + isMatch(...args: any[]): any; + isMatchWith(...args: any[]): any; + isNaN(...args: any[]): any; + isNative(...args: any[]): any; + isNil(...args: any[]): any; + isNull(...args: any[]): any; + isNumber(...args: any[]): any; + isObject(...args: any[]): any; + isObjectLike(...args: any[]): any; + isPlainObject(...args: any[]): any; + isRegExp(...args: any[]): any; + isSafeInteger(...args: any[]): any; + isSet(...args: any[]): any; + isString(...args: any[]): any; + isSymbol(...args: any[]): any; + isTypedArray(...args: any[]): any; + isUndefined(...args: any[]): any; + isWeakMap(...args: any[]): any; + isWeakSet(...args: any[]): any; + iteratee(...args: any[]): any; + join(...args: any[]): any; + kebabCase(...args: any[]): any; + keyBy(...args: any[]): any; + keys(...args: any[]): any; + keysIn(...args: any[]): any; + last(...args: any[]): any; + lastIndexOf(...args: any[]): any; + lowerCase(...args: any[]): any; + lowerFirst(...args: any[]): any; + lt(...args: any[]): any; + lte(...args: any[]): any; + map(...args: any[]): any; + mapKeys(...args: any[]): any; + mapValues(...args: any[]): any; + matches(...args: any[]): any; + matchesProperty(...args: any[]): any; + max(...args: any[]): any; + maxBy(...args: any[]): any; + mean(...args: any[]): any; + meanBy(...args: any[]): any; + memoize(...args: any[]): any; + merge(...args: any[]): any; + mergeWith(...args: any[]): any; + method(...args: any[]): any; + methodOf(...args: any[]): any; + min(...args: any[]): any; + minBy(...args: any[]): any; + mixin(...args: any[]): any; + multiply(...args: any[]): any; + negate(...args: any[]): any; + next(): any; + noConflict(...args: any[]): any; + noop(...args: any[]): any; + now(...args: any[]): any; + nth(...args: any[]): any; + nthArg(...args: any[]): any; + omit(...args: any[]): any; + omitBy(...args: any[]): any; + once(...args: any[]): any; + orderBy(...args: any[]): any; + over(...args: any[]): any; + overArgs(...args: any[]): any; + overEvery(...args: any[]): any; + overSome(...args: any[]): any; + pad(...args: any[]): any; + padEnd(...args: any[]): any; + padStart(...args: any[]): any; + parseInt(...args: any[]): any; + partial(...args: any[]): any; + partialRight(...args: any[]): any; + partition(...args: any[]): any; + pick(...args: any[]): any; + pickBy(...args: any[]): any; + plant(value: any): any; + pop(...args: any[]): any; + property(...args: any[]): any; + propertyOf(...args: any[]): any; + pull(...args: any[]): any; + pullAll(...args: any[]): any; + pullAllBy(...args: any[]): any; + pullAllWith(...args: any[]): any; + pullAt(...args: any[]): any; + push(...args: any[]): any; + random(...args: any[]): any; + range(...args: any[]): any; + rangeRight(...args: any[]): any; + rearg(...args: any[]): any; + reduce(...args: any[]): any; + reduceRight(...args: any[]): any; + reject(...args: any[]): any; + remove(...args: any[]): any; + repeat(...args: any[]): any; + replace(...args: any[]): any; + rest(...args: any[]): any; + result(...args: any[]): any; + reverse(): any; + round(...args: any[]): any; + runInContext(...args: any[]): any; + sample(...args: any[]): any; + sampleSize(...args: any[]): any; + set(...args: any[]): any; + setWith(...args: any[]): any; + shift(...args: any[]): any; + shuffle(...args: any[]): any; + size(...args: any[]): any; + slice(...args: any[]): any; + snakeCase(...args: any[]): any; + some(...args: any[]): any; + sort(...args: any[]): any; + sortBy(...args: any[]): any; + sortedIndex(...args: any[]): any; + sortedIndexBy(...args: any[]): any; + sortedIndexOf(...args: any[]): any; + sortedLastIndex(...args: any[]): any; + sortedLastIndexBy(...args: any[]): any; + sortedLastIndexOf(...args: any[]): any; + sortedUniq(...args: any[]): any; + sortedUniqBy(...args: any[]): any; + splice(...args: any[]): any; + split(...args: any[]): any; + spread(...args: any[]): any; + startCase(...args: any[]): any; + startsWith(...args: any[]): any; + stubArray(...args: any[]): any; + stubFalse(...args: any[]): any; + stubObject(...args: any[]): any; + stubString(...args: any[]): any; + stubTrue(...args: any[]): any; + subtract(...args: any[]): any; + sum(...args: any[]): any; + sumBy(...args: any[]): any; + tail(...args: any[]): any; + take(...args: any[]): any; + takeRight(...args: any[]): any; + takeRightWhile(...args: any[]): any; + takeWhile(...args: any[]): any; + tap(...args: any[]): any; + template(...args: any[]): any; + throttle(...args: any[]): any; + thru(...args: any[]): any; + times(...args: any[]): any; + toArray(...args: any[]): any; + toFinite(...args: any[]): any; + toInteger(...args: any[]): any; + toJSON(): any; + toLength(...args: any[]): any; + toLower(...args: any[]): any; + toNumber(...args: any[]): any; + toPairs(...args: any[]): any; + toPairsIn(...args: any[]): any; + toPath(...args: any[]): any; + toPlainObject(...args: any[]): any; + toSafeInteger(...args: any[]): any; + toUpper(...args: any[]): any; + transform(...args: any[]): any; + trim(...args: any[]): any; + trimEnd(...args: any[]): any; + trimStart(...args: any[]): any; + truncate(...args: any[]): any; + unary(...args: any[]): any; + unescape(...args: any[]): any; + union(...args: any[]): any; + unionBy(...args: any[]): any; + unionWith(...args: any[]): any; + uniq(...args: any[]): any; + uniqBy(...args: any[]): any; + uniqWith(...args: any[]): any; + uniqueId(...args: any[]): any; + unset(...args: any[]): any; + unshift(...args: any[]): any; + unzip(...args: any[]): any; + unzipWith(...args: any[]): any; + update(...args: any[]): any; + updateWith(...args: any[]): any; + upperCase(...args: any[]): any; + upperFirst(...args: any[]): any; + value(): any; + valueOf(): any; + values(...args: any[]): any; + valuesIn(...args: any[]): any; + without(...args: any[]): any; + words(...args: any[]): any; + wrap(...args: any[]): any; + xor(...args: any[]): any; + xorBy(...args: any[]): any; + xorWith(...args: any[]): any; + zip(...args: any[]): any; + zipObject(...args: any[]): any; + zipObjectDeep(...args: any[]): any; + zipWith(...args: any[]): any; +} +declare namespace example { + function bind(...args: any[]): any; + namespace bind { + // Circular reference from example.bind + const placeholder: any; + } + function bindKey(...args: any[]): any; + namespace bindKey { + // Circular reference from example.bindKey + const placeholder: any; + } + function curry(func: any, arity: any, guard: any): any; + namespace curry { + // Circular reference from example.curry + const placeholder: any; + } + function curryRight(func: any, arity: any, guard: any): any; + namespace curryRight { + // Circular reference from example.curryRight + const placeholder: any; + } + function memoize(func: any, resolver: any): any; + namespace memoize { + class Cache { + constructor(entries: any); + clear(): void; + get(key: any): any; + has(key: any): any; + set(key: any, value: any): any; + } + } + function partial(...args: any[]): any; + namespace partial { + // Circular reference from example.partial + const placeholder: any; + } + function partialRight(...args: any[]): any; + namespace partialRight { + // Circular reference from example.partialRight + const placeholder: any; + } +} \ No newline at end of file diff --git a/tests/baselines/reference/jsFileCompilationWithEnabledCompositeOption.errors.txt b/tests/baselines/reference/jsFileCompilationWithEnabledCompositeOption.errors.txt new file mode 100644 index 00000000000..7133188adc0 --- /dev/null +++ b/tests/baselines/reference/jsFileCompilationWithEnabledCompositeOption.errors.txt @@ -0,0 +1,12 @@ +error TS5053: Option 'allowJs' cannot be specified with option 'composite'. + + +!!! error TS5053: Option 'allowJs' cannot be specified with option 'composite'. +==== tests/cases/compiler/a.ts (0 errors) ==== + class c { + } + +==== tests/cases/compiler/b.js (0 errors) ==== + function foo() { + } + \ No newline at end of file diff --git a/tests/baselines/reference/jsFileCompilationWithEnabledCompositeOption.js b/tests/baselines/reference/jsFileCompilationWithEnabledCompositeOption.js new file mode 100644 index 00000000000..e34edb10ff4 --- /dev/null +++ b/tests/baselines/reference/jsFileCompilationWithEnabledCompositeOption.js @@ -0,0 +1,24 @@ +//// [tests/cases/compiler/jsFileCompilationWithEnabledCompositeOption.ts] //// + +//// [a.ts] +class c { +} + +//// [b.js] +function foo() { +} + + +//// [out.js] +var c = /** @class */ (function () { + function c() { + } + return c; +}()); +function foo() { +} + + +//// [out.d.ts] +declare class c { +} diff --git a/tests/baselines/reference/jsFileCompilationWithEnabledCompositeOption.symbols b/tests/baselines/reference/jsFileCompilationWithEnabledCompositeOption.symbols new file mode 100644 index 00000000000..5260b8d6cf3 --- /dev/null +++ b/tests/baselines/reference/jsFileCompilationWithEnabledCompositeOption.symbols @@ -0,0 +1,10 @@ +=== tests/cases/compiler/a.ts === +class c { +>c : Symbol(c, Decl(a.ts, 0, 0)) +} + +=== tests/cases/compiler/b.js === +function foo() { +>foo : Symbol(foo, Decl(b.js, 0, 0)) +} + diff --git a/tests/baselines/reference/jsFileCompilationWithEnabledCompositeOption.types b/tests/baselines/reference/jsFileCompilationWithEnabledCompositeOption.types new file mode 100644 index 00000000000..dce83eeb8eb --- /dev/null +++ b/tests/baselines/reference/jsFileCompilationWithEnabledCompositeOption.types @@ -0,0 +1,10 @@ +=== tests/cases/compiler/a.ts === +class c { +>c : c +} + +=== tests/cases/compiler/b.js === +function foo() { +>foo : () => void +} + diff --git a/tests/baselines/reference/keyofAndIndexedAccessErrors.errors.txt b/tests/baselines/reference/keyofAndIndexedAccessErrors.errors.txt index dd5cbe050a9..4053189aea3 100644 --- a/tests/baselines/reference/keyofAndIndexedAccessErrors.errors.txt +++ b/tests/baselines/reference/keyofAndIndexedAccessErrors.errors.txt @@ -61,9 +61,11 @@ tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(114,5): error Type 'string' is not assignable to type 'J'. tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(117,5): error TS2322: Type 'T[K]' is not assignable to type 'U[J]'. Type 'T' is not assignable to type 'U'. +tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(122,5): error TS2322: Type '42' is not assignable to type 'keyof T'. +tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(123,5): error TS2322: Type '"hello"' is not assignable to type 'keyof T'. -==== tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts (36 errors) ==== +==== tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts (38 errors) ==== class Shape { name: string; width: number; @@ -281,4 +283,14 @@ tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(117,5): error !!! error TS2322: Type 'T[K]' is not assignable to type 'U[J]'. !!! error TS2322: Type 'T' is not assignable to type 'U'. } + + // The constraint of 'keyof T' is 'keyof T' + function f4(k: keyof T) { + k = 42; // error + ~ +!!! error TS2322: Type '42' is not assignable to type 'keyof T'. + k = "hello"; // error + ~ +!!! error TS2322: Type '"hello"' is not assignable to type 'keyof T'. + } \ No newline at end of file diff --git a/tests/baselines/reference/keyofAndIndexedAccessErrors.js b/tests/baselines/reference/keyofAndIndexedAccessErrors.js index c64f4df5331..a3124ce4c71 100644 --- a/tests/baselines/reference/keyofAndIndexedAccessErrors.js +++ b/tests/baselines/reference/keyofAndIndexedAccessErrors.js @@ -117,6 +117,12 @@ function f3, U extends T, J extends K>( tk = uj; uj = tk; // error } + +// The constraint of 'keyof T' is 'keyof T' +function f4(k: keyof T) { + k = 42; // error + k = "hello"; // error +} //// [keyofAndIndexedAccessErrors.js] @@ -178,3 +184,8 @@ function f3(t, k, tk, u, j, uk, tj, uj) { tk = uj; uj = tk; // error } +// The constraint of 'keyof T' is 'keyof T' +function f4(k) { + k = 42; // error + k = "hello"; // error +} diff --git a/tests/baselines/reference/keyofAndIndexedAccessErrors.symbols b/tests/baselines/reference/keyofAndIndexedAccessErrors.symbols index f5c5f41dddf..72739eb76d7 100644 --- a/tests/baselines/reference/keyofAndIndexedAccessErrors.symbols +++ b/tests/baselines/reference/keyofAndIndexedAccessErrors.symbols @@ -412,3 +412,19 @@ function f3, U extends T, J extends K>( >tk : Symbol(tk, Decl(keyofAndIndexedAccessErrors.ts, 99, 15)) } +// The constraint of 'keyof T' is 'keyof T' +function f4(k: keyof T) { +>f4 : Symbol(f4, Decl(keyofAndIndexedAccessErrors.ts, 117, 1)) +>T : Symbol(T, Decl(keyofAndIndexedAccessErrors.ts, 120, 12)) +>K : Symbol(K, Decl(keyofAndIndexedAccessErrors.ts, 120, 25)) +>T : Symbol(T, Decl(keyofAndIndexedAccessErrors.ts, 120, 12)) +>k : Symbol(k, Decl(keyofAndIndexedAccessErrors.ts, 120, 50)) +>T : Symbol(T, Decl(keyofAndIndexedAccessErrors.ts, 120, 12)) + + k = 42; // error +>k : Symbol(k, Decl(keyofAndIndexedAccessErrors.ts, 120, 50)) + + k = "hello"; // error +>k : Symbol(k, Decl(keyofAndIndexedAccessErrors.ts, 120, 50)) +} + diff --git a/tests/baselines/reference/keyofAndIndexedAccessErrors.types b/tests/baselines/reference/keyofAndIndexedAccessErrors.types index c15f5ebb6ef..e5f8b6ee8e3 100644 --- a/tests/baselines/reference/keyofAndIndexedAccessErrors.types +++ b/tests/baselines/reference/keyofAndIndexedAccessErrors.types @@ -396,3 +396,19 @@ function f3, U extends T, J extends K>( >tk : T[K] } +// The constraint of 'keyof T' is 'keyof T' +function f4(k: keyof T) { +>f4 : (k: keyof T) => void +>k : keyof T + + k = 42; // error +>k = 42 : 42 +>k : keyof T +>42 : 42 + + k = "hello"; // error +>k = "hello" : "hello" +>k : keyof T +>"hello" : "hello" +} + diff --git a/tests/baselines/reference/mappedTypes4.types b/tests/baselines/reference/mappedTypes4.types index 039aabb7306..fac8d982e92 100644 --- a/tests/baselines/reference/mappedTypes4.types +++ b/tests/baselines/reference/mappedTypes4.types @@ -45,7 +45,7 @@ function boxify(obj: T): Boxified { } return obj; >obj : any ->obj : never +>obj : T } type A = { a: string }; diff --git a/tests/baselines/reference/recursiveTypeRelations.errors.txt b/tests/baselines/reference/recursiveTypeRelations.errors.txt index 8c5596958c3..e39ca75dab9 100644 --- a/tests/baselines/reference/recursiveTypeRelations.errors.txt +++ b/tests/baselines/reference/recursiveTypeRelations.errors.txt @@ -1,9 +1,12 @@ tests/cases/compiler/recursiveTypeRelations.ts(8,5): error TS2391: Function implementation is missing or not immediately following the declaration. tests/cases/compiler/recursiveTypeRelations.ts(27,38): error TS2304: Cannot find name 'ClassNameObject'. +tests/cases/compiler/recursiveTypeRelations.ts(27,55): error TS2345: Argument of type '(obj: any, key: keyof S) => any' is not assignable to parameter of type '(previousValue: any, currentValue: string, currentIndex: number, array: string[]) => any'. + Types of parameters 'key' and 'currentValue' are incompatible. + Type 'string' is not assignable to type 'keyof S'. tests/cases/compiler/recursiveTypeRelations.ts(27,61): error TS2304: Cannot find name 'ClassNameObject'. -==== tests/cases/compiler/recursiveTypeRelations.ts (3 errors) ==== +==== tests/cases/compiler/recursiveTypeRelations.ts (4 errors) ==== // Repro from #14896 type Attributes = { @@ -35,6 +38,10 @@ tests/cases/compiler/recursiveTypeRelations.ts(27,61): error TS2304: Cannot find return Object.keys(arg).reduce((obj: ClassNameObject, key: keyof S) => { ~~~~~~~~~~~~~~~ !!! error TS2304: Cannot find name 'ClassNameObject'. + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2345: Argument of type '(obj: any, key: keyof S) => any' is not assignable to parameter of type '(previousValue: any, currentValue: string, currentIndex: number, array: string[]) => any'. +!!! error TS2345: Types of parameters 'key' and 'currentValue' are incompatible. +!!! error TS2345: Type 'string' is not assignable to type 'keyof S'. ~~~~~~~~~~~~~~~ !!! error TS2304: Cannot find name 'ClassNameObject'. const exportedClassName = styles[key]; diff --git a/tests/baselines/reference/strictTypeofUnionNarrowing.types b/tests/baselines/reference/strictTypeofUnionNarrowing.types index 3039ece15d6..1ec44e14ff0 100644 --- a/tests/baselines/reference/strictTypeofUnionNarrowing.types +++ b/tests/baselines/reference/strictTypeofUnionNarrowing.types @@ -12,7 +12,7 @@ function stringify1(anything: { toString(): string } | undefined): string { >"string" : "string" >anything.toUpperCase() : string >anything.toUpperCase : () => string ->anything : { toString(): string; } & string +>anything : string >toUpperCase : () => string >"" : "" } @@ -29,7 +29,7 @@ function stringify2(anything: {} | undefined): string { >"string" : "string" >anything.toUpperCase() : string >anything.toUpperCase : () => string ->anything : string & {} +>anything : string >toUpperCase : () => string >"" : "" } @@ -64,7 +64,7 @@ function stringify4(anything: { toString?(): string } | undefined): string { >"string" : "string" >anything.toUpperCase() : string >anything.toUpperCase : () => string ->anything : {} & string +>anything : string >toUpperCase : () => string >"" : "" } diff --git a/tests/baselines/reference/typeGuardOfFormTypeOfPrimitiveSubtype.types b/tests/baselines/reference/typeGuardOfFormTypeOfPrimitiveSubtype.types index 6510b73d44d..4787c07d758 100644 --- a/tests/baselines/reference/typeGuardOfFormTypeOfPrimitiveSubtype.types +++ b/tests/baselines/reference/typeGuardOfFormTypeOfPrimitiveSubtype.types @@ -14,7 +14,7 @@ if (typeof a === "number") { let c: number = a; >c : number ->a : number & {} +>a : number } if (typeof a === "string") { >typeof a === "string" : boolean @@ -24,7 +24,7 @@ if (typeof a === "string") { let c: string = a; >c : string ->a : string & {} +>a : string } if (typeof a === "boolean") { >typeof a === "boolean" : boolean @@ -34,7 +34,7 @@ if (typeof a === "boolean") { let c: boolean = a; >c : boolean ->a : (false & {}) | (true & {}) +>a : boolean } if (typeof b === "number") { @@ -45,7 +45,7 @@ if (typeof b === "number") { let c: number = b; >c : number ->b : { toString(): string; } & number +>b : number } if (typeof b === "string") { >typeof b === "string" : boolean @@ -55,7 +55,7 @@ if (typeof b === "string") { let c: string = b; >c : string ->b : { toString(): string; } & string +>b : string } if (typeof b === "boolean") { >typeof b === "boolean" : boolean @@ -65,6 +65,6 @@ if (typeof b === "boolean") { let c: boolean = b; >c : boolean ->b : ({ toString(): string; } & false) | ({ toString(): string; } & true) +>b : boolean } diff --git a/tests/cases/compiler/declarationEmitToDeclarationDirWithCompositeOption.ts b/tests/cases/compiler/declarationEmitToDeclarationDirWithCompositeOption.ts new file mode 100644 index 00000000000..77a4c3099cf --- /dev/null +++ b/tests/cases/compiler/declarationEmitToDeclarationDirWithCompositeOption.ts @@ -0,0 +1,11 @@ +// @composite: true +// @filename: /foo/tsconfig.json +{ + "compilerOptions": { "composite": true, "declarationDir": "out" } +} + +// @filename: /foo/test.ts +interface Foo { + x: number; +} +export default Foo; diff --git a/tests/cases/compiler/declarationEmitToDeclarationDirWithDeclarationOption.ts b/tests/cases/compiler/declarationEmitToDeclarationDirWithDeclarationOption.ts new file mode 100644 index 00000000000..e3d4231a8f6 --- /dev/null +++ b/tests/cases/compiler/declarationEmitToDeclarationDirWithDeclarationOption.ts @@ -0,0 +1,11 @@ +// @declaration: true +// @filename: /foo/tsconfig.json +{ + "compilerOptions": { "declaration": true, "declarationDir": "out" } +} + +// @filename: /foo/test.ts +interface Foo { + x: number; +} +export default Foo; diff --git a/tests/cases/compiler/declarationEmitToDeclarationDirWithoutCompositeAndDeclarationOptions.ts b/tests/cases/compiler/declarationEmitToDeclarationDirWithoutCompositeAndDeclarationOptions.ts new file mode 100644 index 00000000000..08405e5fc96 --- /dev/null +++ b/tests/cases/compiler/declarationEmitToDeclarationDirWithoutCompositeAndDeclarationOptions.ts @@ -0,0 +1,10 @@ +// @filename: /foo/tsconfig.json +{ + "compilerOptions": { "declarationDir": "out" } +} + +// @filename: /foo/test.ts +interface Foo { + x: number; +} +export default Foo; diff --git a/tests/cases/compiler/jsFileCompilationWithEnabledCompositeOption.ts b/tests/cases/compiler/jsFileCompilationWithEnabledCompositeOption.ts new file mode 100644 index 00000000000..90340ef5d3a --- /dev/null +++ b/tests/cases/compiler/jsFileCompilationWithEnabledCompositeOption.ts @@ -0,0 +1,10 @@ +// @allowJs: true +// @out: out.js +// @composite: true +// @filename: a.ts +class c { +} + +// @filename: b.js +function foo() { +} diff --git a/tests/cases/conformance/controlFlow/controlFlowTruthiness.ts b/tests/cases/conformance/controlFlow/controlFlowTruthiness.ts index ba1947da13b..0245f9bfea0 100644 --- a/tests/cases/conformance/controlFlow/controlFlowTruthiness.ts +++ b/tests/cases/conformance/controlFlow/controlFlowTruthiness.ts @@ -68,3 +68,30 @@ function f6() { y; // string | undefined } } + +function f7(x: {}) { + if (x) { + x; // {} + } + else { + x; // {} + } +} + +function f8(x: T) { + if (x) { + x; // {} + } + else { + x; // {} + } +} + +function f9(x: T) { + if (x) { + x; // {} + } + else { + x; // never + } +} \ No newline at end of file diff --git a/tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts b/tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts index bb2b9d95117..3660ff418e8 100644 --- a/tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts +++ b/tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts @@ -116,3 +116,9 @@ function f3, U extends T, J extends K>( tk = uj; uj = tk; // error } + +// The constraint of 'keyof T' is 'keyof T' +function f4(k: keyof T) { + k = 42; // error + k = "hello"; // error +} diff --git a/tests/cases/fourslash/codeFixCannotFindModule_all.ts b/tests/cases/fourslash/codeFixCannotFindModule_all.ts index 044d0c5deb0..1b1b36d13ad 100644 --- a/tests/cases/fourslash/codeFixCannotFindModule_all.ts +++ b/tests/cases/fourslash/codeFixCannotFindModule_all.ts @@ -21,12 +21,11 @@ test.setTypesRegistry({ goTo.marker(); verify.codeFixAll({ - fixId: "fixCannotFindModule", + fixId: "installTypesPackage", fixAllDescription: "Install all missing types packages", + newFileContent: {}, // no changes commands: [ { packageName: "@types/abs", file: "/a.ts", type: "install package" }, { packageName: "@types/zap", file: "/a.ts", type: "install package" }, ], - newFileContent: `import * as abs from "abs"; -import * as zap from "zap";` // unchanged }); diff --git a/tests/cases/fourslash/codeFixCannotFindModule_generateTypes.ts b/tests/cases/fourslash/codeFixCannotFindModule_generateTypes.ts new file mode 100644 index 00000000000..af8707a9f81 --- /dev/null +++ b/tests/cases/fourslash/codeFixCannotFindModule_generateTypes.ts @@ -0,0 +1,44 @@ +/// + +// @Filename: /dir/node_modules/plus/index.js +////module.exports = function plus(x, y) { return x + y; }; + +// @Filename: /dir/a.ts +////import plus = require("plus"); +////plus; + +// @Filename: /dir/tsconfig.json +////{ +//// "compilerOptions": { +//// "paths": { +//// "nota": ["valid_pathmapping"], +//// dontCrash: 37, +//// "*": ["abc123"], +//// } +//// } +////} + +goTo.file("/dir/a.ts"); +verify.codeFix({ + description: "Generate types for 'plus'", + newFileContent: { + "/dir/tsconfig.json": +`{ + "compilerOptions": { + "baseUrl": ".", + "paths": { + "*": ["types/*"], + "nota": ["valid_pathmapping"], + dontCrash: 37, + "*": ["abc123"], + } + } +}`, + }, + commands: [{ + type: "generate types", + file: "/dir/a.ts", + fileToGenerateTypesFor: "/dir/node_modules/plus/index.js", + outputFileName: "/dir/types/plus.d.ts", + }], +}); diff --git a/tests/cases/fourslash/codeFixCannotFindModule_generateTypes_all.ts b/tests/cases/fourslash/codeFixCannotFindModule_generateTypes_all.ts new file mode 100644 index 00000000000..183ca3324a4 --- /dev/null +++ b/tests/cases/fourslash/codeFixCannotFindModule_generateTypes_all.ts @@ -0,0 +1,47 @@ +/// + +// @Filename: /dir/node_modules/plus/index.js +////module.exports = function plus(x, y) { return x + y; }; + +// @Filename: /dir/node_modules/times/index.js +////module.exports = function times(x, y) { return x * y; }; + +// @Filename: /dir/a.ts +////import plus = require("plus"); +////import times = require("times"); +////plus; + +// @Filename: /dir/tsconfig.json +////{ +//// "compilerOptions": {} +////} + +goTo.file("/dir/a.ts"); +verify.codeFixAll({ + fixAllDescription: "Generate types for all packages without types", + fixId: "generateTypes", + newFileContent: { + "/dir/tsconfig.json": +// TODO: GH#26618 +`{ + "compilerOptions": { + "baseUrl": ".", + "paths": { "*": ["types/*"] }, +} +}`, + }, + commands: [ + { + type: "generate types", + file: "/dir/a.ts", + fileToGenerateTypesFor: "/dir/node_modules/plus/index.js", + outputFileName: "/dir/types/plus.d.ts", + }, + { + type: "generate types", + file: "/dir/a.ts", + fileToGenerateTypesFor: "/dir/node_modules/times/index.js", + outputFileName: "/dir/types/times.d.ts", + }, + ], +}); diff --git a/tests/cases/fourslash/codeFixCannotFindModule_generateTypes_noExistingCompilerOptions.ts b/tests/cases/fourslash/codeFixCannotFindModule_generateTypes_noExistingCompilerOptions.ts new file mode 100644 index 00000000000..99a22078143 --- /dev/null +++ b/tests/cases/fourslash/codeFixCannotFindModule_generateTypes_noExistingCompilerOptions.ts @@ -0,0 +1,28 @@ +/// + +// @Filename: /dir/node_modules/plus/index.js +////module.exports = function plus(x, y) { return x * y; }; + +// @Filename: /dir/a.ts +////import plus = require("plus"); +////plus; + +// @Filename: /dir/tsconfig.json +////{} + +goTo.file("/dir/a.ts"); +verify.codeFix({ + description: "Generate types for 'plus'", + newFileContent: { + "/dir/tsconfig.json": +`{ + "compilerOptions": { "baseUrl": ".", "paths": { "*": ["types/*"] } }, +}`, + }, + commands: [{ + type: "generate types", + file: "/dir/a.ts", + fileToGenerateTypesFor: "/dir/node_modules/plus/index.js", + outputFileName: "/dir/types/plus.d.ts", + }], +}); diff --git a/tests/cases/fourslash/codeFixCannotFindModule_generateTypes_notForNonexistentPackage.ts b/tests/cases/fourslash/codeFixCannotFindModule_generateTypes_notForNonexistentPackage.ts new file mode 100644 index 00000000000..a4e6450e933 --- /dev/null +++ b/tests/cases/fourslash/codeFixCannotFindModule_generateTypes_notForNonexistentPackage.ts @@ -0,0 +1,13 @@ +/// + +// @Filename: /a.ts +////import plus = require("plus"); +////plus; + +// @Filename: /tsconfig.json +////{ +//// "compilerOptions": {} +////} + +goTo.file("/a.ts"); +verify.codeFixAvailable([]); diff --git a/tests/cases/fourslash/codeFixCannotFindModule_generateTypes_typesDirectoryExists.ts b/tests/cases/fourslash/codeFixCannotFindModule_generateTypes_typesDirectoryExists.ts new file mode 100644 index 00000000000..5c095f84ba7 --- /dev/null +++ b/tests/cases/fourslash/codeFixCannotFindModule_generateTypes_typesDirectoryExists.ts @@ -0,0 +1,30 @@ +/// + +// @Filename: /dir/node_modules/plus/index.js +////module.exports = function plus(x, y) { return x * y; }; + +// @Filename: /dir/a.ts +////import plus = require("plus"); +////plus; + +// @Filename: /dir/tsconfig.json +////{ +//// "compilerOptions": { +//// "baseUrl": "base", +//// "paths": { +//// "*": ["myTypes/*"], +//// }, +//// }, +////} + +goTo.file("/dir/a.ts"); +verify.codeFix({ + description: "Generate types for 'plus'", + newFileContent: {}, // no change + commands: [{ + type: "generate types", + file: "/dir/a.ts", + fileToGenerateTypesFor: "/dir/node_modules/plus/index.js", + outputFileName: "/dir/base/myTypes/plus.d.ts", + }], +}); diff --git a/tests/cases/fourslash/codeFixGenerateDefinitions.ts b/tests/cases/fourslash/codeFixGenerateDefinitions.ts new file mode 100644 index 00000000000..41dc24d3a3c --- /dev/null +++ b/tests/cases/fourslash/codeFixGenerateDefinitions.ts @@ -0,0 +1,9 @@ +/// + +// @Filename: /node_modules/foo/index.d.ts +////module.exports = 0; + +// @Filename: /a.ts +////import * as foo from "foo"; + +verify.not.codeFixAvailable(); diff --git a/tests/cases/fourslash/codeFixUndeclaredMethodFunctionArgs.ts b/tests/cases/fourslash/codeFixUndeclaredMethodFunctionArgs.ts index 1389a50935b..478b2a7c647 100644 --- a/tests/cases/fourslash/codeFixUndeclaredMethodFunctionArgs.ts +++ b/tests/cases/fourslash/codeFixUndeclaredMethodFunctionArgs.ts @@ -28,5 +28,5 @@ verify.codeFix({ foo1(arg0: () => number, arg1: () => string, arg2: () => boolean): any { throw new Error("Method not implemented."); } - ` + `, }); diff --git a/tests/cases/fourslash/formatWithTabs.ts b/tests/cases/fourslash/formatWithTabs.ts index c33808bd26a..e1b8878f4a9 100644 --- a/tests/cases/fourslash/formatWithTabs.ts +++ b/tests/cases/fourslash/formatWithTabs.ts @@ -13,4 +13,4 @@ format.document(); verify.currentFileContentIs( `const foo = [ 1 -];`); \ No newline at end of file +];`); diff --git a/tests/cases/fourslash/fourslash.ts b/tests/cases/fourslash/fourslash.ts index cf4752a65ab..e254b2b51db 100644 --- a/tests/cases/fourslash/fourslash.ts +++ b/tests/cases/fourslash/fourslash.ts @@ -284,7 +284,7 @@ declare namespace FourSlashInterface { docCommentTemplateAt(markerName: string | FourSlashInterface.Marker, expectedOffset: number, expectedText: string): void; noDocCommentTemplateAt(markerName: string | FourSlashInterface.Marker): void; rangeAfterCodeFix(expectedText: string, includeWhiteSpace?: boolean, errorCode?: number, index?: number): void; - codeFixAll(options: { fixId: string, fixAllDescription: string, newFileContent: string, commands?: {}[] }): void; + codeFixAll(options: { fixId: string, fixAllDescription: string, newFileContent: NewFileContent, commands?: {}[] }): void; fileAfterApplyingRefactorAtMarker(markerName: string, expectedContent: string, refactorNameToApply: string, actionName: string, formattingOptions?: FormatCodeOptions): void; rangeIs(expectedText: string, includeWhiteSpace?: boolean): void; fileAfterApplyingRefactorAtMarker(markerName: string, expectedContent: string, refactorNameToApply: string, formattingOptions?: FormatCodeOptions): void; @@ -348,6 +348,8 @@ declare namespace FourSlashInterface { readonly preferences?: UserPreferences; }): void; noMoveToNewFile(): void; + + generateTypes(...options: GenerateTypesOptions[]): void; } class edit { backspace(count?: number): void; @@ -637,6 +639,13 @@ declare namespace FourSlashInterface { readonly text: string | undefined; } + interface GenerateTypesOptions { + readonly name?: string; + readonly value: unknown; + readonly output?: string | undefined; + readonly outputBaseline?: string; + } + type ArrayOrSingle = T | ReadonlyArray; type NewFileContent = string | { readonly [fileName: string]: string }; } diff --git a/tests/cases/fourslash/generateTypes.ts b/tests/cases/fourslash/generateTypes.ts new file mode 100644 index 00000000000..bfe521e2046 --- /dev/null +++ b/tests/cases/fourslash/generateTypes.ts @@ -0,0 +1,109 @@ +/// + +////dummy text + +verify.generateTypes( +{ + value: 0, + output: +`export = example; +declare const example: number;`, +}, +{ + value: (x, y) => x + y, + output: +`export = example; +declare function example(x: any, y: any): void;`, +}, +{ + // non-arrow functions have different toString(), so important to test + value: function(x, y) { + return x * y; + function inner() { + arguments; // Should not affect type inference + } + }, + output: +`export = example; +declare function example(x: any, y: any): any;`, +}, +{ + value: function(x) { arguments; }, + output: +`export = example; +declare function example(x: any, ...args: any[]): void;`, +}, + +{ + value: ({ default() {} }), + output: +`export default function _default(): void;`, +}, + +{ + value: ({ default: class {} }), + output: +`export default class _default { +}`, +}, + +{ + value: new Date(), + output: +`export = example; +declare const example: Date;`, +}, + +{ + value: [0], + output: +`export = example; +declare const example: number[];`, +}, +{ + value: [() => 0, () => ""], + output: +`export = example; +declare const example: Function[];`, +}, +{ + value: (() => { + const a = []; + a.push(a); + return a; + })(), + output: +`export = example; +declare const example: any[];`, +}, +{ + value: (() => { + const o = { + default: 0, + a: 0, + b: "", + self: null, + fn: x => x, + ns1: { x: 0, default: 0 }, + ns2: { fn: x => x, default: 0 }, + }; + o.self = o; + return o; + })(), + output: +`export const a: number; +export const b: string; +export default _default; +export const _default: number; +export function fn(x: any): void; +export const ns1: { + default: number; + x: number; +}; +export namespace ns2 { + function fn(x: any): void; +} +// Circular reference from example +export const self: any;`, +}, +); diff --git a/tests/cases/fourslash/generateTypes_baselines.ts b/tests/cases/fourslash/generateTypes_baselines.ts new file mode 100644 index 00000000000..a8363b30283 --- /dev/null +++ b/tests/cases/fourslash/generateTypes_baselines.ts @@ -0,0 +1,35 @@ +/// + +////dummy text + +verify.generateTypes( + // would like to test against the real "global" but that may vary between node versions. + { + value: { + Array: ignore(Array, ["values"]), + Boolean, + Date, + Math, + Number, + RegExp, + String: ignore(String, ["padStart", "padEnd", "trimStart", "trimEnd"]), + Symbol: ignore(Symbol, ["asyncIterator"]), + }, + outputBaseline: "global", + }, + { value: require("lodash"), outputBaseline: "lodash" }, +); + +// Ignore properties only present in newer versions of node. +function ignore(Cls: Function, ignored: ReadonlyArray): Function { + class Copy {} + for (const name of Object.getOwnPropertyNames(Cls)) { + if (ignored.includes(name) || name === "arguments" || name === "caller" || name === "name" || name === "length" || name === "prototype") continue; + Copy[name] = Cls[name]; + } + for (const name of Object.getOwnPropertyNames(Cls.prototype)) { + if (ignored.includes(name)) continue; + Copy.prototype[name] = Cls.prototype[name]; + } + return Copy; +} diff --git a/tests/cases/fourslash/generateTypes_classes.ts b/tests/cases/fourslash/generateTypes_classes.ts new file mode 100644 index 00000000000..94fd43fe529 --- /dev/null +++ b/tests/cases/fourslash/generateTypes_classes.ts @@ -0,0 +1,109 @@ +/// + +////dummy text + +verify.generateTypes( +{ + value: class {}, + output: +`export = example; +declare class example { +}`, +}, + +{ + value: class { + constructor(x) { + (this as any).x = x; + // Code inside this function should be ignored + function f(this: any) { + this.y = 0; + } + // Same for this class + class Other { constructor() { (this as any).z = 0; } } + } + }, + output: +`export = example; +declare class example { + constructor(x: any); + x: any; +}`, +}, +{ + value: { x: 0, export: 0 }, + output: `export const x: number;`, +}, +{ + value: (() => { + class Super { + superField = 0; // TODO: climb to prototype.constructor and get instance fields? + superMethod() {} + static superStaticMethod() {} + } + class C extends Super { + constructor() { + super(); + (this as any)._privateField = 0; + (this as any).field = 0; + } + + _privateMethod() {} + method(_p) { + (this as any).otherField = 0; // TODO: include this in output? + } + + static _privateStatic() {} + static staticMethod(_s: any) {} + static staticMethodWithNoNamespaceMembers(_p: any) {} + + static _privateStaticField = 0; + static staticField = 0; + static innerClass = class {}; + } + (C.prototype as any).prototypeNonFunction = 0; // ignored + (C.staticMethod as any).staticMethodProperty = 0; + (C.staticMethod as any)._staticFieldPrivateMember = 0; + (C.prototype.method as any).methodMember = 0; // ignored + // Non-identifier names should be ignored. + (C as any)["&"] = function() {}; + (C.prototype as any)["&"] = function() {}; + return C; + })(), + output: +`export = example; +declare class example { + static staticField: number; + static staticMethodWithNoNamespaceMembers(_p: any): void; + static superStaticMethod(): void; + field: any; + method(_p: any): void; + superMethod(): void; +} +declare namespace example { + class innerClass { + } + function staticMethod(_s: any): void; + namespace staticMethod { + const staticMethodProperty: number; + } +}`, +}, + +{ + value: (() => { + function F() { this.x = 0; } + (F as any).staticMethod = function() {} + F.prototype.method = function() { } + return F; + })(), + output: +`export = example; +declare class example { + static staticMethod(): void; + x: any; + method(): void; +}`, +}, + +);