|
1 | 1 | import { expect, it } from "vitest"; |
2 | 2 | import { graphForJavaScript } from "../../../tests/fixtures/javascriptSemanticGraph.js"; |
3 | 3 |
|
| 4 | +it.each([ |
| 5 | + "const { mode: selected, fallback = 0 } = config;", |
| 6 | + 'const { mode: selected, ["fallback"]: later } = config;', |
| 7 | + "const { mode: selected, nested: { fallback = 0 } } = config;", |
| 8 | + "const { mode: selected, ...later } = config;", |
| 9 | + "const { mode: selected, accessor: later } = config;", |
| 10 | + "let selected; const target = {}; ({ mode: selected, fallback: target.value } = config);", |
| 11 | +])( |
| 12 | + "retains a proved read before a later unproven pattern operation", |
| 13 | + (read) => { |
| 14 | + const graph = graphForJavaScript(` |
| 15 | + const config = { mode: "strict", fallback: 0, nested: { fallback: 0 }, get accessor() { return 0; } }; |
| 16 | + ${read} |
| 17 | + config.mode = "loose"; |
| 18 | + `); |
| 19 | + const selected = graph.nodes.find( |
| 20 | + ({ kind, label }) => kind === "binding" && label === "selected", |
| 21 | + ); |
| 22 | + expect( |
| 23 | + graph.relations.find( |
| 24 | + (edge) => |
| 25 | + edge.relation === "destructures" && |
| 26 | + edge.target_node_id === selected?.node_id, |
| 27 | + ), |
| 28 | + ).toMatchObject({ resolution: "resolved" }); |
| 29 | + }, |
| 30 | +); |
| 31 | + |
| 32 | +it("leaves the next read uncertain after a source-mutating assignment target", () => { |
| 33 | + const graph = graphForJavaScript(` |
| 34 | + const config = { first: 1, mode: "strict" }; |
| 35 | + let selected; |
| 36 | + const target = { set value(_) { delete config.mode; } }; |
| 37 | + ({ first: target.value, mode: selected } = config); |
| 38 | + config.mode = "loose"; |
| 39 | + `); |
| 40 | + expect( |
| 41 | + graph.relations.find(({ relation }) => relation === "destructures"), |
| 42 | + ).toMatchObject({ resolution: "candidate" }); |
| 43 | +}); |
| 44 | + |
| 45 | +it.each([ |
| 46 | + { read: "const { mode: selected } = config;", relation: "destructures" }, |
| 47 | + { |
| 48 | + read: "let selected; ({ mode: selected } = config);", |
| 49 | + relation: "destructures", |
| 50 | + }, |
| 51 | + { read: "const selected = config.mode;", relation: "reads-property" }, |
| 52 | +] as const)( |
| 53 | + "keeps earlier $relation at its read position", |
| 54 | + ({ read, relation }) => { |
| 55 | + const source = 'const config = { mode: "strict", other: 0 };'; |
| 56 | + for (const [before, after, resolution] of [ |
| 57 | + ["", 'config.mode = "loose";', "resolved"], |
| 58 | + ["", "config.other = 1;", "resolved"], |
| 59 | + ["delete config.mode;", "", "candidate"], |
| 60 | + ["mutate(config);", "", "candidate"], |
| 61 | + ]) { |
| 62 | + const graph = graphForJavaScript( |
| 63 | + [source, before, read, after].join("\n"), |
| 64 | + ); |
| 65 | + expect( |
| 66 | + graph.relations.find((edge) => edge.relation === relation), |
| 67 | + ).toMatchObject({ resolution }); |
| 68 | + } |
| 69 | + }, |
| 70 | +); |
| 71 | + |
| 72 | +it("keeps a direct nested data-slot dependency after a later write", () => { |
| 73 | + const graph = graphForJavaScript(` |
| 74 | + const config = { nested: { mode: "strict" } }; |
| 75 | + const { mode: selected } = config.nested; |
| 76 | + config.nested.mode = "loose"; |
| 77 | + `); |
| 78 | + expect( |
| 79 | + graph.relations.find(({ relation }) => relation === "destructures"), |
| 80 | + ).toMatchObject({ resolution: "resolved" }); |
| 81 | +}); |
| 82 | + |
| 83 | +it.each([ |
| 84 | + `const config = { mode: "strict" }; const { first = (delete config.mode), mode: selected } = config;`, |
| 85 | + `const config = { get first() { delete this.mode; return {}; }, mode: "strict" }; |
| 86 | + const { first: {}, mode: selected } = config;`, |
| 87 | +])( |
| 88 | + "leaves a dependency uncertain after an effectful earlier pattern lookup", |
| 89 | + (source) => { |
| 90 | + const graph = graphForJavaScript(source + 'config.mode = "loose";'); |
| 91 | + const selected = graph.nodes.find( |
| 92 | + ({ kind, label }) => kind === "binding" && label === "selected", |
| 93 | + ); |
| 94 | + expect( |
| 95 | + graph.relations.find( |
| 96 | + (edge) => |
| 97 | + edge.relation === "destructures" && |
| 98 | + edge.target_node_id === selected?.node_id, |
| 99 | + ), |
| 100 | + ).toMatchObject({ resolution: "candidate" }); |
| 101 | + }, |
| 102 | +); |
| 103 | + |
4 | 104 | it("links declared and assigned destructuring bindings to their exact source slots", () => { |
5 | 105 | const cases = [ |
6 | 106 | { pattern: "{ a: selected }", source: "{ a: 1 }", path: ["a"] }, |
|
0 commit comments