From 4d3cff1e5a6f34e967bcda1ee1cf002295b8f9e3 Mon Sep 17 00:00:00 2001 From: zhengbli Date: Fri, 11 Mar 2016 15:56:36 -0800 Subject: [PATCH 01/56] Add upper limit for the program size, fix readDirectory for the symlink files --- src/compiler/commandLineParser.ts | 2 +- src/compiler/diagnosticMessages.json | 4 ++++ src/compiler/program.ts | 16 ++++++++++++- src/compiler/sys.ts | 12 +++++----- src/compiler/utilities.ts | 6 +++++ src/server/editorServices.ts | 36 ++++++++++++++++++++++++---- 6 files changed, 63 insertions(+), 13 deletions(-) diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index cf55f030c33..a11d06a6445 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -566,7 +566,7 @@ namespace ts { } else { // by default exclude node_modules, and any specificied output directory - exclude = ["node_modules"]; + exclude = ["node_modules", "bower_components"]; const outDir = json["compilerOptions"] && json["compilerOptions"]["outDir"]; if (outDir) { exclude.push(outDir); diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 15a3a4e5ef4..b5208e3aad4 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -2824,5 +2824,9 @@ "Unknown typing option '{0}'.": { "category": "Error", "code": 17010 + }, + "Too many javascript files in the project. Consider add to the `exclude` list in the config file.": { + "category": "Error", + "code": 17012 } } diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 495fbde5a99..406eeb6cd91 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -748,7 +748,21 @@ namespace ts { } if (!tryReuseStructureFromOldProgram()) { - forEach(rootNames, name => processRootFile(name, /*isDefaultLib*/ false)); + let programSize = 0; + for (const name of rootNames) { + const path = toPath(name, currentDirectory, getCanonicalFileName); + if (programSize <= maxProgramSize) { + processRootFile(name, /*isDefaultLib*/ false); + if (!hasTypeScriptFileExtension(name) && filesByName.get(path)) { + programSize += filesByName.get(path).text.length; + } + } + else { + programDiagnostics.add(createCompilerDiagnostic(Diagnostics.Too_many_javascript_files_in_the_project_Consider_add_to_the_exclude_list_in_the_config_file)); + break; + } + } + // Do not process the default library if: // - The '--noLib' flag is used. // - A 'no-default-lib' reference comment is encountered in diff --git a/src/compiler/sys.ts b/src/compiler/sys.ts index 4cf94655b35..6d7847ab588 100644 --- a/src/compiler/sys.ts +++ b/src/compiler/sys.ts @@ -74,7 +74,7 @@ namespace ts { watchDirectory?(path: string, callback: DirectoryWatcherCallback, recursive?: boolean): FileWatcher; }; - export var sys: System = (function () { + export var sys: System = (function() { function getWScriptSystem(): System { @@ -404,8 +404,8 @@ namespace ts { const watchedFileSet = createWatchedFileSet(); function isNode4OrLater(): boolean { - return parseInt(process.version.charAt(1)) >= 4; - } + return parseInt(process.version.charAt(1)) >= 4; + } const platform: string = _os.platform(); // win32\win64 are case insensitive platforms, MacOS (darwin) by default is also case insensitive @@ -500,7 +500,7 @@ namespace ts { for (const current of files) { const name = combinePaths(path, current); if (!contains(exclude, getCanonicalPath(name))) { - const stat = _fs.statSync(name); + const stat = _fs.lstatSync(name); if (stat.isFile()) { if (!extension || fileExtensionIs(name, extension)) { result.push(name); @@ -532,7 +532,7 @@ namespace ts { // and https://github.com/Microsoft/TypeScript/issues/4643), therefore // if the current node.js version is newer than 4, use `fs.watch` instead. const watchSet = isNode4OrLater() ? watchedFileSet : pollingWatchedFileSet; - const watchedFile = watchSet.addFile(filePath, callback); + const watchedFile = watchSet.addFile(filePath, callback); return { close: () => watchSet.removeFile(watchedFile) }; @@ -562,7 +562,7 @@ namespace ts { } ); }, - resolvePath: function (path: string): string { + resolvePath: function(path: string): string { return _path.resolve(path); }, fileExists, diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index de10d469c87..72ad016f596 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -2484,6 +2484,10 @@ namespace ts { return forEach(supportedJavascriptExtensions, extension => fileExtensionIs(fileName, extension)); } + export function hasTypeScriptFileExtension(fileName: string) { + return forEach(supportedTypeScriptExtensions, extension => fileExtensionIs(fileName, extension)); + } + /** * Replace each instance of non-ascii characters by one, two, three, or four escape sequences * representing the UTF-8 encoding of the character, and return the expanded char code list. @@ -2866,4 +2870,6 @@ namespace ts { export function isParameterPropertyDeclaration(node: ParameterDeclaration): boolean { return node.flags & NodeFlags.AccessibilityModifier && node.parent.kind === SyntaxKind.Constructor && isClassLike(node.parent.parent); } + + export const maxProgramSize = 35 * 1024 * 1024; } diff --git a/src/server/editorServices.ts b/src/server/editorServices.ts index 71907735b91..26b40b3d908 100644 --- a/src/server/editorServices.ts +++ b/src/server/editorServices.ts @@ -1217,13 +1217,35 @@ namespace ts.server { } else { const project = this.createProject(configFilename, projectOptions); + let programSize = 0; + + // As the project openning might not be complete if there are too many files, + // therefore to surface the diagnostics we need to make sure the given client file is opened. + if (clientFileName) { + const currentClientFileInfo = this.openFile(clientFileName, /*openedByClient*/ true); + project.addRoot(currentClientFileInfo); + programSize += currentClientFileInfo.content.length; + } + for (const rootFilename of projectOptions.files) { - if (this.host.fileExists(rootFilename)) { - const info = this.openFile(rootFilename, /*openedByClient*/ clientFileName == rootFilename); - project.addRoot(info); + if (rootFilename === clientFileName) { + continue; + } + + if (programSize <= maxProgramSize) { + if (this.host.fileExists(rootFilename)) { + const info = this.openFile(rootFilename, /*openedByClient*/ false); + project.addRoot(info); + if (!hasTypeScriptFileExtension(rootFilename)) { + programSize += info.content.length; + } + } + else { + return { errorMsg: "specified file " + rootFilename + " not found" }; + } } else { - return { errorMsg: "specified file " + rootFilename + " not found" }; + break; } } project.finishGraph(); @@ -1251,11 +1273,15 @@ namespace ts.server { return error; } else { - const oldFileNames = project.compilerService.host.roots.map(info => info.fileName); + const oldFileNames = project.projectOptions ? project.projectOptions.files : project.compilerService.host.roots.map(info => info.fileName); const newFileNames = projectOptions.files; const fileNamesToRemove = oldFileNames.filter(f => newFileNames.indexOf(f) < 0); const fileNamesToAdd = newFileNames.filter(f => oldFileNames.indexOf(f) < 0); + if (fileNamesToAdd.length === 0 && fileNamesToRemove.length === 0) { + return; + } + for (const fileName of fileNamesToRemove) { const info = this.getScriptInfo(fileName); if (info) { From b155fa847a6dd9d069bf6cb2e8e7dcebb16cda08 Mon Sep 17 00:00:00 2001 From: zhengbli Date: Fri, 11 Mar 2016 16:18:40 -0800 Subject: [PATCH 02/56] Add comments --- src/compiler/sys.ts | 3 +++ src/server/editorServices.ts | 7 +++---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/compiler/sys.ts b/src/compiler/sys.ts index 6d7847ab588..c70985cd018 100644 --- a/src/compiler/sys.ts +++ b/src/compiler/sys.ts @@ -500,6 +500,9 @@ namespace ts { for (const current of files) { const name = combinePaths(path, current); if (!contains(exclude, getCanonicalPath(name))) { + // fs.statSync would throw an exception if the file is a symlink + // whose linked file doesn't exist. fs.lstatSync would return a stat + // object for the symlink file itself in this case const stat = _fs.lstatSync(name); if (stat.isFile()) { if (!extension || fileExtensionIs(name, extension)) { diff --git a/src/server/editorServices.ts b/src/server/editorServices.ts index 26b40b3d908..4b49c2944cd 100644 --- a/src/server/editorServices.ts +++ b/src/server/editorServices.ts @@ -1273,15 +1273,14 @@ namespace ts.server { return error; } else { + // if the project is too large, the root files might not have been all loaded if the total + // program size reached the upper limit. In that case project.projectOptions.files should + // be more precise. However this would only happen for configured project. const oldFileNames = project.projectOptions ? project.projectOptions.files : project.compilerService.host.roots.map(info => info.fileName); const newFileNames = projectOptions.files; const fileNamesToRemove = oldFileNames.filter(f => newFileNames.indexOf(f) < 0); const fileNamesToAdd = newFileNames.filter(f => oldFileNames.indexOf(f) < 0); - if (fileNamesToAdd.length === 0 && fileNamesToRemove.length === 0) { - return; - } - for (const fileName of fileNamesToRemove) { const info = this.getScriptInfo(fileName); if (info) { From a3aa0002a3a88b3225ef97f0ff4aacf748b53827 Mon Sep 17 00:00:00 2001 From: zhengbli Date: Mon, 14 Mar 2016 14:51:24 -0700 Subject: [PATCH 03/56] CR feedback / Change upper limit / Add disableSizeLimit compiler option --- src/compiler/commandLineParser.ts | 5 ++++ src/compiler/diagnosticMessages.json | 7 +++-- src/compiler/program.ts | 38 +++++++++++++++++++--------- src/compiler/sys.ts | 20 ++++++++------- src/compiler/types.ts | 1 + src/compiler/utilities.ts | 2 +- src/server/editorServices.ts | 23 ++++++++++++----- 7 files changed, 65 insertions(+), 31 deletions(-) diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index a11d06a6445..72deb16f0c1 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -332,6 +332,11 @@ namespace ts { name: "noImplicitUseStrict", type: "boolean", description: Diagnostics.Do_not_emit_use_strict_directives_in_module_output + }, + { + name: "disableSizeLimit", + type: "boolean", + description: Diagnostics.Disable_the_upper_limit_for_the_total_file_size_of_a_project } ]; diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index b5208e3aad4..d96b10f61ad 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -2620,7 +2620,10 @@ "category": "Message", "code": 6112 }, - + "Disable the upper limit for the total file size of a project.": { + "category": "Message", + "code": 6113 + }, "Variable '{0}' implicitly has an '{1}' type.": { "category": "Error", "code": 7005 @@ -2825,7 +2828,7 @@ "category": "Error", "code": 17010 }, - "Too many javascript files in the project. Consider add to the `exclude` list in the config file.": { + "Too many JavaScript files in the project. Use an exact 'files' list, or use the 'exclude' setting in project configuration to limit included source folders. The likely folder to exclude is '{0}'. To disable the project size limit, set the 'disableSizeLimit' compiler option to 'true'": { "category": "Error", "code": 17012 } diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 406eeb6cd91..cdb4d3b417c 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -742,24 +742,38 @@ namespace ts { (oldOptions.target !== options.target) || (oldOptions.noLib !== options.noLib) || (oldOptions.jsx !== options.jsx) || - (oldOptions.allowJs !== options.allowJs)) { + (oldOptions.allowJs !== options.allowJs) || + (oldOptions.disableSizeLimit !== options.disableSizeLimit)) { oldProgram = undefined; } } if (!tryReuseStructureFromOldProgram()) { - let programSize = 0; - for (const name of rootNames) { - const path = toPath(name, currentDirectory, getCanonicalFileName); - if (programSize <= maxProgramSize) { - processRootFile(name, /*isDefaultLib*/ false); - if (!hasTypeScriptFileExtension(name) && filesByName.get(path)) { - programSize += filesByName.get(path).text.length; + if (options.disableSizeLimit === true) { + forEach(rootNames, name => processRootFile(name, /*isDefaultLib*/ false)); + } + else { + let programSize = 0; + for (const name of rootNames) { + const path = toPath(name, currentDirectory, getCanonicalFileName); + if (programSize <= maxProgramSize) { + processRootFile(name, /*isDefaultLib*/ false); + const file = filesByName.get(path); + if (!hasTypeScriptFileExtension(name) && file && file.text) { + programSize += file.text.length; + } + } + else { + // If the program size limit was reached when processing a file, this file is + // likely in the problematic folder than contains too many files + const commonSourceDirectory = getCommonSourceDirectory(); + let rootLevelDirectory = path.substring(0, Math.max(commonSourceDirectory.length, path.indexOf(directorySeparator, commonSourceDirectory.length))); + if (rootLevelDirectory[rootLevelDirectory.length - 1] !== directorySeparator) { + rootLevelDirectory += directorySeparator; + } + programDiagnostics.add(createCompilerDiagnostic(Diagnostics.Too_many_JavaScript_files_in_the_project_Use_an_exact_files_list_or_use_the_exclude_setting_in_project_configuration_to_limit_included_source_folders_The_likely_folder_to_exclude_is_0_To_disable_the_project_size_limit_set_the_disableSizeLimit_compiler_option_to_true, rootLevelDirectory)); + break; } - } - else { - programDiagnostics.add(createCompilerDiagnostic(Diagnostics.Too_many_javascript_files_in_the_project_Consider_add_to_the_exclude_list_in_the_config_file)); - break; } } diff --git a/src/compiler/sys.ts b/src/compiler/sys.ts index c70985cd018..1c7164aeb01 100644 --- a/src/compiler/sys.ts +++ b/src/compiler/sys.ts @@ -501,17 +501,19 @@ namespace ts { const name = combinePaths(path, current); if (!contains(exclude, getCanonicalPath(name))) { // fs.statSync would throw an exception if the file is a symlink - // whose linked file doesn't exist. fs.lstatSync would return a stat - // object for the symlink file itself in this case - const stat = _fs.lstatSync(name); - if (stat.isFile()) { - if (!extension || fileExtensionIs(name, extension)) { - result.push(name); + // whose linked file doesn't exist. + try { + const stat = _fs.statSync(name); + if (stat.isFile()) { + if (!extension || fileExtensionIs(name, extension)) { + result.push(name); + } + } + else if (stat.isDirectory()) { + directories.push(name); } } - else if (stat.isDirectory()) { - directories.push(name); - } + catch (e) { } } } for (const current of directories) { diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 155f5c1a77f..73dc959e163 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -2437,6 +2437,7 @@ namespace ts { allowSyntheticDefaultImports?: boolean; allowJs?: boolean; noImplicitUseStrict?: boolean; + disableSizeLimit?: boolean; /* @internal */ stripInternal?: boolean; // Skip checking lib.d.ts to help speed up tests. diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 72ad016f596..9073e481729 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -2871,5 +2871,5 @@ namespace ts { return node.flags & NodeFlags.AccessibilityModifier && node.parent.kind === SyntaxKind.Constructor && isClassLike(node.parent.parent); } - export const maxProgramSize = 35 * 1024 * 1024; + export const maxProgramSize = 20 * 1024 * 1024; } diff --git a/src/server/editorServices.ts b/src/server/editorServices.ts index 4b49c2944cd..9cbb038f04e 100644 --- a/src/server/editorServices.ts +++ b/src/server/editorServices.ts @@ -1222,9 +1222,14 @@ namespace ts.server { // As the project openning might not be complete if there are too many files, // therefore to surface the diagnostics we need to make sure the given client file is opened. if (clientFileName) { - const currentClientFileInfo = this.openFile(clientFileName, /*openedByClient*/ true); - project.addRoot(currentClientFileInfo); - programSize += currentClientFileInfo.content.length; + if (this.host.fileExists(clientFileName)) { + const currentClientFileInfo = this.openFile(clientFileName, /*openedByClient*/ true); + project.addRoot(currentClientFileInfo); + programSize += currentClientFileInfo.content.length; + } + else { + return { errorMsg: "specified file " + clientFileName + " not found" }; + } } for (const rootFilename of projectOptions.files) { @@ -1232,8 +1237,12 @@ namespace ts.server { continue; } - if (programSize <= maxProgramSize) { - if (this.host.fileExists(rootFilename)) { + if (this.host.fileExists(rootFilename)) { + if (projectOptions.compilerOptions.disableSizeLimit === true) { + const info = this.openFile(rootFilename, /*openedByClient*/ false); + project.addRoot(info); + } + else if (programSize <= maxProgramSize) { const info = this.openFile(rootFilename, /*openedByClient*/ false); project.addRoot(info); if (!hasTypeScriptFileExtension(rootFilename)) { @@ -1241,11 +1250,11 @@ namespace ts.server { } } else { - return { errorMsg: "specified file " + rootFilename + " not found" }; + break; } } else { - break; + return { errorMsg: "specified file " + rootFilename + " not found" }; } } project.finishGraph(); From a6a466c752298446ccde7894cf62a4365d6eee25 Mon Sep 17 00:00:00 2001 From: zhengbli Date: Mon, 14 Mar 2016 20:14:17 -0700 Subject: [PATCH 04/56] online and offline CR feedback --- src/compiler/commandLineParser.ts | 3 +- src/compiler/diagnosticMessages.json | 6 +-- src/compiler/program.ts | 55 ++++++++++++++-------------- src/compiler/utilities.ts | 2 +- src/server/editorServices.ts | 10 ++--- src/services/services.ts | 3 +- 6 files changed, 37 insertions(+), 42 deletions(-) diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index 72deb16f0c1..3b7f423bf86 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -335,8 +335,7 @@ namespace ts { }, { name: "disableSizeLimit", - type: "boolean", - description: Diagnostics.Disable_the_upper_limit_for_the_total_file_size_of_a_project + type: "boolean" } ]; diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index d96b10f61ad..f8a7b627a7c 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -2620,10 +2620,6 @@ "category": "Message", "code": 6112 }, - "Disable the upper limit for the total file size of a project.": { - "category": "Message", - "code": 6113 - }, "Variable '{0}' implicitly has an '{1}' type.": { "category": "Error", "code": 7005 @@ -2828,7 +2824,7 @@ "category": "Error", "code": 17010 }, - "Too many JavaScript files in the project. Use an exact 'files' list, or use the 'exclude' setting in project configuration to limit included source folders. The likely folder to exclude is '{0}'. To disable the project size limit, set the 'disableSizeLimit' compiler option to 'true'": { + "Too many JavaScript files in the project. Consider specifying the 'exclude' setting in project configuration to limit included source folders. The likely folder to exclude is '{0}'. To disable the project size limit, set the 'disableSizeLimit' compiler option to 'true'.": { "category": "Error", "code": 17012 } diff --git a/src/compiler/program.ts b/src/compiler/program.ts index cdb4d3b417c..8f2f95fa4fe 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -696,6 +696,7 @@ namespace ts { let diagnosticsProducingTypeChecker: TypeChecker; let noDiagnosticsTypeChecker: TypeChecker; let classifiableNames: Map; + let programSizeForNonTsFiles = 0; let skipDefaultLib = options.noLib; const supportedExtensions = getSupportedExtensions(options); @@ -749,34 +750,7 @@ namespace ts { } if (!tryReuseStructureFromOldProgram()) { - if (options.disableSizeLimit === true) { - forEach(rootNames, name => processRootFile(name, /*isDefaultLib*/ false)); - } - else { - let programSize = 0; - for (const name of rootNames) { - const path = toPath(name, currentDirectory, getCanonicalFileName); - if (programSize <= maxProgramSize) { - processRootFile(name, /*isDefaultLib*/ false); - const file = filesByName.get(path); - if (!hasTypeScriptFileExtension(name) && file && file.text) { - programSize += file.text.length; - } - } - else { - // If the program size limit was reached when processing a file, this file is - // likely in the problematic folder than contains too many files - const commonSourceDirectory = getCommonSourceDirectory(); - let rootLevelDirectory = path.substring(0, Math.max(commonSourceDirectory.length, path.indexOf(directorySeparator, commonSourceDirectory.length))); - if (rootLevelDirectory[rootLevelDirectory.length - 1] !== directorySeparator) { - rootLevelDirectory += directorySeparator; - } - programDiagnostics.add(createCompilerDiagnostic(Diagnostics.Too_many_JavaScript_files_in_the_project_Use_an_exact_files_list_or_use_the_exclude_setting_in_project_configuration_to_limit_included_source_folders_The_likely_folder_to_exclude_is_0_To_disable_the_project_size_limit_set_the_disableSizeLimit_compiler_option_to_true, rootLevelDirectory)); - break; - } - } - } - + forEach(rootNames, name => processRootFile(name, /*isDefaultLib*/ false)); // Do not process the default library if: // - The '--noLib' flag is used. // - A 'no-default-lib' reference comment is encountered in @@ -1480,6 +1454,27 @@ namespace ts { return file; } + if (!options.disableSizeLimit) { + if (programSizeForNonTsFiles === -1) { + return; + } + if (programSizeForNonTsFiles > maxProgramSizeForNonTsFiles) { + // If the program size limit was reached when processing a file, this file is + // likely in the problematic folder than contains too many files. + // Normally the folder is one level down from the commonSourceDirectory, for example, + // if the commonSourceDirectory is "/src/", and the last processed path was "/src/node_modules/a/b.js", + // we should show in the error message "/src/node_modules/". + const commonSourceDirectory = getCommonSourceDirectory(); + let rootLevelDirectory = path.substring(0, Math.max(commonSourceDirectory.length, path.indexOf(directorySeparator, commonSourceDirectory.length))); + if (rootLevelDirectory[rootLevelDirectory.length - 1] !== directorySeparator) { + rootLevelDirectory += directorySeparator; + } + programDiagnostics.add(createCompilerDiagnostic(Diagnostics.Too_many_JavaScript_files_in_the_project_Consider_specifying_the_exclude_setting_in_project_configuration_to_limit_included_source_folders_The_likely_folder_to_exclude_is_0_To_disable_the_project_size_limit_set_the_disableSizeLimit_compiler_option_to_true, rootLevelDirectory)); + programSizeForNonTsFiles = -1; + return; + } + } + // We haven't looked for this file, do so now and cache result const file = host.getSourceFile(fileName, options.target, hostErrorMessage => { if (refFile !== undefined && refPos !== undefined && refEnd !== undefined) { @@ -1491,6 +1486,10 @@ namespace ts { } }); + if (!options.disableSizeLimit && file && file.text && !hasTypeScriptFileExtension(file.fileName)) { + programSizeForNonTsFiles += file.text.length; + } + filesByName.set(path, file); if (file) { file.wasReferenced = file.wasReferenced || isReference; diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 9073e481729..7974fb66cd6 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -2871,5 +2871,5 @@ namespace ts { return node.flags & NodeFlags.AccessibilityModifier && node.parent.kind === SyntaxKind.Constructor && isClassLike(node.parent.parent); } - export const maxProgramSize = 20 * 1024 * 1024; + export const maxProgramSizeForNonTsFiles = 20 * 1024 * 1024; } diff --git a/src/server/editorServices.ts b/src/server/editorServices.ts index 9cbb038f04e..456ff0ed26a 100644 --- a/src/server/editorServices.ts +++ b/src/server/editorServices.ts @@ -1217,7 +1217,7 @@ namespace ts.server { } else { const project = this.createProject(configFilename, projectOptions); - let programSize = 0; + let programSizeForNonTsFiles = 0; // As the project openning might not be complete if there are too many files, // therefore to surface the diagnostics we need to make sure the given client file is opened. @@ -1225,7 +1225,7 @@ namespace ts.server { if (this.host.fileExists(clientFileName)) { const currentClientFileInfo = this.openFile(clientFileName, /*openedByClient*/ true); project.addRoot(currentClientFileInfo); - programSize += currentClientFileInfo.content.length; + programSizeForNonTsFiles += currentClientFileInfo.content.length; } else { return { errorMsg: "specified file " + clientFileName + " not found" }; @@ -1238,15 +1238,15 @@ namespace ts.server { } if (this.host.fileExists(rootFilename)) { - if (projectOptions.compilerOptions.disableSizeLimit === true) { + if (projectOptions.compilerOptions.disableSizeLimit) { const info = this.openFile(rootFilename, /*openedByClient*/ false); project.addRoot(info); } - else if (programSize <= maxProgramSize) { + else if (programSizeForNonTsFiles <= maxProgramSizeForNonTsFiles) { const info = this.openFile(rootFilename, /*openedByClient*/ false); project.addRoot(info); if (!hasTypeScriptFileExtension(rootFilename)) { - programSize += info.content.length; + programSizeForNonTsFiles += info.content.length; } } else { diff --git a/src/services/services.ts b/src/services/services.ts index a562ecf18cf..e72c647db1b 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -2760,7 +2760,8 @@ namespace ts { oldSettings.module !== newSettings.module || oldSettings.noResolve !== newSettings.noResolve || oldSettings.jsx !== newSettings.jsx || - oldSettings.allowJs !== newSettings.allowJs); + oldSettings.allowJs !== newSettings.allowJs || + oldSettings.disableSizeLimit !== oldSettings.disableSizeLimit); // Now create a new compiler const compilerHost: CompilerHost = { From d4eb3b8d12420494d64d42b40e203c86c58daa3c Mon Sep 17 00:00:00 2001 From: zhengbli Date: Mon, 14 Mar 2016 20:43:22 -0700 Subject: [PATCH 05/56] Don't count current opened client file if it's TS file --- src/server/editorServices.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/server/editorServices.ts b/src/server/editorServices.ts index 456ff0ed26a..6f8006986ed 100644 --- a/src/server/editorServices.ts +++ b/src/server/editorServices.ts @@ -1225,7 +1225,9 @@ namespace ts.server { if (this.host.fileExists(clientFileName)) { const currentClientFileInfo = this.openFile(clientFileName, /*openedByClient*/ true); project.addRoot(currentClientFileInfo); - programSizeForNonTsFiles += currentClientFileInfo.content.length; + if (!hasTypeScriptFileExtension(currentClientFileInfo.fileName) && currentClientFileInfo.content) { + programSizeForNonTsFiles += currentClientFileInfo.content.length; + } } else { return { errorMsg: "specified file " + clientFileName + " not found" }; From 225e3b4f45e0c300b7f8dda3df1c1886887ed95e Mon Sep 17 00:00:00 2001 From: zhengbli Date: Wed, 16 Mar 2016 23:01:24 -0700 Subject: [PATCH 06/56] Speed up file searching --- src/compiler/commandLineParser.ts | 4 ++-- src/compiler/sys.ts | 17 +++++++++++++++-- src/compiler/types.ts | 2 +- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index 34a84ac793f..f535140eeaf 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -637,10 +637,10 @@ namespace ts { const supportedExtensions = getSupportedExtensions(options); Debug.assert(indexOf(supportedExtensions, ".ts") < indexOf(supportedExtensions, ".d.ts"), "Changed priority of extensions to pick"); + const potentialFiles = host.readDirectory(basePath, supportedExtensions, exclude); // Get files of supported extensions in their order of resolution for (const extension of supportedExtensions) { - const filesInDirWithExtension = host.readDirectory(basePath, extension, exclude); - for (const fileName of filesInDirWithExtension) { + for (const fileName of potentialFiles) { // .ts extension would read the .d.ts extension files too but since .d.ts is lower priority extension, // lets pick them when its turn comes up if (extension === ".ts" && fileExtensionIs(fileName, ".d.ts")) { diff --git a/src/compiler/sys.ts b/src/compiler/sys.ts index 1c7164aeb01..924fe92990b 100644 --- a/src/compiler/sys.ts +++ b/src/compiler/sys.ts @@ -489,11 +489,24 @@ namespace ts { return fileSystemEntryExists(path, FileSystemEntryKind.Directory); } - function readDirectory(path: string, extension?: string, exclude?: string[]): string[] { + function readDirectory(path: string, extension?: string | string[], exclude?: string[]): string[] { const result: string[] = []; exclude = map(exclude, s => getCanonicalPath(combinePaths(path, s))); visitDirectory(path); return result; + + function checkExtension(name: string) { + if (!extension) { + return true; + } + if (typeof extension === "string") { + return fileExtensionIs(name, extension); + } + if (typeof extension === "string[]") { + return forEach(extension, ext => fileExtensionIs(name, ext)); + } + } + function visitDirectory(path: string) { const files = _fs.readdirSync(path || ".").sort(); const directories: string[] = []; @@ -505,7 +518,7 @@ namespace ts { try { const stat = _fs.statSync(name); if (stat.isFile()) { - if (!extension || fileExtensionIs(name, extension)) { + if (checkExtension(name)) { result.push(name); } } diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 95a1e32c401..af765eb17b0 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -1580,7 +1580,7 @@ namespace ts { } export interface ParseConfigHost { - readDirectory(rootDir: string, extension: string, exclude: string[]): string[]; + readDirectory(rootDir: string, extension: string | string[], exclude: string[]): string[]; } export interface WriteFileCallback { From c8e0b000070bb5bc1568e99fb46b53b3492a3c50 Mon Sep 17 00:00:00 2001 From: zhengbli Date: Thu, 17 Mar 2016 15:55:53 -0700 Subject: [PATCH 07/56] Make language service optional for a project --- src/compiler/commandLineParser.ts | 10 +- src/compiler/program.ts | 46 ++++---- src/compiler/sys.ts | 78 ++++++++------ src/compiler/types.ts | 5 +- src/compiler/utilities.ts | 2 - src/server/editorServices.ts | 168 +++++++++++++++++++++++------- src/server/protocol.d.ts | 1 + src/server/session.ts | 53 ++++++---- 8 files changed, 249 insertions(+), 114 deletions(-) diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index f535140eeaf..d7e58b20ca5 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -637,7 +637,15 @@ namespace ts { const supportedExtensions = getSupportedExtensions(options); Debug.assert(indexOf(supportedExtensions, ".ts") < indexOf(supportedExtensions, ".d.ts"), "Changed priority of extensions to pick"); - const potentialFiles = host.readDirectory(basePath, supportedExtensions, exclude); + const potentialFiles: string[] = []; + if (host.readDirectoryWithMultipleExtensions) { + addRange(potentialFiles, host.readDirectoryWithMultipleExtensions(basePath, supportedExtensions, exclude)); + } + else { + for (const extension of supportedExtensions) { + addRange(potentialFiles, host.readDirectory(basePath, extension, exclude)); + } + } // Get files of supported extensions in their order of resolution for (const extension of supportedExtensions) { for (const fileName of potentialFiles) { diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 2da1fffdb8b..64a487baad2 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -7,6 +7,7 @@ namespace ts { /* @internal */ export let emitTime = 0; /* @internal */ export let ioReadTime = 0; /* @internal */ export let ioWriteTime = 0; + /* @internal */ export const maxProgramSizeForNonTsFiles = 20 * 1024 * 1024; /** The version of the TypeScript compiler release */ @@ -696,6 +697,7 @@ namespace ts { let diagnosticsProducingTypeChecker: TypeChecker; let noDiagnosticsTypeChecker: TypeChecker; let classifiableNames: Map; + let programSizeLimitExceeded = false; let programSizeForNonTsFiles = 0; let skipDefaultLib = options.noLib; @@ -792,6 +794,11 @@ namespace ts { return program; + function exceedProgramSizeLimit() { + return !options.disableSizeLimit && programSizeLimitExceeded; + } + + function getCommonSourceDirectory() { if (typeof commonSourceDirectory === "undefined") { if (options.rootDir && checkSourceFilesBelongToPath(files, options.rootDir)) { @@ -1417,7 +1424,7 @@ namespace ts { } } - if (diagnostic) { + if (diagnostic && !exceedProgramSizeLimit()) { if (refFile !== undefined && refEnd !== undefined && refPos !== undefined) { fileProcessingDiagnostics.add(createFileDiagnostic(refFile, refPos, refEnd - refPos, diagnostic, ...diagnosticArgument)); } @@ -1454,25 +1461,9 @@ namespace ts { return file; } - if (!options.disableSizeLimit) { - if (programSizeForNonTsFiles === -1) { - return; - } - if (programSizeForNonTsFiles > maxProgramSizeForNonTsFiles) { - // If the program size limit was reached when processing a file, this file is - // likely in the problematic folder than contains too many files. - // Normally the folder is one level down from the commonSourceDirectory, for example, - // if the commonSourceDirectory is "/src/", and the last processed path was "/src/node_modules/a/b.js", - // we should show in the error message "/src/node_modules/". - const commonSourceDirectory = getCommonSourceDirectory(); - let rootLevelDirectory = path.substring(0, Math.max(commonSourceDirectory.length, path.indexOf(directorySeparator, commonSourceDirectory.length))); - if (rootLevelDirectory[rootLevelDirectory.length - 1] !== directorySeparator) { - rootLevelDirectory += directorySeparator; - } - programDiagnostics.add(createCompilerDiagnostic(Diagnostics.Too_many_JavaScript_files_in_the_project_Consider_specifying_the_exclude_setting_in_project_configuration_to_limit_included_source_folders_The_likely_folder_to_exclude_is_0_To_disable_the_project_size_limit_set_the_disableSizeLimit_compiler_option_to_true, rootLevelDirectory)); - programSizeForNonTsFiles = -1; - return; - } + const isNonTsFile = !hasTypeScriptFileExtension(fileName); + if (isNonTsFile && exceedProgramSizeLimit()) { + return undefined; } // We haven't looked for this file, do so now and cache result @@ -1488,6 +1479,21 @@ namespace ts { if (!options.disableSizeLimit && file && file.text && !hasTypeScriptFileExtension(file.fileName)) { programSizeForNonTsFiles += file.text.length; + if (programSizeForNonTsFiles > maxProgramSizeForNonTsFiles) { + // If the program size limit was reached when processing a file, this file is + // likely in the problematic folder than contains too many files. + // Normally the folder is one level down from the commonSourceDirectory, for example, + // if the commonSourceDirectory is "/src/", and the last processed path was "/src/node_modules/a/b.js", + // we should show in the error message "/src/node_modules/". + const commonSourceDirectory = getCommonSourceDirectory(); + let rootLevelDirectory = path.substring(0, Math.max(commonSourceDirectory.length, path.indexOf(directorySeparator, commonSourceDirectory.length))); + if (rootLevelDirectory[rootLevelDirectory.length - 1] !== directorySeparator) { + rootLevelDirectory += directorySeparator; + } + programDiagnostics.add(createCompilerDiagnostic(Diagnostics.Too_many_JavaScript_files_in_the_project_Consider_specifying_the_exclude_setting_in_project_configuration_to_limit_included_source_folders_The_likely_folder_to_exclude_is_0_To_disable_the_project_size_limit_set_the_disableSizeLimit_compiler_option_to_true, rootLevelDirectory)); + programSizeLimitExceeded = true; + return undefined; + } } filesByName.set(path, file); diff --git a/src/compiler/sys.ts b/src/compiler/sys.ts index 924fe92990b..3b8b9b3f39f 100644 --- a/src/compiler/sys.ts +++ b/src/compiler/sys.ts @@ -10,6 +10,7 @@ namespace ts { useCaseSensitiveFileNames: boolean; write(s: string): void; readFile(path: string, encoding?: string): string; + getFileSize?(path: string): number; writeFile(path: string, data: string, writeByteOrderMark?: boolean): void; watchFile?(path: Path, callback: FileWatcherCallback): FileWatcher; watchDirectory?(path: string, callback: DirectoryWatcherCallback, recursive?: boolean): FileWatcher; @@ -20,6 +21,7 @@ namespace ts { getExecutingFilePath(): string; getCurrentDirectory(): string; readDirectory(path: string, extension?: string, exclude?: string[]): string[]; + readDirectoryWithMultipleExtensions?(path: string, extensions: string[], exclude?: string[]): string[]; getMemoryUsage?(): number; exit(exitCode?: number): void; } @@ -489,10 +491,32 @@ namespace ts { return fileSystemEntryExists(path, FileSystemEntryKind.Directory); } - function readDirectory(path: string, extension?: string | string[], exclude?: string[]): string[] { + function visitDirectory(path: string, extension: string | string[], exclude: string[]) { const result: string[] = []; - exclude = map(exclude, s => getCanonicalPath(combinePaths(path, s))); - visitDirectory(path); + const files = _fs.readdirSync(path || ".").sort(); + const directories: string[] = []; + for (const current of files) { + const name = combinePaths(path, current); + if (!contains(exclude, getCanonicalPath(name))) { + // fs.statSync would throw an exception if the file is a symlink + // whose linked file doesn't exist. + try { + const stat = _fs.statSync(name); + if (stat.isFile()) { + if (checkExtension(name)) { + result.push(name); + } + } + else if (stat.isDirectory()) { + directories.push(name); + } + } + catch (e) { } + } + } + for (const current of directories) { + visitDirectory(current, extension, exclude); + } return result; function checkExtension(name: string) { @@ -502,37 +526,20 @@ namespace ts { if (typeof extension === "string") { return fileExtensionIs(name, extension); } - if (typeof extension === "string[]") { + else { return forEach(extension, ext => fileExtensionIs(name, ext)); } } + } - function visitDirectory(path: string) { - const files = _fs.readdirSync(path || ".").sort(); - const directories: string[] = []; - for (const current of files) { - const name = combinePaths(path, current); - if (!contains(exclude, getCanonicalPath(name))) { - // fs.statSync would throw an exception if the file is a symlink - // whose linked file doesn't exist. - try { - const stat = _fs.statSync(name); - if (stat.isFile()) { - if (checkExtension(name)) { - result.push(name); - } - } - else if (stat.isDirectory()) { - directories.push(name); - } - } - catch (e) { } - } - } - for (const current of directories) { - visitDirectory(current); - } - } + function readDirectoryWithMultipleExtensions(path: string, extensions: string[], exclude?: string[]): string[] { + exclude = map(exclude, s => getCanonicalPath(combinePaths(path, s))); + return visitDirectory(path, extensions, exclude); + } + + function readDirectory(path: string, extension?: string, exclude?: string[]): string[] { + exclude = map(exclude, s => getCanonicalPath(combinePaths(path, s))); + return visitDirectory(path, extension, exclude); } return { @@ -597,12 +604,23 @@ namespace ts { return process.cwd(); }, readDirectory, + readDirectoryWithMultipleExtensions, getMemoryUsage() { if (global.gc) { global.gc(); } return process.memoryUsage().heapUsed; }, + getFileSize(path) { + try { + const stat = _fs.statSync(path); + if (stat.isFile()) { + return stat.size; + } + } + catch (e) { } + return 0; + }, exit(exitCode?: number): void { process.exit(exitCode); } diff --git a/src/compiler/types.ts b/src/compiler/types.ts index af765eb17b0..3638cc3a92f 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -1580,7 +1580,8 @@ namespace ts { } export interface ParseConfigHost { - readDirectory(rootDir: string, extension: string | string[], exclude: string[]): string[]; + readDirectory(rootDir: string, extension: string, exclude: string[]): string[]; + readDirectoryWithMultipleExtensions?(rootDir: string, extensions: string[], exclude: string[]): string[]; } export interface WriteFileCallback { @@ -2440,7 +2441,7 @@ namespace ts { allowJs?: boolean; noImplicitUseStrict?: boolean; disableSizeLimit?: boolean; - lib?: string[]; + lib?: string[]; /* @internal */ stripInternal?: boolean; // Skip checking lib.d.ts to help speed up tests. diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 68db246cb9a..6520e339ac9 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -2870,6 +2870,4 @@ namespace ts { export function isParameterPropertyDeclaration(node: ParameterDeclaration): boolean { return node.flags & NodeFlags.AccessibilityModifier && node.parent.kind === SyntaxKind.Constructor && isClassLike(node.parent.parent); } - - export const maxProgramSizeForNonTsFiles = 20 * 1024 * 1024; } diff --git a/src/server/editorServices.ts b/src/server/editorServices.ts index 8c3429ac698..2eef384df5c 100644 --- a/src/server/editorServices.ts +++ b/src/server/editorServices.ts @@ -347,12 +347,29 @@ namespace ts.server { /** Used for configured projects which may have multiple open roots */ openRefCount = 0; - constructor(public projectService: ProjectService, public projectOptions?: ProjectOptions) { + constructor( + public projectService: ProjectService, + public projectOptions?: ProjectOptions, + public languageServiceDiabled?: boolean) { if (projectOptions && projectOptions.files) { // If files are listed explicitly, allow all extensions projectOptions.compilerOptions.allowNonTsExtensions = true; } - this.compilerService = new CompilerService(this, projectOptions && projectOptions.compilerOptions); + if (!languageServiceDiabled) { + this.compilerService = new CompilerService(this, projectOptions && projectOptions.compilerOptions); + } + } + + enableLanguageService() { + // if the language service was disabled, we should re-initiate the compiler service + if (this.languageServiceDiabled) { + this.compilerService = new CompilerService(this, this.projectOptions && this.projectOptions.compilerOptions); + } + this.languageServiceDiabled = false; + } + + disableLanguageService() { + this.languageServiceDiabled = true; } addOpenRef() { @@ -369,19 +386,36 @@ namespace ts.server { } getRootFiles() { + if (this.languageServiceDiabled) { + // When the languageService was disabled, only return file list if it is a configured project + return this.projectOptions ? this.projectOptions.files : undefined; + } + return this.compilerService.host.roots.map(info => info.fileName); } getFileNames() { + if (this.languageServiceDiabled) { + return this.projectOptions ? this.projectOptions.files : undefined; + } + const sourceFiles = this.program.getSourceFiles(); return sourceFiles.map(sourceFile => sourceFile.fileName); } getSourceFile(info: ScriptInfo) { + if (this.languageServiceDiabled) { + return undefined; + } + return this.filenameToSourceFile[info.fileName]; } getSourceFileFromName(filename: string, requireOpen?: boolean) { + if (this.languageServiceDiabled) { + return undefined; + } + const info = this.projectService.getScriptInfo(filename); if (info) { if ((!requireOpen) || info.isOpen) { @@ -391,15 +425,30 @@ namespace ts.server { } isRoot(info: ScriptInfo) { + if (this.languageServiceDiabled) { + if (!this.projectOptions) { + return undefined; + } + return forEach(this.projectOptions.files, file => toPath(file, file, createGetCanonicalFileName(this.projectService.host.useCaseSensitiveFileNames)) === info.path); + } + return this.compilerService.host.roots.some(root => root === info); } removeReferencedFile(info: ScriptInfo) { + if (this.languageServiceDiabled) { + return; + } + this.compilerService.host.removeReferencedFile(info); this.updateGraph(); } updateFileMap() { + if (this.languageServiceDiabled) { + return; + } + this.filenameToSourceFile = {}; const sourceFiles = this.program.getSourceFiles(); for (let i = 0, len = sourceFiles.length; i < len; i++) { @@ -409,11 +458,19 @@ namespace ts.server { } finishGraph() { + if (this.languageServiceDiabled) { + return; + } + this.updateGraph(); this.compilerService.languageService.getNavigateToItems(".*"); } updateGraph() { + if (this.languageServiceDiabled) { + return; + } + this.program = this.compilerService.languageService.getProgram(); this.updateFileMap(); } @@ -424,15 +481,32 @@ namespace ts.server { // add a root file to project addRoot(info: ScriptInfo) { + if (this.languageServiceDiabled) { + return; + } + this.compilerService.host.addRoot(info); } // remove a root file from project removeRoot(info: ScriptInfo) { + if (this.languageServiceDiabled) { + return; + } + this.compilerService.host.removeRoot(info); } filesToString() { + if (this.languageServiceDiabled) { + if (this.projectOptions) { + let strBuilder = ""; + ts.forEach(this.projectOptions.files, + file => { strBuilder += file + "\n"; }); + return strBuilder; + } + } + let strBuilder = ""; ts.forEachValue(this.filenameToSourceFile, sourceFile => { strBuilder += sourceFile.fileName + "\n"; }); @@ -443,7 +517,9 @@ namespace ts.server { this.projectOptions = projectOptions; if (projectOptions.compilerOptions) { projectOptions.compilerOptions.allowNonTsExtensions = true; - this.compilerService.setCompilerOptions(projectOptions.compilerOptions); + if (!this.languageServiceDiabled) { + this.compilerService.setCompilerOptions(projectOptions.compilerOptions); + } } } } @@ -1055,10 +1131,12 @@ namespace ts.server { * @param fileContent is a known version of the file content that is more up to date than the one on disk */ openClientFile(fileName: string, fileContent?: string) { + this.log("start openClientFile: " + new Date().getTime()); this.openOrUpdateConfiguredProjectForFile(fileName); const info = this.openFile(fileName, /*openedByClient*/ true, fileContent); this.addOpenFile(info); this.printProjects(); + this.log("end openClientFile: " + new Date().getTime()); return info; } @@ -1068,6 +1146,7 @@ namespace ts.server { * the tsconfig file content and update the project; otherwise we create a new one. */ openOrUpdateConfiguredProjectForFile(fileName: string) { + this.log("start openOrUpdateConfiguredProjectForFile: " + new Date().getTime()); const searchPath = ts.normalizePath(getDirectoryPath(fileName)); this.log("Search path: " + searchPath, "Info"); const configFileName = this.findConfigFile(searchPath); @@ -1091,6 +1170,7 @@ namespace ts.server { else { this.log("No config files found."); } + this.log("end openOrUpdateConfiguredProjectForFile: " + new Date().getTime()); } /** @@ -1207,53 +1287,49 @@ namespace ts.server { return { succeeded: true, projectOptions }; } } + } + private exceedTotalNonTsFileSizeLimit(fileNames: string[]) { + let totalNonTsFileSize = 0; + for (const fileName of fileNames) { + if (hasTypeScriptFileExtension(fileName)) { + continue; + } + totalNonTsFileSize += this.host.getFileSize(fileName); + if (totalNonTsFileSize > maxProgramSizeForNonTsFiles) { + return true; + } + } + return false; } openConfigFile(configFilename: string, clientFileName?: string): ProjectOpenResult { + this.log("start openConfigFile: " + new Date().getTime()); const { succeeded, projectOptions, error } = this.configFileToProjectOptions(configFilename); + this.log("finish reading config file: " + new Date().getTime()); if (!succeeded) { + this.log("finish openConfigFile: " + new Date().getTime()); return error; } else { - const project = this.createProject(configFilename, projectOptions); - let programSizeForNonTsFiles = 0; + if (!projectOptions.compilerOptions.disableSizeLimit && projectOptions.compilerOptions.allowJs) { + if (this.exceedTotalNonTsFileSizeLimit(projectOptions.files)) { + const project = this.createProject(configFilename, projectOptions, /*languageServiceDisabled*/ true); - // As the project openning might not be complete if there are too many files, - // therefore to surface the diagnostics we need to make sure the given client file is opened. - if (clientFileName) { - if (this.host.fileExists(clientFileName)) { - const currentClientFileInfo = this.openFile(clientFileName, /*openedByClient*/ true); - project.addRoot(currentClientFileInfo); - if (!hasTypeScriptFileExtension(currentClientFileInfo.fileName) && currentClientFileInfo.content) { - programSizeForNonTsFiles += currentClientFileInfo.content.length; - } - } - else { - return { errorMsg: "specified file " + clientFileName + " not found" }; + // for configured projects with languageService disabled, we only watch its config file, + // do not care about the directory changes in the folder. + project.projectFileWatcher = this.host.watchFile( + toPath(configFilename, configFilename, createGetCanonicalFileName(sys.useCaseSensitiveFileNames)), + _ => this.watchedProjectConfigFileChanged(project)); + return { success: true, project }; } } + const project = this.createProject(configFilename, projectOptions); for (const rootFilename of projectOptions.files) { - if (rootFilename === clientFileName) { - continue; - } - if (this.host.fileExists(rootFilename)) { - if (projectOptions.compilerOptions.disableSizeLimit) { - const info = this.openFile(rootFilename, /*openedByClient*/ false); - project.addRoot(info); - } - else if (programSizeForNonTsFiles <= maxProgramSizeForNonTsFiles) { - const info = this.openFile(rootFilename, /*openedByClient*/ false); - project.addRoot(info); - if (!hasTypeScriptFileExtension(rootFilename)) { - programSizeForNonTsFiles += info.content.length; - } - } - else { - break; - } + const info = this.openFile(rootFilename, /*openedByClient*/ clientFileName == rootFilename); + project.addRoot(info); } else { return { errorMsg: "specified file " + rootFilename + " not found" }; @@ -1269,6 +1345,7 @@ namespace ts.server { path => this.directoryWatchedForSourceFilesChanged(project, path), /*recursive*/ true ); + this.log("finish openConfigFile: " + new Date().getTime()); return { success: true, project: project }; } } @@ -1284,6 +1361,23 @@ namespace ts.server { return error; } else { + if (this.exceedTotalNonTsFileSizeLimit(projectOptions.files)) { + project.disableLanguageService(); + project.directoryWatcher.close(); + project.directoryWatcher = undefined; + project.setProjectOptions(projectOptions); + return; + } + + project.enableLanguageService(); + if (!project.directoryWatcher) { + project.directoryWatcher = this.host.watchDirectory( + ts.getDirectoryPath(project.projectFilename), + path => this.directoryWatchedForSourceFilesChanged(project, path), + /*recursive*/ true + ); + } + // if the project is too large, the root files might not have been all loaded if the total // program size reached the upper limit. In that case project.projectOptions.files should // be more precise. However this would only happen for configured project. @@ -1330,8 +1424,8 @@ namespace ts.server { } } - createProject(projectFilename: string, projectOptions?: ProjectOptions) { - const project = new Project(this, projectOptions); + createProject(projectFilename: string, projectOptions?: ProjectOptions, languageServiceDisabled?: boolean) { + const project = new Project(this, projectOptions, languageServiceDisabled); project.projectFilename = projectFilename; return project; } diff --git a/src/server/protocol.d.ts b/src/server/protocol.d.ts index 5ed227ebaa7..3a9746dc109 100644 --- a/src/server/protocol.d.ts +++ b/src/server/protocol.d.ts @@ -123,6 +123,7 @@ declare namespace ts.server.protocol { * The list of normalized file name in the project, including 'lib.d.ts' */ fileNames?: string[]; + languageServiceDisabled?: boolean; } /** diff --git a/src/server/session.ts b/src/server/session.ts index f0975a3f947..a66fbc8a5e4 100644 --- a/src/server/session.ts +++ b/src/server/session.ts @@ -288,7 +288,7 @@ namespace ts.server { private getDefinition(line: number, offset: number, fileName: string): protocol.FileSpan[] { const file = ts.normalizePath(fileName); const project = this.projectService.getProjectForFile(file); - if (!project) { + if (!project || project.languageServiceDiabled) { throw Errors.NoProject; } @@ -310,7 +310,7 @@ namespace ts.server { private getTypeDefinition(line: number, offset: number, fileName: string): protocol.FileSpan[] { const file = ts.normalizePath(fileName); const project = this.projectService.getProjectForFile(file); - if (!project) { + if (!project || project.languageServiceDiabled) { throw Errors.NoProject; } @@ -333,7 +333,7 @@ namespace ts.server { fileName = ts.normalizePath(fileName); const project = this.projectService.getProjectForFile(fileName); - if (!project) { + if (!project || project.languageServiceDiabled) { throw Errors.NoProject; } @@ -363,7 +363,7 @@ namespace ts.server { fileName = ts.normalizePath(fileName); const project = this.projectService.getProjectForFile(fileName); - if (!project) { + if (!project || project.languageServiceDiabled) { throw Errors.NoProject; } @@ -396,24 +396,29 @@ namespace ts.server { } private getProjectInfo(fileName: string, needFileNameList: boolean): protocol.ProjectInfo { + this.logger.info("start getProjectInfo:" + new Date().getTime()); fileName = ts.normalizePath(fileName); const project = this.projectService.getProjectForFile(fileName); + if (!project) { + throw Errors.NoProject; + } const projectInfo: protocol.ProjectInfo = { - configFileName: project.projectFilename + configFileName: project.projectFilename, + languageServiceDisabled: project.languageServiceDiabled }; if (needFileNameList) { projectInfo.fileNames = project.getFileNames(); } - + this.logger.info("end getProjectInfo:" + new Date().getTime()); return projectInfo; } private getRenameLocations(line: number, offset: number, fileName: string, findInComments: boolean, findInStrings: boolean): protocol.RenameResponseBody { const file = ts.normalizePath(fileName); const project = this.projectService.getProjectForFile(file); - if (!project) { + if (!project || project.languageServiceDiabled) { throw Errors.NoProject; } @@ -483,7 +488,7 @@ namespace ts.server { // can avoid duplicates by eliminating same ref file from subsequent projects const file = ts.normalizePath(fileName); const project = this.projectService.getProjectForFile(file); - if (!project) { + if (!project || project.languageServiceDiabled) { throw Errors.NoProject; } @@ -537,7 +542,7 @@ namespace ts.server { private getQuickInfo(line: number, offset: number, fileName: string): protocol.QuickInfoResponseBody { const file = ts.normalizePath(fileName); const project = this.projectService.getProjectForFile(file); - if (!project) { + if (!project || project.languageServiceDiabled) { throw Errors.NoProject; } @@ -563,7 +568,7 @@ namespace ts.server { private getFormattingEditsForRange(line: number, offset: number, endLine: number, endOffset: number, fileName: string): protocol.CodeEdit[] { const file = ts.normalizePath(fileName); const project = this.projectService.getProjectForFile(file); - if (!project) { + if (!project || project.languageServiceDiabled) { throw Errors.NoProject; } @@ -591,7 +596,7 @@ namespace ts.server { const file = ts.normalizePath(fileName); const project = this.projectService.getProjectForFile(file); - if (!project) { + if (!project || project.languageServiceDiabled) { throw Errors.NoProject; } @@ -669,7 +674,7 @@ namespace ts.server { } const file = ts.normalizePath(fileName); const project = this.projectService.getProjectForFile(file); - if (!project) { + if (!project || project.languageServiceDiabled) { throw Errors.NoProject; } @@ -693,7 +698,7 @@ namespace ts.server { entryNames: string[], fileName: string): protocol.CompletionEntryDetails[] { const file = ts.normalizePath(fileName); const project = this.projectService.getProjectForFile(file); - if (!project) { + if (!project || project.languageServiceDiabled) { throw Errors.NoProject; } @@ -712,7 +717,7 @@ namespace ts.server { private getSignatureHelpItems(line: number, offset: number, fileName: string): protocol.SignatureHelpItems { const file = ts.normalizePath(fileName); const project = this.projectService.getProjectForFile(file); - if (!project) { + if (!project || project.languageServiceDiabled) { throw Errors.NoProject; } @@ -742,7 +747,7 @@ namespace ts.server { const checkList = fileNames.reduce((accum: PendingErrorCheck[], fileName: string) => { fileName = ts.normalizePath(fileName); const project = this.projectService.getProjectForFile(fileName); - if (project) { + if (project && !project.languageServiceDiabled) { accum.push({ fileName, project }); } return accum; @@ -756,7 +761,7 @@ namespace ts.server { private change(line: number, offset: number, endLine: number, endOffset: number, insertString: string, fileName: string) { const file = ts.normalizePath(fileName); const project = this.projectService.getProjectForFile(file); - if (project) { + if (project && !project.languageServiceDiabled) { const compilerService = project.compilerService; const start = compilerService.host.lineOffsetToPosition(file, line, offset); const end = compilerService.host.lineOffsetToPosition(file, endLine, endOffset); @@ -772,7 +777,7 @@ namespace ts.server { const file = ts.normalizePath(fileName); const tmpfile = ts.normalizePath(tempFileName); const project = this.projectService.getProjectForFile(file); - if (project) { + if (project && !project.languageServiceDiabled) { this.changeSeq++; // make sure no changes happen before this one is finished project.compilerService.host.reloadScript(file, tmpfile, () => { @@ -786,7 +791,7 @@ namespace ts.server { const tmpfile = ts.normalizePath(tempFileName); const project = this.projectService.getProjectForFile(file); - if (project) { + if (project && !project.languageServiceDiabled) { project.compilerService.host.saveTo(file, tmpfile); } } @@ -821,7 +826,7 @@ namespace ts.server { private getNavigationBarItems(fileName: string): protocol.NavigationBarItem[] { const file = ts.normalizePath(fileName); const project = this.projectService.getProjectForFile(file); - if (!project) { + if (!project || project.languageServiceDiabled) { throw Errors.NoProject; } @@ -837,7 +842,7 @@ namespace ts.server { private getNavigateToItems(searchValue: string, fileName: string, maxResultCount?: number): protocol.NavtoItem[] { const file = ts.normalizePath(fileName); const project = this.projectService.getProjectForFile(file); - if (!project) { + if (!project || project.languageServiceDiabled) { throw Errors.NoProject; } @@ -877,7 +882,7 @@ namespace ts.server { const file = ts.normalizePath(fileName); const project = this.projectService.getProjectForFile(file); - if (!project) { + if (!project || project.languageServiceDiabled) { throw Errors.NoProject; } @@ -896,7 +901,11 @@ namespace ts.server { } getDiagnosticsForProject(delay: number, fileName: string) { - const { fileNames } = this.getProjectInfo(fileName, /*needFileNameList*/ true); + const { fileNames, languageServiceDisabled } = this.getProjectInfo(fileName, /*needFileNameList*/ true); + if (languageServiceDisabled) { + return; + } + // No need to analyze lib.d.ts let fileNamesInProject = fileNames.filter((value, index, array) => value.indexOf("lib.d.ts") < 0); From cb46f16406d32c1a8d38e57999ead4421fd9e496 Mon Sep 17 00:00:00 2001 From: zhengbli Date: Thu, 17 Mar 2016 19:01:17 -0700 Subject: [PATCH 08/56] Fix failed tests --- src/compiler/commandLineParser.ts | 10 ++++++++-- src/compiler/sys.ts | 14 ++++++++------ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index d7e58b20ca5..5ba54c6553c 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -649,6 +649,10 @@ namespace ts { // Get files of supported extensions in their order of resolution for (const extension of supportedExtensions) { for (const fileName of potentialFiles) { + if (!fileExtensionIs(fileName, extension)) { + continue; + } + // .ts extension would read the .d.ts extension files too but since .d.ts is lower priority extension, // lets pick them when its turn comes up if (extension === ".ts" && fileExtensionIs(fileName, ".d.ts")) { @@ -669,8 +673,10 @@ namespace ts { } } - filesSeen[fileName] = true; - fileNames.push(fileName); + if (!filesSeen[fileName]) { + filesSeen[fileName] = true; + fileNames.push(fileName); + } } } } diff --git a/src/compiler/sys.ts b/src/compiler/sys.ts index 3b8b9b3f39f..7cdc1624582 100644 --- a/src/compiler/sys.ts +++ b/src/compiler/sys.ts @@ -491,8 +491,7 @@ namespace ts { return fileSystemEntryExists(path, FileSystemEntryKind.Directory); } - function visitDirectory(path: string, extension: string | string[], exclude: string[]) { - const result: string[] = []; + function visitDirectory(path: string, result: string[], extension: string | string[], exclude: string[]) { const files = _fs.readdirSync(path || ".").sort(); const directories: string[] = []; for (const current of files) { @@ -515,9 +514,8 @@ namespace ts { } } for (const current of directories) { - visitDirectory(current, extension, exclude); + visitDirectory(current, result, extension, exclude); } - return result; function checkExtension(name: string) { if (!extension) { @@ -533,13 +531,17 @@ namespace ts { } function readDirectoryWithMultipleExtensions(path: string, extensions: string[], exclude?: string[]): string[] { + const result: string[] = []; exclude = map(exclude, s => getCanonicalPath(combinePaths(path, s))); - return visitDirectory(path, extensions, exclude); + visitDirectory(path, result, extensions, exclude); + return result; } function readDirectory(path: string, extension?: string, exclude?: string[]): string[] { + const result: string[] = []; exclude = map(exclude, s => getCanonicalPath(combinePaths(path, s))); - return visitDirectory(path, extension, exclude); + visitDirectory(path, result, extension, exclude); + return result; } return { From 74e3d7bb0133f3214770f8aadababa8ef0d4d15c Mon Sep 17 00:00:00 2001 From: zhengbli Date: Fri, 18 Mar 2016 11:00:26 -0700 Subject: [PATCH 09/56] Fix project updateing issue after editing config file --- src/server/editorServices.ts | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/src/server/editorServices.ts b/src/server/editorServices.ts index 2eef384df5c..2c9e26e47cb 100644 --- a/src/server/editorServices.ts +++ b/src/server/editorServices.ts @@ -1362,20 +1362,36 @@ namespace ts.server { } else { if (this.exceedTotalNonTsFileSizeLimit(projectOptions.files)) { + project.setProjectOptions(projectOptions); + if (project.languageServiceDiabled) { + return; + } + project.disableLanguageService(); project.directoryWatcher.close(); - project.directoryWatcher = undefined; - project.setProjectOptions(projectOptions); return; } - project.enableLanguageService(); - if (!project.directoryWatcher) { + if (project.languageServiceDiabled) { + project.setProjectOptions(projectOptions); + project.enableLanguageService(); project.directoryWatcher = this.host.watchDirectory( ts.getDirectoryPath(project.projectFilename), path => this.directoryWatchedForSourceFilesChanged(project, path), /*recursive*/ true ); + + for (const rootFilename of projectOptions.files) { + if (this.host.fileExists(rootFilename)) { + const info = this.openFile(rootFilename, /*openedByClient*/ false); + project.addRoot(info); + } + else { + return { errorMsg: "specified file " + rootFilename + " not found" }; + } + } + project.finishGraph(); + return; } // if the project is too large, the root files might not have been all loaded if the total @@ -1396,6 +1412,9 @@ namespace ts.server { for (const fileName of fileNamesToAdd) { let info = this.getScriptInfo(fileName); if (!info) { + if (!this.host.fileExists(info.fileName)) { + return { errorMsg: "specified file " + info.fileName + " not found" }; + } info = this.openFile(fileName, /*openedByClient*/ false); } else { From d387050aaab9f635ff3543563aced5c8a9a844db Mon Sep 17 00:00:00 2001 From: zhengbli Date: Thu, 9 Jun 2016 15:30:55 -0700 Subject: [PATCH 10/56] Fix merging issues and multiple project scenario --- src/compiler/program.ts | 2 +- src/compiler/sys.ts | 4 ++-- src/server/editorServices.ts | 10 ++-------- src/server/session.ts | 20 +++++++++++--------- 4 files changed, 16 insertions(+), 20 deletions(-) diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 8b38d56c2b2..99ca9377d54 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -1219,7 +1219,7 @@ namespace ts { (oldOptions.noLib !== options.noLib) || (oldOptions.jsx !== options.jsx) || (oldOptions.allowJs !== options.allowJs) || - (oldOptions.disableSizeLimit !== options.disableSizeLimit) || + (oldOptions.disableSizeLimit !== options.disableSizeLimit) || (oldOptions.rootDir !== options.rootDir) || (oldOptions.typesSearchPaths !== options.typesSearchPaths) || (oldOptions.configFilePath !== options.configFilePath) || diff --git a/src/compiler/sys.ts b/src/compiler/sys.ts index 1e04e97e4c5..659c021c6be 100644 --- a/src/compiler/sys.ts +++ b/src/compiler/sys.ts @@ -28,7 +28,7 @@ namespace ts { getDirectories(path: string): string[]; readDirectory(path: string, extension?: string, exclude?: string[]): string[]; readDirectoryWithMultipleExtensions?(path: string, extensions: string[], exclude?: string[]): string[]; - getModifiedTime?(path: string): Date; + getModifiedTime?(path: string): Date; createHash?(data: string): string; getMemoryUsage?(): number; exit(exitCode?: number): void; @@ -549,7 +549,7 @@ namespace ts { getDirectories, readDirectory, readDirectoryWithMultipleExtensions, - getModifiedTime(path) { + getModifiedTime(path) { try { return _fs.statSync(path).mtime; } diff --git a/src/server/editorServices.ts b/src/server/editorServices.ts index 9f361cb3703..bd29dc65ac4 100644 --- a/src/server/editorServices.ts +++ b/src/server/editorServices.ts @@ -1217,7 +1217,7 @@ namespace ts.server { else { this.log("No config files found."); } - this.log("end openOrUpdateConfiguredProjectForFile: " + new Date().getTime()); + this.log("end openOrUpdateConfiguredProjectForFile: " + new Date().getTime()); return configFileName ? { configFileName } : {}; } @@ -1398,7 +1398,7 @@ namespace ts.server { } } - updateConfiguredProject(project: Project) { + updateConfiguredProject(project: Project): Diagnostic[] { if (!this.host.fileExists(project.projectFilename)) { this.log("Config file deleted"); this.removeProject(project); @@ -1434,9 +1434,6 @@ namespace ts.server { const info = this.openFile(rootFilename, /*openedByClient*/ false); project.addRoot(info); } - else { - return { errorMsg: "specified file " + rootFilename + " not found" }; - } } project.finishGraph(); return; @@ -1460,9 +1457,6 @@ namespace ts.server { for (const fileName of fileNamesToAdd) { let info = this.getScriptInfo(fileName); if (!info) { - if (!this.host.fileExists(info.fileName)) { - return { errorMsg: "specified file " + info.fileName + " not found" }; - } info = this.openFile(fileName, /*openedByClient*/ false); } else { diff --git a/src/server/session.ts b/src/server/session.ts index 6f07e979906..17fe4b758f2 100644 --- a/src/server/session.ts +++ b/src/server/session.ts @@ -444,11 +444,12 @@ namespace ts.server { const file = ts.normalizePath(fileName); const info = this.projectService.getScriptInfo(file); const projects = this.projectService.findReferencingProjects(info); - if (!projects.length) { + const projectsWithLanguageServiceEnabeld = ts.filter(projects, p => !p.languageServiceDiabled); + if (projects.length === 0 || projectsWithLanguageServiceEnabeld.length === 0) { throw Errors.NoProject; } - const defaultProject = projects[0]; + const defaultProject = projectsWithLanguageServiceEnabeld[0]; // The rename info should be the same for every project const defaultProjectCompilerService = defaultProject.compilerService; const position = defaultProjectCompilerService.host.lineOffsetToPosition(file, line, offset); @@ -465,7 +466,7 @@ namespace ts.server { } const fileSpans = combineProjectOutput( - projects, + projectsWithLanguageServiceEnabeld, (project: Project) => { const compilerService = project.compilerService; const renameLocations = compilerService.languageService.findRenameLocations(file, position, findInStrings, findInComments); @@ -526,11 +527,12 @@ namespace ts.server { const file = ts.normalizePath(fileName); const info = this.projectService.getScriptInfo(file); const projects = this.projectService.findReferencingProjects(info); - if (!projects.length) { + const projectsWithLanguageServiceEnabeld = ts.filter(projects, p => !p.languageServiceDiabled); + if (projects.length === 0 || projectsWithLanguageServiceEnabeld.length === 0) { throw Errors.NoProject; } - const defaultProject = projects[0]; + const defaultProject = projectsWithLanguageServiceEnabeld[0]; const position = defaultProject.compilerService.host.lineOffsetToPosition(file, line, offset); const nameInfo = defaultProject.compilerService.languageService.getQuickInfoAtPosition(file, position); if (!nameInfo) { @@ -542,7 +544,7 @@ namespace ts.server { const nameColStart = defaultProject.compilerService.host.positionToLineOffset(file, nameSpan.start).offset; const nameText = defaultProject.compilerService.host.getScriptSnapshot(file).getText(nameSpan.start, ts.textSpanEnd(nameSpan)); const refs = combineProjectOutput( - projects, + projectsWithLanguageServiceEnabeld, (project: Project) => { const compilerService = project.compilerService; const references = compilerService.languageService.getReferencesAtPosition(file, position); @@ -902,13 +904,13 @@ namespace ts.server { const file = ts.normalizePath(fileName); const info = this.projectService.getScriptInfo(file); const projects = this.projectService.findReferencingProjects(info); - const defaultProject = projects[0]; - if (!defaultProject) { + const projectsWithLanguageServiceEnabeld = ts.filter(projects, p => !p.languageServiceDiabled); + if (projects.length === 0 || projectsWithLanguageServiceEnabeld.length === 0) { throw Errors.NoProject; } const allNavToItems = combineProjectOutput( - projects, + projectsWithLanguageServiceEnabeld, (project: Project) => { const compilerService = project.compilerService; const navItems = compilerService.languageService.getNavigateToItems(searchValue, maxResultCount); From 4383f1a15f43a99286a2bdf0b35cde83e7bb3e1e Mon Sep 17 00:00:00 2001 From: zhengbli Date: Thu, 9 Jun 2016 16:28:42 -0700 Subject: [PATCH 11/56] Refactoring --- src/compiler/commandLineParser.ts | 2 +- src/compiler/diagnosticMessages.json | 4 --- src/compiler/program.ts | 38 ++-------------------------- src/server/editorServices.ts | 26 ++++++++++++++++--- src/server/protocol.d.ts | 3 +++ 5 files changed, 28 insertions(+), 45 deletions(-) diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index f8ab0330dad..ccde539fafa 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -408,7 +408,7 @@ namespace ts { description: Diagnostics.Specify_library_files_to_be_included_in_the_compilation_Colon }, { - name: "disableSizeLimit", + name: "disableProjectSizeLimit", type: "boolean" }, { diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 47fa84dd9ce..44062885677 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -2984,9 +2984,5 @@ "Unknown typing option '{0}'.": { "category": "Error", "code": 17010 - }, - "Too many JavaScript files in the project. Consider specifying the 'exclude' setting in project configuration to limit included source folders. The likely folder to exclude is '{0}'. To disable the project size limit, set the 'disableSizeLimit' compiler option to 'true'.": { - "category": "Error", - "code": 17012 } } diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 99ca9377d54..06f32c4f4a2 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -7,9 +7,6 @@ namespace ts { /* @internal */ export let emitTime = 0; /* @internal */ export let ioReadTime = 0; /* @internal */ export let ioWriteTime = 0; - /* @internal */ export const maxProgramSizeForNonTsFiles = 20 * 1024 * 1024; - - /** The version of the TypeScript compiler release */ const emptyArray: any[] = []; @@ -19,6 +16,7 @@ namespace ts { "node_modules/@types/", ]; + /** The version of the TypeScript compiler release */ export const version = "1.9.0"; export function findConfigFile(searchPath: string, fileExists: (fileName: string) => boolean): string { @@ -1059,8 +1057,6 @@ namespace ts { let diagnosticsProducingTypeChecker: TypeChecker; let noDiagnosticsTypeChecker: TypeChecker; let classifiableNames: Map; - let programSizeLimitExceeded = false; - let programSizeForNonTsFiles = 0; let resolvedTypeReferenceDirectives: Map = {}; let fileProcessingDiagnostics = createDiagnosticCollection(); @@ -1166,11 +1162,6 @@ namespace ts { return program; - function exceedProgramSizeLimit() { - return !options.disableSizeLimit && programSizeLimitExceeded; - } - - function getCommonSourceDirectory() { if (typeof commonSourceDirectory === "undefined") { if (options.rootDir && checkSourceFilesBelongToPath(files, options.rootDir)) { @@ -1219,7 +1210,6 @@ namespace ts { (oldOptions.noLib !== options.noLib) || (oldOptions.jsx !== options.jsx) || (oldOptions.allowJs !== options.allowJs) || - (oldOptions.disableSizeLimit !== options.disableSizeLimit) || (oldOptions.rootDir !== options.rootDir) || (oldOptions.typesSearchPaths !== options.typesSearchPaths) || (oldOptions.configFilePath !== options.configFilePath) || @@ -1838,7 +1828,7 @@ namespace ts { } } - if (diagnostic && !exceedProgramSizeLimit()) { + if (diagnostic) { if (refFile !== undefined && refEnd !== undefined && refPos !== undefined) { fileProcessingDiagnostics.add(createFileDiagnostic(refFile, refPos, refEnd - refPos, diagnostic, ...diagnosticArgument)); } @@ -1871,11 +1861,6 @@ namespace ts { return file; } - const isNonTsFile = !hasTypeScriptFileExtension(fileName); - if (isNonTsFile && exceedProgramSizeLimit()) { - return undefined; - } - // We haven't looked for this file, do so now and cache result const file = host.getSourceFile(fileName, options.target, hostErrorMessage => { if (refFile !== undefined && refPos !== undefined && refEnd !== undefined) { @@ -1887,25 +1872,6 @@ namespace ts { } }); - if (!options.disableSizeLimit && file && file.text && !hasTypeScriptFileExtension(file.fileName)) { - programSizeForNonTsFiles += file.text.length; - if (programSizeForNonTsFiles > maxProgramSizeForNonTsFiles) { - // If the program size limit was reached when processing a file, this file is - // likely in the problematic folder than contains too many files. - // Normally the folder is one level down from the commonSourceDirectory, for example, - // if the commonSourceDirectory is "/src/", and the last processed path was "/src/node_modules/a/b.js", - // we should show in the error message "/src/node_modules/". - const commonSourceDirectory = getCommonSourceDirectory(); - let rootLevelDirectory = path.substring(0, Math.max(commonSourceDirectory.length, path.indexOf(directorySeparator, commonSourceDirectory.length))); - if (rootLevelDirectory[rootLevelDirectory.length - 1] !== directorySeparator) { - rootLevelDirectory += directorySeparator; - } - programDiagnostics.add(createCompilerDiagnostic(Diagnostics.Too_many_JavaScript_files_in_the_project_Consider_specifying_the_exclude_setting_in_project_configuration_to_limit_included_source_folders_The_likely_folder_to_exclude_is_0_To_disable_the_project_size_limit_set_the_disableSizeLimit_compiler_option_to_true, rootLevelDirectory)); - programSizeLimitExceeded = true; - return undefined; - } - } - filesByName.set(path, file); if (file) { file.path = path; diff --git a/src/server/editorServices.ts b/src/server/editorServices.ts index bd29dc65ac4..5af2989a717 100644 --- a/src/server/editorServices.ts +++ b/src/server/editorServices.ts @@ -27,6 +27,8 @@ namespace ts.server { }); } + export const maxProgramSizeForNonTsFiles = 20 * 1024 * 1024; + export class ScriptInfo { svc: ScriptVersionCache; children: ScriptInfo[] = []; // files referenced by this file @@ -384,7 +386,7 @@ namespace ts.server { constructor( public projectService: ProjectService, public projectOptions?: ProjectOptions, - public languageServiceDiabled?: boolean) { + public languageServiceDiabled = false) { if (projectOptions && projectOptions.files) { // If files are listed explicitly, allow all extensions projectOptions.compilerOptions.allowNonTsExtensions = true; @@ -430,7 +432,16 @@ namespace ts.server { getFileNames() { if (this.languageServiceDiabled) { - return this.projectOptions ? this.projectOptions.files : undefined; + if (!this.projectOptions) { + return undefined; + } + + const fileNames: string[] = []; + if (this.projectOptions && this.projectOptions.compilerOptions) { + fileNames.push(getDefaultLibFilePath(this.projectOptions.compilerOptions)); + } + ts.addRange(fileNames, this.projectOptions.files); + return fileNames; } const sourceFiles = this.program.getSourceFiles(); @@ -1340,6 +1351,10 @@ namespace ts.server { private exceedTotalNonTsFileSizeLimit(fileNames: string[]) { let totalNonTsFileSize = 0; + if (!this.host.getFileSize) { + return false; + } + for (const fileName of fileNames) { if (hasTypeScriptFileExtension(fileName)) { continue; @@ -1409,14 +1424,17 @@ namespace ts.server { return errors; } else { - if (this.exceedTotalNonTsFileSizeLimit(projectOptions.files)) { + if (this.exceedTotalNonTsFileSizeLimit(projectOptions.files) && projectOptions.compilerOptions && !projectOptions.compilerOptions.disableSizeLimit) { project.setProjectOptions(projectOptions); if (project.languageServiceDiabled) { return; } project.disableLanguageService(); - project.directoryWatcher.close(); + if (project.directoryWatcher) { + project.directoryWatcher.close(); + project.directoryWatcher = undefined; + } return; } diff --git a/src/server/protocol.d.ts b/src/server/protocol.d.ts index 018162d0fff..4ebf1aff5d6 100644 --- a/src/server/protocol.d.ts +++ b/src/server/protocol.d.ts @@ -123,6 +123,9 @@ declare namespace ts.server.protocol { * The list of normalized file name in the project, including 'lib.d.ts' */ fileNames?: string[]; + /** + * Indicates if the project has a active language service instance + */ languageServiceDisabled?: boolean; } From e41b10bbc743516b991d072540128034b5913c6d Mon Sep 17 00:00:00 2001 From: zhengbli Date: Fri, 10 Jun 2016 01:42:35 -0700 Subject: [PATCH 12/56] add test and spit commandLineParser changes to another PR --- src/compiler/commandLineParser.ts | 16 +----- src/compiler/sys.ts | 57 +++++-------------- src/compiler/types.ts | 1 - src/server/editorServices.ts | 8 --- src/server/protocol.d.ts | 4 +- src/server/session.ts | 2 - .../cases/unittests/tsserverProjectSystem.ts | 48 +++++++++++++++- 7 files changed, 66 insertions(+), 70 deletions(-) diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index ccde539fafa..7a73949cf7a 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -727,22 +727,10 @@ namespace ts { const supportedExtensions = getSupportedExtensions(options); Debug.assert(indexOf(supportedExtensions, ".ts") < indexOf(supportedExtensions, ".d.ts"), "Changed priority of extensions to pick"); - const potentialFiles: string[] = []; - if (host.readDirectoryWithMultipleExtensions) { - addRange(potentialFiles, host.readDirectoryWithMultipleExtensions(basePath, supportedExtensions, exclude)); - } - else { - for (const extension of supportedExtensions) { - addRange(potentialFiles, host.readDirectory(basePath, extension, exclude)); - } - } // Get files of supported extensions in their order of resolution for (const extension of supportedExtensions) { - for (const fileName of potentialFiles) { - if (!fileExtensionIs(fileName, extension)) { - continue; - } - + const filesInDirWithExtension = host.readDirectory(basePath, extension, exclude); + for (const fileName of filesInDirWithExtension) { // .ts extension would read the .d.ts extension files too but since .d.ts is lower priority extension, // lets pick them when its turn comes up if (extension === ".ts" && fileExtensionIs(fileName, ".d.ts")) { diff --git a/src/compiler/sys.ts b/src/compiler/sys.ts index 659c021c6be..db4270ad271 100644 --- a/src/compiler/sys.ts +++ b/src/compiler/sys.ts @@ -27,7 +27,6 @@ namespace ts { getCurrentDirectory(): string; getDirectories(path: string): string[]; readDirectory(path: string, extension?: string, exclude?: string[]): string[]; - readDirectoryWithMultipleExtensions?(path: string, extensions: string[], exclude?: string[]): string[]; getModifiedTime?(path: string): Date; createHash?(data: string): string; getMemoryUsage?(): number; @@ -416,23 +415,25 @@ namespace ts { return filter(_fs.readdirSync(path), p => fileSystemEntryExists(combinePaths(path, p), FileSystemEntryKind.Directory)); } - function visitDirectory(path: string, result: string[], extension: string | string[], exclude: string[]) { - const files = _fs.readdirSync(path || ".").sort(); - const directories: string[] = []; - for (const current of files) { + function readDirectory(path: string, extension?: string, exclude?: string[]): string[] { + const result: string[] = []; + exclude = map(exclude, s => getCanonicalPath(combinePaths(path, s))); + visitDirectory(path); + return result; + function visitDirectory(path: string) { + const files = _fs.readdirSync(path || ".").sort(); + const directories: string[] = []; + for (const current of files) { // This is necessary because on some file system node fails to exclude // "." and "..". See https://github.com/nodejs/node/issues/4002 if (current === "." || current === "..") { continue; } - const name = combinePaths(path, current); - if (!contains(exclude, getCanonicalPath(name))) { - // fs.statSync would throw an exception if the file is a symlink - // whose linked file doesn't exist. - try { + const name = combinePaths(path, current); + if (!contains(exclude, getCanonicalPath(name))) { const stat = _fs.statSync(name); if (stat.isFile()) { - if (checkExtension(name)) { + if (!extension || fileExtensionIs(name, extension)) { result.push(name); } } @@ -440,38 +441,11 @@ namespace ts { directories.push(name); } } - catch (e) { } + } + for (const current of directories) { + visitDirectory(current); } } - for (const current of directories) { - visitDirectory(current, result, extension, exclude); - } - - function checkExtension(name: string) { - if (!extension) { - return true; - } - if (typeof extension === "string") { - return fileExtensionIs(name, extension); - } - else { - return forEach(extension, ext => fileExtensionIs(name, ext)); - } - } - } - - function readDirectoryWithMultipleExtensions(path: string, extensions: string[], exclude?: string[]): string[] { - const result: string[] = []; - exclude = map(exclude, s => getCanonicalPath(combinePaths(path, s))); - visitDirectory(path, result, extensions, exclude); - return result; - } - - function readDirectory(path: string, extension?: string, exclude?: string[]): string[] { - const result: string[] = []; - exclude = map(exclude, s => getCanonicalPath(combinePaths(path, s))); - visitDirectory(path, result, extension, exclude); - return result; } return { @@ -548,7 +522,6 @@ namespace ts { }, getDirectories, readDirectory, - readDirectoryWithMultipleExtensions, getModifiedTime(path) { try { return _fs.statSync(path).mtime; diff --git a/src/compiler/types.ts b/src/compiler/types.ts index a386b3c1ff2..d43cd9f910d 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -1670,7 +1670,6 @@ namespace ts { export interface ParseConfigHost { readDirectory(rootDir: string, extension: string, exclude: string[]): string[]; - readDirectoryWithMultipleExtensions?(rootDir: string, extensions: string[], exclude: string[]): string[]; } export interface WriteFileCallback { diff --git a/src/server/editorServices.ts b/src/server/editorServices.ts index 5af2989a717..572acc61a18 100644 --- a/src/server/editorServices.ts +++ b/src/server/editorServices.ts @@ -1184,12 +1184,10 @@ namespace ts.server { * @param fileContent is a known version of the file content that is more up to date than the one on disk */ openClientFile(fileName: string, fileContent?: string, scriptKind?: ScriptKind): { configFileName?: string, configFileErrors?: Diagnostic[] } { - this.log("start openClientFile: " + new Date().getTime()); const { configFileName, configFileErrors } = this.openOrUpdateConfiguredProjectForFile(fileName); const info = this.openFile(fileName, /*openedByClient*/ true, fileContent, scriptKind); this.addOpenFile(info); this.printProjects(); - this.log("end openClientFile: " + new Date().getTime()); return { configFileName, configFileErrors }; } @@ -1199,7 +1197,6 @@ namespace ts.server { * the tsconfig file content and update the project; otherwise we create a new one. */ openOrUpdateConfiguredProjectForFile(fileName: string): { configFileName?: string, configFileErrors?: Diagnostic[] } { - this.log("start openOrUpdateConfiguredProjectForFile: " + new Date().getTime()); const searchPath = ts.normalizePath(getDirectoryPath(fileName)); this.log("Search path: " + searchPath, "Info"); const configFileName = this.findConfigFile(searchPath); @@ -1228,7 +1225,6 @@ namespace ts.server { else { this.log("No config files found."); } - this.log("end openOrUpdateConfiguredProjectForFile: " + new Date().getTime()); return configFileName ? { configFileName } : {}; } @@ -1368,11 +1364,8 @@ namespace ts.server { } openConfigFile(configFilename: string, clientFileName?: string): { success: boolean, project?: Project, errors?: Diagnostic[] } { - this.log("start openConfigFile: " + new Date().getTime()); const { succeeded, projectOptions, errors } = this.configFileToProjectOptions(configFilename); - this.log("finish reading config file: " + new Date().getTime()); if (!succeeded) { - this.log("finish openConfigFile: " + new Date().getTime()); return { success: false, errors }; } else { @@ -1408,7 +1401,6 @@ namespace ts.server { path => this.directoryWatchedForSourceFilesChanged(project, path), /*recursive*/ true ); - this.log("finish openConfigFile: " + new Date().getTime()); return { success: true, project: project, errors }; } } diff --git a/src/server/protocol.d.ts b/src/server/protocol.d.ts index 4ebf1aff5d6..4cdd331f1d9 100644 --- a/src/server/protocol.d.ts +++ b/src/server/protocol.d.ts @@ -124,8 +124,8 @@ declare namespace ts.server.protocol { */ fileNames?: string[]; /** - * Indicates if the project has a active language service instance - */ + * Indicates if the project has a active language service instance + */ languageServiceDisabled?: boolean; } diff --git a/src/server/session.ts b/src/server/session.ts index 17fe4b758f2..fa5fd378a0b 100644 --- a/src/server/session.ts +++ b/src/server/session.ts @@ -421,7 +421,6 @@ namespace ts.server { } private getProjectInfo(fileName: string, needFileNameList: boolean): protocol.ProjectInfo { - this.logger.info("start getProjectInfo:" + new Date().getTime()); fileName = ts.normalizePath(fileName); const project = this.projectService.getProjectForFile(fileName); if (!project) { @@ -436,7 +435,6 @@ namespace ts.server { if (needFileNameList) { projectInfo.fileNames = project.getFileNames(); } - this.logger.info("end getProjectInfo:" + new Date().getTime()); return projectInfo; } diff --git a/tests/cases/unittests/tsserverProjectSystem.ts b/tests/cases/unittests/tsserverProjectSystem.ts index cf382d09463..d7b808f79b7 100644 --- a/tests/cases/unittests/tsserverProjectSystem.ts +++ b/tests/cases/unittests/tsserverProjectSystem.ts @@ -25,6 +25,7 @@ namespace ts { interface FileOrFolder { path: string; content?: string; + fileSize?: number; } interface FSEntry { @@ -34,6 +35,7 @@ namespace ts { interface File extends FSEntry { content: string; + fileSize?: number; } interface Folder extends FSEntry { @@ -157,7 +159,7 @@ namespace ts { const path = this.toPath(fileOrFolder.path); const fullPath = getNormalizedAbsolutePath(fileOrFolder.path, this.currentDirectory); if (typeof fileOrFolder.content === "string") { - const entry = { path, content: fileOrFolder.content, fullPath }; + const entry = { path, content: fileOrFolder.content, fullPath, fileSize: fileOrFolder.fileSize }; this.fs.set(path, entry); addFolder(getDirectoryPath(fullPath), this.toPath, this.fs).entries.push(entry); } @@ -172,6 +174,17 @@ namespace ts { return this.fs.contains(path) && isFile(this.fs.get(path)); }; + getFileSize(s: string) { + const path = this.toPath(s); + if (this.fs.contains(path)) { + const entry = this.fs.get(path); + if (isFile(entry)) { + return entry.fileSize ? entry.fileSize : entry.content.length; + } + } + return undefined; + } + directoryExists(s: string) { const path = this.toPath(s); return this.fs.contains(path) && isFolder(this.fs.get(path)); @@ -567,5 +580,38 @@ namespace ts { checkConfiguredProjectActualFiles(project, [file1.path, classicModuleFile.path]); checkNumberOfInferredProjects(projectService, 1); }); + + it("should disable language service for project with too many non-ts files", () => { + const jsFiles: FileOrFolder[] = []; + const configFile: FileOrFolder = { + path: `/a/b/jsconfig.json`, + content: "{}" + }; + jsFiles.push(configFile); + for (let i = 0; i < 1000; i++) { + jsFiles.push({ + path: `/a/b/file${i}.js`, + content: "", + fileSize: 50000 + }); + } + + const host = new TestServerHost(/*useCaseSensitiveFileNames*/ false, getExecutingFilePathFromLibFile(libFile), "/", jsFiles); + const projectService = new server.ProjectService(host, nullLogger); + projectService.openClientFile(jsFiles[1].path); + projectService.openClientFile(jsFiles[2].path); + checkNumberOfConfiguredProjects(projectService, 1); + checkNumberOfInferredProjects(projectService, 0); + + const project = projectService.configuredProjects[0]; + assert(project.languageServiceDiabled, "the project's language service is expected to be disabled"); + + configFile.content = `{ + "files": ["/a/b/file1.js", "/a/b/file2.js", "/a/b/file3.js"] + }`; + host.reloadFS(jsFiles); + host.triggerFileWatcherCallback(configFile.path); + assert(!project.languageServiceDiabled, "after the config file change, the project's language service is expected to be enabled."); + }); }); } \ No newline at end of file From 1e7790df06ab2070899bdc427eb001e0a0e7181a Mon Sep 17 00:00:00 2001 From: zhengbli Date: Fri, 10 Jun 2016 03:44:44 -0700 Subject: [PATCH 13/56] Fix #8523 --- src/server/editorServices.ts | 1 + .../cases/unittests/tsserverProjectSystem.ts | 27 +++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/server/editorServices.ts b/src/server/editorServices.ts index 628522c7aa6..ea43e2a5853 100644 --- a/src/server/editorServices.ts +++ b/src/server/editorServices.ts @@ -803,6 +803,7 @@ namespace ts.server { else { this.findReferencingProjects(info); if (info.defaultProject) { + info.defaultProject.addOpenRef(); this.openFilesReferenced.push(info); } else { diff --git a/tests/cases/unittests/tsserverProjectSystem.ts b/tests/cases/unittests/tsserverProjectSystem.ts index cf382d09463..bd049632b9d 100644 --- a/tests/cases/unittests/tsserverProjectSystem.ts +++ b/tests/cases/unittests/tsserverProjectSystem.ts @@ -567,5 +567,32 @@ namespace ts { checkConfiguredProjectActualFiles(project, [file1.path, classicModuleFile.path]); checkNumberOfInferredProjects(projectService, 1); }); + + it("should keep the configured project when the opened file is referenced by the project but not its root", () => { + const file1: FileOrFolder = { + path: "/a/b/main.ts", + content: "import { objA } from './obj-a';" + }; + const file2: FileOrFolder = { + path: "/a/b/obj-a.ts", + content: `export const objA = Object.assign({foo: "bar"}, {bar: "baz"});` + }; + const configFile: FileOrFolder = { + path: "/a/b/tsconfig.json", + content: `{ + "compilerOptions": { + "target": "es6" + }, + "files": [ "main.ts" ] + }` + }; + const host = new TestServerHost(/*useCaseSensitiveFileNames*/ false, getExecutingFilePathFromLibFile(libFile), "/", [file1, file2, configFile]); + const projectService = new server.ProjectService(host, nullLogger); + projectService.openClientFile(file1.path); + projectService.closeClientFile(file1.path); + projectService.openClientFile(file2.path); + checkNumberOfConfiguredProjects(projectService, 1); + checkNumberOfInferredProjects(projectService, 0); + }); }); } \ No newline at end of file From 85ac67f4ee207b4af01e708f4a114d23289e1147 Mon Sep 17 00:00:00 2001 From: tinza123 Date: Fri, 10 Jun 2016 11:07:01 -0700 Subject: [PATCH 14/56] check the declaration and use order if both are not in module file --- src/compiler/checker.ts | 3 ++- src/services/services.ts | 1 + .../constDeclarations-useBeforeDefinition2.errors.txt | 11 +++++++++++ .../jsFileCompilationLetDeclarationOrder2.errors.txt | 5 ++++- .../letDeclarations-useBeforeDefinition2.errors.txt | 11 +++++++++++ 5 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 tests/baselines/reference/constDeclarations-useBeforeDefinition2.errors.txt create mode 100644 tests/baselines/reference/letDeclarations-useBeforeDefinition2.errors.txt diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 044f42414b7..26c58ebe752 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -562,7 +562,8 @@ namespace ts { const declarationFile = getSourceFileOfNode(declaration); const useFile = getSourceFileOfNode(usage); if (declarationFile !== useFile) { - if (modulekind || (!compilerOptions.outFile && !compilerOptions.out)) { + if ((modulekind && (declarationFile.externalModuleIndicator || useFile.externalModuleIndicator)) || + (!compilerOptions.outFile && !compilerOptions.out)) { // nodes are in different files and order cannot be determines return true; } diff --git a/src/services/services.ts b/src/services/services.ts index cae2af356a0..e87a9f0d475 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -1,4 +1,5 @@ /// +/// /// /// diff --git a/tests/baselines/reference/constDeclarations-useBeforeDefinition2.errors.txt b/tests/baselines/reference/constDeclarations-useBeforeDefinition2.errors.txt new file mode 100644 index 00000000000..a4d28f51878 --- /dev/null +++ b/tests/baselines/reference/constDeclarations-useBeforeDefinition2.errors.txt @@ -0,0 +1,11 @@ +tests/cases/compiler/file1.ts(2,1): error TS2448: Block-scoped variable 'c' used before its declaration. + + +==== tests/cases/compiler/file1.ts (1 errors) ==== + + c; + ~ +!!! error TS2448: Block-scoped variable 'c' used before its declaration. + +==== tests/cases/compiler/file2.ts (0 errors) ==== + const c = 0; \ No newline at end of file diff --git a/tests/baselines/reference/jsFileCompilationLetDeclarationOrder2.errors.txt b/tests/baselines/reference/jsFileCompilationLetDeclarationOrder2.errors.txt index 21390cc2955..b88c73e0439 100644 --- a/tests/baselines/reference/jsFileCompilationLetDeclarationOrder2.errors.txt +++ b/tests/baselines/reference/jsFileCompilationLetDeclarationOrder2.errors.txt @@ -1,10 +1,13 @@ error TS5053: Option 'allowJs' cannot be specified with option 'declaration'. +tests/cases/compiler/a.ts(2,1): error TS2448: Block-scoped variable 'a' used before its declaration. !!! error TS5053: Option 'allowJs' cannot be specified with option 'declaration'. -==== tests/cases/compiler/a.ts (0 errors) ==== +==== tests/cases/compiler/a.ts (1 errors) ==== let b = 30; a = 10; + ~ +!!! error TS2448: Block-scoped variable 'a' used before its declaration. ==== tests/cases/compiler/b.js (0 errors) ==== let a = 10; b = 30; diff --git a/tests/baselines/reference/letDeclarations-useBeforeDefinition2.errors.txt b/tests/baselines/reference/letDeclarations-useBeforeDefinition2.errors.txt new file mode 100644 index 00000000000..5b8633312d9 --- /dev/null +++ b/tests/baselines/reference/letDeclarations-useBeforeDefinition2.errors.txt @@ -0,0 +1,11 @@ +tests/cases/compiler/file1.ts(2,1): error TS2448: Block-scoped variable 'l' used before its declaration. + + +==== tests/cases/compiler/file1.ts (1 errors) ==== + + l; + ~ +!!! error TS2448: Block-scoped variable 'l' used before its declaration. + +==== tests/cases/compiler/file2.ts (0 errors) ==== + const l = 0; \ No newline at end of file From 4a8f94a5535548a3b9cc8eaf4c02c0830fba088f Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Fri, 10 Jun 2016 16:17:32 -0700 Subject: [PATCH 15/56] Type guards using discriminant properties of string literal types --- src/compiler/binder.ts | 56 ++++++++++++++++++++++++++++++----------- src/compiler/checker.ts | 47 ++++++++++++++++++++++++++++------ 2 files changed, 80 insertions(+), 23 deletions(-) diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index 8c48d984b78..f9d0f443347 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -577,12 +577,6 @@ namespace ts { } } - function isNarrowableReference(expr: Expression): boolean { - return expr.kind === SyntaxKind.Identifier || - expr.kind === SyntaxKind.ThisKeyword || - expr.kind === SyntaxKind.PropertyAccessExpression && isNarrowableReference((expr).expression); - } - function isNarrowingExpression(expr: Expression): boolean { switch (expr.kind) { case SyntaxKind.Identifier: @@ -590,7 +584,7 @@ namespace ts { case SyntaxKind.PropertyAccessExpression: return isNarrowableReference(expr); case SyntaxKind.CallExpression: - return true; + return hasNarrowableArgument(expr); case SyntaxKind.ParenthesizedExpression: return isNarrowingExpression((expr).expression); case SyntaxKind.BinaryExpression: @@ -601,6 +595,27 @@ namespace ts { return false; } + function isNarrowableReference(expr: Expression): boolean { + return expr.kind === SyntaxKind.Identifier || + expr.kind === SyntaxKind.ThisKeyword || + expr.kind === SyntaxKind.PropertyAccessExpression && isNarrowableReference((expr).expression); + } + + function hasNarrowableArgument(expr: CallExpression) { + if (expr.arguments) { + for (const argument of expr.arguments) { + if (isNarrowableReference(argument)) { + return true; + } + } + } + if (expr.expression.kind === SyntaxKind.PropertyAccessExpression && + isNarrowableReference((expr.expression).expression)) { + return true; + } + return false; + } + function isNarrowingBinaryExpression(expr: BinaryExpression) { switch (expr.operatorToken.kind) { case SyntaxKind.EqualsToken: @@ -609,21 +624,32 @@ namespace ts { case SyntaxKind.ExclamationEqualsToken: case SyntaxKind.EqualsEqualsEqualsToken: case SyntaxKind.ExclamationEqualsEqualsToken: - if (isNarrowingExpression(expr.left) && (expr.right.kind === SyntaxKind.NullKeyword || expr.right.kind === SyntaxKind.Identifier)) { - return true; - } - if (expr.left.kind === SyntaxKind.TypeOfExpression && isNarrowingExpression((expr.left).expression) && expr.right.kind === SyntaxKind.StringLiteral) { - return true; - } - return false; + return (expr.right.kind === SyntaxKind.NullKeyword || expr.right.kind === SyntaxKind.Identifier && (expr.right).text === "undefined") && isNarrowableOperand(expr.left) || + expr.left.kind === SyntaxKind.PropertyAccessExpression && isNarrowableReference((expr.left).expression) || + expr.left.kind === SyntaxKind.TypeOfExpression && isNarrowableOperand((expr.left).expression) && expr.right.kind === SyntaxKind.StringLiteral; case SyntaxKind.InstanceOfKeyword: - return isNarrowingExpression(expr.left); + return isNarrowableOperand(expr.left); case SyntaxKind.CommaToken: return isNarrowingExpression(expr.right); } return false; } + function isNarrowableOperand(expr: Expression): boolean { + switch (expr.kind) { + case SyntaxKind.ParenthesizedExpression: + return isNarrowableOperand((expr).expression); + case SyntaxKind.BinaryExpression: + switch ((expr).operatorToken.kind) { + case SyntaxKind.EqualsToken: + return isNarrowableOperand((expr).left); + case SyntaxKind.CommaToken: + return isNarrowableOperand((expr).right); + } + } + return isNarrowableReference(expr); + } + function createBranchLabel(): FlowLabel { return { flags: FlowFlags.BranchLabel, diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 65335117cf2..105401c0015 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -5160,7 +5160,6 @@ namespace ts { if (hasProperty(stringLiteralTypes, text)) { return stringLiteralTypes[text]; } - const type = stringLiteralTypes[text] = createType(TypeFlags.StringLiteral); type.text = text; return type; @@ -5625,6 +5624,10 @@ namespace ts { return checkTypeComparableTo(source, target, /*errorNode*/ undefined); } + function areTypesComparable(type1: Type, type2: Type): boolean { + return isTypeComparableTo(type1, type2) || isTypeComparableTo(type2, type1); + } + function checkTypeSubtypeOf(source: Type, target: Type, errorNode: Node, headMessage?: DiagnosticMessage, containingMessageChain?: DiagnosticMessageChain): boolean { return checkTypeRelatedTo(source, target, subtypeRelation, errorNode, headMessage, containingMessageChain); } @@ -6805,8 +6808,10 @@ namespace ts { return !!getPropertyOfType(type, "0"); } - function isStringLiteralType(type: Type) { - return type.flags & TypeFlags.StringLiteral; + function isStringLiteralUnionType(type: Type): boolean { + return type.flags & TypeFlags.StringLiteral ? true : + type.flags & TypeFlags.Union ? forEach((type).types, isStringLiteralUnionType) : + false; } /** @@ -7873,6 +7878,9 @@ namespace ts { if (isNullOrUndefinedLiteral(expr.right)) { return narrowTypeByNullCheck(type, expr, assumeTrue); } + if (expr.left.kind === SyntaxKind.PropertyAccessExpression) { + return narrowTypeByDiscriminant(type, expr, assumeTrue); + } if (expr.left.kind === SyntaxKind.TypeOfExpression && expr.right.kind === SyntaxKind.StringLiteral) { return narrowTypeByTypeof(type, expr, assumeTrue); } @@ -7903,6 +7911,33 @@ namespace ts { return getTypeWithFacts(type, facts); } + function narrowTypeByDiscriminant(type: Type, expr: BinaryExpression, assumeTrue: boolean): Type { + // We have '==', '!=', '===', or '!==' operator with property access on left + if (!(type.flags & TypeFlags.Union) || !isMatchingReference(reference, (expr.left).expression)) { + return type; + } + const propName = (expr.left).name.text; + const propType = getTypeOfPropertyOfType(type, propName); + if (!propType || !isStringLiteralUnionType(propType)) { + return type; + } + const discriminantType = expr.right.kind === SyntaxKind.StringLiteral ? getStringLiteralTypeForText((expr.right).text) : checkExpression(expr.right); + if (!isStringLiteralUnionType(discriminantType)) { + return type; + } + if (expr.operatorToken.kind === SyntaxKind.ExclamationEqualsToken || + expr.operatorToken.kind === SyntaxKind.ExclamationEqualsEqualsToken) { + assumeTrue = !assumeTrue; + } + if (assumeTrue) { + return getUnionType(filter((type).types, t => areTypesComparable(getTypeOfPropertyOfType(t, propName), discriminantType))); + } + if (discriminantType.flags & TypeFlags.StringLiteral) { + return getUnionType(filter((type).types, t => getTypeOfPropertyOfType(t, propName) !== discriminantType)); + } + return type; + } + function narrowTypeByTypeof(type: Type, expr: BinaryExpression, assumeTrue: boolean): Type { // We have '==', '!=', '====', or !==' operator with 'typeof xxx' on the left // and string literal on the right @@ -8892,10 +8927,6 @@ namespace ts { return applyToContextualType(type, t => getIndexTypeOfStructuredType(t, kind)); } - function contextualTypeIsStringLiteralType(type: Type): boolean { - return !!(type.flags & TypeFlags.Union ? forEach((type).types, isStringLiteralType) : isStringLiteralType(type)); - } - // Return true if the given contextual type is a tuple-like type function contextualTypeIsTupleLikeType(type: Type): boolean { return !!(type.flags & TypeFlags.Union ? forEach((type).types, isTupleLikeType) : isTupleLikeType(type)); @@ -12557,7 +12588,7 @@ namespace ts { function checkStringLiteralExpression(node: StringLiteral): Type { const contextualType = getContextualType(node); - if (contextualType && contextualTypeIsStringLiteralType(contextualType)) { + if (contextualType && isStringLiteralUnionType(contextualType)) { return getStringLiteralTypeForText(node.text); } From e86f1837d0c2ce2e835445f5a4d01c95a70572e9 Mon Sep 17 00:00:00 2001 From: Mohamed Hegazy Date: Sat, 11 Jun 2016 13:12:08 -0700 Subject: [PATCH 16/56] Fix #9098: report missing function impelementation errors for merged classes and namespaces --- src/compiler/checker.ts | 7 +- .../missingFunctionImplementation.errors.txt | 140 +++++++++++++++ .../missingFunctionImplementation.js | 168 ++++++++++++++++++ .../missingFunctionImplementation2.errors.txt | 16 ++ .../missingFunctionImplementation2.js | 15 ++ .../compiler/missingFunctionImplementation.ts | 79 ++++++++ .../missingFunctionImplementation2.ts | 8 + 7 files changed, 428 insertions(+), 5 deletions(-) create mode 100644 tests/baselines/reference/missingFunctionImplementation.errors.txt create mode 100644 tests/baselines/reference/missingFunctionImplementation.js create mode 100644 tests/baselines/reference/missingFunctionImplementation2.errors.txt create mode 100644 tests/baselines/reference/missingFunctionImplementation2.js create mode 100644 tests/cases/compiler/missingFunctionImplementation.ts create mode 100644 tests/cases/compiler/missingFunctionImplementation2.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 28082914256..f35a53ce368 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -13552,9 +13552,6 @@ namespace ts { } } - // when checking exported function declarations across modules check only duplicate implementations - // names and consistency of modifiers are verified when we check local symbol - const isExportSymbolInsideModule = symbol.parent && symbol.parent.flags & SymbolFlags.Module; let duplicateFunctionDeclaration = false; let multipleConstructorImplementation = false; for (const current of declarations) { @@ -13587,7 +13584,7 @@ namespace ts { duplicateFunctionDeclaration = true; } } - else if (!isExportSymbolInsideModule && previousDeclaration && previousDeclaration.parent === node.parent && previousDeclaration.end !== node.pos) { + else if (previousDeclaration && previousDeclaration.parent === node.parent && previousDeclaration.end !== node.pos) { reportImplementationExpectedError(previousDeclaration); } @@ -13621,7 +13618,7 @@ namespace ts { } // Abstract methods can't have an implementation -- in particular, they don't need one. - if (!isExportSymbolInsideModule && lastSeenNonAmbientDeclaration && !lastSeenNonAmbientDeclaration.body && + if (lastSeenNonAmbientDeclaration && !lastSeenNonAmbientDeclaration.body && !(lastSeenNonAmbientDeclaration.flags & NodeFlags.Abstract) && !lastSeenNonAmbientDeclaration.questionToken) { reportImplementationExpectedError(lastSeenNonAmbientDeclaration); } diff --git a/tests/baselines/reference/missingFunctionImplementation.errors.txt b/tests/baselines/reference/missingFunctionImplementation.errors.txt new file mode 100644 index 00000000000..4cf58d97d49 --- /dev/null +++ b/tests/baselines/reference/missingFunctionImplementation.errors.txt @@ -0,0 +1,140 @@ +tests/cases/compiler/missingFunctionImplementation.ts(3,3): error TS2391: Function implementation is missing or not immediately following the declaration. +tests/cases/compiler/missingFunctionImplementation.ts(8,3): error TS2391: Function implementation is missing or not immediately following the declaration. +tests/cases/compiler/missingFunctionImplementation.ts(16,3): error TS2391: Function implementation is missing or not immediately following the declaration. +tests/cases/compiler/missingFunctionImplementation.ts(22,10): error TS2391: Function implementation is missing or not immediately following the declaration. +tests/cases/compiler/missingFunctionImplementation.ts(28,10): error TS2391: Function implementation is missing or not immediately following the declaration. +tests/cases/compiler/missingFunctionImplementation.ts(33,10): error TS2391: Function implementation is missing or not immediately following the declaration. +tests/cases/compiler/missingFunctionImplementation.ts(41,10): error TS2391: Function implementation is missing or not immediately following the declaration. +tests/cases/compiler/missingFunctionImplementation.ts(48,10): error TS2300: Duplicate identifier 'm'. +tests/cases/compiler/missingFunctionImplementation.ts(49,10): error TS2300: Duplicate identifier 'm'. +tests/cases/compiler/missingFunctionImplementation.ts(49,10): error TS2391: Function implementation is missing or not immediately following the declaration. +tests/cases/compiler/missingFunctionImplementation.ts(52,19): error TS2300: Duplicate identifier 'm'. +tests/cases/compiler/missingFunctionImplementation.ts(57,10): error TS2300: Duplicate identifier 'm'. +tests/cases/compiler/missingFunctionImplementation.ts(60,19): error TS2300: Duplicate identifier 'm'. +tests/cases/compiler/missingFunctionImplementation.ts(60,19): error TS2391: Function implementation is missing or not immediately following the declaration. +tests/cases/compiler/missingFunctionImplementation.ts(65,19): error TS2391: Function implementation is missing or not immediately following the declaration. +tests/cases/compiler/missingFunctionImplementation.ts(73,19): error TS2393: Duplicate function implementation. +tests/cases/compiler/missingFunctionImplementation.ts(74,19): error TS2393: Duplicate function implementation. +tests/cases/compiler/missingFunctionImplementation.ts(75,19): error TS2393: Duplicate function implementation. +tests/cases/compiler/missingFunctionImplementation.ts(78,19): error TS2393: Duplicate function implementation. + + +==== tests/cases/compiler/missingFunctionImplementation.ts (19 errors) ==== + + export class C1 { + m(): void; + ~ +!!! error TS2391: Function implementation is missing or not immediately following the declaration. + } + + // merged with a namespace + export class C2 { + m(): void; + ~ +!!! error TS2391: Function implementation is missing or not immediately following the declaration. + } + export namespace C2 { } + + + // merged with a namespace, multiple overloads + class C3 { + m(a, b); + m(a); + ~ +!!! error TS2391: Function implementation is missing or not immediately following the declaration. + } + namespace C3 { } + + // static methods, multiple overloads + class C4 { + static m(a): void; + ~ +!!! error TS2391: Function implementation is missing or not immediately following the declaration. + } + + // static methods, multiple overloads + class C5 { + static m(a): void; + static m(): void; + ~ +!!! error TS2391: Function implementation is missing or not immediately following the declaration. + } + + // merged with namespace, static methods + class C6 { + static m(): void; + ~ +!!! error TS2391: Function implementation is missing or not immediately following the declaration. + } + namespace C6 { + } + + // merged with namespace, static methods, multiple overloads + class C7 { + static m(a): void; + static m(): void; + ~ +!!! error TS2391: Function implementation is missing or not immediately following the declaration. + } + namespace C7 { + } + + // merged with namespace, static methods, duplicate declarations + class C8 { + static m(a): void; + ~ +!!! error TS2300: Duplicate identifier 'm'. + static m(a, b): void; + ~ +!!! error TS2300: Duplicate identifier 'm'. + ~ +!!! error TS2391: Function implementation is missing or not immediately following the declaration. + } + namespace C8 { + export function m(a?, b?): void { } + ~ +!!! error TS2300: Duplicate identifier 'm'. + } + + // merged with namespace, static methods, duplicate declarations + class C9 { + static m(a): void { } + ~ +!!! error TS2300: Duplicate identifier 'm'. + } + namespace C9 { + export function m(a): void; + ~ +!!! error TS2300: Duplicate identifier 'm'. + ~ +!!! error TS2391: Function implementation is missing or not immediately following the declaration. + } + + // merged namespaces + namespace N10 { + export function m(a): void; + ~ +!!! error TS2391: Function implementation is missing or not immediately following the declaration. + } + namespace N10 { + export function m(a): void { } + } + + // merged namespaces, duplicate defintions + namespace N12 { + export function m(a): void; + ~ +!!! error TS2393: Duplicate function implementation. + export function m(): void; + ~ +!!! error TS2393: Duplicate function implementation. + export function m(a?): void { } + ~ +!!! error TS2393: Duplicate function implementation. + } + namespace N12 { + export function m(a): void { } + ~ +!!! error TS2393: Duplicate function implementation. + } + \ No newline at end of file diff --git a/tests/baselines/reference/missingFunctionImplementation.js b/tests/baselines/reference/missingFunctionImplementation.js new file mode 100644 index 00000000000..a452c8080b7 --- /dev/null +++ b/tests/baselines/reference/missingFunctionImplementation.js @@ -0,0 +1,168 @@ +//// [missingFunctionImplementation.ts] + +export class C1 { + m(): void; +} + +// merged with a namespace +export class C2 { + m(): void; +} +export namespace C2 { } + + +// merged with a namespace, multiple overloads +class C3 { + m(a, b); + m(a); +} +namespace C3 { } + +// static methods, multiple overloads +class C4 { + static m(a): void; +} + +// static methods, multiple overloads +class C5 { + static m(a): void; + static m(): void; +} + +// merged with namespace, static methods +class C6 { + static m(): void; +} +namespace C6 { +} + +// merged with namespace, static methods, multiple overloads +class C7 { + static m(a): void; + static m(): void; +} +namespace C7 { +} + +// merged with namespace, static methods, duplicate declarations +class C8 { + static m(a): void; + static m(a, b): void; +} +namespace C8 { + export function m(a?, b?): void { } +} + +// merged with namespace, static methods, duplicate declarations +class C9 { + static m(a): void { } +} +namespace C9 { + export function m(a): void; +} + +// merged namespaces +namespace N10 { + export function m(a): void; +} +namespace N10 { + export function m(a): void { } +} + +// merged namespaces, duplicate defintions +namespace N12 { + export function m(a): void; + export function m(): void; + export function m(a?): void { } +} +namespace N12 { + export function m(a): void { } +} + + +//// [missingFunctionImplementation.js] +"use strict"; +var C1 = (function () { + function C1() { + } + return C1; +}()); +exports.C1 = C1; +// merged with a namespace +var C2 = (function () { + function C2() { + } + return C2; +}()); +exports.C2 = C2; +// merged with a namespace, multiple overloads +var C3 = (function () { + function C3() { + } + return C3; +}()); +// static methods, multiple overloads +var C4 = (function () { + function C4() { + } + return C4; +}()); +// static methods, multiple overloads +var C5 = (function () { + function C5() { + } + return C5; +}()); +// merged with namespace, static methods +var C6 = (function () { + function C6() { + } + return C6; +}()); +// merged with namespace, static methods, multiple overloads +var C7 = (function () { + function C7() { + } + return C7; +}()); +// merged with namespace, static methods, duplicate declarations +var C8 = (function () { + function C8() { + } + return C8; +}()); +var C8; +(function (C8) { + function m(a, b) { } + C8.m = m; +})(C8 || (C8 = {})); +// merged with namespace, static methods, duplicate declarations +var C9 = (function () { + function C9() { + } + C9.m = function (a) { }; + return C9; +}()); +var C9; +(function (C9) { +})(C9 || (C9 = {})); +// merged namespaces +var N10; +(function (N10) { +})(N10 || (N10 = {})); +var N10; +(function (N10) { + function m(a) { } + N10.m = m; +})(N10 || (N10 = {})); +// merged namespaces, duplicate defintions +var N12; +(function (N12) { + function m(a) { } + N12.m = m; +})(N12 || (N12 = {})); +var N12; +(function (N12) { + function m(a) { } + N12.m = m; +})(N12 || (N12 = {})); diff --git a/tests/baselines/reference/missingFunctionImplementation2.errors.txt b/tests/baselines/reference/missingFunctionImplementation2.errors.txt new file mode 100644 index 00000000000..a695482e1fb --- /dev/null +++ b/tests/baselines/reference/missingFunctionImplementation2.errors.txt @@ -0,0 +1,16 @@ +tests/cases/compiler/missingFunctionImplementation2_a.ts(3,19): error TS2384: Overload signatures must all be ambient or non-ambient. +tests/cases/compiler/missingFunctionImplementation2_b.ts(1,17): error TS2391: Function implementation is missing or not immediately following the declaration. + + +==== tests/cases/compiler/missingFunctionImplementation2_a.ts (1 errors) ==== + export {}; + declare module "./missingFunctionImplementation2_b.ts" { + export function f(a, b): void; + ~ +!!! error TS2384: Overload signatures must all be ambient or non-ambient. + } + +==== tests/cases/compiler/missingFunctionImplementation2_b.ts (1 errors) ==== + export function f(a?, b?); + ~ +!!! error TS2391: Function implementation is missing or not immediately following the declaration. \ No newline at end of file diff --git a/tests/baselines/reference/missingFunctionImplementation2.js b/tests/baselines/reference/missingFunctionImplementation2.js new file mode 100644 index 00000000000..104b6dd9443 --- /dev/null +++ b/tests/baselines/reference/missingFunctionImplementation2.js @@ -0,0 +1,15 @@ +//// [tests/cases/compiler/missingFunctionImplementation2.ts] //// + +//// [missingFunctionImplementation2_a.ts] +export {}; +declare module "./missingFunctionImplementation2_b.ts" { + export function f(a, b): void; +} + +//// [missingFunctionImplementation2_b.ts] +export function f(a?, b?); + +//// [missingFunctionImplementation2_a.js] +"use strict"; +//// [missingFunctionImplementation2_b.js] +"use strict"; diff --git a/tests/cases/compiler/missingFunctionImplementation.ts b/tests/cases/compiler/missingFunctionImplementation.ts new file mode 100644 index 00000000000..e261db3a667 --- /dev/null +++ b/tests/cases/compiler/missingFunctionImplementation.ts @@ -0,0 +1,79 @@ + +export class C1 { + m(): void; +} + +// merged with a namespace +export class C2 { + m(): void; +} +export namespace C2 { } + + +// merged with a namespace, multiple overloads +class C3 { + m(a, b); + m(a); +} +namespace C3 { } + +// static methods, multiple overloads +class C4 { + static m(a): void; +} + +// static methods, multiple overloads +class C5 { + static m(a): void; + static m(): void; +} + +// merged with namespace, static methods +class C6 { + static m(): void; +} +namespace C6 { +} + +// merged with namespace, static methods, multiple overloads +class C7 { + static m(a): void; + static m(): void; +} +namespace C7 { +} + +// merged with namespace, static methods, duplicate declarations +class C8 { + static m(a): void; + static m(a, b): void; +} +namespace C8 { + export function m(a?, b?): void { } +} + +// merged with namespace, static methods, duplicate declarations +class C9 { + static m(a): void { } +} +namespace C9 { + export function m(a): void; +} + +// merged namespaces +namespace N10 { + export function m(a): void; +} +namespace N10 { + export function m(a): void { } +} + +// merged namespaces, duplicate defintions +namespace N12 { + export function m(a): void; + export function m(): void; + export function m(a?): void { } +} +namespace N12 { + export function m(a): void { } +} diff --git a/tests/cases/compiler/missingFunctionImplementation2.ts b/tests/cases/compiler/missingFunctionImplementation2.ts new file mode 100644 index 00000000000..25909b6add4 --- /dev/null +++ b/tests/cases/compiler/missingFunctionImplementation2.ts @@ -0,0 +1,8 @@ +// @Filename: missingFunctionImplementation2_a.ts +export {}; +declare module "./missingFunctionImplementation2_b.ts" { + export function f(a, b): void; +} + +// @Filename: missingFunctionImplementation2_b.ts +export function f(a?, b?); \ No newline at end of file From ce156460eb85e1b5a4e45bd55535fa860a6262a4 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Mon, 13 Jun 2016 14:29:04 -0700 Subject: [PATCH 17/56] Narrow type in case/default sections in switch on discriminant property --- src/compiler/binder.ts | 54 +++++++++++++++++++++++--------------- src/compiler/checker.ts | 58 +++++++++++++++++++++++++++++++++++++++++ src/compiler/types.ts | 13 +++++++-- 3 files changed, 102 insertions(+), 23 deletions(-) diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index f9d0f443347..4155e2e195f 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -693,8 +693,23 @@ namespace ts { setFlowNodeReferenced(antecedent); return { flags, - antecedent, expression, + antecedent + }; + } + + function createFlowSwitchClause(antecedent: FlowNode, switchStatement: SwitchStatement, clauseStart: number, clauseEnd: number): FlowNode { + const expr = switchStatement.expression; + if (expr.kind !== SyntaxKind.PropertyAccessExpression || !isNarrowableReference((expr).expression)) { + return antecedent; + } + setFlowNodeReferenced(antecedent); + return { + flags: FlowFlags.SwitchClause, + switchStatement, + clauseStart, + clauseEnd, + antecedent }; } @@ -923,9 +938,9 @@ namespace ts { preSwitchCaseFlow = currentFlow; bind(node.caseBlock); addAntecedent(postSwitchLabel, currentFlow); - const hasNonEmptyDefault = forEach(node.caseBlock.clauses, c => c.kind === SyntaxKind.DefaultClause && c.statements.length); - if (!hasNonEmptyDefault) { - addAntecedent(postSwitchLabel, preSwitchCaseFlow); + const hasDefault = forEach(node.caseBlock.clauses, c => c.kind === SyntaxKind.DefaultClause); + if (!hasDefault) { + addAntecedent(postSwitchLabel, createFlowSwitchClause(preSwitchCaseFlow, node, 0, 0)); } currentBreakTarget = saveBreakTarget; preSwitchCaseFlow = savePreSwitchCaseFlow; @@ -934,25 +949,22 @@ namespace ts { function bindCaseBlock(node: CaseBlock): void { const clauses = node.clauses; + let fallthroughFlow = unreachableFlow; for (let i = 0; i < clauses.length; i++) { - const clause = clauses[i]; - if (clause.statements.length) { - if (currentFlow.flags & FlowFlags.Unreachable) { - currentFlow = preSwitchCaseFlow; - } - else { - const preCaseLabel = createBranchLabel(); - addAntecedent(preCaseLabel, preSwitchCaseFlow); - addAntecedent(preCaseLabel, currentFlow); - currentFlow = finishFlowLabel(preCaseLabel); - } - bind(clause); - if (!(currentFlow.flags & FlowFlags.Unreachable) && i !== clauses.length - 1 && options.noFallthroughCasesInSwitch) { - errorOnFirstToken(clause, Diagnostics.Fallthrough_case_in_switch); - } + const clauseStart = i; + while (!clauses[i].statements.length && i + 1 < clauses.length) { + bind(clauses[i]); + i++; } - else { - bind(clause); + const preCaseLabel = createBranchLabel(); + addAntecedent(preCaseLabel, createFlowSwitchClause(preSwitchCaseFlow, node.parent, clauseStart, i + 1)); + addAntecedent(preCaseLabel, fallthroughFlow); + currentFlow = finishFlowLabel(preCaseLabel); + const clause = clauses[i]; + bind(clause); + fallthroughFlow = currentFlow; + if (!(currentFlow.flags & FlowFlags.Unreachable) && i !== clauses.length - 1 && options.noFallthroughCasesInSwitch) { + errorOnFirstToken(clause, Diagnostics.Fallthrough_case_in_switch); } } } diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 105401c0015..bb268b55da8 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -7676,6 +7676,29 @@ namespace ts { return node; } + function getTypeOfSwitchClause(clause: CaseClause | DefaultClause) { + if (clause.kind === SyntaxKind.CaseClause) { + const expr = (clause).expression; + return expr.kind === SyntaxKind.StringLiteral ? getStringLiteralTypeForText((expr).text) : checkExpression(expr); + } + return undefined; + } + + function getSwitchClauseTypes(switchStatement: SwitchStatement): Type[] { + const links = getNodeLinks(switchStatement); + if (!links.switchTypes) { + // If all case clauses specify expressions that have unit types, we return an array + // of those unit types. Otherwise we return an empty array. + const types = map(switchStatement.caseBlock.clauses, getTypeOfSwitchClause); + links.switchTypes = forEach(types, t => !t || t.flags & TypeFlags.StringLiteral) ? types : emptyArray; + } + return links.switchTypes; + } + + function eachTypeContainedIn(source: Type, types: Type[]) { + return source.flags & TypeFlags.Union ? !forEach((source).types, t => !contains(types, t)) : contains(types, source); + } + function getFlowTypeOfReference(reference: Node, declaredType: Type, assumeInitialized: boolean, includeOuterFunctions: boolean) { let key: string; if (!reference.flowNode || assumeInitialized && !(declaredType.flags & TypeFlags.Narrowable)) { @@ -7713,6 +7736,9 @@ namespace ts { else if (flow.flags & FlowFlags.Condition) { type = getTypeAtFlowCondition(flow); } + else if (flow.flags & FlowFlags.SwitchClause) { + type = getTypeAtSwitchClause(flow); + } else if (flow.flags & FlowFlags.Label) { if ((flow).antecedents.length === 1) { flow = (flow).antecedents[0]; @@ -7796,6 +7822,11 @@ namespace ts { return type; } + function getTypeAtSwitchClause(flow: FlowSwitchClause) { + const type = getTypeAtFlowNode(flow.antecedent); + return narrowTypeBySwitchOnDiscriminant(type, flow.switchStatement, flow.clauseStart, flow.clauseEnd); + } + function getTypeAtFlowBranchLabel(flow: FlowLabel) { const antecedentTypes: Type[] = []; for (const antecedent of flow.antecedents) { @@ -7938,6 +7969,33 @@ namespace ts { return type; } + function narrowTypeBySwitchOnDiscriminant(type: Type, switchStatement: SwitchStatement, clauseStart: number, clauseEnd: number) { + // We have switch statement with property access expression + if (!(type.flags & TypeFlags.Union) || !isMatchingReference(reference, (switchStatement.expression).expression)) { + return type; + } + const propName = (switchStatement.expression).name.text; + const propType = getTypeOfPropertyOfType(type, propName); + if (!propType || !isStringLiteralUnionType(propType)) { + return type; + } + const switchTypes = getSwitchClauseTypes(switchStatement); + if (!switchTypes.length) { + return type; + } + const types = (type).types; + const clauseTypes = switchTypes.slice(clauseStart, clauseEnd); + const hasDefaultClause = clauseStart === clauseEnd || contains(clauseTypes, undefined); + const caseTypes = hasDefaultClause ? filter(clauseTypes, t => !!t) : clauseTypes; + const discriminantType = caseTypes.length ? getUnionType(caseTypes) : undefined; + const caseType = discriminantType && getUnionType(filter(types, t => isTypeComparableTo(discriminantType, getTypeOfPropertyOfType(t, propName)))); + if (!hasDefaultClause) { + return caseType; + } + const defaultType = getUnionType(filter(types, t => !eachTypeContainedIn(getTypeOfPropertyOfType(t, propName), switchTypes))); + return caseType ? getUnionType([caseType, defaultType]) : defaultType; + } + function narrowTypeByTypeof(type: Type, expr: BinaryExpression, assumeTrue: boolean): Type { // We have '==', '!=', '====', or !==' operator with 'typeof xxx' on the left // and string literal on the right diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 9f78d98629e..fbcdc594656 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -1554,8 +1554,9 @@ namespace ts { Assignment = 1 << 4, // Assignment TrueCondition = 1 << 5, // Condition known to be true FalseCondition = 1 << 6, // Condition known to be false - Referenced = 1 << 7, // Referenced as antecedent once - Shared = 1 << 8, // Referenced as antecedent more than once + SwitchClause = 1 << 7, // Switch statement clause + Referenced = 1 << 8, // Referenced as antecedent once + Shared = 1 << 9, // Referenced as antecedent more than once Label = BranchLabel | LoopLabel, Condition = TrueCondition | FalseCondition } @@ -1591,6 +1592,13 @@ namespace ts { antecedent: FlowNode; } + export interface FlowSwitchClause extends FlowNode { + switchStatement: SwitchStatement; + clauseStart: number; // Start index of case/default clause range + clauseEnd: number; // End index of case/default clause range + antecedent: FlowNode; + } + export interface AmdDependency { path: string; name: string; @@ -2170,6 +2178,7 @@ namespace ts { resolvedJsxType?: Type; // resolved element attributes type of a JSX openinglike element hasSuperCall?: boolean; // recorded result when we try to find super-call. We only try to find one if this flag is undefined, indicating that we haven't made an attempt. superCall?: ExpressionStatement; // Cached first super-call found in the constructor. Used in checking whether super is called before this-accessing + switchTypes?: Type[]; // Cached array of switch case expression types } export const enum TypeFlags { From c90b0fe17dec9ce42a3079b1adc806d860bf6c43 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Mon, 13 Jun 2016 16:20:13 -0700 Subject: [PATCH 18/56] No implicit returns following exhaustive switch statements --- src/compiler/binder.ts | 11 +++++++++-- src/compiler/checker.ts | 36 ++++++++++++++++++++++++++++++++++-- src/compiler/types.ts | 1 + 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index 4155e2e195f..e413c20f79c 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -650,6 +650,11 @@ namespace ts { return isNarrowableReference(expr); } + function isNarrowingSwitchStatement(switchStatement: SwitchStatement) { + const expr = switchStatement.expression; + return expr.kind === SyntaxKind.PropertyAccessExpression && isNarrowableReference((expr).expression); + } + function createBranchLabel(): FlowLabel { return { flags: FlowFlags.BranchLabel, @@ -699,8 +704,7 @@ namespace ts { } function createFlowSwitchClause(antecedent: FlowNode, switchStatement: SwitchStatement, clauseStart: number, clauseEnd: number): FlowNode { - const expr = switchStatement.expression; - if (expr.kind !== SyntaxKind.PropertyAccessExpression || !isNarrowableReference((expr).expression)) { + if (!isNarrowingSwitchStatement(switchStatement)) { return antecedent; } setFlowNodeReferenced(antecedent); @@ -939,6 +943,9 @@ namespace ts { bind(node.caseBlock); addAntecedent(postSwitchLabel, currentFlow); const hasDefault = forEach(node.caseBlock.clauses, c => c.kind === SyntaxKind.DefaultClause); + // We mark a switch statement as possibly exhaustive if it has no default clause and if all + // case clauses have unreachable end points (e.g. they all return). + node.possiblyExhaustive = !hasDefault && !postSwitchLabel.antecedents; if (!hasDefault) { addAntecedent(postSwitchLabel, createFlowSwitchClause(preSwitchCaseFlow, node, 0, 0)); } diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index bb268b55da8..46e683fc0c8 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -11808,10 +11808,42 @@ namespace ts { return aggregatedTypes; } + function isExhaustiveSwitchStatement(node: SwitchStatement): boolean { + const expr = node.expression; + if (!node.possiblyExhaustive || expr.kind !== SyntaxKind.PropertyAccessExpression) { + return false; + } + const type = checkExpression((expr).expression); + if (!(type.flags & TypeFlags.Union)) { + return false; + } + const propName = (expr).name.text; + const propType = getTypeOfPropertyOfType(type, propName); + if (!propType || !isStringLiteralUnionType(propType)) { + return false; + } + const switchTypes = getSwitchClauseTypes(node); + if (!switchTypes.length) { + return false; + } + return eachTypeContainedIn(propType, switchTypes); + } + + function functionHasImplicitReturn(func: FunctionLikeDeclaration) { + if (!(func.flags & NodeFlags.HasImplicitReturn)) { + return false; + } + const lastStatement = lastOrUndefined((func.body).statements); + if (lastStatement && lastStatement.kind === SyntaxKind.SwitchStatement && isExhaustiveSwitchStatement(lastStatement)) { + return false; + } + return true; + } + function checkAndAggregateReturnExpressionTypes(func: FunctionLikeDeclaration, contextualMapper: TypeMapper): Type[] { const isAsync = isAsyncFunctionLike(func); const aggregatedTypes: Type[] = []; - let hasReturnWithNoExpression = !!(func.flags & NodeFlags.HasImplicitReturn); + let hasReturnWithNoExpression = functionHasImplicitReturn(func); let hasReturnOfTypeNever = false; forEachReturnStatement(func.body, returnStatement => { const expr = returnStatement.expression; @@ -11868,7 +11900,7 @@ namespace ts { // If all we have is a function signature, or an arrow function with an expression body, then there is nothing to check. // also if HasImplicitReturn flag is not set this means that all codepaths in function body end with return or throw - if (nodeIsMissing(func.body) || func.body.kind !== SyntaxKind.Block || !(func.flags & NodeFlags.HasImplicitReturn)) { + if (nodeIsMissing(func.body) || func.body.kind !== SyntaxKind.Block || !functionHasImplicitReturn(func)) { return; } diff --git a/src/compiler/types.ts b/src/compiler/types.ts index fbcdc594656..d44023ac773 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -1188,6 +1188,7 @@ namespace ts { export interface SwitchStatement extends Statement { expression: Expression; caseBlock: CaseBlock; + possiblyExhaustive?: boolean; } // @kind(SyntaxKind.CaseBlock) From 00376f42d84b01fc8ccb15d85d30baafcd1c2eaa Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Tue, 14 Jun 2016 09:33:15 -0700 Subject: [PATCH 19/56] Narrow non-union types to ensure consistent results --- src/compiler/checker.ts | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 46e683fc0c8..cc28f0fcbd2 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -7699,6 +7699,12 @@ namespace ts { return source.flags & TypeFlags.Union ? !forEach((source).types, t => !contains(types, t)) : contains(types, source); } + function filterType(type: Type, f: (t: Type) => boolean): Type { + return type.flags & TypeFlags.Union ? + getUnionType(filter((type).types, f)) : + f(type) ? type : neverType; + } + function getFlowTypeOfReference(reference: Node, declaredType: Type, assumeInitialized: boolean, includeOuterFunctions: boolean) { let key: string; if (!reference.flowNode || assumeInitialized && !(declaredType.flags & TypeFlags.Narrowable)) { @@ -7944,7 +7950,7 @@ namespace ts { function narrowTypeByDiscriminant(type: Type, expr: BinaryExpression, assumeTrue: boolean): Type { // We have '==', '!=', '===', or '!==' operator with property access on left - if (!(type.flags & TypeFlags.Union) || !isMatchingReference(reference, (expr.left).expression)) { + if (!isMatchingReference(reference, (expr.left).expression)) { return type; } const propName = (expr.left).name.text; @@ -7961,17 +7967,17 @@ namespace ts { assumeTrue = !assumeTrue; } if (assumeTrue) { - return getUnionType(filter((type).types, t => areTypesComparable(getTypeOfPropertyOfType(t, propName), discriminantType))); + return filterType(type, t => areTypesComparable(getTypeOfPropertyOfType(t, propName), discriminantType)); } if (discriminantType.flags & TypeFlags.StringLiteral) { - return getUnionType(filter((type).types, t => getTypeOfPropertyOfType(t, propName) !== discriminantType)); + return filterType(type, t => getTypeOfPropertyOfType(t, propName) !== discriminantType); } return type; } function narrowTypeBySwitchOnDiscriminant(type: Type, switchStatement: SwitchStatement, clauseStart: number, clauseEnd: number) { // We have switch statement with property access expression - if (!(type.flags & TypeFlags.Union) || !isMatchingReference(reference, (switchStatement.expression).expression)) { + if (!isMatchingReference(reference, (switchStatement.expression).expression)) { return type; } const propName = (switchStatement.expression).name.text; @@ -7983,16 +7989,15 @@ namespace ts { if (!switchTypes.length) { return type; } - const types = (type).types; const clauseTypes = switchTypes.slice(clauseStart, clauseEnd); const hasDefaultClause = clauseStart === clauseEnd || contains(clauseTypes, undefined); const caseTypes = hasDefaultClause ? filter(clauseTypes, t => !!t) : clauseTypes; const discriminantType = caseTypes.length ? getUnionType(caseTypes) : undefined; - const caseType = discriminantType && getUnionType(filter(types, t => isTypeComparableTo(discriminantType, getTypeOfPropertyOfType(t, propName)))); + const caseType = discriminantType && filterType(type, t => isTypeComparableTo(discriminantType, getTypeOfPropertyOfType(t, propName))); if (!hasDefaultClause) { return caseType; } - const defaultType = getUnionType(filter(types, t => !eachTypeContainedIn(getTypeOfPropertyOfType(t, propName), switchTypes))); + const defaultType = filterType(type, t => !eachTypeContainedIn(getTypeOfPropertyOfType(t, propName), switchTypes)); return caseType ? getUnionType([caseType, defaultType]) : defaultType; } From 5ff7c29d40f0b1f99b3b5513624eb0adaee74cfb Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Tue, 14 Jun 2016 09:33:56 -0700 Subject: [PATCH 20/56] Add tests --- .../reference/discriminatedUnionTypes1.js | 252 ++++++++++ .../discriminatedUnionTypes1.symbols | 402 +++++++++++++++ .../reference/discriminatedUnionTypes1.types | 465 ++++++++++++++++++ .../types/union/discriminatedUnionTypes1.ts | 142 ++++++ 4 files changed, 1261 insertions(+) create mode 100644 tests/baselines/reference/discriminatedUnionTypes1.js create mode 100644 tests/baselines/reference/discriminatedUnionTypes1.symbols create mode 100644 tests/baselines/reference/discriminatedUnionTypes1.types create mode 100644 tests/cases/conformance/types/union/discriminatedUnionTypes1.ts diff --git a/tests/baselines/reference/discriminatedUnionTypes1.js b/tests/baselines/reference/discriminatedUnionTypes1.js new file mode 100644 index 00000000000..8679689569a --- /dev/null +++ b/tests/baselines/reference/discriminatedUnionTypes1.js @@ -0,0 +1,252 @@ +//// [discriminatedUnionTypes1.ts] +interface Square { + kind: "square"; + size: number; +} + +interface Rectangle { + kind: "rectangle"; + width: number; + height: number; +} + +interface Circle { + kind: "circle"; + radius: number; +} + +type Shape = Square | Rectangle | Circle; + +function area1(s: Shape) { + if (s.kind === "square") { + return s.size * s.size; + } + else if (s.kind === "circle") { + return Math.PI * s.radius * s.radius; + } + else if (s.kind === "rectangle") { + return s.width * s.height; + } + else { + return 0; + } +} + +function area2(s: Shape) { + switch (s.kind) { + case "square": return s.size * s.size; + case "rectangle": return s.width * s.height; + case "circle": return Math.PI * s.radius * s.radius; + } +} + +function assertNever(x: never): never { + throw new Error("Unexpected object: " + x); +} + +function area3(s: Shape) { + switch (s.kind) { + case "square": return s.size * s.size; + case "rectangle": return s.width * s.height; + case "circle": return Math.PI * s.radius * s.radius; + default: return assertNever(s); + } +} + +function area4(s: Shape) { + switch (s.kind) { + case "square": return s.size * s.size; + case "rectangle": return s.width * s.height; + case "circle": return Math.PI * s.radius * s.radius; + } + return assertNever(s); +} + +type Message = + { kind: "A", x: string } | + { kind: "B" | "C", y: number } | + { kind: "D" }; + +function f1(m: Message) { + if (m.kind === "A") { + m; // { kind: "A", x: string } + } + else if (m.kind === "D") { + m; // { kind: "D" } + } + else { + m; // { kind: "B" | "C", y: number } + } +} + +function f2(m: Message) { + if (m.kind === "A") { + return; + } + m; // { kind: "B" | "C", y: number } | { kind: "D" } +} + +function f3(m: Message) { + if (m.kind === "X") { + m; // never + } +} + +function f4(m: Message, x: "A" | "D") { + if (m.kind == x) { + m; // { kind: "A", x: string } | { kind: "D" } + } +} + +function f5(m: Message) { + switch (m.kind) { + case "A": + m; // { kind: "A", x: string } + break; + case "D": + m; // { kind: "D" } + break; + default: + m; // { kind: "B" | "C", y: number } + } +} + +function f6(m: Message) { + switch (m.kind) { + case "A": + m; // { kind: "A", x: string } + case "D": + m; // { kind: "A", x: string } | { kind: "D" } + break; + default: + m; // { kind: "B" | "C", y: number } + } +} + +function f7(m: Message) { + switch (m.kind) { + case "A": + case "B": + return; + } + m; // { kind: "B" | "C", y: number } | { kind: "D" } +} + +function f8(m: Message) { + switch (m.kind) { + case "A": + return; + case "D": + throw new Error(); + } + m; // { kind: "B" | "C", y: number } +} + +//// [discriminatedUnionTypes1.js] +function area1(s) { + if (s.kind === "square") { + return s.size * s.size; + } + else if (s.kind === "circle") { + return Math.PI * s.radius * s.radius; + } + else if (s.kind === "rectangle") { + return s.width * s.height; + } + else { + return 0; + } +} +function area2(s) { + switch (s.kind) { + case "square": return s.size * s.size; + case "rectangle": return s.width * s.height; + case "circle": return Math.PI * s.radius * s.radius; + } +} +function assertNever(x) { + throw new Error("Unexpected object: " + x); +} +function area3(s) { + switch (s.kind) { + case "square": return s.size * s.size; + case "rectangle": return s.width * s.height; + case "circle": return Math.PI * s.radius * s.radius; + default: return assertNever(s); + } +} +function area4(s) { + switch (s.kind) { + case "square": return s.size * s.size; + case "rectangle": return s.width * s.height; + case "circle": return Math.PI * s.radius * s.radius; + } + return assertNever(s); +} +function f1(m) { + if (m.kind === "A") { + m; // { kind: "A", x: string } + } + else if (m.kind === "D") { + m; // { kind: "D" } + } + else { + m; // { kind: "B" | "C", y: number } + } +} +function f2(m) { + if (m.kind === "A") { + return; + } + m; // { kind: "B" | "C", y: number } | { kind: "D" } +} +function f3(m) { + if (m.kind === "X") { + m; // never + } +} +function f4(m, x) { + if (m.kind == x) { + m; // { kind: "A", x: string } | { kind: "D" } + } +} +function f5(m) { + switch (m.kind) { + case "A": + m; // { kind: "A", x: string } + break; + case "D": + m; // { kind: "D" } + break; + default: + m; // { kind: "B" | "C", y: number } + } +} +function f6(m) { + switch (m.kind) { + case "A": + m; // { kind: "A", x: string } + case "D": + m; // { kind: "A", x: string } | { kind: "D" } + break; + default: + m; // { kind: "B" | "C", y: number } + } +} +function f7(m) { + switch (m.kind) { + case "A": + case "B": + return; + } + m; // { kind: "B" | "C", y: number } | { kind: "D" } +} +function f8(m) { + switch (m.kind) { + case "A": + return; + case "D": + throw new Error(); + } + m; // { kind: "B" | "C", y: number } +} diff --git a/tests/baselines/reference/discriminatedUnionTypes1.symbols b/tests/baselines/reference/discriminatedUnionTypes1.symbols new file mode 100644 index 00000000000..38069447423 --- /dev/null +++ b/tests/baselines/reference/discriminatedUnionTypes1.symbols @@ -0,0 +1,402 @@ +=== tests/cases/conformance/types/union/discriminatedUnionTypes1.ts === +interface Square { +>Square : Symbol(Square, Decl(discriminatedUnionTypes1.ts, 0, 0)) + + kind: "square"; +>kind : Symbol(Square.kind, Decl(discriminatedUnionTypes1.ts, 0, 18)) + + size: number; +>size : Symbol(Square.size, Decl(discriminatedUnionTypes1.ts, 1, 19)) +} + +interface Rectangle { +>Rectangle : Symbol(Rectangle, Decl(discriminatedUnionTypes1.ts, 3, 1)) + + kind: "rectangle"; +>kind : Symbol(Rectangle.kind, Decl(discriminatedUnionTypes1.ts, 5, 21)) + + width: number; +>width : Symbol(Rectangle.width, Decl(discriminatedUnionTypes1.ts, 6, 22)) + + height: number; +>height : Symbol(Rectangle.height, Decl(discriminatedUnionTypes1.ts, 7, 18)) +} + +interface Circle { +>Circle : Symbol(Circle, Decl(discriminatedUnionTypes1.ts, 9, 1)) + + kind: "circle"; +>kind : Symbol(Circle.kind, Decl(discriminatedUnionTypes1.ts, 11, 18)) + + radius: number; +>radius : Symbol(Circle.radius, Decl(discriminatedUnionTypes1.ts, 12, 19)) +} + +type Shape = Square | Rectangle | Circle; +>Shape : Symbol(Shape, Decl(discriminatedUnionTypes1.ts, 14, 1)) +>Square : Symbol(Square, Decl(discriminatedUnionTypes1.ts, 0, 0)) +>Rectangle : Symbol(Rectangle, Decl(discriminatedUnionTypes1.ts, 3, 1)) +>Circle : Symbol(Circle, Decl(discriminatedUnionTypes1.ts, 9, 1)) + +function area1(s: Shape) { +>area1 : Symbol(area1, Decl(discriminatedUnionTypes1.ts, 16, 41)) +>s : Symbol(s, Decl(discriminatedUnionTypes1.ts, 18, 15)) +>Shape : Symbol(Shape, Decl(discriminatedUnionTypes1.ts, 14, 1)) + + if (s.kind === "square") { +>s.kind : Symbol(kind, Decl(discriminatedUnionTypes1.ts, 0, 18), Decl(discriminatedUnionTypes1.ts, 5, 21), Decl(discriminatedUnionTypes1.ts, 11, 18)) +>s : Symbol(s, Decl(discriminatedUnionTypes1.ts, 18, 15)) +>kind : Symbol(kind, Decl(discriminatedUnionTypes1.ts, 0, 18), Decl(discriminatedUnionTypes1.ts, 5, 21), Decl(discriminatedUnionTypes1.ts, 11, 18)) + + return s.size * s.size; +>s.size : Symbol(Square.size, Decl(discriminatedUnionTypes1.ts, 1, 19)) +>s : Symbol(s, Decl(discriminatedUnionTypes1.ts, 18, 15)) +>size : Symbol(Square.size, Decl(discriminatedUnionTypes1.ts, 1, 19)) +>s.size : Symbol(Square.size, Decl(discriminatedUnionTypes1.ts, 1, 19)) +>s : Symbol(s, Decl(discriminatedUnionTypes1.ts, 18, 15)) +>size : Symbol(Square.size, Decl(discriminatedUnionTypes1.ts, 1, 19)) + } + else if (s.kind === "circle") { +>s.kind : Symbol(kind, Decl(discriminatedUnionTypes1.ts, 5, 21), Decl(discriminatedUnionTypes1.ts, 11, 18)) +>s : Symbol(s, Decl(discriminatedUnionTypes1.ts, 18, 15)) +>kind : Symbol(kind, Decl(discriminatedUnionTypes1.ts, 5, 21), Decl(discriminatedUnionTypes1.ts, 11, 18)) + + return Math.PI * s.radius * s.radius; +>Math.PI : Symbol(Math.PI, Decl(lib.d.ts, --, --)) +>Math : Symbol(Math, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +>PI : Symbol(Math.PI, Decl(lib.d.ts, --, --)) +>s.radius : Symbol(Circle.radius, Decl(discriminatedUnionTypes1.ts, 12, 19)) +>s : Symbol(s, Decl(discriminatedUnionTypes1.ts, 18, 15)) +>radius : Symbol(Circle.radius, Decl(discriminatedUnionTypes1.ts, 12, 19)) +>s.radius : Symbol(Circle.radius, Decl(discriminatedUnionTypes1.ts, 12, 19)) +>s : Symbol(s, Decl(discriminatedUnionTypes1.ts, 18, 15)) +>radius : Symbol(Circle.radius, Decl(discriminatedUnionTypes1.ts, 12, 19)) + } + else if (s.kind === "rectangle") { +>s.kind : Symbol(Rectangle.kind, Decl(discriminatedUnionTypes1.ts, 5, 21)) +>s : Symbol(s, Decl(discriminatedUnionTypes1.ts, 18, 15)) +>kind : Symbol(Rectangle.kind, Decl(discriminatedUnionTypes1.ts, 5, 21)) + + return s.width * s.height; +>s.width : Symbol(Rectangle.width, Decl(discriminatedUnionTypes1.ts, 6, 22)) +>s : Symbol(s, Decl(discriminatedUnionTypes1.ts, 18, 15)) +>width : Symbol(Rectangle.width, Decl(discriminatedUnionTypes1.ts, 6, 22)) +>s.height : Symbol(Rectangle.height, Decl(discriminatedUnionTypes1.ts, 7, 18)) +>s : Symbol(s, Decl(discriminatedUnionTypes1.ts, 18, 15)) +>height : Symbol(Rectangle.height, Decl(discriminatedUnionTypes1.ts, 7, 18)) + } + else { + return 0; + } +} + +function area2(s: Shape) { +>area2 : Symbol(area2, Decl(discriminatedUnionTypes1.ts, 31, 1)) +>s : Symbol(s, Decl(discriminatedUnionTypes1.ts, 33, 15)) +>Shape : Symbol(Shape, Decl(discriminatedUnionTypes1.ts, 14, 1)) + + switch (s.kind) { +>s.kind : Symbol(kind, Decl(discriminatedUnionTypes1.ts, 0, 18), Decl(discriminatedUnionTypes1.ts, 5, 21), Decl(discriminatedUnionTypes1.ts, 11, 18)) +>s : Symbol(s, Decl(discriminatedUnionTypes1.ts, 33, 15)) +>kind : Symbol(kind, Decl(discriminatedUnionTypes1.ts, 0, 18), Decl(discriminatedUnionTypes1.ts, 5, 21), Decl(discriminatedUnionTypes1.ts, 11, 18)) + + case "square": return s.size * s.size; +>s.size : Symbol(Square.size, Decl(discriminatedUnionTypes1.ts, 1, 19)) +>s : Symbol(s, Decl(discriminatedUnionTypes1.ts, 33, 15)) +>size : Symbol(Square.size, Decl(discriminatedUnionTypes1.ts, 1, 19)) +>s.size : Symbol(Square.size, Decl(discriminatedUnionTypes1.ts, 1, 19)) +>s : Symbol(s, Decl(discriminatedUnionTypes1.ts, 33, 15)) +>size : Symbol(Square.size, Decl(discriminatedUnionTypes1.ts, 1, 19)) + + case "rectangle": return s.width * s.height; +>s.width : Symbol(Rectangle.width, Decl(discriminatedUnionTypes1.ts, 6, 22)) +>s : Symbol(s, Decl(discriminatedUnionTypes1.ts, 33, 15)) +>width : Symbol(Rectangle.width, Decl(discriminatedUnionTypes1.ts, 6, 22)) +>s.height : Symbol(Rectangle.height, Decl(discriminatedUnionTypes1.ts, 7, 18)) +>s : Symbol(s, Decl(discriminatedUnionTypes1.ts, 33, 15)) +>height : Symbol(Rectangle.height, Decl(discriminatedUnionTypes1.ts, 7, 18)) + + case "circle": return Math.PI * s.radius * s.radius; +>Math.PI : Symbol(Math.PI, Decl(lib.d.ts, --, --)) +>Math : Symbol(Math, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +>PI : Symbol(Math.PI, Decl(lib.d.ts, --, --)) +>s.radius : Symbol(Circle.radius, Decl(discriminatedUnionTypes1.ts, 12, 19)) +>s : Symbol(s, Decl(discriminatedUnionTypes1.ts, 33, 15)) +>radius : Symbol(Circle.radius, Decl(discriminatedUnionTypes1.ts, 12, 19)) +>s.radius : Symbol(Circle.radius, Decl(discriminatedUnionTypes1.ts, 12, 19)) +>s : Symbol(s, Decl(discriminatedUnionTypes1.ts, 33, 15)) +>radius : Symbol(Circle.radius, Decl(discriminatedUnionTypes1.ts, 12, 19)) + } +} + +function assertNever(x: never): never { +>assertNever : Symbol(assertNever, Decl(discriminatedUnionTypes1.ts, 39, 1)) +>x : Symbol(x, Decl(discriminatedUnionTypes1.ts, 41, 21)) + + throw new Error("Unexpected object: " + x); +>Error : Symbol(Error, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +>x : Symbol(x, Decl(discriminatedUnionTypes1.ts, 41, 21)) +} + +function area3(s: Shape) { +>area3 : Symbol(area3, Decl(discriminatedUnionTypes1.ts, 43, 1)) +>s : Symbol(s, Decl(discriminatedUnionTypes1.ts, 45, 15)) +>Shape : Symbol(Shape, Decl(discriminatedUnionTypes1.ts, 14, 1)) + + switch (s.kind) { +>s.kind : Symbol(kind, Decl(discriminatedUnionTypes1.ts, 0, 18), Decl(discriminatedUnionTypes1.ts, 5, 21), Decl(discriminatedUnionTypes1.ts, 11, 18)) +>s : Symbol(s, Decl(discriminatedUnionTypes1.ts, 45, 15)) +>kind : Symbol(kind, Decl(discriminatedUnionTypes1.ts, 0, 18), Decl(discriminatedUnionTypes1.ts, 5, 21), Decl(discriminatedUnionTypes1.ts, 11, 18)) + + case "square": return s.size * s.size; +>s.size : Symbol(Square.size, Decl(discriminatedUnionTypes1.ts, 1, 19)) +>s : Symbol(s, Decl(discriminatedUnionTypes1.ts, 45, 15)) +>size : Symbol(Square.size, Decl(discriminatedUnionTypes1.ts, 1, 19)) +>s.size : Symbol(Square.size, Decl(discriminatedUnionTypes1.ts, 1, 19)) +>s : Symbol(s, Decl(discriminatedUnionTypes1.ts, 45, 15)) +>size : Symbol(Square.size, Decl(discriminatedUnionTypes1.ts, 1, 19)) + + case "rectangle": return s.width * s.height; +>s.width : Symbol(Rectangle.width, Decl(discriminatedUnionTypes1.ts, 6, 22)) +>s : Symbol(s, Decl(discriminatedUnionTypes1.ts, 45, 15)) +>width : Symbol(Rectangle.width, Decl(discriminatedUnionTypes1.ts, 6, 22)) +>s.height : Symbol(Rectangle.height, Decl(discriminatedUnionTypes1.ts, 7, 18)) +>s : Symbol(s, Decl(discriminatedUnionTypes1.ts, 45, 15)) +>height : Symbol(Rectangle.height, Decl(discriminatedUnionTypes1.ts, 7, 18)) + + case "circle": return Math.PI * s.radius * s.radius; +>Math.PI : Symbol(Math.PI, Decl(lib.d.ts, --, --)) +>Math : Symbol(Math, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +>PI : Symbol(Math.PI, Decl(lib.d.ts, --, --)) +>s.radius : Symbol(Circle.radius, Decl(discriminatedUnionTypes1.ts, 12, 19)) +>s : Symbol(s, Decl(discriminatedUnionTypes1.ts, 45, 15)) +>radius : Symbol(Circle.radius, Decl(discriminatedUnionTypes1.ts, 12, 19)) +>s.radius : Symbol(Circle.radius, Decl(discriminatedUnionTypes1.ts, 12, 19)) +>s : Symbol(s, Decl(discriminatedUnionTypes1.ts, 45, 15)) +>radius : Symbol(Circle.radius, Decl(discriminatedUnionTypes1.ts, 12, 19)) + + default: return assertNever(s); +>assertNever : Symbol(assertNever, Decl(discriminatedUnionTypes1.ts, 39, 1)) +>s : Symbol(s, Decl(discriminatedUnionTypes1.ts, 45, 15)) + } +} + +function area4(s: Shape) { +>area4 : Symbol(area4, Decl(discriminatedUnionTypes1.ts, 52, 1)) +>s : Symbol(s, Decl(discriminatedUnionTypes1.ts, 54, 15)) +>Shape : Symbol(Shape, Decl(discriminatedUnionTypes1.ts, 14, 1)) + + switch (s.kind) { +>s.kind : Symbol(kind, Decl(discriminatedUnionTypes1.ts, 0, 18), Decl(discriminatedUnionTypes1.ts, 5, 21), Decl(discriminatedUnionTypes1.ts, 11, 18)) +>s : Symbol(s, Decl(discriminatedUnionTypes1.ts, 54, 15)) +>kind : Symbol(kind, Decl(discriminatedUnionTypes1.ts, 0, 18), Decl(discriminatedUnionTypes1.ts, 5, 21), Decl(discriminatedUnionTypes1.ts, 11, 18)) + + case "square": return s.size * s.size; +>s.size : Symbol(Square.size, Decl(discriminatedUnionTypes1.ts, 1, 19)) +>s : Symbol(s, Decl(discriminatedUnionTypes1.ts, 54, 15)) +>size : Symbol(Square.size, Decl(discriminatedUnionTypes1.ts, 1, 19)) +>s.size : Symbol(Square.size, Decl(discriminatedUnionTypes1.ts, 1, 19)) +>s : Symbol(s, Decl(discriminatedUnionTypes1.ts, 54, 15)) +>size : Symbol(Square.size, Decl(discriminatedUnionTypes1.ts, 1, 19)) + + case "rectangle": return s.width * s.height; +>s.width : Symbol(Rectangle.width, Decl(discriminatedUnionTypes1.ts, 6, 22)) +>s : Symbol(s, Decl(discriminatedUnionTypes1.ts, 54, 15)) +>width : Symbol(Rectangle.width, Decl(discriminatedUnionTypes1.ts, 6, 22)) +>s.height : Symbol(Rectangle.height, Decl(discriminatedUnionTypes1.ts, 7, 18)) +>s : Symbol(s, Decl(discriminatedUnionTypes1.ts, 54, 15)) +>height : Symbol(Rectangle.height, Decl(discriminatedUnionTypes1.ts, 7, 18)) + + case "circle": return Math.PI * s.radius * s.radius; +>Math.PI : Symbol(Math.PI, Decl(lib.d.ts, --, --)) +>Math : Symbol(Math, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +>PI : Symbol(Math.PI, Decl(lib.d.ts, --, --)) +>s.radius : Symbol(Circle.radius, Decl(discriminatedUnionTypes1.ts, 12, 19)) +>s : Symbol(s, Decl(discriminatedUnionTypes1.ts, 54, 15)) +>radius : Symbol(Circle.radius, Decl(discriminatedUnionTypes1.ts, 12, 19)) +>s.radius : Symbol(Circle.radius, Decl(discriminatedUnionTypes1.ts, 12, 19)) +>s : Symbol(s, Decl(discriminatedUnionTypes1.ts, 54, 15)) +>radius : Symbol(Circle.radius, Decl(discriminatedUnionTypes1.ts, 12, 19)) + } + return assertNever(s); +>assertNever : Symbol(assertNever, Decl(discriminatedUnionTypes1.ts, 39, 1)) +>s : Symbol(s, Decl(discriminatedUnionTypes1.ts, 54, 15)) +} + +type Message = +>Message : Symbol(Message, Decl(discriminatedUnionTypes1.ts, 61, 1)) + + { kind: "A", x: string } | +>kind : Symbol(kind, Decl(discriminatedUnionTypes1.ts, 64, 5)) +>x : Symbol(x, Decl(discriminatedUnionTypes1.ts, 64, 16)) + + { kind: "B" | "C", y: number } | +>kind : Symbol(kind, Decl(discriminatedUnionTypes1.ts, 65, 5)) +>y : Symbol(y, Decl(discriminatedUnionTypes1.ts, 65, 22)) + + { kind: "D" }; +>kind : Symbol(kind, Decl(discriminatedUnionTypes1.ts, 66, 5)) + +function f1(m: Message) { +>f1 : Symbol(f1, Decl(discriminatedUnionTypes1.ts, 66, 18)) +>m : Symbol(m, Decl(discriminatedUnionTypes1.ts, 68, 12)) +>Message : Symbol(Message, Decl(discriminatedUnionTypes1.ts, 61, 1)) + + if (m.kind === "A") { +>m.kind : Symbol(kind, Decl(discriminatedUnionTypes1.ts, 64, 5), Decl(discriminatedUnionTypes1.ts, 65, 5), Decl(discriminatedUnionTypes1.ts, 66, 5)) +>m : Symbol(m, Decl(discriminatedUnionTypes1.ts, 68, 12)) +>kind : Symbol(kind, Decl(discriminatedUnionTypes1.ts, 64, 5), Decl(discriminatedUnionTypes1.ts, 65, 5), Decl(discriminatedUnionTypes1.ts, 66, 5)) + + m; // { kind: "A", x: string } +>m : Symbol(m, Decl(discriminatedUnionTypes1.ts, 68, 12)) + } + else if (m.kind === "D") { +>m.kind : Symbol(kind, Decl(discriminatedUnionTypes1.ts, 65, 5), Decl(discriminatedUnionTypes1.ts, 66, 5)) +>m : Symbol(m, Decl(discriminatedUnionTypes1.ts, 68, 12)) +>kind : Symbol(kind, Decl(discriminatedUnionTypes1.ts, 65, 5), Decl(discriminatedUnionTypes1.ts, 66, 5)) + + m; // { kind: "D" } +>m : Symbol(m, Decl(discriminatedUnionTypes1.ts, 68, 12)) + } + else { + m; // { kind: "B" | "C", y: number } +>m : Symbol(m, Decl(discriminatedUnionTypes1.ts, 68, 12)) + } +} + +function f2(m: Message) { +>f2 : Symbol(f2, Decl(discriminatedUnionTypes1.ts, 78, 1)) +>m : Symbol(m, Decl(discriminatedUnionTypes1.ts, 80, 12)) +>Message : Symbol(Message, Decl(discriminatedUnionTypes1.ts, 61, 1)) + + if (m.kind === "A") { +>m.kind : Symbol(kind, Decl(discriminatedUnionTypes1.ts, 64, 5), Decl(discriminatedUnionTypes1.ts, 65, 5), Decl(discriminatedUnionTypes1.ts, 66, 5)) +>m : Symbol(m, Decl(discriminatedUnionTypes1.ts, 80, 12)) +>kind : Symbol(kind, Decl(discriminatedUnionTypes1.ts, 64, 5), Decl(discriminatedUnionTypes1.ts, 65, 5), Decl(discriminatedUnionTypes1.ts, 66, 5)) + + return; + } + m; // { kind: "B" | "C", y: number } | { kind: "D" } +>m : Symbol(m, Decl(discriminatedUnionTypes1.ts, 80, 12)) +} + +function f3(m: Message) { +>f3 : Symbol(f3, Decl(discriminatedUnionTypes1.ts, 85, 1)) +>m : Symbol(m, Decl(discriminatedUnionTypes1.ts, 87, 12)) +>Message : Symbol(Message, Decl(discriminatedUnionTypes1.ts, 61, 1)) + + if (m.kind === "X") { +>m.kind : Symbol(kind, Decl(discriminatedUnionTypes1.ts, 64, 5), Decl(discriminatedUnionTypes1.ts, 65, 5), Decl(discriminatedUnionTypes1.ts, 66, 5)) +>m : Symbol(m, Decl(discriminatedUnionTypes1.ts, 87, 12)) +>kind : Symbol(kind, Decl(discriminatedUnionTypes1.ts, 64, 5), Decl(discriminatedUnionTypes1.ts, 65, 5), Decl(discriminatedUnionTypes1.ts, 66, 5)) + + m; // never +>m : Symbol(m, Decl(discriminatedUnionTypes1.ts, 87, 12)) + } +} + +function f4(m: Message, x: "A" | "D") { +>f4 : Symbol(f4, Decl(discriminatedUnionTypes1.ts, 91, 1)) +>m : Symbol(m, Decl(discriminatedUnionTypes1.ts, 93, 12)) +>Message : Symbol(Message, Decl(discriminatedUnionTypes1.ts, 61, 1)) +>x : Symbol(x, Decl(discriminatedUnionTypes1.ts, 93, 23)) + + if (m.kind == x) { +>m.kind : Symbol(kind, Decl(discriminatedUnionTypes1.ts, 64, 5), Decl(discriminatedUnionTypes1.ts, 65, 5), Decl(discriminatedUnionTypes1.ts, 66, 5)) +>m : Symbol(m, Decl(discriminatedUnionTypes1.ts, 93, 12)) +>kind : Symbol(kind, Decl(discriminatedUnionTypes1.ts, 64, 5), Decl(discriminatedUnionTypes1.ts, 65, 5), Decl(discriminatedUnionTypes1.ts, 66, 5)) +>x : Symbol(x, Decl(discriminatedUnionTypes1.ts, 93, 23)) + + m; // { kind: "A", x: string } | { kind: "D" } +>m : Symbol(m, Decl(discriminatedUnionTypes1.ts, 93, 12)) + } +} + +function f5(m: Message) { +>f5 : Symbol(f5, Decl(discriminatedUnionTypes1.ts, 97, 1)) +>m : Symbol(m, Decl(discriminatedUnionTypes1.ts, 99, 12)) +>Message : Symbol(Message, Decl(discriminatedUnionTypes1.ts, 61, 1)) + + switch (m.kind) { +>m.kind : Symbol(kind, Decl(discriminatedUnionTypes1.ts, 64, 5), Decl(discriminatedUnionTypes1.ts, 65, 5), Decl(discriminatedUnionTypes1.ts, 66, 5)) +>m : Symbol(m, Decl(discriminatedUnionTypes1.ts, 99, 12)) +>kind : Symbol(kind, Decl(discriminatedUnionTypes1.ts, 64, 5), Decl(discriminatedUnionTypes1.ts, 65, 5), Decl(discriminatedUnionTypes1.ts, 66, 5)) + + case "A": + m; // { kind: "A", x: string } +>m : Symbol(m, Decl(discriminatedUnionTypes1.ts, 99, 12)) + + break; + case "D": + m; // { kind: "D" } +>m : Symbol(m, Decl(discriminatedUnionTypes1.ts, 99, 12)) + + break; + default: + m; // { kind: "B" | "C", y: number } +>m : Symbol(m, Decl(discriminatedUnionTypes1.ts, 99, 12)) + } +} + +function f6(m: Message) { +>f6 : Symbol(f6, Decl(discriminatedUnionTypes1.ts, 110, 1)) +>m : Symbol(m, Decl(discriminatedUnionTypes1.ts, 112, 12)) +>Message : Symbol(Message, Decl(discriminatedUnionTypes1.ts, 61, 1)) + + switch (m.kind) { +>m.kind : Symbol(kind, Decl(discriminatedUnionTypes1.ts, 64, 5), Decl(discriminatedUnionTypes1.ts, 65, 5), Decl(discriminatedUnionTypes1.ts, 66, 5)) +>m : Symbol(m, Decl(discriminatedUnionTypes1.ts, 112, 12)) +>kind : Symbol(kind, Decl(discriminatedUnionTypes1.ts, 64, 5), Decl(discriminatedUnionTypes1.ts, 65, 5), Decl(discriminatedUnionTypes1.ts, 66, 5)) + + case "A": + m; // { kind: "A", x: string } +>m : Symbol(m, Decl(discriminatedUnionTypes1.ts, 112, 12)) + + case "D": + m; // { kind: "A", x: string } | { kind: "D" } +>m : Symbol(m, Decl(discriminatedUnionTypes1.ts, 112, 12)) + + break; + default: + m; // { kind: "B" | "C", y: number } +>m : Symbol(m, Decl(discriminatedUnionTypes1.ts, 112, 12)) + } +} + +function f7(m: Message) { +>f7 : Symbol(f7, Decl(discriminatedUnionTypes1.ts, 122, 1)) +>m : Symbol(m, Decl(discriminatedUnionTypes1.ts, 124, 12)) +>Message : Symbol(Message, Decl(discriminatedUnionTypes1.ts, 61, 1)) + + switch (m.kind) { +>m.kind : Symbol(kind, Decl(discriminatedUnionTypes1.ts, 64, 5), Decl(discriminatedUnionTypes1.ts, 65, 5), Decl(discriminatedUnionTypes1.ts, 66, 5)) +>m : Symbol(m, Decl(discriminatedUnionTypes1.ts, 124, 12)) +>kind : Symbol(kind, Decl(discriminatedUnionTypes1.ts, 64, 5), Decl(discriminatedUnionTypes1.ts, 65, 5), Decl(discriminatedUnionTypes1.ts, 66, 5)) + + case "A": + case "B": + return; + } + m; // { kind: "B" | "C", y: number } | { kind: "D" } +>m : Symbol(m, Decl(discriminatedUnionTypes1.ts, 124, 12)) +} + +function f8(m: Message) { +>f8 : Symbol(f8, Decl(discriminatedUnionTypes1.ts, 131, 1)) +>m : Symbol(m, Decl(discriminatedUnionTypes1.ts, 133, 12)) +>Message : Symbol(Message, Decl(discriminatedUnionTypes1.ts, 61, 1)) + + switch (m.kind) { +>m.kind : Symbol(kind, Decl(discriminatedUnionTypes1.ts, 64, 5), Decl(discriminatedUnionTypes1.ts, 65, 5), Decl(discriminatedUnionTypes1.ts, 66, 5)) +>m : Symbol(m, Decl(discriminatedUnionTypes1.ts, 133, 12)) +>kind : Symbol(kind, Decl(discriminatedUnionTypes1.ts, 64, 5), Decl(discriminatedUnionTypes1.ts, 65, 5), Decl(discriminatedUnionTypes1.ts, 66, 5)) + + case "A": + return; + case "D": + throw new Error(); +>Error : Symbol(Error, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) + } + m; // { kind: "B" | "C", y: number } +>m : Symbol(m, Decl(discriminatedUnionTypes1.ts, 133, 12)) +} diff --git a/tests/baselines/reference/discriminatedUnionTypes1.types b/tests/baselines/reference/discriminatedUnionTypes1.types new file mode 100644 index 00000000000..234de28eaba --- /dev/null +++ b/tests/baselines/reference/discriminatedUnionTypes1.types @@ -0,0 +1,465 @@ +=== tests/cases/conformance/types/union/discriminatedUnionTypes1.ts === +interface Square { +>Square : Square + + kind: "square"; +>kind : "square" + + size: number; +>size : number +} + +interface Rectangle { +>Rectangle : Rectangle + + kind: "rectangle"; +>kind : "rectangle" + + width: number; +>width : number + + height: number; +>height : number +} + +interface Circle { +>Circle : Circle + + kind: "circle"; +>kind : "circle" + + radius: number; +>radius : number +} + +type Shape = Square | Rectangle | Circle; +>Shape : Square | Rectangle | Circle +>Square : Square +>Rectangle : Rectangle +>Circle : Circle + +function area1(s: Shape) { +>area1 : (s: Square | Rectangle | Circle) => number +>s : Square | Rectangle | Circle +>Shape : Square | Rectangle | Circle + + if (s.kind === "square") { +>s.kind === "square" : boolean +>s.kind : "square" | "rectangle" | "circle" +>s : Square | Rectangle | Circle +>kind : "square" | "rectangle" | "circle" +>"square" : string + + return s.size * s.size; +>s.size * s.size : number +>s.size : number +>s : Square +>size : number +>s.size : number +>s : Square +>size : number + } + else if (s.kind === "circle") { +>s.kind === "circle" : boolean +>s.kind : "rectangle" | "circle" +>s : Rectangle | Circle +>kind : "rectangle" | "circle" +>"circle" : string + + return Math.PI * s.radius * s.radius; +>Math.PI * s.radius * s.radius : number +>Math.PI * s.radius : number +>Math.PI : number +>Math : Math +>PI : number +>s.radius : number +>s : Circle +>radius : number +>s.radius : number +>s : Circle +>radius : number + } + else if (s.kind === "rectangle") { +>s.kind === "rectangle" : boolean +>s.kind : "rectangle" +>s : Rectangle +>kind : "rectangle" +>"rectangle" : string + + return s.width * s.height; +>s.width * s.height : number +>s.width : number +>s : Rectangle +>width : number +>s.height : number +>s : Rectangle +>height : number + } + else { + return 0; +>0 : number + } +} + +function area2(s: Shape) { +>area2 : (s: Square | Rectangle | Circle) => number +>s : Square | Rectangle | Circle +>Shape : Square | Rectangle | Circle + + switch (s.kind) { +>s.kind : "square" | "rectangle" | "circle" +>s : Square | Rectangle | Circle +>kind : "square" | "rectangle" | "circle" + + case "square": return s.size * s.size; +>"square" : string +>s.size * s.size : number +>s.size : number +>s : Square +>size : number +>s.size : number +>s : Square +>size : number + + case "rectangle": return s.width * s.height; +>"rectangle" : string +>s.width * s.height : number +>s.width : number +>s : Rectangle +>width : number +>s.height : number +>s : Rectangle +>height : number + + case "circle": return Math.PI * s.radius * s.radius; +>"circle" : string +>Math.PI * s.radius * s.radius : number +>Math.PI * s.radius : number +>Math.PI : number +>Math : Math +>PI : number +>s.radius : number +>s : Circle +>radius : number +>s.radius : number +>s : Circle +>radius : number + } +} + +function assertNever(x: never): never { +>assertNever : (x: never) => never +>x : never + + throw new Error("Unexpected object: " + x); +>new Error("Unexpected object: " + x) : Error +>Error : ErrorConstructor +>"Unexpected object: " + x : string +>"Unexpected object: " : string +>x : never +} + +function area3(s: Shape) { +>area3 : (s: Square | Rectangle | Circle) => number +>s : Square | Rectangle | Circle +>Shape : Square | Rectangle | Circle + + switch (s.kind) { +>s.kind : "square" | "rectangle" | "circle" +>s : Square | Rectangle | Circle +>kind : "square" | "rectangle" | "circle" + + case "square": return s.size * s.size; +>"square" : string +>s.size * s.size : number +>s.size : number +>s : Square +>size : number +>s.size : number +>s : Square +>size : number + + case "rectangle": return s.width * s.height; +>"rectangle" : string +>s.width * s.height : number +>s.width : number +>s : Rectangle +>width : number +>s.height : number +>s : Rectangle +>height : number + + case "circle": return Math.PI * s.radius * s.radius; +>"circle" : string +>Math.PI * s.radius * s.radius : number +>Math.PI * s.radius : number +>Math.PI : number +>Math : Math +>PI : number +>s.radius : number +>s : Circle +>radius : number +>s.radius : number +>s : Circle +>radius : number + + default: return assertNever(s); +>assertNever(s) : never +>assertNever : (x: never) => never +>s : never + } +} + +function area4(s: Shape) { +>area4 : (s: Square | Rectangle | Circle) => number +>s : Square | Rectangle | Circle +>Shape : Square | Rectangle | Circle + + switch (s.kind) { +>s.kind : "square" | "rectangle" | "circle" +>s : Square | Rectangle | Circle +>kind : "square" | "rectangle" | "circle" + + case "square": return s.size * s.size; +>"square" : string +>s.size * s.size : number +>s.size : number +>s : Square +>size : number +>s.size : number +>s : Square +>size : number + + case "rectangle": return s.width * s.height; +>"rectangle" : string +>s.width * s.height : number +>s.width : number +>s : Rectangle +>width : number +>s.height : number +>s : Rectangle +>height : number + + case "circle": return Math.PI * s.radius * s.radius; +>"circle" : string +>Math.PI * s.radius * s.radius : number +>Math.PI * s.radius : number +>Math.PI : number +>Math : Math +>PI : number +>s.radius : number +>s : Circle +>radius : number +>s.radius : number +>s : Circle +>radius : number + } + return assertNever(s); +>assertNever(s) : never +>assertNever : (x: never) => never +>s : never +} + +type Message = +>Message : { kind: "A"; x: string; } | { kind: "B" | "C"; y: number; } | { kind: "D"; } + + { kind: "A", x: string } | +>kind : "A" +>x : string + + { kind: "B" | "C", y: number } | +>kind : "B" | "C" +>y : number + + { kind: "D" }; +>kind : "D" + +function f1(m: Message) { +>f1 : (m: { kind: "A"; x: string; } | { kind: "B" | "C"; y: number; } | { kind: "D"; }) => void +>m : { kind: "A"; x: string; } | { kind: "B" | "C"; y: number; } | { kind: "D"; } +>Message : { kind: "A"; x: string; } | { kind: "B" | "C"; y: number; } | { kind: "D"; } + + if (m.kind === "A") { +>m.kind === "A" : boolean +>m.kind : "A" | "B" | "C" | "D" +>m : { kind: "A"; x: string; } | { kind: "B" | "C"; y: number; } | { kind: "D"; } +>kind : "A" | "B" | "C" | "D" +>"A" : string + + m; // { kind: "A", x: string } +>m : { kind: "A"; x: string; } + } + else if (m.kind === "D") { +>m.kind === "D" : boolean +>m.kind : "B" | "C" | "D" +>m : { kind: "B" | "C"; y: number; } | { kind: "D"; } +>kind : "B" | "C" | "D" +>"D" : string + + m; // { kind: "D" } +>m : { kind: "D"; } + } + else { + m; // { kind: "B" | "C", y: number } +>m : { kind: "B" | "C"; y: number; } + } +} + +function f2(m: Message) { +>f2 : (m: { kind: "A"; x: string; } | { kind: "B" | "C"; y: number; } | { kind: "D"; }) => void +>m : { kind: "A"; x: string; } | { kind: "B" | "C"; y: number; } | { kind: "D"; } +>Message : { kind: "A"; x: string; } | { kind: "B" | "C"; y: number; } | { kind: "D"; } + + if (m.kind === "A") { +>m.kind === "A" : boolean +>m.kind : "A" | "B" | "C" | "D" +>m : { kind: "A"; x: string; } | { kind: "B" | "C"; y: number; } | { kind: "D"; } +>kind : "A" | "B" | "C" | "D" +>"A" : string + + return; + } + m; // { kind: "B" | "C", y: number } | { kind: "D" } +>m : { kind: "B" | "C"; y: number; } | { kind: "D"; } +} + +function f3(m: Message) { +>f3 : (m: { kind: "A"; x: string; } | { kind: "B" | "C"; y: number; } | { kind: "D"; }) => void +>m : { kind: "A"; x: string; } | { kind: "B" | "C"; y: number; } | { kind: "D"; } +>Message : { kind: "A"; x: string; } | { kind: "B" | "C"; y: number; } | { kind: "D"; } + + if (m.kind === "X") { +>m.kind === "X" : boolean +>m.kind : "A" | "B" | "C" | "D" +>m : { kind: "A"; x: string; } | { kind: "B" | "C"; y: number; } | { kind: "D"; } +>kind : "A" | "B" | "C" | "D" +>"X" : string + + m; // never +>m : never + } +} + +function f4(m: Message, x: "A" | "D") { +>f4 : (m: { kind: "A"; x: string; } | { kind: "B" | "C"; y: number; } | { kind: "D"; }, x: "A" | "D") => void +>m : { kind: "A"; x: string; } | { kind: "B" | "C"; y: number; } | { kind: "D"; } +>Message : { kind: "A"; x: string; } | { kind: "B" | "C"; y: number; } | { kind: "D"; } +>x : "A" | "D" + + if (m.kind == x) { +>m.kind == x : boolean +>m.kind : "A" | "B" | "C" | "D" +>m : { kind: "A"; x: string; } | { kind: "B" | "C"; y: number; } | { kind: "D"; } +>kind : "A" | "B" | "C" | "D" +>x : "A" | "D" + + m; // { kind: "A", x: string } | { kind: "D" } +>m : { kind: "A"; x: string; } | { kind: "D"; } + } +} + +function f5(m: Message) { +>f5 : (m: { kind: "A"; x: string; } | { kind: "B" | "C"; y: number; } | { kind: "D"; }) => void +>m : { kind: "A"; x: string; } | { kind: "B" | "C"; y: number; } | { kind: "D"; } +>Message : { kind: "A"; x: string; } | { kind: "B" | "C"; y: number; } | { kind: "D"; } + + switch (m.kind) { +>m.kind : "A" | "B" | "C" | "D" +>m : { kind: "A"; x: string; } | { kind: "B" | "C"; y: number; } | { kind: "D"; } +>kind : "A" | "B" | "C" | "D" + + case "A": +>"A" : string + + m; // { kind: "A", x: string } +>m : { kind: "A"; x: string; } + + break; + case "D": +>"D" : string + + m; // { kind: "D" } +>m : { kind: "D"; } + + break; + default: + m; // { kind: "B" | "C", y: number } +>m : { kind: "B" | "C"; y: number; } + } +} + +function f6(m: Message) { +>f6 : (m: { kind: "A"; x: string; } | { kind: "B" | "C"; y: number; } | { kind: "D"; }) => void +>m : { kind: "A"; x: string; } | { kind: "B" | "C"; y: number; } | { kind: "D"; } +>Message : { kind: "A"; x: string; } | { kind: "B" | "C"; y: number; } | { kind: "D"; } + + switch (m.kind) { +>m.kind : "A" | "B" | "C" | "D" +>m : { kind: "A"; x: string; } | { kind: "B" | "C"; y: number; } | { kind: "D"; } +>kind : "A" | "B" | "C" | "D" + + case "A": +>"A" : string + + m; // { kind: "A", x: string } +>m : { kind: "A"; x: string; } + + case "D": +>"D" : string + + m; // { kind: "A", x: string } | { kind: "D" } +>m : { kind: "D"; } | { kind: "A"; x: string; } + + break; + default: + m; // { kind: "B" | "C", y: number } +>m : { kind: "B" | "C"; y: number; } + } +} + +function f7(m: Message) { +>f7 : (m: { kind: "A"; x: string; } | { kind: "B" | "C"; y: number; } | { kind: "D"; }) => void +>m : { kind: "A"; x: string; } | { kind: "B" | "C"; y: number; } | { kind: "D"; } +>Message : { kind: "A"; x: string; } | { kind: "B" | "C"; y: number; } | { kind: "D"; } + + switch (m.kind) { +>m.kind : "A" | "B" | "C" | "D" +>m : { kind: "A"; x: string; } | { kind: "B" | "C"; y: number; } | { kind: "D"; } +>kind : "A" | "B" | "C" | "D" + + case "A": +>"A" : string + + case "B": +>"B" : string + + return; + } + m; // { kind: "B" | "C", y: number } | { kind: "D" } +>m : { kind: "B" | "C"; y: number; } | { kind: "D"; } +} + +function f8(m: Message) { +>f8 : (m: { kind: "A"; x: string; } | { kind: "B" | "C"; y: number; } | { kind: "D"; }) => void +>m : { kind: "A"; x: string; } | { kind: "B" | "C"; y: number; } | { kind: "D"; } +>Message : { kind: "A"; x: string; } | { kind: "B" | "C"; y: number; } | { kind: "D"; } + + switch (m.kind) { +>m.kind : "A" | "B" | "C" | "D" +>m : { kind: "A"; x: string; } | { kind: "B" | "C"; y: number; } | { kind: "D"; } +>kind : "A" | "B" | "C" | "D" + + case "A": +>"A" : string + + return; + case "D": +>"D" : string + + throw new Error(); +>new Error() : Error +>Error : ErrorConstructor + } + m; // { kind: "B" | "C", y: number } +>m : { kind: "B" | "C"; y: number; } +} diff --git a/tests/cases/conformance/types/union/discriminatedUnionTypes1.ts b/tests/cases/conformance/types/union/discriminatedUnionTypes1.ts new file mode 100644 index 00000000000..404162308b9 --- /dev/null +++ b/tests/cases/conformance/types/union/discriminatedUnionTypes1.ts @@ -0,0 +1,142 @@ +interface Square { + kind: "square"; + size: number; +} + +interface Rectangle { + kind: "rectangle"; + width: number; + height: number; +} + +interface Circle { + kind: "circle"; + radius: number; +} + +type Shape = Square | Rectangle | Circle; + +function area1(s: Shape) { + if (s.kind === "square") { + return s.size * s.size; + } + else if (s.kind === "circle") { + return Math.PI * s.radius * s.radius; + } + else if (s.kind === "rectangle") { + return s.width * s.height; + } + else { + return 0; + } +} + +function area2(s: Shape) { + switch (s.kind) { + case "square": return s.size * s.size; + case "rectangle": return s.width * s.height; + case "circle": return Math.PI * s.radius * s.radius; + } +} + +function assertNever(x: never): never { + throw new Error("Unexpected object: " + x); +} + +function area3(s: Shape) { + switch (s.kind) { + case "square": return s.size * s.size; + case "rectangle": return s.width * s.height; + case "circle": return Math.PI * s.radius * s.radius; + default: return assertNever(s); + } +} + +function area4(s: Shape) { + switch (s.kind) { + case "square": return s.size * s.size; + case "rectangle": return s.width * s.height; + case "circle": return Math.PI * s.radius * s.radius; + } + return assertNever(s); +} + +type Message = + { kind: "A", x: string } | + { kind: "B" | "C", y: number } | + { kind: "D" }; + +function f1(m: Message) { + if (m.kind === "A") { + m; // { kind: "A", x: string } + } + else if (m.kind === "D") { + m; // { kind: "D" } + } + else { + m; // { kind: "B" | "C", y: number } + } +} + +function f2(m: Message) { + if (m.kind === "A") { + return; + } + m; // { kind: "B" | "C", y: number } | { kind: "D" } +} + +function f3(m: Message) { + if (m.kind === "X") { + m; // never + } +} + +function f4(m: Message, x: "A" | "D") { + if (m.kind == x) { + m; // { kind: "A", x: string } | { kind: "D" } + } +} + +function f5(m: Message) { + switch (m.kind) { + case "A": + m; // { kind: "A", x: string } + break; + case "D": + m; // { kind: "D" } + break; + default: + m; // { kind: "B" | "C", y: number } + } +} + +function f6(m: Message) { + switch (m.kind) { + case "A": + m; // { kind: "A", x: string } + case "D": + m; // { kind: "A", x: string } | { kind: "D" } + break; + default: + m; // { kind: "B" | "C", y: number } + } +} + +function f7(m: Message) { + switch (m.kind) { + case "A": + case "B": + return; + } + m; // { kind: "B" | "C", y: number } | { kind: "D" } +} + +function f8(m: Message) { + switch (m.kind) { + case "A": + return; + case "D": + throw new Error(); + } + m; // { kind: "B" | "C", y: number } +} \ No newline at end of file From 5ea469a8a88c2becfc8b05d984f35efea5434cc9 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Mon, 13 Jun 2016 17:05:44 -0700 Subject: [PATCH 21/56] No Need to store dot token when parsing property access expression --- src/compiler/emitter.ts | 15 ++++++++------- src/compiler/parser.ts | 4 +--- src/compiler/types.ts | 1 - tests/cases/unittests/incrementalParser.ts | 2 +- 4 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 481fe1b0c3e..101778e7322 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -2076,7 +2076,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge function createPropertyAccessExpression(expression: Expression, name: Identifier): PropertyAccessExpression { const result = createSynthesizedNode(SyntaxKind.PropertyAccessExpression); result.expression = parenthesizeForAccess(expression); - result.dotToken = createSynthesizedNode(SyntaxKind.DotToken); result.name = name; return result; } @@ -2223,11 +2222,11 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge // Returns 'true' if the code was actually indented, false otherwise. // If the code is not indented, an optional valueToWriteWhenNotIndenting will be // emitted instead. - function indentIfOnDifferentLines(parent: Node, node1: Node, node2: Node, valueToWriteWhenNotIndenting?: string): boolean { + function indentIfOnDifferentLines(parent: Node, node1: TextRange, node2: TextRange, valueToWriteWhenNotIndenting?: string): boolean { const realNodesAreOnDifferentLines = !nodeIsSynthesized(parent) && !nodeEndIsOnSameLineAsNodeStart(node1, node2); // Always use a newline for synthesized code if the synthesizer desires it. - const synthesizedNodeIsOnDifferentLine = synthesizedNodeStartsOnNewLine(node2); + const synthesizedNodeIsOnDifferentLine = synthesizedNodeStartsOnNewLine(node2 as Node); if (realNodesAreOnDifferentLines || synthesizedNodeIsOnDifferentLine) { increaseIndent(); @@ -2257,7 +2256,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge } emit(node.expression); - const indentedBeforeDot = indentIfOnDifferentLines(node, node.expression, node.dotToken); + const dotRangeStart = nodeIsSynthesized(node.expression) ? -1 : node.expression.end; + const dotRangeEnd = nodeIsSynthesized(node.expression) ? -1 : skipTrivia(currentText, node.expression.end) + 1; + const dotToken = { pos: dotRangeStart, end: dotRangeEnd }; + const indentedBeforeDot = indentIfOnDifferentLines(node, node.expression, dotToken); // 1 .toString is a valid property access, emit a space after the literal // Also emit a space if expression is a integer const enum value - it will appear in generated code as numeric literal @@ -2283,7 +2285,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge write("."); } - const indentedAfterDot = indentIfOnDifferentLines(node, node.dotToken, node.name); + const indentedAfterDot = indentIfOnDifferentLines(node, dotToken, node.name); emit(node.name); decreaseIndentIf(indentedBeforeDot, indentedAfterDot); } @@ -2780,7 +2782,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge const identifier = emitTempVariableAssignment(leftHandSideExpression.expression, /*canDefineTempVariablesInPlace*/ false, /*shouldEmitCommaBeforeAssignment*/ false); synthesizedLHS.expression = identifier; - (synthesizedLHS).dotToken = leftHandSideExpression.dotToken; (synthesizedLHS).name = leftHandSideExpression.name; write(", "); } @@ -3758,7 +3759,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge getLineOfLocalPositionFromLineMap(currentLineMap, node2.end); } - function nodeEndIsOnSameLineAsNodeStart(node1: Node, node2: Node) { + function nodeEndIsOnSameLineAsNodeStart(node1: TextRange, node2: TextRange) { return getLineOfLocalPositionFromLineMap(currentLineMap, node1.end) === getLineOfLocalPositionFromLineMap(currentLineMap, skipTrivia(currentText, node2.pos)); } diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index a31cf0cbe93..b2f3755ec46 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -137,7 +137,6 @@ namespace ts { return visitNodes(cbNodes, (node).properties); case SyntaxKind.PropertyAccessExpression: return visitNode(cbNode, (node).expression) || - visitNode(cbNode, (node).dotToken) || visitNode(cbNode, (node).name); case SyntaxKind.ElementAccessExpression: return visitNode(cbNode, (node).expression) || @@ -3571,7 +3570,7 @@ namespace ts { // If it wasn't then just try to parse out a '.' and report an error. const node = createNode(SyntaxKind.PropertyAccessExpression, expression.pos); node.expression = expression; - node.dotToken = parseExpectedToken(SyntaxKind.DotToken, /*reportAtCurrentPosition*/ false, Diagnostics.super_must_be_followed_by_an_argument_list_or_member_access); + parseExpectedToken(SyntaxKind.DotToken, /*reportAtCurrentPosition*/ false, Diagnostics.super_must_be_followed_by_an_argument_list_or_member_access); node.name = parseRightSideOfDot(/*allowIdentifierNames*/ true); return finishNode(node); } @@ -3807,7 +3806,6 @@ namespace ts { if (dotToken) { const propertyAccess = createNode(SyntaxKind.PropertyAccessExpression, expression.pos); propertyAccess.expression = expression; - propertyAccess.dotToken = dotToken; propertyAccess.name = parseRightSideOfDot(/*allowIdentifierNames*/ true); expression = finishNode(propertyAccess); continue; diff --git a/src/compiler/types.ts b/src/compiler/types.ts index db34bb7c6e8..b28223b7520 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -980,7 +980,6 @@ namespace ts { // @kind(SyntaxKind.PropertyAccessExpression) export interface PropertyAccessExpression extends MemberExpression, Declaration { expression: LeftHandSideExpression; - dotToken: Node; name: Identifier; } diff --git a/tests/cases/unittests/incrementalParser.ts b/tests/cases/unittests/incrementalParser.ts index 0988d51e11b..741c9e54cad 100644 --- a/tests/cases/unittests/incrementalParser.ts +++ b/tests/cases/unittests/incrementalParser.ts @@ -647,7 +647,7 @@ module m3 { }\ const oldText = ScriptSnapshot.fromString(source); const newTextAndChange = withInsert(oldText, 0, ""); - compareTrees(oldText, newTextAndChange.text, newTextAndChange.textChangeRange, 8); + compareTrees(oldText, newTextAndChange.text, newTextAndChange.textChangeRange, 7); }); it("Class to interface", () => { From 5a90c6777aec2284e3ae65a7f335be69af210a5f Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Tue, 14 Jun 2016 11:33:41 -0700 Subject: [PATCH 22/56] Added tests. --- .../types/tuple/emptyTuples/emptyTuplesTypeAssertion01.ts | 4 ++++ .../types/tuple/emptyTuples/emptyTuplesTypeAssertion02.ts | 4 ++++ 2 files changed, 8 insertions(+) create mode 100644 tests/cases/conformance/types/tuple/emptyTuples/emptyTuplesTypeAssertion01.ts create mode 100644 tests/cases/conformance/types/tuple/emptyTuples/emptyTuplesTypeAssertion02.ts diff --git a/tests/cases/conformance/types/tuple/emptyTuples/emptyTuplesTypeAssertion01.ts b/tests/cases/conformance/types/tuple/emptyTuples/emptyTuplesTypeAssertion01.ts new file mode 100644 index 00000000000..2e1e2d42499 --- /dev/null +++ b/tests/cases/conformance/types/tuple/emptyTuples/emptyTuplesTypeAssertion01.ts @@ -0,0 +1,4 @@ +// @declaration: true + +let x = <[]>[]; +let y = x[0]; \ No newline at end of file diff --git a/tests/cases/conformance/types/tuple/emptyTuples/emptyTuplesTypeAssertion02.ts b/tests/cases/conformance/types/tuple/emptyTuples/emptyTuplesTypeAssertion02.ts new file mode 100644 index 00000000000..43f63e9ce77 --- /dev/null +++ b/tests/cases/conformance/types/tuple/emptyTuples/emptyTuplesTypeAssertion02.ts @@ -0,0 +1,4 @@ +// @declaration: true + +let x = [] as []; +let y = x[0]; \ No newline at end of file From 0a1c4c60da9609155ba65dcfc5932ce748c3c3ba Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Tue, 14 Jun 2016 11:36:14 -0700 Subject: [PATCH 23/56] Accepted baselines. --- .../reference/emptyTuplesTypeAssertion01.js | 27 +++++++++++++++++++ .../emptyTuplesTypeAssertion01.symbols | 9 +++++++ .../emptyTuplesTypeAssertion01.types | 13 +++++++++ .../reference/emptyTuplesTypeAssertion02.js | 27 +++++++++++++++++++ .../emptyTuplesTypeAssertion02.symbols | 9 +++++++ .../emptyTuplesTypeAssertion02.types | 13 +++++++++ 6 files changed, 98 insertions(+) create mode 100644 tests/baselines/reference/emptyTuplesTypeAssertion01.js create mode 100644 tests/baselines/reference/emptyTuplesTypeAssertion01.symbols create mode 100644 tests/baselines/reference/emptyTuplesTypeAssertion01.types create mode 100644 tests/baselines/reference/emptyTuplesTypeAssertion02.js create mode 100644 tests/baselines/reference/emptyTuplesTypeAssertion02.symbols create mode 100644 tests/baselines/reference/emptyTuplesTypeAssertion02.types diff --git a/tests/baselines/reference/emptyTuplesTypeAssertion01.js b/tests/baselines/reference/emptyTuplesTypeAssertion01.js new file mode 100644 index 00000000000..b02caf3ceaf --- /dev/null +++ b/tests/baselines/reference/emptyTuplesTypeAssertion01.js @@ -0,0 +1,27 @@ +//// [emptyTuplesTypeAssertion01.ts] + +let x = <[]>[]; +let y = x[0]; + +//// [emptyTuplesTypeAssertion01.js] +var x = []; +var y = x[0]; + + +//// [emptyTuplesTypeAssertion01.d.ts] +declare let x: []; +declare let y: never; + + +//// [DtsFileErrors] + + +tests/cases/conformance/types/tuple/emptyTuples/emptyTuplesTypeAssertion01.d.ts(1,16): error TS1122: A tuple type element list cannot be empty. + + +==== tests/cases/conformance/types/tuple/emptyTuples/emptyTuplesTypeAssertion01.d.ts (1 errors) ==== + declare let x: []; + ~~ +!!! error TS1122: A tuple type element list cannot be empty. + declare let y: never; + \ No newline at end of file diff --git a/tests/baselines/reference/emptyTuplesTypeAssertion01.symbols b/tests/baselines/reference/emptyTuplesTypeAssertion01.symbols new file mode 100644 index 00000000000..d6f3cd4e9db --- /dev/null +++ b/tests/baselines/reference/emptyTuplesTypeAssertion01.symbols @@ -0,0 +1,9 @@ +=== tests/cases/conformance/types/tuple/emptyTuples/emptyTuplesTypeAssertion01.ts === + +let x = <[]>[]; +>x : Symbol(x, Decl(emptyTuplesTypeAssertion01.ts, 1, 3)) + +let y = x[0]; +>y : Symbol(y, Decl(emptyTuplesTypeAssertion01.ts, 2, 3)) +>x : Symbol(x, Decl(emptyTuplesTypeAssertion01.ts, 1, 3)) + diff --git a/tests/baselines/reference/emptyTuplesTypeAssertion01.types b/tests/baselines/reference/emptyTuplesTypeAssertion01.types new file mode 100644 index 00000000000..e0ef78c86e7 --- /dev/null +++ b/tests/baselines/reference/emptyTuplesTypeAssertion01.types @@ -0,0 +1,13 @@ +=== tests/cases/conformance/types/tuple/emptyTuples/emptyTuplesTypeAssertion01.ts === + +let x = <[]>[]; +>x : [] +><[]>[] : [] +>[] : undefined[] + +let y = x[0]; +>y : never +>x[0] : never +>x : [] +>0 : number + diff --git a/tests/baselines/reference/emptyTuplesTypeAssertion02.js b/tests/baselines/reference/emptyTuplesTypeAssertion02.js new file mode 100644 index 00000000000..0a6c3e5a984 --- /dev/null +++ b/tests/baselines/reference/emptyTuplesTypeAssertion02.js @@ -0,0 +1,27 @@ +//// [emptyTuplesTypeAssertion02.ts] + +let x = [] as []; +let y = x[0]; + +//// [emptyTuplesTypeAssertion02.js] +var x = []; +var y = x[0]; + + +//// [emptyTuplesTypeAssertion02.d.ts] +declare let x: []; +declare let y: never; + + +//// [DtsFileErrors] + + +tests/cases/conformance/types/tuple/emptyTuples/emptyTuplesTypeAssertion02.d.ts(1,16): error TS1122: A tuple type element list cannot be empty. + + +==== tests/cases/conformance/types/tuple/emptyTuples/emptyTuplesTypeAssertion02.d.ts (1 errors) ==== + declare let x: []; + ~~ +!!! error TS1122: A tuple type element list cannot be empty. + declare let y: never; + \ No newline at end of file diff --git a/tests/baselines/reference/emptyTuplesTypeAssertion02.symbols b/tests/baselines/reference/emptyTuplesTypeAssertion02.symbols new file mode 100644 index 00000000000..a5eeec95a32 --- /dev/null +++ b/tests/baselines/reference/emptyTuplesTypeAssertion02.symbols @@ -0,0 +1,9 @@ +=== tests/cases/conformance/types/tuple/emptyTuples/emptyTuplesTypeAssertion02.ts === + +let x = [] as []; +>x : Symbol(x, Decl(emptyTuplesTypeAssertion02.ts, 1, 3)) + +let y = x[0]; +>y : Symbol(y, Decl(emptyTuplesTypeAssertion02.ts, 2, 3)) +>x : Symbol(x, Decl(emptyTuplesTypeAssertion02.ts, 1, 3)) + diff --git a/tests/baselines/reference/emptyTuplesTypeAssertion02.types b/tests/baselines/reference/emptyTuplesTypeAssertion02.types new file mode 100644 index 00000000000..2dc7e823516 --- /dev/null +++ b/tests/baselines/reference/emptyTuplesTypeAssertion02.types @@ -0,0 +1,13 @@ +=== tests/cases/conformance/types/tuple/emptyTuples/emptyTuplesTypeAssertion02.ts === + +let x = [] as []; +>x : [] +>[] as [] : [] +>[] : undefined[] + +let y = x[0]; +>y : never +>x[0] : never +>x : [] +>0 : number + From c9bab058d549fc846b346c45c1df5ac87b707cef Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Tue, 14 Jun 2016 13:06:28 -0700 Subject: [PATCH 24/56] Check tuple types when getting the type node's type. --- src/compiler/checker.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 47b0a53a8c3..51ef0a6058c 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -5027,6 +5027,7 @@ namespace ts { function getTypeFromTupleTypeNode(node: TupleTypeNode): Type { const links = getNodeLinks(node); if (!links.resolvedType) { + checkTupleType(node); links.resolvedType = createTupleType(map(node.elementTypes, getTypeFromTypeNode)); } return links.resolvedType; From 82b385f33122eeebdd266d851ebd479020838847 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Tue, 14 Jun 2016 15:15:42 -0700 Subject: [PATCH 25/56] Accepted baselines. --- .../emptyTuplesTypeAssertion01.errors.txt | 9 +++++++++ .../reference/emptyTuplesTypeAssertion01.js | 14 -------------- .../emptyTuplesTypeAssertion02.errors.txt | 9 +++++++++ .../reference/emptyTuplesTypeAssertion02.js | 14 -------------- 4 files changed, 18 insertions(+), 28 deletions(-) create mode 100644 tests/baselines/reference/emptyTuplesTypeAssertion01.errors.txt create mode 100644 tests/baselines/reference/emptyTuplesTypeAssertion02.errors.txt diff --git a/tests/baselines/reference/emptyTuplesTypeAssertion01.errors.txt b/tests/baselines/reference/emptyTuplesTypeAssertion01.errors.txt new file mode 100644 index 00000000000..4d70873bd74 --- /dev/null +++ b/tests/baselines/reference/emptyTuplesTypeAssertion01.errors.txt @@ -0,0 +1,9 @@ +tests/cases/conformance/types/tuple/emptyTuples/emptyTuplesTypeAssertion01.ts(2,10): error TS1122: A tuple type element list cannot be empty. + + +==== tests/cases/conformance/types/tuple/emptyTuples/emptyTuplesTypeAssertion01.ts (1 errors) ==== + + let x = <[]>[]; + ~~ +!!! error TS1122: A tuple type element list cannot be empty. + let y = x[0]; \ No newline at end of file diff --git a/tests/baselines/reference/emptyTuplesTypeAssertion01.js b/tests/baselines/reference/emptyTuplesTypeAssertion01.js index b02caf3ceaf..6ab1ec82bd5 100644 --- a/tests/baselines/reference/emptyTuplesTypeAssertion01.js +++ b/tests/baselines/reference/emptyTuplesTypeAssertion01.js @@ -11,17 +11,3 @@ var y = x[0]; //// [emptyTuplesTypeAssertion01.d.ts] declare let x: []; declare let y: never; - - -//// [DtsFileErrors] - - -tests/cases/conformance/types/tuple/emptyTuples/emptyTuplesTypeAssertion01.d.ts(1,16): error TS1122: A tuple type element list cannot be empty. - - -==== tests/cases/conformance/types/tuple/emptyTuples/emptyTuplesTypeAssertion01.d.ts (1 errors) ==== - declare let x: []; - ~~ -!!! error TS1122: A tuple type element list cannot be empty. - declare let y: never; - \ No newline at end of file diff --git a/tests/baselines/reference/emptyTuplesTypeAssertion02.errors.txt b/tests/baselines/reference/emptyTuplesTypeAssertion02.errors.txt new file mode 100644 index 00000000000..0cbc0d7371c --- /dev/null +++ b/tests/baselines/reference/emptyTuplesTypeAssertion02.errors.txt @@ -0,0 +1,9 @@ +tests/cases/conformance/types/tuple/emptyTuples/emptyTuplesTypeAssertion02.ts(2,15): error TS1122: A tuple type element list cannot be empty. + + +==== tests/cases/conformance/types/tuple/emptyTuples/emptyTuplesTypeAssertion02.ts (1 errors) ==== + + let x = [] as []; + ~~ +!!! error TS1122: A tuple type element list cannot be empty. + let y = x[0]; \ No newline at end of file diff --git a/tests/baselines/reference/emptyTuplesTypeAssertion02.js b/tests/baselines/reference/emptyTuplesTypeAssertion02.js index 0a6c3e5a984..ad112732cd2 100644 --- a/tests/baselines/reference/emptyTuplesTypeAssertion02.js +++ b/tests/baselines/reference/emptyTuplesTypeAssertion02.js @@ -11,17 +11,3 @@ var y = x[0]; //// [emptyTuplesTypeAssertion02.d.ts] declare let x: []; declare let y: never; - - -//// [DtsFileErrors] - - -tests/cases/conformance/types/tuple/emptyTuples/emptyTuplesTypeAssertion02.d.ts(1,16): error TS1122: A tuple type element list cannot be empty. - - -==== tests/cases/conformance/types/tuple/emptyTuples/emptyTuplesTypeAssertion02.d.ts (1 errors) ==== - declare let x: []; - ~~ -!!! error TS1122: A tuple type element list cannot be empty. - declare let y: never; - \ No newline at end of file From 784a76530cb6f1dfe99b1092514773c3947465d0 Mon Sep 17 00:00:00 2001 From: Mohamed Hegazy Date: Tue, 14 Jun 2016 17:44:57 -0700 Subject: [PATCH 26/56] Fix #9173: clear out lib and types before creating a program in transpileModule --- src/services/services.ts | 4 ++++ tests/cases/unittests/transpile.ts | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/services/services.ts b/src/services/services.ts index 8bf6a572ec7..c20f95a56f2 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -2001,6 +2001,10 @@ namespace ts { // so pass --noLib to avoid reporting a file not found error. options.noLib = true; + // Clear out the lib and types option as well + options.lib = undefined; + options.types = undefined; + // We are not doing a full typecheck, we are not resolving the whole context, // so pass --noResolve to avoid reporting missing file errors. options.noResolve = true; diff --git a/tests/cases/unittests/transpile.ts b/tests/cases/unittests/transpile.ts index a4b1416ed29..cac44994420 100644 --- a/tests/cases/unittests/transpile.ts +++ b/tests/cases/unittests/transpile.ts @@ -304,6 +304,24 @@ var x = 0;`, test("var x", { expectedOutput: `"use strict";\r\nvar x;\r\n`, options: { fileName: "http://somewhere/directory//directory2/file.ts" } }); }); + it("Support options with lib values", () => { + const input = "const a = 10;"; + const output = `"use strict";\r\nvar a = 10;\r\n`; + test(input, { + expectedOutput: output, + options: { compilerOptions: { lib: ["es6", "dom"], module: ModuleKind.CommonJS }, fileName: "input.js", reportDiagnostics: true } + }); + }); + + it("Support options with types values", () => { + const input = "const a = 10;"; + const output = `"use strict";\r\nvar a = 10;\r\n`; + test(input, { + expectedOutput: output, + options: { compilerOptions: { types: ["jquery", "typescript"], module: ModuleKind.CommonJS }, fileName: "input.js", reportDiagnostics: true } + }); + }); + describe("String values for enums", () => { it("Accepts strings instead of enum values", () => { test(`export const x = 0`, { From 14c2bcf73ad5e5eead446beaf8955211b96a8957 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Tue, 14 Jun 2016 19:02:47 -0700 Subject: [PATCH 27/56] Added tests. --- .../typeAssertions/duplicatePropertiesInTypeAssertions01.ts | 3 +++ .../typeAssertions/duplicatePropertiesInTypeAssertions02.ts | 3 +++ 2 files changed, 6 insertions(+) create mode 100644 tests/cases/conformance/expressions/typeAssertions/duplicatePropertiesInTypeAssertions01.ts create mode 100644 tests/cases/conformance/expressions/typeAssertions/duplicatePropertiesInTypeAssertions02.ts diff --git a/tests/cases/conformance/expressions/typeAssertions/duplicatePropertiesInTypeAssertions01.ts b/tests/cases/conformance/expressions/typeAssertions/duplicatePropertiesInTypeAssertions01.ts new file mode 100644 index 00000000000..2f3da304c99 --- /dev/null +++ b/tests/cases/conformance/expressions/typeAssertions/duplicatePropertiesInTypeAssertions01.ts @@ -0,0 +1,3 @@ +// @declaration: true + +let x = <{a: number; a: number}>{}; \ No newline at end of file diff --git a/tests/cases/conformance/expressions/typeAssertions/duplicatePropertiesInTypeAssertions02.ts b/tests/cases/conformance/expressions/typeAssertions/duplicatePropertiesInTypeAssertions02.ts new file mode 100644 index 00000000000..265fdbe5321 --- /dev/null +++ b/tests/cases/conformance/expressions/typeAssertions/duplicatePropertiesInTypeAssertions02.ts @@ -0,0 +1,3 @@ +// @declaration: true + +let x = {} as {a: number; a: number}; \ No newline at end of file From f786c5c1874f20ec793204342c86f3ebfa77b24c Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Tue, 14 Jun 2016 19:03:00 -0700 Subject: [PATCH 28/56] Accepted baselines. --- .../duplicatePropertiesInTypeAssertions01.js | 12 ++++++++++++ .../duplicatePropertiesInTypeAssertions01.symbols | 7 +++++++ .../duplicatePropertiesInTypeAssertions01.types | 9 +++++++++ .../duplicatePropertiesInTypeAssertions02.js | 12 ++++++++++++ .../duplicatePropertiesInTypeAssertions02.symbols | 7 +++++++ .../duplicatePropertiesInTypeAssertions02.types | 9 +++++++++ 6 files changed, 56 insertions(+) create mode 100644 tests/baselines/reference/duplicatePropertiesInTypeAssertions01.js create mode 100644 tests/baselines/reference/duplicatePropertiesInTypeAssertions01.symbols create mode 100644 tests/baselines/reference/duplicatePropertiesInTypeAssertions01.types create mode 100644 tests/baselines/reference/duplicatePropertiesInTypeAssertions02.js create mode 100644 tests/baselines/reference/duplicatePropertiesInTypeAssertions02.symbols create mode 100644 tests/baselines/reference/duplicatePropertiesInTypeAssertions02.types diff --git a/tests/baselines/reference/duplicatePropertiesInTypeAssertions01.js b/tests/baselines/reference/duplicatePropertiesInTypeAssertions01.js new file mode 100644 index 00000000000..03f46ad110a --- /dev/null +++ b/tests/baselines/reference/duplicatePropertiesInTypeAssertions01.js @@ -0,0 +1,12 @@ +//// [duplicatePropertiesInTypeAssertions01.ts] + +let x = <{a: number; a: number}>{}; + +//// [duplicatePropertiesInTypeAssertions01.js] +var x = {}; + + +//// [duplicatePropertiesInTypeAssertions01.d.ts] +declare let x: { + a: number; +}; diff --git a/tests/baselines/reference/duplicatePropertiesInTypeAssertions01.symbols b/tests/baselines/reference/duplicatePropertiesInTypeAssertions01.symbols new file mode 100644 index 00000000000..e6320bc3f8e --- /dev/null +++ b/tests/baselines/reference/duplicatePropertiesInTypeAssertions01.symbols @@ -0,0 +1,7 @@ +=== tests/cases/conformance/expressions/typeAssertions/duplicatePropertiesInTypeAssertions01.ts === + +let x = <{a: number; a: number}>{}; +>x : Symbol(x, Decl(duplicatePropertiesInTypeAssertions01.ts, 1, 3)) +>a : Symbol(a, Decl(duplicatePropertiesInTypeAssertions01.ts, 1, 10), Decl(duplicatePropertiesInTypeAssertions01.ts, 1, 20)) +>a : Symbol(a, Decl(duplicatePropertiesInTypeAssertions01.ts, 1, 10), Decl(duplicatePropertiesInTypeAssertions01.ts, 1, 20)) + diff --git a/tests/baselines/reference/duplicatePropertiesInTypeAssertions01.types b/tests/baselines/reference/duplicatePropertiesInTypeAssertions01.types new file mode 100644 index 00000000000..ac185f8e265 --- /dev/null +++ b/tests/baselines/reference/duplicatePropertiesInTypeAssertions01.types @@ -0,0 +1,9 @@ +=== tests/cases/conformance/expressions/typeAssertions/duplicatePropertiesInTypeAssertions01.ts === + +let x = <{a: number; a: number}>{}; +>x : { a: number; } +><{a: number; a: number}>{} : { a: number; } +>a : number +>a : number +>{} : {} + diff --git a/tests/baselines/reference/duplicatePropertiesInTypeAssertions02.js b/tests/baselines/reference/duplicatePropertiesInTypeAssertions02.js new file mode 100644 index 00000000000..8aa4449595a --- /dev/null +++ b/tests/baselines/reference/duplicatePropertiesInTypeAssertions02.js @@ -0,0 +1,12 @@ +//// [duplicatePropertiesInTypeAssertions02.ts] + +let x = {} as {a: number; a: number}; + +//// [duplicatePropertiesInTypeAssertions02.js] +var x = {}; + + +//// [duplicatePropertiesInTypeAssertions02.d.ts] +declare let x: { + a: number; +}; diff --git a/tests/baselines/reference/duplicatePropertiesInTypeAssertions02.symbols b/tests/baselines/reference/duplicatePropertiesInTypeAssertions02.symbols new file mode 100644 index 00000000000..152307cc669 --- /dev/null +++ b/tests/baselines/reference/duplicatePropertiesInTypeAssertions02.symbols @@ -0,0 +1,7 @@ +=== tests/cases/conformance/expressions/typeAssertions/duplicatePropertiesInTypeAssertions02.ts === + +let x = {} as {a: number; a: number}; +>x : Symbol(x, Decl(duplicatePropertiesInTypeAssertions02.ts, 1, 3)) +>a : Symbol(a, Decl(duplicatePropertiesInTypeAssertions02.ts, 1, 15), Decl(duplicatePropertiesInTypeAssertions02.ts, 1, 25)) +>a : Symbol(a, Decl(duplicatePropertiesInTypeAssertions02.ts, 1, 15), Decl(duplicatePropertiesInTypeAssertions02.ts, 1, 25)) + diff --git a/tests/baselines/reference/duplicatePropertiesInTypeAssertions02.types b/tests/baselines/reference/duplicatePropertiesInTypeAssertions02.types new file mode 100644 index 00000000000..3e3f8cd7e59 --- /dev/null +++ b/tests/baselines/reference/duplicatePropertiesInTypeAssertions02.types @@ -0,0 +1,9 @@ +=== tests/cases/conformance/expressions/typeAssertions/duplicatePropertiesInTypeAssertions02.ts === + +let x = {} as {a: number; a: number}; +>x : { a: number; } +>{} as {a: number; a: number} : { a: number; } +>{} : {} +>a : number +>a : number + From 52a96ac8192385fe49749a6bab7812de6fcc6ece Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Tue, 14 Jun 2016 19:13:32 -0700 Subject: [PATCH 29/56] Always check type assertion types. --- src/compiler/checker.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 51ef0a6058c..e343762ade0 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -5027,7 +5027,6 @@ namespace ts { function getTypeFromTupleTypeNode(node: TupleTypeNode): Type { const links = getNodeLinks(node); if (!links.resolvedType) { - checkTupleType(node); links.resolvedType = createTupleType(map(node.elementTypes, getTypeFromTypeNode)); } return links.resolvedType; @@ -11521,7 +11520,10 @@ namespace ts { function checkAssertion(node: AssertionExpression) { const exprType = getRegularTypeOfObjectLiteral(checkExpression(node.expression)); + + checkSourceElement(node.type); const targetType = getTypeFromTypeNode(node.type); + if (produceDiagnostics && targetType !== unknownType) { const widenedType = getWidenedType(exprType); if (!isTypeComparableTo(targetType, widenedType)) { From da3333e967a39aba6fffc7325f0a790f3ada1af1 Mon Sep 17 00:00:00 2001 From: Mohamed Hegazy Date: Tue, 14 Jun 2016 19:23:04 -0700 Subject: [PATCH 30/56] Clear out unused compiler options when transpiling --- src/services/services.ts | 10 +- tests/cases/unittests/transpile.ts | 225 +++++++++++++++++++++++++++-- 2 files changed, 220 insertions(+), 15 deletions(-) diff --git a/src/services/services.ts b/src/services/services.ts index c20f95a56f2..19167a5f6e0 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -2001,9 +2001,17 @@ namespace ts { // so pass --noLib to avoid reporting a file not found error. options.noLib = true; - // Clear out the lib and types option as well + // Clear out other settings that would not be participate in transpiling this module options.lib = undefined; options.types = undefined; + options.noEmit = undefined; + options.noEmitOnError = undefined; + options.paths = undefined; + options.rootDirs = undefined; + options.declaration = undefined; + options.declarationDir = undefined; + options.out = undefined; + options.outFile = undefined; // We are not doing a full typecheck, we are not resolving the whole context, // so pass --noResolve to avoid reporting missing file errors. diff --git a/tests/cases/unittests/transpile.ts b/tests/cases/unittests/transpile.ts index cac44994420..c8025bfc0a2 100644 --- a/tests/cases/unittests/transpile.ts +++ b/tests/cases/unittests/transpile.ts @@ -1,5 +1,9 @@ /// +interface ObjectConstructor { + assign(target: T, source: U): T & U; +} + namespace ts { describe("Transpile", () => { @@ -21,6 +25,9 @@ namespace ts { assert.equal(expectedDiagnosticTexts[i], diagnostics[i] && diagnostics[i].messageText); } }; + if (diagnostics.length !== n && diagnostics.length) { + console.log(JSON.stringify(diagnostics, undefined, 2)); + } assert.equal(diagnostics.length, n, "Resuting diagnostics count does not match expected"); } @@ -89,6 +96,19 @@ namespace ts { } + function testCompilerOption(options: CompilerOptions, input?: string, output?: string): void { + input = input || "x = 0;"; + output = output || `"use strict";\r\nx = 0;\r\n`; + test(input, { + expectedOutput: output, + options: { + compilerOptions: Object.assign({ module: ModuleKind.CommonJS, newLine: NewLineKind.CarriageReturnLineFeed }, options), + fileName: "input.js", + reportDiagnostics: true + } + }); + } + it("Generates no diagnostics with valid inputs", () => { // No errors test(`var x = 0;`, { options: { compilerOptions: { module: ModuleKind.CommonJS } } }); @@ -304,21 +324,198 @@ var x = 0;`, test("var x", { expectedOutput: `"use strict";\r\nvar x;\r\n`, options: { fileName: "http://somewhere/directory//directory2/file.ts" } }); }); - it("Support options with lib values", () => { - const input = "const a = 10;"; - const output = `"use strict";\r\nvar a = 10;\r\n`; - test(input, { - expectedOutput: output, - options: { compilerOptions: { lib: ["es6", "dom"], module: ModuleKind.CommonJS }, fileName: "input.js", reportDiagnostics: true } - }); - }); + describe("Works with all compiler options", () => { - it("Support options with types values", () => { - const input = "const a = 10;"; - const output = `"use strict";\r\nvar a = 10;\r\n`; - test(input, { - expectedOutput: output, - options: { compilerOptions: { types: ["jquery", "typescript"], module: ModuleKind.CommonJS }, fileName: "input.js", reportDiagnostics: true } + it("Supports setting 'allowJs'", () => { + testCompilerOption({ allowJs: true }); + }); + + it("Supports setting 'allowSyntheticDefaultImports'", () => { + testCompilerOption({ allowSyntheticDefaultImports: true }); + }); + + it("Supports setting 'allowUnreachableCode'", () => { + testCompilerOption({ allowUnreachableCode: true }); + }); + + it("Supports setting 'allowUnusedLabels'", () => { + testCompilerOption({ allowUnusedLabels: true }); + }); + + it("Supports setting 'baseUrl'", () => { + testCompilerOption({ baseUrl: "./folder/baseUrl" }); + }); + + it("Supports setting 'charset'", () => { + testCompilerOption({ charset: "en-us" }); + }); + + it("Supports setting 'declaration'", () => { + testCompilerOption({ declaration: true }); + }); + + it("Supports setting 'declarationDir'", () => { + testCompilerOption({ declarationDir: "out/declarations" }); + }); + + it("Supports setting 'emitBOM'", () => { + testCompilerOption({ emitBOM: true }); + }); + + it("Supports setting 'emitDecoratorMetadata'", () => { + testCompilerOption({ emitDecoratorMetadata: true, experimentalDecorators: true }); + }); + + it("Supports setting 'experimentalDecorators'", () => { + testCompilerOption({ experimentalDecorators: true }); + }); + + it("Supports setting 'forceConsistentCasingInFileNames'", () => { + testCompilerOption({ forceConsistentCasingInFileNames: true }); + }); + + it("Supports setting 'isolatedModules'", () => { + testCompilerOption({ isolatedModules: true }); + }); + + it("Supports setting 'jsx'", () => { + testCompilerOption({ jsx: 1 }); + }); + + it("Supports setting 'lib'", () => { + testCompilerOption({ lib: ["es2015", "dom"] }); + }); + + it("Supports setting 'locale'", () => { + testCompilerOption({ locale: "en-us" }); + }); + + it("Supports setting 'module'", () => { + testCompilerOption({ module: 1 }); + }); + + it("Supports setting 'moduleResolution'", () => { + testCompilerOption({ moduleResolution: 2 }); + }); + + it("Supports setting 'newLine'", () => { + testCompilerOption({ newLine: 0 }); + }); + + it("Supports setting 'noEmit'", () => { + testCompilerOption({ noEmit: true }); + }); + + it("Supports setting 'noEmitHelpers'", () => { + testCompilerOption({ noEmitHelpers: true }); + }); + + it("Supports setting 'noEmitOnError'", () => { + testCompilerOption({ noEmitOnError: true }); + }); + + it("Supports setting 'noErrorTruncation'", () => { + testCompilerOption({ noErrorTruncation: true }); + }); + + it("Supports setting 'noFallthroughCasesInSwitch'", () => { + testCompilerOption({ noFallthroughCasesInSwitch: true }); + }); + + it("Supports setting 'noImplicitAny'", () => { + testCompilerOption({ noImplicitAny: true }); + }); + + it("Supports setting 'noImplicitReturns'", () => { + testCompilerOption({ noImplicitReturns: true }); + }); + + it("Supports setting 'noImplicitThis'", () => { + testCompilerOption({ noImplicitThis: true }); + }); + + it("Supports setting 'noImplicitUseStrict'", () => { + testCompilerOption({ noImplicitUseStrict: true }, "x;", "x;\r\n"); + }); + + it("Supports setting 'noLib'", () => { + testCompilerOption({ noLib: true }); + }); + + it("Supports setting 'noResolve'", () => { + testCompilerOption({ noResolve: true }); + }); + + it("Supports setting 'out'", () => { + testCompilerOption({ out: "./out" }); + }); + + it("Supports setting 'outDir'", () => { + testCompilerOption({ outDir: "./outDir" }); + }); + + it("Supports setting 'outFile'", () => { + testCompilerOption({ outFile: "./outFile" }); + }); + + it("Supports setting 'paths'", () => { + testCompilerOption({ paths: { "*": ["./generated*"] } }); + }); + + it("Supports setting 'preserveConstEnums'", () => { + testCompilerOption({ preserveConstEnums: true }); + }); + + it("Supports setting 'reactNamespace'", () => { + testCompilerOption({ reactNamespace: "react" }); + }); + + it("Supports setting 'removeComments'", () => { + testCompilerOption({ removeComments: true }); + }); + + it("Supports setting 'rootDir'", () => { + testCompilerOption({ rootDir: "./rootDir" }); + }); + + it("Supports setting 'rootDirs'", () => { + testCompilerOption({ rootDirs: ["./a", "./b"] }); + }); + + it("Supports setting 'skipLibCheck'", () => { + testCompilerOption({ skipLibCheck: true }); + }); + + it("Supports setting 'skipDefaultLibCheck'", () => { + testCompilerOption({ skipDefaultLibCheck: true }); + }); + + it("Supports setting 'strictNullChecks'", () => { + testCompilerOption({ strictNullChecks: true }); + }); + + it("Supports setting 'stripInternal'", () => { + testCompilerOption({ stripInternal: true }); + }); + + it("Supports setting 'suppressExcessPropertyErrors'", () => { + testCompilerOption({ suppressExcessPropertyErrors: true }); + }); + + it("Supports setting 'suppressImplicitAnyIndexErrors'", () => { + testCompilerOption({ suppressImplicitAnyIndexErrors: true }); + }); + + it("Supports setting 'target'", () => { + testCompilerOption({ target: 2 }); + }); + + it("Supports setting 'types'", () => { + testCompilerOption({ types: ["jquery", "jasmine"] }); + }); + + it("Supports setting 'typeRoots'", () => { + testCompilerOption({ typeRoots: ["./folder"] }); }); }); From 74a784c8bef3a91864fcfe1c3203b5bdce0ef80c Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Tue, 14 Jun 2016 22:38:35 -0700 Subject: [PATCH 31/56] Accepted baselines. --- .../defaultValueInFunctionTypes.errors.txt | 7 +++++-- ...duplicatePropertiesInTypeAssertions01.errors.txt | 11 +++++++++++ .../duplicatePropertiesInTypeAssertions01.symbols | 7 ------- .../duplicatePropertiesInTypeAssertions01.types | 9 --------- ...duplicatePropertiesInTypeAssertions02.errors.txt | 11 +++++++++++ .../duplicatePropertiesInTypeAssertions02.symbols | 7 ------- .../duplicatePropertiesInTypeAssertions02.types | 9 --------- .../reference/emptyTuplesTypeAssertion01.symbols | 9 --------- .../reference/emptyTuplesTypeAssertion01.types | 13 ------------- .../reference/emptyTuplesTypeAssertion02.symbols | 9 --------- .../reference/emptyTuplesTypeAssertion02.types | 13 ------------- 11 files changed, 27 insertions(+), 78 deletions(-) create mode 100644 tests/baselines/reference/duplicatePropertiesInTypeAssertions01.errors.txt delete mode 100644 tests/baselines/reference/duplicatePropertiesInTypeAssertions01.symbols delete mode 100644 tests/baselines/reference/duplicatePropertiesInTypeAssertions01.types create mode 100644 tests/baselines/reference/duplicatePropertiesInTypeAssertions02.errors.txt delete mode 100644 tests/baselines/reference/duplicatePropertiesInTypeAssertions02.symbols delete mode 100644 tests/baselines/reference/duplicatePropertiesInTypeAssertions02.types delete mode 100644 tests/baselines/reference/emptyTuplesTypeAssertion01.symbols delete mode 100644 tests/baselines/reference/emptyTuplesTypeAssertion01.types delete mode 100644 tests/baselines/reference/emptyTuplesTypeAssertion02.symbols delete mode 100644 tests/baselines/reference/emptyTuplesTypeAssertion02.types diff --git a/tests/baselines/reference/defaultValueInFunctionTypes.errors.txt b/tests/baselines/reference/defaultValueInFunctionTypes.errors.txt index 01a807b3e7d..5544b9caae0 100644 --- a/tests/baselines/reference/defaultValueInFunctionTypes.errors.txt +++ b/tests/baselines/reference/defaultValueInFunctionTypes.errors.txt @@ -1,8 +1,11 @@ tests/cases/compiler/defaultValueInFunctionTypes.ts(1,9): error TS2371: A parameter initializer is only allowed in a function or constructor implementation. +tests/cases/compiler/defaultValueInFunctionTypes.ts(2,11): error TS2371: A parameter initializer is only allowed in a function or constructor implementation. -==== tests/cases/compiler/defaultValueInFunctionTypes.ts (1 errors) ==== +==== tests/cases/compiler/defaultValueInFunctionTypes.ts (2 errors) ==== var x: (a: number = 1) => number; ~~~~~~~~~~~~~ !!! error TS2371: A parameter initializer is only allowed in a function or constructor implementation. - var y = <(a : string = "") => any>(undefined) \ No newline at end of file + var y = <(a : string = "") => any>(undefined) + ~~~~~~~~~~~~~~~ +!!! error TS2371: A parameter initializer is only allowed in a function or constructor implementation. \ No newline at end of file diff --git a/tests/baselines/reference/duplicatePropertiesInTypeAssertions01.errors.txt b/tests/baselines/reference/duplicatePropertiesInTypeAssertions01.errors.txt new file mode 100644 index 00000000000..80b108bbd66 --- /dev/null +++ b/tests/baselines/reference/duplicatePropertiesInTypeAssertions01.errors.txt @@ -0,0 +1,11 @@ +tests/cases/conformance/expressions/typeAssertions/duplicatePropertiesInTypeAssertions01.ts(2,11): error TS2300: Duplicate identifier 'a'. +tests/cases/conformance/expressions/typeAssertions/duplicatePropertiesInTypeAssertions01.ts(2,22): error TS2300: Duplicate identifier 'a'. + + +==== tests/cases/conformance/expressions/typeAssertions/duplicatePropertiesInTypeAssertions01.ts (2 errors) ==== + + let x = <{a: number; a: number}>{}; + ~ +!!! error TS2300: Duplicate identifier 'a'. + ~ +!!! error TS2300: Duplicate identifier 'a'. \ No newline at end of file diff --git a/tests/baselines/reference/duplicatePropertiesInTypeAssertions01.symbols b/tests/baselines/reference/duplicatePropertiesInTypeAssertions01.symbols deleted file mode 100644 index e6320bc3f8e..00000000000 --- a/tests/baselines/reference/duplicatePropertiesInTypeAssertions01.symbols +++ /dev/null @@ -1,7 +0,0 @@ -=== tests/cases/conformance/expressions/typeAssertions/duplicatePropertiesInTypeAssertions01.ts === - -let x = <{a: number; a: number}>{}; ->x : Symbol(x, Decl(duplicatePropertiesInTypeAssertions01.ts, 1, 3)) ->a : Symbol(a, Decl(duplicatePropertiesInTypeAssertions01.ts, 1, 10), Decl(duplicatePropertiesInTypeAssertions01.ts, 1, 20)) ->a : Symbol(a, Decl(duplicatePropertiesInTypeAssertions01.ts, 1, 10), Decl(duplicatePropertiesInTypeAssertions01.ts, 1, 20)) - diff --git a/tests/baselines/reference/duplicatePropertiesInTypeAssertions01.types b/tests/baselines/reference/duplicatePropertiesInTypeAssertions01.types deleted file mode 100644 index ac185f8e265..00000000000 --- a/tests/baselines/reference/duplicatePropertiesInTypeAssertions01.types +++ /dev/null @@ -1,9 +0,0 @@ -=== tests/cases/conformance/expressions/typeAssertions/duplicatePropertiesInTypeAssertions01.ts === - -let x = <{a: number; a: number}>{}; ->x : { a: number; } -><{a: number; a: number}>{} : { a: number; } ->a : number ->a : number ->{} : {} - diff --git a/tests/baselines/reference/duplicatePropertiesInTypeAssertions02.errors.txt b/tests/baselines/reference/duplicatePropertiesInTypeAssertions02.errors.txt new file mode 100644 index 00000000000..eeb9ffa9302 --- /dev/null +++ b/tests/baselines/reference/duplicatePropertiesInTypeAssertions02.errors.txt @@ -0,0 +1,11 @@ +tests/cases/conformance/expressions/typeAssertions/duplicatePropertiesInTypeAssertions02.ts(2,16): error TS2300: Duplicate identifier 'a'. +tests/cases/conformance/expressions/typeAssertions/duplicatePropertiesInTypeAssertions02.ts(2,27): error TS2300: Duplicate identifier 'a'. + + +==== tests/cases/conformance/expressions/typeAssertions/duplicatePropertiesInTypeAssertions02.ts (2 errors) ==== + + let x = {} as {a: number; a: number}; + ~ +!!! error TS2300: Duplicate identifier 'a'. + ~ +!!! error TS2300: Duplicate identifier 'a'. \ No newline at end of file diff --git a/tests/baselines/reference/duplicatePropertiesInTypeAssertions02.symbols b/tests/baselines/reference/duplicatePropertiesInTypeAssertions02.symbols deleted file mode 100644 index 152307cc669..00000000000 --- a/tests/baselines/reference/duplicatePropertiesInTypeAssertions02.symbols +++ /dev/null @@ -1,7 +0,0 @@ -=== tests/cases/conformance/expressions/typeAssertions/duplicatePropertiesInTypeAssertions02.ts === - -let x = {} as {a: number; a: number}; ->x : Symbol(x, Decl(duplicatePropertiesInTypeAssertions02.ts, 1, 3)) ->a : Symbol(a, Decl(duplicatePropertiesInTypeAssertions02.ts, 1, 15), Decl(duplicatePropertiesInTypeAssertions02.ts, 1, 25)) ->a : Symbol(a, Decl(duplicatePropertiesInTypeAssertions02.ts, 1, 15), Decl(duplicatePropertiesInTypeAssertions02.ts, 1, 25)) - diff --git a/tests/baselines/reference/duplicatePropertiesInTypeAssertions02.types b/tests/baselines/reference/duplicatePropertiesInTypeAssertions02.types deleted file mode 100644 index 3e3f8cd7e59..00000000000 --- a/tests/baselines/reference/duplicatePropertiesInTypeAssertions02.types +++ /dev/null @@ -1,9 +0,0 @@ -=== tests/cases/conformance/expressions/typeAssertions/duplicatePropertiesInTypeAssertions02.ts === - -let x = {} as {a: number; a: number}; ->x : { a: number; } ->{} as {a: number; a: number} : { a: number; } ->{} : {} ->a : number ->a : number - diff --git a/tests/baselines/reference/emptyTuplesTypeAssertion01.symbols b/tests/baselines/reference/emptyTuplesTypeAssertion01.symbols deleted file mode 100644 index d6f3cd4e9db..00000000000 --- a/tests/baselines/reference/emptyTuplesTypeAssertion01.symbols +++ /dev/null @@ -1,9 +0,0 @@ -=== tests/cases/conformance/types/tuple/emptyTuples/emptyTuplesTypeAssertion01.ts === - -let x = <[]>[]; ->x : Symbol(x, Decl(emptyTuplesTypeAssertion01.ts, 1, 3)) - -let y = x[0]; ->y : Symbol(y, Decl(emptyTuplesTypeAssertion01.ts, 2, 3)) ->x : Symbol(x, Decl(emptyTuplesTypeAssertion01.ts, 1, 3)) - diff --git a/tests/baselines/reference/emptyTuplesTypeAssertion01.types b/tests/baselines/reference/emptyTuplesTypeAssertion01.types deleted file mode 100644 index e0ef78c86e7..00000000000 --- a/tests/baselines/reference/emptyTuplesTypeAssertion01.types +++ /dev/null @@ -1,13 +0,0 @@ -=== tests/cases/conformance/types/tuple/emptyTuples/emptyTuplesTypeAssertion01.ts === - -let x = <[]>[]; ->x : [] -><[]>[] : [] ->[] : undefined[] - -let y = x[0]; ->y : never ->x[0] : never ->x : [] ->0 : number - diff --git a/tests/baselines/reference/emptyTuplesTypeAssertion02.symbols b/tests/baselines/reference/emptyTuplesTypeAssertion02.symbols deleted file mode 100644 index a5eeec95a32..00000000000 --- a/tests/baselines/reference/emptyTuplesTypeAssertion02.symbols +++ /dev/null @@ -1,9 +0,0 @@ -=== tests/cases/conformance/types/tuple/emptyTuples/emptyTuplesTypeAssertion02.ts === - -let x = [] as []; ->x : Symbol(x, Decl(emptyTuplesTypeAssertion02.ts, 1, 3)) - -let y = x[0]; ->y : Symbol(y, Decl(emptyTuplesTypeAssertion02.ts, 2, 3)) ->x : Symbol(x, Decl(emptyTuplesTypeAssertion02.ts, 1, 3)) - diff --git a/tests/baselines/reference/emptyTuplesTypeAssertion02.types b/tests/baselines/reference/emptyTuplesTypeAssertion02.types deleted file mode 100644 index 2dc7e823516..00000000000 --- a/tests/baselines/reference/emptyTuplesTypeAssertion02.types +++ /dev/null @@ -1,13 +0,0 @@ -=== tests/cases/conformance/types/tuple/emptyTuples/emptyTuplesTypeAssertion02.ts === - -let x = [] as []; ->x : [] ->[] as [] : [] ->[] : undefined[] - -let y = x[0]; ->y : never ->x[0] : never ->x : [] ->0 : number - From 386fa3e1f6de1193539ed483362ece1375562292 Mon Sep 17 00:00:00 2001 From: Herrington Darkholme Date: Wed, 15 Jun 2016 20:54:56 +0800 Subject: [PATCH 32/56] improve error message for extending interface --- src/compiler/checker.ts | 22 +++++++++++++++++++++- src/compiler/diagnosticMessages.json | 4 ++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index a2fe0065983..d5f89bee8bf 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -852,7 +852,8 @@ namespace ts { if (!result) { if (nameNotFoundMessage) { - if (!checkAndReportErrorForMissingPrefix(errorLocation, name, nameArg)) { + if (!checkAndReportErrorForMissingPrefix(errorLocation, name, nameArg) && + !checkAndReportErrorForExtendingInterface(errorLocation, name)) { error(errorLocation, nameNotFoundMessage, typeof nameArg === "string" ? nameArg : declarationNameToString(nameArg)); } } @@ -936,6 +937,25 @@ namespace ts { return false; } + + function checkAndReportErrorForExtendingInterface(errorLocation: Node, name: string): boolean { + if (!errorLocation || errorLocation.kind !== SyntaxKind.Identifier || + !errorLocation.parent || !errorLocation.parent.parent || + errorLocation.parent.parent.kind !== SyntaxKind.HeritageClause) { + return false; + } + const heritageClause = errorLocation.parent.parent; + if (heritageClause.token !== SyntaxKind.ExtendsKeyword) { + return false; + } + const enclosingScope = heritageClause.parent.parent.locals; + if (enclosingScope && getSymbol(enclosingScope, name, SymbolFlags.Interface)) { + error(errorLocation, Diagnostics.Cannot_extend_an_interface_0_Did_you_mean_implements, name); + return true; + } + return false; + } + function checkResolvedBlockScopedVariable(result: Symbol, errorLocation: Node): void { Debug.assert((result.flags & SymbolFlags.BlockScopedVariable) !== 0); // Block-scoped variables cannot be used before their definition diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index b78c342481f..30a4e1575e5 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -1935,6 +1935,10 @@ "category": "Error", "code": 2688 }, + "Cannot extend an interface '{0}'. Did you mean 'implements'?": { + "category": "Error", + "code": 2689 + }, "Import declaration '{0}' is using private name '{1}'.": { "category": "Error", "code": 4000 From 85434c59fcaad4919ad4e1545a9842495324c145 Mon Sep 17 00:00:00 2001 From: Herrington Darkholme Date: Wed, 15 Jun 2016 20:55:50 +0800 Subject: [PATCH 33/56] accept baselines --- .../reference/classExtendingClassLikeType.errors.txt | 4 ++-- .../reference/classExtendsEveryObjectType.errors.txt | 4 ++-- .../baselines/reference/classExtendsInterface.errors.txt | 8 ++++---- .../genericTypeReferenceWithoutTypeArgument2.errors.txt | 4 ++-- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/tests/baselines/reference/classExtendingClassLikeType.errors.txt b/tests/baselines/reference/classExtendingClassLikeType.errors.txt index 6f9aef927ea..5edefd78ec5 100644 --- a/tests/baselines/reference/classExtendingClassLikeType.errors.txt +++ b/tests/baselines/reference/classExtendingClassLikeType.errors.txt @@ -1,4 +1,4 @@ -tests/cases/conformance/classes/classDeclarations/classExtendingClassLikeType.ts(7,18): error TS2304: Cannot find name 'Base'. +tests/cases/conformance/classes/classDeclarations/classExtendingClassLikeType.ts(7,18): error TS2689: Cannot extend an interface 'Base'. Did you mean 'implements'? tests/cases/conformance/classes/classDeclarations/classExtendingClassLikeType.ts(45,18): error TS2508: No base constructor has the specified number of type arguments. tests/cases/conformance/classes/classDeclarations/classExtendingClassLikeType.ts(56,18): error TS2510: Base constructors must all have the same return type. @@ -12,7 +12,7 @@ tests/cases/conformance/classes/classDeclarations/classExtendingClassLikeType.ts // Error, no Base constructor function class D0 extends Base { ~~~~ -!!! error TS2304: Cannot find name 'Base'. +!!! error TS2689: Cannot extend an interface 'Base'. Did you mean 'implements'? } interface BaseConstructor { diff --git a/tests/baselines/reference/classExtendsEveryObjectType.errors.txt b/tests/baselines/reference/classExtendsEveryObjectType.errors.txt index 0339dfad13a..e1c5137e3a9 100644 --- a/tests/baselines/reference/classExtendsEveryObjectType.errors.txt +++ b/tests/baselines/reference/classExtendsEveryObjectType.errors.txt @@ -1,4 +1,4 @@ -tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendsEveryObjectType.ts(4,17): error TS2304: Cannot find name 'I'. +tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendsEveryObjectType.ts(4,17): error TS2689: Cannot extend an interface 'I'. Did you mean 'implements'? tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendsEveryObjectType.ts(6,18): error TS2507: Type '{ foo: any; }' is not a constructor function type. tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendsEveryObjectType.ts(6,25): error TS2304: Cannot find name 'string'. tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendsEveryObjectType.ts(6,31): error TS1005: ',' expected. @@ -14,7 +14,7 @@ tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/cla } class C extends I { } // error ~ -!!! error TS2304: Cannot find name 'I'. +!!! error TS2689: Cannot extend an interface 'I'. Did you mean 'implements'? class C2 extends { foo: string; } { } // error ~~~~~~~~~~~~~~~~ diff --git a/tests/baselines/reference/classExtendsInterface.errors.txt b/tests/baselines/reference/classExtendsInterface.errors.txt index 2ecca6cf2cf..3714bbb2323 100644 --- a/tests/baselines/reference/classExtendsInterface.errors.txt +++ b/tests/baselines/reference/classExtendsInterface.errors.txt @@ -1,17 +1,17 @@ -tests/cases/compiler/classExtendsInterface.ts(2,17): error TS2304: Cannot find name 'Comparable'. -tests/cases/compiler/classExtendsInterface.ts(6,21): error TS2304: Cannot find name 'Comparable2'. +tests/cases/compiler/classExtendsInterface.ts(2,17): error TS2689: Cannot extend an interface 'Comparable'. Did you mean 'implements'? +tests/cases/compiler/classExtendsInterface.ts(6,21): error TS2689: Cannot extend an interface 'Comparable2'. Did you mean 'implements'? ==== tests/cases/compiler/classExtendsInterface.ts (2 errors) ==== interface Comparable {} class A extends Comparable {} ~~~~~~~~~~ -!!! error TS2304: Cannot find name 'Comparable'. +!!! error TS2689: Cannot extend an interface 'Comparable'. Did you mean 'implements'? class B implements Comparable {} interface Comparable2 {} class A2 extends Comparable2 {} ~~~~~~~~~~~ -!!! error TS2304: Cannot find name 'Comparable2'. +!!! error TS2689: Cannot extend an interface 'Comparable2'. Did you mean 'implements'? class B2 implements Comparable2 {} \ No newline at end of file diff --git a/tests/baselines/reference/genericTypeReferenceWithoutTypeArgument2.errors.txt b/tests/baselines/reference/genericTypeReferenceWithoutTypeArgument2.errors.txt index e590ad42a7d..b6767a3b938 100644 --- a/tests/baselines/reference/genericTypeReferenceWithoutTypeArgument2.errors.txt +++ b/tests/baselines/reference/genericTypeReferenceWithoutTypeArgument2.errors.txt @@ -13,7 +13,7 @@ tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenc tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument2.ts(18,23): error TS2314: Generic type 'I' requires 1 type argument(s). tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument2.ts(18,27): error TS2314: Generic type 'I' requires 1 type argument(s). tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument2.ts(18,38): error TS2314: Generic type 'I' requires 1 type argument(s). -tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument2.ts(20,17): error TS2304: Cannot find name 'I'. +tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument2.ts(20,17): error TS2689: Cannot extend an interface 'I'. Did you mean 'implements'? tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument2.ts(23,21): error TS2314: Generic type 'I' requires 1 type argument(s). tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument2.ts(29,18): error TS2304: Cannot find name 'M'. tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument2.ts(30,24): error TS2314: Generic type 'E' requires 1 type argument(s). @@ -76,7 +76,7 @@ tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenc class D extends I { ~ -!!! error TS2304: Cannot find name 'I'. +!!! error TS2689: Cannot extend an interface 'I'. Did you mean 'implements'? } interface U extends I {} From 8a025fcff422e448092997f280dd1236a37c0adb Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Wed, 15 Jun 2016 05:52:10 -0700 Subject: [PATCH 34/56] Use helper functions to simplify range tests --- src/harness/fourslash.ts | 115 +++++++++++++----- .../fourslash/ambientShorthandFindAllRefs.ts | 11 +- .../findAllRefsForComputedProperties.ts | 10 +- .../findAllRefsForComputedProperties2.ts | 10 +- .../findAllRefsForDefaultExport01.ts | 10 +- .../findAllRefsForDefaultExport02.ts | 10 +- .../findAllRefsForDefaultExport03.ts | 10 +- .../findAllRefsForDefaultExport04.ts | 7 +- .../findAllRefsForDefaultExport05.ts | 7 +- .../findAllRefsForDefaultExport06.ts | 7 +- .../findAllRefsForFunctionExpression01.ts | 11 +- .../findAllRefsForObjectLiteralProperties.ts | 11 +- .../findAllRefsForStringLiteralTypes.ts | 10 +- ...findAllRefsForVariableInExtendsClause01.ts | 11 +- ...findAllRefsForVariableInExtendsClause02.ts | 10 +- .../fourslash/findAllRefsInClassExpression.ts | 10 +- .../findAllRefsInheritedProperties1.ts | 19 +-- .../findAllRefsInheritedProperties2.ts | 19 +-- .../findAllRefsInheritedProperties3.ts | 27 ++-- .../findAllRefsInheritedProperties4.ts | 21 +--- .../findAllRefsInheritedProperties5.ts | 21 +--- .../fourslash/findAllRefsInsideTemplates1.ts | 8 +- .../fourslash/findAllRefsInsideTemplates2.ts | 8 +- ...lRefsObjectBindingElementPropertyName01.ts | 10 +- ...lRefsObjectBindingElementPropertyName02.ts | 10 +- ...lRefsObjectBindingElementPropertyName03.ts | 10 +- ...lRefsObjectBindingElementPropertyName04.ts | 10 +- ...lRefsObjectBindingElementPropertyName06.ts | 10 +- ...lRefsObjectBindingElementPropertyName07.ts | 10 +- ...lRefsObjectBindingElementPropertyName09.ts | 10 +- ...lRefsObjectBindingElementPropertyName10.ts | 10 +- .../findAllRefsOnPrivateParameterProperty1.ts | 8 +- ...indAllRefsParameterPropertyDeclaration1.ts | 13 +- ...indAllRefsParameterPropertyDeclaration2.ts | 11 +- ...indAllRefsParameterPropertyDeclaration3.ts | 11 +- ...sPropertyContextuallyTypedByTypeParam01.ts | 10 +- .../findAllRefsWithLeadingUnderscoreNames1.ts | 10 +- .../findAllRefsWithLeadingUnderscoreNames2.ts | 10 +- .../findAllRefsWithLeadingUnderscoreNames3.ts | 10 +- .../findAllRefsWithLeadingUnderscoreNames4.ts | 10 +- .../findAllRefsWithLeadingUnderscoreNames5.ts | 10 +- .../findAllRefsWithLeadingUnderscoreNames6.ts | 10 +- .../findAllRefsWithLeadingUnderscoreNames7.ts | 9 +- .../findAllRefsWithLeadingUnderscoreNames8.ts | 9 +- .../findAllRefsWithLeadingUnderscoreNames9.ts | 9 +- tests/cases/fourslash/fourslash.ts | 20 ++- ...tOccurrencesIsDefinitionOfArrowFunction.ts | 7 +- ...OccurrencesIsDefinitionOfBindingPattern.ts | 7 +- .../getOccurrencesIsDefinitionOfClass.ts | 7 +- ...currencesIsDefinitionOfComputedProperty.ts | 10 +- .../getOccurrencesIsDefinitionOfEnum.ts | 7 +- .../getOccurrencesIsDefinitionOfExport.ts | 7 +- .../getOccurrencesIsDefinitionOfFunction.ts | 7 +- .../getOccurrencesIsDefinitionOfInterface.ts | 7 +- ...rencesIsDefinitionOfInterfaceClassMerge.ts | 7 +- .../getOccurrencesIsDefinitionOfNamespace.ts | 7 +- ...rencesIsDefinitionOfNumberNamedProperty.ts | 7 +- .../getOccurrencesIsDefinitionOfParameter.ts | 7 +- ...rencesIsDefinitionOfStringNamedProperty.ts | 7 +- .../getOccurrencesIsDefinitionOfTypeAlias.ts | 7 +- .../getOccurrencesIsDefinitionOfVariable.ts | 7 +- tests/cases/fourslash/referenceToClass.ts | 27 ++-- ...rencesForStaticsAndMembersWithSameNames.ts | 35 ++---- ...referencesForStringLiteralPropertyNames.ts | 15 +-- ...eferencesForStringLiteralPropertyNames2.ts | 11 +- ...eferencesForStringLiteralPropertyNames3.ts | 12 +- ...eferencesForStringLiteralPropertyNames4.ts | 3 +- .../fourslash/referencesForUnionProperties.ts | 24 ++-- tests/cases/fourslash/referencesInComment.ts | 14 +-- .../renameImportAndExportInDiffFiles.ts | 16 +-- tests/cases/fourslash/server/references01.ts | 11 +- .../server/referencesInConfiguredProject.ts | 10 +- .../shims-pp/getReferencesAtPosition.ts | 9 +- .../shims/getReferencesAtPosition.ts | 9 +- 74 files changed, 268 insertions(+), 659 deletions(-) diff --git a/src/harness/fourslash.ts b/src/harness/fourslash.ts index 9f675e3ffd0..695e19b4667 100644 --- a/src/harness/fourslash.ts +++ b/src/harness/fourslash.ts @@ -730,30 +730,6 @@ namespace FourSlash { } } - public verifyReferencesAtPositionListContains(fileName: string, start: number, end: number, isWriteAccess?: boolean, isDefinition?: boolean) { - const references = this.getReferencesAtCaret(); - - if (!references || references.length === 0) { - this.raiseError("verifyReferencesAtPositionListContains failed - found 0 references, expected at least one."); - } - - for (let i = 0; i < references.length; i++) { - const reference = references[i]; - if (reference && reference.fileName === fileName && reference.textSpan.start === start && ts.textSpanEnd(reference.textSpan) === end) { - if (typeof isWriteAccess !== "undefined" && reference.isWriteAccess !== isWriteAccess) { - this.raiseError(`verifyReferencesAtPositionListContains failed - item isWriteAccess value does not match, actual: ${reference.isWriteAccess}, expected: ${isWriteAccess}.`); - } - if (typeof isDefinition !== "undefined" && reference.isDefinition !== isDefinition) { - this.raiseError(`verifyReferencesAtPositionListContains failed - item isDefinition value does not match, actual: ${reference.isDefinition}, expected: ${isDefinition}.`); - } - return; - } - } - - const missingItem = { fileName, start, end, isWriteAccess, isDefinition }; - this.raiseError(`verifyReferencesAtPositionListContains failed - could not find the item: ${stringify(missingItem)} in the returned list: (${stringify(references)})`); - } - public verifyReferencesCountIs(count: number, localFilesOnly = true) { const references = this.getReferencesAtCaret(); let referencesCount = 0; @@ -777,6 +753,73 @@ namespace FourSlash { } } + public verifyReferencesAre(expectedReferences: Range[]) { + const actualReferences = this.getReferencesAtCaret() || []; + + if (actualReferences.length > expectedReferences.length) { + // Find the unaccounted-for reference. + for (const actual of actualReferences) { + if (!ts.forEach(expectedReferences, r => r.start === actual.textSpan.start)) { + this.raiseError(`A reference ${actual} is unaccounted for.`); + } + } + // Probably will never reach here. + this.raiseError(`There are ${actualReferences.length} references but only ${expectedReferences.length} were expected.`); + } + + for (const reference of expectedReferences) { + const {fileName, start, end} = reference; + if (reference.marker) { + const {isWriteAccess, isDefinition} = reference.marker.data; + this.verifyReferencesWorker(actualReferences, fileName, start, end, isWriteAccess, isDefinition); + } + else { + this.verifyReferencesWorker(actualReferences, fileName, start, end); + } + } + } + + public verifyReferencesOf({fileName, start}: Range, references: Range[]) { + this.openFile(fileName); + this.goToPosition(start); + this.verifyReferencesAre(references); + } + + public verifyRangesReferenceEachOther(ranges?: Range[]) { + ranges = ranges || this.getRanges(); + assert(ranges.length); + for (const range of ranges) { + this.verifyReferencesOf(range, ranges); + } + } + + public verifyReferencesAtPositionListContains(fileName: string, start: number, end: number, isWriteAccess?: boolean, isDefinition?: boolean) { + const references = this.getReferencesAtCaret(); + if (!references || references.length === 0) { + this.raiseError("verifyReferencesAtPositionListContains failed - found 0 references, expected at least one."); + } + this.verifyReferencesWorker(references, fileName, start, end, isWriteAccess, isDefinition); + } + + private verifyReferencesWorker(references: ts.ReferenceEntry[], fileName: string, start: number, end: number, isWriteAccess?: boolean, isDefinition?: boolean) { + for (let i = 0; i < references.length; i++) { + const reference = references[i]; + if (reference && reference.fileName === fileName && reference.textSpan.start === start && ts.textSpanEnd(reference.textSpan) === end) { + if (typeof isWriteAccess !== "undefined" && reference.isWriteAccess !== isWriteAccess) { + this.raiseError(`verifyReferencesAtPositionListContains failed - item isWriteAccess value does not match, actual: ${reference.isWriteAccess}, expected: ${isWriteAccess}.`); + } + if (typeof isDefinition !== "undefined" && reference.isDefinition !== isDefinition) { + this.raiseError(`verifyReferencesAtPositionListContains failed - item isDefinition value does not match, actual: ${reference.isDefinition}, expected: ${isDefinition}.`); + } + return; + } + } + + const missingItem = { fileName, start, end, isWriteAccess, isDefinition }; + this.raiseError(`verifyReferencesAtPositionListContains failed - could not find the item: ${stringify(missingItem)} in the returned list: (${stringify(references)})`); + + } + private getMemberListAtCaret() { return this.languageService.getCompletionsAtPosition(this.activeFile.fileName, this.currentCaretPosition); } @@ -2836,14 +2879,6 @@ namespace FourSlashInterface { this.state.verifyMemberListIsEmpty(this.negative); } - public referencesCountIs(count: number) { - this.state.verifyReferencesCountIs(count, /*localFilesOnly*/ false); - } - - public referencesAtPositionContains(range: FourSlash.Range, isWriteAccess?: boolean, isDefinition?: boolean) { - this.state.verifyReferencesAtPositionListContains(range.fileName, range.start, range.end, isWriteAccess, isDefinition); - } - public signatureHelpPresent() { this.state.verifySignatureHelpPresent(!this.negative); } @@ -2935,6 +2970,22 @@ namespace FourSlashInterface { this.state.verifyGetEmitOutputContentsForCurrentFile(expected); } + public referencesCountIs(count: number) { + this.state.verifyReferencesCountIs(count, /*localFilesOnly*/ false); + } + + public referencesAre(ranges: FourSlash.Range[]) { + this.state.verifyReferencesAre(ranges); + } + + public referencesOf(start: FourSlash.Range, references: FourSlash.Range[]) { + this.state.verifyReferencesOf(start, references); + } + + public rangesReferenceEachOther(ranges?: FourSlash.Range[]) { + this.state.verifyRangesReferenceEachOther(ranges); + } + public currentParameterHelpArgumentNameIs(name: string) { this.state.verifyCurrentParameterHelpName(name); } diff --git a/tests/cases/fourslash/ambientShorthandFindAllRefs.ts b/tests/cases/fourslash/ambientShorthandFindAllRefs.ts index 16fddd1869d..2f0dbf914e7 100644 --- a/tests/cases/fourslash/ambientShorthandFindAllRefs.ts +++ b/tests/cases/fourslash/ambientShorthandFindAllRefs.ts @@ -9,13 +9,4 @@ // @Filename: user2.ts ////import {[|x|]} from "jquery"; -let ranges = test.ranges(); -for (let range of ranges) { - goTo.file(range.fileName); - goTo.position(range.start); - - verify.referencesCountIs(ranges.length); - for (let expectedRange of ranges) { - verify.referencesAtPositionContains(expectedRange); - } -} +verify.rangesReferenceEachOther(); diff --git a/tests/cases/fourslash/findAllRefsForComputedProperties.ts b/tests/cases/fourslash/findAllRefsForComputedProperties.ts index 3ea226b3ce4..065d34c6fc5 100644 --- a/tests/cases/fourslash/findAllRefsForComputedProperties.ts +++ b/tests/cases/fourslash/findAllRefsForComputedProperties.ts @@ -13,12 +13,4 @@ //// ["[|prop1|]"]: function () { }, ////} -let ranges = test.ranges(); -for (let range of ranges) { - goTo.position(range.start); - - verify.referencesCountIs(ranges.length); - for (let expectedReference of ranges) { - verify.referencesAtPositionContains(expectedReference); - } -} \ No newline at end of file +verify.rangesReferenceEachOther(); diff --git a/tests/cases/fourslash/findAllRefsForComputedProperties2.ts b/tests/cases/fourslash/findAllRefsForComputedProperties2.ts index b9f6e9538eb..37a9bd8470e 100644 --- a/tests/cases/fourslash/findAllRefsForComputedProperties2.ts +++ b/tests/cases/fourslash/findAllRefsForComputedProperties2.ts @@ -12,12 +12,4 @@ //// ["[|42|]"]: function () { } ////} -let ranges = test.ranges(); -for (let range of ranges) { - goTo.position(range.start); - - verify.referencesCountIs(ranges.length); - for (let expectedReference of ranges) { - verify.referencesAtPositionContains(expectedReference); - } -} \ No newline at end of file +verify.rangesReferenceEachOther(); diff --git a/tests/cases/fourslash/findAllRefsForDefaultExport01.ts b/tests/cases/fourslash/findAllRefsForDefaultExport01.ts index 2f40af93c02..39079294a3b 100644 --- a/tests/cases/fourslash/findAllRefsForDefaultExport01.ts +++ b/tests/cases/fourslash/findAllRefsForDefaultExport01.ts @@ -7,12 +7,4 @@ //// ////var y = new [|DefaultExportedClass|]; -let ranges = test.ranges() -for (let range of ranges) { - goTo.position(range.start); - - verify.referencesCountIs(ranges.length); - for (let expectedReference of ranges) { - verify.referencesAtPositionContains(expectedReference); - } -} \ No newline at end of file +verify.rangesReferenceEachOther(); diff --git a/tests/cases/fourslash/findAllRefsForDefaultExport02.ts b/tests/cases/fourslash/findAllRefsForDefaultExport02.ts index db773c74e3c..19f31ece9b0 100644 --- a/tests/cases/fourslash/findAllRefsForDefaultExport02.ts +++ b/tests/cases/fourslash/findAllRefsForDefaultExport02.ts @@ -8,12 +8,4 @@ //// ////var y = [|DefaultExportedFunction|](); -let ranges = test.ranges() -for (let range of ranges) { - goTo.position(range.start); - - verify.referencesCountIs(ranges.length); - for (let expectedReference of ranges) { - verify.referencesAtPositionContains(expectedReference); - } -} \ No newline at end of file +verify.rangesReferenceEachOther(); diff --git a/tests/cases/fourslash/findAllRefsForDefaultExport03.ts b/tests/cases/fourslash/findAllRefsForDefaultExport03.ts index f753d17de49..b9bbfa14a64 100644 --- a/tests/cases/fourslash/findAllRefsForDefaultExport03.ts +++ b/tests/cases/fourslash/findAllRefsForDefaultExport03.ts @@ -14,12 +14,4 @@ //// var local = 100; ////} -let ranges = test.ranges() -for (let range of ranges) { - goTo.position(range.start); - - verify.referencesCountIs(ranges.length); - for (let expectedReference of ranges) { - verify.referencesAtPositionContains(expectedReference); - } -} \ No newline at end of file +verify.rangesReferenceEachOther(); diff --git a/tests/cases/fourslash/findAllRefsForDefaultExport04.ts b/tests/cases/fourslash/findAllRefsForDefaultExport04.ts index 45b008b55fe..44b7ee0a06f 100644 --- a/tests/cases/fourslash/findAllRefsForDefaultExport04.ts +++ b/tests/cases/fourslash/findAllRefsForDefaultExport04.ts @@ -20,9 +20,4 @@ // site is included in the references to the namespace. goTo.marker(); -let ranges = test.ranges(); -verify.referencesCountIs(ranges.length); - -for (let expectedReference of ranges) { - verify.referencesAtPositionContains(expectedReference); -} +verify.referencesAre(test.ranges()); diff --git a/tests/cases/fourslash/findAllRefsForDefaultExport05.ts b/tests/cases/fourslash/findAllRefsForDefaultExport05.ts index 6655138da3b..0f06792458d 100644 --- a/tests/cases/fourslash/findAllRefsForDefaultExport05.ts +++ b/tests/cases/fourslash/findAllRefsForDefaultExport05.ts @@ -20,9 +20,4 @@ // and all value-uses of 'f' are included in the references to the function. goTo.marker(); -let ranges = test.ranges(); -verify.referencesCountIs(ranges.length); - -for (let expectedReference of ranges) { - verify.referencesAtPositionContains(expectedReference); -} +verify.referencesAre(test.ranges()); diff --git a/tests/cases/fourslash/findAllRefsForDefaultExport06.ts b/tests/cases/fourslash/findAllRefsForDefaultExport06.ts index 12c187b4b0c..cdd9b4f980c 100644 --- a/tests/cases/fourslash/findAllRefsForDefaultExport06.ts +++ b/tests/cases/fourslash/findAllRefsForDefaultExport06.ts @@ -20,9 +20,4 @@ // and all value-uses of 'f' are included in the references to the function. goTo.marker(); -let ranges = test.ranges(); -verify.referencesCountIs(ranges.length); - -for (let expectedReference of ranges) { - verify.referencesAtPositionContains(expectedReference); -} +verify.referencesAre(test.ranges()); diff --git a/tests/cases/fourslash/findAllRefsForFunctionExpression01.ts b/tests/cases/fourslash/findAllRefsForFunctionExpression01.ts index a312a277ebb..35d5c45e3d9 100644 --- a/tests/cases/fourslash/findAllRefsForFunctionExpression01.ts +++ b/tests/cases/fourslash/findAllRefsForFunctionExpression01.ts @@ -9,13 +9,4 @@ /////// ////foo(); - -let ranges = test.ranges() -for (let range of ranges) { - goTo.position(range.start); - - verify.referencesCountIs(ranges.length); - for (let expectedReference of ranges) { - verify.referencesAtPositionContains(expectedReference); - } -} \ No newline at end of file +verify.rangesReferenceEachOther(); diff --git a/tests/cases/fourslash/findAllRefsForObjectLiteralProperties.ts b/tests/cases/fourslash/findAllRefsForObjectLiteralProperties.ts index cb5e702dc90..cea4db81ea1 100644 --- a/tests/cases/fourslash/findAllRefsForObjectLiteralProperties.ts +++ b/tests/cases/fourslash/findAllRefsForObjectLiteralProperties.ts @@ -8,13 +8,4 @@ //// ////let {[|property|]: pVar} = x; - -let ranges = test.ranges(); -for (let range of ranges) { - goTo.position(range.start); - - verify.referencesCountIs(ranges.length); - for (let expectedReference of ranges) { - verify.referencesAtPositionContains(expectedReference); - } -} \ No newline at end of file +verify.rangesReferenceEachOther(); diff --git a/tests/cases/fourslash/findAllRefsForStringLiteralTypes.ts b/tests/cases/fourslash/findAllRefsForStringLiteralTypes.ts index dcf7240312c..45e981d8a84 100644 --- a/tests/cases/fourslash/findAllRefsForStringLiteralTypes.ts +++ b/tests/cases/fourslash/findAllRefsForStringLiteralTypes.ts @@ -3,12 +3,4 @@ ////type Options = "[|option 1|]" | "option 2"; ////let myOption: Options = "[|option 1|]"; -let ranges = test.ranges(); -for (let range of ranges) { - goTo.position(range.start); - - verify.referencesCountIs(ranges.length); - for (let expectedReference of ranges) { - verify.referencesAtPositionContains(expectedReference); - } -} \ No newline at end of file +verify.rangesReferenceEachOther(); diff --git a/tests/cases/fourslash/findAllRefsForVariableInExtendsClause01.ts b/tests/cases/fourslash/findAllRefsForVariableInExtendsClause01.ts index 1ec144d7e82..dd8fb1026c4 100644 --- a/tests/cases/fourslash/findAllRefsForVariableInExtendsClause01.ts +++ b/tests/cases/fourslash/findAllRefsForVariableInExtendsClause01.ts @@ -1,15 +1,6 @@ /// - ////var [|Base|] = class { }; ////class C extends [|Base|] { } -let ranges = test.ranges(); -for (let range of ranges) { - goTo.position(range.start); - - verify.referencesCountIs(ranges.length); - for (let expectedReference of ranges) { - verify.referencesAtPositionContains(expectedReference); - } -} \ No newline at end of file +verify.rangesReferenceEachOther(); diff --git a/tests/cases/fourslash/findAllRefsForVariableInExtendsClause02.ts b/tests/cases/fourslash/findAllRefsForVariableInExtendsClause02.ts index 0f06a3c4202..3fc41358dfd 100644 --- a/tests/cases/fourslash/findAllRefsForVariableInExtendsClause02.ts +++ b/tests/cases/fourslash/findAllRefsForVariableInExtendsClause02.ts @@ -6,12 +6,4 @@ //// interface I extends [|Base|] { } ////} -let ranges = test.ranges(); -for (let range of ranges) { - goTo.position(range.start); - - verify.referencesCountIs(ranges.length); - for (let expectedReference of ranges) { - verify.referencesAtPositionContains(expectedReference); - } -} \ No newline at end of file +verify.rangesReferenceEachOther(); diff --git a/tests/cases/fourslash/findAllRefsInClassExpression.ts b/tests/cases/fourslash/findAllRefsInClassExpression.ts index 951acdd336c..f874bcbcb5c 100644 --- a/tests/cases/fourslash/findAllRefsInClassExpression.ts +++ b/tests/cases/fourslash/findAllRefsInClassExpression.ts @@ -5,12 +5,4 @@ //// [|boom|](){} ////} -let ranges = test.ranges() -for (let range of ranges) { - goTo.position(range.start); - - verify.referencesCountIs(ranges.length); - for (let expectedReference of ranges) { - verify.referencesAtPositionContains(expectedReference); - } -} \ No newline at end of file +verify.rangesReferenceEachOther(); diff --git a/tests/cases/fourslash/findAllRefsInheritedProperties1.ts b/tests/cases/fourslash/findAllRefsInheritedProperties1.ts index b2755923d37..358ba3f249d 100644 --- a/tests/cases/fourslash/findAllRefsInheritedProperties1.ts +++ b/tests/cases/fourslash/findAllRefsInheritedProperties1.ts @@ -9,17 +9,8 @@ //// v.[|doStuff|](); //// v.[|propName|]; -function verifyReferences(query: FourSlashInterface.Range, references: FourSlashInterface.Range[]) { - goTo.position(query.start); - for (const ref of references) { - verify.referencesAtPositionContains(ref); - } -} - -const ranges = test.ranges(); -verify.assertHasRanges(ranges); -const [r0, r1, r2, r3] = ranges; -verifyReferences(r0, [r0, r2]); -verifyReferences(r1, [r1, r3]); -verifyReferences(r2, [r0, r2]); -verifyReferences(r3, [r1, r3]); \ No newline at end of file +const [r0, r1, r2, r3] = test.ranges(); +verify.referencesOf(r0, [r0, r2]); +verify.referencesOf(r1, [r1, r3]); +verify.referencesOf(r2, [r0, r2]); +verify.referencesOf(r3, [r1, r3]); diff --git a/tests/cases/fourslash/findAllRefsInheritedProperties2.ts b/tests/cases/fourslash/findAllRefsInheritedProperties2.ts index 1ab92c251da..9fe36fbb48e 100644 --- a/tests/cases/fourslash/findAllRefsInheritedProperties2.ts +++ b/tests/cases/fourslash/findAllRefsInheritedProperties2.ts @@ -9,17 +9,8 @@ //// v.[|doStuff|](); // r2 //// v.[|propName|]; // r3 -function verifyReferences(query: FourSlashInterface.Range, references: FourSlashInterface.Range[]) { - goTo.position(query.start); - for (const ref of references) { - verify.referencesAtPositionContains(ref); - } -} - -const ranges = test.ranges(); -verify.assertHasRanges(ranges); -const [r0, r1, r2, r3] = ranges; -verifyReferences(r0, [r0, r2]); -verifyReferences(r1, [r1, r3]); -verifyReferences(r2, [r0, r2]); -verifyReferences(r3, [r1, r3]); \ No newline at end of file +const [r0, r1, r2, r3] = test.ranges(); +verify.referencesOf(r0, [r0, r2]); +verify.referencesOf(r1, [r1, r3]); +verify.referencesOf(r2, [r0, r2]); +verify.referencesOf(r3, [r1, r3]); diff --git a/tests/cases/fourslash/findAllRefsInheritedProperties3.ts b/tests/cases/fourslash/findAllRefsInheritedProperties3.ts index 9a46b08f357..772fb78a2e8 100644 --- a/tests/cases/fourslash/findAllRefsInheritedProperties3.ts +++ b/tests/cases/fourslash/findAllRefsInheritedProperties3.ts @@ -17,21 +17,12 @@ //// v.[|propName|]; // r6 //// v.[|doStuff|](); // r7 -function verifyReferences(query: FourSlashInterface.Range, references: FourSlashInterface.Range[]) { - goTo.position(query.start); - for (const ref of references) { - verify.referencesAtPositionContains(ref); - } -} - -const ranges = test.ranges(); -verify.assertHasRanges(ranges); -const [r0, r1, r2, r3, r4, r5, r6, r7] = ranges; -verifyReferences(r0, [r0]); -verifyReferences(r1, [r1, r5, r6]); -verifyReferences(r2, [r2, r4, r7]); -verifyReferences(r3, [r3, r5, r6]); -verifyReferences(r4, [r2, r4, r7]); -verifyReferences(r5, [r1, r3, r5, r6]); -verifyReferences(r6, [r1, r3, r5, r6]); -verifyReferences(r7, [r2, r4, r7]); \ No newline at end of file +const [r0, r1, r2, r3, r4, r5, r6, r7] = test.ranges(); +verify.referencesOf(r0, [r0, r4, r7]); +verify.referencesOf(r1, [r1, r5, r6]); +verify.referencesOf(r2, [r2, r4, r7]); +verify.referencesOf(r3, [r3, r5, r6]); +verify.referencesOf(r4, [r0, r2, r4, r7]); +verify.referencesOf(r5, [r1, r3, r5, r6]); +verify.referencesOf(r6, [r1, r3, r5, r6]); +verify.referencesOf(r7, [r0, r2, r4, r7]); diff --git a/tests/cases/fourslash/findAllRefsInheritedProperties4.ts b/tests/cases/fourslash/findAllRefsInheritedProperties4.ts index bcd41331f73..1ecb85bfae1 100644 --- a/tests/cases/fourslash/findAllRefsInheritedProperties4.ts +++ b/tests/cases/fourslash/findAllRefsInheritedProperties4.ts @@ -13,18 +13,9 @@ //// d.[|prop0|]; // r3 //// d.[|prop1|]; // r4 -function verifyReferences(query: FourSlashInterface.Range, references: FourSlashInterface.Range[]) { - goTo.position(query.start); - for (const ref of references) { - verify.referencesAtPositionContains(ref); - } -} - -const ranges = test.ranges(); -verify.assertHasRanges(ranges); -const [r0, r1, r2, r3, r4] = ranges; -verifyReferences(r0, [r0, r2, r3]); -verifyReferences(r1, [r1]); -verifyReferences(r2, [r0, r2, r3]); -verifyReferences(r3, [r0, r2, r3]); -verifyReferences(r4, []); \ No newline at end of file +const [r0, r1, r2, r3, r4] = test.ranges(); +verify.referencesOf(r0, [r0, r2, r3]); +verify.referencesOf(r1, [r1]); +verify.referencesOf(r2, [r0, r2, r3]); +verify.referencesOf(r3, [r0, r2, r3]); +verify.referencesOf(r4, []); diff --git a/tests/cases/fourslash/findAllRefsInheritedProperties5.ts b/tests/cases/fourslash/findAllRefsInheritedProperties5.ts index d4e02a36b09..6d6dbb392bf 100644 --- a/tests/cases/fourslash/findAllRefsInheritedProperties5.ts +++ b/tests/cases/fourslash/findAllRefsInheritedProperties5.ts @@ -13,18 +13,9 @@ //// d.[|prop0|]; // r3 //// d.[|prop1|]; // r4 -function verifyReferences(query: FourSlashInterface.Range, references: FourSlashInterface.Range[]) { - goTo.position(query.start); - for (const ref of references) { - verify.referencesAtPositionContains(ref); - } -} - -const ranges = test.ranges(); -verify.assertHasRanges(ranges); -const [r0, r1, r2, r3, r4] = ranges; -verifyReferences(r0, [r0]); -verifyReferences(r1, [r1]); -verifyReferences(r2, [r2, r3]); -verifyReferences(r3, [r2, r3]); -verifyReferences(r4, []); +const [r0, r1, r2, r3, r4] = test.ranges(); +verify.referencesOf(r0, [r0]); +verify.referencesOf(r1, [r1]); +verify.referencesOf(r2, [r2, r3]); +verify.referencesOf(r3, [r2, r3]); +verify.referencesOf(r4, []); diff --git a/tests/cases/fourslash/findAllRefsInsideTemplates1.ts b/tests/cases/fourslash/findAllRefsInsideTemplates1.ts index dd4461751de..9ab4c624230 100644 --- a/tests/cases/fourslash/findAllRefsInsideTemplates1.ts +++ b/tests/cases/fourslash/findAllRefsInsideTemplates1.ts @@ -3,10 +3,4 @@ ////var [|x|] = 10; ////var y = `${ [|x|] } ${ [|x|] }` -test.ranges().forEach(targetRange => { - goTo.position(targetRange.start); - - test.ranges().forEach(range => { - verify.referencesAtPositionContains(range); - }); -}); \ No newline at end of file +verify.rangesReferenceEachOther(); diff --git a/tests/cases/fourslash/findAllRefsInsideTemplates2.ts b/tests/cases/fourslash/findAllRefsInsideTemplates2.ts index 7f585234082..63265eb944b 100644 --- a/tests/cases/fourslash/findAllRefsInsideTemplates2.ts +++ b/tests/cases/fourslash/findAllRefsInsideTemplates2.ts @@ -3,10 +3,4 @@ ////function [|f|](...rest: any[]) { } ////[|f|] `${ [|f|] } ${ [|f|] }` -test.ranges().forEach(targetRange => { - goTo.position(targetRange.start); - - test.ranges().forEach(range => { - verify.referencesAtPositionContains(range); - }); -}); \ No newline at end of file +verify.rangesReferenceEachOther(); diff --git a/tests/cases/fourslash/findAllRefsObjectBindingElementPropertyName01.ts b/tests/cases/fourslash/findAllRefsObjectBindingElementPropertyName01.ts index 0d03561515d..895b2168b23 100644 --- a/tests/cases/fourslash/findAllRefsObjectBindingElementPropertyName01.ts +++ b/tests/cases/fourslash/findAllRefsObjectBindingElementPropertyName01.ts @@ -8,12 +8,4 @@ ////var foo: I; ////var { [|property1|]: prop1 } = foo; -let ranges = test.ranges(); -for (let range of ranges) { - goTo.position(range.start); - - verify.referencesCountIs(ranges.length); - for (let expectedRange of ranges) { - verify.referencesAtPositionContains(expectedRange); - } -} \ No newline at end of file +verify.rangesReferenceEachOther(); diff --git a/tests/cases/fourslash/findAllRefsObjectBindingElementPropertyName02.ts b/tests/cases/fourslash/findAllRefsObjectBindingElementPropertyName02.ts index 86514051557..67a31f33d1e 100644 --- a/tests/cases/fourslash/findAllRefsObjectBindingElementPropertyName02.ts +++ b/tests/cases/fourslash/findAllRefsObjectBindingElementPropertyName02.ts @@ -8,12 +8,4 @@ ////var foo: I; ////var { [|property1|]: {} } = foo; -let ranges = test.ranges(); -for (let range of ranges) { - goTo.position(range.start); - - verify.referencesCountIs(ranges.length); - for (let expectedRange of ranges) { - verify.referencesAtPositionContains(expectedRange); - } -} \ No newline at end of file +verify.rangesReferenceEachOther(); diff --git a/tests/cases/fourslash/findAllRefsObjectBindingElementPropertyName03.ts b/tests/cases/fourslash/findAllRefsObjectBindingElementPropertyName03.ts index 8dc2b1e7bb8..40ecf0139a7 100644 --- a/tests/cases/fourslash/findAllRefsObjectBindingElementPropertyName03.ts +++ b/tests/cases/fourslash/findAllRefsObjectBindingElementPropertyName03.ts @@ -8,12 +8,4 @@ ////var foo: I; ////var [{ [|property1|]: prop1 }, { [|property1|], property2 } ] = [foo, foo]; -let ranges = test.ranges(); -for (let range of ranges) { - goTo.position(range.start); - - verify.referencesCountIs(ranges.length); - for (let expectedRange of ranges) { - verify.referencesAtPositionContains(expectedRange); - } -} \ No newline at end of file +verify.rangesReferenceEachOther(); diff --git a/tests/cases/fourslash/findAllRefsObjectBindingElementPropertyName04.ts b/tests/cases/fourslash/findAllRefsObjectBindingElementPropertyName04.ts index dfa0997774e..5696d242fed 100644 --- a/tests/cases/fourslash/findAllRefsObjectBindingElementPropertyName04.ts +++ b/tests/cases/fourslash/findAllRefsObjectBindingElementPropertyName04.ts @@ -5,17 +5,11 @@ //// property2: string; ////} //// -////function f({ /**/[|property1|]: p1 }: I, +////function f({ [|property1|]: p1 }: I, //// { [|property1|] }: I, //// { property1: p2 }) { //// //// return [|property1|] + 1; ////} -goTo.marker(); - -let ranges = test.ranges(); -verify.referencesCountIs(ranges.length); -for (let expectedRange of ranges) { - verify.referencesAtPositionContains(expectedRange); -} \ No newline at end of file +verify.rangesReferenceEachOther(); diff --git a/tests/cases/fourslash/findAllRefsObjectBindingElementPropertyName06.ts b/tests/cases/fourslash/findAllRefsObjectBindingElementPropertyName06.ts index 379d1d4d5f5..8be45ac87e6 100644 --- a/tests/cases/fourslash/findAllRefsObjectBindingElementPropertyName06.ts +++ b/tests/cases/fourslash/findAllRefsObjectBindingElementPropertyName06.ts @@ -19,12 +19,4 @@ // Note: if this test ever changes, consider updating // 'quickInfoForObjectBindingElementPropertyName05.ts' -let ranges = test.ranges(); -for (let range of ranges) { - goTo.position(range.start); - - verify.referencesCountIs(ranges.length); - for (let expectedRange of ranges) { - verify.referencesAtPositionContains(expectedRange); - } -} \ No newline at end of file +verify.rangesReferenceEachOther(); diff --git a/tests/cases/fourslash/findAllRefsObjectBindingElementPropertyName07.ts b/tests/cases/fourslash/findAllRefsObjectBindingElementPropertyName07.ts index 6448d2396b3..96f414dc060 100644 --- a/tests/cases/fourslash/findAllRefsObjectBindingElementPropertyName07.ts +++ b/tests/cases/fourslash/findAllRefsObjectBindingElementPropertyName07.ts @@ -4,12 +4,4 @@ //// ////p, [{ [|a|]: p, b }] = [{ [|a|]: 10, b: true }]; -let ranges = test.ranges(); -for (let range of ranges) { - goTo.position(range.start); - - verify.referencesCountIs(ranges.length); - for (let expectedRange of ranges) { - verify.referencesAtPositionContains(expectedRange); - } -} \ No newline at end of file +verify.rangesReferenceEachOther(); diff --git a/tests/cases/fourslash/findAllRefsObjectBindingElementPropertyName09.ts b/tests/cases/fourslash/findAllRefsObjectBindingElementPropertyName09.ts index 0b82c73e31d..5696d242fed 100644 --- a/tests/cases/fourslash/findAllRefsObjectBindingElementPropertyName09.ts +++ b/tests/cases/fourslash/findAllRefsObjectBindingElementPropertyName09.ts @@ -6,16 +6,10 @@ ////} //// ////function f({ [|property1|]: p1 }: I, -//// { /**/[|property1|] }: I, +//// { [|property1|] }: I, //// { property1: p2 }) { //// //// return [|property1|] + 1; ////} -goTo.marker(); - -let ranges = test.ranges(); -verify.referencesCountIs(ranges.length); -for (let expectedRange of ranges) { - verify.referencesAtPositionContains(expectedRange); -} \ No newline at end of file +verify.rangesReferenceEachOther(); diff --git a/tests/cases/fourslash/findAllRefsObjectBindingElementPropertyName10.ts b/tests/cases/fourslash/findAllRefsObjectBindingElementPropertyName10.ts index 7b8be1aa91c..6ffa4b03b68 100644 --- a/tests/cases/fourslash/findAllRefsObjectBindingElementPropertyName10.ts +++ b/tests/cases/fourslash/findAllRefsObjectBindingElementPropertyName10.ts @@ -8,12 +8,4 @@ ////function f ({ [|next|]: { [|next|]: x} }: Recursive) { ////} -let ranges = test.ranges(); -for (let range of ranges) { - goTo.position(range.start); - - verify.referencesCountIs(ranges.length); - for (let expectedRange of ranges) { - verify.referencesAtPositionContains(expectedRange); - } -} \ No newline at end of file +verify.rangesReferenceEachOther(); diff --git a/tests/cases/fourslash/findAllRefsOnPrivateParameterProperty1.ts b/tests/cases/fourslash/findAllRefsOnPrivateParameterProperty1.ts index 6686a771b34..3b8b5084a6b 100644 --- a/tests/cases/fourslash/findAllRefsOnPrivateParameterProperty1.ts +++ b/tests/cases/fourslash/findAllRefsOnPrivateParameterProperty1.ts @@ -9,10 +9,4 @@ //// } ////} -test.ranges().forEach(r => { - goTo.position(r.start); - - test.ranges().forEach(range => { - verify.referencesAtPositionContains(range); - }); -}); +verify.rangesReferenceEachOther(); diff --git a/tests/cases/fourslash/findAllRefsParameterPropertyDeclaration1.ts b/tests/cases/fourslash/findAllRefsParameterPropertyDeclaration1.ts index 4018698f4ef..8f30e3cc853 100644 --- a/tests/cases/fourslash/findAllRefsParameterPropertyDeclaration1.ts +++ b/tests/cases/fourslash/findAllRefsParameterPropertyDeclaration1.ts @@ -7,15 +7,4 @@ //// } //// } -const ranges = test.ranges(); -verify.assertHasRanges(ranges); -for (const range of ranges) { - goTo.position(range.start); - - if (ranges.length) { - verify.referencesCountIs(ranges.length); - for (const expectedRange of ranges) { - verify.referencesAtPositionContains(expectedRange); - } - } -} \ No newline at end of file +verify.rangesReferenceEachOther(); diff --git a/tests/cases/fourslash/findAllRefsParameterPropertyDeclaration2.ts b/tests/cases/fourslash/findAllRefsParameterPropertyDeclaration2.ts index a450a77e2dc..d9656e8b5cf 100644 --- a/tests/cases/fourslash/findAllRefsParameterPropertyDeclaration2.ts +++ b/tests/cases/fourslash/findAllRefsParameterPropertyDeclaration2.ts @@ -7,13 +7,4 @@ //// } //// } -let ranges = test.ranges(); -verify.assertHasRanges(ranges); -for (let range of ranges) { - goTo.position(range.start); - - verify.referencesCountIs(ranges.length); - for (let expectedRange of ranges) { - verify.referencesAtPositionContains(expectedRange); - } -} \ No newline at end of file +verify.rangesReferenceEachOther(); diff --git a/tests/cases/fourslash/findAllRefsParameterPropertyDeclaration3.ts b/tests/cases/fourslash/findAllRefsParameterPropertyDeclaration3.ts index 82fd67dfc9b..dc48ffedb21 100644 --- a/tests/cases/fourslash/findAllRefsParameterPropertyDeclaration3.ts +++ b/tests/cases/fourslash/findAllRefsParameterPropertyDeclaration3.ts @@ -7,13 +7,4 @@ //// } //// } -const ranges = test.ranges(); -verify.assertHasRanges(ranges); -for (const range of ranges) { - goTo.position(range.start); - - verify.referencesCountIs(ranges.length); - for (const expectedRange of ranges) { - verify.referencesAtPositionContains(expectedRange); - } -} \ No newline at end of file +verify.rangesReferenceEachOther(); diff --git a/tests/cases/fourslash/findAllRefsPropertyContextuallyTypedByTypeParam01.ts b/tests/cases/fourslash/findAllRefsPropertyContextuallyTypedByTypeParam01.ts index 357b355971d..f5b0143ac4b 100644 --- a/tests/cases/fourslash/findAllRefsPropertyContextuallyTypedByTypeParam01.ts +++ b/tests/cases/fourslash/findAllRefsPropertyContextuallyTypedByTypeParam01.ts @@ -17,12 +17,4 @@ //// [|a|]: "ss" ////}; -let ranges = test.ranges() -for (let range of ranges) { - goTo.position(range.start); - - verify.referencesCountIs(ranges.length); - for (let expectedReference of ranges) { - verify.referencesAtPositionContains(expectedReference); - } -} \ No newline at end of file +verify.rangesReferenceEachOther(); diff --git a/tests/cases/fourslash/findAllRefsWithLeadingUnderscoreNames1.ts b/tests/cases/fourslash/findAllRefsWithLeadingUnderscoreNames1.ts index 369258832a9..7cc47ee34ea 100644 --- a/tests/cases/fourslash/findAllRefsWithLeadingUnderscoreNames1.ts +++ b/tests/cases/fourslash/findAllRefsWithLeadingUnderscoreNames1.ts @@ -7,12 +7,4 @@ ////var x: Foo; ////x.[|_bar|]; - -test.ranges().forEach(r1 => { - goTo.position(r1.start); - verify.referencesCountIs(2); - - test.ranges().forEach(r2 => { - verify.referencesAtPositionContains(r2); - }); -}); \ No newline at end of file +verify.rangesReferenceEachOther(); diff --git a/tests/cases/fourslash/findAllRefsWithLeadingUnderscoreNames2.ts b/tests/cases/fourslash/findAllRefsWithLeadingUnderscoreNames2.ts index 53adb8236a4..d1449016b59 100644 --- a/tests/cases/fourslash/findAllRefsWithLeadingUnderscoreNames2.ts +++ b/tests/cases/fourslash/findAllRefsWithLeadingUnderscoreNames2.ts @@ -7,12 +7,4 @@ ////var x: Foo; ////x.[|__bar|]; - -test.ranges().forEach(r1 => { - goTo.position(r1.start); - verify.referencesCountIs(2); - - test.ranges().forEach(r2 => { - verify.referencesAtPositionContains(r2); - }); -}); \ No newline at end of file +verify.rangesReferenceEachOther(); diff --git a/tests/cases/fourslash/findAllRefsWithLeadingUnderscoreNames3.ts b/tests/cases/fourslash/findAllRefsWithLeadingUnderscoreNames3.ts index 1793ec6ddc4..e0d6f7f3456 100644 --- a/tests/cases/fourslash/findAllRefsWithLeadingUnderscoreNames3.ts +++ b/tests/cases/fourslash/findAllRefsWithLeadingUnderscoreNames3.ts @@ -7,12 +7,4 @@ ////var x: Foo; ////x.[|___bar|]; - -test.ranges().forEach(r1 => { - goTo.position(r1.start); - verify.referencesCountIs(2); - - test.ranges().forEach(r2 => { - verify.referencesAtPositionContains(r2); - }); -}); \ No newline at end of file +verify.rangesReferenceEachOther(); diff --git a/tests/cases/fourslash/findAllRefsWithLeadingUnderscoreNames4.ts b/tests/cases/fourslash/findAllRefsWithLeadingUnderscoreNames4.ts index 694ecdc79ab..5a8de54db4d 100644 --- a/tests/cases/fourslash/findAllRefsWithLeadingUnderscoreNames4.ts +++ b/tests/cases/fourslash/findAllRefsWithLeadingUnderscoreNames4.ts @@ -7,12 +7,4 @@ ////var x: Foo; ////x.[|____bar|]; - -test.ranges().forEach(r1 => { - goTo.position(r1.start); - verify.referencesCountIs(2); - - test.ranges().forEach(r2 => { - verify.referencesAtPositionContains(r2); - }); -}); \ No newline at end of file +verify.rangesReferenceEachOther(); diff --git a/tests/cases/fourslash/findAllRefsWithLeadingUnderscoreNames5.ts b/tests/cases/fourslash/findAllRefsWithLeadingUnderscoreNames5.ts index a110597f4b7..4ab28f164fe 100644 --- a/tests/cases/fourslash/findAllRefsWithLeadingUnderscoreNames5.ts +++ b/tests/cases/fourslash/findAllRefsWithLeadingUnderscoreNames5.ts @@ -13,12 +13,4 @@ ////x.[|___bar|]; ////x.____bar; - -test.ranges().forEach(r1 => { - goTo.position(r1.start); - verify.referencesCountIs(2); - - test.ranges().forEach(r2 => { - verify.referencesAtPositionContains(r2); - }); -}); \ No newline at end of file +verify.rangesReferenceEachOther(); diff --git a/tests/cases/fourslash/findAllRefsWithLeadingUnderscoreNames6.ts b/tests/cases/fourslash/findAllRefsWithLeadingUnderscoreNames6.ts index 97ed29eb178..191b0bdbc14 100644 --- a/tests/cases/fourslash/findAllRefsWithLeadingUnderscoreNames6.ts +++ b/tests/cases/fourslash/findAllRefsWithLeadingUnderscoreNames6.ts @@ -13,12 +13,4 @@ ////x.___bar; ////x.____bar; - -test.ranges().forEach(r1 => { - goTo.position(r1.start); - verify.referencesCountIs(2); - - test.ranges().forEach(r2 => { - verify.referencesAtPositionContains(r2); - }); -}); \ No newline at end of file +verify.rangesReferenceEachOther(); diff --git a/tests/cases/fourslash/findAllRefsWithLeadingUnderscoreNames7.ts b/tests/cases/fourslash/findAllRefsWithLeadingUnderscoreNames7.ts index afcaf625ede..f3578193096 100644 --- a/tests/cases/fourslash/findAllRefsWithLeadingUnderscoreNames7.ts +++ b/tests/cases/fourslash/findAllRefsWithLeadingUnderscoreNames7.ts @@ -4,11 +4,4 @@ //// [|__foo|](); ////} -test.ranges().forEach(r => { - goTo.position(r.start); - verify.referencesCountIs(2); - - test.ranges().forEach(range => { - verify.referencesAtPositionContains(range); - }); -}); \ No newline at end of file +verify.rangesReferenceEachOther(); diff --git a/tests/cases/fourslash/findAllRefsWithLeadingUnderscoreNames8.ts b/tests/cases/fourslash/findAllRefsWithLeadingUnderscoreNames8.ts index 08ed3414366..15c66f4e7c6 100644 --- a/tests/cases/fourslash/findAllRefsWithLeadingUnderscoreNames8.ts +++ b/tests/cases/fourslash/findAllRefsWithLeadingUnderscoreNames8.ts @@ -4,11 +4,4 @@ //// [|__foo|](); ////}) -test.ranges().forEach(r => { - goTo.position(r.start); - verify.referencesCountIs(2); - - test.ranges().forEach(range => { - verify.referencesAtPositionContains(range); - }); -}); \ No newline at end of file +verify.rangesReferenceEachOther(); diff --git a/tests/cases/fourslash/findAllRefsWithLeadingUnderscoreNames9.ts b/tests/cases/fourslash/findAllRefsWithLeadingUnderscoreNames9.ts index a3923783c46..cc33b618c41 100644 --- a/tests/cases/fourslash/findAllRefsWithLeadingUnderscoreNames9.ts +++ b/tests/cases/fourslash/findAllRefsWithLeadingUnderscoreNames9.ts @@ -4,11 +4,4 @@ //// [|___foo|](); ////}) -test.ranges().forEach(r => { - goTo.position(r.start); - verify.referencesCountIs(2); - - test.ranges().forEach(range => { - verify.referencesAtPositionContains(range); - }); -}); \ No newline at end of file +verify.rangesReferenceEachOther(); diff --git a/tests/cases/fourslash/fourslash.ts b/tests/cases/fourslash/fourslash.ts index 78de5b02358..dd8a61ac458 100644 --- a/tests/cases/fourslash/fourslash.ts +++ b/tests/cases/fourslash/fourslash.ts @@ -124,8 +124,6 @@ declare namespace FourSlashInterface { completionListIsEmpty(): void; completionListAllowsNewIdentifier(): void; memberListIsEmpty(): void; - referencesCountIs(count: number): void; - referencesAtPositionContains(range: Range, isWriteAccess?: boolean, isDefinition?: boolean): void; signatureHelpPresent(): void; errorExistsBetweenMarkers(startMarker: string, endMarker: string): void; errorExistsAfterMarker(markerName?: string): void; @@ -154,6 +152,24 @@ declare namespace FourSlashInterface { currentFileContentIs(text: string): void; verifyGetEmitOutputForCurrentFile(expected: string): void; verifyGetEmitOutputContentsForCurrentFile(expected: ts.OutputFile[]): void; + referencesCountIs(count: number): void; + /** + * Asserts that the given ranges are the references from the current position. + * If ranges have markers, those markers may have "isDefinition" and "isWriteAccess" data + * (otherwise these properties pf the reference are not tested). + * Order of ranges does not matter. + */ + referencesAre(ranges: Range[]): void; + /** + * Like `referencesAre`, but goes to `start` first. + * `start` should be included in `references`. + */ + referencesOf(start: Range, references: Range[]): void; + /** + * Performs `referencesOf` for every range on the whole set. + * If `ranges` is omitted, this is `test.ranges()`. + */ + rangesReferenceEachOther(ranges?: Range[]): void; currentParameterHelpArgumentNameIs(name: string): void; currentParameterSpanIs(parameter: string): void; currentParameterHelpArgumentDocCommentIs(docComment: string): void; diff --git a/tests/cases/fourslash/getOccurrencesIsDefinitionOfArrowFunction.ts b/tests/cases/fourslash/getOccurrencesIsDefinitionOfArrowFunction.ts index eb9980c946d..57b4c14ffba 100644 --- a/tests/cases/fourslash/getOccurrencesIsDefinitionOfArrowFunction.ts +++ b/tests/cases/fourslash/getOccurrencesIsDefinitionOfArrowFunction.ts @@ -1,8 +1,5 @@ /// ////var [|{| "isDefinition": true |}f|] = x => x + 1; ////[|{| "isDefinition": false |}f|](12); -var firstRange = test.ranges()[0]; -goTo.position(firstRange.start, firstRange.fileName); -test.ranges().forEach(range => { - verify.referencesAtPositionContains(range, undefined, range.marker.data.isDefinition); -}); + +verify.rangesReferenceEachOther(); diff --git a/tests/cases/fourslash/getOccurrencesIsDefinitionOfBindingPattern.ts b/tests/cases/fourslash/getOccurrencesIsDefinitionOfBindingPattern.ts index 9a30687c59e..e60921ec3b8 100644 --- a/tests/cases/fourslash/getOccurrencesIsDefinitionOfBindingPattern.ts +++ b/tests/cases/fourslash/getOccurrencesIsDefinitionOfBindingPattern.ts @@ -1,8 +1,5 @@ /// ////const { [|{| "isDefinition": true |}x|], y } = { x: 1, y: 2 }; ////const z = [|{| "isDefinition": false |}x|]; -var firstRange = test.ranges()[0]; -goTo.position(firstRange.start, firstRange.fileName); -test.ranges().forEach(range => { - verify.referencesAtPositionContains(range, undefined, range.marker.data.isDefinition); -}); + +verify.rangesReferenceEachOther(); diff --git a/tests/cases/fourslash/getOccurrencesIsDefinitionOfClass.ts b/tests/cases/fourslash/getOccurrencesIsDefinitionOfClass.ts index 04b1f90681a..0a3c2e4231c 100644 --- a/tests/cases/fourslash/getOccurrencesIsDefinitionOfClass.ts +++ b/tests/cases/fourslash/getOccurrencesIsDefinitionOfClass.ts @@ -6,8 +6,5 @@ //// } ////} ////let c = new [|{| "isDefinition": false |}C|](); -var firstRange = test.ranges()[0]; -goTo.position(firstRange.start, firstRange.fileName); -test.ranges().forEach(range => { - verify.referencesAtPositionContains(range, undefined, range.marker.data.isDefinition); -}); + +verify.rangesReferenceEachOther(); diff --git a/tests/cases/fourslash/getOccurrencesIsDefinitionOfComputedProperty.ts b/tests/cases/fourslash/getOccurrencesIsDefinitionOfComputedProperty.ts index 8896694db50..81df97eff2d 100644 --- a/tests/cases/fourslash/getOccurrencesIsDefinitionOfComputedProperty.ts +++ b/tests/cases/fourslash/getOccurrencesIsDefinitionOfComputedProperty.ts @@ -1,9 +1,7 @@ /// -////let o = { ["[|{| "isDefinition": true |}foo|]"]: 12 }; +////let o = { ["/**/[|{| "isDefinition": true |}foo|]"]: 12 }; ////let y = o.[|{| "isDefinition": false |}foo|]; ////let z = o['[|{| "isDefinition": false |}foo|]']; -var firstRange = test.ranges()[0]; -goTo.position(firstRange.start, firstRange.fileName); -test.ranges().forEach(range => { - verify.referencesAtPositionContains(range, undefined, range.marker.data.isDefinition); -}); + +goTo.marker(); +verify.referencesAre(test.ranges()); diff --git a/tests/cases/fourslash/getOccurrencesIsDefinitionOfEnum.ts b/tests/cases/fourslash/getOccurrencesIsDefinitionOfEnum.ts index a5764206bce..5b77bf6158a 100644 --- a/tests/cases/fourslash/getOccurrencesIsDefinitionOfEnum.ts +++ b/tests/cases/fourslash/getOccurrencesIsDefinitionOfEnum.ts @@ -4,8 +4,5 @@ //// Second ////} ////let first = [|{| "isDefinition": false |}E|].First; -var firstRange = test.ranges()[0]; -goTo.position(firstRange.start, firstRange.fileName); -test.ranges().forEach(range => { - verify.referencesAtPositionContains(range, undefined, range.marker.data.isDefinition); -}); + +verify.rangesReferenceEachOther(); diff --git a/tests/cases/fourslash/getOccurrencesIsDefinitionOfExport.ts b/tests/cases/fourslash/getOccurrencesIsDefinitionOfExport.ts index f863af9184f..759df4fa383 100644 --- a/tests/cases/fourslash/getOccurrencesIsDefinitionOfExport.ts +++ b/tests/cases/fourslash/getOccurrencesIsDefinitionOfExport.ts @@ -4,8 +4,5 @@ // @Filename: main.ts ////import { [|{| "isDefinition": true |}x|] } from "./m"; ////const y = [|{| "isDefinition": false |}x|]; -var firstRange = test.ranges()[0]; -goTo.position(firstRange.start, firstRange.fileName); -test.ranges().forEach(range => { - verify.referencesAtPositionContains(range, undefined, range.marker.data.isDefinition); -}); + +verify.rangesReferenceEachOther(); diff --git a/tests/cases/fourslash/getOccurrencesIsDefinitionOfFunction.ts b/tests/cases/fourslash/getOccurrencesIsDefinitionOfFunction.ts index 456d953092d..357ef088d61 100644 --- a/tests/cases/fourslash/getOccurrencesIsDefinitionOfFunction.ts +++ b/tests/cases/fourslash/getOccurrencesIsDefinitionOfFunction.ts @@ -2,8 +2,5 @@ ////function [|{| "isDefinition": true |}func|](x: number) { ////} ////[|{| "isDefinition": false |}func|](x) -var firstRange = test.ranges()[0]; -goTo.position(firstRange.start, firstRange.fileName); -test.ranges().forEach(range => { - verify.referencesAtPositionContains(range, undefined, range.marker.data.isDefinition); -}); + +verify.rangesReferenceEachOther(); diff --git a/tests/cases/fourslash/getOccurrencesIsDefinitionOfInterface.ts b/tests/cases/fourslash/getOccurrencesIsDefinitionOfInterface.ts index 51d1e858185..b7117995d84 100644 --- a/tests/cases/fourslash/getOccurrencesIsDefinitionOfInterface.ts +++ b/tests/cases/fourslash/getOccurrencesIsDefinitionOfInterface.ts @@ -3,8 +3,5 @@ //// p: number; ////} ////let i: [|{| "isDefinition": false |}I|] = { p: 12 }; -var firstRange = test.ranges()[0]; -goTo.position(firstRange.start, firstRange.fileName); -test.ranges().forEach(range => { - verify.referencesAtPositionContains(range, undefined, range.marker.data.isDefinition); -}); + +verify.rangesReferenceEachOther(); diff --git a/tests/cases/fourslash/getOccurrencesIsDefinitionOfInterfaceClassMerge.ts b/tests/cases/fourslash/getOccurrencesIsDefinitionOfInterfaceClassMerge.ts index 7efefa17a4b..b0506490c70 100644 --- a/tests/cases/fourslash/getOccurrencesIsDefinitionOfInterfaceClassMerge.ts +++ b/tests/cases/fourslash/getOccurrencesIsDefinitionOfInterfaceClassMerge.ts @@ -12,8 +12,5 @@ ////} ////let i: [|{| "isDefinition": false |}Numbers|] = new [|{| "isDefinition": false |}Numbers|](); ////let x = i.f(i.p + i.m); -var firstRange = test.ranges()[0]; -goTo.position(firstRange.start, firstRange.fileName); -test.ranges().forEach(range => { - verify.referencesAtPositionContains(range, undefined, range.marker.data.isDefinition); -}); + +verify.rangesReferenceEachOther(); diff --git a/tests/cases/fourslash/getOccurrencesIsDefinitionOfNamespace.ts b/tests/cases/fourslash/getOccurrencesIsDefinitionOfNamespace.ts index 86b92ec9ce7..ff1cf00fa9b 100644 --- a/tests/cases/fourslash/getOccurrencesIsDefinitionOfNamespace.ts +++ b/tests/cases/fourslash/getOccurrencesIsDefinitionOfNamespace.ts @@ -3,8 +3,5 @@ //// export var n = 12; ////} ////let x = [|{| "isDefinition": false |}Numbers|].n + 1; -var firstRange = test.ranges()[0]; -goTo.position(firstRange.start, firstRange.fileName); -test.ranges().forEach(range => { - verify.referencesAtPositionContains(range, undefined, range.marker.data.isDefinition); -}); + +verify.rangesReferenceEachOther(); diff --git a/tests/cases/fourslash/getOccurrencesIsDefinitionOfNumberNamedProperty.ts b/tests/cases/fourslash/getOccurrencesIsDefinitionOfNumberNamedProperty.ts index 7e3e084948f..a041dab435a 100644 --- a/tests/cases/fourslash/getOccurrencesIsDefinitionOfNumberNamedProperty.ts +++ b/tests/cases/fourslash/getOccurrencesIsDefinitionOfNumberNamedProperty.ts @@ -1,8 +1,5 @@ /// ////let o = { [|{| "isDefinition": true |}1|]: 12 }; ////let y = o[[|{| "isDefinition": false |}1|]]; -var firstRange = test.ranges()[0]; -goTo.position(firstRange.start, firstRange.fileName); -test.ranges().forEach(range => { - verify.referencesAtPositionContains(range, undefined, range.marker.data.isDefinition); -}); + +verify.rangesReferenceEachOther(); diff --git a/tests/cases/fourslash/getOccurrencesIsDefinitionOfParameter.ts b/tests/cases/fourslash/getOccurrencesIsDefinitionOfParameter.ts index cdb0e281d53..cdcc47014fd 100644 --- a/tests/cases/fourslash/getOccurrencesIsDefinitionOfParameter.ts +++ b/tests/cases/fourslash/getOccurrencesIsDefinitionOfParameter.ts @@ -2,8 +2,5 @@ ////function f([|{| "isDefinition": true |}x|]: number) { //// return [|{| "isDefinition": false |}x|] + 1 ////} -var firstRange = test.ranges()[0]; -goTo.position(firstRange.start, firstRange.fileName); -test.ranges().forEach(range => { - verify.referencesAtPositionContains(range, undefined, range.marker.data.isDefinition); -}); + +verify.rangesReferenceEachOther(); diff --git a/tests/cases/fourslash/getOccurrencesIsDefinitionOfStringNamedProperty.ts b/tests/cases/fourslash/getOccurrencesIsDefinitionOfStringNamedProperty.ts index 291aec90dda..383cf49a2e3 100644 --- a/tests/cases/fourslash/getOccurrencesIsDefinitionOfStringNamedProperty.ts +++ b/tests/cases/fourslash/getOccurrencesIsDefinitionOfStringNamedProperty.ts @@ -1,8 +1,5 @@ /// ////let o = { "[|{| "isDefinition": true |}x|]": 12 }; ////let y = o.[|{| "isDefinition": false |}x|]; -var firstRange = test.ranges()[0]; -goTo.position(firstRange.start, firstRange.fileName); -test.ranges().forEach(range => { - verify.referencesAtPositionContains(range, undefined, range.marker.data.isDefinition); -}); + +verify.rangesReferenceEachOther(); diff --git a/tests/cases/fourslash/getOccurrencesIsDefinitionOfTypeAlias.ts b/tests/cases/fourslash/getOccurrencesIsDefinitionOfTypeAlias.ts index 44a7c64a93a..2bd66830c69 100644 --- a/tests/cases/fourslash/getOccurrencesIsDefinitionOfTypeAlias.ts +++ b/tests/cases/fourslash/getOccurrencesIsDefinitionOfTypeAlias.ts @@ -1,8 +1,5 @@ /// ////type [|{| "isDefinition": true |}Alias|]= number; ////let n: [|{| "isDefinition": false |}Alias|] = 12; -var firstRange = test.ranges()[0]; -goTo.position(firstRange.start, firstRange.fileName); -test.ranges().forEach(range => { - verify.referencesAtPositionContains(range, undefined, range.marker.data.isDefinition); -}); + +verify.rangesReferenceEachOther(); diff --git a/tests/cases/fourslash/getOccurrencesIsDefinitionOfVariable.ts b/tests/cases/fourslash/getOccurrencesIsDefinitionOfVariable.ts index 8d046c67e3a..78529186ad3 100644 --- a/tests/cases/fourslash/getOccurrencesIsDefinitionOfVariable.ts +++ b/tests/cases/fourslash/getOccurrencesIsDefinitionOfVariable.ts @@ -16,8 +16,5 @@ //// ////[|{| "isDefinition": false |}x|] += 1; ////[|{| "isDefinition": false |}x|] <<= 1; -var firstRange = test.ranges()[0]; -goTo.position(firstRange.start, firstRange.fileName); -test.ranges().forEach(range => { - verify.referencesAtPositionContains(range, undefined, range.marker.data.isDefinition); -}); + +verify.rangesReferenceEachOther(); diff --git a/tests/cases/fourslash/referenceToClass.ts b/tests/cases/fourslash/referenceToClass.ts index 2907a468577..3e523487bd1 100644 --- a/tests/cases/fourslash/referenceToClass.ts +++ b/tests/cases/fourslash/referenceToClass.ts @@ -3,34 +3,21 @@ // Class references should work across file and not find local variables. // @Filename: referenceToClass_1.ts -////class /*1*/foo { -//// public n: /*2*/foo; +////class [|foo|] { +//// public n: [|foo|]; //// public foo: number; ////} //// ////class bar { -//// public n: fo/*3*/o; -//// public k = new foo(); +//// public n: [|foo|]; +//// public k = new [|foo|](); ////} //// ////module mod { -//// var k: foo = null; +//// var k: [|foo|] = null; ////} // @Filename: referenceToClass_2.ts -////var k: /*4*/foo; +////var k: [|foo|]; -goTo.marker("1"); -verify.referencesCountIs(6); - -goTo.marker("2"); - -verify.referencesCountIs(6); - -goTo.marker("3"); - -verify.referencesCountIs(6); - -goTo.marker("4"); - -verify.referencesCountIs(6); \ No newline at end of file +verify.rangesReferenceEachOther(); diff --git a/tests/cases/fourslash/referencesForStaticsAndMembersWithSameNames.ts b/tests/cases/fourslash/referencesForStaticsAndMembersWithSameNames.ts index fe83f47757b..cada3e0fcf4 100644 --- a/tests/cases/fourslash/referencesForStaticsAndMembersWithSameNames.ts +++ b/tests/cases/fourslash/referencesForStaticsAndMembersWithSameNames.ts @@ -3,12 +3,12 @@ ////module FindRef4 { //// module MixedStaticsClassTest { //// export class Foo { -//// b/*3*/ar: Foo; -//// static b/*4*/ar: Foo; +//// [|bar|]: Foo; +//// static [|bar|]: Foo; //// -//// public f/*1*/oo(): void { +//// public [|foo|](): void { //// } -//// public static f/*2*/oo(): void { +//// public static [|foo|](): void { //// } //// } //// } @@ -16,34 +16,25 @@ //// function test() { //// // instance function //// var x = new MixedStaticsClassTest.Foo(); -//// x.foo(); -//// x.bar; -//// -//// var y = new MixedStaticsClassTest.Foo(); -//// y.foo(); -//// y.bar; +//// x.[|foo|](); +//// x.[|bar|]; //// //// // static function -//// MixedStaticsClassTest.Foo.foo(); -//// MixedStaticsClassTest.Foo.bar; +//// MixedStaticsClassTest.Foo.[|foo|](); +//// MixedStaticsClassTest.Foo.[|bar|]; //// } ////} -// this line triggers a semantic/syntactic error check, remove line when 788570 is fixed -edit.insert(''); +const [fooBar, fooStaticBar, fooFoo, fooStaticFoo, xFoo, xBar, staticFoo, staticBar] = test.ranges(); // References to a member method with the same name as a static. -goTo.marker("1"); -verify.referencesCountIs(3); +verify.referencesOf(fooFoo, [fooFoo, xFoo]); // References to a static method with the same name as a member. -goTo.marker("2"); -verify.referencesCountIs(2); +verify.referencesOf(fooStaticFoo, [fooStaticFoo, staticFoo]); // References to a member property with the same name as a static. -goTo.marker("3"); -verify.referencesCountIs(3); +verify.referencesOf(fooBar, [fooBar, xBar]); // References to a static property with the same name as a member. -goTo.marker("4"); -verify.referencesCountIs(2); +verify.referencesOf(fooStaticBar, [fooStaticBar, staticBar]); diff --git a/tests/cases/fourslash/referencesForStringLiteralPropertyNames.ts b/tests/cases/fourslash/referencesForStringLiteralPropertyNames.ts index e2f61d9cab3..2db0325d324 100644 --- a/tests/cases/fourslash/referencesForStringLiteralPropertyNames.ts +++ b/tests/cases/fourslash/referencesForStringLiteralPropertyNames.ts @@ -1,16 +1,13 @@ /// ////class Foo { -//// public /*1*/"ss": any; +//// public "[|ss|]": any; ////} //// ////var x: Foo; -////x.ss; -////x[/*2*/"ss"]; -////x = { "ss": 0 }; -////x = { /*3*/ss: 0 }; +////x.[|ss|]; +////x["[|ss|]"]; +////x = { "[|ss|]": 0 }; +////x = { [|ss|]: 0 }; -test.markers().forEach((m) => { - goTo.position(m.position, m.fileName); - verify.referencesCountIs(5); -}); +verify.rangesReferenceEachOther(); diff --git a/tests/cases/fourslash/referencesForStringLiteralPropertyNames2.ts b/tests/cases/fourslash/referencesForStringLiteralPropertyNames2.ts index 67b956dcd02..275a2b2756c 100644 --- a/tests/cases/fourslash/referencesForStringLiteralPropertyNames2.ts +++ b/tests/cases/fourslash/referencesForStringLiteralPropertyNames2.ts @@ -1,15 +1,10 @@ /// ////class Foo { -//// /*1*/"blah"() { return 0; } +//// "[|blah|]"() { return 0; } ////} //// ////var x: Foo; -////x./*2*/blah; +////x.[|blah|]; - -goTo.marker("1"); -verify.referencesCountIs(2); - -goTo.marker("2"); -verify.referencesCountIs(2); +verify.rangesReferenceEachOther(); diff --git a/tests/cases/fourslash/referencesForStringLiteralPropertyNames3.ts b/tests/cases/fourslash/referencesForStringLiteralPropertyNames3.ts index 3288aec9b92..55a067ae8c3 100644 --- a/tests/cases/fourslash/referencesForStringLiteralPropertyNames3.ts +++ b/tests/cases/fourslash/referencesForStringLiteralPropertyNames3.ts @@ -1,15 +1,11 @@ /// ////class Foo2 { -//// get /*1*/"42"() { return 0; } -//// set /*2*/42(n) { } +//// get "[|42|]"() { return 0; } +//// set [|42|](n) { } ////} //// ////var y: Foo2; -////y[42]; +////y[[|42|]]; -goTo.marker("1"); -verify.referencesCountIs(3); - -goTo.marker("2"); -verify.referencesCountIs(3); \ No newline at end of file +verify.rangesReferenceEachOther(); diff --git a/tests/cases/fourslash/referencesForStringLiteralPropertyNames4.ts b/tests/cases/fourslash/referencesForStringLiteralPropertyNames4.ts index 08d04188ee9..34cad8f11e8 100644 --- a/tests/cases/fourslash/referencesForStringLiteralPropertyNames4.ts +++ b/tests/cases/fourslash/referencesForStringLiteralPropertyNames4.ts @@ -4,5 +4,4 @@ ////x["[|someProperty|]"] = 3; ////x./*1*/[|someProperty|] = 5; -goTo.marker("1"); -test.ranges().forEach(r => verify.referencesAtPositionContains(r)); \ No newline at end of file +verify.rangesReferenceEachOther(); diff --git a/tests/cases/fourslash/referencesForUnionProperties.ts b/tests/cases/fourslash/referencesForUnionProperties.ts index d5f0a2fd516..07eebeb5f74 100644 --- a/tests/cases/fourslash/referencesForUnionProperties.ts +++ b/tests/cases/fourslash/referencesForUnionProperties.ts @@ -1,16 +1,16 @@ /// ////interface One { -//// common: { /*1*/a: number; }; +//// common: { [|a|]: number; }; ////} //// ////interface Base { -//// /*2*/a: string; +//// [|a|]: string; //// b: string; ////} //// ////interface HasAOrB extends Base { -//// /*3*/a: string; +//// [|a|]: string; //// b: string; ////} //// @@ -20,16 +20,10 @@ //// ////var x : One | Two; //// -////x.common./*4*/a; +////x.common.[|a|]; -goTo.marker("1"); -verify.referencesCountIs(2); // One.common.a, x.common.a - -goTo.marker("2"); -verify.referencesCountIs(3); // Base.a, HasAOrB.a, x.common.a - -goTo.marker("3"); -verify.referencesCountIs(3); // Base.a, HasAOrB.a, x.common.a - -goTo.marker("4"); -verify.referencesCountIs(4); // One.common.a, Base.a, HasAOrB.a, x.common.a \ No newline at end of file +const [one, base, hasAOrB, x] = test.ranges(); +verify.referencesOf(one, [one, x]); +verify.referencesOf(base, [base, hasAOrB, x]); +verify.referencesOf(hasAOrB, [base, hasAOrB, x]); +verify.referencesOf(x, [one, base, hasAOrB, x]); diff --git a/tests/cases/fourslash/referencesInComment.ts b/tests/cases/fourslash/referencesInComment.ts index 7ae9fdec9d2..00f7ba26860 100644 --- a/tests/cases/fourslash/referencesInComment.ts +++ b/tests/cases/fourslash/referencesInComment.ts @@ -5,14 +5,6 @@ ////class foo { } ////var bar = 0; -goTo.marker("1"); -verify.referencesCountIs(0); - -goTo.marker("2"); -verify.referencesCountIs(0); - -goTo.marker("3"); -verify.referencesCountIs(0); - -goTo.marker("4"); -verify.referencesCountIs(0); \ No newline at end of file +for (const marker of test.markers()) { + verify.referencesCountIs(0); +} diff --git a/tests/cases/fourslash/renameImportAndExportInDiffFiles.ts b/tests/cases/fourslash/renameImportAndExportInDiffFiles.ts index 7b098721196..e982d6a4c7b 100644 --- a/tests/cases/fourslash/renameImportAndExportInDiffFiles.ts +++ b/tests/cases/fourslash/renameImportAndExportInDiffFiles.ts @@ -1,18 +1,10 @@ /// // @Filename: a.ts -////export var /*1*/a; +////export var [|a|]; // @Filename: b.ts -////import { /*2*/a } from './a'; -////export { /*3*/a }; +////import { [|a|] } from './a'; +////export { [|a|] }; -goTo.file("a.ts"); -goTo.marker("1"); - -goTo.file("b.ts"); -goTo.marker("2"); -verify.referencesCountIs(3); - -goTo.marker("3"); -verify.referencesCountIs(3); \ No newline at end of file +verify.rangesReferenceEachOther(); diff --git a/tests/cases/fourslash/server/references01.ts b/tests/cases/fourslash/server/references01.ts index 0ecbeb43de3..528f79de5b4 100644 --- a/tests/cases/fourslash/server/references01.ts +++ b/tests/cases/fourslash/server/references01.ts @@ -3,16 +3,13 @@ // Global class reference. // @Filename: referencesForGlobals_1.ts -////class /*2*/globalClass { +////class [|globalClass|] { //// public f() { } ////} // @Filename: referencesForGlobals_2.ts /////// -////var c = /*1*/globalClass(); +////var c = [|globalClass|](); -goTo.marker("1"); -verify.referencesCountIs(2); - -goTo.marker("2"); -verify.referencesCountIs(2); \ No newline at end of file +// Must reverse ranges so that referencesForGlobals_2 goes first -- otherwise referencesForGlobals_1 won't pick it up. +verify.rangesReferenceEachOther(test.ranges().reverse()); diff --git a/tests/cases/fourslash/server/referencesInConfiguredProject.ts b/tests/cases/fourslash/server/referencesInConfiguredProject.ts index 9a0d7095a93..109b5e36e4d 100644 --- a/tests/cases/fourslash/server/referencesInConfiguredProject.ts +++ b/tests/cases/fourslash/server/referencesInConfiguredProject.ts @@ -3,18 +3,14 @@ // Global class reference. // @Filename: referencesForGlobals_1.ts -////class /*2*/globalClass { +////class [|globalClass|] { //// public f() { } ////} // @Filename: referencesForGlobals_2.ts -////var c = /*1*/globalClass(); +////var c = [|globalClass|](); // @Filename: tsconfig.json ////{ "files": ["referencesForGlobals_1.ts", "referencesForGlobals_2.ts"] } -goTo.marker("1"); -verify.referencesCountIs(2); - -goTo.marker("2"); -verify.referencesCountIs(2); \ No newline at end of file +verify.rangesReferenceEachOther(); diff --git a/tests/cases/fourslash/shims-pp/getReferencesAtPosition.ts b/tests/cases/fourslash/shims-pp/getReferencesAtPosition.ts index a25a6e1285f..cb829807602 100644 --- a/tests/cases/fourslash/shims-pp/getReferencesAtPosition.ts +++ b/tests/cases/fourslash/shims-pp/getReferencesAtPosition.ts @@ -7,7 +7,7 @@ //// //// } //// -//// public /*1*/start(){ +//// public [|start|](){ //// return this; //// } //// @@ -20,10 +20,7 @@ ////import Second = require("./findAllRefsOnDefinition-import"); //// ////var second = new Second.Test() -////second.start(); +////second.[|start|](); ////second.stop(); -goTo.file("findAllRefsOnDefinition-import.ts"); -goTo.marker("1"); - -verify.referencesCountIs(2); \ No newline at end of file +verify.rangesReferenceEachOther(); diff --git a/tests/cases/fourslash/shims/getReferencesAtPosition.ts b/tests/cases/fourslash/shims/getReferencesAtPosition.ts index a25a6e1285f..cb829807602 100644 --- a/tests/cases/fourslash/shims/getReferencesAtPosition.ts +++ b/tests/cases/fourslash/shims/getReferencesAtPosition.ts @@ -7,7 +7,7 @@ //// //// } //// -//// public /*1*/start(){ +//// public [|start|](){ //// return this; //// } //// @@ -20,10 +20,7 @@ ////import Second = require("./findAllRefsOnDefinition-import"); //// ////var second = new Second.Test() -////second.start(); +////second.[|start|](); ////second.stop(); -goTo.file("findAllRefsOnDefinition-import.ts"); -goTo.marker("1"); - -verify.referencesCountIs(2); \ No newline at end of file +verify.rangesReferenceEachOther(); From 6366a6d6a3df47b15ce26809b52b0952d4f8c9a1 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Wed, 15 Jun 2016 09:28:28 -0700 Subject: [PATCH 35/56] Remove String, Number, and Boolean from TypeFlags.Falsy --- src/compiler/types.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/compiler/types.ts b/src/compiler/types.ts index db34bb7c6e8..255f1dff0c8 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -2224,7 +2224,8 @@ namespace ts { /* @internal */ Nullable = Undefined | Null, - Falsy = String | Number | Boolean | Void | Undefined | Null, + /* @internal */ + Falsy = Void | Undefined | Null, // TODO: Add false, 0, and "" /* @internal */ Intrinsic = Any | String | Number | Boolean | ESSymbol | Void | Undefined | Null | Never, /* @internal */ From 28b241e6156ea11ed20ed3dda4eda354b04272d5 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Wed, 15 Jun 2016 09:28:45 -0700 Subject: [PATCH 36/56] Add regression test --- tests/cases/compiler/strictNullLogicalAndOr.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 tests/cases/compiler/strictNullLogicalAndOr.ts diff --git a/tests/cases/compiler/strictNullLogicalAndOr.ts b/tests/cases/compiler/strictNullLogicalAndOr.ts new file mode 100644 index 00000000000..68055f8b20b --- /dev/null +++ b/tests/cases/compiler/strictNullLogicalAndOr.ts @@ -0,0 +1,15 @@ +// @strictNullChecks: true + +// Repro from #9113 + +let sinOrCos = Math.random() < .5; +let choice = sinOrCos && Math.sin || Math.cos; + +choice(Math.PI); + +function sq(n?: number): number { + const r = n !== undefined && n*n || 0; + return r; +} + +sq(3); \ No newline at end of file From c9e5bcb27645ad092fded94f686eb2c30dde15a2 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Wed, 15 Jun 2016 09:35:45 -0700 Subject: [PATCH 37/56] Accept new baselines --- .../logicalAndOperatorStrictMode.types | 104 +++++++++--------- .../reference/strictNullLogicalAndOr.js | 26 +++++ .../reference/strictNullLogicalAndOr.symbols | 44 ++++++++ .../reference/strictNullLogicalAndOr.types | 57 ++++++++++ 4 files changed, 179 insertions(+), 52 deletions(-) create mode 100644 tests/baselines/reference/strictNullLogicalAndOr.js create mode 100644 tests/baselines/reference/strictNullLogicalAndOr.symbols create mode 100644 tests/baselines/reference/strictNullLogicalAndOr.types diff --git a/tests/baselines/reference/logicalAndOperatorStrictMode.types b/tests/baselines/reference/logicalAndOperatorStrictMode.types index ca225d4561a..a824af2f811 100644 --- a/tests/baselines/reference/logicalAndOperatorStrictMode.types +++ b/tests/baselines/reference/logicalAndOperatorStrictMode.types @@ -86,8 +86,8 @@ const a8 = a && z; >z : string | number | undefined const s1 = s && a; ->s1 : number[] | string ->s && a : number[] | string +>s1 : number[] +>s && a : number[] >s : string >a : number[] @@ -98,32 +98,32 @@ const s2 = s && s; >s : string const s3 = s && x; ->s3 : number | string ->s && x : number | string +>s3 : number +>s && x : number >s : string >x : number const s4 = s && b; ->s4 : boolean | string ->s && b : boolean | string +>s4 : boolean +>s && b : boolean >s : string >b : boolean const s5 = s && v; ->s5 : void | string ->s && v : void | string +>s5 : void +>s && v : void >s : string >v : void const s6 = s && u; ->s6 : string | undefined ->s && u : string | undefined +>s6 : undefined +>s && u : undefined >s : string >u : undefined const s7 = s && n; ->s7 : string | null ->s && n : string | null +>s7 : null +>s && n : null >s : string >n : null @@ -134,14 +134,14 @@ const s8 = s && z; >z : string | number | undefined const x1 = x && a; ->x1 : number[] | number ->x && a : number[] | number +>x1 : number[] +>x && a : number[] >x : number >a : number[] const x2 = x && s; ->x2 : string | number ->x && s : string | number +>x2 : string +>x && s : string >x : number >s : string @@ -152,26 +152,26 @@ const x3 = x && x; >x : number const x4 = x && b; ->x4 : boolean | number ->x && b : boolean | number +>x4 : boolean +>x && b : boolean >x : number >b : boolean const x5 = x && v; ->x5 : void | number ->x && v : void | number +>x5 : void +>x && v : void >x : number >v : void const x6 = x && u; ->x6 : number | undefined ->x && u : number | undefined +>x6 : undefined +>x && u : undefined >x : number >u : undefined const x7 = x && n; ->x7 : number | null ->x && n : number | null +>x7 : null +>x && n : null >x : number >n : null @@ -182,20 +182,20 @@ const x8 = x && z; >z : string | number | undefined const b1 = b && a; ->b1 : number[] | boolean ->b && a : number[] | boolean +>b1 : number[] +>b && a : number[] >b : boolean >a : number[] const b2 = b && s; ->b2 : string | boolean ->b && s : string | boolean +>b2 : string +>b && s : string >b : boolean >s : string const b3 = b && x; ->b3 : number | boolean ->b && x : number | boolean +>b3 : number +>b && x : number >b : boolean >x : number @@ -206,26 +206,26 @@ const b4 = b && b; >b : boolean const b5 = b && v; ->b5 : void | boolean ->b && v : void | boolean +>b5 : void +>b && v : void >b : boolean >v : void const b6 = b && u; ->b6 : boolean | undefined ->b && u : boolean | undefined +>b6 : undefined +>b && u : undefined >b : boolean >u : undefined const b7 = b && n; ->b7 : boolean | null ->b && n : boolean | null +>b7 : null +>b && n : null >b : boolean >n : null const b8 = b && z; ->b8 : string | number | boolean | undefined ->b && z : string | number | boolean | undefined +>b8 : string | number | undefined +>b && z : string | number | undefined >b : boolean >z : string | number | undefined @@ -374,44 +374,44 @@ const n8 = n && z; >z : string | number | undefined const z1 = z && a; ->z1 : number[] | string | number | undefined ->z && a : number[] | string | number | undefined +>z1 : number[] | undefined +>z && a : number[] | undefined >z : string | number | undefined >a : number[] const z2 = z && s; ->z2 : string | number | undefined ->z && s : string | number | undefined +>z2 : string | undefined +>z && s : string | undefined >z : string | number | undefined >s : string const z3 = z && x; ->z3 : number | string | undefined ->z && x : number | string | undefined +>z3 : number | undefined +>z && x : number | undefined >z : string | number | undefined >x : number const z4 = z && b; ->z4 : boolean | string | number | undefined ->z && b : boolean | string | number | undefined +>z4 : boolean | undefined +>z && b : boolean | undefined >z : string | number | undefined >b : boolean const z5 = z && v; ->z5 : void | string | number ->z && v : void | string | number +>z5 : void +>z && v : void >z : string | number | undefined >v : void const z6 = z && u; ->z6 : string | number | undefined ->z && u : string | number | undefined +>z6 : undefined +>z && u : undefined >z : string | number | undefined >u : undefined const z7 = z && n; ->z7 : string | number | null | undefined ->z && n : string | number | null | undefined +>z7 : null | undefined +>z && n : null | undefined >z : string | number | undefined >n : null diff --git a/tests/baselines/reference/strictNullLogicalAndOr.js b/tests/baselines/reference/strictNullLogicalAndOr.js new file mode 100644 index 00000000000..f4dafe334b5 --- /dev/null +++ b/tests/baselines/reference/strictNullLogicalAndOr.js @@ -0,0 +1,26 @@ +//// [strictNullLogicalAndOr.ts] + +// Repro from #9113 + +let sinOrCos = Math.random() < .5; +let choice = sinOrCos && Math.sin || Math.cos; + +choice(Math.PI); + +function sq(n?: number): number { + const r = n !== undefined && n*n || 0; + return r; +} + +sq(3); + +//// [strictNullLogicalAndOr.js] +// Repro from #9113 +var sinOrCos = Math.random() < .5; +var choice = sinOrCos && Math.sin || Math.cos; +choice(Math.PI); +function sq(n) { + var r = n !== undefined && n * n || 0; + return r; +} +sq(3); diff --git a/tests/baselines/reference/strictNullLogicalAndOr.symbols b/tests/baselines/reference/strictNullLogicalAndOr.symbols new file mode 100644 index 00000000000..8bb0343b9c0 --- /dev/null +++ b/tests/baselines/reference/strictNullLogicalAndOr.symbols @@ -0,0 +1,44 @@ +=== tests/cases/compiler/strictNullLogicalAndOr.ts === + +// Repro from #9113 + +let sinOrCos = Math.random() < .5; +>sinOrCos : Symbol(sinOrCos, Decl(strictNullLogicalAndOr.ts, 3, 3)) +>Math.random : Symbol(Math.random, Decl(lib.d.ts, --, --)) +>Math : Symbol(Math, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +>random : Symbol(Math.random, Decl(lib.d.ts, --, --)) + +let choice = sinOrCos && Math.sin || Math.cos; +>choice : Symbol(choice, Decl(strictNullLogicalAndOr.ts, 4, 3)) +>sinOrCos : Symbol(sinOrCos, Decl(strictNullLogicalAndOr.ts, 3, 3)) +>Math.sin : Symbol(Math.sin, Decl(lib.d.ts, --, --)) +>Math : Symbol(Math, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +>sin : Symbol(Math.sin, Decl(lib.d.ts, --, --)) +>Math.cos : Symbol(Math.cos, Decl(lib.d.ts, --, --)) +>Math : Symbol(Math, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +>cos : Symbol(Math.cos, Decl(lib.d.ts, --, --)) + +choice(Math.PI); +>choice : Symbol(choice, Decl(strictNullLogicalAndOr.ts, 4, 3)) +>Math.PI : Symbol(Math.PI, Decl(lib.d.ts, --, --)) +>Math : Symbol(Math, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +>PI : Symbol(Math.PI, Decl(lib.d.ts, --, --)) + +function sq(n?: number): number { +>sq : Symbol(sq, Decl(strictNullLogicalAndOr.ts, 6, 16)) +>n : Symbol(n, Decl(strictNullLogicalAndOr.ts, 8, 12)) + + const r = n !== undefined && n*n || 0; +>r : Symbol(r, Decl(strictNullLogicalAndOr.ts, 9, 7)) +>n : Symbol(n, Decl(strictNullLogicalAndOr.ts, 8, 12)) +>undefined : Symbol(undefined) +>n : Symbol(n, Decl(strictNullLogicalAndOr.ts, 8, 12)) +>n : Symbol(n, Decl(strictNullLogicalAndOr.ts, 8, 12)) + + return r; +>r : Symbol(r, Decl(strictNullLogicalAndOr.ts, 9, 7)) +} + +sq(3); +>sq : Symbol(sq, Decl(strictNullLogicalAndOr.ts, 6, 16)) + diff --git a/tests/baselines/reference/strictNullLogicalAndOr.types b/tests/baselines/reference/strictNullLogicalAndOr.types new file mode 100644 index 00000000000..a11a7033fb6 --- /dev/null +++ b/tests/baselines/reference/strictNullLogicalAndOr.types @@ -0,0 +1,57 @@ +=== tests/cases/compiler/strictNullLogicalAndOr.ts === + +// Repro from #9113 + +let sinOrCos = Math.random() < .5; +>sinOrCos : boolean +>Math.random() < .5 : boolean +>Math.random() : number +>Math.random : () => number +>Math : Math +>random : () => number +>.5 : number + +let choice = sinOrCos && Math.sin || Math.cos; +>choice : (x: number) => number +>sinOrCos && Math.sin || Math.cos : (x: number) => number +>sinOrCos && Math.sin : (x: number) => number +>sinOrCos : boolean +>Math.sin : (x: number) => number +>Math : Math +>sin : (x: number) => number +>Math.cos : (x: number) => number +>Math : Math +>cos : (x: number) => number + +choice(Math.PI); +>choice(Math.PI) : number +>choice : (x: number) => number +>Math.PI : number +>Math : Math +>PI : number + +function sq(n?: number): number { +>sq : (n?: number | undefined) => number +>n : number | undefined + + const r = n !== undefined && n*n || 0; +>r : number +>n !== undefined && n*n || 0 : number +>n !== undefined && n*n : number +>n !== undefined : boolean +>n : number | undefined +>undefined : undefined +>n*n : number +>n : number +>n : number +>0 : number + + return r; +>r : number +} + +sq(3); +>sq(3) : number +>sq : (n?: number | undefined) => number +>3 : number + From dd0411a2f35383a328f619d74335089d2188f883 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Wed, 15 Jun 2016 09:42:52 -0700 Subject: [PATCH 38/56] Allow property declarations in .js files --- src/compiler/diagnosticMessages.json | 4 --- src/compiler/program.ts | 15 +++++++-- .../getJavaScriptSemanticDiagnostics18.ts | 33 +++++++++++++++---- 3 files changed, 40 insertions(+), 12 deletions(-) diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index b78c342481f..46f1db97625 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -2921,10 +2921,6 @@ "category": "Error", "code": 8012 }, - "'property declarations' can only be used in a .ts file.": { - "category": "Error", - "code": 8014 - }, "'enum declarations' can only be used in a .ts file.": { "category": "Error", "code": 8015 diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 15afbe18265..111343e08ff 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -1601,8 +1601,19 @@ namespace ts { } break; case SyntaxKind.PropertyDeclaration: - diagnostics.push(createDiagnosticForNode(node, Diagnostics.property_declarations_can_only_be_used_in_a_ts_file)); - return true; + const propertyDeclaration = node; + if (propertyDeclaration.modifiers) { + for (const modifier of propertyDeclaration.modifiers) { + if (modifier.kind !== SyntaxKind.StaticKeyword) { + diagnostics.push(createDiagnosticForNode(modifier, Diagnostics._0_can_only_be_used_in_a_ts_file, tokenToString(modifier.kind))); + return true; + } + } + } + if (checkTypeAnnotation((node).type)) { + return true; + } + break; case SyntaxKind.EnumDeclaration: diagnostics.push(createDiagnosticForNode(node, Diagnostics.enum_declarations_can_only_be_used_in_a_ts_file)); return true; diff --git a/tests/cases/fourslash/getJavaScriptSemanticDiagnostics18.ts b/tests/cases/fourslash/getJavaScriptSemanticDiagnostics18.ts index fdaafa9aa51..707d1537fc5 100644 --- a/tests/cases/fourslash/getJavaScriptSemanticDiagnostics18.ts +++ b/tests/cases/fourslash/getJavaScriptSemanticDiagnostics18.ts @@ -2,14 +2,35 @@ // @allowJs: true // @Filename: a.js -//// class C { v } +////class C { +//// x; // Regular property declaration allowed +//// static y; // static allowed +//// public z; // public not allowed +////} +goTo.file("a.js"); verify.getSemanticDiagnostics(`[ { - "message": "'property declarations' can only be used in a .ts file.", - "start": 10, - "length": 1, + "message": "\'public\' can only be used in a .ts file.", + "start": 93, + "length": 6, "category": "error", - "code": 8014 + "code": 8009 } -]`); \ No newline at end of file +]`); + +// @Filename: b.js +////class C { +//// x: number; // Types not allowed +////} + +goTo.file("b.js"); +verify.getSemanticDiagnostics(`[ + { + "message": "'types' can only be used in a .ts file.", + "start": 17, + "length": 6, + "category": "error", + "code": 8010 + } +]`); From 9b6472aec0efd2fbf7ed185d5f49a580c1ee45ed Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Wed, 15 Jun 2016 11:00:27 -0700 Subject: [PATCH 39/56] Remove old test --- .../jsFileCompilationPropertySyntaxOfClass.errors.txt | 9 --------- .../compiler/jsFileCompilationPropertySyntaxOfClass.ts | 3 --- 2 files changed, 12 deletions(-) delete mode 100644 tests/baselines/reference/jsFileCompilationPropertySyntaxOfClass.errors.txt delete mode 100644 tests/cases/compiler/jsFileCompilationPropertySyntaxOfClass.ts diff --git a/tests/baselines/reference/jsFileCompilationPropertySyntaxOfClass.errors.txt b/tests/baselines/reference/jsFileCompilationPropertySyntaxOfClass.errors.txt deleted file mode 100644 index fd5ea666459..00000000000 --- a/tests/baselines/reference/jsFileCompilationPropertySyntaxOfClass.errors.txt +++ /dev/null @@ -1,9 +0,0 @@ -error TS5055: Cannot write file 'tests/cases/compiler/a.js' because it would overwrite input file. -tests/cases/compiler/a.js(1,11): error TS8014: 'property declarations' can only be used in a .ts file. - - -!!! error TS5055: Cannot write file 'tests/cases/compiler/a.js' because it would overwrite input file. -==== tests/cases/compiler/a.js (1 errors) ==== - class C { v } - ~ -!!! error TS8014: 'property declarations' can only be used in a .ts file. \ No newline at end of file diff --git a/tests/cases/compiler/jsFileCompilationPropertySyntaxOfClass.ts b/tests/cases/compiler/jsFileCompilationPropertySyntaxOfClass.ts deleted file mode 100644 index 62a0058bcef..00000000000 --- a/tests/cases/compiler/jsFileCompilationPropertySyntaxOfClass.ts +++ /dev/null @@ -1,3 +0,0 @@ -// @allowJs: true -// @filename: a.js -class C { v } \ No newline at end of file From 95ddfc7efc788c14900dc20da03f2b805e495854 Mon Sep 17 00:00:00 2001 From: Mohamed Hegazy Date: Wed, 15 Jun 2016 11:38:28 -0700 Subject: [PATCH 40/56] Do not use Object.assing in test --- tests/cases/unittests/transpile.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/tests/cases/unittests/transpile.ts b/tests/cases/unittests/transpile.ts index c8025bfc0a2..6c88c2558fc 100644 --- a/tests/cases/unittests/transpile.ts +++ b/tests/cases/unittests/transpile.ts @@ -1,9 +1,5 @@ /// -interface ObjectConstructor { - assign(target: T, source: U): T & U; -} - namespace ts { describe("Transpile", () => { @@ -102,7 +98,7 @@ namespace ts { test(input, { expectedOutput: output, options: { - compilerOptions: Object.assign({ module: ModuleKind.CommonJS, newLine: NewLineKind.CarriageReturnLineFeed }, options), + compilerOptions: extend(options, { module: ModuleKind.CommonJS, newLine: NewLineKind.CarriageReturnLineFeed }), fileName: "input.js", reportDiagnostics: true } From ce45ee797c919b914ce65f4e5ef1065696d04662 Mon Sep 17 00:00:00 2001 From: Mohamed Hegazy Date: Wed, 15 Jun 2016 12:23:00 -0700 Subject: [PATCH 41/56] Fix comment --- src/services/services.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/services/services.ts b/src/services/services.ts index 19167a5f6e0..62a50db65ef 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -2001,7 +2001,7 @@ namespace ts { // so pass --noLib to avoid reporting a file not found error. options.noLib = true; - // Clear out other settings that would not be participate in transpiling this module + // Clear out other settings that would not be used in transpiling this module options.lib = undefined; options.types = undefined; options.noEmit = undefined; From 550d91249b1c4c72b6b0eef1fa275794c75b9faa Mon Sep 17 00:00:00 2001 From: zhengbli Date: Wed, 15 Jun 2016 14:40:29 -0700 Subject: [PATCH 42/56] Refactor code to make if statements cheaper --- src/server/editorServices.ts | 7 ++----- src/server/session.ts | 6 +++--- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/server/editorServices.ts b/src/server/editorServices.ts index 615e843dcac..346b2e00a10 100644 --- a/src/server/editorServices.ts +++ b/src/server/editorServices.ts @@ -475,10 +475,7 @@ namespace ts.server { isRoot(info: ScriptInfo) { if (this.languageServiceDiabled) { - if (!this.projectOptions) { - return undefined; - } - return forEach(this.projectOptions.files, file => toPath(file, file, createGetCanonicalFileName(this.projectService.host.useCaseSensitiveFileNames)) === info.path); + return undefined; } return this.compilerService.host.roots.some(root => root === info); @@ -1421,7 +1418,7 @@ namespace ts.server { return errors; } else { - if (this.exceedTotalNonTsFileSizeLimit(projectOptions.files) && projectOptions.compilerOptions && !projectOptions.compilerOptions.disableSizeLimit) { + if (projectOptions.compilerOptions && !projectOptions.compilerOptions.disableSizeLimit && this.exceedTotalNonTsFileSizeLimit(projectOptions.files)) { project.setProjectOptions(projectOptions); if (project.languageServiceDiabled) { return; diff --git a/src/server/session.ts b/src/server/session.ts index 75abd946515..65540082f60 100644 --- a/src/server/session.ts +++ b/src/server/session.ts @@ -443,7 +443,7 @@ namespace ts.server { const info = this.projectService.getScriptInfo(file); const projects = this.projectService.findReferencingProjects(info); const projectsWithLanguageServiceEnabeld = ts.filter(projects, p => !p.languageServiceDiabled); - if (projects.length === 0 || projectsWithLanguageServiceEnabeld.length === 0) { + if (projectsWithLanguageServiceEnabeld.length === 0) { throw Errors.NoProject; } @@ -526,7 +526,7 @@ namespace ts.server { const info = this.projectService.getScriptInfo(file); const projects = this.projectService.findReferencingProjects(info); const projectsWithLanguageServiceEnabeld = ts.filter(projects, p => !p.languageServiceDiabled); - if (projects.length === 0 || projectsWithLanguageServiceEnabeld.length === 0) { + if (projectsWithLanguageServiceEnabeld.length === 0) { throw Errors.NoProject; } @@ -904,7 +904,7 @@ namespace ts.server { const info = this.projectService.getScriptInfo(file); const projects = this.projectService.findReferencingProjects(info); const projectsWithLanguageServiceEnabeld = ts.filter(projects, p => !p.languageServiceDiabled); - if (projects.length === 0 || projectsWithLanguageServiceEnabeld.length === 0) { + if (projectsWithLanguageServiceEnabeld.length === 0) { throw Errors.NoProject; } From c721b5f981de8ca1bd6e7b71fb5f17d9368d996f Mon Sep 17 00:00:00 2001 From: Herrington Darkholme Date: Fri, 17 Jun 2016 01:19:34 +0800 Subject: [PATCH 43/56] add tests & add branches for module interface --- src/compiler/checker.ts | 35 ++++++++++++------- tests/cases/compiler/classExtendsInterface.ts | 23 +++++++++++- 2 files changed, 44 insertions(+), 14 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index d5f89bee8bf..b0484fc890c 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -853,7 +853,7 @@ namespace ts { if (!result) { if (nameNotFoundMessage) { if (!checkAndReportErrorForMissingPrefix(errorLocation, name, nameArg) && - !checkAndReportErrorForExtendingInterface(errorLocation, name)) { + !checkAndReportErrorForExtendingInterface(errorLocation)) { error(errorLocation, nameNotFoundMessage, typeof nameArg === "string" ? nameArg : declarationNameToString(nameArg)); } } @@ -938,21 +938,30 @@ namespace ts { } - function checkAndReportErrorForExtendingInterface(errorLocation: Node, name: string): boolean { - if (!errorLocation || errorLocation.kind !== SyntaxKind.Identifier || - !errorLocation.parent || !errorLocation.parent.parent || - errorLocation.parent.parent.kind !== SyntaxKind.HeritageClause) { + function checkAndReportErrorForExtendingInterface(errorLocation: Node): boolean { + const container = getContainingClass(errorLocation); + const heritageClause = getAncestor(errorLocation, SyntaxKind.HeritageClause); + if (!container || !heritageClause || heritageClause.token !== SyntaxKind.ExtendsKeyword) { return false; } - const heritageClause = errorLocation.parent.parent; - if (heritageClause.token !== SyntaxKind.ExtendsKeyword) { - return false; + if (errorLocation.kind === SyntaxKind.Identifier) { + const name = (errorLocation).text; + const interfaceOrModule = resolveName( + errorLocation, name, + SymbolFlags.Interface | SymbolFlags.HasExports, + /*errorMessage*/ undefined, /*nameArg*/ undefined) + if (!interfaceOrModule) { + return false; + } + if (interfaceOrModule.flags & SymbolFlags.Interface) { + error(errorLocation, Diagnostics.Cannot_extend_an_interface_0_Did_you_mean_implements, name); + return true; + } } - const enclosingScope = heritageClause.parent.parent.locals; - if (enclosingScope && getSymbol(enclosingScope, name, SymbolFlags.Interface)) { - error(errorLocation, Diagnostics.Cannot_extend_an_interface_0_Did_you_mean_implements, name); - return true; + else if (errorLocation.kind === SyntaxKind.PropertyAccessExpression) { + // todo } + return false; } @@ -10058,7 +10067,7 @@ namespace ts { } const prop = getPropertyOfType(apparentType, right.text); if (!prop) { - if (right.text) { + if (right.text && !checkAndReportErrorForExtendingInterface(node)) { error(right, Diagnostics.Property_0_does_not_exist_on_type_1, declarationNameToString(right), typeToString(type.flags & TypeFlags.ThisType ? apparentType : type)); } return unknownType; diff --git a/tests/cases/compiler/classExtendsInterface.ts b/tests/cases/compiler/classExtendsInterface.ts index 36f42167d1a..883680619ed 100644 --- a/tests/cases/compiler/classExtendsInterface.ts +++ b/tests/cases/compiler/classExtendsInterface.ts @@ -1,7 +1,28 @@ interface Comparable {} class A extends Comparable {} class B implements Comparable {} - + interface Comparable2 {} class A2 extends Comparable2 {} class B2 implements Comparable2 {} + +function Factory(a: any): {new()} { + return null +} + +class C extends Factory(Comparable) {} + +module M { + export interface I1 {} + export interface I2 {} +} +class C1 extends M.I1 {} +class C2 extends M.I2 {} + +namespace N { + export interface I1 {} + export interface I2 {} +} + +class D1 extends N.I1 {} +class D2 extends N.I2 {} From 0e6f8eb2bca776acdb11a6cbf45b37b2d195650a Mon Sep 17 00:00:00 2001 From: Yui Date: Thu, 16 Jun 2016 10:50:01 -0700 Subject: [PATCH 44/56] Using baselines for transpile unittests (#9195) * Conver to Transpile unittest to use baselines instead * Add baselines * Fix linting error --- ...ring as enum values for compile-options.js | 2 + .../Does not generate semantic diagnostics.js | 3 + ... expected syntactic diagnostics.errors.txt | 7 + ...enerates expected syntactic diagnostics.js | 4 + .../transpile/Generates module output.js | 5 + ...diagnostics for missing file references.js | 4 + ... diagnostics for missing module imports.js | 2 + ...erates no diagnostics with valid inputs.js | 3 + ...extra errors for file without extension.js | 3 + .../transpile/Rename dependencies - AMD.js | 5 + .../transpile/Rename dependencies - System.js | 15 + .../transpile/Rename dependencies - UMD.js | 13 + ...r-options input is empty object.errors.txt | 6 + ... compiler-options input is empty object.js | 2 + ...r-options input is empty string.errors.txt | 6 + ... compiler-options input is empty string.js | 2 + ...ons module-kind is out-of-range.errors.txt | 6 + ...ler-options module-kind is out-of-range.js | 2 + ...s target-script is out-of-range.errors.txt | 6 + ...r-options target-script is out-of-range.js | 2 + .../reference/transpile/Sets module name.js | 12 + .../Support options with lib values.js | 3 + .../Support options with types values.js | 3 + .../Supports backslashes in file name.js | 3 + .../transpile/Supports urls in file name.js | 3 + ... with emit decorators and emit metadata.js | 19 + .../Uses correct newLine character.js | 3 + .../transpile/transpile .js files.js | 3 + ...anspile file as tsx if jsx is specified.js | 3 + tests/cases/unittests/transpile.ts | 527 +++++++----------- 30 files changed, 362 insertions(+), 315 deletions(-) create mode 100644 tests/baselines/reference/transpile/Accepts string as enum values for compile-options.js create mode 100644 tests/baselines/reference/transpile/Does not generate semantic diagnostics.js create mode 100644 tests/baselines/reference/transpile/Generates expected syntactic diagnostics.errors.txt create mode 100644 tests/baselines/reference/transpile/Generates expected syntactic diagnostics.js create mode 100644 tests/baselines/reference/transpile/Generates module output.js create mode 100644 tests/baselines/reference/transpile/Generates no diagnostics for missing file references.js create mode 100644 tests/baselines/reference/transpile/Generates no diagnostics for missing module imports.js create mode 100644 tests/baselines/reference/transpile/Generates no diagnostics with valid inputs.js create mode 100644 tests/baselines/reference/transpile/No extra errors for file without extension.js create mode 100644 tests/baselines/reference/transpile/Rename dependencies - AMD.js create mode 100644 tests/baselines/reference/transpile/Rename dependencies - System.js create mode 100644 tests/baselines/reference/transpile/Rename dependencies - UMD.js create mode 100644 tests/baselines/reference/transpile/Report an error when compiler-options input is empty object.errors.txt create mode 100644 tests/baselines/reference/transpile/Report an error when compiler-options input is empty object.js create mode 100644 tests/baselines/reference/transpile/Report an error when compiler-options input is empty string.errors.txt create mode 100644 tests/baselines/reference/transpile/Report an error when compiler-options input is empty string.js create mode 100644 tests/baselines/reference/transpile/Report an error when compiler-options module-kind is out-of-range.errors.txt create mode 100644 tests/baselines/reference/transpile/Report an error when compiler-options module-kind is out-of-range.js create mode 100644 tests/baselines/reference/transpile/Report an error when compiler-options target-script is out-of-range.errors.txt create mode 100644 tests/baselines/reference/transpile/Report an error when compiler-options target-script is out-of-range.js create mode 100644 tests/baselines/reference/transpile/Sets module name.js create mode 100644 tests/baselines/reference/transpile/Support options with lib values.js create mode 100644 tests/baselines/reference/transpile/Support options with types values.js create mode 100644 tests/baselines/reference/transpile/Supports backslashes in file name.js create mode 100644 tests/baselines/reference/transpile/Supports urls in file name.js create mode 100644 tests/baselines/reference/transpile/Transpile with emit decorators and emit metadata.js create mode 100644 tests/baselines/reference/transpile/Uses correct newLine character.js create mode 100644 tests/baselines/reference/transpile/transpile .js files.js create mode 100644 tests/baselines/reference/transpile/transpile file as tsx if jsx is specified.js diff --git a/tests/baselines/reference/transpile/Accepts string as enum values for compile-options.js b/tests/baselines/reference/transpile/Accepts string as enum values for compile-options.js new file mode 100644 index 00000000000..981e2e706cb --- /dev/null +++ b/tests/baselines/reference/transpile/Accepts string as enum values for compile-options.js @@ -0,0 +1,2 @@ +export const x = 0; +//# sourceMappingURL=file.js.map \ No newline at end of file diff --git a/tests/baselines/reference/transpile/Does not generate semantic diagnostics.js b/tests/baselines/reference/transpile/Does not generate semantic diagnostics.js new file mode 100644 index 00000000000..61a703e13bb --- /dev/null +++ b/tests/baselines/reference/transpile/Does not generate semantic diagnostics.js @@ -0,0 +1,3 @@ +"use strict"; +var x = 0; +//# sourceMappingURL=file.js.map \ No newline at end of file diff --git a/tests/baselines/reference/transpile/Generates expected syntactic diagnostics.errors.txt b/tests/baselines/reference/transpile/Generates expected syntactic diagnostics.errors.txt new file mode 100644 index 00000000000..6fbdba6f2c6 --- /dev/null +++ b/tests/baselines/reference/transpile/Generates expected syntactic diagnostics.errors.txt @@ -0,0 +1,7 @@ +file.ts(1,3): error TS1005: ';' expected. + + +==== file.ts (1 errors) ==== + a b + ~ +!!! error TS1005: ';' expected. \ No newline at end of file diff --git a/tests/baselines/reference/transpile/Generates expected syntactic diagnostics.js b/tests/baselines/reference/transpile/Generates expected syntactic diagnostics.js new file mode 100644 index 00000000000..9d108d63313 --- /dev/null +++ b/tests/baselines/reference/transpile/Generates expected syntactic diagnostics.js @@ -0,0 +1,4 @@ +"use strict"; +a; +b; +//# sourceMappingURL=file.js.map \ No newline at end of file diff --git a/tests/baselines/reference/transpile/Generates module output.js b/tests/baselines/reference/transpile/Generates module output.js new file mode 100644 index 00000000000..9eadd1f2717 --- /dev/null +++ b/tests/baselines/reference/transpile/Generates module output.js @@ -0,0 +1,5 @@ +define(["require", "exports"], function (require, exports) { + "use strict"; + var x = 0; +}); +//# sourceMappingURL=file.js.map \ No newline at end of file diff --git a/tests/baselines/reference/transpile/Generates no diagnostics for missing file references.js b/tests/baselines/reference/transpile/Generates no diagnostics for missing file references.js new file mode 100644 index 00000000000..88d98628eee --- /dev/null +++ b/tests/baselines/reference/transpile/Generates no diagnostics for missing file references.js @@ -0,0 +1,4 @@ +"use strict"; +/// +var x = 0; +//# sourceMappingURL=file.js.map \ No newline at end of file diff --git a/tests/baselines/reference/transpile/Generates no diagnostics for missing module imports.js b/tests/baselines/reference/transpile/Generates no diagnostics for missing module imports.js new file mode 100644 index 00000000000..1ceb1bcd146 --- /dev/null +++ b/tests/baselines/reference/transpile/Generates no diagnostics for missing module imports.js @@ -0,0 +1,2 @@ +"use strict"; +//# sourceMappingURL=file.js.map \ No newline at end of file diff --git a/tests/baselines/reference/transpile/Generates no diagnostics with valid inputs.js b/tests/baselines/reference/transpile/Generates no diagnostics with valid inputs.js new file mode 100644 index 00000000000..61a703e13bb --- /dev/null +++ b/tests/baselines/reference/transpile/Generates no diagnostics with valid inputs.js @@ -0,0 +1,3 @@ +"use strict"; +var x = 0; +//# sourceMappingURL=file.js.map \ No newline at end of file diff --git a/tests/baselines/reference/transpile/No extra errors for file without extension.js b/tests/baselines/reference/transpile/No extra errors for file without extension.js new file mode 100644 index 00000000000..61a703e13bb --- /dev/null +++ b/tests/baselines/reference/transpile/No extra errors for file without extension.js @@ -0,0 +1,3 @@ +"use strict"; +var x = 0; +//# sourceMappingURL=file.js.map \ No newline at end of file diff --git a/tests/baselines/reference/transpile/Rename dependencies - AMD.js b/tests/baselines/reference/transpile/Rename dependencies - AMD.js new file mode 100644 index 00000000000..a0dd948c9fc --- /dev/null +++ b/tests/baselines/reference/transpile/Rename dependencies - AMD.js @@ -0,0 +1,5 @@ +define(["require", "exports", "SomeOtherName"], function (require, exports, SomeName_1) { + "use strict"; + use(SomeName_1.foo); +}); +//# sourceMappingURL=file.js.map \ No newline at end of file diff --git a/tests/baselines/reference/transpile/Rename dependencies - System.js b/tests/baselines/reference/transpile/Rename dependencies - System.js new file mode 100644 index 00000000000..8f3fcaa8559 --- /dev/null +++ b/tests/baselines/reference/transpile/Rename dependencies - System.js @@ -0,0 +1,15 @@ +System.register(["SomeOtherName"], function(exports_1, context_1) { + "use strict"; + var __moduleName = context_1 && context_1.id; + var SomeName_1; + return { + setters:[ + function (SomeName_1_1) { + SomeName_1 = SomeName_1_1; + }], + execute: function() { + use(SomeName_1.foo); + } + } +}); +//# sourceMappingURL=file.js.map \ No newline at end of file diff --git a/tests/baselines/reference/transpile/Rename dependencies - UMD.js b/tests/baselines/reference/transpile/Rename dependencies - UMD.js new file mode 100644 index 00000000000..94a7249678f --- /dev/null +++ b/tests/baselines/reference/transpile/Rename dependencies - UMD.js @@ -0,0 +1,13 @@ +(function (factory) { + if (typeof module === 'object' && typeof module.exports === 'object') { + var v = factory(require, exports); if (v !== undefined) module.exports = v; + } + else if (typeof define === 'function' && define.amd) { + define(["require", "exports", "SomeOtherName"], factory); + } +})(function (require, exports) { + "use strict"; + var SomeName_1 = require("SomeOtherName"); + use(SomeName_1.foo); +}); +//# sourceMappingURL=file.js.map \ No newline at end of file diff --git a/tests/baselines/reference/transpile/Report an error when compiler-options input is empty object.errors.txt b/tests/baselines/reference/transpile/Report an error when compiler-options input is empty object.errors.txt new file mode 100644 index 00000000000..d7d6eb69300 --- /dev/null +++ b/tests/baselines/reference/transpile/Report an error when compiler-options input is empty object.errors.txt @@ -0,0 +1,6 @@ +error TS6046: Argument for '--module' option must be: 'none', 'commonjs', 'amd', 'system', 'umd', 'es6', 'es2015' + + +!!! error TS6046: Argument for '--module' option must be: 'none', 'commonjs', 'amd', 'system', 'umd', 'es6', 'es2015' +==== file.ts (0 errors) ==== + \ No newline at end of file diff --git a/tests/baselines/reference/transpile/Report an error when compiler-options input is empty object.js b/tests/baselines/reference/transpile/Report an error when compiler-options input is empty object.js new file mode 100644 index 00000000000..1ceb1bcd146 --- /dev/null +++ b/tests/baselines/reference/transpile/Report an error when compiler-options input is empty object.js @@ -0,0 +1,2 @@ +"use strict"; +//# sourceMappingURL=file.js.map \ No newline at end of file diff --git a/tests/baselines/reference/transpile/Report an error when compiler-options input is empty string.errors.txt b/tests/baselines/reference/transpile/Report an error when compiler-options input is empty string.errors.txt new file mode 100644 index 00000000000..d7d6eb69300 --- /dev/null +++ b/tests/baselines/reference/transpile/Report an error when compiler-options input is empty string.errors.txt @@ -0,0 +1,6 @@ +error TS6046: Argument for '--module' option must be: 'none', 'commonjs', 'amd', 'system', 'umd', 'es6', 'es2015' + + +!!! error TS6046: Argument for '--module' option must be: 'none', 'commonjs', 'amd', 'system', 'umd', 'es6', 'es2015' +==== file.ts (0 errors) ==== + \ No newline at end of file diff --git a/tests/baselines/reference/transpile/Report an error when compiler-options input is empty string.js b/tests/baselines/reference/transpile/Report an error when compiler-options input is empty string.js new file mode 100644 index 00000000000..1ceb1bcd146 --- /dev/null +++ b/tests/baselines/reference/transpile/Report an error when compiler-options input is empty string.js @@ -0,0 +1,2 @@ +"use strict"; +//# sourceMappingURL=file.js.map \ No newline at end of file diff --git a/tests/baselines/reference/transpile/Report an error when compiler-options module-kind is out-of-range.errors.txt b/tests/baselines/reference/transpile/Report an error when compiler-options module-kind is out-of-range.errors.txt new file mode 100644 index 00000000000..d7d6eb69300 --- /dev/null +++ b/tests/baselines/reference/transpile/Report an error when compiler-options module-kind is out-of-range.errors.txt @@ -0,0 +1,6 @@ +error TS6046: Argument for '--module' option must be: 'none', 'commonjs', 'amd', 'system', 'umd', 'es6', 'es2015' + + +!!! error TS6046: Argument for '--module' option must be: 'none', 'commonjs', 'amd', 'system', 'umd', 'es6', 'es2015' +==== file.ts (0 errors) ==== + \ No newline at end of file diff --git a/tests/baselines/reference/transpile/Report an error when compiler-options module-kind is out-of-range.js b/tests/baselines/reference/transpile/Report an error when compiler-options module-kind is out-of-range.js new file mode 100644 index 00000000000..1ceb1bcd146 --- /dev/null +++ b/tests/baselines/reference/transpile/Report an error when compiler-options module-kind is out-of-range.js @@ -0,0 +1,2 @@ +"use strict"; +//# sourceMappingURL=file.js.map \ No newline at end of file diff --git a/tests/baselines/reference/transpile/Report an error when compiler-options target-script is out-of-range.errors.txt b/tests/baselines/reference/transpile/Report an error when compiler-options target-script is out-of-range.errors.txt new file mode 100644 index 00000000000..d7d6eb69300 --- /dev/null +++ b/tests/baselines/reference/transpile/Report an error when compiler-options target-script is out-of-range.errors.txt @@ -0,0 +1,6 @@ +error TS6046: Argument for '--module' option must be: 'none', 'commonjs', 'amd', 'system', 'umd', 'es6', 'es2015' + + +!!! error TS6046: Argument for '--module' option must be: 'none', 'commonjs', 'amd', 'system', 'umd', 'es6', 'es2015' +==== file.ts (0 errors) ==== + \ No newline at end of file diff --git a/tests/baselines/reference/transpile/Report an error when compiler-options target-script is out-of-range.js b/tests/baselines/reference/transpile/Report an error when compiler-options target-script is out-of-range.js new file mode 100644 index 00000000000..1ceb1bcd146 --- /dev/null +++ b/tests/baselines/reference/transpile/Report an error when compiler-options target-script is out-of-range.js @@ -0,0 +1,2 @@ +"use strict"; +//# sourceMappingURL=file.js.map \ No newline at end of file diff --git a/tests/baselines/reference/transpile/Sets module name.js b/tests/baselines/reference/transpile/Sets module name.js new file mode 100644 index 00000000000..683266f8383 --- /dev/null +++ b/tests/baselines/reference/transpile/Sets module name.js @@ -0,0 +1,12 @@ +System.register("NamedModule", [], function(exports_1, context_1) { + "use strict"; + var __moduleName = context_1 && context_1.id; + var x; + return { + setters:[], + execute: function() { + var x = 1; + } + } +}); +//# sourceMappingURL=file.js.map \ No newline at end of file diff --git a/tests/baselines/reference/transpile/Support options with lib values.js b/tests/baselines/reference/transpile/Support options with lib values.js new file mode 100644 index 00000000000..36c68f08b9f --- /dev/null +++ b/tests/baselines/reference/transpile/Support options with lib values.js @@ -0,0 +1,3 @@ +"use strict"; +var a = 10; +//# sourceMappingURL=input.js.map \ No newline at end of file diff --git a/tests/baselines/reference/transpile/Support options with types values.js b/tests/baselines/reference/transpile/Support options with types values.js new file mode 100644 index 00000000000..36c68f08b9f --- /dev/null +++ b/tests/baselines/reference/transpile/Support options with types values.js @@ -0,0 +1,3 @@ +"use strict"; +var a = 10; +//# sourceMappingURL=input.js.map \ No newline at end of file diff --git a/tests/baselines/reference/transpile/Supports backslashes in file name.js b/tests/baselines/reference/transpile/Supports backslashes in file name.js new file mode 100644 index 00000000000..942449753b0 --- /dev/null +++ b/tests/baselines/reference/transpile/Supports backslashes in file name.js @@ -0,0 +1,3 @@ +"use strict"; +var x; +//# sourceMappingURL=b.js.map \ No newline at end of file diff --git a/tests/baselines/reference/transpile/Supports urls in file name.js b/tests/baselines/reference/transpile/Supports urls in file name.js new file mode 100644 index 00000000000..3923d3c9a41 --- /dev/null +++ b/tests/baselines/reference/transpile/Supports urls in file name.js @@ -0,0 +1,3 @@ +"use strict"; +var x; +//# sourceMappingURL=file.js.map \ No newline at end of file diff --git a/tests/baselines/reference/transpile/Transpile with emit decorators and emit metadata.js b/tests/baselines/reference/transpile/Transpile with emit decorators and emit metadata.js new file mode 100644 index 00000000000..407ee216d58 --- /dev/null +++ b/tests/baselines/reference/transpile/Transpile with emit decorators and emit metadata.js @@ -0,0 +1,19 @@ +"use strict"; +var db_1 = require('./db'); +function someDecorator(target) { + return target; +} +var MyClass = (function () { + function MyClass(db) { + this.db = db; + this.db.doSomething(); + } + MyClass = __decorate([ + someDecorator, + __metadata('design:paramtypes', [(typeof (_a = typeof db_1.db !== 'undefined' && db_1.db) === 'function' && _a) || Object]) + ], MyClass); + return MyClass; + var _a; +}()); +exports.MyClass = MyClass; +//# sourceMappingURL=file.js.map \ No newline at end of file diff --git a/tests/baselines/reference/transpile/Uses correct newLine character.js b/tests/baselines/reference/transpile/Uses correct newLine character.js new file mode 100644 index 00000000000..bab9c3c4443 --- /dev/null +++ b/tests/baselines/reference/transpile/Uses correct newLine character.js @@ -0,0 +1,3 @@ +"use strict"; +var x = 0; +//# sourceMappingURL=file.js.map \ No newline at end of file diff --git a/tests/baselines/reference/transpile/transpile .js files.js b/tests/baselines/reference/transpile/transpile .js files.js new file mode 100644 index 00000000000..c17099d84ba --- /dev/null +++ b/tests/baselines/reference/transpile/transpile .js files.js @@ -0,0 +1,3 @@ +"use strict"; +var a = 10; +//# sourceMappingURL=input.js.map \ No newline at end of file diff --git a/tests/baselines/reference/transpile/transpile file as tsx if jsx is specified.js b/tests/baselines/reference/transpile/transpile file as tsx if jsx is specified.js new file mode 100644 index 00000000000..baa27ee64ce --- /dev/null +++ b/tests/baselines/reference/transpile/transpile file as tsx if jsx is specified.js @@ -0,0 +1,3 @@ +"use strict"; +var x = React.createElement("div", null); +//# sourceMappingURL=file.js.map \ No newline at end of file diff --git a/tests/cases/unittests/transpile.ts b/tests/cases/unittests/transpile.ts index cac44994420..3f29002e6a2 100644 --- a/tests/cases/unittests/transpile.ts +++ b/tests/cases/unittests/transpile.ts @@ -5,346 +5,243 @@ namespace ts { interface TranspileTestSettings { options?: TranspileOptions; - expectedOutput?: string; - expectedDiagnosticCodes?: number[]; - expectedDiagnosticTexts?: string[]; } - function checkDiagnostics(diagnostics: Diagnostic[], expectedDiagnosticCodes: number[] = [], expectedDiagnosticTexts?: string[]) { - const n = expectedDiagnosticCodes.length; - if (expectedDiagnosticTexts) { - assert.equal(n, expectedDiagnosticTexts.length); - } - for (let i = 0; i < n; i++) { - assert.equal(expectedDiagnosticCodes[i], diagnostics[i] && diagnostics[i].code, `Could not find expected diagnostic.`); - if (expectedDiagnosticTexts) { - assert.equal(expectedDiagnosticTexts[i], diagnostics[i] && diagnostics[i].messageText); - } - }; - assert.equal(diagnostics.length, n, "Resuting diagnostics count does not match expected"); - } + function transpilesCorrectly(name: string, input: string, testSettings: TranspileTestSettings) { + describe(name, () => { + let justName: string; + let transpileOptions: TranspileOptions; + let canUseOldTranspile: boolean; + let toBeCompiled: Harness.Compiler.TestFile[]; + let transpileResult: TranspileOutput; + let oldTranspileResult: string; + let oldTranspileDiagnostics: Diagnostic[]; - function test(input: string, testSettings: TranspileTestSettings): void { + before(() => { + transpileOptions = testSettings.options || {}; + if (!transpileOptions.compilerOptions) { + transpileOptions.compilerOptions = {}; + } - const transpileOptions: TranspileOptions = testSettings.options || {}; - if (!transpileOptions.compilerOptions) { - transpileOptions.compilerOptions = {}; - } - if (transpileOptions.compilerOptions.newLine === undefined) { - // use \r\n as default new line - transpileOptions.compilerOptions.newLine = ts.NewLineKind.CarriageReturnLineFeed; - } + if (transpileOptions.compilerOptions.newLine === undefined) { + // use \r\n as default new line + transpileOptions.compilerOptions.newLine = ts.NewLineKind.CarriageReturnLineFeed; + } - const canUseOldTranspile = !transpileOptions.renamedDependencies; + transpileOptions.compilerOptions.sourceMap = true; - transpileOptions.reportDiagnostics = true; - const transpileModuleResult = transpileModule(input, transpileOptions); + if (!transpileOptions.fileName) { + transpileOptions.fileName = transpileOptions.compilerOptions.jsx ? "file.tsx" : "file.ts"; + } - checkDiagnostics(transpileModuleResult.diagnostics, testSettings.expectedDiagnosticCodes, testSettings.expectedDiagnosticTexts); + transpileOptions.reportDiagnostics = true; - if (testSettings.expectedOutput !== undefined) { - assert.equal(transpileModuleResult.outputText, testSettings.expectedOutput); - } + justName = "transpile/" + name.replace(/[^a-z0-9\-. ]/ig, "") + (transpileOptions.compilerOptions.jsx ? ".tsx" : ".ts"); + toBeCompiled = [{ + unitName: transpileOptions.fileName, + content: input + }]; - if (canUseOldTranspile) { - const diagnostics: Diagnostic[] = []; - const transpileResult = transpile(input, transpileOptions.compilerOptions, transpileOptions.fileName, diagnostics, transpileOptions.moduleName); - checkDiagnostics(diagnostics, testSettings.expectedDiagnosticCodes, testSettings.expectedDiagnosticTexts); - if (testSettings.expectedOutput) { - assert.equal(transpileResult, testSettings.expectedOutput); - } - } + canUseOldTranspile = !transpileOptions.renamedDependencies; + transpileResult = transpileModule(input, transpileOptions); - // check source maps - if (!transpileOptions.compilerOptions) { - transpileOptions.compilerOptions = {}; - } - - if (!transpileOptions.fileName) { - transpileOptions.fileName = transpileOptions.compilerOptions.jsx ? "file.tsx" : "file.ts"; - } - - transpileOptions.compilerOptions.sourceMap = true; - const transpileModuleResultWithSourceMap = transpileModule(input, transpileOptions); - assert.isTrue(transpileModuleResultWithSourceMap.sourceMapText !== undefined); - - const expectedSourceMapFileName = removeFileExtension(getBaseFileName(normalizeSlashes(transpileOptions.fileName))) + ".js.map"; - const expectedSourceMappingUrlLine = `//# sourceMappingURL=${expectedSourceMapFileName}`; - - if (testSettings.expectedOutput !== undefined) { - assert.equal(transpileModuleResultWithSourceMap.outputText, testSettings.expectedOutput + expectedSourceMappingUrlLine); - } - else { - // expected output is not set, just verify that output text has sourceMappingURL as a last line - const output = transpileModuleResultWithSourceMap.outputText; - assert.isTrue(output.length >= expectedSourceMappingUrlLine.length); - if (output.length === expectedSourceMappingUrlLine.length) { - assert.equal(output, expectedSourceMappingUrlLine); - } - else { - const suffix = getNewLineCharacter(transpileOptions.compilerOptions) + expectedSourceMappingUrlLine; - assert.isTrue(output.indexOf(suffix, output.length - suffix.length) !== -1); - } - } - - } - - it("Generates no diagnostics with valid inputs", () => { - // No errors - test(`var x = 0;`, { options: { compilerOptions: { module: ModuleKind.CommonJS } } }); - }); - - it("Generates no diagnostics for missing file references", () => { - test(`/// -var x = 0;`, - { options: { compilerOptions: { module: ModuleKind.CommonJS } } }); - }); - - it("Generates no diagnostics for missing module imports", () => { - test(`import {a} from "module2";`, - { options: { compilerOptions: { module: ModuleKind.CommonJS } } }); - }); - - it("Generates expected syntactic diagnostics", () => { - test(`a b`, - { options: { compilerOptions: { module: ModuleKind.CommonJS } }, expectedDiagnosticCodes: [1005] }); /// 1005: ';' Expected - }); - - it("Does not generate semantic diagnostics", () => { - test(`var x: string = 0;`, - { options: { compilerOptions: { module: ModuleKind.CommonJS } } }); - }); - - it("Generates module output", () => { - test(`var x = 0;`, - { - options: { compilerOptions: { module: ModuleKind.AMD } }, - expectedOutput: `define(["require", "exports"], function (require, exports) {\r\n "use strict";\r\n var x = 0;\r\n});\r\n` + if (canUseOldTranspile) { + oldTranspileDiagnostics = []; + oldTranspileResult = transpile(input, transpileOptions.compilerOptions, transpileOptions.fileName, oldTranspileDiagnostics, transpileOptions.moduleName); + } }); - }); - it("Uses correct newLine character", () => { - test(`var x = 0;`, - { - options: { compilerOptions: { module: ModuleKind.CommonJS, newLine: NewLineKind.LineFeed } }, - expectedOutput: `"use strict";\nvar x = 0;\n` + after(() => { + justName = undefined; + transpileOptions = undefined; + canUseOldTranspile = undefined; + toBeCompiled = undefined; + transpileResult = undefined; + oldTranspileResult = undefined; + oldTranspileDiagnostics = undefined; }); - }); - it("Sets module name", () => { - const output = - `System.register("NamedModule", [], function(exports_1, context_1) {\n` + - ` "use strict";\n` + - ` var __moduleName = context_1 && context_1.id;\n` + - ` var x;\n` + - ` return {\n` + - ` setters:[],\n` + - ` execute: function() {\n` + - ` var x = 1;\n` + - ` }\n` + - ` }\n` + - `});\n`; - test("var x = 1;", - { - options: { compilerOptions: { module: ModuleKind.System, newLine: NewLineKind.LineFeed }, moduleName: "NamedModule" }, - expectedOutput: output - }); - }); - - it("No extra errors for file without extension", () => { - test(`"use strict";\r\nvar x = 0;`, { options: { compilerOptions: { module: ModuleKind.CommonJS }, fileName: "file" } }); - }); - - it("Rename dependencies - System", () => { - const input = - `import {foo} from "SomeName";\n` + - `declare function use(a: any);\n` + - `use(foo);`; - const output = - `System.register(["SomeOtherName"], function(exports_1, context_1) {\n` + - ` "use strict";\n` + - ` var __moduleName = context_1 && context_1.id;\n` + - ` var SomeName_1;\n` + - ` return {\n` + - ` setters:[\n` + - ` function (SomeName_1_1) {\n` + - ` SomeName_1 = SomeName_1_1;\n` + - ` }],\n` + - ` execute: function() {\n` + - ` use(SomeName_1.foo);\n` + - ` }\n` + - ` }\n` + - `});\n`; - - test(input, - { - options: { compilerOptions: { module: ModuleKind.System, newLine: NewLineKind.LineFeed }, renamedDependencies: { "SomeName": "SomeOtherName" } }, - expectedOutput: output - }); - }); - - it("Rename dependencies - AMD", () => { - const input = - `import {foo} from "SomeName";\n` + - `declare function use(a: any);\n` + - `use(foo);`; - const output = - `define(["require", "exports", "SomeOtherName"], function (require, exports, SomeName_1) {\n` + - ` "use strict";\n` + - ` use(SomeName_1.foo);\n` + - `});\n`; - - test(input, - { - options: { compilerOptions: { module: ModuleKind.AMD, newLine: NewLineKind.LineFeed }, renamedDependencies: { "SomeName": "SomeOtherName" } }, - expectedOutput: output - }); - }); - - it("Rename dependencies - UMD", () => { - const input = - `import {foo} from "SomeName";\n` + - `declare function use(a: any);\n` + - `use(foo);`; - const output = - `(function (factory) {\n` + - ` if (typeof module === 'object' && typeof module.exports === 'object') {\n` + - ` var v = factory(require, exports); if (v !== undefined) module.exports = v;\n` + - ` }\n` + - ` else if (typeof define === 'function' && define.amd) {\n` + - ` define(["require", "exports", "SomeOtherName"], factory);\n` + - ` }\n` + - `})(function (require, exports) {\n` + - ` "use strict";\n` + - ` var SomeName_1 = require("SomeOtherName");\n` + - ` use(SomeName_1.foo);\n` + - `});\n`; - - test(input, - { - options: { compilerOptions: { module: ModuleKind.UMD, newLine: NewLineKind.LineFeed }, renamedDependencies: { "SomeName": "SomeOtherName" } }, - expectedOutput: output - }); - }); - - it("Transpile with emit decorators and emit metadata", () => { - const input = - `import {db} from './db';\n` + - `function someDecorator(target) {\n` + - ` return target;\n` + - `} \n` + - `@someDecorator\n` + - `class MyClass {\n` + - ` db: db;\n` + - ` constructor(db: db) {\n` + - ` this.db = db;\n` + - ` this.db.doSomething(); \n` + - ` }\n` + - `}\n` + - `export {MyClass}; \n`; - const output = - `"use strict";\n` + - `var db_1 = require(\'./db\');\n` + - `function someDecorator(target) {\n` + - ` return target;\n` + - `}\n` + - `var MyClass = (function () {\n` + - ` function MyClass(db) {\n` + - ` this.db = db;\n` + - ` this.db.doSomething();\n` + - ` }\n` + - ` MyClass = __decorate([\n` + - ` someDecorator, \n` + - ` __metadata(\'design:paramtypes\', [(typeof (_a = typeof db_1.db !== \'undefined\' && db_1.db) === \'function\' && _a) || Object])\n` + - ` ], MyClass);\n` + - ` return MyClass;\n` + - ` var _a;\n` + - `}());\n` + - `exports.MyClass = MyClass;\n`; - - test(input, - { - options: { - compilerOptions: { - module: ModuleKind.CommonJS, - newLine: NewLineKind.LineFeed, - noEmitHelpers: true, - emitDecoratorMetadata: true, - experimentalDecorators: true, - target: ScriptTarget.ES5, + it("Correct errors for " + justName, () => { + Harness.Baseline.runBaseline("Correct errors", justName.replace(/\.tsx?$/, ".errors.txt"), () => { + if (transpileResult.diagnostics.length === 0) { + /* tslint:disable:no-null-keyword */ + return null; + /* tslint:enable:no-null-keyword */ } - }, - expectedOutput: output + + return Harness.Compiler.getErrorBaseline(toBeCompiled, transpileResult.diagnostics); + }); }); - }); - it("Supports backslashes in file name", () => { - test("var x", { expectedOutput: `"use strict";\r\nvar x;\r\n`, options: { fileName: "a\\b.ts" }}); - }); + if (canUseOldTranspile) { + it("Correct errors (old transpile) for " + justName, () => { + Harness.Baseline.runBaseline("Correct errors", justName.replace(/\.tsx?$/, ".oldTranspile.errors.txt"), () => { + if (oldTranspileDiagnostics.length === 0) { + /* tslint:disable:no-null-keyword */ + return null; + /* tslint:enable:no-null-keyword */ + } - it("transpile file as 'tsx' if 'jsx' is specified", () => { - const input = `var x =
`; - const output = `"use strict";\nvar x = React.createElement("div", null);\n`; - test(input, { - expectedOutput: output, - options: { compilerOptions: { jsx: JsxEmit.React, newLine: NewLineKind.LineFeed } } - }); - }); + return Harness.Compiler.getErrorBaseline(toBeCompiled, oldTranspileDiagnostics); + }); + }); + } - it("transpile .js files", () => { - const input = "const a = 10;"; - const output = `"use strict";\nvar a = 10;\n`; - test(input, { - expectedOutput: output, - options: { compilerOptions: { newLine: NewLineKind.LineFeed, module: ModuleKind.CommonJS }, fileName: "input.js", reportDiagnostics: true } - }); - }); - - it("Supports urls in file name", () => { - test("var x", { expectedOutput: `"use strict";\r\nvar x;\r\n`, options: { fileName: "http://somewhere/directory//directory2/file.ts" } }); - }); - - it("Support options with lib values", () => { - const input = "const a = 10;"; - const output = `"use strict";\r\nvar a = 10;\r\n`; - test(input, { - expectedOutput: output, - options: { compilerOptions: { lib: ["es6", "dom"], module: ModuleKind.CommonJS }, fileName: "input.js", reportDiagnostics: true } - }); - }); - - it("Support options with types values", () => { - const input = "const a = 10;"; - const output = `"use strict";\r\nvar a = 10;\r\n`; - test(input, { - expectedOutput: output, - options: { compilerOptions: { types: ["jquery", "typescript"], module: ModuleKind.CommonJS }, fileName: "input.js", reportDiagnostics: true } - }); - }); - - describe("String values for enums", () => { - it("Accepts strings instead of enum values", () => { - test(`export const x = 0`, { - options: { - compilerOptions: { - module: "es6", - // Capitalization and spaces ignored - target: " Es6 " + it("Correct output for " + justName, () => { + Harness.Baseline.runBaseline("Correct output", justName.replace(/\.tsx?$/, ".js"), () => { + if (transpileResult.outputText) { + return transpileResult.outputText; } - }, - expectedOutput: "export const x = 0;\r\n" + else { + // This can happen if compiler recieve invalid compiler-options + /* tslint:disable:no-null-keyword */ + return null; + /* tslint:enable:no-null-keyword */ + } + }); }); - }); - it("Fails on bad value", () => { - for (const value in [123, {}, ""]) { - test(``, { - options: { compilerOptions: { module: value } }, - expectedDiagnosticCodes: [6046], - expectedDiagnosticTexts: ["Argument for '--module' option must be: 'none', 'commonjs', 'amd', 'system', 'umd', 'es6', 'es2015'"] + + if (canUseOldTranspile) { + it("Correct output (old transpile) for " + justName, () => { + Harness.Baseline.runBaseline("Correct output", justName.replace(/\.tsx?$/, ".oldTranspile.js"), () => { + return oldTranspileResult; + }); }); } }); + } + + transpilesCorrectly("Generates no diagnostics with valid inputs", `var x = 0;`, { + options: { compilerOptions: { module: ModuleKind.CommonJS } } + }); + + transpilesCorrectly("Generates no diagnostics for missing file references", `/// +var x = 0;`, { + options: { compilerOptions: { module: ModuleKind.CommonJS } } + }); + + transpilesCorrectly("Generates no diagnostics for missing module imports", `import {a} from "module2";`, { + options: { compilerOptions: { module: ModuleKind.CommonJS } } + }); + + transpilesCorrectly("Generates expected syntactic diagnostics", `a b`, { + options: { compilerOptions: { module: ModuleKind.CommonJS } } + }); + + transpilesCorrectly("Does not generate semantic diagnostics", `var x: string = 0;`, { + options: { compilerOptions: { module: ModuleKind.CommonJS } } + }); + + transpilesCorrectly("Generates module output", `var x = 0;`, { + options: { compilerOptions: { module: ModuleKind.AMD } } + }); + + transpilesCorrectly("Uses correct newLine character", `var x = 0;`, { + options: { compilerOptions: { module: ModuleKind.CommonJS, newLine: NewLineKind.LineFeed } } + }); + + transpilesCorrectly("Sets module name", "var x = 1;", { + options: { compilerOptions: { module: ModuleKind.System, newLine: NewLineKind.LineFeed }, moduleName: "NamedModule" } + }); + + transpilesCorrectly("No extra errors for file without extension", `"use strict";\r\nvar x = 0;`, { + options: { compilerOptions: { module: ModuleKind.CommonJS }, fileName: "file" } + }); + + transpilesCorrectly("Rename dependencies - System", + `import {foo} from "SomeName";\n` + + `declare function use(a: any);\n` + + `use(foo);`, { + options: { compilerOptions: { module: ModuleKind.System, newLine: NewLineKind.LineFeed }, renamedDependencies: { "SomeName": "SomeOtherName" } } + }); + + transpilesCorrectly("Rename dependencies - AMD", + `import {foo} from "SomeName";\n` + + `declare function use(a: any);\n` + + `use(foo);`, { + options: { compilerOptions: { module: ModuleKind.AMD, newLine: NewLineKind.LineFeed }, renamedDependencies: { "SomeName": "SomeOtherName" } } + }); + + transpilesCorrectly("Rename dependencies - UMD", + `import {foo} from "SomeName";\n` + + `declare function use(a: any);\n` + + `use(foo);`, { + options: { compilerOptions: { module: ModuleKind.UMD, newLine: NewLineKind.LineFeed }, renamedDependencies: { "SomeName": "SomeOtherName" } } + }); + + transpilesCorrectly("Transpile with emit decorators and emit metadata", + `import {db} from './db';\n` + + `function someDecorator(target) {\n` + + ` return target;\n` + + `} \n` + + `@someDecorator\n` + + `class MyClass {\n` + + ` db: db;\n` + + ` constructor(db: db) {\n` + + ` this.db = db;\n` + + ` this.db.doSomething(); \n` + + ` }\n` + + `}\n` + + `export {MyClass}; \n`, { + options: { + compilerOptions: { + module: ModuleKind.CommonJS, + newLine: NewLineKind.LineFeed, + noEmitHelpers: true, + emitDecoratorMetadata: true, + experimentalDecorators: true, + target: ScriptTarget.ES5, + } + } + }); + + transpilesCorrectly("Supports backslashes in file name", "var x", { + options: { fileName: "a\\b.ts" } + }); + + transpilesCorrectly("transpile file as 'tsx' if 'jsx' is specified", `var x =
`, { + options: { compilerOptions: { jsx: JsxEmit.React, newLine: NewLineKind.LineFeed } } + }); + + transpilesCorrectly("transpile .js files", "const a = 10;", { + options: { compilerOptions: { newLine: NewLineKind.LineFeed, module: ModuleKind.CommonJS }, fileName: "input.js", reportDiagnostics: true } + }); + + transpilesCorrectly("Supports urls in file name", "var x", { + options: { fileName: "http://somewhere/directory//directory2/file.ts" } + }); + + transpilesCorrectly("Accepts string as enum values for compile-options", "export const x = 0", { + options: { compilerOptions: { + module: "es6", + // Capitalization and spaces ignored + target: " Es6 " + }} + }); + + transpilesCorrectly("Report an error when compiler-options module-kind is out-of-range", "", { + options: { compilerOptions: { module: 123 }} + }); + + transpilesCorrectly("Report an error when compiler-options target-script is out-of-range", "", { + options: { compilerOptions: { module: 123 }} + }); + + transpilesCorrectly("Report an error when compiler-options input is empty object", "", { + options: { compilerOptions: { module: {} }} + }); + + transpilesCorrectly("Report an error when compiler-options input is empty string", "", { + options: { compilerOptions: { module: "" }} + }); + + transpilesCorrectly("Support options with lib values", "const a = 10;", { + options: { compilerOptions: { lib: ["es6", "dom"], module: ModuleKind.CommonJS }, fileName: "input.js", reportDiagnostics: true } + }); + + transpilesCorrectly("Support options with types values", "const a = 10;", { + options: { compilerOptions: { types: ["jquery", "typescript"], module: ModuleKind.CommonJS }, fileName: "input.js", reportDiagnostics: true } }); }); } From 38962eea82f9bbb07c9657d0b7e2bb64d292c1a6 Mon Sep 17 00:00:00 2001 From: Herrington Darkholme Date: Fri, 17 Jun 2016 10:22:56 +0800 Subject: [PATCH 45/56] use resolveEntityName to find interface --- src/compiler/checker.ts | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index b0484fc890c..6081770f62e 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -939,29 +939,26 @@ namespace ts { function checkAndReportErrorForExtendingInterface(errorLocation: Node): boolean { - const container = getContainingClass(errorLocation); - const heritageClause = getAncestor(errorLocation, SyntaxKind.HeritageClause); - if (!container || !heritageClause || heritageClause.token !== SyntaxKind.ExtendsKeyword) { + let parentClassExpression = errorLocation; + while (parentClassExpression) { + const kind = parentClassExpression.kind; + if (kind === SyntaxKind.Identifier || kind === SyntaxKind.PropertyAccessExpression) { + parentClassExpression = parentClassExpression.parent; + continue; + } + if (kind === SyntaxKind.ExpressionWithTypeArguments) { + break; + } return false; } - if (errorLocation.kind === SyntaxKind.Identifier) { - const name = (errorLocation).text; - const interfaceOrModule = resolveName( - errorLocation, name, - SymbolFlags.Interface | SymbolFlags.HasExports, - /*errorMessage*/ undefined, /*nameArg*/ undefined) - if (!interfaceOrModule) { - return false; - } - if (interfaceOrModule.flags & SymbolFlags.Interface) { - error(errorLocation, Diagnostics.Cannot_extend_an_interface_0_Did_you_mean_implements, name); - return true; - } + if (!parentClassExpression) { + return false; } - else if (errorLocation.kind === SyntaxKind.PropertyAccessExpression) { - // todo + const expression = (parentClassExpression).expression; + if (resolveEntityName(expression, SymbolFlags.Interface, true)) { + error(errorLocation, Diagnostics.Cannot_extend_an_interface_0_Did_you_mean_implements, getTextOfNode(expression)); + return true; } - return false; } From 877977a4509bf9e2f2cec89108d389919975afe0 Mon Sep 17 00:00:00 2001 From: Herrington Darkholme Date: Fri, 17 Jun 2016 11:07:18 +0800 Subject: [PATCH 46/56] add new tests for extends interface --- src/compiler/checker.ts | 2 +- .../classExtendsInterface.errors.txt | 2 +- .../reference/classExtendsInterface.js | 2 +- ...assExtendsInterfaceInExpression.errors.txt | 14 ++++++ .../classExtendsInterfaceInExpression.js | 26 +++++++++++ .../classExtendsInterfaceInModule.errors.txt | 27 ++++++++++++ .../classExtendsInterfaceInModule.js | 44 +++++++++++++++++++ tests/cases/compiler/classExtendsInterface.ts | 21 --------- .../classExtendsInterfaceInExpression.ts | 7 +++ .../compiler/classExtendsInterfaceInModule.ts | 14 ++++++ 10 files changed, 135 insertions(+), 24 deletions(-) create mode 100644 tests/baselines/reference/classExtendsInterfaceInExpression.errors.txt create mode 100644 tests/baselines/reference/classExtendsInterfaceInExpression.js create mode 100644 tests/baselines/reference/classExtendsInterfaceInModule.errors.txt create mode 100644 tests/baselines/reference/classExtendsInterfaceInModule.js create mode 100644 tests/cases/compiler/classExtendsInterfaceInExpression.ts create mode 100644 tests/cases/compiler/classExtendsInterfaceInModule.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 6081770f62e..0cd9144e4be 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -955,7 +955,7 @@ namespace ts { return false; } const expression = (parentClassExpression).expression; - if (resolveEntityName(expression, SymbolFlags.Interface, true)) { + if (resolveEntityName(expression, SymbolFlags.Interface, /*ignoreErrors*/ true)) { error(errorLocation, Diagnostics.Cannot_extend_an_interface_0_Did_you_mean_implements, getTextOfNode(expression)); return true; } diff --git a/tests/baselines/reference/classExtendsInterface.errors.txt b/tests/baselines/reference/classExtendsInterface.errors.txt index 3714bbb2323..72b8466105c 100644 --- a/tests/baselines/reference/classExtendsInterface.errors.txt +++ b/tests/baselines/reference/classExtendsInterface.errors.txt @@ -8,7 +8,7 @@ tests/cases/compiler/classExtendsInterface.ts(6,21): error TS2689: Cannot extend ~~~~~~~~~~ !!! error TS2689: Cannot extend an interface 'Comparable'. Did you mean 'implements'? class B implements Comparable {} - + interface Comparable2 {} class A2 extends Comparable2 {} ~~~~~~~~~~~ diff --git a/tests/baselines/reference/classExtendsInterface.js b/tests/baselines/reference/classExtendsInterface.js index c06c4e66d8b..b324f7382d8 100644 --- a/tests/baselines/reference/classExtendsInterface.js +++ b/tests/baselines/reference/classExtendsInterface.js @@ -2,7 +2,7 @@ interface Comparable {} class A extends Comparable {} class B implements Comparable {} - + interface Comparable2 {} class A2 extends Comparable2 {} class B2 implements Comparable2 {} diff --git a/tests/baselines/reference/classExtendsInterfaceInExpression.errors.txt b/tests/baselines/reference/classExtendsInterfaceInExpression.errors.txt new file mode 100644 index 00000000000..a2f6e1f4b6c --- /dev/null +++ b/tests/baselines/reference/classExtendsInterfaceInExpression.errors.txt @@ -0,0 +1,14 @@ +tests/cases/compiler/classExtendsInterfaceInExpression.ts(7,25): error TS2304: Cannot find name 'A'. + + +==== tests/cases/compiler/classExtendsInterfaceInExpression.ts (1 errors) ==== + interface A {} + + function factory(a: any): {new(): Object} { + return null + } + + class C extends factory(A) {} + ~ +!!! error TS2304: Cannot find name 'A'. + \ No newline at end of file diff --git a/tests/baselines/reference/classExtendsInterfaceInExpression.js b/tests/baselines/reference/classExtendsInterfaceInExpression.js new file mode 100644 index 00000000000..1b4fa112cc8 --- /dev/null +++ b/tests/baselines/reference/classExtendsInterfaceInExpression.js @@ -0,0 +1,26 @@ +//// [classExtendsInterfaceInExpression.ts] +interface A {} + +function factory(a: any): {new(): Object} { + return null +} + +class C extends factory(A) {} + + +//// [classExtendsInterfaceInExpression.js] +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +function factory(a) { + return null; +} +var C = (function (_super) { + __extends(C, _super); + function C() { + _super.apply(this, arguments); + } + return C; +}(factory(A))); diff --git a/tests/baselines/reference/classExtendsInterfaceInModule.errors.txt b/tests/baselines/reference/classExtendsInterfaceInModule.errors.txt new file mode 100644 index 00000000000..9a87ddf51b6 --- /dev/null +++ b/tests/baselines/reference/classExtendsInterfaceInModule.errors.txt @@ -0,0 +1,27 @@ +tests/cases/compiler/classExtendsInterfaceInModule.ts(5,18): error TS2689: Cannot extend an interface 'M.I1'. Did you mean 'implements'? +tests/cases/compiler/classExtendsInterfaceInModule.ts(6,21): error TS2689: Cannot extend an interface 'M.I2'. Did you mean 'implements'? +tests/cases/compiler/classExtendsInterfaceInModule.ts(14,17): error TS2689: Cannot extend an interface 'Mod.Nested.I'. Did you mean 'implements'? + + +==== tests/cases/compiler/classExtendsInterfaceInModule.ts (3 errors) ==== + module M { + export interface I1 {} + export interface I2 {} + } + class C1 extends M.I1 {} + ~ +!!! error TS2689: Cannot extend an interface 'M.I1'. Did you mean 'implements'? + class C2 extends M.I2 {} + ~ +!!! error TS2689: Cannot extend an interface 'M.I2'. Did you mean 'implements'? + + module Mod { + export namespace Nested { + export interface I {} + } + } + + class D extends Mod.Nested.I {} + ~~~ +!!! error TS2689: Cannot extend an interface 'Mod.Nested.I'. Did you mean 'implements'? + \ No newline at end of file diff --git a/tests/baselines/reference/classExtendsInterfaceInModule.js b/tests/baselines/reference/classExtendsInterfaceInModule.js new file mode 100644 index 00000000000..45415488700 --- /dev/null +++ b/tests/baselines/reference/classExtendsInterfaceInModule.js @@ -0,0 +1,44 @@ +//// [classExtendsInterfaceInModule.ts] +module M { + export interface I1 {} + export interface I2 {} +} +class C1 extends M.I1 {} +class C2 extends M.I2 {} + +module Mod { + export namespace Nested { + export interface I {} + } +} + +class D extends Mod.Nested.I {} + + +//// [classExtendsInterfaceInModule.js] +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var C1 = (function (_super) { + __extends(C1, _super); + function C1() { + _super.apply(this, arguments); + } + return C1; +}(M.I1)); +var C2 = (function (_super) { + __extends(C2, _super); + function C2() { + _super.apply(this, arguments); + } + return C2; +}(M.I2)); +var D = (function (_super) { + __extends(D, _super); + function D() { + _super.apply(this, arguments); + } + return D; +}(Mod.Nested.I)); diff --git a/tests/cases/compiler/classExtendsInterface.ts b/tests/cases/compiler/classExtendsInterface.ts index 883680619ed..eaab1d60fc4 100644 --- a/tests/cases/compiler/classExtendsInterface.ts +++ b/tests/cases/compiler/classExtendsInterface.ts @@ -5,24 +5,3 @@ class B implements Comparable {} interface Comparable2 {} class A2 extends Comparable2 {} class B2 implements Comparable2 {} - -function Factory(a: any): {new()} { - return null -} - -class C extends Factory(Comparable) {} - -module M { - export interface I1 {} - export interface I2 {} -} -class C1 extends M.I1 {} -class C2 extends M.I2 {} - -namespace N { - export interface I1 {} - export interface I2 {} -} - -class D1 extends N.I1 {} -class D2 extends N.I2 {} diff --git a/tests/cases/compiler/classExtendsInterfaceInExpression.ts b/tests/cases/compiler/classExtendsInterfaceInExpression.ts new file mode 100644 index 00000000000..4dce2ae59bd --- /dev/null +++ b/tests/cases/compiler/classExtendsInterfaceInExpression.ts @@ -0,0 +1,7 @@ +interface A {} + +function factory(a: any): {new(): Object} { + return null +} + +class C extends factory(A) {} diff --git a/tests/cases/compiler/classExtendsInterfaceInModule.ts b/tests/cases/compiler/classExtendsInterfaceInModule.ts new file mode 100644 index 00000000000..4ea24e5dd7d --- /dev/null +++ b/tests/cases/compiler/classExtendsInterfaceInModule.ts @@ -0,0 +1,14 @@ +module M { + export interface I1 {} + export interface I2 {} +} +class C1 extends M.I1 {} +class C2 extends M.I2 {} + +module Mod { + export namespace Nested { + export interface I {} + } +} + +class D extends Mod.Nested.I {} From 2a9636b1beb87581536af331c90c343d821b76bc Mon Sep 17 00:00:00 2001 From: Herrington Darkholme Date: Fri, 17 Jun 2016 11:20:00 +0800 Subject: [PATCH 47/56] address code style --- .../reference/classExtendsInterfaceInExpression.errors.txt | 2 +- tests/baselines/reference/classExtendsInterfaceInExpression.js | 2 +- tests/cases/compiler/classExtendsInterfaceInExpression.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/baselines/reference/classExtendsInterfaceInExpression.errors.txt b/tests/baselines/reference/classExtendsInterfaceInExpression.errors.txt index a2f6e1f4b6c..64b160ea766 100644 --- a/tests/baselines/reference/classExtendsInterfaceInExpression.errors.txt +++ b/tests/baselines/reference/classExtendsInterfaceInExpression.errors.txt @@ -5,7 +5,7 @@ tests/cases/compiler/classExtendsInterfaceInExpression.ts(7,25): error TS2304: C interface A {} function factory(a: any): {new(): Object} { - return null + return null; } class C extends factory(A) {} diff --git a/tests/baselines/reference/classExtendsInterfaceInExpression.js b/tests/baselines/reference/classExtendsInterfaceInExpression.js index 1b4fa112cc8..69e63f92570 100644 --- a/tests/baselines/reference/classExtendsInterfaceInExpression.js +++ b/tests/baselines/reference/classExtendsInterfaceInExpression.js @@ -2,7 +2,7 @@ interface A {} function factory(a: any): {new(): Object} { - return null + return null; } class C extends factory(A) {} diff --git a/tests/cases/compiler/classExtendsInterfaceInExpression.ts b/tests/cases/compiler/classExtendsInterfaceInExpression.ts index 4dce2ae59bd..002631afedf 100644 --- a/tests/cases/compiler/classExtendsInterfaceInExpression.ts +++ b/tests/cases/compiler/classExtendsInterfaceInExpression.ts @@ -1,7 +1,7 @@ interface A {} function factory(a: any): {new(): Object} { - return null + return null; } class C extends factory(A) {} From 166bc49f0c666f5a2a75bcfacd421e6b8f5135c7 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Thu, 16 Jun 2016 13:20:12 -0700 Subject: [PATCH 48/56] Refactor navigation bar --- src/compiler/core.ts | 11 + src/compiler/parser.ts | 8 +- src/server/client.ts | 17 +- src/server/editorServices.ts | 14 +- src/server/session.ts | 10 +- src/services/navigationBar.ts | 1374 +++++++---------- src/services/services.ts | 2 +- .../navbar_contains-no-duplicates.ts | 14 +- tests/cases/fourslash/navbar_exportDefault.ts | 16 +- ...BarAnonymousClassAndFunctionExpressions.ts | 147 ++ ...arAnonymousClassAndFunctionExpressions2.ts | 64 + .../fourslash/navigationBarGetterAndSetter.ts | 48 + tests/cases/fourslash/navigationBarImports.ts | 30 + .../fourslash/navigationBarItemsFunctions.ts | 14 + .../navigationBarItemsFunctionsBroken.ts | 6 + .../navigationBarItemsFunctionsBroken2.ts | 10 + ...ionBarItemsInsideMethodsAndConstructors.ts | 12 +- .../fourslash/navigationBarItemsItems2.ts | 7 +- ...rItemsItemsContainsNoAnonymousFunctions.ts | 80 - .../navigationBarItemsMissingName1.ts | 5 + .../navigationBarItemsMissingName2.ts | 10 +- .../fourslash/navigationBarItemsModules.ts | 196 +-- ...ationBarItemsMultilineStringIdentifiers.ts | 56 +- tests/cases/fourslash/navigationBarJsDoc.ts | 28 +- tests/cases/fourslash/navigationBarMerging.ts | 189 +++ .../cases/fourslash/navigationBarVariables.ts | 53 + .../server/jsdocTypedefTagNavigateTo.ts | 48 +- 27 files changed, 1416 insertions(+), 1053 deletions(-) create mode 100644 tests/cases/fourslash/navigationBarAnonymousClassAndFunctionExpressions.ts create mode 100644 tests/cases/fourslash/navigationBarAnonymousClassAndFunctionExpressions2.ts create mode 100644 tests/cases/fourslash/navigationBarGetterAndSetter.ts create mode 100644 tests/cases/fourslash/navigationBarImports.ts delete mode 100644 tests/cases/fourslash/navigationBarItemsItemsContainsNoAnonymousFunctions.ts create mode 100644 tests/cases/fourslash/navigationBarMerging.ts create mode 100644 tests/cases/fourslash/navigationBarVariables.ts diff --git a/src/compiler/core.ts b/src/compiler/core.ts index 8a2040a83df..10a0526d6ae 100644 --- a/src/compiler/core.ts +++ b/src/compiler/core.ts @@ -138,6 +138,17 @@ namespace ts { return result; } + export function filterMutate(array: T[], f: (x: T) => boolean): void { + let outIndex = 0; + for (const item of array) { + if (f(item)) { + array[outIndex] = item; + outIndex++; + } + } + array.length = outIndex; + } + export function map(array: T[], f: (x: T) => U): U[] { let result: U[]; if (array) { diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index b2f3755ec46..6e0a37c3d9f 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -17,19 +17,19 @@ namespace ts { } function visitNode(cbNode: (node: Node) => T, node: Node): T { - if (node) { + if (node !== void 0) { return cbNode(node); } } function visitNodeArray(cbNodes: (nodes: Node[]) => T, nodes: Node[]) { - if (nodes) { + if (nodes !== void 0) { return cbNodes(nodes); } } function visitEachNode(cbNode: (node: Node) => T, nodes: Node[]) { - if (nodes) { + if (nodes !== void 0) { for (const node of nodes) { const result = cbNode(node); if (result) { @@ -44,7 +44,7 @@ namespace ts { // embedded arrays are flattened and the 'cbNode' callback is invoked for each element. If a callback returns // a truthy value, iteration stops and that value is returned. Otherwise, undefined is returned. export function forEachChild(node: Node, cbNode: (node: Node) => T, cbNodeArray?: (nodes: Node[]) => T): T { - if (!node) { + if (node === void 0) { return; } // The visitXXX functions could be written as local functions that close over the cbNode and cbNodeArray diff --git a/src/server/client.ts b/src/server/client.ts index 864dac9fdaa..09cfa2ac739 100644 --- a/src/server/client.ts +++ b/src/server/client.ts @@ -45,8 +45,9 @@ namespace ts.server { return lineMap; } - private lineOffsetToPosition(fileName: string, lineOffset: protocol.Location): number { - return ts.computePositionOfLineAndCharacter(this.getLineMap(fileName), lineOffset.line - 1, lineOffset.offset - 1); + private lineOffsetToPosition(fileName: string, lineOffset: protocol.Location, lineMap?: number[]): number { + lineMap = lineMap || this.getLineMap(fileName); + return ts.computePositionOfLineAndCharacter(lineMap, lineOffset.line - 1, lineOffset.offset - 1); } private positionToOneBasedLineOffset(fileName: string, position: number): protocol.Location { @@ -449,7 +450,7 @@ namespace ts.server { return this.lastRenameEntry.locations; } - decodeNavigationBarItems(items: protocol.NavigationBarItem[], fileName: string): NavigationBarItem[] { + decodeNavigationBarItems(items: protocol.NavigationBarItem[], fileName: string, lineMap: number[]): NavigationBarItem[] { if (!items) { return []; } @@ -458,8 +459,11 @@ namespace ts.server { text: item.text, kind: item.kind, kindModifiers: item.kindModifiers || "", - spans: item.spans.map(span => createTextSpanFromBounds(this.lineOffsetToPosition(fileName, span.start), this.lineOffsetToPosition(fileName, span.end))), - childItems: this.decodeNavigationBarItems(item.childItems, fileName), + spans: item.spans.map(span => + createTextSpanFromBounds( + this.lineOffsetToPosition(fileName, span.start, lineMap), + this.lineOffsetToPosition(fileName, span.end, lineMap))), + childItems: this.decodeNavigationBarItems(item.childItems, fileName, lineMap), indent: item.indent, bolded: false, grayed: false @@ -474,7 +478,8 @@ namespace ts.server { const request = this.processRequest(CommandNames.NavBar, args); const response = this.processResponse(request); - return this.decodeNavigationBarItems(response.body, fileName); + const lineMap = this.getLineMap(fileName); + return this.decodeNavigationBarItems(response.body, fileName, lineMap); } getNameOrDottedNameSpan(fileName: string, startPos: number, endPos: number): TextSpan { diff --git a/src/server/editorServices.ts b/src/server/editorServices.ts index 346b2e00a10..74dbb7a0785 100644 --- a/src/server/editorServices.ts +++ b/src/server/editorServices.ts @@ -359,12 +359,16 @@ namespace ts.server { * @param line 1-based index * @param offset 1-based index */ - positionToLineOffset(filename: string, position: number): ILineInfo { + positionToLineOffset(filename: string, position: number, lineIndex?: LineIndex): ILineInfo { + lineIndex = lineIndex || this.getLineIndex(filename); + const lineOffset = lineIndex.charOffsetToLineNumberAndPos(position); + return { line: lineOffset.line, offset: lineOffset.offset + 1 }; + } + + getLineIndex(filename: string): LineIndex { const path = toPath(filename, this.host.getCurrentDirectory(), this.getCanonicalFileName); const script: ScriptInfo = this.filenameToScript.get(path); - const index = script.snap().index; - const lineOffset = index.charOffsetToLineNumberAndPos(position); - return { line: lineOffset.line, offset: lineOffset.offset + 1 }; + return script.snap().index; } } @@ -1452,7 +1456,7 @@ namespace ts.server { } // if the project is too large, the root files might not have been all loaded if the total - // program size reached the upper limit. In that case project.projectOptions.files should + // program size reached the upper limit. In that case project.projectOptions.files should // be more precise. However this would only happen for configured project. const oldFileNames = project.projectOptions ? project.projectOptions.files : project.compilerService.host.roots.map(info => info.fileName); const newFileNames = ts.filter(projectOptions.files, f => this.host.fileExists(f)); diff --git a/src/server/session.ts b/src/server/session.ts index 65540082f60..aafc02a2b0b 100644 --- a/src/server/session.ts +++ b/src/server/session.ts @@ -863,7 +863,7 @@ namespace ts.server { this.projectService.closeClientFile(file); } - private decorateNavigationBarItem(project: Project, fileName: string, items: ts.NavigationBarItem[]): protocol.NavigationBarItem[] { + private decorateNavigationBarItem(project: Project, fileName: string, items: ts.NavigationBarItem[], lineIndex: LineIndex): protocol.NavigationBarItem[] { if (!items) { return undefined; } @@ -875,10 +875,10 @@ namespace ts.server { kind: item.kind, kindModifiers: item.kindModifiers, spans: item.spans.map(span => ({ - start: compilerService.host.positionToLineOffset(fileName, span.start), - end: compilerService.host.positionToLineOffset(fileName, ts.textSpanEnd(span)) + start: compilerService.host.positionToLineOffset(fileName, span.start, lineIndex), + end: compilerService.host.positionToLineOffset(fileName, ts.textSpanEnd(span), lineIndex) })), - childItems: this.decorateNavigationBarItem(project, fileName, item.childItems), + childItems: this.decorateNavigationBarItem(project, fileName, item.childItems, lineIndex), indent: item.indent })); } @@ -896,7 +896,7 @@ namespace ts.server { return undefined; } - return this.decorateNavigationBarItem(project, fileName, items); + return this.decorateNavigationBarItem(project, fileName, items, compilerService.host.getLineIndex(fileName)); } private getNavigateToItems(searchValue: string, fileName: string, maxResultCount?: number): protocol.NavtoItem[] { diff --git a/src/services/navigationBar.ts b/src/services/navigationBar.ts index 59dc4a38283..e722f33bc70 100644 --- a/src/services/navigationBar.ts +++ b/src/services/navigationBar.ts @@ -2,860 +2,660 @@ /* @internal */ namespace ts.NavigationBar { - export function getNavigationBarItems(sourceFile: SourceFile, compilerOptions: CompilerOptions): ts.NavigationBarItem[] { - // TODO: Handle JS files differently in 'navbar' calls for now, but ideally we should unify - // the 'navbar' and 'navto' logic for TypeScript and JavaScript. - if (isSourceFileJavaScript(sourceFile)) { - return getJsNavigationBarItems(sourceFile, compilerOptions); + /** + * Represents a navigation bar item and its children. + * The returned NavigationBarItem is more complicated and doesn't include 'parent', so we use these to do work before converting. + */ + interface NavigationBarNode { + node: Node; + additionalNodes: Node[] | undefined; + parent: NavigationBarNode | undefined; // Present for all but root node + children: NavigationBarNode[] | undefined; + indent: number; // # of parents + } + + export function getNavigationBarItems(sourceFile_: SourceFile): NavigationBarItem[] { + sourceFile = sourceFile_; + const result = map(topLevelItems(rootNavigationBarNode(sourceFile)), convertToTopLevelItem); + sourceFile = void 0; + return result; + } + + // Keep sourceFile handy so we don't have to search for it every time we need to call `getText`. + let sourceFile: SourceFile; + function nodeText(node: Node): string { + return node.getText(sourceFile); + } + + function navigationBarNodeKind(n: NavigationBarNode): SyntaxKind { + return n.node.kind; + } + + function pushChild(parent: NavigationBarNode, child: NavigationBarNode): void { + if (parent.children !== void 0) { + parent.children.push(child); + } + else { + parent.children = [child]; + } + } + + /* + For performance, we keep navigation bar parents on a stack rather than passing them through each recursion. + `parent` is the current parent and is *not* stored in parentsStack. + `startNode` sets a new parent and `endNode` returns to the previous parent. + */ + const parentsStack: NavigationBarNode[] = []; + let parent: NavigationBarNode; + + function rootNavigationBarNode(sourceFile: SourceFile): NavigationBarNode { + Debug.assert(!parentsStack.length); + const root: NavigationBarNode = { node: sourceFile, additionalNodes: void 0, parent: void 0, children: void 0, indent: 0 }; + parent = root; + for (const statement of sourceFile.statements) { + addChildrenRecursively(statement); + } + endNode(); + Debug.assert(parent === void 0 && !parentsStack.length); + return root; + } + + function addLeafNode(node: Node): void { + pushChild(parent, emptyNavigationBarNode(node)); + } + + function emptyNavigationBarNode(node: Node): NavigationBarNode { + return { + node, + additionalNodes: void 0, + parent, + children: void 0, + indent: parent.indent + 1 + }; + } + + /** + * Add a new level of NavigationBarNodes. + * This pushes to the stack, so you must call `endNode` when you are done adding to this node. + */ + function startNode(node: Node): void { + const navNode: NavigationBarNode = emptyNavigationBarNode(node); + pushChild(parent, navNode); + + // Save the old parent + parentsStack.push(parent); + parent = navNode; + } + + /** Call after calling `startNode` and adding children to it. */ + function endNode(): void { + if (parent.children !== void 0) { + mergeChildren(parent.children); + sortChildren(parent.children); + } + parent = parentsStack.pop(); + } + + function addNodeWithRecursiveChild(node: Node, child: Node): void { + startNode(node); + addChildrenRecursively(child); + endNode(); + } + + /** Look for navigation bar items in node's subtree, adding them to the current `parent`. */ + function addChildrenRecursively(node: Node): void { + if (node === void 0 || isToken(node)) { + return; } - return getItemsWorker(getTopLevelNodes(sourceFile), createTopLevelItem); + switch (node.kind) { + case SyntaxKind.Constructor: + // Get parameter properties, and treat them as being on the *same* level as the constructor, not under it. + const ctr = node; + addNodeWithRecursiveChild(ctr, ctr.body); - function getIndent(node: Node): number { - let indent = 1; // Global node is the only one with indent 0. + // Parameter properties are children of the class, not the constructor. + for (const param of ctr.parameters) { + if (isParameterPropertyDeclaration(param)) { + addLeafNode(param); + } + } + break; - let current = node.parent; - while (current) { - switch (current.kind) { - case SyntaxKind.ModuleDeclaration: - // If we have a module declared as A.B.C, it is more "intuitive" - // to say it only has a single layer of depth - do { - current = current.parent; - } - while (current.kind === SyntaxKind.ModuleDeclaration); + case SyntaxKind.MethodDeclaration: + case SyntaxKind.GetAccessor: + case SyntaxKind.SetAccessor: + case SyntaxKind.MethodSignature: + if (!hasDynamicName((node))) { + addNodeWithRecursiveChild(node, (node).body); + } + break; - // fall through - case SyntaxKind.ClassDeclaration: - case SyntaxKind.EnumDeclaration: - case SyntaxKind.InterfaceDeclaration: - case SyntaxKind.FunctionDeclaration: - indent++; + case SyntaxKind.PropertyDeclaration: + case SyntaxKind.PropertySignature: + if (!hasDynamicName((node))) { + addLeafNode(node); + } + break; + + case SyntaxKind.ImportClause: + let importClause = node; + // Handle default import case e.g.: + // import d from "mod"; + if (importClause.name !== void 0) { + addLeafNode(importClause); } - current = current.parent; - } - - return indent; - } - - function getChildNodes(nodes: Node[]): Node[] { - const childNodes: Node[] = []; - - function visit(node: Node) { - switch (node.kind) { - case SyntaxKind.VariableStatement: - forEach((node).declarationList.declarations, visit); - break; - case SyntaxKind.ObjectBindingPattern: - case SyntaxKind.ArrayBindingPattern: - forEach((node).elements, visit); - break; - - case SyntaxKind.ExportDeclaration: - // Handle named exports case e.g.: - // export {a, b as B} from "mod"; - if ((node).exportClause) { - forEach((node).exportClause.elements, visit); + // Handle named bindings in imports e.g.: + // import * as NS from "mod"; + // import {a, b as B} from "mod"; + const {namedBindings} = importClause; + if (namedBindings !== void 0) { + if (namedBindings.kind === SyntaxKind.NamespaceImport) { + addLeafNode(namedBindings); + } + else { + for (const element of (namedBindings).elements) { + addLeafNode(element); } - break; - - case SyntaxKind.ImportDeclaration: - let importClause = (node).importClause; - if (importClause) { - // Handle default import case e.g.: - // import d from "mod"; - if (importClause.name) { - childNodes.push(importClause); - } - - // Handle named bindings in imports e.g.: - // import * as NS from "mod"; - // import {a, b as B} from "mod"; - if (importClause.namedBindings) { - if (importClause.namedBindings.kind === SyntaxKind.NamespaceImport) { - childNodes.push(importClause.namedBindings); - } - else { - forEach((importClause.namedBindings).elements, visit); - } - } - } - break; - - case SyntaxKind.BindingElement: - case SyntaxKind.VariableDeclaration: - if (isBindingPattern((node).name)) { - visit((node).name); - break; - } - // Fall through - case SyntaxKind.ClassDeclaration: - case SyntaxKind.EnumDeclaration: - case SyntaxKind.InterfaceDeclaration: - case SyntaxKind.ModuleDeclaration: - case SyntaxKind.FunctionDeclaration: - case SyntaxKind.ImportEqualsDeclaration: - case SyntaxKind.ImportSpecifier: - case SyntaxKind.ExportSpecifier: - case SyntaxKind.TypeAliasDeclaration: - childNodes.push(node); - break; + } } - } + break; - // for (let i = 0, n = nodes.length; i < n; i++) { - // let node = nodes[i]; - - // if (node.kind === SyntaxKind.ClassDeclaration || - // node.kind === SyntaxKind.EnumDeclaration || - // node.kind === SyntaxKind.InterfaceDeclaration || - // node.kind === SyntaxKind.ModuleDeclaration || - // node.kind === SyntaxKind.FunctionDeclaration) { - - // childNodes.push(node); - // } - // else if (node.kind === SyntaxKind.VariableStatement) { - // childNodes.push.apply(childNodes, (node).declarations); - // } - // } - forEach(nodes, visit); - return sortNodes(childNodes); - } - - function getTopLevelNodes(node: SourceFile): Node[] { - const topLevelNodes: Node[] = []; - topLevelNodes.push(node); - - addTopLevelNodes(node.statements, topLevelNodes); - - return topLevelNodes; - } - - function sortNodes(nodes: Node[]): Node[] { - return nodes.slice(0).sort((n1: Declaration, n2: Declaration) => { - if (n1.name && n2.name) { - return localeCompareFix(getPropertyNameForPropertyNameNode(n1.name), getPropertyNameForPropertyNameNode(n2.name)); + case SyntaxKind.BindingElement: + case SyntaxKind.VariableDeclaration: + const decl = node; + const name = decl.name; + if (isBindingPattern(name)) { + addChildrenRecursively(name); } - else if (n1.name) { - return 1; - } - else if (n2.name) { - return -1; + else if (decl.initializer !== void 0 && isFunctionOrClassExpression(decl.initializer)) { + // For `const x = function() {}`, just use the function node, not the const. + addChildrenRecursively(decl.initializer); } else { - return n1.kind - n2.kind; + addNodeWithRecursiveChild(decl, decl.initializer); } - }); + break; - // node 0.10 treats "a" as greater than "B". - // For consistency, sort alphabetically, falling back to which is lower-case. - function localeCompareFix(a: string, b: string) { - const cmp = a.toLowerCase().localeCompare(b.toLowerCase()); - if (cmp !== 0) - return cmp; - // Return the *opposite* of the `<` operator, which works the same in node 0.10 and 6.0. - return a < b ? 1 : a > b ? -1 : 0; - } - } + case SyntaxKind.ArrowFunction: + case SyntaxKind.FunctionDeclaration: + case SyntaxKind.FunctionExpression: + addNodeWithRecursiveChild(node, (node).body); + break; - function addTopLevelNodes(nodes: Node[], topLevelNodes: Node[]): void { - nodes = sortNodes(nodes); + case SyntaxKind.EnumDeclaration: + startNode(node); + for (const member of (node).members) { + if (!isComputedProperty(member)) { + addLeafNode(member); + } + } + endNode(); + break; - for (const node of nodes) { - switch (node.kind) { - case SyntaxKind.ClassDeclaration: - topLevelNodes.push(node); - for (const member of (node).members) { - if (member.kind === SyntaxKind.MethodDeclaration || member.kind === SyntaxKind.Constructor) { - type FunctionLikeMember = MethodDeclaration | ConstructorDeclaration; - if ((member).body) { - // We do not include methods that does not have child functions in it, because of duplications. - if (hasNamedFunctionDeclarations(((member).body).statements)) { - topLevelNodes.push(member); - } - addTopLevelNodes(((member).body).statements, topLevelNodes); - } + case SyntaxKind.ClassDeclaration: + case SyntaxKind.ClassExpression: + case SyntaxKind.InterfaceDeclaration: + startNode(node); + for (const member of (node).members) { + addChildrenRecursively(member); + } + endNode(); + break; + + case SyntaxKind.ModuleDeclaration: + addNodeWithRecursiveChild(node, getInteriorModule(node).body); + break; + + case SyntaxKind.ExportSpecifier: + case SyntaxKind.ImportEqualsDeclaration: + case SyntaxKind.IndexSignature: + case SyntaxKind.CallSignature: + case SyntaxKind.ConstructSignature: + case SyntaxKind.TypeAliasDeclaration: + addLeafNode(node); + break; + + default: + if (node.jsDocComments !== void 0) { + for (const jsDocComment of node.jsDocComments) { + for (const tag of jsDocComment.tags) { + if (tag.kind === SyntaxKind.JSDocTypedefTag) { + addLeafNode(tag); } } - break; - case SyntaxKind.EnumDeclaration: - case SyntaxKind.InterfaceDeclaration: - case SyntaxKind.TypeAliasDeclaration: - topLevelNodes.push(node); - break; - - case SyntaxKind.ModuleDeclaration: - let moduleDeclaration = node; - topLevelNodes.push(node); - const inner = getInnermostModule(moduleDeclaration); - if (inner.body) { - addTopLevelNodes((inner.body).statements, topLevelNodes); - } - break; - - case SyntaxKind.FunctionDeclaration: - let functionDeclaration = node; - if (isTopLevelFunctionDeclaration(functionDeclaration)) { - topLevelNodes.push(node); - addTopLevelNodes((functionDeclaration.body).statements, topLevelNodes); - } - break; + } } - } - } - function hasNamedFunctionDeclarations(nodes: NodeArray): boolean { - for (const s of nodes) { - if (s.kind === SyntaxKind.FunctionDeclaration && !isEmpty((s).name.text)) { + forEachChild(node, addChildrenRecursively); + } + } + + /** Merge declarations of the same kind. */ + function mergeChildren(children: NavigationBarNode[]): void { + const nameToItems: Map = {}; + filterMutate(children, child => { + const decl = child.node; + const name = decl.name && nodeText(decl.name); + if (name === void 0) { + // Anonymous items are never merged. + return true; + } + + const itemsWithSameName = getProperty(nameToItems, name); + if (itemsWithSameName === void 0) { + nameToItems[name] = child; + return true; + } + + if (itemsWithSameName instanceof Array) { + for (const itemWithSameName of itemsWithSameName) { + if (tryMerge(itemWithSameName, child)) { + return false; + } + } + itemsWithSameName.push(child); + return true; + } + else { + const itemWithSameName = itemsWithSameName; + if (tryMerge(itemWithSameName, child)) { + return false; + } + nameToItems[name] = [itemWithSameName, child]; + return true; + } + + function tryMerge(a: NavigationBarNode, b: NavigationBarNode): boolean { + if (shouldReallyMerge(a.node, b.node)) { + merge(a, b); return true; } + return false; } - return false; - } + }); - function isTopLevelFunctionDeclaration(functionDeclaration: FunctionLikeDeclaration): boolean { - if (functionDeclaration.kind === SyntaxKind.FunctionDeclaration) { - // A function declaration is 'top level' if it contains any function declarations - // within it. - if (functionDeclaration.body && functionDeclaration.body.kind === SyntaxKind.Block) { - // Proper function declarations can only have identifier names - if (hasNamedFunctionDeclarations((functionDeclaration.body).statements)) { - return true; - } + /** a and b have the same name, but they may not be mergeable. */ + function shouldReallyMerge(a: Node, b: Node): boolean { + return a.kind === b.kind && (a.kind !== SyntaxKind.ModuleDeclaration || areSameModule(a, b)); - // Or if it is not parented by another function. I.e all functions at module scope are 'top level'. - if (!isFunctionBlock(functionDeclaration.parent)) { - return true; - } - - // Or if it is nested inside class methods and constructors. - else { - // We have made sure that a grand parent node exists with 'isFunctionBlock()' above. - const grandParentKind = functionDeclaration.parent.parent.kind; - if (grandParentKind === SyntaxKind.MethodDeclaration || - grandParentKind === SyntaxKind.Constructor) { - - return true; - } - } + // We use 1 NavNode to represent 'A.B.C', but there are multiple source nodes. + // Only merge module nodes that have the same chain. Don't merge 'A.B.C' with 'A'! + function areSameModule(a: ModuleDeclaration, b: ModuleDeclaration): boolean { + if (a.body.kind !== b.body.kind) { + return false; } - } - - return false; - } - - function getItemsWorker(nodes: Node[], createItem: (n: Node) => ts.NavigationBarItem): ts.NavigationBarItem[] { - const items: ts.NavigationBarItem[] = []; - - const keyToItem: Map = {}; - - for (const child of nodes) { - const item = createItem(child); - if (item !== undefined) { - if (item.text.length > 0) { - const key = item.text + "-" + item.kind + "-" + item.indent; - - const itemWithSameName = keyToItem[key]; - if (itemWithSameName) { - // We had an item with the same name. Merge these items together. - merge(itemWithSameName, item); - } - else { - keyToItem[key] = item; - items.push(item); - } - } - } - } - - return items; - } - - function merge(target: ts.NavigationBarItem, source: ts.NavigationBarItem) { - // First, add any spans in the source to the target. - addRange(target.spans, source.spans); - - if (source.childItems) { - if (!target.childItems) { - target.childItems = []; - } - - // Next, recursively merge or add any children in the source as appropriate. - outer: - for (const sourceChild of source.childItems) { - for (const targetChild of target.childItems) { - if (targetChild.text === sourceChild.text && targetChild.kind === sourceChild.kind) { - // Found a match. merge them. - merge(targetChild, sourceChild); - continue outer; - } - } - - // Didn't find a match, just add this child to the list. - target.childItems.push(sourceChild); + if (a.body.kind !== SyntaxKind.ModuleDeclaration) { + return true; } + return areSameModule(a.body, b.body); } } - function createChildItem(node: Node): ts.NavigationBarItem { - switch (node.kind) { - case SyntaxKind.Parameter: - if (isBindingPattern((node).name)) { - break; + /** Merge source into target. Source should be thrown away after this is called. */ + function merge(target: NavigationBarNode, source: NavigationBarNode): void { + target.additionalNodes = target.additionalNodes || []; + target.additionalNodes.push(source.node); + if (source.additionalNodes !== void 0) { + target.additionalNodes.push(...source.additionalNodes); + } + + target.children = concatenate(target.children, source.children); + if (target.children !== void 0) { + mergeChildren(target.children); + sortChildren(target.children); + } + } + } + + /** Recursively ensure that each NavNode's children are in sorted order. */ + function sortChildren(children: NavigationBarNode[]): void { + children.sort(compareChildren); + } + + function compareChildren(child1: NavigationBarNode, child2: NavigationBarNode): number { + const name1 = tryGetName(child1.node), name2 = tryGetName(child2.node); + if (name1 !== void 0 && name2 !== void 0) { + const cmp = localeCompareFix(name1, name2); + return cmp !== 0 ? cmp : navigationBarNodeKind(child1) - navigationBarNodeKind(child2); + } + else { + return name1 !== void 0 ? 1 : name2 !== void 0 ? -1 : navigationBarNodeKind(child1) - navigationBarNodeKind(child2); + } + } + + // More efficient to create a collator once and use its `compare` than to call `a.localeCompare(b)` many times. + const collator: { compare(a: string, b: string): number } = typeof Intl === "undefined" ? void 0 : new Intl.Collator(); + // Intl is missing in Safari, and node 0.10 treats "a" as greater than "B". + const localeCompareIsCorrect = collator && collator.compare("a", "B") < 0; + const localeCompareFix: (a: string, b: string) => number = localeCompareIsCorrect ? collator.compare : function(a, b) { + // This isn't perfect, but it passes all of our tests. + for (let i = 0; i < Math.min(a.length, b.length); i++) { + const chA = a.charAt(i), chB = b.charAt(i); + if (chA === "\"" && chB === "'") { + return 1; + } + if (chA === "'" && chB === "\"") { + return -1; + } + const cmp = chA.toLocaleLowerCase().localeCompare(chB.toLocaleLowerCase()); + if (cmp !== 0) { + return cmp; + } + } + return a.length - b.length; + }; + + /** + * This differs from getItemName because this is just used for sorting. + * We only sort nodes by name that have a more-or-less "direct" name, as opposed to `new()` and the like. + * So `new()` can still come before an `aardvark` method. + */ + function tryGetName(node: Node): string | undefined { + if (node.kind === SyntaxKind.ModuleDeclaration) { + return getModuleName(node); + } + + const decl = node; + if (decl.name !== void 0) { + return getPropertyNameForPropertyNameNode(decl.name); + } + switch (node.kind) { + case SyntaxKind.FunctionExpression: + case SyntaxKind.ArrowFunction: + case SyntaxKind.ClassExpression: + return getFunctionOrClassName(node); + case SyntaxKind.JSDocTypedefTag: + return getJSDocTypedefTagName(node); + default: + return void 0; + } + } + + function getItemName(node: Node): string { + if (node.kind === SyntaxKind.ModuleDeclaration) { + return getModuleName(node); + } + + const name = (node).name; + if (name !== void 0) { + const text = nodeText(name); + if (text.length > 0) { + return text; + } + } + + switch (node.kind) { + case SyntaxKind.SourceFile: + const sourceFile = node; + return isExternalModule(sourceFile) + ? `"${escapeString(getBaseFileName(removeFileExtension(normalizePath(sourceFile.fileName))))}"` + : ""; + case SyntaxKind.ArrowFunction: + case SyntaxKind.FunctionDeclaration: + case SyntaxKind.FunctionExpression: + case SyntaxKind.ClassDeclaration: + case SyntaxKind.ClassExpression: + if (node.flags & NodeFlags.Default) { + return "default"; + } + return getFunctionOrClassName(node); + case SyntaxKind.Constructor: + return "constructor"; + case SyntaxKind.ConstructSignature: + return "new()"; + case SyntaxKind.CallSignature: + return "()"; + case SyntaxKind.IndexSignature: + return "[]"; + case SyntaxKind.JSDocTypedefTag: + return getJSDocTypedefTagName(node); + default: + Debug.fail(); + return ""; + } + } + + function getJSDocTypedefTagName(node: JSDocTypedefTag): string { + if (node.name !== void 0) { + return node.name.text; + } + else { + const parentNode = node.parent && node.parent.parent; + if (parentNode && parentNode.kind === SyntaxKind.VariableStatement) { + if ((parentNode).declarationList.declarations.length > 0) { + const nameIdentifier = (parentNode).declarationList.declarations[0].name; + if (nameIdentifier.kind === SyntaxKind.Identifier) { + return (nameIdentifier).text; } - if ((node.flags & NodeFlags.Modifier) === 0) { - return undefined; + } + } + return ""; + } + } + + /** Flattens the NavNode tree to a list, keeping only the top-level items. */ + function topLevelItems(root: NavigationBarNode): NavigationBarNode[] { + const topLevel: NavigationBarNode[] = []; + function recur(item: NavigationBarNode) { + if (isTopLevel(item)) { + topLevel.push(item); + if (item.children !== void 0) { + for (const child of item.children) { + recur(child); } - return createItem(node, getTextOfNode((node).name), ts.ScriptElementKind.memberVariableElement); - - case SyntaxKind.MethodDeclaration: - case SyntaxKind.MethodSignature: - return createItem(node, getTextOfNode((node).name), ts.ScriptElementKind.memberFunctionElement); - - case SyntaxKind.GetAccessor: - return createItem(node, getTextOfNode((node).name), ts.ScriptElementKind.memberGetAccessorElement); - - case SyntaxKind.SetAccessor: - return createItem(node, getTextOfNode((node).name), ts.ScriptElementKind.memberSetAccessorElement); - - case SyntaxKind.IndexSignature: - return createItem(node, "[]", ts.ScriptElementKind.indexSignatureElement); - - case SyntaxKind.EnumDeclaration: - return createItem(node, getTextOfNode((node).name), ts.ScriptElementKind.enumElement); - - case SyntaxKind.EnumMember: - return createItem(node, getTextOfNode((node).name), ts.ScriptElementKind.memberVariableElement); - - case SyntaxKind.ModuleDeclaration: - return createItem(node, getModuleName(node), ts.ScriptElementKind.moduleElement); - - case SyntaxKind.InterfaceDeclaration: - return createItem(node, getTextOfNode((node).name), ts.ScriptElementKind.interfaceElement); - - case SyntaxKind.TypeAliasDeclaration: - return createItem(node, getTextOfNode((node).name), ts.ScriptElementKind.typeElement); - - case SyntaxKind.CallSignature: - return createItem(node, "()", ts.ScriptElementKind.callSignatureElement); - - case SyntaxKind.ConstructSignature: - return createItem(node, "new()", ts.ScriptElementKind.constructSignatureElement); - - case SyntaxKind.PropertyDeclaration: - case SyntaxKind.PropertySignature: - return createItem(node, getTextOfNode((node).name), ts.ScriptElementKind.memberVariableElement); + } + } + } + recur(root); + return topLevel; + function isTopLevel(item: NavigationBarNode): boolean { + switch (navigationBarNodeKind(item)) { case SyntaxKind.ClassDeclaration: - return createItem(node, getTextOfNode((node).name), ts.ScriptElementKind.classElement); - - case SyntaxKind.FunctionDeclaration: - return createItem(node, getTextOfNode((node).name), ts.ScriptElementKind.functionElement); - - case SyntaxKind.VariableDeclaration: - case SyntaxKind.BindingElement: - let variableDeclarationNode: Node; - let name: Node; - - if (node.kind === SyntaxKind.BindingElement) { - name = (node).name; - variableDeclarationNode = node; - // binding elements are added only for variable declarations - // bubble up to the containing variable declaration - while (variableDeclarationNode && variableDeclarationNode.kind !== SyntaxKind.VariableDeclaration) { - variableDeclarationNode = variableDeclarationNode.parent; - } - Debug.assert(variableDeclarationNode !== undefined); - } - else { - Debug.assert(!isBindingPattern((node).name)); - variableDeclarationNode = node; - name = (node).name; - } - - if (isConst(variableDeclarationNode)) { - return createItem(node, getTextOfNode(name), ts.ScriptElementKind.constElement); - } - else if (isLet(variableDeclarationNode)) { - return createItem(node, getTextOfNode(name), ts.ScriptElementKind.letElement); - } - else { - return createItem(node, getTextOfNode(name), ts.ScriptElementKind.variableElement); - } + case SyntaxKind.ClassExpression: + case SyntaxKind.EnumDeclaration: + case SyntaxKind.InterfaceDeclaration: + case SyntaxKind.ModuleDeclaration: + case SyntaxKind.SourceFile: + case SyntaxKind.TypeAliasDeclaration: + case SyntaxKind.JSDocTypedefTag: + return true; case SyntaxKind.Constructor: - return createItem(node, "constructor", ts.ScriptElementKind.constructorImplementationElement); + case SyntaxKind.MethodDeclaration: + case SyntaxKind.GetAccessor: + case SyntaxKind.SetAccessor: + return hasSomeImportantChild(item); - case SyntaxKind.ExportSpecifier: - case SyntaxKind.ImportSpecifier: - case SyntaxKind.ImportEqualsDeclaration: - case SyntaxKind.ImportClause: - case SyntaxKind.NamespaceImport: - return createItem(node, getTextOfNode((node).name), ts.ScriptElementKind.alias); + case SyntaxKind.ArrowFunction: + case SyntaxKind.FunctionDeclaration: + case SyntaxKind.FunctionExpression: + return isTopLevelFunctionDeclaration(item); + + default: + return false; } + function isTopLevelFunctionDeclaration(item: NavigationBarNode): boolean { + if ((item.node).body === void 0) { + return false; + } - return undefined; - - function createItem(node: Node, name: string, scriptElementKind: string): NavigationBarItem { - return getNavigationBarItem(name, scriptElementKind, getNodeModifiers(node), [getNodeSpan(node)]); + switch (navigationBarNodeKind(item.parent)) { + case SyntaxKind.ModuleBlock: + case SyntaxKind.SourceFile: + case SyntaxKind.MethodDeclaration: + case SyntaxKind.Constructor: + return true; + default: + return hasSomeImportantChild(item); + } + } + function hasSomeImportantChild(item: NavigationBarNode) { + return forEach(item.children, child => { + const childKind = navigationBarNodeKind(child); + return childKind !== SyntaxKind.VariableDeclaration && childKind !== SyntaxKind.BindingElement; + }); } } + } - function isEmpty(text: string) { - return !text || text.trim() === ""; - } + // NavigationBarItem requires an array, but will not mutate it, so just give it this for performance. + const emptyChildItemArray: NavigationBarItem[] = []; - function getNavigationBarItem(text: string, kind: string, kindModifiers: string, spans: TextSpan[], childItems: NavigationBarItem[] = [], indent = 0): NavigationBarItem { - if (isEmpty(text)) { - return undefined; - } + function convertToTopLevelItem(n: NavigationBarNode): NavigationBarItem { + return { + text: getItemName(n.node), + kind: nodeKind(n.node), + kindModifiers: getNodeModifiers(n.node), + spans: getSpans(n), + childItems: map(n.children, convertToChildItem) || emptyChildItemArray, + indent: n.indent, + bolded: false, + grayed: false + }; + function convertToChildItem(n: NavigationBarNode): NavigationBarItem { return { - text, - kind, - kindModifiers, - spans, - childItems, - indent, + text: getItemName(n.node), + kind: nodeKind(n.node), + kindModifiers: getNodeModifiers(n.node), + spans: getSpans(n), + childItems: emptyChildItemArray, + indent: 0, bolded: false, grayed: false }; } - function createTopLevelItem(node: Node): ts.NavigationBarItem { - switch (node.kind) { - case SyntaxKind.SourceFile: - return createSourceFileItem(node); - - case SyntaxKind.ClassDeclaration: - return createClassItem(node); - - case SyntaxKind.MethodDeclaration: - case SyntaxKind.Constructor: - return createMemberFunctionLikeItem(node); - - case SyntaxKind.EnumDeclaration: - return createEnumItem(node); - - case SyntaxKind.InterfaceDeclaration: - return createInterfaceItem(node); - - case SyntaxKind.ModuleDeclaration: - return createModuleItem(node); - - case SyntaxKind.FunctionDeclaration: - return createFunctionItem(node); - - case SyntaxKind.TypeAliasDeclaration: - return createTypeAliasItem(node); - } - - return undefined; - - function createModuleItem(node: ModuleDeclaration): NavigationBarItem { - const moduleName = getModuleName(node); - - const body = getInnermostModule(node).body; - const childItems = body ? getItemsWorker(getChildNodes(body.statements), createChildItem) : []; - - return getNavigationBarItem(moduleName, - ts.ScriptElementKind.moduleElement, - getNodeModifiers(node), - [getNodeSpan(node)], - childItems, - getIndent(node)); - } - - function createFunctionItem(node: FunctionDeclaration): ts.NavigationBarItem { - if (node.body && node.body.kind === SyntaxKind.Block) { - const childItems = getItemsWorker(sortNodes((node.body).statements), createChildItem); - - return getNavigationBarItem(!node.name ? "default" : node.name.text, - ts.ScriptElementKind.functionElement, - getNodeModifiers(node), - [getNodeSpan(node)], - childItems, - getIndent(node)); + function getSpans(n: NavigationBarNode): TextSpan[] { + const spans = [getNodeSpan(n.node)]; + if (n.additionalNodes !== void 0) { + for (const node of n.additionalNodes) { + spans.push(getNodeSpan(node)); } - - return undefined; } - - function createTypeAliasItem(node: TypeAliasDeclaration): ts.NavigationBarItem { - return getNavigationBarItem(node.name.text, - ts.ScriptElementKind.typeElement, - getNodeModifiers(node), - [getNodeSpan(node)], - [], - getIndent(node)); - } - - function createMemberFunctionLikeItem(node: MethodDeclaration | ConstructorDeclaration): ts.NavigationBarItem { - if (node.body && node.body.kind === SyntaxKind.Block) { - const childItems = getItemsWorker(sortNodes((node.body).statements), createChildItem); - let scriptElementKind: string; - let memberFunctionName: string; - if (node.kind === SyntaxKind.MethodDeclaration) { - memberFunctionName = getPropertyNameForPropertyNameNode(node.name); - scriptElementKind = ts.ScriptElementKind.memberFunctionElement; - } - else { - memberFunctionName = "constructor"; - scriptElementKind = ts.ScriptElementKind.constructorImplementationElement; - } - - return getNavigationBarItem(memberFunctionName, - scriptElementKind, - getNodeModifiers(node), - [getNodeSpan(node)], - childItems, - getIndent(node)); - } - - return undefined; - } - - function createSourceFileItem(node: SourceFile): ts.NavigationBarItem { - const childItems = getItemsWorker(getChildNodes(node.statements), createChildItem); - - const rootName = isExternalModule(node) - ? "\"" + escapeString(getBaseFileName(removeFileExtension(normalizePath(node.fileName)))) + "\"" - : ""; - - return getNavigationBarItem(rootName, - ts.ScriptElementKind.moduleElement, - ts.ScriptElementKindModifier.none, - [getNodeSpan(node)], - childItems); - } - - function createClassItem(node: ClassDeclaration): ts.NavigationBarItem { - let childItems: NavigationBarItem[]; - - if (node.members) { - const constructor = forEach(node.members, member => { - return member.kind === SyntaxKind.Constructor && member; - }); - - // Add the constructor parameters in as children of the class (for property parameters). - // Note that *all non-binding pattern named* parameters will be added to the nodes array, but parameters that - // are not properties will be filtered out later by createChildItem. - const nodes: Node[] = removeDynamicallyNamedProperties(node); - if (constructor) { - addRange(nodes, filter(constructor.parameters, p => !isBindingPattern(p.name))); - } - - childItems = getItemsWorker(sortNodes(nodes), createChildItem); - } - - const nodeName = !node.name ? "default" : node.name.text; - - return getNavigationBarItem( - nodeName, - ts.ScriptElementKind.classElement, - getNodeModifiers(node), - [getNodeSpan(node)], - childItems, - getIndent(node)); - } - - function createEnumItem(node: EnumDeclaration): ts.NavigationBarItem { - const childItems = getItemsWorker(sortNodes(removeComputedProperties(node)), createChildItem); - return getNavigationBarItem( - node.name.text, - ts.ScriptElementKind.enumElement, - getNodeModifiers(node), - [getNodeSpan(node)], - childItems, - getIndent(node)); - } - - function createInterfaceItem(node: InterfaceDeclaration): ts.NavigationBarItem { - const childItems = getItemsWorker(sortNodes(removeDynamicallyNamedProperties(node)), createChildItem); - return getNavigationBarItem( - node.name.text, - ts.ScriptElementKind.interfaceElement, - getNodeModifiers(node), - [getNodeSpan(node)], - childItems, - getIndent(node)); - } - } - - function getModuleName(moduleDeclaration: ModuleDeclaration): string { - // We want to maintain quotation marks. - if (isAmbientModule(moduleDeclaration)) { - return getTextOfNode(moduleDeclaration.name); - } - - // Otherwise, we need to aggregate each identifier to build up the qualified name. - const result: string[] = []; - - result.push(moduleDeclaration.name.text); - - while (moduleDeclaration.body && moduleDeclaration.body.kind === SyntaxKind.ModuleDeclaration) { - moduleDeclaration = moduleDeclaration.body; - - result.push(moduleDeclaration.name.text); - } - - return result.join("."); - } - - function removeComputedProperties(node: EnumDeclaration): Declaration[] { - return filter(node.members, member => member.name === undefined || member.name.kind !== SyntaxKind.ComputedPropertyName); - } - - /** - * Like removeComputedProperties, but retains the properties with well known symbol names - */ - function removeDynamicallyNamedProperties(node: ClassDeclaration | InterfaceDeclaration): Declaration[] { - return filter(node.members, member => !hasDynamicName(member)); - } - - function getInnermostModule(node: ModuleDeclaration): ModuleDeclaration { - while (node.body && node.body.kind === SyntaxKind.ModuleDeclaration) { - node = node.body; - } - - return node; - } - - function getNodeSpan(node: Node) { - return node.kind === SyntaxKind.SourceFile - ? createTextSpanFromBounds(node.getFullStart(), node.getEnd()) - : createTextSpanFromBounds(node.getStart(), node.getEnd()); - } - - function getTextOfNode(node: Node): string { - return getTextOfNodeFromSourceText(sourceFile.text, node); + return spans; } } - export function getJsNavigationBarItems(sourceFile: SourceFile, compilerOptions: CompilerOptions): NavigationBarItem[] { - const anonFnText = ""; - const anonClassText = ""; - let indent = 0; + // TODO: GH#9145: We should just use getNodeKind. No reason why navigationBar and navigateTo should have different behaviors. + function nodeKind(node: Node): string { + switch (node.kind) { + case SyntaxKind.SourceFile: + return ScriptElementKind.moduleElement; - const rootName = isExternalModule(sourceFile) ? - "\"" + escapeString(getBaseFileName(removeFileExtension(normalizePath(sourceFile.fileName)))) + "\"" - : ""; + case SyntaxKind.EnumMember: + return ScriptElementKind.memberVariableElement; - const sourceFileItem = getNavBarItem(rootName, ScriptElementKind.moduleElement, [getNodeSpan(sourceFile)]); - let topItem = sourceFileItem; + case SyntaxKind.VariableDeclaration: + case SyntaxKind.BindingElement: + let variableDeclarationNode: Node; + let name: Node; - // Walk the whole file, because we want to also find function expressions - which may be in variable initializer, - // call arguments, expressions, etc... - forEachChild(sourceFile, visitNode); - - function visitNode(node: Node) { - const newItem = createNavBarItem(node); - - if (newItem) { - topItem.childItems.push(newItem); - } - - if (node.jsDocComments && node.jsDocComments.length > 0) { - for (const jsDocComment of node.jsDocComments) { - visitNode(jsDocComment); - } - } - - // Add a level if traversing into a container - if (newItem && (isFunctionLike(node) || isClassLike(node))) { - const lastTop = topItem; - indent++; - topItem = newItem; - forEachChild(node, visitNode); - topItem = lastTop; - indent--; - - // If the last item added was an anonymous function expression, and it had no children, discard it. - if (newItem && newItem.text === anonFnText && newItem.childItems.length === 0) { - topItem.childItems.pop(); - } - } - else { - forEachChild(node, visitNode); - } - } - - function createNavBarItem(node: Node): NavigationBarItem { - switch (node.kind) { - case SyntaxKind.VariableDeclaration: - // Only add to the navbar if at the top-level of the file - // Note: "const" and "let" are also SyntaxKind.VariableDeclarations - if (node.parent/*VariableDeclarationList*/.parent/*VariableStatement*/ - .parent/*SourceFile*/.kind !== SyntaxKind.SourceFile) { - return undefined; + if (node.kind === SyntaxKind.BindingElement) { + name = (node).name; + variableDeclarationNode = node; + // binding elements are added only for variable declarations + // bubble up to the containing variable declaration + while (variableDeclarationNode && variableDeclarationNode.kind !== SyntaxKind.VariableDeclaration) { + variableDeclarationNode = variableDeclarationNode.parent; } - // If it is initialized with a function expression, handle it when we reach the function expression node - const varDecl = node as VariableDeclaration; - if (varDecl.initializer && (varDecl.initializer.kind === SyntaxKind.FunctionExpression || - varDecl.initializer.kind === SyntaxKind.ArrowFunction || - varDecl.initializer.kind === SyntaxKind.ClassExpression)) { - return undefined; - } - // Fall through - case SyntaxKind.FunctionDeclaration: - case SyntaxKind.ClassDeclaration: - case SyntaxKind.Constructor: - case SyntaxKind.GetAccessor: - case SyntaxKind.SetAccessor: - // "export default function().." looks just like a regular function/class declaration, except with the 'default' flag - const name = node.flags && (node.flags & NodeFlags.Default) && !(node as (Declaration)).name ? "default" : - node.kind === SyntaxKind.Constructor ? "constructor" : - declarationNameToString((node as (Declaration)).name); - return getNavBarItem(name, getScriptKindForElementKind(node.kind), [getNodeSpan(node)]); - case SyntaxKind.FunctionExpression: - case SyntaxKind.ArrowFunction: - case SyntaxKind.ClassExpression: - return getDefineModuleItem(node) || getFunctionOrClassExpressionItem(node); - case SyntaxKind.MethodDeclaration: - const methodDecl = node as MethodDeclaration; - return getNavBarItem(declarationNameToString(methodDecl.name), - ScriptElementKind.memberFunctionElement, - [getNodeSpan(node)]); - case SyntaxKind.ExportAssignment: - // e.g. "export default " - return getNavBarItem("default", ScriptElementKind.variableElement, [getNodeSpan(node)]); - case SyntaxKind.ImportClause: // e.g. 'def' in: import def from 'mod' (in ImportDeclaration) - if (!(node as ImportClause).name) { - // No default import (this node is still a parent of named & namespace imports, which are handled below) - return undefined; - } - // fall through - case SyntaxKind.ImportSpecifier: // e.g. 'id' in: import {id} from 'mod' (in NamedImports, in ImportClause) - case SyntaxKind.NamespaceImport: // e.g. '* as ns' in: import * as ns from 'mod' (in ImportClause) - case SyntaxKind.ExportSpecifier: // e.g. 'a' or 'b' in: export {a, foo as b} from 'mod' - // Export specifiers are only interesting if they are reexports from another module, or renamed, else they are already globals - if (node.kind === SyntaxKind.ExportSpecifier) { - if (!(node.parent.parent as ExportDeclaration).moduleSpecifier && !(node as ExportSpecifier).propertyName) { - return undefined; - } - } - const decl = node as (ImportSpecifier | ImportClause | NamespaceImport | ExportSpecifier); - if (!decl.name) { - return undefined; - } - const declName = declarationNameToString(decl.name); - return getNavBarItem(declName, ScriptElementKind.constElement, [getNodeSpan(node)]); - case SyntaxKind.JSDocTypedefTag: - if ((node).name) { - return getNavBarItem( - (node).name.text, - ScriptElementKind.typeElement, - [getNodeSpan(node)]); - } - else { - const parentNode = node.parent && node.parent.parent; - if (parentNode && parentNode.kind === SyntaxKind.VariableStatement) { - if ((parentNode).declarationList.declarations.length > 0) { - const nameIdentifier = (parentNode).declarationList.declarations[0].name; - if (nameIdentifier.kind === SyntaxKind.Identifier) { - return getNavBarItem( - (nameIdentifier).text, - ScriptElementKind.typeElement, - [getNodeSpan(node)]); - } - } - } - } - default: - return undefined; - } - } - - function getNavBarItem(text: string, kind: string, spans: TextSpan[], kindModifiers = ScriptElementKindModifier.none): NavigationBarItem { - return { - text, kind, kindModifiers, spans, childItems: [], indent, bolded: false, grayed: false - }; - } - - function getDefineModuleItem(node: Node): NavigationBarItem { - if (node.kind !== SyntaxKind.FunctionExpression && node.kind !== SyntaxKind.ArrowFunction) { - return undefined; - } - - // No match if this is not a call expression to an identifier named 'define' - if (node.parent.kind !== SyntaxKind.CallExpression) { - return undefined; - } - const callExpr = node.parent as CallExpression; - if (callExpr.expression.kind !== SyntaxKind.Identifier || callExpr.expression.getText() !== "define") { - return undefined; - } - - // Return a module of either the given text in the first argument, or of the source file path - let defaultName = node.getSourceFile().fileName; - if (callExpr.arguments[0].kind === SyntaxKind.StringLiteral) { - defaultName = ((callExpr.arguments[0]) as StringLiteral).text; - } - return getNavBarItem(defaultName, ScriptElementKind.moduleElement, [getNodeSpan(node.parent)]); - } - - function getFunctionOrClassExpressionItem(node: Node): NavigationBarItem { - if (node.kind !== SyntaxKind.FunctionExpression && - node.kind !== SyntaxKind.ArrowFunction && - node.kind !== SyntaxKind.ClassExpression) { - return undefined; - } - - const fnExpr = node as FunctionExpression | ArrowFunction | ClassExpression; - let fnName: string; - if (fnExpr.name && getFullWidth(fnExpr.name) > 0) { - // The expression has an identifier, so use that as the name - fnName = declarationNameToString(fnExpr.name); - } - else { - // See if it is a var initializer. If so, use the var name. - if (fnExpr.parent.kind === SyntaxKind.VariableDeclaration) { - fnName = declarationNameToString((fnExpr.parent as VariableDeclaration).name); - } - // See if it is of the form " = function(){...}". If so, use the text from the left-hand side. - else if (fnExpr.parent.kind === SyntaxKind.BinaryExpression && - (fnExpr.parent as BinaryExpression).operatorToken.kind === SyntaxKind.EqualsToken) { - fnName = (fnExpr.parent as BinaryExpression).left.getText(); - } - // See if it is a property assignment, and if so use the property name - else if (fnExpr.parent.kind === SyntaxKind.PropertyAssignment && - (fnExpr.parent as PropertyAssignment).name) { - fnName = (fnExpr.parent as PropertyAssignment).name.getText(); + Debug.assert(variableDeclarationNode !== void 0); } else { - fnName = node.kind === SyntaxKind.ClassExpression ? anonClassText : anonFnText; + Debug.assert(!isBindingPattern((node).name)); + variableDeclarationNode = node; + name = (node).name; } - } - const scriptKind = node.kind === SyntaxKind.ClassExpression ? ScriptElementKind.classElement : ScriptElementKind.functionElement; - return getNavBarItem(fnName, scriptKind, [getNodeSpan(node)]); + + if (isConst(variableDeclarationNode)) { + return ts.ScriptElementKind.constElement; + } + else if (isLet(variableDeclarationNode)) { + return ts.ScriptElementKind.letElement; + } + else { + return ts.ScriptElementKind.variableElement; + } + + case SyntaxKind.ArrowFunction: + return ts.ScriptElementKind.functionElement; + + case SyntaxKind.JSDocTypedefTag: + return ScriptElementKind.typeElement; + + default: + return getNodeKind(node); + } + } + + function getModuleName(moduleDeclaration: ModuleDeclaration): string { + // We want to maintain quotation marks. + if (isAmbientModule(moduleDeclaration)) { + return getTextOfNode(moduleDeclaration.name); } - function getNodeSpan(node: Node) { - return node.kind === SyntaxKind.SourceFile - ? createTextSpanFromBounds(node.getFullStart(), node.getEnd()) - : createTextSpanFromBounds(node.getStart(), node.getEnd()); + // Otherwise, we need to aggregate each identifier to build up the qualified name. + const result: string[] = []; + + result.push(moduleDeclaration.name.text); + + while (moduleDeclaration.body && moduleDeclaration.body.kind === SyntaxKind.ModuleDeclaration) { + moduleDeclaration = moduleDeclaration.body; + + result.push(moduleDeclaration.name.text); } - function getScriptKindForElementKind(kind: SyntaxKind) { - switch (kind) { - case SyntaxKind.VariableDeclaration: - return ScriptElementKind.variableElement; - case SyntaxKind.FunctionDeclaration: - return ScriptElementKind.functionElement; - case SyntaxKind.ClassDeclaration: - return ScriptElementKind.classElement; - case SyntaxKind.Constructor: - return ScriptElementKind.constructorImplementationElement; - case SyntaxKind.GetAccessor: - return ScriptElementKind.memberGetAccessorElement; - case SyntaxKind.SetAccessor: - return ScriptElementKind.memberSetAccessorElement; - default: - return "unknown"; - } - } + return result.join("."); + } - return sourceFileItem.childItems; + /** + * For 'module A.B.C', we want to get the node for 'C'. + * We store 'A' as associated with a NavNode, and use getModuleName to traverse down again. + */ + function getInteriorModule(decl: ModuleDeclaration): ModuleDeclaration { + return decl.body.kind === SyntaxKind.ModuleDeclaration ? getInteriorModule(decl.body) : decl; + } + + function isComputedProperty(member: EnumMember): boolean { + return member.name === void 0 || member.name.kind === SyntaxKind.ComputedPropertyName; + } + + function getNodeSpan(node: Node): TextSpan { + return node.kind === SyntaxKind.SourceFile + ? createTextSpanFromBounds(node.getFullStart(), node.getEnd()) + : createTextSpanFromBounds(node.getStart(sourceFile), node.getEnd()); + } + + function getFunctionOrClassName(node: FunctionExpression | FunctionDeclaration | ArrowFunction | ClassLikeDeclaration): string { + if (node.name !== void 0 && getFullWidth(node.name) > 0) { + return declarationNameToString(node.name); + } + // See if it is a var initializer. If so, use the var name. + else if (node.parent.kind === SyntaxKind.VariableDeclaration) { + return declarationNameToString((node.parent as VariableDeclaration).name); + } + // See if it is of the form " = function(){...}". If so, use the text from the left-hand side. + else if (node.parent.kind === SyntaxKind.BinaryExpression && + (node.parent as BinaryExpression).operatorToken.kind === SyntaxKind.EqualsToken) { + return nodeText((node.parent as BinaryExpression).left); + } + // See if it is a property assignment, and if so use the property name + else if (node.parent.kind === SyntaxKind.PropertyAssignment && (node.parent as PropertyAssignment).name) { + return nodeText((node.parent as PropertyAssignment).name); + } + // Default exports are named "default" + else if (node.flags & NodeFlags.Default) { + return "default"; + } + else { + return isClassLike(node) ? "" : ""; + } + } + + function isFunctionOrClassExpression(node: Node): boolean { + return node.kind === SyntaxKind.FunctionExpression || node.kind === SyntaxKind.ArrowFunction || node.kind === SyntaxKind.ClassExpression; } } diff --git a/src/services/services.ts b/src/services/services.ts index 983ddfbf250..163be48695d 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -7039,7 +7039,7 @@ namespace ts { function getNavigationBarItems(fileName: string): NavigationBarItem[] { const sourceFile = syntaxTreeCache.getCurrentSourceFile(fileName); - return NavigationBar.getNavigationBarItems(sourceFile, host.getCompilationSettings()); + return NavigationBar.getNavigationBarItems(sourceFile); } function getSemanticClassifications(fileName: string, span: TextSpan): ClassifiedSpan[] { diff --git a/tests/cases/fourslash/navbar_contains-no-duplicates.ts b/tests/cases/fourslash/navbar_contains-no-duplicates.ts index e259a7bef90..10e4027ff84 100644 --- a/tests/cases/fourslash/navbar_contains-no-duplicates.ts +++ b/tests/cases/fourslash/navbar_contains-no-duplicates.ts @@ -7,7 +7,7 @@ //// } //// } //// } -//// +//// //// declare module Windows { //// export module Foundation { //// export var B; @@ -16,13 +16,13 @@ //// } //// } //// } -//// +//// //// class ABC { //// public foo() { //// return 3; //// } //// } -//// +//// //// module ABC { //// export var x = 3; //// } @@ -95,13 +95,13 @@ verify.navigationBar([ "kindModifiers": "export,declare" }, { - "text": "Test", - "kind": "class", + "text": "B", + "kind": "var", "kindModifiers": "export,declare" }, { - "text": "B", - "kind": "var", + "text": "Test", + "kind": "class", "kindModifiers": "export,declare" }, { diff --git a/tests/cases/fourslash/navbar_exportDefault.ts b/tests/cases/fourslash/navbar_exportDefault.ts index dc99cff7d64..e547c9129a6 100644 --- a/tests/cases/fourslash/navbar_exportDefault.ts +++ b/tests/cases/fourslash/navbar_exportDefault.ts @@ -16,7 +16,14 @@ goTo.file("a.ts"); verify.navigationBar([ { "text": "\"a\"", - "kind": "module" + "kind": "module", + "childItems": [ + { + "text": "default", + "kind": "class", + "kindModifiers": "export" + } + ] }, { "text": "default", @@ -52,6 +59,13 @@ verify.navigationBar([ { "text": "\"c\"", "kind": "module", + "childItems": [ + { + "text": "default", + "kind": "function", + "kindModifiers": "export" + } + ] }, { "text": "default", diff --git a/tests/cases/fourslash/navigationBarAnonymousClassAndFunctionExpressions.ts b/tests/cases/fourslash/navigationBarAnonymousClassAndFunctionExpressions.ts new file mode 100644 index 00000000000..f15959812fc --- /dev/null +++ b/tests/cases/fourslash/navigationBarAnonymousClassAndFunctionExpressions.ts @@ -0,0 +1,147 @@ +/// + +////global.cls = class { }; +////(function() { +//// const x = () => { +//// // Presence of inner function causes x to be a top-level function. +//// function xx() {} +//// }; +//// const y = { +//// // This is not a top-level function (contains nothing, but shows up in childItems of its parent.) +//// foo: function() {} +//// }; +//// (function nest() { +//// function moreNest() {} +//// })(); +////})(); +////(function() { // Different anonymous functions are not merged +//// // These will only show up as childItems. +//// function z() {} +//// console.log(function() {}) +////}) +////(function classes() { +//// // Classes show up in top-level regardless of whether they have names or inner declarations. +//// const cls2 = class { }; +//// console.log(class cls3 {}); +//// (class { }); +////}) + +verify.navigationBar([ + { + "text": "", + "kind": "module", + "childItems": [ + { + "text": "", + "kind": "function" + }, + { + "text": "", + "kind": "function" + }, + { + "text": "classes", + "kind": "function" + }, + { + "text": "global.cls", + "kind": "class" + } + ] + }, + { + "text": "", + "kind": "function", + "childItems": [ + { + "text": "nest", + "kind": "function" + }, + { + "text": "x", + "kind": "function" + }, + { + "text": "y", + "kind": "const" + } + ], + "indent": 1 + }, + { + "text": "nest", + "kind": "function", + "childItems": [ + { + "text": "moreNest", + "kind": "function" + } + ], + "indent": 2 + }, + { + "text": "x", + "kind": "function", + "childItems": [ + { + "text": "xx", + "kind": "function" + } + ], + "indent": 2 + }, + { + "text": "", + "kind": "function", + "childItems": [ + { + "text": "", + "kind": "function" + }, + { + "text": "z", + "kind": "function" + } + ], + "indent": 1 + }, + { + "text": "classes", + "kind": "function", + "childItems": [ + { + "text": "", + "kind": "class" + }, + { + "text": "cls2", + "kind": "class" + }, + { + "text": "cls3", + "kind": "class" + } + ], + "indent": 1 + }, + { + "text": "", + "kind": "class", + "indent": 2 + }, + { + "text": "cls2", + "kind": "class", + "indent": 2 + }, + { + "text": "cls3", + "kind": "class", + "indent": 2 + }, + { + "text": "global.cls", + "kind": "class", + "indent": 1 + } +]); diff --git a/tests/cases/fourslash/navigationBarAnonymousClassAndFunctionExpressions2.ts b/tests/cases/fourslash/navigationBarAnonymousClassAndFunctionExpressions2.ts new file mode 100644 index 00000000000..214a14949ac --- /dev/null +++ b/tests/cases/fourslash/navigationBarAnonymousClassAndFunctionExpressions2.ts @@ -0,0 +1,64 @@ +/// + +////console.log(console.log(class Y {}, class X {}), console.log(class B {}, class A {})); +////console.log(class Cls { meth() {} }); + +verify.navigationBar([ + { + "text": "", + "kind": "module", + "childItems": [ + { + "text": "A", + "kind": "class" + }, + { + "text": "B", + "kind": "class" + }, + { + "text": "Cls", + "kind": "class" + }, + { + "text": "X", + "kind": "class" + }, + { + "text": "Y", + "kind": "class" + } + ] + }, + { + "text": "A", + "kind": "class", + "indent": 1 + }, + { + "text": "B", + "kind": "class", + "indent": 1 + }, + { + "text": "Cls", + "kind": "class", + "childItems": [ + { + "text": "meth", + "kind": "method" + } + ], + "indent": 1 + }, + { + "text": "X", + "kind": "class", + "indent": 1 + }, + { + "text": "Y", + "kind": "class", + "indent": 1 + } +]); diff --git a/tests/cases/fourslash/navigationBarGetterAndSetter.ts b/tests/cases/fourslash/navigationBarGetterAndSetter.ts new file mode 100644 index 00000000000..48105e4e9af --- /dev/null +++ b/tests/cases/fourslash/navigationBarGetterAndSetter.ts @@ -0,0 +1,48 @@ +/// + +////class X { +//// get x() {} +//// set x(value) { +//// // Inner declaration should make the setter top-level. +//// function f() {} +//// } +////} + +verify.navigationBar([ + { + "text": "", + "kind": "module", + "childItems": [ + { + "text": "X", + "kind": "class" + } + ] + }, + { + "text": "X", + "kind": "class", + "childItems": [ + { + "text": "x", + "kind": "getter" + }, + { + "text": "x", + "kind": "setter" + } + ], + "indent": 1 + }, + { + "text": "x", + "kind": "setter", + "childItems": [ + { + "text": "f", + "kind": "function" + } + ], + "indent": 2 + } +]); diff --git a/tests/cases/fourslash/navigationBarImports.ts b/tests/cases/fourslash/navigationBarImports.ts new file mode 100644 index 00000000000..43cb99c556a --- /dev/null +++ b/tests/cases/fourslash/navigationBarImports.ts @@ -0,0 +1,30 @@ +/// + +////import a, {b} from "m"; +////import c = require("m"); +////import * as d from "m"; + +verify.navigationBar([ + { + "text": "\"navigationBarImports\"", + "kind": "module", + "childItems": [ + { + "text": "a", + "kind": "alias" + }, + { + "text": "b", + "kind": "alias" + }, + { + "text": "c", + "kind": "alias" + }, + { + "text": "d", + "kind": "alias" + } + ] + } +]); diff --git a/tests/cases/fourslash/navigationBarItemsFunctions.ts b/tests/cases/fourslash/navigationBarItemsFunctions.ts index 91546462a25..f5aa3fb681d 100644 --- a/tests/cases/fourslash/navigationBarItemsFunctions.ts +++ b/tests/cases/fourslash/navigationBarItemsFunctions.ts @@ -32,6 +32,12 @@ verify.navigationBar([ { "text": "baz", "kind": "function", + "childItems": [ + { + "text": "v", + "kind": "var" + } + ], "indent": 1 }, { @@ -41,6 +47,10 @@ verify.navigationBar([ { "text": "bar", "kind": "function" + }, + { + "text": "x", + "kind": "var" } ], "indent": 1 @@ -52,6 +62,10 @@ verify.navigationBar([ { "text": "biz", "kind": "function" + }, + { + "text": "y", + "kind": "var" } ], "indent": 2 diff --git a/tests/cases/fourslash/navigationBarItemsFunctionsBroken.ts b/tests/cases/fourslash/navigationBarItemsFunctionsBroken.ts index 96dcbb944ae..2f17a5006ff 100644 --- a/tests/cases/fourslash/navigationBarItemsFunctionsBroken.ts +++ b/tests/cases/fourslash/navigationBarItemsFunctionsBroken.ts @@ -18,6 +18,12 @@ verify.navigationBar([ { "text": "f", "kind": "function", + "childItems": [ + { + "text": "", + "kind": "function" + } + ], "indent": 1 } ]); diff --git a/tests/cases/fourslash/navigationBarItemsFunctionsBroken2.ts b/tests/cases/fourslash/navigationBarItemsFunctionsBroken2.ts index 127bde8c9e7..f907bdad067 100644 --- a/tests/cases/fourslash/navigationBarItemsFunctionsBroken2.ts +++ b/tests/cases/fourslash/navigationBarItemsFunctionsBroken2.ts @@ -10,6 +10,10 @@ verify.navigationBar([ "text": "", "kind": "module", "childItems": [ + { + "text": "", + "kind": "function" + }, { "text": "f", "kind": "function" @@ -19,6 +23,12 @@ verify.navigationBar([ { "text": "f", "kind": "function", + "childItems": [ + { + "text": "", + "kind": "function" + } + ], "indent": 1 } ]); diff --git a/tests/cases/fourslash/navigationBarItemsInsideMethodsAndConstructors.ts b/tests/cases/fourslash/navigationBarItemsInsideMethodsAndConstructors.ts index 28ec25c6f1d..3af3b351114 100644 --- a/tests/cases/fourslash/navigationBarItemsInsideMethodsAndConstructors.ts +++ b/tests/cases/fourslash/navigationBarItemsInsideMethodsAndConstructors.ts @@ -76,17 +76,17 @@ verify.navigationBar([ "kind": "property" } ], - "indent": 2 + "indent": 3 }, { "text": "LocalFunctionInConstructor", "kind": "function", - "indent": 2 + "indent": 3 }, { "text": "LocalInterfaceInConstrcutor", "kind": "interface", - "indent": 2 + "indent": 3 }, { "text": "method", @@ -116,7 +116,7 @@ verify.navigationBar([ "kind": "property" } ], - "indent": 2 + "indent": 3 }, { "text": "LocalFunctionInMethod", @@ -127,11 +127,11 @@ verify.navigationBar([ "kind": "function" } ], - "indent": 2 + "indent": 3 }, { "text": "LocalInterfaceInMethod", "kind": "interface", - "indent": 2 + "indent": 3 } ]); diff --git a/tests/cases/fourslash/navigationBarItemsItems2.ts b/tests/cases/fourslash/navigationBarItemsItems2.ts index 762531d8778..6ab0827e8f9 100644 --- a/tests/cases/fourslash/navigationBarItemsItems2.ts +++ b/tests/cases/fourslash/navigationBarItemsItems2.ts @@ -12,6 +12,11 @@ verify.navigationBar([ "text": "\"navigationBarItemsItems2\"", "kind": "module", "childItems": [ + { + "text": "", + "kind": "class", + "kindModifiers": "export" + }, { "text": "A", "kind": "module" @@ -19,7 +24,7 @@ verify.navigationBar([ ] }, { - "text": "default", + "text": "", "kind": "class", "kindModifiers": "export", "indent": 1 diff --git a/tests/cases/fourslash/navigationBarItemsItemsContainsNoAnonymousFunctions.ts b/tests/cases/fourslash/navigationBarItemsItemsContainsNoAnonymousFunctions.ts deleted file mode 100644 index 276e19624c9..00000000000 --- a/tests/cases/fourslash/navigationBarItemsItemsContainsNoAnonymousFunctions.ts +++ /dev/null @@ -1,80 +0,0 @@ -/// -// @Filename: scriptLexicalStructureItemsContainsNoAnonymouseFunctions_0.ts -/////*file1*/ -////(function() { -//// // this should not be included -//// var x = 0; -//// -//// // this should not be included either -//// function foo() { -//// -//// } -////})(); -//// -// @Filename: scriptLexicalStructureItemsContainsNoAnonymouseFunctions_1.ts -/////*file2*/ -////var x = function() { -//// // this should not be included -//// var x = 0; -//// -//// // this should not be included either -//// function foo() { -////}; -//// -// @Filename: scriptLexicalStructureItemsContainsNoAnonymouseFunctions_2.ts -////// Named functions should still show up -/////*file3*/ -////function foo() { -////} -////function bar() { -////} - -goTo.marker("file1"); -verify.navigationBar([ - { - "text": "", - "kind": "module" - } -]); - -goTo.marker("file2"); -verify.navigationBar([ - { - "text": "", - "kind": "module", - "childItems": [ - { - "text": "x", - "kind": "var" - } - ] - } -]); - -goTo.marker("file3"); -verify.navigationBar([ - { - "text": "", - "kind": "module", - "childItems": [ - { - "text": "bar", - "kind": "function" - }, - { - "text": "foo", - "kind": "function" - } - ] - }, - { - "text": "bar", - "kind": "function", - "indent": 1 - }, - { - "text": "foo", - "kind": "function", - "indent": 1 - } -]); diff --git a/tests/cases/fourslash/navigationBarItemsMissingName1.ts b/tests/cases/fourslash/navigationBarItemsMissingName1.ts index af0e89683bc..2a445a5e1ed 100644 --- a/tests/cases/fourslash/navigationBarItemsMissingName1.ts +++ b/tests/cases/fourslash/navigationBarItemsMissingName1.ts @@ -8,6 +8,11 @@ verify.navigationBar([ "text": "\"navigationBarItemsMissingName1\"", "kind": "module", "childItems": [ + { + "text": "", + "kind": "function", + "kindModifiers": "export" + }, { "text": "C", "kind": "class" diff --git a/tests/cases/fourslash/navigationBarItemsMissingName2.ts b/tests/cases/fourslash/navigationBarItemsMissingName2.ts index 2eda3b07855..a878c5be845 100644 --- a/tests/cases/fourslash/navigationBarItemsMissingName2.ts +++ b/tests/cases/fourslash/navigationBarItemsMissingName2.ts @@ -9,10 +9,16 @@ verify.navigationBar([ { "text": "", - "kind": "module" + "kind": "module", + "childItems": [ + { + "text": "", + "kind": "class" + } + ] }, { - "text": "default", + "text": "", "kind": "class", "childItems": [ { diff --git a/tests/cases/fourslash/navigationBarItemsModules.ts b/tests/cases/fourslash/navigationBarItemsModules.ts index 979f22084e5..ec11cd52b94 100644 --- a/tests/cases/fourslash/navigationBarItemsModules.ts +++ b/tests/cases/fourslash/navigationBarItemsModules.ts @@ -28,107 +28,107 @@ //The declarations of A.B.C.x do not get merged, so the 4 vars are independent. //The two 'A' modules, however, do get merged, so in reality we have 7 modules. verify.navigationBar([ - { - "text": "", - "kind": "module", - "childItems": [ - { - "text": "A.B.C", - "kind": "module" - }, - { - "text": "A.B", - "kind": "module" - }, - { - "text": "A", - "kind": "module" - }, - { - "text": "\"X.Y.Z\"", + { + "text": "", "kind": "module", - "kindModifiers": "declare" - }, - { + "childItems": [ + { + "text": "'X2.Y2.Z2'", + "kind": "module", + "kindModifiers": "declare" + }, + { + "text": "\"X.Y.Z\"", + "kind": "module", + "kindModifiers": "declare" + }, + { + "text": "A", + "kind": "module" + }, + { + "text": "A.B", + "kind": "module" + }, + { + "text": "A.B.C", + "kind": "module" + } + ] + }, + { "text": "'X2.Y2.Z2'", "kind": "module", - "kindModifiers": "declare" - } - ] - }, - { - "text": "A.B.C", - "kind": "module", - "childItems": [ - { - "text": "x", - "kind": "var", - "kindModifiers": "export" - } - ], - "indent": 1 - }, - { - "text": "A.B", - "kind": "module", - "childItems": [ - { - "text": "y", - "kind": "var", - "kindModifiers": "export" - } - ], - "indent": 1 - }, - { - "text": "A", - "kind": "module", - "childItems": [ - { - "text": "z", - "kind": "var", - "kindModifiers": "export" - }, - { + "kindModifiers": "declare", + "indent": 1 + }, + { + "text": "\"X.Y.Z\"", + "kind": "module", + "kindModifiers": "declare", + "indent": 1 + }, + { + "text": "A", + "kind": "module", + "childItems": [ + { + "text": "B", + "kind": "module" + }, + { + "text": "z", + "kind": "var", + "kindModifiers": "export" + } + ], + "indent": 1 + }, + { "text": "B", - "kind": "module" - } - ], - "indent": 1 - }, - { - "text": "B", - "kind": "module", - "childItems": [ - { + "kind": "module", + "childItems": [ + { + "text": "C", + "kind": "module" + } + ], + "indent": 2 + }, + { "text": "C", - "kind": "module" - } - ], - "indent": 2 - }, - { - "text": "C", - "kind": "module", - "childItems": [ - { - "text": "x", - "kind": "var", - "kindModifiers": "declare" - } - ], - "indent": 3 - }, - { - "text": "\"X.Y.Z\"", - "kind": "module", - "kindModifiers": "declare", - "indent": 1 - }, - { - "text": "'X2.Y2.Z2'", - "kind": "module", - "kindModifiers": "declare", - "indent": 1 - } + "kind": "module", + "childItems": [ + { + "text": "x", + "kind": "var", + "kindModifiers": "declare" + } + ], + "indent": 3 + }, + { + "text": "A.B", + "kind": "module", + "childItems": [ + { + "text": "y", + "kind": "var", + "kindModifiers": "export" + } + ], + "indent": 1 + }, + { + "text": "A.B.C", + "kind": "module", + "childItems": [ + { + "text": "x", + "kind": "var", + "kindModifiers": "export" + } + ], + "indent": 1 + } ]); diff --git a/tests/cases/fourslash/navigationBarItemsMultilineStringIdentifiers.ts b/tests/cases/fourslash/navigationBarItemsMultilineStringIdentifiers.ts index 2ee4c93a2f1..4a29d718f2e 100644 --- a/tests/cases/fourslash/navigationBarItemsMultilineStringIdentifiers.ts +++ b/tests/cases/fourslash/navigationBarItemsMultilineStringIdentifiers.ts @@ -30,12 +30,9 @@ verify.navigationBar([ "kind": "module", "childItems": [ { - "text": "Bar", - "kind": "class" - }, - { - "text": "Foo", - "kind": "interface" + "text": "\"Multiline\\\nMadness\"", + "kind": "module", + "kindModifiers": "declare" }, { "text": "\"Multiline\\r\\nMadness\"", @@ -43,17 +40,38 @@ verify.navigationBar([ "kindModifiers": "declare" }, { - "text": "\"Multiline\\\nMadness\"", + "text": "\"MultilineMadness\"", "kind": "module", "kindModifiers": "declare" }, { - "text": "\"MultilineMadness\"", - "kind": "module", - "kindModifiers": "declare" + "text": "Bar", + "kind": "class" + }, + { + "text": "Foo", + "kind": "interface" } ] }, + { + "text": "\"Multiline\\\nMadness\"", + "kind": "module", + "kindModifiers": "declare", + "indent": 1 + }, + { + "text": "\"Multiline\\r\\nMadness\"", + "kind": "module", + "kindModifiers": "declare", + "indent": 1 + }, + { + "text": "\"MultilineMadness\"", + "kind": "module", + "kindModifiers": "declare", + "indent": 1 + }, { "text": "Bar", "kind": "class", @@ -83,23 +101,5 @@ verify.navigationBar([ } ], "indent": 1 - }, - { - "text": "\"Multiline\\r\\nMadness\"", - "kind": "module", - "kindModifiers": "declare", - "indent": 1 - }, - { - "text": "\"Multiline\\\nMadness\"", - "kind": "module", - "kindModifiers": "declare", - "indent": 1 - }, - { - "text": "\"MultilineMadness\"", - "kind": "module", - "kindModifiers": "declare", - "indent": 1 } ]); diff --git a/tests/cases/fourslash/navigationBarJsDoc.ts b/tests/cases/fourslash/navigationBarJsDoc.ts index 20a235bce95..a2d33e216bf 100644 --- a/tests/cases/fourslash/navigationBarJsDoc.ts +++ b/tests/cases/fourslash/navigationBarJsDoc.ts @@ -6,16 +6,32 @@ ////const x = 0; verify.navigationBar([ + { + "text": "", + "kind": "module", + "childItems": [ + { + "text": "NumberLike", + "kind": "type" + }, + { + "text": "x", + "kind": "const" + }, + { + "text": "x", + "kind": "type" + } + ] + }, { "text": "NumberLike", - "kind": "type" + "kind": "type", + "indent": 1, }, { "text": "x", - "kind": "type" - }, - { - "text": "x", - "kind": "var" + "kind": "type", + "indent": 1 } ]); diff --git a/tests/cases/fourslash/navigationBarMerging.ts b/tests/cases/fourslash/navigationBarMerging.ts new file mode 100644 index 00000000000..192efe5db44 --- /dev/null +++ b/tests/cases/fourslash/navigationBarMerging.ts @@ -0,0 +1,189 @@ +/// + +// @Filename: file1.ts +////module a { +//// function foo() {} +////} +////module b { +//// function foo() {} +////} +////module a { +//// function bar() {} +////} + +verify.navigationBar([ + { + "text": "", + "kind": "module", + "childItems": [ + { + "text": "a", + "kind": "module" + }, + { + "text": "b", + "kind": "module" + } + ] + }, + { + "text": "a", + "kind": "module", + "childItems": [ + { + "text": "bar", + "kind": "function" + }, + { + "text": "foo", + "kind": "function" + } + ], + "indent": 1 + }, + { + "text": "b", + "kind": "module", + "childItems": [ + { + "text": "foo", + "kind": "function" + } + ], + "indent": 1 + } +]); + +// Does not merge unlike declarations. +// @Filename: file2.ts +////module a {} +////function a() {} + +goTo.file("file2.ts"); +verify.navigationBar([ + { + "text": "", + "kind": "module", + "childItems": [ + { + "text": "a", + "kind": "function" + }, + { + "text": "a", + "kind": "module" + } + ] + }, + { + "text": "a", + "kind": "function", + "indent": 1 + }, + { + "text": "a", + "kind": "module", + "indent": 1 + } +]); + +// Merges recursively +// @Filename: file3.ts +////module a { +//// interface A { +//// foo: number; +//// } +////} +////module a { +//// interface A { +//// bar: number; +//// } +////} + +goTo.file("file3.ts"); +verify.navigationBar([ + { + "text": "", + "kind": "module", + "childItems": [ + { + "text": "a", + "kind": "module" + } + ] + }, + { + "text": "a", + "kind": "module", + "childItems": [ + { + "text": "A", + "kind": "interface" + } + ], + "indent": 1 + }, + { + "text": "A", + "kind": "interface", + "childItems": [ + { + "text": "bar", + "kind": "property" + }, + { + "text": "foo", + "kind": "property" + } + ], + "indent": 2 + } +]); + +// Does not merge 'module A' with 'module A.B' + +// @Filename: file4.ts +////module A { export var x; } +////module A.B { export var y; } + +goTo.file("file4.ts"); +verify.navigationBar([ + { + "text": "", + "kind": "module", + "childItems": [ + { + "text": "A", + "kind": "module" + }, + { + "text": "A.B", + "kind": "module" + } + ] + }, + { + "text": "A", + "kind": "module", + "childItems": [ + { + "text": "x", + "kind": "var", + "kindModifiers": "export" + } + ], + "indent": 1 + }, + { + "text": "A.B", + "kind": "module", + "childItems": [ + { + "text": "y", + "kind": "var", + "kindModifiers": "export" + } + ], + "indent": 1 + } +]); diff --git a/tests/cases/fourslash/navigationBarVariables.ts b/tests/cases/fourslash/navigationBarVariables.ts new file mode 100644 index 00000000000..42344c96f73 --- /dev/null +++ b/tests/cases/fourslash/navigationBarVariables.ts @@ -0,0 +1,53 @@ +/// + +////var x = 0; +////let y = 1; +////const z = 2; + +verify.navigationBar([ + { + "text": "", + "kind": "module", + "childItems": [ + { + "text": "x", + "kind": "var" + }, + { + "text": "y", + "kind": "let" + }, + { + "text": "z", + "kind": "const" + } + ] + } +]); + +// @Filename: file2.ts +////var {a} = 0; +////let {a: b} = 0; +////const [c] = 0; + +goTo.file("file2.ts"); +verify.navigationBar([ + { + "text": "", + "kind": "module", + "childItems": [ + { + "text": "a", + "kind": "var" + }, + { + "text": "b", + "kind": "let" + }, + { + "text": "c", + "kind": "const" + } + ] + } +]); diff --git a/tests/cases/fourslash/server/jsdocTypedefTagNavigateTo.ts b/tests/cases/fourslash/server/jsdocTypedefTagNavigateTo.ts index 77cd75aa44c..1c617091f4c 100644 --- a/tests/cases/fourslash/server/jsdocTypedefTagNavigateTo.ts +++ b/tests/cases/fourslash/server/jsdocTypedefTagNavigateTo.ts @@ -11,20 +11,36 @@ //// var numberLike; verify.navigationBar([ - { - "text": "NumberLike", - "kind": "type" - }, - { - "text": "NumberLike2", - "kind": "type" - }, - { - "text": "NumberLike2", - "kind": "var" - }, - { - "text": "numberLike", - "kind": "var" - } + { + "text": "", + "kind": "module", + "childItems": [ + { + "text": "numberLike", + "kind": "var" + }, + { + "text": "NumberLike", + "kind": "type" + }, + { + "text": "NumberLike2", + "kind": "var" + }, + { + "text": "NumberLike2", + "kind": "type" + } + ] + }, + { + "text": "NumberLike", + "kind": "type", + "indent": 1, + }, + { + "text": "NumberLike2", + "kind": "type", + "indent": 1 + } ]); \ No newline at end of file From 48a340f43caeeb38949b77af3569ff2ea9549b36 Mon Sep 17 00:00:00 2001 From: zhengbli Date: Fri, 17 Jun 2016 13:02:26 -0700 Subject: [PATCH 49/56] routine dom update --- src/lib/dom.generated.d.ts | 664 ++++++++++++++++++++++++++++--- src/lib/webworker.generated.d.ts | 203 +++++++++- 2 files changed, 793 insertions(+), 74 deletions(-) diff --git a/src/lib/dom.generated.d.ts b/src/lib/dom.generated.d.ts index 5865e485754..40cd883af61 100644 --- a/src/lib/dom.generated.d.ts +++ b/src/lib/dom.generated.d.ts @@ -4,7 +4,7 @@ ///////////////////////////// interface Algorithm { - name?: string; + name: string; } interface AriaRequestEventInit extends EventInit { @@ -851,6 +851,7 @@ interface UIEventInit extends EventInit { } interface WebGLContextAttributes { + failIfMajorPerformanceCaveat?: boolean; alpha?: boolean; depth?: boolean; stencil?: boolean; @@ -919,7 +920,7 @@ interface ApplicationCache extends EventTarget { oncached: (ev: Event) => any; onchecking: (ev: Event) => any; ondownloading: (ev: Event) => any; - onerror: (ev: Event) => any; + onerror: (ev: ErrorEvent) => any; onnoupdate: (ev: Event) => any; onobsolete: (ev: Event) => any; onprogress: (ev: ProgressEvent) => any; @@ -2410,7 +2411,7 @@ interface Document extends Node, GlobalEventHandlers, NodeSelector, DocumentEven * Fires when the user aborts the download. * @param ev The event. */ - onabort: (ev: Event) => any; + onabort: (ev: UIEvent) => any; /** * Fires when the object is set as the active element. * @param ev The event. @@ -2512,7 +2513,7 @@ interface Document extends Node, GlobalEventHandlers, NodeSelector, DocumentEven * Fires when an error occurs during object loading. * @param ev The event. */ - onerror: (ev: Event) => any; + onerror: (ev: ErrorEvent) => any; /** * Fires when the object receives focus. * @param ev The event. @@ -2988,7 +2989,7 @@ interface Document extends Node, GlobalEventHandlers, NodeSelector, DocumentEven * Returns a reference to the first object with the specified value of the ID or NAME attribute. * @param elementId String that specifies the ID value. Case-insensitive. */ - getElementById(elementId: string): HTMLElement; + getElementById(elementId: string): HTMLElement | null; getElementsByClassName(classNames: string): HTMLCollectionOf; /** * Gets a collection of objects based on the value of the NAME or ID attribute. @@ -3463,7 +3464,7 @@ interface Element extends Node, GlobalEventHandlers, ElementTraversal, NodeSelec readonly scrollWidth: number; readonly tagName: string; innerHTML: string; - getAttribute(name?: string): string | null; + getAttribute(name: string): string | null; getAttributeNS(namespaceURI: string, localName: string): string; getAttributeNode(name: string): Attr; getAttributeNodeNS(namespaceURI: string, localName: string): Attr; @@ -3673,6 +3674,7 @@ interface Element extends Node, GlobalEventHandlers, ElementTraversal, NodeSelec webkitRequestFullscreen(): void; getElementsByClassName(classNames: string): NodeListOf; matches(selector: string): boolean; + closest(selector: string): Element | null; addEventListener(type: "MSGestureChange", listener: (ev: MSGestureEvent) => any, useCapture?: boolean): void; addEventListener(type: "MSGestureDoubleTap", listener: (ev: MSGestureEvent) => any, useCapture?: boolean): void; addEventListener(type: "MSGestureEnd", listener: (ev: MSGestureEvent) => any, useCapture?: boolean): void; @@ -4203,7 +4205,7 @@ interface HTMLBodyElement extends HTMLElement { onbeforeprint: (ev: Event) => any; onbeforeunload: (ev: BeforeUnloadEvent) => any; onblur: (ev: FocusEvent) => any; - onerror: (ev: Event) => any; + onerror: (ev: ErrorEvent) => any; onfocus: (ev: FocusEvent) => any; onhashchange: (ev: HashChangeEvent) => any; onload: (ev: Event) => any; @@ -4432,9 +4434,9 @@ interface HTMLCanvasElement extends HTMLElement { * Returns an object that provides methods and properties for drawing and manipulating images and graphics on a canvas element in a document. A context object includes information about colors, line widths, fonts, and other graphic parameters that can be drawn on a canvas. * @param contextId The identifier (ID) of the type of canvas to create. Internet Explorer 9 and Internet Explorer 10 support only a 2-D context using canvas.getContext("2d"); IE11 Preview also supports 3-D or WebGL context using canvas.getContext("experimental-webgl"); */ - getContext(contextId: "2d"): CanvasRenderingContext2D; - getContext(contextId: "experimental-webgl"): WebGLRenderingContext; - getContext(contextId: string, ...args: any[]): CanvasRenderingContext2D | WebGLRenderingContext; + getContext(contextId: "2d", contextAttributes?: Canvas2DContextAttributes): CanvasRenderingContext2D | null; + getContext(contextId: "webgl" | "experimental-webgl", contextAttributes?: WebGLContextAttributes): WebGLRenderingContext | null; + getContext(contextId: string, contextAttributes?: {}): CanvasRenderingContext2D | WebGLRenderingContext | null; /** * Returns a blob object encoded as a Portable Network Graphics (PNG) format from a canvas image or drawing. */ @@ -4444,7 +4446,7 @@ interface HTMLCanvasElement extends HTMLElement { * @param type The standard MIME type for the image format to return. If you do not specify this parameter, the default value is a PNG format image. */ toDataURL(type?: string, ...args: any[]): string; - toBlob(): Blob; + toBlob(callback: (result: Blob | null) => void, ... arguments: any[]): void; } declare var HTMLCanvasElement: { @@ -4542,7 +4544,7 @@ interface HTMLElement extends Element { readonly offsetParent: Element; readonly offsetTop: number; readonly offsetWidth: number; - onabort: (ev: Event) => any; + onabort: (ev: UIEvent) => any; onactivate: (ev: UIEvent) => any; onbeforeactivate: (ev: UIEvent) => any; onbeforecopy: (ev: ClipboardEvent) => any; @@ -4570,7 +4572,7 @@ interface HTMLElement extends Element { ondurationchange: (ev: Event) => any; onemptied: (ev: Event) => any; onended: (ev: MediaStreamErrorEvent) => any; - onerror: (ev: Event) => any; + onerror: (ev: ErrorEvent) => any; onfocus: (ev: FocusEvent) => any; oninput: (ev: Event) => any; oninvalid: (ev: Event) => any; @@ -5120,7 +5122,7 @@ interface HTMLFrameSetElement extends HTMLElement { * Fires when the object loses the input focus. */ onblur: (ev: FocusEvent) => any; - onerror: (ev: Event) => any; + onerror: (ev: ErrorEvent) => any; /** * Fires when the object receives focus. */ @@ -5643,7 +5645,7 @@ interface HTMLInputElement extends HTMLElement { /** * Returns a FileList object on a file type input object. */ - readonly files: FileList; + readonly files: FileList | null; /** * Retrieves a reference to the form that the object is embedded in. */ @@ -6614,7 +6616,7 @@ declare var HTMLOptionElement: { create(): HTMLOptionElement; } -interface HTMLOptionsCollection extends HTMLCollection { +interface HTMLOptionsCollection extends HTMLCollectionOf { length: number; selectedIndex: number; add(element: HTMLOptionElement | HTMLOptGroupElement, before?: HTMLElement | number): void; @@ -6778,7 +6780,7 @@ interface HTMLSelectElement extends HTMLElement { * Sets or retrieves the name of the object. */ name: string; - options: HTMLCollectionOf; + readonly options: HTMLOptionsCollection; /** * When present, marks an element that can't be submitted without a value. */ @@ -7574,7 +7576,7 @@ interface IDBDatabase extends EventTarget { readonly name: string; readonly objectStoreNames: DOMStringList; onabort: (ev: Event) => any; - onerror: (ev: Event) => any; + onerror: (ev: ErrorEvent) => any; version: number; onversionchange: (ev: IDBVersionChangeEvent) => any; close(): void; @@ -7677,7 +7679,7 @@ declare var IDBOpenDBRequest: { interface IDBRequest extends EventTarget { readonly error: DOMError; - onerror: (ev: Event) => any; + onerror: (ev: ErrorEvent) => any; onsuccess: (ev: Event) => any; readonly readyState: string; readonly result: any; @@ -7699,7 +7701,7 @@ interface IDBTransaction extends EventTarget { readonly mode: string; onabort: (ev: Event) => any; oncomplete: (ev: Event) => any; - onerror: (ev: Event) => any; + onerror: (ev: ErrorEvent) => any; abort(): void; objectStore(name: string): IDBObjectStore; readonly READ_ONLY: string; @@ -7842,7 +7844,7 @@ declare var MSApp: MSApp; interface MSAppAsyncOperation extends EventTarget { readonly error: DOMError; oncomplete: (ev: Event) => any; - onerror: (ev: Event) => any; + onerror: (ev: ErrorEvent) => any; readonly readyState: number; readonly result: any; start(): void; @@ -8202,7 +8204,7 @@ declare var MSStreamReader: { interface MSWebViewAsyncOperation extends EventTarget { readonly error: DOMError; oncomplete: (ev: Event) => any; - onerror: (ev: Event) => any; + onerror: (ev: ErrorEvent) => any; readonly readyState: number; readonly result: any; readonly target: MSHTMLWebViewElement; @@ -8738,7 +8740,7 @@ interface Node extends EventTarget { contains(child: Node): boolean; hasAttributes(): boolean; hasChildNodes(): boolean; - insertBefore(newChild: Node, refChild: Node): Node; + insertBefore(newChild: Node, refChild: Node | null): Node; isDefaultNamespace(namespaceURI: string | null): boolean; isEqualNode(arg: Node): boolean; isSameNode(other: Node): boolean; @@ -8747,7 +8749,6 @@ interface Node extends EventTarget { normalize(): void; removeChild(oldChild: Node): Node; replaceChild(newChild: Node, oldChild: Node): Node; - contains(node: Node): boolean; readonly ATTRIBUTE_NODE: number; readonly CDATA_SECTION_NODE: number; readonly COMMENT_NODE: number; @@ -9283,7 +9284,7 @@ declare var RTCDTMFToneChangeEvent: { interface RTCDtlsTransport extends RTCStatsProvider { ondtlsstatechange: ((ev: RTCDtlsTransportStateChangedEvent) => any) | null; - onerror: ((ev: Event) => any) | null; + onerror: ((ev: ErrorEvent) => any) | null; readonly state: string; readonly transport: RTCIceTransport; getLocalParameters(): RTCDtlsParameters; @@ -9338,7 +9339,7 @@ declare var RTCIceCandidatePairChangedEvent: { interface RTCIceGatherer extends RTCStatsProvider { readonly component: string; - onerror: ((ev: Event) => any) | null; + onerror: ((ev: ErrorEvent) => any) | null; onlocalcandidate: ((ev: RTCIceGathererEvent) => any) | null; createAssociatedGatherer(): RTCIceGatherer; getLocalCandidates(): RTCIceCandidate[]; @@ -9397,7 +9398,7 @@ declare var RTCIceTransportStateChangedEvent: { } interface RTCRtpReceiver extends RTCStatsProvider { - onerror: ((ev: Event) => any) | null; + onerror: ((ev: ErrorEvent) => any) | null; readonly rtcpTransport: RTCDtlsTransport; readonly track: MediaStreamTrack | null; readonly transport: RTCDtlsTransport | RTCSrtpSdesTransport; @@ -9417,7 +9418,7 @@ declare var RTCRtpReceiver: { } interface RTCRtpSender extends RTCStatsProvider { - onerror: ((ev: Event) => any) | null; + onerror: ((ev: ErrorEvent) => any) | null; onssrcconflict: ((ev: RTCSsrcConflictEvent) => any) | null; readonly rtcpTransport: RTCDtlsTransport; readonly track: MediaStreamTrack; @@ -9438,7 +9439,7 @@ declare var RTCRtpSender: { } interface RTCSrtpSdesTransport extends EventTarget { - onerror: ((ev: Event) => any) | null; + onerror: ((ev: ErrorEvent) => any) | null; readonly transport: RTCIceTransport; addEventListener(type: "error", listener: (ev: ErrorEvent) => any, useCapture?: boolean): void; addEventListener(type: string, listener: EventListenerOrEventListenerObject, useCapture?: boolean): void; @@ -11478,18 +11479,24 @@ declare var StyleSheetPageList: { } interface SubtleCrypto { - decrypt(algorithm: string | Algorithm, key: CryptoKey, data: ArrayBufferView): PromiseLike; - deriveBits(algorithm: string | Algorithm, baseKey: CryptoKey, length: number): PromiseLike; - deriveKey(algorithm: string | Algorithm, baseKey: CryptoKey, derivedKeyType: string | Algorithm, extractable: boolean, keyUsages: string[]): PromiseLike; - digest(algorithm: string | Algorithm, data: ArrayBufferView): PromiseLike; - encrypt(algorithm: string | Algorithm, key: CryptoKey, data: ArrayBufferView): PromiseLike; - exportKey(format: string, key: CryptoKey): PromiseLike; - generateKey(algorithm: string | Algorithm, extractable: boolean, keyUsages: string[]): PromiseLike; - importKey(format: string, keyData: ArrayBufferView, algorithm: string | Algorithm | null, extractable: boolean, keyUsages: string[]): PromiseLike; - sign(algorithm: string | Algorithm, key: CryptoKey, data: ArrayBufferView): PromiseLike; - unwrapKey(format: string, wrappedKey: ArrayBufferView, unwrappingKey: CryptoKey, unwrapAlgorithm: string | Algorithm, unwrappedKeyAlgorithm: string | Algorithm | null, extractable: boolean, keyUsages: string[]): PromiseLike; - verify(algorithm: string | Algorithm, key: CryptoKey, signature: ArrayBufferView, data: ArrayBufferView): PromiseLike; - wrapKey(format: string, key: CryptoKey, wrappingKey: CryptoKey, wrapAlgorithm: string | Algorithm): PromiseLike; + decrypt(algorithm: string | RsaOaepParams | AesCtrParams | AesCbcParams | AesCmacParams | AesGcmParams | AesCfbParams, key: CryptoKey, data: BufferSource): PromiseLike; + deriveBits(algorithm: string | EcdhKeyDeriveParams | DhKeyDeriveParams | ConcatParams | HkdfCtrParams | Pbkdf2Params, baseKey: CryptoKey, length: number): PromiseLike; + deriveKey(algorithm: string | EcdhKeyDeriveParams | DhKeyDeriveParams | ConcatParams | HkdfCtrParams | Pbkdf2Params, baseKey: CryptoKey, derivedKeyType: string | AesDerivedKeyParams | HmacImportParams | ConcatParams | HkdfCtrParams | Pbkdf2Params, extractable: boolean, keyUsages: string[]): PromiseLike; + digest(algorithm: AlgorithmIdentifier, data: BufferSource): PromiseLike; + encrypt(algorithm: string | RsaOaepParams | AesCtrParams | AesCbcParams | AesCmacParams | AesGcmParams | AesCfbParams, key: CryptoKey, data: BufferSource): PromiseLike; + exportKey(format: "jwk", key: CryptoKey): PromiseLike; + exportKey(format: "raw" | "pkcs8" | "spki", key: CryptoKey): PromiseLike; + exportKey(format: string, key: CryptoKey): PromiseLike; + generateKey(algorithm: string, extractable: boolean, keyUsages: string[]): PromiseLike; + generateKey(algorithm: RsaHashedKeyGenParams | EcKeyGenParams | DhKeyGenParams, extractable: boolean, keyUsages: string[]): PromiseLike; + generateKey(algorithm: AesKeyGenParams | HmacKeyGenParams | Pbkdf2Params, extractable: boolean, keyUsages: string[]): PromiseLike; + importKey(format: "jwk", keyData: JsonWebKey, algorithm: string | RsaHashedImportParams | EcKeyImportParams | HmacImportParams | DhImportKeyParams, extractable:boolean, keyUsages: string[]): PromiseLike; + importKey(format: "raw" | "pkcs8" | "spki", keyData: BufferSource, algorithm: string | RsaHashedImportParams | EcKeyImportParams | HmacImportParams | DhImportKeyParams, extractable:boolean, keyUsages: string[]): PromiseLike; + importKey(format: string, keyData: JsonWebKey | BufferSource, algorithm: string | RsaHashedImportParams | EcKeyImportParams | HmacImportParams | DhImportKeyParams, extractable:boolean, keyUsages: string[]): PromiseLike; + sign(algorithm: string | RsaPssParams | EcdsaParams | AesCmacParams, key: CryptoKey, data: BufferSource): PromiseLike; + unwrapKey(format: string, wrappedKey: BufferSource, unwrappingKey: CryptoKey, unwrapAlgorithm: AlgorithmIdentifier, unwrappedKeyAlgorithm: AlgorithmIdentifier, extractable: boolean, keyUsages: string[]): PromiseLike; + verify(algorithm: string | RsaPssParams | EcdsaParams | AesCmacParams, key: CryptoKey, signature: BufferSource, data: BufferSource): PromiseLike; + wrapKey(format: string, key: CryptoKey, wrappingKey: CryptoKey, wrapAlgorithm: AlgorithmIdentifier): PromiseLike; } declare var SubtleCrypto: { @@ -11557,7 +11564,7 @@ interface TextTrack extends EventTarget { readonly language: string; mode: any; oncuechange: (ev: Event) => any; - onerror: (ev: Event) => any; + onerror: (ev: ErrorEvent) => any; onload: (ev: Event) => any; readonly readyState: number; addCue(cue: TextTrackCue): void; @@ -12046,18 +12053,12 @@ interface WebGLRenderingContext { stencilMaskSeparate(face: number, mask: number): void; stencilOp(fail: number, zfail: number, zpass: number): void; stencilOpSeparate(face: number, fail: number, zfail: number, zpass: number): void; - texImage2D(target: number, level: number, internalformat: number, width: number, height: number, border: number, format: number, type: number, pixels: ArrayBufferView): void; - texImage2D(target: number, level: number, internalformat: number, format: number, type: number, image: HTMLImageElement): void; - texImage2D(target: number, level: number, internalformat: number, format: number, type: number, canvas: HTMLCanvasElement): void; - texImage2D(target: number, level: number, internalformat: number, format: number, type: number, video: HTMLVideoElement): void; - texImage2D(target: number, level: number, internalformat: number, format: number, type: number, pixels: ImageData): void; + texImage2D(target: number, level: number, internalformat: number, width: number, height: number, border: number, format: number, type: number, pixels?: ArrayBufferView): void; + texImage2D(target: number, level: number, internalformat: number, format: number, type: number, pixels?: ImageData | HTMLVideoElement | HTMLImageElement | HTMLCanvasElement): void; texParameterf(target: number, pname: number, param: number): void; texParameteri(target: number, pname: number, param: number): void; - texSubImage2D(target: number, level: number, xoffset: number, yoffset: number, width: number, height: number, format: number, type: number, pixels: ArrayBufferView): void; - texSubImage2D(target: number, level: number, xoffset: number, yoffset: number, format: number, type: number, image: HTMLImageElement): void; - texSubImage2D(target: number, level: number, xoffset: number, yoffset: number, format: number, type: number, canvas: HTMLCanvasElement): void; - texSubImage2D(target: number, level: number, xoffset: number, yoffset: number, format: number, type: number, video: HTMLVideoElement): void; - texSubImage2D(target: number, level: number, xoffset: number, yoffset: number, format: number, type: number, pixels: ImageData): void; + texSubImage2D(target: number, level: number, xoffset: number, yoffset: number, width: number, height: number, format: number, type: number, pixels?: ArrayBufferView): void; + texSubImage2D(target: number, level: number, xoffset: number, yoffset: number, format: number, type: number, pixels?: ImageData | HTMLVideoElement | HTMLImageElement | HTMLCanvasElement): void; uniform1f(location: WebGLUniformLocation | null, x: number): void; uniform1fv(location: WebGLUniformLocation, v: Float32Array | number[]): void; uniform1i(location: WebGLUniformLocation | null, x: number): void; @@ -12780,7 +12781,7 @@ interface WebSocket extends EventTarget { readonly bufferedAmount: number; readonly extensions: string; onclose: (ev: CloseEvent) => any; - onerror: (ev: Event) => any; + onerror: (ev: ErrorEvent) => any; onmessage: (ev: MessageEvent) => any; onopen: (ev: Event) => any; readonly protocol: string; @@ -12855,7 +12856,7 @@ interface Window extends EventTarget, WindowTimers, WindowSessionStorage, Window name: string; readonly navigator: Navigator; offscreenBuffering: string | boolean; - onabort: (ev: Event) => any; + onabort: (ev: UIEvent) => any; onafterprint: (ev: Event) => any; onbeforeprint: (ev: Event) => any; onbeforeunload: (ev: BeforeUnloadEvent) => any; @@ -12971,6 +12972,7 @@ interface Window extends EventTarget, WindowTimers, WindowSessionStorage, Window readonly top: Window; readonly window: Window; URL: typeof URL; + Blob: typeof Blob; alert(message?: any): void; blur(): void; cancelAnimationFrame(handle: number): void; @@ -13127,7 +13129,6 @@ declare var XMLDocument: { } interface XMLHttpRequest extends EventTarget, XMLHttpRequestEventTarget { - msCaching: string; onreadystatechange: (ev: ProgressEvent) => any; readonly readyState: number; readonly response: any; @@ -13139,6 +13140,7 @@ interface XMLHttpRequest extends EventTarget, XMLHttpRequestEventTarget { timeout: number; readonly upload: XMLHttpRequestUpload; withCredentials: boolean; + msCaching?: string; abort(): void; getAllResponseHeaders(): string; getResponseHeader(header: string): string | null; @@ -13277,7 +13279,7 @@ declare var XSLTProcessor: { } interface AbstractWorker { - onerror: (ev: Event) => any; + onerror: (ev: ErrorEvent) => any; addEventListener(type: "error", listener: (ev: ErrorEvent) => any, useCapture?: boolean): void; addEventListener(type: string, listener: EventListenerOrEventListenerObject, useCapture?: boolean): void; } @@ -13436,7 +13438,7 @@ interface LinkStyle { interface MSBaseReader { onabort: (ev: Event) => any; - onerror: (ev: Event) => any; + onerror: (ev: ErrorEvent) => any; onload: (ev: Event) => any; onloadend: (ev: ProgressEvent) => any; onloadstart: (ev: Event) => any; @@ -13501,7 +13503,359 @@ interface NavigatorUserMedia { } interface NodeSelector { + querySelector(selectors: "a"): HTMLAnchorElement; + querySelector(selectors: "abbr"): HTMLElement; + querySelector(selectors: "acronym"): HTMLElement; + querySelector(selectors: "address"): HTMLElement; + querySelector(selectors: "applet"): HTMLAppletElement; + querySelector(selectors: "area"): HTMLAreaElement; + querySelector(selectors: "article"): HTMLElement; + querySelector(selectors: "aside"): HTMLElement; + querySelector(selectors: "audio"): HTMLAudioElement; + querySelector(selectors: "b"): HTMLElement; + querySelector(selectors: "base"): HTMLBaseElement; + querySelector(selectors: "basefont"): HTMLBaseFontElement; + querySelector(selectors: "bdo"): HTMLElement; + querySelector(selectors: "big"): HTMLElement; + querySelector(selectors: "blockquote"): HTMLQuoteElement; + querySelector(selectors: "body"): HTMLBodyElement; + querySelector(selectors: "br"): HTMLBRElement; + querySelector(selectors: "button"): HTMLButtonElement; + querySelector(selectors: "canvas"): HTMLCanvasElement; + querySelector(selectors: "caption"): HTMLTableCaptionElement; + querySelector(selectors: "center"): HTMLElement; + querySelector(selectors: "circle"): SVGCircleElement; + querySelector(selectors: "cite"): HTMLElement; + querySelector(selectors: "clippath"): SVGClipPathElement; + querySelector(selectors: "code"): HTMLElement; + querySelector(selectors: "col"): HTMLTableColElement; + querySelector(selectors: "colgroup"): HTMLTableColElement; + querySelector(selectors: "datalist"): HTMLDataListElement; + querySelector(selectors: "dd"): HTMLElement; + querySelector(selectors: "defs"): SVGDefsElement; + querySelector(selectors: "del"): HTMLModElement; + querySelector(selectors: "desc"): SVGDescElement; + querySelector(selectors: "dfn"): HTMLElement; + querySelector(selectors: "dir"): HTMLDirectoryElement; + querySelector(selectors: "div"): HTMLDivElement; + querySelector(selectors: "dl"): HTMLDListElement; + querySelector(selectors: "dt"): HTMLElement; + querySelector(selectors: "ellipse"): SVGEllipseElement; + querySelector(selectors: "em"): HTMLElement; + querySelector(selectors: "embed"): HTMLEmbedElement; + querySelector(selectors: "feblend"): SVGFEBlendElement; + querySelector(selectors: "fecolormatrix"): SVGFEColorMatrixElement; + querySelector(selectors: "fecomponenttransfer"): SVGFEComponentTransferElement; + querySelector(selectors: "fecomposite"): SVGFECompositeElement; + querySelector(selectors: "feconvolvematrix"): SVGFEConvolveMatrixElement; + querySelector(selectors: "fediffuselighting"): SVGFEDiffuseLightingElement; + querySelector(selectors: "fedisplacementmap"): SVGFEDisplacementMapElement; + querySelector(selectors: "fedistantlight"): SVGFEDistantLightElement; + querySelector(selectors: "feflood"): SVGFEFloodElement; + querySelector(selectors: "fefunca"): SVGFEFuncAElement; + querySelector(selectors: "fefuncb"): SVGFEFuncBElement; + querySelector(selectors: "fefuncg"): SVGFEFuncGElement; + querySelector(selectors: "fefuncr"): SVGFEFuncRElement; + querySelector(selectors: "fegaussianblur"): SVGFEGaussianBlurElement; + querySelector(selectors: "feimage"): SVGFEImageElement; + querySelector(selectors: "femerge"): SVGFEMergeElement; + querySelector(selectors: "femergenode"): SVGFEMergeNodeElement; + querySelector(selectors: "femorphology"): SVGFEMorphologyElement; + querySelector(selectors: "feoffset"): SVGFEOffsetElement; + querySelector(selectors: "fepointlight"): SVGFEPointLightElement; + querySelector(selectors: "fespecularlighting"): SVGFESpecularLightingElement; + querySelector(selectors: "fespotlight"): SVGFESpotLightElement; + querySelector(selectors: "fetile"): SVGFETileElement; + querySelector(selectors: "feturbulence"): SVGFETurbulenceElement; + querySelector(selectors: "fieldset"): HTMLFieldSetElement; + querySelector(selectors: "figcaption"): HTMLElement; + querySelector(selectors: "figure"): HTMLElement; + querySelector(selectors: "filter"): SVGFilterElement; + querySelector(selectors: "font"): HTMLFontElement; + querySelector(selectors: "footer"): HTMLElement; + querySelector(selectors: "foreignobject"): SVGForeignObjectElement; + querySelector(selectors: "form"): HTMLFormElement; + querySelector(selectors: "frame"): HTMLFrameElement; + querySelector(selectors: "frameset"): HTMLFrameSetElement; + querySelector(selectors: "g"): SVGGElement; + querySelector(selectors: "h1"): HTMLHeadingElement; + querySelector(selectors: "h2"): HTMLHeadingElement; + querySelector(selectors: "h3"): HTMLHeadingElement; + querySelector(selectors: "h4"): HTMLHeadingElement; + querySelector(selectors: "h5"): HTMLHeadingElement; + querySelector(selectors: "h6"): HTMLHeadingElement; + querySelector(selectors: "head"): HTMLHeadElement; + querySelector(selectors: "header"): HTMLElement; + querySelector(selectors: "hgroup"): HTMLElement; + querySelector(selectors: "hr"): HTMLHRElement; + querySelector(selectors: "html"): HTMLHtmlElement; + querySelector(selectors: "i"): HTMLElement; + querySelector(selectors: "iframe"): HTMLIFrameElement; + querySelector(selectors: "image"): SVGImageElement; + querySelector(selectors: "img"): HTMLImageElement; + querySelector(selectors: "input"): HTMLInputElement; + querySelector(selectors: "ins"): HTMLModElement; + querySelector(selectors: "isindex"): HTMLUnknownElement; + querySelector(selectors: "kbd"): HTMLElement; + querySelector(selectors: "keygen"): HTMLElement; + querySelector(selectors: "label"): HTMLLabelElement; + querySelector(selectors: "legend"): HTMLLegendElement; + querySelector(selectors: "li"): HTMLLIElement; + querySelector(selectors: "line"): SVGLineElement; + querySelector(selectors: "lineargradient"): SVGLinearGradientElement; + querySelector(selectors: "link"): HTMLLinkElement; + querySelector(selectors: "listing"): HTMLPreElement; + querySelector(selectors: "map"): HTMLMapElement; + querySelector(selectors: "mark"): HTMLElement; + querySelector(selectors: "marker"): SVGMarkerElement; + querySelector(selectors: "marquee"): HTMLMarqueeElement; + querySelector(selectors: "mask"): SVGMaskElement; + querySelector(selectors: "menu"): HTMLMenuElement; + querySelector(selectors: "meta"): HTMLMetaElement; + querySelector(selectors: "metadata"): SVGMetadataElement; + querySelector(selectors: "meter"): HTMLMeterElement; + querySelector(selectors: "nav"): HTMLElement; + querySelector(selectors: "nextid"): HTMLUnknownElement; + querySelector(selectors: "nobr"): HTMLElement; + querySelector(selectors: "noframes"): HTMLElement; + querySelector(selectors: "noscript"): HTMLElement; + querySelector(selectors: "object"): HTMLObjectElement; + querySelector(selectors: "ol"): HTMLOListElement; + querySelector(selectors: "optgroup"): HTMLOptGroupElement; + querySelector(selectors: "option"): HTMLOptionElement; + querySelector(selectors: "p"): HTMLParagraphElement; + querySelector(selectors: "param"): HTMLParamElement; + querySelector(selectors: "path"): SVGPathElement; + querySelector(selectors: "pattern"): SVGPatternElement; + querySelector(selectors: "picture"): HTMLPictureElement; + querySelector(selectors: "plaintext"): HTMLElement; + querySelector(selectors: "polygon"): SVGPolygonElement; + querySelector(selectors: "polyline"): SVGPolylineElement; + querySelector(selectors: "pre"): HTMLPreElement; + querySelector(selectors: "progress"): HTMLProgressElement; + querySelector(selectors: "q"): HTMLQuoteElement; + querySelector(selectors: "radialgradient"): SVGRadialGradientElement; + querySelector(selectors: "rect"): SVGRectElement; + querySelector(selectors: "rt"): HTMLElement; + querySelector(selectors: "ruby"): HTMLElement; + querySelector(selectors: "s"): HTMLElement; + querySelector(selectors: "samp"): HTMLElement; + querySelector(selectors: "script"): HTMLScriptElement; + querySelector(selectors: "section"): HTMLElement; + querySelector(selectors: "select"): HTMLSelectElement; + querySelector(selectors: "small"): HTMLElement; + querySelector(selectors: "source"): HTMLSourceElement; + querySelector(selectors: "span"): HTMLSpanElement; + querySelector(selectors: "stop"): SVGStopElement; + querySelector(selectors: "strike"): HTMLElement; + querySelector(selectors: "strong"): HTMLElement; + querySelector(selectors: "style"): HTMLStyleElement; + querySelector(selectors: "sub"): HTMLElement; + querySelector(selectors: "sup"): HTMLElement; + querySelector(selectors: "svg"): SVGSVGElement; + querySelector(selectors: "switch"): SVGSwitchElement; + querySelector(selectors: "symbol"): SVGSymbolElement; + querySelector(selectors: "table"): HTMLTableElement; + querySelector(selectors: "tbody"): HTMLTableSectionElement; + querySelector(selectors: "td"): HTMLTableDataCellElement; + querySelector(selectors: "template"): HTMLTemplateElement; + querySelector(selectors: "text"): SVGTextElement; + querySelector(selectors: "textpath"): SVGTextPathElement; + querySelector(selectors: "textarea"): HTMLTextAreaElement; + querySelector(selectors: "tfoot"): HTMLTableSectionElement; + querySelector(selectors: "th"): HTMLTableHeaderCellElement; + querySelector(selectors: "thead"): HTMLTableSectionElement; + querySelector(selectors: "title"): HTMLTitleElement; + querySelector(selectors: "tr"): HTMLTableRowElement; + querySelector(selectors: "track"): HTMLTrackElement; + querySelector(selectors: "tspan"): SVGTSpanElement; + querySelector(selectors: "tt"): HTMLElement; + querySelector(selectors: "u"): HTMLElement; + querySelector(selectors: "ul"): HTMLUListElement; + querySelector(selectors: "use"): SVGUseElement; + querySelector(selectors: "var"): HTMLElement; + querySelector(selectors: "video"): HTMLVideoElement; + querySelector(selectors: "view"): SVGViewElement; + querySelector(selectors: "wbr"): HTMLElement; + querySelector(selectors: "x-ms-webview"): MSHTMLWebViewElement; + querySelector(selectors: "xmp"): HTMLPreElement; querySelector(selectors: string): Element; + querySelectorAll(selectors: "a"): NodeListOf; + querySelectorAll(selectors: "abbr"): NodeListOf; + querySelectorAll(selectors: "acronym"): NodeListOf; + querySelectorAll(selectors: "address"): NodeListOf; + querySelectorAll(selectors: "applet"): NodeListOf; + querySelectorAll(selectors: "area"): NodeListOf; + querySelectorAll(selectors: "article"): NodeListOf; + querySelectorAll(selectors: "aside"): NodeListOf; + querySelectorAll(selectors: "audio"): NodeListOf; + querySelectorAll(selectors: "b"): NodeListOf; + querySelectorAll(selectors: "base"): NodeListOf; + querySelectorAll(selectors: "basefont"): NodeListOf; + querySelectorAll(selectors: "bdo"): NodeListOf; + querySelectorAll(selectors: "big"): NodeListOf; + querySelectorAll(selectors: "blockquote"): NodeListOf; + querySelectorAll(selectors: "body"): NodeListOf; + querySelectorAll(selectors: "br"): NodeListOf; + querySelectorAll(selectors: "button"): NodeListOf; + querySelectorAll(selectors: "canvas"): NodeListOf; + querySelectorAll(selectors: "caption"): NodeListOf; + querySelectorAll(selectors: "center"): NodeListOf; + querySelectorAll(selectors: "circle"): NodeListOf; + querySelectorAll(selectors: "cite"): NodeListOf; + querySelectorAll(selectors: "clippath"): NodeListOf; + querySelectorAll(selectors: "code"): NodeListOf; + querySelectorAll(selectors: "col"): NodeListOf; + querySelectorAll(selectors: "colgroup"): NodeListOf; + querySelectorAll(selectors: "datalist"): NodeListOf; + querySelectorAll(selectors: "dd"): NodeListOf; + querySelectorAll(selectors: "defs"): NodeListOf; + querySelectorAll(selectors: "del"): NodeListOf; + querySelectorAll(selectors: "desc"): NodeListOf; + querySelectorAll(selectors: "dfn"): NodeListOf; + querySelectorAll(selectors: "dir"): NodeListOf; + querySelectorAll(selectors: "div"): NodeListOf; + querySelectorAll(selectors: "dl"): NodeListOf; + querySelectorAll(selectors: "dt"): NodeListOf; + querySelectorAll(selectors: "ellipse"): NodeListOf; + querySelectorAll(selectors: "em"): NodeListOf; + querySelectorAll(selectors: "embed"): NodeListOf; + querySelectorAll(selectors: "feblend"): NodeListOf; + querySelectorAll(selectors: "fecolormatrix"): NodeListOf; + querySelectorAll(selectors: "fecomponenttransfer"): NodeListOf; + querySelectorAll(selectors: "fecomposite"): NodeListOf; + querySelectorAll(selectors: "feconvolvematrix"): NodeListOf; + querySelectorAll(selectors: "fediffuselighting"): NodeListOf; + querySelectorAll(selectors: "fedisplacementmap"): NodeListOf; + querySelectorAll(selectors: "fedistantlight"): NodeListOf; + querySelectorAll(selectors: "feflood"): NodeListOf; + querySelectorAll(selectors: "fefunca"): NodeListOf; + querySelectorAll(selectors: "fefuncb"): NodeListOf; + querySelectorAll(selectors: "fefuncg"): NodeListOf; + querySelectorAll(selectors: "fefuncr"): NodeListOf; + querySelectorAll(selectors: "fegaussianblur"): NodeListOf; + querySelectorAll(selectors: "feimage"): NodeListOf; + querySelectorAll(selectors: "femerge"): NodeListOf; + querySelectorAll(selectors: "femergenode"): NodeListOf; + querySelectorAll(selectors: "femorphology"): NodeListOf; + querySelectorAll(selectors: "feoffset"): NodeListOf; + querySelectorAll(selectors: "fepointlight"): NodeListOf; + querySelectorAll(selectors: "fespecularlighting"): NodeListOf; + querySelectorAll(selectors: "fespotlight"): NodeListOf; + querySelectorAll(selectors: "fetile"): NodeListOf; + querySelectorAll(selectors: "feturbulence"): NodeListOf; + querySelectorAll(selectors: "fieldset"): NodeListOf; + querySelectorAll(selectors: "figcaption"): NodeListOf; + querySelectorAll(selectors: "figure"): NodeListOf; + querySelectorAll(selectors: "filter"): NodeListOf; + querySelectorAll(selectors: "font"): NodeListOf; + querySelectorAll(selectors: "footer"): NodeListOf; + querySelectorAll(selectors: "foreignobject"): NodeListOf; + querySelectorAll(selectors: "form"): NodeListOf; + querySelectorAll(selectors: "frame"): NodeListOf; + querySelectorAll(selectors: "frameset"): NodeListOf; + querySelectorAll(selectors: "g"): NodeListOf; + querySelectorAll(selectors: "h1"): NodeListOf; + querySelectorAll(selectors: "h2"): NodeListOf; + querySelectorAll(selectors: "h3"): NodeListOf; + querySelectorAll(selectors: "h4"): NodeListOf; + querySelectorAll(selectors: "h5"): NodeListOf; + querySelectorAll(selectors: "h6"): NodeListOf; + querySelectorAll(selectors: "head"): NodeListOf; + querySelectorAll(selectors: "header"): NodeListOf; + querySelectorAll(selectors: "hgroup"): NodeListOf; + querySelectorAll(selectors: "hr"): NodeListOf; + querySelectorAll(selectors: "html"): NodeListOf; + querySelectorAll(selectors: "i"): NodeListOf; + querySelectorAll(selectors: "iframe"): NodeListOf; + querySelectorAll(selectors: "image"): NodeListOf; + querySelectorAll(selectors: "img"): NodeListOf; + querySelectorAll(selectors: "input"): NodeListOf; + querySelectorAll(selectors: "ins"): NodeListOf; + querySelectorAll(selectors: "isindex"): NodeListOf; + querySelectorAll(selectors: "kbd"): NodeListOf; + querySelectorAll(selectors: "keygen"): NodeListOf; + querySelectorAll(selectors: "label"): NodeListOf; + querySelectorAll(selectors: "legend"): NodeListOf; + querySelectorAll(selectors: "li"): NodeListOf; + querySelectorAll(selectors: "line"): NodeListOf; + querySelectorAll(selectors: "lineargradient"): NodeListOf; + querySelectorAll(selectors: "link"): NodeListOf; + querySelectorAll(selectors: "listing"): NodeListOf; + querySelectorAll(selectors: "map"): NodeListOf; + querySelectorAll(selectors: "mark"): NodeListOf; + querySelectorAll(selectors: "marker"): NodeListOf; + querySelectorAll(selectors: "marquee"): NodeListOf; + querySelectorAll(selectors: "mask"): NodeListOf; + querySelectorAll(selectors: "menu"): NodeListOf; + querySelectorAll(selectors: "meta"): NodeListOf; + querySelectorAll(selectors: "metadata"): NodeListOf; + querySelectorAll(selectors: "meter"): NodeListOf; + querySelectorAll(selectors: "nav"): NodeListOf; + querySelectorAll(selectors: "nextid"): NodeListOf; + querySelectorAll(selectors: "nobr"): NodeListOf; + querySelectorAll(selectors: "noframes"): NodeListOf; + querySelectorAll(selectors: "noscript"): NodeListOf; + querySelectorAll(selectors: "object"): NodeListOf; + querySelectorAll(selectors: "ol"): NodeListOf; + querySelectorAll(selectors: "optgroup"): NodeListOf; + querySelectorAll(selectors: "option"): NodeListOf; + querySelectorAll(selectors: "p"): NodeListOf; + querySelectorAll(selectors: "param"): NodeListOf; + querySelectorAll(selectors: "path"): NodeListOf; + querySelectorAll(selectors: "pattern"): NodeListOf; + querySelectorAll(selectors: "picture"): NodeListOf; + querySelectorAll(selectors: "plaintext"): NodeListOf; + querySelectorAll(selectors: "polygon"): NodeListOf; + querySelectorAll(selectors: "polyline"): NodeListOf; + querySelectorAll(selectors: "pre"): NodeListOf; + querySelectorAll(selectors: "progress"): NodeListOf; + querySelectorAll(selectors: "q"): NodeListOf; + querySelectorAll(selectors: "radialgradient"): NodeListOf; + querySelectorAll(selectors: "rect"): NodeListOf; + querySelectorAll(selectors: "rt"): NodeListOf; + querySelectorAll(selectors: "ruby"): NodeListOf; + querySelectorAll(selectors: "s"): NodeListOf; + querySelectorAll(selectors: "samp"): NodeListOf; + querySelectorAll(selectors: "script"): NodeListOf; + querySelectorAll(selectors: "section"): NodeListOf; + querySelectorAll(selectors: "select"): NodeListOf; + querySelectorAll(selectors: "small"): NodeListOf; + querySelectorAll(selectors: "source"): NodeListOf; + querySelectorAll(selectors: "span"): NodeListOf; + querySelectorAll(selectors: "stop"): NodeListOf; + querySelectorAll(selectors: "strike"): NodeListOf; + querySelectorAll(selectors: "strong"): NodeListOf; + querySelectorAll(selectors: "style"): NodeListOf; + querySelectorAll(selectors: "sub"): NodeListOf; + querySelectorAll(selectors: "sup"): NodeListOf; + querySelectorAll(selectors: "svg"): NodeListOf; + querySelectorAll(selectors: "switch"): NodeListOf; + querySelectorAll(selectors: "symbol"): NodeListOf; + querySelectorAll(selectors: "table"): NodeListOf; + querySelectorAll(selectors: "tbody"): NodeListOf; + querySelectorAll(selectors: "td"): NodeListOf; + querySelectorAll(selectors: "template"): NodeListOf; + querySelectorAll(selectors: "text"): NodeListOf; + querySelectorAll(selectors: "textpath"): NodeListOf; + querySelectorAll(selectors: "textarea"): NodeListOf; + querySelectorAll(selectors: "tfoot"): NodeListOf; + querySelectorAll(selectors: "th"): NodeListOf; + querySelectorAll(selectors: "thead"): NodeListOf; + querySelectorAll(selectors: "title"): NodeListOf; + querySelectorAll(selectors: "tr"): NodeListOf; + querySelectorAll(selectors: "track"): NodeListOf; + querySelectorAll(selectors: "tspan"): NodeListOf; + querySelectorAll(selectors: "tt"): NodeListOf; + querySelectorAll(selectors: "u"): NodeListOf; + querySelectorAll(selectors: "ul"): NodeListOf; + querySelectorAll(selectors: "use"): NodeListOf; + querySelectorAll(selectors: "var"): NodeListOf; + querySelectorAll(selectors: "video"): NodeListOf; + querySelectorAll(selectors: "view"): NodeListOf; + querySelectorAll(selectors: "wbr"): NodeListOf; + querySelectorAll(selectors: "x-ms-webview"): NodeListOf; + querySelectorAll(selectors: "xmp"): NodeListOf; querySelectorAll(selectors: string): NodeListOf; } @@ -13589,18 +13943,21 @@ interface WindowSessionStorage { interface WindowTimers extends Object, WindowTimersExtension { clearInterval(handle: number): void; clearTimeout(handle: number): void; + setInterval(handler: (...args: any[]) => void, timeout: number): number; setInterval(handler: any, timeout?: any, ...args: any[]): number; + setTimeout(handler: (...args: any[]) => void, timeout: number): number; setTimeout(handler: any, timeout?: any, ...args: any[]): number; } interface WindowTimersExtension { clearImmediate(handle: number): void; - setImmediate(expression: any, ...args: any[]): number; + setImmediate(handler: (...args: any[]) => void): number; + setImmediate(handler: any, ...args: any[]): number; } interface XMLHttpRequestEventTarget { onabort: (ev: Event) => any; - onerror: (ev: Event) => any; + onerror: (ev: ErrorEvent) => any; onload: (ev: Event) => any; onloadend: (ev: ProgressEvent) => any; onloadstart: (ev: Event) => any; @@ -13624,6 +13981,13 @@ interface StorageEventInit extends EventInit { storageArea?: Storage; } +interface Canvas2DContextAttributes { + alpha?: boolean; + willReadFrequently?: boolean; + storage?: boolean; + [attribute: string]: boolean | string; +} + interface NodeListOf extends NodeList { length: number; item(index: number): TNode; @@ -13673,6 +14037,177 @@ interface ClipboardEventInit extends EventInit { interface IDBArrayKey extends Array { } +interface RsaKeyGenParams extends Algorithm { + modulusLength: number; + publicExponent: Uint8Array; +} + +interface RsaHashedKeyGenParams extends RsaKeyGenParams { + hash: AlgorithmIdentifier; +} + +interface RsaKeyAlgorithm extends KeyAlgorithm { + modulusLength: number; + publicExponent: Uint8Array; +} + +interface RsaHashedKeyAlgorithm extends RsaKeyAlgorithm { + hash: AlgorithmIdentifier; +} + +interface RsaHashedImportParams { + hash: AlgorithmIdentifier; +} + +interface RsaPssParams { + saltLength: number; +} + +interface RsaOaepParams extends Algorithm { + label?: BufferSource; +} + +interface EcdsaParams extends Algorithm { + hash: AlgorithmIdentifier; +} + +interface EcKeyGenParams extends Algorithm { + typedCurve: string; +} + +interface EcKeyAlgorithm extends KeyAlgorithm { + typedCurve: string; +} + +interface EcKeyImportParams { + namedCurve: string; +} + +interface EcdhKeyDeriveParams extends Algorithm { + public: CryptoKey; +} + +interface AesCtrParams extends Algorithm { + counter: BufferSource; + length: number; +} + +interface AesKeyAlgorithm extends KeyAlgorithm { + length: number; +} + +interface AesKeyGenParams extends Algorithm { + length: number; +} + +interface AesDerivedKeyParams extends Algorithm { + length: number; +} + +interface AesCbcParams extends Algorithm { + iv: BufferSource; +} + +interface AesCmacParams extends Algorithm { + length: number; +} + +interface AesGcmParams extends Algorithm { + iv: BufferSource; + additionalData?: BufferSource; + tagLength?: number; +} + +interface AesCfbParams extends Algorithm { + iv: BufferSource; +} + +interface HmacImportParams extends Algorithm { + hash?: AlgorithmIdentifier; + length?: number; +} + +interface HmacKeyAlgorithm extends KeyAlgorithm { + hash: AlgorithmIdentifier; + length: number; +} + +interface HmacKeyGenParams extends Algorithm { + hash: AlgorithmIdentifier; + length?: number; +} + +interface DhKeyGenParams extends Algorithm { + prime: Uint8Array; + generator: Uint8Array; +} + +interface DhKeyAlgorithm extends KeyAlgorithm { + prime: Uint8Array; + generator: Uint8Array; +} + +interface DhKeyDeriveParams extends Algorithm { + public: CryptoKey; +} + +interface DhImportKeyParams extends Algorithm { + prime: Uint8Array; + generator: Uint8Array; +} + +interface ConcatParams extends Algorithm { + hash?: AlgorithmIdentifier; + algorithmId: Uint8Array; + partyUInfo: Uint8Array; + partyVInfo: Uint8Array; + publicInfo?: Uint8Array; + privateInfo?: Uint8Array; +} + +interface HkdfCtrParams extends Algorithm { + hash: AlgorithmIdentifier; + label: BufferSource; + context: BufferSource; +} + +interface Pbkdf2Params extends Algorithm { + salt: BufferSource; + iterations: number; + hash: AlgorithmIdentifier; +} + +interface RsaOtherPrimesInfo { + r: string; + d: string; + t: string; +} + +interface JsonWebKey { + kty: string; + use?: string; + key_ops?: string[]; + alg?: string; + kid?: string; + x5u?: string; + x5c?: string; + x5t?: string; + ext?: boolean; + crv?: string; + x?: string; + y?: string; + d?: string; + n?: string; + e?: string; + p?: string; + q?: string; + dp?: string; + dq?: string; + qi?: string; + oth?: RsaOtherPrimesInfo[]; + k?: string; +} + declare type EventListenerOrEventListenerObject = EventListener | EventListenerObject; interface ErrorEventHandler { @@ -13746,7 +14281,7 @@ declare var msCredentials: MSCredentials; declare var name: string; declare var navigator: Navigator; declare var offscreenBuffering: string | boolean; -declare var onabort: (ev: Event) => any; +declare var onabort: (ev: UIEvent) => any; declare var onafterprint: (ev: Event) => any; declare var onbeforeprint: (ev: Event) => any; declare var onbeforeunload: (ev: BeforeUnloadEvent) => any; @@ -13896,10 +14431,13 @@ declare function dispatchEvent(evt: Event): boolean; declare function removeEventListener(type: string, listener?: EventListenerOrEventListenerObject, useCapture?: boolean): void; declare function clearInterval(handle: number): void; declare function clearTimeout(handle: number): void; +declare function setInterval(handler: (...args: any[]) => void, timeout: number): number; declare function setInterval(handler: any, timeout?: any, ...args: any[]): number; +declare function setTimeout(handler: (...args: any[]) => void, timeout: number): number; declare function setTimeout(handler: any, timeout?: any, ...args: any[]): number; declare function clearImmediate(handle: number): void; -declare function setImmediate(expression: any, ...args: any[]): number; +declare function setImmediate(handler: (...args: any[]) => void): number; +declare function setImmediate(handler: any, ...args: any[]): number; declare var sessionStorage: Storage; declare var localStorage: Storage; declare var console: Console; @@ -14042,4 +14580,6 @@ type MSOutboundPayload = MSVideoSendPayload | MSAudioSendPayload; type RTCIceGatherCandidate = RTCIceCandidate | RTCIceCandidateComplete; type RTCTransport = RTCDtlsTransport | RTCSrtpSdesTransport; type payloadtype = number; -type IDBValidKey = number | string | Date | IDBArrayKey; \ No newline at end of file +type IDBValidKey = number | string | Date | IDBArrayKey; +type BufferSource = ArrayBuffer | ArrayBufferView; +type MouseWheelEvent = WheelEvent; \ No newline at end of file diff --git a/src/lib/webworker.generated.d.ts b/src/lib/webworker.generated.d.ts index f5c77b8594d..6db86d71b2a 100644 --- a/src/lib/webworker.generated.d.ts +++ b/src/lib/webworker.generated.d.ts @@ -323,7 +323,7 @@ interface IDBDatabase extends EventTarget { readonly name: string; readonly objectStoreNames: DOMStringList; onabort: (ev: Event) => any; - onerror: (ev: Event) => any; + onerror: (ev: ErrorEvent) => any; version: number; onversionchange: (ev: IDBVersionChangeEvent) => any; close(): void; @@ -426,7 +426,7 @@ declare var IDBOpenDBRequest: { interface IDBRequest extends EventTarget { readonly error: DOMError; - onerror: (ev: Event) => any; + onerror: (ev: ErrorEvent) => any; onsuccess: (ev: Event) => any; readonly readyState: string; readonly result: any; @@ -448,7 +448,7 @@ interface IDBTransaction extends EventTarget { readonly mode: string; onabort: (ev: Event) => any; oncomplete: (ev: Event) => any; - onerror: (ev: Event) => any; + onerror: (ev: ErrorEvent) => any; abort(): void; objectStore(name: string): IDBObjectStore; readonly READ_ONLY: string; @@ -516,7 +516,7 @@ declare var MSApp: MSApp; interface MSAppAsyncOperation extends EventTarget { readonly error: DOMError; oncomplete: (ev: Event) => any; - onerror: (ev: Event) => any; + onerror: (ev: ErrorEvent) => any; readonly readyState: number; readonly result: any; start(): void; @@ -665,7 +665,7 @@ interface WebSocket extends EventTarget { readonly bufferedAmount: number; readonly extensions: string; onclose: (ev: CloseEvent) => any; - onerror: (ev: Event) => any; + onerror: (ev: ErrorEvent) => any; onmessage: (ev: MessageEvent) => any; onopen: (ev: Event) => any; readonly protocol: string; @@ -708,7 +708,6 @@ declare var Worker: { } interface XMLHttpRequest extends EventTarget, XMLHttpRequestEventTarget { - msCaching: string; onreadystatechange: (ev: ProgressEvent) => any; readonly readyState: number; readonly response: any; @@ -720,6 +719,7 @@ interface XMLHttpRequest extends EventTarget, XMLHttpRequestEventTarget { timeout: number; readonly upload: XMLHttpRequestUpload; withCredentials: boolean; + msCaching?: string; abort(): void; getAllResponseHeaders(): string; getResponseHeader(header: string): string | null; @@ -766,14 +766,14 @@ declare var XMLHttpRequestUpload: { } interface AbstractWorker { - onerror: (ev: Event) => any; + onerror: (ev: ErrorEvent) => any; addEventListener(type: "error", listener: (ev: ErrorEvent) => any, useCapture?: boolean): void; addEventListener(type: string, listener: EventListenerOrEventListenerObject, useCapture?: boolean): void; } interface MSBaseReader { onabort: (ev: Event) => any; - onerror: (ev: Event) => any; + onerror: (ev: ErrorEvent) => any; onload: (ev: Event) => any; onloadend: (ev: ProgressEvent) => any; onloadstart: (ev: Event) => any; @@ -819,7 +819,7 @@ interface WindowConsole { interface XMLHttpRequestEventTarget { onabort: (ev: Event) => any; - onerror: (ev: Event) => any; + onerror: (ev: ErrorEvent) => any; onload: (ev: Event) => any; onloadend: (ev: ProgressEvent) => any; onloadstart: (ev: Event) => any; @@ -849,7 +849,7 @@ declare var FileReaderSync: { interface WorkerGlobalScope extends EventTarget, WorkerUtils, DedicatedWorkerGlobalScope, WindowConsole { readonly location: WorkerLocation; - onerror: (ev: Event) => any; + onerror: (ev: ErrorEvent) => any; readonly self: WorkerGlobalScope; close(): void; msWriteProfilerMark(profilerMarkName: string): void; @@ -905,8 +905,11 @@ interface WorkerUtils extends Object, WindowBase64 { clearInterval(handle: number): void; clearTimeout(handle: number): void; importScripts(...urls: string[]): void; + setImmediate(handler: (...args: any[]) => void): number; setImmediate(handler: any, ...args: any[]): number; + setInterval(handler: (...args: any[]) => void, timeout: number): number; setInterval(handler: any, timeout?: any, ...args: any[]): number; + setTimeout(handler: (...args: any[]) => void, timeout: number): number; setTimeout(handler: any, timeout?: any, ...args: any[]): number; } @@ -942,6 +945,177 @@ interface ProgressEventInit extends EventInit { interface IDBArrayKey extends Array { } +interface RsaKeyGenParams extends Algorithm { + modulusLength: number; + publicExponent: Uint8Array; +} + +interface RsaHashedKeyGenParams extends RsaKeyGenParams { + hash: AlgorithmIdentifier; +} + +interface RsaKeyAlgorithm extends KeyAlgorithm { + modulusLength: number; + publicExponent: Uint8Array; +} + +interface RsaHashedKeyAlgorithm extends RsaKeyAlgorithm { + hash: AlgorithmIdentifier; +} + +interface RsaHashedImportParams { + hash: AlgorithmIdentifier; +} + +interface RsaPssParams { + saltLength: number; +} + +interface RsaOaepParams extends Algorithm { + label?: BufferSource; +} + +interface EcdsaParams extends Algorithm { + hash: AlgorithmIdentifier; +} + +interface EcKeyGenParams extends Algorithm { + typedCurve: string; +} + +interface EcKeyAlgorithm extends KeyAlgorithm { + typedCurve: string; +} + +interface EcKeyImportParams { + namedCurve: string; +} + +interface EcdhKeyDeriveParams extends Algorithm { + public: CryptoKey; +} + +interface AesCtrParams extends Algorithm { + counter: BufferSource; + length: number; +} + +interface AesKeyAlgorithm extends KeyAlgorithm { + length: number; +} + +interface AesKeyGenParams extends Algorithm { + length: number; +} + +interface AesDerivedKeyParams extends Algorithm { + length: number; +} + +interface AesCbcParams extends Algorithm { + iv: BufferSource; +} + +interface AesCmacParams extends Algorithm { + length: number; +} + +interface AesGcmParams extends Algorithm { + iv: BufferSource; + additionalData?: BufferSource; + tagLength?: number; +} + +interface AesCfbParams extends Algorithm { + iv: BufferSource; +} + +interface HmacImportParams extends Algorithm { + hash?: AlgorithmIdentifier; + length?: number; +} + +interface HmacKeyAlgorithm extends KeyAlgorithm { + hash: AlgorithmIdentifier; + length: number; +} + +interface HmacKeyGenParams extends Algorithm { + hash: AlgorithmIdentifier; + length?: number; +} + +interface DhKeyGenParams extends Algorithm { + prime: Uint8Array; + generator: Uint8Array; +} + +interface DhKeyAlgorithm extends KeyAlgorithm { + prime: Uint8Array; + generator: Uint8Array; +} + +interface DhKeyDeriveParams extends Algorithm { + public: CryptoKey; +} + +interface DhImportKeyParams extends Algorithm { + prime: Uint8Array; + generator: Uint8Array; +} + +interface ConcatParams extends Algorithm { + hash?: AlgorithmIdentifier; + algorithmId: Uint8Array; + partyUInfo: Uint8Array; + partyVInfo: Uint8Array; + publicInfo?: Uint8Array; + privateInfo?: Uint8Array; +} + +interface HkdfCtrParams extends Algorithm { + hash: AlgorithmIdentifier; + label: BufferSource; + context: BufferSource; +} + +interface Pbkdf2Params extends Algorithm { + salt: BufferSource; + iterations: number; + hash: AlgorithmIdentifier; +} + +interface RsaOtherPrimesInfo { + r: string; + d: string; + t: string; +} + +interface JsonWebKey { + kty: string; + use?: string; + key_ops?: string[]; + alg?: string; + kid?: string; + x5u?: string; + x5c?: string; + x5t?: string; + ext?: boolean; + crv?: string; + x?: string; + y?: string; + d?: string; + n?: string; + e?: string; + p?: string; + q?: string; + dp?: string; + dq?: string; + qi?: string; + oth?: RsaOtherPrimesInfo[]; + k?: string; +} + declare type EventListenerOrEventListenerObject = EventListener | EventListenerObject; interface ErrorEventHandler { @@ -975,7 +1149,7 @@ interface FunctionStringCallback { (data: string): void; } declare var location: WorkerLocation; -declare var onerror: (ev: Event) => any; +declare var onerror: (ev: ErrorEvent) => any; declare var self: WorkerGlobalScope; declare function close(): void; declare function msWriteProfilerMark(profilerMarkName: string): void; @@ -990,8 +1164,11 @@ declare function clearImmediate(handle: number): void; declare function clearInterval(handle: number): void; declare function clearTimeout(handle: number): void; declare function importScripts(...urls: string[]): void; +declare function setImmediate(handler: (...args: any[]) => void): number; declare function setImmediate(handler: any, ...args: any[]): number; +declare function setInterval(handler: (...args: any[]) => void, timeout: number): number; declare function setInterval(handler: any, timeout?: any, ...args: any[]): number; +declare function setTimeout(handler: (...args: any[]) => void, timeout: number): number; declare function setTimeout(handler: any, timeout?: any, ...args: any[]): number; declare function atob(encodedString: string): string; declare function btoa(rawString: string): string; @@ -1002,4 +1179,6 @@ declare function addEventListener(type: "error", listener: (ev: ErrorEvent) => a declare function addEventListener(type: "message", listener: (ev: MessageEvent) => any, useCapture?: boolean): void; declare function addEventListener(type: string, listener: EventListenerOrEventListenerObject, useCapture?: boolean): void; type IDBKeyPath = string; -type IDBValidKey = number | string | Date | IDBArrayKey; \ No newline at end of file +type IDBValidKey = number | string | Date | IDBArrayKey; +type BufferSource = ArrayBuffer | ArrayBufferView; +type MouseWheelEvent = WheelEvent; \ No newline at end of file From e9226af3f6495b9eff8f40554f5a51e0c2d0b46d Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Fri, 17 Jun 2016 17:18:16 -0700 Subject: [PATCH 50/56] Array#map -> ts.map. --- src/compiler/program.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 111343e08ff..a01f3a4c5b2 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -180,7 +180,7 @@ namespace ts { function getEffectiveTypeRoots(options: CompilerOptions, host: ModuleResolutionHost) { return options.typeRoots || - defaultTypeRoots.map(d => combinePaths(options.configFilePath ? getDirectoryPath(options.configFilePath) : host.getCurrentDirectory(), d)); + map(defaultTypeRoots, d => combinePaths(options.configFilePath ? getDirectoryPath(options.configFilePath) : host.getCurrentDirectory(), d)); } /** From f568ad0ce446557c2198f0892f76d9f7db63d709 Mon Sep 17 00:00:00 2001 From: Mohamed Hegazy Date: Sat, 18 Jun 2016 12:01:29 -0700 Subject: [PATCH 51/56] Add conditional index signature for Canvas2DContextAttributes (https://github.com/Microsoft/TypeScript/issues/9244) --- src/lib/dom.generated.d.ts | 140 ++++++++++++++++++------------------- 1 file changed, 70 insertions(+), 70 deletions(-) diff --git a/src/lib/dom.generated.d.ts b/src/lib/dom.generated.d.ts index 40cd883af61..0911586dd5d 100644 --- a/src/lib/dom.generated.d.ts +++ b/src/lib/dom.generated.d.ts @@ -2290,7 +2290,7 @@ declare var DeviceRotationRate: { interface Document extends Node, GlobalEventHandlers, NodeSelector, DocumentEvent { /** - * Sets or gets the URL for the current document. + * Sets or gets the URL for the current document. */ readonly URL: string; /** @@ -2318,7 +2318,7 @@ interface Document extends Node, GlobalEventHandlers, NodeSelector, DocumentEven */ applets: HTMLCollectionOf; /** - * Deprecated. Sets or retrieves a value that indicates the background color behind the object. + * Deprecated. Sets or retrieves a value that indicates the background color behind the object. */ bgColor: string; /** @@ -2346,19 +2346,19 @@ interface Document extends Node, GlobalEventHandlers, NodeSelector, DocumentEven */ designMode: string; /** - * Sets or retrieves a value that indicates the reading order of the object. + * Sets or retrieves a value that indicates the reading order of the object. */ dir: string; /** - * Gets an object representing the document type declaration associated with the current document. + * Gets an object representing the document type declaration associated with the current document. */ readonly doctype: DocumentType; /** - * Gets a reference to the root node of the document. + * Gets a reference to the root node of the document. */ documentElement: HTMLElement; /** - * Sets or gets the security domain of the document. + * Sets or gets the security domain of the document. */ domain: string; /** @@ -2382,7 +2382,7 @@ interface Document extends Node, GlobalEventHandlers, NodeSelector, DocumentEven */ images: HTMLCollectionOf; /** - * Gets the implementation object of the current document. + * Gets the implementation object of the current document. */ readonly implementation: DOMImplementation; /** @@ -2390,11 +2390,11 @@ interface Document extends Node, GlobalEventHandlers, NodeSelector, DocumentEven */ readonly inputEncoding: string | null; /** - * Gets the date that the page was last modified, if the page supplies one. + * Gets the date that the page was last modified, if the page supplies one. */ readonly lastModified: string; /** - * Sets or gets the color of the document links. + * Sets or gets the color of the document links. */ linkColor: string; /** @@ -2402,7 +2402,7 @@ interface Document extends Node, GlobalEventHandlers, NodeSelector, DocumentEven */ links: HTMLCollectionOf; /** - * Contains information about the current URL. + * Contains information about the current URL. */ readonly location: Location; msCSSOMElementFloatMetrics: boolean; @@ -2427,19 +2427,19 @@ interface Document extends Node, GlobalEventHandlers, NodeSelector, DocumentEven * @param ev The event. */ onbeforedeactivate: (ev: UIEvent) => any; - /** - * Fires when the object loses the input focus. + /** + * Fires when the object loses the input focus. * @param ev The focus event. */ onblur: (ev: FocusEvent) => any; /** - * Occurs when playback is possible, but would require further buffering. + * Occurs when playback is possible, but would require further buffering. * @param ev The event. */ oncanplay: (ev: Event) => any; oncanplaythrough: (ev: Event) => any; /** - * Fires when the contents of the object or selection have changed. + * Fires when the contents of the object or selection have changed. * @param ev The event. */ onchange: (ev: Event) => any; @@ -2449,7 +2449,7 @@ interface Document extends Node, GlobalEventHandlers, NodeSelector, DocumentEven */ onclick: (ev: MouseEvent) => any; /** - * Fires when the user clicks the right mouse button in the client area, opening the context menu. + * Fires when the user clicks the right mouse button in the client area, opening the context menu. * @param ev The mouse event. */ oncontextmenu: (ev: PointerEvent) => any; @@ -2473,12 +2473,12 @@ interface Document extends Node, GlobalEventHandlers, NodeSelector, DocumentEven * @param ev The event. */ ondragend: (ev: DragEvent) => any; - /** + /** * Fires on the target element when the user drags the object to a valid drop target. * @param ev The drag event. */ ondragenter: (ev: DragEvent) => any; - /** + /** * Fires on the target object when the user moves the mouse out of a valid drop target during a drag operation. * @param ev The drag event. */ @@ -2489,23 +2489,23 @@ interface Document extends Node, GlobalEventHandlers, NodeSelector, DocumentEven */ ondragover: (ev: DragEvent) => any; /** - * Fires on the source object when the user starts to drag a text selection or selected object. + * Fires on the source object when the user starts to drag a text selection or selected object. * @param ev The event. */ ondragstart: (ev: DragEvent) => any; ondrop: (ev: DragEvent) => any; /** - * Occurs when the duration attribute is updated. + * Occurs when the duration attribute is updated. * @param ev The event. */ ondurationchange: (ev: Event) => any; /** - * Occurs when the media element is reset to its initial state. + * Occurs when the media element is reset to its initial state. * @param ev The event. */ onemptied: (ev: Event) => any; /** - * Occurs when the end of playback is reached. + * Occurs when the end of playback is reached. * @param ev The event */ onended: (ev: MediaStreamErrorEvent) => any; @@ -2515,7 +2515,7 @@ interface Document extends Node, GlobalEventHandlers, NodeSelector, DocumentEven */ onerror: (ev: ErrorEvent) => any; /** - * Fires when the object receives focus. + * Fires when the object receives focus. * @param ev The event. */ onfocus: (ev: FocusEvent) => any; @@ -2539,12 +2539,12 @@ interface Document extends Node, GlobalEventHandlers, NodeSelector, DocumentEven */ onkeyup: (ev: KeyboardEvent) => any; /** - * Fires immediately after the browser loads the object. + * Fires immediately after the browser loads the object. * @param ev The event. */ onload: (ev: Event) => any; /** - * Occurs when media data is loaded at the current playback position. + * Occurs when media data is loaded at the current playback position. * @param ev The event. */ onloadeddata: (ev: Event) => any; @@ -2554,22 +2554,22 @@ interface Document extends Node, GlobalEventHandlers, NodeSelector, DocumentEven */ onloadedmetadata: (ev: Event) => any; /** - * Occurs when Internet Explorer begins looking for media data. + * Occurs when Internet Explorer begins looking for media data. * @param ev The event. */ onloadstart: (ev: Event) => any; /** - * Fires when the user clicks the object with either mouse button. + * Fires when the user clicks the object with either mouse button. * @param ev The mouse event. */ onmousedown: (ev: MouseEvent) => any; /** - * Fires when the user moves the mouse over the object. + * Fires when the user moves the mouse over the object. * @param ev The mouse event. */ onmousemove: (ev: MouseEvent) => any; /** - * Fires when the user moves the mouse pointer outside the boundaries of the object. + * Fires when the user moves the mouse pointer outside the boundaries of the object. * @param ev The mouse event. */ onmouseout: (ev: MouseEvent) => any; @@ -2579,12 +2579,12 @@ interface Document extends Node, GlobalEventHandlers, NodeSelector, DocumentEven */ onmouseover: (ev: MouseEvent) => any; /** - * Fires when the user releases a mouse button while the mouse is over the object. + * Fires when the user releases a mouse button while the mouse is over the object. * @param ev The mouse event. */ onmouseup: (ev: MouseEvent) => any; /** - * Fires when the wheel button is rotated. + * Fires when the wheel button is rotated. * @param ev The mouse event */ onmousewheel: (ev: WheelEvent) => any; @@ -2606,7 +2606,7 @@ interface Document extends Node, GlobalEventHandlers, NodeSelector, DocumentEven onmspointerover: (ev: MSPointerEvent) => any; onmspointerup: (ev: MSPointerEvent) => any; /** - * Occurs when an item is removed from a Jump List of a webpage running in Site Mode. + * Occurs when an item is removed from a Jump List of a webpage running in Site Mode. * @param ev The event. */ onmssitemodejumplistitemremoved: (ev: MSSiteModeEvent) => any; @@ -2621,24 +2621,24 @@ interface Document extends Node, GlobalEventHandlers, NodeSelector, DocumentEven */ onpause: (ev: Event) => any; /** - * Occurs when the play method is requested. + * Occurs when the play method is requested. * @param ev The event. */ onplay: (ev: Event) => any; /** - * Occurs when the audio or video has started playing. + * Occurs when the audio or video has started playing. * @param ev The event. */ onplaying: (ev: Event) => any; onpointerlockchange: (ev: Event) => any; onpointerlockerror: (ev: Event) => any; /** - * Occurs to indicate progress while downloading media data. + * Occurs to indicate progress while downloading media data. * @param ev The event. */ onprogress: (ev: ProgressEvent) => any; /** - * Occurs when the playback rate is increased or decreased. + * Occurs when the playback rate is increased or decreased. * @param ev The event. */ onratechange: (ev: Event) => any; @@ -2648,22 +2648,22 @@ interface Document extends Node, GlobalEventHandlers, NodeSelector, DocumentEven */ onreadystatechange: (ev: ProgressEvent) => any; /** - * Fires when the user resets a form. + * Fires when the user resets a form. * @param ev The event. */ onreset: (ev: Event) => any; /** - * Fires when the user repositions the scroll box in the scroll bar on the object. + * Fires when the user repositions the scroll box in the scroll bar on the object. * @param ev The event. */ onscroll: (ev: UIEvent) => any; /** - * Occurs when the seek operation ends. + * Occurs when the seek operation ends. * @param ev The event. */ onseeked: (ev: Event) => any; /** - * Occurs when the current playback position is moved. + * Occurs when the current playback position is moved. * @param ev The event. */ onseeking: (ev: Event) => any; @@ -2679,7 +2679,7 @@ interface Document extends Node, GlobalEventHandlers, NodeSelector, DocumentEven onselectionchange: (ev: Event) => any; onselectstart: (ev: Event) => any; /** - * Occurs when the download has stopped. + * Occurs when the download has stopped. * @param ev The event. */ onstalled: (ev: Event) => any; @@ -2690,7 +2690,7 @@ interface Document extends Node, GlobalEventHandlers, NodeSelector, DocumentEven onstop: (ev: Event) => any; onsubmit: (ev: Event) => any; /** - * Occurs if the load operation has been intentionally halted. + * Occurs if the load operation has been intentionally halted. * @param ev The event. */ onsuspend: (ev: Event) => any; @@ -2709,7 +2709,7 @@ interface Document extends Node, GlobalEventHandlers, NodeSelector, DocumentEven */ onvolumechange: (ev: Event) => any; /** - * Occurs when playback stops because the next frame of a video resource is not available. + * Occurs when playback stops because the next frame of a video resource is not available. * @param ev The event. */ onwaiting: (ev: Event) => any; @@ -2743,7 +2743,7 @@ interface Document extends Node, GlobalEventHandlers, NodeSelector, DocumentEven */ title: string; readonly visibilityState: string; - /** + /** * Sets or gets the color of the links that the user has visited. */ vlinkColor: string; @@ -2933,7 +2933,7 @@ interface Document extends Node, GlobalEventHandlers, NodeSelector, DocumentEven createExpression(expression: string, resolver: XPathNSResolver): XPathExpression; createNSResolver(nodeResolver: Node): XPathNSResolver; /** - * Creates a NodeIterator object that you can use to traverse filtered lists of nodes or elements in a document. + * Creates a NodeIterator object that you can use to traverse filtered lists of nodes or elements in a document. * @param root The root element or node to start traversing on. * @param whatToShow The type of nodes or elements to appear in the node list * @param filter A custom NodeFilter function to use. For more information, see filter. Use null for no filter. @@ -2942,11 +2942,11 @@ interface Document extends Node, GlobalEventHandlers, NodeSelector, DocumentEven createNodeIterator(root: Node, whatToShow?: number, filter?: NodeFilter, entityReferenceExpansion?: boolean): NodeIterator; createProcessingInstruction(target: string, data: string): ProcessingInstruction; /** - * Returns an empty range object that has both of its boundary points positioned at the beginning of the document. + * Returns an empty range object that has both of its boundary points positioned at the beginning of the document. */ createRange(): Range; /** - * Creates a text string from the specified value. + * Creates a text string from the specified value. * @param data String that specifies the nodeValue property of the text node. */ createTextNode(data: string): Text; @@ -2961,7 +2961,7 @@ interface Document extends Node, GlobalEventHandlers, NodeSelector, DocumentEven */ createTreeWalker(root: Node, whatToShow?: number, filter?: NodeFilter, entityReferenceExpansion?: boolean): TreeWalker; /** - * Returns the element for the specified x coordinate and the specified y coordinate. + * Returns the element for the specified x coordinate and the specified y coordinate. * @param x The x-offset * @param y The y-offset */ @@ -3199,7 +3199,7 @@ interface Document extends Node, GlobalEventHandlers, NodeSelector, DocumentEven * @param replace Specifies whether the existing entry for the document is replaced in the history list. */ open(url?: string, name?: string, features?: string, replace?: boolean): Document; - /** + /** * Returns a Boolean value that indicates whether a specified command can be successfully executed using execCommand, given the current state of the document. * @param commandId Specifies a command identifier. */ @@ -3221,7 +3221,7 @@ interface Document extends Node, GlobalEventHandlers, NodeSelector, DocumentEven queryCommandSupported(commandId: string): boolean; /** * Retrieves the string associated with a command. - * @param commandId String that contains the identifier of a command. This can be any command identifier given in the list of Command Identifiers. + * @param commandId String that contains the identifier of a command. This can be any command identifier given in the list of Command Identifiers. */ queryCommandText(commandId: string): string; /** @@ -3237,12 +3237,12 @@ interface Document extends Node, GlobalEventHandlers, NodeSelector, DocumentEven webkitCancelFullScreen(): void; webkitExitFullscreen(): void; /** - * Writes one or more HTML expressions to a document in the specified window. + * Writes one or more HTML expressions to a document in the specified window. * @param content Specifies the text and HTML tags to write. */ write(...content: string[]): void; /** - * Writes one or more HTML expressions, followed by a carriage return, to a document in the specified window. + * Writes one or more HTML expressions, followed by a carriage return, to a document in the specified window. * @param content The text and HTML tags to write. */ writeln(...content: string[]): void; @@ -3971,12 +3971,12 @@ interface HTMLAnchorElement extends HTMLElement { */ target: string; /** - * Retrieves or sets the text of the object as a string. + * Retrieves or sets the text of the object as a string. */ text: string; type: string; urn: string; - /** + /** * Returns a string representation of an object. */ toString(): string; @@ -4078,7 +4078,7 @@ interface HTMLAreaElement extends HTMLElement { */ host: string; /** - * Sets or retrieves the host name part of the location or URL. + * Sets or retrieves the host name part of the location or URL. */ hostname: string; /** @@ -4114,7 +4114,7 @@ interface HTMLAreaElement extends HTMLElement { * Sets or retrieves the window or frame at which to target content. */ target: string; - /** + /** * Returns a string representation of an object. */ toString(): string; @@ -4380,7 +4380,7 @@ interface HTMLButtonElement extends HTMLElement { * Overrides the target attribute on a form element. */ formTarget: string; - /** + /** * Sets or retrieves the name of the object. */ name: string; @@ -4397,7 +4397,7 @@ interface HTMLButtonElement extends HTMLElement { * Returns a ValidityState object that represents the validity states of an element. */ readonly validity: ValidityState; - /** + /** * Sets or retrieves the default or selected value of the control. */ value: string; @@ -4504,7 +4504,7 @@ declare var HTMLDirectoryElement: { interface HTMLDivElement extends HTMLElement { /** - * Sets or retrieves how the object is aligned with adjacent text. + * Sets or retrieves how the object is aligned with adjacent text. */ align: string; /** @@ -5647,7 +5647,7 @@ interface HTMLInputElement extends HTMLElement { */ readonly files: FileList | null; /** - * Retrieves a reference to the form that the object is embedded in. + * Retrieves a reference to the form that the object is embedded in. */ readonly form: HTMLFormElement; /** @@ -6367,7 +6367,7 @@ interface HTMLMetaElement extends HTMLElement { */ scheme: string; /** - * Sets or retrieves the URL property that will be loaded after the specified time has elapsed. + * Sets or retrieves the URL property that will be loaded after the specified time has elapsed. */ url: string; } @@ -6630,7 +6630,7 @@ declare var HTMLOptionsCollection: { interface HTMLParagraphElement extends HTMLElement { /** - * Sets or retrieves how the object is aligned with adjacent text. + * Sets or retrieves how the object is aligned with adjacent text. */ align: string; clear: string; @@ -6732,10 +6732,10 @@ interface HTMLScriptElement extends HTMLElement { */ defer: boolean; /** - * Sets or retrieves the event for which the script is written. + * Sets or retrieves the event for which the script is written. */ event: string; - /** + /** * Sets or retrieves the object that is bound to the event script. */ htmlFor: string; @@ -6744,7 +6744,7 @@ interface HTMLScriptElement extends HTMLElement { */ src: string; /** - * Retrieves or sets the text of the object as a string. + * Retrieves or sets the text of the object as a string. */ text: string; /** @@ -6765,7 +6765,7 @@ interface HTMLSelectElement extends HTMLElement { autofocus: boolean; disabled: boolean; /** - * Retrieves a reference to the form that the object is embedded in. + * Retrieves a reference to the form that the object is embedded in. */ readonly form: HTMLFormElement; /** @@ -6791,7 +6791,7 @@ interface HTMLSelectElement extends HTMLElement { selectedIndex: number; selectedOptions: HTMLCollectionOf; /** - * Sets or retrieves the number of rows in the list box. + * Sets or retrieves the number of rows in the list box. */ size: number; /** @@ -6817,7 +6817,7 @@ interface HTMLSelectElement extends HTMLElement { /** * Adds an element to the areas, controlRange, or options collection. * @param element Variant of type Number that specifies the index position in the collection where the element is placed. If no value is given, the method places the element at the end of the collection. - * @param before Variant of type Object that specifies an element to insert before, or null to append the object to the collection. + * @param before Variant of type Object that specifies an element to insert before, or null to append the object to the collection. */ add(element: HTMLElement, before?: HTMLElement | number): void; /** @@ -7012,7 +7012,7 @@ interface HTMLTableElement extends HTMLElement { */ border: string; /** - * Sets or retrieves the border color of the object. + * Sets or retrieves the border color of the object. */ borderColor: any; /** @@ -7307,7 +7307,7 @@ declare var HTMLTextAreaElement: { interface HTMLTitleElement extends HTMLElement { /** - * Retrieves or sets the text of the object as a string. + * Retrieves or sets the text of the object as a string. */ text: string; } @@ -13985,7 +13985,7 @@ interface Canvas2DContextAttributes { alpha?: boolean; willReadFrequently?: boolean; storage?: boolean; - [attribute: string]: boolean | string; + [attribute: string]: boolean | string | undefined; } interface NodeListOf extends NodeList { From dfed7625afc06738f249c3536195590abdd13457 Mon Sep 17 00:00:00 2001 From: Mohamed Hegazy Date: Sat, 18 Jun 2016 12:40:22 -0700 Subject: [PATCH 52/56] Add libcheck tests --- src/harness/harness.ts | 2 +- tests/baselines/reference/verifyDefaultLib_dom.js | 6 ++++++ tests/baselines/reference/verifyDefaultLib_dom.symbols | 6 ++++++ tests/baselines/reference/verifyDefaultLib_dom.types | 6 ++++++ tests/cases/compiler/verifyDefaultLib_dom.ts | 7 +++++++ tests/cases/compiler/verifyDefaultLib_webworker.ts | 7 +++++++ 6 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 tests/baselines/reference/verifyDefaultLib_dom.js create mode 100644 tests/baselines/reference/verifyDefaultLib_dom.symbols create mode 100644 tests/baselines/reference/verifyDefaultLib_dom.types create mode 100644 tests/cases/compiler/verifyDefaultLib_dom.ts create mode 100644 tests/cases/compiler/verifyDefaultLib_webworker.ts diff --git a/src/harness/harness.ts b/src/harness/harness.ts index f804dc4eb6a..0b4a58260ec 100644 --- a/src/harness/harness.ts +++ b/src/harness/harness.ts @@ -1051,7 +1051,7 @@ namespace Harness { options.target = options.target || ts.ScriptTarget.ES3; options.newLine = options.newLine || ts.NewLineKind.CarriageReturnLineFeed; options.noErrorTruncation = true; - options.skipDefaultLibCheck = true; + options.skipDefaultLibCheck = typeof options.skipDefaultLibCheck === "undefined" ? true : options.skipDefaultLibCheck; if (typeof currentDirectory === "undefined") { currentDirectory = Harness.IO.getCurrentDirectory(); diff --git a/tests/baselines/reference/verifyDefaultLib_dom.js b/tests/baselines/reference/verifyDefaultLib_dom.js new file mode 100644 index 00000000000..b65e133572a --- /dev/null +++ b/tests/baselines/reference/verifyDefaultLib_dom.js @@ -0,0 +1,6 @@ +//// [verifyDefaultLib_dom.ts] + +var x: HTMLElement; + +//// [verifyDefaultLib_dom.js] +var x; diff --git a/tests/baselines/reference/verifyDefaultLib_dom.symbols b/tests/baselines/reference/verifyDefaultLib_dom.symbols new file mode 100644 index 00000000000..53502b040c3 --- /dev/null +++ b/tests/baselines/reference/verifyDefaultLib_dom.symbols @@ -0,0 +1,6 @@ +=== tests/cases/compiler/verifyDefaultLib_dom.ts === + +var x: HTMLElement; +>x : Symbol(x, Decl(verifyDefaultLib_dom.ts, 1, 3)) +>HTMLElement : Symbol(HTMLElement, Decl(lib.dom.d.ts, --, --), Decl(lib.dom.d.ts, --, --)) + diff --git a/tests/baselines/reference/verifyDefaultLib_dom.types b/tests/baselines/reference/verifyDefaultLib_dom.types new file mode 100644 index 00000000000..aafd063846d --- /dev/null +++ b/tests/baselines/reference/verifyDefaultLib_dom.types @@ -0,0 +1,6 @@ +=== tests/cases/compiler/verifyDefaultLib_dom.ts === + +var x: HTMLElement; +>x : HTMLElement +>HTMLElement : HTMLElement + diff --git a/tests/cases/compiler/verifyDefaultLib_dom.ts b/tests/cases/compiler/verifyDefaultLib_dom.ts new file mode 100644 index 00000000000..519485320ae --- /dev/null +++ b/tests/cases/compiler/verifyDefaultLib_dom.ts @@ -0,0 +1,7 @@ +// @strictNullChecks: true +// @noImplicitAny: true +// @noImplicitThis: true +// @skipDefaultLibCheck: false +// @lib: es2015,es2016,es2017,dom,scripthost + +var x: HTMLElement; \ No newline at end of file diff --git a/tests/cases/compiler/verifyDefaultLib_webworker.ts b/tests/cases/compiler/verifyDefaultLib_webworker.ts new file mode 100644 index 00000000000..7da62387d2f --- /dev/null +++ b/tests/cases/compiler/verifyDefaultLib_webworker.ts @@ -0,0 +1,7 @@ +// @strictNullChecks: true +// @noImplicitAny: true +// @noImplicitThis: true +// @skipDefaultLibCheck: false +// @lib: es2015,es2016,es2017,webworker + +var x: Worker; \ No newline at end of file From 634818cbac8a064dd5efac3c90859fa0d7d6e7f0 Mon Sep 17 00:00:00 2001 From: zhengbli Date: Sat, 18 Jun 2016 20:35:52 -0700 Subject: [PATCH 53/56] Add missing worker types --- src/lib/webworker.generated.d.ts | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/lib/webworker.generated.d.ts b/src/lib/webworker.generated.d.ts index 6db86d71b2a..56c8cc84367 100644 --- a/src/lib/webworker.generated.d.ts +++ b/src/lib/webworker.generated.d.ts @@ -3,6 +3,10 @@ /// IE Worker APIs ///////////////////////////// +interface Algorithm { + name: string; +} + interface EventInit { bubbles?: boolean; cancelable?: boolean; @@ -18,6 +22,10 @@ interface IDBObjectStoreParameters { keyPath?: IDBKeyPath; } +interface KeyAlgorithm { + name?: string; +} + interface EventListener { (evt: Event): void; } @@ -107,6 +115,18 @@ declare var Coordinates: { new(): Coordinates; } +interface CryptoKey { + readonly algorithm: KeyAlgorithm; + readonly extractable: boolean; + readonly type: string; + readonly usages: string[]; +} + +declare var CryptoKey: { + prototype: CryptoKey; + new(): CryptoKey; +} + interface DOMError { readonly name: string; toString(): string; @@ -1178,7 +1198,7 @@ declare var console: Console; declare function addEventListener(type: "error", listener: (ev: ErrorEvent) => any, useCapture?: boolean): void; declare function addEventListener(type: "message", listener: (ev: MessageEvent) => any, useCapture?: boolean): void; declare function addEventListener(type: string, listener: EventListenerOrEventListenerObject, useCapture?: boolean): void; +type AlgorithmIdentifier = string | Algorithm; type IDBKeyPath = string; type IDBValidKey = number | string | Date | IDBArrayKey; -type BufferSource = ArrayBuffer | ArrayBufferView; -type MouseWheelEvent = WheelEvent; \ No newline at end of file +type BufferSource = ArrayBuffer | ArrayBufferView; \ No newline at end of file From 5697d21a580b3b0048327e05c8bb08e39b8257b8 Mon Sep 17 00:00:00 2001 From: Mohamed Hegazy Date: Mon, 20 Jun 2016 00:19:42 -0700 Subject: [PATCH 54/56] Accept webworker baselines --- tests/baselines/reference/verifyDefaultLib_webworker.js | 6 ++++++ .../baselines/reference/verifyDefaultLib_webworker.symbols | 6 ++++++ tests/baselines/reference/verifyDefaultLib_webworker.types | 6 ++++++ 3 files changed, 18 insertions(+) create mode 100644 tests/baselines/reference/verifyDefaultLib_webworker.js create mode 100644 tests/baselines/reference/verifyDefaultLib_webworker.symbols create mode 100644 tests/baselines/reference/verifyDefaultLib_webworker.types diff --git a/tests/baselines/reference/verifyDefaultLib_webworker.js b/tests/baselines/reference/verifyDefaultLib_webworker.js new file mode 100644 index 00000000000..515b5d9632f --- /dev/null +++ b/tests/baselines/reference/verifyDefaultLib_webworker.js @@ -0,0 +1,6 @@ +//// [verifyDefaultLib_webworker.ts] + +var x: Worker; + +//// [verifyDefaultLib_webworker.js] +var x; diff --git a/tests/baselines/reference/verifyDefaultLib_webworker.symbols b/tests/baselines/reference/verifyDefaultLib_webworker.symbols new file mode 100644 index 00000000000..ac27e7c83dc --- /dev/null +++ b/tests/baselines/reference/verifyDefaultLib_webworker.symbols @@ -0,0 +1,6 @@ +=== tests/cases/compiler/verifyDefaultLib_webworker.ts === + +var x: Worker; +>x : Symbol(x, Decl(verifyDefaultLib_webworker.ts, 1, 3)) +>Worker : Symbol(Worker, Decl(lib.webworker.d.ts, --, --), Decl(lib.webworker.d.ts, --, --)) + diff --git a/tests/baselines/reference/verifyDefaultLib_webworker.types b/tests/baselines/reference/verifyDefaultLib_webworker.types new file mode 100644 index 00000000000..38accb39842 --- /dev/null +++ b/tests/baselines/reference/verifyDefaultLib_webworker.types @@ -0,0 +1,6 @@ +=== tests/cases/compiler/verifyDefaultLib_webworker.ts === + +var x: Worker; +>x : Worker +>Worker : Worker + From c0eb472b62e0552b602b64baf8124c39af1e2582 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Mon, 20 Jun 2016 08:27:41 -0700 Subject: [PATCH 55/56] Classify `this` in parameter position as a keyword --- src/services/services.ts | 3 ++- tests/cases/fourslash/classifyThisParameter.ts | 13 +++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 tests/cases/fourslash/classifyThisParameter.ts diff --git a/src/services/services.ts b/src/services/services.ts index 983ddfbf250..5dd73abe82f 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -7533,7 +7533,8 @@ namespace ts { return; case SyntaxKind.Parameter: if ((token.parent).name === token) { - return ClassificationType.parameterName; + const isThis = token.kind === SyntaxKind.Identifier && (token).originalKeywordKind === SyntaxKind.ThisKeyword; + return isThis ? ClassificationType.keyword : ClassificationType.parameterName; } return; } diff --git a/tests/cases/fourslash/classifyThisParameter.ts b/tests/cases/fourslash/classifyThisParameter.ts new file mode 100644 index 00000000000..5e0c9a9d4b6 --- /dev/null +++ b/tests/cases/fourslash/classifyThisParameter.ts @@ -0,0 +1,13 @@ +/// + +////function f(this){} + +var c = classification; +verify.syntacticClassificationsAre( + c.keyword("function"), + c.identifier("f"), + c.punctuation("("), + c.keyword("this"), + c.punctuation(")"), + c.punctuation("{"), + c.punctuation("}")); From 95cfaafdee9127b66535abc22cbe6722ee7dbd3a Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Mon, 20 Jun 2016 11:30:48 -0700 Subject: [PATCH 56/56] Use implicit boolean casts; it doesn't hurt performance --- src/compiler/parser.ts | 8 ++--- src/services/navigationBar.ts | 68 +++++++++++++++++------------------ 2 files changed, 38 insertions(+), 38 deletions(-) diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 6e0a37c3d9f..b2f3755ec46 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -17,19 +17,19 @@ namespace ts { } function visitNode(cbNode: (node: Node) => T, node: Node): T { - if (node !== void 0) { + if (node) { return cbNode(node); } } function visitNodeArray(cbNodes: (nodes: Node[]) => T, nodes: Node[]) { - if (nodes !== void 0) { + if (nodes) { return cbNodes(nodes); } } function visitEachNode(cbNode: (node: Node) => T, nodes: Node[]) { - if (nodes !== void 0) { + if (nodes) { for (const node of nodes) { const result = cbNode(node); if (result) { @@ -44,7 +44,7 @@ namespace ts { // embedded arrays are flattened and the 'cbNode' callback is invoked for each element. If a callback returns // a truthy value, iteration stops and that value is returned. Otherwise, undefined is returned. export function forEachChild(node: Node, cbNode: (node: Node) => T, cbNodeArray?: (nodes: Node[]) => T): T { - if (node === void 0) { + if (!node) { return; } // The visitXXX functions could be written as local functions that close over the cbNode and cbNodeArray diff --git a/src/services/navigationBar.ts b/src/services/navigationBar.ts index e722f33bc70..bfbaf5a6287 100644 --- a/src/services/navigationBar.ts +++ b/src/services/navigationBar.ts @@ -14,17 +14,17 @@ namespace ts.NavigationBar { indent: number; // # of parents } - export function getNavigationBarItems(sourceFile_: SourceFile): NavigationBarItem[] { - sourceFile = sourceFile_; + export function getNavigationBarItems(sourceFile: SourceFile): NavigationBarItem[] { + curSourceFile = sourceFile; const result = map(topLevelItems(rootNavigationBarNode(sourceFile)), convertToTopLevelItem); - sourceFile = void 0; + curSourceFile = undefined; return result; } // Keep sourceFile handy so we don't have to search for it every time we need to call `getText`. - let sourceFile: SourceFile; + let curSourceFile: SourceFile; function nodeText(node: Node): string { - return node.getText(sourceFile); + return node.getText(curSourceFile); } function navigationBarNodeKind(n: NavigationBarNode): SyntaxKind { @@ -32,7 +32,7 @@ namespace ts.NavigationBar { } function pushChild(parent: NavigationBarNode, child: NavigationBarNode): void { - if (parent.children !== void 0) { + if (parent.children) { parent.children.push(child); } else { @@ -50,13 +50,13 @@ namespace ts.NavigationBar { function rootNavigationBarNode(sourceFile: SourceFile): NavigationBarNode { Debug.assert(!parentsStack.length); - const root: NavigationBarNode = { node: sourceFile, additionalNodes: void 0, parent: void 0, children: void 0, indent: 0 }; + const root: NavigationBarNode = { node: sourceFile, additionalNodes: undefined, parent: undefined, children: undefined, indent: 0 }; parent = root; for (const statement of sourceFile.statements) { addChildrenRecursively(statement); } endNode(); - Debug.assert(parent === void 0 && !parentsStack.length); + Debug.assert(!parent && !parentsStack.length); return root; } @@ -67,9 +67,9 @@ namespace ts.NavigationBar { function emptyNavigationBarNode(node: Node): NavigationBarNode { return { node, - additionalNodes: void 0, + additionalNodes: undefined, parent, - children: void 0, + children: undefined, indent: parent.indent + 1 }; } @@ -89,7 +89,7 @@ namespace ts.NavigationBar { /** Call after calling `startNode` and adding children to it. */ function endNode(): void { - if (parent.children !== void 0) { + if (parent.children) { mergeChildren(parent.children); sortChildren(parent.children); } @@ -104,7 +104,7 @@ namespace ts.NavigationBar { /** Look for navigation bar items in node's subtree, adding them to the current `parent`. */ function addChildrenRecursively(node: Node): void { - if (node === void 0 || isToken(node)) { + if (!node || isToken(node)) { return; } @@ -142,7 +142,7 @@ namespace ts.NavigationBar { let importClause = node; // Handle default import case e.g.: // import d from "mod"; - if (importClause.name !== void 0) { + if (importClause.name) { addLeafNode(importClause); } @@ -150,7 +150,7 @@ namespace ts.NavigationBar { // import * as NS from "mod"; // import {a, b as B} from "mod"; const {namedBindings} = importClause; - if (namedBindings !== void 0) { + if (namedBindings) { if (namedBindings.kind === SyntaxKind.NamespaceImport) { addLeafNode(namedBindings); } @@ -169,7 +169,7 @@ namespace ts.NavigationBar { if (isBindingPattern(name)) { addChildrenRecursively(name); } - else if (decl.initializer !== void 0 && isFunctionOrClassExpression(decl.initializer)) { + else if (decl.initializer && isFunctionOrClassExpression(decl.initializer)) { // For `const x = function() {}`, just use the function node, not the const. addChildrenRecursively(decl.initializer); } @@ -218,7 +218,7 @@ namespace ts.NavigationBar { break; default: - if (node.jsDocComments !== void 0) { + if (node.jsDocComments) { for (const jsDocComment of node.jsDocComments) { for (const tag of jsDocComment.tags) { if (tag.kind === SyntaxKind.JSDocTypedefTag) { @@ -238,13 +238,13 @@ namespace ts.NavigationBar { filterMutate(children, child => { const decl = child.node; const name = decl.name && nodeText(decl.name); - if (name === void 0) { + if (!name) { // Anonymous items are never merged. return true; } const itemsWithSameName = getProperty(nameToItems, name); - if (itemsWithSameName === void 0) { + if (!itemsWithSameName) { nameToItems[name] = child; return true; } @@ -297,12 +297,12 @@ namespace ts.NavigationBar { function merge(target: NavigationBarNode, source: NavigationBarNode): void { target.additionalNodes = target.additionalNodes || []; target.additionalNodes.push(source.node); - if (source.additionalNodes !== void 0) { + if (source.additionalNodes) { target.additionalNodes.push(...source.additionalNodes); } target.children = concatenate(target.children, source.children); - if (target.children !== void 0) { + if (target.children) { mergeChildren(target.children); sortChildren(target.children); } @@ -316,17 +316,17 @@ namespace ts.NavigationBar { function compareChildren(child1: NavigationBarNode, child2: NavigationBarNode): number { const name1 = tryGetName(child1.node), name2 = tryGetName(child2.node); - if (name1 !== void 0 && name2 !== void 0) { + if (name1 && name2) { const cmp = localeCompareFix(name1, name2); return cmp !== 0 ? cmp : navigationBarNodeKind(child1) - navigationBarNodeKind(child2); } else { - return name1 !== void 0 ? 1 : name2 !== void 0 ? -1 : navigationBarNodeKind(child1) - navigationBarNodeKind(child2); + return name1 ? 1 : name2 ? -1 : navigationBarNodeKind(child1) - navigationBarNodeKind(child2); } } // More efficient to create a collator once and use its `compare` than to call `a.localeCompare(b)` many times. - const collator: { compare(a: string, b: string): number } = typeof Intl === "undefined" ? void 0 : new Intl.Collator(); + const collator: { compare(a: string, b: string): number } = typeof Intl === "undefined" ? undefined : new Intl.Collator(); // Intl is missing in Safari, and node 0.10 treats "a" as greater than "B". const localeCompareIsCorrect = collator && collator.compare("a", "B") < 0; const localeCompareFix: (a: string, b: string) => number = localeCompareIsCorrect ? collator.compare : function(a, b) { @@ -358,7 +358,7 @@ namespace ts.NavigationBar { } const decl = node; - if (decl.name !== void 0) { + if (decl.name) { return getPropertyNameForPropertyNameNode(decl.name); } switch (node.kind) { @@ -369,7 +369,7 @@ namespace ts.NavigationBar { case SyntaxKind.JSDocTypedefTag: return getJSDocTypedefTagName(node); default: - return void 0; + return undefined; } } @@ -379,7 +379,7 @@ namespace ts.NavigationBar { } const name = (node).name; - if (name !== void 0) { + if (name) { const text = nodeText(name); if (text.length > 0) { return text; @@ -418,7 +418,7 @@ namespace ts.NavigationBar { } function getJSDocTypedefTagName(node: JSDocTypedefTag): string { - if (node.name !== void 0) { + if (node.name) { return node.name.text; } else { @@ -441,7 +441,7 @@ namespace ts.NavigationBar { function recur(item: NavigationBarNode) { if (isTopLevel(item)) { topLevel.push(item); - if (item.children !== void 0) { + if (item.children) { for (const child of item.children) { recur(child); } @@ -478,7 +478,7 @@ namespace ts.NavigationBar { return false; } function isTopLevelFunctionDeclaration(item: NavigationBarNode): boolean { - if ((item.node).body === void 0) { + if (!(item.node).body) { return false; } @@ -531,7 +531,7 @@ namespace ts.NavigationBar { function getSpans(n: NavigationBarNode): TextSpan[] { const spans = [getNodeSpan(n.node)]; - if (n.additionalNodes !== void 0) { + if (n.additionalNodes) { for (const node of n.additionalNodes) { spans.push(getNodeSpan(node)); } @@ -562,7 +562,7 @@ namespace ts.NavigationBar { while (variableDeclarationNode && variableDeclarationNode.kind !== SyntaxKind.VariableDeclaration) { variableDeclarationNode = variableDeclarationNode.parent; } - Debug.assert(variableDeclarationNode !== void 0); + Debug.assert(!!variableDeclarationNode); } else { Debug.assert(!isBindingPattern((node).name)); @@ -620,17 +620,17 @@ namespace ts.NavigationBar { } function isComputedProperty(member: EnumMember): boolean { - return member.name === void 0 || member.name.kind === SyntaxKind.ComputedPropertyName; + return !member.name || member.name.kind === SyntaxKind.ComputedPropertyName; } function getNodeSpan(node: Node): TextSpan { return node.kind === SyntaxKind.SourceFile ? createTextSpanFromBounds(node.getFullStart(), node.getEnd()) - : createTextSpanFromBounds(node.getStart(sourceFile), node.getEnd()); + : createTextSpanFromBounds(node.getStart(curSourceFile), node.getEnd()); } function getFunctionOrClassName(node: FunctionExpression | FunctionDeclaration | ArrowFunction | ClassLikeDeclaration): string { - if (node.name !== void 0 && getFullWidth(node.name) > 0) { + if (node.name && getFullWidth(node.name) > 0) { return declarationNameToString(node.name); } // See if it is a var initializer. If so, use the var name.