Stop inadvertently exempting expression statements from check

This commit is contained in:
Andrew Casey
2017-10-03 12:23:32 -07:00
parent 2a4ab08655
commit 7b1147fbce
4 changed files with 42 additions and 8 deletions

View File

@@ -40,6 +40,13 @@ function F() {
}
`);
testExtractConstant("extractConstant_ExpressionStatementConsumesLocal", `
function F() {
let i = 0;
[#|i++|];
}
`);
testExtractConstant("extractConstant_BlockScopes_NoDependencies",
`for (let i = 0; i < 10; i++) {
for (let j = 0; j < 10; j++) {

View File

@@ -1351,14 +1351,13 @@ namespace ts.refactor.extractSymbol {
}
for (let i = 0; i < scopes.length; i++) {
if (!isReadonlyArray(targetRange.range)) {
const scopeUsages = usagesPerScope[i];
// Special case: in the innermost scope, all usages are available.
// (The computed value reflects the value at the top-level of the scope, but the
// local will actually be declared at the same level as the extracted expression).
if (i > 0 && (scopeUsages.usages.size > 0 || scopeUsages.typeParameterUsages.size > 0)) {
constantErrorsPerScope[i].push(createDiagnosticForNode(targetRange.range, Messages.CannotAccessVariablesFromNestedScopes));
}
const scopeUsages = usagesPerScope[i];
// Special case: in the innermost scope, all usages are available.
// (The computed value reflects the value at the top-level of the scope, but the
// local will actually be declared at the same level as the extracted expression).
if (i > 0 && (scopeUsages.usages.size > 0 || scopeUsages.typeParameterUsages.size > 0)) {
const errorNode = isReadonlyArray(targetRange.range) ? targetRange.range[0] : targetRange.range;
constantErrorsPerScope[i].push(createDiagnosticForNode(errorNode, Messages.CannotAccessVariablesFromNestedScopes));
}
let hasWrite = false;

View File

@@ -0,0 +1,14 @@
// ==ORIGINAL==
function F() {
let i = 0;
i++;
}
// ==SCOPE::Extract to constant in enclosing scope==
function F() {
let i = 0;
const /*RENAME*/newLocal = i++;
}

View File

@@ -0,0 +1,14 @@
// ==ORIGINAL==
function F() {
let i = 0;
i++;
}
// ==SCOPE::Extract to constant in enclosing scope==
function F() {
let i = 0;
const /*RENAME*/newLocal = i++;
}