chore: sync current main into the CI baseline repair
Validate the baseline repairs against the latest JavaScript semantic fix. Co-authored-by: Kaoru0822 <208281322+kaoru0822-kitauji@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { analyzeJavaScriptSemantics } from "./javascriptSemanticAnalysis.js";
|
||||
|
||||
const observe = (source: string) => {
|
||||
const ir = analyzeJavaScriptSemantics(source);
|
||||
return {
|
||||
binding: ir.bindings.find((item) => item.name === "x")?.value,
|
||||
returned: ir.callables.find((item) => item.name === "read")?.returnSites[0]
|
||||
?.value,
|
||||
};
|
||||
};
|
||||
|
||||
describe("JavaScript semantic analysis: logical assignment", () => {
|
||||
it.each(["||=", "??=", "&&="])(
|
||||
"keeps a parameter uncertain after %s",
|
||||
(operator) => {
|
||||
const { binding, returned } = observe(
|
||||
`function read(x) { x ${operator} 2; return x; }`,
|
||||
);
|
||||
expect(binding?.status).toBe("unknown");
|
||||
expect(returned?.status).toBe("unknown");
|
||||
},
|
||||
);
|
||||
|
||||
it.each(["||=", "??=", "&&="])(
|
||||
"keeps a destructured parameter uncertain after %s",
|
||||
(operator) => {
|
||||
const { binding, returned } = observe(
|
||||
`function read({ x }) { x ${operator} 2; return x; }`,
|
||||
);
|
||||
expect(binding?.status).toBe("unknown");
|
||||
expect(returned?.status).toBe("unknown");
|
||||
},
|
||||
);
|
||||
|
||||
it.each(["||=", "??=", "&&="])(
|
||||
"keeps a catch binding uncertain after %s",
|
||||
(operator) => {
|
||||
const { binding, returned } = observe(
|
||||
`function read() { try { run(); } catch (x) { x ${operator} 2; return x; } }`,
|
||||
);
|
||||
expect(binding?.status).toBe("unknown");
|
||||
expect(returned?.status).toBe("unknown");
|
||||
},
|
||||
);
|
||||
|
||||
it.each([
|
||||
["let", "||="],
|
||||
["let", "??="],
|
||||
["var", "||="],
|
||||
["var", "??="],
|
||||
])(
|
||||
"keeps an uninitialized %s local exact after %s",
|
||||
(declaration, operator) => {
|
||||
const { binding, returned } = observe(
|
||||
`function read() { ${declaration} x; x ${operator} 2; return x; }`,
|
||||
);
|
||||
expect(binding).toEqual({ status: "literal", value: 2 });
|
||||
expect(returned).toEqual({ status: "literal", value: 2 });
|
||||
},
|
||||
);
|
||||
|
||||
it("keeps an unconditional parameter assignment exact", () => {
|
||||
const { binding, returned } = observe(
|
||||
"function read(x) { x = 2; return x; }",
|
||||
);
|
||||
expect(binding).toEqual({ status: "literal", value: 2 });
|
||||
expect(returned).toEqual({ status: "literal", value: 2 });
|
||||
});
|
||||
|
||||
it("keeps an unchanged parameter unknown", () => {
|
||||
expect(observe("function read(x) { return x; }").returned?.status).toBe(
|
||||
"unknown",
|
||||
);
|
||||
});
|
||||
|
||||
it("reproduces the issue #2646 inputs without an exact literal", () => {
|
||||
for (const operator of ["||=", "??="]) {
|
||||
const { binding, returned } = observe(
|
||||
`function read(x) { x ${operator} 2; return x; }`,
|
||||
);
|
||||
expect(binding).not.toEqual({ status: "literal", value: 2 });
|
||||
expect(returned).not.toEqual({ status: "literal", value: 2 });
|
||||
}
|
||||
const readOr = (x: number) => {
|
||||
x ||= 2;
|
||||
return x;
|
||||
};
|
||||
const readNullish = (x: number) => {
|
||||
x ??= 2;
|
||||
return x;
|
||||
};
|
||||
expect(readOr(5)).toBe(5);
|
||||
expect(readNullish(5)).toBe(5);
|
||||
});
|
||||
});
|
||||
@@ -919,6 +919,21 @@ const bindImportEquals = (
|
||||
});
|
||||
};
|
||||
|
||||
// A parameter or catch binding starts with a caller-supplied value that a
|
||||
// conditional logical assignment can retain, so its RHS is not exact.
|
||||
const hasIncomingValue = (
|
||||
identifier: t.Identifier,
|
||||
scope: JavaScriptSemanticScopeState,
|
||||
state: JavaScriptSemanticAnalysisState,
|
||||
): boolean => {
|
||||
const kind = resolveSemanticBindingFromScope(
|
||||
scope,
|
||||
identifier.name,
|
||||
state,
|
||||
)?.kind;
|
||||
return kind === "parameter" || kind === "catch";
|
||||
};
|
||||
|
||||
const bindAssignment = (
|
||||
node: t.Node,
|
||||
scope: JavaScriptSemanticScopeState,
|
||||
@@ -929,8 +944,8 @@ const bindAssignment = (
|
||||
addAssignment(
|
||||
node.left,
|
||||
node.operator === "=" ||
|
||||
node.operator === "||=" ||
|
||||
node.operator === "??="
|
||||
((node.operator === "||=" || node.operator === "??=") &&
|
||||
!hasIncomingValue(node.left, scope, state))
|
||||
? node.right
|
||||
: node,
|
||||
scope,
|
||||
|
||||
Reference in New Issue
Block a user