MCPcopy
hub / github.com/webpack/webpack / createLazyProxy

Function createLazyProxy

examples/custom-javascript-parser/internals/oxc-parse.js:110–176  ·  view source on GitHub ↗
(node, locationResolver, cache)

Source from the content-addressed store, hash-verified

108 * @returns {NodeWithRangeAndLocation<T>} proxy node
109 */
110const createLazyProxy = (node, locationResolver, cache) => {
111 if (cache.has(node)) {
112 return /** @type {NodeWithRangeAndLocation<T>} */ (cache.get(node));
113 }
114
115 const proxy =
116 /** @type {NodeWithRangeAndLocation<T>} */
117 (
118 new Proxy(node, {
119 has(target, prop) {
120 return prop === "loc" || prop in target;
121 },
122 get(target, prop, receiver) {
123 if (prop === "loc") {
124 const loc = locationResolver.getLocation(target.start, target.end);
125 return {
126 start: {
127 line: loc[0],
128 offset: loc[1]
129 },
130 end: {
131 line: loc[2],
132 offset: loc[3]
133 }
134 };
135 }
136
137 const value = Reflect.get(target, prop, receiver);
138
139 if (Array.isArray(value)) {
140 return value.map((node) => {
141 if (
142 !node ||
143 typeof node !== "object" ||
144 Array.isArray(node) ||
145 !node.type
146 ) {
147 return node;
148 }
149
150 return createLazyProxy(node, locationResolver, cache);
151 });
152 } else if (
153 value &&
154 typeof value === "object" &&
155 /** @type {T} */ (value).type
156 ) {
157 return createLazyProxy(
158 /** @type {NodeWithRange<T>} */
159 (
160 /** @type {unknown} */
161 (value)
162 ),
163 locationResolver,
164 cache
165 );
166 }
167

Callers 2

getFunction · 0.85
oxcParseFunction · 0.85

Calls 3

hasMethod · 0.45
getMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected