mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-05 08:11:30 -06:00
Don't allow to rename string literal (#24477)
This commit is contained in:
parent
364fce393d
commit
011a4df4dd
@ -21,39 +21,31 @@ namespace ts.Rename {
|
||||
|
||||
function getRenameInfoForNode(node: Node, typeChecker: TypeChecker, sourceFile: SourceFile, isDefinedInLibraryFile: (declaration: Node) => boolean): RenameInfo | undefined {
|
||||
const symbol = typeChecker.getSymbolAtLocation(node);
|
||||
|
||||
if (!symbol) return;
|
||||
// Only allow a symbol to be renamed if it actually has at least one declaration.
|
||||
if (symbol) {
|
||||
const { declarations } = symbol;
|
||||
if (declarations && declarations.length > 0) {
|
||||
// Disallow rename for elements that are defined in the standard TypeScript library.
|
||||
if (declarations.some(isDefinedInLibraryFile)) {
|
||||
return getRenameInfoError(Diagnostics.You_cannot_rename_elements_that_are_defined_in_the_standard_TypeScript_library);
|
||||
}
|
||||
const { declarations } = symbol;
|
||||
if (!declarations || declarations.length === 0) return;
|
||||
|
||||
// Cannot rename `default` as in `import { default as foo } from "./someModule";
|
||||
if (isIdentifier(node) && node.originalKeywordKind === SyntaxKind.DefaultKeyword && symbol.parent!.flags & SymbolFlags.Module) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// Can't rename a module name.
|
||||
if (isStringLiteralLike(node) && tryGetImportFromModuleSpecifier(node)) return undefined;
|
||||
|
||||
const kind = SymbolDisplay.getSymbolKind(typeChecker, symbol, node);
|
||||
const specifierName = (isImportOrExportSpecifierName(node) || isStringOrNumericLiteral(node) && node.parent.kind === SyntaxKind.ComputedPropertyName)
|
||||
? stripQuotes(getTextOfIdentifierOrLiteral(node))
|
||||
: undefined;
|
||||
const displayName = specifierName || typeChecker.symbolToString(symbol);
|
||||
const fullDisplayName = specifierName || typeChecker.getFullyQualifiedName(symbol);
|
||||
return getRenameInfoSuccess(displayName, fullDisplayName, kind, SymbolDisplay.getSymbolModifiers(symbol), node, sourceFile);
|
||||
}
|
||||
// Disallow rename for elements that are defined in the standard TypeScript library.
|
||||
if (declarations.some(isDefinedInLibraryFile)) {
|
||||
return getRenameInfoError(Diagnostics.You_cannot_rename_elements_that_are_defined_in_the_standard_TypeScript_library);
|
||||
}
|
||||
else if (isStringLiteral(node)) {
|
||||
if (isDefinedInLibraryFile(node)) {
|
||||
return getRenameInfoError(Diagnostics.You_cannot_rename_elements_that_are_defined_in_the_standard_TypeScript_library);
|
||||
}
|
||||
return getRenameInfoSuccess(node.text, node.text, ScriptElementKind.variableElement, ScriptElementKindModifier.none, node, sourceFile);
|
||||
|
||||
// Cannot rename `default` as in `import { default as foo } from "./someModule";
|
||||
if (isIdentifier(node) && node.originalKeywordKind === SyntaxKind.DefaultKeyword && symbol.parent!.flags & SymbolFlags.Module) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// Can't rename a module name.
|
||||
if (isStringLiteralLike(node) && tryGetImportFromModuleSpecifier(node)) return undefined;
|
||||
|
||||
const kind = SymbolDisplay.getSymbolKind(typeChecker, symbol, node);
|
||||
const specifierName = (isImportOrExportSpecifierName(node) || isStringOrNumericLiteral(node) && node.parent.kind === SyntaxKind.ComputedPropertyName)
|
||||
? stripQuotes(getTextOfIdentifierOrLiteral(node))
|
||||
: undefined;
|
||||
const displayName = specifierName || typeChecker.symbolToString(symbol);
|
||||
const fullDisplayName = specifierName || typeChecker.getFullyQualifiedName(symbol);
|
||||
return getRenameInfoSuccess(displayName, fullDisplayName, kind, SymbolDisplay.getSymbolModifiers(symbol), node, sourceFile);
|
||||
}
|
||||
|
||||
function getRenameInfoSuccess(displayName: string, fullDisplayName: string, kind: ScriptElementKind, kindModifiers: string, node: Node, sourceFile: SourceFile): RenameInfo {
|
||||
|
||||
@ -11,4 +11,4 @@
|
||||
////
|
||||
////animate({ deltaX: 100, deltaY: 100, easing: "[|ease-in-out|]" });
|
||||
|
||||
verify.rangesAreRenameLocations();
|
||||
goTo.eachRange(() => { verify.renameInfoFailed(); });
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user