Skip to content

Commit 21bbdd3

Browse files
author
Armando Aguirre
authored
Merge pull request #17415 from armanio123/FixRenameInDefaultLibrary
Excluded the default library from rename service.
2 parents 66abcb9 + 1ab67c0 commit 21bbdd3

File tree

4 files changed

+58
-17
lines changed

4 files changed

+58
-17
lines changed

src/compiler/program.ts

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,8 @@ namespace ts {
438438
host = host || createCompilerHost(options);
439439

440440
let skipDefaultLib = options.noLib;
441+
const getDefaultLibraryFileName = memoize(() => host.getDefaultLibFileName(options));
442+
const defaultLibraryPath = host.getDefaultLibLocation ? host.getDefaultLibLocation() : getDirectoryPath(getDefaultLibraryFileName());
441443
const programDiagnostics = createDiagnosticCollection();
442444
const currentDirectory = host.getCurrentDirectory();
443445
const supportedExtensions = getSupportedExtensions(options);
@@ -513,12 +515,11 @@ namespace ts {
513515
// If '--lib' is not specified, include default library file according to '--target'
514516
// otherwise, using options specified in '--lib' instead of '--target' default library file
515517
if (!options.lib) {
516-
processRootFile(host.getDefaultLibFileName(options), /*isDefaultLib*/ true);
518+
processRootFile(getDefaultLibraryFileName(), /*isDefaultLib*/ true);
517519
}
518520
else {
519-
const libDirectory = host.getDefaultLibLocation ? host.getDefaultLibLocation() : getDirectoryPath(host.getDefaultLibFileName(options));
520521
forEach(options.lib, libFileName => {
521-
processRootFile(combinePaths(libDirectory, libFileName), /*isDefaultLib*/ true);
522+
processRootFile(combinePaths(defaultLibraryPath, libFileName), /*isDefaultLib*/ true);
522523
});
523524
}
524525
}
@@ -557,6 +558,7 @@ namespace ts {
557558
getFileProcessingDiagnostics: () => fileProcessingDiagnostics,
558559
getResolvedTypeReferenceDirectives: () => resolvedTypeReferenceDirectives,
559560
isSourceFileFromExternalLibrary,
561+
isSourceFileDefaultLibrary,
560562
dropDiagnosticsProducingTypeChecker,
561563
getSourceFileFromReference,
562564
sourceFileToPackageName,
@@ -977,6 +979,18 @@ namespace ts {
977979
return sourceFilesFoundSearchingNodeModules.get(file.path);
978980
}
979981

982+
function isSourceFileDefaultLibrary(file: SourceFile): boolean {
983+
if (file.hasNoDefaultLib) {
984+
return true;
985+
}
986+
987+
if (defaultLibraryPath && defaultLibraryPath.length !== 0) {
988+
return containsPath(defaultLibraryPath, file.path, currentDirectory, /*ignoreCase*/ !host.useCaseSensitiveFileNames());
989+
}
990+
991+
return compareStrings(file.fileName, getDefaultLibraryFileName(), /*ignoreCase*/ !host.useCaseSensitiveFileNames()) === Comparison.EqualTo;
992+
}
993+
980994
function getDiagnosticsProducingTypeChecker() {
981995
return diagnosticsProducingTypeChecker || (diagnosticsProducingTypeChecker = createTypeChecker(program, /*produceDiagnostics:*/ true));
982996
}
@@ -1208,7 +1222,7 @@ namespace ts {
12081222
diagnostics.push(createDiagnosticForNode(node, Diagnostics._0_can_only_be_used_in_a_ts_file, "?"));
12091223
return;
12101224
}
1211-
// falls through
1225+
// falls through
12121226
case SyntaxKind.MethodDeclaration:
12131227
case SyntaxKind.MethodSignature:
12141228
case SyntaxKind.Constructor:
@@ -1290,7 +1304,7 @@ namespace ts {
12901304
diagnostics.push(createDiagnosticForNodeArray(nodes, Diagnostics.type_parameter_declarations_can_only_be_used_in_a_ts_file));
12911305
return;
12921306
}
1293-
// falls through
1307+
// falls through
12941308
case SyntaxKind.VariableStatement:
12951309
// Check modifiers
12961310
if (nodes === (<ClassDeclaration | FunctionLikeDeclaration | VariableStatement>parent).modifiers) {
@@ -1338,8 +1352,8 @@ namespace ts {
13381352
if (isConstValid) {
13391353
continue;
13401354
}
1341-
// to report error,
1342-
// falls through
1355+
// to report error,
1356+
// falls through
13431357
case SyntaxKind.PublicKeyword:
13441358
case SyntaxKind.PrivateKeyword:
13451359
case SyntaxKind.ProtectedKeyword:
@@ -1553,10 +1567,10 @@ namespace ts {
15531567
}
15541568

15551569
function getSourceFileFromReferenceWorker(
1556-
fileName: string,
1557-
getSourceFile: (fileName: string) => SourceFile | undefined,
1558-
fail?: (diagnostic: DiagnosticMessage, ...argument: string[]) => void,
1559-
refFile?: SourceFile): SourceFile | undefined {
1570+
fileName: string,
1571+
getSourceFile: (fileName: string) => SourceFile | undefined,
1572+
fail?: (diagnostic: DiagnosticMessage, ...argument: string[]) => void,
1573+
refFile?: SourceFile): SourceFile | undefined {
15601574

15611575
if (hasExtension(fileName)) {
15621576
if (!options.allowNonTsExtensions && !forEach(supportedExtensions, extension => fileExtensionIs(host.getCanonicalFileName(fileName), extension))) {

src/compiler/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2513,6 +2513,8 @@ namespace ts {
25132513
/* @internal */ getFileProcessingDiagnostics(): DiagnosticCollection;
25142514
/* @internal */ getResolvedTypeReferenceDirectives(): Map<ResolvedTypeReferenceDirective>;
25152515
isSourceFileFromExternalLibrary(file: SourceFile): boolean;
2516+
/* @internal */ isSourceFileDefaultLibrary(file: SourceFile): boolean;
2517+
25162518
// For testing purposes only.
25172519
/* @internal */ structureIsReused?: StructureIsReused;
25182520

src/services/services.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ namespace ts {
672672
if (!hasModifier(node, ModifierFlags.ParameterPropertyModifier)) {
673673
break;
674674
}
675-
// falls through
675+
// falls through
676676
case SyntaxKind.VariableDeclaration:
677677
case SyntaxKind.BindingElement: {
678678
const decl = <VariableDeclaration>node;
@@ -684,7 +684,7 @@ namespace ts {
684684
visit(decl.initializer);
685685
}
686686
}
687-
// falls through
687+
// falls through
688688
case SyntaxKind.EnumMember:
689689
case SyntaxKind.PropertyDeclaration:
690690
case SyntaxKind.PropertySignature:
@@ -737,7 +737,7 @@ namespace ts {
737737

738738
class SourceMapSourceObject implements SourceMapSource {
739739
lineMap: number[];
740-
constructor (public fileName: string, public text: string, public skipTrivia?: (pos: number) => number) {}
740+
constructor(public fileName: string, public text: string, public skipTrivia?: (pos: number) => number) { }
741741

742742
public getLineAndCharacterOfPosition(pos: number): LineAndCharacter {
743743
return ts.getLineAndCharacterOfPosition(this, pos);
@@ -1532,7 +1532,21 @@ namespace ts {
15321532

15331533
function getReferences(fileName: string, position: number, options?: FindAllReferences.Options) {
15341534
synchronizeHostData();
1535-
return FindAllReferences.findReferencedEntries(program, cancellationToken, program.getSourceFiles(), getValidSourceFile(fileName), position, options);
1535+
1536+
// Exclude default library when renaming as commonly user don't want to change that file.
1537+
let sourceFiles: SourceFile[] = [];
1538+
if (options && options.isForRename) {
1539+
for (const sourceFile of program.getSourceFiles()) {
1540+
if (!program.isSourceFileDefaultLibrary(sourceFile)) {
1541+
sourceFiles.push(sourceFile);
1542+
}
1543+
}
1544+
}
1545+
else {
1546+
sourceFiles = program.getSourceFiles().slice();
1547+
}
1548+
1549+
return FindAllReferences.findReferencedEntries(program, cancellationToken, sourceFiles, getValidSourceFile(fileName), position, options);
15361550
}
15371551

15381552
function findReferences(fileName: string, position: number): ReferencedSymbol[] {
@@ -2132,7 +2146,7 @@ namespace ts {
21322146
isLiteralComputedPropertyDeclarationName(node);
21332147
}
21342148

2135-
function isObjectLiteralElement(node: Node): node is ObjectLiteralElement {
2149+
function isObjectLiteralElement(node: Node): node is ObjectLiteralElement {
21362150
switch (node.kind) {
21372151
case SyntaxKind.JsxAttribute:
21382152
case SyntaxKind.JsxSpreadAttribute:
@@ -2157,7 +2171,7 @@ namespace ts {
21572171
if (node.parent.kind === SyntaxKind.ComputedPropertyName) {
21582172
return isObjectLiteralElement(node.parent.parent) ? node.parent.parent : undefined;
21592173
}
2160-
// falls through
2174+
// falls through
21612175
case SyntaxKind.Identifier:
21622176
return isObjectLiteralElement(node.parent) &&
21632177
(node.parent.parent.kind === SyntaxKind.ObjectLiteralExpression || node.parent.parent.kind === SyntaxKind.JsxAttributes) &&
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// Tests that tokens found on the default library are not renamed.
4+
// "test" is a comment on the default library.
5+
6+
// @Filename: file1.ts
7+
//// var [|test|] = "foo";
8+
//// console.log([|test|]);
9+
10+
const ranges = test.ranges();
11+
verify.renameLocations(ranges[0], { findInComments: true, ranges });

0 commit comments

Comments
 (0)