Skip lone variable declarations

This commit is contained in:
Andrew Branch
2019-04-18 16:03:53 -07:00
parent fed910fd0c
commit 5479893f00
2 changed files with 28 additions and 17 deletions

View File

@@ -18,16 +18,19 @@ namespace ts.SelectionRange {
}
if (positionShouldSnapToNode(pos, node, nextNode)) {
// Blocks are effectively redundant with SyntaxLists.
// TemplateSpans, along with the SyntaxLists containing them,
// are a somewhat unintuitive grouping of things that should be
// considered independently. A VariableStatements children are just
// a VaraiableDeclarationList and a semicolon.
// 1. Blocks are effectively redundant with SyntaxLists.
// 2. TemplateSpans, along with the SyntaxLists containing them, are a somewhat unintuitive grouping
// of things that should be considered independently.
// 3. A VariableStatements children are just a VaraiableDeclarationList and a semicolon.
// 4. A lone VariableDeclaration in a VaraibleDeclaration feels redundant with the VariableStatement.
//
// Dive in without pushing a selection range.
if (isBlock(node)
|| isTemplateSpan(node) || isTemplateHead(node)
|| prevNode && isTemplateHead(prevNode)
|| isVariableDeclarationList(node) && isVariableStatement(parentNode)) {
|| isVariableDeclarationList(node) && isVariableStatement(parentNode)
|| isSyntaxList(node) && isVariableDeclarationList(parentNode)
|| isVariableDeclaration(node) && isSyntaxList(parentNode) && children.length === 1) {
parentNode = node;
break;
}

View File

@@ -568,18 +568,13 @@ const { x, y: a, ...zs = {} } = {};`);
start: { line: 2, offset: 7 },
end: { line: 2, offset: 30 } },
parent: {
textSpan: { // { x, y: a, ...zs = {} } = {}
start: { line: 2, offset: 7 },
end: { line: 2, offset: 35 } },
textSpan: { // whole line
start: { line: 2, offset: 1 },
end: { line: 2, offset: 36 } },
parent: {
textSpan: { // whole line
start: { line: 2, offset: 1 },
end: { line: 2, offset: 36 } },
parent: {
textSpan: {
start: { line: 1, offset: 1 },
end: { line: 2, offset: 36 } } } } } } } } },
});
textSpan: {
start: { line: 1, offset: 1 },
end: { line: 2, offset: 36 } } } } } } } } });
});
it("consumes all whitespace in a multi-line function parameter list", () => {
@@ -644,5 +639,18 @@ function square(x) {
start: { line: 1, offset: 1 },
end: { line: 7, offset: 2 } } } } } }]);
});
it("skips lone VariableDeclarations in a declaration list", () => {
const getSelectionRange = setup("/file.ts", `const x = 3;`);
const locations = getSelectionRange([{ line: 1, offset: 7 }]); // x
assert.deepEqual(locations, [{
textSpan: {
start: { line: 1, offset: 7 },
end: { line: 1, offset: 8 } },
parent: {
textSpan: {
start: { line: 1, offset: 1 },
end: { line: 1, offset: 13 } } } }]);
});
});
}