mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-06 20:14:01 -06:00
Harden Extract Symbol against symbols without declarations
Fixes #21793
This commit is contained in:
parent
57d94b9661
commit
e65a1a429c
@ -552,6 +552,11 @@ export default class {
|
||||
M() {
|
||||
[#|1 + 1|];
|
||||
}
|
||||
}`);
|
||||
|
||||
testExtractFunction("extractFunction_NoDeclarations", `
|
||||
function F() {
|
||||
[#|arguments.length|]; // arguments has no declaration
|
||||
}`);
|
||||
});
|
||||
|
||||
|
||||
@ -1689,7 +1689,8 @@ namespace ts.refactor.extractSymbol {
|
||||
return symbolId;
|
||||
}
|
||||
// find first declaration in this file
|
||||
const declInFile = find(symbol.getDeclarations(), d => d.getSourceFile() === sourceFile);
|
||||
const decls = symbol.getDeclarations();
|
||||
const declInFile = decls && find(decls, d => d.getSourceFile() === sourceFile);
|
||||
if (!declInFile) {
|
||||
return undefined;
|
||||
}
|
||||
@ -1782,7 +1783,8 @@ namespace ts.refactor.extractSymbol {
|
||||
if (!symbol) {
|
||||
return undefined;
|
||||
}
|
||||
if (symbol.getDeclarations().some(d => d.parent === scopeDecl)) {
|
||||
const decls = symbol.getDeclarations();
|
||||
if (decls && decls.some(d => d.parent === scopeDecl)) {
|
||||
return createIdentifier(symbol.name);
|
||||
}
|
||||
const prefix = tryReplaceWithQualifiedNameOrPropertyAccess(symbol.parent, scopeDecl, isTypeNode);
|
||||
|
||||
@ -0,0 +1,24 @@
|
||||
// ==ORIGINAL==
|
||||
|
||||
function F() {
|
||||
/*[#|*/arguments.length/*|]*/; // arguments has no declaration
|
||||
}
|
||||
// ==SCOPE::Extract to inner function in function 'F'==
|
||||
|
||||
function F() {
|
||||
/*RENAME*/newFunction(); // arguments has no declaration
|
||||
|
||||
|
||||
function newFunction() {
|
||||
arguments.length;
|
||||
}
|
||||
}
|
||||
// ==SCOPE::Extract to function in global scope==
|
||||
|
||||
function F() {
|
||||
/*RENAME*/newFunction(); // arguments has no declaration
|
||||
}
|
||||
|
||||
function newFunction() {
|
||||
arguments.length;
|
||||
}
|
||||
@ -0,0 +1,24 @@
|
||||
// ==ORIGINAL==
|
||||
|
||||
function F() {
|
||||
/*[#|*/arguments.length/*|]*/; // arguments has no declaration
|
||||
}
|
||||
// ==SCOPE::Extract to inner function in function 'F'==
|
||||
|
||||
function F() {
|
||||
/*RENAME*/newFunction(); // arguments has no declaration
|
||||
|
||||
|
||||
function newFunction() {
|
||||
arguments.length;
|
||||
}
|
||||
}
|
||||
// ==SCOPE::Extract to function in global scope==
|
||||
|
||||
function F() {
|
||||
/*RENAME*/newFunction(); // arguments has no declaration
|
||||
}
|
||||
|
||||
function newFunction() {
|
||||
arguments.length;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user