@optique/core 1.0.0-dev.1786 → 1.0.0-dev.1787

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,100 @@
1
+ const require_annotations = require('./annotations.cjs');
2
+ const require_parser = require('./parser.cjs');
3
+
4
+ //#region src/annotation-state.ts
5
+ /**
6
+ * Shared targets for annotation-view proxies.
7
+ *
8
+ * @internal
9
+ */
10
+ const annotationViewTargets = /* @__PURE__ */ new WeakMap();
11
+ /**
12
+ * Unwraps an annotation-view proxy to its original target object.
13
+ *
14
+ * @internal
15
+ */
16
+ function unwrapAnnotationView(value) {
17
+ if (value == null || typeof value !== "object") return value;
18
+ return annotationViewTargets.get(value) ?? value;
19
+ }
20
+ /**
21
+ * Creates a proxy that exposes annotations without changing the target shape.
22
+ *
23
+ * @internal
24
+ */
25
+ function withAnnotationView(state, annotations) {
26
+ const target = unwrapAnnotationView(state);
27
+ const view = new Proxy(target, {
28
+ get(target$1, key) {
29
+ if (key === require_annotations.annotationKey) return annotations;
30
+ const value = Reflect.get(target$1, key, target$1);
31
+ return typeof value === "function" ? value.bind(target$1) : value;
32
+ },
33
+ has(target$1, key) {
34
+ return key === require_annotations.annotationKey || Reflect.has(target$1, key);
35
+ }
36
+ });
37
+ annotationViewTargets.set(view, target);
38
+ return view;
39
+ }
40
+ /**
41
+ * Removes Optique's internal primitive-state annotation wrapper when present.
42
+ *
43
+ * @internal
44
+ */
45
+ function normalizeInjectedAnnotationState(state) {
46
+ return require_annotations.unwrapInjectedAnnotationWrapper(state);
47
+ }
48
+ /**
49
+ * Returns whether a state is still the annotation-wrapped initial sentinel.
50
+ *
51
+ * @internal
52
+ */
53
+ function isAnnotationWrappedInitialState(state) {
54
+ return normalizeInjectedAnnotationState(state) === void 0;
55
+ }
56
+ /**
57
+ * Propagates parent annotations into a child parse state when the child parser
58
+ * explicitly opts into parent annotation inheritance.
59
+ *
60
+ * @internal
61
+ */
62
+ function getWrappedChildParseState(parentState, childState, parser) {
63
+ const annotations = require_annotations.getAnnotations(parentState);
64
+ const shouldInheritAnnotations = Reflect.get(parser, require_parser.inheritParentAnnotationsKey) === true;
65
+ if (childState == null) {
66
+ if (annotations !== void 0 && shouldInheritAnnotations) return require_annotations.injectAnnotations({}, annotations);
67
+ return childState;
68
+ }
69
+ if (annotations === void 0 || typeof childState !== "object" || require_annotations.getAnnotations(childState) === annotations || !shouldInheritAnnotations) return childState;
70
+ const injectedState = require_annotations.injectAnnotations(childState, annotations);
71
+ return require_annotations.getAnnotations(injectedState) === annotations ? injectedState : childState;
72
+ }
73
+ /**
74
+ * Propagates parent annotations into a child state while preserving the child
75
+ * state's shape for parsers that do not opt into full wrapper injection.
76
+ *
77
+ * @internal
78
+ */
79
+ function getWrappedChildState(parentState, childState, parser) {
80
+ const annotations = require_annotations.getAnnotations(parentState);
81
+ const shouldInheritAnnotations = Reflect.get(parser, require_parser.inheritParentAnnotationsKey) === true;
82
+ if (childState == null) {
83
+ if (annotations !== void 0 && shouldInheritAnnotations) return require_annotations.injectAnnotations({}, annotations);
84
+ return childState;
85
+ }
86
+ if (annotations === void 0 || typeof childState !== "object" || require_annotations.getAnnotations(childState) === annotations) return childState;
87
+ if (shouldInheritAnnotations) {
88
+ const injectedState = require_annotations.injectAnnotations(childState, annotations);
89
+ if (require_annotations.getAnnotations(injectedState) === annotations) return injectedState;
90
+ }
91
+ return withAnnotationView(childState, annotations);
92
+ }
93
+
94
+ //#endregion
95
+ exports.annotationViewTargets = annotationViewTargets;
96
+ exports.getWrappedChildParseState = getWrappedChildParseState;
97
+ exports.getWrappedChildState = getWrappedChildState;
98
+ exports.isAnnotationWrappedInitialState = isAnnotationWrappedInitialState;
99
+ exports.normalizeInjectedAnnotationState = normalizeInjectedAnnotationState;
100
+ exports.unwrapAnnotationView = unwrapAnnotationView;
@@ -0,0 +1,95 @@
1
+ import { annotationKey, getAnnotations, injectAnnotations, unwrapInjectedAnnotationWrapper } from "./annotations.js";
2
+ import { inheritParentAnnotationsKey } from "./parser.js";
3
+
4
+ //#region src/annotation-state.ts
5
+ /**
6
+ * Shared targets for annotation-view proxies.
7
+ *
8
+ * @internal
9
+ */
10
+ const annotationViewTargets = /* @__PURE__ */ new WeakMap();
11
+ /**
12
+ * Unwraps an annotation-view proxy to its original target object.
13
+ *
14
+ * @internal
15
+ */
16
+ function unwrapAnnotationView(value) {
17
+ if (value == null || typeof value !== "object") return value;
18
+ return annotationViewTargets.get(value) ?? value;
19
+ }
20
+ /**
21
+ * Creates a proxy that exposes annotations without changing the target shape.
22
+ *
23
+ * @internal
24
+ */
25
+ function withAnnotationView(state, annotations) {
26
+ const target = unwrapAnnotationView(state);
27
+ const view = new Proxy(target, {
28
+ get(target$1, key) {
29
+ if (key === annotationKey) return annotations;
30
+ const value = Reflect.get(target$1, key, target$1);
31
+ return typeof value === "function" ? value.bind(target$1) : value;
32
+ },
33
+ has(target$1, key) {
34
+ return key === annotationKey || Reflect.has(target$1, key);
35
+ }
36
+ });
37
+ annotationViewTargets.set(view, target);
38
+ return view;
39
+ }
40
+ /**
41
+ * Removes Optique's internal primitive-state annotation wrapper when present.
42
+ *
43
+ * @internal
44
+ */
45
+ function normalizeInjectedAnnotationState(state) {
46
+ return unwrapInjectedAnnotationWrapper(state);
47
+ }
48
+ /**
49
+ * Returns whether a state is still the annotation-wrapped initial sentinel.
50
+ *
51
+ * @internal
52
+ */
53
+ function isAnnotationWrappedInitialState(state) {
54
+ return normalizeInjectedAnnotationState(state) === void 0;
55
+ }
56
+ /**
57
+ * Propagates parent annotations into a child parse state when the child parser
58
+ * explicitly opts into parent annotation inheritance.
59
+ *
60
+ * @internal
61
+ */
62
+ function getWrappedChildParseState(parentState, childState, parser) {
63
+ const annotations = getAnnotations(parentState);
64
+ const shouldInheritAnnotations = Reflect.get(parser, inheritParentAnnotationsKey) === true;
65
+ if (childState == null) {
66
+ if (annotations !== void 0 && shouldInheritAnnotations) return injectAnnotations({}, annotations);
67
+ return childState;
68
+ }
69
+ if (annotations === void 0 || typeof childState !== "object" || getAnnotations(childState) === annotations || !shouldInheritAnnotations) return childState;
70
+ const injectedState = injectAnnotations(childState, annotations);
71
+ return getAnnotations(injectedState) === annotations ? injectedState : childState;
72
+ }
73
+ /**
74
+ * Propagates parent annotations into a child state while preserving the child
75
+ * state's shape for parsers that do not opt into full wrapper injection.
76
+ *
77
+ * @internal
78
+ */
79
+ function getWrappedChildState(parentState, childState, parser) {
80
+ const annotations = getAnnotations(parentState);
81
+ const shouldInheritAnnotations = Reflect.get(parser, inheritParentAnnotationsKey) === true;
82
+ if (childState == null) {
83
+ if (annotations !== void 0 && shouldInheritAnnotations) return injectAnnotations({}, annotations);
84
+ return childState;
85
+ }
86
+ if (annotations === void 0 || typeof childState !== "object" || getAnnotations(childState) === annotations) return childState;
87
+ if (shouldInheritAnnotations) {
88
+ const injectedState = injectAnnotations(childState, annotations);
89
+ if (getAnnotations(injectedState) === annotations) return injectedState;
90
+ }
91
+ return withAnnotationView(childState, annotations);
92
+ }
93
+
94
+ //#endregion
95
+ export { annotationViewTargets, getWrappedChildParseState, getWrappedChildState, isAnnotationWrappedInitialState, normalizeInjectedAnnotationState, unwrapAnnotationView };