mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-09 16:39:46 -05:00
Merge pull request #21845 from amcasey/GH21793
Harden Extract Symbol against symbols without declarations
This commit is contained in:
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user