Skip to content
This repository has been archived by the owner on Sep 4, 2023. It is now read-only.

Commit

Permalink
fix(core): unbox keep unresolvable/conditional with symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
astahmer committed Mar 3, 2023
1 parent 82597fb commit 3da520c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/modern-lobsters-lie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@box-extractor/core": patch
---

fix(core): unbox keep unresolvable/conditional with symbols
19 changes: 13 additions & 6 deletions packages/box-extractor/src/extractor/unbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ import type { BoxNode, LiteralValue } from "./type-factory";
import { visitBoxNode } from "./visitBoxNode";
import { cacheMap } from "./getBoxLiteralValue";

const unresolvable = Symbol.for("unresolvable");
const conditional = Symbol.for("conditional");

export const isUnresolvable = (value: unknown): value is typeof unresolvable => value === unresolvable;
export const isConditional = (value: unknown): value is typeof conditional => value === conditional;

export const unbox = (rootNode: BoxNode | undefined, localCacheMap: WeakMap<BoxNode, unknown> = cacheMap) => {
if (!rootNode) return;
if (rootNode.isObject() || rootNode.isLiteral()) return rootNode.value;
Expand All @@ -14,11 +20,6 @@ export const unbox = (rootNode: BoxNode | undefined, localCacheMap: WeakMap<BoxN

// TODO return infos like: has circular ? has unresolvable ? has conditional ? has empty initializer ?
visitBoxNode(rootNode, (node, key, parentNode, traversal) => {
if (node.isUnresolvable() || node.isConditional() || node.isEmptyInitializer()) {
traversal.skip();
return;
}

if (localCacheMap.has(node)) {
if (parentNode) {
const parentPath = pathByNode.get(parentNode) ?? [];
Expand All @@ -32,7 +33,7 @@ export const unbox = (rootNode: BoxNode | undefined, localCacheMap: WeakMap<BoxN
return;
}

let current: LiteralValue;
let current: LiteralValue | typeof unresolvable | typeof conditional;
if (node.isObject()) {
current = node.value;
traversal.skip();
Expand All @@ -42,6 +43,12 @@ export const unbox = (rootNode: BoxNode | undefined, localCacheMap: WeakMap<BoxN
current = [];
} else if (node.isLiteral()) {
current = node.value;
} else if (node.isUnresolvable()) {
current = unresolvable;
} else if (node.isConditional()) {
current = conditional;
} else if (node.isEmptyInitializer()) {
current = true;
}

if (parentNode && parentNode !== prevParent) {
Expand Down

0 comments on commit 3da520c

Please sign in to comment.