react-compiler-runtime
Advanced tools
Comparing version 0.0.0-experimental-8d8e73f-20241011 to 0.0.0-experimental-a7bf2bd-20241110
@@ -14,3 +14,312 @@ /** | ||
"use no memo";"use strict";function e(e){var t=Object.create(null);return e&&Object.keys(e).forEach((function(n){if("default"!==n){var o=Object.getOwnPropertyDescriptor(e,n);Object.defineProperty(t,n,o.get?o:{enumerable:!0,get:function(){return e[n]}})}})),t.default=e,Object.freeze(t)}var t,n,o=e(require("react"));const{useRef:r,useEffect:c,isValidElement:a}=o,i=null!==(t=o.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE)&&void 0!==t?t:o.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED,s=Symbol.for("react.memo_cache_sentinel"),u="function"==typeof(null===(n=o.__COMPILER_RUNTIME)||void 0===n?void 0:n.c)?o.__COMPILER_RUNTIME.c:function(e){return o.useMemo((()=>{const t=new Array(e);for(let n=0;n<e;n++)t[n]=s;return t[s]=!0,t}),[])},l={};["readContext","useCallback","useContext","useEffect","useImperativeHandle","useInsertionEffect","useLayoutEffect","useMemo","useReducer","useRef","useState","useDebugValue","useDeferredValue","useTransition","useMutableSource","useSyncExternalStore","useId","unstable_isNewReconciler","getCacheSignal","getCacheForType","useCacheRefresh"].forEach((e=>{l[e]=()=>{throw new Error(`[React] Unexpected React hook call (${e}) from a React Forget compiled function. Check that all hooks are called directly and named according to convention ('use[A-Z]') `)}}));let f=null;var p;function h(e){return i.ReactCurrentDispatcher.current=e,i.ReactCurrentDispatcher.current}l.useMemoCache=e=>{if(null==f)throw new Error("React Forget internal invariant violation: unexpected null dispatcher");return f.useMemoCache(e)},function(e){e[e.PushGuardContext=0]="PushGuardContext",e[e.PopGuardContext=1]="PopGuardContext",e[e.PushExpectHook=2]="PushExpectHook",e[e.PopExpectHook=3]="PopExpectHook"}(p||(p={}));const d=[];const y=new Map;const R=new Set;exports.$dispatcherGuard=function(e){const t=i.ReactCurrentDispatcher.current;if(e===p.PushGuardContext){if(d.push(t),1===d.length&&(f=t),t===l)throw new Error("[React] Unexpected call to custom hook or component from a React Forget compiled function. Check that (1) all hooks are called directly and named according to convention ('use[A-Z]') and (2) components are returned as JSX instead of being directly invoked.");h(l)}else if(e===p.PopGuardContext){const e=d.pop();if(null==e)throw new Error("React Forget internal error: unexpected null in guard stack");0===d.length&&(f=null),h(e)}else if(e===p.PushExpectHook)d.push(t),h(f);else{if(e!==p.PopExpectHook)throw new Error("Forget internal error: unreachable block"+e);{const e=d.pop();if(null==e)throw new Error("React Forget internal error: unexpected null in guard stack");h(e)}}},exports.$makeReadOnly=function(){throw new Error("TODO: implement $makeReadOnly in react-compiler-runtime")},exports.$reset=function(e){for(let t=0;t<e.length;t++)e[t]=s},exports.$structuralCheck=function(e,t,n,o,r,c){function i(e,t,a,i){const s=`${o}:${c} [${r}] ${n}${a} changed from ${e} to ${t} at depth ${i}`;R.has(s)||(R.add(s),console.error(s))}!function e(t,n,o,r){if(!(r>2)&&t!==n)if(typeof t!=typeof n)i("type "+typeof t,"type "+typeof n,o,r);else if("object"==typeof t){const c=Array.isArray(t),s=Array.isArray(n);if(null===t&&null!==n)i("null","type "+typeof n,o,r);else if(null===n)i("type "+typeof t,"null",o,r);else if(t instanceof Map)if(n instanceof Map)if(t.size!==n.size)i(`Map instance with size ${t.size}`,`Map instance with size ${n.size}`,o,r);else for(const[c,a]of t)n.has(c)?e(a,n.get(c),`${o}.get(${c})`,r+1):i(`Map instance with key ${c}`,`Map instance without key ${c}`,o,r);else i("Map instance","other value",o,r);else if(n instanceof Map)i("other value","Map instance",o,r);else if(t instanceof Set)if(n instanceof Set)if(t.size!==n.size)i(`Set instance with size ${t.size}`,`Set instance with size ${n.size}`,o,r);else for(const e of n)t.has(e)||i(`Set instance without element ${e}`,`Set instance with element ${e}`,o,r);else i("Set instance","other value",o,r);else if(n instanceof Set)i("other value","Set instance",o,r);else if(c||s)if(c!==s)i("type "+(c?"array":"object"),"type "+(s?"array":"object"),o,r);else if(t.length!==n.length)i(`array with length ${t.length}`,`array with length ${n.length}`,o,r);else for(let c=0;c<t.length;c++)e(t[c],n[c],`${o}[${c}]`,r+1);else if(a(t)||a(n))a(t)!==a(n)?i("type "+(a(t)?"React element":"object"),"type "+(a(n)?"React element":"object"),o,r):t.type!==n.type?i(`React element of type ${t.type}`,`React element of type ${n.type}`,o,r):e(t.props,n.props,`[props of ${o}]`,r+1);else{for(const e in n)e in t||i(`object without key ${e}`,`object with key ${e}`,o,r);for(const c in t)c in n?e(t[c],n[c],`${o}.${c}`,r+1):i(`object with key ${c}`,`object without key ${c}`,o,r)}}else{if("function"==typeof t)return;isNaN(t)||isNaN(n)?isNaN(t)!==isNaN(n)&&i(""+(isNaN(t)?"NaN":"non-NaN value"),""+(isNaN(n)?"NaN":"non-NaN value"),o,r):t!==n&&i(t,n,o,r)}}(e,t,"",0)},exports.c=u,exports.clearRenderCounterRegistry=function(){for(const e of y.values())e.forEach((e=>{e.count=0}))},exports.renderCounterRegistry=y,exports.useRenderCounter=function(e){const t=r(null);null!=t.current&&(t.current.count+=1),c((()=>{if(null==t.current){const n={count:0};!function(e,t){let n=y.get(e);null==n&&(n=new Set,y.set(e,n)),n.add(t)}(e,n),t.current=n}return()=>{null!==t.current&&function(e,t){const n=y.get(e);null!=n&&n.delete(t)}(e,t.current)}}))}; | ||
"use no memo";'use strict'; | ||
var React = require('react'); | ||
function _interopNamespaceDefault(e) { | ||
var n = Object.create(null); | ||
if (e) { | ||
Object.keys(e).forEach(function (k) { | ||
if (k !== 'default') { | ||
var d = Object.getOwnPropertyDescriptor(e, k); | ||
Object.defineProperty(n, k, d.get ? d : { | ||
enumerable: true, | ||
get: function () { return e[k]; } | ||
}); | ||
} | ||
}); | ||
} | ||
n.default = e; | ||
return Object.freeze(n); | ||
} | ||
var React__namespace = /*#__PURE__*/_interopNamespaceDefault(React); | ||
var _a, _b; | ||
const { useRef, useEffect, isValidElement } = React__namespace; | ||
const ReactSecretInternals = (_a = React__namespace.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE) !== null && _a !== void 0 ? _a : React__namespace.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED; | ||
const $empty = Symbol.for('react.memo_cache_sentinel'); | ||
const c = typeof ((_b = React__namespace.__COMPILER_RUNTIME) === null || _b === void 0 ? void 0 : _b.c) === 'function' | ||
? | ||
React__namespace.__COMPILER_RUNTIME.c | ||
: function c(size) { | ||
return React__namespace.useMemo(() => { | ||
const $ = new Array(size); | ||
for (let ii = 0; ii < size; ii++) { | ||
$[ii] = $empty; | ||
} | ||
$[$empty] = true; | ||
return $; | ||
}, []); | ||
}; | ||
const LazyGuardDispatcher = {}; | ||
[ | ||
'readContext', | ||
'useCallback', | ||
'useContext', | ||
'useEffect', | ||
'useImperativeHandle', | ||
'useInsertionEffect', | ||
'useLayoutEffect', | ||
'useMemo', | ||
'useReducer', | ||
'useRef', | ||
'useState', | ||
'useDebugValue', | ||
'useDeferredValue', | ||
'useTransition', | ||
'useMutableSource', | ||
'useSyncExternalStore', | ||
'useId', | ||
'unstable_isNewReconciler', | ||
'getCacheSignal', | ||
'getCacheForType', | ||
'useCacheRefresh', | ||
].forEach(name => { | ||
LazyGuardDispatcher[name] = () => { | ||
throw new Error(`[React] Unexpected React hook call (${name}) from a React compiled function. ` + | ||
"Check that all hooks are called directly and named according to convention ('use[A-Z]') "); | ||
}; | ||
}); | ||
let originalDispatcher = null; | ||
LazyGuardDispatcher['useMemoCache'] = (count) => { | ||
if (originalDispatcher == null) { | ||
throw new Error('React Compiler internal invariant violation: unexpected null dispatcher'); | ||
} | ||
else { | ||
return originalDispatcher.useMemoCache(count); | ||
} | ||
}; | ||
var GuardKind; | ||
(function (GuardKind) { | ||
GuardKind[GuardKind["PushGuardContext"] = 0] = "PushGuardContext"; | ||
GuardKind[GuardKind["PopGuardContext"] = 1] = "PopGuardContext"; | ||
GuardKind[GuardKind["PushExpectHook"] = 2] = "PushExpectHook"; | ||
GuardKind[GuardKind["PopExpectHook"] = 3] = "PopExpectHook"; | ||
})(GuardKind || (GuardKind = {})); | ||
function setCurrent(newDispatcher) { | ||
ReactSecretInternals.ReactCurrentDispatcher.current = newDispatcher; | ||
return ReactSecretInternals.ReactCurrentDispatcher.current; | ||
} | ||
const guardFrames = []; | ||
function $dispatcherGuard(kind) { | ||
const curr = ReactSecretInternals.ReactCurrentDispatcher.current; | ||
if (kind === GuardKind.PushGuardContext) { | ||
guardFrames.push(curr); | ||
if (guardFrames.length === 1) { | ||
originalDispatcher = curr; | ||
} | ||
if (curr === LazyGuardDispatcher) { | ||
throw new Error(`[React] Unexpected call to custom hook or component from a React compiled function. ` + | ||
"Check that (1) all hooks are called directly and named according to convention ('use[A-Z]') " + | ||
'and (2) components are returned as JSX instead of being directly invoked.'); | ||
} | ||
setCurrent(LazyGuardDispatcher); | ||
} | ||
else if (kind === GuardKind.PopGuardContext) { | ||
const lastFrame = guardFrames.pop(); | ||
if (lastFrame == null) { | ||
throw new Error('React Compiler internal error: unexpected null in guard stack'); | ||
} | ||
if (guardFrames.length === 0) { | ||
originalDispatcher = null; | ||
} | ||
setCurrent(lastFrame); | ||
} | ||
else if (kind === GuardKind.PushExpectHook) { | ||
guardFrames.push(curr); | ||
setCurrent(originalDispatcher); | ||
} | ||
else if (kind === GuardKind.PopExpectHook) { | ||
const lastFrame = guardFrames.pop(); | ||
if (lastFrame == null) { | ||
throw new Error('React Compiler internal error: unexpected null in guard stack'); | ||
} | ||
setCurrent(lastFrame); | ||
} | ||
else { | ||
throw new Error('React Compiler internal error: unreachable block' + kind); | ||
} | ||
} | ||
function $reset($) { | ||
for (let ii = 0; ii < $.length; ii++) { | ||
$[ii] = $empty; | ||
} | ||
} | ||
function $makeReadOnly() { | ||
throw new Error('TODO: implement $makeReadOnly in react-compiler-runtime'); | ||
} | ||
const renderCounterRegistry = new Map(); | ||
function clearRenderCounterRegistry() { | ||
for (const counters of renderCounterRegistry.values()) { | ||
counters.forEach(counter => { | ||
counter.count = 0; | ||
}); | ||
} | ||
} | ||
function registerRenderCounter(name, val) { | ||
let counters = renderCounterRegistry.get(name); | ||
if (counters == null) { | ||
counters = new Set(); | ||
renderCounterRegistry.set(name, counters); | ||
} | ||
counters.add(val); | ||
} | ||
function removeRenderCounter(name, val) { | ||
const counters = renderCounterRegistry.get(name); | ||
if (counters == null) { | ||
return; | ||
} | ||
counters.delete(val); | ||
} | ||
function useRenderCounter(name) { | ||
const val = useRef(null); | ||
if (val.current != null) { | ||
val.current.count += 1; | ||
} | ||
useEffect(() => { | ||
if (val.current == null) { | ||
const counter = { count: 0 }; | ||
registerRenderCounter(name, counter); | ||
val.current = counter; | ||
} | ||
return () => { | ||
if (val.current !== null) { | ||
removeRenderCounter(name, val.current); | ||
} | ||
}; | ||
}); | ||
} | ||
const seenErrors = new Set(); | ||
function $structuralCheck(oldValue, newValue, variableName, fnName, kind, loc) { | ||
function error(l, r, path, depth) { | ||
const str = `${fnName}:${loc} [${kind}] ${variableName}${path} changed from ${l} to ${r} at depth ${depth}`; | ||
if (seenErrors.has(str)) { | ||
return; | ||
} | ||
seenErrors.add(str); | ||
console.error(str); | ||
} | ||
const depthLimit = 2; | ||
function recur(oldValue, newValue, path, depth) { | ||
if (depth > depthLimit) { | ||
return; | ||
} | ||
else if (oldValue === newValue) { | ||
return; | ||
} | ||
else if (typeof oldValue !== typeof newValue) { | ||
error(`type ${typeof oldValue}`, `type ${typeof newValue}`, path, depth); | ||
} | ||
else if (typeof oldValue === 'object') { | ||
const oldArray = Array.isArray(oldValue); | ||
const newArray = Array.isArray(newValue); | ||
if (oldValue === null && newValue !== null) { | ||
error('null', `type ${typeof newValue}`, path, depth); | ||
} | ||
else if (newValue === null) { | ||
error(`type ${typeof oldValue}`, 'null', path, depth); | ||
} | ||
else if (oldValue instanceof Map) { | ||
if (!(newValue instanceof Map)) { | ||
error(`Map instance`, `other value`, path, depth); | ||
} | ||
else if (oldValue.size !== newValue.size) { | ||
error(`Map instance with size ${oldValue.size}`, `Map instance with size ${newValue.size}`, path, depth); | ||
} | ||
else { | ||
for (const [k, v] of oldValue) { | ||
if (!newValue.has(k)) { | ||
error(`Map instance with key ${k}`, `Map instance without key ${k}`, path, depth); | ||
} | ||
else { | ||
recur(v, newValue.get(k), `${path}.get(${k})`, depth + 1); | ||
} | ||
} | ||
} | ||
} | ||
else if (newValue instanceof Map) { | ||
error('other value', `Map instance`, path, depth); | ||
} | ||
else if (oldValue instanceof Set) { | ||
if (!(newValue instanceof Set)) { | ||
error(`Set instance`, `other value`, path, depth); | ||
} | ||
else if (oldValue.size !== newValue.size) { | ||
error(`Set instance with size ${oldValue.size}`, `Set instance with size ${newValue.size}`, path, depth); | ||
} | ||
else { | ||
for (const v of newValue) { | ||
if (!oldValue.has(v)) { | ||
error(`Set instance without element ${v}`, `Set instance with element ${v}`, path, depth); | ||
} | ||
} | ||
} | ||
} | ||
else if (newValue instanceof Set) { | ||
error('other value', `Set instance`, path, depth); | ||
} | ||
else if (oldArray || newArray) { | ||
if (oldArray !== newArray) { | ||
error(`type ${oldArray ? 'array' : 'object'}`, `type ${newArray ? 'array' : 'object'}`, path, depth); | ||
} | ||
else if (oldValue.length !== newValue.length) { | ||
error(`array with length ${oldValue.length}`, `array with length ${newValue.length}`, path, depth); | ||
} | ||
else { | ||
for (let ii = 0; ii < oldValue.length; ii++) { | ||
recur(oldValue[ii], newValue[ii], `${path}[${ii}]`, depth + 1); | ||
} | ||
} | ||
} | ||
else if (isValidElement(oldValue) || isValidElement(newValue)) { | ||
if (isValidElement(oldValue) !== isValidElement(newValue)) { | ||
error(`type ${isValidElement(oldValue) ? 'React element' : 'object'}`, `type ${isValidElement(newValue) ? 'React element' : 'object'}`, path, depth); | ||
} | ||
else if (oldValue.type !== newValue.type) { | ||
error(`React element of type ${oldValue.type}`, `React element of type ${newValue.type}`, path, depth); | ||
} | ||
else { | ||
recur(oldValue.props, newValue.props, `[props of ${path}]`, depth + 1); | ||
} | ||
} | ||
else { | ||
for (const key in newValue) { | ||
if (!(key in oldValue)) { | ||
error(`object without key ${key}`, `object with key ${key}`, path, depth); | ||
} | ||
} | ||
for (const key in oldValue) { | ||
if (!(key in newValue)) { | ||
error(`object with key ${key}`, `object without key ${key}`, path, depth); | ||
} | ||
else { | ||
recur(oldValue[key], newValue[key], `${path}.${key}`, depth + 1); | ||
} | ||
} | ||
} | ||
} | ||
else if (typeof oldValue === 'function') { | ||
return; | ||
} | ||
else if (isNaN(oldValue) || isNaN(newValue)) { | ||
if (isNaN(oldValue) !== isNaN(newValue)) { | ||
error(`${isNaN(oldValue) ? 'NaN' : 'non-NaN value'}`, `${isNaN(newValue) ? 'NaN' : 'non-NaN value'}`, path, depth); | ||
} | ||
} | ||
else if (oldValue !== newValue) { | ||
error(oldValue, newValue, path, depth); | ||
} | ||
} | ||
recur(oldValue, newValue, '', 0); | ||
} | ||
exports.$dispatcherGuard = $dispatcherGuard; | ||
exports.$makeReadOnly = $makeReadOnly; | ||
exports.$reset = $reset; | ||
exports.$structuralCheck = $structuralCheck; | ||
exports.c = c; | ||
exports.clearRenderCounterRegistry = clearRenderCounterRegistry; | ||
exports.renderCounterRegistry = renderCounterRegistry; | ||
exports.useRenderCounter = useRenderCounter; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "react-compiler-runtime", | ||
"version": "0.0.0-experimental-8d8e73f-20241011", | ||
"version": "0.0.0-experimental-a7bf2bd-20241110", | ||
"description": "Runtime for React Compiler", | ||
@@ -12,3 +12,3 @@ "license": "MIT", | ||
"peerDependencies": { | ||
"react": "^18.2.0 || ^19.0.0" | ||
"react": "^17.0.0 || ^18.0.0 || ^19.0.0" | ||
}, | ||
@@ -15,0 +15,0 @@ "scripts": { |
@@ -8,3 +8,3 @@ /** | ||
'use no forget'; | ||
'use no memo'; | ||
@@ -71,3 +71,3 @@ import * as React from 'react'; | ||
throw new Error( | ||
`[React] Unexpected React hook call (${name}) from a React Forget compiled function. ` + | ||
`[React] Unexpected React hook call (${name}) from a React compiled function. ` + | ||
"Check that all hooks are called directly and named according to convention ('use[A-Z]') ", | ||
@@ -84,3 +84,3 @@ ); | ||
throw new Error( | ||
'React Forget internal invariant violation: unexpected null dispatcher', | ||
'React Compiler internal invariant violation: unexpected null dispatcher', | ||
); | ||
@@ -109,8 +109,8 @@ } else { | ||
* of the no-conditional-hook-calls rule. | ||
* As Forget needs to statically understand which calls to move out of | ||
* conditional branches (i.e. Forget cannot memoize the results of hook | ||
* As React Compiler needs to statically understand which calls to move out of | ||
* conditional branches (i.e. React Compiler cannot memoize the results of hook | ||
* calls), its understanding of "the rules of React" are more restrictive. | ||
* This validation throws on unsound inputs at runtime. | ||
* | ||
* Components should only be invoked through React as Forget could memoize | ||
* Components should only be invoked through React as React Compiler could memoize | ||
* the call to AnotherComponent, introducing conditional hook calls in its | ||
@@ -155,3 +155,3 @@ * compiled output. | ||
throw new Error( | ||
`[React] Unexpected call to custom hook or component from a React Forget compiled function. ` + | ||
`[React] Unexpected call to custom hook or component from a React compiled function. ` + | ||
"Check that (1) all hooks are called directly and named according to convention ('use[A-Z]') " + | ||
@@ -168,3 +168,3 @@ 'and (2) components are returned as JSX instead of being directly invoked.', | ||
throw new Error( | ||
'React Forget internal error: unexpected null in guard stack', | ||
'React Compiler internal error: unexpected null in guard stack', | ||
); | ||
@@ -185,3 +185,3 @@ } | ||
throw new Error( | ||
'React Forget internal error: unexpected null in guard stack', | ||
'React Compiler internal error: unexpected null in guard stack', | ||
); | ||
@@ -191,3 +191,3 @@ } | ||
} else { | ||
throw new Error('Forget internal error: unreachable block' + kind); | ||
throw new Error('React Compiler internal error: unreachable block' + kind); | ||
} | ||
@@ -194,0 +194,0 @@ } |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
72222
190339
731
3
43
59
21
59