Fix completion record for variable declarations (#13596)
This commit is contained in:
parent
e0dc925bbe
commit
d1f908924c
@ -193,11 +193,19 @@ function getStatementListCompletion(
|
|||||||
}
|
}
|
||||||
} else if (paths.length) {
|
} else if (paths.length) {
|
||||||
// When we are in a context where `break` must not exist, we can skip linear
|
// When we are in a context where `break` must not exist, we can skip linear
|
||||||
// search on statement lists and assume that the last statement determines
|
// search on statement lists and assume that the last
|
||||||
// the completion
|
// non-variable-declaration statement determines the completion.
|
||||||
completions = completions.concat(
|
for (let i = paths.length - 1; i >= 0; i--) {
|
||||||
_getCompletionRecords(paths[paths.length - 1], context),
|
const pathCompletions = _getCompletionRecords(paths[i], context);
|
||||||
);
|
if (
|
||||||
|
pathCompletions.length > 1 ||
|
||||||
|
(pathCompletions.length === 1 &&
|
||||||
|
!pathCompletions[0].path.isVariableDeclaration())
|
||||||
|
) {
|
||||||
|
completions = completions.concat(pathCompletions);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return completions;
|
return completions;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -109,6 +109,44 @@ describe("path/family", function () {
|
|||||||
expect(consequentHasScope).toBe(true);
|
expect(consequentHasScope).toBe(true);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
describe("getCompletionRecords", function () {
|
||||||
|
it("should skip variable declarations", function () {
|
||||||
|
const ast = parse("'foo' + 'bar'; var a = 10; let b = 20; const c = 30;");
|
||||||
|
let records = [];
|
||||||
|
traverse(ast, {
|
||||||
|
Program(path) {
|
||||||
|
records = path.getCompletionRecords();
|
||||||
|
},
|
||||||
|
});
|
||||||
|
expect(records).toHaveLength(1);
|
||||||
|
expect(records[0].node.type).toBe("ExpressionStatement");
|
||||||
|
expect(records[0].node.expression.type).toBe("BinaryExpression");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should skip variable declarations in a BlockStatement", function () {
|
||||||
|
const ast = parse("'foo' + 'bar'; { var a = 10; }");
|
||||||
|
let records = [];
|
||||||
|
traverse(ast, {
|
||||||
|
Program(path) {
|
||||||
|
records = path.getCompletionRecords();
|
||||||
|
},
|
||||||
|
});
|
||||||
|
expect(records).toHaveLength(1);
|
||||||
|
expect(records[0].node.type).toBe("ExpressionStatement");
|
||||||
|
expect(records[0].node.expression.type).toBe("BinaryExpression");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should be empty if there are only variable declarations", function () {
|
||||||
|
const ast = parse("var a = 10; let b = 20; const c = 30;");
|
||||||
|
let records = [];
|
||||||
|
traverse(ast, {
|
||||||
|
Program(path) {
|
||||||
|
records = path.getCompletionRecords();
|
||||||
|
},
|
||||||
|
});
|
||||||
|
expect(records).toHaveLength(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
function hop(o, key) {
|
function hop(o, key) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user