From 39eb187e5c530ac1124bfeefd7c2c811493e55de Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Fri, 11 Nov 2016 01:22:06 -0800 Subject: [PATCH 01/31] Added diagnostic. --- src/compiler/diagnosticMessages.json | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 9a353e6fb36..1ae59b61acc 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -3159,6 +3159,10 @@ "category": "Message", "code": 90007 }, + "Add 'this.' to unresolved variable.": { + "category": "Message", + "code": 90008 + }, "Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig": { "category": "Error", "code": 90009 From da356b075a4829e616bebe26196c5944ac9393c9 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Fri, 11 Nov 2016 01:25:50 -0800 Subject: [PATCH 02/31] Added tests for missing 'this' property accesses. --- tests/cases/fourslash/codeFixAddForgottenThis01.ts | 10 ++++++++++ tests/cases/fourslash/codeFixAddForgottenThis02.ts | 9 +++++++++ 2 files changed, 19 insertions(+) create mode 100644 tests/cases/fourslash/codeFixAddForgottenThis01.ts create mode 100644 tests/cases/fourslash/codeFixAddForgottenThis02.ts diff --git a/tests/cases/fourslash/codeFixAddForgottenThis01.ts b/tests/cases/fourslash/codeFixAddForgottenThis01.ts new file mode 100644 index 00000000000..90d191b6ba5 --- /dev/null +++ b/tests/cases/fourslash/codeFixAddForgottenThis01.ts @@ -0,0 +1,10 @@ +/// + +////class C { +//// foo: number; +//// constructor() { +//// [|foo = 10|]; +//// } +////} + +verify.rangeAfterCodeFix("this.foo = 10"); \ No newline at end of file diff --git a/tests/cases/fourslash/codeFixAddForgottenThis02.ts b/tests/cases/fourslash/codeFixAddForgottenThis02.ts new file mode 100644 index 00000000000..b387f349073 --- /dev/null +++ b/tests/cases/fourslash/codeFixAddForgottenThis02.ts @@ -0,0 +1,9 @@ +/// + +////class C { +//// constructor(public foo) { +//// } +//// bar() { [|foo = 10|] }; +////} + +verify.rangeAfterCodeFix("this.foo = 10"); \ No newline at end of file From f2c0aa4444db61ee3870c6d3dc7b8cb74cc44e05 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Fri, 11 Nov 2016 01:27:26 -0800 Subject: [PATCH 03/31] Added fix for missing 'this.' property accesses. --- .../codefixes/fixForgottenThisPropertyAccess.ts | 16 ++++++++++++++++ src/services/codefixes/fixes.ts | 3 ++- src/services/tsconfig.json | 3 ++- 3 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 src/services/codefixes/fixForgottenThisPropertyAccess.ts diff --git a/src/services/codefixes/fixForgottenThisPropertyAccess.ts b/src/services/codefixes/fixForgottenThisPropertyAccess.ts new file mode 100644 index 00000000000..c9d59dd8337 --- /dev/null +++ b/src/services/codefixes/fixForgottenThisPropertyAccess.ts @@ -0,0 +1,16 @@ +/* @internal */ +namespace ts.codefix { + registerCodeFix({ + errorCodes: [Diagnostics.Cannot_find_name_0_Did_you_mean_the_instance_member_this_0.code], + getCodeActions: (context: CodeFixContext) => { + const sourceFile = context.sourceFile; + const token = getTokenAtPosition(sourceFile, context.span.start); + const start = token.getStart(sourceFile); + + return [{ + description: getLocaleSpecificMessage(Diagnostics.Add_this_to_unresolved_variable), + changes: [{ fileName: sourceFile.fileName, textChanges: [{ newText: "this.", span: { start, length: 0 } }] }] + }]; + } + }); +} \ No newline at end of file diff --git a/src/services/codefixes/fixes.ts b/src/services/codefixes/fixes.ts index 982b6abc84b..da51f04ff8c 100644 --- a/src/services/codefixes/fixes.ts +++ b/src/services/codefixes/fixes.ts @@ -3,4 +3,5 @@ /// /// /// -/// \ No newline at end of file +/// +/// \ No newline at end of file diff --git a/src/services/tsconfig.json b/src/services/tsconfig.json index 69efe29ba39..699ffcd7612 100644 --- a/src/services/tsconfig.json +++ b/src/services/tsconfig.json @@ -91,6 +91,7 @@ "codeFixes/fixClassIncorrectlyImplementsInterface.ts", "codeFixes/fixClassDoesntImplementInheritedAbstractMember.ts", "codeFixes/fixClassSuperMustPrecedeThisAccess.ts", - "codeFixes/fixConstructorForDerivedNeedSuperCall.ts" + "codeFixes/fixConstructorForDerivedNeedSuperCall.ts", + "codefixes/fixForgottenThisPropertyAccess.ts" ] } From b220831ab7a89fd7e03889911cea738e9daafd31 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Tue, 29 Nov 2016 07:01:31 -0800 Subject: [PATCH 04/31] Sort matched files by include order --- src/compiler/commandLineParser.ts | 1 - src/compiler/core.ts | 123 +++++++++++------- src/harness/unittests/matchFiles.ts | 52 ++++++-- .../nodeModulesMaxDepthExceeded.errors.txt | 20 +-- .../amd/nodeModulesMaxDepthExceeded.json | 4 +- .../nodeModulesMaxDepthExceeded.errors.txt | 20 +-- .../node/nodeModulesMaxDepthExceeded.json | 4 +- 7 files changed, 141 insertions(+), 83 deletions(-) diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index 3c6a86134ea..a81698469e7 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -1264,7 +1264,6 @@ namespace ts { const literalFiles = reduceProperties(literalFileMap, addFileToOutput, []); const wildcardFiles = reduceProperties(wildcardFileMap, addFileToOutput, []); - wildcardFiles.sort(host.useCaseSensitiveFileNames ? compareStrings : compareStringsCaseInsensitive); return { fileNames: literalFiles.concat(wildcardFiles), wildcardDirectories diff --git a/src/compiler/core.ts b/src/compiler/core.ts index 89057dd2939..8406404132d 100644 --- a/src/compiler/core.ts +++ b/src/compiler/core.ts @@ -1668,7 +1668,19 @@ namespace ts { const singleAsteriskRegexFragmentFiles = "([^./]|(\\.(?!min\\.js$))?)*"; const singleAsteriskRegexFragmentOther = "[^/]*"; - export function getRegularExpressionForWildcard(specs: string[], basePath: string, usage: "files" | "directories" | "exclude") { + export function getRegularExpressionForWildcard(specs: string[], basePath: string, usage: "files" | "directories" | "exclude"): string | undefined { + const patterns = getRegularExpressionsForWildcards(specs, basePath, usage); + if (!patterns || !patterns.length) { + return undefined; + } + + const pattern = patterns.map(pattern => `(${pattern})`).join("|"); + // If excluding, match "foo/bar/baz...", but if including, only allow "foo". + const terminator = usage === "exclude" ? "($|/)" : "$"; + return `^(${pattern})${terminator}`; + } + + function getRegularExpressionsForWildcards(specs: string[], basePath: string, usage: "files" | "directories" | "exclude"): string[] | undefined { if (specs === undefined || specs.length === 0) { return undefined; } @@ -1682,33 +1694,8 @@ namespace ts { */ const doubleAsteriskRegexFragment = usage === "exclude" ? "(/.+?)?" : "(/[^/.][^/]*)*?"; - let pattern = ""; - let hasWrittenSubpattern = false; - for (const spec of specs) { - if (!spec) { - continue; - } - - const subPattern = getSubPatternFromSpec(spec, basePath, usage, singleAsteriskRegexFragment, doubleAsteriskRegexFragment, replaceWildcardCharacter); - if (subPattern === undefined) { - continue; - } - - if (hasWrittenSubpattern) { - pattern += "|"; - } - - pattern += "(" + subPattern + ")"; - hasWrittenSubpattern = true; - } - - if (!pattern) { - return undefined; - } - - // If excluding, match "foo/bar/baz...", but if including, only allow "foo". - const terminator = usage === "exclude" ? "($|/)" : "$"; - return `^(${pattern})${terminator}`; + return flatMap(specs, spec => + spec && getSubPatternFromSpec(spec, basePath, usage, singleAsteriskRegexFragment, doubleAsteriskRegexFragment, replaceWildcardCharacter)); } /** @@ -1803,6 +1790,9 @@ namespace ts { } export interface FileMatcherPatterns { + /** One pattern for each "include" spec. */ + includeFilePatterns: string[]; + /** One pattern matching one of any of the "include" specs. */ includeFilePattern: string; includeDirectoryPattern: string; excludePattern: string; @@ -1815,6 +1805,7 @@ namespace ts { const absolutePath = combinePaths(currentDirectory, path); return { + includeFilePatterns: map(getRegularExpressionsForWildcards(includes, absolutePath, "files"), pattern => `^${pattern}$`), includeFilePattern: getRegularExpressionForWildcard(includes, absolutePath, "files"), includeDirectoryPattern: getRegularExpressionForWildcard(includes, absolutePath, "directories"), excludePattern: getRegularExpressionForWildcard(excludes, absolutePath, "exclude"), @@ -1829,38 +1820,76 @@ namespace ts { const patterns = getFileMatcherPatterns(path, excludes, includes, useCaseSensitiveFileNames, currentDirectory); const regexFlag = useCaseSensitiveFileNames ? "" : "i"; - const includeFileRegex = patterns.includeFilePattern && new RegExp(patterns.includeFilePattern, regexFlag); + const includeFileRegexes = patterns.includeFilePatterns && patterns.includeFilePatterns.map(pattern => new RegExp(pattern, regexFlag)); const includeDirectoryRegex = patterns.includeDirectoryPattern && new RegExp(patterns.includeDirectoryPattern, regexFlag); const excludeRegex = patterns.excludePattern && new RegExp(patterns.excludePattern, regexFlag); - const result: string[] = []; + // Associate an array of results with each include regex. This keeps results in order of the "include" order. + // If there are no "includes", then just put everything in results[0]. + const results: string[][] = includeFileRegexes ? includeFileRegexes.map(() => []) : [[]]; + for (const basePath of patterns.basePaths) { - visitDirectory(basePath, combinePaths(currentDirectory, basePath)); + forEachFileInRecursiveDirectories(basePath, combinePaths(currentDirectory, basePath), { useCaseSensitiveFileNames, getFileSystemEntries, includeDirectory, visitFile }); } - return result; + + return flatten(results); + + function includeDirectory(absoluteDirectoryName: string): boolean { + return (!includeDirectoryRegex || includeDirectoryRegex.test(absoluteDirectoryName)) && + (!excludeRegex || !excludeRegex.test(absoluteDirectoryName)); + } + + function visitFile(fileName: string, absoluteFileName: string): void { + if (extensions && !fileExtensionIsAny(fileName, extensions) || + excludeRegex && excludeRegex.test(absoluteFileName)) { + return; + } + + if (!includeFileRegexes) { + results[0].push(fileName); + } + else { + for (let i = 0; i < includeFileRegexes.length; i++) { + if (includeFileRegexes[i].test(absoluteFileName)) { + results[i].push(fileName); + // Only include a file once. + break; + } + } + } + } + } + + interface RecursiveDirectoryVisitor { + useCaseSensitiveFileNames: boolean; + getFileSystemEntries: (path: string) => FileSystemEntries; + includeDirectory: (absoluteDirectoryName: string) => boolean; + visitFile: (fileName: string, absoluteFileName: string) => void; + } + + function forEachFileInRecursiveDirectories(start: string, absoluteStart: string, visitor: RecursiveDirectoryVisitor): void { + visitDirectory(start, absoluteStart); function visitDirectory(path: string, absolutePath: string) { - const { files, directories } = getFileSystemEntries(path); + let { files, directories } = visitor.getFileSystemEntries(path); + files = sorted(files); + directories = sorted(directories); - for (const current of files) { - const name = combinePaths(path, current); - const absoluteName = combinePaths(absolutePath, current); - if ((!extensions || fileExtensionIsAny(name, extensions)) && - (!includeFileRegex || includeFileRegex.test(absoluteName)) && - (!excludeRegex || !excludeRegex.test(absoluteName))) { - result.push(name); - } + for (const file of files) { + visitor.visitFile(combinePaths(path, file), combinePaths(absolutePath, file)); } - for (const current of directories) { - const name = combinePaths(path, current); - const absoluteName = combinePaths(absolutePath, current); - if ((!includeDirectoryRegex || includeDirectoryRegex.test(absoluteName)) && - (!excludeRegex || !excludeRegex.test(absoluteName))) { - visitDirectory(name, absoluteName); + for (const dir of directories) { + const absoluteName = combinePaths(absolutePath, dir); + if (visitor.includeDirectory(absoluteName)) { + visitDirectory(combinePaths(path, dir), absoluteName); } } } + + function sorted(names: string[]): string[] { + return names.slice().sort(visitor.useCaseSensitiveFileNames ? compareStrings : compareStringsCaseInsensitive); + } } /** diff --git a/src/harness/unittests/matchFiles.ts b/src/harness/unittests/matchFiles.ts index c562fcefe91..00efe73c3e2 100644 --- a/src/harness/unittests/matchFiles.ts +++ b/src/harness/unittests/matchFiles.ts @@ -346,9 +346,9 @@ namespace ts { fileNames: [ "c:/dev/a.ts", "c:/dev/b.ts", + "c:/dev/node_modules/a.ts", "c:/dev/bower_components/a.ts", - "c:/dev/jspm_packages/a.ts", - "c:/dev/node_modules/a.ts" + "c:/dev/jspm_packages/a.ts" ], wildcardDirectories: {}, }; @@ -373,9 +373,9 @@ namespace ts { options: {}, errors: [], fileNames: [ + "c:/dev/node_modules/a.ts", "c:/dev/bower_components/a.ts", - "c:/dev/jspm_packages/a.ts", - "c:/dev/node_modules/a.ts" + "c:/dev/jspm_packages/a.ts" ], wildcardDirectories: {}, }; @@ -398,9 +398,9 @@ namespace ts { fileNames: [ "c:/dev/a.ts", "c:/dev/b.ts", + "c:/dev/node_modules/a.ts", "c:/dev/bower_components/a.ts", - "c:/dev/jspm_packages/a.ts", - "c:/dev/node_modules/a.ts" + "c:/dev/jspm_packages/a.ts" ], wildcardDirectories: {}, }; @@ -410,6 +410,36 @@ namespace ts { }); describe("with wildcard include list", () => { + it("is sorted in include order, then in alphabetical order", () => { + const json = { + include: [ + "z/*.ts", + "x/*.ts" + ] + }; + const expected: ts.ParsedCommandLine = { + options: {}, + errors: [], + fileNames: [ + "c:/dev/z/a.ts", + "c:/dev/z/aba.ts", + "c:/dev/z/abz.ts", + "c:/dev/z/b.ts", + "c:/dev/z/bba.ts", + "c:/dev/z/bbz.ts", + "c:/dev/x/a.ts", + "c:/dev/x/aa.ts", + "c:/dev/x/b.ts" + ], + wildcardDirectories: { + "c:/dev/z": ts.WatchDirectoryFlags.None, + "c:/dev/x": ts.WatchDirectoryFlags.None + }, + }; + const actual = ts.parseJsonConfigFileContent(json, caseInsensitiveHost, caseInsensitiveBasePath); + assertParsed(actual, expected); + }); + it("same named declarations are excluded", () => { const json = { include: [ @@ -506,8 +536,8 @@ namespace ts { options: {}, errors: [], fileNames: [ - "c:/dev/x/a.ts", "c:/dev/x/y/a.ts", + "c:/dev/x/a.ts", "c:/dev/z/a.ts" ], wildcardDirectories: { @@ -1230,8 +1260,8 @@ namespace ts { options: {}, errors: [], fileNames: [ - "c:/dev/.z/.b.ts", - "c:/dev/x/.y/a.ts" + "c:/dev/x/.y/a.ts", + "c:/dev/.z/.b.ts" ], wildcardDirectories: {} }; @@ -1271,8 +1301,8 @@ namespace ts { options: {}, errors: [], fileNames: [ - "c:/dev/.z/.b.ts", - "c:/dev/x/.y/a.ts" + "c:/dev/x/.y/a.ts", + "c:/dev/.z/.b.ts" ], wildcardDirectories: { "c:/dev/.z": ts.WatchDirectoryFlags.Recursive, diff --git a/tests/baselines/reference/project/nodeModulesMaxDepthExceeded/amd/nodeModulesMaxDepthExceeded.errors.txt b/tests/baselines/reference/project/nodeModulesMaxDepthExceeded/amd/nodeModulesMaxDepthExceeded.errors.txt index 99e7b193f0c..5b5199ad7ae 100644 --- a/tests/baselines/reference/project/nodeModulesMaxDepthExceeded/amd/nodeModulesMaxDepthExceeded.errors.txt +++ b/tests/baselines/reference/project/nodeModulesMaxDepthExceeded/amd/nodeModulesMaxDepthExceeded.errors.txt @@ -2,19 +2,10 @@ maxDepthExceeded/root.ts(3,1): error TS2322: Type '"10"' is not assignable to ty maxDepthExceeded/root.ts(4,4): error TS2540: Cannot assign to 'rel' because it is a constant or a read-only property. -==== entry.js (0 errors) ==== - var m3 = require("m3"); - - module.exports = { - "a": 42, - "b": "hello, world", - "person": m3.person - }; - ==== relative.js (0 errors) ==== exports.relativeProp = true; -==== maxDepthExceeded/node_modules/m1/index.js (0 errors) ==== +==== index.js (0 errors) ==== var m2 = require('m2'); var rel = require('./relative'); @@ -40,4 +31,13 @@ maxDepthExceeded/root.ts(4,4): error TS2540: Cannot assign to 'rel' because it i !!! error TS2540: Cannot assign to 'rel' because it is a constant or a read-only property. m1.f2.person.age = "10"; // OK if stopped at 2 modules: person will be "any". + +==== entry.js (0 errors) ==== + var m3 = require("m3"); + + module.exports = { + "a": 42, + "b": "hello, world", + "person": m3.person + }; \ No newline at end of file diff --git a/tests/baselines/reference/project/nodeModulesMaxDepthExceeded/amd/nodeModulesMaxDepthExceeded.json b/tests/baselines/reference/project/nodeModulesMaxDepthExceeded/amd/nodeModulesMaxDepthExceeded.json index 86e856dc7b8..9efa0e936ac 100644 --- a/tests/baselines/reference/project/nodeModulesMaxDepthExceeded/amd/nodeModulesMaxDepthExceeded.json +++ b/tests/baselines/reference/project/nodeModulesMaxDepthExceeded/amd/nodeModulesMaxDepthExceeded.json @@ -7,10 +7,10 @@ "project": "maxDepthExceeded", "resolvedInputFiles": [ "lib.d.ts", - "maxDepthExceeded/node_modules/m2/entry.js", "maxDepthExceeded/node_modules/m1/relative.js", "maxDepthExceeded/node_modules/m1/index.js", - "maxDepthExceeded/root.ts" + "maxDepthExceeded/root.ts", + "maxDepthExceeded/node_modules/m2/entry.js" ], "emittedFiles": [ "maxDepthExceeded/built/node_modules/m1/relative.js", diff --git a/tests/baselines/reference/project/nodeModulesMaxDepthExceeded/node/nodeModulesMaxDepthExceeded.errors.txt b/tests/baselines/reference/project/nodeModulesMaxDepthExceeded/node/nodeModulesMaxDepthExceeded.errors.txt index 99e7b193f0c..5b5199ad7ae 100644 --- a/tests/baselines/reference/project/nodeModulesMaxDepthExceeded/node/nodeModulesMaxDepthExceeded.errors.txt +++ b/tests/baselines/reference/project/nodeModulesMaxDepthExceeded/node/nodeModulesMaxDepthExceeded.errors.txt @@ -2,19 +2,10 @@ maxDepthExceeded/root.ts(3,1): error TS2322: Type '"10"' is not assignable to ty maxDepthExceeded/root.ts(4,4): error TS2540: Cannot assign to 'rel' because it is a constant or a read-only property. -==== entry.js (0 errors) ==== - var m3 = require("m3"); - - module.exports = { - "a": 42, - "b": "hello, world", - "person": m3.person - }; - ==== relative.js (0 errors) ==== exports.relativeProp = true; -==== maxDepthExceeded/node_modules/m1/index.js (0 errors) ==== +==== index.js (0 errors) ==== var m2 = require('m2'); var rel = require('./relative'); @@ -40,4 +31,13 @@ maxDepthExceeded/root.ts(4,4): error TS2540: Cannot assign to 'rel' because it i !!! error TS2540: Cannot assign to 'rel' because it is a constant or a read-only property. m1.f2.person.age = "10"; // OK if stopped at 2 modules: person will be "any". + +==== entry.js (0 errors) ==== + var m3 = require("m3"); + + module.exports = { + "a": 42, + "b": "hello, world", + "person": m3.person + }; \ No newline at end of file diff --git a/tests/baselines/reference/project/nodeModulesMaxDepthExceeded/node/nodeModulesMaxDepthExceeded.json b/tests/baselines/reference/project/nodeModulesMaxDepthExceeded/node/nodeModulesMaxDepthExceeded.json index 86e856dc7b8..9efa0e936ac 100644 --- a/tests/baselines/reference/project/nodeModulesMaxDepthExceeded/node/nodeModulesMaxDepthExceeded.json +++ b/tests/baselines/reference/project/nodeModulesMaxDepthExceeded/node/nodeModulesMaxDepthExceeded.json @@ -7,10 +7,10 @@ "project": "maxDepthExceeded", "resolvedInputFiles": [ "lib.d.ts", - "maxDepthExceeded/node_modules/m2/entry.js", "maxDepthExceeded/node_modules/m1/relative.js", "maxDepthExceeded/node_modules/m1/index.js", - "maxDepthExceeded/root.ts" + "maxDepthExceeded/root.ts", + "maxDepthExceeded/node_modules/m2/entry.js" ], "emittedFiles": [ "maxDepthExceeded/built/node_modules/m1/relative.js", From e8af1d285beb1f3517fd049fb6373958b1b15c4a Mon Sep 17 00:00:00 2001 From: Herrington Darkholme Date: Sun, 8 Jan 2017 18:31:56 +0800 Subject: [PATCH 05/31] update declaration file to use intrinsic object type --- src/lib/es2015.collection.d.ts | 4 ++-- src/lib/es2015.core.d.ts | 2 +- src/lib/es2015.iterable.d.ts | 6 +++--- src/lib/es2015.proxy.d.ts | 4 ++-- src/lib/es2015.symbol.wellknown.d.ts | 4 ++-- src/lib/es5.d.ts | 4 ++-- 6 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/lib/es2015.collection.d.ts b/src/lib/es2015.collection.d.ts index 6a2c43cd050..f93053e4288 100644 --- a/src/lib/es2015.collection.d.ts +++ b/src/lib/es2015.collection.d.ts @@ -22,7 +22,7 @@ interface ReadonlyMap { readonly size: number; } -interface WeakMap { +interface WeakMap { delete(key: K): boolean; get(key: K): V | undefined; has(key: K): boolean; @@ -31,7 +31,7 @@ interface WeakMap { interface WeakMapConstructor { new (): WeakMap; - new (entries?: [K, V][]): WeakMap; + new (entries?: [K, V][]): WeakMap; readonly prototype: WeakMap; } declare var WeakMap: WeakMapConstructor; diff --git a/src/lib/es2015.core.d.ts b/src/lib/es2015.core.d.ts index 356512ecf58..eb23ce7901f 100644 --- a/src/lib/es2015.core.d.ts +++ b/src/lib/es2015.core.d.ts @@ -325,7 +325,7 @@ interface ObjectConstructor { * @param o The object to change its prototype. * @param proto The value of the new prototype or null. */ - setPrototypeOf(o: any, proto: any): any; + setPrototypeOf(o: any, proto: object | null): any; /** * Gets the own property descriptor of the specified object. diff --git a/src/lib/es2015.iterable.d.ts b/src/lib/es2015.iterable.d.ts index de339e2bf2c..c8b5158bcd6 100644 --- a/src/lib/es2015.iterable.d.ts +++ b/src/lib/es2015.iterable.d.ts @@ -99,10 +99,10 @@ interface MapConstructor { new (iterable: Iterable<[K, V]>): Map; } -interface WeakMap { } +interface WeakMap { } interface WeakMapConstructor { - new (iterable: Iterable<[K, V]>): WeakMap; + new (iterable: Iterable<[K, V]>): WeakMap; } interface Set { @@ -442,4 +442,4 @@ interface Float64ArrayConstructor { * @param thisArg Value of 'this' used to invoke the mapfn. */ from(arrayLike: Iterable, mapfn?: (v: number, k: number) => number, thisArg?: any): Float64Array; -} \ No newline at end of file +} diff --git a/src/lib/es2015.proxy.d.ts b/src/lib/es2015.proxy.d.ts index 34aedce0128..efccfd47cc5 100644 --- a/src/lib/es2015.proxy.d.ts +++ b/src/lib/es2015.proxy.d.ts @@ -1,5 +1,5 @@ interface ProxyHandler { - getPrototypeOf? (target: T): {} | null; + getPrototypeOf? (target: T): object | null; setPrototypeOf? (target: T, v: any): boolean; isExtensible? (target: T): boolean; preventExtensions? (target: T): boolean; @@ -12,7 +12,7 @@ interface ProxyHandler { enumerate? (target: T): PropertyKey[]; ownKeys? (target: T): PropertyKey[]; apply? (target: T, thisArg: any, argArray?: any): any; - construct? (target: T, argArray: any, newTarget?: any): {}; + construct? (target: T, argArray: any, newTarget?: any): object } interface ProxyConstructor { diff --git a/src/lib/es2015.symbol.wellknown.d.ts b/src/lib/es2015.symbol.wellknown.d.ts index 145d8af6b10..7177b78e483 100644 --- a/src/lib/es2015.symbol.wellknown.d.ts +++ b/src/lib/es2015.symbol.wellknown.d.ts @@ -110,7 +110,7 @@ interface Map { readonly [Symbol.toStringTag]: "Map"; } -interface WeakMap{ +interface WeakMap{ readonly [Symbol.toStringTag]: "WeakMap"; } @@ -324,4 +324,4 @@ interface Float32Array { */ interface Float64Array { readonly [Symbol.toStringTag]: "Float64Array"; -} \ No newline at end of file +} diff --git a/src/lib/es5.d.ts b/src/lib/es5.d.ts index e5eabe950a2..038d0344fda 100644 --- a/src/lib/es5.d.ts +++ b/src/lib/es5.d.ts @@ -146,14 +146,14 @@ interface ObjectConstructor { * Creates an object that has the specified prototype, and that optionally contains specified properties. * @param o Object to use as a prototype. May be null */ - create(o: T): T; + create(o: T): T; /** * Creates an object that has the specified prototype, and that optionally contains specified properties. * @param o Object to use as a prototype. May be null * @param properties JavaScript object that contains one or more property descriptors. */ - create(o: any, properties: PropertyDescriptorMap): any; + create(o: object | null, properties: PropertyDescriptorMap): any; /** * Adds a property to an object, or modifies attributes of an existing property. From 54d64ceb98425d13ae038e91e46849c5c93ab151 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Mon, 9 Jan 2017 06:55:42 -0800 Subject: [PATCH 06/31] Respond to PR comment --- src/compiler/core.ts | 83 ++++++++++++++++++-------------------------- 1 file changed, 34 insertions(+), 49 deletions(-) diff --git a/src/compiler/core.ts b/src/compiler/core.ts index 3aab91f53ad..89131e16359 100644 --- a/src/compiler/core.ts +++ b/src/compiler/core.ts @@ -164,6 +164,16 @@ namespace ts { return undefined; } + /** Works like Array.prototype.findIndex, returning `-1` if no element satisfying the predicate is found. */ + export function findIndex(array: T[], predicate: (element: T, index: number) => boolean): number { + for (let i = 0; i < array.length; i++) { + if (predicate(array[i], i)) { + return i; + } + } + return -1; + } + /** * Returns the first truthy result of `callback`, or else fails. * This is like `forEach`, but never returns undefined. @@ -1810,68 +1820,43 @@ namespace ts { // If there are no "includes", then just put everything in results[0]. const results: string[][] = includeFileRegexes ? includeFileRegexes.map(() => []) : [[]]; + const comparer = useCaseSensitiveFileNames ? compareStrings : compareStringsCaseInsensitive; for (const basePath of patterns.basePaths) { - forEachFileInRecursiveDirectories(basePath, combinePaths(currentDirectory, basePath), { useCaseSensitiveFileNames, getFileSystemEntries, includeDirectory, visitFile }); + visitDirectory(basePath, combinePaths(currentDirectory, basePath)); } return flatten(results); - function includeDirectory(absoluteDirectoryName: string): boolean { - return (!includeDirectoryRegex || includeDirectoryRegex.test(absoluteDirectoryName)) && - (!excludeRegex || !excludeRegex.test(absoluteDirectoryName)); - } + function visitDirectory(path: string, absolutePath: string) { + let { files, directories } = getFileSystemEntries(path); + files = files.slice().sort(comparer); + directories = directories.slice().sort(comparer); - function visitFile(fileName: string, absoluteFileName: string): void { - if (extensions && !fileExtensionIsAny(fileName, extensions) || - excludeRegex && excludeRegex.test(absoluteFileName)) { - return; - } - - if (!includeFileRegexes) { - results[0].push(fileName); - } - else { - for (let i = 0; i < includeFileRegexes.length; i++) { - if (includeFileRegexes[i].test(absoluteFileName)) { - results[i].push(fileName); - // Only include a file once. - break; + for (const current of files) { + const name = combinePaths(path, current); + const absoluteName = combinePaths(absolutePath, current); + if (extensions && !fileExtensionIsAny(name, extensions)) continue; + if (excludeRegex && excludeRegex.test(absoluteName)) continue; + if (!includeFileRegexes) { + results[0].push(name); + } + else { + const includeIndex = findIndex(includeFileRegexes, re => re.test(absoluteName)); + if (includeIndex !== -1) { + results[includeIndex].push(name); } } } - } - } - interface RecursiveDirectoryVisitor { - useCaseSensitiveFileNames: boolean; - getFileSystemEntries: (path: string) => FileSystemEntries; - includeDirectory: (absoluteDirectoryName: string) => boolean; - visitFile: (fileName: string, absoluteFileName: string) => void; - } - - function forEachFileInRecursiveDirectories(start: string, absoluteStart: string, visitor: RecursiveDirectoryVisitor): void { - visitDirectory(start, absoluteStart); - - function visitDirectory(path: string, absolutePath: string) { - let { files, directories } = visitor.getFileSystemEntries(path); - files = sorted(files); - directories = sorted(directories); - - for (const file of files) { - visitor.visitFile(combinePaths(path, file), combinePaths(absolutePath, file)); - } - - for (const dir of directories) { - const absoluteName = combinePaths(absolutePath, dir); - if (visitor.includeDirectory(absoluteName)) { - visitDirectory(combinePaths(path, dir), absoluteName); + for (const current of directories) { + const name = combinePaths(path, current); + const absoluteName = combinePaths(absolutePath, current); + if ((!includeDirectoryRegex || includeDirectoryRegex.test(absoluteName)) && + (!excludeRegex || !excludeRegex.test(absoluteName))) { + visitDirectory(name, absoluteName); } } } - - function sorted(names: string[]): string[] { - return names.slice().sort(visitor.useCaseSensitiveFileNames ? compareStrings : compareStringsCaseInsensitive); - } } /** From 77a504bb6495c277d5c8742426cf58162edbd06a Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Fri, 20 Jan 2017 14:58:07 -0800 Subject: [PATCH 07/31] Clean up code in findAllReferences --- src/services/findAllReferences.ts | 217 ++++++++++++++++-------------- src/services/utilities.ts | 2 +- 2 files changed, 115 insertions(+), 104 deletions(-) diff --git a/src/services/findAllReferences.ts b/src/services/findAllReferences.ts index 82a5da453b7..3c38c4e7536 100644 --- a/src/services/findAllReferences.ts +++ b/src/services/findAllReferences.ts @@ -7,30 +7,9 @@ namespace ts.FindAllReferences { export function getReferencedSymbolsForNode(typeChecker: TypeChecker, cancellationToken: CancellationToken, node: Node, sourceFiles: SourceFile[], findInStrings: boolean, findInComments: boolean, implementations: boolean): ReferencedSymbol[] | undefined { if (!implementations) { - if (isTypeKeyword(node.kind)) { - return getAllReferencesForKeyword(sourceFiles, node.kind, cancellationToken); - } - - // Labels - if (isLabelName(node)) { - if (isJumpStatementTarget(node)) { - const labelDefinition = getTargetLabel((node.parent), (node).text); - // if we have a label definition, look within its statement for references, if not, then - // the label is undefined and we have no results.. - return labelDefinition && getLabelReferencesInNode(labelDefinition.parent, labelDefinition, cancellationToken); - } - else { - // it is a label definition and not a target, search within the parent labeledStatement - return getLabelReferencesInNode(node.parent, node, cancellationToken); - } - } - - if (isThis(node)) { - return getReferencesForThisKeyword(node, sourceFiles, typeChecker, cancellationToken); - } - - if (node.kind === SyntaxKind.SuperKeyword) { - return getReferencesForSuperKeyword(node, typeChecker, cancellationToken); + const special = getReferencedSymbolsSpecial(node, sourceFiles, typeChecker, cancellationToken); + if (special) { + return special; } } @@ -69,9 +48,8 @@ namespace ts.FindAllReferences { // Maps from a symbol ID to the ReferencedSymbol entry in 'result'. const symbolToIndex: number[] = []; - let result: ReferencedSymbol[]; + const result: ReferencedSymbol[] = []; if (scope) { - result = []; getReferencesInNode(scope, symbol, declaredName, node, searchMeaning, findInStrings, findInComments, result, symbolToIndex, implementations, typeChecker, cancellationToken); } else { @@ -79,7 +57,6 @@ namespace ts.FindAllReferences { for (const sourceFile of sourceFiles) { cancellationToken.throwIfCancellationRequested(); if (sourceFileHasName(sourceFile, internedName)) { - result = result || []; getReferencesInNode(sourceFile, symbol, declaredName, node, searchMeaning, findInStrings, findInComments, result, symbolToIndex, implementations, typeChecker, cancellationToken); } } @@ -88,6 +65,37 @@ namespace ts.FindAllReferences { return result; } + /** getReferencedSymbols for special node kinds. */ + function getReferencedSymbolsSpecial(node: Node, sourceFiles: SourceFile[], typeChecker: TypeChecker, cancellationToken: CancellationToken): ReferencedSymbol[] | undefined { + if (isTypeKeyword(node.kind)) { + return getAllReferencesForKeyword(sourceFiles, node.kind, cancellationToken); + } + + // Labels + if (isLabelName(node)) { + if (isJumpStatementTarget(node)) { + const labelDefinition = getTargetLabel((node.parent), (node).text); + // if we have a label definition, look within its statement for references, if not, then + // the label is undefined and we have no results.. + return labelDefinition && getLabelReferencesInNode(labelDefinition.parent, labelDefinition, cancellationToken); + } + else { + // it is a label definition and not a target, search within the parent labeledStatement + return getLabelReferencesInNode(node.parent, node, cancellationToken); + } + } + + if (isThis(node)) { + return getReferencesForThisKeyword(node, sourceFiles, typeChecker, cancellationToken); + } + + if (node.kind === SyntaxKind.SuperKeyword) { + return getReferencesForSuperKeyword(node, typeChecker, cancellationToken); + } + + return undefined; + } + function sourceFileHasName(sourceFile: SourceFile, name: string): boolean { return getNameTable(sourceFile).get(name) !== undefined; } @@ -166,7 +174,7 @@ namespace ts.FindAllReferences { // If this is an export or import specifier it could have been renamed using the 'as' syntax. // If so we want to search for whatever under the cursor. if (isImportOrExportSpecifierName(location)) { - return location.getText(); + return location.text; } // Try to get the local symbol if we're dealing with an 'export default' @@ -401,6 +409,9 @@ namespace ts.FindAllReferences { const inheritsFromCache: Map = createMap(); // Build the set of symbols to search for, initially it has only the current symbol const searchSymbols = populateSearchSymbolSet(searchSymbol, searchLocation, typeChecker, implementations); + function isSearchedFor(symbol: Symbol): boolean { + return contains(searchSymbols, symbol); + } for (const position of possiblePositions) { cancellationToken.throwIfCancellationRequested(); @@ -438,7 +449,7 @@ namespace ts.FindAllReferences { if (referenceSymbol) { const referenceSymbolDeclaration = referenceSymbol.valueDeclaration; const shorthandValueSymbol = typeChecker.getShorthandAssignmentValueSymbol(referenceSymbolDeclaration); - const relatedSymbol = getRelatedSymbol(searchSymbols, referenceSymbol, referenceLocation, + const relatedSymbol = getRelatedSymbol(isSearchedFor, referenceSymbol, referenceLocation, /*searchLocationIsConstructor*/ searchLocation.kind === SyntaxKind.ConstructorKeyword, parents, inheritsFromCache, typeChecker); if (relatedSymbol) { @@ -450,7 +461,7 @@ namespace ts.FindAllReferences { * the position in short-hand property assignment excluding property accessing. However, if we do findAllReference at the * position of property accessing, the referenceEntry of such position will be handled in the first case. */ - else if (!(referenceSymbol.flags & SymbolFlags.Transient) && searchSymbols.indexOf(shorthandValueSymbol) >= 0) { + else if (!(referenceSymbol.flags & SymbolFlags.Transient) && isSearchedFor(shorthandValueSymbol)) { addReferenceToRelatedSymbol(referenceSymbolDeclaration.name, shorthandValueSymbol); } else if (searchLocation.kind === SyntaxKind.ConstructorKeyword) { @@ -482,10 +493,6 @@ namespace ts.FindAllReferences { } } - function getPropertyAccessExpressionFromRightHandSide(node: Node): PropertyAccessExpression { - return isRightSideOfPropertyAccess(node) && node.parent; - } - /** Adds references when a constructor is used with `new this()` in its own class and `super()` calls in subclasses. */ function findAdditionalConstructorReferences(referenceSymbol: Symbol, referenceLocation: Node): void { Debug.assert(isClassLike(searchSymbol.valueDeclaration)); @@ -494,7 +501,7 @@ namespace ts.FindAllReferences { if (referenceSymbol === searchSymbol && isClassLike(referenceClass)) { Debug.assert(referenceClass.name === referenceLocation); // This is the class declaration containing the constructor. - addReferences(findOwnConstructorCalls(searchSymbol)); + addReferences(findOwnConstructorCalls(searchSymbol, sourceFile)); } else { // If this class appears in `extends C`, then the extending class' "super" calls are references. @@ -512,58 +519,6 @@ namespace ts.FindAllReferences { } } - /** `classSymbol` is the class where the constructor was defined. - * Reference the constructor and all calls to `new this()`. - */ - function findOwnConstructorCalls(classSymbol: Symbol): Node[] { - const result: Node[] = []; - - for (const decl of classSymbol.members.get("__constructor").declarations) { - const ctrKeyword = ts.findChildOfKind(decl, ts.SyntaxKind.ConstructorKeyword, sourceFile)! - Debug.assert(decl.kind === SyntaxKind.Constructor && !!ctrKeyword); - result.push(ctrKeyword); - } - - classSymbol.exports.forEach(member => { - const decl = member.valueDeclaration; - if (decl && decl.kind === SyntaxKind.MethodDeclaration) { - const body = (decl).body; - if (body) { - forEachDescendantOfKind(body, SyntaxKind.ThisKeyword, thisKeyword => { - if (isNewExpressionTarget(thisKeyword)) { - result.push(thisKeyword); - } - }); - } - } - }); - - return result; - } - - /** Find references to `super` in the constructor of an extending class. */ - function superConstructorAccesses(cls: ClassLikeDeclaration): Node[] { - const symbol = cls.symbol; - const ctr = symbol.members.get("__constructor"); - if (!ctr) { - return []; - } - - const result: Node[] = []; - for (const decl of ctr.declarations) { - Debug.assert(decl.kind === SyntaxKind.Constructor); - const body = (decl).body; - if (body) { - forEachDescendantOfKind(body, SyntaxKind.SuperKeyword, node => { - if (isCallExpressionTarget(node)) { - result.push(node); - } - }); - } - }; - return result; - } - function getReferencedSymbol(symbol: Symbol): ReferencedSymbol { const symbolId = getSymbolId(symbol); let index = symbolToIndex[symbolId]; @@ -591,6 +546,62 @@ namespace ts.FindAllReferences { } } + function getPropertyAccessExpressionFromRightHandSide(node: Node): PropertyAccessExpression { + return isRightSideOfPropertyAccess(node) && node.parent; + } + + /** `classSymbol` is the class where the constructor was defined. + * Reference the constructor and all calls to `new this()`. + */ + function findOwnConstructorCalls(classSymbol: Symbol, sourceFile: SourceFile): Node[] { + const result: Node[] = []; + + for (const decl of classSymbol.members.get("__constructor").declarations) { + const ctrKeyword = ts.findChildOfKind(decl, ts.SyntaxKind.ConstructorKeyword, sourceFile)! + Debug.assert(decl.kind === SyntaxKind.Constructor && !!ctrKeyword); + result.push(ctrKeyword); + } + + classSymbol.exports.forEach(member => { + const decl = member.valueDeclaration; + if (decl && decl.kind === SyntaxKind.MethodDeclaration) { + const body = (decl).body; + if (body) { + forEachDescendantOfKind(body, SyntaxKind.ThisKeyword, thisKeyword => { + if (isNewExpressionTarget(thisKeyword)) { + result.push(thisKeyword); + } + }); + } + } + }); + + return result; + } + + /** Find references to `super` in the constructor of an extending class. */ + function superConstructorAccesses(cls: ClassLikeDeclaration): Node[] { + const symbol = cls.symbol; + const ctr = symbol.members.get("__constructor"); + if (!ctr) { + return []; + } + + const result: Node[] = []; + for (const decl of ctr.declarations) { + Debug.assert(decl.kind === SyntaxKind.Constructor); + const body = (decl).body; + if (body) { + forEachDescendantOfKind(body, SyntaxKind.SuperKeyword, node => { + if (isCallExpressionTarget(node)) { + result.push(node); + } + }); + } + }; + return result; + } + function getImplementationReferenceEntryForNode(refNode: Node, result: ReferenceEntry[], typeChecker: TypeChecker): void { // Check if we found a function/propertyAssignment/method with an implementation or initializer if (isDeclarationName(refNode) && isImplementation(refNode.parent)) { @@ -805,13 +816,13 @@ namespace ts.FindAllReferences { const sourceFile = searchSpaceNode.getSourceFile(); const possiblePositions = getPossibleSymbolReferencePositions(sourceFile, "super", searchSpaceNode.getStart(), searchSpaceNode.getEnd(), cancellationToken); - forEach(possiblePositions, position => { + for (const position of possiblePositions) { cancellationToken.throwIfCancellationRequested(); const node = getTouchingWord(sourceFile, position); if (!node || node.kind !== SyntaxKind.SuperKeyword) { - return; + continue; } const container = getSuperContainer(node, /*stopOnFunctions*/ false); @@ -822,7 +833,7 @@ namespace ts.FindAllReferences { if (container && (ModifierFlags.Static & getModifierFlags(container)) === staticFlag && container.parent.symbol === searchSpaceNode.symbol) { references.push(getReferenceEntryFromNode(node)); } - }); + } const definition = getDefinition(searchSpaceNode.symbol, superKeyword, typeChecker); return [{ definition, references }]; @@ -1057,7 +1068,7 @@ namespace ts.FindAllReferences { // If this is a union property, add all the symbols from all its source symbols in all unioned types. // If the symbol is an instantiation from a another symbol (e.g. widened symbol) , add the root the list - forEach(typeChecker.getRootSymbols(symbol), rootSymbol => { + for (const rootSymbol of typeChecker.getRootSymbols(symbol)) { if (rootSymbol !== symbol) { result.push(rootSymbol); } @@ -1066,7 +1077,7 @@ namespace ts.FindAllReferences { if (!implementations && rootSymbol.parent && rootSymbol.parent.flags & (SymbolFlags.Class | SymbolFlags.Interface)) { getPropertySymbolsFromBaseTypes(rootSymbol.parent, rootSymbol.getName(), result, /*previousIterationSymbolsCache*/ createMap(), typeChecker); } - }); + } return result; } @@ -1130,17 +1141,17 @@ namespace ts.FindAllReferences { } } - function getRelatedSymbol(searchSymbols: Symbol[], referenceSymbol: Symbol, referenceLocation: Node, searchLocationIsConstructor: boolean, parents: Symbol[] | undefined, cache: Map, typeChecker: TypeChecker): Symbol { - if (contains(searchSymbols, referenceSymbol)) { + function getRelatedSymbol(isSearchedFor: (symbol: Symbol) => boolean, referenceSymbol: Symbol, referenceLocation: Node, searchLocationIsConstructor: boolean, parents: Symbol[] | undefined, cache: Map, typeChecker: TypeChecker): Symbol | undefined { + if (isSearchedFor(referenceSymbol)) { // If we are searching for constructor uses, they must be 'new' expressions. - return (!searchLocationIsConstructor || isNewExpressionTarget(referenceLocation)) && referenceSymbol; + return (!searchLocationIsConstructor || isNewExpressionTarget(referenceLocation)) ? referenceSymbol : undefined; } // If the reference symbol is an alias, check if what it is aliasing is one of the search // symbols but by looking up for related symbol of this alias so it can handle multiple level of indirectness. const aliasSymbol = getAliasSymbolForPropertyNameSymbol(referenceSymbol, referenceLocation, typeChecker); if (aliasSymbol) { - return getRelatedSymbol(searchSymbols, aliasSymbol, referenceLocation, searchLocationIsConstructor, parents, cache, typeChecker); + return getRelatedSymbol(isSearchedFor, aliasSymbol, referenceLocation, searchLocationIsConstructor, parents, cache, typeChecker); } // If the reference location is in an object literal, try to get the contextual type for the @@ -1148,9 +1159,8 @@ namespace ts.FindAllReferences { // compare to our searchSymbol const containingObjectLiteralElement = getContainingObjectLiteralElement(referenceLocation); if (containingObjectLiteralElement) { - const contextualSymbol = forEach(getPropertySymbolsFromContextualType(containingObjectLiteralElement, typeChecker), contextualSymbol => { - return forEach(typeChecker.getRootSymbols(contextualSymbol), s => searchSymbols.indexOf(s) >= 0 ? s : undefined); - }); + const contextualSymbol = forEach(getPropertySymbolsFromContextualType(containingObjectLiteralElement, typeChecker), contextualSymbol => + find(typeChecker.getRootSymbols(contextualSymbol), isSearchedFor)); if (contextualSymbol) { return contextualSymbol; @@ -1161,7 +1171,7 @@ namespace ts.FindAllReferences { // In below eg. get 'property' from type of elems iterating type // for ( { property: p2 } of elems) { } const propertySymbol = getPropertySymbolOfDestructuringAssignment(referenceLocation, typeChecker); - if (propertySymbol && searchSymbols.indexOf(propertySymbol) >= 0) { + if (propertySymbol && isSearchedFor(propertySymbol)) { return propertySymbol; } } @@ -1170,7 +1180,7 @@ namespace ts.FindAllReferences { // then include the binding element in the related symbols // let { a } : { a }; const bindingElementPropertySymbol = getPropertySymbolOfObjectBindingPatternWithoutPropertyName(referenceSymbol, typeChecker); - if (bindingElementPropertySymbol && searchSymbols.indexOf(bindingElementPropertySymbol) >= 0) { + if (bindingElementPropertySymbol && isSearchedFor(bindingElementPropertySymbol)) { return bindingElementPropertySymbol; } @@ -1178,7 +1188,7 @@ namespace ts.FindAllReferences { // Or a union property, use its underlying unioned symbols return forEach(typeChecker.getRootSymbols(referenceSymbol), rootSymbol => { // if it is in the list, then we are done - if (searchSymbols.indexOf(rootSymbol) >= 0) { + if (isSearchedFor(rootSymbol)) { return rootSymbol; } @@ -1195,7 +1205,7 @@ namespace ts.FindAllReferences { const result: Symbol[] = []; getPropertySymbolsFromBaseTypes(rootSymbol.parent, rootSymbol.getName(), result, /*previousIterationSymbolsCache*/ createMap(), typeChecker); - return forEach(result, s => searchSymbols.indexOf(s) >= 0 ? s : undefined); + return find(result, isSearchedFor); } return undefined; @@ -1214,6 +1224,7 @@ namespace ts.FindAllReferences { return (node.name).text; } + /** Gets all symbols for one property. Does not get symbols for every property. */ function getPropertySymbolsFromContextualType(node: ObjectLiteralElement, typeChecker: TypeChecker): Symbol[] { const objectLiteral = node.parent; const contextualType = typeChecker.getContextualType(objectLiteral); diff --git a/src/services/utilities.ts b/src/services/utilities.ts index a1680da69c3..f83f9ef8231 100644 --- a/src/services/utilities.ts +++ b/src/services/utilities.ts @@ -1319,7 +1319,7 @@ namespace ts { return name; } - export function isImportOrExportSpecifierName(location: Node): boolean { + export function isImportOrExportSpecifierName(location: Node): location is Identifier { return location.parent && (location.parent.kind === SyntaxKind.ImportSpecifier || location.parent.kind === SyntaxKind.ExportSpecifier) && (location.parent).propertyName === location; From 0ca4cb25d68f370ac6bc5013f71f43b96b6f1e7a Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Mon, 23 Jan 2017 13:23:26 -0800 Subject: [PATCH 08/31] Support find-all-references for default exports --- src/compiler/utilities.ts | 8 +- src/services/findAllReferences.ts | 114 ++++++++++-------- .../fourslash/findAllRefsForDefaultExport.ts | 11 ++ 3 files changed, 82 insertions(+), 51 deletions(-) create mode 100644 tests/cases/fourslash/findAllRefsForDefaultExport.ts diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index b7cc0f0de6d..5c63d8bb60c 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -395,7 +395,7 @@ namespace ts { function isShorthandAmbientModule(node: Node): boolean { // The only kind of module that can be missing a body is a shorthand ambient module. - return node.kind === SyntaxKind.ModuleDeclaration && (!(node).body); + return node && node.kind === SyntaxKind.ModuleDeclaration && (!(node).body); } export function isBlockScopedContainerTopLevel(node: Node): boolean { @@ -3104,7 +3104,11 @@ namespace ts { } export function getLocalSymbolForExportDefault(symbol: Symbol) { - return symbol && symbol.valueDeclaration && hasModifier(symbol.valueDeclaration, ModifierFlags.Default) ? symbol.valueDeclaration.localSymbol : undefined; + return isExportDefaultSymbol(symbol) ? symbol.valueDeclaration.localSymbol : undefined; + } + + export function isExportDefaultSymbol(symbol: Symbol): boolean { + return symbol && symbol.valueDeclaration && hasModifier(symbol.valueDeclaration, ModifierFlags.Default); } /** Return ".ts", ".d.ts", or ".tsx", if that is the extension. */ diff --git a/src/services/findAllReferences.ts b/src/services/findAllReferences.ts index 3c38c4e7536..83006c66d77 100644 --- a/src/services/findAllReferences.ts +++ b/src/services/findAllReferences.ts @@ -15,14 +15,13 @@ namespace ts.FindAllReferences { // `getSymbolAtLocation` normally returns the symbol of the class when given the constructor keyword, // so we have to specify that we want the constructor symbol. - const symbol = typeChecker.getSymbolAtLocation(node); - - if (!implementations && !symbol && node.kind === SyntaxKind.StringLiteral) { - return getReferencesForStringLiteral(node, sourceFiles, typeChecker, cancellationToken); - } + let symbol = typeChecker.getSymbolAtLocation(node); // Could not find a symbol e.g. unknown identifier if (!symbol) { + if (!implementations && node.kind === SyntaxKind.StringLiteral) { + return getReferencesForStringLiteral(node, sourceFiles, typeChecker, cancellationToken); + } // Can't have references to something that we have no symbol for. return undefined; } @@ -34,9 +33,27 @@ namespace ts.FindAllReferences { return undefined; } + const aliasedSymbol = followAliasIfNecessary(symbol, node, typeChecker); + const isShorthandModule = ts.isShorthandAmbientModuleSymbol(aliasedSymbol); + // Don't follow alias for shorthand modules because we lose information that way. + if (!isShorthandModule) { + symbol = aliasedSymbol; + } + // Compute the meaning from the location and the symbol it references const searchMeaning = getIntersectingMeaningFromDeclarations(getMeaningFromLocation(node), declarations); + const result: ReferencedSymbol[] = []; + // Maps from a symbol ID to the ReferencedSymbol entry in 'result'. + const symbolToIndex: number[] = []; + const inheritsFromCache: Map = createMap(); + + // Build the set of symbols to search for, initially it has only the current symbol + const searchSymbols = populateSearchSymbolSet(symbol, node, typeChecker, implementations, isShorthandModule ? aliasedSymbol : undefined); + function isSearchedFor(symbol: Symbol): boolean { + return contains(searchSymbols, symbol); + } + // Get the text to search for. // Note: if this is an external module symbol, the name doesn't include quotes. const declaredName = stripQuotes(getDeclaredName(typeChecker, symbol, node)); @@ -44,25 +61,28 @@ namespace ts.FindAllReferences { // Try to get the smallest valid scope that we can limit our search to; // otherwise we'll need to search globally (i.e. include each file). const scope = getSymbolScope(symbol); - - // Maps from a symbol ID to the ReferencedSymbol entry in 'result'. - const symbolToIndex: number[] = []; - - const result: ReferencedSymbol[] = []; if (scope) { - getReferencesInNode(scope, symbol, declaredName, node, searchMeaning, findInStrings, findInComments, result, symbolToIndex, implementations, typeChecker, cancellationToken); + getRefs(scope, declaredName); } else { - const internedName = getInternedName(symbol, node); + const isDefault = isExportDefaultSymbol(symbol); + const internedName = isDefault ? symbol.valueDeclaration.localSymbol.name : getInternedName(symbol, node); for (const sourceFile of sourceFiles) { cancellationToken.throwIfCancellationRequested(); - if (sourceFileHasName(sourceFile, internedName)) { - getReferencesInNode(sourceFile, symbol, declaredName, node, searchMeaning, findInStrings, findInComments, result, symbolToIndex, implementations, typeChecker, cancellationToken); + const searchName = (isDefault ? getDefaultImportName(symbol, sourceFile, typeChecker) : undefined) || + (sourceFileHasName(sourceFile, internedName) ? declaredName : undefined); + if (searchName !== undefined) { + getRefs(sourceFile, searchName); } } } return result; + + function getRefs(scope: ts.Node, searchName: string): void { + getReferencesInNode(scope, symbol, searchName, node, searchMeaning, findInStrings, findInComments, result, + symbolToIndex, implementations, typeChecker, cancellationToken, isSearchedFor, inheritsFromCache); + } } /** getReferencedSymbols for special node kinds. */ @@ -100,6 +120,23 @@ namespace ts.FindAllReferences { return getNameTable(sourceFile).get(name) !== undefined; } + /** + * Given a symbol, see if any of the imports in a source file reference it. + * Only call this if `symbol` is a default export. + */ + function getDefaultImportName(symbol: Symbol, sourceFile: SourceFile, checker: ts.TypeChecker): string | undefined { + for (const importSpecifier of sourceFile.imports) { + const importDecl = importSpecifier.parent as ts.ImportDeclaration; + Debug.assert(importDecl.moduleSpecifier === importSpecifier); + const defaultName = importDecl.importClause.name; + const defaultReferencedSymbol = checker.getAliasedSymbol(checker.getSymbolAtLocation(defaultName)); + if (symbol === defaultReferencedSymbol) { + return defaultName.text; + } + } + return undefined; + } + function getDefinition(symbol: Symbol, node: Node, typeChecker: TypeChecker): ReferencedSymbolDefinitionInfo { const { displayParts, symbolKind } = SymbolDisplay.getSymbolDisplayPartsDocumentationAndSymbolKind(typeChecker, symbol, node.getSourceFile(), getContainerNode(node), node); const name = displayParts.map(p => p.text).join(""); @@ -177,11 +214,6 @@ namespace ts.FindAllReferences { return location.text; } - // Try to get the local symbol if we're dealing with an 'export default' - // since that symbol has the "true" name. - const localExportDefaultSymbol = getLocalSymbolForExportDefault(symbol); - symbol = localExportDefaultSymbol || symbol; - return stripQuotes(symbol.name); } @@ -398,7 +430,9 @@ namespace ts.FindAllReferences { symbolToIndex: number[], implementations: boolean, typeChecker: TypeChecker, - cancellationToken: CancellationToken): void { + cancellationToken: CancellationToken, + isSearchedFor: (symbol: Symbol) => boolean, + inheritsFromCache: Map): void { const sourceFile = container.getSourceFile(); @@ -406,12 +440,6 @@ namespace ts.FindAllReferences { const possiblePositions = getPossibleSymbolReferencePositions(sourceFile, searchText, start, container.getEnd(), cancellationToken); const parents = getParentSymbolsOfPropertyAccess(); - const inheritsFromCache: Map = createMap(); - // Build the set of symbols to search for, initially it has only the current symbol - const searchSymbols = populateSearchSymbolSet(searchSymbol, searchLocation, typeChecker, implementations); - function isSearchedFor(symbol: Symbol): boolean { - return contains(searchSymbols, symbol); - } for (const position of possiblePositions) { cancellationToken.throwIfCancellationRequested(); @@ -456,11 +484,11 @@ namespace ts.FindAllReferences { addReferenceToRelatedSymbol(referenceLocation, relatedSymbol); } /* Because in short-hand property assignment, an identifier which stored as name of the short-hand property assignment - * has two meaning : property name and property value. Therefore when we do findAllReference at the position where - * an identifier is declared, the language service should return the position of the variable declaration as well as - * the position in short-hand property assignment excluding property accessing. However, if we do findAllReference at the - * position of property accessing, the referenceEntry of such position will be handled in the first case. - */ + * has two meanings: property name and property value. Therefore when we do findAllReference at the position where + * an identifier is declared, the language service should return the position of the variable declaration as well as + * the position in short-hand property assignment excluding property accessing. However, if we do findAllReference at the + * position of property accessing, the referenceEntry of such position will be handled in the first case. + */ else if (!(referenceSymbol.flags & SymbolFlags.Transient) && isSearchedFor(shorthandValueSymbol)) { addReferenceToRelatedSymbol(referenceSymbolDeclaration.name, shorthandValueSymbol); } @@ -472,10 +500,10 @@ namespace ts.FindAllReferences { return; /* If we are just looking for implementations and this is a property access expression, we need to get the - * symbol of the local type of the symbol the property is being accessed on. This is because our search - * symbol may have a different parent symbol if the local type's symbol does not declare the property - * being accessed (i.e. it is declared in some parent class or interface) - */ + * symbol of the local type of the symbol the property is being accessed on. This is because our search + * symbol may have a different parent symbol if the local type's symbol does not declare the property + * being accessed (i.e. it is declared in some parent class or interface) + */ function getParentSymbolsOfPropertyAccess(): Symbol[] | undefined { if (implementations) { const propertyAccessExpression = getPropertyAccessExpressionFromRightHandSide(searchLocation); @@ -994,7 +1022,7 @@ namespace ts.FindAllReferences { } } - function populateSearchSymbolSet(symbol: Symbol, location: Node, typeChecker: TypeChecker, implementations: boolean): Symbol[] { + function populateSearchSymbolSet(symbol: Symbol, location: Node, typeChecker: TypeChecker, implementations: boolean, aliasSymbol?: Symbol): Symbol[] { // The search set contains at least the current symbol let result = [symbol]; @@ -1009,18 +1037,6 @@ namespace ts.FindAllReferences { } } - // If the symbol is an alias, add what it aliases to the list - // import {a} from "mod"; - // export {a} - // If the symbol is an alias to default declaration, add what it aliases to the list - // declare "mod" { export default class B { } } - // import B from "mod"; - //// For export specifiers, the exported name can be referring to a local symbol, e.g.: - //// import {a} from "mod"; - //// export {a as somethingElse} - //// We want the *local* declaration of 'a' as declared in the import, - //// *not* as declared within "mod" (or farther) - const aliasSymbol = getAliasSymbolForPropertyNameSymbol(symbol, location, typeChecker); if (aliasSymbol) { result = result.concat(populateSearchSymbolSet(aliasSymbol, location, typeChecker, implementations)); } @@ -1225,7 +1241,7 @@ namespace ts.FindAllReferences { } /** Gets all symbols for one property. Does not get symbols for every property. */ - function getPropertySymbolsFromContextualType(node: ObjectLiteralElement, typeChecker: TypeChecker): Symbol[] { + function getPropertySymbolsFromContextualType(node: ObjectLiteralElement, typeChecker: TypeChecker): Symbol[] | undefined { const objectLiteral = node.parent; const contextualType = typeChecker.getContextualType(objectLiteral); const name = getNameFromObjectLiteralElement(node); diff --git a/tests/cases/fourslash/findAllRefsForDefaultExport.ts b/tests/cases/fourslash/findAllRefsForDefaultExport.ts new file mode 100644 index 00000000000..a27227b907a --- /dev/null +++ b/tests/cases/fourslash/findAllRefsForDefaultExport.ts @@ -0,0 +1,11 @@ +/// + +// @Filename: a.ts +////export default function /*def*/[|f|]() {} + +// @Filename: b.ts +////import [|g|] from "./a"; +/////*ref*/[|g|](); + +verify.rangesReferenceEachOther(); +verify.goToDefinition("ref", "def"); From e8c3d548eb66c020b02cfa135b492f9480ef639c Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Tue, 24 Jan 2017 12:56:21 -0800 Subject: [PATCH 09/31] Fix tests --- src/services/documentHighlights.ts | 2 +- src/services/findAllReferences.ts | 109 +++++++++++------- src/services/goToImplementation.ts | 2 +- src/services/services.ts | 10 +- tests/cases/fourslash/renameDefaultImport.ts | 17 ++- .../renameDefaultImportDifferentName.ts | 14 ++- .../cases/fourslash/renameImportAndExport.ts | 4 +- 7 files changed, 104 insertions(+), 54 deletions(-) diff --git a/src/services/documentHighlights.ts b/src/services/documentHighlights.ts index 778a007ded5..ff2b819ec26 100644 --- a/src/services/documentHighlights.ts +++ b/src/services/documentHighlights.ts @@ -17,7 +17,7 @@ namespace ts.DocumentHighlights { } function getSemanticDocumentHighlights(node: Node, typeChecker: TypeChecker, cancellationToken: CancellationToken, sourceFilesToSearch: SourceFile[]): DocumentHighlights[] { - const referencedSymbols = FindAllReferences.getReferencedSymbolsForNode(typeChecker, cancellationToken, node, sourceFilesToSearch, /*findInStrings*/false, /*findInComments*/false, /*implementations*/false); + const referencedSymbols = FindAllReferences.getReferencedSymbolsForNode(typeChecker, cancellationToken, node, sourceFilesToSearch); return referencedSymbols && convertReferencedSymbols(referencedSymbols); } diff --git a/src/services/findAllReferences.ts b/src/services/findAllReferences.ts index 83006c66d77..df568f6df3e 100644 --- a/src/services/findAllReferences.ts +++ b/src/services/findAllReferences.ts @@ -1,11 +1,11 @@ /* @internal */ namespace ts.FindAllReferences { - export function findReferencedSymbols(typeChecker: TypeChecker, cancellationToken: CancellationToken, sourceFiles: SourceFile[], sourceFile: SourceFile, position: number, findInStrings: boolean, findInComments: boolean): ReferencedSymbol[] | undefined { + export function findReferencedSymbols(typeChecker: TypeChecker, cancellationToken: CancellationToken, sourceFiles: SourceFile[], sourceFile: SourceFile, position: number, findInStrings: boolean, findInComments: boolean, isForRename: boolean): ReferencedSymbol[] | undefined { const node = getTouchingPropertyName(sourceFile, position, /*includeJsDocComment*/ true); - return getReferencedSymbolsForNode(typeChecker, cancellationToken, node, sourceFiles, findInStrings, findInComments, /*implementations*/false); + return getReferencedSymbolsForNode(typeChecker, cancellationToken, node, sourceFiles, findInStrings, findInComments, isForRename); } - export function getReferencedSymbolsForNode(typeChecker: TypeChecker, cancellationToken: CancellationToken, node: Node, sourceFiles: SourceFile[], findInStrings: boolean, findInComments: boolean, implementations: boolean): ReferencedSymbol[] | undefined { + export function getReferencedSymbolsForNode(typeChecker: TypeChecker, cancellationToken: CancellationToken, node: Node, sourceFiles: SourceFile[], findInStrings?: boolean, findInComments?: boolean, isForRename?: boolean, implementations?: boolean): ReferencedSymbol[] | undefined { if (!implementations) { const special = getReferencedSymbolsSpecial(node, sourceFiles, typeChecker, cancellationToken); if (special) { @@ -33,11 +33,16 @@ namespace ts.FindAllReferences { return undefined; } - const aliasedSymbol = followAliasIfNecessary(symbol, node, typeChecker); - const isShorthandModule = ts.isShorthandAmbientModuleSymbol(aliasedSymbol); - // Don't follow alias for shorthand modules because we lose information that way. - if (!isShorthandModule) { - symbol = aliasedSymbol; + const { symbol: aliasedSymbol, shorthandModuleSymbol } = followAliases(symbol, node, typeChecker, isForRename); + symbol = aliasedSymbol; + + // Build the set of symbols to search for, initially it has only the current symbol + const searchSymbols = populateSearchSymbolSet(symbol, node, typeChecker, implementations); + if (shorthandModuleSymbol) { + searchSymbols.push(shorthandModuleSymbol); + } + function isSearchedFor(symbol: Symbol): boolean { + return contains(searchSymbols, symbol); } // Compute the meaning from the location and the symbol it references @@ -48,12 +53,6 @@ namespace ts.FindAllReferences { const symbolToIndex: number[] = []; const inheritsFromCache: Map = createMap(); - // Build the set of symbols to search for, initially it has only the current symbol - const searchSymbols = populateSearchSymbolSet(symbol, node, typeChecker, implementations, isShorthandModule ? aliasedSymbol : undefined); - function isSearchedFor(symbol: Symbol): boolean { - return contains(searchSymbols, symbol); - } - // Get the text to search for. // Note: if this is an external module symbol, the name doesn't include quotes. const declaredName = stripQuotes(getDeclaredName(typeChecker, symbol, node)); @@ -116,6 +115,31 @@ namespace ts.FindAllReferences { return undefined; } + /** + * Follows aliases to get to the original declaration of a symbol. + * For a shorthand ambient module, we don't follow the alias to it, but we will need to add it to the set of search symbols. + */ + function followAliases(symbol: Symbol, node: Node, typeChecker: TypeChecker, isForRename: boolean): { symbol: Symbol, shorthandModuleSymbol?: Symbol } { + while (true) { + // When renaming a default import, only rename in the current file + if (isForRename && isImportDefaultSymbol(symbol)) { + return { symbol }; + } + + const aliasedSymbol = getAliasSymbolForPropertyNameSymbol(symbol, node, typeChecker); + // Don't follow alias if it goes to unknown symbol. This can happen if it points to an untyped module. + if (!aliasedSymbol || !aliasedSymbol.declarations) { + return { symbol }; + } + + if (ts.isShorthandAmbientModuleSymbol(aliasedSymbol)) { + return { symbol, shorthandModuleSymbol: aliasedSymbol }; + } + + symbol = aliasedSymbol; + } + } + function sourceFileHasName(sourceFile: SourceFile, name: string): boolean { return getNameTable(sourceFile).get(name) !== undefined; } @@ -157,29 +181,30 @@ namespace ts.FindAllReferences { } function getAliasSymbolForPropertyNameSymbol(symbol: Symbol, location: Node, typeChecker: TypeChecker): Symbol | undefined { - if (symbol.flags & SymbolFlags.Alias) { - // Default import get alias - const defaultImport = getDeclarationOfKind(symbol, SyntaxKind.ImportClause); - if (defaultImport) { - return typeChecker.getAliasedSymbol(symbol); - } + if (!(symbol.flags & SymbolFlags.Alias)) { + return undefined; + } - const importOrExportSpecifier = forEach(symbol.declarations, - declaration => (declaration.kind === SyntaxKind.ImportSpecifier || - declaration.kind === SyntaxKind.ExportSpecifier) ? declaration : undefined); - if (importOrExportSpecifier && - // export { a } - (!importOrExportSpecifier.propertyName || - // export {a as class } where a is location - importOrExportSpecifier.propertyName === location)) { - // If Import specifier -> get alias - // else Export specifier -> get local target - return importOrExportSpecifier.kind === SyntaxKind.ImportSpecifier ? - typeChecker.getAliasedSymbol(symbol) : - typeChecker.getExportSpecifierLocalTargetSymbol(importOrExportSpecifier); - } + // Default import get alias + const defaultImport = getDeclarationOfKind(symbol, SyntaxKind.ImportClause); + if (defaultImport) { + return typeChecker.getAliasedSymbol(symbol); + } + + const importOrExportSpecifier = forEach(symbol.declarations, + declaration => (declaration.kind === SyntaxKind.ImportSpecifier || + declaration.kind === SyntaxKind.ExportSpecifier) ? declaration : undefined); + if (importOrExportSpecifier && + // export { a } + (!importOrExportSpecifier.propertyName || + // export {a as class } where a is location + importOrExportSpecifier.propertyName === location)) { + // If Import specifier -> get alias + // else Export specifier -> get local target + return importOrExportSpecifier.kind === SyntaxKind.ImportSpecifier ? + typeChecker.getAliasedSymbol(symbol) : + typeChecker.getExportSpecifierLocalTargetSymbol(importOrExportSpecifier); } - return undefined; } function followAliasIfNecessary(symbol: Symbol, location: Node, typeChecker: TypeChecker): Symbol { @@ -1022,9 +1047,9 @@ namespace ts.FindAllReferences { } } - function populateSearchSymbolSet(symbol: Symbol, location: Node, typeChecker: TypeChecker, implementations: boolean, aliasSymbol?: Symbol): Symbol[] { + function populateSearchSymbolSet(symbol: Symbol, location: Node, typeChecker: TypeChecker, implementations: boolean): Symbol[] { // The search set contains at least the current symbol - let result = [symbol]; + const result = [symbol]; // If the location is name of property symbol from object literal destructuring pattern // Search the property symbol @@ -1037,10 +1062,6 @@ namespace ts.FindAllReferences { } } - if (aliasSymbol) { - result = result.concat(populateSearchSymbolSet(aliasSymbol, location, typeChecker, implementations)); - } - // If the location is in a context sensitive location (i.e. in an object literal) try // to get a contextual type for it, and add the property symbol from the contextual // type to the search set @@ -1072,7 +1093,7 @@ namespace ts.FindAllReferences { // Property Declaration symbol is a member of the class, so the symbol is stored in its class Declaration.symbol.members if (symbol.valueDeclaration && symbol.valueDeclaration.kind === SyntaxKind.Parameter && isParameterPropertyDeclaration(symbol.valueDeclaration)) { - result = result.concat(typeChecker.getSymbolsOfParameterPropertyDeclaration(symbol.valueDeclaration, symbol.name)); + addRange(result, typeChecker.getSymbolsOfParameterPropertyDeclaration(symbol.valueDeclaration, symbol.name)); } // If this is symbol of binding element without propertyName declaration in Object binding pattern @@ -1447,4 +1468,8 @@ namespace ts.FindAllReferences { return false; } + + function isImportDefaultSymbol(symbol: Symbol): boolean { + return symbol.declarations[0].kind === SyntaxKind.ImportClause; + } } diff --git a/src/services/goToImplementation.ts b/src/services/goToImplementation.ts index 123d29630ad..9135e1fe0f0 100644 --- a/src/services/goToImplementation.ts +++ b/src/services/goToImplementation.ts @@ -17,7 +17,7 @@ namespace ts.GoToImplementation { else { // Perform "Find all References" and retrieve only those that are implementations const referencedSymbols = FindAllReferences.getReferencedSymbolsForNode(typeChecker, cancellationToken, - node, sourceFiles, /*findInStrings*/false, /*findInComments*/false, /*implementations*/true); + node, sourceFiles, /*findInStrings*/false, /*findInComments*/false, /*isForRename*/false, /*implementations*/true); const result = flatMap(referencedSymbols, symbol => map(symbol.references, ({ textSpan, fileName }) => ({ textSpan, fileName }))); diff --git a/src/services/services.ts b/src/services/services.ts index cb38b1d500e..00a8c9a9f2e 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -1406,25 +1406,25 @@ namespace ts { } function findRenameLocations(fileName: string, position: number, findInStrings: boolean, findInComments: boolean): RenameLocation[] { - const referencedSymbols = findReferencedSymbols(fileName, position, findInStrings, findInComments); + const referencedSymbols = findReferencedSymbols(fileName, position, findInStrings, findInComments, /*isForRename*/true); return FindAllReferences.convertReferences(referencedSymbols); } function getReferencesAtPosition(fileName: string, position: number): ReferenceEntry[] { - const referencedSymbols = findReferencedSymbols(fileName, position, /*findInStrings*/ false, /*findInComments*/ false); + const referencedSymbols = findReferencedSymbols(fileName, position, /*findInStrings*/ false, /*findInComments*/ false, /*isForRename*/false); return FindAllReferences.convertReferences(referencedSymbols); } function findReferences(fileName: string, position: number): ReferencedSymbol[] { - const referencedSymbols = findReferencedSymbols(fileName, position, /*findInStrings*/ false, /*findInComments*/ false); + const referencedSymbols = findReferencedSymbols(fileName, position, /*findInStrings*/ false, /*findInComments*/ false, /*isForRename*/false); // Only include referenced symbols that have a valid definition. return filter(referencedSymbols, rs => !!rs.definition); } - function findReferencedSymbols(fileName: string, position: number, findInStrings: boolean, findInComments: boolean): ReferencedSymbol[] { + function findReferencedSymbols(fileName: string, position: number, findInStrings: boolean, findInComments: boolean, isForRename: boolean): ReferencedSymbol[] { synchronizeHostData(); - return FindAllReferences.findReferencedSymbols(program.getTypeChecker(), cancellationToken, program.getSourceFiles(), getValidSourceFile(fileName), position, findInStrings, findInComments); + return FindAllReferences.findReferencedSymbols(program.getTypeChecker(), cancellationToken, program.getSourceFiles(), getValidSourceFile(fileName), position, findInStrings, findInComments, isForRename); } /// NavigateTo diff --git a/tests/cases/fourslash/renameDefaultImport.ts b/tests/cases/fourslash/renameDefaultImport.ts index 8d9b65d78d6..ff297ffa147 100644 --- a/tests/cases/fourslash/renameDefaultImport.ts +++ b/tests/cases/fourslash/renameDefaultImport.ts @@ -1,7 +1,7 @@ /// // @Filename: B.ts -////export default class [|B|] { +////export default class /*1*/[|B|] { //// test() { //// } ////} @@ -11,4 +11,17 @@ ////let b = new [|B|](); ////b.test(); -verify.rangesAreRenameLocations(); +goTo.marker("1"); +verify.occurrencesAtPositionCount(1); + +const [C, B0, B1] = test.ranges(); +verify.rangesReferenceEachOther(); + +goTo.rangeStart(C); +verify.renameLocations(false, false, [C, B0, B1]); + +const rangesInB = [B0, B1]; +for (const r of rangesInB) { + goTo.rangeStart(r); + verify.renameLocations(false, false, rangesInB); +} diff --git a/tests/cases/fourslash/renameDefaultImportDifferentName.ts b/tests/cases/fourslash/renameDefaultImportDifferentName.ts index aec699aadda..be968c9b883 100644 --- a/tests/cases/fourslash/renameDefaultImportDifferentName.ts +++ b/tests/cases/fourslash/renameDefaultImportDifferentName.ts @@ -1,7 +1,7 @@ /// // @Filename: B.ts -////export default class /*1*/C { +////export default class /*1*/[|C|] { //// test() { //// } ////} @@ -14,4 +14,14 @@ goTo.marker("1"); verify.occurrencesAtPositionCount(1); -verify.rangesAreRenameLocations(); +const [C, B0, B1] = test.ranges(); +verify.rangesReferenceEachOther(); + +goTo.rangeStart(C); +verify.renameLocations(false, false, [C, B0, B1]); + +const rangesInB = [B0, B1]; +for (const r of rangesInB) { + goTo.rangeStart(r); + verify.renameLocations(false, false, rangesInB); +} diff --git a/tests/cases/fourslash/renameImportAndExport.ts b/tests/cases/fourslash/renameImportAndExport.ts index fe8bce4b208..4545277c564 100644 --- a/tests/cases/fourslash/renameImportAndExport.ts +++ b/tests/cases/fourslash/renameImportAndExport.ts @@ -3,4 +3,6 @@ ////import [|a|] from "module"; ////export { [|a|] }; -verify.rangesAreRenameLocations(); +const [r0, r1] = test.ranges(); +verify.referencesOf(r1, [r0, r1]); +//verify.rangesAreRenameLocations(); From 20249e5c4a66e5abef595c52e87be5b1eabff468 Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Tue, 24 Jan 2017 13:42:05 -0800 Subject: [PATCH 10/31] More exhaustive needsDotDotForPropertyAccess --- src/compiler/emitter.ts | 6 ++- src/compiler/utilities.ts | 42 +++++++++++++++++-- .../propertyAccessNumericLiterals.es6.js | 14 +++++++ .../propertyAccessNumericLiterals.es6.symbols | 21 ++++++++++ .../propertyAccessNumericLiterals.es6.types | 31 ++++++++++++++ .../propertyAccessNumericLiterals.js | 15 +++++++ .../propertyAccessNumericLiterals.symbols | 25 +++++++++++ .../propertyAccessNumericLiterals.types | 37 ++++++++++++++++ .../propertyAccessNumericLiterals.es6.ts | 6 +++ .../propertyAccessNumericLiterals.ts | 7 ++++ 10 files changed, 199 insertions(+), 5 deletions(-) create mode 100644 tests/baselines/reference/propertyAccessNumericLiterals.es6.js create mode 100644 tests/baselines/reference/propertyAccessNumericLiterals.es6.symbols create mode 100644 tests/baselines/reference/propertyAccessNumericLiterals.es6.types create mode 100644 tests/baselines/reference/propertyAccessNumericLiterals.js create mode 100644 tests/baselines/reference/propertyAccessNumericLiterals.symbols create mode 100644 tests/baselines/reference/propertyAccessNumericLiterals.types create mode 100644 tests/cases/conformance/es6/propertyAccess/propertyAccessNumericLiterals.es6.ts create mode 100644 tests/cases/conformance/expressions/propertyAccess/propertyAccessNumericLiterals.ts diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index cc006a06637..27285a4c2dc 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -1054,9 +1054,11 @@ namespace ts { // Also emit a dot if expression is a integer const enum value - it will appear in generated code as numeric literal function needsDotDotForPropertyAccess(expression: Expression) { if (expression.kind === SyntaxKind.NumericLiteral) { - // check if numeric literal was originally written with a dot + // check if numeric literal is a decimal literal that was originally written with a dot const text = getLiteralTextOfNode(expression); - return text.indexOf(tokenToString(SyntaxKind.DotToken)) < 0; + return getNumericLiteralFlags(text, /*hint*/ NumericLiteralFlags.All) === NumericLiteralFlags.None + && !(expression).isOctalLiteral + && text.indexOf(tokenToString(SyntaxKind.DotToken)) < 0; } else if (isPropertyAccessExpression(expression) || isElementAccessExpression(expression)) { // check if constant enum value is integer diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index b7cc0f0de6d..b1d2ba0854c 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -341,16 +341,52 @@ namespace ts { } export function isBinaryOrOctalIntegerLiteral(node: LiteralLikeNode, text: string) { - if (node.kind === SyntaxKind.NumericLiteral && text.length > 1) { + return node.kind === SyntaxKind.NumericLiteral + && (getNumericLiteralFlags(text, /*hint*/ NumericLiteralFlags.BinaryOrOctal) & NumericLiteralFlags.BinaryOrOctal) !== 0; + } + + export const enum NumericLiteralFlags { + None = 0, + Hexadecimal = 1 << 0, + Binary = 1 << 1, + Octal = 1 << 2, + Scientific = 1 << 3, + + BinaryOrOctal = Binary | Octal, + BinaryOrOctalOrHexadecimal = BinaryOrOctal | Hexadecimal, + All = Hexadecimal | Binary | Octal | Scientific, + } + + /** + * Scans a numeric literal string to determine the form of the number. + * @param text Numeric literal text + * @param hint If `Scientific` or `All` is specified, performs a more expensive check to scan for scientific notation. + */ + export function getNumericLiteralFlags(text: string, hint?: NumericLiteralFlags) { + if (text.length > 1) { switch (text.charCodeAt(1)) { case CharacterCodes.b: case CharacterCodes.B: + return NumericLiteralFlags.Binary; case CharacterCodes.o: case CharacterCodes.O: - return true; + return NumericLiteralFlags.Octal; + case CharacterCodes.x: + case CharacterCodes.X: + return NumericLiteralFlags.Hexadecimal; + } + + if (hint & NumericLiteralFlags.Scientific) { + for (let i = text.length - 1; i >= 0; i--) { + switch (text.charCodeAt(i)) { + case CharacterCodes.e: + case CharacterCodes.E: + return NumericLiteralFlags.Scientific; + } + } } } - return false; + return NumericLiteralFlags.None; } function getQuotedEscapedLiteralText(leftQuote: string, text: string, rightQuote: string) { diff --git a/tests/baselines/reference/propertyAccessNumericLiterals.es6.js b/tests/baselines/reference/propertyAccessNumericLiterals.es6.js new file mode 100644 index 00000000000..66ac4178155 --- /dev/null +++ b/tests/baselines/reference/propertyAccessNumericLiterals.es6.js @@ -0,0 +1,14 @@ +//// [propertyAccessNumericLiterals.es6.ts] +0xffffffff.toString(); +0o01234.toString(); +0b01101101.toString(); +1234..toString(); +1e0.toString(); + + +//// [propertyAccessNumericLiterals.es6.js] +0xffffffff.toString(); +0o01234.toString(); +0b01101101.toString(); +1234..toString(); +1e0.toString(); diff --git a/tests/baselines/reference/propertyAccessNumericLiterals.es6.symbols b/tests/baselines/reference/propertyAccessNumericLiterals.es6.symbols new file mode 100644 index 00000000000..dc0be21d84f --- /dev/null +++ b/tests/baselines/reference/propertyAccessNumericLiterals.es6.symbols @@ -0,0 +1,21 @@ +=== tests/cases/conformance/es6/propertyAccess/propertyAccessNumericLiterals.es6.ts === +0xffffffff.toString(); +>0xffffffff.toString : Symbol(Number.toString, Decl(lib.es5.d.ts, --, --)) +>toString : Symbol(Number.toString, Decl(lib.es5.d.ts, --, --)) + +0o01234.toString(); +>0o01234.toString : Symbol(Number.toString, Decl(lib.es5.d.ts, --, --)) +>toString : Symbol(Number.toString, Decl(lib.es5.d.ts, --, --)) + +0b01101101.toString(); +>0b01101101.toString : Symbol(Number.toString, Decl(lib.es5.d.ts, --, --)) +>toString : Symbol(Number.toString, Decl(lib.es5.d.ts, --, --)) + +1234..toString(); +>1234..toString : Symbol(Number.toString, Decl(lib.es5.d.ts, --, --)) +>toString : Symbol(Number.toString, Decl(lib.es5.d.ts, --, --)) + +1e0.toString(); +>1e0.toString : Symbol(Number.toString, Decl(lib.es5.d.ts, --, --)) +>toString : Symbol(Number.toString, Decl(lib.es5.d.ts, --, --)) + diff --git a/tests/baselines/reference/propertyAccessNumericLiterals.es6.types b/tests/baselines/reference/propertyAccessNumericLiterals.es6.types new file mode 100644 index 00000000000..9d32cc5b6e8 --- /dev/null +++ b/tests/baselines/reference/propertyAccessNumericLiterals.es6.types @@ -0,0 +1,31 @@ +=== tests/cases/conformance/es6/propertyAccess/propertyAccessNumericLiterals.es6.ts === +0xffffffff.toString(); +>0xffffffff.toString() : string +>0xffffffff.toString : (radix?: number) => string +>0xffffffff : 4294967295 +>toString : (radix?: number) => string + +0o01234.toString(); +>0o01234.toString() : string +>0o01234.toString : (radix?: number) => string +>0o01234 : 668 +>toString : (radix?: number) => string + +0b01101101.toString(); +>0b01101101.toString() : string +>0b01101101.toString : (radix?: number) => string +>0b01101101 : 109 +>toString : (radix?: number) => string + +1234..toString(); +>1234..toString() : string +>1234..toString : (radix?: number) => string +>1234. : 1234 +>toString : (radix?: number) => string + +1e0.toString(); +>1e0.toString() : string +>1e0.toString : (radix?: number) => string +>1e0 : 1 +>toString : (radix?: number) => string + diff --git a/tests/baselines/reference/propertyAccessNumericLiterals.js b/tests/baselines/reference/propertyAccessNumericLiterals.js new file mode 100644 index 00000000000..1a7eb6c0a21 --- /dev/null +++ b/tests/baselines/reference/propertyAccessNumericLiterals.js @@ -0,0 +1,15 @@ +//// [propertyAccessNumericLiterals.ts] +0xffffffff.toString(); +0o01234.toString(); +0b01101101.toString(); +1234..toString(); +1e0.toString(); +000.toString(); + +//// [propertyAccessNumericLiterals.js] +0xffffffff.toString(); +668..toString(); +109..toString(); +1234..toString(); +1e0.toString(); +000.toString(); diff --git a/tests/baselines/reference/propertyAccessNumericLiterals.symbols b/tests/baselines/reference/propertyAccessNumericLiterals.symbols new file mode 100644 index 00000000000..5be13615e90 --- /dev/null +++ b/tests/baselines/reference/propertyAccessNumericLiterals.symbols @@ -0,0 +1,25 @@ +=== tests/cases/conformance/expressions/propertyAccess/propertyAccessNumericLiterals.ts === +0xffffffff.toString(); +>0xffffffff.toString : Symbol(Number.toString, Decl(lib.d.ts, --, --)) +>toString : Symbol(Number.toString, Decl(lib.d.ts, --, --)) + +0o01234.toString(); +>0o01234.toString : Symbol(Number.toString, Decl(lib.d.ts, --, --)) +>toString : Symbol(Number.toString, Decl(lib.d.ts, --, --)) + +0b01101101.toString(); +>0b01101101.toString : Symbol(Number.toString, Decl(lib.d.ts, --, --)) +>toString : Symbol(Number.toString, Decl(lib.d.ts, --, --)) + +1234..toString(); +>1234..toString : Symbol(Number.toString, Decl(lib.d.ts, --, --)) +>toString : Symbol(Number.toString, Decl(lib.d.ts, --, --)) + +1e0.toString(); +>1e0.toString : Symbol(Number.toString, Decl(lib.d.ts, --, --)) +>toString : Symbol(Number.toString, Decl(lib.d.ts, --, --)) + +000.toString(); +>000.toString : Symbol(Number.toString, Decl(lib.d.ts, --, --)) +>toString : Symbol(Number.toString, Decl(lib.d.ts, --, --)) + diff --git a/tests/baselines/reference/propertyAccessNumericLiterals.types b/tests/baselines/reference/propertyAccessNumericLiterals.types new file mode 100644 index 00000000000..a2567ff2537 --- /dev/null +++ b/tests/baselines/reference/propertyAccessNumericLiterals.types @@ -0,0 +1,37 @@ +=== tests/cases/conformance/expressions/propertyAccess/propertyAccessNumericLiterals.ts === +0xffffffff.toString(); +>0xffffffff.toString() : string +>0xffffffff.toString : (radix?: number) => string +>0xffffffff : 4294967295 +>toString : (radix?: number) => string + +0o01234.toString(); +>0o01234.toString() : string +>0o01234.toString : (radix?: number) => string +>0o01234 : 668 +>toString : (radix?: number) => string + +0b01101101.toString(); +>0b01101101.toString() : string +>0b01101101.toString : (radix?: number) => string +>0b01101101 : 109 +>toString : (radix?: number) => string + +1234..toString(); +>1234..toString() : string +>1234..toString : (radix?: number) => string +>1234. : 1234 +>toString : (radix?: number) => string + +1e0.toString(); +>1e0.toString() : string +>1e0.toString : (radix?: number) => string +>1e0 : 1 +>toString : (radix?: number) => string + +000.toString(); +>000.toString() : string +>000.toString : (radix?: number) => string +>000 : 0 +>toString : (radix?: number) => string + diff --git a/tests/cases/conformance/es6/propertyAccess/propertyAccessNumericLiterals.es6.ts b/tests/cases/conformance/es6/propertyAccess/propertyAccessNumericLiterals.es6.ts new file mode 100644 index 00000000000..81e39965a6a --- /dev/null +++ b/tests/cases/conformance/es6/propertyAccess/propertyAccessNumericLiterals.es6.ts @@ -0,0 +1,6 @@ +// @target: es6 +0xffffffff.toString(); +0o01234.toString(); +0b01101101.toString(); +1234..toString(); +1e0.toString(); diff --git a/tests/cases/conformance/expressions/propertyAccess/propertyAccessNumericLiterals.ts b/tests/cases/conformance/expressions/propertyAccess/propertyAccessNumericLiterals.ts new file mode 100644 index 00000000000..318b05fc0b5 --- /dev/null +++ b/tests/cases/conformance/expressions/propertyAccess/propertyAccessNumericLiterals.ts @@ -0,0 +1,7 @@ +// @target: es3 +0xffffffff.toString(); +0o01234.toString(); +0b01101101.toString(); +1234..toString(); +1e0.toString(); +000.toString(); \ No newline at end of file From 50442b5a23148a9dd818e716748b48be00776a8a Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Tue, 24 Jan 2017 13:57:42 -0800 Subject: [PATCH 11/31] Undo change to test --- tests/cases/fourslash/renameImportAndExport.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/cases/fourslash/renameImportAndExport.ts b/tests/cases/fourslash/renameImportAndExport.ts index 4545277c564..fe8bce4b208 100644 --- a/tests/cases/fourslash/renameImportAndExport.ts +++ b/tests/cases/fourslash/renameImportAndExport.ts @@ -3,6 +3,4 @@ ////import [|a|] from "module"; ////export { [|a|] }; -const [r0, r1] = test.ranges(); -verify.referencesOf(r1, [r0, r1]); -//verify.rangesAreRenameLocations(); +verify.rangesAreRenameLocations(); From 97e964a08f242eb806962d9d53e1d92f7d764fc8 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Wed, 25 Jan 2017 14:15:06 -0800 Subject: [PATCH 12/31] Fix typo --- tests/cases/fourslash/findAllRefsForMappedType.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/cases/fourslash/findAllRefsForMappedType.ts b/tests/cases/fourslash/findAllRefsForMappedType.ts index 620eae0815c..38fb3a226c6 100644 --- a/tests/cases/fourslash/findAllRefsForMappedType.ts +++ b/tests/cases/fourslash/findAllRefsForMappedType.ts @@ -1,7 +1,7 @@ /// ////interface T { [|a|]: number }; -////type U { [K in keyof T]: string }; +////type U = { [K in keyof T]: string }; ////type V = { [K in keyof U]: boolean }; ////const u: U = { [|a|]: "" } ////const v: V = { [|a|]: true } From 5bf9b30c5d596037a10bdf78213b711fac20efbf Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Thu, 26 Jan 2017 09:50:38 -0800 Subject: [PATCH 13/31] Replace `isSearchedFor` with directly passing `searchSymbols` --- src/services/findAllReferences.ts | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/src/services/findAllReferences.ts b/src/services/findAllReferences.ts index df568f6df3e..28474839cf3 100644 --- a/src/services/findAllReferences.ts +++ b/src/services/findAllReferences.ts @@ -41,9 +41,6 @@ namespace ts.FindAllReferences { if (shorthandModuleSymbol) { searchSymbols.push(shorthandModuleSymbol); } - function isSearchedFor(symbol: Symbol): boolean { - return contains(searchSymbols, symbol); - } // Compute the meaning from the location and the symbol it references const searchMeaning = getIntersectingMeaningFromDeclarations(getMeaningFromLocation(node), declarations); @@ -80,7 +77,7 @@ namespace ts.FindAllReferences { function getRefs(scope: ts.Node, searchName: string): void { getReferencesInNode(scope, symbol, searchName, node, searchMeaning, findInStrings, findInComments, result, - symbolToIndex, implementations, typeChecker, cancellationToken, isSearchedFor, inheritsFromCache); + symbolToIndex, implementations, typeChecker, cancellationToken, searchSymbols, inheritsFromCache); } } @@ -456,7 +453,7 @@ namespace ts.FindAllReferences { implementations: boolean, typeChecker: TypeChecker, cancellationToken: CancellationToken, - isSearchedFor: (symbol: Symbol) => boolean, + searchSymbols: Symbol[], inheritsFromCache: Map): void { const sourceFile = container.getSourceFile(); @@ -502,7 +499,7 @@ namespace ts.FindAllReferences { if (referenceSymbol) { const referenceSymbolDeclaration = referenceSymbol.valueDeclaration; const shorthandValueSymbol = typeChecker.getShorthandAssignmentValueSymbol(referenceSymbolDeclaration); - const relatedSymbol = getRelatedSymbol(isSearchedFor, referenceSymbol, referenceLocation, + const relatedSymbol = getRelatedSymbol(searchSymbols, referenceSymbol, referenceLocation, /*searchLocationIsConstructor*/ searchLocation.kind === SyntaxKind.ConstructorKeyword, parents, inheritsFromCache, typeChecker); if (relatedSymbol) { @@ -514,7 +511,7 @@ namespace ts.FindAllReferences { * the position in short-hand property assignment excluding property accessing. However, if we do findAllReference at the * position of property accessing, the referenceEntry of such position will be handled in the first case. */ - else if (!(referenceSymbol.flags & SymbolFlags.Transient) && isSearchedFor(shorthandValueSymbol)) { + else if (!(referenceSymbol.flags & SymbolFlags.Transient) && contains(searchSymbols, shorthandValueSymbol)) { addReferenceToRelatedSymbol(referenceSymbolDeclaration.name, shorthandValueSymbol); } else if (searchLocation.kind === SyntaxKind.ConstructorKeyword) { @@ -1178,8 +1175,8 @@ namespace ts.FindAllReferences { } } - function getRelatedSymbol(isSearchedFor: (symbol: Symbol) => boolean, referenceSymbol: Symbol, referenceLocation: Node, searchLocationIsConstructor: boolean, parents: Symbol[] | undefined, cache: Map, typeChecker: TypeChecker): Symbol | undefined { - if (isSearchedFor(referenceSymbol)) { + function getRelatedSymbol(searchSymbols: Symbol[], referenceSymbol: Symbol, referenceLocation: Node, searchLocationIsConstructor: boolean, parents: Symbol[] | undefined, cache: Map, typeChecker: TypeChecker): Symbol | undefined { + if (contains(searchSymbols, referenceSymbol)) { // If we are searching for constructor uses, they must be 'new' expressions. return (!searchLocationIsConstructor || isNewExpressionTarget(referenceLocation)) ? referenceSymbol : undefined; } @@ -1188,7 +1185,7 @@ namespace ts.FindAllReferences { // symbols but by looking up for related symbol of this alias so it can handle multiple level of indirectness. const aliasSymbol = getAliasSymbolForPropertyNameSymbol(referenceSymbol, referenceLocation, typeChecker); if (aliasSymbol) { - return getRelatedSymbol(isSearchedFor, aliasSymbol, referenceLocation, searchLocationIsConstructor, parents, cache, typeChecker); + return getRelatedSymbol(searchSymbols, aliasSymbol, referenceLocation, searchLocationIsConstructor, parents, cache, typeChecker); } // If the reference location is in an object literal, try to get the contextual type for the @@ -1197,7 +1194,7 @@ namespace ts.FindAllReferences { const containingObjectLiteralElement = getContainingObjectLiteralElement(referenceLocation); if (containingObjectLiteralElement) { const contextualSymbol = forEach(getPropertySymbolsFromContextualType(containingObjectLiteralElement, typeChecker), contextualSymbol => - find(typeChecker.getRootSymbols(contextualSymbol), isSearchedFor)); + find(typeChecker.getRootSymbols(contextualSymbol), symbol => contains(searchSymbols, symbol))); if (contextualSymbol) { return contextualSymbol; @@ -1208,7 +1205,7 @@ namespace ts.FindAllReferences { // In below eg. get 'property' from type of elems iterating type // for ( { property: p2 } of elems) { } const propertySymbol = getPropertySymbolOfDestructuringAssignment(referenceLocation, typeChecker); - if (propertySymbol && isSearchedFor(propertySymbol)) { + if (propertySymbol && contains(searchSymbols, propertySymbol)) { return propertySymbol; } } @@ -1217,7 +1214,7 @@ namespace ts.FindAllReferences { // then include the binding element in the related symbols // let { a } : { a }; const bindingElementPropertySymbol = getPropertySymbolOfObjectBindingPatternWithoutPropertyName(referenceSymbol, typeChecker); - if (bindingElementPropertySymbol && isSearchedFor(bindingElementPropertySymbol)) { + if (bindingElementPropertySymbol && contains(searchSymbols, bindingElementPropertySymbol)) { return bindingElementPropertySymbol; } @@ -1225,7 +1222,7 @@ namespace ts.FindAllReferences { // Or a union property, use its underlying unioned symbols return forEach(typeChecker.getRootSymbols(referenceSymbol), rootSymbol => { // if it is in the list, then we are done - if (isSearchedFor(rootSymbol)) { + if (contains(searchSymbols, rootSymbol)) { return rootSymbol; } @@ -1242,7 +1239,7 @@ namespace ts.FindAllReferences { const result: Symbol[] = []; getPropertySymbolsFromBaseTypes(rootSymbol.parent, rootSymbol.getName(), result, /*previousIterationSymbolsCache*/ createMap(), typeChecker); - return find(result, isSearchedFor); + return find(result, symbol => contains(searchSymbols, symbol)); } return undefined; From 71d1a3f051c0990c07a510dc811dfc4be4b3b6d7 Mon Sep 17 00:00:00 2001 From: Vladimir Matveev Date: Thu, 26 Jan 2017 11:38:50 -0800 Subject: [PATCH 14/31] property handle misspelled namepath in @typedef tag (#13702) --- src/compiler/parser.ts | 9 ++++++-- .../misspelledJsDocTypedefTags.symbols | 8 +++++++ .../misspelledJsDocTypedefTags.types | 22 +++++++++++++++++++ .../compiler/misspelledJsDocTypedefTags.ts | 9 ++++++++ 4 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 tests/baselines/reference/misspelledJsDocTypedefTags.symbols create mode 100644 tests/baselines/reference/misspelledJsDocTypedefTags.types create mode 100644 tests/cases/compiler/misspelledJsDocTypedefTags.ts diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index c795cab14a3..c5439aa051b 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -6693,10 +6693,15 @@ namespace ts { typedefTag.fullName = parseJSDocTypeNameWithNamespace(/*flags*/ 0); if (typedefTag.fullName) { let rightNode = typedefTag.fullName; - while (rightNode.kind !== SyntaxKind.Identifier) { + while (true) { + if (rightNode.kind === SyntaxKind.Identifier || !rightNode.body) { + // if node is identifier - use it as name + // otherwise use name of the rightmost part that we were able to parse + typedefTag.name = rightNode.kind === SyntaxKind.Identifier ? rightNode : rightNode.name; + break; + } rightNode = rightNode.body; } - typedefTag.name = rightNode; } typedefTag.typeExpression = typeExpression; skipWhitespace(); diff --git a/tests/baselines/reference/misspelledJsDocTypedefTags.symbols b/tests/baselines/reference/misspelledJsDocTypedefTags.symbols new file mode 100644 index 00000000000..bf3f6e7fab0 --- /dev/null +++ b/tests/baselines/reference/misspelledJsDocTypedefTags.symbols @@ -0,0 +1,8 @@ +=== tests/cases/compiler/a.js === + +No type information for this code./** @typedef {{ endTime: number, screenshots: number}} A.*/ +No type information for this code.Animation.AnimationModel.ScreenshotCapture.Request; +No type information for this code. +No type information for this code./** @typedef {{ endTime: number, screenshots: !B.}} */ +No type information for this code.Animation.AnimationModel.ScreenshotCapture.Request; +No type information for this code. \ No newline at end of file diff --git a/tests/baselines/reference/misspelledJsDocTypedefTags.types b/tests/baselines/reference/misspelledJsDocTypedefTags.types new file mode 100644 index 00000000000..70cc011ea69 --- /dev/null +++ b/tests/baselines/reference/misspelledJsDocTypedefTags.types @@ -0,0 +1,22 @@ +=== tests/cases/compiler/a.js === + +/** @typedef {{ endTime: number, screenshots: number}} A.*/ +Animation.AnimationModel.ScreenshotCapture.Request; +>Animation.AnimationModel.ScreenshotCapture.Request : any +>Animation.AnimationModel.ScreenshotCapture : any +>Animation.AnimationModel : any +>Animation : any +>AnimationModel : any +>ScreenshotCapture : any +>Request : any + +/** @typedef {{ endTime: number, screenshots: !B.}} */ +Animation.AnimationModel.ScreenshotCapture.Request; +>Animation.AnimationModel.ScreenshotCapture.Request : any +>Animation.AnimationModel.ScreenshotCapture : any +>Animation.AnimationModel : any +>Animation : any +>AnimationModel : any +>ScreenshotCapture : any +>Request : any + diff --git a/tests/cases/compiler/misspelledJsDocTypedefTags.ts b/tests/cases/compiler/misspelledJsDocTypedefTags.ts new file mode 100644 index 00000000000..3f3221020bd --- /dev/null +++ b/tests/cases/compiler/misspelledJsDocTypedefTags.ts @@ -0,0 +1,9 @@ +// @allowJs: true +// @noEmit: true + +// @filename: a.js +/** @typedef {{ endTime: number, screenshots: number}} A.*/ +Animation.AnimationModel.ScreenshotCapture.Request; + +/** @typedef {{ endTime: number, screenshots: !B.}} */ +Animation.AnimationModel.ScreenshotCapture.Request; \ No newline at end of file From cabcaaadcbd505f264dc396aad97cee427459171 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Thu, 26 Jan 2017 16:04:58 -0800 Subject: [PATCH 15/31] Property assignment is not an assignment target In a destructuring assignment, a property assignment is not an assignment target. Its initialiser is. For example: ```ts ({ source: target} = o); ``` Here, `target` is the assignment target. `source` is not. Previously, both were assignment targets. --- src/compiler/utilities.ts | 11 +++++--- ...ertyAssignmentNameIsNotAssignmentTarget.js | 17 +++++++++++++ ...ssignmentNameIsNotAssignmentTarget.symbols | 21 ++++++++++++++++ ...yAssignmentNameIsNotAssignmentTarget.types | 25 +++++++++++++++++++ ...ertyAssignmentNameIsNotAssignmentTarget.ts | 7 ++++++ 5 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 tests/baselines/reference/destructuringPropertyAssignmentNameIsNotAssignmentTarget.js create mode 100644 tests/baselines/reference/destructuringPropertyAssignmentNameIsNotAssignmentTarget.symbols create mode 100644 tests/baselines/reference/destructuringPropertyAssignmentNameIsNotAssignmentTarget.types create mode 100644 tests/cases/compiler/destructuringPropertyAssignmentNameIsNotAssignmentTarget.ts diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 5c63d8bb60c..17e9fe8e1d7 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -1662,11 +1662,15 @@ namespace ts { node = parent; break; case SyntaxKind.ShorthandPropertyAssignment: - if ((parent).name !== node) { + if ((parent as ShorthandPropertyAssignment).name !== node) { return AssignmentKind.None; } - // Fall through + node = parent.parent; + break; case SyntaxKind.PropertyAssignment: + if ((parent as ShorthandPropertyAssignment).name === node) { + return AssignmentKind.None; + } node = parent.parent; break; default: @@ -1678,7 +1682,8 @@ namespace ts { // A node is an assignment target if it is on the left hand side of an '=' token, if it is parented by a property // assignment in an object literal that is an assignment target, or if it is parented by an array literal that is - // an assignment target. Examples include 'a = xxx', '{ p: a } = xxx', '[{ p: a}] = xxx'. + // an assignment target. Examples include 'a = xxx', '{ p: a } = xxx', '[{ a }] = xxx'. + // (Note that `p` is not a target in the above examples, only `a`.) export function isAssignmentTarget(node: Node): boolean { return getAssignmentTargetKind(node) !== AssignmentKind.None; } diff --git a/tests/baselines/reference/destructuringPropertyAssignmentNameIsNotAssignmentTarget.js b/tests/baselines/reference/destructuringPropertyAssignmentNameIsNotAssignmentTarget.js new file mode 100644 index 00000000000..b2ba970ef51 --- /dev/null +++ b/tests/baselines/reference/destructuringPropertyAssignmentNameIsNotAssignmentTarget.js @@ -0,0 +1,17 @@ +//// [destructuringPropertyAssignmentNameIsNotAssignmentTarget.ts] +// test for #10668 +function qux(bar: { value: number }) { + let foo: number; + ({ value: foo } = bar); + let x = () => bar; +} + + + +//// [destructuringPropertyAssignmentNameIsNotAssignmentTarget.js] +// test for #10668 +function qux(bar) { + var foo; + (foo = bar.value); + var x = function () { return bar; }; +} diff --git a/tests/baselines/reference/destructuringPropertyAssignmentNameIsNotAssignmentTarget.symbols b/tests/baselines/reference/destructuringPropertyAssignmentNameIsNotAssignmentTarget.symbols new file mode 100644 index 00000000000..b9ebe219f65 --- /dev/null +++ b/tests/baselines/reference/destructuringPropertyAssignmentNameIsNotAssignmentTarget.symbols @@ -0,0 +1,21 @@ +=== tests/cases/compiler/destructuringPropertyAssignmentNameIsNotAssignmentTarget.ts === +// test for #10668 +function qux(bar: { value: number }) { +>qux : Symbol(qux, Decl(destructuringPropertyAssignmentNameIsNotAssignmentTarget.ts, 0, 0)) +>bar : Symbol(bar, Decl(destructuringPropertyAssignmentNameIsNotAssignmentTarget.ts, 1, 13)) +>value : Symbol(value, Decl(destructuringPropertyAssignmentNameIsNotAssignmentTarget.ts, 1, 19)) + + let foo: number; +>foo : Symbol(foo, Decl(destructuringPropertyAssignmentNameIsNotAssignmentTarget.ts, 2, 7)) + + ({ value: foo } = bar); +>value : Symbol(value, Decl(destructuringPropertyAssignmentNameIsNotAssignmentTarget.ts, 3, 6)) +>foo : Symbol(foo, Decl(destructuringPropertyAssignmentNameIsNotAssignmentTarget.ts, 2, 7)) +>bar : Symbol(bar, Decl(destructuringPropertyAssignmentNameIsNotAssignmentTarget.ts, 1, 13)) + + let x = () => bar; +>x : Symbol(x, Decl(destructuringPropertyAssignmentNameIsNotAssignmentTarget.ts, 4, 7)) +>bar : Symbol(bar, Decl(destructuringPropertyAssignmentNameIsNotAssignmentTarget.ts, 1, 13)) +} + + diff --git a/tests/baselines/reference/destructuringPropertyAssignmentNameIsNotAssignmentTarget.types b/tests/baselines/reference/destructuringPropertyAssignmentNameIsNotAssignmentTarget.types new file mode 100644 index 00000000000..e0428fc959f --- /dev/null +++ b/tests/baselines/reference/destructuringPropertyAssignmentNameIsNotAssignmentTarget.types @@ -0,0 +1,25 @@ +=== tests/cases/compiler/destructuringPropertyAssignmentNameIsNotAssignmentTarget.ts === +// test for #10668 +function qux(bar: { value: number }) { +>qux : (bar: { value: number; }) => void +>bar : { value: number; } +>value : number + + let foo: number; +>foo : number + + ({ value: foo } = bar); +>({ value: foo } = bar) : { value: number; } +>{ value: foo } = bar : { value: number; } +>{ value: foo } : { value: number; } +>value : number +>foo : number +>bar : { value: number; } + + let x = () => bar; +>x : () => { value: number; } +>() => bar : () => { value: number; } +>bar : { value: number; } +} + + diff --git a/tests/cases/compiler/destructuringPropertyAssignmentNameIsNotAssignmentTarget.ts b/tests/cases/compiler/destructuringPropertyAssignmentNameIsNotAssignmentTarget.ts new file mode 100644 index 00000000000..c0002f3e80a --- /dev/null +++ b/tests/cases/compiler/destructuringPropertyAssignmentNameIsNotAssignmentTarget.ts @@ -0,0 +1,7 @@ +// test for #10668 +function qux(bar: { value: number }) { + let foo: number; + ({ value: foo } = bar); + let x = () => bar; +} + From 4d67b0c2b6f4cb99b74db702eda6eb62c1cda7f4 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Fri, 27 Jan 2017 09:53:57 -0800 Subject: [PATCH 16/31] Allow super to access method signatures Previously, super was only allowed to access method *declarations*. But method signatures can come from interfaces that merge with classes, and should also be accessible as methods on super. --- src/compiler/checker.ts | 13 ++++--- .../superHasMethodsFromMergedInterface.js | 37 +++++++++++++++++++ ...superHasMethodsFromMergedInterface.symbols | 23 ++++++++++++ .../superHasMethodsFromMergedInterface.types | 24 ++++++++++++ .../superHasMethodsFromMergedInterface.ts | 7 ++++ 5 files changed, 99 insertions(+), 5 deletions(-) create mode 100644 tests/baselines/reference/superHasMethodsFromMergedInterface.js create mode 100644 tests/baselines/reference/superHasMethodsFromMergedInterface.symbols create mode 100644 tests/baselines/reference/superHasMethodsFromMergedInterface.types create mode 100644 tests/cases/compiler/superHasMethodsFromMergedInterface.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 7c8a0b7337f..7d0f2cb0bbb 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -12337,12 +12337,15 @@ namespace ts { // - In a static member function or static member accessor // where this references the constructor function object of a derived class, // a super property access is permitted and must specify a public static member function of the base class. - if (languageVersion < ScriptTarget.ES2015 && getDeclarationKindFromSymbol(prop) !== SyntaxKind.MethodDeclaration) { - // `prop` refers to a *property* declared in the super class - // rather than a *method*, so it does not satisfy the above criteria. + if (languageVersion < ScriptTarget.ES2015) { + const propKind = getDeclarationKindFromSymbol(prop); + if (propKind !== SyntaxKind.MethodDeclaration && propKind !== SyntaxKind.MethodSignature) { + // `prop` refers to a *property* declared in the super class + // rather than a *method*, so it does not satisfy the above criteria. - error(errorNode, Diagnostics.Only_public_and_protected_methods_of_the_base_class_are_accessible_via_the_super_keyword); - return false; + error(errorNode, Diagnostics.Only_public_and_protected_methods_of_the_base_class_are_accessible_via_the_super_keyword); + return false; + } } if (flags & ModifierFlags.Abstract) { diff --git a/tests/baselines/reference/superHasMethodsFromMergedInterface.js b/tests/baselines/reference/superHasMethodsFromMergedInterface.js new file mode 100644 index 00000000000..e7086106210 --- /dev/null +++ b/tests/baselines/reference/superHasMethodsFromMergedInterface.js @@ -0,0 +1,37 @@ +//// [superHasMethodsFromMergedInterface.ts] +class C { m1() { } } +interface C { m2(): void } +class Sub extends C { + m3() { + super.m2(); + } +} + + +//// [superHasMethodsFromMergedInterface.js] +var __extends = (this && this.__extends) || (function () { + var extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; + return function (d, b) { + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + }; +})(); +var C = (function () { + function C() { + } + C.prototype.m1 = function () { }; + return C; +}()); +var Sub = (function (_super) { + __extends(Sub, _super); + function Sub() { + return _super !== null && _super.apply(this, arguments) || this; + } + Sub.prototype.m3 = function () { + _super.prototype.m2.call(this); + }; + return Sub; +}(C)); diff --git a/tests/baselines/reference/superHasMethodsFromMergedInterface.symbols b/tests/baselines/reference/superHasMethodsFromMergedInterface.symbols new file mode 100644 index 00000000000..ec0af8064ed --- /dev/null +++ b/tests/baselines/reference/superHasMethodsFromMergedInterface.symbols @@ -0,0 +1,23 @@ +=== tests/cases/compiler/superHasMethodsFromMergedInterface.ts === +class C { m1() { } } +>C : Symbol(C, Decl(superHasMethodsFromMergedInterface.ts, 0, 0), Decl(superHasMethodsFromMergedInterface.ts, 0, 20)) +>m1 : Symbol(C.m1, Decl(superHasMethodsFromMergedInterface.ts, 0, 9)) + +interface C { m2(): void } +>C : Symbol(C, Decl(superHasMethodsFromMergedInterface.ts, 0, 0), Decl(superHasMethodsFromMergedInterface.ts, 0, 20)) +>m2 : Symbol(C.m2, Decl(superHasMethodsFromMergedInterface.ts, 1, 13)) + +class Sub extends C { +>Sub : Symbol(Sub, Decl(superHasMethodsFromMergedInterface.ts, 1, 26)) +>C : Symbol(C, Decl(superHasMethodsFromMergedInterface.ts, 0, 0), Decl(superHasMethodsFromMergedInterface.ts, 0, 20)) + + m3() { +>m3 : Symbol(Sub.m3, Decl(superHasMethodsFromMergedInterface.ts, 2, 21)) + + super.m2(); +>super.m2 : Symbol(C.m2, Decl(superHasMethodsFromMergedInterface.ts, 1, 13)) +>super : Symbol(C, Decl(superHasMethodsFromMergedInterface.ts, 0, 0), Decl(superHasMethodsFromMergedInterface.ts, 0, 20)) +>m2 : Symbol(C.m2, Decl(superHasMethodsFromMergedInterface.ts, 1, 13)) + } +} + diff --git a/tests/baselines/reference/superHasMethodsFromMergedInterface.types b/tests/baselines/reference/superHasMethodsFromMergedInterface.types new file mode 100644 index 00000000000..7f314d90bed --- /dev/null +++ b/tests/baselines/reference/superHasMethodsFromMergedInterface.types @@ -0,0 +1,24 @@ +=== tests/cases/compiler/superHasMethodsFromMergedInterface.ts === +class C { m1() { } } +>C : C +>m1 : () => void + +interface C { m2(): void } +>C : C +>m2 : () => void + +class Sub extends C { +>Sub : Sub +>C : C + + m3() { +>m3 : () => void + + super.m2(); +>super.m2() : void +>super.m2 : () => void +>super : C +>m2 : () => void + } +} + diff --git a/tests/cases/compiler/superHasMethodsFromMergedInterface.ts b/tests/cases/compiler/superHasMethodsFromMergedInterface.ts new file mode 100644 index 00000000000..cdca6ae65bf --- /dev/null +++ b/tests/cases/compiler/superHasMethodsFromMergedInterface.ts @@ -0,0 +1,7 @@ +class C { m1() { } } +interface C { m2(): void } +class Sub extends C { + m3() { + super.m2(); + } +} From 89b72ac9cec52d3a663f17daaabebc219c207644 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Sat, 28 Jan 2017 14:46:29 -0800 Subject: [PATCH 17/31] Support mixin classes --- src/compiler/checker.ts | 86 +++++++++++++++++++++++----- src/compiler/diagnosticMessages.json | 4 ++ 2 files changed, 76 insertions(+), 14 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 7c8a0b7337f..6e0f0d6509a 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -2465,7 +2465,8 @@ namespace ts { const symbol = type.symbol; if (symbol) { // Always use 'typeof T' for type of class, enum, and module objects - if (symbol.flags & (SymbolFlags.Class | SymbolFlags.Enum | SymbolFlags.ValueModule)) { + if (symbol.flags & SymbolFlags.Class && !getBaseTypeVariableOfClass(symbol) || + symbol.flags & (SymbolFlags.Enum | SymbolFlags.ValueModule)) { writeTypeOfSymbol(type, flags); } else if (shouldWriteTypeOfFunctionSymbol()) { @@ -3639,6 +3640,11 @@ namespace ts { return links.type; } + function getBaseTypeVariableOfClass(symbol: Symbol) { + const baseConstructorType = getBaseConstructorTypeOfClass(getDeclaredTypeOfClassOrInterface(symbol)); + return baseConstructorType.flags & TypeFlags.TypeVariable ? baseConstructorType : undefined; + } + function getTypeOfFuncClassEnumModule(symbol: Symbol): Type { const links = getSymbolLinks(symbol); if (!links.type) { @@ -3647,8 +3653,13 @@ namespace ts { } else { const type = createObjectType(ObjectFlags.Anonymous, symbol); - links.type = strictNullChecks && symbol.flags & SymbolFlags.Optional ? - includeFalsyTypes(type, TypeFlags.Undefined) : type; + if (symbol.flags & SymbolFlags.Class) { + const baseTypeVariable = getBaseTypeVariableOfClass(symbol); + links.type = baseTypeVariable ? getIntersectionType([type, baseTypeVariable]) : type; + } + else { + links.type = strictNullChecks && symbol.flags & SymbolFlags.Optional ? includeFalsyTypes(type, TypeFlags.Undefined) : type; + } } } return links.type; @@ -3812,8 +3823,26 @@ namespace ts { return concatenate(getOuterTypeParametersOfClassOrInterface(symbol), getLocalTypeParametersOfClassOrInterfaceOrTypeAlias(symbol)); } + // A type is a mixin constructor if it has a single construct signature taking no type parameters and a single + // rest parameter of type any[]. + function isMixinConstructorType(type: Type) { + const signatures = getSignaturesOfType(type, SignatureKind.Construct); + if (signatures.length === 1) { + const s = signatures[0]; + return !s.typeParameters && s.parameters.length === 1 && s.hasRestParameter && getTypeOfParameter(s.parameters[0]) === anyArrayType; + } + return false; + } + function isConstructorType(type: Type): boolean { - return isValidBaseType(type) && getSignaturesOfType(type, SignatureKind.Construct).length > 0; + if (isValidBaseType(type) && getSignaturesOfType(type, SignatureKind.Construct).length > 0) { + return true; + } + if (type.flags & TypeFlags.TypeVariable) { + const constraint = getBaseConstraintOfType(type); + return isValidBaseType(constraint) && isMixinConstructorType(constraint); + } + return false; } function getBaseTypeNodeOfClass(type: InterfaceType): ExpressionWithTypeArguments { @@ -3892,7 +3921,7 @@ namespace ts { function resolveBaseTypesOfClass(type: InterfaceType): void { type.resolvedBaseTypes = type.resolvedBaseTypes || emptyArray; - const baseConstructorType = getBaseConstructorTypeOfClass(type); + const baseConstructorType = getApparentType(getBaseConstructorTypeOfClass(type)); if (!(baseConstructorType.flags & (TypeFlags.Object | TypeFlags.Intersection))) { return; } @@ -4547,11 +4576,32 @@ namespace ts { // intersection type use getPropertiesOfType (only the language service uses this). let callSignatures: Signature[] = emptyArray; let constructSignatures: Signature[] = emptyArray; - let stringIndexInfo: IndexInfo = undefined; - let numberIndexInfo: IndexInfo = undefined; - for (const t of type.types) { + let stringIndexInfo: IndexInfo; + let numberIndexInfo: IndexInfo; + let mixinTypes: Type[]; + const count = type.types.length; + for (let i = 0; i < count; i++) { + const t = type.types[i]; + // When a type T is preceded by a mixin constructor type, the return type of each construct signature + // in T is intersected with the return type of the mixin constructor, and the mixin construct signature + // is removed. For example, the intersection '{ new(...args: any[]) => A } & { new(s: string) => B }' + // has a single construct signature 'new(s: string) => A & B'. + let signatures = getSignaturesOfType(t, SignatureKind.Construct); + if (i < count - 1 && isMixinConstructorType(t)) { + (mixinTypes || (mixinTypes = [])).push(getReturnTypeOfSignature(signatures[0])); + } + else { + if (mixinTypes) { + signatures = map(signatures, s => { + const clone = cloneSignature(s); + clone.resolvedReturnType = getIntersectionType([...mixinTypes, getReturnTypeOfSignature(s)]); + return clone; + }); + mixinTypes = undefined; + } + constructSignatures = concatenate(constructSignatures, signatures); + } callSignatures = concatenate(callSignatures, getSignaturesOfType(t, SignatureKind.Call)); - constructSignatures = concatenate(constructSignatures, getSignaturesOfType(t, SignatureKind.Construct)); stringIndexInfo = intersectIndexInfos(stringIndexInfo, getIndexInfoOfType(t, IndexKind.String)); numberIndexInfo = intersectIndexInfos(numberIndexInfo, getIndexInfoOfType(t, IndexKind.Number)); } @@ -4593,7 +4643,7 @@ namespace ts { constructSignatures = getDefaultConstructSignatures(classType); } const baseConstructorType = getBaseConstructorTypeOfClass(classType); - if (baseConstructorType.flags & (TypeFlags.Object | TypeFlags.Intersection)) { + if (baseConstructorType.flags & (TypeFlags.Object | TypeFlags.Intersection | TypeFlags.TypeVariable)) { members = createSymbolTable(getNamedMembers(members)); addInheritedMembers(members, getPropertiesOfType(baseConstructorType)); } @@ -4890,6 +4940,7 @@ namespace ts { function createUnionOrIntersectionProperty(containingType: UnionOrIntersectionType, name: string): Symbol { const types = containingType.types; + const excludeModifiers = containingType.flags & TypeFlags.Union ? ModifierFlags.Private | ModifierFlags.Protected : 0; let props: Symbol[]; // Flags we want to propagate to the result if they exist in all source symbols let commonFlags = (containingType.flags & TypeFlags.Intersection) ? SymbolFlags.Optional : SymbolFlags.None; @@ -4899,7 +4950,7 @@ namespace ts { const type = getApparentType(current); if (type !== unknownType) { const prop = getPropertyOfType(type, name); - if (prop && !(getDeclarationModifierFlagsFromSymbol(prop) & (ModifierFlags.Private | ModifierFlags.Protected))) { + if (prop && !(getDeclarationModifierFlagsFromSymbol(prop) & excludeModifiers)) { commonFlags &= prop.flags; if (!props) { props = [prop]; @@ -6965,9 +7016,12 @@ namespace ts { result.properties = resolved.properties; result.callSignatures = emptyArray; result.constructSignatures = emptyArray; - type = result; + return result; } } + else if (type.flags & TypeFlags.Intersection) { + return getIntersectionType(map((type).types, getTypeWithoutSignatures)); + } return type; } @@ -18387,7 +18441,8 @@ namespace ts { const baseTypes = getBaseTypes(type); if (baseTypes.length && produceDiagnostics) { const baseType = baseTypes[0]; - const staticBaseType = getBaseConstructorTypeOfClass(type); + const baseConstructorType = getBaseConstructorTypeOfClass(type); + const staticBaseType = getApparentType(baseConstructorType); checkBaseTypeAccessibility(staticBaseType, baseTypeNode); checkSourceElement(baseTypeNode.expression); if (baseTypeNode.typeArguments) { @@ -18401,6 +18456,9 @@ namespace ts { checkTypeAssignableTo(typeWithThis, getTypeWithThisArgument(baseType, type.thisType), node.name || node, Diagnostics.Class_0_incorrectly_extends_base_class_1); checkTypeAssignableTo(staticType, getTypeWithoutSignatures(staticBaseType), node.name || node, Diagnostics.Class_static_side_0_incorrectly_extends_base_class_static_side_1); + if (baseConstructorType.flags & TypeFlags.TypeVariable && !isMixinConstructorType(staticType)) { + error(node.name || node, Diagnostics.A_mixin_class_must_have_a_constructor_with_a_single_rest_parameter_of_type_any); + } if (baseType.symbol && baseType.symbol.valueDeclaration && !isInAmbientContext(baseType.symbol.valueDeclaration) && @@ -18410,7 +18468,7 @@ namespace ts { } } - if (!(staticBaseType.symbol && staticBaseType.symbol.flags & SymbolFlags.Class)) { + if (!(staticBaseType.symbol && staticBaseType.symbol.flags & SymbolFlags.Class) && !(baseConstructorType.flags & TypeFlags.TypeVariable)) { // When the static base type is a "class-like" constructor function (but not actually a class), we verify // that all instantiated base constructor signatures return the same type. We can simply compare the type // references (as opposed to checking the structure of the types) because elsewhere we have already checked diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 43a9cce4de6..3fa399d3d38 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -1783,6 +1783,10 @@ "category": "Error", "code": 2544 }, + "A mixin class must have a constructor with a single rest parameter of type 'any[]'.": { + "category": "Error", + "code": 2545 + }, "JSX element attributes type '{0}' may not be a union type.": { "category": "Error", "code": 2600 From 763df852c610d9bc1e098fb7529474e7a7e52f92 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Sun, 29 Jan 2017 08:14:28 -0800 Subject: [PATCH 18/31] Revise intersection construct signature mixin algorithm --- src/compiler/checker.ts | 40 +++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 6e0f0d6509a..dff8953e913 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -4571,6 +4571,19 @@ namespace ts { getUnionType([info1.type, info2.type]), info1.isReadonly || info2.isReadonly); } + function includeMixinType(type: Type, types: Type[], index: number): Type { + const mixedTypes: Type[] = []; + for (let i = 0; i < types.length; i++) { + if (i === index) { + mixedTypes.push(type); + } + else if (isMixinConstructorType(types[i])) { + mixedTypes.push(getReturnTypeOfSignature(getSignaturesOfType(types[i], SignatureKind.Construct)[0])); + } + } + return getIntersectionType(mixedTypes); + } + function resolveIntersectionTypeMembers(type: IntersectionType) { // The members and properties collections are empty for intersection types. To get all properties of an // intersection type use getPropertiesOfType (only the language service uses this). @@ -4578,26 +4591,23 @@ namespace ts { let constructSignatures: Signature[] = emptyArray; let stringIndexInfo: IndexInfo; let numberIndexInfo: IndexInfo; - let mixinTypes: Type[]; - const count = type.types.length; - for (let i = 0; i < count; i++) { + const types = type.types; + const mixinCount = countWhere(types, isMixinConstructorType); + for (let i = 0; i < types.length; i++) { const t = type.types[i]; - // When a type T is preceded by a mixin constructor type, the return type of each construct signature - // in T is intersected with the return type of the mixin constructor, and the mixin construct signature - // is removed. For example, the intersection '{ new(...args: any[]) => A } & { new(s: string) => B }' - // has a single construct signature 'new(s: string) => A & B'. - let signatures = getSignaturesOfType(t, SignatureKind.Construct); - if (i < count - 1 && isMixinConstructorType(t)) { - (mixinTypes || (mixinTypes = [])).push(getReturnTypeOfSignature(signatures[0])); - } - else { - if (mixinTypes) { + // When an intersection type contains mixin constructor types, the construct signatures from + // those types are discarded and their return types are mixed into the return types of all + // other construct signatures in the intersection type. For example, the intersection type + // '{ new(...args: any[]) => A } & { new(s: string) => B }' has a single construct signature + // 'new(s: string) => A & B'. + if (mixinCount === 0 || mixinCount === types.length && i === 0 || !isMixinConstructorType(t)) { + let signatures = getSignaturesOfType(t, SignatureKind.Construct); + if (signatures.length && mixinCount > 0) { signatures = map(signatures, s => { const clone = cloneSignature(s); - clone.resolvedReturnType = getIntersectionType([...mixinTypes, getReturnTypeOfSignature(s)]); + clone.resolvedReturnType = includeMixinType(getReturnTypeOfSignature(s), types, i); return clone; }); - mixinTypes = undefined; } constructSignatures = concatenate(constructSignatures, signatures); } From 56b1dcd8eaa3ce996867e0f586c768f6fa422cae Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Sun, 29 Jan 2017 12:39:15 -0800 Subject: [PATCH 19/31] Add tests --- .../classes/mixinClassesAnnotated.ts | 67 +++++++++++++ .../classes/mixinClassesAnonymous.ts | 56 +++++++++++ .../classes/mixinClassesMembers.ts | 99 +++++++++++++++++++ 3 files changed, 222 insertions(+) create mode 100644 tests/cases/conformance/classes/mixinClassesAnnotated.ts create mode 100644 tests/cases/conformance/classes/mixinClassesAnonymous.ts create mode 100644 tests/cases/conformance/classes/mixinClassesMembers.ts diff --git a/tests/cases/conformance/classes/mixinClassesAnnotated.ts b/tests/cases/conformance/classes/mixinClassesAnnotated.ts new file mode 100644 index 00000000000..62f5e30c9a0 --- /dev/null +++ b/tests/cases/conformance/classes/mixinClassesAnnotated.ts @@ -0,0 +1,67 @@ +// @declaration: true + +type Constructor = new(...args: any[]) => T; + +class Base { + constructor(public x: number, public y: number) {} +} + +class Derived extends Base { + constructor(x: number, y: number, public z: number) { + super(x, y); + } +} + +interface Printable { + print(): void; +} + +const Printable = >(superClass: T): Constructor & { message: string } & T => + class extends superClass { + static message = "hello"; + print() { + const output = this.x + "," + this.y; + } + } + +interface Tagged { + _tag: string; +} + +function Tagged>(superClass: T): Constructor & T { + class C extends superClass { + _tag: string; + constructor(...args: any[]) { + super(...args); + this._tag = "hello"; + } + } + return C; +} + +const Thing1 = Tagged(Derived); +const Thing2 = Tagged(Printable(Derived)); +Thing2.message; + +function f1() { + const thing = new Thing1(1, 2, 3); + thing.x; + thing._tag; +} + +function f2() { + const thing = new Thing2(1, 2, 3); + thing.x; + thing._tag; + thing.print(); +} + +class Thing3 extends Thing2 { + constructor(tag: string) { + super(10, 20, 30); + this._tag = tag; + } + test() { + this.print(); + } +} diff --git a/tests/cases/conformance/classes/mixinClassesAnonymous.ts b/tests/cases/conformance/classes/mixinClassesAnonymous.ts new file mode 100644 index 00000000000..24289b61e3e --- /dev/null +++ b/tests/cases/conformance/classes/mixinClassesAnonymous.ts @@ -0,0 +1,56 @@ +type Constructor = new(...args: any[]) => T; + +class Base { + constructor(public x: number, public y: number) {} +} + +class Derived extends Base { + constructor(x: number, y: number, public z: number) { + super(x, y); + } +} + +const Printable = >(superClass: T) => class extends superClass { + static message = "hello"; + print() { + const output = this.x + "," + this.y; + } +} + +function Tagged>(superClass: T) { + class C extends superClass { + _tag: string; + constructor(...args: any[]) { + super(...args); + this._tag = "hello"; + } + } + return C; +} + +const Thing1 = Tagged(Derived); +const Thing2 = Tagged(Printable(Derived)); +Thing2.message; + +function f1() { + const thing = new Thing1(1, 2, 3); + thing.x; + thing._tag; +} + +function f2() { + const thing = new Thing2(1, 2, 3); + thing.x; + thing._tag; + thing.print(); +} + +class Thing3 extends Thing2 { + constructor(tag: string) { + super(10, 20, 30); + this._tag = tag; + } + test() { + this.print(); + } +} diff --git a/tests/cases/conformance/classes/mixinClassesMembers.ts b/tests/cases/conformance/classes/mixinClassesMembers.ts new file mode 100644 index 00000000000..905518c88f8 --- /dev/null +++ b/tests/cases/conformance/classes/mixinClassesMembers.ts @@ -0,0 +1,99 @@ +// @declaration: true + +declare class C1 { + public a: number; + protected b: number; + private c: number; + constructor(s: string); + constructor(n: number); +} + +declare class M1 { + constructor(...args: any[]); + p: number; + static p: number; +} + +declare class M2 { + constructor(...args: any[]); + f(): number; + static f(): number; +} + +declare const Mixed1: typeof M1 & typeof C1; +declare const Mixed2: typeof C1 & typeof M1; +declare const Mixed3: typeof M2 & typeof M1 & typeof C1; +declare const Mixed4: typeof C1 & typeof M1 & typeof M2; +declare const Mixed5: typeof M1 & typeof M2; + +function f1() { + let x1 = new Mixed1("hello"); + let x2 = new Mixed1(42); + let x3 = new Mixed2("hello"); + let x4 = new Mixed2(42); + let x5 = new Mixed3("hello"); + let x6 = new Mixed3(42); + let x7 = new Mixed4("hello"); + let x8 = new Mixed4(42); + let x9 = new Mixed5(); +} + +function f2() { + let x = new Mixed1("hello"); + x.a; + x.p; + Mixed1.p; +} + +function f3() { + let x = new Mixed2("hello"); + x.a; + x.p; + Mixed2.p; +} + +function f4() { + let x = new Mixed3("hello"); + x.a; + x.p; + x.f(); + Mixed3.p; + Mixed3.f(); +} + +function f5() { + let x = new Mixed4("hello"); + x.a; + x.p; + x.f(); + Mixed4.p; + Mixed4.f(); +} + +function f6() { + let x = new Mixed5(); + x.p; + x.f(); + Mixed5.p; + Mixed5.f(); +} + +class C2 extends Mixed1 { + constructor() { + super("hello"); + this.a; + this.b; + this.p; + } +} + +class C3 extends Mixed3 { + constructor() { + super(42); + this.a; + this.b; + this.p; + this.f(); + } + f() { return super.f(); } +} From 004dbf458c3f577140f2c4fbcab06f55ef9250d3 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Sun, 29 Jan 2017 12:39:33 -0800 Subject: [PATCH 20/31] Accept new baselines --- .../reference/mixinClassesAnnotated.js | 183 +++++++++ .../reference/mixinClassesAnnotated.symbols | 193 ++++++++++ .../reference/mixinClassesAnnotated.types | 224 +++++++++++ .../reference/mixinClassesAnonymous.js | 140 +++++++ .../reference/mixinClassesAnonymous.symbols | 169 +++++++++ .../reference/mixinClassesAnonymous.types | 200 ++++++++++ .../reference/mixinClassesMembers.js | 220 +++++++++++ .../reference/mixinClassesMembers.symbols | 309 +++++++++++++++ .../reference/mixinClassesMembers.types | 352 ++++++++++++++++++ 9 files changed, 1990 insertions(+) create mode 100644 tests/baselines/reference/mixinClassesAnnotated.js create mode 100644 tests/baselines/reference/mixinClassesAnnotated.symbols create mode 100644 tests/baselines/reference/mixinClassesAnnotated.types create mode 100644 tests/baselines/reference/mixinClassesAnonymous.js create mode 100644 tests/baselines/reference/mixinClassesAnonymous.symbols create mode 100644 tests/baselines/reference/mixinClassesAnonymous.types create mode 100644 tests/baselines/reference/mixinClassesMembers.js create mode 100644 tests/baselines/reference/mixinClassesMembers.symbols create mode 100644 tests/baselines/reference/mixinClassesMembers.types diff --git a/tests/baselines/reference/mixinClassesAnnotated.js b/tests/baselines/reference/mixinClassesAnnotated.js new file mode 100644 index 00000000000..e3a781033ff --- /dev/null +++ b/tests/baselines/reference/mixinClassesAnnotated.js @@ -0,0 +1,183 @@ +//// [mixinClassesAnnotated.ts] + +type Constructor = new(...args: any[]) => T; + +class Base { + constructor(public x: number, public y: number) {} +} + +class Derived extends Base { + constructor(x: number, y: number, public z: number) { + super(x, y); + } +} + +interface Printable { + print(): void; +} + +const Printable = >(superClass: T): Constructor & { message: string } & T => + class extends superClass { + static message = "hello"; + print() { + const output = this.x + "," + this.y; + } + } + +interface Tagged { + _tag: string; +} + +function Tagged>(superClass: T): Constructor & T { + class C extends superClass { + _tag: string; + constructor(...args: any[]) { + super(...args); + this._tag = "hello"; + } + } + return C; +} + +const Thing1 = Tagged(Derived); +const Thing2 = Tagged(Printable(Derived)); +Thing2.message; + +function f1() { + const thing = new Thing1(1, 2, 3); + thing.x; + thing._tag; +} + +function f2() { + const thing = new Thing2(1, 2, 3); + thing.x; + thing._tag; + thing.print(); +} + +class Thing3 extends Thing2 { + constructor(tag: string) { + super(10, 20, 30); + this._tag = tag; + } + test() { + this.print(); + } +} + + +//// [mixinClassesAnnotated.js] +var __extends = (this && this.__extends) || (function () { + var extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; + return function (d, b) { + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + }; +})(); +var Base = (function () { + function Base(x, y) { + this.x = x; + this.y = y; + } + return Base; +}()); +var Derived = (function (_super) { + __extends(Derived, _super); + function Derived(x, y, z) { + var _this = _super.call(this, x, y) || this; + _this.z = z; + return _this; + } + return Derived; +}(Base)); +var Printable = function (superClass) { return _a = (function (_super) { + __extends(class_1, _super); + function class_1() { + return _super !== null && _super.apply(this, arguments) || this; + } + class_1.prototype.print = function () { + var output = this.x + "," + this.y; + }; + return class_1; + }(superClass)), + _a.message = "hello", + _a; var _a; }; +function Tagged(superClass) { + var C = (function (_super) { + __extends(C, _super); + function C() { + var args = []; + for (var _i = 0; _i < arguments.length; _i++) { + args[_i] = arguments[_i]; + } + var _this = _super.apply(this, args) || this; + _this._tag = "hello"; + return _this; + } + return C; + }(superClass)); + return C; +} +var Thing1 = Tagged(Derived); +var Thing2 = Tagged(Printable(Derived)); +Thing2.message; +function f1() { + var thing = new Thing1(1, 2, 3); + thing.x; + thing._tag; +} +function f2() { + var thing = new Thing2(1, 2, 3); + thing.x; + thing._tag; + thing.print(); +} +var Thing3 = (function (_super) { + __extends(Thing3, _super); + function Thing3(tag) { + var _this = _super.call(this, 10, 20, 30) || this; + _this._tag = tag; + return _this; + } + Thing3.prototype.test = function () { + this.print(); + }; + return Thing3; +}(Thing2)); + + +//// [mixinClassesAnnotated.d.ts] +declare type Constructor = new (...args: any[]) => T; +declare class Base { + x: number; + y: number; + constructor(x: number, y: number); +} +declare class Derived extends Base { + z: number; + constructor(x: number, y: number, z: number); +} +interface Printable { + print(): void; +} +declare const Printable: >(superClass: T) => Constructor & { + message: string; +} & T; +interface Tagged { + _tag: string; +} +declare function Tagged>(superClass: T): Constructor & T; +declare const Thing1: Constructor & typeof Derived; +declare const Thing2: Constructor & Constructor & { + message: string; +} & typeof Derived; +declare function f1(): void; +declare function f2(): void; +declare class Thing3 extends Thing2 { + constructor(tag: string); + test(): void; +} diff --git a/tests/baselines/reference/mixinClassesAnnotated.symbols b/tests/baselines/reference/mixinClassesAnnotated.symbols new file mode 100644 index 00000000000..5a03789e2e6 --- /dev/null +++ b/tests/baselines/reference/mixinClassesAnnotated.symbols @@ -0,0 +1,193 @@ +=== tests/cases/conformance/classes/mixinClassesAnnotated.ts === + +type Constructor = new(...args: any[]) => T; +>Constructor : Symbol(Constructor, Decl(mixinClassesAnnotated.ts, 0, 0)) +>T : Symbol(T, Decl(mixinClassesAnnotated.ts, 1, 17)) +>args : Symbol(args, Decl(mixinClassesAnnotated.ts, 1, 26)) +>T : Symbol(T, Decl(mixinClassesAnnotated.ts, 1, 17)) + +class Base { +>Base : Symbol(Base, Decl(mixinClassesAnnotated.ts, 1, 47)) + + constructor(public x: number, public y: number) {} +>x : Symbol(Base.x, Decl(mixinClassesAnnotated.ts, 4, 16)) +>y : Symbol(Base.y, Decl(mixinClassesAnnotated.ts, 4, 33)) +} + +class Derived extends Base { +>Derived : Symbol(Derived, Decl(mixinClassesAnnotated.ts, 5, 1)) +>Base : Symbol(Base, Decl(mixinClassesAnnotated.ts, 1, 47)) + + constructor(x: number, y: number, public z: number) { +>x : Symbol(x, Decl(mixinClassesAnnotated.ts, 8, 16)) +>y : Symbol(y, Decl(mixinClassesAnnotated.ts, 8, 26)) +>z : Symbol(Derived.z, Decl(mixinClassesAnnotated.ts, 8, 37)) + + super(x, y); +>super : Symbol(Base, Decl(mixinClassesAnnotated.ts, 1, 47)) +>x : Symbol(x, Decl(mixinClassesAnnotated.ts, 8, 16)) +>y : Symbol(y, Decl(mixinClassesAnnotated.ts, 8, 26)) + } +} + +interface Printable { +>Printable : Symbol(Printable, Decl(mixinClassesAnnotated.ts, 11, 1), Decl(mixinClassesAnnotated.ts, 17, 5)) + + print(): void; +>print : Symbol(Printable.print, Decl(mixinClassesAnnotated.ts, 13, 21)) +} + +const Printable = >(superClass: T): Constructor & { message: string } & T => +>Printable : Symbol(Printable, Decl(mixinClassesAnnotated.ts, 11, 1), Decl(mixinClassesAnnotated.ts, 17, 5)) +>T : Symbol(T, Decl(mixinClassesAnnotated.ts, 17, 19)) +>Constructor : Symbol(Constructor, Decl(mixinClassesAnnotated.ts, 0, 0)) +>Base : Symbol(Base, Decl(mixinClassesAnnotated.ts, 1, 47)) +>superClass : Symbol(superClass, Decl(mixinClassesAnnotated.ts, 17, 48)) +>T : Symbol(T, Decl(mixinClassesAnnotated.ts, 17, 19)) +>Constructor : Symbol(Constructor, Decl(mixinClassesAnnotated.ts, 0, 0)) +>Printable : Symbol(Printable, Decl(mixinClassesAnnotated.ts, 11, 1), Decl(mixinClassesAnnotated.ts, 17, 5)) +>message : Symbol(message, Decl(mixinClassesAnnotated.ts, 17, 90)) +>T : Symbol(T, Decl(mixinClassesAnnotated.ts, 17, 19)) + + class extends superClass { +>superClass : Symbol(superClass, Decl(mixinClassesAnnotated.ts, 17, 48)) + + static message = "hello"; +>message : Symbol((Anonymous class).message, Decl(mixinClassesAnnotated.ts, 18, 30)) + + print() { +>print : Symbol((Anonymous class).print, Decl(mixinClassesAnnotated.ts, 19, 33)) + + const output = this.x + "," + this.y; +>output : Symbol(output, Decl(mixinClassesAnnotated.ts, 21, 17)) +>this.x : Symbol(Base.x, Decl(mixinClassesAnnotated.ts, 4, 16)) +>this : Symbol((Anonymous class), Decl(mixinClassesAnnotated.ts, 17, 115)) +>x : Symbol(Base.x, Decl(mixinClassesAnnotated.ts, 4, 16)) +>this.y : Symbol(Base.y, Decl(mixinClassesAnnotated.ts, 4, 33)) +>this : Symbol((Anonymous class), Decl(mixinClassesAnnotated.ts, 17, 115)) +>y : Symbol(Base.y, Decl(mixinClassesAnnotated.ts, 4, 33)) + } + } + +interface Tagged { +>Tagged : Symbol(Tagged, Decl(mixinClassesAnnotated.ts, 23, 5), Decl(mixinClassesAnnotated.ts, 27, 1)) + + _tag: string; +>_tag : Symbol(Tagged._tag, Decl(mixinClassesAnnotated.ts, 25, 18)) +} + +function Tagged>(superClass: T): Constructor & T { +>Tagged : Symbol(Tagged, Decl(mixinClassesAnnotated.ts, 23, 5), Decl(mixinClassesAnnotated.ts, 27, 1)) +>T : Symbol(T, Decl(mixinClassesAnnotated.ts, 29, 16)) +>Constructor : Symbol(Constructor, Decl(mixinClassesAnnotated.ts, 0, 0)) +>superClass : Symbol(superClass, Decl(mixinClassesAnnotated.ts, 29, 43)) +>T : Symbol(T, Decl(mixinClassesAnnotated.ts, 29, 16)) +>Constructor : Symbol(Constructor, Decl(mixinClassesAnnotated.ts, 0, 0)) +>Tagged : Symbol(Tagged, Decl(mixinClassesAnnotated.ts, 23, 5), Decl(mixinClassesAnnotated.ts, 27, 1)) +>T : Symbol(T, Decl(mixinClassesAnnotated.ts, 29, 16)) + + class C extends superClass { +>C : Symbol(C, Decl(mixinClassesAnnotated.ts, 29, 84)) +>superClass : Symbol(superClass, Decl(mixinClassesAnnotated.ts, 29, 43)) + + _tag: string; +>_tag : Symbol(C._tag, Decl(mixinClassesAnnotated.ts, 30, 32)) + + constructor(...args: any[]) { +>args : Symbol(args, Decl(mixinClassesAnnotated.ts, 32, 20)) + + super(...args); +>super : Symbol(T, Decl(mixinClassesAnnotated.ts, 29, 16)) +>args : Symbol(args, Decl(mixinClassesAnnotated.ts, 32, 20)) + + this._tag = "hello"; +>this._tag : Symbol(C._tag, Decl(mixinClassesAnnotated.ts, 30, 32)) +>this : Symbol(C, Decl(mixinClassesAnnotated.ts, 29, 84)) +>_tag : Symbol(C._tag, Decl(mixinClassesAnnotated.ts, 30, 32)) + } + } + return C; +>C : Symbol(C, Decl(mixinClassesAnnotated.ts, 29, 84)) +} + +const Thing1 = Tagged(Derived); +>Thing1 : Symbol(Thing1, Decl(mixinClassesAnnotated.ts, 40, 5)) +>Tagged : Symbol(Tagged, Decl(mixinClassesAnnotated.ts, 23, 5), Decl(mixinClassesAnnotated.ts, 27, 1)) +>Derived : Symbol(Derived, Decl(mixinClassesAnnotated.ts, 5, 1)) + +const Thing2 = Tagged(Printable(Derived)); +>Thing2 : Symbol(Thing2, Decl(mixinClassesAnnotated.ts, 41, 5)) +>Tagged : Symbol(Tagged, Decl(mixinClassesAnnotated.ts, 23, 5), Decl(mixinClassesAnnotated.ts, 27, 1)) +>Printable : Symbol(Printable, Decl(mixinClassesAnnotated.ts, 11, 1), Decl(mixinClassesAnnotated.ts, 17, 5)) +>Derived : Symbol(Derived, Decl(mixinClassesAnnotated.ts, 5, 1)) + +Thing2.message; +>Thing2.message : Symbol(message, Decl(mixinClassesAnnotated.ts, 17, 90)) +>Thing2 : Symbol(Thing2, Decl(mixinClassesAnnotated.ts, 41, 5)) +>message : Symbol(message, Decl(mixinClassesAnnotated.ts, 17, 90)) + +function f1() { +>f1 : Symbol(f1, Decl(mixinClassesAnnotated.ts, 42, 15)) + + const thing = new Thing1(1, 2, 3); +>thing : Symbol(thing, Decl(mixinClassesAnnotated.ts, 45, 9)) +>Thing1 : Symbol(Thing1, Decl(mixinClassesAnnotated.ts, 40, 5)) + + thing.x; +>thing.x : Symbol(Base.x, Decl(mixinClassesAnnotated.ts, 4, 16)) +>thing : Symbol(thing, Decl(mixinClassesAnnotated.ts, 45, 9)) +>x : Symbol(Base.x, Decl(mixinClassesAnnotated.ts, 4, 16)) + + thing._tag; +>thing._tag : Symbol(Tagged._tag, Decl(mixinClassesAnnotated.ts, 25, 18)) +>thing : Symbol(thing, Decl(mixinClassesAnnotated.ts, 45, 9)) +>_tag : Symbol(Tagged._tag, Decl(mixinClassesAnnotated.ts, 25, 18)) +} + +function f2() { +>f2 : Symbol(f2, Decl(mixinClassesAnnotated.ts, 48, 1)) + + const thing = new Thing2(1, 2, 3); +>thing : Symbol(thing, Decl(mixinClassesAnnotated.ts, 51, 9)) +>Thing2 : Symbol(Thing2, Decl(mixinClassesAnnotated.ts, 41, 5)) + + thing.x; +>thing.x : Symbol(Base.x, Decl(mixinClassesAnnotated.ts, 4, 16)) +>thing : Symbol(thing, Decl(mixinClassesAnnotated.ts, 51, 9)) +>x : Symbol(Base.x, Decl(mixinClassesAnnotated.ts, 4, 16)) + + thing._tag; +>thing._tag : Symbol(Tagged._tag, Decl(mixinClassesAnnotated.ts, 25, 18)) +>thing : Symbol(thing, Decl(mixinClassesAnnotated.ts, 51, 9)) +>_tag : Symbol(Tagged._tag, Decl(mixinClassesAnnotated.ts, 25, 18)) + + thing.print(); +>thing.print : Symbol(Printable.print, Decl(mixinClassesAnnotated.ts, 13, 21)) +>thing : Symbol(thing, Decl(mixinClassesAnnotated.ts, 51, 9)) +>print : Symbol(Printable.print, Decl(mixinClassesAnnotated.ts, 13, 21)) +} + +class Thing3 extends Thing2 { +>Thing3 : Symbol(Thing3, Decl(mixinClassesAnnotated.ts, 55, 1)) +>Thing2 : Symbol(Thing2, Decl(mixinClassesAnnotated.ts, 41, 5)) + + constructor(tag: string) { +>tag : Symbol(tag, Decl(mixinClassesAnnotated.ts, 58, 16)) + + super(10, 20, 30); + this._tag = tag; +>this._tag : Symbol(Tagged._tag, Decl(mixinClassesAnnotated.ts, 25, 18)) +>this : Symbol(Thing3, Decl(mixinClassesAnnotated.ts, 55, 1)) +>_tag : Symbol(Tagged._tag, Decl(mixinClassesAnnotated.ts, 25, 18)) +>tag : Symbol(tag, Decl(mixinClassesAnnotated.ts, 58, 16)) + } + test() { +>test : Symbol(Thing3.test, Decl(mixinClassesAnnotated.ts, 61, 5)) + + this.print(); +>this.print : Symbol(Printable.print, Decl(mixinClassesAnnotated.ts, 13, 21)) +>this : Symbol(Thing3, Decl(mixinClassesAnnotated.ts, 55, 1)) +>print : Symbol(Printable.print, Decl(mixinClassesAnnotated.ts, 13, 21)) + } +} + diff --git a/tests/baselines/reference/mixinClassesAnnotated.types b/tests/baselines/reference/mixinClassesAnnotated.types new file mode 100644 index 00000000000..afaf4c4d25e --- /dev/null +++ b/tests/baselines/reference/mixinClassesAnnotated.types @@ -0,0 +1,224 @@ +=== tests/cases/conformance/classes/mixinClassesAnnotated.ts === + +type Constructor = new(...args: any[]) => T; +>Constructor : Constructor +>T : T +>args : any[] +>T : T + +class Base { +>Base : Base + + constructor(public x: number, public y: number) {} +>x : number +>y : number +} + +class Derived extends Base { +>Derived : Derived +>Base : Base + + constructor(x: number, y: number, public z: number) { +>x : number +>y : number +>z : number + + super(x, y); +>super(x, y) : void +>super : typeof Base +>x : number +>y : number + } +} + +interface Printable { +>Printable : Printable + + print(): void; +>print : () => void +} + +const Printable = >(superClass: T): Constructor & { message: string } & T => +>Printable : >(superClass: T) => Constructor & { message: string; } & T +>>(superClass: T): Constructor & { message: string } & T => class extends superClass { static message = "hello"; print() { const output = this.x + "," + this.y; } } : >(superClass: T) => Constructor & { message: string; } & T +>T : T +>Constructor : Constructor +>Base : Base +>superClass : T +>T : T +>Constructor : Constructor +>Printable : Printable +>message : string +>T : T + + class extends superClass { +>class extends superClass { static message = "hello"; print() { const output = this.x + "," + this.y; } } : { new (...args: any[]): (Anonymous class); prototype: .(Anonymous class); message: string; } & T +>superClass : Base + + static message = "hello"; +>message : string +>"hello" : "hello" + + print() { +>print : () => void + + const output = this.x + "," + this.y; +>output : string +>this.x + "," + this.y : string +>this.x + "," : string +>this.x : number +>this : this +>x : number +>"," : "," +>this.y : number +>this : this +>y : number + } + } + +interface Tagged { +>Tagged : Tagged + + _tag: string; +>_tag : string +} + +function Tagged>(superClass: T): Constructor & T { +>Tagged : >(superClass: T) => Constructor & T +>T : T +>Constructor : Constructor +>superClass : T +>T : T +>Constructor : Constructor +>Tagged : Tagged +>T : T + + class C extends superClass { +>C : C +>superClass : {} + + _tag: string; +>_tag : string + + constructor(...args: any[]) { +>args : any[] + + super(...args); +>super(...args) : void +>super : T +>...args : any +>args : any[] + + this._tag = "hello"; +>this._tag = "hello" : "hello" +>this._tag : string +>this : this +>_tag : string +>"hello" : "hello" + } + } + return C; +>C : { new (...args: any[]): C; prototype: Tagged.C; } & T +} + +const Thing1 = Tagged(Derived); +>Thing1 : Constructor & typeof Derived +>Tagged(Derived) : Constructor & typeof Derived +>Tagged : >(superClass: T) => Constructor & T +>Derived : typeof Derived + +const Thing2 = Tagged(Printable(Derived)); +>Thing2 : Constructor & Constructor & { message: string; } & typeof Derived +>Tagged(Printable(Derived)) : Constructor & Constructor & { message: string; } & typeof Derived +>Tagged : >(superClass: T) => Constructor & T +>Printable(Derived) : Constructor & { message: string; } & typeof Derived +>Printable : >(superClass: T) => Constructor & { message: string; } & T +>Derived : typeof Derived + +Thing2.message; +>Thing2.message : string +>Thing2 : Constructor & Constructor & { message: string; } & typeof Derived +>message : string + +function f1() { +>f1 : () => void + + const thing = new Thing1(1, 2, 3); +>thing : Tagged & Derived +>new Thing1(1, 2, 3) : Tagged & Derived +>Thing1 : Constructor & typeof Derived +>1 : 1 +>2 : 2 +>3 : 3 + + thing.x; +>thing.x : number +>thing : Tagged & Derived +>x : number + + thing._tag; +>thing._tag : string +>thing : Tagged & Derived +>_tag : string +} + +function f2() { +>f2 : () => void + + const thing = new Thing2(1, 2, 3); +>thing : Tagged & Printable & Derived +>new Thing2(1, 2, 3) : Tagged & Printable & Derived +>Thing2 : Constructor & Constructor & { message: string; } & typeof Derived +>1 : 1 +>2 : 2 +>3 : 3 + + thing.x; +>thing.x : number +>thing : Tagged & Printable & Derived +>x : number + + thing._tag; +>thing._tag : string +>thing : Tagged & Printable & Derived +>_tag : string + + thing.print(); +>thing.print() : void +>thing.print : () => void +>thing : Tagged & Printable & Derived +>print : () => void +} + +class Thing3 extends Thing2 { +>Thing3 : Thing3 +>Thing2 : Tagged & Printable & Derived + + constructor(tag: string) { +>tag : string + + super(10, 20, 30); +>super(10, 20, 30) : void +>super : Constructor & Constructor & { message: string; } & typeof Derived +>10 : 10 +>20 : 20 +>30 : 30 + + this._tag = tag; +>this._tag = tag : string +>this._tag : string +>this : this +>_tag : string +>tag : string + } + test() { +>test : () => void + + this.print(); +>this.print() : void +>this.print : () => void +>this : this +>print : () => void + } +} + diff --git a/tests/baselines/reference/mixinClassesAnonymous.js b/tests/baselines/reference/mixinClassesAnonymous.js new file mode 100644 index 00000000000..c7b8ab5fd63 --- /dev/null +++ b/tests/baselines/reference/mixinClassesAnonymous.js @@ -0,0 +1,140 @@ +//// [mixinClassesAnonymous.ts] +type Constructor = new(...args: any[]) => T; + +class Base { + constructor(public x: number, public y: number) {} +} + +class Derived extends Base { + constructor(x: number, y: number, public z: number) { + super(x, y); + } +} + +const Printable = >(superClass: T) => class extends superClass { + static message = "hello"; + print() { + const output = this.x + "," + this.y; + } +} + +function Tagged>(superClass: T) { + class C extends superClass { + _tag: string; + constructor(...args: any[]) { + super(...args); + this._tag = "hello"; + } + } + return C; +} + +const Thing1 = Tagged(Derived); +const Thing2 = Tagged(Printable(Derived)); +Thing2.message; + +function f1() { + const thing = new Thing1(1, 2, 3); + thing.x; + thing._tag; +} + +function f2() { + const thing = new Thing2(1, 2, 3); + thing.x; + thing._tag; + thing.print(); +} + +class Thing3 extends Thing2 { + constructor(tag: string) { + super(10, 20, 30); + this._tag = tag; + } + test() { + this.print(); + } +} + + +//// [mixinClassesAnonymous.js] +var __extends = (this && this.__extends) || (function () { + var extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; + return function (d, b) { + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + }; +})(); +var Base = (function () { + function Base(x, y) { + this.x = x; + this.y = y; + } + return Base; +}()); +var Derived = (function (_super) { + __extends(Derived, _super); + function Derived(x, y, z) { + var _this = _super.call(this, x, y) || this; + _this.z = z; + return _this; + } + return Derived; +}(Base)); +var Printable = function (superClass) { return _a = (function (_super) { + __extends(class_1, _super); + function class_1() { + return _super !== null && _super.apply(this, arguments) || this; + } + class_1.prototype.print = function () { + var output = this.x + "," + this.y; + }; + return class_1; + }(superClass)), + _a.message = "hello", + _a; var _a; }; +function Tagged(superClass) { + var C = (function (_super) { + __extends(C, _super); + function C() { + var args = []; + for (var _i = 0; _i < arguments.length; _i++) { + args[_i] = arguments[_i]; + } + var _this = _super.apply(this, args) || this; + _this._tag = "hello"; + return _this; + } + return C; + }(superClass)); + return C; +} +var Thing1 = Tagged(Derived); +var Thing2 = Tagged(Printable(Derived)); +Thing2.message; +function f1() { + var thing = new Thing1(1, 2, 3); + thing.x; + thing._tag; +} +function f2() { + var thing = new Thing2(1, 2, 3); + thing.x; + thing._tag; + thing.print(); +} +var Thing3 = (function (_super) { + __extends(Thing3, _super); + function Thing3(tag) { + var _this = _super.call(this, 10, 20, 30) || this; + _this._tag = tag; + return _this; + } + Thing3.prototype.test = function () { + this.print(); + }; + return Thing3; +}(Thing2)); diff --git a/tests/baselines/reference/mixinClassesAnonymous.symbols b/tests/baselines/reference/mixinClassesAnonymous.symbols new file mode 100644 index 00000000000..204d9c589fd --- /dev/null +++ b/tests/baselines/reference/mixinClassesAnonymous.symbols @@ -0,0 +1,169 @@ +=== tests/cases/conformance/classes/mixinClassesAnonymous.ts === +type Constructor = new(...args: any[]) => T; +>Constructor : Symbol(Constructor, Decl(mixinClassesAnonymous.ts, 0, 0)) +>T : Symbol(T, Decl(mixinClassesAnonymous.ts, 0, 17)) +>args : Symbol(args, Decl(mixinClassesAnonymous.ts, 0, 26)) +>T : Symbol(T, Decl(mixinClassesAnonymous.ts, 0, 17)) + +class Base { +>Base : Symbol(Base, Decl(mixinClassesAnonymous.ts, 0, 47)) + + constructor(public x: number, public y: number) {} +>x : Symbol(Base.x, Decl(mixinClassesAnonymous.ts, 3, 16)) +>y : Symbol(Base.y, Decl(mixinClassesAnonymous.ts, 3, 33)) +} + +class Derived extends Base { +>Derived : Symbol(Derived, Decl(mixinClassesAnonymous.ts, 4, 1)) +>Base : Symbol(Base, Decl(mixinClassesAnonymous.ts, 0, 47)) + + constructor(x: number, y: number, public z: number) { +>x : Symbol(x, Decl(mixinClassesAnonymous.ts, 7, 16)) +>y : Symbol(y, Decl(mixinClassesAnonymous.ts, 7, 26)) +>z : Symbol(Derived.z, Decl(mixinClassesAnonymous.ts, 7, 37)) + + super(x, y); +>super : Symbol(Base, Decl(mixinClassesAnonymous.ts, 0, 47)) +>x : Symbol(x, Decl(mixinClassesAnonymous.ts, 7, 16)) +>y : Symbol(y, Decl(mixinClassesAnonymous.ts, 7, 26)) + } +} + +const Printable = >(superClass: T) => class extends superClass { +>Printable : Symbol(Printable, Decl(mixinClassesAnonymous.ts, 12, 5)) +>T : Symbol(T, Decl(mixinClassesAnonymous.ts, 12, 19)) +>Constructor : Symbol(Constructor, Decl(mixinClassesAnonymous.ts, 0, 0)) +>Base : Symbol(Base, Decl(mixinClassesAnonymous.ts, 0, 47)) +>superClass : Symbol(superClass, Decl(mixinClassesAnonymous.ts, 12, 48)) +>T : Symbol(T, Decl(mixinClassesAnonymous.ts, 12, 19)) +>superClass : Symbol(superClass, Decl(mixinClassesAnonymous.ts, 12, 48)) + + static message = "hello"; +>message : Symbol((Anonymous class).message, Decl(mixinClassesAnonymous.ts, 12, 92)) + + print() { +>print : Symbol((Anonymous class).print, Decl(mixinClassesAnonymous.ts, 13, 29)) + + const output = this.x + "," + this.y; +>output : Symbol(output, Decl(mixinClassesAnonymous.ts, 15, 13)) +>this.x : Symbol(Base.x, Decl(mixinClassesAnonymous.ts, 3, 16)) +>this : Symbol((Anonymous class), Decl(mixinClassesAnonymous.ts, 12, 65)) +>x : Symbol(Base.x, Decl(mixinClassesAnonymous.ts, 3, 16)) +>this.y : Symbol(Base.y, Decl(mixinClassesAnonymous.ts, 3, 33)) +>this : Symbol((Anonymous class), Decl(mixinClassesAnonymous.ts, 12, 65)) +>y : Symbol(Base.y, Decl(mixinClassesAnonymous.ts, 3, 33)) + } +} + +function Tagged>(superClass: T) { +>Tagged : Symbol(Tagged, Decl(mixinClassesAnonymous.ts, 17, 1)) +>T : Symbol(T, Decl(mixinClassesAnonymous.ts, 19, 16)) +>Constructor : Symbol(Constructor, Decl(mixinClassesAnonymous.ts, 0, 0)) +>superClass : Symbol(superClass, Decl(mixinClassesAnonymous.ts, 19, 43)) +>T : Symbol(T, Decl(mixinClassesAnonymous.ts, 19, 16)) + + class C extends superClass { +>C : Symbol(C, Decl(mixinClassesAnonymous.ts, 19, 59)) +>superClass : Symbol(superClass, Decl(mixinClassesAnonymous.ts, 19, 43)) + + _tag: string; +>_tag : Symbol(C._tag, Decl(mixinClassesAnonymous.ts, 20, 32)) + + constructor(...args: any[]) { +>args : Symbol(args, Decl(mixinClassesAnonymous.ts, 22, 20)) + + super(...args); +>super : Symbol(T, Decl(mixinClassesAnonymous.ts, 19, 16)) +>args : Symbol(args, Decl(mixinClassesAnonymous.ts, 22, 20)) + + this._tag = "hello"; +>this._tag : Symbol(C._tag, Decl(mixinClassesAnonymous.ts, 20, 32)) +>this : Symbol(C, Decl(mixinClassesAnonymous.ts, 19, 59)) +>_tag : Symbol(C._tag, Decl(mixinClassesAnonymous.ts, 20, 32)) + } + } + return C; +>C : Symbol(C, Decl(mixinClassesAnonymous.ts, 19, 59)) +} + +const Thing1 = Tagged(Derived); +>Thing1 : Symbol(Thing1, Decl(mixinClassesAnonymous.ts, 30, 5)) +>Tagged : Symbol(Tagged, Decl(mixinClassesAnonymous.ts, 17, 1)) +>Derived : Symbol(Derived, Decl(mixinClassesAnonymous.ts, 4, 1)) + +const Thing2 = Tagged(Printable(Derived)); +>Thing2 : Symbol(Thing2, Decl(mixinClassesAnonymous.ts, 31, 5)) +>Tagged : Symbol(Tagged, Decl(mixinClassesAnonymous.ts, 17, 1)) +>Printable : Symbol(Printable, Decl(mixinClassesAnonymous.ts, 12, 5)) +>Derived : Symbol(Derived, Decl(mixinClassesAnonymous.ts, 4, 1)) + +Thing2.message; +>Thing2.message : Symbol((Anonymous class).message, Decl(mixinClassesAnonymous.ts, 12, 92)) +>Thing2 : Symbol(Thing2, Decl(mixinClassesAnonymous.ts, 31, 5)) +>message : Symbol((Anonymous class).message, Decl(mixinClassesAnonymous.ts, 12, 92)) + +function f1() { +>f1 : Symbol(f1, Decl(mixinClassesAnonymous.ts, 32, 15)) + + const thing = new Thing1(1, 2, 3); +>thing : Symbol(thing, Decl(mixinClassesAnonymous.ts, 35, 9)) +>Thing1 : Symbol(Thing1, Decl(mixinClassesAnonymous.ts, 30, 5)) + + thing.x; +>thing.x : Symbol(Base.x, Decl(mixinClassesAnonymous.ts, 3, 16)) +>thing : Symbol(thing, Decl(mixinClassesAnonymous.ts, 35, 9)) +>x : Symbol(Base.x, Decl(mixinClassesAnonymous.ts, 3, 16)) + + thing._tag; +>thing._tag : Symbol(C._tag, Decl(mixinClassesAnonymous.ts, 20, 32)) +>thing : Symbol(thing, Decl(mixinClassesAnonymous.ts, 35, 9)) +>_tag : Symbol(C._tag, Decl(mixinClassesAnonymous.ts, 20, 32)) +} + +function f2() { +>f2 : Symbol(f2, Decl(mixinClassesAnonymous.ts, 38, 1)) + + const thing = new Thing2(1, 2, 3); +>thing : Symbol(thing, Decl(mixinClassesAnonymous.ts, 41, 9)) +>Thing2 : Symbol(Thing2, Decl(mixinClassesAnonymous.ts, 31, 5)) + + thing.x; +>thing.x : Symbol(Base.x, Decl(mixinClassesAnonymous.ts, 3, 16)) +>thing : Symbol(thing, Decl(mixinClassesAnonymous.ts, 41, 9)) +>x : Symbol(Base.x, Decl(mixinClassesAnonymous.ts, 3, 16)) + + thing._tag; +>thing._tag : Symbol(C._tag, Decl(mixinClassesAnonymous.ts, 20, 32)) +>thing : Symbol(thing, Decl(mixinClassesAnonymous.ts, 41, 9)) +>_tag : Symbol(C._tag, Decl(mixinClassesAnonymous.ts, 20, 32)) + + thing.print(); +>thing.print : Symbol((Anonymous class).print, Decl(mixinClassesAnonymous.ts, 13, 29)) +>thing : Symbol(thing, Decl(mixinClassesAnonymous.ts, 41, 9)) +>print : Symbol((Anonymous class).print, Decl(mixinClassesAnonymous.ts, 13, 29)) +} + +class Thing3 extends Thing2 { +>Thing3 : Symbol(Thing3, Decl(mixinClassesAnonymous.ts, 45, 1)) +>Thing2 : Symbol(Thing2, Decl(mixinClassesAnonymous.ts, 31, 5)) + + constructor(tag: string) { +>tag : Symbol(tag, Decl(mixinClassesAnonymous.ts, 48, 16)) + + super(10, 20, 30); + this._tag = tag; +>this._tag : Symbol(C._tag, Decl(mixinClassesAnonymous.ts, 20, 32)) +>this : Symbol(Thing3, Decl(mixinClassesAnonymous.ts, 45, 1)) +>_tag : Symbol(C._tag, Decl(mixinClassesAnonymous.ts, 20, 32)) +>tag : Symbol(tag, Decl(mixinClassesAnonymous.ts, 48, 16)) + } + test() { +>test : Symbol(Thing3.test, Decl(mixinClassesAnonymous.ts, 51, 5)) + + this.print(); +>this.print : Symbol((Anonymous class).print, Decl(mixinClassesAnonymous.ts, 13, 29)) +>this : Symbol(Thing3, Decl(mixinClassesAnonymous.ts, 45, 1)) +>print : Symbol((Anonymous class).print, Decl(mixinClassesAnonymous.ts, 13, 29)) + } +} + diff --git a/tests/baselines/reference/mixinClassesAnonymous.types b/tests/baselines/reference/mixinClassesAnonymous.types new file mode 100644 index 00000000000..54b1c2c3819 --- /dev/null +++ b/tests/baselines/reference/mixinClassesAnonymous.types @@ -0,0 +1,200 @@ +=== tests/cases/conformance/classes/mixinClassesAnonymous.ts === +type Constructor = new(...args: any[]) => T; +>Constructor : Constructor +>T : T +>args : any[] +>T : T + +class Base { +>Base : Base + + constructor(public x: number, public y: number) {} +>x : number +>y : number +} + +class Derived extends Base { +>Derived : Derived +>Base : Base + + constructor(x: number, y: number, public z: number) { +>x : number +>y : number +>z : number + + super(x, y); +>super(x, y) : void +>super : typeof Base +>x : number +>y : number + } +} + +const Printable = >(superClass: T) => class extends superClass { +>Printable : >(superClass: T) => { new (...args: any[]): (Anonymous class); prototype: .(Anonymous class); message: string; } & T +>>(superClass: T) => class extends superClass { static message = "hello"; print() { const output = this.x + "," + this.y; }} : >(superClass: T) => { new (...args: any[]): (Anonymous class); prototype: .(Anonymous class); message: string; } & T +>T : T +>Constructor : Constructor +>Base : Base +>superClass : T +>T : T +>class extends superClass { static message = "hello"; print() { const output = this.x + "," + this.y; }} : { new (...args: any[]): (Anonymous class); prototype: .(Anonymous class); message: string; } & T +>superClass : Base + + static message = "hello"; +>message : string +>"hello" : "hello" + + print() { +>print : () => void + + const output = this.x + "," + this.y; +>output : string +>this.x + "," + this.y : string +>this.x + "," : string +>this.x : number +>this : this +>x : number +>"," : "," +>this.y : number +>this : this +>y : number + } +} + +function Tagged>(superClass: T) { +>Tagged : >(superClass: T) => { new (...args: any[]): C; prototype: Tagged.C; } & T +>T : T +>Constructor : Constructor +>superClass : T +>T : T + + class C extends superClass { +>C : C +>superClass : {} + + _tag: string; +>_tag : string + + constructor(...args: any[]) { +>args : any[] + + super(...args); +>super(...args) : void +>super : T +>...args : any +>args : any[] + + this._tag = "hello"; +>this._tag = "hello" : "hello" +>this._tag : string +>this : this +>_tag : string +>"hello" : "hello" + } + } + return C; +>C : { new (...args: any[]): C; prototype: Tagged.C; } & T +} + +const Thing1 = Tagged(Derived); +>Thing1 : { new (...args: any[]): Tagged.C; prototype: Tagged.C; } & typeof Derived +>Tagged(Derived) : { new (...args: any[]): Tagged.C; prototype: Tagged.C; } & typeof Derived +>Tagged : >(superClass: T) => { new (...args: any[]): C; prototype: Tagged.C; } & T +>Derived : typeof Derived + +const Thing2 = Tagged(Printable(Derived)); +>Thing2 : { new (...args: any[]): Tagged<{ new (...args: any[]): .(Anonymous class); prototype: .(Anonymous class); message: string; } & typeof Derived>.C; prototype: Tagged.C; } & { new (...args: any[]): .(Anonymous class); prototype: .(Anonymous class); message: string; } & typeof Derived +>Tagged(Printable(Derived)) : { new (...args: any[]): Tagged<{ new (...args: any[]): .(Anonymous class); prototype: .(Anonymous class); message: string; } & typeof Derived>.C; prototype: Tagged.C; } & { new (...args: any[]): .(Anonymous class); prototype: .(Anonymous class); message: string; } & typeof Derived +>Tagged : >(superClass: T) => { new (...args: any[]): C; prototype: Tagged.C; } & T +>Printable(Derived) : { new (...args: any[]): .(Anonymous class); prototype: .(Anonymous class); message: string; } & typeof Derived +>Printable : >(superClass: T) => { new (...args: any[]): (Anonymous class); prototype: .(Anonymous class); message: string; } & T +>Derived : typeof Derived + +Thing2.message; +>Thing2.message : string +>Thing2 : { new (...args: any[]): Tagged<{ new (...args: any[]): .(Anonymous class); prototype: .(Anonymous class); message: string; } & typeof Derived>.C; prototype: Tagged.C; } & { new (...args: any[]): .(Anonymous class); prototype: .(Anonymous class); message: string; } & typeof Derived +>message : string + +function f1() { +>f1 : () => void + + const thing = new Thing1(1, 2, 3); +>thing : Tagged.C & Derived +>new Thing1(1, 2, 3) : Tagged.C & Derived +>Thing1 : { new (...args: any[]): Tagged.C; prototype: Tagged.C; } & typeof Derived +>1 : 1 +>2 : 2 +>3 : 3 + + thing.x; +>thing.x : number +>thing : Tagged.C & Derived +>x : number + + thing._tag; +>thing._tag : string +>thing : Tagged.C & Derived +>_tag : string +} + +function f2() { +>f2 : () => void + + const thing = new Thing2(1, 2, 3); +>thing : Tagged<{ new (...args: any[]): .(Anonymous class); prototype: .(Anonymous class); message: string; } & typeof Derived>.C & .(Anonymous class) & Derived +>new Thing2(1, 2, 3) : Tagged<{ new (...args: any[]): .(Anonymous class); prototype: .(Anonymous class); message: string; } & typeof Derived>.C & .(Anonymous class) & Derived +>Thing2 : { new (...args: any[]): Tagged<{ new (...args: any[]): .(Anonymous class); prototype: .(Anonymous class); message: string; } & typeof Derived>.C; prototype: Tagged.C; } & { new (...args: any[]): .(Anonymous class); prototype: .(Anonymous class); message: string; } & typeof Derived +>1 : 1 +>2 : 2 +>3 : 3 + + thing.x; +>thing.x : number +>thing : Tagged<{ new (...args: any[]): .(Anonymous class); prototype: .(Anonymous class); message: string; } & typeof Derived>.C & .(Anonymous class) & Derived +>x : number + + thing._tag; +>thing._tag : string +>thing : Tagged<{ new (...args: any[]): .(Anonymous class); prototype: .(Anonymous class); message: string; } & typeof Derived>.C & .(Anonymous class) & Derived +>_tag : string + + thing.print(); +>thing.print() : void +>thing.print : () => void +>thing : Tagged<{ new (...args: any[]): .(Anonymous class); prototype: .(Anonymous class); message: string; } & typeof Derived>.C & .(Anonymous class) & Derived +>print : () => void +} + +class Thing3 extends Thing2 { +>Thing3 : Thing3 +>Thing2 : Tagged<{ new (...args: any[]): .(Anonymous class); prototype: .(Anonymous class); message: string; } & typeof Derived>.C & .(Anonymous class) & Derived + + constructor(tag: string) { +>tag : string + + super(10, 20, 30); +>super(10, 20, 30) : void +>super : { new (...args: any[]): Tagged<{ new (...args: any[]): .(Anonymous class); prototype: .(Anonymous class); message: string; } & typeof Derived>.C; prototype: Tagged.C; } & { new (...args: any[]): .(Anonymous class); prototype: .(Anonymous class); message: string; } & typeof Derived +>10 : 10 +>20 : 20 +>30 : 30 + + this._tag = tag; +>this._tag = tag : string +>this._tag : string +>this : this +>_tag : string +>tag : string + } + test() { +>test : () => void + + this.print(); +>this.print() : void +>this.print : () => void +>this : this +>print : () => void + } +} + diff --git a/tests/baselines/reference/mixinClassesMembers.js b/tests/baselines/reference/mixinClassesMembers.js new file mode 100644 index 00000000000..01fc1bb0032 --- /dev/null +++ b/tests/baselines/reference/mixinClassesMembers.js @@ -0,0 +1,220 @@ +//// [mixinClassesMembers.ts] + +declare class C1 { + public a: number; + protected b: number; + private c: number; + constructor(s: string); + constructor(n: number); +} + +declare class M1 { + constructor(...args: any[]); + p: number; + static p: number; +} + +declare class M2 { + constructor(...args: any[]); + f(): number; + static f(): number; +} + +declare const Mixed1: typeof M1 & typeof C1; +declare const Mixed2: typeof C1 & typeof M1; +declare const Mixed3: typeof M2 & typeof M1 & typeof C1; +declare const Mixed4: typeof C1 & typeof M1 & typeof M2; +declare const Mixed5: typeof M1 & typeof M2; + +function f1() { + let x1 = new Mixed1("hello"); + let x2 = new Mixed1(42); + let x3 = new Mixed2("hello"); + let x4 = new Mixed2(42); + let x5 = new Mixed3("hello"); + let x6 = new Mixed3(42); + let x7 = new Mixed4("hello"); + let x8 = new Mixed4(42); + let x9 = new Mixed5(); +} + +function f2() { + let x = new Mixed1("hello"); + x.a; + x.p; + Mixed1.p; +} + +function f3() { + let x = new Mixed2("hello"); + x.a; + x.p; + Mixed2.p; +} + +function f4() { + let x = new Mixed3("hello"); + x.a; + x.p; + x.f(); + Mixed3.p; + Mixed3.f(); +} + +function f5() { + let x = new Mixed4("hello"); + x.a; + x.p; + x.f(); + Mixed4.p; + Mixed4.f(); +} + +function f6() { + let x = new Mixed5(); + x.p; + x.f(); + Mixed5.p; + Mixed5.f(); +} + +class C2 extends Mixed1 { + constructor() { + super("hello"); + this.a; + this.b; + this.p; + } +} + +class C3 extends Mixed3 { + constructor() { + super(42); + this.a; + this.b; + this.p; + this.f(); + } + f() { return super.f(); } +} + + +//// [mixinClassesMembers.js] +var __extends = (this && this.__extends) || (function () { + var extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; + return function (d, b) { + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + }; +})(); +function f1() { + var x1 = new Mixed1("hello"); + var x2 = new Mixed1(42); + var x3 = new Mixed2("hello"); + var x4 = new Mixed2(42); + var x5 = new Mixed3("hello"); + var x6 = new Mixed3(42); + var x7 = new Mixed4("hello"); + var x8 = new Mixed4(42); + var x9 = new Mixed5(); +} +function f2() { + var x = new Mixed1("hello"); + x.a; + x.p; + Mixed1.p; +} +function f3() { + var x = new Mixed2("hello"); + x.a; + x.p; + Mixed2.p; +} +function f4() { + var x = new Mixed3("hello"); + x.a; + x.p; + x.f(); + Mixed3.p; + Mixed3.f(); +} +function f5() { + var x = new Mixed4("hello"); + x.a; + x.p; + x.f(); + Mixed4.p; + Mixed4.f(); +} +function f6() { + var x = new Mixed5(); + x.p; + x.f(); + Mixed5.p; + Mixed5.f(); +} +var C2 = (function (_super) { + __extends(C2, _super); + function C2() { + var _this = _super.call(this, "hello") || this; + _this.a; + _this.b; + _this.p; + return _this; + } + return C2; +}(Mixed1)); +var C3 = (function (_super) { + __extends(C3, _super); + function C3() { + var _this = _super.call(this, 42) || this; + _this.a; + _this.b; + _this.p; + _this.f(); + return _this; + } + C3.prototype.f = function () { return _super.prototype.f.call(this); }; + return C3; +}(Mixed3)); + + +//// [mixinClassesMembers.d.ts] +declare class C1 { + a: number; + protected b: number; + private c; + constructor(s: string); + constructor(n: number); +} +declare class M1 { + constructor(...args: any[]); + p: number; + static p: number; +} +declare class M2 { + constructor(...args: any[]); + f(): number; + static f(): number; +} +declare const Mixed1: typeof M1 & typeof C1; +declare const Mixed2: typeof C1 & typeof M1; +declare const Mixed3: typeof M2 & typeof M1 & typeof C1; +declare const Mixed4: typeof C1 & typeof M1 & typeof M2; +declare const Mixed5: typeof M1 & typeof M2; +declare function f1(): void; +declare function f2(): void; +declare function f3(): void; +declare function f4(): void; +declare function f5(): void; +declare function f6(): void; +declare class C2 extends Mixed1 { + constructor(); +} +declare class C3 extends Mixed3 { + constructor(); + f(): number; +} diff --git a/tests/baselines/reference/mixinClassesMembers.symbols b/tests/baselines/reference/mixinClassesMembers.symbols new file mode 100644 index 00000000000..fabfe7e6d86 --- /dev/null +++ b/tests/baselines/reference/mixinClassesMembers.symbols @@ -0,0 +1,309 @@ +=== tests/cases/conformance/classes/mixinClassesMembers.ts === + +declare class C1 { +>C1 : Symbol(C1, Decl(mixinClassesMembers.ts, 0, 0)) + + public a: number; +>a : Symbol(C1.a, Decl(mixinClassesMembers.ts, 1, 18)) + + protected b: number; +>b : Symbol(C1.b, Decl(mixinClassesMembers.ts, 2, 21)) + + private c: number; +>c : Symbol(C1.c, Decl(mixinClassesMembers.ts, 3, 24)) + + constructor(s: string); +>s : Symbol(s, Decl(mixinClassesMembers.ts, 5, 16)) + + constructor(n: number); +>n : Symbol(n, Decl(mixinClassesMembers.ts, 6, 16)) +} + +declare class M1 { +>M1 : Symbol(M1, Decl(mixinClassesMembers.ts, 7, 1)) + + constructor(...args: any[]); +>args : Symbol(args, Decl(mixinClassesMembers.ts, 10, 16)) + + p: number; +>p : Symbol(M1.p, Decl(mixinClassesMembers.ts, 10, 32)) + + static p: number; +>p : Symbol(M1.p, Decl(mixinClassesMembers.ts, 11, 14)) +} + +declare class M2 { +>M2 : Symbol(M2, Decl(mixinClassesMembers.ts, 13, 1)) + + constructor(...args: any[]); +>args : Symbol(args, Decl(mixinClassesMembers.ts, 16, 16)) + + f(): number; +>f : Symbol(M2.f, Decl(mixinClassesMembers.ts, 16, 32)) + + static f(): number; +>f : Symbol(M2.f, Decl(mixinClassesMembers.ts, 17, 16)) +} + +declare const Mixed1: typeof M1 & typeof C1; +>Mixed1 : Symbol(Mixed1, Decl(mixinClassesMembers.ts, 21, 13)) +>M1 : Symbol(M1, Decl(mixinClassesMembers.ts, 7, 1)) +>C1 : Symbol(C1, Decl(mixinClassesMembers.ts, 0, 0)) + +declare const Mixed2: typeof C1 & typeof M1; +>Mixed2 : Symbol(Mixed2, Decl(mixinClassesMembers.ts, 22, 13)) +>C1 : Symbol(C1, Decl(mixinClassesMembers.ts, 0, 0)) +>M1 : Symbol(M1, Decl(mixinClassesMembers.ts, 7, 1)) + +declare const Mixed3: typeof M2 & typeof M1 & typeof C1; +>Mixed3 : Symbol(Mixed3, Decl(mixinClassesMembers.ts, 23, 13)) +>M2 : Symbol(M2, Decl(mixinClassesMembers.ts, 13, 1)) +>M1 : Symbol(M1, Decl(mixinClassesMembers.ts, 7, 1)) +>C1 : Symbol(C1, Decl(mixinClassesMembers.ts, 0, 0)) + +declare const Mixed4: typeof C1 & typeof M1 & typeof M2; +>Mixed4 : Symbol(Mixed4, Decl(mixinClassesMembers.ts, 24, 13)) +>C1 : Symbol(C1, Decl(mixinClassesMembers.ts, 0, 0)) +>M1 : Symbol(M1, Decl(mixinClassesMembers.ts, 7, 1)) +>M2 : Symbol(M2, Decl(mixinClassesMembers.ts, 13, 1)) + +declare const Mixed5: typeof M1 & typeof M2; +>Mixed5 : Symbol(Mixed5, Decl(mixinClassesMembers.ts, 25, 13)) +>M1 : Symbol(M1, Decl(mixinClassesMembers.ts, 7, 1)) +>M2 : Symbol(M2, Decl(mixinClassesMembers.ts, 13, 1)) + +function f1() { +>f1 : Symbol(f1, Decl(mixinClassesMembers.ts, 25, 44)) + + let x1 = new Mixed1("hello"); +>x1 : Symbol(x1, Decl(mixinClassesMembers.ts, 28, 7)) +>Mixed1 : Symbol(Mixed1, Decl(mixinClassesMembers.ts, 21, 13)) + + let x2 = new Mixed1(42); +>x2 : Symbol(x2, Decl(mixinClassesMembers.ts, 29, 7)) +>Mixed1 : Symbol(Mixed1, Decl(mixinClassesMembers.ts, 21, 13)) + + let x3 = new Mixed2("hello"); +>x3 : Symbol(x3, Decl(mixinClassesMembers.ts, 30, 7)) +>Mixed2 : Symbol(Mixed2, Decl(mixinClassesMembers.ts, 22, 13)) + + let x4 = new Mixed2(42); +>x4 : Symbol(x4, Decl(mixinClassesMembers.ts, 31, 7)) +>Mixed2 : Symbol(Mixed2, Decl(mixinClassesMembers.ts, 22, 13)) + + let x5 = new Mixed3("hello"); +>x5 : Symbol(x5, Decl(mixinClassesMembers.ts, 32, 7)) +>Mixed3 : Symbol(Mixed3, Decl(mixinClassesMembers.ts, 23, 13)) + + let x6 = new Mixed3(42); +>x6 : Symbol(x6, Decl(mixinClassesMembers.ts, 33, 7)) +>Mixed3 : Symbol(Mixed3, Decl(mixinClassesMembers.ts, 23, 13)) + + let x7 = new Mixed4("hello"); +>x7 : Symbol(x7, Decl(mixinClassesMembers.ts, 34, 7)) +>Mixed4 : Symbol(Mixed4, Decl(mixinClassesMembers.ts, 24, 13)) + + let x8 = new Mixed4(42); +>x8 : Symbol(x8, Decl(mixinClassesMembers.ts, 35, 7)) +>Mixed4 : Symbol(Mixed4, Decl(mixinClassesMembers.ts, 24, 13)) + + let x9 = new Mixed5(); +>x9 : Symbol(x9, Decl(mixinClassesMembers.ts, 36, 7)) +>Mixed5 : Symbol(Mixed5, Decl(mixinClassesMembers.ts, 25, 13)) +} + +function f2() { +>f2 : Symbol(f2, Decl(mixinClassesMembers.ts, 37, 1)) + + let x = new Mixed1("hello"); +>x : Symbol(x, Decl(mixinClassesMembers.ts, 40, 7)) +>Mixed1 : Symbol(Mixed1, Decl(mixinClassesMembers.ts, 21, 13)) + + x.a; +>x.a : Symbol(C1.a, Decl(mixinClassesMembers.ts, 1, 18)) +>x : Symbol(x, Decl(mixinClassesMembers.ts, 40, 7)) +>a : Symbol(C1.a, Decl(mixinClassesMembers.ts, 1, 18)) + + x.p; +>x.p : Symbol(M1.p, Decl(mixinClassesMembers.ts, 10, 32)) +>x : Symbol(x, Decl(mixinClassesMembers.ts, 40, 7)) +>p : Symbol(M1.p, Decl(mixinClassesMembers.ts, 10, 32)) + + Mixed1.p; +>Mixed1.p : Symbol(M1.p, Decl(mixinClassesMembers.ts, 11, 14)) +>Mixed1 : Symbol(Mixed1, Decl(mixinClassesMembers.ts, 21, 13)) +>p : Symbol(M1.p, Decl(mixinClassesMembers.ts, 11, 14)) +} + +function f3() { +>f3 : Symbol(f3, Decl(mixinClassesMembers.ts, 44, 1)) + + let x = new Mixed2("hello"); +>x : Symbol(x, Decl(mixinClassesMembers.ts, 47, 7)) +>Mixed2 : Symbol(Mixed2, Decl(mixinClassesMembers.ts, 22, 13)) + + x.a; +>x.a : Symbol(C1.a, Decl(mixinClassesMembers.ts, 1, 18)) +>x : Symbol(x, Decl(mixinClassesMembers.ts, 47, 7)) +>a : Symbol(C1.a, Decl(mixinClassesMembers.ts, 1, 18)) + + x.p; +>x.p : Symbol(M1.p, Decl(mixinClassesMembers.ts, 10, 32)) +>x : Symbol(x, Decl(mixinClassesMembers.ts, 47, 7)) +>p : Symbol(M1.p, Decl(mixinClassesMembers.ts, 10, 32)) + + Mixed2.p; +>Mixed2.p : Symbol(M1.p, Decl(mixinClassesMembers.ts, 11, 14)) +>Mixed2 : Symbol(Mixed2, Decl(mixinClassesMembers.ts, 22, 13)) +>p : Symbol(M1.p, Decl(mixinClassesMembers.ts, 11, 14)) +} + +function f4() { +>f4 : Symbol(f4, Decl(mixinClassesMembers.ts, 51, 1)) + + let x = new Mixed3("hello"); +>x : Symbol(x, Decl(mixinClassesMembers.ts, 54, 7)) +>Mixed3 : Symbol(Mixed3, Decl(mixinClassesMembers.ts, 23, 13)) + + x.a; +>x.a : Symbol(C1.a, Decl(mixinClassesMembers.ts, 1, 18)) +>x : Symbol(x, Decl(mixinClassesMembers.ts, 54, 7)) +>a : Symbol(C1.a, Decl(mixinClassesMembers.ts, 1, 18)) + + x.p; +>x.p : Symbol(M1.p, Decl(mixinClassesMembers.ts, 10, 32)) +>x : Symbol(x, Decl(mixinClassesMembers.ts, 54, 7)) +>p : Symbol(M1.p, Decl(mixinClassesMembers.ts, 10, 32)) + + x.f(); +>x.f : Symbol(M2.f, Decl(mixinClassesMembers.ts, 16, 32)) +>x : Symbol(x, Decl(mixinClassesMembers.ts, 54, 7)) +>f : Symbol(M2.f, Decl(mixinClassesMembers.ts, 16, 32)) + + Mixed3.p; +>Mixed3.p : Symbol(M1.p, Decl(mixinClassesMembers.ts, 11, 14)) +>Mixed3 : Symbol(Mixed3, Decl(mixinClassesMembers.ts, 23, 13)) +>p : Symbol(M1.p, Decl(mixinClassesMembers.ts, 11, 14)) + + Mixed3.f(); +>Mixed3.f : Symbol(M2.f, Decl(mixinClassesMembers.ts, 17, 16)) +>Mixed3 : Symbol(Mixed3, Decl(mixinClassesMembers.ts, 23, 13)) +>f : Symbol(M2.f, Decl(mixinClassesMembers.ts, 17, 16)) +} + +function f5() { +>f5 : Symbol(f5, Decl(mixinClassesMembers.ts, 60, 1)) + + let x = new Mixed4("hello"); +>x : Symbol(x, Decl(mixinClassesMembers.ts, 63, 7)) +>Mixed4 : Symbol(Mixed4, Decl(mixinClassesMembers.ts, 24, 13)) + + x.a; +>x.a : Symbol(C1.a, Decl(mixinClassesMembers.ts, 1, 18)) +>x : Symbol(x, Decl(mixinClassesMembers.ts, 63, 7)) +>a : Symbol(C1.a, Decl(mixinClassesMembers.ts, 1, 18)) + + x.p; +>x.p : Symbol(M1.p, Decl(mixinClassesMembers.ts, 10, 32)) +>x : Symbol(x, Decl(mixinClassesMembers.ts, 63, 7)) +>p : Symbol(M1.p, Decl(mixinClassesMembers.ts, 10, 32)) + + x.f(); +>x.f : Symbol(M2.f, Decl(mixinClassesMembers.ts, 16, 32)) +>x : Symbol(x, Decl(mixinClassesMembers.ts, 63, 7)) +>f : Symbol(M2.f, Decl(mixinClassesMembers.ts, 16, 32)) + + Mixed4.p; +>Mixed4.p : Symbol(M1.p, Decl(mixinClassesMembers.ts, 11, 14)) +>Mixed4 : Symbol(Mixed4, Decl(mixinClassesMembers.ts, 24, 13)) +>p : Symbol(M1.p, Decl(mixinClassesMembers.ts, 11, 14)) + + Mixed4.f(); +>Mixed4.f : Symbol(M2.f, Decl(mixinClassesMembers.ts, 17, 16)) +>Mixed4 : Symbol(Mixed4, Decl(mixinClassesMembers.ts, 24, 13)) +>f : Symbol(M2.f, Decl(mixinClassesMembers.ts, 17, 16)) +} + +function f6() { +>f6 : Symbol(f6, Decl(mixinClassesMembers.ts, 69, 1)) + + let x = new Mixed5(); +>x : Symbol(x, Decl(mixinClassesMembers.ts, 72, 7)) +>Mixed5 : Symbol(Mixed5, Decl(mixinClassesMembers.ts, 25, 13)) + + x.p; +>x.p : Symbol(M1.p, Decl(mixinClassesMembers.ts, 10, 32)) +>x : Symbol(x, Decl(mixinClassesMembers.ts, 72, 7)) +>p : Symbol(M1.p, Decl(mixinClassesMembers.ts, 10, 32)) + + x.f(); +>x.f : Symbol(M2.f, Decl(mixinClassesMembers.ts, 16, 32)) +>x : Symbol(x, Decl(mixinClassesMembers.ts, 72, 7)) +>f : Symbol(M2.f, Decl(mixinClassesMembers.ts, 16, 32)) + + Mixed5.p; +>Mixed5.p : Symbol(M1.p, Decl(mixinClassesMembers.ts, 11, 14)) +>Mixed5 : Symbol(Mixed5, Decl(mixinClassesMembers.ts, 25, 13)) +>p : Symbol(M1.p, Decl(mixinClassesMembers.ts, 11, 14)) + + Mixed5.f(); +>Mixed5.f : Symbol(M2.f, Decl(mixinClassesMembers.ts, 17, 16)) +>Mixed5 : Symbol(Mixed5, Decl(mixinClassesMembers.ts, 25, 13)) +>f : Symbol(M2.f, Decl(mixinClassesMembers.ts, 17, 16)) +} + +class C2 extends Mixed1 { +>C2 : Symbol(C2, Decl(mixinClassesMembers.ts, 77, 1)) +>Mixed1 : Symbol(Mixed1, Decl(mixinClassesMembers.ts, 21, 13)) + + constructor() { + super("hello"); + this.a; +>this.a : Symbol(C1.a, Decl(mixinClassesMembers.ts, 1, 18)) +>this : Symbol(C2, Decl(mixinClassesMembers.ts, 77, 1)) +>a : Symbol(C1.a, Decl(mixinClassesMembers.ts, 1, 18)) + + this.b; +>this.b : Symbol(C1.b, Decl(mixinClassesMembers.ts, 2, 21)) +>this : Symbol(C2, Decl(mixinClassesMembers.ts, 77, 1)) +>b : Symbol(C1.b, Decl(mixinClassesMembers.ts, 2, 21)) + + this.p; +>this.p : Symbol(M1.p, Decl(mixinClassesMembers.ts, 10, 32)) +>this : Symbol(C2, Decl(mixinClassesMembers.ts, 77, 1)) +>p : Symbol(M1.p, Decl(mixinClassesMembers.ts, 10, 32)) + } +} + +class C3 extends Mixed3 { +>C3 : Symbol(C3, Decl(mixinClassesMembers.ts, 86, 1)) +>Mixed3 : Symbol(Mixed3, Decl(mixinClassesMembers.ts, 23, 13)) + + constructor() { + super(42); + this.a; +>this.a : Symbol(C1.a, Decl(mixinClassesMembers.ts, 1, 18)) +>this : Symbol(C3, Decl(mixinClassesMembers.ts, 86, 1)) +>a : Symbol(C1.a, Decl(mixinClassesMembers.ts, 1, 18)) + + this.b; +>this.b : Symbol(C1.b, Decl(mixinClassesMembers.ts, 2, 21)) +>this : Symbol(C3, Decl(mixinClassesMembers.ts, 86, 1)) +>b : Symbol(C1.b, Decl(mixinClassesMembers.ts, 2, 21)) + + this.p; +>this.p : Symbol(M1.p, Decl(mixinClassesMembers.ts, 10, 32)) +>this : Symbol(C3, Decl(mixinClassesMembers.ts, 86, 1)) +>p : Symbol(M1.p, Decl(mixinClassesMembers.ts, 10, 32)) + + this.f(); +>this.f : Symbol(C3.f, Decl(mixinClassesMembers.ts, 95, 5)) +>this : Symbol(C3, Decl(mixinClassesMembers.ts, 86, 1)) +>f : Symbol(C3.f, Decl(mixinClassesMembers.ts, 95, 5)) + } + f() { return super.f(); } +>f : Symbol(C3.f, Decl(mixinClassesMembers.ts, 95, 5)) +>super.f : Symbol(M2.f, Decl(mixinClassesMembers.ts, 16, 32)) +>f : Symbol(M2.f, Decl(mixinClassesMembers.ts, 16, 32)) +} + diff --git a/tests/baselines/reference/mixinClassesMembers.types b/tests/baselines/reference/mixinClassesMembers.types new file mode 100644 index 00000000000..c8b29175415 --- /dev/null +++ b/tests/baselines/reference/mixinClassesMembers.types @@ -0,0 +1,352 @@ +=== tests/cases/conformance/classes/mixinClassesMembers.ts === + +declare class C1 { +>C1 : C1 + + public a: number; +>a : number + + protected b: number; +>b : number + + private c: number; +>c : number + + constructor(s: string); +>s : string + + constructor(n: number); +>n : number +} + +declare class M1 { +>M1 : M1 + + constructor(...args: any[]); +>args : any[] + + p: number; +>p : number + + static p: number; +>p : number +} + +declare class M2 { +>M2 : M2 + + constructor(...args: any[]); +>args : any[] + + f(): number; +>f : () => number + + static f(): number; +>f : () => number +} + +declare const Mixed1: typeof M1 & typeof C1; +>Mixed1 : typeof M1 & typeof C1 +>M1 : typeof M1 +>C1 : typeof C1 + +declare const Mixed2: typeof C1 & typeof M1; +>Mixed2 : typeof C1 & typeof M1 +>C1 : typeof C1 +>M1 : typeof M1 + +declare const Mixed3: typeof M2 & typeof M1 & typeof C1; +>Mixed3 : typeof M2 & typeof M1 & typeof C1 +>M2 : typeof M2 +>M1 : typeof M1 +>C1 : typeof C1 + +declare const Mixed4: typeof C1 & typeof M1 & typeof M2; +>Mixed4 : typeof C1 & typeof M1 & typeof M2 +>C1 : typeof C1 +>M1 : typeof M1 +>M2 : typeof M2 + +declare const Mixed5: typeof M1 & typeof M2; +>Mixed5 : typeof M1 & typeof M2 +>M1 : typeof M1 +>M2 : typeof M2 + +function f1() { +>f1 : () => void + + let x1 = new Mixed1("hello"); +>x1 : M1 & C1 +>new Mixed1("hello") : M1 & C1 +>Mixed1 : typeof M1 & typeof C1 +>"hello" : "hello" + + let x2 = new Mixed1(42); +>x2 : M1 & C1 +>new Mixed1(42) : M1 & C1 +>Mixed1 : typeof M1 & typeof C1 +>42 : 42 + + let x3 = new Mixed2("hello"); +>x3 : C1 & M1 +>new Mixed2("hello") : C1 & M1 +>Mixed2 : typeof C1 & typeof M1 +>"hello" : "hello" + + let x4 = new Mixed2(42); +>x4 : C1 & M1 +>new Mixed2(42) : C1 & M1 +>Mixed2 : typeof C1 & typeof M1 +>42 : 42 + + let x5 = new Mixed3("hello"); +>x5 : M2 & M1 & C1 +>new Mixed3("hello") : M2 & M1 & C1 +>Mixed3 : typeof M2 & typeof M1 & typeof C1 +>"hello" : "hello" + + let x6 = new Mixed3(42); +>x6 : M2 & M1 & C1 +>new Mixed3(42) : M2 & M1 & C1 +>Mixed3 : typeof M2 & typeof M1 & typeof C1 +>42 : 42 + + let x7 = new Mixed4("hello"); +>x7 : C1 & M1 & M2 +>new Mixed4("hello") : C1 & M1 & M2 +>Mixed4 : typeof C1 & typeof M1 & typeof M2 +>"hello" : "hello" + + let x8 = new Mixed4(42); +>x8 : C1 & M1 & M2 +>new Mixed4(42) : C1 & M1 & M2 +>Mixed4 : typeof C1 & typeof M1 & typeof M2 +>42 : 42 + + let x9 = new Mixed5(); +>x9 : M1 & M2 +>new Mixed5() : M1 & M2 +>Mixed5 : typeof M1 & typeof M2 +} + +function f2() { +>f2 : () => void + + let x = new Mixed1("hello"); +>x : M1 & C1 +>new Mixed1("hello") : M1 & C1 +>Mixed1 : typeof M1 & typeof C1 +>"hello" : "hello" + + x.a; +>x.a : number +>x : M1 & C1 +>a : number + + x.p; +>x.p : number +>x : M1 & C1 +>p : number + + Mixed1.p; +>Mixed1.p : number +>Mixed1 : typeof M1 & typeof C1 +>p : number +} + +function f3() { +>f3 : () => void + + let x = new Mixed2("hello"); +>x : C1 & M1 +>new Mixed2("hello") : C1 & M1 +>Mixed2 : typeof C1 & typeof M1 +>"hello" : "hello" + + x.a; +>x.a : number +>x : C1 & M1 +>a : number + + x.p; +>x.p : number +>x : C1 & M1 +>p : number + + Mixed2.p; +>Mixed2.p : number +>Mixed2 : typeof C1 & typeof M1 +>p : number +} + +function f4() { +>f4 : () => void + + let x = new Mixed3("hello"); +>x : M2 & M1 & C1 +>new Mixed3("hello") : M2 & M1 & C1 +>Mixed3 : typeof M2 & typeof M1 & typeof C1 +>"hello" : "hello" + + x.a; +>x.a : number +>x : M2 & M1 & C1 +>a : number + + x.p; +>x.p : number +>x : M2 & M1 & C1 +>p : number + + x.f(); +>x.f() : number +>x.f : () => number +>x : M2 & M1 & C1 +>f : () => number + + Mixed3.p; +>Mixed3.p : number +>Mixed3 : typeof M2 & typeof M1 & typeof C1 +>p : number + + Mixed3.f(); +>Mixed3.f() : number +>Mixed3.f : () => number +>Mixed3 : typeof M2 & typeof M1 & typeof C1 +>f : () => number +} + +function f5() { +>f5 : () => void + + let x = new Mixed4("hello"); +>x : C1 & M1 & M2 +>new Mixed4("hello") : C1 & M1 & M2 +>Mixed4 : typeof C1 & typeof M1 & typeof M2 +>"hello" : "hello" + + x.a; +>x.a : number +>x : C1 & M1 & M2 +>a : number + + x.p; +>x.p : number +>x : C1 & M1 & M2 +>p : number + + x.f(); +>x.f() : number +>x.f : () => number +>x : C1 & M1 & M2 +>f : () => number + + Mixed4.p; +>Mixed4.p : number +>Mixed4 : typeof C1 & typeof M1 & typeof M2 +>p : number + + Mixed4.f(); +>Mixed4.f() : number +>Mixed4.f : () => number +>Mixed4 : typeof C1 & typeof M1 & typeof M2 +>f : () => number +} + +function f6() { +>f6 : () => void + + let x = new Mixed5(); +>x : M1 & M2 +>new Mixed5() : M1 & M2 +>Mixed5 : typeof M1 & typeof M2 + + x.p; +>x.p : number +>x : M1 & M2 +>p : number + + x.f(); +>x.f() : number +>x.f : () => number +>x : M1 & M2 +>f : () => number + + Mixed5.p; +>Mixed5.p : number +>Mixed5 : typeof M1 & typeof M2 +>p : number + + Mixed5.f(); +>Mixed5.f() : number +>Mixed5.f : () => number +>Mixed5 : typeof M1 & typeof M2 +>f : () => number +} + +class C2 extends Mixed1 { +>C2 : C2 +>Mixed1 : M1 & C1 + + constructor() { + super("hello"); +>super("hello") : void +>super : typeof M1 & typeof C1 +>"hello" : "hello" + + this.a; +>this.a : number +>this : this +>a : number + + this.b; +>this.b : number +>this : this +>b : number + + this.p; +>this.p : number +>this : this +>p : number + } +} + +class C3 extends Mixed3 { +>C3 : C3 +>Mixed3 : M2 & M1 & C1 + + constructor() { + super(42); +>super(42) : void +>super : typeof M2 & typeof M1 & typeof C1 +>42 : 42 + + this.a; +>this.a : number +>this : this +>a : number + + this.b; +>this.b : number +>this : this +>b : number + + this.p; +>this.p : number +>this : this +>p : number + + this.f(); +>this.f() : number +>this.f : () => number +>this : this +>f : () => number + } + f() { return super.f(); } +>f : () => number +>super.f() : number +>super.f : () => number +>super : M2 & M1 & C1 +>f : () => number +} + From 5282a8d0f5c1ddcf104eb44b36923f7f8d26a240 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Mon, 30 Jan 2017 08:55:32 -0800 Subject: [PATCH 21/31] ES6 target uses Object.assign for emitting spread Also omits the __assign helper --- src/compiler/transformers/esnext.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/compiler/transformers/esnext.ts b/src/compiler/transformers/esnext.ts index 0a529a8e871..ee92ec7c613 100644 --- a/src/compiler/transformers/esnext.ts +++ b/src/compiler/transformers/esnext.ts @@ -402,6 +402,11 @@ namespace ts { }; export function createAssignHelper(context: TransformationContext, attributesSegments: Expression[]) { + if (context.getCompilerOptions().target === ScriptTarget.ES2015) { + return createCall(createPropertyAccess(createIdentifier("Object"), "assign"), + /*typeArguments*/ undefined, + attributesSegments); + } context.requestEmitHelper(assignHelper); return createCall( getHelperName("__assign"), From a67a7493317d2ceff8037a0da5f1cebb356e2960 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Mon, 30 Jan 2017 10:42:11 -0800 Subject: [PATCH 22/31] Emit Object.assign for spread for targets >= ES6 --- src/compiler/transformers/esnext.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/transformers/esnext.ts b/src/compiler/transformers/esnext.ts index ee92ec7c613..8b5a095a17e 100644 --- a/src/compiler/transformers/esnext.ts +++ b/src/compiler/transformers/esnext.ts @@ -402,7 +402,7 @@ namespace ts { }; export function createAssignHelper(context: TransformationContext, attributesSegments: Expression[]) { - if (context.getCompilerOptions().target === ScriptTarget.ES2015) { + if (context.getCompilerOptions().target >= ScriptTarget.ES2015) { return createCall(createPropertyAccess(createIdentifier("Object"), "assign"), /*typeArguments*/ undefined, attributesSegments); From 46cdac1ec73a58fd8b0350e517d314395b9f0f0e Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Mon, 30 Jan 2017 10:42:39 -0800 Subject: [PATCH 23/31] Update baselines --- tests/baselines/reference/objectRest2.js | 10 +--------- tests/baselines/reference/objectRestForOf.js | 10 +--------- 2 files changed, 2 insertions(+), 18 deletions(-) diff --git a/tests/baselines/reference/objectRest2.js b/tests/baselines/reference/objectRest2.js index 6abff9e56a1..e46d22db80a 100644 --- a/tests/baselines/reference/objectRest2.js +++ b/tests/baselines/reference/objectRest2.js @@ -15,14 +15,6 @@ rootConnection('test'); //// [objectRest2.js] -var __assign = (this && this.__assign) || Object.assign || function(t) { - for (var s, i = 1, n = arguments.length; i < n; i++) { - s = arguments[i]; - for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) - t[p] = s[p]; - } - return t; -}; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } @@ -35,7 +27,7 @@ function rootConnection(name) { return { resolve: (context, args) => __awaiter(this, void 0, void 0, function* () { const { objects } = yield { objects: 12 }; - return __assign({}, connectionFromArray(objects, args)); + return Object.assign({}, connectionFromArray(objects, args)); }) }; } diff --git a/tests/baselines/reference/objectRestForOf.js b/tests/baselines/reference/objectRestForOf.js index fd81f77512e..f8a88fe2868 100644 --- a/tests/baselines/reference/objectRestForOf.js +++ b/tests/baselines/reference/objectRestForOf.js @@ -15,14 +15,6 @@ for (const norest of array.map(a => ({ ...a, x: 'a string' }))) { //// [objectRestForOf.js] -var __assign = (this && this.__assign) || Object.assign || function(t) { - for (var s, i = 1, n = arguments.length; i < n; i++) { - s = arguments[i]; - for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) - t[p] = s[p]; - } - return t; -}; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) @@ -43,6 +35,6 @@ for (let _b of array) { ({ x: xx } = _b, rrestOff = __rest(_b, ["x"])); [xx, rrestOff]; } -for (const norest of array.map(a => (__assign({}, a, { x: 'a string' })))) { +for (const norest of array.map(a => (Object.assign({}, a, { x: 'a string' })))) { [norest.x, norest.y]; } From cca68adf877f5c91fcb8dd00842e985cecfdaa63 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Mon, 30 Jan 2017 12:42:19 -0800 Subject: [PATCH 24/31] Import helpers skips __assign when target >= ES6 Instead, Object.assign is emitted. --- src/compiler/checker.ts | 2 +- tests/baselines/reference/importHelpersES6.js | 6 +++++- .../baselines/reference/importHelpersES6.symbols | 13 ++++++++----- tests/baselines/reference/importHelpersES6.types | 16 +++++++++++----- tests/cases/compiler/importHelpersES6.ts | 4 +++- 5 files changed, 28 insertions(+), 13 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 7d0f2cb0bbb..95dff83cd94 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -11721,7 +11721,7 @@ namespace ts { member = prop; } else if (memberDecl.kind === SyntaxKind.SpreadAssignment) { - if (languageVersion < ScriptTarget.ESNext) { + if (languageVersion < ScriptTarget.ES2015) { checkExternalEmitHelpers(memberDecl, ExternalEmitHelpers.Assign); } if (propertiesArray.length > 0) { diff --git a/tests/baselines/reference/importHelpersES6.js b/tests/baselines/reference/importHelpersES6.js index 209f3c34588..bc17939e2cc 100644 --- a/tests/baselines/reference/importHelpersES6.js +++ b/tests/baselines/reference/importHelpersES6.js @@ -5,10 +5,12 @@ declare var dec: any; @dec export class A { } + +const o = { a: 1 }; +const y = { ...o }; //// [tslib.d.ts] export declare function __extends(d: Function, b: Function): void; -export declare function __assign(t: any, ...sources: any[]): any; export declare function __decorate(decorators: Function[], target: any, key?: string | symbol, desc?: any): any; export declare function __param(paramIndex: number, decorator: Function): Function; export declare function __metadata(metadataKey: any, metadataValue: any): Function; @@ -23,3 +25,5 @@ A = tslib_1.__decorate([ dec ], A); export { A }; +const o = { a: 1 }; +const y = Object.assign({}, o); diff --git a/tests/baselines/reference/importHelpersES6.symbols b/tests/baselines/reference/importHelpersES6.symbols index 284a2251a6b..66a8b131aec 100644 --- a/tests/baselines/reference/importHelpersES6.symbols +++ b/tests/baselines/reference/importHelpersES6.symbols @@ -8,6 +8,14 @@ declare var dec: any; } +const o = { a: 1 }; +>o : Symbol(o, Decl(a.ts, 5, 5)) +>a : Symbol(a, Decl(a.ts, 5, 11)) + +const y = { ...o }; +>y : Symbol(y, Decl(a.ts, 6, 5)) +>o : Symbol(o, Decl(a.ts, 5, 5)) + === tests/cases/compiler/tslib.d.ts === export declare function __extends(d: Function, b: Function): void; >__extends : Symbol(__extends, Decl(tslib.d.ts, --, --)) @@ -16,11 +24,6 @@ export declare function __extends(d: Function, b: Function): void; >b : Symbol(b, Decl(tslib.d.ts, --, --)) >Function : Symbol(Function, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --)) -export declare function __assign(t: any, ...sources: any[]): any; ->__assign : Symbol(__assign, Decl(tslib.d.ts, --, --)) ->t : Symbol(t, Decl(tslib.d.ts, --, --)) ->sources : Symbol(sources, Decl(tslib.d.ts, --, --)) - export declare function __decorate(decorators: Function[], target: any, key?: string | symbol, desc?: any): any; >__decorate : Symbol(__decorate, Decl(tslib.d.ts, --, --)) >decorators : Symbol(decorators, Decl(tslib.d.ts, --, --)) diff --git a/tests/baselines/reference/importHelpersES6.types b/tests/baselines/reference/importHelpersES6.types index ebfd5a5a2a5..26f30e32e58 100644 --- a/tests/baselines/reference/importHelpersES6.types +++ b/tests/baselines/reference/importHelpersES6.types @@ -8,6 +8,17 @@ declare var dec: any; } +const o = { a: 1 }; +>o : { a: number; } +>{ a: 1 } : { a: number; } +>a : number +>1 : 1 + +const y = { ...o }; +>y : { a: number; } +>{ ...o } : { a: number; } +>o : { a: number; } + === tests/cases/compiler/tslib.d.ts === export declare function __extends(d: Function, b: Function): void; >__extends : (d: Function, b: Function) => void @@ -16,11 +27,6 @@ export declare function __extends(d: Function, b: Function): void; >b : Function >Function : Function -export declare function __assign(t: any, ...sources: any[]): any; ->__assign : (t: any, ...sources: any[]) => any ->t : any ->sources : any[] - export declare function __decorate(decorators: Function[], target: any, key?: string | symbol, desc?: any): any; >__decorate : (decorators: Function[], target: any, key?: string | symbol, desc?: any) => any >decorators : Function[] diff --git a/tests/cases/compiler/importHelpersES6.ts b/tests/cases/compiler/importHelpersES6.ts index 9be680a88d6..0267156cf7e 100644 --- a/tests/cases/compiler/importHelpersES6.ts +++ b/tests/cases/compiler/importHelpersES6.ts @@ -7,9 +7,11 @@ declare var dec: any; } +const o = { a: 1 }; +const y = { ...o }; + // @filename: tslib.d.ts export declare function __extends(d: Function, b: Function): void; -export declare function __assign(t: any, ...sources: any[]): any; export declare function __decorate(decorators: Function[], target: any, key?: string | symbol, desc?: any): any; export declare function __param(paramIndex: number, decorator: Function): Function; export declare function __metadata(metadataKey: any, metadataValue: any): Function; From 9b6f9305d99ed4424ac473f85955a9b10314d805 Mon Sep 17 00:00:00 2001 From: Vladimir Matveev Date: Mon, 30 Jan 2017 13:19:44 -0800 Subject: [PATCH 25/31] support untyped imports in shims (#13758) --- src/services/shims.ts | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/services/shims.ts b/src/services/shims.ts index cf6bceb816c..487947ee458 100644 --- a/src/services/shims.ts +++ b/src/services/shims.ts @@ -385,7 +385,10 @@ namespace ts { if (settingsJson == null || settingsJson == "") { throw Error("LanguageServiceShimHostAdapter.getCompilationSettings: empty compilationSettings"); } - return JSON.parse(settingsJson); + const compilerOptions = JSON.parse(settingsJson); + // permit language service to handle all files (filtering should be performed on the host side) + compilerOptions.allowNonTsExtensions = true; + return compilerOptions; } public getScriptFileNames(): string[] { @@ -1061,12 +1064,6 @@ namespace ts { const compilerOptions = JSON.parse(compilerOptionsJson); const result = resolveModuleName(moduleName, normalizeSlashes(fileName), compilerOptions, this.host); const resolvedFileName = result.resolvedModule ? result.resolvedModule.resolvedFileName : undefined; - if (resolvedFileName && !compilerOptions.allowJs && fileExtensionIs(resolvedFileName, ".js")) { - return { - resolvedFileName: undefined, - failedLookupLocations: [] - }; - } return { resolvedFileName, failedLookupLocations: result.failedLookupLocations From 16103a9ca2ba1d23e6e781fb16da83950eb073b8 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Mon, 30 Jan 2017 15:40:31 -0800 Subject: [PATCH 26/31] Fix isSymbolInScopeOfMappedTypeParameter to include mapped types --- src/compiler/checker.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index c2e0c66ce3f..780bda03b0a 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -6864,6 +6864,11 @@ namespace ts { } } break; + case SyntaxKind.MappedType: + if (contains(mappedTypes, getDeclaredTypeOfTypeParameter(getSymbolOfNode((node).typeParameter)))) { + return true; + } + break; case SyntaxKind.JSDocFunctionType: const func = node as JSDocFunctionType; for (const p of func.parameters) { From 34b4dbb26000d726600c4565ccb75cbc4eb53d06 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Mon, 30 Jan 2017 15:47:18 -0800 Subject: [PATCH 27/31] Add regression test --- .../conformance/types/keyof/keyofAndIndexedAccess.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/cases/conformance/types/keyof/keyofAndIndexedAccess.ts b/tests/cases/conformance/types/keyof/keyofAndIndexedAccess.ts index 04c26fbe8f7..a8a5acd450f 100644 --- a/tests/cases/conformance/types/keyof/keyofAndIndexedAccess.ts +++ b/tests/cases/conformance/types/keyof/keyofAndIndexedAccess.ts @@ -517,3 +517,13 @@ class B extends A<{ x: number}> { p.x; } } + +// Repro from #13749 + +class Form { + private childFormFactories: {[K in keyof T]: (v: T[K]) => Form} + + public set(prop: K, value: T[K]) { + this.childFormFactories[prop](value) + } +} From ebba3d6ba672458aa9f98ceef665e153c570505c Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Mon, 30 Jan 2017 15:47:24 -0800 Subject: [PATCH 28/31] Accept new baselines --- .../reference/keyofAndIndexedAccess.js | 23 +++++++++++ .../reference/keyofAndIndexedAccess.symbols | 36 ++++++++++++++++++ .../reference/keyofAndIndexedAccess.types | 38 +++++++++++++++++++ 3 files changed, 97 insertions(+) diff --git a/tests/baselines/reference/keyofAndIndexedAccess.js b/tests/baselines/reference/keyofAndIndexedAccess.js index e7df8bbd5a1..62b848e97df 100644 --- a/tests/baselines/reference/keyofAndIndexedAccess.js +++ b/tests/baselines/reference/keyofAndIndexedAccess.js @@ -516,6 +516,16 @@ class B extends A<{ x: number}> { p.x; } } + +// Repro from #13749 + +class Form { + private childFormFactories: {[K in keyof T]: (v: T[K]) => Form} + + public set(prop: K, value: T[K]) { + this.childFormFactories[prop](value) + } +} //// [keyofAndIndexedAccess.js] @@ -862,6 +872,15 @@ var B = (function (_super) { }; return B; }(A)); +// Repro from #13749 +var Form = (function () { + function Form() { + } + Form.prototype.set = function (prop, value) { + this.childFormFactories[prop](value); + }; + return Form; +}()); //// [keyofAndIndexedAccess.d.ts] @@ -1104,3 +1123,7 @@ declare class B extends A<{ }> { f(p: this["props"]): void; } +declare class Form { + private childFormFactories; + set(prop: K, value: T[K]): void; +} diff --git a/tests/baselines/reference/keyofAndIndexedAccess.symbols b/tests/baselines/reference/keyofAndIndexedAccess.symbols index 01501cd719c..54fff19f428 100644 --- a/tests/baselines/reference/keyofAndIndexedAccess.symbols +++ b/tests/baselines/reference/keyofAndIndexedAccess.symbols @@ -1844,3 +1844,39 @@ class B extends A<{ x: number}> { } } +// Repro from #13749 + +class Form { +>Form : Symbol(Form, Decl(keyofAndIndexedAccess.ts, 516, 1)) +>T : Symbol(T, Decl(keyofAndIndexedAccess.ts, 520, 11)) + + private childFormFactories: {[K in keyof T]: (v: T[K]) => Form} +>childFormFactories : Symbol(Form.childFormFactories, Decl(keyofAndIndexedAccess.ts, 520, 15)) +>K : Symbol(K, Decl(keyofAndIndexedAccess.ts, 521, 34)) +>T : Symbol(T, Decl(keyofAndIndexedAccess.ts, 520, 11)) +>v : Symbol(v, Decl(keyofAndIndexedAccess.ts, 521, 50)) +>T : Symbol(T, Decl(keyofAndIndexedAccess.ts, 520, 11)) +>K : Symbol(K, Decl(keyofAndIndexedAccess.ts, 521, 34)) +>Form : Symbol(Form, Decl(keyofAndIndexedAccess.ts, 516, 1)) +>T : Symbol(T, Decl(keyofAndIndexedAccess.ts, 520, 11)) +>K : Symbol(K, Decl(keyofAndIndexedAccess.ts, 521, 34)) + + public set(prop: K, value: T[K]) { +>set : Symbol(Form.set, Decl(keyofAndIndexedAccess.ts, 521, 73)) +>K : Symbol(K, Decl(keyofAndIndexedAccess.ts, 523, 15)) +>T : Symbol(T, Decl(keyofAndIndexedAccess.ts, 520, 11)) +>prop : Symbol(prop, Decl(keyofAndIndexedAccess.ts, 523, 34)) +>K : Symbol(K, Decl(keyofAndIndexedAccess.ts, 523, 15)) +>value : Symbol(value, Decl(keyofAndIndexedAccess.ts, 523, 42)) +>T : Symbol(T, Decl(keyofAndIndexedAccess.ts, 520, 11)) +>K : Symbol(K, Decl(keyofAndIndexedAccess.ts, 523, 15)) + + this.childFormFactories[prop](value) +>this.childFormFactories : Symbol(Form.childFormFactories, Decl(keyofAndIndexedAccess.ts, 520, 15)) +>this : Symbol(Form, Decl(keyofAndIndexedAccess.ts, 516, 1)) +>childFormFactories : Symbol(Form.childFormFactories, Decl(keyofAndIndexedAccess.ts, 520, 15)) +>prop : Symbol(prop, Decl(keyofAndIndexedAccess.ts, 523, 34)) +>value : Symbol(value, Decl(keyofAndIndexedAccess.ts, 523, 42)) + } +} + diff --git a/tests/baselines/reference/keyofAndIndexedAccess.types b/tests/baselines/reference/keyofAndIndexedAccess.types index 21c9d645ce4..e987ee9043b 100644 --- a/tests/baselines/reference/keyofAndIndexedAccess.types +++ b/tests/baselines/reference/keyofAndIndexedAccess.types @@ -2166,3 +2166,41 @@ class B extends A<{ x: number}> { } } +// Repro from #13749 + +class Form { +>Form : Form +>T : T + + private childFormFactories: {[K in keyof T]: (v: T[K]) => Form} +>childFormFactories : { [K in keyof T]: (v: T[K]) => Form; } +>K : K +>T : T +>v : T[K] +>T : T +>K : K +>Form : Form +>T : T +>K : K + + public set(prop: K, value: T[K]) { +>set : (prop: K, value: T[K]) => void +>K : K +>T : T +>prop : K +>K : K +>value : T[K] +>T : T +>K : K + + this.childFormFactories[prop](value) +>this.childFormFactories[prop](value) : Form +>this.childFormFactories[prop] : (v: T[K]) => Form +>this.childFormFactories : { [K in keyof T]: (v: T[K]) => Form; } +>this : this +>childFormFactories : { [K in keyof T]: (v: T[K]) => Form; } +>prop : K +>value : T[K] + } +} + From 7e7057a89d6780f0b093fef4be26c4ddeb4c62ef Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Mon, 30 Jan 2017 16:53:31 -0800 Subject: [PATCH 29/31] Empty object type assignable to mapped type with optional properties --- src/compiler/checker.ts | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 780bda03b0a..26c27cb3be2 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -7687,7 +7687,7 @@ namespace ts { function isKnownProperty(type: Type, name: string): boolean { if (type.flags & TypeFlags.Object) { const resolved = resolveStructuredTypeMembers(type); - if ((relation === assignableRelation || relation === comparableRelation) && (type === globalObjectType || isEmptyObjectType(resolved)) || + if ((relation === assignableRelation || relation === comparableRelation) && (type === globalObjectType || isEmptyResolvedType(resolved)) || resolved.stringIndexInfo || (resolved.numberIndexInfo && isNumericLiteralName(name)) || getPropertyOfType(type, name)) { @@ -7704,7 +7704,7 @@ namespace ts { return false; } - function isEmptyObjectType(t: ResolvedType) { + function isEmptyResolvedType(t: ResolvedType) { return t.properties.length === 0 && t.callSignatures.length === 0 && t.constructSignatures.length === 0 && @@ -7712,6 +7712,10 @@ namespace ts { !t.numberIndexInfo; } + function isEmptyObjectType(type: Type) { + return type.flags & TypeFlags.Object && isEmptyResolvedType(resolveStructuredTypeMembers(type)); + } + function hasExcessProperties(source: FreshObjectLiteralType, target: Type, reportErrors: boolean): boolean { if (maybeTypeOfKind(target, TypeFlags.Object) && !(getObjectFlags(target) & ObjectFlags.ObjectLiteralPatternWithComputedProperties)) { for (const prop of getPropertiesOfObjectType(source)) { @@ -7925,10 +7929,14 @@ namespace ts { } } } + else if ((target).declaration.questionToken && isEmptyObjectType(source)) { + return Ternary.True; + + } } else if (relation !== identityRelation) { const resolved = resolveStructuredTypeMembers(target); - if (isEmptyObjectType(resolved) || resolved.stringIndexInfo && resolved.stringIndexInfo.type.flags & TypeFlags.Any) { + if (isEmptyResolvedType(resolved) || resolved.stringIndexInfo && resolved.stringIndexInfo.type.flags & TypeFlags.Any) { return Ternary.True; } } From 647e1836c3a8898d85b5c5a9371cd9bb98eec2b1 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Mon, 30 Jan 2017 16:53:43 -0800 Subject: [PATCH 30/31] Add tests --- .../types/mapped/mappedTypesAndObjects.ts | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/tests/cases/conformance/types/mapped/mappedTypesAndObjects.ts b/tests/cases/conformance/types/mapped/mappedTypesAndObjects.ts index 8023f6e7d8d..b3530b46965 100644 --- a/tests/cases/conformance/types/mapped/mappedTypesAndObjects.ts +++ b/tests/cases/conformance/types/mapped/mappedTypesAndObjects.ts @@ -13,23 +13,33 @@ function f2(x: Partial, y: Readonly) { obj = y; } +function f3(x: Partial) { + x = {}; +} + // Repro from #12900 interface Base { - foo: { [key: string]: any }; - bar: any; - baz: any; + foo: { [key: string]: any }; + bar: any; + baz: any; } interface E1 extends Base { - foo: T; + foo: T; } interface Something { name: string, value: string }; interface E2 extends Base { - foo: Partial; // or other mapped type + foo: Partial; // or other mapped type } interface E3 extends Base { - foo: Partial; // or other mapped type -} \ No newline at end of file + foo: Partial; // or other mapped type +} + +// Repro from #13747 + +class Form { + private values: {[P in keyof T]?: T[P]} = {} +} From 86591016186b41fc8b9db73fbbe8bdd3036c638d Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Mon, 30 Jan 2017 16:57:11 -0800 Subject: [PATCH 31/31] Accept new baselines --- .../reference/mappedTypesAndObjects.js | 39 +++++++-- .../reference/mappedTypesAndObjects.symbols | 82 ++++++++++++------- .../reference/mappedTypesAndObjects.types | 41 ++++++++-- 3 files changed, 121 insertions(+), 41 deletions(-) diff --git a/tests/baselines/reference/mappedTypesAndObjects.js b/tests/baselines/reference/mappedTypesAndObjects.js index 53b39209693..f320cc9624e 100644 --- a/tests/baselines/reference/mappedTypesAndObjects.js +++ b/tests/baselines/reference/mappedTypesAndObjects.js @@ -12,26 +12,37 @@ function f2(x: Partial, y: Readonly) { obj = y; } +function f3(x: Partial) { + x = {}; +} + // Repro from #12900 interface Base { - foo: { [key: string]: any }; - bar: any; - baz: any; + foo: { [key: string]: any }; + bar: any; + baz: any; } interface E1 extends Base { - foo: T; + foo: T; } interface Something { name: string, value: string }; interface E2 extends Base { - foo: Partial; // or other mapped type + foo: Partial; // or other mapped type } interface E3 extends Base { - foo: Partial; // or other mapped type -} + foo: Partial; // or other mapped type +} + +// Repro from #13747 + +class Form { + private values: {[P in keyof T]?: T[P]} = {} +} + //// [mappedTypesAndObjects.js] function f1(x, y) { @@ -44,12 +55,23 @@ function f2(x, y) { obj = x; obj = y; } +function f3(x) { + x = {}; +} ; +// Repro from #13747 +var Form = (function () { + function Form() { + this.values = {}; + } + return Form; +}()); //// [mappedTypesAndObjects.d.ts] declare function f1(x: Partial, y: Readonly): void; declare function f2(x: Partial, y: Readonly): void; +declare function f3(x: Partial): void; interface Base { foo: { [key: string]: any; @@ -70,3 +92,6 @@ interface E2 extends Base { interface E3 extends Base { foo: Partial; } +declare class Form { + private values; +} diff --git a/tests/baselines/reference/mappedTypesAndObjects.symbols b/tests/baselines/reference/mappedTypesAndObjects.symbols index 1690f1b6b04..6fb5d720fb3 100644 --- a/tests/baselines/reference/mappedTypesAndObjects.symbols +++ b/tests/baselines/reference/mappedTypesAndObjects.symbols @@ -45,54 +45,80 @@ function f2(x: Partial, y: Readonly) { >y : Symbol(y, Decl(mappedTypesAndObjects.ts, 7, 29)) } +function f3(x: Partial) { +>f3 : Symbol(f3, Decl(mappedTypesAndObjects.ts, 11, 1)) +>T : Symbol(T, Decl(mappedTypesAndObjects.ts, 13, 12)) +>x : Symbol(x, Decl(mappedTypesAndObjects.ts, 13, 15)) +>Partial : Symbol(Partial, Decl(lib.d.ts, --, --)) +>T : Symbol(T, Decl(mappedTypesAndObjects.ts, 13, 12)) + + x = {}; +>x : Symbol(x, Decl(mappedTypesAndObjects.ts, 13, 15)) +} + // Repro from #12900 interface Base { ->Base : Symbol(Base, Decl(mappedTypesAndObjects.ts, 11, 1)) +>Base : Symbol(Base, Decl(mappedTypesAndObjects.ts, 15, 1)) - foo: { [key: string]: any }; ->foo : Symbol(Base.foo, Decl(mappedTypesAndObjects.ts, 15, 16)) ->key : Symbol(key, Decl(mappedTypesAndObjects.ts, 16, 11)) + foo: { [key: string]: any }; +>foo : Symbol(Base.foo, Decl(mappedTypesAndObjects.ts, 19, 16)) +>key : Symbol(key, Decl(mappedTypesAndObjects.ts, 20, 12)) - bar: any; ->bar : Symbol(Base.bar, Decl(mappedTypesAndObjects.ts, 16, 31)) + bar: any; +>bar : Symbol(Base.bar, Decl(mappedTypesAndObjects.ts, 20, 32)) - baz: any; ->baz : Symbol(Base.baz, Decl(mappedTypesAndObjects.ts, 17, 12)) + baz: any; +>baz : Symbol(Base.baz, Decl(mappedTypesAndObjects.ts, 21, 13)) } interface E1 extends Base { ->E1 : Symbol(E1, Decl(mappedTypesAndObjects.ts, 19, 1)) ->T : Symbol(T, Decl(mappedTypesAndObjects.ts, 21, 13)) ->Base : Symbol(Base, Decl(mappedTypesAndObjects.ts, 11, 1)) +>E1 : Symbol(E1, Decl(mappedTypesAndObjects.ts, 23, 1)) +>T : Symbol(T, Decl(mappedTypesAndObjects.ts, 25, 13)) +>Base : Symbol(Base, Decl(mappedTypesAndObjects.ts, 15, 1)) - foo: T; ->foo : Symbol(E1.foo, Decl(mappedTypesAndObjects.ts, 21, 30)) ->T : Symbol(T, Decl(mappedTypesAndObjects.ts, 21, 13)) + foo: T; +>foo : Symbol(E1.foo, Decl(mappedTypesAndObjects.ts, 25, 30)) +>T : Symbol(T, Decl(mappedTypesAndObjects.ts, 25, 13)) } interface Something { name: string, value: string }; ->Something : Symbol(Something, Decl(mappedTypesAndObjects.ts, 23, 1)) ->name : Symbol(Something.name, Decl(mappedTypesAndObjects.ts, 25, 21)) ->value : Symbol(Something.value, Decl(mappedTypesAndObjects.ts, 25, 35)) +>Something : Symbol(Something, Decl(mappedTypesAndObjects.ts, 27, 1)) +>name : Symbol(Something.name, Decl(mappedTypesAndObjects.ts, 29, 21)) +>value : Symbol(Something.value, Decl(mappedTypesAndObjects.ts, 29, 35)) interface E2 extends Base { ->E2 : Symbol(E2, Decl(mappedTypesAndObjects.ts, 25, 52)) ->Base : Symbol(Base, Decl(mappedTypesAndObjects.ts, 11, 1)) +>E2 : Symbol(E2, Decl(mappedTypesAndObjects.ts, 29, 52)) +>Base : Symbol(Base, Decl(mappedTypesAndObjects.ts, 15, 1)) - foo: Partial; // or other mapped type ->foo : Symbol(E2.foo, Decl(mappedTypesAndObjects.ts, 26, 27)) + foo: Partial; // or other mapped type +>foo : Symbol(E2.foo, Decl(mappedTypesAndObjects.ts, 30, 27)) >Partial : Symbol(Partial, Decl(lib.d.ts, --, --)) ->Something : Symbol(Something, Decl(mappedTypesAndObjects.ts, 23, 1)) +>Something : Symbol(Something, Decl(mappedTypesAndObjects.ts, 27, 1)) } interface E3 extends Base { ->E3 : Symbol(E3, Decl(mappedTypesAndObjects.ts, 28, 1)) ->T : Symbol(T, Decl(mappedTypesAndObjects.ts, 30, 13)) ->Base : Symbol(Base, Decl(mappedTypesAndObjects.ts, 11, 1)) +>E3 : Symbol(E3, Decl(mappedTypesAndObjects.ts, 32, 1)) +>T : Symbol(T, Decl(mappedTypesAndObjects.ts, 34, 13)) +>Base : Symbol(Base, Decl(mappedTypesAndObjects.ts, 15, 1)) - foo: Partial; // or other mapped type ->foo : Symbol(E3.foo, Decl(mappedTypesAndObjects.ts, 30, 30)) + foo: Partial; // or other mapped type +>foo : Symbol(E3.foo, Decl(mappedTypesAndObjects.ts, 34, 30)) >Partial : Symbol(Partial, Decl(lib.d.ts, --, --)) ->T : Symbol(T, Decl(mappedTypesAndObjects.ts, 30, 13)) +>T : Symbol(T, Decl(mappedTypesAndObjects.ts, 34, 13)) } + +// Repro from #13747 + +class Form { +>Form : Symbol(Form, Decl(mappedTypesAndObjects.ts, 36, 1)) +>T : Symbol(T, Decl(mappedTypesAndObjects.ts, 40, 11)) + + private values: {[P in keyof T]?: T[P]} = {} +>values : Symbol(Form.values, Decl(mappedTypesAndObjects.ts, 40, 15)) +>P : Symbol(P, Decl(mappedTypesAndObjects.ts, 41, 22)) +>T : Symbol(T, Decl(mappedTypesAndObjects.ts, 40, 11)) +>T : Symbol(T, Decl(mappedTypesAndObjects.ts, 40, 11)) +>P : Symbol(P, Decl(mappedTypesAndObjects.ts, 41, 22)) +} + diff --git a/tests/baselines/reference/mappedTypesAndObjects.types b/tests/baselines/reference/mappedTypesAndObjects.types index e6f8e53f558..75f71082f31 100644 --- a/tests/baselines/reference/mappedTypesAndObjects.types +++ b/tests/baselines/reference/mappedTypesAndObjects.types @@ -49,19 +49,32 @@ function f2(x: Partial, y: Readonly) { >y : Readonly } +function f3(x: Partial) { +>f3 : (x: Partial) => void +>T : T +>x : Partial +>Partial : Partial +>T : T + + x = {}; +>x = {} : {} +>x : Partial +>{} : {} +} + // Repro from #12900 interface Base { >Base : Base - foo: { [key: string]: any }; + foo: { [key: string]: any }; >foo : { [key: string]: any; } >key : string - bar: any; + bar: any; >bar : any - baz: any; + baz: any; >baz : any } @@ -70,7 +83,7 @@ interface E1 extends Base { >T : T >Base : Base - foo: T; + foo: T; >foo : T >T : T } @@ -84,7 +97,7 @@ interface E2 extends Base { >E2 : E2 >Base : Base - foo: Partial; // or other mapped type + foo: Partial; // or other mapped type >foo : Partial >Partial : Partial >Something : Something @@ -95,8 +108,24 @@ interface E3 extends Base { >T : T >Base : Base - foo: Partial; // or other mapped type + foo: Partial; // or other mapped type >foo : Partial >Partial : Partial >T : T } + +// Repro from #13747 + +class Form { +>Form : Form +>T : T + + private values: {[P in keyof T]?: T[P]} = {} +>values : { [P in keyof T]?: T[P] | undefined; } +>P : P +>T : T +>T : T +>P : P +>{} : {} +} +