From 5b860557867bed3a10b33a98c0ac99a8e9cd5dab Mon Sep 17 00:00:00 2001 From: Armando Aguirre Date: Tue, 25 Jul 2017 14:21:57 -0700 Subject: [PATCH 1/5] Excluded the default library from rename service. --- src/compiler/program.ts | 5 +++++ src/compiler/types.ts | 1 + src/services/services.ts | 43 ++++++++++++++++++++++++++-------------- 3 files changed, 34 insertions(+), 15 deletions(-) diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 305058285f9..55edb377373 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -555,6 +555,7 @@ namespace ts { getFileProcessingDiagnostics: () => fileProcessingDiagnostics, getResolvedTypeReferenceDirectives: () => resolvedTypeReferenceDirectives, isSourceFileFromExternalLibrary, + isSourceFileDefaultLibrary, dropDiagnosticsProducingTypeChecker, getSourceFileFromReference, sourceFileToPackageName, @@ -975,6 +976,10 @@ namespace ts { return sourceFilesFoundSearchingNodeModules.get(file.path); } + function isSourceFileDefaultLibrary(file: SourceFile): boolean { + return file.fileName === host.getDefaultLibFileName(options); + } + function getDiagnosticsProducingTypeChecker() { return diagnosticsProducingTypeChecker || (diagnosticsProducingTypeChecker = createTypeChecker(program, /*produceDiagnostics:*/ true)); } diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 2b3e5ab36df..66a4517a5bd 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -2452,6 +2452,7 @@ namespace ts { /* @internal */ getFileProcessingDiagnostics(): DiagnosticCollection; /* @internal */ getResolvedTypeReferenceDirectives(): Map; /* @internal */ isSourceFileFromExternalLibrary(file: SourceFile): boolean; + /* @internal */ isSourceFileDefaultLibrary(file: SourceFile): boolean; // For testing purposes only. /* @internal */ structureIsReused?: StructureIsReused; diff --git a/src/services/services.ts b/src/services/services.ts index ed9732cc9cf..998a5d36898 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -670,7 +670,7 @@ namespace ts { if (!hasModifier(node, ModifierFlags.ParameterPropertyModifier)) { break; } - // falls through + // falls through case SyntaxKind.VariableDeclaration: case SyntaxKind.BindingElement: { const decl = node; @@ -682,7 +682,7 @@ namespace ts { visit(decl.initializer); } } - // falls through + // falls through case SyntaxKind.EnumMember: case SyntaxKind.PropertyDeclaration: case SyntaxKind.PropertySignature: @@ -729,7 +729,7 @@ namespace ts { class SourceMapSourceObject implements SourceMapSource { lineMap: number[]; - constructor (public fileName: string, public text: string, public skipTrivia?: (pos: number) => number) {} + constructor(public fileName: string, public text: string, public skipTrivia?: (pos: number) => number) { } public getLineAndCharacterOfPosition(pos: number): LineAndCharacter { return ts.getLineAndCharacterOfPosition(this, pos); @@ -1130,14 +1130,14 @@ namespace ts { const newSettings = hostCache.compilationSettings(); const shouldCreateNewSourceFiles = oldSettings && (oldSettings.target !== newSettings.target || - oldSettings.module !== newSettings.module || - oldSettings.moduleResolution !== newSettings.moduleResolution || - oldSettings.noResolve !== newSettings.noResolve || - oldSettings.jsx !== newSettings.jsx || - oldSettings.allowJs !== newSettings.allowJs || - oldSettings.disableSizeLimit !== oldSettings.disableSizeLimit || - oldSettings.baseUrl !== newSettings.baseUrl || - !equalOwnProperties(oldSettings.paths, newSettings.paths)); + oldSettings.module !== newSettings.module || + oldSettings.moduleResolution !== newSettings.moduleResolution || + oldSettings.noResolve !== newSettings.noResolve || + oldSettings.jsx !== newSettings.jsx || + oldSettings.allowJs !== newSettings.allowJs || + oldSettings.disableSizeLimit !== oldSettings.disableSizeLimit || + oldSettings.baseUrl !== newSettings.baseUrl || + !equalOwnProperties(oldSettings.paths, newSettings.paths)); // Now create a new compiler const compilerHost: CompilerHost = { @@ -1365,7 +1365,7 @@ namespace ts { function getCompilerOptionsDiagnostics() { synchronizeHostData(); return program.getOptionsDiagnostics(cancellationToken).concat( - program.getGlobalDiagnostics(cancellationToken)); + program.getGlobalDiagnostics(cancellationToken)); } function getCompletionsAtPosition(fileName: string, position: number): CompletionInfo { @@ -1510,7 +1510,20 @@ namespace ts { function getReferences(fileName: string, position: number, options?: FindAllReferences.Options) { synchronizeHostData(); - return FindAllReferences.findReferencedEntries(program, cancellationToken, program.getSourceFiles(), getValidSourceFile(fileName), position, options); + + //Exclude default library when renaming as commonly user don't want to change that file. + let sourceFiles: SourceFile[] = []; + if (options.isForRename) { + for (let sourceFile of program.getSourceFiles()) { + if (!program.isSourceFileDefaultLibrary(sourceFile)) { + sourceFiles.push(sourceFile); + } + } + } else { + sourceFiles = program.getSourceFiles(); + } + + return FindAllReferences.findReferencedEntries(program, cancellationToken, sourceFiles, getValidSourceFile(fileName), position, options); } function findReferences(fileName: string, position: number): ReferencedSymbol[] { @@ -2100,7 +2113,7 @@ namespace ts { isLiteralComputedPropertyDeclarationName(node); } - function isObjectLiteralElement(node: Node): node is ObjectLiteralElement { + function isObjectLiteralElement(node: Node): node is ObjectLiteralElement { switch (node.kind) { case SyntaxKind.JsxAttribute: case SyntaxKind.JsxSpreadAttribute: @@ -2125,7 +2138,7 @@ namespace ts { if (node.parent.kind === SyntaxKind.ComputedPropertyName) { return isObjectLiteralElement(node.parent.parent) ? node.parent.parent : undefined; } - // falls through + // falls through case SyntaxKind.Identifier: return isObjectLiteralElement(node.parent) && (node.parent.parent.kind === SyntaxKind.ObjectLiteralExpression || node.parent.parent.kind === SyntaxKind.JsxAttributes) && From 675b6fb90ce3b8e659a6acc4f161a64cb31b111a Mon Sep 17 00:00:00 2001 From: Armando Aguirre Date: Tue, 25 Jul 2017 16:29:08 -0700 Subject: [PATCH 2/5] Fixed failing tests and lint. Added unit tests. --- src/services/services.ts | 9 +++++---- tests/cases/fourslash/renameDefaultLibDontWork.ts | 11 +++++++++++ 2 files changed, 16 insertions(+), 4 deletions(-) create mode 100644 tests/cases/fourslash/renameDefaultLibDontWork.ts diff --git a/src/services/services.ts b/src/services/services.ts index 998a5d36898..2a1b0bf4eb0 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -1511,15 +1511,16 @@ namespace ts { function getReferences(fileName: string, position: number, options?: FindAllReferences.Options) { synchronizeHostData(); - //Exclude default library when renaming as commonly user don't want to change that file. + // Exclude default library when renaming as commonly user don't want to change that file. let sourceFiles: SourceFile[] = []; - if (options.isForRename) { - for (let sourceFile of program.getSourceFiles()) { + if (options && options.isForRename) { + for (const sourceFile of program.getSourceFiles()) { if (!program.isSourceFileDefaultLibrary(sourceFile)) { sourceFiles.push(sourceFile); } } - } else { + } + else { sourceFiles = program.getSourceFiles(); } diff --git a/tests/cases/fourslash/renameDefaultLibDontWork.ts b/tests/cases/fourslash/renameDefaultLibDontWork.ts new file mode 100644 index 00000000000..5d2cfb43eb4 --- /dev/null +++ b/tests/cases/fourslash/renameDefaultLibDontWork.ts @@ -0,0 +1,11 @@ +/// + +// Tests that tokens found on the default library are not renamed. +// "test" is a comment on the default library. + +// @Filename: file1.ts +//// var [|test|] = "foo"; +//// console.log([|test|]); + +const ranges = test.ranges(); +verify.renameLocations(ranges[0], { findInComments: true, ranges }); \ No newline at end of file From 88262dbeb0dfb60af0e27735c10c5a8d9e7b3e27 Mon Sep 17 00:00:00 2001 From: Armando Aguirre Date: Mon, 14 Aug 2017 18:19:43 -0700 Subject: [PATCH 3/5] Changed check for default library by first checking hasNoDefaultLib, second library path and third name of file. --- src/compiler/program.ts | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 55edb377373..25a526ddaf4 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -436,6 +436,7 @@ namespace ts { host = host || createCompilerHost(options); let skipDefaultLib = options.noLib; + const defaultLibraryPath = host.getDefaultLibLocation ? host.getDefaultLibLocation() : getDirectoryPath(host.getDefaultLibFileName(options)); const programDiagnostics = createDiagnosticCollection(); const currentDirectory = host.getCurrentDirectory(); const supportedExtensions = getSupportedExtensions(options); @@ -977,7 +978,15 @@ namespace ts { } function isSourceFileDefaultLibrary(file: SourceFile): boolean { - return file.fileName === host.getDefaultLibFileName(options); + if (file.hasNoDefaultLib) { + return true; + } + + if (defaultLibraryPath !== undefined && defaultLibraryPath.length !== 0) { + return comparePaths(defaultLibraryPath, file.path, currentDirectory, /*ignoreCase*/ true) === Comparison.EqualTo; + } + + return compareStrings(file.fileName, host.getDefaultLibFileName(options), /*ignoreCase*/ true) === Comparison.EqualTo; } function getDiagnosticsProducingTypeChecker() { @@ -1209,7 +1218,7 @@ namespace ts { diagnostics.push(createDiagnosticForNode(node, Diagnostics._0_can_only_be_used_in_a_ts_file, "?")); return; } - // falls through + // falls through case SyntaxKind.MethodDeclaration: case SyntaxKind.MethodSignature: case SyntaxKind.Constructor: @@ -1291,7 +1300,7 @@ namespace ts { diagnostics.push(createDiagnosticForNodeArray(nodes, Diagnostics.type_parameter_declarations_can_only_be_used_in_a_ts_file)); return; } - // falls through + // falls through case SyntaxKind.VariableStatement: // Check modifiers if (nodes === (parent).modifiers) { @@ -1339,8 +1348,8 @@ namespace ts { if (isConstValid) { continue; } - // to report error, - // falls through + // to report error, + // falls through case SyntaxKind.PublicKeyword: case SyntaxKind.PrivateKeyword: case SyntaxKind.ProtectedKeyword: @@ -1556,10 +1565,10 @@ namespace ts { } function getSourceFileFromReferenceWorker( - fileName: string, - getSourceFile: (fileName: string) => SourceFile | undefined, - fail?: (diagnostic: DiagnosticMessage, ...argument: string[]) => void, - refFile?: SourceFile): SourceFile | undefined { + fileName: string, + getSourceFile: (fileName: string) => SourceFile | undefined, + fail?: (diagnostic: DiagnosticMessage, ...argument: string[]) => void, + refFile?: SourceFile): SourceFile | undefined { if (hasExtension(fileName)) { if (!options.allowNonTsExtensions && !forEach(supportedExtensions, extension => fileExtensionIs(host.getCanonicalFileName(fileName), extension))) { From 9dd574b1a92d02dc941a3160d92671281265161b Mon Sep 17 00:00:00 2001 From: Armando Aguirre Date: Tue, 15 Aug 2017 18:44:28 -0700 Subject: [PATCH 4/5] Added host.useCaseSens..., memoized getDefaultLibFileName. --- src/compiler/program.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 25a526ddaf4..bc8f65b35cb 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -325,12 +325,12 @@ namespace ts { } output += sys.newLine; - output += `${ relativeFileName }(${ firstLine + 1 },${ firstLineChar + 1 }): `; + output += `${relativeFileName}(${firstLine + 1},${firstLineChar + 1}): `; } const categoryColor = getCategoryFormat(diagnostic.category); const category = DiagnosticCategory[diagnostic.category].toLowerCase(); - output += `${ formatAndReset(category, categoryColor) } TS${ diagnostic.code }: ${ flattenDiagnosticMessageText(diagnostic.messageText, sys.newLine) }`; + output += `${formatAndReset(category, categoryColor)} TS${diagnostic.code}: ${flattenDiagnosticMessageText(diagnostic.messageText, sys.newLine)}`; output += sys.newLine; } return output; @@ -436,7 +436,8 @@ namespace ts { host = host || createCompilerHost(options); let skipDefaultLib = options.noLib; - const defaultLibraryPath = host.getDefaultLibLocation ? host.getDefaultLibLocation() : getDirectoryPath(host.getDefaultLibFileName(options)); + const getDefaultLibraryFileName = memoize(() => host.getDefaultLibFileName(options)); + const defaultLibraryPath = host.getDefaultLibLocation ? host.getDefaultLibLocation() : getDirectoryPath(getDefaultLibraryFileName()); const programDiagnostics = createDiagnosticCollection(); const currentDirectory = host.getCurrentDirectory(); const supportedExtensions = getSupportedExtensions(options); @@ -512,12 +513,11 @@ namespace ts { // If '--lib' is not specified, include default library file according to '--target' // otherwise, using options specified in '--lib' instead of '--target' default library file if (!options.lib) { - processRootFile(host.getDefaultLibFileName(options), /*isDefaultLib*/ true); + processRootFile(getDefaultLibraryFileName(), /*isDefaultLib*/ true); } else { - const libDirectory = host.getDefaultLibLocation ? host.getDefaultLibLocation() : getDirectoryPath(host.getDefaultLibFileName(options)); forEach(options.lib, libFileName => { - processRootFile(combinePaths(libDirectory, libFileName), /*isDefaultLib*/ true); + processRootFile(combinePaths(defaultLibraryPath, libFileName), /*isDefaultLib*/ true); }); } } @@ -982,11 +982,11 @@ namespace ts { return true; } - if (defaultLibraryPath !== undefined && defaultLibraryPath.length !== 0) { - return comparePaths(defaultLibraryPath, file.path, currentDirectory, /*ignoreCase*/ true) === Comparison.EqualTo; + if (defaultLibraryPath && defaultLibraryPath.length !== 0) { + return containsPath(defaultLibraryPath, file.path, currentDirectory, /*ignoreCase*/ !host.useCaseSensitiveFileNames()); } - return compareStrings(file.fileName, host.getDefaultLibFileName(options), /*ignoreCase*/ true) === Comparison.EqualTo; + return compareStrings(file.fileName, getDefaultLibraryFileName(), /*ignoreCase*/ !host.useCaseSensitiveFileNames()) === Comparison.EqualTo; } function getDiagnosticsProducingTypeChecker() { From 1ab67c0f222f79e4e380febcd7bd8988aa3c92d5 Mon Sep 17 00:00:00 2001 From: Armando Aguirre Date: Thu, 14 Sep 2017 12:48:04 -0700 Subject: [PATCH 5/5] Fixed sourceFiles type error --- 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 9cb7d6638b9..90f9acd82ea 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -1543,7 +1543,7 @@ namespace ts { } } else { - sourceFiles = program.getSourceFiles(); + sourceFiles = program.getSourceFiles().slice(); } return FindAllReferences.findReferencedEntries(program, cancellationToken, sourceFiles, getValidSourceFile(fileName), position, options);