From 5b860557867bed3a10b33a98c0ac99a8e9cd5dab Mon Sep 17 00:00:00 2001 From: Armando Aguirre Date: Tue, 25 Jul 2017 14:21:57 -0700 Subject: [PATCH] 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) &&