fix(semantics): retain trailing rest argument flows (#756)
Signed-off-by: Rudy Celekli <rudy@gradiahq.com>
This commit is contained in:
@@ -234,3 +234,52 @@ describe("JavaScript semantic analysis: calls 2", () => {
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("JavaScript semantic rest parameter flow", () => {
|
||||
it.each([
|
||||
{
|
||||
parameters: "first, ...rest",
|
||||
arguments: "1, 2, 3",
|
||||
expected: [
|
||||
[0, "first"],
|
||||
[1, "rest"],
|
||||
[2, "rest"],
|
||||
],
|
||||
},
|
||||
{
|
||||
parameters: "...rest",
|
||||
arguments: "1, 2, 3",
|
||||
expected: [
|
||||
[0, "rest"],
|
||||
[1, "rest"],
|
||||
[2, "rest"],
|
||||
],
|
||||
},
|
||||
{
|
||||
parameters: "first, second",
|
||||
arguments: "1, 2, 3",
|
||||
expected: [
|
||||
[0, "first"],
|
||||
[1, "second"],
|
||||
],
|
||||
},
|
||||
{
|
||||
parameters: "first, ...rest",
|
||||
arguments: "1, ...values, 3",
|
||||
expected: [[0, "first"]],
|
||||
},
|
||||
])(
|
||||
"links exact positions for ($parameters) called with ($arguments)",
|
||||
({ parameters, arguments: arguments_, expected }) => {
|
||||
const ir = analyzeJavaScriptSemantics(
|
||||
`function collect(${parameters}) { return 1; } collect(${arguments_});`,
|
||||
);
|
||||
expect(
|
||||
ir.argumentFlows.map(({ argumentIndex, parameterBindingId }) => [
|
||||
argumentIndex,
|
||||
semanticBinding(ir, parameterBindingId)?.name,
|
||||
]),
|
||||
).toEqual(expected);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
@@ -179,22 +179,29 @@ export const parameterBindings = (
|
||||
binding.definitions.some(
|
||||
({ kind, location }) =>
|
||||
kind === "parameter" &&
|
||||
parameterIndex(callable, location, state) === index,
|
||||
parameterAcceptsArgument(callable, location, index, state),
|
||||
),
|
||||
)
|
||||
.sort((left, right) => compareCodePoints(left.bindingId, right.bindingId));
|
||||
};
|
||||
|
||||
const parameterIndex = (
|
||||
const parameterAcceptsArgument = (
|
||||
callable: JavaScriptSemanticCallable,
|
||||
location: JavaScriptSemanticArgumentFlow["parameterLocation"],
|
||||
argumentIndex: number,
|
||||
state: JavaScriptSemanticAnalysisState,
|
||||
): number => {
|
||||
): boolean => {
|
||||
const node = state.callableNodesById.get(callable.callableId);
|
||||
if (!t.isFunction(node)) return -1;
|
||||
return node.params.findIndex((parameter) =>
|
||||
if (!t.isFunction(node)) return false;
|
||||
const parameterIndex = node.params.findIndex((parameter) =>
|
||||
rangeContains(range(parameter), location),
|
||||
);
|
||||
if (parameterIndex === argumentIndex) return true;
|
||||
return (
|
||||
parameterIndex >= 0 &&
|
||||
parameterIndex < argumentIndex &&
|
||||
t.isRestElement(node.params[parameterIndex])
|
||||
);
|
||||
};
|
||||
|
||||
const rangeContains = (
|
||||
|
||||
Reference in New Issue
Block a user