Skip to content

Commit

Permalink
Use safe access in access step (#448)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjie authored Aug 4, 2023
2 parents afb88f2 + 3af4310 commit cb859f8
Show file tree
Hide file tree
Showing 6 changed files with 243 additions and 180 deletions.
5 changes: 5 additions & 0 deletions .changeset/hip-scissors-learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"grafast": patch
---

Fix access step to use safe access pattern even when compiled
4 changes: 2 additions & 2 deletions grafast/grafast/src/steps/access.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function constructDestructureFunction(
): void {
const n = path.length;
/** 0 - slow mode; 1 - middle mode; 2 - turbo mode */
let mode: 0 | 1 | 2 = n > 50 ? 0 : n > 5 ? 1 : 2;
let mode: 0 | 1 | 2 = n > 50 || n < 1 ? 0 : n > 5 ? 1 : 2;

for (let i = 0; i < n; i++) {
const pathItem = path[i];
Expand Down Expand Up @@ -110,7 +110,7 @@ function constructDestructureFunction(
}
te.runInBatch<Factory>(
te`function (fallback, ${te.join(names, ", ")}) {
return (_meta, value) => value${te.join(access, "")}${
return (_meta, value) => value?.${te.join(access, "?.")}${
fallback === undefined ? te.blank : te.cache` ?? fallback`
};
}`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
nodeId: "WyJwZW9wbGUiLDFd",
personSecretByPersonId: null,
},
nxPerson: null,
leftArmById: {
nodeId: "WyJsZWZ0X2FybXMiLDQyXQ==",
id: 42,
Expand Down
Loading

0 comments on commit cb859f8

Please sign in to comment.