Excluded the default library from rename service.#17415
Conversation
|
@armanio123, |
| 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) { } |
There was a problem hiding this comment.
All these white-space changes are interesting. Is it passing the linter? (Was it before?)
There was a problem hiding this comment.
Yes it is passing linter (before and after). This was done by VS Code formatting, I thought it should be pushed.
| } | ||
|
|
||
| function isSourceFileDefaultLibrary(file: SourceFile): boolean { | ||
| return file.fileName === host.getDefaultLibFileName(options); |
There was a problem hiding this comment.
I thought @rbuckton was factoring the core libraries into a set of smaller files that get added. If that's the case, I'm not sure this would work (as there wouldn't be just one default lib filename).
There was a problem hiding this comment.
If that the case, we might want to change this method to check for all of the different files. I'll talk to @rbuckton and get his input.
There was a problem hiding this comment.
Based on what I have discussed with @rbuckton the better way of doing this it to check for GetDefaultLibDirectory. Every file that is there is going to be considered core library.
There was a problem hiding this comment.
Perhaps this should be named isSourceFileReadonly or isSourceFileLockedForEdits so that we can later use the same functionality to prevent renames in declarations pulled in from node_modules or other "typeRoots" locations.
| let sourceFiles: SourceFile[] = []; | ||
| if (options.isForRename) { | ||
| for (let sourceFile of program.getSourceFiles()) { | ||
| if (!program.isSourceFileDefaultLibrary(sourceFile)) { |
There was a problem hiding this comment.
So previously if you tried to rename something that was defined in the core library (e.g. a JavaScript API such as Array.map, or a DOM API such as window.addEventListener), it would error because you can't rename something in the core library. With this change, will that rename now proceed, but only for all your program files (i.e. leaving you in an error state)? I'm not sure that's a better behavior.
There was a problem hiding this comment.
That functionality will remain the same. That happens on the function getRenameInfo.
This piece of code specifically checks for the locations of the tokens (not the token type). It will only exclude the default library from being referenced when searching for the token to be renamed.
billti
left a comment
There was a problem hiding this comment.
I'd follow up with Ron on the core library not being one file piece, and Ryan on the behavior when renaming a core API now - but if they're happy, then looks good to me.
| 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. |
There was a problem hiding this comment.
Will this block renaming a symbol declared in a default library? I would think that if a symbol is declared in a default library then we shouldn't allow the rename to proceed at all, rather than renaming it in a user's source files and resulting in possible compile time errors.
There was a problem hiding this comment.
There's already a feature for that. This will avoid renaming anything on the default library, like comments or strings.
| } | ||
|
|
||
| function isSourceFileDefaultLibrary(file: SourceFile): boolean { | ||
| return file.fileName === host.getDefaultLibFileName(options); |
There was a problem hiding this comment.
Perhaps this should be named isSourceFileReadonly or isSourceFileLockedForEdits so that we can later use the same functionality to prevent renames in declarations pulled in from node_modules or other "typeRoots" locations.
6cc5710 to
675b6fb
Compare
…second library path and third name of file.
| return true; | ||
| } | ||
|
|
||
| if (defaultLibraryPath !== undefined && defaultLibraryPath.length !== 0) { |
There was a problem hiding this comment.
This can be simplified: if (defaultLibraryPath)
| } | ||
|
|
||
| if (defaultLibraryPath !== undefined && defaultLibraryPath.length !== 0) { | ||
| return comparePaths(defaultLibraryPath, file.path, currentDirectory, /*ignoreCase*/ true) === Comparison.EqualTo; |
There was a problem hiding this comment.
Do not unconditionally pass true for ignoreCase, instead use !host.useCaseInsensitiveFileNames().
There was a problem hiding this comment.
I don't think comparePaths is the one you want to call. I think you meant to call containsPath here.
| return comparePaths(defaultLibraryPath, file.path, currentDirectory, /*ignoreCase*/ true) === Comparison.EqualTo; | ||
| } | ||
|
|
||
| return compareStrings(file.fileName, host.getDefaultLibFileName(options), /*ignoreCase*/ true) === Comparison.EqualTo; |
There was a problem hiding this comment.
Do not unconditionally pass true for ignoreCase, instead use !host.useCaseInsensitiveFileNames().
| host = host || createCompilerHost(options); | ||
|
|
||
| let skipDefaultLib = options.noLib; | ||
| const defaultLibraryPath = host.getDefaultLibLocation ? host.getDefaultLibLocation() : getDirectoryPath(host.getDefaultLibFileName(options)); |
There was a problem hiding this comment.
There is also a libDirectory variable in this function that does the same thing. Consider combining the two and possibly storing the result of host.getDefaultLibFileName(options) in a variable rather than requery it each time.
rbuckton
left a comment
There was a problem hiding this comment.
Approved, with a minor comment.
| host = host || createCompilerHost(options); | ||
|
|
||
| let skipDefaultLib = options.noLib; | ||
| const getDefaultLibraryFileName = memoize(() => host.getDefaultLibFileName(options)); |
There was a problem hiding this comment.
Why bother with memoize and not just have const defaultLibraryFileName = host.getDefaultLibraryFileName(options)?
There was a problem hiding this comment.
Depending on the options and some logic, the variable might end up not being needed. This was done for lazy evaluation.
|
I do not think this is the best solution here. the default libraries are not any special. users can replace them. and the same issue persists if some one say renamed A better solution is to block rename for symbols coming from a .d.ts file unless the source of the rename operation is that file. see #8227 |
Excludes the default library when renaming tokens.