Skip to content

Harden Extract Symbol against symbols without declarations #21845

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/harness/unittests/extractFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,11 @@ export default class {
M() {
[#|1 + 1|];
}
}`);

testExtractFunction("extractFunction_NoDeclarations", `
function F() {
[#|arguments.length|]; // arguments has no declaration
}`);
});

Expand Down
6 changes: 4 additions & 2 deletions src/services/refactors/extractSymbol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;
}