From f9ae3e4f2b529a2f59a0fd0f9dae1bb01c94f9a7 Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Thu, 3 Dec 2015 10:44:24 -0800 Subject: [PATCH 01/84] Initial support for globs in tsconfig.json --- src/compiler/commandLineParser.ts | 573 +++++++++++++++++- src/compiler/core.ts | 70 +++ src/compiler/diagnosticMessages.json | 4 + src/compiler/sys.ts | 46 +- src/compiler/types.ts | 21 + src/harness/external/chai.d.ts | 3 + src/harness/harness.ts | 28 +- src/harness/harnessLanguageService.ts | 15 +- src/harness/projectsRunner.ts | 19 +- src/harness/rwcRunner.ts | 9 +- src/services/shims.ts | 62 +- .../cases/unittests/cachingInServerLSHost.ts | 46 +- tests/cases/unittests/expandFiles.ts | 249 ++++++++ tests/cases/unittests/session.ts | 30 +- 14 files changed, 1087 insertions(+), 88 deletions(-) create mode 100644 tests/cases/unittests/expandFiles.ts diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index 034b7022e8232..f850ac00dba68 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -495,46 +495,51 @@ namespace ts { }; function getFileNames(): string[] { - let fileNames: string[] = []; + let fileNames: string[]; if (hasProperty(json, "files")) { - if (json["files"] instanceof Array) { - fileNames = map(json["files"], s => combinePaths(basePath, s)); + if (isArray(json["files"])) { + fileNames = json["files"]; } else { errors.push(createCompilerDiagnostic(Diagnostics.Compiler_option_0_requires_a_value_of_type_1, "files", "Array")); } } - else { - const filesSeen: Map = {}; - const exclude = json["exclude"] instanceof Array ? map(json["exclude"], normalizeSlashes) : undefined; - const supportedExtensions = getSupportedExtensions(options); - Debug.assert(indexOf(supportedExtensions, ".ts") < indexOf(supportedExtensions, ".d.ts"), "Changed priority of extensions to pick"); - - // 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) { - // .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")) { - continue; - } - // If this is one of the output extension (which would be .d.ts and .js if we are allowing compilation of js files) - // do not include this file if we included .ts or .tsx file with same base name as it could be output of the earlier compilation - if (extension === ".d.ts" || (options.allowJs && contains(supportedJavascriptExtensions, extension))) { - const baseName = fileName.substr(0, fileName.length - extension.length); - if (hasProperty(filesSeen, baseName + ".ts") || hasProperty(filesSeen, baseName + ".tsx")) { - continue; - } - } + let includeSpecs: string[]; + if (hasProperty(json, "include")) { + if (isArray(json["include"])) { + includeSpecs = json["include"]; + } + else { + errors.push(createCompilerDiagnostic(Diagnostics.Compiler_option_0_requires_a_value_of_type_1, "include", "Array")); + } + } + + let excludeSpecs: string[]; + if (hasProperty(json, "exclude")) { + if (isArray(json["exclude"])) { + excludeSpecs = json["exclude"]; + } + else { + errors.push(createCompilerDiagnostic(Diagnostics.Compiler_option_0_requires_a_value_of_type_1, "exclude", "Array")); + } + } - filesSeen[fileName] = true; - fileNames.push(fileName); + if (fileNames === undefined && includeSpecs === undefined) { + includeSpecs = ["**/*.ts"]; + if (options.jsx) { + includeSpecs.push("**/*.tsx"); + } + + if (options.allowJs) { + includeSpecs.push("**/*.js"); + if (options.jsx) { + includeSpecs.push("**/*.jsx"); } } } - return fileNames; + + return expandFiles(fileNames, includeSpecs, excludeSpecs, basePath, options, host, errors); } } @@ -584,4 +589,514 @@ namespace ts { return { options, errors }; } + + // Simplified whitelist, forces escaping of any non-word (or digit), non-whitespace character. + const reservedCharacterPattern = /[^\w\s]/g; + + const enum ExpandResult { + Ok, + Error + } + + /** + * Expands an array of file specifications. + * + * @param fileNames The literal file names to include. + * @param includeSpecs The file specifications to expand. + * @param excludeSpecs The file specifications to exclude. + * @param basePath The base path for any relative file specifications. + * @param options Compiler options. + * @param host The host used to resolve files and directories. + * @param errors An array for diagnostic reporting. + */ + export function expandFiles(fileNames: string[], includeSpecs: string[], excludeSpecs: string[], basePath: string, options: CompilerOptions, host: ParseConfigHost, errors?: Diagnostic[]): string[] { + basePath = normalizePath(basePath); + basePath = removeTrailingDirectorySeparator(basePath); + + const excludePattern = includeSpecs ? createExcludeRegularExpression(excludeSpecs, basePath, options, host, errors) : undefined; + const fileSet = createFileMap(host.useCaseSensitiveFileNames ? caseSensitiveKeyMapper : caseInsensitiveKeyMapper); + + // include every literal file. + if (fileNames) { + for (const fileName of fileNames) { + const path = toPath(fileName, basePath, caseSensitiveKeyMapper); + if (!fileSet.contains(path)) { + fileSet.set(path, path); + } + } + } + + // expand and include the provided files into the file set. + if (includeSpecs) { + for (let includeSpec of includeSpecs) { + includeSpec = normalizePath(includeSpec); + includeSpec = removeTrailingDirectorySeparator(includeSpec); + expandFileSpec(basePath, includeSpec, 0, excludePattern, options, host, errors, fileSet); + } + } + + const output = fileSet.reduce(addFileToOutput, []); + return output; + } + + /** + * Expands a file specification with wildcards. + * + * @param basePath The directory to expand. + * @param fileSpec The original file specification. + * @param start The starting offset in the file specification. + * @param excludePattern A pattern used to exclude a file specification. + * @param options Compiler options. + * @param host The host used to resolve files and directories. + * @param errors An array for diagnostic reporting. + * @param fileSet The set of matching files. + * @param isExpandingRecursiveDirectory A value indicating whether the file specification includes a recursive directory wildcard prior to the start of this segment. + */ + function expandFileSpec(basePath: string, fileSpec: string, start: number, excludePattern: RegExp, options: CompilerOptions, host: ParseConfigHost, errors: Diagnostic[], fileSet: FileMap, isExpandingRecursiveDirectory?: boolean): ExpandResult { + // Skip expansion if the base path matches an exclude pattern. + if (isExcludedPath(excludePattern, basePath)) { + return ExpandResult.Ok; + } + + // Find the offset of the next wildcard in the file specification + let offset = indexOfWildcard(fileSpec, start); + if (offset < 0) { + // There were no more wildcards, so include the file. + const path = toPath(fileSpec.substring(start), basePath, caseSensitiveKeyMapper); + includeFile(path, excludePattern, options, host, fileSet); + return ExpandResult.Ok; + } + + // Find the last directory separator before the wildcard to get the leading path. + offset = fileSpec.lastIndexOf(directorySeparator, offset); + if (offset > start) { + // The wildcard occurs in a later segment, include remaining path up to + // wildcard in prefix. + basePath = combinePaths(basePath, fileSpec.substring(start, offset)); + + // Skip this wildcard path if the base path now matches an exclude pattern. + if (isExcludedPath(excludePattern, basePath)) { + return ExpandResult.Ok; + } + + start = offset + 1; + } + + // Find the offset of the next directory separator to extract the wildcard path segment. + offset = getEndOfPathSegment(fileSpec, start); + + // Check if the current offset is the beginning of a recursive directory pattern. + if (isRecursiveDirectoryWildcard(fileSpec, start, offset)) { + if (offset >= fileSpec.length) { + // If there is no file specification following the recursive directory pattern + // we cannot match any files, so we will ignore this pattern. + return ExpandResult.Ok; + } + + // Stop expansion if a file specification contains more than one recursive directory pattern. + if (isExpandingRecursiveDirectory) { + if (errors) { + errors.push(createCompilerDiagnostic(Diagnostics.File_specification_cannot_contain_multiple_recursive_directory_wildcards_Asterisk_Asterisk_Colon_0, fileSpec)); + } + + return ExpandResult.Error; + } + + // Expand the recursive directory pattern. + return expandRecursiveDirectory(basePath, fileSpec, offset + 1, excludePattern, options, host, errors, fileSet); + } + + // Match the entries in the directory against the wildcard pattern. + const pattern = createRegularExpressionFromWildcard(fileSpec, start, offset, host); + + // If there are no more directory separators (the offset is at the end of the file specification), then + // this must be a file. + if (offset >= fileSpec.length) { + const files = host.readFileNames(basePath); + for (const extension of getSupportedExtensions(options)) { + for (const file of files) { + if (fileExtensionIs(file, extension)) { + const path = toPath(file, basePath, caseSensitiveKeyMapper); + + // .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(file, ".d.ts")) { + continue; + } + + // If this is one of the output extension (which would be .d.ts and .js if we are allowing compilation of js files) + // do not include this file if we included .ts or .tsx file with same base name as it could be output of the earlier compilation + if (extension === ".d.ts" || (options.allowJs && contains(supportedJavascriptExtensions, extension))) { + if (fileSet.contains(changeExtension(path, ".ts")) || fileSet.contains(changeExtension(path, ".tsx"))) { + continue; + } + } + + // This wildcard has no further directory to process, so include the file. + includeFile(path, excludePattern, options, host, fileSet); + } + } + } + } + else { + const directories = host.readDirectoryNames(basePath); + for (const directory of directories) { + // If this was a directory, process the directory. + const path = toPath(directory, basePath, caseSensitiveKeyMapper); + if (expandFileSpec(path, fileSpec, offset + 1, excludePattern, options, host, errors, fileSet, isExpandingRecursiveDirectory) === ExpandResult.Error) { + return ExpandResult.Error; + } + } + } + + return ExpandResult.Ok; + } + + /** + * Expands a `**` recursive directory wildcard. + * + * @param basePath The directory to recursively expand. + * @param fileSpec The original file specification. + * @param start The starting offset in the file specification. + * @param excludePattern A pattern used to exclude a file specification. + * @param options Compiler options. + * @param host The host used to resolve files and directories. + * @param errors An array for diagnostic reporting. + * @param fileSet The set of matching files. + */ + function expandRecursiveDirectory(basePath: string, fileSpec: string, start: number, excludePattern: RegExp, options: CompilerOptions, host: ParseConfigHost, errors: Diagnostic[], fileSet: FileMap): ExpandResult { + // expand the non-recursive part of the file specification against the prefix path. + if (expandFileSpec(basePath, fileSpec, start, excludePattern, options, host, errors, fileSet, /*isExpandingRecursiveDirectory*/ true) === ExpandResult.Error) { + return ExpandResult.Error; + } + + // Recursively expand each subdirectory. + const directories = host.readDirectoryNames(basePath); + for (const entry of directories) { + const path = combinePaths(basePath, entry); + if (expandRecursiveDirectory(path, fileSpec, start, excludePattern, options, host, errors, fileSet) === ExpandResult.Error) { + return ExpandResult.Error; + } + } + + return ExpandResult.Ok; + } + + /** + * Attempts to include a file in a file set. + * + * @param file The file to include. + * @param excludePattern A pattern used to exclude a file specification. + * @param options Compiler options. + * @param host The host used to resolve files and directories. + * @param fileSet The set of matching files. + */ + function includeFile(file: Path, excludePattern: RegExp, options: CompilerOptions, host: ParseConfigHost, fileSet: FileMap): void { + // Ignore the file if it should be excluded. + if (isExcludedPath(excludePattern, file)) { + return; + } + + // Ignore the file if it doesn't exist. + if (!host.fileExists(file)) { + return; + } + + // Ignore the file if it does not have a supported extension. + if (!options.allowNonTsExtensions && !isSupportedSourceFileName(file, options)) { + return; + } + + if (!fileSet.contains(file)) { + fileSet.set(file, file); + } + } + + /** + * Adds a file to an array of files. + * + * @param output The output array. + * @param file The file path. + */ + function addFileToOutput(output: string[], file: string) { + output.push(file); + return output; + } + + /** + * Determines whether a path should be excluded. + * + * @param excludePattern A pattern used to exclude a file specification. + * @param path The path to test for exclusion. + */ + function isExcludedPath(excludePattern: RegExp, path: string) { + return excludePattern ? excludePattern.test(path) : false; + } + + /** + * Creates a regular expression from a glob-style wildcard. + * + * @param fileSpec The file specification. + * @param start The starting offset in the file specification. + * @param end The end offset in the file specification. + * @param host The host used to resolve files and directories. + */ + function createRegularExpressionFromWildcard(fileSpec: string, start: number, end: number, host: ParseConfigHost): RegExp { + const pattern = createPatternFromWildcard(fileSpec, start, end); + return new RegExp("^" + pattern + "$", host.useCaseSensitiveFileNames ? "" : "i"); + } + + /** + * Creates a pattern from a wildcard segment. + * + * @param fileSpec The file specification. + * @param start The starting offset in the file specification. + * @param end The end offset in the file specification. + */ + function createPatternFromWildcard(fileSpec: string, start: number, end: number): string { + let pattern = ""; + let offset = indexOfWildcard(fileSpec, start); + while (offset >= 0 && offset < end) { + if (offset > start) { + // Escape and append the non-wildcard portion to the regular expression + pattern += escapeRegularExpressionText(fileSpec, start, offset); + } + + const charCode = fileSpec.charCodeAt(offset); + if (charCode === CharacterCodes.asterisk) { + // Append a multi-character (zero or more characters) pattern to the regular expression + pattern += "[^/]*"; + } + else if (charCode === CharacterCodes.question) { + // Append a single-character (zero or one character) pattern to the regular expression + pattern += "[^/]"; + } + + start = offset + 1; + offset = indexOfWildcard(fileSpec, start); + } + + // Escape and append any remaining non-wildcard portion. + if (start < end) { + pattern += escapeRegularExpressionText(fileSpec, start, end); + } + + return pattern; + } + + /** + * Creates a regular expression from a glob-style wildcard used to exclude a file. + * + * @param excludeSpecs The file specifications to exclude. + * @param basePath The prefix path. + * @param options Compiler options. + * @param host The host used to resolve files and directories. + * @param errors An array for diagnostic reporting. + */ + function createExcludeRegularExpression(excludeSpecs: string[], basePath: string, options: CompilerOptions, host: ParseConfigHost, errors: Diagnostic[]): RegExp { + // Ignore an empty exclusion list + if (!excludeSpecs || excludeSpecs.length === 0) { + return undefined; + } + + basePath = escapeRegularExpressionText(basePath, 0, basePath.length); + + let pattern = ""; + for (const excludeSpec of excludeSpecs) { + const excludePattern = createExcludePattern(excludeSpec, basePath, options, host, errors); + if (excludePattern) { + if (pattern.length > 0) { + pattern += "|"; + } + + pattern += "(" + excludePattern + ")"; + } + } + + if (pattern.length > 0) { + return new RegExp("^(" + pattern + ")($|/)", host.useCaseSensitiveFileNames ? "" : "i"); + } + + return undefined; + } + + /** + * Creates a pattern for used to exclude a file. + * + * @param excludeSpec The file specification to exclude. + * @param basePath The base path for the exclude pattern. + * @param options Compiler options. + * @param host The host used to resolve files and directories. + * @param errors An array for diagnostic reporting. + */ + function createExcludePattern(excludeSpec: string, basePath: string, options: CompilerOptions, host: ParseConfigHost, errors: Diagnostic[]): string { + if (!excludeSpec) { + return undefined; + } + + excludeSpec = normalizePath(excludeSpec); + excludeSpec = removeTrailingDirectorySeparator(excludeSpec); + + let pattern = isRootedDiskPath(excludeSpec) ? "" : basePath; + let hasRecursiveDirectoryWildcard = false; + let segmentStart = 0; + let segmentEnd = getEndOfPathSegment(excludeSpec, segmentStart); + while (segmentStart < segmentEnd) { + if (isRecursiveDirectoryWildcard(excludeSpec, segmentStart, segmentEnd)) { + if (hasRecursiveDirectoryWildcard) { + if (errors) { + errors.push(createCompilerDiagnostic(Diagnostics.File_specification_cannot_contain_multiple_recursive_directory_wildcards_Asterisk_Asterisk_Colon_0, excludeSpec)); + } + + return undefined; + } + + // As an optimization, if the recursive directory is the last + // wildcard, or is followed by only `*` or `*.ts`, don't add the + // remaining pattern and exit the loop. + if (canElideRecursiveDirectorySegment(excludeSpec, segmentEnd, options, host)) { + break; + } + + hasRecursiveDirectoryWildcard = true; + pattern += "(/.+)?"; + } + else { + if (pattern) { + pattern += directorySeparator; + } + + pattern += createPatternFromWildcard(excludeSpec, segmentStart, segmentEnd); + } + + segmentStart = segmentEnd + 1; + segmentEnd = getEndOfPathSegment(excludeSpec, segmentStart); + } + + return pattern; + } + + /** + * Determines whether a recursive directory segment can be elided when + * building a regular expression to exclude a path. + * + * @param excludeSpec The file specification used to exclude a path. + * @param segmentEnd The end position of the recursive directory segment. + * @param options Compiler options. + * @param host The host used to resolve files and directories. + */ + function canElideRecursiveDirectorySegment(excludeSpec: string, segmentEnd: number, options: CompilerOptions, host: ParseConfigHost) { + // If there are no segments after this segment, the pattern for this segment may be elided. + if (segmentEnd + 1 >= excludeSpec.length) { + return true; + } + + // If the following segment is a wildcard that may be elided, the pattern for this segment may be elided. + return canElideWildcardSegment(excludeSpec, segmentEnd + 1, options, host); + } + + /** + * Determines whether a wildcard segment can be elided when building a + * regular expression to exclude a path. + * + * @param excludeSpec The file specification used to exclude a path. + * @param segmentStart The starting position of the segment. + * @param options Compiler options. + * @param host The host used to resolve files and directories. + */ + function canElideWildcardSegment(excludeSpec: string, segmentStart: number, options: CompilerOptions, host: ParseConfigHost) { + const charCode = excludeSpec.charCodeAt(segmentStart); + if (charCode === CharacterCodes.asterisk) { + const end = excludeSpec.length; + + // If the segment consists only of `*`, we may elide this segment. + if (segmentStart + 1 === end) { + return true; + } + + // If the segment consists only of `*.ts`, and we do not allow + // any other extensions for source files, we may elide this segment. + if (!options.allowNonTsExtensions && !options.jsx && !options.allowJs && segmentStart + 4 === end) { + const segment = excludeSpec.substr(segmentStart); + return fileExtensionIs(host.useCaseSensitiveFileNames ? segment : segment.toLowerCase(), ".ts"); + } + } + return false; + } + + /** + * Escape regular expression reserved tokens. + * + * @param text The text to escape. + * @param start The starting offset in the string. + * @param end The ending offset in the string. + */ + function escapeRegularExpressionText(text: string, start: number, end: number) { + return text.substring(start, end).replace(reservedCharacterPattern, "\\$&"); + } + + /** + * Determines whether the wildcard at the current offset is a recursive directory wildcard. + * + * @param fileSpec The file specification. + * @param segmentStart The starting offset of a segment in the file specification. + * @param segmentEnd The ending offset of a segment in the file specification. + */ + function isRecursiveDirectoryWildcard(fileSpec: string, segmentStart: number, segmentEnd: number) { + return segmentEnd - segmentStart === 2 && + fileSpec.charCodeAt(segmentStart) === CharacterCodes.asterisk && + fileSpec.charCodeAt(segmentStart + 1) === CharacterCodes.asterisk; + } + + /** + * Gets the index of the next wildcard character in a file specification. + * + * @param fileSpec The file specification. + * @param start The starting offset in the file specification. + */ + function indexOfWildcard(fileSpec: string, start: number): number { + for (let i = start; i < fileSpec.length; ++i) { + const ch = fileSpec.charCodeAt(i); + if (ch === CharacterCodes.asterisk || ch === CharacterCodes.question) { + return i; + } + } + + return -1; + } + + /** + * Get the end position of a path segment, either the index of the next directory separator or + * the provided end position. + * + * @param fileSpec The file specification. + * @param segmentStart The start offset in the file specification. + */ + function getEndOfPathSegment(fileSpec: string, segmentStart: number): number { + const end = fileSpec.length; + if (segmentStart >= end) { + return end; + } + + const offset = fileSpec.indexOf(directorySeparator, segmentStart); + return offset < 0 ? end : offset; + } + + /** + * Gets a case sensitive key. + * + * @param key The original key. + */ + function caseSensitiveKeyMapper(key: string) { + return key; + } + + /** + * Gets a case insensitive key. + * + * @param key The original key. + */ + function caseInsensitiveKeyMapper(key: string) { + return key.toLowerCase(); + } } diff --git a/src/compiler/core.ts b/src/compiler/core.ts index cae7bd8210341..aae4b54b5c7e6 100644 --- a/src/compiler/core.ts +++ b/src/compiler/core.ts @@ -25,6 +25,7 @@ namespace ts { contains, remove, forEachValue: forEachValueInMap, + reduce, clear }; @@ -34,6 +35,10 @@ namespace ts { } } + function reduce(callback: (memo: U, value: T, key: Path) => U, initial: U) { + return reduceProperties(files, callback, initial); + } + // path should already be well-formed so it does not need to be normalized function get(path: Path): T { return files[toKey(path)]; @@ -490,6 +495,24 @@ namespace ts { return a < b ? Comparison.LessThan : Comparison.GreaterThan; } + export function compareStrings(a: string, b: string, ignoreCase?: boolean): Comparison { + if (a === b) return Comparison.EqualTo; + if (a === undefined) return Comparison.LessThan; + if (b === undefined) return Comparison.GreaterThan; + if (ignoreCase) { + if (String.prototype.localeCompare) { + const result = a.localeCompare(b, /*locales*/ undefined, { usage: "sort", sensitivity: "accent" }); + return result < 0 ? Comparison.LessThan : result > 0 ? Comparison.GreaterThan : Comparison.EqualTo; + } + + a = a.toUpperCase(); + b = b.toUpperCase(); + if (a === b) return Comparison.EqualTo; + } + + return a < b ? Comparison.LessThan : Comparison.GreaterThan; + } + function getDiagnosticFileName(diagnostic: Diagnostic): string { return diagnostic.file ? diagnostic.file.fileName : undefined; } @@ -756,6 +779,49 @@ namespace ts { return path1 + directorySeparator + path2; } + /** + * Removes a trailing directory separator from a path. + * @param path The path. + */ + export function removeTrailingDirectorySeparator(path: string) { + if (path.charAt(path.length - 1) === directorySeparator) { + return path.substr(0, path.length - 1); + } + + return path; + } + + /** + * Adds a trailing directory separator to a path, if it does not already have one. + * @param path The path. + */ + export function ensureTrailingDirectorySeparator(path: string) { + if (path.charAt(path.length - 1) !== directorySeparator) { + return path + directorySeparator; + } + + return path; + } + + export function comparePaths(a: string, b: string, currentDirectory: string, ignoreCase?: boolean) { + if (a === b) return Comparison.EqualTo; + if (a === undefined) return Comparison.LessThan; + if (b === undefined) return Comparison.GreaterThan; + a = removeTrailingDirectorySeparator(a); + b = removeTrailingDirectorySeparator(b); + const aComponents = getNormalizedPathComponents(a, currentDirectory); + const bComponents = getNormalizedPathComponents(b, currentDirectory); + const sharedLength = Math.min(aComponents.length, bComponents.length); + for (let i = 0; i < sharedLength; ++i) { + const result = compareStrings(aComponents[i], bComponents[i], ignoreCase); + if (result !== Comparison.EqualTo) { + return result; + } + } + + return compareValues(aComponents.length, bComponents.length); + } + export function fileExtensionIs(path: string, extension: string): boolean { const pathLen = path.length; const extLen = extension.length; @@ -794,6 +860,10 @@ namespace ts { return path; } + export function changeExtension(path: T, newExtension: string): T { + return (removeFileExtension(path) + newExtension); + } + const backslashOrDoubleQuote = /[\"\\]/g; const escapedCharsRegExp = /[\u0000-\u001f\t\v\f\b\r\n\u2028\u2029\u0085]/g; const escapedCharsMap: Map = { diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 0c19f6d8247a0..6a1d0e5ce650f 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -2020,6 +2020,10 @@ "category": "Error", "code": 5009 }, + "File specification cannot contain multiple recursive directory wildcards ('**'): '{0}'.": { + "category": "Error", + "code": 5011 + }, "Cannot read file '{0}': {1}": { "category": "Error", "code": 5012 diff --git a/src/compiler/sys.ts b/src/compiler/sys.ts index 3a90ca42fa152..fc8e90ac38fc3 100644 --- a/src/compiler/sys.ts +++ b/src/compiler/sys.ts @@ -17,6 +17,8 @@ namespace ts { getExecutingFilePath(): string; getCurrentDirectory(): string; readDirectory(path: string, extension?: string, exclude?: string[]): string[]; + readFileNames(path: string): string[]; + readDirectoryNames(path: string): string[]; getMemoryUsage?(): number; exit(exitCode?: number): void; } @@ -155,6 +157,16 @@ namespace ts { } } + function readFileNames(path: string): string[] { + const folder = fso.GetFolder(path || "."); + return getNames(folder.files); + } + + function readDirectoryNames(path: string): string[] { + const folder = fso.GetFolder(path || "."); + return getNames(folder.directories); + } + return { args, newLine: "\r\n", @@ -185,6 +197,8 @@ namespace ts { return new ActiveXObject("WScript.Shell").CurrentDirectory; }, readDirectory, + readFileNames, + readDirectoryNames, exit(exitCode?: number): void { try { WScript.Quit(exitCode); @@ -281,7 +295,7 @@ namespace ts { // REVIEW: for now this implementation uses polling. // The advantage of polling is that it works reliably // on all os and with network mounted files. - // For 90 referenced files, the average time to detect + // For 90 referenced files, the average time to detect // changes is 2*msInterval (by default 5 seconds). // The overhead of this is .04 percent (1/2500) with // average pause of < 1 millisecond (and max @@ -381,6 +395,30 @@ namespace ts { } } + function readFileNames(path: string): string[] { + const entries = _fs.readdirSync(path || "."); + const files: string[] = []; + for (const entry of entries) { + const stat = _fs.statSync(combinePaths(path, entry)); + if (stat.isFile()) { + files.push(entry); + } + } + return files.sort(); + } + + function readDirectoryNames(path: string): string[] { + const entries = _fs.readdirSync(path || "."); + const directories: string[] = []; + for (const entry of entries) { + const stat = _fs.statSync(combinePaths(path, entry)); + if (stat.isDirectory()) { + directories.push(entry); + } + } + return directories.sort(); + } + return { args: process.argv.slice(2), newLine: _os.EOL, @@ -406,7 +444,7 @@ namespace ts { }; }, watchDirectory: (path, callback, recursive) => { - // Node 4.0 `fs.watch` function supports the "recursive" option on both OSX and Windows + // Node 4.0 `fs.watch` function supports the "recursive" option on both OSX and Windows // (ref: https://github.com/nodejs/node/pull/2649 and https://github.com/Microsoft/TypeScript/issues/4643) return _fs.watch( path, @@ -426,7 +464,7 @@ namespace ts { return _path.resolve(path); }, fileExists(path: string): boolean { - return _fs.existsSync(path); + return _fs.existsSync(path) && _fs.statSync(path).isFile(); }, directoryExists(path: string) { return _fs.existsSync(path) && _fs.statSync(path).isDirectory(); @@ -443,6 +481,8 @@ namespace ts { return process.cwd(); }, readDirectory, + readFileNames, + readDirectoryNames, getMemoryUsage() { if (global.gc) { global.gc(); diff --git a/src/compiler/types.ts b/src/compiler/types.ts index dbd0322aa1802..ffd9ddc206d1d 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -14,6 +14,7 @@ namespace ts { remove(fileName: Path): void; forEachValue(f: (key: Path, v: T) => void): void; + reduce(f: (memo: U, value: T, key: Path) => U, initial: U): U; clear(): void; } @@ -1582,7 +1583,27 @@ namespace ts { } export interface ParseConfigHost { + useCaseSensitiveFileNames: boolean; + readDirectory(rootDir: string, extension: string, exclude: string[]): string[]; + + /** + * Gets a value indicating whether the specified path exists. + * @param path The path to test. + */ + fileExists(path: string): boolean; + + /** + * Reads the files names in the directory. + * @param rootDir The directory path. + */ + readFileNames(rootDir: string): string[]; + + /** + * Reads the directory names in the directory. + * @param rootDir The directory path. + */ + readDirectoryNames(rootDir: string): string[]; } export interface WriteFileCallback { diff --git a/src/harness/external/chai.d.ts b/src/harness/external/chai.d.ts index 814de75e7b254..fc25980d3a099 100644 --- a/src/harness/external/chai.d.ts +++ b/src/harness/external/chai.d.ts @@ -167,6 +167,9 @@ declare module chai { module assert { function equal(actual: any, expected: any, message?: string): void; function notEqual(actual: any, expected: any, message?: string): void; + function deepEqual(actual: any, expected: any, message?: string): void; + function notDeepEqual(actual: any, expected: any, message?: string): void; + function lengthOf(object: any[], length: number, message?: string): void; function isTrue(value: any, message?: string): void; function isFalse(value: any, message?: string): void; function isNull(value: any, message?: string): void; diff --git a/src/harness/harness.ts b/src/harness/harness.ts index 15f3813e54d2c..d8ccc512343c5 100644 --- a/src/harness/harness.ts +++ b/src/harness/harness.ts @@ -1,7 +1,7 @@ // // Copyright (c) Microsoft Corporation. All rights reserved. -// +// // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at @@ -282,7 +282,7 @@ namespace Utils { break; case "parserContextFlags": - // Clear the flag that are produced by aggregating child values.. That is ephemeral + // Clear the flag that are produced by aggregating child values.. That is ephemeral // data we don't care about in the dump. We only care what the parser set directly // on the ast. let value = n.parserContextFlags & ts.ParserContextFlags.ParserGeneratedFlags; @@ -356,7 +356,7 @@ namespace Utils { assert.equal(node1.kind, node2.kind, "node1.kind !== node2.kind"); assert.equal(node1.flags, node2.flags, "node1.flags !== node2.flags"); - // call this on both nodes to ensure all propagated flags have been set (and thus can be + // call this on both nodes to ensure all propagated flags have been set (and thus can be // compared). assert.equal(ts.containsParseError(node1), ts.containsParseError(node2)); assert.equal(node1.parserContextFlags, node2.parserContextFlags, "node1.parserContextFlags !== node2.parserContextFlags"); @@ -436,6 +436,8 @@ namespace Harness { getExecutingFilePath(): string; exit(exitCode?: number): void; readDirectory(path: string, extension?: string, exclude?: string[]): string[]; + readDirectoryNames(path: string): string[]; + readFileNames(path: string): string[]; } export var IO: IO; @@ -474,6 +476,8 @@ namespace Harness { export const fileExists: typeof IO.fileExists = fso.FileExists; export const log: typeof IO.log = global.WScript && global.WScript.StdOut.WriteLine; export const readDirectory: typeof IO.readDirectory = (path, extension, exclude) => ts.sys.readDirectory(path, extension, exclude); + export const readDirectoryNames: typeof IO.readDirectoryNames = path => ts.sys.readDirectoryNames(path); + export const readFileNames: typeof IO.readFileNames = path => ts.sys.readFileNames(path); export function createDirectory(path: string) { if (directoryExists(path)) { @@ -544,6 +548,8 @@ namespace Harness { export const log: typeof IO.log = s => console.log(s); export const readDirectory: typeof IO.readDirectory = (path, extension, exclude) => ts.sys.readDirectory(path, extension, exclude); + export const readDirectoryNames: typeof IO.readDirectoryNames = path => ts.sys.readDirectoryNames(path); + export const readFileNames: typeof IO.readFileNames = path => ts.sys.readFileNames(path); export function createDirectory(path: string) { if (!directoryExists(path)) { @@ -752,6 +758,14 @@ namespace Harness { export function readDirectory(path: string, extension?: string, exclude?: string[]) { return listFiles(path).filter(f => !extension || ts.fileExtensionIs(f, extension)); } + + export function readDirectoryNames(path: string): string[] { + return []; + } + + export function readFileNames(path: string) { + return readDirectory(path); + } } } @@ -777,7 +791,7 @@ namespace Harness { (emittedFile: string, emittedLine: number, emittedColumn: number, sourceFile: string, sourceLine: number, sourceColumn: number, sourceName: string): void; } - // Settings + // Settings export let userSpecifiedRoot = ""; export let lightMode = false; @@ -816,7 +830,7 @@ namespace Harness { fileName: string, sourceText: string, languageVersion: ts.ScriptTarget) { - // We'll only assert invariants outside of light mode. + // We'll only assert invariants outside of light mode. const shouldAssertInvariants = !Harness.lightMode; // Only set the parent nodes if we're asserting invariants. We don't need them otherwise. @@ -911,7 +925,7 @@ namespace Harness { libFiles?: string; } - // Additional options not already in ts.optionDeclarations + // Additional options not already in ts.optionDeclarations const harnessOptionDeclarations: ts.CommandLineOption[] = [ { name: "allowNonTsExtensions", type: "boolean" }, { name: "useCaseSensitiveFileNames", type: "boolean" }, @@ -1149,7 +1163,7 @@ namespace Harness { errLines.forEach(e => outputLines.push(e)); // do not count errors from lib.d.ts here, they are computed separately as numLibraryDiagnostics - // if lib.d.ts is explicitly included in input files and there are some errors in it (i.e. because of duplicate identifiers) + // if lib.d.ts is explicitly included in input files and there are some errors in it (i.e. because of duplicate identifiers) // then they will be added twice thus triggering 'total errors' assertion with condition // 'totalErrorsReportedInNonLibraryFiles + numLibraryDiagnostics + numTest262HarnessDiagnostics, diagnostics.length diff --git a/src/harness/harnessLanguageService.ts b/src/harness/harnessLanguageService.ts index 3c7814df56258..e12105e3393ea 100644 --- a/src/harness/harnessLanguageService.ts +++ b/src/harness/harnessLanguageService.ts @@ -259,6 +259,12 @@ namespace Harness.LanguageService { readDirectory(rootDir: string, extension: string): string { throw new Error("NYI"); } + readDirectoryNames(path: string): string { + throw new Error("Not implemented."); + } + readFileNames(path: string): string { + throw new Error("Not implemented."); + } fileExists(fileName: string) { return this.getScriptInfo(fileName) !== undefined; } readFile(fileName: string) { const snapshot = this.nativeHost.getScriptSnapshot(fileName); @@ -572,6 +578,14 @@ namespace Harness.LanguageService { throw new Error("Not implemented Yet."); } + readDirectoryNames(path: string): string[] { + throw new Error("Not implemented."); + } + + readFileNames(path: string): string[] { + throw new Error("Not implemented."); + } + watchFile(fileName: string, callback: (fileName: string) => void): ts.FileWatcher { return { close() { } }; } @@ -644,4 +658,3 @@ namespace Harness.LanguageService { getPreProcessedFileInfo(fileName: string, fileContents: string): ts.PreProcessedFileInfo { throw new Error("getPreProcessedFileInfo is not available using the server interface."); } } } - \ No newline at end of file diff --git a/src/harness/projectsRunner.ts b/src/harness/projectsRunner.ts index 5210266345759..655c2f07c2bd9 100644 --- a/src/harness/projectsRunner.ts +++ b/src/harness/projectsRunner.ts @@ -210,7 +210,14 @@ class ProjectRunner extends RunnerBase { } const configObject = result.config; - const configParseResult = ts.parseJsonConfigFileContent(configObject, { readDirectory }, ts.getDirectoryPath(configFileName), compilerOptions); + const configParseHost: ts.ParseConfigHost = { + useCaseSensitiveFileNames: Harness.IO.useCaseSensitiveFileNames(), + fileExists, + readDirectory, + readDirectoryNames, + readFileNames + }; + const configParseResult = ts.parseJsonConfigFileContent(configObject, configParseHost, ts.getDirectoryPath(configFileName), compilerOptions); if (configParseResult.errors.length > 0) { return { moduleKind, @@ -237,7 +244,7 @@ class ProjectRunner extends RunnerBase { mapRoot: testCase.resolveMapRoot && testCase.mapRoot ? Harness.IO.resolvePath(testCase.mapRoot) : testCase.mapRoot, sourceRoot: testCase.resolveSourceRoot && testCase.sourceRoot ? Harness.IO.resolvePath(testCase.sourceRoot) : testCase.sourceRoot, module: moduleKind, - moduleResolution: ts.ModuleResolutionKind.Classic, // currently all tests use classic module resolution kind, this will change in the future + moduleResolution: ts.ModuleResolutionKind.Classic, // currently all tests use classic module resolution kind, this will change in the future }; // Set the values specified using json const optionNameMap: ts.Map = {}; @@ -278,6 +285,14 @@ class ProjectRunner extends RunnerBase { return result; } + function readDirectoryNames(path: string) { + return Harness.IO.readDirectoryNames(getFileNameInTheProjectTest(path)); + } + + function readFileNames(path: string) { + return Harness.IO.readFileNames(getFileNameInTheProjectTest(path)); + } + function fileExists(fileName: string): boolean { return Harness.IO.fileExists(getFileNameInTheProjectTest(fileName)); } diff --git a/src/harness/rwcRunner.ts b/src/harness/rwcRunner.ts index ce570a7d6ad01..5b4e2a7a3d96d 100644 --- a/src/harness/rwcRunner.ts +++ b/src/harness/rwcRunner.ts @@ -76,7 +76,14 @@ namespace RWC { if (tsconfigFile) { const tsconfigFileContents = getHarnessCompilerInputUnit(tsconfigFile.path); const parsedTsconfigFileContents = ts.parseConfigFileTextToJson(tsconfigFile.path, tsconfigFileContents.content); - const configParseResult = ts.parseJsonConfigFileContent(parsedTsconfigFileContents.config, Harness.IO, ts.getDirectoryPath(tsconfigFile.path)); + const configParseHost: ts.ParseConfigHost = { + useCaseSensitiveFileNames: Harness.IO.useCaseSensitiveFileNames(), + fileExists: Harness.IO.fileExists, + readDirectory: Harness.IO.readDirectory, + readDirectoryNames: Harness.IO.readDirectoryNames, + readFileNames: Harness.IO.readFileNames, + }; + const configParseResult = ts.parseJsonConfigFileContent(parsedTsconfigFileContents.config, configParseHost, ts.getDirectoryPath(tsconfigFile.path)); fileNames = configParseResult.fileNames; opts.options = ts.extend(opts.options, configParseResult.options); } diff --git a/src/services/shims.ts b/src/services/shims.ts index 6b656ea2738fb..4c35f5aaf1b64 100644 --- a/src/services/shims.ts +++ b/src/services/shims.ts @@ -60,7 +60,7 @@ namespace ts { getNewLine?(): string; getProjectVersion?(): string; useCaseSensitiveFileNames?(): boolean; - + getModuleResolutionsForFile?(fileName: string): string; } @@ -73,6 +73,9 @@ namespace ts { * when enumerating the directory. */ readDirectory(rootDir: string, extension: string, exclude?: string): string; + readDirectoryNames?(rootDir: string): string; + readFileNames?(rootDir: string): string; + useCaseSensitiveFileNames?: boolean; } /// @@ -272,12 +275,12 @@ namespace ts { private files: string[]; private loggingEnabled = false; private tracingEnabled = false; - + public resolveModuleNames: (moduleName: string[], containingFile: string) => ResolvedModule[]; - + constructor(private shimHost: LanguageServiceShimHost) { // if shimHost is a COM object then property check will become method call with no arguments. - // 'in' does not have this effect. + // 'in' does not have this effect. if ("getModuleResolutionsForFile" in this.shimHost) { this.resolveModuleNames = (moduleNames: string[], containingFile: string) => { let resolutionsInFile = >JSON.parse(this.shimHost.getModuleResolutionsForFile(containingFile)); @@ -407,7 +410,18 @@ namespace ts { export class CoreServicesShimHostAdapter implements ParseConfigHost { + public useCaseSensitiveFileNames: boolean; + constructor(private shimHost: CoreServicesShimHost) { + if (typeof shimHost.useCaseSensitiveFileNames === "boolean") { + this.useCaseSensitiveFileNames = shimHost.useCaseSensitiveFileNames; + } + else if (sys) { + this.useCaseSensitiveFileNames = sys.useCaseSensitiveFileNames; + } + else { + this.useCaseSensitiveFileNames = true; + } } public readDirectory(rootDir: string, extension: string, exclude: string[]): string[] { @@ -424,11 +438,41 @@ namespace ts { } return JSON.parse(encoded); } - + + public readDirectoryNames(path: string): string[] { + if (this.shimHost.readDirectory) { + const encoded = this.shimHost.readDirectoryNames(path); + return JSON.parse(encoded); + } + + if (sys) { + path = normalizePath(path); + path = ensureTrailingDirectorySeparator(path); + return sys.readDirectoryNames(path); + } + + return []; + } + + public readFileNames(path: string): string[] { + if (this.shimHost.readFileNames) { + const encoded = this.shimHost.readFileNames(path); + return JSON.parse(encoded); + } + + if (sys) { + path = normalizePath(path); + path = ensureTrailingDirectorySeparator(path); + return sys.readFileNames(path); + } + + return []; + } + public fileExists(fileName: string): boolean { return this.shimHost.fileExists(fileName); } - + public readFile(fileName: string): string { return this.shimHost.readFile(fileName); } @@ -940,7 +984,7 @@ namespace ts { private forwardJSONCall(actionDescription: string, action: () => any): any { return forwardJSONCall(this.logger, actionDescription, action, this.logPerformance); } - + public resolveModuleName(fileName: string, moduleName: string, compilerOptionsJson: string): string { return this.forwardJSONCall(`resolveModuleName('${fileName}')`, () => { let compilerOptions = JSON.parse(compilerOptionsJson); @@ -949,14 +993,14 @@ namespace ts { resolvedFileName: result.resolvedModule ? result.resolvedModule.resolvedFileName: undefined, failedLookupLocations: result.failedLookupLocations }; - }); + }); } public getPreProcessedFileInfo(fileName: string, sourceTextSnapshot: IScriptSnapshot): string { return this.forwardJSONCall( "getPreProcessedFileInfo('" + fileName + "')", () => { - // for now treat files as JavaScript + // for now treat files as JavaScript var result = preProcessFile(sourceTextSnapshot.getText(0, sourceTextSnapshot.getLength()), /* readImportFiles */ true, /* detectJavaScriptImports */ true); var convertResult = { referencedFiles: [], diff --git a/tests/cases/unittests/cachingInServerLSHost.ts b/tests/cases/unittests/cachingInServerLSHost.ts index 88b44a693b940..f0a138c27a196 100644 --- a/tests/cases/unittests/cachingInServerLSHost.ts +++ b/tests/cases/unittests/cachingInServerLSHost.ts @@ -39,6 +39,8 @@ module ts { readDirectory: (path: string, extension?: string, exclude?: string[]): string[] => { throw new Error("NYI"); }, + readDirectoryNames: (path: string): string[] => { throw new Error("NYI"); }, + readFileNames: (path: string): string[] => { throw new Error("NYI"); }, exit: (exitCode?: number) => { }, watchFile: (path, callback) => { @@ -69,8 +71,8 @@ module ts { let projectService = new server.ProjectService(serverHost, logger); let rootScriptInfo = projectService.openFile(rootFile, /* openedByClient */true); let project = projectService.createInferredProject(rootScriptInfo); - project.setProjectOptions( {files: [rootScriptInfo.fileName], compilerOptions: {module: ts.ModuleKind.AMD} } ); - return { + project.setProjectOptions( {files: [rootScriptInfo.fileName], compilerOptions: {module: ts.ModuleKind.AMD} } ); + return { project, rootScriptInfo }; @@ -87,22 +89,22 @@ module ts { name: "c:/f1.ts", content: `foo()` }; - + let serverHost = createDefaultServerHost({ [root.name]: root, [imported.name]: imported }); let { project, rootScriptInfo } = createProject(root.name, serverHost); // ensure that imported file was found let diags = project.compilerService.languageService.getSemanticDiagnostics(imported.name); assert.equal(diags.length, 1); - + let originalFileExists = serverHost.fileExists; { // patch fileExists to make sure that disk is not touched serverHost.fileExists = (fileName): boolean => { - assert.isTrue(false, "fileExists should not be called"); + assert.isTrue(false, "fileExists should not be called"); return false; }; - + let newContent = `import {x} from "f1" var x: string = 1;`; rootScriptInfo.editContent(0, rootScriptInfo.content.length, newContent); @@ -123,7 +125,7 @@ module ts { }; let newContent = `import {x} from "f2"`; rootScriptInfo.editContent(0, rootScriptInfo.content.length, newContent); - + try { // trigger synchronization to make sure that LSHost will try to find 'f2' module on disk project.compilerService.languageService.getSemanticDiagnostics(imported.name); @@ -132,7 +134,7 @@ module ts { catch(e) { assert.isTrue(e.message.indexOf(`Could not find file: '${imported.name}'.`) === 0); } - + assert.isTrue(fileExistsIsCalled); } { @@ -140,45 +142,45 @@ module ts { serverHost.fileExists = (fileName): boolean => { if (fileName === "lib.d.ts") { return false; - } + } fileExistsCalled = true; assert.isTrue(fileName.indexOf('/f1.') !== -1); return originalFileExists(fileName); }; - + let newContent = `import {x} from "f1"`; rootScriptInfo.editContent(0, rootScriptInfo.content.length, newContent); project.compilerService.languageService.getSemanticDiagnostics(imported.name); assert.isTrue(fileExistsCalled); - + // setting compiler options discards module resolution cache fileExistsCalled = false; - + let opts = ts.clone(project.projectOptions); opts.compilerOptions = ts.clone(opts.compilerOptions); opts.compilerOptions.target = ts.ScriptTarget.ES5; project.setProjectOptions(opts); - + project.compilerService.languageService.getSemanticDiagnostics(imported.name); assert.isTrue(fileExistsCalled); } }); - + it("loads missing files from disk", () => { let root: File = { name: 'c:/foo.ts', content: `import {x} from "bar"` }; - + let imported: File = { name: 'c:/bar.d.ts', content: `export var y = 1` - }; - + }; + let fileMap: Map = { [root.name]: root }; let serverHost = createDefaultServerHost(fileMap); let originalFileExists = serverHost.fileExists; - + let fileExistsCalledForBar = false; serverHost.fileExists = fileName => { if (fileName === "lib.d.ts") { @@ -187,22 +189,22 @@ module ts { if (!fileExistsCalledForBar) { fileExistsCalledForBar = fileName.indexOf("/bar.") !== -1; } - + return originalFileExists(fileName); }; - + let { project, rootScriptInfo } = createProject(root.name, serverHost); let diags = project.compilerService.languageService.getSemanticDiagnostics(root.name); assert.isTrue(fileExistsCalledForBar, "'fileExists' should be called"); assert.isTrue(diags.length === 1, "one diagnostic expected"); assert.isTrue(typeof diags[0].messageText === "string" && ((diags[0].messageText).indexOf("Cannot find module") === 0), "should be 'cannot find module' message"); - + // assert that import will success once file appear on disk fileMap[imported.name] = imported; fileExistsCalledForBar = false; rootScriptInfo.editContent(0, rootScriptInfo.content.length, `import {y} from "bar"`) - + diags = project.compilerService.languageService.getSemanticDiagnostics(root.name); assert.isTrue(fileExistsCalledForBar, "'fileExists' should be called"); assert.isTrue(diags.length === 0); diff --git a/tests/cases/unittests/expandFiles.ts b/tests/cases/unittests/expandFiles.ts new file mode 100644 index 0000000000000..a9d12a7326057 --- /dev/null +++ b/tests/cases/unittests/expandFiles.ts @@ -0,0 +1,249 @@ +/// +/// + +describe("expandFiles", () => { + it("fail", () => { + assert.isTrue(false, "just checking"); + }); + + const basePath = "c:/dev/"; + const caseInsensitiveHost = createMockParseConfigHost( + basePath, + /*files*/ [ + "c:/dev/a.ts", + "c:/dev/a.d.ts", + "c:/dev/a.js", + "c:/dev/b.ts", + "c:/dev/b.js", + "c:/dev/c.d.ts", + "c:/dev/z/a.ts", + "c:/dev/z/abz.ts", + "c:/dev/z/aba.ts", + "c:/dev/z/b.ts", + "c:/dev/z/bbz.ts", + "c:/dev/z/bba.ts", + "c:/dev/x/a.ts", + "c:/dev/x/aa.ts", + "c:/dev/x/b.ts", + "c:/dev/x/y/a.ts", + "c:/dev/x/y/b.ts" + ], + /*ignoreCase*/ true); + + const caseSensitiveHost = createMockParseConfigHost( + basePath, + /*files*/ [ + "c:/dev/a.ts", + "c:/dev/a.d.ts", + "c:/dev/a.js", + "c:/dev/b.ts", + "c:/dev/b.js", + "c:/dev/A.ts", + "c:/dev/B.ts", + "c:/dev/c.d.ts", + "c:/dev/z/a.ts", + "c:/dev/z/abz.ts", + "c:/dev/z/aba.ts", + "c:/dev/z/b.ts", + "c:/dev/z/bbz.ts", + "c:/dev/z/bba.ts", + "c:/dev/x/a.ts", + "c:/dev/x/b.ts", + "c:/dev/x/y/a.ts", + "c:/dev/x/y/b.ts", + ], + /*ignoreCase*/ false); + + const expect = _chai.expect; + describe("with literal file list", () => { + it("without exclusions", () => { + const fileNames = ["a.ts", "b.ts"]; + const results = ts.expandFiles(fileNames, /*includeSpecs*/ undefined, /*excludeSpecs*/ undefined, basePath, {}, caseInsensitiveHost); + assert.deepEqual(results, ["c:/dev/a.ts", "c:/dev/b.ts"]); + }); + it("missing files are still present", () => { + const fileNames = ["z.ts", "x.ts"]; + const results = ts.expandFiles(fileNames, /*includeSpecs*/ undefined, /*excludeSpecs*/ undefined, basePath, {}, caseInsensitiveHost); + assert.deepEqual(results, ["c:/dev/z.ts", "c:/dev/x.ts"]); + }); + it("are not removed due to excludes", () => { + const fileNames = ["a.ts", "b.ts"]; + const excludeSpecs = ["b.ts"]; + const results = ts.expandFiles(fileNames, /*includeSpecs*/ undefined, excludeSpecs, basePath, {}, caseInsensitiveHost); + assert.deepEqual(results, ["c:/dev/a.ts", "c:/dev/b.ts"]); + }); + }); + + describe("with literal include list", () => { + it("without exclusions", () => { + const includeSpecs = ["a.ts", "b.ts"]; + const results = ts.expandFiles(/*fileNames*/ undefined, includeSpecs, /*excludeSpecs*/ undefined, basePath, {}, caseInsensitiveHost); + assert.deepEqual(results, ["c:/dev/a.ts", "c:/dev/b.ts"]); + }); + it("with non .ts file extensions are excluded", () => { + const includeSpecs = ["a.js", "b.js"]; + const results = ts.expandFiles(/*fileNames*/ undefined, includeSpecs, /*excludeSpecs*/ undefined, basePath, {}, caseInsensitiveHost); + assert.deepEqual(results, []); + }); + it("with missing files are excluded", () => { + const includeSpecs = ["z.ts", "x.ts"]; + const results = ts.expandFiles(/*fileNames*/ undefined, includeSpecs, /*excludeSpecs*/ undefined, basePath, {}, caseInsensitiveHost); + assert.deepEqual(results, []); + }); + it("with literal excludes", () => { + const includeSpecs = ["a.ts", "b.ts"]; + const excludeSpecs = ["b.ts"]; + const results = ts.expandFiles(/*fileNames*/ undefined, includeSpecs, excludeSpecs, basePath, {}, caseInsensitiveHost); + assert.deepEqual(results, ["c:/dev/a.ts"]); + }); + it("with wildcard excludes", () => { + const includeSpecs = ["a.ts", "b.ts", "z/a.ts", "z/abz.ts", "z/aba.ts", "x/b.ts"]; + const excludeSpecs = ["*.ts", "z/??z.ts", "*/b.ts"]; + const results = ts.expandFiles(/*fileNames*/ undefined, includeSpecs, excludeSpecs, basePath, {}, caseInsensitiveHost); + assert.deepEqual(results, ["c:/dev/z/a.ts", "c:/dev/z/aba.ts"]); + }); + it("with recursive excludes", () => { + const includeSpecs = ["a.ts", "b.ts", "x/a.ts", "x/b.ts", "x/y/a.ts", "x/y/b.ts"]; + const excludeSpecs = ["**/b.ts"]; + const results = ts.expandFiles(/*fileNames*/ undefined, includeSpecs, excludeSpecs, basePath, {}, caseInsensitiveHost); + assert.deepEqual(results, ["c:/dev/a.ts", "c:/dev/x/a.ts", "c:/dev/x/y/a.ts"]); + }); + it("with case sensitive exclude", () => { + const includeSpecs = ["B.ts"]; + const excludeSpecs = ["**/b.ts"]; + const results = ts.expandFiles(/*fileNames*/ undefined, includeSpecs, excludeSpecs, basePath, {}, caseSensitiveHost); + assert.deepEqual(results, ["c:/dev/B.ts"]); + }); + }); + + describe("with wildcard include list", () => { + it("same named declarations are excluded", () => { + const includeSpecs = ["*.ts"]; + const results = ts.expandFiles(/*fileNames*/ undefined, includeSpecs, /*excludeSpecs*/ undefined, basePath, {}, caseInsensitiveHost); + assert.deepEqual(results, ["c:/dev/a.ts", "c:/dev/b.ts", "c:/dev/c.d.ts"]); + }); + it("`*` matches only ts files", () => { + const includeSpecs = ["*"]; + const results = ts.expandFiles(/*fileNames*/ undefined, includeSpecs, /*excludeSpecs*/ undefined, basePath, {}, caseInsensitiveHost); + assert.deepEqual(results, ["c:/dev/a.ts", "c:/dev/b.ts", "c:/dev/c.d.ts"]); + }); + it("`?` matches only a single character", () => { + const includeSpecs = ["x/?.ts"]; + const results = ts.expandFiles(/*fileNames*/ undefined, includeSpecs, /*excludeSpecs*/ undefined, basePath, {}, caseInsensitiveHost); + assert.deepEqual(results, ["c:/dev/x/a.ts", "c:/dev/x/b.ts"]); + }); + it("with recursive directory", () => { + const includeSpecs = ["**/a.ts"]; + const results = ts.expandFiles(/*fileNames*/ undefined, includeSpecs, /*excludeSpecs*/ undefined, basePath, {}, caseInsensitiveHost); + assert.deepEqual(results, ["c:/dev/a.ts", "c:/dev/x/a.ts", "c:/dev/x/y/a.ts", "c:/dev/z/a.ts"]); + }); + it("case sensitive", () => { + const includeSpecs = ["**/A.ts"]; + const results = ts.expandFiles(/*fileNames*/ undefined, includeSpecs, /*excludeSpecs*/ undefined, basePath, {}, caseSensitiveHost); + assert.deepEqual(results, ["c:/dev/A.ts"]); + }); + it("with missing files are excluded", () => { + const includeSpecs = ["*/z.ts"]; + const results = ts.expandFiles(/*fileNames*/ undefined, includeSpecs, /*excludeSpecs*/ undefined, basePath, {}, caseInsensitiveHost); + assert.deepEqual(results, []); + }); + it("always include literal files", () => { + const fileNames = ["a.ts"]; + const includeSpecs = ["*/z.ts"]; + const excludeSpecs = ["**/a.ts"]; + const results = ts.expandFiles(fileNames, includeSpecs, excludeSpecs, basePath, {}, caseInsensitiveHost); + assert.deepEqual(results, ["c:/dev/a.ts"]); + }); + }); + + function createMockParseConfigHost(basePath: string, files: string[], ignoreCase: boolean): ts.ParseConfigHost { + const fileSet: ts.Map = {}; + const directorySet: ts.Map<{ files: ts.Map; directories: ts.Map; }> = {}; + + files.sort((a, b) => ts.comparePaths(a, b, basePath, ignoreCase)); + for (const file of files) { + addFile(ts.getNormalizedAbsolutePath(file, basePath)); + } + + return { + useCaseSensitiveFileNames: !ignoreCase, + fileExists, + readDirectory, + readFileNames, + readDirectoryNames + }; + + function fileExists(path: string): boolean { + const fileKey = ignoreCase ? path.toLowerCase() : path; + return ts.hasProperty(fileSet, fileKey); + } + + function directoryExists(path: string): boolean { + const directoryKey = ignoreCase ? path.toLowerCase() : path; + return ts.hasProperty(directorySet, directoryKey); + } + + function readDirectory(rootDir: string, extension?: string, exclude?: string[]): string[] { + throw new Error("Not implemented"); + } + + function readFileNames(path: string) { + const files = getDirectoryEntry(path).files; + const result: string[] = []; + ts.forEachKey(files, key => { result.push(key); }); + result.sort((a, b) => ts.compareStrings(a, b, ignoreCase)); + return result; + } + + function readDirectoryNames(path: string) { + const directories = getDirectoryEntry(path).directories; + const result: string[] = []; + ts.forEachKey(directories, key => { result.push(key); }); + result.sort((a, b) => ts.compareStrings(a, b, ignoreCase)); + return result; + } + + function getDirectoryEntry(path: string) { + path = ts.getNormalizedAbsolutePath(path, basePath); + path = ts.removeTrailingDirectorySeparator(path); + const directoryKey = ignoreCase ? path.toLowerCase() : path; + return ts.getProperty(directorySet, directoryKey); + } + + function addFile(file: string) { + const fileKey = ignoreCase ? file.toLowerCase() : file; + if (!ts.hasProperty(fileSet, fileKey)) { + fileSet[fileKey] = file; + const name = ts.getBaseFileName(file); + const parent = ts.getDirectoryPath(file); + addToDirectory(parent, name, "file"); + } + } + + function addDirectory(directory: string) { + directory = ts.removeTrailingDirectorySeparator(directory); + const directoryKey = ignoreCase ? directory.toLowerCase() : directory; + if (!ts.hasProperty(directorySet, directoryKey)) { + directorySet[directoryKey] = { files: {}, directories: {} }; + const name = ts.getBaseFileName(directory); + const parent = ts.getDirectoryPath(directory); + if (parent !== directory) { + addToDirectory(parent, name, "directory"); + } + } + } + + function addToDirectory(directory: string, entry: string, type: "file" | "directory") { + addDirectory(directory); + directory = ts.removeTrailingDirectorySeparator(directory); + const directoryKey = ignoreCase ? directory.toLowerCase() : directory; + const entryKey = ignoreCase ? entry.toLowerCase() : entry; + const directoryEntry = directorySet[directoryKey]; + const entries = type === "file" ? directoryEntry.files : directoryEntry.directories; + if (!ts.hasProperty(entries, entryKey)) { + entries[entryKey] = entry; + } + } + } +}); + diff --git a/tests/cases/unittests/session.ts b/tests/cases/unittests/session.ts index c1dfd508942fd..8e35baa391022 100644 --- a/tests/cases/unittests/session.ts +++ b/tests/cases/unittests/session.ts @@ -16,6 +16,8 @@ namespace ts.server { getExecutingFilePath(): string { return void 0; }, getCurrentDirectory(): string { return void 0; }, readDirectory(): string[] { return []; }, + readDirectoryNames(): string[] { return []; }, + readFileNames(): string[] { return []; }, exit(): void {} }; const mockLogger: Logger = { @@ -28,7 +30,7 @@ namespace ts.server { endGroup(): void {}, msg(s: string, type?: string): void {}, }; - + describe("the Session class", () => { let session: Session; let lastSent: protocol.Message; @@ -204,7 +206,7 @@ namespace ts.server { .to.throw(`Protocol handler already exists for command "${command}"`); }); }); - + describe("event", () => { it("can format event responses and send them", () => { const evt = "notify-test"; @@ -315,7 +317,7 @@ namespace ts.server { responseRequired: true })); } - + send(msg: protocol.Message) { this.client.handle(msg); } @@ -323,7 +325,7 @@ namespace ts.server { enqueue(msg: protocol.Request) { this.queue.unshift(msg); } - + handleRequest(msg: protocol.Request) { let response: protocol.Response; try { @@ -345,7 +347,7 @@ namespace ts.server { } } } - + class InProcClient { private server: InProcSession; private seq = 0; @@ -379,7 +381,7 @@ namespace ts.server { connect(session: InProcSession): void { this.server = session; } - + execute(command: string, args: any, callback: (resp: protocol.Response) => void): void { if (!this.server) { return; @@ -394,7 +396,7 @@ namespace ts.server { this.callbacks[this.seq] = callback; } }; - + it("can be constructed and respond to commands", (done) => { const cli = new InProcClient(); const session = new InProcSession(cli); @@ -402,23 +404,23 @@ namespace ts.server { data: true }; const toEvent = { - data: false + data: false }; let responses = 0; // Connect the client cli.connect(session); - + // Add an event handler cli.on("testevent", (eventinfo) => { expect(eventinfo).to.equal(toEvent); responses++; expect(responses).to.equal(1); }); - + // Trigger said event from the server session.event(toEvent, "testevent"); - + // Queue an echo command cli.execute("echo", toEcho, (resp) => { assert(resp.success, resp.message); @@ -426,7 +428,7 @@ namespace ts.server { expect(responses).to.equal(2); expect(resp.body).to.deep.equal(toEcho); }); - + // Queue a configure command cli.execute("configure", { hostInfo: "unit test", @@ -436,10 +438,10 @@ namespace ts.server { }, (resp) => { assert(resp.success, resp.message); responses++; - expect(responses).to.equal(3); + expect(responses).to.equal(3); done(); }); - + // Consume the queue and trigger the callbacks session.consumeQueue(); }); From 30575dbd7cc4ac62c044e6d1eef77de3affd11ce Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Mon, 7 Dec 2015 14:58:13 -0800 Subject: [PATCH 02/84] Added caching, more tests --- Jakefile.js | 3 +- src/compiler/commandLineParser.ts | 474 ++++++++++++++++------ src/compiler/core.ts | 88 ++++- src/compiler/diagnosticMessages.json | 4 + src/compiler/types.ts | 20 +- src/harness/external/chai.d.ts | 6 +- src/harness/harnessLanguageService.ts | 1 + src/harness/projectsRunner.ts | 5 + src/harness/rwcRunner.ts | 1 + src/server/editorServices.ts | 57 ++- src/services/shims.ts | 13 + tests/cases/unittests/expandFiles.ts | 540 ++++++++++++++++++-------- 12 files changed, 924 insertions(+), 288 deletions(-) diff --git a/Jakefile.js b/Jakefile.js index 6243ce8ff97ea..a3f1d31f26896 100644 --- a/Jakefile.js +++ b/Jakefile.js @@ -160,7 +160,8 @@ var harnessSources = harnessCoreSources.concat([ "reuseProgramStructure.ts", "cachingInServerLSHost.ts", "moduleResolution.ts", - "tsconfigParsing.ts" + "tsconfigParsing.ts", + "expandFiles.ts" ].map(function (f) { return path.join(unittestsDirectory, f); })).concat([ diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index f850ac00dba68..6499ec6327fc4 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -488,13 +488,15 @@ namespace ts { const { options: optionsFromJsonConfigFile, errors } = convertCompilerOptionsFromJson(json["compilerOptions"], basePath); const options = extend(existingOptions, optionsFromJsonConfigFile); + const { fileNames, wildcardDirectories } = getFileNames(); return { options, - fileNames: getFileNames(), - errors + fileNames, + errors, + wildcardDirectories }; - function getFileNames(): string[] { + function getFileNames(): ExpandResult { let fileNames: string[]; if (hasProperty(json, "files")) { if (isArray(json["files"])) { @@ -526,17 +528,7 @@ namespace ts { } if (fileNames === undefined && includeSpecs === undefined) { - includeSpecs = ["**/*.ts"]; - if (options.jsx) { - includeSpecs.push("**/*.tsx"); - } - - if (options.allowJs) { - includeSpecs.push("**/*.js"); - if (options.jsx) { - includeSpecs.push("**/*.jsx"); - } - } + includeSpecs = ["**/*"]; } return expandFiles(fileNames, includeSpecs, excludeSpecs, basePath, options, host, errors); @@ -593,11 +585,45 @@ namespace ts { // Simplified whitelist, forces escaping of any non-word (or digit), non-whitespace character. const reservedCharacterPattern = /[^\w\s]/g; - const enum ExpandResult { + const enum ExpansionState { Ok, Error } + interface ExpansionContext { + /** A pattern used to exclude a file specification. */ + excludePattern: RegExp; + /** Compiler options. */ + options: CompilerOptions; + /** The host used to resolve files and directories. */ + host: ParseConfigHost; + /** Errors to report. */ + errors: Diagnostic[]; + /** The set of literal files. */ + literalFiles: FileMap; + /** The set of files matching a wildcard. */ + wildcardFiles: FileMap; + /** Directories to be watched. */ + wildcardDirectories: FileMap; + /** Supported extensions. */ + supportedExtensions: string[]; + /** + * Path cache, used to reduce calls to the file system. `true` indicates a file exists, + * `false` indicates a file or directory does not exist. A DirectoryResult + * indicates the file and subdirectory names in a directory. */ + cache: FileMap; + } + + const enum FileSystemEntryKind { + File, + Directory + } + + interface DirectoryResult { + files?: string[]; + directories?: string[]; + } + /** * Expands an array of file specifications. * @@ -609,62 +635,118 @@ namespace ts { * @param host The host used to resolve files and directories. * @param errors An array for diagnostic reporting. */ - export function expandFiles(fileNames: string[], includeSpecs: string[], excludeSpecs: string[], basePath: string, options: CompilerOptions, host: ParseConfigHost, errors?: Diagnostic[]): string[] { + export function expandFiles(fileNames: string[], includeSpecs: string[], excludeSpecs: string[], basePath: string, options: CompilerOptions, host: ParseConfigHost, errors?: Diagnostic[]): ExpandResult { basePath = normalizePath(basePath); basePath = removeTrailingDirectorySeparator(basePath); + // The exclude spec list is converted into a regular expression, which allows us to quickly + // test whether a file or directory should be excluded before recursively traversing the + // file system. const excludePattern = includeSpecs ? createExcludeRegularExpression(excludeSpecs, basePath, options, host, errors) : undefined; - const fileSet = createFileMap(host.useCaseSensitiveFileNames ? caseSensitiveKeyMapper : caseInsensitiveKeyMapper); + const keyMapper = host.useCaseSensitiveFileNames ? caseSensitiveKeyMapper : caseInsensitiveKeyMapper; + + // Literal file names (provided via the "files" array in tsconfig.json) are stored in a + // file map with a possibly invariant key. We use this map later when when including + // wildcard paths. + const literalFiles = createFileMap(keyMapper); + + // Wildcard paths (provided via the "includes" array in tscofnig.json) are stored in a + // file map with a possibly invariant key. We use this map to store paths matched + // via wildcard, and to handle extension priority. + const wildcardFiles = createFileMap(keyMapper); + + // Wildcard directories (provided as part of a wildcard path) are stored in a + // file map that marks whether it was a regular wildcard match (with a `*` or `?` token), + // or a recursive directory. This information is used by filesystem watchers to monitor for + // new entries in these paths. + const wildcardDirectories = createFileMap(keyMapper); + + // To reduce the overhead of disk I/O (and marshalling to managed code when hosted in + // Visual Studio), file system queries are cached during the expansion session. + // If present, a cache entry can be one of three values: + // - A `false` value indicates the file or directory did not exist. + // - A `true` value indicates the path is a file and it exists. + // - An object value indicates the path is a directory and exists. The object may have + // zero, one, or both of the following properties: + // - A "files" array, which contains the file names in the directory. + // - A "directories" array, which contains the subdirectory names in the directory. + const cache = createFileMap(keyMapper); + + // Rather than requery this for each file and filespec, we querythe supported extensions + // once and store it on the expansion context. + const supportedExtensions = getSupportedExtensions(options); + + // The expansion context holds references to shared information for the various expansion + // operations to reduce the overhead of closures. + const context: ExpansionContext = { + options, + host, + errors, + excludePattern, + literalFiles, + wildcardFiles, + wildcardDirectories, + supportedExtensions, + cache + }; - // include every literal file. + // Literal files are always included verbatim. An "include" or "exclude" specification cannot + // remove a literal file. if (fileNames) { for (const fileName of fileNames) { const path = toPath(fileName, basePath, caseSensitiveKeyMapper); - if (!fileSet.contains(path)) { - fileSet.set(path, path); + if (!literalFiles.contains(path)) { + literalFiles.set(path, path); } } } - // expand and include the provided files into the file set. + // Each "include" specification is expanded and matching files are added. if (includeSpecs) { for (let includeSpec of includeSpecs) { includeSpec = normalizePath(includeSpec); includeSpec = removeTrailingDirectorySeparator(includeSpec); - expandFileSpec(basePath, includeSpec, 0, excludePattern, options, host, errors, fileSet); + expandFileSpec(includeSpec, basePath, 0, context); } } - const output = fileSet.reduce(addFileToOutput, []); - return output; + return { + fileNames: wildcardFiles.reduce(addFileToOutput, literalFiles.reduce(addFileToOutput, [])), + wildcardDirectories: wildcardDirectories.reduce>(addDirectoryToOutput, {}), + }; } /** * Expands a file specification with wildcards. * - * @param basePath The directory to expand. * @param fileSpec The original file specification. + * @param basePath The directory to expand. This path must exist. * @param start The starting offset in the file specification. - * @param excludePattern A pattern used to exclude a file specification. - * @param options Compiler options. - * @param host The host used to resolve files and directories. - * @param errors An array for diagnostic reporting. - * @param fileSet The set of matching files. + * @param context The expansion context. * @param isExpandingRecursiveDirectory A value indicating whether the file specification includes a recursive directory wildcard prior to the start of this segment. */ - function expandFileSpec(basePath: string, fileSpec: string, start: number, excludePattern: RegExp, options: CompilerOptions, host: ParseConfigHost, errors: Diagnostic[], fileSet: FileMap, isExpandingRecursiveDirectory?: boolean): ExpandResult { + function expandFileSpec(fileSpec: string, basePath: Path, start: number, context: ExpansionContext, isExpandingRecursiveDirectory?: boolean): ExpansionState { + // A file specification must always point to a file. As a result, we always assume the + // path segment following the last directory separator points to a file. The only + // exception is when the final path segment is the recursive directory pattern "**", in + // which case we report an error. + const { host, options, errors, wildcardFiles, wildcardDirectories, excludePattern, cache, supportedExtensions } = context; + // Skip expansion if the base path matches an exclude pattern. - if (isExcludedPath(excludePattern, basePath)) { - return ExpandResult.Ok; + if (isExcludedPath(basePath, excludePattern)) { + return ExpansionState.Ok; } - // Find the offset of the next wildcard in the file specification + // Find the offset of the next wildcard in the file specification. If there are no more + // wildcards, we can include the file if it exists and isn't excluded. let offset = indexOfWildcard(fileSpec, start); if (offset < 0) { - // There were no more wildcards, so include the file. const path = toPath(fileSpec.substring(start), basePath, caseSensitiveKeyMapper); - includeFile(path, excludePattern, options, host, fileSet); - return ExpandResult.Ok; + if (!isExcludedPath(path, excludePattern) && pathExists(path, FileSystemEntryKind.File, context)) { + includeFile(path, context, /*wildcardHasExtension*/ true); + } + + return ExpansionState.Ok; } // Find the last directory separator before the wildcard to get the leading path. @@ -672,11 +754,11 @@ namespace ts { if (offset > start) { // The wildcard occurs in a later segment, include remaining path up to // wildcard in prefix. - basePath = combinePaths(basePath, fileSpec.substring(start, offset)); + basePath = toPath(fileSpec.substring(start, offset), basePath, caseSensitiveKeyMapper); // Skip this wildcard path if the base path now matches an exclude pattern. - if (isExcludedPath(excludePattern, basePath)) { - return ExpandResult.Ok; + if (isExcludedPath(basePath, excludePattern) || !pathExists(basePath, FileSystemEntryKind.Directory, context)) { + return ExpansionState.Ok; } start = offset + 1; @@ -687,23 +769,34 @@ namespace ts { // Check if the current offset is the beginning of a recursive directory pattern. if (isRecursiveDirectoryWildcard(fileSpec, start, offset)) { - if (offset >= fileSpec.length) { - // If there is no file specification following the recursive directory pattern - // we cannot match any files, so we will ignore this pattern. - return ExpandResult.Ok; - } - // Stop expansion if a file specification contains more than one recursive directory pattern. if (isExpandingRecursiveDirectory) { if (errors) { errors.push(createCompilerDiagnostic(Diagnostics.File_specification_cannot_contain_multiple_recursive_directory_wildcards_Asterisk_Asterisk_Colon_0, fileSpec)); } - return ExpandResult.Error; + return ExpansionState.Error; + } + + if (offset >= fileSpec.length) { + // If there is no file specification following the recursive directory pattern + // then we report an error as we cannot match any files. + if (errors) { + errors.push(createCompilerDiagnostic(Diagnostics.File_specification_cannot_end_in_a_recursive_directory_wildcard_Asterisk_Asterisk_Colon_0, fileSpec)); + } + + return ExpansionState.Error; } + // Keep track of the recursive wildcard directory + wildcardDirectories.set(basePath, WatchDirectoryFlags.Recursive); + // Expand the recursive directory pattern. - return expandRecursiveDirectory(basePath, fileSpec, offset + 1, excludePattern, options, host, errors, fileSet); + return expandRecursiveDirectory(fileSpec, basePath, offset + 1, context); + } + + if (!isExpandingRecursiveDirectory) { + wildcardDirectories.set(basePath, WatchDirectoryFlags.None); } // Match the entries in the directory against the wildcard pattern. @@ -712,103 +805,224 @@ namespace ts { // If there are no more directory separators (the offset is at the end of the file specification), then // this must be a file. if (offset >= fileSpec.length) { - const files = host.readFileNames(basePath); - for (const extension of getSupportedExtensions(options)) { - for (const file of files) { - if (fileExtensionIs(file, extension)) { - const path = toPath(file, basePath, caseSensitiveKeyMapper); - - // .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(file, ".d.ts")) { - continue; - } + const wildcardHasExtension = fileSegmentHasExtension(fileSpec, start); + const fileNames = readDirectory(basePath, FileSystemEntryKind.File, context); + for (const fileName of fileNames) { + // Skip the file if it doesn't match the pattern. + if (!pattern.test(fileName)) { + continue; + } - // If this is one of the output extension (which would be .d.ts and .js if we are allowing compilation of js files) - // do not include this file if we included .ts or .tsx file with same base name as it could be output of the earlier compilation - if (extension === ".d.ts" || (options.allowJs && contains(supportedJavascriptExtensions, extension))) { - if (fileSet.contains(changeExtension(path, ".ts")) || fileSet.contains(changeExtension(path, ".tsx"))) { - continue; - } - } + const path = toPath(fileName, basePath, caseSensitiveKeyMapper); - // This wildcard has no further directory to process, so include the file. - includeFile(path, excludePattern, options, host, fileSet); - } + // If we have excluded this path, we should skip the file. + if (isExcludedPath(path, excludePattern)) { + continue; } + + // This wildcard has no further directory to process, so include the file. + includeFile(path, context, wildcardHasExtension); } } else { - const directories = host.readDirectoryNames(basePath); - for (const directory of directories) { - // If this was a directory, process the directory. - const path = toPath(directory, basePath, caseSensitiveKeyMapper); - if (expandFileSpec(path, fileSpec, offset + 1, excludePattern, options, host, errors, fileSet, isExpandingRecursiveDirectory) === ExpandResult.Error) { - return ExpandResult.Error; + const directoryNames = readDirectory(basePath, FileSystemEntryKind.Directory, context); + for (const directoryName of directoryNames) { + if (pattern.test(directoryName)) { + const newBasePath = toPath(directoryName, basePath, caseSensitiveKeyMapper); + + // Expand the entries in this directory. + if (expandFileSpec(fileSpec, newBasePath, offset + 1, context, isExpandingRecursiveDirectory) === ExpansionState.Error) { + return ExpansionState.Error; + } } } } - return ExpandResult.Ok; + return ExpansionState.Ok; } /** * Expands a `**` recursive directory wildcard. * - * @param basePath The directory to recursively expand. * @param fileSpec The original file specification. + * @param basePath The directory to recursively expand. * @param start The starting offset in the file specification. - * @param excludePattern A pattern used to exclude a file specification. - * @param options Compiler options. - * @param host The host used to resolve files and directories. - * @param errors An array for diagnostic reporting. - * @param fileSet The set of matching files. + * @param context The expansion context. */ - function expandRecursiveDirectory(basePath: string, fileSpec: string, start: number, excludePattern: RegExp, options: CompilerOptions, host: ParseConfigHost, errors: Diagnostic[], fileSet: FileMap): ExpandResult { - // expand the non-recursive part of the file specification against the prefix path. - if (expandFileSpec(basePath, fileSpec, start, excludePattern, options, host, errors, fileSet, /*isExpandingRecursiveDirectory*/ true) === ExpandResult.Error) { - return ExpandResult.Error; + function expandRecursiveDirectory(fileSpec: string, basePath: Path, start: number, context: ExpansionContext): ExpansionState { + // Skip the directory if it is excluded. + if (isExcludedPath(basePath, context.excludePattern)) { + return ExpansionState.Ok; + } + + // Expand the non-recursive part of the file specification against the prefix path. + if (expandFileSpec(fileSpec, basePath, start, context, /*isExpandingRecursiveDirectory*/ true) === ExpansionState.Error) { + return ExpansionState.Error; } // Recursively expand each subdirectory. - const directories = host.readDirectoryNames(basePath); - for (const entry of directories) { - const path = combinePaths(basePath, entry); - if (expandRecursiveDirectory(path, fileSpec, start, excludePattern, options, host, errors, fileSet) === ExpandResult.Error) { - return ExpandResult.Error; + const directoryNames = readDirectory(basePath, FileSystemEntryKind.Directory, context); + for (const directoryName of directoryNames) { + const newBasePath = toPath(directoryName, basePath, caseSensitiveKeyMapper); + if (expandRecursiveDirectory(fileSpec, newBasePath, start, context) === ExpansionState.Error) { + return ExpansionState.Error; } } - return ExpandResult.Ok; + return ExpansionState.Ok; } /** * Attempts to include a file in a file set. * * @param file The file to include. - * @param excludePattern A pattern used to exclude a file specification. - * @param options Compiler options. - * @param host The host used to resolve files and directories. - * @param fileSet The set of matching files. + * @param context The expansion context. + * @param wildcardHasExtension A value indicating whether the wildcard supplied an explicit extension. */ - function includeFile(file: Path, excludePattern: RegExp, options: CompilerOptions, host: ParseConfigHost, fileSet: FileMap): void { - // Ignore the file if it should be excluded. - if (isExcludedPath(excludePattern, file)) { + function includeFile(file: Path, context: ExpansionContext, wildcardHasExtension: boolean): void { + const { options, literalFiles, wildcardFiles, excludePattern, supportedExtensions } = context; + + // Ignore the file if it does not have a supported extension. + if ((!wildcardHasExtension || !options.allowNonTsExtensions) && !isSupportedSourceFileName(file, options)) { return; } - // Ignore the file if it doesn't exist. - if (!host.fileExists(file)) { + // If we have already included a literal or wildcard path with a + // higher priority extension, we should skip this file. + // + // This handles cases where we may encounter both .ts and + // .d.ts (or .js if "allowJs" is enabled) in the same + // directory when they are compilation outputs. + const extensionPriority = getExtensionPriority(file, supportedExtensions); + if (hasFileWithHigherPriorityExtension(file, extensionPriority, context)) { return; } - // Ignore the file if it does not have a supported extension. - if (!options.allowNonTsExtensions && !isSupportedSourceFileName(file, options)) { - return; + // We may have included a wildcard path with a lower priority + // extension due to the user-defined order of entries in the + // "include" array. If there is a lower priority extension in the + // same directory, we should remove it. + removeWildcardFilesWithLowerPriorityExtension(file, extensionPriority, context); + + if (!literalFiles.contains(file) && !wildcardFiles.contains(file)) { + wildcardFiles.set(file, file); } + } + + /** + * Tests whether a path exists and is a specific kind of item. Results are + * cached for performance. + * + * @param path The path to tests. + * @param kind The kind of file system entry to find. + * @param context The expansion context. + */ + function pathExists(path: Path, kind: FileSystemEntryKind, context: ExpansionContext) { + const { cache, host } = context; + const entry = cache.get(path); + if (entry === false) { + // If the entry is strictly `false` then the path doesn`t exist, regardless of its kind. + return false; + } + else if (entry === true) { + // If the entry is strictly `true` then a file exists at this path. + return kind === FileSystemEntryKind.File; + } + else if (typeof entry === "object") { + // If the entry is an object, then a directory exists at this path. + return kind === FileSystemEntryKind.Directory; + } + else { + // The entry does not exist in the cache, so we need to check the host. + if (kind === FileSystemEntryKind.File) { + const result = host.fileExists(path); + cache.set(path, result); + return result; + } + else if (kind === FileSystemEntryKind.Directory) { + const result = host.directoryExists(path); + cache.set(path, result ? {} : false); + return result; + } + } + + return false; + } + + /** + * Reads the contents of a directory for a specific kind of item. Results are + * cached for performance. + * + * @param basePath The path to the directory. The path must already exist. + * @param kind The kind of file system entry to find. + * @param context The expansion context. + */ + function readDirectory(basePath: Path, kind: FileSystemEntryKind, context: ExpansionContext) { + const { cache, host } = context; + + let entry = cache.get(basePath); + if (entry === undefined) { + entry = {}; + cache.set(basePath, entry); + } + + if (typeof entry === "object") { + if (kind === FileSystemEntryKind.File) { + if (entry.files === undefined) { + entry.files = host.readFileNames(basePath); + } + + return entry.files; + } + else if (kind === FileSystemEntryKind.Directory) { + if (entry.directories === undefined) { + entry.directories = host.readDirectoryNames(basePath); + } + + return entry.directories; + } + } + + return []; + } - if (!fileSet.contains(file)) { - fileSet.set(file, file); + /** + * Determines whether a literal or wildcard file has already been included that has a higher + * extension priority. + * + * @param file The path to the file. + * @param extensionPriority The priority of the extension. + * @param context The expansion context. + */ + function hasFileWithHigherPriorityExtension(file: Path, extensionPriority: ExtensionPriority, context: ExpansionContext) { + const { literalFiles, wildcardFiles, supportedExtensions } = context; + const adjustedExtensionPriority = adjustExtensionPriority(extensionPriority); + for (let i = ExtensionPriority.Highest; i < adjustedExtensionPriority; ++i) { + const higherPriorityExtension = supportedExtensions[i]; + const higherPriorityPath = changeExtension(file, higherPriorityExtension); + if (literalFiles.contains(higherPriorityPath) || wildcardFiles.contains(higherPriorityPath)) { + return true; + } + } + + return false; + } + + /** + * Removes files included via wildcard expansion with a lower extension priority that have + * already been included. + * + * @param file The path to the file. + * @param extensionPriority The priority of the extension. + * @param context The expansion context. + */ + function removeWildcardFilesWithLowerPriorityExtension(file: Path, extensionPriority: ExtensionPriority, context: ExpansionContext) { + const { wildcardFiles, supportedExtensions } = context; + const nextExtensionPriority = getNextLowestExtensionPriority(extensionPriority); + for (let i = nextExtensionPriority; i < supportedExtensions.length; ++i) { + const lowerPriorityExtension = supportedExtensions[i]; + const lowerPriorityPath = changeExtension(file, lowerPriorityExtension); + wildcardFiles.remove(lowerPriorityPath); } } @@ -823,16 +1037,48 @@ namespace ts { return output; } + /** + * Adds a watched directory to an output map. + * + * @param output The output map. + * @param flags The directory flags. + * @param directory The directory path. + */ + function addDirectoryToOutput(output: Map, flags: WatchDirectoryFlags, directory: string) { + output[directory] = flags; + return output; + } + /** * Determines whether a path should be excluded. * - * @param excludePattern A pattern used to exclude a file specification. * @param path The path to test for exclusion. + * @param excludePattern A pattern used to exclude a file specification. */ - function isExcludedPath(excludePattern: RegExp, path: string) { + function isExcludedPath(path: string, excludePattern: RegExp) { return excludePattern ? excludePattern.test(path) : false; } + /** + * Determines whether a file segment contains a valid extension. + * + * @param fileSpec The file specification. + * @param segmentStart The offset to the start of the file segment in the specification. + */ + function fileSegmentHasExtension(fileSpec: string, segmentStart: number) { + // if the final path segment does not have a . token, the file does not have an extension. + if (fileSpec.indexOf(".", segmentStart) === -1) { + return false; + } + + // if the extension for the final path segment is (".*"), then the file does not have an extension. + if (fileExtensionIs(fileSpec, ".*")) { + return false; + } + + return true; + } + /** * Creates a regular expression from a glob-style wildcard. * @@ -858,17 +1104,17 @@ namespace ts { let offset = indexOfWildcard(fileSpec, start); while (offset >= 0 && offset < end) { if (offset > start) { - // Escape and append the non-wildcard portion to the regular expression + // Escape and append the non-wildcard portion to the regular expression. pattern += escapeRegularExpressionText(fileSpec, start, offset); } const charCode = fileSpec.charCodeAt(offset); if (charCode === CharacterCodes.asterisk) { - // Append a multi-character (zero or more characters) pattern to the regular expression - pattern += "[^/]*"; + // Append a multi-character (zero or more characters) pattern to the regular expression. + pattern += "[^/]*?"; } else if (charCode === CharacterCodes.question) { - // Append a single-character (zero or one character) pattern to the regular expression + // Append a single-character pattern to the regular expression. pattern += "[^/]"; } @@ -1054,8 +1300,8 @@ namespace ts { * @param fileSpec The file specification. * @param start The starting offset in the file specification. */ - function indexOfWildcard(fileSpec: string, start: number): number { - for (let i = start; i < fileSpec.length; ++i) { + function indexOfWildcard(fileSpec: string, start: number, end: number = fileSpec.length): number { + for (let i = start; i < end; ++i) { const ch = fileSpec.charCodeAt(i); if (ch === CharacterCodes.asterisk || ch === CharacterCodes.question) { return i; diff --git a/src/compiler/core.ts b/src/compiler/core.ts index aae4b54b5c7e6..998766e8d9deb 100644 --- a/src/compiler/core.ts +++ b/src/compiler/core.ts @@ -26,7 +26,8 @@ namespace ts { remove, forEachValue: forEachValueInMap, reduce, - clear + clear, + mergeFrom }; function forEachValueInMap(f: (key: Path, value: T) => void) { @@ -61,6 +62,16 @@ namespace ts { files = {}; } + function mergeFrom(other: FileMap) { + other.forEachValue(mergeFromOther); + } + + function mergeFromOther(key: Path, value: T) { + if (!contains(key)) { + set(key, value); + } + } + function toKey(path: Path): string { return keyMapper ? keyMapper(path) : path; } @@ -822,6 +833,28 @@ namespace ts { return compareValues(aComponents.length, bComponents.length); } + export function containsPath(parent: string, child: string, currentDirectory: string, ignoreCase?: boolean) { + if (parent === undefined || child === undefined) return false; + if (parent === child) return true; + parent = removeTrailingDirectorySeparator(parent); + child = removeTrailingDirectorySeparator(child); + if (parent === child) return true; + const parentComponents = getNormalizedPathComponents(parent, currentDirectory); + const childComponents = getNormalizedPathComponents(child, currentDirectory); + if (childComponents.length < parentComponents.length) { + return false; + } + + for (let i = 0; i < parentComponents.length; ++i) { + const result = compareStrings(parentComponents[i], childComponents[i], ignoreCase); + if (result !== Comparison.EqualTo) { + return false; + } + } + + return true; + } + export function fileExtensionIs(path: string, extension: string): boolean { const pathLen = path.length; const extLen = extension.length; @@ -850,6 +883,59 @@ namespace ts { return false; } + /** + * Extension boundaries by priority. Lower numbers indicate higher priorities, and are + * aligned to the offset of the highest priority extension in the + * allSupportedExtensions array. + */ + export const enum ExtensionPriority { + TypeScriptFiles = 0, + DeclarationAndJavaScriptFiles = 2, + Limit = 5, + + Highest = TypeScriptFiles, + Lowest = DeclarationAndJavaScriptFiles, + } + + export function getExtensionPriority(path: string, supportedExtensions: string[]): ExtensionPriority { + for (let i = supportedExtensions.length - 1; i >= 0; i--) { + if (fileExtensionIs(path, supportedExtensions[i])) { + return adjustExtensionPriority(i); + } + } + + // If its not in the list of supported extensions, this is likely a + // TypeScript file with a non-ts extension + return ExtensionPriority.Highest; + } + + /** + * Adjusts an extension priority to be the highest priority within the same range. + */ + export function adjustExtensionPriority(extensionPriority: ExtensionPriority): ExtensionPriority { + if (extensionPriority < ExtensionPriority.DeclarationAndJavaScriptFiles) { + return ExtensionPriority.TypeScriptFiles; + } + else if (extensionPriority < ExtensionPriority.Limit) { + return ExtensionPriority.DeclarationAndJavaScriptFiles; + } + else { + return ExtensionPriority.Limit; + } + } + + /** + * Gets the next lowest extension priority for a given priority. + */ + export function getNextLowestExtensionPriority(extensionPriority: ExtensionPriority): ExtensionPriority { + if (extensionPriority < ExtensionPriority.DeclarationAndJavaScriptFiles) { + return ExtensionPriority.DeclarationAndJavaScriptFiles; + } + else { + return ExtensionPriority.Limit; + } + } + const extensionsToRemove = [".d.ts", ".ts", ".js", ".tsx", ".jsx"]; export function removeFileExtension(path: string): string { for (const ext of extensionsToRemove) { diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 6a1d0e5ce650f..c607bace4efec 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -2020,6 +2020,10 @@ "category": "Error", "code": 5009 }, + "File specification cannot end in a recursive directory wildcard ('**'): '{0}'.": { + "category": "Error", + "code": 5010 + }, "File specification cannot contain multiple recursive directory wildcards ('**'): '{0}'.": { "category": "Error", "code": 5011 diff --git a/src/compiler/types.ts b/src/compiler/types.ts index ffd9ddc206d1d..3d16f70d04995 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -15,6 +15,7 @@ namespace ts { forEachValue(f: (key: Path, v: T) => void): void; reduce(f: (memo: U, value: T, key: Path) => U, initial: U): U; + mergeFrom(other: FileMap): void; clear(): void; } @@ -1588,11 +1589,17 @@ namespace ts { readDirectory(rootDir: string, extension: string, exclude: string[]): string[]; /** - * Gets a value indicating whether the specified path exists. + * Gets a value indicating whether the specified path exists and is a file. * @param path The path to test. */ fileExists(path: string): boolean; + /** + * Gets a value indicating whether the specified path exists and is a directory. + * @param path The path to test. + */ + directoryExists(path: string): boolean; + /** * Reads the files names in the directory. * @param rootDir The directory path. @@ -2460,6 +2467,17 @@ namespace ts { options: CompilerOptions; fileNames: string[]; errors: Diagnostic[]; + wildcardDirectories?: Map; + } + + export const enum WatchDirectoryFlags { + None = 0, + Recursive = 1 << 0, + } + + export interface ExpandResult { + fileNames: string[]; + wildcardDirectories: Map; } /* @internal */ diff --git a/src/harness/external/chai.d.ts b/src/harness/external/chai.d.ts index fc25980d3a099..fba0024d254ee 100644 --- a/src/harness/external/chai.d.ts +++ b/src/harness/external/chai.d.ts @@ -167,12 +167,14 @@ declare module chai { module assert { function equal(actual: any, expected: any, message?: string): void; function notEqual(actual: any, expected: any, message?: string): void; - function deepEqual(actual: any, expected: any, message?: string): void; - function notDeepEqual(actual: any, expected: any, message?: string): void; + function deepEqual(actual: T, expected: T, message?: string): void; + function notDeepEqual(actual: T, expected: T, message?: string): void; function lengthOf(object: any[], length: number, message?: string): void; function isTrue(value: any, message?: string): void; function isFalse(value: any, message?: string): void; function isNull(value: any, message?: string): void; function isNotNull(value: any, message?: string): void; + function isUndefined(value: any, message?: string): void; + function isDefined(value: any, message?: string): void; } } \ No newline at end of file diff --git a/src/harness/harnessLanguageService.ts b/src/harness/harnessLanguageService.ts index e12105e3393ea..521d17cfc8fce 100644 --- a/src/harness/harnessLanguageService.ts +++ b/src/harness/harnessLanguageService.ts @@ -266,6 +266,7 @@ namespace Harness.LanguageService { throw new Error("Not implemented."); } fileExists(fileName: string) { return this.getScriptInfo(fileName) !== undefined; } + directoryExists(directoryName: string) { return false; } readFile(fileName: string) { const snapshot = this.nativeHost.getScriptSnapshot(fileName); return snapshot && snapshot.getText(0, snapshot.getLength()); diff --git a/src/harness/projectsRunner.ts b/src/harness/projectsRunner.ts index 655c2f07c2bd9..76c7952428a4b 100644 --- a/src/harness/projectsRunner.ts +++ b/src/harness/projectsRunner.ts @@ -213,6 +213,7 @@ class ProjectRunner extends RunnerBase { const configParseHost: ts.ParseConfigHost = { useCaseSensitiveFileNames: Harness.IO.useCaseSensitiveFileNames(), fileExists, + directoryExists, readDirectory, readDirectoryNames, readFileNames @@ -297,6 +298,10 @@ class ProjectRunner extends RunnerBase { return Harness.IO.fileExists(getFileNameInTheProjectTest(fileName)); } + function directoryExists(directoryName: string): boolean { + return Harness.IO.directoryExists(getFileNameInTheProjectTest(directoryName)); + } + function getSourceFileText(fileName: string): string { let text: string = undefined; try { diff --git a/src/harness/rwcRunner.ts b/src/harness/rwcRunner.ts index 5b4e2a7a3d96d..826f6b6726068 100644 --- a/src/harness/rwcRunner.ts +++ b/src/harness/rwcRunner.ts @@ -79,6 +79,7 @@ namespace RWC { const configParseHost: ts.ParseConfigHost = { useCaseSensitiveFileNames: Harness.IO.useCaseSensitiveFileNames(), fileExists: Harness.IO.fileExists, + directoryExists: Harness.IO.directoryExists, readDirectory: Harness.IO.readDirectory, readDirectoryNames: Harness.IO.readDirectoryNames, readFileNames: Harness.IO.readFileNames, diff --git a/src/server/editorServices.ts b/src/server/editorServices.ts index 0305b7595c276..0e1c1c97f0370 100644 --- a/src/server/editorServices.ts +++ b/src/server/editorServices.ts @@ -119,7 +119,7 @@ namespace ts.server { if (!resolution) { const existingResolution = currentResolutionsInFile && ts.lookUp(currentResolutionsInFile, moduleName); if (moduleResolutionIsValid(existingResolution)) { - // ok, it is safe to use existing module resolution results + // ok, it is safe to use existing module resolution results resolution = existingResolution; } else { @@ -144,8 +144,8 @@ namespace ts.server { } if (resolution.resolvedModule) { - // TODO: consider checking failedLookupLocations - // TODO: use lastCheckTime to track expiration for module name resolution + // TODO: consider checking failedLookupLocations + // TODO: use lastCheckTime to track expiration for module name resolution return true; } @@ -354,6 +354,7 @@ namespace ts.server { export interface ProjectOptions { // these fields can be present in the project file files?: string[]; + wildcardDirectories?: ts.Map; compilerOptions?: ts.CompilerOptions; } @@ -362,6 +363,7 @@ namespace ts.server { projectFilename: string; projectFileWatcher: FileWatcher; directoryWatcher: FileWatcher; + directoriesWatchedForWildcards: Map; // Used to keep track of what directories are watched for this project directoriesWatchedForTsconfig: string[] = []; program: ts.Program; @@ -510,7 +512,7 @@ namespace ts.server { openFileRootsConfigured: ScriptInfo[] = []; // a path to directory watcher map that detects added tsconfig files directoryWatchersForTsconfig: ts.Map = {}; - // count of how many projects are using the directory watcher. If the + // count of how many projects are using the directory watcher. If the // number becomes 0 for a watcher, then we should close it. directoryWatchersRefCount: ts.Map = {}; hostConfiguration: HostConfiguration; @@ -590,11 +592,11 @@ namespace ts.server { // We check if the project file list has changed. If so, we update the project. if (!arrayIsEqualTo(currentRootFiles && currentRootFiles.sort(), newRootFiles && newRootFiles.sort())) { // For configured projects, the change is made outside the tsconfig file, and - // it is not likely to affect the project for other files opened by the client. We can + // it is not likely to affect the project for other files opened by the client. We can // just update the current project. this.updateConfiguredProject(project); - // Call updateProjectStructure to clean up inferred projects we may have + // Call updateProjectStructure to clean up inferred projects we may have // created for the new files this.updateProjectStructure(); } @@ -739,6 +741,8 @@ namespace ts.server { if (project.isConfiguredProject()) { project.projectFileWatcher.close(); project.directoryWatcher.close(); + forEachValue(project.directoriesWatchedForWildcards, watcher => { watcher.close(); }); + delete project.directoriesWatchedForWildcards; this.configuredProjects = copyListRemovingItem(project, this.configuredProjects); } else { @@ -816,8 +820,8 @@ namespace ts.server { * @param info The file that has been closed or newly configured */ closeOpenFile(info: ScriptInfo) { - // Closing file should trigger re-reading the file content from disk. This is - // because the user may chose to discard the buffer content before saving + // Closing file should trigger re-reading the file content from disk. This is + // because the user may chose to discard the buffer content before saving // to the disk, and the server's version of the file can be out of sync. info.svc.reloadFromFile(info.fileName); @@ -915,8 +919,8 @@ namespace ts.server { } /** - * This function is to update the project structure for every projects. - * It is called on the premise that all the configured projects are + * This function is to update the project structure for every projects. + * It is called on the premise that all the configured projects are * up to date. */ updateProjectStructure() { @@ -970,7 +974,7 @@ namespace ts.server { if (rootFile.defaultProject && rootFile.defaultProject.isConfiguredProject()) { // If the root file has already been added into a configured project, - // meaning the original inferred project is gone already. + // meaning the original inferred project is gone already. if (!rootedProject.isConfiguredProject()) { this.removeProject(rootedProject); } @@ -1075,9 +1079,9 @@ namespace ts.server { } /** - * This function tries to search for a tsconfig.json for the given file. If we found it, + * This function tries to search for a tsconfig.json for the given file. If we found it, * we first detect if there is already a configured project created for it: if so, we re-read - * the tsconfig file content and update the project; otherwise we create a new one. + * the tsconfig file content and update the project; otherwise we create a new one. */ openOrUpdateConfiguredProjectForFile(fileName: string) { const searchPath = ts.normalizePath(getDirectoryPath(fileName)); @@ -1215,7 +1219,8 @@ namespace ts.server { else { const projectOptions: ProjectOptions = { files: parsedCommandLine.fileNames, - compilerOptions: parsedCommandLine.options + wildcardDirectories: parsedCommandLine.wildcardDirectories, + compilerOptions: parsedCommandLine.options, }; return { succeeded: true, projectOptions }; } @@ -1241,12 +1246,30 @@ namespace ts.server { } project.finishGraph(); project.projectFileWatcher = this.host.watchFile(configFilename, _ => this.watchedProjectConfigFileChanged(project)); - this.log("Add recursive watcher for: " + ts.getDirectoryPath(configFilename)); + + const configDirectoryPath = ts.getDirectoryPath(configFilename); + + this.log("Add recursive watcher for: " + configDirectoryPath); project.directoryWatcher = this.host.watchDirectory( - ts.getDirectoryPath(configFilename), + configDirectoryPath, path => this.directoryWatchedForSourceFilesChanged(project, path), /*recursive*/ true ); + + project.directoriesWatchedForWildcards = reduceProperties(projectOptions.wildcardDirectories, (watchers, flag, directory) => { + if (comparePaths(configDirectoryPath, directory, ".", !this.host.useCaseSensitiveFileNames) !== Comparison.EqualTo) { + const recursive = (flag & WatchDirectoryFlags.Recursive) !== 0; + this.log(`Add ${ recursive ? "recursive " : ""}watcher for: ${directory}`); + watchers[directory] = this.host.watchDirectory( + directory, + path => this.directoryWatchedForSourceFilesChanged(project, path), + recursive + ); + } + + return watchers; + }, >{}); + return { success: true, project: project }; } } @@ -1280,7 +1303,7 @@ namespace ts.server { info = this.openFile(fileName, /*openedByClient*/ false); } else { - // if the root file was opened by client, it would belong to either + // if the root file was opened by client, it would belong to either // openFileRoots or openFileReferenced. if (info.isOpen) { if (this.openFileRoots.indexOf(info) >= 0) { diff --git a/src/services/shims.ts b/src/services/shims.ts index 4c35f5aaf1b64..a7f85d2445230 100644 --- a/src/services/shims.ts +++ b/src/services/shims.ts @@ -75,6 +75,7 @@ namespace ts { readDirectory(rootDir: string, extension: string, exclude?: string): string; readDirectoryNames?(rootDir: string): string; readFileNames?(rootDir: string): string; + directoryExists?(path: string): boolean; useCaseSensitiveFileNames?: boolean; } @@ -473,6 +474,18 @@ namespace ts { return this.shimHost.fileExists(fileName); } + public directoryExists(directoryName: string): boolean { + if (this.shimHost.directoryExists) { + return this.shimHost.directoryExists(directoryName); + } + + if (sys) { + return sys.directoryExists(directoryName); + } + + return false; + } + public readFile(fileName: string): string { return this.shimHost.readFile(fileName); } diff --git a/tests/cases/unittests/expandFiles.ts b/tests/cases/unittests/expandFiles.ts index a9d12a7326057..3aac938f178ec 100644 --- a/tests/cases/unittests/expandFiles.ts +++ b/tests/cases/unittests/expandFiles.ts @@ -1,164 +1,395 @@ /// /// -describe("expandFiles", () => { - it("fail", () => { - assert.isTrue(false, "just checking"); - }); +namespace ts { + const caseInsensitiveBasePath = "c:/dev/"; + const caseInsensitiveHost = createMockParseConfigHost(/*ignoreCase*/ true, caseInsensitiveBasePath, [ + "a.ts", + "a.d.ts", + "a.js", + "b.ts", + "b.js", + "c.d.ts", + "z/a.ts", + "z/abz.ts", + "z/aba.ts", + "z/b.ts", + "z/bbz.ts", + "z/bba.ts", + "x/a.ts", + "x/aa.ts", + "x/b.ts", + "x/y/a.ts", + "x/y/b.ts", + "js/a.js", + "js/b.js", + ]); - const basePath = "c:/dev/"; - const caseInsensitiveHost = createMockParseConfigHost( - basePath, - /*files*/ [ - "c:/dev/a.ts", - "c:/dev/a.d.ts", - "c:/dev/a.js", - "c:/dev/b.ts", - "c:/dev/b.js", - "c:/dev/c.d.ts", - "c:/dev/z/a.ts", - "c:/dev/z/abz.ts", - "c:/dev/z/aba.ts", - "c:/dev/z/b.ts", - "c:/dev/z/bbz.ts", - "c:/dev/z/bba.ts", - "c:/dev/x/a.ts", - "c:/dev/x/aa.ts", - "c:/dev/x/b.ts", - "c:/dev/x/y/a.ts", - "c:/dev/x/y/b.ts" - ], - /*ignoreCase*/ true); - - const caseSensitiveHost = createMockParseConfigHost( - basePath, - /*files*/ [ - "c:/dev/a.ts", - "c:/dev/a.d.ts", - "c:/dev/a.js", - "c:/dev/b.ts", - "c:/dev/b.js", - "c:/dev/A.ts", - "c:/dev/B.ts", - "c:/dev/c.d.ts", - "c:/dev/z/a.ts", - "c:/dev/z/abz.ts", - "c:/dev/z/aba.ts", - "c:/dev/z/b.ts", - "c:/dev/z/bbz.ts", - "c:/dev/z/bba.ts", - "c:/dev/x/a.ts", - "c:/dev/x/b.ts", - "c:/dev/x/y/a.ts", - "c:/dev/x/y/b.ts", - ], - /*ignoreCase*/ false); - - const expect = _chai.expect; - describe("with literal file list", () => { - it("without exclusions", () => { - const fileNames = ["a.ts", "b.ts"]; - const results = ts.expandFiles(fileNames, /*includeSpecs*/ undefined, /*excludeSpecs*/ undefined, basePath, {}, caseInsensitiveHost); - assert.deepEqual(results, ["c:/dev/a.ts", "c:/dev/b.ts"]); - }); - it("missing files are still present", () => { - const fileNames = ["z.ts", "x.ts"]; - const results = ts.expandFiles(fileNames, /*includeSpecs*/ undefined, /*excludeSpecs*/ undefined, basePath, {}, caseInsensitiveHost); - assert.deepEqual(results, ["c:/dev/z.ts", "c:/dev/x.ts"]); - }); - it("are not removed due to excludes", () => { - const fileNames = ["a.ts", "b.ts"]; - const excludeSpecs = ["b.ts"]; - const results = ts.expandFiles(fileNames, /*includeSpecs*/ undefined, excludeSpecs, basePath, {}, caseInsensitiveHost); - assert.deepEqual(results, ["c:/dev/a.ts", "c:/dev/b.ts"]); - }); - }); + const caseSensitiveBasePath = "/dev/"; + const caseSensitiveHost = createMockParseConfigHost(/*ignoreCase*/ false, caseSensitiveBasePath, [ + "a.ts", + "a.d.ts", + "a.js", + "b.ts", + "b.js", + "A.ts", + "B.ts", + "c.d.ts", + "z/a.ts", + "z/abz.ts", + "z/aba.ts", + "z/b.ts", + "z/bbz.ts", + "z/bba.ts", + "x/a.ts", + "x/b.ts", + "x/y/a.ts", + "x/y/b.ts", + "js/a.js", + "js/b.js", + ]); - describe("with literal include list", () => { - it("without exclusions", () => { - const includeSpecs = ["a.ts", "b.ts"]; - const results = ts.expandFiles(/*fileNames*/ undefined, includeSpecs, /*excludeSpecs*/ undefined, basePath, {}, caseInsensitiveHost); - assert.deepEqual(results, ["c:/dev/a.ts", "c:/dev/b.ts"]); - }); - it("with non .ts file extensions are excluded", () => { - const includeSpecs = ["a.js", "b.js"]; - const results = ts.expandFiles(/*fileNames*/ undefined, includeSpecs, /*excludeSpecs*/ undefined, basePath, {}, caseInsensitiveHost); - assert.deepEqual(results, []); - }); - it("with missing files are excluded", () => { - const includeSpecs = ["z.ts", "x.ts"]; - const results = ts.expandFiles(/*fileNames*/ undefined, includeSpecs, /*excludeSpecs*/ undefined, basePath, {}, caseInsensitiveHost); - assert.deepEqual(results, []); - }); - it("with literal excludes", () => { - const includeSpecs = ["a.ts", "b.ts"]; - const excludeSpecs = ["b.ts"]; - const results = ts.expandFiles(/*fileNames*/ undefined, includeSpecs, excludeSpecs, basePath, {}, caseInsensitiveHost); - assert.deepEqual(results, ["c:/dev/a.ts"]); - }); - it("with wildcard excludes", () => { - const includeSpecs = ["a.ts", "b.ts", "z/a.ts", "z/abz.ts", "z/aba.ts", "x/b.ts"]; - const excludeSpecs = ["*.ts", "z/??z.ts", "*/b.ts"]; - const results = ts.expandFiles(/*fileNames*/ undefined, includeSpecs, excludeSpecs, basePath, {}, caseInsensitiveHost); - assert.deepEqual(results, ["c:/dev/z/a.ts", "c:/dev/z/aba.ts"]); - }); - it("with recursive excludes", () => { - const includeSpecs = ["a.ts", "b.ts", "x/a.ts", "x/b.ts", "x/y/a.ts", "x/y/b.ts"]; - const excludeSpecs = ["**/b.ts"]; - const results = ts.expandFiles(/*fileNames*/ undefined, includeSpecs, excludeSpecs, basePath, {}, caseInsensitiveHost); - assert.deepEqual(results, ["c:/dev/a.ts", "c:/dev/x/a.ts", "c:/dev/x/y/a.ts"]); - }); - it("with case sensitive exclude", () => { - const includeSpecs = ["B.ts"]; - const excludeSpecs = ["**/b.ts"]; - const results = ts.expandFiles(/*fileNames*/ undefined, includeSpecs, excludeSpecs, basePath, {}, caseSensitiveHost); - assert.deepEqual(results, ["c:/dev/B.ts"]); - }); - }); + const caseInsensitiveMixedExtensionHost = createMockParseConfigHost(/*ignoreCase*/ true, caseInsensitiveBasePath, [ + "a.ts", + "a.d.ts", + "a.js", + "b.tsx", + "b.d.ts", + "b.jsx", + "c.tsx", + "c.js", + "d.js", + "e.jsx", + "f.other" + ]); - describe("with wildcard include list", () => { - it("same named declarations are excluded", () => { - const includeSpecs = ["*.ts"]; - const results = ts.expandFiles(/*fileNames*/ undefined, includeSpecs, /*excludeSpecs*/ undefined, basePath, {}, caseInsensitiveHost); - assert.deepEqual(results, ["c:/dev/a.ts", "c:/dev/b.ts", "c:/dev/c.d.ts"]); - }); - it("`*` matches only ts files", () => { - const includeSpecs = ["*"]; - const results = ts.expandFiles(/*fileNames*/ undefined, includeSpecs, /*excludeSpecs*/ undefined, basePath, {}, caseInsensitiveHost); - assert.deepEqual(results, ["c:/dev/a.ts", "c:/dev/b.ts", "c:/dev/c.d.ts"]); + describe("expandFiles", () => { + describe("with literal file list", () => { + it("without exclusions", () => { + const fileNames = ["a.ts", "b.ts"]; + const expected: ts.ExpandResult = { + fileNames: ["c:/dev/a.ts", "c:/dev/b.ts"], + wildcardDirectories: {}, + }; + const actual = ts.expandFiles(fileNames, /*includeSpecs*/ undefined, /*excludeSpecs*/ undefined, caseInsensitiveBasePath, {}, caseInsensitiveHost); + assert.deepEqual(actual, expected); + }); + it("missing files are still present", () => { + const fileNames = ["z.ts", "x.ts"]; + const expected: ts.ExpandResult = { + fileNames: ["c:/dev/z.ts", "c:/dev/x.ts"], + wildcardDirectories: {}, + }; + const actual = ts.expandFiles(fileNames, /*includeSpecs*/ undefined, /*excludeSpecs*/ undefined, caseInsensitiveBasePath, {}, caseInsensitiveHost); + assert.deepEqual(actual, expected); + }); + it("are not removed due to excludes", () => { + const fileNames = ["a.ts", "b.ts"]; + const excludeSpecs = ["b.ts"]; + const expected: ts.ExpandResult = { + fileNames: ["c:/dev/a.ts", "c:/dev/b.ts"], + wildcardDirectories: {}, + }; + const actual = ts.expandFiles(fileNames, /*includeSpecs*/ undefined, excludeSpecs, caseInsensitiveBasePath, {}, caseInsensitiveHost); + assert.deepEqual(actual, expected); + }); }); - it("`?` matches only a single character", () => { - const includeSpecs = ["x/?.ts"]; - const results = ts.expandFiles(/*fileNames*/ undefined, includeSpecs, /*excludeSpecs*/ undefined, basePath, {}, caseInsensitiveHost); - assert.deepEqual(results, ["c:/dev/x/a.ts", "c:/dev/x/b.ts"]); - }); - it("with recursive directory", () => { - const includeSpecs = ["**/a.ts"]; - const results = ts.expandFiles(/*fileNames*/ undefined, includeSpecs, /*excludeSpecs*/ undefined, basePath, {}, caseInsensitiveHost); - assert.deepEqual(results, ["c:/dev/a.ts", "c:/dev/x/a.ts", "c:/dev/x/y/a.ts", "c:/dev/z/a.ts"]); - }); - it("case sensitive", () => { - const includeSpecs = ["**/A.ts"]; - const results = ts.expandFiles(/*fileNames*/ undefined, includeSpecs, /*excludeSpecs*/ undefined, basePath, {}, caseSensitiveHost); - assert.deepEqual(results, ["c:/dev/A.ts"]); + + describe("with literal include list", () => { + it("without exclusions", () => { + const includeSpecs = ["a.ts", "b.ts"]; + const expected: ts.ExpandResult = { + fileNames: ["c:/dev/a.ts", "c:/dev/b.ts"], + wildcardDirectories: {}, + }; + const actual = ts.expandFiles(/*fileNames*/ undefined, includeSpecs, /*excludeSpecs*/ undefined, caseInsensitiveBasePath, {}, caseInsensitiveHost); + assert.deepEqual(actual, expected); + }); + it("with non .ts file extensions are excluded", () => { + const includeSpecs = ["a.js", "b.js"]; + const expected: ts.ExpandResult = { + fileNames: [], + wildcardDirectories: {}, + }; + const actual = ts.expandFiles(/*fileNames*/ undefined, includeSpecs, /*excludeSpecs*/ undefined, caseInsensitiveBasePath, {}, caseInsensitiveHost); + assert.deepEqual(actual, expected); + }); + it("with missing files are excluded", () => { + const includeSpecs = ["z.ts", "x.ts"]; + const expected: ts.ExpandResult = { + fileNames: [], + wildcardDirectories: {}, + }; + const actual = ts.expandFiles(/*fileNames*/ undefined, includeSpecs, /*excludeSpecs*/ undefined, caseInsensitiveBasePath, {}, caseInsensitiveHost); + assert.deepEqual(actual, expected); + }); + it("with literal excludes", () => { + const includeSpecs = ["a.ts", "b.ts"]; + const excludeSpecs = ["b.ts"]; + const expected: ts.ExpandResult = { + fileNames: ["c:/dev/a.ts"], + wildcardDirectories: {}, + }; + const actual = ts.expandFiles(/*fileNames*/ undefined, includeSpecs, excludeSpecs, caseInsensitiveBasePath, {}, caseInsensitiveHost); + assert.deepEqual(actual, expected); + }); + it("with wildcard excludes", () => { + const includeSpecs = ["a.ts", "b.ts", "z/a.ts", "z/abz.ts", "z/aba.ts", "x/b.ts"]; + const excludeSpecs = ["*.ts", "z/??z.ts", "*/b.ts"]; + const expected: ts.ExpandResult = { + fileNames: ["c:/dev/z/a.ts", "c:/dev/z/aba.ts"], + wildcardDirectories: {}, + }; + const actual = ts.expandFiles(/*fileNames*/ undefined, includeSpecs, excludeSpecs, caseInsensitiveBasePath, {}, caseInsensitiveHost); + assert.deepEqual(actual, expected); + }); + it("with recursive excludes", () => { + const includeSpecs = ["a.ts", "b.ts", "x/a.ts", "x/b.ts", "x/y/a.ts", "x/y/b.ts"]; + const excludeSpecs = ["**/b.ts"]; + const expected: ts.ExpandResult = { + fileNames: ["c:/dev/a.ts", "c:/dev/x/a.ts", "c:/dev/x/y/a.ts"], + wildcardDirectories: {}, + }; + const actual = ts.expandFiles(/*fileNames*/ undefined, includeSpecs, excludeSpecs, caseInsensitiveBasePath, {}, caseInsensitiveHost); + assert.deepEqual(actual, expected); + }); + it("with case sensitive exclude", () => { + const includeSpecs = ["B.ts"]; + const excludeSpecs = ["**/b.ts"]; + const expected: ts.ExpandResult = { + fileNames: ["/dev/B.ts"], + wildcardDirectories: {}, + }; + const actual = ts.expandFiles(/*fileNames*/ undefined, includeSpecs, excludeSpecs, caseSensitiveBasePath, {}, caseSensitiveHost); + assert.deepEqual(actual, expected); + }); }); - it("with missing files are excluded", () => { - const includeSpecs = ["*/z.ts"]; - const results = ts.expandFiles(/*fileNames*/ undefined, includeSpecs, /*excludeSpecs*/ undefined, basePath, {}, caseInsensitiveHost); - assert.deepEqual(results, []); + + describe("with wildcard include list", () => { + it("same named declarations are excluded", () => { + const includeSpecs = ["*.ts"]; + const expected: ts.ExpandResult = { + fileNames: ["c:/dev/a.ts", "c:/dev/b.ts", "c:/dev/c.d.ts"], + wildcardDirectories: { + "c:/dev": ts.WatchDirectoryFlags.None + }, + }; + const actual = ts.expandFiles(/*fileNames*/ undefined, includeSpecs, /*excludeSpecs*/ undefined, caseInsensitiveBasePath, {}, caseInsensitiveHost); + assert.deepEqual(actual, expected); + }); + it("`*` matches only ts files", () => { + const includeSpecs = ["*"]; + const expected: ts.ExpandResult = { + fileNames: ["c:/dev/a.ts", "c:/dev/b.ts", "c:/dev/c.d.ts"], + wildcardDirectories: { + "c:/dev": ts.WatchDirectoryFlags.None + }, + }; + const actual = ts.expandFiles(/*fileNames*/ undefined, includeSpecs, /*excludeSpecs*/ undefined, caseInsensitiveBasePath, {}, caseInsensitiveHost); + assert.deepEqual(actual, expected); + }); + it("`?` matches only a single character", () => { + const includeSpecs = ["x/?.ts"]; + const expected: ts.ExpandResult = { + fileNames: ["c:/dev/x/a.ts", "c:/dev/x/b.ts"], + wildcardDirectories: { + "c:/dev/x": ts.WatchDirectoryFlags.None + }, + }; + const actual = ts.expandFiles(/*fileNames*/ undefined, includeSpecs, /*excludeSpecs*/ undefined, caseInsensitiveBasePath, {}, caseInsensitiveHost); + assert.deepEqual(actual, expected); + }); + it("with recursive directory", () => { + const includeSpecs = ["**/a.ts"]; + const expected: ts.ExpandResult = { + fileNames: ["c:/dev/a.ts", "c:/dev/x/a.ts", "c:/dev/x/y/a.ts", "c:/dev/z/a.ts"], + wildcardDirectories: { + "c:/dev": ts.WatchDirectoryFlags.Recursive + }, + }; + const actual = ts.expandFiles(/*fileNames*/ undefined, includeSpecs, /*excludeSpecs*/ undefined, caseInsensitiveBasePath, {}, caseInsensitiveHost); + assert.deepEqual(actual, expected); + }); + it("case sensitive", () => { + const includeSpecs = ["**/A.ts"]; + const expected: ts.ExpandResult = { + fileNames: ["/dev/A.ts"], + wildcardDirectories: { + "/dev": ts.WatchDirectoryFlags.Recursive + }, + }; + const actual = ts.expandFiles(/*fileNames*/ undefined, includeSpecs, /*excludeSpecs*/ undefined, caseSensitiveBasePath, {}, caseSensitiveHost); + assert.deepEqual(actual, expected); + }); + it("with missing files are excluded", () => { + const includeSpecs = ["*/z.ts"]; + const expected: ts.ExpandResult = { + fileNames: [], + wildcardDirectories: { + "c:/dev": ts.WatchDirectoryFlags.None + }, + }; + const actual = ts.expandFiles(/*fileNames*/ undefined, includeSpecs, /*excludeSpecs*/ undefined, caseInsensitiveBasePath, {}, caseInsensitiveHost); + assert.deepEqual(actual, expected); + }); + it("always include literal files", () => { + const fileNames = ["a.ts"]; + const includeSpecs = ["*/z.ts"]; + const excludeSpecs = ["**/a.ts"]; + const expected: ts.ExpandResult = { + fileNames: ["c:/dev/a.ts"], + wildcardDirectories: { + "c:/dev": ts.WatchDirectoryFlags.None + }, + }; + const actual = ts.expandFiles(fileNames, includeSpecs, excludeSpecs, caseInsensitiveBasePath, {}, caseInsensitiveHost); + assert.deepEqual(actual, expected); + }); + it("exclude folders", () => { + const includeSpecs = ["**/*"]; + const excludeSpecs = ["z", "x"]; + const expected: ts.ExpandResult = { + fileNames: [ + "c:/dev/a.ts", + "c:/dev/b.ts", + "c:/dev/c.d.ts" + ], + wildcardDirectories: { + "c:/dev": ts.WatchDirectoryFlags.Recursive + } + }; + const actual = ts.expandFiles(/*fileNames*/ undefined, includeSpecs, excludeSpecs, caseInsensitiveBasePath, {}, caseInsensitiveHost); + assert.deepEqual(actual, expected); + }); + it("exclude .js files when allowJs=false", () => { + const includeSpecs = ["js/*"]; + const expected: ts.ExpandResult = { + fileNames: [], + wildcardDirectories: { + "c:/dev/js": ts.WatchDirectoryFlags.None + } + }; + const actual = ts.expandFiles(/*fileNames*/ undefined, includeSpecs, /*excludeSpecs*/ undefined, caseInsensitiveBasePath, {}, caseInsensitiveHost); + assert.deepEqual(actual, expected); + }); + it("include .js files when allowJs=true", () => { + const includeSpecs = ["js/*"]; + const expected: ts.ExpandResult = { + fileNames: ["c:/dev/js/a.js", "c:/dev/js/b.js"], + wildcardDirectories: { + "c:/dev/js": ts.WatchDirectoryFlags.None + } + }; + const actual = ts.expandFiles(/*fileNames*/ undefined, includeSpecs, /*excludeSpecs*/ undefined, caseInsensitiveBasePath, { allowJs: true }, caseInsensitiveHost); + assert.deepEqual(actual, expected); + }); }); - it("always include literal files", () => { - const fileNames = ["a.ts"]; - const includeSpecs = ["*/z.ts"]; - const excludeSpecs = ["**/a.ts"]; - const results = ts.expandFiles(fileNames, includeSpecs, excludeSpecs, basePath, {}, caseInsensitiveHost); - assert.deepEqual(results, ["c:/dev/a.ts"]); + + describe("when called from parseJsonConfigFileContent", () => { + it("with jsx=none, allowJs=false", () => { + const json: any = { + "compilerOptions": { + "jsx": "none", + "allowJs": false + } + }; + const expected: ts.ExpandResult = { + fileNames: [ + "c:/dev/a.ts", + "c:/dev/b.tsx", + "c:/dev/c.tsx", + ], + wildcardDirectories: { + "c:/dev": ts.WatchDirectoryFlags.Recursive + } + }; + const actual = ts.parseJsonConfigFileContent(json, caseInsensitiveMixedExtensionHost, caseInsensitiveBasePath); + assert.deepEqual(actual.fileNames, expected.fileNames); + assert.deepEqual(actual.wildcardDirectories, expected.wildcardDirectories); + }); + it("with jsx=preserve, allowJs=false", () => { + const json: any = { + "compilerOptions": { + "jsx": "preserve", + "allowJs": false + } + }; + const expected: ts.ExpandResult = { + fileNames: [ + "c:/dev/a.ts", + "c:/dev/b.tsx", + "c:/dev/c.tsx", + ], + wildcardDirectories: { + "c:/dev": ts.WatchDirectoryFlags.Recursive + } + }; + const actual = ts.parseJsonConfigFileContent(json, caseInsensitiveMixedExtensionHost, caseInsensitiveBasePath); + assert.deepEqual(actual.fileNames, expected.fileNames); + assert.deepEqual(actual.wildcardDirectories, expected.wildcardDirectories); + }); + it("with jsx=none, allowJs=true", () => { + const json: any = { + "compilerOptions": { + "jsx": "none", + "allowJs": true + } + }; + const expected: ts.ExpandResult = { + fileNames: [ + "c:/dev/a.ts", + "c:/dev/b.tsx", + "c:/dev/c.tsx", + "c:/dev/d.js", + "c:/dev/e.jsx", + ], + wildcardDirectories: { + "c:/dev": ts.WatchDirectoryFlags.Recursive + } + }; + const actual = ts.parseJsonConfigFileContent(json, caseInsensitiveMixedExtensionHost, caseInsensitiveBasePath); + assert.deepEqual(actual.fileNames, expected.fileNames); + assert.deepEqual(actual.wildcardDirectories, expected.wildcardDirectories); + }); + it("with jsx=preserve, allowJs=true", () => { + const json: any = { + "compilerOptions": { + "jsx": "preserve", + "allowJs": true + } + }; + const expected: ts.ExpandResult = { + fileNames: [ + "c:/dev/a.ts", + "c:/dev/b.tsx", + "c:/dev/c.tsx", + "c:/dev/d.js", + "c:/dev/e.jsx", + ], + wildcardDirectories: { + "c:/dev": ts.WatchDirectoryFlags.Recursive + } + }; + const actual = ts.parseJsonConfigFileContent(json, caseInsensitiveMixedExtensionHost, caseInsensitiveBasePath); + assert.deepEqual(actual.fileNames, expected.fileNames); + assert.deepEqual(actual.wildcardDirectories, expected.wildcardDirectories); + }); }); }); - function createMockParseConfigHost(basePath: string, files: string[], ignoreCase: boolean): ts.ParseConfigHost { + interface DirectoryEntry { + files: ts.Map; + directories: ts.Map; + } + + interface TestParseConfigHost extends ts.ParseConfigHost { + basePath: string; + } + + function createMockParseConfigHost(ignoreCase: boolean, basePath: string, files: string[]): TestParseConfigHost { const fileSet: ts.Map = {}; - const directorySet: ts.Map<{ files: ts.Map; directories: ts.Map; }> = {}; + const directorySet: ts.Map = {}; + const emptyDirectory: DirectoryEntry = { files: {}, directories: {} }; files.sort((a, b) => ts.comparePaths(a, b, basePath, ignoreCase)); for (const file of files) { @@ -167,18 +398,24 @@ describe("expandFiles", () => { return { useCaseSensitiveFileNames: !ignoreCase, + basePath, fileExists, + directoryExists, readDirectory, readFileNames, readDirectoryNames }; function fileExists(path: string): boolean { + path = ts.getNormalizedAbsolutePath(path, basePath); + path = ts.removeTrailingDirectorySeparator(path); const fileKey = ignoreCase ? path.toLowerCase() : path; return ts.hasProperty(fileSet, fileKey); } function directoryExists(path: string): boolean { + path = ts.getNormalizedAbsolutePath(path, basePath); + path = ts.removeTrailingDirectorySeparator(path); const directoryKey = ignoreCase ? path.toLowerCase() : path; return ts.hasProperty(directorySet, directoryKey); } @@ -188,7 +425,7 @@ describe("expandFiles", () => { } function readFileNames(path: string) { - const files = getDirectoryEntry(path).files; + const { files } = getDirectoryEntry(path) || emptyDirectory; const result: string[] = []; ts.forEachKey(files, key => { result.push(key); }); result.sort((a, b) => ts.compareStrings(a, b, ignoreCase)); @@ -196,7 +433,7 @@ describe("expandFiles", () => { } function readDirectoryNames(path: string) { - const directories = getDirectoryEntry(path).directories; + const { directories } = getDirectoryEntry(path); // || emptyDirectory; const result: string[] = []; ts.forEachKey(directories, key => { result.push(key); }); result.sort((a, b) => ts.compareStrings(a, b, ignoreCase)); @@ -245,5 +482,4 @@ describe("expandFiles", () => { } } } -}); - +} \ No newline at end of file From def3ba10854c2cb72bf7f60b71e6e459dfb79efb Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Mon, 7 Dec 2015 15:32:32 -0800 Subject: [PATCH 03/84] Added stubs to ChakraHost interface for readDirectoryNames/readFileNames --- src/compiler/sys.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/compiler/sys.ts b/src/compiler/sys.ts index 11a7646bd0997..5ff8dbf679661 100644 --- a/src/compiler/sys.ts +++ b/src/compiler/sys.ts @@ -62,6 +62,8 @@ namespace ts { readFile(path: string): string; writeFile(path: string, contents: string): void; readDirectory(path: string, extension?: string, exclude?: string[]): string[]; + readDirectoryNames(path: string): string[]; + readFileNames(path: string): string[]; }; export var sys: System = (function () { @@ -537,6 +539,8 @@ namespace ts { getExecutingFilePath: () => ChakraHost.executingFile, getCurrentDirectory: () => ChakraHost.currentDirectory, readDirectory: ChakraHost.readDirectory, + readFileNames: ChakraHost.readFileNames, + readDirectoryNames: ChakraHost.readDirectoryNames, exit: ChakraHost.quit, }; } From c3c3bca6fab303c94fe3ff369c9749081d30e40e Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Tue, 8 Dec 2015 10:54:23 -0800 Subject: [PATCH 04/84] Fixed typos in comments --- src/compiler/commandLineParser.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index 6499ec6327fc4..4d2480ffef946 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -646,12 +646,12 @@ namespace ts { const keyMapper = host.useCaseSensitiveFileNames ? caseSensitiveKeyMapper : caseInsensitiveKeyMapper; // Literal file names (provided via the "files" array in tsconfig.json) are stored in a - // file map with a possibly invariant key. We use this map later when when including + // file map with a possibly case insensitive key. We use this map later when when including // wildcard paths. const literalFiles = createFileMap(keyMapper); - // Wildcard paths (provided via the "includes" array in tscofnig.json) are stored in a - // file map with a possibly invariant key. We use this map to store paths matched + // Wildcard paths (provided via the "includes" array in tsconfig.json) are stored in a + // file map with a possibly case insensitive key. We use this map to store paths matched // via wildcard, and to handle extension priority. const wildcardFiles = createFileMap(keyMapper); @@ -672,7 +672,7 @@ namespace ts { // - A "directories" array, which contains the subdirectory names in the directory. const cache = createFileMap(keyMapper); - // Rather than requery this for each file and filespec, we querythe supported extensions + // Rather than requery this for each file and filespec, we query the supported extensions // once and store it on the expansion context. const supportedExtensions = getSupportedExtensions(options); From bf9af46e5a1d35a5d83f95e0260dff1689967440 Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Tue, 8 Dec 2015 10:57:04 -0800 Subject: [PATCH 05/84] Changed name of 'reduce' method added to FileSet --- src/compiler/commandLineParser.ts | 4 ++-- src/compiler/core.ts | 4 ++-- src/compiler/types.ts | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index 4d2480ffef946..b4339e78f2adc 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -711,8 +711,8 @@ namespace ts { } return { - fileNames: wildcardFiles.reduce(addFileToOutput, literalFiles.reduce(addFileToOutput, [])), - wildcardDirectories: wildcardDirectories.reduce>(addDirectoryToOutput, {}), + fileNames: wildcardFiles.reduceProperties(addFileToOutput, literalFiles.reduceProperties(addFileToOutput, [])), + wildcardDirectories: wildcardDirectories.reduceProperties>(addDirectoryToOutput, {}), }; } diff --git a/src/compiler/core.ts b/src/compiler/core.ts index 998766e8d9deb..3e6981996e199 100644 --- a/src/compiler/core.ts +++ b/src/compiler/core.ts @@ -25,7 +25,7 @@ namespace ts { contains, remove, forEachValue: forEachValueInMap, - reduce, + reduceProperties: reducePropertiesInMap, clear, mergeFrom }; @@ -36,7 +36,7 @@ namespace ts { } } - function reduce(callback: (memo: U, value: T, key: Path) => U, initial: U) { + function reducePropertiesInMap(callback: (memo: U, value: T, key: Path) => U, initial: U) { return reduceProperties(files, callback, initial); } diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 4b84c27be99c1..4c62904f4e843 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -14,7 +14,7 @@ namespace ts { remove(fileName: Path): void; forEachValue(f: (key: Path, v: T) => void): void; - reduce(f: (memo: U, value: T, key: Path) => U, initial: U): U; + reduceProperties(f: (memo: U, value: T, key: Path) => U, initial: U): U; mergeFrom(other: FileMap): void; clear(): void; } From d8572508ee0650e58a7602098498b1309c301d2d Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Mon, 14 Dec 2015 15:21:12 -0800 Subject: [PATCH 06/84] Heavily revised implementation that relies on an updated 'readDirectory' API. --- Jakefile.js | 1 + src/compiler/commandLineParser.ts | 731 +++--------------- src/compiler/core.ts | 203 ++++- src/compiler/sys.ts | 119 +-- src/compiler/types.ts | 22 +- src/harness/harness.ts | 39 +- src/harness/harnessLanguageService.ts | 10 +- src/harness/loggedIO.ts | 9 +- src/harness/projectsRunner.ts | 15 +- src/harness/rwcRunner.ts | 3 - src/harness/vfs.ts | 160 ++++ src/services/shims.ts | 76 +- .../cases/unittests/cachingInServerLSHost.ts | 4 +- tests/cases/unittests/expandFiles.ts | 259 +++---- tests/cases/unittests/session.ts | 2 - 15 files changed, 629 insertions(+), 1024 deletions(-) create mode 100644 src/harness/vfs.ts diff --git a/Jakefile.js b/Jakefile.js index 58677e19a75c6..0ce1d5a95ece2 100644 --- a/Jakefile.js +++ b/Jakefile.js @@ -131,6 +131,7 @@ var languageServiceLibrarySources = [ var harnessCoreSources = [ "harness.ts", + "vfs.ts", "sourceMapRecorder.ts", "harnessLanguageService.ts", "fourslash.ts", diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index b4339e78f2adc..c81b3b63244fc 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -582,47 +582,8 @@ namespace ts { return { options, errors }; } - // Simplified whitelist, forces escaping of any non-word (or digit), non-whitespace character. - const reservedCharacterPattern = /[^\w\s]/g; - - const enum ExpansionState { - Ok, - Error - } - - interface ExpansionContext { - /** A pattern used to exclude a file specification. */ - excludePattern: RegExp; - /** Compiler options. */ - options: CompilerOptions; - /** The host used to resolve files and directories. */ - host: ParseConfigHost; - /** Errors to report. */ - errors: Diagnostic[]; - /** The set of literal files. */ - literalFiles: FileMap; - /** The set of files matching a wildcard. */ - wildcardFiles: FileMap; - /** Directories to be watched. */ - wildcardDirectories: FileMap; - /** Supported extensions. */ - supportedExtensions: string[]; - /** - * Path cache, used to reduce calls to the file system. `true` indicates a file exists, - * `false` indicates a file or directory does not exist. A DirectoryResult - * indicates the file and subdirectory names in a directory. */ - cache: FileMap; - } - - const enum FileSystemEntryKind { - File, - Directory - } - - interface DirectoryResult { - files?: string[]; - directories?: string[]; - } + const invalidTrailingRecursionPattern = /(^|\/)\*\*\/?$/; + const invalidMultipleRecursionPatterns = /(^|\/)\*\*\/(.*\/)?\*\*($|\/)/; /** * Expands an array of file specifications. @@ -642,348 +603,143 @@ namespace ts { // The exclude spec list is converted into a regular expression, which allows us to quickly // test whether a file or directory should be excluded before recursively traversing the // file system. - const excludePattern = includeSpecs ? createExcludeRegularExpression(excludeSpecs, basePath, options, host, errors) : undefined; const keyMapper = host.useCaseSensitiveFileNames ? caseSensitiveKeyMapper : caseInsensitiveKeyMapper; // Literal file names (provided via the "files" array in tsconfig.json) are stored in a // file map with a possibly case insensitive key. We use this map later when when including // wildcard paths. - const literalFiles = createFileMap(keyMapper); + const literalFileMap: Map = {}; // Wildcard paths (provided via the "includes" array in tsconfig.json) are stored in a // file map with a possibly case insensitive key. We use this map to store paths matched // via wildcard, and to handle extension priority. - const wildcardFiles = createFileMap(keyMapper); + const wildcardFileMap: Map = {}; // Wildcard directories (provided as part of a wildcard path) are stored in a // file map that marks whether it was a regular wildcard match (with a `*` or `?` token), // or a recursive directory. This information is used by filesystem watchers to monitor for // new entries in these paths. - const wildcardDirectories = createFileMap(keyMapper); - - // To reduce the overhead of disk I/O (and marshalling to managed code when hosted in - // Visual Studio), file system queries are cached during the expansion session. - // If present, a cache entry can be one of three values: - // - A `false` value indicates the file or directory did not exist. - // - A `true` value indicates the path is a file and it exists. - // - An object value indicates the path is a directory and exists. The object may have - // zero, one, or both of the following properties: - // - A "files" array, which contains the file names in the directory. - // - A "directories" array, which contains the subdirectory names in the directory. - const cache = createFileMap(keyMapper); + const wildcardDirectories: Map = getWildcardDirectories(includeSpecs, basePath, host.useCaseSensitiveFileNames); // Rather than requery this for each file and filespec, we query the supported extensions // once and store it on the expansion context. const supportedExtensions = getSupportedExtensions(options); - // The expansion context holds references to shared information for the various expansion - // operations to reduce the overhead of closures. - const context: ExpansionContext = { - options, - host, - errors, - excludePattern, - literalFiles, - wildcardFiles, - wildcardDirectories, - supportedExtensions, - cache - }; - // Literal files are always included verbatim. An "include" or "exclude" specification cannot // remove a literal file. if (fileNames) { for (const fileName of fileNames) { - const path = toPath(fileName, basePath, caseSensitiveKeyMapper); - if (!literalFiles.contains(path)) { - literalFiles.set(path, path); - } + const file = combinePaths(basePath, fileName); + literalFileMap[keyMapper(file)] = file; } } - // Each "include" specification is expanded and matching files are added. if (includeSpecs) { - for (let includeSpec of includeSpecs) { - includeSpec = normalizePath(includeSpec); - includeSpec = removeTrailingDirectorySeparator(includeSpec); - expandFileSpec(includeSpec, basePath, 0, context); - } - } - - return { - fileNames: wildcardFiles.reduceProperties(addFileToOutput, literalFiles.reduceProperties(addFileToOutput, [])), - wildcardDirectories: wildcardDirectories.reduceProperties>(addDirectoryToOutput, {}), - }; - } - - /** - * Expands a file specification with wildcards. - * - * @param fileSpec The original file specification. - * @param basePath The directory to expand. This path must exist. - * @param start The starting offset in the file specification. - * @param context The expansion context. - * @param isExpandingRecursiveDirectory A value indicating whether the file specification includes a recursive directory wildcard prior to the start of this segment. - */ - function expandFileSpec(fileSpec: string, basePath: Path, start: number, context: ExpansionContext, isExpandingRecursiveDirectory?: boolean): ExpansionState { - // A file specification must always point to a file. As a result, we always assume the - // path segment following the last directory separator points to a file. The only - // exception is when the final path segment is the recursive directory pattern "**", in - // which case we report an error. - const { host, options, errors, wildcardFiles, wildcardDirectories, excludePattern, cache, supportedExtensions } = context; - - // Skip expansion if the base path matches an exclude pattern. - if (isExcludedPath(basePath, excludePattern)) { - return ExpansionState.Ok; - } - - // Find the offset of the next wildcard in the file specification. If there are no more - // wildcards, we can include the file if it exists and isn't excluded. - let offset = indexOfWildcard(fileSpec, start); - if (offset < 0) { - const path = toPath(fileSpec.substring(start), basePath, caseSensitiveKeyMapper); - if (!isExcludedPath(path, excludePattern) && pathExists(path, FileSystemEntryKind.File, context)) { - includeFile(path, context, /*wildcardHasExtension*/ true); - } - - return ExpansionState.Ok; - } - - // Find the last directory separator before the wildcard to get the leading path. - offset = fileSpec.lastIndexOf(directorySeparator, offset); - if (offset > start) { - // The wildcard occurs in a later segment, include remaining path up to - // wildcard in prefix. - basePath = toPath(fileSpec.substring(start, offset), basePath, caseSensitiveKeyMapper); - - // Skip this wildcard path if the base path now matches an exclude pattern. - if (isExcludedPath(basePath, excludePattern) || !pathExists(basePath, FileSystemEntryKind.Directory, context)) { - return ExpansionState.Ok; - } - - start = offset + 1; - } - - // Find the offset of the next directory separator to extract the wildcard path segment. - offset = getEndOfPathSegment(fileSpec, start); - - // Check if the current offset is the beginning of a recursive directory pattern. - if (isRecursiveDirectoryWildcard(fileSpec, start, offset)) { - // Stop expansion if a file specification contains more than one recursive directory pattern. - if (isExpandingRecursiveDirectory) { - if (errors) { - errors.push(createCompilerDiagnostic(Diagnostics.File_specification_cannot_contain_multiple_recursive_directory_wildcards_Asterisk_Asterisk_Colon_0, fileSpec)); - } - - return ExpansionState.Error; - } - - if (offset >= fileSpec.length) { - // If there is no file specification following the recursive directory pattern - // then we report an error as we cannot match any files. - if (errors) { - errors.push(createCompilerDiagnostic(Diagnostics.File_specification_cannot_end_in_a_recursive_directory_wildcard_Asterisk_Asterisk_Colon_0, fileSpec)); - } - - return ExpansionState.Error; - } - - // Keep track of the recursive wildcard directory - wildcardDirectories.set(basePath, WatchDirectoryFlags.Recursive); - - // Expand the recursive directory pattern. - return expandRecursiveDirectory(fileSpec, basePath, offset + 1, context); - } - - if (!isExpandingRecursiveDirectory) { - wildcardDirectories.set(basePath, WatchDirectoryFlags.None); - } - - // Match the entries in the directory against the wildcard pattern. - const pattern = createRegularExpressionFromWildcard(fileSpec, start, offset, host); - - // If there are no more directory separators (the offset is at the end of the file specification), then - // this must be a file. - if (offset >= fileSpec.length) { - const wildcardHasExtension = fileSegmentHasExtension(fileSpec, start); - const fileNames = readDirectory(basePath, FileSystemEntryKind.File, context); - for (const fileName of fileNames) { - // Skip the file if it doesn't match the pattern. - if (!pattern.test(fileName)) { + includeSpecs = validateSpecs(includeSpecs, errors, /*allowTrailingRecursion*/ false); + if (excludeSpecs) { + excludeSpecs = validateSpecs(excludeSpecs, errors, /*allowTrailingRecursion*/ true); + } + + for (const file of host.readDirectory(basePath, supportedExtensions, excludeSpecs, includeSpecs)) { + // If we have already included a literal or wildcard path with a + // higher priority extension, we should skip this file. + // + // This handles cases where we may encounter both .ts and + // .d.ts (or .js if "allowJs" is enabled) in the same + // directory when they are compilation outputs. + if (hasFileWithHigherPriorityExtension(file, literalFileMap, wildcardFileMap, supportedExtensions, keyMapper)) { continue; } - const path = toPath(fileName, basePath, caseSensitiveKeyMapper); + // We may have included a wildcard path with a lower priority + // extension due to the user-defined order of entries in the + // "include" array. If there is a lower priority extension in the + // same directory, we should remove it. + removeWildcardFilesWithLowerPriorityExtension(file, wildcardFileMap, supportedExtensions, keyMapper); - // If we have excluded this path, we should skip the file. - if (isExcludedPath(path, excludePattern)) { - continue; + const key = keyMapper(file); + if (!hasProperty(literalFileMap, key) && !hasProperty(wildcardFileMap, key)) { + wildcardFileMap[key] = file; } - - // This wildcard has no further directory to process, so include the file. - includeFile(path, context, wildcardHasExtension); } } - else { - const directoryNames = readDirectory(basePath, FileSystemEntryKind.Directory, context); - for (const directoryName of directoryNames) { - if (pattern.test(directoryName)) { - const newBasePath = toPath(directoryName, basePath, caseSensitiveKeyMapper); - // Expand the entries in this directory. - if (expandFileSpec(fileSpec, newBasePath, offset + 1, context, isExpandingRecursiveDirectory) === ExpansionState.Error) { - return ExpansionState.Error; - } - } - } - } - - return ExpansionState.Ok; + const literalFiles = reduceProperties(literalFileMap, addFileToOutput, []); + const wildcardFiles = reduceProperties(wildcardFileMap, addFileToOutput, []); + wildcardFiles.sort(host.useCaseSensitiveFileNames ? compareStrings : compareStringsCaseInsensitive); + return { + fileNames: literalFiles.concat(wildcardFiles), + wildcardDirectories + }; } - /** - * Expands a `**` recursive directory wildcard. - * - * @param fileSpec The original file specification. - * @param basePath The directory to recursively expand. - * @param start The starting offset in the file specification. - * @param context The expansion context. - */ - function expandRecursiveDirectory(fileSpec: string, basePath: Path, start: number, context: ExpansionContext): ExpansionState { - // Skip the directory if it is excluded. - if (isExcludedPath(basePath, context.excludePattern)) { - return ExpansionState.Ok; - } - - // Expand the non-recursive part of the file specification against the prefix path. - if (expandFileSpec(fileSpec, basePath, start, context, /*isExpandingRecursiveDirectory*/ true) === ExpansionState.Error) { - return ExpansionState.Error; - } - - // Recursively expand each subdirectory. - const directoryNames = readDirectory(basePath, FileSystemEntryKind.Directory, context); - for (const directoryName of directoryNames) { - const newBasePath = toPath(directoryName, basePath, caseSensitiveKeyMapper); - if (expandRecursiveDirectory(fileSpec, newBasePath, start, context) === ExpansionState.Error) { - return ExpansionState.Error; + function validateSpecs(specs: string[], errors: Diagnostic[], allowTrailingRecursion: boolean) { + const validSpecs: string[] = []; + for (const spec of specs) { + if (!allowTrailingRecursion && invalidTrailingRecursionPattern.test(spec)) { + errors.push(createCompilerDiagnostic(Diagnostics.File_specification_cannot_contain_multiple_recursive_directory_wildcards_Asterisk_Asterisk_Colon_0, spec)); } - } - - return ExpansionState.Ok; - } - - /** - * Attempts to include a file in a file set. - * - * @param file The file to include. - * @param context The expansion context. - * @param wildcardHasExtension A value indicating whether the wildcard supplied an explicit extension. - */ - function includeFile(file: Path, context: ExpansionContext, wildcardHasExtension: boolean): void { - const { options, literalFiles, wildcardFiles, excludePattern, supportedExtensions } = context; - - // Ignore the file if it does not have a supported extension. - if ((!wildcardHasExtension || !options.allowNonTsExtensions) && !isSupportedSourceFileName(file, options)) { - return; - } - - // If we have already included a literal or wildcard path with a - // higher priority extension, we should skip this file. - // - // This handles cases where we may encounter both .ts and - // .d.ts (or .js if "allowJs" is enabled) in the same - // directory when they are compilation outputs. - const extensionPriority = getExtensionPriority(file, supportedExtensions); - if (hasFileWithHigherPriorityExtension(file, extensionPriority, context)) { - return; - } - - // We may have included a wildcard path with a lower priority - // extension due to the user-defined order of entries in the - // "include" array. If there is a lower priority extension in the - // same directory, we should remove it. - removeWildcardFilesWithLowerPriorityExtension(file, extensionPriority, context); - - if (!literalFiles.contains(file) && !wildcardFiles.contains(file)) { - wildcardFiles.set(file, file); - } - } - - /** - * Tests whether a path exists and is a specific kind of item. Results are - * cached for performance. - * - * @param path The path to tests. - * @param kind The kind of file system entry to find. - * @param context The expansion context. - */ - function pathExists(path: Path, kind: FileSystemEntryKind, context: ExpansionContext) { - const { cache, host } = context; - const entry = cache.get(path); - if (entry === false) { - // If the entry is strictly `false` then the path doesn`t exist, regardless of its kind. - return false; - } - else if (entry === true) { - // If the entry is strictly `true` then a file exists at this path. - return kind === FileSystemEntryKind.File; - } - else if (typeof entry === "object") { - // If the entry is an object, then a directory exists at this path. - return kind === FileSystemEntryKind.Directory; - } - else { - // The entry does not exist in the cache, so we need to check the host. - if (kind === FileSystemEntryKind.File) { - const result = host.fileExists(path); - cache.set(path, result); - return result; + else if (invalidMultipleRecursionPatterns.test(spec)) { + errors.push(createCompilerDiagnostic(Diagnostics.File_specification_cannot_end_in_a_recursive_directory_wildcard_Asterisk_Asterisk_Colon_0, spec)); } - else if (kind === FileSystemEntryKind.Directory) { - const result = host.directoryExists(path); - cache.set(path, result ? {} : false); - return result; + else { + validSpecs.push(spec); } } - return false; + return validSpecs; } + const watchRecursivePattern = /\/[^/]*?[*?][^/]*\//; + const wildcardDirectoryPattern = /^[^*?]*(?=\/[^/]*[*?])/; + /** - * Reads the contents of a directory for a specific kind of item. Results are - * cached for performance. - * - * @param basePath The path to the directory. The path must already exist. - * @param kind The kind of file system entry to find. - * @param context The expansion context. + * Gets directories in a set of include patterns that should be watched for changes. */ - function readDirectory(basePath: Path, kind: FileSystemEntryKind, context: ExpansionContext) { - const { cache, host } = context; - - let entry = cache.get(basePath); - if (entry === undefined) { - entry = {}; - cache.set(basePath, entry); - } - - if (typeof entry === "object") { - if (kind === FileSystemEntryKind.File) { - if (entry.files === undefined) { - entry.files = host.readFileNames(basePath); + function getWildcardDirectories(includes: string[], path: string, useCaseSensitiveFileNames: boolean) { + // We watch a directory recursively if it contains a wildcard anywhere in a directory segment + // of the pattern: + // + // /a/b/**/d - Watch /a/b recursively to catch changes to any d in any subfolder recursively + // /a/b/*/d - Watch /a/b recursively to catch any d in any immediate subfolder, even if a new subfolder is added + // + // We watch a directory without recursion if it contains a wildcard in the file segment of + // the pattern: + // + // /a/b/* - Watch /a/b directly to catch any new file + // /a/b/a?z - Watch /a/b directly to catch any new file matching a?z + const wildcardDirectories: Map = {}; + if (includes !== undefined) { + const recursiveKeys: string[] = []; + for (const include of includes) { + const name = combinePaths(path, include); + const match = wildcardDirectoryPattern.exec(name); + if (match) { + const key = useCaseSensitiveFileNames ? match[0] : match[0].toLowerCase(); + const flags = watchRecursivePattern.test(name) ? WatchDirectoryFlags.Recursive : WatchDirectoryFlags.None; + const existingFlags = getProperty(wildcardDirectories, key); + if (existingFlags === undefined || existingFlags < flags) { + wildcardDirectories[key] = flags; + if (flags === WatchDirectoryFlags.Recursive) { + recursiveKeys.push(key); + } + } } - - return entry.files; } - else if (kind === FileSystemEntryKind.Directory) { - if (entry.directories === undefined) { - entry.directories = host.readDirectoryNames(basePath); - } - return entry.directories; + // Remove any subpaths under an existing recursively watched directory. + for (const key in wildcardDirectories) { + if (hasProperty(wildcardDirectories, key)) { + for (const recursiveKey in recursiveKeys) { + if (containsPath(recursiveKey, key, path, !useCaseSensitiveFileNames)) { + delete wildcardDirectories[key]; + } + } + } } } - return []; + return wildcardDirectories; } /** @@ -994,13 +750,13 @@ namespace ts { * @param extensionPriority The priority of the extension. * @param context The expansion context. */ - function hasFileWithHigherPriorityExtension(file: Path, extensionPriority: ExtensionPriority, context: ExpansionContext) { - const { literalFiles, wildcardFiles, supportedExtensions } = context; + function hasFileWithHigherPriorityExtension(file: string, literalFiles: Map, wildcardFiles: Map, extensions: string[], keyMapper: (value: string) => string) { + const extensionPriority = getExtensionPriority(file, extensions); const adjustedExtensionPriority = adjustExtensionPriority(extensionPriority); for (let i = ExtensionPriority.Highest; i < adjustedExtensionPriority; ++i) { - const higherPriorityExtension = supportedExtensions[i]; - const higherPriorityPath = changeExtension(file, higherPriorityExtension); - if (literalFiles.contains(higherPriorityPath) || wildcardFiles.contains(higherPriorityPath)) { + const higherPriorityExtension = extensions[i]; + const higherPriorityPath = keyMapper(changeExtension(file, higherPriorityExtension)); + if (hasProperty(literalFiles, higherPriorityPath) || hasProperty(wildcardFiles, higherPriorityPath)) { return true; } } @@ -1016,13 +772,13 @@ namespace ts { * @param extensionPriority The priority of the extension. * @param context The expansion context. */ - function removeWildcardFilesWithLowerPriorityExtension(file: Path, extensionPriority: ExtensionPriority, context: ExpansionContext) { - const { wildcardFiles, supportedExtensions } = context; + function removeWildcardFilesWithLowerPriorityExtension(file: string, wildcardFiles: Map, extensions: string[], keyMapper: (value: string) => string) { + const extensionPriority = getExtensionPriority(file, extensions); const nextExtensionPriority = getNextLowestExtensionPriority(extensionPriority); - for (let i = nextExtensionPriority; i < supportedExtensions.length; ++i) { - const lowerPriorityExtension = supportedExtensions[i]; - const lowerPriorityPath = changeExtension(file, lowerPriorityExtension); - wildcardFiles.remove(lowerPriorityPath); + for (let i = nextExtensionPriority; i < extensions.length; ++i) { + const lowerPriorityExtension = extensions[i]; + const lowerPriorityPath = keyMapper(changeExtension(file, lowerPriorityExtension)); + delete wildcardFiles[lowerPriorityPath]; } } @@ -1037,297 +793,6 @@ namespace ts { return output; } - /** - * Adds a watched directory to an output map. - * - * @param output The output map. - * @param flags The directory flags. - * @param directory The directory path. - */ - function addDirectoryToOutput(output: Map, flags: WatchDirectoryFlags, directory: string) { - output[directory] = flags; - return output; - } - - /** - * Determines whether a path should be excluded. - * - * @param path The path to test for exclusion. - * @param excludePattern A pattern used to exclude a file specification. - */ - function isExcludedPath(path: string, excludePattern: RegExp) { - return excludePattern ? excludePattern.test(path) : false; - } - - /** - * Determines whether a file segment contains a valid extension. - * - * @param fileSpec The file specification. - * @param segmentStart The offset to the start of the file segment in the specification. - */ - function fileSegmentHasExtension(fileSpec: string, segmentStart: number) { - // if the final path segment does not have a . token, the file does not have an extension. - if (fileSpec.indexOf(".", segmentStart) === -1) { - return false; - } - - // if the extension for the final path segment is (".*"), then the file does not have an extension. - if (fileExtensionIs(fileSpec, ".*")) { - return false; - } - - return true; - } - - /** - * Creates a regular expression from a glob-style wildcard. - * - * @param fileSpec The file specification. - * @param start The starting offset in the file specification. - * @param end The end offset in the file specification. - * @param host The host used to resolve files and directories. - */ - function createRegularExpressionFromWildcard(fileSpec: string, start: number, end: number, host: ParseConfigHost): RegExp { - const pattern = createPatternFromWildcard(fileSpec, start, end); - return new RegExp("^" + pattern + "$", host.useCaseSensitiveFileNames ? "" : "i"); - } - - /** - * Creates a pattern from a wildcard segment. - * - * @param fileSpec The file specification. - * @param start The starting offset in the file specification. - * @param end The end offset in the file specification. - */ - function createPatternFromWildcard(fileSpec: string, start: number, end: number): string { - let pattern = ""; - let offset = indexOfWildcard(fileSpec, start); - while (offset >= 0 && offset < end) { - if (offset > start) { - // Escape and append the non-wildcard portion to the regular expression. - pattern += escapeRegularExpressionText(fileSpec, start, offset); - } - - const charCode = fileSpec.charCodeAt(offset); - if (charCode === CharacterCodes.asterisk) { - // Append a multi-character (zero or more characters) pattern to the regular expression. - pattern += "[^/]*?"; - } - else if (charCode === CharacterCodes.question) { - // Append a single-character pattern to the regular expression. - pattern += "[^/]"; - } - - start = offset + 1; - offset = indexOfWildcard(fileSpec, start); - } - - // Escape and append any remaining non-wildcard portion. - if (start < end) { - pattern += escapeRegularExpressionText(fileSpec, start, end); - } - - return pattern; - } - - /** - * Creates a regular expression from a glob-style wildcard used to exclude a file. - * - * @param excludeSpecs The file specifications to exclude. - * @param basePath The prefix path. - * @param options Compiler options. - * @param host The host used to resolve files and directories. - * @param errors An array for diagnostic reporting. - */ - function createExcludeRegularExpression(excludeSpecs: string[], basePath: string, options: CompilerOptions, host: ParseConfigHost, errors: Diagnostic[]): RegExp { - // Ignore an empty exclusion list - if (!excludeSpecs || excludeSpecs.length === 0) { - return undefined; - } - - basePath = escapeRegularExpressionText(basePath, 0, basePath.length); - - let pattern = ""; - for (const excludeSpec of excludeSpecs) { - const excludePattern = createExcludePattern(excludeSpec, basePath, options, host, errors); - if (excludePattern) { - if (pattern.length > 0) { - pattern += "|"; - } - - pattern += "(" + excludePattern + ")"; - } - } - - if (pattern.length > 0) { - return new RegExp("^(" + pattern + ")($|/)", host.useCaseSensitiveFileNames ? "" : "i"); - } - - return undefined; - } - - /** - * Creates a pattern for used to exclude a file. - * - * @param excludeSpec The file specification to exclude. - * @param basePath The base path for the exclude pattern. - * @param options Compiler options. - * @param host The host used to resolve files and directories. - * @param errors An array for diagnostic reporting. - */ - function createExcludePattern(excludeSpec: string, basePath: string, options: CompilerOptions, host: ParseConfigHost, errors: Diagnostic[]): string { - if (!excludeSpec) { - return undefined; - } - - excludeSpec = normalizePath(excludeSpec); - excludeSpec = removeTrailingDirectorySeparator(excludeSpec); - - let pattern = isRootedDiskPath(excludeSpec) ? "" : basePath; - let hasRecursiveDirectoryWildcard = false; - let segmentStart = 0; - let segmentEnd = getEndOfPathSegment(excludeSpec, segmentStart); - while (segmentStart < segmentEnd) { - if (isRecursiveDirectoryWildcard(excludeSpec, segmentStart, segmentEnd)) { - if (hasRecursiveDirectoryWildcard) { - if (errors) { - errors.push(createCompilerDiagnostic(Diagnostics.File_specification_cannot_contain_multiple_recursive_directory_wildcards_Asterisk_Asterisk_Colon_0, excludeSpec)); - } - - return undefined; - } - - // As an optimization, if the recursive directory is the last - // wildcard, or is followed by only `*` or `*.ts`, don't add the - // remaining pattern and exit the loop. - if (canElideRecursiveDirectorySegment(excludeSpec, segmentEnd, options, host)) { - break; - } - - hasRecursiveDirectoryWildcard = true; - pattern += "(/.+)?"; - } - else { - if (pattern) { - pattern += directorySeparator; - } - - pattern += createPatternFromWildcard(excludeSpec, segmentStart, segmentEnd); - } - - segmentStart = segmentEnd + 1; - segmentEnd = getEndOfPathSegment(excludeSpec, segmentStart); - } - - return pattern; - } - - /** - * Determines whether a recursive directory segment can be elided when - * building a regular expression to exclude a path. - * - * @param excludeSpec The file specification used to exclude a path. - * @param segmentEnd The end position of the recursive directory segment. - * @param options Compiler options. - * @param host The host used to resolve files and directories. - */ - function canElideRecursiveDirectorySegment(excludeSpec: string, segmentEnd: number, options: CompilerOptions, host: ParseConfigHost) { - // If there are no segments after this segment, the pattern for this segment may be elided. - if (segmentEnd + 1 >= excludeSpec.length) { - return true; - } - - // If the following segment is a wildcard that may be elided, the pattern for this segment may be elided. - return canElideWildcardSegment(excludeSpec, segmentEnd + 1, options, host); - } - - /** - * Determines whether a wildcard segment can be elided when building a - * regular expression to exclude a path. - * - * @param excludeSpec The file specification used to exclude a path. - * @param segmentStart The starting position of the segment. - * @param options Compiler options. - * @param host The host used to resolve files and directories. - */ - function canElideWildcardSegment(excludeSpec: string, segmentStart: number, options: CompilerOptions, host: ParseConfigHost) { - const charCode = excludeSpec.charCodeAt(segmentStart); - if (charCode === CharacterCodes.asterisk) { - const end = excludeSpec.length; - - // If the segment consists only of `*`, we may elide this segment. - if (segmentStart + 1 === end) { - return true; - } - - // If the segment consists only of `*.ts`, and we do not allow - // any other extensions for source files, we may elide this segment. - if (!options.allowNonTsExtensions && !options.jsx && !options.allowJs && segmentStart + 4 === end) { - const segment = excludeSpec.substr(segmentStart); - return fileExtensionIs(host.useCaseSensitiveFileNames ? segment : segment.toLowerCase(), ".ts"); - } - } - return false; - } - - /** - * Escape regular expression reserved tokens. - * - * @param text The text to escape. - * @param start The starting offset in the string. - * @param end The ending offset in the string. - */ - function escapeRegularExpressionText(text: string, start: number, end: number) { - return text.substring(start, end).replace(reservedCharacterPattern, "\\$&"); - } - - /** - * Determines whether the wildcard at the current offset is a recursive directory wildcard. - * - * @param fileSpec The file specification. - * @param segmentStart The starting offset of a segment in the file specification. - * @param segmentEnd The ending offset of a segment in the file specification. - */ - function isRecursiveDirectoryWildcard(fileSpec: string, segmentStart: number, segmentEnd: number) { - return segmentEnd - segmentStart === 2 && - fileSpec.charCodeAt(segmentStart) === CharacterCodes.asterisk && - fileSpec.charCodeAt(segmentStart + 1) === CharacterCodes.asterisk; - } - - /** - * Gets the index of the next wildcard character in a file specification. - * - * @param fileSpec The file specification. - * @param start The starting offset in the file specification. - */ - function indexOfWildcard(fileSpec: string, start: number, end: number = fileSpec.length): number { - for (let i = start; i < end; ++i) { - const ch = fileSpec.charCodeAt(i); - if (ch === CharacterCodes.asterisk || ch === CharacterCodes.question) { - return i; - } - } - - return -1; - } - - /** - * Get the end position of a path segment, either the index of the next directory separator or - * the provided end position. - * - * @param fileSpec The file specification. - * @param segmentStart The start offset in the file specification. - */ - function getEndOfPathSegment(fileSpec: string, segmentStart: number): number { - const end = fileSpec.length; - if (segmentStart >= end) { - return end; - } - - const offset = fileSpec.indexOf(directorySeparator, segmentStart); - return offset < 0 ? end : offset; - } - /** * Gets a case sensitive key. * diff --git a/src/compiler/core.ts b/src/compiler/core.ts index 3e6981996e199..465d4d229d88a 100644 --- a/src/compiler/core.ts +++ b/src/compiler/core.ts @@ -25,9 +25,7 @@ namespace ts { contains, remove, forEachValue: forEachValueInMap, - reduceProperties: reducePropertiesInMap, clear, - mergeFrom }; function forEachValueInMap(f: (key: Path, value: T) => void) { @@ -36,10 +34,6 @@ namespace ts { } } - function reducePropertiesInMap(callback: (memo: U, value: T, key: Path) => U, initial: U) { - return reduceProperties(files, callback, initial); - } - // path should already be well-formed so it does not need to be normalized function get(path: Path): T { return files[toKey(path)]; @@ -62,16 +56,6 @@ namespace ts { files = {}; } - function mergeFrom(other: FileMap) { - other.forEachValue(mergeFromOther); - } - - function mergeFromOther(key: Path, value: T) { - if (!contains(key)) { - set(key, value); - } - } - function toKey(path: Path): string { return keyMapper ? keyMapper(path) : path; } @@ -131,6 +115,15 @@ namespace ts { return -1; } + export function indexOfAnyCharCode(text: string, charCodes: number[], start?: number): number { + for (let i = start || 0, len = text.length; i < len; ++i) { + if (contains(charCodes, text.charCodeAt(i))) { + return i; + } + } + return -1; + } + export function countWhere(array: T[], predicate: (x: T) => boolean): number { let count = 0; if (array) { @@ -524,6 +517,10 @@ namespace ts { return a < b ? Comparison.LessThan : Comparison.GreaterThan; } + export function compareStringsCaseInsensitive(a: string, b: string) { + return compareStrings(a, b, /*ignoreCase*/ true); + } + function getDiagnosticFileName(diagnostic: Diagnostic): string { return diagnostic.file ? diagnostic.file.fileName : undefined; } @@ -861,6 +858,180 @@ namespace ts { return pathLen > extLen && path.substr(pathLen - extLen, extLen) === extension; } + export function fileExtensionIsAny(path: string, extensions: string[]): boolean { + for (const extension of extensions) { + if (fileExtensionIs(path, extension)) { + return true; + } + } + + return false; + } + + + // Reserved characters, forces escaping of any non-word (or digit), non-whitespace character. + // It may be inefficient (we could just match (/[-[\]{}()*+?.,\\^$|#\s]/g), but this is future + // proof. + const reservedCharacterPattern = /[^\w\s\/]/g; + const wildcardCharCodes = [CharacterCodes.asterisk, CharacterCodes.question]; + + export function getRegularExpressionForWildcard(specs: string[], basePath: string, usage: "files" | "directories" | "exclude", useCaseSensitiveFileNames: boolean) { + if (specs === undefined || specs.length === 0) { + return undefined; + } + + let pattern = ""; + let hasWrittenSubpattern = false; + spec: for (const spec of specs) { + if (!spec) { + continue; + } + + let subpattern = ""; + let hasRecursiveDirectoryWildcard = false; + let hasWrittenComponent = false; + const components = getNormalizedPathComponents(spec, basePath); + if (usage !== "exclude" && components[components.length - 1] === "**") { + continue spec; + } + + // getNormalizedPathComponents includes the separator for the root component. + // We need to remove to create our regex correctly. + components[0] = removeTrailingDirectorySeparator(components[0]); + + let optionalCount = 0; + for (const component of components) { + if (component === "**") { + if (hasRecursiveDirectoryWildcard) { + continue spec; + } + + subpattern += "(/.+?)?"; + hasRecursiveDirectoryWildcard = true; + hasWrittenComponent = true; + } + else { + if (usage === "directories") { + subpattern += "("; + optionalCount++; + } + + if (hasWrittenComponent) { + subpattern += directorySeparator; + } + + subpattern += component.replace(reservedCharacterPattern, replaceWildcardCharacter); + hasWrittenComponent = true; + } + } + + while (optionalCount > 0) { + subpattern += ")?"; + optionalCount--; + } + + if (hasWrittenSubpattern) { + pattern += "|"; + } + + pattern += "(" + subpattern + ")"; + hasWrittenSubpattern = true; + } + + if (!pattern) { + return undefined; + } + + return new RegExp("^(" + pattern + (usage === "exclude" ? ")($|/)" : ")$"), useCaseSensitiveFileNames ? "" : "i"); + } + + function replaceWildcardCharacter(match: string) { + return match === "*" ? "[^/]*" : match === "?" ? "[^/]" : "\\" + match; + } + + export interface FileSystemEntries { + files: string[]; + directories: string[]; + } + + export function matchFiles(path: string, extensions: string[], excludes: string[], includes: string[], useCaseSensitiveFileNames: boolean, currentDirectory: string, getFileSystemEntries: (path: string) => FileSystemEntries): string[] { + path = normalizePath(path); + currentDirectory = normalizePath(currentDirectory); + const absolutePath = combinePaths(currentDirectory, path); + const includeFileRegex = getRegularExpressionForWildcard(includes, absolutePath, "files", useCaseSensitiveFileNames); + const includeDirectoryRegex = getRegularExpressionForWildcard(includes, absolutePath, "directories", useCaseSensitiveFileNames); + const excludeRegex = getRegularExpressionForWildcard(excludes, absolutePath, "exclude", useCaseSensitiveFileNames); + const result: string[] = []; + for (const basePath of getBasePaths(path, includes, useCaseSensitiveFileNames)) { + visitDirectory(basePath, combinePaths(currentDirectory, basePath)); + } + return result; + + function visitDirectory(path: string, absolutePath: string) { + const { files, directories } = getFileSystemEntries(path); + + for (const current of files) { + const name = combinePaths(path, current); + const absoluteName = combinePaths(absolutePath, current); + if ((!extensions || fileExtensionIsAny(name, extensions)) && + (!includeFileRegex || includeFileRegex.test(absoluteName)) && + (!excludeRegex || !excludeRegex.test(absoluteName))) { + result.push(name); + } + } + + for (const current of directories) { + const name = combinePaths(path, current); + const absoluteName = combinePaths(absolutePath, current); + if ((!includeDirectoryRegex || includeDirectoryRegex.test(absoluteName)) && + (!excludeRegex || !excludeRegex.test(absoluteName))) { + visitDirectory(name, absoluteName); + } + } + } + } + + /** + * Computes the unique non-wildcard base paths amongst the provided include patterns. + */ + function getBasePaths(path: string, includes: string[], useCaseSensitiveFileNames: boolean) { + // Storage for our results in the form of literal paths (e.g. the paths as written by the user). + const basePaths: string[] = [path]; + if (includes) { + // Storage for literal base paths amongst the include patterns. + const includeBasePaths: string[] = []; + for (const include of includes) { + if (isRootedDiskPath(include)) { + const wildcardOffset = indexOfAnyCharCode(include, wildcardCharCodes); + const includeBasePath = wildcardOffset < 0 + ? removeTrailingDirectorySeparator(getDirectoryPath(include)) + : include.substring(0, include.lastIndexOf(directorySeparator, wildcardOffset)); + + // Append the literal and canonical candidate base paths. + includeBasePaths.push(includeBasePath); + } + } + + // Sort the offsets array using either the literal or canonical path representations. + includeBasePaths.sort(useCaseSensitiveFileNames ? compareStrings : compareStringsCaseInsensitive); + + // Iterate over each include base path and include unique base paths that are not a + // subpath of an existing base path + include: for (let i = 0; i < includeBasePaths.length; ++i) { + const includeBasePath = includeBasePaths[i]; + for (let j = 0; j < basePaths.length; ++j) { + if (containsPath(basePaths[j], includeBasePath, path, !useCaseSensitiveFileNames)) { + continue include; + } + } + + basePaths.push(includeBasePath); + } + } + + return basePaths; + } + /** * List of supported extensions in order of file resolution precedence. */ diff --git a/src/compiler/sys.ts b/src/compiler/sys.ts index 5ff8dbf679661..cad64adb4cf52 100644 --- a/src/compiler/sys.ts +++ b/src/compiler/sys.ts @@ -16,9 +16,7 @@ namespace ts { createDirectory(path: string): void; getExecutingFilePath(): string; getCurrentDirectory(): string; - readDirectory(path: string, extension?: string, exclude?: string[]): string[]; - readFileNames(path: string): string[]; - readDirectoryNames(path: string): string[]; + readDirectory(path: string, extensions?: string[], exclude?: string[], include?: string[]): string[]; getMemoryUsage?(): number; exit(exitCode?: number): void; } @@ -61,9 +59,7 @@ namespace ts { resolvePath(path: string): string; readFile(path: string): string; writeFile(path: string, contents: string): void; - readDirectory(path: string, extension?: string, exclude?: string[]): string[]; - readDirectoryNames(path: string): string[]; - readFileNames(path: string): string[]; + readDirectory(path: string, extensions?: string[], exclude?: string[], include?: string[]): string[]; }; export var sys: System = (function () { @@ -71,6 +67,7 @@ namespace ts { function getWScriptSystem(): System { const fso = new ActiveXObject("Scripting.FileSystemObject"); + const shell = new ActiveXObject("WScript.Shell"); const fileStream = new ActiveXObject("ADODB.Stream"); fileStream.Type = 2 /*text*/; @@ -150,38 +147,20 @@ namespace ts { return result.sort(); } - 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) { + function getAccessibleFileSystemEntries(path: string): FileSystemEntries { + try { const folder = fso.GetFolder(path || "."); const files = getNames(folder.files); - for (const current of files) { - const name = combinePaths(path, current); - if ((!extension || fileExtensionIs(name, extension)) && !contains(exclude, getCanonicalPath(name))) { - result.push(name); - } - } - const subfolders = getNames(folder.subfolders); - for (const current of subfolders) { - const name = combinePaths(path, current); - if (!contains(exclude, getCanonicalPath(name))) { - visitDirectory(name); - } - } + const directories = getNames(folder.subfolders); + return { files, directories }; + } + catch (e) { + return { files: [], directories: [] }; } } - function readFileNames(path: string): string[] { - const folder = fso.GetFolder(path || "."); - return getNames(folder.files); - } - - function readDirectoryNames(path: string): string[] { - const folder = fso.GetFolder(path || "."); - return getNames(folder.directories); + function readDirectory(path: string, extensions?: string[], excludes?: string[], includes?: string[]): string[] { + return matchFiles(path, extensions, excludes, includes, /*useCaseSensitiveFileNames*/ false, shell.CurrentDirectory, getAccessibleFileSystemEntries); } return { @@ -211,11 +190,9 @@ namespace ts { return WScript.ScriptFullName; }, getCurrentDirectory() { - return new ActiveXObject("WScript.Shell").CurrentDirectory; + return shell.CurrentDirectory; }, readDirectory, - readFileNames, - readDirectoryNames, exit(exitCode?: number): void { try { WScript.Quit(exitCode); @@ -385,56 +362,30 @@ namespace ts { return useCaseSensitiveFileNames ? path.toLowerCase() : path; } - 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(); + function getAccessibleFileSystemEntries(path: string): FileSystemEntries { + try { + const entries = _fs.readdirSync(path || ".").sort(); + const files: string[] = []; const directories: string[] = []; - for (const current of files) { - const name = combinePaths(path, current); - if (!contains(exclude, getCanonicalPath(name))) { - const stat = _fs.statSync(name); - if (stat.isFile()) { - if (!extension || fileExtensionIs(name, extension)) { - result.push(name); - } - } - else if (stat.isDirectory()) { - directories.push(name); - } + for (const entry of entries) { + const name = combinePaths(path, entry); + const stat = _fs.statSync(name); + if (stat.isFile()) { + files.push(entry); + } + else if (stat.isDirectory()) { + directories.push(entry); } } - for (const current of directories) { - visitDirectory(current); - } + return { files, directories }; } - } - - function readFileNames(path: string): string[] { - const entries = _fs.readdirSync(path || "."); - const files: string[] = []; - for (const entry of entries) { - const stat = _fs.statSync(combinePaths(path, entry)); - if (stat.isFile()) { - files.push(entry); - } + catch (e) { + return { files: [], directories: [] }; } - return files.sort(); } - function readDirectoryNames(path: string): string[] { - const entries = _fs.readdirSync(path || "."); - const directories: string[] = []; - for (const entry of entries) { - const stat = _fs.statSync(combinePaths(path, entry)); - if (stat.isDirectory()) { - directories.push(entry); - } - } - return directories.sort(); + function readDirectory(path: string, extensions?: string[], excludes?: string[], includes?: string[]): string[] { + return matchFiles(path, extensions, excludes, includes, useCaseSensitiveFileNames, process.cwd(), getAccessibleFileSystemEntries); } return { @@ -482,7 +433,7 @@ namespace ts { return _path.resolve(path); }, fileExists(path: string): boolean { - return _fs.existsSync(path) && _fs.statSync(path).isFile(); + return _fs.existsSync(path); }, directoryExists(path: string) { return _fs.existsSync(path) && _fs.statSync(path).isDirectory(); @@ -499,8 +450,6 @@ namespace ts { return process.cwd(); }, readDirectory, - readFileNames, - readDirectoryNames, getMemoryUsage() { if (global.gc) { global.gc(); @@ -539,8 +488,6 @@ namespace ts { getExecutingFilePath: () => ChakraHost.executingFile, getCurrentDirectory: () => ChakraHost.currentDirectory, readDirectory: ChakraHost.readDirectory, - readFileNames: ChakraHost.readFileNames, - readDirectoryNames: ChakraHost.readDirectoryNames, exit: ChakraHost.quit, }; } @@ -560,6 +507,4 @@ namespace ts { return undefined; // Unsupported host } })(); -} - - +} \ No newline at end of file diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 4c62904f4e843..3ddc0b447a94e 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -14,8 +14,6 @@ namespace ts { remove(fileName: Path): void; forEachValue(f: (key: Path, v: T) => void): void; - reduceProperties(f: (memo: U, value: T, key: Path) => U, initial: U): U; - mergeFrom(other: FileMap): void; clear(): void; } @@ -1586,31 +1584,13 @@ namespace ts { export interface ParseConfigHost { useCaseSensitiveFileNames: boolean; - readDirectory(rootDir: string, extension: string, exclude: string[]): string[]; + readDirectory(rootDir: string, extensions: string[], excludes: string[], includes: string[]): string[]; /** * Gets a value indicating whether the specified path exists and is a file. * @param path The path to test. */ fileExists(path: string): boolean; - - /** - * Gets a value indicating whether the specified path exists and is a directory. - * @param path The path to test. - */ - directoryExists(path: string): boolean; - - /** - * Reads the files names in the directory. - * @param rootDir The directory path. - */ - readFileNames(rootDir: string): string[]; - - /** - * Reads the directory names in the directory. - * @param rootDir The directory path. - */ - readDirectoryNames(rootDir: string): string[]; } export interface WriteFileCallback { diff --git a/src/harness/harness.ts b/src/harness/harness.ts index d8ccc512343c5..509c193666f29 100644 --- a/src/harness/harness.ts +++ b/src/harness/harness.ts @@ -23,6 +23,7 @@ /// /// /// +/// /* tslint:disable:no-null */ // Block scoped definitions work poorly for global variables, temporarily enable var @@ -435,9 +436,7 @@ namespace Harness { args(): string[]; getExecutingFilePath(): string; exit(exitCode?: number): void; - readDirectory(path: string, extension?: string, exclude?: string[]): string[]; - readDirectoryNames(path: string): string[]; - readFileNames(path: string): string[]; + readDirectory(path: string, extension?: string[], exclude?: string[], include?: string[]): string[]; } export var IO: IO; @@ -475,9 +474,7 @@ namespace Harness { export const directoryExists: typeof IO.directoryExists = fso.FolderExists; export const fileExists: typeof IO.fileExists = fso.FileExists; export const log: typeof IO.log = global.WScript && global.WScript.StdOut.WriteLine; - export const readDirectory: typeof IO.readDirectory = (path, extension, exclude) => ts.sys.readDirectory(path, extension, exclude); - export const readDirectoryNames: typeof IO.readDirectoryNames = path => ts.sys.readDirectoryNames(path); - export const readFileNames: typeof IO.readFileNames = path => ts.sys.readFileNames(path); + export const readDirectory: typeof IO.readDirectory = (path, extension, exclude, include) => ts.sys.readDirectory(path, extension, exclude, include); export function createDirectory(path: string) { if (directoryExists(path)) { @@ -547,9 +544,7 @@ namespace Harness { export const fileExists: typeof IO.fileExists = fs.existsSync; export const log: typeof IO.log = s => console.log(s); - export const readDirectory: typeof IO.readDirectory = (path, extension, exclude) => ts.sys.readDirectory(path, extension, exclude); - export const readDirectoryNames: typeof IO.readDirectoryNames = path => ts.sys.readDirectoryNames(path); - export const readFileNames: typeof IO.readFileNames = path => ts.sys.readFileNames(path); + export const readDirectory: typeof IO.readDirectory = (path, extension, exclude, include) => ts.sys.readDirectory(path, extension, exclude, include); export function createDirectory(path: string) { if (!directoryExists(path)) { @@ -755,16 +750,22 @@ namespace Harness { Http.writeToServerSync(serverRoot + path, "WRITE", contents); } - export function readDirectory(path: string, extension?: string, exclude?: string[]) { - return listFiles(path).filter(f => !extension || ts.fileExtensionIs(f, extension)); - } - - export function readDirectoryNames(path: string): string[] { - return []; - } - - export function readFileNames(path: string) { - return readDirectory(path); + export function readDirectory(path: string, extension?: string[], exclude?: string[], include?: string[]) { + const fs = new Utils.VirtualFileSystem(path, useCaseSensitiveFileNames()); + for (const file in listFiles(path)) { + fs.addFile(file); + } + return ts.matchFiles(path, extension, exclude, include, useCaseSensitiveFileNames(), getCurrentDirectory(), path => { + const entry = fs.traversePath(path); + if (entry && entry.isDirectory()) { + const directory = entry; + return { + files: ts.map(directory.getFiles(), f => f.name), + directories: ts.map(directory.getDirectories(), d => d.name) + }; + } + return { files: [], directories: [] }; + }); } } } diff --git a/src/harness/harnessLanguageService.ts b/src/harness/harnessLanguageService.ts index 521d17cfc8fce..4db96f344dfc7 100644 --- a/src/harness/harnessLanguageService.ts +++ b/src/harness/harnessLanguageService.ts @@ -575,18 +575,10 @@ namespace Harness.LanguageService { return this.host.getCurrentDirectory(); } - readDirectory(path: string, extension?: string): string[] { + readDirectory(path: string, extension?: string[], exclude?: string[], include?: string[]): string[] { throw new Error("Not implemented Yet."); } - readDirectoryNames(path: string): string[] { - throw new Error("Not implemented."); - } - - readFileNames(path: string): string[] { - throw new Error("Not implemented."); - } - watchFile(fileName: string, callback: (fileName: string) => void): ts.FileWatcher { return { close() { } }; } diff --git a/src/harness/loggedIO.ts b/src/harness/loggedIO.ts index 0bae91f7976fc..a6df6382bd0c4 100644 --- a/src/harness/loggedIO.ts +++ b/src/harness/loggedIO.ts @@ -62,8 +62,9 @@ interface IOLog { }[]; directoriesRead: { path: string, - extension: string, + extension: string[], exclude: string[], + include: string[], result: string[] }[]; } @@ -217,9 +218,9 @@ namespace Playback { memoize(path => findResultByPath(wrapper, replayLog.filesRead, path).contents)); wrapper.readDirectory = recordReplay(wrapper.readDirectory, underlying)( - (path, extension, exclude) => { - const result = (underlying).readDirectory(path, extension, exclude); - const logEntry = { path, extension, exclude, result }; + (path, extension, exclude, include) => { + const result = (underlying).readDirectory(path, extension, exclude, include); + const logEntry = { path, extension, exclude, include, result }; recordLog.directoriesRead.push(logEntry); return result; }, diff --git a/src/harness/projectsRunner.ts b/src/harness/projectsRunner.ts index 76c7952428a4b..bff391cf9af3a 100644 --- a/src/harness/projectsRunner.ts +++ b/src/harness/projectsRunner.ts @@ -213,10 +213,7 @@ class ProjectRunner extends RunnerBase { const configParseHost: ts.ParseConfigHost = { useCaseSensitiveFileNames: Harness.IO.useCaseSensitiveFileNames(), fileExists, - directoryExists, readDirectory, - readDirectoryNames, - readFileNames }; const configParseResult = ts.parseJsonConfigFileContent(configObject, configParseHost, ts.getDirectoryPath(configFileName), compilerOptions); if (configParseResult.errors.length > 0) { @@ -276,8 +273,8 @@ class ProjectRunner extends RunnerBase { : ts.normalizeSlashes(testCase.projectRoot) + "/" + ts.normalizeSlashes(fileName); } - function readDirectory(rootDir: string, extension: string, exclude: string[]): string[] { - const harnessReadDirectoryResult = Harness.IO.readDirectory(getFileNameInTheProjectTest(rootDir), extension, exclude); + function readDirectory(rootDir: string, extension: string[], exclude: string[], include: string[]): string[] { + const harnessReadDirectoryResult = Harness.IO.readDirectory(getFileNameInTheProjectTest(rootDir), extension, exclude, include); const result: string[] = []; for (let i = 0; i < harnessReadDirectoryResult.length; i++) { result[i] = ts.getRelativePathToDirectoryOrUrl(testCase.projectRoot, harnessReadDirectoryResult[i], @@ -286,14 +283,6 @@ class ProjectRunner extends RunnerBase { return result; } - function readDirectoryNames(path: string) { - return Harness.IO.readDirectoryNames(getFileNameInTheProjectTest(path)); - } - - function readFileNames(path: string) { - return Harness.IO.readFileNames(getFileNameInTheProjectTest(path)); - } - function fileExists(fileName: string): boolean { return Harness.IO.fileExists(getFileNameInTheProjectTest(fileName)); } diff --git a/src/harness/rwcRunner.ts b/src/harness/rwcRunner.ts index 826f6b6726068..791c684c79c16 100644 --- a/src/harness/rwcRunner.ts +++ b/src/harness/rwcRunner.ts @@ -79,10 +79,7 @@ namespace RWC { const configParseHost: ts.ParseConfigHost = { useCaseSensitiveFileNames: Harness.IO.useCaseSensitiveFileNames(), fileExists: Harness.IO.fileExists, - directoryExists: Harness.IO.directoryExists, readDirectory: Harness.IO.readDirectory, - readDirectoryNames: Harness.IO.readDirectoryNames, - readFileNames: Harness.IO.readFileNames, }; const configParseResult = ts.parseJsonConfigFileContent(parsedTsconfigFileContents.config, configParseHost, ts.getDirectoryPath(tsconfigFile.path)); fileNames = configParseResult.fileNames; diff --git a/src/harness/vfs.ts b/src/harness/vfs.ts new file mode 100644 index 0000000000000..66bbd715fbc60 --- /dev/null +++ b/src/harness/vfs.ts @@ -0,0 +1,160 @@ +/// +namespace Utils { + export class VirtualFileSystemEntry { + fileSystem: VirtualFileSystem; + name: string; + + constructor(fileSystem: VirtualFileSystem, name: string) { + this.fileSystem = fileSystem; + this.name = name; + } + + isDirectory() { return false; } + isFile() { return false; } + isFileSystem() { return false; } + } + + export class VirtualFile extends VirtualFileSystemEntry { + content: string; + isFile() { return true; } + } + + export abstract class VirtualFileSystemContainer extends VirtualFileSystemEntry { + abstract getFileSystemEntries(): VirtualFileSystemEntry[]; + + getFileSystemEntry(name: string): VirtualFileSystemEntry { + for (const entry of this.getFileSystemEntries()) { + if (this.fileSystem.sameName(entry.name, name)) { + return entry; + } + } + return undefined; + } + + getDirectories(): VirtualDirectory[] { + return ts.filter(this.getFileSystemEntries(), entry => entry.isDirectory()); + } + + getFiles(): VirtualFile[] { + return ts.filter(this.getFileSystemEntries(), entry => entry.isFile()); + } + + getDirectory(name: string): VirtualDirectory { + const entry = this.getFileSystemEntry(name); + return entry.isDirectory() ? entry : undefined; + } + + getFile(name: string): VirtualFile { + const entry = this.getFileSystemEntry(name); + return entry.isFile() ? entry : undefined; + } + } + + export class VirtualDirectory extends VirtualFileSystemContainer { + private entries: VirtualFileSystemEntry[] = []; + + isDirectory() { return true; } + + getFileSystemEntries() { return this.entries.slice(); } + + addDirectory(name: string): VirtualDirectory { + const entry = this.getFileSystemEntry(name); + if (entry === undefined) { + const directory = new VirtualDirectory(this.fileSystem, name); + this.entries.push(directory); + return directory; + } + else if (entry.isDirectory()) { + return entry; + } + else { + return undefined; + } + } + + addFile(name: string, content?: string): VirtualFile { + const entry = this.getFileSystemEntry(name); + if (entry === undefined) { + const file = new VirtualFile(this.fileSystem, name); + file.content = content; + this.entries.push(file); + return file; + } + else if (entry.isFile()) { + const file = entry; + file.content = content; + return file; + } + else { + return undefined; + } + } + } + + export class VirtualFileSystem extends VirtualFileSystemContainer { + private root: VirtualDirectory; + + currentDirectory: string; + useCaseSensitiveFileNames: boolean; + + constructor(currentDirectory: string, useCaseSensitiveFileNames: boolean) { + super(undefined, ""); + this.fileSystem = this; + this.root = new VirtualDirectory(this, ""); + this.currentDirectory = currentDirectory; + this.useCaseSensitiveFileNames = useCaseSensitiveFileNames; + } + + isFileSystem() { return true; } + + getFileSystemEntries() { return this.root.getFileSystemEntries(); } + + addDirectory(path: string) { + const components = ts.getNormalizedPathComponents(path, this.currentDirectory); + let directory: VirtualDirectory = this.root; + for (const component of components) { + directory = directory.addDirectory(component); + if (directory === undefined) { + break; + } + } + + return directory; + } + + addFile(path: string, content?: string) { + const absolutePath = ts.getNormalizedAbsolutePath(path, this.currentDirectory); + const fileName = ts.getBaseFileName(path); + const directoryPath = ts.getDirectoryPath(absolutePath); + const directory = this.addDirectory(directoryPath); + return directory ? directory.addFile(fileName, content) : undefined; + } + + fileExists(path: string) { + const entry = this.traversePath(path); + return entry !== undefined && entry.isFile(); + } + + sameName(a: string, b: string) { + return this.useCaseSensitiveFileNames ? a === b : a.toLowerCase() === b.toLowerCase(); + } + + traversePath(path: string) { + let directory: VirtualDirectory = this.root; + for (const component of ts.getNormalizedPathComponents(path, this.currentDirectory)) { + const entry = directory.getFileSystemEntry(component); + if (entry === undefined) { + return undefined; + } + else if (entry.isDirectory()) { + directory = entry; + } + else { + return entry; + } + } + + return directory; + } + } +} \ No newline at end of file diff --git a/src/services/shims.ts b/src/services/shims.ts index a7f85d2445230..cf6f3885a2c3a 100644 --- a/src/services/shims.ts +++ b/src/services/shims.ts @@ -72,10 +72,7 @@ namespace ts { * @param exclude A JSON encoded string[] containing the paths to exclude * when enumerating the directory. */ - readDirectory(rootDir: string, extension: string, exclude?: string): string; - readDirectoryNames?(rootDir: string): string; - readFileNames?(rootDir: string): string; - directoryExists?(path: string): boolean; + readDirectory(rootDir: string, extension: string, exclude?: string, include?: string): string; useCaseSensitiveFileNames?: boolean; } @@ -425,70 +422,43 @@ namespace ts { } } - public readDirectory(rootDir: string, extension: string, exclude: string[]): string[] { - // Wrap the API changes for 1.5 release. This try/catch - // should be removed once TypeScript 1.5 has shipped. + public readDirectory(rootDir: string, extensions: string[], exclude: string[], include: string[]): string[] { + // Wrap the API changes for 1.8 release. This try/catch + // should be removed once TypeScript 1.8 has shipped. // Also consider removing the optional designation for // the exclude param at this time. - var encoded: string; try { - encoded = this.shimHost.readDirectory(rootDir, extension, JSON.stringify(exclude)); + return JSON.parse(this.shimHost.readDirectory( + rootDir, + JSON.stringify(extensions), + JSON.stringify(exclude), + JSON.stringify(include))); } catch (e) { - encoded = this.shimHost.readDirectory(rootDir, extension); - } - return JSON.parse(encoded); - } - - public readDirectoryNames(path: string): string[] { - if (this.shimHost.readDirectory) { - const encoded = this.shimHost.readDirectoryNames(path); - return JSON.parse(encoded); - } - - if (sys) { - path = normalizePath(path); - path = ensureTrailingDirectorySeparator(path); - return sys.readDirectoryNames(path); - } - - return []; - } - - public readFileNames(path: string): string[] { - if (this.shimHost.readFileNames) { - const encoded = this.shimHost.readFileNames(path); - return JSON.parse(encoded); - } - - if (sys) { - path = normalizePath(path); - path = ensureTrailingDirectorySeparator(path); - return sys.readFileNames(path); + let results: string[] = []; + for (const extension of extensions) { + for (const file of this.readDirectoryFallback(rootDir, extension, exclude)) + { + if (!contains(results, file)) { + results.push(file); + } + } + } + return results; } - - return []; } public fileExists(fileName: string): boolean { return this.shimHost.fileExists(fileName); } - public directoryExists(directoryName: string): boolean { - if (this.shimHost.directoryExists) { - return this.shimHost.directoryExists(directoryName); - } - - if (sys) { - return sys.directoryExists(directoryName); - } - - return false; - } - public readFile(fileName: string): string { return this.shimHost.readFile(fileName); } + + private readDirectoryFallback(rootDir: string, extension: string, exclude: string[]) { + return JSON.parse(this.shimHost.readDirectory(rootDir, extension, JSON.stringify(exclude))); + } } function simpleForwardCall(logger: Logger, actionDescription: string, action: () => any, logPerformance: boolean): any { diff --git a/tests/cases/unittests/cachingInServerLSHost.ts b/tests/cases/unittests/cachingInServerLSHost.ts index f0a138c27a196..0d2a9ebf54d40 100644 --- a/tests/cases/unittests/cachingInServerLSHost.ts +++ b/tests/cases/unittests/cachingInServerLSHost.ts @@ -36,11 +36,9 @@ module ts { getCurrentDirectory: (): string => { return ""; }, - readDirectory: (path: string, extension?: string, exclude?: string[]): string[] => { + readDirectory: (path: string, extension?: string[], exclude?: string[], include?: string[]): string[] => { throw new Error("NYI"); }, - readDirectoryNames: (path: string): string[] => { throw new Error("NYI"); }, - readFileNames: (path: string): string[] => { throw new Error("NYI"); }, exit: (exitCode?: number) => { }, watchFile: (path, callback) => { diff --git a/tests/cases/unittests/expandFiles.ts b/tests/cases/unittests/expandFiles.ts index 3aac938f178ec..02065407e27df 100644 --- a/tests/cases/unittests/expandFiles.ts +++ b/tests/cases/unittests/expandFiles.ts @@ -2,65 +2,91 @@ /// namespace ts { + class MockParseConfigHost extends Utils.VirtualFileSystem implements ParseConfigHost { + constructor(currentDirectory: string, ignoreCase: boolean, files: string[]) { + super(currentDirectory, ignoreCase); + for (const file of files) { + this.addFile(file); + } + } + + readDirectory(path: string, extensions: string[], excludes: string[], includes: string[]) { + return matchFiles(path, extensions, excludes, includes, this.useCaseSensitiveFileNames, this.currentDirectory, (path: string) => this.getAccessibleFileSystemEntries(path)); + } + + getAccessibleFileSystemEntries(path: string) { + const entry = this.traversePath(path); + if (entry && entry.isDirectory()) { + const directory = entry; + return { + files: map(directory.getFiles(), f => f.name), + directories: map(directory.getDirectories(), d => d.name) + }; + } + return { files: [], directories: [] }; + } + } + const caseInsensitiveBasePath = "c:/dev/"; - const caseInsensitiveHost = createMockParseConfigHost(/*ignoreCase*/ true, caseInsensitiveBasePath, [ - "a.ts", - "a.d.ts", - "a.js", - "b.ts", - "b.js", - "c.d.ts", - "z/a.ts", - "z/abz.ts", - "z/aba.ts", - "z/b.ts", - "z/bbz.ts", - "z/bba.ts", - "x/a.ts", - "x/aa.ts", - "x/b.ts", - "x/y/a.ts", - "x/y/b.ts", - "js/a.js", - "js/b.js", + const caseInsensitiveHost = new MockParseConfigHost(caseInsensitiveBasePath, /*useCaseSensitiveFileNames*/ false, [ + "c:/dev/a.ts", + "c:/dev/a.d.ts", + "c:/dev/a.js", + "c:/dev/b.ts", + "c:/dev/b.js", + "c:/dev/c.d.ts", + "c:/dev/z/a.ts", + "c:/dev/z/abz.ts", + "c:/dev/z/aba.ts", + "c:/dev/z/b.ts", + "c:/dev/z/bbz.ts", + "c:/dev/z/bba.ts", + "c:/dev/x/a.ts", + "c:/dev/x/aa.ts", + "c:/dev/x/b.ts", + "c:/dev/x/y/a.ts", + "c:/dev/x/y/b.ts", + "c:/dev/js/a.js", + "c:/dev/js/b.js", + "c:/ext/ext.ts" ]); const caseSensitiveBasePath = "/dev/"; - const caseSensitiveHost = createMockParseConfigHost(/*ignoreCase*/ false, caseSensitiveBasePath, [ - "a.ts", - "a.d.ts", - "a.js", - "b.ts", - "b.js", - "A.ts", - "B.ts", - "c.d.ts", - "z/a.ts", - "z/abz.ts", - "z/aba.ts", - "z/b.ts", - "z/bbz.ts", - "z/bba.ts", - "x/a.ts", - "x/b.ts", - "x/y/a.ts", - "x/y/b.ts", - "js/a.js", - "js/b.js", + const caseSensitiveHost = new MockParseConfigHost(caseSensitiveBasePath, /*useCaseSensitiveFileNames*/ true, [ + "/dev/a.ts", + "/dev/a.d.ts", + "/dev/a.js", + "/dev/b.ts", + "/dev/b.js", + "/dev/A.ts", + "/dev/B.ts", + "/dev/c.d.ts", + "/dev/z/a.ts", + "/dev/z/abz.ts", + "/dev/z/aba.ts", + "/dev/z/b.ts", + "/dev/z/bbz.ts", + "/dev/z/bba.ts", + "/dev/x/a.ts", + "/dev/x/b.ts", + "/dev/x/y/a.ts", + "/dev/x/y/b.ts", + "/dev/js/a.js", + "/dev/js/b.js", ]); - const caseInsensitiveMixedExtensionHost = createMockParseConfigHost(/*ignoreCase*/ true, caseInsensitiveBasePath, [ - "a.ts", - "a.d.ts", - "a.js", - "b.tsx", - "b.d.ts", - "b.jsx", - "c.tsx", - "c.js", - "d.js", - "e.jsx", - "f.other" + const caseInsensitiveMixedExtensionHost = new MockParseConfigHost(caseInsensitiveBasePath, /*useCaseSensitiveFileNames*/ false, [ + "c:/dev/a.ts", + "c:/dev/a.d.ts", + "c:/dev/a.js", + "c:/dev/b.tsx", + "c:/dev/b.d.ts", + "c:/dev/b.jsx", + "c:/dev/c.tsx", + "c:/dev/c.js", + "c:/dev/d.js", + "c:/dev/e.jsx", + "c:/dev/f.other" ]); describe("expandFiles", () => { @@ -226,7 +252,7 @@ namespace ts { const expected: ts.ExpandResult = { fileNames: [], wildcardDirectories: { - "c:/dev": ts.WatchDirectoryFlags.None + "c:/dev": ts.WatchDirectoryFlags.Recursive }, }; const actual = ts.expandFiles(/*fileNames*/ undefined, includeSpecs, /*excludeSpecs*/ undefined, caseInsensitiveBasePath, {}, caseInsensitiveHost); @@ -239,7 +265,7 @@ namespace ts { const expected: ts.ExpandResult = { fileNames: ["c:/dev/a.ts"], wildcardDirectories: { - "c:/dev": ts.WatchDirectoryFlags.None + "c:/dev": ts.WatchDirectoryFlags.Recursive }, }; const actual = ts.expandFiles(fileNames, includeSpecs, excludeSpecs, caseInsensitiveBasePath, {}, caseInsensitiveHost); @@ -283,6 +309,23 @@ namespace ts { const actual = ts.expandFiles(/*fileNames*/ undefined, includeSpecs, /*excludeSpecs*/ undefined, caseInsensitiveBasePath, { allowJs: true }, caseInsensitiveHost); assert.deepEqual(actual, expected); }); + it("include paths outside of the project", () => { + const includeSpecs = ["*", "c:/ext/*"]; + const expected: ts.ExpandResult = { + fileNames: [ + "c:/dev/a.ts", + "c:/dev/b.ts", + "c:/dev/c.d.ts", + "c:/ext/ext.ts", + ], + wildcardDirectories: { + "c:/dev": ts.WatchDirectoryFlags.None, + "c:/ext": ts.WatchDirectoryFlags.None + } + }; + const actual = ts.expandFiles(/*fileNames*/ undefined, includeSpecs, /*excludeSpecs*/ undefined, caseInsensitiveBasePath, {}, caseInsensitiveHost); + assert.deepEqual(actual, expected); + }); }); describe("when called from parseJsonConfigFileContent", () => { @@ -376,110 +419,4 @@ namespace ts { }); }); }); - - interface DirectoryEntry { - files: ts.Map; - directories: ts.Map; - } - - interface TestParseConfigHost extends ts.ParseConfigHost { - basePath: string; - } - - function createMockParseConfigHost(ignoreCase: boolean, basePath: string, files: string[]): TestParseConfigHost { - const fileSet: ts.Map = {}; - const directorySet: ts.Map = {}; - const emptyDirectory: DirectoryEntry = { files: {}, directories: {} }; - - files.sort((a, b) => ts.comparePaths(a, b, basePath, ignoreCase)); - for (const file of files) { - addFile(ts.getNormalizedAbsolutePath(file, basePath)); - } - - return { - useCaseSensitiveFileNames: !ignoreCase, - basePath, - fileExists, - directoryExists, - readDirectory, - readFileNames, - readDirectoryNames - }; - - function fileExists(path: string): boolean { - path = ts.getNormalizedAbsolutePath(path, basePath); - path = ts.removeTrailingDirectorySeparator(path); - const fileKey = ignoreCase ? path.toLowerCase() : path; - return ts.hasProperty(fileSet, fileKey); - } - - function directoryExists(path: string): boolean { - path = ts.getNormalizedAbsolutePath(path, basePath); - path = ts.removeTrailingDirectorySeparator(path); - const directoryKey = ignoreCase ? path.toLowerCase() : path; - return ts.hasProperty(directorySet, directoryKey); - } - - function readDirectory(rootDir: string, extension?: string, exclude?: string[]): string[] { - throw new Error("Not implemented"); - } - - function readFileNames(path: string) { - const { files } = getDirectoryEntry(path) || emptyDirectory; - const result: string[] = []; - ts.forEachKey(files, key => { result.push(key); }); - result.sort((a, b) => ts.compareStrings(a, b, ignoreCase)); - return result; - } - - function readDirectoryNames(path: string) { - const { directories } = getDirectoryEntry(path); // || emptyDirectory; - const result: string[] = []; - ts.forEachKey(directories, key => { result.push(key); }); - result.sort((a, b) => ts.compareStrings(a, b, ignoreCase)); - return result; - } - - function getDirectoryEntry(path: string) { - path = ts.getNormalizedAbsolutePath(path, basePath); - path = ts.removeTrailingDirectorySeparator(path); - const directoryKey = ignoreCase ? path.toLowerCase() : path; - return ts.getProperty(directorySet, directoryKey); - } - - function addFile(file: string) { - const fileKey = ignoreCase ? file.toLowerCase() : file; - if (!ts.hasProperty(fileSet, fileKey)) { - fileSet[fileKey] = file; - const name = ts.getBaseFileName(file); - const parent = ts.getDirectoryPath(file); - addToDirectory(parent, name, "file"); - } - } - - function addDirectory(directory: string) { - directory = ts.removeTrailingDirectorySeparator(directory); - const directoryKey = ignoreCase ? directory.toLowerCase() : directory; - if (!ts.hasProperty(directorySet, directoryKey)) { - directorySet[directoryKey] = { files: {}, directories: {} }; - const name = ts.getBaseFileName(directory); - const parent = ts.getDirectoryPath(directory); - if (parent !== directory) { - addToDirectory(parent, name, "directory"); - } - } - } - - function addToDirectory(directory: string, entry: string, type: "file" | "directory") { - addDirectory(directory); - directory = ts.removeTrailingDirectorySeparator(directory); - const directoryKey = ignoreCase ? directory.toLowerCase() : directory; - const entryKey = ignoreCase ? entry.toLowerCase() : entry; - const directoryEntry = directorySet[directoryKey]; - const entries = type === "file" ? directoryEntry.files : directoryEntry.directories; - if (!ts.hasProperty(entries, entryKey)) { - entries[entryKey] = entry; - } - } - } } \ No newline at end of file diff --git a/tests/cases/unittests/session.ts b/tests/cases/unittests/session.ts index 8e35baa391022..7c44b895ce051 100644 --- a/tests/cases/unittests/session.ts +++ b/tests/cases/unittests/session.ts @@ -16,8 +16,6 @@ namespace ts.server { getExecutingFilePath(): string { return void 0; }, getCurrentDirectory(): string { return void 0; }, readDirectory(): string[] { return []; }, - readDirectoryNames(): string[] { return []; }, - readFileNames(): string[] { return []; }, exit(): void {} }; const mockLogger: Logger = { From 94a5327e9a5209af798415f0e18dce48ac074668 Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Wed, 16 Dec 2015 13:42:17 -0800 Subject: [PATCH 07/84] more tests --- src/compiler/commandLineParser.ts | 44 ++- tests/cases/unittests/expandFiles.ts | 545 +++++++++++++++++++++------ 2 files changed, 454 insertions(+), 135 deletions(-) diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index c81b3b63244fc..a5cd7abc50522 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -531,7 +531,7 @@ namespace ts { includeSpecs = ["**/*"]; } - return expandFiles(fileNames, includeSpecs, excludeSpecs, basePath, options, host, errors); + return matchFileNames(fileNames, includeSpecs, excludeSpecs, basePath, options, host, errors); } } @@ -589,14 +589,14 @@ namespace ts { * Expands an array of file specifications. * * @param fileNames The literal file names to include. - * @param includeSpecs The file specifications to expand. - * @param excludeSpecs The file specifications to exclude. + * @param include The wildcard file specifications to include. + * @param exclude The wildcard file specifications to exclude. * @param basePath The base path for any relative file specifications. * @param options Compiler options. * @param host The host used to resolve files and directories. * @param errors An array for diagnostic reporting. */ - export function expandFiles(fileNames: string[], includeSpecs: string[], excludeSpecs: string[], basePath: string, options: CompilerOptions, host: ParseConfigHost, errors?: Diagnostic[]): ExpandResult { + function matchFileNames(fileNames: string[], include: string[], exclude: string[], basePath: string, options: CompilerOptions, host: ParseConfigHost, errors: Diagnostic[]): ExpandResult { basePath = normalizePath(basePath); basePath = removeTrailingDirectorySeparator(basePath); @@ -615,11 +615,19 @@ namespace ts { // via wildcard, and to handle extension priority. const wildcardFileMap: Map = {}; + if (include) { + include = validateSpecs(include, errors, /*allowTrailingRecursion*/ false); + } + + if (exclude) { + exclude = validateSpecs(exclude, errors, /*allowTrailingRecursion*/ true); + } + // Wildcard directories (provided as part of a wildcard path) are stored in a // file map that marks whether it was a regular wildcard match (with a `*` or `?` token), // or a recursive directory. This information is used by filesystem watchers to monitor for // new entries in these paths. - const wildcardDirectories: Map = getWildcardDirectories(includeSpecs, basePath, host.useCaseSensitiveFileNames); + const wildcardDirectories: Map = getWildcardDirectories(include, exclude, basePath, host.useCaseSensitiveFileNames); // Rather than requery this for each file and filespec, we query the supported extensions // once and store it on the expansion context. @@ -634,13 +642,8 @@ namespace ts { } } - if (includeSpecs) { - includeSpecs = validateSpecs(includeSpecs, errors, /*allowTrailingRecursion*/ false); - if (excludeSpecs) { - excludeSpecs = validateSpecs(excludeSpecs, errors, /*allowTrailingRecursion*/ true); - } - - for (const file of host.readDirectory(basePath, supportedExtensions, excludeSpecs, includeSpecs)) { + if (include && include.length > 0) { + for (const file of host.readDirectory(basePath, supportedExtensions, exclude, include)) { // If we have already included a literal or wildcard path with a // higher priority extension, we should skip this file. // @@ -677,10 +680,10 @@ namespace ts { const validSpecs: string[] = []; for (const spec of specs) { if (!allowTrailingRecursion && invalidTrailingRecursionPattern.test(spec)) { - errors.push(createCompilerDiagnostic(Diagnostics.File_specification_cannot_contain_multiple_recursive_directory_wildcards_Asterisk_Asterisk_Colon_0, spec)); + errors.push(createCompilerDiagnostic(Diagnostics.File_specification_cannot_end_in_a_recursive_directory_wildcard_Asterisk_Asterisk_Colon_0, spec)); } else if (invalidMultipleRecursionPatterns.test(spec)) { - errors.push(createCompilerDiagnostic(Diagnostics.File_specification_cannot_end_in_a_recursive_directory_wildcard_Asterisk_Asterisk_Colon_0, spec)); + errors.push(createCompilerDiagnostic(Diagnostics.File_specification_cannot_contain_multiple_recursive_directory_wildcards_Asterisk_Asterisk_Colon_0, spec)); } else { validSpecs.push(spec); @@ -696,7 +699,7 @@ namespace ts { /** * Gets directories in a set of include patterns that should be watched for changes. */ - function getWildcardDirectories(includes: string[], path: string, useCaseSensitiveFileNames: boolean) { + function getWildcardDirectories(include: string[], exclude: string[], path: string, useCaseSensitiveFileNames: boolean) { // We watch a directory recursively if it contains a wildcard anywhere in a directory segment // of the pattern: // @@ -708,11 +711,16 @@ namespace ts { // // /a/b/* - Watch /a/b directly to catch any new file // /a/b/a?z - Watch /a/b directly to catch any new file matching a?z + const excludeRegExp = getRegularExpressionForWildcard(exclude, path, "exclude", useCaseSensitiveFileNames); const wildcardDirectories: Map = {}; - if (includes !== undefined) { + if (include !== undefined) { const recursiveKeys: string[] = []; - for (const include of includes) { - const name = combinePaths(path, include); + for (const file of include) { + const name = combinePaths(path, file); + if (excludeRegExp && excludeRegExp.test(name)) { + continue; + } + const match = wildcardDirectoryPattern.exec(name); if (match) { const key = useCaseSensitiveFileNames ? match[0] : match[0].toLowerCase(); diff --git a/tests/cases/unittests/expandFiles.ts b/tests/cases/unittests/expandFiles.ts index 02065407e27df..bbda14a2cfa8d 100644 --- a/tests/cases/unittests/expandFiles.ts +++ b/tests/cases/unittests/expandFiles.ts @@ -92,189 +92,376 @@ namespace ts { describe("expandFiles", () => { describe("with literal file list", () => { it("without exclusions", () => { - const fileNames = ["a.ts", "b.ts"]; - const expected: ts.ExpandResult = { - fileNames: ["c:/dev/a.ts", "c:/dev/b.ts"], + const json = { + files: [ + "a.ts", + "b.ts" + ] + }; + const expected: ts.ParsedCommandLine = { + options: {}, + errors: [], + fileNames: [ + "c:/dev/a.ts", + "c:/dev/b.ts" + ], wildcardDirectories: {}, }; - const actual = ts.expandFiles(fileNames, /*includeSpecs*/ undefined, /*excludeSpecs*/ undefined, caseInsensitiveBasePath, {}, caseInsensitiveHost); + const actual = ts.parseJsonConfigFileContent(json, caseInsensitiveHost, caseInsensitiveBasePath); assert.deepEqual(actual, expected); }); it("missing files are still present", () => { - const fileNames = ["z.ts", "x.ts"]; - const expected: ts.ExpandResult = { - fileNames: ["c:/dev/z.ts", "c:/dev/x.ts"], + const json = { + files: [ + "z.ts", + "x.ts" + ] + }; + const expected: ts.ParsedCommandLine = { + options: {}, + errors: [], + fileNames: [ + "c:/dev/z.ts", + "c:/dev/x.ts" + ], wildcardDirectories: {}, }; - const actual = ts.expandFiles(fileNames, /*includeSpecs*/ undefined, /*excludeSpecs*/ undefined, caseInsensitiveBasePath, {}, caseInsensitiveHost); + const actual = ts.parseJsonConfigFileContent(json, caseInsensitiveHost, caseInsensitiveBasePath); assert.deepEqual(actual, expected); }); it("are not removed due to excludes", () => { - const fileNames = ["a.ts", "b.ts"]; - const excludeSpecs = ["b.ts"]; - const expected: ts.ExpandResult = { - fileNames: ["c:/dev/a.ts", "c:/dev/b.ts"], + const json = { + files: [ + "a.ts", + "b.ts" + ], + exclude: [ + "b.ts" + ] + }; + const expected: ts.ParsedCommandLine = { + options: {}, + errors: [], + fileNames: [ + "c:/dev/a.ts", + "c:/dev/b.ts" + ], wildcardDirectories: {}, }; - const actual = ts.expandFiles(fileNames, /*includeSpecs*/ undefined, excludeSpecs, caseInsensitiveBasePath, {}, caseInsensitiveHost); + const actual = ts.parseJsonConfigFileContent(json, caseInsensitiveHost, caseInsensitiveBasePath); assert.deepEqual(actual, expected); }); }); describe("with literal include list", () => { it("without exclusions", () => { - const includeSpecs = ["a.ts", "b.ts"]; - const expected: ts.ExpandResult = { - fileNames: ["c:/dev/a.ts", "c:/dev/b.ts"], + const json = { + include: [ + "a.ts", + "b.ts" + ] + }; + const expected: ts.ParsedCommandLine = { + options: {}, + errors: [], + fileNames: [ + "c:/dev/a.ts", + "c:/dev/b.ts" + ], wildcardDirectories: {}, }; - const actual = ts.expandFiles(/*fileNames*/ undefined, includeSpecs, /*excludeSpecs*/ undefined, caseInsensitiveBasePath, {}, caseInsensitiveHost); + const actual = ts.parseJsonConfigFileContent(json, caseInsensitiveHost, caseInsensitiveBasePath); assert.deepEqual(actual, expected); }); it("with non .ts file extensions are excluded", () => { - const includeSpecs = ["a.js", "b.js"]; - const expected: ts.ExpandResult = { + const json = { + include: [ + "a.js", + "b.js" + ] + }; + const expected: ts.ParsedCommandLine = { + options: {}, + errors: [], fileNames: [], wildcardDirectories: {}, }; - const actual = ts.expandFiles(/*fileNames*/ undefined, includeSpecs, /*excludeSpecs*/ undefined, caseInsensitiveBasePath, {}, caseInsensitiveHost); + const actual = ts.parseJsonConfigFileContent(json, caseInsensitiveHost, caseInsensitiveBasePath); assert.deepEqual(actual, expected); }); it("with missing files are excluded", () => { - const includeSpecs = ["z.ts", "x.ts"]; - const expected: ts.ExpandResult = { + const json = { + include: [ + "z.ts", + "x.ts" + ] + }; + const expected: ts.ParsedCommandLine = { + options: {}, + errors: [], fileNames: [], wildcardDirectories: {}, }; - const actual = ts.expandFiles(/*fileNames*/ undefined, includeSpecs, /*excludeSpecs*/ undefined, caseInsensitiveBasePath, {}, caseInsensitiveHost); + const actual = ts.parseJsonConfigFileContent(json, caseInsensitiveHost, caseInsensitiveBasePath); assert.deepEqual(actual, expected); }); it("with literal excludes", () => { - const includeSpecs = ["a.ts", "b.ts"]; - const excludeSpecs = ["b.ts"]; - const expected: ts.ExpandResult = { - fileNames: ["c:/dev/a.ts"], + const json = { + include: [ + "a.ts", + "b.ts" + ], + exclude: [ + "b.ts" + ] + }; + const expected: ts.ParsedCommandLine = { + options: {}, + errors: [], + fileNames: [ + "c:/dev/a.ts" + ], wildcardDirectories: {}, }; - const actual = ts.expandFiles(/*fileNames*/ undefined, includeSpecs, excludeSpecs, caseInsensitiveBasePath, {}, caseInsensitiveHost); + const actual = ts.parseJsonConfigFileContent(json, caseInsensitiveHost, caseInsensitiveBasePath); assert.deepEqual(actual, expected); }); it("with wildcard excludes", () => { - const includeSpecs = ["a.ts", "b.ts", "z/a.ts", "z/abz.ts", "z/aba.ts", "x/b.ts"]; - const excludeSpecs = ["*.ts", "z/??z.ts", "*/b.ts"]; - const expected: ts.ExpandResult = { - fileNames: ["c:/dev/z/a.ts", "c:/dev/z/aba.ts"], + const json = { + include: [ + "a.ts", + "b.ts", + "z/a.ts", + "z/abz.ts", + "z/aba.ts", + "x/b.ts" + ], + exclude: [ + "*.ts", + "z/??z.ts", + "*/b.ts" + ] + }; + const expected: ts.ParsedCommandLine = { + options: {}, + errors: [], + fileNames: [ + "c:/dev/z/a.ts", + "c:/dev/z/aba.ts" + ], wildcardDirectories: {}, }; - const actual = ts.expandFiles(/*fileNames*/ undefined, includeSpecs, excludeSpecs, caseInsensitiveBasePath, {}, caseInsensitiveHost); + const actual = ts.parseJsonConfigFileContent(json, caseInsensitiveHost, caseInsensitiveBasePath); assert.deepEqual(actual, expected); }); it("with recursive excludes", () => { - const includeSpecs = ["a.ts", "b.ts", "x/a.ts", "x/b.ts", "x/y/a.ts", "x/y/b.ts"]; - const excludeSpecs = ["**/b.ts"]; - const expected: ts.ExpandResult = { - fileNames: ["c:/dev/a.ts", "c:/dev/x/a.ts", "c:/dev/x/y/a.ts"], + const json = { + include: [ + "a.ts", + "b.ts", + "x/a.ts", + "x/b.ts", + "x/y/a.ts", + "x/y/b.ts" + ], + exclude: [ + "**/b.ts" + ] + }; + const expected: ts.ParsedCommandLine = { + options: {}, + errors: [], + fileNames: [ + "c:/dev/a.ts", + "c:/dev/x/a.ts", + "c:/dev/x/y/a.ts" + ], wildcardDirectories: {}, }; - const actual = ts.expandFiles(/*fileNames*/ undefined, includeSpecs, excludeSpecs, caseInsensitiveBasePath, {}, caseInsensitiveHost); + const actual = ts.parseJsonConfigFileContent(json, caseInsensitiveHost, caseInsensitiveBasePath); assert.deepEqual(actual, expected); }); it("with case sensitive exclude", () => { - const includeSpecs = ["B.ts"]; - const excludeSpecs = ["**/b.ts"]; - const expected: ts.ExpandResult = { - fileNames: ["/dev/B.ts"], + const json = { + include: [ + "B.ts" + ], + exclude: [ + "**/b.ts" + ] + }; + const expected: ts.ParsedCommandLine = { + options: {}, + errors: [], + fileNames: [ + "/dev/B.ts" + ], wildcardDirectories: {}, }; - const actual = ts.expandFiles(/*fileNames*/ undefined, includeSpecs, excludeSpecs, caseSensitiveBasePath, {}, caseSensitiveHost); + const actual = ts.parseJsonConfigFileContent(json, caseSensitiveHost, caseSensitiveBasePath); assert.deepEqual(actual, expected); }); }); describe("with wildcard include list", () => { it("same named declarations are excluded", () => { - const includeSpecs = ["*.ts"]; - const expected: ts.ExpandResult = { - fileNames: ["c:/dev/a.ts", "c:/dev/b.ts", "c:/dev/c.d.ts"], + const json = { + include: [ + "*.ts" + ] + }; + const expected: ts.ParsedCommandLine = { + options: {}, + errors: [], + fileNames: [ + "c:/dev/a.ts", + "c:/dev/b.ts", + "c:/dev/c.d.ts" + ], wildcardDirectories: { "c:/dev": ts.WatchDirectoryFlags.None }, }; - const actual = ts.expandFiles(/*fileNames*/ undefined, includeSpecs, /*excludeSpecs*/ undefined, caseInsensitiveBasePath, {}, caseInsensitiveHost); + const actual = ts.parseJsonConfigFileContent(json, caseInsensitiveHost, caseInsensitiveBasePath); assert.deepEqual(actual, expected); }); it("`*` matches only ts files", () => { - const includeSpecs = ["*"]; - const expected: ts.ExpandResult = { - fileNames: ["c:/dev/a.ts", "c:/dev/b.ts", "c:/dev/c.d.ts"], + const json = { + include: [ + "*" + ] + }; + const expected: ts.ParsedCommandLine = { + options: {}, + errors: [], + fileNames: [ + "c:/dev/a.ts", + "c:/dev/b.ts", + "c:/dev/c.d.ts" + ], wildcardDirectories: { "c:/dev": ts.WatchDirectoryFlags.None }, }; - const actual = ts.expandFiles(/*fileNames*/ undefined, includeSpecs, /*excludeSpecs*/ undefined, caseInsensitiveBasePath, {}, caseInsensitiveHost); + const actual = ts.parseJsonConfigFileContent(json, caseInsensitiveHost, caseInsensitiveBasePath); assert.deepEqual(actual, expected); }); it("`?` matches only a single character", () => { - const includeSpecs = ["x/?.ts"]; - const expected: ts.ExpandResult = { - fileNames: ["c:/dev/x/a.ts", "c:/dev/x/b.ts"], + const json = { + include: [ + "x/?.ts" + ] + }; + const expected: ts.ParsedCommandLine = { + options: {}, + errors: [], + fileNames: [ + "c:/dev/x/a.ts", + "c:/dev/x/b.ts" + ], wildcardDirectories: { "c:/dev/x": ts.WatchDirectoryFlags.None }, }; - const actual = ts.expandFiles(/*fileNames*/ undefined, includeSpecs, /*excludeSpecs*/ undefined, caseInsensitiveBasePath, {}, caseInsensitiveHost); + const actual = ts.parseJsonConfigFileContent(json, caseInsensitiveHost, caseInsensitiveBasePath); assert.deepEqual(actual, expected); }); it("with recursive directory", () => { - const includeSpecs = ["**/a.ts"]; - const expected: ts.ExpandResult = { - fileNames: ["c:/dev/a.ts", "c:/dev/x/a.ts", "c:/dev/x/y/a.ts", "c:/dev/z/a.ts"], + const json = { + include: [ + "**/a.ts" + ] + }; + const expected: ts.ParsedCommandLine = { + options: {}, + errors: [], + fileNames: [ + "c:/dev/a.ts", + "c:/dev/x/a.ts", + "c:/dev/x/y/a.ts", + "c:/dev/z/a.ts" + ], wildcardDirectories: { "c:/dev": ts.WatchDirectoryFlags.Recursive }, }; - const actual = ts.expandFiles(/*fileNames*/ undefined, includeSpecs, /*excludeSpecs*/ undefined, caseInsensitiveBasePath, {}, caseInsensitiveHost); + const actual = ts.parseJsonConfigFileContent(json, caseInsensitiveHost, caseInsensitiveBasePath); assert.deepEqual(actual, expected); }); it("case sensitive", () => { - const includeSpecs = ["**/A.ts"]; - const expected: ts.ExpandResult = { - fileNames: ["/dev/A.ts"], + const json = { + include: [ + "**/A.ts" + ] + }; + const expected: ts.ParsedCommandLine = { + options: {}, + errors: [], + fileNames: [ + "/dev/A.ts" + ], wildcardDirectories: { "/dev": ts.WatchDirectoryFlags.Recursive }, }; - const actual = ts.expandFiles(/*fileNames*/ undefined, includeSpecs, /*excludeSpecs*/ undefined, caseSensitiveBasePath, {}, caseSensitiveHost); + const actual = ts.parseJsonConfigFileContent(json, caseSensitiveHost, caseSensitiveBasePath); assert.deepEqual(actual, expected); }); it("with missing files are excluded", () => { - const includeSpecs = ["*/z.ts"]; - const expected: ts.ExpandResult = { + const json = { + include: [ + "*/z.ts" + ] + }; + const expected: ts.ParsedCommandLine = { + options: {}, + errors: [], fileNames: [], wildcardDirectories: { "c:/dev": ts.WatchDirectoryFlags.Recursive }, }; - const actual = ts.expandFiles(/*fileNames*/ undefined, includeSpecs, /*excludeSpecs*/ undefined, caseInsensitiveBasePath, {}, caseInsensitiveHost); + const actual = ts.parseJsonConfigFileContent(json, caseInsensitiveHost, caseInsensitiveBasePath); assert.deepEqual(actual, expected); }); it("always include literal files", () => { - const fileNames = ["a.ts"]; - const includeSpecs = ["*/z.ts"]; - const excludeSpecs = ["**/a.ts"]; - const expected: ts.ExpandResult = { - fileNames: ["c:/dev/a.ts"], + const json = { + files: [ + "a.ts" + ], + include: [ + "*/z.ts" + ], + exclude: [ + "**/a.ts" + ] + }; + const expected: ts.ParsedCommandLine = { + options: {}, + errors: [], + fileNames: [ + "c:/dev/a.ts" + ], wildcardDirectories: { "c:/dev": ts.WatchDirectoryFlags.Recursive }, }; - const actual = ts.expandFiles(fileNames, includeSpecs, excludeSpecs, caseInsensitiveBasePath, {}, caseInsensitiveHost); + const actual = ts.parseJsonConfigFileContent(json, caseInsensitiveHost, caseInsensitiveBasePath); assert.deepEqual(actual, expected); }); it("exclude folders", () => { - const includeSpecs = ["**/*"]; - const excludeSpecs = ["z", "x"]; - const expected: ts.ExpandResult = { + const json = { + include: [ + "**/*" + ], + exclude: [ + "z", + "x" + ] + }; + const expected: ts.ParsedCommandLine = { + options: {}, + errors: [], fileNames: [ "c:/dev/a.ts", "c:/dev/b.ts", @@ -284,34 +471,66 @@ namespace ts { "c:/dev": ts.WatchDirectoryFlags.Recursive } }; - const actual = ts.expandFiles(/*fileNames*/ undefined, includeSpecs, excludeSpecs, caseInsensitiveBasePath, {}, caseInsensitiveHost); + const actual = ts.parseJsonConfigFileContent(json, caseInsensitiveHost, caseInsensitiveBasePath); assert.deepEqual(actual, expected); }); it("exclude .js files when allowJs=false", () => { - const includeSpecs = ["js/*"]; - const expected: ts.ExpandResult = { + const json = { + compilerOptions: { + allowJs: false + }, + include: [ + "js/*" + ] + }; + const expected: ts.ParsedCommandLine = { + options: { + allowJs: false + }, + errors: [], fileNames: [], wildcardDirectories: { "c:/dev/js": ts.WatchDirectoryFlags.None } }; - const actual = ts.expandFiles(/*fileNames*/ undefined, includeSpecs, /*excludeSpecs*/ undefined, caseInsensitiveBasePath, {}, caseInsensitiveHost); + const actual = ts.parseJsonConfigFileContent(json, caseInsensitiveHost, caseInsensitiveBasePath); assert.deepEqual(actual, expected); }); it("include .js files when allowJs=true", () => { - const includeSpecs = ["js/*"]; - const expected: ts.ExpandResult = { - fileNames: ["c:/dev/js/a.js", "c:/dev/js/b.js"], + const json = { + compilerOptions: { + allowJs: true + }, + include: [ + "js/*" + ] + }; + const expected: ts.ParsedCommandLine = { + options: { + allowJs: true + }, + errors: [], + fileNames: [ + "c:/dev/js/a.js", + "c:/dev/js/b.js" + ], wildcardDirectories: { "c:/dev/js": ts.WatchDirectoryFlags.None } }; - const actual = ts.expandFiles(/*fileNames*/ undefined, includeSpecs, /*excludeSpecs*/ undefined, caseInsensitiveBasePath, { allowJs: true }, caseInsensitiveHost); + const actual = ts.parseJsonConfigFileContent(json, caseInsensitiveHost, caseInsensitiveBasePath); assert.deepEqual(actual, expected); }); it("include paths outside of the project", () => { - const includeSpecs = ["*", "c:/ext/*"]; - const expected: ts.ExpandResult = { + const json = { + include: [ + "*", + "c:/ext/*" + ] + }; + const expected: ts.ParsedCommandLine = { + options: {}, + errors: [], fileNames: [ "c:/dev/a.ts", "c:/dev/b.ts", @@ -323,20 +542,20 @@ namespace ts { "c:/ext": ts.WatchDirectoryFlags.None } }; - const actual = ts.expandFiles(/*fileNames*/ undefined, includeSpecs, /*excludeSpecs*/ undefined, caseInsensitiveBasePath, {}, caseInsensitiveHost); + const actual = ts.parseJsonConfigFileContent(json, caseInsensitiveHost, caseInsensitiveBasePath); assert.deepEqual(actual, expected); }); - }); - - describe("when called from parseJsonConfigFileContent", () => { it("with jsx=none, allowJs=false", () => { - const json: any = { - "compilerOptions": { - "jsx": "none", - "allowJs": false + const json = { + compilerOptions: { + allowJs: false } }; - const expected: ts.ExpandResult = { + const expected: ts.ParsedCommandLine = { + options: { + allowJs: false + }, + errors: [], fileNames: [ "c:/dev/a.ts", "c:/dev/b.tsx", @@ -347,17 +566,21 @@ namespace ts { } }; const actual = ts.parseJsonConfigFileContent(json, caseInsensitiveMixedExtensionHost, caseInsensitiveBasePath); - assert.deepEqual(actual.fileNames, expected.fileNames); - assert.deepEqual(actual.wildcardDirectories, expected.wildcardDirectories); + assert.deepEqual(actual, expected); }); it("with jsx=preserve, allowJs=false", () => { - const json: any = { - "compilerOptions": { - "jsx": "preserve", - "allowJs": false + const json = { + compilerOptions: { + jsx: "preserve", + allowJs: false } }; - const expected: ts.ExpandResult = { + const expected: ts.ParsedCommandLine = { + options: { + jsx: ts.JsxEmit.Preserve, + allowJs: false + }, + errors: [], fileNames: [ "c:/dev/a.ts", "c:/dev/b.tsx", @@ -368,17 +591,19 @@ namespace ts { } }; const actual = ts.parseJsonConfigFileContent(json, caseInsensitiveMixedExtensionHost, caseInsensitiveBasePath); - assert.deepEqual(actual.fileNames, expected.fileNames); - assert.deepEqual(actual.wildcardDirectories, expected.wildcardDirectories); + assert.deepEqual(actual, expected); }); it("with jsx=none, allowJs=true", () => { - const json: any = { - "compilerOptions": { - "jsx": "none", - "allowJs": true + const json = { + compilerOptions: { + allowJs: true } }; - const expected: ts.ExpandResult = { + const expected: ts.ParsedCommandLine = { + options: { + allowJs: true + }, + errors: [], fileNames: [ "c:/dev/a.ts", "c:/dev/b.tsx", @@ -391,17 +616,21 @@ namespace ts { } }; const actual = ts.parseJsonConfigFileContent(json, caseInsensitiveMixedExtensionHost, caseInsensitiveBasePath); - assert.deepEqual(actual.fileNames, expected.fileNames); - assert.deepEqual(actual.wildcardDirectories, expected.wildcardDirectories); + assert.deepEqual(actual, expected); }); it("with jsx=preserve, allowJs=true", () => { - const json: any = { - "compilerOptions": { - "jsx": "preserve", - "allowJs": true + const json = { + compilerOptions: { + jsx: "preserve", + allowJs: true } }; - const expected: ts.ExpandResult = { + const expected: ts.ParsedCommandLine = { + options: { + jsx: ts.JsxEmit.Preserve, + allowJs: true + }, + errors: [], fileNames: [ "c:/dev/a.ts", "c:/dev/b.tsx", @@ -414,8 +643,90 @@ namespace ts { } }; const actual = ts.parseJsonConfigFileContent(json, caseInsensitiveMixedExtensionHost, caseInsensitiveBasePath); - assert.deepEqual(actual.fileNames, expected.fileNames); - assert.deepEqual(actual.wildcardDirectories, expected.wildcardDirectories); + assert.deepEqual(actual, expected); + }); + describe("with trailing recursive directory", () => { + it("in includes", () => { + const json = { + include: [ + "**" + ] + }; + const expected: ts.ParsedCommandLine = { + options: {}, + errors: [ + ts.createCompilerDiagnostic(ts.Diagnostics.File_specification_cannot_end_in_a_recursive_directory_wildcard_Asterisk_Asterisk_Colon_0, "**") + ], + fileNames: [], + wildcardDirectories: {} + }; + const actual = ts.parseJsonConfigFileContent(json, caseInsensitiveHost, caseInsensitiveBasePath); + assert.deepEqual(actual, expected); + }); + it("in excludes", () => { + const json = { + include: [ + "**/*" + ], + exclude: [ + "**" + ] + }; + const expected: ts.ParsedCommandLine = { + options: {}, + errors: [], + fileNames: [], + wildcardDirectories: {} + }; + const actual = ts.parseJsonConfigFileContent(json, caseInsensitiveHost, caseInsensitiveBasePath); + assert.deepEqual(actual, expected); + }); + }); + describe("with multiple recursive directory patterns", () => { + it("in includes", () => { + const json = { + include: [ + "**/x/**/*" + ] + }; + const expected: ts.ParsedCommandLine = { + options: {}, + errors: [ + ts.createCompilerDiagnostic(ts.Diagnostics.File_specification_cannot_contain_multiple_recursive_directory_wildcards_Asterisk_Asterisk_Colon_0, "**/x/**/*") + ], + fileNames: [], + wildcardDirectories: {} + }; + const actual = ts.parseJsonConfigFileContent(json, caseInsensitiveHost, caseInsensitiveBasePath); + assert.deepEqual(actual, expected); + }); + it("in excludes", () => { + const json = { + include: [ + "**/a.ts" + ], + exclude: [ + "**/x/**" + ] + }; + const expected: ts.ParsedCommandLine = { + options: {}, + errors: [ + ts.createCompilerDiagnostic(ts.Diagnostics.File_specification_cannot_contain_multiple_recursive_directory_wildcards_Asterisk_Asterisk_Colon_0, "**/x/**") + ], + fileNames: [ + "c:/dev/a.ts", + "c:/dev/x/a.ts", + "c:/dev/x/y/a.ts", + "c:/dev/z/a.ts" + ], + wildcardDirectories: { + "c:/dev": ts.WatchDirectoryFlags.Recursive + } + }; + const actual = ts.parseJsonConfigFileContent(json, caseInsensitiveHost, caseInsensitiveBasePath); + assert.deepEqual(actual, expected); + }); }); }); }); From 6f85fe9ac467727bd33f607c95e7eaa6912ae57b Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Wed, 16 Dec 2015 15:18:25 -0800 Subject: [PATCH 08/84] Minor update to shims.ts for forthcoming VS support for globs. --- src/services/shims.ts | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/src/services/shims.ts b/src/services/shims.ts index cf6f3885a2c3a..46056eb110fff 100644 --- a/src/services/shims.ts +++ b/src/services/shims.ts @@ -73,7 +73,7 @@ namespace ts { * when enumerating the directory. */ readDirectory(rootDir: string, extension: string, exclude?: string, include?: string): string; - useCaseSensitiveFileNames?: boolean; + useCaseSensitiveFileNames?(): boolean; } /// @@ -411,15 +411,7 @@ namespace ts { public useCaseSensitiveFileNames: boolean; constructor(private shimHost: CoreServicesShimHost) { - if (typeof shimHost.useCaseSensitiveFileNames === "boolean") { - this.useCaseSensitiveFileNames = shimHost.useCaseSensitiveFileNames; - } - else if (sys) { - this.useCaseSensitiveFileNames = sys.useCaseSensitiveFileNames; - } - else { - this.useCaseSensitiveFileNames = true; - } + this.useCaseSensitiveFileNames = this.shimHost.useCaseSensitiveFileNames ? this.shimHost.useCaseSensitiveFileNames() : false; } public readDirectory(rootDir: string, extensions: string[], exclude: string[], include: string[]): string[] { @@ -435,7 +427,7 @@ namespace ts { JSON.stringify(include))); } catch (e) { - let results: string[] = []; + const results: string[] = []; for (const extension of extensions) { for (const file of this.readDirectoryFallback(rootDir, extension, exclude)) { From d23df3430ee26f9a5a078c84f5aae229598156d0 Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Wed, 16 Dec 2015 15:49:31 -0800 Subject: [PATCH 09/84] Detailed comments for regular expressions and renamed some files. --- Jakefile.js | 4 +- src/compiler/commandLineParser.ts | 55 ++++++++++++++++++- src/harness/harness.ts | 2 +- src/harness/{vfs.ts => virtualFileSystem.ts} | 0 .../{expandFiles.ts => matchFiles.ts} | 3 +- 5 files changed, 57 insertions(+), 7 deletions(-) rename src/harness/{vfs.ts => virtualFileSystem.ts} (100%) rename tests/cases/unittests/{expandFiles.ts => matchFiles.ts} (97%) diff --git a/Jakefile.js b/Jakefile.js index cf7f69d41dc73..d46c2437ccbc4 100644 --- a/Jakefile.js +++ b/Jakefile.js @@ -131,7 +131,7 @@ var languageServiceLibrarySources = [ var harnessCoreSources = [ "harness.ts", - "vfs.ts", + "virtualFileSystem.ts", "sourceMapRecorder.ts", "harnessLanguageService.ts", "fourslash.ts", @@ -163,7 +163,7 @@ var harnessSources = harnessCoreSources.concat([ "cachingInServerLSHost.ts", "moduleResolution.ts", "tsconfigParsing.ts", - "expandFiles.ts" + "matchFiles.ts" ].map(function (f) { return path.join(unittestsDirectory, f); })).concat([ diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index a5cd7abc50522..6935cf28bcdac 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -582,9 +582,61 @@ namespace ts { return { options, errors }; } + /** + * Tests for a path that ends in a recursive directory wildcard. + * Matches **, /**, /**\/, and /**\/, but not a**b. + * + * NOTE: used \/ in place of / above to avoid ending the comment. + * + * Breakdown: + * (^|\/) # matches either the beginning of the string or a directory separator. + * \*\* # matches the recursive directory wildcard "**". + * \/?$ # matches an optional trailing directory separator at the end of the string. + */ const invalidTrailingRecursionPattern = /(^|\/)\*\*\/?$/; + + /** + * Tests for a path with multiple recursive directory wildcards. + * Matches **\/** and **\/a/**, but not **\/a**b. + * + * NOTE: used \/ in place of / above to avoid ending the comment. + * + * Breakdown: + * (^|\/) # matches either the beginning of the string or a directory separator. + * \*\*\/ # matches a recursive directory wildcard "**" followed by a directory separator. + * (.*\/)? # optionally matches any number of characters followed by a directory separator. + * \*\* # matches a recursive directory wildcard "**" + * ($|\/) # matches either the end of the string or a directory separator. + */ const invalidMultipleRecursionPatterns = /(^|\/)\*\*\/(.*\/)?\*\*($|\/)/; + /** + * Tests for a path containing a wildcard character in a directory component of the path. + * Matches /*\/, /?/, and /a*b/, but not /a/ or /a/*. + * + * NOTE: used \/ in place of / above to avoid ending the comment. + * + * Breakdown: + * \/ # matches a directory separator. + * [^/]*? # matches any number of characters excluding directory separators (non-greedy). + * [*?] # matches either a wildcard character (* or ?) + * [^/]* # matches any number of characters excluding directory separators (greedy). + * \/ # matches a directory separator. + */ + const watchRecursivePattern = /\/[^/]*?[*?][^/]*\//; + + /** + * Matches the portion of a wildcard path that does not contain wildcards. + * Matches /a of /a/*, or /a/b/c of /a/b/c/?/d. + * + * Breakdown: + * ^ # matches the beginning of the string + * [^*?]* # matches any number of non-wildcard characters + * (?=\/[^/]*[*?]) # lookahead that matches a directory separator followed by + * # a path component that contains at least one wildcard character (* or ?). + */ + const wildcardDirectoryPattern = /^[^*?]*(?=\/[^/]*[*?])/; + /** * Expands an array of file specifications. * @@ -693,9 +745,6 @@ namespace ts { return validSpecs; } - const watchRecursivePattern = /\/[^/]*?[*?][^/]*\//; - const wildcardDirectoryPattern = /^[^*?]*(?=\/[^/]*[*?])/; - /** * Gets directories in a set of include patterns that should be watched for changes. */ diff --git a/src/harness/harness.ts b/src/harness/harness.ts index 509c193666f29..f33cafc456d1e 100644 --- a/src/harness/harness.ts +++ b/src/harness/harness.ts @@ -23,7 +23,7 @@ /// /// /// -/// +/// /* tslint:disable:no-null */ // Block scoped definitions work poorly for global variables, temporarily enable var diff --git a/src/harness/vfs.ts b/src/harness/virtualFileSystem.ts similarity index 100% rename from src/harness/vfs.ts rename to src/harness/virtualFileSystem.ts diff --git a/tests/cases/unittests/expandFiles.ts b/tests/cases/unittests/matchFiles.ts similarity index 97% rename from tests/cases/unittests/expandFiles.ts rename to tests/cases/unittests/matchFiles.ts index bbda14a2cfa8d..faccd31b88f40 100644 --- a/tests/cases/unittests/expandFiles.ts +++ b/tests/cases/unittests/matchFiles.ts @@ -1,5 +1,6 @@ /// /// +/// namespace ts { class MockParseConfigHost extends Utils.VirtualFileSystem implements ParseConfigHost { @@ -89,7 +90,7 @@ namespace ts { "c:/dev/f.other" ]); - describe("expandFiles", () => { + describe("matchFiles", () => { describe("with literal file list", () => { it("without exclusions", () => { const json = { From c2249177f58eb8221a9d4709f65cb900e207d15a Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Mon, 4 Jan 2016 11:37:25 -0800 Subject: [PATCH 10/84] Comment cleanup --- src/compiler/commandLineParser.ts | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index 6935cf28bcdac..48acf3055ad1e 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -584,9 +584,9 @@ namespace ts { /** * Tests for a path that ends in a recursive directory wildcard. - * Matches **, /**, /**\/, and /**\/, but not a**b. + * Matches **, \**, **\, and \**\, but not a**b. * - * NOTE: used \/ in place of / above to avoid ending the comment. + * NOTE: used \ in place of / above to avoid issues with multiline comments. * * Breakdown: * (^|\/) # matches either the beginning of the string or a directory separator. @@ -597,9 +597,9 @@ namespace ts { /** * Tests for a path with multiple recursive directory wildcards. - * Matches **\/** and **\/a/**, but not **\/a**b. + * Matches **\** and **\a\**, but not **\a**b. * - * NOTE: used \/ in place of / above to avoid ending the comment. + * NOTE: used \ in place of / above to avoid issues with multiline comments. * * Breakdown: * (^|\/) # matches either the beginning of the string or a directory separator. @@ -612,9 +612,9 @@ namespace ts { /** * Tests for a path containing a wildcard character in a directory component of the path. - * Matches /*\/, /?/, and /a*b/, but not /a/ or /a/*. + * Matches \*\, \?\, and \a*b\, but not \a\ or \a\*. * - * NOTE: used \/ in place of / above to avoid ending the comment. + * NOTE: used \ in place of / above to avoid issues with multiline comments. * * Breakdown: * \/ # matches a directory separator. @@ -627,7 +627,9 @@ namespace ts { /** * Matches the portion of a wildcard path that does not contain wildcards. - * Matches /a of /a/*, or /a/b/c of /a/b/c/?/d. + * Matches \a of \a\*, or \a\b\c of \a\b\c\?\d. + * + * NOTE: used \ in place of / above to avoid issues with multiline comments. * * Breakdown: * ^ # matches the beginning of the string From c1205ebdfbfebc94b35abab251237bdfbff213c5 Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Tue, 5 Jan 2016 10:11:44 -0800 Subject: [PATCH 11/84] Fixed new linter warnings --- src/compiler/commandLineParser.ts | 4 ++-- src/compiler/core.ts | 10 +++++----- src/compiler/sys.ts | 10 +--------- src/harness/projectsRunner.ts | 4 ---- 4 files changed, 8 insertions(+), 20 deletions(-) diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index 759b443e74240..52f9ef05f37b1 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -816,7 +816,7 @@ namespace ts { function hasFileWithHigherPriorityExtension(file: string, literalFiles: Map, wildcardFiles: Map, extensions: string[], keyMapper: (value: string) => string) { const extensionPriority = getExtensionPriority(file, extensions); const adjustedExtensionPriority = adjustExtensionPriority(extensionPriority); - for (let i = ExtensionPriority.Highest; i < adjustedExtensionPriority; ++i) { + for (let i = ExtensionPriority.Highest; i < adjustedExtensionPriority; i++) { const higherPriorityExtension = extensions[i]; const higherPriorityPath = keyMapper(changeExtension(file, higherPriorityExtension)); if (hasProperty(literalFiles, higherPriorityPath) || hasProperty(wildcardFiles, higherPriorityPath)) { @@ -838,7 +838,7 @@ namespace ts { function removeWildcardFilesWithLowerPriorityExtension(file: string, wildcardFiles: Map, extensions: string[], keyMapper: (value: string) => string) { const extensionPriority = getExtensionPriority(file, extensions); const nextExtensionPriority = getNextLowestExtensionPriority(extensionPriority); - for (let i = nextExtensionPriority; i < extensions.length; ++i) { + for (let i = nextExtensionPriority; i < extensions.length; i++) { const lowerPriorityExtension = extensions[i]; const lowerPriorityPath = keyMapper(changeExtension(file, lowerPriorityExtension)); delete wildcardFiles[lowerPriorityPath]; diff --git a/src/compiler/core.ts b/src/compiler/core.ts index 137a603407a4c..d0f27a2046f61 100644 --- a/src/compiler/core.ts +++ b/src/compiler/core.ts @@ -116,7 +116,7 @@ namespace ts { } export function indexOfAnyCharCode(text: string, charCodes: number[], start?: number): number { - for (let i = start || 0, len = text.length; i < len; ++i) { + for (let i = start || 0, len = text.length; i < len; i++) { if (contains(charCodes, text.charCodeAt(i))) { return i; } @@ -824,7 +824,7 @@ namespace ts { const aComponents = getNormalizedPathComponents(a, currentDirectory); const bComponents = getNormalizedPathComponents(b, currentDirectory); const sharedLength = Math.min(aComponents.length, bComponents.length); - for (let i = 0; i < sharedLength; ++i) { + for (let i = 0; i < sharedLength; i++) { const result = compareStrings(aComponents[i], bComponents[i], ignoreCase); if (result !== Comparison.EqualTo) { return result; @@ -846,7 +846,7 @@ namespace ts { return false; } - for (let i = 0; i < parentComponents.length; ++i) { + for (let i = 0; i < parentComponents.length; i++) { const result = compareStrings(parentComponents[i], childComponents[i], ignoreCase); if (result !== Comparison.EqualTo) { return false; @@ -1021,9 +1021,9 @@ namespace ts { // Iterate over each include base path and include unique base paths that are not a // subpath of an existing base path - include: for (let i = 0; i < includeBasePaths.length; ++i) { + include: for (let i = 0; i < includeBasePaths.length; i++) { const includeBasePath = includeBasePaths[i]; - for (let j = 0; j < basePaths.length; ++j) { + for (let j = 0; j < basePaths.length; j++) { if (containsPath(basePaths[j], includeBasePath, path, !useCaseSensitiveFileNames)) { continue include; } diff --git a/src/compiler/sys.ts b/src/compiler/sys.ts index 9b97c99eaa397..2c23e4016eb30 100644 --- a/src/compiler/sys.ts +++ b/src/compiler/sys.ts @@ -139,10 +139,6 @@ namespace ts { } } - function getCanonicalPath(path: string): string { - return path.toLowerCase(); - } - function getNames(collection: any): string[] { const result: string[] = []; for (let e = new Enumerator(collection); !e.atEnd(); e.moveNext()) { @@ -357,10 +353,6 @@ namespace ts { } } - function getCanonicalPath(path: string): string { - return useCaseSensitiveFileNames ? path.toLowerCase() : path; - } - function getAccessibleFileSystemEntries(path: string): FileSystemEntries { try { const entries = _fs.readdirSync(path || ".").sort(); @@ -501,4 +493,4 @@ namespace ts { return undefined; // Unsupported host } })(); -} +} diff --git a/src/harness/projectsRunner.ts b/src/harness/projectsRunner.ts index bcb91fbbfdaf3..769e72d385127 100644 --- a/src/harness/projectsRunner.ts +++ b/src/harness/projectsRunner.ts @@ -287,10 +287,6 @@ class ProjectRunner extends RunnerBase { return Harness.IO.fileExists(getFileNameInTheProjectTest(fileName)); } - function directoryExists(directoryName: string): boolean { - return Harness.IO.directoryExists(getFileNameInTheProjectTest(directoryName)); - } - function getSourceFileText(fileName: string): string { let text: string = undefined; try { From 4d3cff1e5a6f34e967bcda1ee1cf002295b8f9e3 Mon Sep 17 00:00:00 2001 From: zhengbli Date: Fri, 11 Mar 2016 15:56:36 -0800 Subject: [PATCH 12/84] 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 cf55f030c3382..a11d06a644585 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 15a3a4e5ef474..b5208e3aad4dc 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 495fbde5a9992..406eeb6cd9112 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 4cf94655b354f..6d7847ab588a6 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 de10d469c87fa..72ad016f5964d 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 71907735b9125..26b40b3d908d9 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 13/84] 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 6d7847ab588a6..c70985cd018a4 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 26b40b3d908d9..4b49c2944cd3f 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 14/84] 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 a11d06a644585..72deb16f0c13f 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 b5208e3aad4dc..d96b10f61ade0 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 406eeb6cd9112..cdb4d3b417cc3 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 c70985cd018a4..1c7164aeb01a5 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 155f5c1a77fb4..73dc959e16385 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 72ad016f5964d..9073e481729a1 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 4b49c2944cd3f..9cbb038f04e75 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 15/84] 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 72deb16f0c13f..3b7f423bf860a 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 d96b10f61ade0..f8a7b627a7cf7 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 cdb4d3b417cc3..8f2f95fa4fe6b 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 9073e481729a1..7974fb66cd601 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 9cbb038f04e75..456ff0ed26a1f 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 a562ecf18cf13..e72c647db1bb4 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 16/84] 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 456ff0ed26a1f..6f8006986ed6c 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 17/84] 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 34a84ac793f8b..f535140eeaf66 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 1c7164aeb01a5..924fe92990b5c 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 95a1e32c401dd..af765eb17b0cb 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 18/84] 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 | 170 +++++++++++++++++++++++------- src/server/protocol.d.ts | 1 + src/server/session.ts | 53 ++++++---- 8 files changed, 250 insertions(+), 115 deletions(-) diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index f535140eeaf66..d7e58b20ca5cd 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 2da1fffdb8b67..64a487baad2ec 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 924fe92990b5c..3b8b9b3f39f95 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 af765eb17b0cb..3638cc3a92f23 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 68db246cb9a3f..6520e339ac909 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 8c3429ac69890..2eef384df5c54 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; - - // 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" }; + if (!projectOptions.compilerOptions.disableSizeLimit && projectOptions.compilerOptions.allowJs) { + if (this.exceedTotalNonTsFileSizeLimit(projectOptions.files)) { + const project = this.createProject(configFilename, projectOptions, /*languageServiceDisabled*/ true); + + // 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 5ed227ebaa7fd..3a9746dc10978 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 f0975a3f947b2..a66fbc8a5e408 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 19/84] 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 d7e58b20ca5cd..5ba54c6553c7e 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 3b8b9b3f39f95..7cdc1624582cc 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 20/84] 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 2eef384df5c54..2c9e26e47cbe3 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 db856431e8ff5413a35f4e536f1a078dcd2a8200 Mon Sep 17 00:00:00 2001 From: Richard Knoll Date: Wed, 25 May 2016 17:07:36 -0700 Subject: [PATCH 21/84] Fixing linter and test errors --- src/compiler/commandLineParser.ts | 15 ++++++++++++++- src/compiler/core.ts | 2 +- src/compiler/sys.ts | 6 +----- src/services/shims.ts | 8 ++++---- tests/cases/unittests/tsconfigParsing.ts | 21 --------------------- 5 files changed, 20 insertions(+), 32 deletions(-) diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index 8dcc21b4d1b34..9fec0b1083d9a 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -713,6 +713,16 @@ namespace ts { errors.push(createCompilerDiagnostic(Diagnostics.Compiler_option_0_requires_a_value_of_type_1, "exclude", "Array")); } } + else { + // By default, exclude common package folders + excludeSpecs = ["node_modules", "bower_components", "jspm_packages"]; + } + + // Always exclude the output directory unless explicitly included + const outDir = json["compilerOptions"] && json["compilerOptions"]["outDir"]; + if (outDir) { + excludeSpecs.push(outDir); + } if (fileNames === undefined && includeSpecs === undefined) { includeSpecs = ["**/*"]; @@ -885,7 +895,6 @@ namespace ts { */ function matchFileNames(fileNames: string[], include: string[], exclude: string[], basePath: string, options: CompilerOptions, host: ParseConfigHost, errors: Diagnostic[]): ExpandResult { basePath = normalizePath(basePath); - basePath = removeTrailingDirectorySeparator(basePath); // The exclude spec list is converted into a regular expression, which allows us to quickly // test whether a file or directory should be excluded before recursively traversing the @@ -941,6 +950,10 @@ namespace ts { continue; } + if (IgnoreFileNamePattern.test(file)) { + continue; + } + // We may have included a wildcard path with a lower priority // extension due to the user-defined order of entries in the // "include" array. If there is a lower priority extension in the diff --git a/src/compiler/core.ts b/src/compiler/core.ts index ae489ad553c12..cc3c2b2811476 100644 --- a/src/compiler/core.ts +++ b/src/compiler/core.ts @@ -1067,7 +1067,7 @@ namespace ts { return basePaths; } - + export function ensureScriptKind(fileName: string, scriptKind?: ScriptKind): ScriptKind { // Using scriptKind as a condition handles both: // - 'scriptKind' is unspecified and thus it is `undefined` diff --git a/src/compiler/sys.ts b/src/compiler/sys.ts index 3191b8b0056a9..8e5e76974bf75 100644 --- a/src/compiler/sys.ts +++ b/src/compiler/sys.ts @@ -367,7 +367,7 @@ namespace ts { const files: string[] = []; const directories: string[] = []; for (const entry of entries) { - // This is necessary because on some file system node fails to exclude + // This is necessary because on some file system node fails to exclude // "." and "..". See https://github.com/nodejs/node/issues/4002 if (entry === "." || entry === "..") { continue; @@ -391,10 +391,6 @@ namespace ts { function readDirectory(path: string, extensions?: string[], excludes?: string[], includes?: string[]): string[] { return matchFiles(path, extensions, excludes, includes, useCaseSensitiveFileNames, process.cwd(), getAccessibleFileSystemEntries); } - - function getCanonicalPath(path: string): string { - return useCaseSensitiveFileNames ? path : path.toLowerCase(); - } const enum FileSystemEntryKind { File, diff --git a/src/services/shims.ts b/src/services/shims.ts index 0610c0e0b57e6..ba9ccdce2dc77 100644 --- a/src/services/shims.ts +++ b/src/services/shims.ts @@ -81,7 +81,7 @@ namespace ts { */ readDirectory(rootDir: string, extension: string, exclude?: string, include?: string, depth?: number): string; useCaseSensitiveFileNames?(): boolean; - trace(s: string): void; + trace(s: string): void; } /// @@ -432,10 +432,10 @@ namespace ts { public directoryExists: (directoryName: string) => boolean; public realpath: (path: string) => string; - public useCaseSensitiveFileNames: boolean; + public useCaseSensitiveFileNames: boolean; constructor(private shimHost: CoreServicesShimHost) { - this.useCaseSensitiveFileNames = this.shimHost.useCaseSensitiveFileNames ? this.shimHost.useCaseSensitiveFileNames() : false; + this.useCaseSensitiveFileNames = this.shimHost.useCaseSensitiveFileNames ? this.shimHost.useCaseSensitiveFileNames() : false; if ("directoryExists" in this.shimHost) { this.directoryExists = directoryName => this.shimHost.directoryExists(directoryName); } @@ -453,7 +453,7 @@ namespace ts { JSON.stringify(extensions), JSON.stringify(exclude), JSON.stringify(include), - depth + depth )); } catch (e) { diff --git a/tests/cases/unittests/tsconfigParsing.ts b/tests/cases/unittests/tsconfigParsing.ts index 581575c94794d..daa2b2bcdf099 100644 --- a/tests/cases/unittests/tsconfigParsing.ts +++ b/tests/cases/unittests/tsconfigParsing.ts @@ -50,27 +50,6 @@ namespace ts { const host: ParseConfigHost = new MockParseConfigHost(basePath, true, allFileList); const parsed = ts.parseJsonConfigFileContent(json, host, basePath, /*existingOptions*/ undefined, configFileName); assert.isTrue(arrayIsEqualTo(parsed.fileNames.sort(), expectedFileList.sort())); - - function mockReadDirectory(rootDir: string, extension: string, exclude: string[]): string[] { - const result: string[] = []; - const fullExcludeDirectories = ts.map(exclude, directory => combinePaths(rootDir, directory)); - for (const file of allFileList) { - let shouldExclude = false; - for (const fullExcludeDirectorie of fullExcludeDirectories) { - if (file.indexOf(fullExcludeDirectorie) >= 0) { - shouldExclude = true; - break; - } - } - if (shouldExclude) { - continue; - } - if (fileExtensionIs(file, extension)) { - result.push(file); - } - } - return result; - } } it("returns empty config for file with only whitespaces", () => { From c340c88706939febc9e6a52f28e3089b94294973 Mon Sep 17 00:00:00 2001 From: Richard Knoll Date: Wed, 25 May 2016 17:08:24 -0700 Subject: [PATCH 22/84] Bringing back excludes error and fixing faulty test --- src/compiler/commandLineParser.ts | 3 ++ src/harness/virtualFileSystem.ts | 26 +++++++++++++++ tests/cases/unittests/matchFiles.ts | 31 ++--------------- tests/cases/unittests/tsconfigParsing.ts | 42 +++++------------------- 4 files changed, 41 insertions(+), 61 deletions(-) diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index 9fec0b1083d9a..d96e3b30cc9cd 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -713,6 +713,9 @@ namespace ts { errors.push(createCompilerDiagnostic(Diagnostics.Compiler_option_0_requires_a_value_of_type_1, "exclude", "Array")); } } + else if (hasProperty(json, "excludes")) { + errors.push(createCompilerDiagnostic(Diagnostics.Unknown_option_excludes_Did_you_mean_exclude)); + } else { // By default, exclude common package folders excludeSpecs = ["node_modules", "bower_components", "jspm_packages"]; diff --git a/src/harness/virtualFileSystem.ts b/src/harness/virtualFileSystem.ts index 66bbd715fbc60..30192b8b8ec4d 100644 --- a/src/harness/virtualFileSystem.ts +++ b/src/harness/virtualFileSystem.ts @@ -1,4 +1,5 @@ /// +/// namespace Utils { export class VirtualFileSystemEntry { fileSystem: VirtualFileSystem; @@ -157,4 +158,29 @@ namespace Utils { return directory; } } + + export class MockParseConfigHost extends VirtualFileSystem implements ts.ParseConfigHost { + constructor(currentDirectory: string, ignoreCase: boolean, files: string[]) { + super(currentDirectory, ignoreCase); + for (const file of files) { + this.addFile(file); + } + } + + readDirectory(path: string, extensions: string[], excludes: string[], includes: string[]) { + return ts.matchFiles(path, extensions, excludes, includes, this.useCaseSensitiveFileNames, this.currentDirectory, (path: string) => this.getAccessibleFileSystemEntries(path)); + } + + getAccessibleFileSystemEntries(path: string) { + const entry = this.traversePath(path); + if (entry && entry.isDirectory()) { + const directory = entry; + return { + files: ts.map(directory.getFiles(), f => f.name), + directories: ts.map(directory.getDirectories(), d => d.name) + }; + } + return { files: [], directories: [] }; + } + } } \ No newline at end of file diff --git a/tests/cases/unittests/matchFiles.ts b/tests/cases/unittests/matchFiles.ts index 792bad51899b7..c2d13cf0393a3 100644 --- a/tests/cases/unittests/matchFiles.ts +++ b/tests/cases/unittests/matchFiles.ts @@ -3,33 +3,8 @@ /// namespace ts { - class MockParseConfigHost extends Utils.VirtualFileSystem implements ParseConfigHost { - constructor(currentDirectory: string, ignoreCase: boolean, files: string[]) { - super(currentDirectory, ignoreCase); - for (const file of files) { - this.addFile(file); - } - } - - readDirectory(path: string, extensions: string[], excludes: string[], includes: string[]) { - return matchFiles(path, extensions, excludes, includes, this.useCaseSensitiveFileNames, this.currentDirectory, (path: string) => this.getAccessibleFileSystemEntries(path)); - } - - getAccessibleFileSystemEntries(path: string) { - const entry = this.traversePath(path); - if (entry && entry.isDirectory()) { - const directory = entry; - return { - files: map(directory.getFiles(), f => f.name), - directories: map(directory.getDirectories(), d => d.name) - }; - } - return { files: [], directories: [] }; - } - } - const caseInsensitiveBasePath = "c:/dev/"; - const caseInsensitiveHost = new MockParseConfigHost(caseInsensitiveBasePath, /*useCaseSensitiveFileNames*/ false, [ + const caseInsensitiveHost = new Utils.MockParseConfigHost(caseInsensitiveBasePath, /*useCaseSensitiveFileNames*/ false, [ "c:/dev/a.ts", "c:/dev/a.d.ts", "c:/dev/a.js", @@ -53,7 +28,7 @@ namespace ts { ]); const caseSensitiveBasePath = "/dev/"; - const caseSensitiveHost = new MockParseConfigHost(caseSensitiveBasePath, /*useCaseSensitiveFileNames*/ true, [ + const caseSensitiveHost = new Utils.MockParseConfigHost(caseSensitiveBasePath, /*useCaseSensitiveFileNames*/ true, [ "/dev/a.ts", "/dev/a.d.ts", "/dev/a.js", @@ -76,7 +51,7 @@ namespace ts { "/dev/js/b.js", ]); - const caseInsensitiveMixedExtensionHost = new MockParseConfigHost(caseInsensitiveBasePath, /*useCaseSensitiveFileNames*/ false, [ + const caseInsensitiveMixedExtensionHost = new Utils.MockParseConfigHost(caseInsensitiveBasePath, /*useCaseSensitiveFileNames*/ false, [ "c:/dev/a.ts", "c:/dev/a.d.ts", "c:/dev/a.js", diff --git a/tests/cases/unittests/tsconfigParsing.ts b/tests/cases/unittests/tsconfigParsing.ts index daa2b2bcdf099..9b833e64a1117 100644 --- a/tests/cases/unittests/tsconfigParsing.ts +++ b/tests/cases/unittests/tsconfigParsing.ts @@ -2,31 +2,6 @@ /// namespace ts { - class MockParseConfigHost extends Utils.VirtualFileSystem implements ParseConfigHost { - constructor(currentDirectory: string, ignoreCase: boolean, files: string[]) { - super(currentDirectory, ignoreCase); - for (const file of files) { - this.addFile(file); - } - } - - readDirectory(path: string, extensions: string[], excludes: string[], includes: string[]) { - return matchFiles(path, extensions, excludes, includes, this.useCaseSensitiveFileNames, this.currentDirectory, (path: string) => this.getAccessibleFileSystemEntries(path)); - } - - getAccessibleFileSystemEntries(path: string) { - const entry = this.traversePath(path); - if (entry && entry.isDirectory()) { - const directory = entry; - return { - files: map(directory.getFiles(), f => f.name), - directories: map(directory.getDirectories(), d => d.name) - }; - } - return { files: [], directories: [] }; - } - } - describe('parseConfigFileTextToJson', () => { function assertParseResult(jsonText: string, expectedConfigObject: { config?: any; error?: Diagnostic }) { let parsed = ts.parseConfigFileTextToJson("/apath/tsconfig.json", jsonText); @@ -41,13 +16,14 @@ namespace ts { function assertParseErrorWithExcludesKeyword(jsonText: string) { let parsed = ts.parseConfigFileTextToJson("/apath/tsconfig.json", jsonText); - let parsedCommand = ts.parseJsonConfigFileContent(parsed, ts.sys, "tests/cases/unittests"); - assert.isTrue(undefined !== parsedCommand.errors); + let parsedCommand = ts.parseJsonConfigFileContent(parsed.config, ts.sys, "tests/cases/unittests"); + assert.isTrue(parsedCommand.errors && parsedCommand.errors.length === 1 && + parsedCommand.errors[0].code === ts.Diagnostics.Unknown_option_excludes_Did_you_mean_exclude.code); } function assertParseFileList(jsonText: string, configFileName: string, basePath: string, allFileList: string[], expectedFileList: string[]) { const json = JSON.parse(jsonText); - const host: ParseConfigHost = new MockParseConfigHost(basePath, true, allFileList); + const host: ParseConfigHost = new Utils.MockParseConfigHost(basePath, true, allFileList); const parsed = ts.parseJsonConfigFileContent(json, host, basePath, /*existingOptions*/ undefined, configFileName); assert.isTrue(arrayIsEqualTo(parsed.fileNames.sort(), expectedFileList.sort())); } @@ -125,19 +101,19 @@ namespace ts { assertParseResult( `{ "compilerOptions": { - "lib": "es5" + "lib": ["es5"] } }`, { - config: { compilerOptions: { lib: "es5" } } + config: { compilerOptions: { lib: ["es5"] } } }); assertParseResult( `{ "compilerOptions": { - "lib": "es5,es6" + "lib": ["es5", "es6"] } }`, { - config: { compilerOptions: { lib: "es5,es6" } } + config: { compilerOptions: { lib: ["es5", "es6"] } } }); }); @@ -145,7 +121,7 @@ namespace ts { assertParseErrorWithExcludesKeyword( `{ "compilerOptions": { - "lib": "es5" + "lib": ["es5"] }, "excludes": [ "foge.ts" From aa5c51c51677600f7ac1960f7d953864b70275bc Mon Sep 17 00:00:00 2001 From: Richard Knoll Date: Thu, 26 May 2016 10:17:43 -0700 Subject: [PATCH 23/84] Fixing lint errors --- src/compiler/sys.ts | 4 ++-- tests/cases/unittests/cachingInServerLSHost.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/compiler/sys.ts b/src/compiler/sys.ts index bdbe04e904457..de7d24b229617 100644 --- a/src/compiler/sys.ts +++ b/src/compiler/sys.ts @@ -425,10 +425,10 @@ namespace ts { function directoryExists(path: string): boolean { return fileSystemEntryExists(path, FileSystemEntryKind.Directory); } - + function getDirectories(path: string): string[] { return filter(_fs.readdirSync(path), p => fileSystemEntryExists(combinePaths(path, p), FileSystemEntryKind.Directory)); - } + } return { args: process.argv.slice(2), diff --git a/tests/cases/unittests/cachingInServerLSHost.ts b/tests/cases/unittests/cachingInServerLSHost.ts index b58ca9898bc7c..65f3cb1ed9b97 100644 --- a/tests/cases/unittests/cachingInServerLSHost.ts +++ b/tests/cases/unittests/cachingInServerLSHost.ts @@ -82,7 +82,7 @@ namespace ts { const projectService = new server.ProjectService(serverHost, logger); const rootScriptInfo = projectService.openFile(rootFile, /* openedByClient */true); const project = projectService.createInferredProject(rootScriptInfo); - project.setProjectOptions( {files: [rootScriptInfo.fileName], compilerOptions: {module: ts.ModuleKind.AMD} } ); + project.setProjectOptions( {files: [rootScriptInfo.fileName], compilerOptions: {module: ts.ModuleKind.AMD} } ); return { project, rootScriptInfo @@ -145,7 +145,7 @@ namespace ts { catch (e) { assert.isTrue(e.message.indexOf(`Could not find file: '${imported.name}'.`) === 0); } - + assert.isTrue(fileExistsIsCalled); } { From 0415b95fd268ffdf1f14f045dc99d5cefb3e8878 Mon Sep 17 00:00:00 2001 From: Richard Knoll Date: Tue, 31 May 2016 10:11:04 -0700 Subject: [PATCH 24/84] Passing regular expressions to native hosts --- src/compiler/commandLineParser.ts | 5 ++-- src/compiler/core.ts | 38 +++++++++++++++++++++++++------ src/compiler/sys.ts | 7 ++++-- src/services/shims.ts | 11 ++++++--- 4 files changed, 47 insertions(+), 14 deletions(-) diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index d21fcd3b89d39..7674dcdd06380 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -1021,13 +1021,14 @@ namespace ts { // // /a/b/* - Watch /a/b directly to catch any new file // /a/b/a?z - Watch /a/b directly to catch any new file matching a?z - const excludeRegExp = getRegularExpressionForWildcard(exclude, path, "exclude", useCaseSensitiveFileNames); + const rawExcludeRegex = getRegularExpressionForWildcard(exclude, path, "exclude"); + const excludeRegex = rawExcludeRegex && new RegExp(rawExcludeRegex, useCaseSensitiveFileNames ? "" : "i"); const wildcardDirectories: Map = {}; if (include !== undefined) { const recursiveKeys: string[] = []; for (const file of include) { const name = combinePaths(path, file); - if (excludeRegExp && excludeRegExp.test(name)) { + if (excludeRegex && excludeRegex.test(name)) { continue; } diff --git a/src/compiler/core.ts b/src/compiler/core.ts index cc3c2b2811476..02d018194311f 100644 --- a/src/compiler/core.ts +++ b/src/compiler/core.ts @@ -911,7 +911,7 @@ namespace ts { const reservedCharacterPattern = /[^\w\s\/]/g; const wildcardCharCodes = [CharacterCodes.asterisk, CharacterCodes.question]; - export function getRegularExpressionForWildcard(specs: string[], basePath: string, usage: "files" | "directories" | "exclude", useCaseSensitiveFileNames: boolean) { + export function getRegularExpressionForWildcard(specs: string[], basePath: string, usage: "files" | "directories" | "exclude") { if (specs === undefined || specs.length === 0) { return undefined; } @@ -978,7 +978,7 @@ namespace ts { return undefined; } - return new RegExp("^(" + pattern + (usage === "exclude" ? ")($|/)" : ")$"), useCaseSensitiveFileNames ? "" : "i"); + return "^(" + pattern + (usage === "exclude" ? ")($|/)" : ")$"); } function replaceWildcardCharacter(match: string) { @@ -990,15 +990,39 @@ namespace ts { directories: string[]; } - export function matchFiles(path: string, extensions: string[], excludes: string[], includes: string[], useCaseSensitiveFileNames: boolean, currentDirectory: string, getFileSystemEntries: (path: string) => FileSystemEntries): string[] { + interface FileMatcherPatterns { + includeFilePattern: string; + includeDirectoryPattern: string; + excludePattern: string; + basePaths: string[]; + } + + export function getFileMatcherPatterns(path: string, extensions: string[], excludes: string[], includes: string[], useCaseSensitiveFileNames: boolean, currentDirectory: string): FileMatcherPatterns { path = normalizePath(path); currentDirectory = normalizePath(currentDirectory); const absolutePath = combinePaths(currentDirectory, path); - const includeFileRegex = getRegularExpressionForWildcard(includes, absolutePath, "files", useCaseSensitiveFileNames); - const includeDirectoryRegex = getRegularExpressionForWildcard(includes, absolutePath, "directories", useCaseSensitiveFileNames); - const excludeRegex = getRegularExpressionForWildcard(excludes, absolutePath, "exclude", useCaseSensitiveFileNames); + + return { + includeFilePattern: getRegularExpressionForWildcard(includes, absolutePath, "files"), + includeDirectoryPattern: getRegularExpressionForWildcard(includes, absolutePath, "directories"), + excludePattern: getRegularExpressionForWildcard(excludes, absolutePath, "exclude"), + basePaths: getBasePaths(path, includes, useCaseSensitiveFileNames) + }; + } + + export function matchFiles(path: string, extensions: string[], excludes: string[], includes: string[], useCaseSensitiveFileNames: boolean, currentDirectory: string, getFileSystemEntries: (path: string) => FileSystemEntries): string[] { + path = normalizePath(path); + currentDirectory = normalizePath(currentDirectory); + + const patterns = getFileMatcherPatterns(path, extensions, excludes, includes, useCaseSensitiveFileNames, currentDirectory); + + const regexFlag = useCaseSensitiveFileNames ? "" : "i"; + const includeFileRegex = patterns.includeFilePattern && new RegExp(patterns.includeFilePattern, regexFlag); + const includeDirectoryRegex = patterns.includeDirectoryPattern && new RegExp(patterns.includeDirectoryPattern, regexFlag); + const excludeRegex = patterns.excludePattern && new RegExp(patterns.excludePattern, regexFlag); + const result: string[] = []; - for (const basePath of getBasePaths(path, includes, useCaseSensitiveFileNames)) { + for (const basePath of patterns.basePaths) { visitDirectory(basePath, combinePaths(currentDirectory, basePath)); } return result; diff --git a/src/compiler/sys.ts b/src/compiler/sys.ts index de7d24b229617..21a78ee1dbe24 100644 --- a/src/compiler/sys.ts +++ b/src/compiler/sys.ts @@ -73,7 +73,7 @@ namespace ts { readFile(path: string): string; writeFile(path: string, contents: string): void; getDirectories(path: string): string[]; - readDirectory(path: string, extensions?: string[], exclude?: string[], include?: string[]): string[]; + readDirectory(path: string, extensions?: string[], basePaths?: string[], excludeEx?: string, includeFileEx?: string, includeDirEx?: string): string[]; watchFile?(path: string, callback: FileWatcherCallback): FileWatcher; watchDirectory?(path: string, callback: DirectoryWatcherCallback, recursive?: boolean): FileWatcher; realpath(path: string): string; @@ -558,7 +558,10 @@ namespace ts { getExecutingFilePath: () => ChakraHost.executingFile, getCurrentDirectory: () => ChakraHost.currentDirectory, getDirectories: ChakraHost.getDirectories, - readDirectory: ChakraHost.readDirectory, + readDirectory: (path: string, extensions?: string[], excludes?: string[], includes?: string[]) => { + const pattern = getFileMatcherPatterns(path, extensions, excludes, includes, !!ChakraHost.useCaseSensitiveFileNames, ChakraHost.currentDirectory); + return ChakraHost.readDirectory(path, extensions, pattern.basePaths, pattern.excludePattern, pattern.includeFilePattern, pattern.includeDirectoryPattern); + }, exit: ChakraHost.quit, realpath }; diff --git a/src/services/shims.ts b/src/services/shims.ts index 032a2712fdb76..75448536bad7e 100644 --- a/src/services/shims.ts +++ b/src/services/shims.ts @@ -80,8 +80,9 @@ namespace ts { * @param exclude A JSON encoded string[] containing the paths to exclude * when enumerating the directory. */ - readDirectory(rootDir: string, extension: string, exclude?: string, include?: string, depth?: number): string; + readDirectory(rootDir: string, extension: string, basePaths?: string, excludeEx?: string, includeFileEx?: string, includeDirEx?: string, depth?: number): string; useCaseSensitiveFileNames?(): boolean; + getCurrentDirectory(): string; trace(s: string): void; } @@ -453,11 +454,15 @@ namespace ts { // Wrap the API changes for 2.0 release. This try/catch // should be removed once TypeScript 2.0 has shipped. try { + const pattern = getFileMatcherPatterns(rootDir, extensions, exclude, include, + this.shimHost.useCaseSensitiveFileNames(), this.shimHost.getCurrentDirectory()); return JSON.parse(this.shimHost.readDirectory( rootDir, JSON.stringify(extensions), - JSON.stringify(exclude), - JSON.stringify(include), + JSON.stringify(pattern.basePaths), + pattern.excludePattern, + pattern.includeFilePattern, + pattern.includeDirectoryPattern, depth )); } From d387050aaab9f635ff3543563aced5c8a9a844db Mon Sep 17 00:00:00 2001 From: zhengbli Date: Thu, 9 Jun 2016 15:30:55 -0700 Subject: [PATCH 25/84] 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 8b38d56c2b297..99ca9377d546b 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 1e04e97e4c5c1..659c021c6be42 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 9f361cb37039b..bd29dc65ac407 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 6f07e97990689..17fe4b758f218 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 26/84] 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 f8ab0330dad92..ccde539fafa36 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 47fa84dd9ce65..44062885677de 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 99ca9377d546b..06f32c4f4a23a 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 bd29dc65ac407..5af2989a717e5 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 018162d0fff1c..4ebf1aff5d636 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 27/84] add test and spit commandLineParser changes to another PR --- src/compiler/commandLineParser.ts | 16 +----- src/compiler/sys.ts | 55 +++++-------------- 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, 65 insertions(+), 69 deletions(-) diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index ccde539fafa36..7a73949cf7a6d 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 659c021c6be42..db4270ad271ca 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,40 +441,13 @@ namespace ts { directories.push(name); } } - catch (e) { } - } - } - 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)); + for (const current of directories) { + visitDirectory(current); } } } - 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 { args: process.argv.slice(2), newLine: _os.EOL, @@ -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 a386b3c1ff2c0..d43cd9f910df5 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 5af2989a717e5..572acc61a18fd 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 4ebf1aff5d636..4cdd331f1d9ad 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 17fe4b758f218..fa5fd378a0b31 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 cf382d094632c..d7b808f79b793 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 28/84] 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 628522c7aa61a..ea43e2a585352 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 cf382d094632c..bd049632b9d13 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 29/84] 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 044f42414b75e..26c58ebe75206 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 cae2af356a0cc..e87a9f0d47525 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 0000000000000..a4d28f5187857 --- /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 21390cc295530..b88c73e043918 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 0000000000000..5b8633312d9a9 --- /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 30/84] 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 8c48d984b7853..f9d0f44334742 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 65335117cf204..105401c001534 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 31/84] 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 28082914256a3..f35a53ce368e4 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 0000000000000..4cf58d97d4909 --- /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 0000000000000..a452c8080b75d --- /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 0000000000000..a695482e1fba3 --- /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 0000000000000..104b6dd9443d6 --- /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 0000000000000..e261db3a6670e --- /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 0000000000000..25909b6add4c2 --- /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 32/84] 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 f9d0f44334742..4155e2e195ff2 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 105401c001534..bb268b55da8ba 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 9f78d98629ef0..fbcdc5946560e 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 33/84] 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 4155e2e195ff2..e413c20f79c91 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 bb268b55da8ba..46e683fc0c896 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 fbcdc5946560e..d44023ac773be 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 34/84] 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 46e683fc0c896..cc28f0fcbd2dc 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 35/84] 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 0000000000000..8679689569a02 --- /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 0000000000000..380694474239a --- /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 0000000000000..234de28eabae5 --- /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 0000000000000..404162308b97d --- /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 36/84] 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 481fe1b0c3e8b..101778e732223 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 a31cf0cbe9382..b2f3755ec46be 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 db34bb7c6e861..b28223b7520ba 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 0988d51e11b26..741c9e54cada7 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 37/84] 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 0000000000000..2e1e2d4249987 --- /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 0000000000000..43f63e9ce778c --- /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 38/84] 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 0000000000000..b02caf3ceaf41 --- /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 0000000000000..d6f3cd4e9db63 --- /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 0000000000000..e0ef78c86e7cf --- /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 0000000000000..0a6c3e5a98423 --- /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 0000000000000..a5eeec95a3259 --- /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 0000000000000..2dc7e823516af --- /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 39/84] 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 47b0a53a8c31c..51ef0a6058c67 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 40/84] 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 0000000000000..4d70873bd74b6 --- /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 b02caf3ceaf41..6ab1ec82bd5bd 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 0000000000000..0cbc0d7371c2c --- /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 0a6c3e5a98423..ad112732cd261 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 41/84] 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 8bf6a572ec754..c20f95a56f2de 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 a4b1416ed291a..cac44994420ce 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 42/84] 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 0000000000000..2f3da304c9958 --- /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 0000000000000..265fdbe5321bc --- /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 43/84] 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 0000000000000..03f46ad110ac1 --- /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 0000000000000..e6320bc3f8e7f --- /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 0000000000000..ac185f8e265eb --- /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 0000000000000..8aa4449595ab1 --- /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 0000000000000..152307cc66903 --- /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 0000000000000..3e3f8cd7e5954 --- /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 44/84] 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 51ef0a6058c67..e343762ade05d 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 45/84] Clear out unused compiler options when transpiling --- src/services/services.ts | 10 +- tests/cases/unittests/transpile.ts | 223 +++++++++++++++++++++++++++-- 2 files changed, 219 insertions(+), 14 deletions(-) diff --git a/src/services/services.ts b/src/services/services.ts index c20f95a56f2de..19167a5f6e00e 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 cac44994420ce..c8025bfc0a248 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("Supports setting 'allowJs'", () => { + testCompilerOption({ allowJs: 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 } + 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 46/84] 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 01a807b3e7d5c..5544b9caae0fd 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 0000000000000..80b108bbd666f --- /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 e6320bc3f8e7f..0000000000000 --- 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 ac185f8e265eb..0000000000000 --- 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 0000000000000..eeb9ffa930245 --- /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 152307cc66903..0000000000000 --- 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 3e3f8cd7e5954..0000000000000 --- 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 d6f3cd4e9db63..0000000000000 --- 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 e0ef78c86e7cf..0000000000000 --- 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 a5eeec95a3259..0000000000000 --- 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 2dc7e823516af..0000000000000 --- 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 47/84] 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 a2fe006598340..d5f89bee8bf36 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 b78c342481f86..30a4e1575e5fd 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 48/84] 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 6f9aef927ead8..5edefd78ec514 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 0339dfad13a44..e1c5137e3a918 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 2ecca6cf2cf2e..3714bbb232301 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 e590ad42a7ddd..b6767a3b9388b 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 49/84] Use helper functions to simplify range tests --- src/harness/fourslash.ts | 113 +++++++++++++----- .../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, 267 insertions(+), 658 deletions(-) diff --git a/src/harness/fourslash.ts b/src/harness/fourslash.ts index 9f675e3ffd0dc..695e19b4667be 100644 --- a/src/harness/fourslash.ts +++ b/src/harness/fourslash.ts @@ -730,13 +730,78 @@ namespace FourSlash { } } - public verifyReferencesAtPositionListContains(fileName: string, start: number, end: number, isWriteAccess?: boolean, isDefinition?: boolean) { + public verifyReferencesCountIs(count: number, localFilesOnly = true) { const references = this.getReferencesAtCaret(); + let referencesCount = 0; + + if (localFilesOnly) { + const localFiles = this.testData.files.map(file => file.fileName); + // Count only the references in local files. Filter the ones in lib and other files. + ts.forEach(references, entry => { + if (localFiles.some((fileName) => fileName === entry.fileName)) { + referencesCount++; + } + }); + } + else { + referencesCount = references && references.length || 0; + } + + if (referencesCount !== count) { + const condition = localFilesOnly ? "excluding libs" : "including libs"; + this.raiseError("Expected references count (" + condition + ") to be " + count + ", but is actually " + referencesCount); + } + } + + 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) { @@ -752,29 +817,7 @@ namespace FourSlash { 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; - - if (localFilesOnly) { - const localFiles = this.testData.files.map(file => file.fileName); - // Count only the references in local files. Filter the ones in lib and other files. - ts.forEach(references, entry => { - if (localFiles.some((fileName) => fileName === entry.fileName)) { - referencesCount++; - } - }); - } - else { - referencesCount = references && references.length || 0; - } - if (referencesCount !== count) { - const condition = localFilesOnly ? "excluding libs" : "including libs"; - this.raiseError("Expected references count (" + condition + ") to be " + count + ", but is actually " + referencesCount); - } } private getMemberListAtCaret() { @@ -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 16fddd1869d78..2f0dbf914e770 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 3ea226b3ce4ce..065d34c6fc5eb 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 b9f6e9538eb83..37a9bd8470e6b 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 2f40af93c0203..39079294a3b5d 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 db773c74e3c31..19f31ece9b037 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 f753d17de49be..b9bbfa14a641c 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 45b008b55fe99..44b7ee0a06f59 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 6655138da3bc8..0f06792458dcb 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 12c187b4b0c07..cdd9b4f980c06 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 a312a277ebbb3..35d5c45e3d91e 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 cb5e702dc9068..cea4db81ea189 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 dcf7240312c1d..45e981d8a84ef 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 1ec144d7e8288..dd8fb1026c43b 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 0f06a3c4202b5..3fc41358dfdd2 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 951acdd336c39..f874bcbcb5cd4 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 b2755923d3708..358ba3f249d47 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 1ab92c251dac7..9fe36fbb48e67 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 9a46b08f35718..772fb78a2e885 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 bcd41331f7301..1ecb85bfae1aa 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 d4e02a36b0927..6d6dbb392bf76 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 dd4461751de73..9ab4c6242300b 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 7f585234082ac..63265eb944b1f 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 0d03561515d48..895b2168b238f 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 8651405155786..67a31f33d1eab 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 8dc2b1e7bb851..40ecf0139a72d 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 dfa0997774ef0..5696d242feda6 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 379d1d4d5f59b..8be45ac87e61c 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 6448d2396b3be..96f414dc06036 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 0b82c73e31d51..5696d242feda6 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 7b8be1aa91c3b..6ffa4b03b685f 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 6686a771b3474..3b8b5084a6bc8 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 4018698f4ef2a..8f30e3cc8530c 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 a450a77e2dc99..d9656e8b5cf1f 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 82fd67dfc9be6..dc48ffedb211c 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 357b355971de6..f5b0143ac4b2e 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 369258832a957..7cc47ee34eaf9 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 53adb8236a478..d1449016b5969 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 1793ec6ddc42d..e0d6f7f3456ae 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 694ecdc79ab09..5a8de54db4dec 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 a110597f4b769..4ab28f164fec1 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 97ed29eb178d6..191b0bdbc147d 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 afcaf625ede44..f3578193096e1 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 08ed3414366ad..15c66f4e7c677 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 a3923783c46cd..cc33b618c41c3 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 78de5b02358b4..dd8a61ac45814 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 eb9980c946d30..57b4c14ffba1c 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 9a30687c59e45..e60921ec3b861 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 04b1f90681a24..0a3c2e4231c9d 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 8896694db50b4..81df97eff2dd0 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 a5764206bce23..5b77bf6158aa9 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 f863af9184fc1..759df4fa383f1 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 456d953092dca..357ef088d6140 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 51d1e858185be..b7117995d8465 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 7efefa17a4b02..b0506490c7074 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 86b92ec9ce729..ff1cf00fa9b3c 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 7e3e084948f4c..a041dab435ac3 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 cdb0e281d53fe..cdcc47014fdfb 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 291aec90dda1c..383cf49a2e34d 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 44a7c64a93a2e..2bd66830c69a0 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 8d046c67e3a89..78529186ad35b 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 2907a46857710..3e523487bd13d 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 fe83f47757b5b..cada3e0fcf496 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 e2f61d9cab3ba..2db0325d32448 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 67b956dcd023a..275a2b2756c0b 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 3288aec9b92df..55a067ae8c338 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 08d04188ee9e7..34cad8f11e891 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 d5f0a2fd51616..07eebeb5f7434 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 7ae9fdec9d2bc..00f7ba268604d 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 7b09872119695..e982d6a4c7bd2 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 0ecbeb43de39a..528f79de5b49b 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 9a0d7095a932e..109b5e36e4d68 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 a25a6e1285f17..cb829807602e4 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 a25a6e1285f17..cb829807602e4 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 50/84] 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 db34bb7c6e861..255f1dff0c82e 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 51/84] 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 0000000000000..68055f8b20bd9 --- /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 52/84] 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 ca225d4561a3e..a824af2f81106 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 0000000000000..f4dafe334b5e5 --- /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 0000000000000..8bb0343b9c0f9 --- /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 0000000000000..a11a7033fb6ac --- /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 53/84] 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 b78c342481f86..46f1db9762502 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 15afbe182656c..111343e08ff26 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 fdaafa9aa516b..707d1537fc5c7 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 54/84] 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 fd5ea666459c1..0000000000000 --- 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 62a0058bcefa7..0000000000000 --- 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 55/84] 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 c8025bfc0a248..6c88c2558fc6c 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 56/84] 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 19167a5f6e00e..62a50db65ef5b 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 57/84] 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 615e843dcacea..346b2e00a109a 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 75abd94651514..65540082f6068 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 478d76347b17c30104c766c0d73b229464d73eff Mon Sep 17 00:00:00 2001 From: zhengbli Date: Thu, 16 Jun 2016 02:16:47 -0700 Subject: [PATCH 58/84] ignore casing when converting a source file path to relative path --- src/compiler/utilities.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 31b464b81cea4..f4b9592043eb1 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -2330,7 +2330,9 @@ namespace ts { export function getSourceFilePathInNewDir(sourceFile: SourceFile, host: EmitHost, newDirPath: string) { let sourceFilePath = getNormalizedAbsolutePath(sourceFile.fileName, host.getCurrentDirectory()); - sourceFilePath = sourceFilePath.replace(host.getCommonSourceDirectory(), ""); + const commonSourceDirectory = host.getCommonSourceDirectory(); + const isSourceFileInCommonSourceDirectory = sourceFilePath.toLowerCase().indexOf(commonSourceDirectory.toLowerCase()) === 0; + sourceFilePath = isSourceFileInCommonSourceDirectory ? sourceFilePath.substring(commonSourceDirectory.length) : sourceFilePath; return combinePaths(newDirPath, sourceFilePath); } From c721b5f981de8ca1bd6e7b71fb5f17d9368d996f Mon Sep 17 00:00:00 2001 From: Herrington Darkholme Date: Fri, 17 Jun 2016 01:19:34 +0800 Subject: [PATCH 59/84] 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 d5f89bee8bf36..b0484fc890cfe 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 36f42167d1a29..883680619ed41 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 60/84] 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 | 471 +++++++----------- 30 files changed, 334 insertions(+), 287 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 0000000000000..981e2e706cb1c --- /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 0000000000000..61a703e13bbba --- /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 0000000000000..6fbdba6f2c6ce --- /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 0000000000000..9d108d6331391 --- /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 0000000000000..9eadd1f271797 --- /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 0000000000000..88d98628eee0c --- /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 0000000000000..1ceb1bcd1464c --- /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 0000000000000..61a703e13bbba --- /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 0000000000000..61a703e13bbba --- /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 0000000000000..a0dd948c9fc05 --- /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 0000000000000..8f3fcaa8559f3 --- /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 0000000000000..94a7249678f01 --- /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 0000000000000..d7d6eb6930028 --- /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 0000000000000..1ceb1bcd1464c --- /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 0000000000000..d7d6eb6930028 --- /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 0000000000000..1ceb1bcd1464c --- /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 0000000000000..d7d6eb6930028 --- /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 0000000000000..1ceb1bcd1464c --- /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 0000000000000..d7d6eb6930028 --- /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 0000000000000..1ceb1bcd1464c --- /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 0000000000000..683266f8383ff --- /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 0000000000000..36c68f08b9f8e --- /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 0000000000000..36c68f08b9f8e --- /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 0000000000000..942449753b0dc --- /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 0000000000000..3923d3c9a4176 --- /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 0000000000000..407ee216d5844 --- /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 0000000000000..bab9c3c444332 --- /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 0000000000000..c17099d84ba28 --- /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 0000000000000..baa27ee64ce18 --- /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 cac44994420ce..3f29002e6a218 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 test(input: string, testSettings: TranspileTestSettings): void { - - 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; - } - - const canUseOldTranspile = !transpileOptions.renamedDependencies; + 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[]; + + before(() => { + 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; + } + + transpileOptions.compilerOptions.sourceMap = true; + + if (!transpileOptions.fileName) { + transpileOptions.fileName = transpileOptions.compilerOptions.jsx ? "file.tsx" : "file.ts"; + } + + transpileOptions.reportDiagnostics = true; + + justName = "transpile/" + name.replace(/[^a-z0-9\-. ]/ig, "") + (transpileOptions.compilerOptions.jsx ? ".tsx" : ".ts"); + toBeCompiled = [{ + unitName: transpileOptions.fileName, + content: input + }]; + + canUseOldTranspile = !transpileOptions.renamedDependencies; + transpileResult = transpileModule(input, transpileOptions); + + if (canUseOldTranspile) { + oldTranspileDiagnostics = []; + oldTranspileResult = transpile(input, transpileOptions.compilerOptions, transpileOptions.fileName, oldTranspileDiagnostics, transpileOptions.moduleName); + } + }); - transpileOptions.reportDiagnostics = true; - const transpileModuleResult = transpileModule(input, transpileOptions); + after(() => { + justName = undefined; + transpileOptions = undefined; + canUseOldTranspile = undefined; + toBeCompiled = undefined; + transpileResult = undefined; + oldTranspileResult = undefined; + oldTranspileDiagnostics = undefined; + }); - checkDiagnostics(transpileModuleResult.diagnostics, testSettings.expectedDiagnosticCodes, testSettings.expectedDiagnosticTexts); + 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 */ + } - if (testSettings.expectedOutput !== undefined) { - assert.equal(transpileModuleResult.outputText, testSettings.expectedOutput); - } + return Harness.Compiler.getErrorBaseline(toBeCompiled, transpileResult.diagnostics); + }); + }); - 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); + 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 */ + } + + return Harness.Compiler.getErrorBaseline(toBeCompiled, oldTranspileDiagnostics); + }); + }); } - } - // check source maps - if (!transpileOptions.compilerOptions) { - transpileOptions.compilerOptions = {}; - } + it("Correct output for " + justName, () => { + Harness.Baseline.runBaseline("Correct output", justName.replace(/\.tsx?$/, ".js"), () => { + if (transpileResult.outputText) { + return transpileResult.outputText; + } + else { + // This can happen if compiler recieve invalid compiler-options + /* tslint:disable:no-null-keyword */ + return null; + /* tslint:enable:no-null-keyword */ + } + }); + }); - 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); + if (canUseOldTranspile) { + it("Correct output (old transpile) for " + justName, () => { + Harness.Baseline.runBaseline("Correct output", justName.replace(/\.tsx?$/, ".oldTranspile.js"), () => { + return oldTranspileResult; + }); + }); + } + }); + } - const expectedSourceMapFileName = removeFileExtension(getBaseFileName(normalizeSlashes(transpileOptions.fileName))) + ".js.map"; - const expectedSourceMappingUrlLine = `//# sourceMappingURL=${expectedSourceMapFileName}`; + transpilesCorrectly("Generates no diagnostics with valid inputs", `var x = 0;`, { + options: { compilerOptions: { module: ModuleKind.CommonJS } } + }); - 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); - } - } + 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 } } + }); - it("Generates no diagnostics with valid inputs", () => { - // No errors - test(`var x = 0;`, { options: { compilerOptions: { module: ModuleKind.CommonJS } } }); + transpilesCorrectly("Generates expected syntactic diagnostics", `a b`, { + options: { compilerOptions: { module: ModuleKind.CommonJS } } }); - it("Generates no diagnostics for missing file references", () => { - test(`/// -var x = 0;`, - { options: { compilerOptions: { module: ModuleKind.CommonJS } } }); + transpilesCorrectly("Does not generate semantic diagnostics", `var x: string = 0;`, { + options: { compilerOptions: { module: ModuleKind.CommonJS } } }); - it("Generates no diagnostics for missing module imports", () => { - test(`import {a} from "module2";`, - { options: { compilerOptions: { module: ModuleKind.CommonJS } } }); + transpilesCorrectly("Generates module output", `var x = 0;`, { + options: { compilerOptions: { module: ModuleKind.AMD } } }); - it("Generates expected syntactic diagnostics", () => { - test(`a b`, - { options: { compilerOptions: { module: ModuleKind.CommonJS } }, expectedDiagnosticCodes: [1005] }); /// 1005: ';' Expected + transpilesCorrectly("Uses correct newLine character", `var x = 0;`, { + options: { compilerOptions: { module: ModuleKind.CommonJS, newLine: NewLineKind.LineFeed } } }); - it("Does not generate semantic diagnostics", () => { - test(`var x: string = 0;`, - { options: { compilerOptions: { module: ModuleKind.CommonJS } } }); + transpilesCorrectly("Sets module name", "var x = 1;", { + options: { compilerOptions: { module: ModuleKind.System, newLine: NewLineKind.LineFeed }, moduleName: "NamedModule" } }); - 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` - }); + transpilesCorrectly("No extra errors for file without extension", `"use strict";\r\nvar x = 0;`, { + options: { compilerOptions: { module: ModuleKind.CommonJS }, fileName: "file" } }); - it("Uses correct newLine character", () => { - test(`var x = 0;`, - { - options: { compilerOptions: { module: ModuleKind.CommonJS, newLine: NewLineKind.LineFeed } }, - expectedOutput: `"use strict";\nvar x = 0;\n` - }); + 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" } } }); - 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 - }); + 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" } } }); - it("No extra errors for file without extension", () => { - test(`"use strict";\r\nvar x = 0;`, { options: { compilerOptions: { module: ModuleKind.CommonJS }, fileName: "file" } }); + 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" } } }); - 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 - }); + 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, + } + } }); - 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 - }); + transpilesCorrectly("Supports backslashes in file name", "var x", { + options: { fileName: "a\\b.ts" } }); - 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 - }); + transpilesCorrectly("transpile file as 'tsx' if 'jsx' is specified", `var x =
`, { + options: { compilerOptions: { jsx: JsxEmit.React, newLine: NewLineKind.LineFeed } } }); - 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, - } - }, - expectedOutput: output - }); + transpilesCorrectly("transpile .js files", "const a = 10;", { + options: { compilerOptions: { newLine: NewLineKind.LineFeed, module: ModuleKind.CommonJS }, fileName: "input.js", reportDiagnostics: true } }); - it("Supports backslashes in file name", () => { - test("var x", { expectedOutput: `"use strict";\r\nvar x;\r\n`, options: { fileName: "a\\b.ts" }}); + transpilesCorrectly("Supports urls in file name", "var x", { + options: { fileName: "http://somewhere/directory//directory2/file.ts" } }); - 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 } } - }); + transpilesCorrectly("Accepts string as enum values for compile-options", "export const x = 0", { + options: { compilerOptions: { + module: "es6", + // Capitalization and spaces ignored + target: " Es6 " + }} }); - 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 } - }); + transpilesCorrectly("Report an error when compiler-options module-kind is out-of-range", "", { + options: { compilerOptions: { module: 123 }} }); - 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" } }); + transpilesCorrectly("Report an error when compiler-options target-script is out-of-range", "", { + options: { compilerOptions: { module: 123 }} }); - 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 } - }); + transpilesCorrectly("Report an error when compiler-options input is empty object", "", { + options: { compilerOptions: { module: {} }} }); - 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 } - }); + transpilesCorrectly("Report an error when compiler-options input is empty string", "", { + options: { compilerOptions: { module: "" }} }); - 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 " - } - }, - expectedOutput: "export const x = 0;\r\n" - }); - }); + transpilesCorrectly("Support options with lib values", "const a = 10;", { + options: { compilerOptions: { lib: ["es6", "dom"], module: ModuleKind.CommonJS }, fileName: "input.js", reportDiagnostics: true } + }); - 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'"] - }); - } - }); + 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 61/84] 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 b0484fc890cfe..6081770f62ef3 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) { - 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; + let parentClassExpression = errorLocation; + while (parentClassExpression) { + const kind = parentClassExpression.kind; + if (kind === SyntaxKind.Identifier || kind === SyntaxKind.PropertyAccessExpression) { + parentClassExpression = parentClassExpression.parent; + continue; } - if (interfaceOrModule.flags & SymbolFlags.Interface) { - error(errorLocation, Diagnostics.Cannot_extend_an_interface_0_Did_you_mean_implements, name); - return true; + if (kind === SyntaxKind.ExpressionWithTypeArguments) { + break; } + return false; } - else if (errorLocation.kind === SyntaxKind.PropertyAccessExpression) { - // todo + if (!parentClassExpression) { + return false; + } + 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 62/84] 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 6081770f62ef3..0cd9144e4be50 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 3714bbb232301..72b8466105c07 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 c06c4e66d8b69..b324f7382d820 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 0000000000000..a2f6e1f4b6ce8 --- /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 0000000000000..1b4fa112cc86e --- /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 0000000000000..9a87ddf51b673 --- /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 0000000000000..454154887003f --- /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 883680619ed41..eaab1d60fc408 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 0000000000000..4dce2ae59bda1 --- /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 0000000000000..4ea24e5dd7d8d --- /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 63/84] 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 a2f6e1f4b6ce8..64b160ea76653 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 1b4fa112cc86e..69e63f925707c 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 4dce2ae59bda1..002631afedf12 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 64/84] 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 | 1304 +++++++---------- 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 | 26 +- tests/cases/fourslash/navigationBarMerging.ts | 189 +++ .../cases/fourslash/navigationBarVariables.ts | 53 + .../server/jsdocTypedefTagNavigateTo.ts | 48 +- 27 files changed, 1380 insertions(+), 1017 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 8a2040a83df07..10a0526d6ae1e 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 b2f3755ec46be..6e0a37c3d9fbf 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 864dac9fdaaa1..09cfa2ac739fc 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 346b2e00a109a..74dbb7a078513 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 65540082f6068..aafc02a2b0b6d 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 59dc4a3828394..e722f33bc703b 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); - } - - return getItemsWorker(getTopLevelNodes(sourceFile), createTopLevelItem); - - function getIndent(node: Node): number { - let indent = 1; // Global node is the only one with indent 0. - - 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); - - // fall through - case SyntaxKind.ClassDeclaration: - case SyntaxKind.EnumDeclaration: - case SyntaxKind.InterfaceDeclaration: - case SyntaxKind.FunctionDeclaration: - indent++; - } - - current = current.parent; - } + /** + * 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 + } - return indent; - } + export function getNavigationBarItems(sourceFile_: SourceFile): NavigationBarItem[] { + sourceFile = sourceFile_; + const result = map(topLevelItems(rootNavigationBarNode(sourceFile)), convertToTopLevelItem); + sourceFile = void 0; + return result; + } - function getChildNodes(nodes: Node[]): Node[] { - const childNodes: Node[] = []; + // 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 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; + function navigationBarNodeKind(n: NavigationBarNode): SyntaxKind { + return n.node.kind; + } - case SyntaxKind.ExportDeclaration: - // Handle named exports case e.g.: - // export {a, b as B} from "mod"; - if ((node).exportClause) { - forEach((node).exportClause.elements, visit); - } - 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); - } + function pushChild(parent: NavigationBarNode, child: NavigationBarNode): void { + if (parent.children !== void 0) { + parent.children.push(child); + } + else { + parent.children = [child]; + } + } - // 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; + /* + 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; + } - 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; - } - } + function addLeafNode(node: Node): void { + pushChild(parent, emptyNavigationBarNode(node)); + } - // for (let i = 0, n = nodes.length; i < n; i++) { - // let node = nodes[i]; + function emptyNavigationBarNode(node: Node): NavigationBarNode { + return { + node, + additionalNodes: void 0, + parent, + children: void 0, + indent: parent.indent + 1 + }; + } - // if (node.kind === SyntaxKind.ClassDeclaration || - // node.kind === SyntaxKind.EnumDeclaration || - // node.kind === SyntaxKind.InterfaceDeclaration || - // node.kind === SyntaxKind.ModuleDeclaration || - // node.kind === SyntaxKind.FunctionDeclaration) { + /** + * 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; + } - // childNodes.push(node); - // } - // else if (node.kind === SyntaxKind.VariableStatement) { - // childNodes.push.apply(childNodes, (node).declarations); - // } - // } - forEach(nodes, visit); - return sortNodes(childNodes); + /** 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 getTopLevelNodes(node: SourceFile): Node[] { - const topLevelNodes: Node[] = []; - topLevelNodes.push(node); - - addTopLevelNodes(node.statements, topLevelNodes); + function addNodeWithRecursiveChild(node: Node, child: Node): void { + startNode(node); + addChildrenRecursively(child); + endNode(); + } - return topLevelNodes; + /** 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; } - 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)); - } - else if (n1.name) { - return 1; + 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); + + // Parameter properties are children of the class, not the constructor. + for (const param of ctr.parameters) { + if (isParameterPropertyDeclaration(param)) { + addLeafNode(param); + } } - else if (n2.name) { - return -1; + break; + + case SyntaxKind.MethodDeclaration: + case SyntaxKind.GetAccessor: + case SyntaxKind.SetAccessor: + case SyntaxKind.MethodSignature: + if (!hasDynamicName((node))) { + addNodeWithRecursiveChild(node, (node).body); } - else { - return n1.kind - n2.kind; - } - }); - - // 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; - } - } + break; - function addTopLevelNodes(nodes: Node[], topLevelNodes: Node[]): void { - nodes = sortNodes(nodes); - - 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); - } - } - } - 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; + case SyntaxKind.PropertyDeclaration: + case SyntaxKind.PropertySignature: + if (!hasDynamicName((node))) { + addLeafNode(node); } - } - } - - function hasNamedFunctionDeclarations(nodes: NodeArray): boolean { - for (const s of nodes) { - if (s.kind === SyntaxKind.FunctionDeclaration && !isEmpty((s).name.text)) { - return true; + break; + + case SyntaxKind.ImportClause: + let importClause = node; + // Handle default import case e.g.: + // import d from "mod"; + if (importClause.name !== void 0) { + addLeafNode(importClause); } - } - 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; - } - // 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; + // 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); } - - // 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; + for (const element of (namedBindings).elements) { + addLeafNode(element); } } } - } - - 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); - } - } + break; + + case SyntaxKind.BindingElement: + case SyntaxKind.VariableDeclaration: + const decl = node; + const name = decl.name; + if (isBindingPattern(name)) { + addChildrenRecursively(name); } - } - - 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 = []; + else if (decl.initializer !== void 0 && isFunctionOrClassExpression(decl.initializer)) { + // For `const x = function() {}`, just use the function node, not the const. + addChildrenRecursively(decl.initializer); } - - // 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); + else { + addNodeWithRecursiveChild(decl, decl.initializer); } - } - } - - function createChildItem(node: Node): ts.NavigationBarItem { - switch (node.kind) { - case SyntaxKind.Parameter: - if (isBindingPattern((node).name)) { - break; - } - if ((node.flags & NodeFlags.Modifier) === 0) { - return undefined; + break; + + case SyntaxKind.ArrowFunction: + case SyntaxKind.FunctionDeclaration: + case SyntaxKind.FunctionExpression: + addNodeWithRecursiveChild(node, (node).body); + break; + + case SyntaxKind.EnumDeclaration: + startNode(node); + for (const member of (node).members) { + if (!isComputedProperty(member)) { + addLeafNode(member); } - 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); - - 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; + } + endNode(); + break; + + 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); + } } - 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.Constructor: - return createItem(node, "constructor", ts.ScriptElementKind.constructorImplementationElement); - - case SyntaxKind.ExportSpecifier: - case SyntaxKind.ImportSpecifier: - case SyntaxKind.ImportEqualsDeclaration: - case SyntaxKind.ImportClause: - case SyntaxKind.NamespaceImport: - return createItem(node, getTextOfNode((node).name), ts.ScriptElementKind.alias); - } - - return undefined; - - function createItem(node: Node, name: string, scriptElementKind: string): NavigationBarItem { - return getNavigationBarItem(name, scriptElementKind, getNodeModifiers(node), [getNodeSpan(node)]); - } - } - - function isEmpty(text: string) { - return !text || text.trim() === ""; - } - - function getNavigationBarItem(text: string, kind: string, kindModifiers: string, spans: TextSpan[], childItems: NavigationBarItem[] = [], indent = 0): NavigationBarItem { - if (isEmpty(text)) { - return undefined; - } + } - return { - text, - kind, - kindModifiers, - spans, - childItems, - indent, - bolded: false, - grayed: false - }; + forEachChild(node, addChildrenRecursively); } + } - 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); + /** 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; } - 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)); + const itemsWithSameName = getProperty(nameToItems, name); + if (itemsWithSameName === void 0) { + nameToItems[name] = child; + return true; } - 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)); + if (itemsWithSameName instanceof Array) { + for (const itemWithSameName of itemsWithSameName) { + if (tryMerge(itemWithSameName, child)) { + return false; + } } - - return undefined; + itemsWithSameName.push(child); + return true; } - - 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)); + else { + const itemWithSameName = itemsWithSameName; + if (tryMerge(itemWithSameName, child)) { + return false; } - - return undefined; + nameToItems[name] = [itemWithSameName, child]; + return true; } - 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 tryMerge(a: NavigationBarNode, b: NavigationBarNode): boolean { + if (shouldReallyMerge(a.node, b.node)) { + merge(a, b); + return true; + } + return false; } + }); - 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))); - } + /** 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)); - childItems = getItemsWorker(sortNodes(nodes), createChildItem); + // 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; } - - const nodeName = !node.name ? "default" : node.name.text; - - return getNavigationBarItem( - nodeName, - ts.ScriptElementKind.classElement, - getNodeModifiers(node), - [getNodeSpan(node)], - childItems, - getIndent(node)); + if (a.body.kind !== SyntaxKind.ModuleDeclaration) { + return true; + } + return areSameModule(a.body, b.body); } + } - 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)); + /** 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); } - 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)); + target.children = concatenate(target.children, source.children); + if (target.children !== void 0) { + mergeChildren(target.children); + sortChildren(target.children); } } + } - 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); + /** Recursively ensure that each NavNode's children are in sorted order. */ + function sortChildren(children: NavigationBarNode[]): void { + children.sort(compareChildren); + } - while (moduleDeclaration.body && moduleDeclaration.body.kind === SyntaxKind.ModuleDeclaration) { - moduleDeclaration = moduleDeclaration.body; + 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); + } + } - result.push(moduleDeclaration.name.text); + // 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 result.join("."); } - - function removeComputedProperties(node: EnumDeclaration): Declaration[] { - return filter(node.members, member => member.name === undefined || member.name.kind !== SyntaxKind.ComputedPropertyName); + 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); } - /** - * Like removeComputedProperties, but retains the properties with well known symbol names - */ - function removeDynamicallyNamedProperties(node: ClassDeclaration | InterfaceDeclaration): Declaration[] { - return filter(node.members, member => !hasDynamicName(member)); + 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 getInnermostModule(node: ModuleDeclaration): ModuleDeclaration { - while (node.body && node.body.kind === SyntaxKind.ModuleDeclaration) { - node = node.body; - } - - return node; + function getItemName(node: Node): string { + if (node.kind === SyntaxKind.ModuleDeclaration) { + return getModuleName(node); } - function getNodeSpan(node: Node) { - return node.kind === SyntaxKind.SourceFile - ? createTextSpanFromBounds(node.getFullStart(), node.getEnd()) - : createTextSpanFromBounds(node.getStart(), node.getEnd()); + const name = (node).name; + if (name !== void 0) { + const text = nodeText(name); + if (text.length > 0) { + return text; + } } - function getTextOfNode(node: Node): string { - return getTextOfNodeFromSourceText(sourceFile.text, node); + 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 ""; } } - export function getJsNavigationBarItems(sourceFile: SourceFile, compilerOptions: CompilerOptions): NavigationBarItem[] { - const anonFnText = ""; - const anonClassText = ""; - let indent = 0; - - const rootName = isExternalModule(sourceFile) ? - "\"" + escapeString(getBaseFileName(removeFileExtension(normalizePath(sourceFile.fileName)))) + "\"" - : ""; - - const sourceFileItem = getNavBarItem(rootName, ScriptElementKind.moduleElement, [getNodeSpan(sourceFile)]); - let topItem = sourceFileItem; - - // 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); + 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; + } } } + return ""; + } + } - // 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(); + /** 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); + } } } - else { - forEachChild(node, visitNode); - } } + recur(root); + return topLevel; - 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 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: + function isTopLevel(item: NavigationBarNode): boolean { + switch (navigationBarNodeKind(item)) { case SyntaxKind.ClassDeclaration: + 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: + case SyntaxKind.MethodDeclaration: 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: + return hasSomeImportantChild(item); + 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)]); - } - } - } - } + case SyntaxKind.FunctionDeclaration: + case SyntaxKind.FunctionExpression: + return isTopLevelFunctionDeclaration(item); + default: - return undefined; + return false; + } + function isTopLevelFunctionDeclaration(item: NavigationBarNode): boolean { + if ((item.node).body === void 0) { + return false; + } + + 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 getNavBarItem(text: string, kind: string, spans: TextSpan[], kindModifiers = ScriptElementKindModifier.none): NavigationBarItem { + // NavigationBarItem requires an array, but will not mutate it, so just give it this for performance. + const emptyChildItemArray: NavigationBarItem[] = []; + + 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, bolded: false, grayed: false + text: getItemName(n.node), + kind: nodeKind(n.node), + kindModifiers: getNodeModifiers(n.node), + spans: getSpans(n), + childItems: emptyChildItemArray, + indent: 0, + 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; + 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 getNavBarItem(defaultName, ScriptElementKind.moduleElement, [getNodeSpan(node.parent)]); + return spans; } + } - 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); + // 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; + + case SyntaxKind.EnumMember: + return ScriptElementKind.memberVariableElement; + + 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 !== void 0); } - // 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(); + else { + Debug.assert(!isBindingPattern((node).name)); + variableDeclarationNode = node; + name = (node).name; } - // 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(); + + if (isConst(variableDeclarationNode)) { + return ts.ScriptElementKind.constElement; + } + else if (isLet(variableDeclarationNode)) { + return ts.ScriptElementKind.letElement; } else { - fnName = node.kind === SyntaxKind.ClassExpression ? anonClassText : anonFnText; + return ts.ScriptElementKind.variableElement; } - } - const scriptKind = node.kind === SyntaxKind.ClassExpression ? ScriptElementKind.classElement : ScriptElementKind.functionElement; - return getNavBarItem(fnName, scriptKind, [getNodeSpan(node)]); + + case SyntaxKind.ArrowFunction: + return ts.ScriptElementKind.functionElement; + + case SyntaxKind.JSDocTypedefTag: + return ScriptElementKind.typeElement; + + default: + return getNodeKind(node); } + } - function getNodeSpan(node: Node) { - return node.kind === SyntaxKind.SourceFile - ? createTextSpanFromBounds(node.getFullStart(), node.getEnd()) - : createTextSpanFromBounds(node.getStart(), node.getEnd()); + function getModuleName(moduleDeclaration: ModuleDeclaration): string { + // We want to maintain quotation marks. + if (isAmbientModule(moduleDeclaration)) { + return getTextOfNode(moduleDeclaration.name); } - 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"; - } + // 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("."); + } + + /** + * 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) ? "" : ""; + } + } - return sourceFileItem.childItems; + 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 983ddfbf250f6..163be48695dfb 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 e259a7bef901f..10e4027ff84c7 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 dc99cff7d649c..e547c9129a69d 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 0000000000000..f15959812fc06 --- /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 0000000000000..214a14949ac5e --- /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 0000000000000..48105e4e9af3a --- /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 0000000000000..43cb99c556a7e --- /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 91546462a2572..f5aa3fb681dd5 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 96dcbb944ae07..2f17a5006ff5f 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 127bde8c9e71d..f907bdad06719 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 28ec25c6f1dd2..3af3b351114b1 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 762531d8778b5..6ab0827e8f959 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 276e19624c92a..0000000000000 --- 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 af0e89683bcfe..2a445a5e1ed61 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 2eda3b07855c7..a878c5be8455b 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 979f22084e587..ec11cd52b942c 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 2ee4c93a2f1fb..4a29d718f2e0a 100644 --- a/tests/cases/fourslash/navigationBarItemsMultilineStringIdentifiers.ts +++ b/tests/cases/fourslash/navigationBarItemsMultilineStringIdentifiers.ts @@ -30,20 +30,12 @@ verify.navigationBar([ "kind": "module", "childItems": [ { - "text": "Bar", - "kind": "class" - }, - { - "text": "Foo", - "kind": "interface" - }, - { - "text": "\"Multiline\\r\\nMadness\"", + "text": "\"Multiline\\\nMadness\"", "kind": "module", "kindModifiers": "declare" }, { - "text": "\"Multiline\\\nMadness\"", + "text": "\"Multiline\\r\\nMadness\"", "kind": "module", "kindModifiers": "declare" }, @@ -51,9 +43,35 @@ verify.navigationBar([ "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 20a235bce9505..a2d33e216bfb5 100644 --- a/tests/cases/fourslash/navigationBarJsDoc.ts +++ b/tests/cases/fourslash/navigationBarJsDoc.ts @@ -7,15 +7,31 @@ verify.navigationBar([ { - "text": "NumberLike", - "kind": "type" + "text": "", + "kind": "module", + "childItems": [ + { + "text": "NumberLike", + "kind": "type" + }, + { + "text": "x", + "kind": "const" + }, + { + "text": "x", + "kind": "type" + } + ] }, { - "text": "x", - "kind": "type" + "text": "NumberLike", + "kind": "type", + "indent": 1, }, { "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 0000000000000..192efe5db44b5 --- /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 0000000000000..42344c96f73df --- /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 77cd75aa44c77..1c617091f4c27 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 65/84] 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 5865e48575407..40cd883af6105 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 f5c77b8594d11..6db86d71b2ae6 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 86cde9e22204be835572216b10757dcf82af8050 Mon Sep 17 00:00:00 2001 From: Richard Knoll Date: Fri, 17 Jun 2016 16:56:23 -0700 Subject: [PATCH 66/84] Updating readDirectory for tsserverProjectSystem unit tests --- .../cases/unittests/tsserverProjectSystem.ts | 41 +++++++++---------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/tests/cases/unittests/tsserverProjectSystem.ts b/tests/cases/unittests/tsserverProjectSystem.ts index cf382d094632c..dcefd491fff9f 100644 --- a/tests/cases/unittests/tsserverProjectSystem.ts +++ b/tests/cases/unittests/tsserverProjectSystem.ts @@ -90,23 +90,6 @@ namespace ts { } } - function readDirectory(folder: FSEntry, ext: string, excludes: Path[], result: string[]): void { - if (!folder || !isFolder(folder) || contains(excludes, folder.path)) { - return; - } - for (const entry of folder.entries) { - if (contains(excludes, entry.path)) { - continue; - } - if (isFolder(entry)) { - readDirectory(entry, ext, excludes, result); - } - else if (fileExtensionIs(entry.path, ext)) { - result.push(entry.fullPath); - } - } - } - function checkNumberOfConfiguredProjects(projectService: server.ProjectService, expected: number) { assert.equal(projectService.configuredProjects.length, expected, `expected ${expected} configured project(s)`); } @@ -188,10 +171,26 @@ namespace ts { } } - readDirectory(path: string, ext: string, excludes: string[]): string[] { - const result: string[] = []; - readDirectory(this.fs.get(this.toPath(path)), ext, map(excludes, e => toPath(e, path, this.getCanonicalFileName)), result); - return result; + readDirectory(path: string, extensions?: string[], exclude?: string[], include?: string[]): string[] { + const that = this; + return ts.matchFiles(path, extensions, exclude, include, this.useCaseSensitiveFileNames, this.getCurrentDirectory(), (dir) => { + const result: FileSystemEntries = { + directories: [], + files : [] + }; + const dirEntry = that.fs.get(that.toPath(dir)); + if (isFolder(dirEntry)) { + dirEntry.entries.forEach((entry) => { + if (isFolder(entry)) { + result.directories.push(entry.fullPath); + } + else if (isFile(entry)) { + result.files.push(entry.fullPath); + } + }); + } + return result; + }); } watchDirectory(directoryName: string, callback: DirectoryWatcherCallback, recursive: boolean): DirectoryWatcher { From e9226af3f6495b9eff8f40554f5a51e0c2d0b46d Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Fri, 17 Jun 2016 17:18:16 -0700 Subject: [PATCH 67/84] 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 111343e08ff26..a01f3a4c5b240 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 95072aab824ba8cc591c778efc7c0e622afa2cd2 Mon Sep 17 00:00:00 2001 From: Richard Knoll Date: Fri, 17 Jun 2016 17:11:13 -0700 Subject: [PATCH 68/84] Responding to PR feedback --- src/compiler/commandLineParser.ts | 8 ++++---- src/compiler/core.ts | 2 +- src/compiler/sys.ts | 10 +++++++++- tests/cases/unittests/cachingInServerLSHost.ts | 2 +- 4 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index 2b83690372a33..9cca35977be4e 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -672,7 +672,7 @@ namespace ts { // Skip over any minified JavaScript files (ending in ".min.js") // Skip over dotted files and folders as well - const IgnoreFileNamePattern = /(\.min\.js$)|([\\/]\.[\w.])/; + const ignoreFileNamePattern = /(\.min\.js$)|([\\/]\.[\w.])/; /** * Parse the contents of a config file (tsconfig.json). * @param json The contents of the config file to parse @@ -969,7 +969,7 @@ namespace ts { continue; } - if (IgnoreFileNamePattern.test(file)) { + if (ignoreFileNamePattern.test(file)) { continue; } @@ -1055,8 +1055,8 @@ namespace ts { // Remove any subpaths under an existing recursively watched directory. for (const key in wildcardDirectories) { if (hasProperty(wildcardDirectories, key)) { - for (const recursiveKey in recursiveKeys) { - if (containsPath(recursiveKey, key, path, !useCaseSensitiveFileNames)) { + for (const recursiveKey of recursiveKeys) { + if (key !== recursiveKey && containsPath(recursiveKey, key, path, !useCaseSensitiveFileNames)) { delete wildcardDirectories[key]; } } diff --git a/src/compiler/core.ts b/src/compiler/core.ts index 56d8262a3ba1c..c41b4baa9dc2a 100644 --- a/src/compiler/core.ts +++ b/src/compiler/core.ts @@ -1211,7 +1211,7 @@ namespace ts { export function isJsxOrTsxExtension(ext: string): boolean { return ext === ".jsx" || ext === ".tsx"; } - + export function changeExtension(path: T, newExtension: string): T { return (removeFileExtension(path) + newExtension); } diff --git a/src/compiler/sys.ts b/src/compiler/sys.ts index 70fef01b50474..8b977c53111d2 100644 --- a/src/compiler/sys.ts +++ b/src/compiler/sys.ts @@ -381,7 +381,15 @@ namespace ts { continue; } const name = combinePaths(path, entry); - const stat = _fs.statSync(name); + + let stat: any; + try { + stat = _fs.statSync(name); + } + catch (e) { + continue; + } + if (stat.isFile()) { files.push(entry); } diff --git a/tests/cases/unittests/cachingInServerLSHost.ts b/tests/cases/unittests/cachingInServerLSHost.ts index 65f3cb1ed9b97..2608a082d6df6 100644 --- a/tests/cases/unittests/cachingInServerLSHost.ts +++ b/tests/cases/unittests/cachingInServerLSHost.ts @@ -82,7 +82,7 @@ namespace ts { const projectService = new server.ProjectService(serverHost, logger); const rootScriptInfo = projectService.openFile(rootFile, /* openedByClient */true); const project = projectService.createInferredProject(rootScriptInfo); - project.setProjectOptions( {files: [rootScriptInfo.fileName], compilerOptions: {module: ts.ModuleKind.AMD} } ); + project.setProjectOptions({ files: [rootScriptInfo.fileName], compilerOptions: { module: ts.ModuleKind.AMD } }); return { project, rootScriptInfo From f568ad0ce446557c2198f0892f76d9f7db63d709 Mon Sep 17 00:00:00 2001 From: Mohamed Hegazy Date: Sat, 18 Jun 2016 12:01:29 -0700 Subject: [PATCH 69/84] 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 40cd883af6105..0911586dd5d54 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 70/84] 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 f804dc4eb6a14..0b4a58260ec36 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 0000000000000..b65e133572ab7 --- /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 0000000000000..53502b040c3bc --- /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 0000000000000..aafd063846d81 --- /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 0000000000000..519485320ae4e --- /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 0000000000000..7da62387d2fde --- /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 71/84] 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 6db86d71b2ae6..56c8cc84367a8 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 72/84] 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 0000000000000..515b5d9632fc5 --- /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 0000000000000..ac27e7c83dcec --- /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 0000000000000..38accb398421f --- /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 73/84] 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 983ddfbf250f6..5dd73abe82f7d 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 0000000000000..5e0c9a9d4b666 --- /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 f73ed594327eeef48e391f2daebea83e32ca5170 Mon Sep 17 00:00:00 2001 From: Richard Knoll Date: Mon, 20 Jun 2016 11:09:48 -0700 Subject: [PATCH 74/84] Adding more matchFiles test cases --- tests/cases/unittests/matchFiles.ts | 189 ++++++++++++++++++++++++++++ 1 file changed, 189 insertions(+) diff --git a/tests/cases/unittests/matchFiles.ts b/tests/cases/unittests/matchFiles.ts index c2d13cf0393a3..b9c538a9e14dd 100644 --- a/tests/cases/unittests/matchFiles.ts +++ b/tests/cases/unittests/matchFiles.ts @@ -65,6 +65,16 @@ namespace ts { "c:/dev/f.other" ]); + const caseInsensitiveCommonFoldersHost = new Utils.MockParseConfigHost(caseInsensitiveBasePath, /*useCaseSensitiveFileNames*/ false, [ + "c:/dev/a.ts", + "c:/dev/a.d.ts", + "c:/dev/a.js", + "c:/dev/b.ts", + "c:/dev/node_modules/a.ts", + "c:/dev/bower_components/a.ts", + "c:/dev/jspm_packages/a.ts" + ]); + describe("matchFiles", () => { describe("with literal file list", () => { it("without exclusions", () => { @@ -297,6 +307,87 @@ namespace ts { assert.deepEqual(actual.wildcardDirectories, expected.wildcardDirectories); assert.deepEqual(actual.errors, expected.errors); }); + it("with common package folders and no exclusions", () => { + const json = { + include: [ + "a.ts", + "b.ts", + "node_modules/a.ts", + "bower_components/a.ts", + "jspm_packages/a.ts" + ] + }; + const expected: ts.ParsedCommandLine = { + options: {}, + errors: [], + fileNames: [ + "c:/dev/a.ts", + "c:/dev/b.ts" + ], + wildcardDirectories: {}, + }; + const actual = ts.parseJsonConfigFileContent(json, caseInsensitiveCommonFoldersHost, caseInsensitiveBasePath); + assert.deepEqual(actual.fileNames, expected.fileNames); + assert.deepEqual(actual.wildcardDirectories, expected.wildcardDirectories); + assert.deepEqual(actual.errors, expected.errors); + }); + it("with common package folders and exclusions", () => { + const json = { + include: [ + "a.ts", + "b.ts", + "node_modules/a.ts", + "bower_components/a.ts", + "jspm_packages/a.ts" + ], + exclude: [ + "a.ts", + "b.ts" + ] + }; + const expected: ts.ParsedCommandLine = { + options: {}, + errors: [], + fileNames: [ + "c:/dev/bower_components/a.ts", + "c:/dev/jspm_packages/a.ts", + "c:/dev/node_modules/a.ts" + ], + wildcardDirectories: {}, + }; + const actual = ts.parseJsonConfigFileContent(json, caseInsensitiveCommonFoldersHost, caseInsensitiveBasePath); + assert.deepEqual(actual.fileNames, expected.fileNames); + assert.deepEqual(actual.wildcardDirectories, expected.wildcardDirectories); + assert.deepEqual(actual.errors, expected.errors); + }); + it("with common package folders and empty exclude", () => { + const json = { + include: [ + "a.ts", + "b.ts", + "node_modules/a.ts", + "bower_components/a.ts", + "jspm_packages/a.ts" + ], + exclude: [] + }; + const expected: ts.ParsedCommandLine = { + options: {}, + errors: [], + fileNames: [ + "c:/dev/a.ts", + "c:/dev/b.ts", + "c:/dev/bower_components/a.ts", + "c:/dev/jspm_packages/a.ts", + "c:/dev/node_modules/a.ts" + ], + wildcardDirectories: {}, + }; + const actual = ts.parseJsonConfigFileContent(json, caseInsensitiveCommonFoldersHost, caseInsensitiveBasePath); + assert.deepEqual(actual.fileNames, expected.fileNames); + assert.deepEqual(actual.wildcardDirectories, expected.wildcardDirectories); + assert.deepEqual(actual.errors, expected.errors); + }); }); describe("with wildcard include list", () => { @@ -392,6 +483,32 @@ namespace ts { assert.deepEqual(actual.wildcardDirectories, expected.wildcardDirectories); assert.deepEqual(actual.errors, expected.errors); }); + it("with multiple recursive directories", () => { + const json = { + include: [ + "x/y/**/a.ts", + "x/**/a.ts", + "z/**/a.ts" + ] + }; + const expected: ts.ParsedCommandLine = { + options: {}, + errors: [], + fileNames: [ + "c:/dev/x/a.ts", + "c:/dev/x/y/a.ts", + "c:/dev/z/a.ts" + ], + wildcardDirectories: { + "c:/dev/x": ts.WatchDirectoryFlags.Recursive, + "c:/dev/z": ts.WatchDirectoryFlags.Recursive + }, + }; + const actual = ts.parseJsonConfigFileContent(json, caseInsensitiveHost, caseInsensitiveBasePath); + assert.deepEqual(actual.fileNames, expected.fileNames); + assert.deepEqual(actual.wildcardDirectories, expected.wildcardDirectories); + assert.deepEqual(actual.errors, expected.errors); + }); it("case sensitive", () => { const json = { include: [ @@ -486,6 +603,78 @@ namespace ts { assert.deepEqual(actual.wildcardDirectories, expected.wildcardDirectories); assert.deepEqual(actual.errors, expected.errors); }); + it("with common package folders and no exclusions", () => { + const json = { + include: [ + "**/a.ts" + ] + }; + const expected: ts.ParsedCommandLine = { + options: {}, + errors: [], + fileNames: [ + "c:/dev/a.ts" + ], + wildcardDirectories: { + "c:/dev": ts.WatchDirectoryFlags.Recursive + }, + }; + const actual = ts.parseJsonConfigFileContent(json, caseInsensitiveCommonFoldersHost, caseInsensitiveBasePath); + assert.deepEqual(actual.fileNames, expected.fileNames); + assert.deepEqual(actual.wildcardDirectories, expected.wildcardDirectories); + assert.deepEqual(actual.errors, expected.errors); + }); + it("with common package folders and exclusions", () => { + const json = { + include: [ + "**/a.ts" + ], + exclude: [ + "a.ts" + ] + }; + const expected: ts.ParsedCommandLine = { + options: {}, + errors: [], + fileNames: [ + "c:/dev/bower_components/a.ts", + "c:/dev/jspm_packages/a.ts", + "c:/dev/node_modules/a.ts" + ], + wildcardDirectories: { + "c:/dev": ts.WatchDirectoryFlags.Recursive + }, + }; + const actual = ts.parseJsonConfigFileContent(json, caseInsensitiveCommonFoldersHost, caseInsensitiveBasePath); + assert.deepEqual(actual.fileNames, expected.fileNames); + assert.deepEqual(actual.wildcardDirectories, expected.wildcardDirectories); + assert.deepEqual(actual.errors, expected.errors); + }); + it("with common package folders and empty exclude", () => { + const json = { + include: [ + "**/a.ts" + ], + exclude: [] + }; + const expected: ts.ParsedCommandLine = { + options: {}, + errors: [], + fileNames: [ + "c:/dev/a.ts", + "c:/dev/bower_components/a.ts", + "c:/dev/jspm_packages/a.ts", + "c:/dev/node_modules/a.ts" + ], + wildcardDirectories: { + "c:/dev": ts.WatchDirectoryFlags.Recursive + }, + }; + const actual = ts.parseJsonConfigFileContent(json, caseInsensitiveCommonFoldersHost, caseInsensitiveBasePath); + assert.deepEqual(actual.fileNames, expected.fileNames); + assert.deepEqual(actual.wildcardDirectories, expected.wildcardDirectories); + assert.deepEqual(actual.errors, expected.errors); + }); it("exclude .js files when allowJs=false", () => { const json = { compilerOptions: { From 95cfaafdee9127b66535abc22cbe6722ee7dbd3a Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Mon, 20 Jun 2016 11:30:48 -0700 Subject: [PATCH 75/84] 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 6e0a37c3d9fbf..b2f3755ec46be 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 e722f33bc703b..bfbaf5a6287ce 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. From d9bd31fc5288577100718c2d2278705b12dd0342 Mon Sep 17 00:00:00 2001 From: zhengbli Date: Mon, 20 Jun 2016 12:39:07 -0700 Subject: [PATCH 76/84] Use getCanonicalFileName --- src/compiler/utilities.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index f4b9592043eb1..b4ef62c6c3e22 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -2331,7 +2331,7 @@ namespace ts { export function getSourceFilePathInNewDir(sourceFile: SourceFile, host: EmitHost, newDirPath: string) { let sourceFilePath = getNormalizedAbsolutePath(sourceFile.fileName, host.getCurrentDirectory()); const commonSourceDirectory = host.getCommonSourceDirectory(); - const isSourceFileInCommonSourceDirectory = sourceFilePath.toLowerCase().indexOf(commonSourceDirectory.toLowerCase()) === 0; + const isSourceFileInCommonSourceDirectory = host.getCanonicalFileName(sourceFilePath).indexOf(host.getCanonicalFileName(commonSourceDirectory)) === 0; sourceFilePath = isSourceFileInCommonSourceDirectory ? sourceFilePath.substring(commonSourceDirectory.length) : sourceFilePath; return combinePaths(newDirPath, sourceFilePath); } From 8e489cb2a44d2dc7491a7ac760482edd432c86e3 Mon Sep 17 00:00:00 2001 From: Basarat Ali Syed Date: Tue, 21 Jun 2016 11:45:42 +1000 Subject: [PATCH 77/84] export interface used by other exported functions --- src/compiler/core.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/core.ts b/src/compiler/core.ts index ed171b720d0a2..4aba46c7839be 100644 --- a/src/compiler/core.ts +++ b/src/compiler/core.ts @@ -1001,7 +1001,7 @@ namespace ts { directories: string[]; } - interface FileMatcherPatterns { + export interface FileMatcherPatterns { includeFilePattern: string; includeDirectoryPattern: string; excludePattern: string; From cdb490bb4ff98ada1bb565071a2c16740df30395 Mon Sep 17 00:00:00 2001 From: Yui T Date: Tue, 21 Jun 2016 10:28:51 -0700 Subject: [PATCH 78/84] Fix from merging with master --- src/compiler/parser.ts | 4 +++- src/compiler/types.ts | 1 + src/services/navigationBar.ts | 4 ++-- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index d6f09ecc2371c..4b973d3a89e35 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -138,6 +138,7 @@ 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) || @@ -3567,7 +3568,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; - parseExpectedToken(SyntaxKind.DotToken, /*reportAtCurrentPosition*/ false, Diagnostics.super_must_be_followed_by_an_argument_list_or_member_access); + node.dotToken = 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); } @@ -3802,6 +3803,7 @@ 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 c016daf09d648..e203dca1b1504 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -1029,6 +1029,7 @@ namespace ts { // @kind(SyntaxKind.PropertyAccessExpression) export interface PropertyAccessExpression extends MemberExpression, Declaration { expression: LeftHandSideExpression; + dotToken: Node; name: Identifier; } diff --git a/src/services/navigationBar.ts b/src/services/navigationBar.ts index bfbaf5a6287ce..60fe70cdbde2b 100644 --- a/src/services/navigationBar.ts +++ b/src/services/navigationBar.ts @@ -397,7 +397,7 @@ namespace ts.NavigationBar { case SyntaxKind.FunctionExpression: case SyntaxKind.ClassDeclaration: case SyntaxKind.ClassExpression: - if (node.flags & NodeFlags.Default) { + if (getModifierFlags(node) & ModifierFlags.Default) { return "default"; } return getFunctionOrClassName(node); @@ -647,7 +647,7 @@ namespace ts.NavigationBar { return nodeText((node.parent as PropertyAssignment).name); } // Default exports are named "default" - else if (node.flags & NodeFlags.Default) { + else if (getModifierFlags(node) & ModifierFlags.Default) { return "default"; } else { From 595d2a12b4330f9305ec11d479ec3756e4229673 Mon Sep 17 00:00:00 2001 From: Yui T Date: Tue, 21 Jun 2016 10:29:05 -0700 Subject: [PATCH 79/84] Update tests and baselines from merging with master --- .../constDeclarations-useBeforeDefinition2.symbols | 9 --------- .../constDeclarations-useBeforeDefinition2.types | 10 ---------- .../letDeclarations-useBeforeDefinition2.symbols | 9 --------- .../letDeclarations-useBeforeDefinition2.types | 10 ---------- ...rror when compiler-options input is empty object.js | 2 -- ...rror when compiler-options input is empty string.js | 2 -- ...hen compiler-options module-kind is out-of-range.js | 2 -- ...n compiler-options target-script is out-of-range.js | 2 -- tests/cases/unittests/incrementalParser.ts | 2 +- 9 files changed, 1 insertion(+), 47 deletions(-) delete mode 100644 tests/baselines/reference/constDeclarations-useBeforeDefinition2.symbols delete mode 100644 tests/baselines/reference/constDeclarations-useBeforeDefinition2.types delete mode 100644 tests/baselines/reference/letDeclarations-useBeforeDefinition2.symbols delete mode 100644 tests/baselines/reference/letDeclarations-useBeforeDefinition2.types delete mode 100644 tests/baselines/reference/transpile/Report an error when compiler-options input is empty object.js delete mode 100644 tests/baselines/reference/transpile/Report an error when compiler-options input is empty string.js delete mode 100644 tests/baselines/reference/transpile/Report an error when compiler-options module-kind is out-of-range.js delete mode 100644 tests/baselines/reference/transpile/Report an error when compiler-options target-script is out-of-range.js diff --git a/tests/baselines/reference/constDeclarations-useBeforeDefinition2.symbols b/tests/baselines/reference/constDeclarations-useBeforeDefinition2.symbols deleted file mode 100644 index 281ce4277337f..0000000000000 --- a/tests/baselines/reference/constDeclarations-useBeforeDefinition2.symbols +++ /dev/null @@ -1,9 +0,0 @@ -=== tests/cases/compiler/file1.ts === - -c; ->c : Symbol(c, Decl(file2.ts, 0, 5)) - -=== tests/cases/compiler/file2.ts === -const c = 0; ->c : Symbol(c, Decl(file2.ts, 0, 5)) - diff --git a/tests/baselines/reference/constDeclarations-useBeforeDefinition2.types b/tests/baselines/reference/constDeclarations-useBeforeDefinition2.types deleted file mode 100644 index ae60fdfa47728..0000000000000 --- a/tests/baselines/reference/constDeclarations-useBeforeDefinition2.types +++ /dev/null @@ -1,10 +0,0 @@ -=== tests/cases/compiler/file1.ts === - -c; ->c : number - -=== tests/cases/compiler/file2.ts === -const c = 0; ->c : number ->0 : number - diff --git a/tests/baselines/reference/letDeclarations-useBeforeDefinition2.symbols b/tests/baselines/reference/letDeclarations-useBeforeDefinition2.symbols deleted file mode 100644 index c5a067ede4dd2..0000000000000 --- a/tests/baselines/reference/letDeclarations-useBeforeDefinition2.symbols +++ /dev/null @@ -1,9 +0,0 @@ -=== tests/cases/compiler/file1.ts === - -l; ->l : Symbol(l, Decl(file2.ts, 0, 5)) - -=== tests/cases/compiler/file2.ts === -const l = 0; ->l : Symbol(l, Decl(file2.ts, 0, 5)) - diff --git a/tests/baselines/reference/letDeclarations-useBeforeDefinition2.types b/tests/baselines/reference/letDeclarations-useBeforeDefinition2.types deleted file mode 100644 index 793a7a78ba71e..0000000000000 --- a/tests/baselines/reference/letDeclarations-useBeforeDefinition2.types +++ /dev/null @@ -1,10 +0,0 @@ -=== tests/cases/compiler/file1.ts === - -l; ->l : number - -=== tests/cases/compiler/file2.ts === -const l = 0; ->l : number ->0 : number - 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 deleted file mode 100644 index 1ceb1bcd1464c..0000000000000 --- a/tests/baselines/reference/transpile/Report an error when compiler-options input is empty object.js +++ /dev/null @@ -1,2 +0,0 @@ -"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.js b/tests/baselines/reference/transpile/Report an error when compiler-options input is empty string.js deleted file mode 100644 index 1ceb1bcd1464c..0000000000000 --- a/tests/baselines/reference/transpile/Report an error when compiler-options input is empty string.js +++ /dev/null @@ -1,2 +0,0 @@ -"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.js b/tests/baselines/reference/transpile/Report an error when compiler-options module-kind is out-of-range.js deleted file mode 100644 index 1ceb1bcd1464c..0000000000000 --- a/tests/baselines/reference/transpile/Report an error when compiler-options module-kind is out-of-range.js +++ /dev/null @@ -1,2 +0,0 @@ -"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.js b/tests/baselines/reference/transpile/Report an error when compiler-options target-script is out-of-range.js deleted file mode 100644 index 1ceb1bcd1464c..0000000000000 --- a/tests/baselines/reference/transpile/Report an error when compiler-options target-script is out-of-range.js +++ /dev/null @@ -1,2 +0,0 @@ -"use strict"; -//# sourceMappingURL=file.js.map \ No newline at end of file diff --git a/tests/cases/unittests/incrementalParser.ts b/tests/cases/unittests/incrementalParser.ts index 741c9e54cada7..0988d51e11b26 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, 7); + compareTrees(oldText, newTextAndChange.text, newTextAndChange.textChangeRange, 8); }); it("Class to interface", () => { From 3040f2dc6186b3a200a4edcb3d3a95e8a297f3ab Mon Sep 17 00:00:00 2001 From: Kanchalai Tanglertsampan Date: Tue, 28 Jun 2016 13:39:46 -0700 Subject: [PATCH 80/84] Remove using dotToken as it is no longer needed --- src/compiler/emitter.ts | 9 +-------- src/compiler/factory.ts | 7 +++---- src/compiler/parser.ts | 4 +--- src/compiler/types.ts | 1 - 4 files changed, 5 insertions(+), 16 deletions(-) diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 9f1f5b3cdbf4d..f5ed3733b10c3 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -1084,16 +1084,9 @@ const _super = (function (geti, seti) { return; } - const indentBeforeDot = needsIndentation(node, node.expression, node.dotToken); - const indentAfterDot = needsIndentation(node, node.dotToken, node.name); - const shouldEmitDotDot = !indentBeforeDot && needsDotDotForPropertyAccess(node.expression); - emitExpression(node.expression); - increaseIndentIf(indentBeforeDot); - write(shouldEmitDotDot ? ".." : "."); - increaseIndentIf(indentAfterDot); + write(needsDotDotForPropertyAccess(node.expression) ? ".." : "."); emit(node.name); - decreaseIndentIf(indentBeforeDot, indentAfterDot); } // 1..toString is a valid property access, emit a dot after the literal diff --git a/src/compiler/factory.ts b/src/compiler/factory.ts index f879f3660e405..c1b80f09f6c15 100644 --- a/src/compiler/factory.ts +++ b/src/compiler/factory.ts @@ -339,20 +339,19 @@ namespace ts { } export function createPropertyAccess(expression: Expression, name: string | Identifier, location?: TextRange) { - return createPropertyAccessWithDotToken(expression, createSynthesizedNode(SyntaxKind.DotToken), name, location, /*flags*/ undefined); + return createPropertyAccessWithDotToken(expression, name, location, /*flags*/ undefined); } - export function createPropertyAccessWithDotToken(expression: Expression, dotToken: Node, name: string | Identifier, location?: TextRange, flags?: NodeFlags) { + export function createPropertyAccessWithDotToken(expression: Expression, name: string | Identifier, location?: TextRange, flags?: NodeFlags) { const node = createNode(SyntaxKind.PropertyAccessExpression, location, flags); node.expression = parenthesizeForAccess(expression); - node.dotToken = dotToken; node.name = typeof name === "string" ? createIdentifier(name) : name; return node; } export function updatePropertyAccess(node: PropertyAccessExpression, expression: Expression, name: Identifier) { if (node.expression !== expression || node.name !== name) { - return updateNode(createPropertyAccessWithDotToken(expression, node.dotToken, name, /*location*/ node, node.flags), node); + return updateNode(createPropertyAccessWithDotToken(expression, name, /*location*/ node, node.flags), node); } return node; } diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 4b973d3a89e35..d6f09ecc2371c 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -138,7 +138,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) || @@ -3568,7 +3567,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); } @@ -3803,7 +3802,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 e203dca1b1504..c016daf09d648 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -1029,7 +1029,6 @@ namespace ts { // @kind(SyntaxKind.PropertyAccessExpression) export interface PropertyAccessExpression extends MemberExpression, Declaration { expression: LeftHandSideExpression; - dotToken: Node; name: Identifier; } From 9abe0f8953216b204fb0a3d4d09b7e532ef7fe26 Mon Sep 17 00:00:00 2001 From: Kanchalai Tanglertsampan Date: Tue, 28 Jun 2016 13:39:57 -0700 Subject: [PATCH 81/84] Update baselines from removing dotToken --- tests/baselines/reference/APISample_watcher.js | 7 ++----- tests/baselines/reference/arrayConcatMap.js | 3 +-- tests/baselines/reference/classAbstractCrashedOnce.js | 3 +-- .../baselines/reference/controlFlowPropertyDeclarations.js | 4 +--- .../reference/enumConflictsWithGlobalIdentifier.js | 3 +-- tests/baselines/reference/enumMemberResolution.js | 3 +-- .../errorRecoveryWithDotFollowedByNamespaceKeyword.js | 3 +-- .../functionsMissingReturnStatementsAndExpressions.js | 3 +-- tests/baselines/reference/genericChainedCalls.js | 5 ++--- tests/baselines/reference/inferenceLimit.js | 6 ++---- .../multiLinePropertyAccessAndArrowFunctionIndent1.js | 6 ++---- .../numericLiteralsWithTrailingDecimalPoints01.js | 3 +-- .../reference/overEagerReturnTypeSpecialization.js | 4 ++-- tests/baselines/reference/parse1.js | 3 +-- tests/baselines/reference/parser509667.js | 3 +-- tests/baselines/reference/promiseVoidErrorCallback.js | 6 ++---- tests/baselines/reference/underscoreTest1.js | 6 +----- tests/baselines/reference/wrappedIncovations1.js | 5 +---- tests/baselines/reference/wrappedIncovations2.js | 5 +---- tests/cases/unittests/incrementalParser.ts | 2 +- 20 files changed, 26 insertions(+), 57 deletions(-) diff --git a/tests/baselines/reference/APISample_watcher.js b/tests/baselines/reference/APISample_watcher.js index 7573f2ba99ea8..6067291e40dfe 100644 --- a/tests/baselines/reference/APISample_watcher.js +++ b/tests/baselines/reference/APISample_watcher.js @@ -169,9 +169,7 @@ function watch(rootFileNames, options) { }); } function logErrors(fileName) { - var allDiagnostics = services.getCompilerOptionsDiagnostics() - .concat(services.getSyntacticDiagnostics(fileName)) - .concat(services.getSemanticDiagnostics(fileName)); + var allDiagnostics = services.getCompilerOptionsDiagnostics().concat(services.getSyntacticDiagnostics(fileName)).concat(services.getSemanticDiagnostics(fileName)); allDiagnostics.forEach(function (diagnostic) { var message = ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n"); if (diagnostic.file) { @@ -185,7 +183,6 @@ function watch(rootFileNames, options) { } } // Initialize files constituting the program as all .ts files in the current directory -var currentDirectoryFiles = fs.readdirSync(process.cwd()). - filter(function (fileName) { return fileName.length >= 3 && fileName.substr(fileName.length - 3, 3) === ".ts"; }); +var currentDirectoryFiles = fs.readdirSync(process.cwd()).filter(function (fileName) { return fileName.length >= 3 && fileName.substr(fileName.length - 3, 3) === ".ts"; }); // Start the watcher watch(currentDirectoryFiles, { module: ts.ModuleKind.CommonJS }); diff --git a/tests/baselines/reference/arrayConcatMap.js b/tests/baselines/reference/arrayConcatMap.js index 715ca158a43ff..dc7272b72237f 100644 --- a/tests/baselines/reference/arrayConcatMap.js +++ b/tests/baselines/reference/arrayConcatMap.js @@ -3,5 +3,4 @@ var x = [].concat([{ a: 1 }], [{ a: 2 }]) .map(b => b.a); //// [arrayConcatMap.js] -var x = [].concat([{ a: 1 }], [{ a: 2 }]) - .map(function (b) { return b.a; }); +var x = [].concat([{ a: 1 }], [{ a: 2 }]).map(function (b) { return b.a; }); diff --git a/tests/baselines/reference/classAbstractCrashedOnce.js b/tests/baselines/reference/classAbstractCrashedOnce.js index 46f0a8938c417..38fb554c930c6 100644 --- a/tests/baselines/reference/classAbstractCrashedOnce.js +++ b/tests/baselines/reference/classAbstractCrashedOnce.js @@ -27,8 +27,7 @@ var bar = (function (_super) { _super.apply(this, arguments); } bar.prototype.test = function () { - this. - ; + this.; }; return bar; }(foo)); diff --git a/tests/baselines/reference/controlFlowPropertyDeclarations.js b/tests/baselines/reference/controlFlowPropertyDeclarations.js index 748f63ea858da..1025f3e51cd6a 100644 --- a/tests/baselines/reference/controlFlowPropertyDeclarations.js +++ b/tests/baselines/reference/controlFlowPropertyDeclarations.js @@ -254,9 +254,7 @@ var HTMLtoJSX = (function () { // If this text is contained within a
, we need to ensure the JSX
                 // whitespace coalescing rules don't eat the whitespace. This means
                 // wrapping newlines and sequences of two or more spaces in variables.
-                text = text
-                    .replace(/\r/g, '')
-                    .replace(/( {2,}|\n|\t|\{|\})/g, function (whitespace) {
+                text = text.replace(/\r/g, '').replace(/( {2,}|\n|\t|\{|\})/g, function (whitespace) {
                     return '{' + JSON.stringify(whitespace) + '}';
                 });
             }
diff --git a/tests/baselines/reference/enumConflictsWithGlobalIdentifier.js b/tests/baselines/reference/enumConflictsWithGlobalIdentifier.js
index 5c32fe5a16b3c..d463073bb81c5 100644
--- a/tests/baselines/reference/enumConflictsWithGlobalIdentifier.js
+++ b/tests/baselines/reference/enumConflictsWithGlobalIdentifier.js
@@ -11,6 +11,5 @@ var Position;
 (function (Position) {
     Position[Position["IgnoreRulesSpecific"] = 0] = "IgnoreRulesSpecific";
 })(Position || (Position = {}));
-var x = IgnoreRulesSpecific.
-;
+var x = IgnoreRulesSpecific.;
 var y = Position.IgnoreRulesSpecific;
diff --git a/tests/baselines/reference/enumMemberResolution.js b/tests/baselines/reference/enumMemberResolution.js
index ec27342471d24..c021eb8d47aed 100644
--- a/tests/baselines/reference/enumMemberResolution.js
+++ b/tests/baselines/reference/enumMemberResolution.js
@@ -12,7 +12,6 @@ var Position2;
 (function (Position2) {
     Position2[Position2["IgnoreRulesSpecific"] = 0] = "IgnoreRulesSpecific";
 })(Position2 || (Position2 = {}));
-var x = IgnoreRulesSpecific.
-; // error
+var x = IgnoreRulesSpecific.; // error
 var y = 1;
 var z = Position2.IgnoreRulesSpecific; // no error
diff --git a/tests/baselines/reference/errorRecoveryWithDotFollowedByNamespaceKeyword.js b/tests/baselines/reference/errorRecoveryWithDotFollowedByNamespaceKeyword.js
index 7eccb5c25ee6a..007b409409421 100644
--- a/tests/baselines/reference/errorRecoveryWithDotFollowedByNamespaceKeyword.js
+++ b/tests/baselines/reference/errorRecoveryWithDotFollowedByNamespaceKeyword.js
@@ -14,8 +14,7 @@ var A;
 (function (A) {
     function foo() {
         if (true) {
-            B.
-            ;
+            B.;
             var B;
             (function (B) {
                 function baz() { }
diff --git a/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions.js b/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions.js
index 17468722e207d..838851815fa25 100644
--- a/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions.js
+++ b/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions.js
@@ -249,8 +249,7 @@ var C = (function () {
             // Not fine, since we can *only* consist of a single throw statement
             // if no return statements are present but we are a get accessor.
             throw null;
-            throw undefined.
-            ;
+            throw undefined.;
         },
         enumerable: true,
         configurable: true
diff --git a/tests/baselines/reference/genericChainedCalls.js b/tests/baselines/reference/genericChainedCalls.js
index a738e19023ba3..71d83a906bf33 100644
--- a/tests/baselines/reference/genericChainedCalls.js
+++ b/tests/baselines/reference/genericChainedCalls.js
@@ -15,9 +15,8 @@ var s3 = s2.func(num => num.toString())
 
 
 //// [genericChainedCalls.js]
-var r1 = v1.func(function (num) { return num.toString(); })
-    .func(function (str) { return str.length; }) // error, number doesn't have a length
-    .func(function (num) { return num.toString(); });
+var r1 = v1.func(function (num) { return num.toString(); }).func(function (str) { return str.length; }) // error, number doesn't have a length
+.func(function (num) { return num.toString(); });
 var s1 = v1.func(function (num) { return num.toString(); });
 var s2 = s1.func(function (str) { return str.length; }); // should also error
 var s3 = s2.func(function (num) { return num.toString(); });
diff --git a/tests/baselines/reference/inferenceLimit.js b/tests/baselines/reference/inferenceLimit.js
index a3fa522683081..cec05fdeddd1a 100644
--- a/tests/baselines/reference/inferenceLimit.js
+++ b/tests/baselines/reference/inferenceLimit.js
@@ -59,15 +59,13 @@ class BrokenClass {
             let result = [];
             let populateItems = (order) => {
                 return new Promise((resolve, reject) => {
-                    this.doStuff(order.id)
-                        .then((items) => {
+                    this.doStuff(order.id).then((items) => {
                         order.items = items;
                         resolve(order);
                     });
                 });
             };
-            return Promise.all(result.map(populateItems))
-                .then((orders) => {
+            return Promise.all(result.map(populateItems)).then((orders) => {
                 resolve(orders);
             });
         });
diff --git a/tests/baselines/reference/multiLinePropertyAccessAndArrowFunctionIndent1.js b/tests/baselines/reference/multiLinePropertyAccessAndArrowFunctionIndent1.js
index 2ada9f1bb5217..46d3bc8fef0c0 100644
--- a/tests/baselines/reference/multiLinePropertyAccessAndArrowFunctionIndent1.js
+++ b/tests/baselines/reference/multiLinePropertyAccessAndArrowFunctionIndent1.js
@@ -7,8 +7,6 @@ return this.edit(role)
 
 //// [multiLinePropertyAccessAndArrowFunctionIndent1.js]
 var _this = this;
-return this.edit(role)
-    .then(function (role) {
-    return _this.roleService.add(role)
-        .then(function (data) { return data.data; });
+return this.edit(role).then(function (role) {
+    return _this.roleService.add(role).then(function (data) { return data.data; });
 });
diff --git a/tests/baselines/reference/numericLiteralsWithTrailingDecimalPoints01.js b/tests/baselines/reference/numericLiteralsWithTrailingDecimalPoints01.js
index 54a0328c27c69..ad5bcc196a97f 100644
--- a/tests/baselines/reference/numericLiteralsWithTrailingDecimalPoints01.js
+++ b/tests/baselines/reference/numericLiteralsWithTrailingDecimalPoints01.js
@@ -32,7 +32,6 @@ var test3 = 3..toString();
 var test4 = 3..toString();
 var test5 = 3..toString();
 var test6 = 3.['toString']();
-var test7 = 3
-    .toString();
+var test7 = 3..toString();
 var test8 = new Number(4).toString();
 var test9 = 3. + 3.;
diff --git a/tests/baselines/reference/overEagerReturnTypeSpecialization.js b/tests/baselines/reference/overEagerReturnTypeSpecialization.js
index 90fcc388b762f..67d2005eca99c 100644
--- a/tests/baselines/reference/overEagerReturnTypeSpecialization.js
+++ b/tests/baselines/reference/overEagerReturnTypeSpecialization.js
@@ -17,6 +17,6 @@ var r2: I1 = v1.func(num => num.toString()) // Correctly returns an I1
-    .func(function (str) { return str.length; }); // should error
+.func(function (str) { return str.length; }); // should error
 var r2 = v1.func(function (num) { return num.toString(); }) // Correctly returns an I1
-    .func(function (str) { return str.length; }); // should be ok 
+.func(function (str) { return str.length; }); // should be ok 
diff --git a/tests/baselines/reference/parse1.js b/tests/baselines/reference/parse1.js
index 4804e31adcdfe..08af81bda176c 100644
--- a/tests/baselines/reference/parse1.js
+++ b/tests/baselines/reference/parse1.js
@@ -8,6 +8,5 @@ function foo() {
 //// [parse1.js]
 var bar = 42;
 function foo() {
-    bar.
-    ;
+    bar.;
 }
diff --git a/tests/baselines/reference/parser509667.js b/tests/baselines/reference/parser509667.js
index 0b68d0e0f59bd..a4892fb969ef6 100644
--- a/tests/baselines/reference/parser509667.js
+++ b/tests/baselines/reference/parser509667.js
@@ -16,8 +16,7 @@ var Foo = (function () {
     function Foo() {
     }
     Foo.prototype.f1 = function () {
-        if (this.
-        )
+        if (this.)
             ;
     };
     Foo.prototype.f2 = function () {
diff --git a/tests/baselines/reference/promiseVoidErrorCallback.js b/tests/baselines/reference/promiseVoidErrorCallback.js
index a9aa0f1b5d081..7031b8b500abf 100644
--- a/tests/baselines/reference/promiseVoidErrorCallback.js
+++ b/tests/baselines/reference/promiseVoidErrorCallback.js
@@ -34,10 +34,8 @@ function f1() {
 function f2(x) {
     return { __t2: x.__t1 + ":foo_21" };
 }
-var x3 = f1()
-    .then(f2, (e) => {
+var x3 = f1().then(f2, (e) => {
     throw e;
-})
-    .then((x) => {
+}).then((x) => {
     return { __t3: x.__t2 + "bar" };
 });
diff --git a/tests/baselines/reference/underscoreTest1.js b/tests/baselines/reference/underscoreTest1.js
index 687a210e6707c..e3a5f1661b8ed 100644
--- a/tests/baselines/reference/underscoreTest1.js
+++ b/tests/baselines/reference/underscoreTest1.js
@@ -1019,11 +1019,7 @@ _.omit({ name: 'moe', age: 50, userid: 'moe1' }, 'userid');
 var iceCream = { flavor: "chocolate" };
 _.defaults(iceCream, { flavor: "vanilla", sprinkles: "lots" });
 _.clone({ name: 'moe' });
-_.chain([1, 2, 3, 200])
-    .filter(function (num) { return num % 2 == 0; })
-    .tap(alert)
-    .map(function (num) { return num * num; })
-    .value();
+_.chain([1, 2, 3, 200]).filter(function (num) { return num % 2 == 0; }).tap(alert).map(function (num) { return num * num; }).value();
 _.has({ a: 1, b: 2, c: 3 }, "b");
 var moe = { name: 'moe', luckyNumbers: [13, 27, 34] };
 var clone = { name: 'moe', luckyNumbers: [13, 27, 34] };
diff --git a/tests/baselines/reference/wrappedIncovations1.js b/tests/baselines/reference/wrappedIncovations1.js
index b9e5ed688f902..5d8f24ad3b8c7 100644
--- a/tests/baselines/reference/wrappedIncovations1.js
+++ b/tests/baselines/reference/wrappedIncovations1.js
@@ -5,7 +5,4 @@ var v = this
   .baz();
 
 //// [wrappedIncovations1.js]
-var v = this
-    .foo()
-    .bar()
-    .baz();
+var v = this.foo().bar().baz();
diff --git a/tests/baselines/reference/wrappedIncovations2.js b/tests/baselines/reference/wrappedIncovations2.js
index 09951996c94d4..3577e92169e89 100644
--- a/tests/baselines/reference/wrappedIncovations2.js
+++ b/tests/baselines/reference/wrappedIncovations2.js
@@ -5,7 +5,4 @@ var v = this.
   baz();
 
 //// [wrappedIncovations2.js]
-var v = this.
-    foo().
-    bar().
-    baz();
+var v = this.foo().bar().baz();
diff --git a/tests/cases/unittests/incrementalParser.ts b/tests/cases/unittests/incrementalParser.ts
index 0988d51e11b26..741c9e54cada7 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 f42aa695a8c70bd72d0fe2d77dbd11aa19cf6a31 Mon Sep 17 00:00:00 2001
From: Kanchalai Tanglertsampan 
Date: Thu, 30 Jun 2016 16:11:06 -0700
Subject: [PATCH 82/84] Address PR: Add NodeEmitFlags to no indent when emit

---
 src/compiler/emitter.ts                         | 17 ++++++++++++++++-
 src/compiler/factory.ts                         | 12 ++++++------
 src/compiler/parser.ts                          |  2 +-
 src/compiler/types.ts                           |  5 +++--
 tests/baselines/reference/APISample_watcher.js  |  7 +++++--
 tests/baselines/reference/arrayConcatMap.js     |  3 ++-
 .../reference/classAbstractCrashedOnce.js       |  3 ++-
 .../controlFlowPropertyDeclarations.js          |  4 +++-
 .../enumConflictsWithGlobalIdentifier.js        |  3 ++-
 .../baselines/reference/enumMemberResolution.js |  3 ++-
 ...RecoveryWithDotFollowedByNamespaceKeyword.js |  3 ++-
 ...ionsMissingReturnStatementsAndExpressions.js |  3 ++-
 tests/baselines/reference/inferenceLimit.js     |  6 ++++--
 ...LinePropertyAccessAndArrowFunctionIndent1.js |  6 ++++--
 ...umericLiteralsWithTrailingDecimalPoints01.js |  3 ++-
 tests/baselines/reference/parse1.js             |  3 ++-
 tests/baselines/reference/parser509667.js       |  3 ++-
 .../reference/promiseVoidErrorCallback.js       |  3 ++-
 tests/baselines/reference/underscoreTest1.js    |  4 +++-
 .../baselines/reference/wrappedIncovations1.js  |  5 ++++-
 .../baselines/reference/wrappedIncovations2.js  |  5 ++++-
 21 files changed, 73 insertions(+), 30 deletions(-)

diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts
index f5ed3733b10c3..b69033127b257 100644
--- a/src/compiler/emitter.ts
+++ b/src/compiler/emitter.ts
@@ -1084,9 +1084,24 @@ const _super = (function (geti, seti) {
                 return;
             }
 
+            let indentBeforeDot = false;
+            let indentAfterDot = false;
+            let shouldEmitDotDot = false;
+            if (!(node.flags & NodeFlags.Synthesized)) {
+                const dotRangeStart = node.expression.end;
+                const dotRangeEnd = skipTrivia(currentText, node.expression.end) + 1;
+                const dotToken = { kind: SyntaxKind.DotToken, pos: dotRangeStart, end: dotRangeEnd };
+                indentBeforeDot = needsIndentation(node, node.expression, dotToken);
+                indentAfterDot = needsIndentation(node, dotToken, node.name);
+                shouldEmitDotDot = !indentBeforeDot && needsDotDotForPropertyAccess(node.expression);
+            }
+
             emitExpression(node.expression);
-            write(needsDotDotForPropertyAccess(node.expression) ? ".." : ".");
+            increaseIndentIf(indentBeforeDot);
+            write(shouldEmitDotDot ? ".." : ".");
+            increaseIndentIf(indentAfterDot);
             emit(node.name);
+            decreaseIndentIf(indentBeforeDot, indentAfterDot);
         }
 
         // 1..toString is a valid property access, emit a dot after the literal
diff --git a/src/compiler/factory.ts b/src/compiler/factory.ts
index c1b80f09f6c15..4f89df8c56cc3 100644
--- a/src/compiler/factory.ts
+++ b/src/compiler/factory.ts
@@ -338,20 +338,20 @@ namespace ts {
         return node;
     }
 
-    export function createPropertyAccess(expression: Expression, name: string | Identifier, location?: TextRange) {
-        return createPropertyAccessWithDotToken(expression, name, location, /*flags*/ undefined);
-    }
-
-    export function createPropertyAccessWithDotToken(expression: Expression, name: string | Identifier, location?: TextRange, flags?: NodeFlags) {
+    export function createPropertyAccess(expression: Expression, name: string | Identifier, location?: TextRange, flags?: NodeFlags) {
         const node = createNode(SyntaxKind.PropertyAccessExpression, location, flags);
         node.expression = parenthesizeForAccess(expression);
+        node.emitFlags = NodeEmitFlags.NoIndentation;
         node.name = typeof name === "string" ? createIdentifier(name) : name;
         return node;
     }
 
     export function updatePropertyAccess(node: PropertyAccessExpression, expression: Expression, name: Identifier) {
         if (node.expression !== expression || node.name !== name) {
-            return updateNode(createPropertyAccessWithDotToken(expression, name, /*location*/ node, node.flags), node);
+            const propertyAccess = createPropertyAccess(expression, name, /*location*/ node, node.flags);
+            // Because we are updating existed propertyAccess we want to inherit its emitFlags instead of using default from createPropertyAccess
+            propertyAccess.emitFlags = node.emitFlags;
+            return updateNode(propertyAccess, node);
         }
         return node;
     }
diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts
index d6f09ecc2371c..1dfe242ac4d2c 100644
--- a/src/compiler/parser.ts
+++ b/src/compiler/parser.ts
@@ -3567,7 +3567,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;
-            parseExpectedToken(SyntaxKind.DotToken, /*reportAtCurrentPosition*/ false, Diagnostics.super_must_be_followed_by_an_argument_list_or_member_access);
+            parseExpected(SyntaxKind.DotToken, Diagnostics.super_must_be_followed_by_an_argument_list_or_member_access);
             node.name = parseRightSideOfDot(/*allowIdentifierNames*/ true);
             return finishNode(node);
         }
diff --git a/src/compiler/types.ts b/src/compiler/types.ts
index c016daf09d648..3c55e773cf68e 100644
--- a/src/compiler/types.ts
+++ b/src/compiler/types.ts
@@ -3080,13 +3080,14 @@ namespace ts {
         ExportName = 1 << 17,                    // Ensure an export prefix is added for an identifier that points to an exported declaration with a local name (see SymbolFlags.ExportHasLocal).
         LocalName = 1 << 18,                     // Ensure an export prefix is not added for an identifier that points to an exported declaration.
         Indented = 1 << 19,                      // Adds an explicit extra indentation level for class and function bodies when printing (used to match old emitter).
+        NoIndentation = 1 << 20,                    // Do not indent the node.
 
         // SourceMap Specialization.
         // TODO(rbuckton): These should be removed once source maps are aligned with the old
         //                 emitter and new baselines are taken. This exists solely to
         //                 align with the old emitter.
-        SourceMapEmitOpenBraceAsToken = 1 << 20,        // Emits the open brace of a block function body as a source mapped token.
-        SourceMapAdjustRestParameterLoop = 1 << 21,     // Emits adjusted source map positions for a ForStatement generated when transforming a rest parameter for ES5/3.
+        SourceMapEmitOpenBraceAsToken = 1 << 21,        // Emits the open brace of a block function body as a source mapped token.
+        SourceMapAdjustRestParameterLoop = 1 << 22,     // Emits adjusted source map positions for a ForStatement generated when transforming a rest parameter for ES5/3.
     }
 
     /** Additional context provided to `visitEachChild` */
diff --git a/tests/baselines/reference/APISample_watcher.js b/tests/baselines/reference/APISample_watcher.js
index 6067291e40dfe..7573f2ba99ea8 100644
--- a/tests/baselines/reference/APISample_watcher.js
+++ b/tests/baselines/reference/APISample_watcher.js
@@ -169,7 +169,9 @@ function watch(rootFileNames, options) {
         });
     }
     function logErrors(fileName) {
-        var allDiagnostics = services.getCompilerOptionsDiagnostics().concat(services.getSyntacticDiagnostics(fileName)).concat(services.getSemanticDiagnostics(fileName));
+        var allDiagnostics = services.getCompilerOptionsDiagnostics()
+            .concat(services.getSyntacticDiagnostics(fileName))
+            .concat(services.getSemanticDiagnostics(fileName));
         allDiagnostics.forEach(function (diagnostic) {
             var message = ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n");
             if (diagnostic.file) {
@@ -183,6 +185,7 @@ function watch(rootFileNames, options) {
     }
 }
 // Initialize files constituting the program as all .ts files in the current directory
-var currentDirectoryFiles = fs.readdirSync(process.cwd()).filter(function (fileName) { return fileName.length >= 3 && fileName.substr(fileName.length - 3, 3) === ".ts"; });
+var currentDirectoryFiles = fs.readdirSync(process.cwd()).
+    filter(function (fileName) { return fileName.length >= 3 && fileName.substr(fileName.length - 3, 3) === ".ts"; });
 // Start the watcher
 watch(currentDirectoryFiles, { module: ts.ModuleKind.CommonJS });
diff --git a/tests/baselines/reference/arrayConcatMap.js b/tests/baselines/reference/arrayConcatMap.js
index dc7272b72237f..715ca158a43ff 100644
--- a/tests/baselines/reference/arrayConcatMap.js
+++ b/tests/baselines/reference/arrayConcatMap.js
@@ -3,4 +3,5 @@ var x = [].concat([{ a: 1 }], [{ a: 2 }])
           .map(b => b.a);
 
 //// [arrayConcatMap.js]
-var x = [].concat([{ a: 1 }], [{ a: 2 }]).map(function (b) { return b.a; });
+var x = [].concat([{ a: 1 }], [{ a: 2 }])
+    .map(function (b) { return b.a; });
diff --git a/tests/baselines/reference/classAbstractCrashedOnce.js b/tests/baselines/reference/classAbstractCrashedOnce.js
index 38fb554c930c6..46f0a8938c417 100644
--- a/tests/baselines/reference/classAbstractCrashedOnce.js
+++ b/tests/baselines/reference/classAbstractCrashedOnce.js
@@ -27,7 +27,8 @@ var bar = (function (_super) {
         _super.apply(this, arguments);
     }
     bar.prototype.test = function () {
-        this.;
+        this.
+        ;
     };
     return bar;
 }(foo));
diff --git a/tests/baselines/reference/controlFlowPropertyDeclarations.js b/tests/baselines/reference/controlFlowPropertyDeclarations.js
index 1025f3e51cd6a..748f63ea858da 100644
--- a/tests/baselines/reference/controlFlowPropertyDeclarations.js
+++ b/tests/baselines/reference/controlFlowPropertyDeclarations.js
@@ -254,7 +254,9 @@ var HTMLtoJSX = (function () {
                 // If this text is contained within a 
, we need to ensure the JSX
                 // whitespace coalescing rules don't eat the whitespace. This means
                 // wrapping newlines and sequences of two or more spaces in variables.
-                text = text.replace(/\r/g, '').replace(/( {2,}|\n|\t|\{|\})/g, function (whitespace) {
+                text = text
+                    .replace(/\r/g, '')
+                    .replace(/( {2,}|\n|\t|\{|\})/g, function (whitespace) {
                     return '{' + JSON.stringify(whitespace) + '}';
                 });
             }
diff --git a/tests/baselines/reference/enumConflictsWithGlobalIdentifier.js b/tests/baselines/reference/enumConflictsWithGlobalIdentifier.js
index d463073bb81c5..5c32fe5a16b3c 100644
--- a/tests/baselines/reference/enumConflictsWithGlobalIdentifier.js
+++ b/tests/baselines/reference/enumConflictsWithGlobalIdentifier.js
@@ -11,5 +11,6 @@ var Position;
 (function (Position) {
     Position[Position["IgnoreRulesSpecific"] = 0] = "IgnoreRulesSpecific";
 })(Position || (Position = {}));
-var x = IgnoreRulesSpecific.;
+var x = IgnoreRulesSpecific.
+;
 var y = Position.IgnoreRulesSpecific;
diff --git a/tests/baselines/reference/enumMemberResolution.js b/tests/baselines/reference/enumMemberResolution.js
index c021eb8d47aed..ec27342471d24 100644
--- a/tests/baselines/reference/enumMemberResolution.js
+++ b/tests/baselines/reference/enumMemberResolution.js
@@ -12,6 +12,7 @@ var Position2;
 (function (Position2) {
     Position2[Position2["IgnoreRulesSpecific"] = 0] = "IgnoreRulesSpecific";
 })(Position2 || (Position2 = {}));
-var x = IgnoreRulesSpecific.; // error
+var x = IgnoreRulesSpecific.
+; // error
 var y = 1;
 var z = Position2.IgnoreRulesSpecific; // no error
diff --git a/tests/baselines/reference/errorRecoveryWithDotFollowedByNamespaceKeyword.js b/tests/baselines/reference/errorRecoveryWithDotFollowedByNamespaceKeyword.js
index 007b409409421..7eccb5c25ee6a 100644
--- a/tests/baselines/reference/errorRecoveryWithDotFollowedByNamespaceKeyword.js
+++ b/tests/baselines/reference/errorRecoveryWithDotFollowedByNamespaceKeyword.js
@@ -14,7 +14,8 @@ var A;
 (function (A) {
     function foo() {
         if (true) {
-            B.;
+            B.
+            ;
             var B;
             (function (B) {
                 function baz() { }
diff --git a/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions.js b/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions.js
index 838851815fa25..17468722e207d 100644
--- a/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions.js
+++ b/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions.js
@@ -249,7 +249,8 @@ var C = (function () {
             // Not fine, since we can *only* consist of a single throw statement
             // if no return statements are present but we are a get accessor.
             throw null;
-            throw undefined.;
+            throw undefined.
+            ;
         },
         enumerable: true,
         configurable: true
diff --git a/tests/baselines/reference/inferenceLimit.js b/tests/baselines/reference/inferenceLimit.js
index cec05fdeddd1a..a3fa522683081 100644
--- a/tests/baselines/reference/inferenceLimit.js
+++ b/tests/baselines/reference/inferenceLimit.js
@@ -59,13 +59,15 @@ class BrokenClass {
             let result = [];
             let populateItems = (order) => {
                 return new Promise((resolve, reject) => {
-                    this.doStuff(order.id).then((items) => {
+                    this.doStuff(order.id)
+                        .then((items) => {
                         order.items = items;
                         resolve(order);
                     });
                 });
             };
-            return Promise.all(result.map(populateItems)).then((orders) => {
+            return Promise.all(result.map(populateItems))
+                .then((orders) => {
                 resolve(orders);
             });
         });
diff --git a/tests/baselines/reference/multiLinePropertyAccessAndArrowFunctionIndent1.js b/tests/baselines/reference/multiLinePropertyAccessAndArrowFunctionIndent1.js
index 46d3bc8fef0c0..2ada9f1bb5217 100644
--- a/tests/baselines/reference/multiLinePropertyAccessAndArrowFunctionIndent1.js
+++ b/tests/baselines/reference/multiLinePropertyAccessAndArrowFunctionIndent1.js
@@ -7,6 +7,8 @@ return this.edit(role)
 
 //// [multiLinePropertyAccessAndArrowFunctionIndent1.js]
 var _this = this;
-return this.edit(role).then(function (role) {
-    return _this.roleService.add(role).then(function (data) { return data.data; });
+return this.edit(role)
+    .then(function (role) {
+    return _this.roleService.add(role)
+        .then(function (data) { return data.data; });
 });
diff --git a/tests/baselines/reference/numericLiteralsWithTrailingDecimalPoints01.js b/tests/baselines/reference/numericLiteralsWithTrailingDecimalPoints01.js
index ad5bcc196a97f..54a0328c27c69 100644
--- a/tests/baselines/reference/numericLiteralsWithTrailingDecimalPoints01.js
+++ b/tests/baselines/reference/numericLiteralsWithTrailingDecimalPoints01.js
@@ -32,6 +32,7 @@ var test3 = 3..toString();
 var test4 = 3..toString();
 var test5 = 3..toString();
 var test6 = 3.['toString']();
-var test7 = 3..toString();
+var test7 = 3
+    .toString();
 var test8 = new Number(4).toString();
 var test9 = 3. + 3.;
diff --git a/tests/baselines/reference/parse1.js b/tests/baselines/reference/parse1.js
index 08af81bda176c..4804e31adcdfe 100644
--- a/tests/baselines/reference/parse1.js
+++ b/tests/baselines/reference/parse1.js
@@ -8,5 +8,6 @@ function foo() {
 //// [parse1.js]
 var bar = 42;
 function foo() {
-    bar.;
+    bar.
+    ;
 }
diff --git a/tests/baselines/reference/parser509667.js b/tests/baselines/reference/parser509667.js
index a4892fb969ef6..0b68d0e0f59bd 100644
--- a/tests/baselines/reference/parser509667.js
+++ b/tests/baselines/reference/parser509667.js
@@ -16,7 +16,8 @@ var Foo = (function () {
     function Foo() {
     }
     Foo.prototype.f1 = function () {
-        if (this.)
+        if (this.
+        )
             ;
     };
     Foo.prototype.f2 = function () {
diff --git a/tests/baselines/reference/promiseVoidErrorCallback.js b/tests/baselines/reference/promiseVoidErrorCallback.js
index 7031b8b500abf..ba68092579fac 100644
--- a/tests/baselines/reference/promiseVoidErrorCallback.js
+++ b/tests/baselines/reference/promiseVoidErrorCallback.js
@@ -34,7 +34,8 @@ function f1() {
 function f2(x) {
     return { __t2: x.__t1 + ":foo_21" };
 }
-var x3 = f1().then(f2, (e) => {
+var x3 = f1()
+    .then(f2, (e) => {
     throw e;
 }).then((x) => {
     return { __t3: x.__t2 + "bar" };
diff --git a/tests/baselines/reference/underscoreTest1.js b/tests/baselines/reference/underscoreTest1.js
index e3a5f1661b8ed..26a60c1a447f5 100644
--- a/tests/baselines/reference/underscoreTest1.js
+++ b/tests/baselines/reference/underscoreTest1.js
@@ -1019,7 +1019,9 @@ _.omit({ name: 'moe', age: 50, userid: 'moe1' }, 'userid');
 var iceCream = { flavor: "chocolate" };
 _.defaults(iceCream, { flavor: "vanilla", sprinkles: "lots" });
 _.clone({ name: 'moe' });
-_.chain([1, 2, 3, 200]).filter(function (num) { return num % 2 == 0; }).tap(alert).map(function (num) { return num * num; }).value();
+_.chain([1, 2, 3, 200])
+    .filter(function (num) { return num % 2 == 0; })
+    .tap(alert).map(function (num) { return num * num; }).value();
 _.has({ a: 1, b: 2, c: 3 }, "b");
 var moe = { name: 'moe', luckyNumbers: [13, 27, 34] };
 var clone = { name: 'moe', luckyNumbers: [13, 27, 34] };
diff --git a/tests/baselines/reference/wrappedIncovations1.js b/tests/baselines/reference/wrappedIncovations1.js
index 5d8f24ad3b8c7..b9e5ed688f902 100644
--- a/tests/baselines/reference/wrappedIncovations1.js
+++ b/tests/baselines/reference/wrappedIncovations1.js
@@ -5,4 +5,7 @@ var v = this
   .baz();
 
 //// [wrappedIncovations1.js]
-var v = this.foo().bar().baz();
+var v = this
+    .foo()
+    .bar()
+    .baz();
diff --git a/tests/baselines/reference/wrappedIncovations2.js b/tests/baselines/reference/wrappedIncovations2.js
index 3577e92169e89..09951996c94d4 100644
--- a/tests/baselines/reference/wrappedIncovations2.js
+++ b/tests/baselines/reference/wrappedIncovations2.js
@@ -5,4 +5,7 @@ var v = this.
   baz();
 
 //// [wrappedIncovations2.js]
-var v = this.foo().bar().baz();
+var v = this.
+    foo().
+    bar().
+    baz();

From 34704cdf8ae4f5eb7507c7bdc2158900951aaef4 Mon Sep 17 00:00:00 2001
From: Kanchalai Tanglertsampan 
Date: Fri, 1 Jul 2016 10:51:12 -0700
Subject: [PATCH 83/84] Address PR; and refactor setting NodeEmitFlags for
 createMemberAccessForPropertyName

---
 src/compiler/emitter.ts         |  5 ++---
 src/compiler/factory.ts         | 13 +++++--------
 src/compiler/transformers/ts.ts |  3 ---
 3 files changed, 7 insertions(+), 14 deletions(-)

diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts
index b69033127b257..647118e361384 100644
--- a/src/compiler/emitter.ts
+++ b/src/compiler/emitter.ts
@@ -1086,15 +1086,14 @@ const _super = (function (geti, seti) {
 
             let indentBeforeDot = false;
             let indentAfterDot = false;
-            let shouldEmitDotDot = false;
-            if (!(node.flags & NodeFlags.Synthesized)) {
+            if (!(node.emitFlags & NodeEmitFlags.NoIndentation)) {
                 const dotRangeStart = node.expression.end;
                 const dotRangeEnd = skipTrivia(currentText, node.expression.end) + 1;
                 const dotToken = { kind: SyntaxKind.DotToken, pos: dotRangeStart, end: dotRangeEnd };
                 indentBeforeDot = needsIndentation(node, node.expression, dotToken);
                 indentAfterDot = needsIndentation(node, dotToken, node.name);
-                shouldEmitDotDot = !indentBeforeDot && needsDotDotForPropertyAccess(node.expression);
             }
+            const shouldEmitDotDot = !indentBeforeDot && needsDotDotForPropertyAccess(node.expression);
 
             emitExpression(node.expression);
             increaseIndentIf(indentBeforeDot);
diff --git a/src/compiler/factory.ts b/src/compiler/factory.ts
index 4f89df8c56cc3..e516d9d1463b8 100644
--- a/src/compiler/factory.ts
+++ b/src/compiler/factory.ts
@@ -936,16 +936,13 @@ namespace ts {
     }
 
     export function createMemberAccessForPropertyName(target: Expression, memberName: PropertyName, setNodeEmitFlags: (node: Node, flags: NodeEmitFlags) => void, location?: TextRange): MemberExpression {
-        if (isIdentifier(memberName)) {
-            const expression = createPropertyAccess(target, memberName, location);
-            setNodeEmitFlags(expression, NodeEmitFlags.NoNestedSourceMaps);
-            return expression;
-        }
-        else if (isComputedPropertyName(memberName)) {
-            return createElementAccess(target, memberName.expression, location);
+        if (isComputedPropertyName(memberName)) {
+             return createElementAccess(target, memberName.expression, location);
         }
         else {
-            return createElementAccess(target, memberName, location);
+            const expression = isIdentifier(memberName) ? createPropertyAccess(target, memberName, location) : createElementAccess(target, memberName, location);
+            setNodeEmitFlags(expression, expression.emitFlags ? NodeEmitFlags.NoNestedSourceMaps | expression.emitFlags : NodeEmitFlags.NoNestedSourceMaps);
+            return expression;
         }
     }
 
diff --git a/src/compiler/transformers/ts.ts b/src/compiler/transformers/ts.ts
index 2faf1fe141c57..30bc5e64c4b2c 100644
--- a/src/compiler/transformers/ts.ts
+++ b/src/compiler/transformers/ts.ts
@@ -1085,9 +1085,6 @@ namespace ts {
             const propertyName = visitPropertyNameOfClassElement(property);
             const initializer = visitNode(property.initializer, visitor, isExpression);
             const memberAccess = createMemberAccessForPropertyName(receiver, propertyName, setNodeEmitFlags, /*location*/ propertyName);
-            if (!isComputedPropertyName(propertyName)) {
-                setNodeEmitFlags(memberAccess, NodeEmitFlags.NoNestedSourceMaps);
-            }
 
             return createAssignment(memberAccess, initializer);
         }

From 26d092340da0d89c6b2de676295b4e4cc36652a2 Mon Sep 17 00:00:00 2001
From: Kanchalai Tanglertsampan 
Date: Fri, 1 Jul 2016 10:51:21 -0700
Subject: [PATCH 84/84] Update baselines

---
 tests/baselines/reference/genericChainedCalls.js             | 5 +++--
 .../baselines/reference/overEagerReturnTypeSpecialization.js | 4 ++--
 tests/baselines/reference/promiseVoidErrorCallback.js        | 3 ++-
 tests/baselines/reference/underscoreTest1.js                 | 4 +++-
 4 files changed, 10 insertions(+), 6 deletions(-)

diff --git a/tests/baselines/reference/genericChainedCalls.js b/tests/baselines/reference/genericChainedCalls.js
index 71d83a906bf33..a738e19023ba3 100644
--- a/tests/baselines/reference/genericChainedCalls.js
+++ b/tests/baselines/reference/genericChainedCalls.js
@@ -15,8 +15,9 @@ var s3 = s2.func(num => num.toString())
 
 
 //// [genericChainedCalls.js]
-var r1 = v1.func(function (num) { return num.toString(); }).func(function (str) { return str.length; }) // error, number doesn't have a length
-.func(function (num) { return num.toString(); });
+var r1 = v1.func(function (num) { return num.toString(); })
+    .func(function (str) { return str.length; }) // error, number doesn't have a length
+    .func(function (num) { return num.toString(); });
 var s1 = v1.func(function (num) { return num.toString(); });
 var s2 = s1.func(function (str) { return str.length; }); // should also error
 var s3 = s2.func(function (num) { return num.toString(); });
diff --git a/tests/baselines/reference/overEagerReturnTypeSpecialization.js b/tests/baselines/reference/overEagerReturnTypeSpecialization.js
index 67d2005eca99c..90fcc388b762f 100644
--- a/tests/baselines/reference/overEagerReturnTypeSpecialization.js
+++ b/tests/baselines/reference/overEagerReturnTypeSpecialization.js
@@ -17,6 +17,6 @@ var r2: I1 = v1.func(num => num.toString()) // Correctly returns an I1
-.func(function (str) { return str.length; }); // should error
+    .func(function (str) { return str.length; }); // should error
 var r2 = v1.func(function (num) { return num.toString(); }) // Correctly returns an I1
-.func(function (str) { return str.length; }); // should be ok 
+    .func(function (str) { return str.length; }); // should be ok 
diff --git a/tests/baselines/reference/promiseVoidErrorCallback.js b/tests/baselines/reference/promiseVoidErrorCallback.js
index ba68092579fac..a9aa0f1b5d081 100644
--- a/tests/baselines/reference/promiseVoidErrorCallback.js
+++ b/tests/baselines/reference/promiseVoidErrorCallback.js
@@ -37,6 +37,7 @@ function f2(x) {
 var x3 = f1()
     .then(f2, (e) => {
     throw e;
-}).then((x) => {
+})
+    .then((x) => {
     return { __t3: x.__t2 + "bar" };
 });
diff --git a/tests/baselines/reference/underscoreTest1.js b/tests/baselines/reference/underscoreTest1.js
index 26a60c1a447f5..687a210e6707c 100644
--- a/tests/baselines/reference/underscoreTest1.js
+++ b/tests/baselines/reference/underscoreTest1.js
@@ -1021,7 +1021,9 @@ _.defaults(iceCream, { flavor: "vanilla", sprinkles: "lots" });
 _.clone({ name: 'moe' });
 _.chain([1, 2, 3, 200])
     .filter(function (num) { return num % 2 == 0; })
-    .tap(alert).map(function (num) { return num * num; }).value();
+    .tap(alert)
+    .map(function (num) { return num * num; })
+    .value();
 _.has({ a: 1, b: 2, c: 3 }, "b");
 var moe = { name: 'moe', luckyNumbers: [13, 27, 34] };
 var clone = { name: 'moe', luckyNumbers: [13, 27, 34] };