Harden Extract Symbol against symbols without declarations

Fixes #21793
This commit is contained in:
Andrew Casey 2018-02-09 14:49:44 -08:00
parent 57d94b9661
commit e65a1a429c
4 changed files with 57 additions and 2 deletions

View File

@ -552,6 +552,11 @@ export default class {
M() {
[#|1 + 1|];
}
}`);
testExtractFunction("extractFunction_NoDeclarations", `
function F() {
[#|arguments.length|]; // arguments has no declaration
}`);
});

View File

@ -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);

View File

@ -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;
}

View File

@ -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;
}