Skip to content
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

Fix completion record for variable declarations #13596

Merged
merged 1 commit into from Jul 27, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 13 additions & 5 deletions packages/babel-traverse/src/path/family.ts
Expand Up @@ -193,11 +193,19 @@ function getStatementListCompletion(
}
} else if (paths.length) {
// 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
// the completion
completions = completions.concat(
_getCompletionRecords(paths[paths.length - 1], context),
);
// search on statement lists and assume that the last
// non-variable-declaration statement determines the completion.
for (let i = paths.length - 1; i >= 0; i--) {
const pathCompletions = _getCompletionRecords(paths[i], context);
if (
pathCompletions.length > 1 ||
(pathCompletions.length === 1 &&
!pathCompletions[0].path.isVariableDeclaration())
) {
completions = completions.concat(pathCompletions);
fedeci marked this conversation as resolved.
Show resolved Hide resolved
break;
}
}
}
return completions;
}
Expand Down
38 changes: 38 additions & 0 deletions packages/babel-traverse/test/family.js
Expand Up @@ -109,6 +109,44 @@ describe("path/family", function () {
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) {
Expand Down