Skip to content

Commit fae73f0

Browse files
author
Maria Solano
committed
More tests
1 parent b788987 commit fae73f0

File tree

5 files changed

+62
-6
lines changed

5 files changed

+62
-6
lines changed

src/services/refactors/inlineVariable.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
Expression,
77
factory,
88
FindAllReferences,
9+
flatMap,
910
getExpressionPrecedence,
1011
getLocaleSpecificMessage,
1112
getTokenAtPosition,
@@ -23,6 +24,7 @@ import {
2324
VariableDeclaration,
2425
} from "../_namespaces/ts";
2526
import { RefactorErrorInfo, registerRefactor } from "../_namespaces/ts.refactor";
27+
import { getReferenceEntriesForNode } from "../findAllReferences";
2628

2729
const refactorName = "Inline variable";
2830
const refactorDescription = getLocaleSpecificMessage(Diagnostics.Inline_variable);
@@ -119,30 +121,30 @@ function getInliningInfo(file: SourceFile, startPosition: number, tryWithReferen
119121
if (isInitializedVariable(parent) && isVariableDeclarationInVariableStatement(parent)) {
120122
// Find all references to the variable.
121123
const name = parent.name;
122-
const referencedSymbols = FindAllReferences.Core.getReferencedSymbolsForNode(name.pos, name, program, program.getSourceFiles(), cancellationToken);
123-
if (!referencedSymbols || referencedSymbols.length !== 1) {
124+
const referenceEntries = getReferenceEntriesForNode(name.pos, name, program, program.getSourceFiles(), cancellationToken);
125+
if (!referenceEntries) {
124126
return undefined;
125127
}
126-
const referenceNodes = getReferenceNodes(referencedSymbols[0].references, name);
128+
const referenceNodes = getReferenceNodes(referenceEntries, name);
127129

128130
return referenceNodes && { references: referenceNodes, declaration: parent, replacement: parent.initializer };
129131
}
130132

131133
if (tryWithReferenceToken && isIdentifier(token)) {
132134
// Try finding the declaration and nodes to replace via the reference token.
133135
const referencedSymbols = FindAllReferences.Core.getReferencedSymbolsForNode(token.pos, token, program, program.getSourceFiles(), cancellationToken);
134-
if (!referencedSymbols || referencedSymbols.length !== 1) {
136+
if (!referencedSymbols) {
135137
return undefined;
136138
}
137139

138-
const { definition, references } = referencedSymbols[0];
140+
const { definition } = referencedSymbols[0];
139141
if (definition?.type !== FindAllReferences.DefinitionKind.Symbol) {
140142
return undefined;
141143
}
142144

143145
const { valueDeclaration } = definition.symbol;
144146
if (valueDeclaration && isInitializedVariable(valueDeclaration) && isVariableDeclarationInVariableStatement(valueDeclaration)) {
145-
const referenceNodes = getReferenceNodes(references, valueDeclaration.name);
147+
const referenceNodes = getReferenceNodes(flatMap(referencedSymbols, ({ references }) => references), valueDeclaration.name);
146148

147149
return referenceNodes && { references: referenceNodes, declaration: valueDeclaration, replacement: valueDeclaration.initializer };
148150
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
////let x/**/ = 1;
4+
////function foo() {
5+
//// console.log(x);
6+
////}
7+
////const y = x + 2;
8+
9+
goTo.marker("");
10+
verify.refactorAvailable("Inline variable");
11+
edit.applyRefactor({
12+
refactorName: "Inline variable",
13+
actionName: "Inline variable",
14+
actionDescription: "Inline variable",
15+
newContent: `function foo() {
16+
console.log(1);
17+
}
18+
const y = 1 + 2;`
19+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
////const x/**/ = 1 + 2;
4+
////const y = x * 3;
5+
6+
goTo.marker("");
7+
verify.refactorAvailable("Inline variable");
8+
edit.applyRefactor({
9+
refactorName: "Inline variable",
10+
actionName: "Inline variable",
11+
actionDescription: "Inline variable",
12+
newContent: "const y = (1 + 2) * 3;"
13+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
////const x = 0;
4+
////const y = /*a*/x/*b*/ + 1;
5+
6+
goTo.select("a", "b");
7+
verify.not.refactorAvailableForTriggerReason("implicit", "Inline variable");
8+
verify.refactorAvailableForTriggerReason("invoked", "Inline variable");
9+
edit.applyRefactor({
10+
refactorName: "Inline variable",
11+
actionName: "Inline variable",
12+
actionDescription: "Inline variable",
13+
newContent: "const y = 0 + 1;",
14+
triggerReason: "invoked"
15+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
////const x/**/ = 0;
4+
////const y = x++ + 1;
5+
6+
goTo.marker("");
7+
verify.not.refactorAvailable("Inline variable");

0 commit comments

Comments
 (0)