diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 00e933d680ab9..f18d396e9dd9b 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -438,6 +438,8 @@ namespace ts { host = host || createCompilerHost(options); let skipDefaultLib = options.noLib; + 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); @@ -513,12 +515,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); }); } } @@ -557,6 +558,7 @@ namespace ts { getFileProcessingDiagnostics: () => fileProcessingDiagnostics, getResolvedTypeReferenceDirectives: () => resolvedTypeReferenceDirectives, isSourceFileFromExternalLibrary, + isSourceFileDefaultLibrary, dropDiagnosticsProducingTypeChecker, getSourceFileFromReference, sourceFileToPackageName, @@ -977,6 +979,18 @@ namespace ts { return sourceFilesFoundSearchingNodeModules.get(file.path); } + function isSourceFileDefaultLibrary(file: SourceFile): boolean { + if (file.hasNoDefaultLib) { + return true; + } + + if (defaultLibraryPath && defaultLibraryPath.length !== 0) { + return containsPath(defaultLibraryPath, file.path, currentDirectory, /*ignoreCase*/ !host.useCaseSensitiveFileNames()); + } + + return compareStrings(file.fileName, getDefaultLibraryFileName(), /*ignoreCase*/ !host.useCaseSensitiveFileNames()) === Comparison.EqualTo; + } + function getDiagnosticsProducingTypeChecker() { return diagnosticsProducingTypeChecker || (diagnosticsProducingTypeChecker = createTypeChecker(program, /*produceDiagnostics:*/ true)); } @@ -1208,7 +1222,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: @@ -1290,7 +1304,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) { @@ -1338,8 +1352,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: @@ -1553,10 +1567,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))) { diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 64cd32e0a0b17..1389da4765fc0 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -2513,6 +2513,8 @@ namespace ts { /* @internal */ getFileProcessingDiagnostics(): DiagnosticCollection; /* @internal */ getResolvedTypeReferenceDirectives(): Map; 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 bdd0eb41e4fd8..90f9acd82eab3 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -672,7 +672,7 @@ namespace ts { if (!hasModifier(node, ModifierFlags.ParameterPropertyModifier)) { break; } - // falls through + // falls through case SyntaxKind.VariableDeclaration: case SyntaxKind.BindingElement: { const decl = node; @@ -684,7 +684,7 @@ namespace ts { visit(decl.initializer); } } - // falls through + // falls through case SyntaxKind.EnumMember: case SyntaxKind.PropertyDeclaration: case SyntaxKind.PropertySignature: @@ -737,7 +737,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); @@ -1532,7 +1532,21 @@ 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 && options.isForRename) { + for (const sourceFile of program.getSourceFiles()) { + if (!program.isSourceFileDefaultLibrary(sourceFile)) { + sourceFiles.push(sourceFile); + } + } + } + else { + sourceFiles = program.getSourceFiles().slice(); + } + + return FindAllReferences.findReferencedEntries(program, cancellationToken, sourceFiles, getValidSourceFile(fileName), position, options); } function findReferences(fileName: string, position: number): ReferencedSymbol[] { @@ -2132,7 +2146,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: @@ -2157,7 +2171,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) && diff --git a/tests/cases/fourslash/renameDefaultLibDontWork.ts b/tests/cases/fourslash/renameDefaultLibDontWork.ts new file mode 100644 index 0000000000000..5d2cfb43eb4b2 --- /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