@lynx-js/react 0.129.0 → 0.129.2
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.
- package/CHANGELOG.md +24 -0
- package/components/tsconfig.tsbuildinfo +1 -1
- package/package.json +3 -4
- package/refresh/.turbo/turbo-build.log +1 -1
- package/runtime/__test__/core/globalProps.test.ts +27 -6
- package/runtime/__test__/core/hooks/useLynxGlobalEventListener.test.jsx +1 -1
- package/runtime/__test__/element-template/fixtures/gesture/direct/index.tsx +33 -0
- package/runtime/__test__/element-template/fixtures/spread/mts/index.tsx +197 -0
- package/runtime/__test__/element-template/imports/no-legacy-imports.test.ts +7 -6
- package/runtime/__test__/element-template/internal/legacy-internal-guardrail.test.ts +1 -2
- package/runtime/__test__/element-template/native/callDestroyLifetimeFun.test.ts +89 -3
- package/runtime/__test__/element-template/native/main-thread-api.test.ts +5 -2
- package/runtime/__test__/element-template/native/mts-destroy.test.ts +88 -0
- package/runtime/__test__/element-template/native/reload-dual-thread-communication.test.ts +3 -0
- package/runtime/__test__/element-template/native/reload.test.ts +118 -2
- package/runtime/__test__/element-template/runtime/background/global-props/compiled-fixtures.test.tsx +5 -5
- package/runtime/__test__/element-template/runtime/background/global-props.test.tsx +1 -1
- package/runtime/__test__/element-template/runtime/background/hydrate.test.ts +70 -0
- package/runtime/__test__/element-template/runtime/background/ref/compiled-fixtures.test.tsx +3 -4
- package/runtime/__test__/element-template/runtime/background/reload.test.ts +74 -0
- package/runtime/__test__/element-template/runtime/background/update-timing.test.ts +15 -0
- package/runtime/__test__/element-template/runtime/patch/element-template-patch.test.tsx +212 -7
- package/runtime/__test__/element-template/runtime/patch/update-timing.test.ts +4 -1
- package/runtime/__test__/element-template/runtime/prop-adapters/gesture-compiled.test.ts +200 -0
- package/runtime/__test__/element-template/runtime/prop-adapters/gesture.test.ts +420 -0
- package/runtime/__test__/element-template/runtime/prop-adapters/spread-mts-compiled.test.ts +527 -0
- package/runtime/__test__/element-template/runtime/prop-adapters/spread.test.ts +185 -6
- package/runtime/__test__/element-template/runtime/render/render-direct-native.test.tsx +21 -1
- package/runtime/__test__/element-template/runtime/render/render-direct-suspense.test.ts +4 -1
- package/runtime/__test__/element-template/runtime/render/render-direct.test.ts +4 -1
- package/runtime/__test__/element-template/runtime/render/render-main-thread.test.ts +101 -1
- package/runtime/__test__/element-template/runtime/template/element-template-handle.test.ts +32 -0
- package/runtime/__test__/element-template/runtime/template/main-thread-dynamic-attr-state.test.ts +309 -26
- package/runtime/__test__/element-template/test-utils/debug/envManager.ts +8 -5
- package/runtime/__test__/element-template/test-utils/mock/mockNativePapi.ts +35 -2
- package/runtime/__test__/guardrails/snapshot-containment.test.ts +45 -11
- package/runtime/__test__/shared/component-stack.test.ts +211 -0
- package/runtime/__test__/snapshot/debug/formatPatch.test.ts +0 -8
- package/runtime/__test__/snapshot/gesture/prepareGestureForCommit.test.js +1 -1
- package/runtime/__test__/snapshot/lifecycle/updateGlobalProps.event.test.jsx +2 -2
- package/runtime/__test__/snapshot/lifecycle/updateGlobalProps.test.jsx +3 -3
- package/runtime/__test__/snapshot/lynx/timing.test.jsx +3 -0
- package/runtime/__test__/snapshot/preact.test.jsx +0 -3
- package/runtime/__test__/snapshot/ref.test.jsx +30 -0
- package/runtime/__test__/snapshot/snapshotPatch.test.jsx +0 -33
- package/runtime/__test__/worklet-runtime/jsFunctionLifecycle.test.js +12 -0
- package/runtime/__test__/worklet-runtime/loadRuntime.test.js +68 -0
- package/runtime/__test__/worklet-runtime/workletRuntime.test.js +30 -0
- package/runtime/lazy/element-template-internal.js +1 -1
- package/runtime/lazy/internal.js +1 -1
- package/runtime/lazy/jsx-dev-runtime.js +1 -1
- package/runtime/lazy/jsx-runtime.js +1 -1
- package/runtime/lazy/legacy-react-runtime.js +1 -1
- package/runtime/lazy/react-lepus.js +1 -1
- package/runtime/lazy/react.js +1 -1
- package/runtime/lib/core/capture-main-thread-object.js +2 -1
- package/runtime/lib/core/capture-main-thread-object.js.map +1 -1
- package/runtime/lib/core/compat/legacy-react-runtime.js +1 -1
- package/runtime/lib/core/compat/legacy-react-runtime.js.map +1 -1
- package/runtime/lib/core/globalProps.js +1 -1
- package/runtime/lib/core/globalProps.js.map +1 -1
- package/runtime/lib/core/lynx/lazy-bundle.js +1 -1
- package/runtime/lib/core/lynx/lazy-bundle.js.map +1 -1
- package/runtime/lib/core/lynx-data-processors.d.ts +125 -1
- package/runtime/lib/core/lynx-data-processors.js +3 -0
- package/runtime/lib/core/lynx-data-processors.js.map +1 -1
- package/runtime/lib/core/runtime-destroy.js +1 -1
- package/runtime/lib/core/runtime-destroy.js.map +1 -1
- package/runtime/lib/document.js +0 -5
- package/runtime/lib/document.js.map +1 -1
- package/runtime/lib/element-template/background/attr-slots.js +5 -2
- package/runtime/lib/element-template/background/attr-slots.js.map +1 -1
- package/runtime/lib/element-template/background/commit-hook.js +11 -12
- package/runtime/lib/element-template/background/commit-hook.js.map +1 -1
- package/runtime/lib/element-template/background/destroy.js +16 -11
- package/runtime/lib/element-template/background/destroy.js.map +1 -1
- package/runtime/lib/element-template/background/hydrate.js +23 -0
- package/runtime/lib/element-template/background/hydrate.js.map +1 -1
- package/runtime/lib/element-template/client/root.d.ts +1 -1
- package/runtime/lib/element-template/client/root.js.map +1 -1
- package/runtime/lib/element-template/debug/alog.d.ts +1 -1
- package/runtime/lib/element-template/debug/alog.js +4 -1
- package/runtime/lib/element-template/debug/alog.js.map +1 -1
- package/runtime/lib/element-template/index.d.ts +2 -2
- package/runtime/lib/element-template/index.js.map +1 -1
- package/runtime/lib/element-template/internal.d.ts +2 -2
- package/runtime/lib/element-template/internal.js +1 -14
- package/runtime/lib/element-template/internal.js.map +1 -1
- package/runtime/lib/element-template/lynx/env.js.map +1 -1
- package/runtime/lib/element-template/native/callDestroyLifetimeFun.js +6 -1
- package/runtime/lib/element-template/native/callDestroyLifetimeFun.js.map +1 -1
- package/runtime/lib/element-template/native/flush.d.ts +1 -0
- package/runtime/lib/element-template/native/flush.js +9 -0
- package/runtime/lib/element-template/native/flush.js.map +1 -0
- package/runtime/lib/element-template/native/main-thread-api.js +3 -7
- package/runtime/lib/element-template/native/main-thread-api.js.map +1 -1
- package/runtime/lib/element-template/native/mts-destroy.js +8 -2
- package/runtime/lib/element-template/native/mts-destroy.js.map +1 -1
- package/runtime/lib/element-template/native/patch-listener.js +57 -50
- package/runtime/lib/element-template/native/patch-listener.js.map +1 -1
- package/runtime/lib/element-template/native/reload-background.js +1 -1
- package/runtime/lib/element-template/native/reload-background.js.map +1 -1
- package/runtime/lib/element-template/native/reload-main-thread.js +35 -10
- package/runtime/lib/element-template/native/reload-main-thread.js.map +1 -1
- package/runtime/lib/element-template/prop-adapters/gesture.d.ts +23 -0
- package/runtime/lib/element-template/prop-adapters/gesture.js +97 -0
- package/runtime/lib/element-template/prop-adapters/gesture.js.map +1 -0
- package/runtime/lib/element-template/prop-adapters/spread.d.ts +6 -1
- package/runtime/lib/element-template/prop-adapters/spread.js +72 -2
- package/runtime/lib/element-template/prop-adapters/spread.js.map +1 -1
- package/runtime/lib/element-template/protocol/opcodes.d.ts +1 -0
- package/runtime/lib/element-template/protocol/opcodes.js +1 -0
- package/runtime/lib/element-template/protocol/opcodes.js.map +1 -1
- package/runtime/lib/element-template/protocol/types.d.ts +7 -1
- package/runtime/lib/element-template/protocol/types.js.map +1 -1
- package/runtime/lib/element-template/runtime/page/root-instance.d.ts +0 -1
- package/runtime/lib/element-template/runtime/page/root-instance.js.map +1 -1
- package/runtime/lib/element-template/runtime/patch.js +31 -2
- package/runtime/lib/element-template/runtime/patch.js.map +1 -1
- package/runtime/lib/element-template/runtime/render/render-components.js +4 -4
- package/runtime/lib/element-template/runtime/render/render-components.js.map +1 -1
- package/runtime/lib/element-template/runtime/render/render-main-thread.js +2 -0
- package/runtime/lib/element-template/runtime/render/render-main-thread.js.map +1 -1
- package/runtime/lib/element-template/runtime/template/attr-slot-plan.d.ts +4 -2
- package/runtime/lib/element-template/runtime/template/attr-slot-plan.js +17 -4
- package/runtime/lib/element-template/runtime/template/attr-slot-plan.js.map +1 -1
- package/runtime/lib/element-template/runtime/template/main-thread-background-function.js +8 -1
- package/runtime/lib/element-template/runtime/template/main-thread-background-function.js.map +1 -1
- package/runtime/lib/element-template/runtime/template/main-thread-dynamic-attr-state.d.ts +7 -1
- package/runtime/lib/element-template/runtime/template/main-thread-dynamic-attr-state.js +380 -50
- package/runtime/lib/element-template/runtime/template/main-thread-dynamic-attr-state.js.map +1 -1
- package/runtime/lib/element-template/runtime/template/main-thread-ref-ctx.d.ts +3 -2
- package/runtime/lib/element-template/runtime/template/main-thread-ref-ctx.js +10 -1
- package/runtime/lib/element-template/runtime/template/main-thread-ref-ctx.js.map +1 -1
- package/runtime/lib/element-template/runtime/template/typed-attributes.js +3 -3
- package/runtime/lib/element-template/runtime/template/typed-attributes.js.map +1 -1
- package/runtime/lib/internal.d.ts +1 -1
- package/runtime/lib/internal.js +1 -1
- package/runtime/lib/internal.js.map +1 -1
- package/runtime/lib/lynx-api.d.ts +2 -125
- package/runtime/lib/lynx-api.js.map +1 -1
- package/runtime/lib/shared/component-stack.d.ts +0 -4
- package/runtime/lib/shared/component-stack.js +21 -5
- package/runtime/lib/shared/component-stack.js.map +1 -1
- package/runtime/lib/snapshot/gesture/{processGestureBagkround.js → processGestureBackground.js} +1 -1
- package/runtime/lib/snapshot/gesture/processGestureBackground.js.map +1 -0
- package/runtime/lib/snapshot/lifecycle/destroy.js +23 -17
- package/runtime/lib/snapshot/lifecycle/destroy.js.map +1 -1
- package/runtime/lib/snapshot/lifecycle/patch/snapshotPatch.d.ts +0 -1
- package/runtime/lib/snapshot/lifecycle/patch/snapshotPatch.js +0 -5
- package/runtime/lib/snapshot/lifecycle/patch/snapshotPatch.js.map +1 -1
- package/runtime/lib/snapshot/lifecycle/patch/snapshotPatchApply.js +0 -10
- package/runtime/lib/snapshot/lifecycle/patch/snapshotPatchApply.js.map +1 -1
- package/runtime/lib/snapshot/lifecycle/patch/updateMainThread.js +56 -51
- package/runtime/lib/snapshot/lifecycle/patch/updateMainThread.js.map +1 -1
- package/runtime/lib/snapshot/lifecycle/ref/delay.d.ts +5 -0
- package/runtime/lib/snapshot/lifecycle/ref/delay.js +1 -1
- package/runtime/lib/snapshot/lifecycle/ref/delay.js.map +1 -1
- package/runtime/lib/snapshot/list/listUpdateInfo.js +0 -30
- package/runtime/lib/snapshot/list/listUpdateInfo.js.map +1 -1
- package/runtime/lib/snapshot/lynx/calledByNative.js +11 -8
- package/runtime/lib/snapshot/lynx/calledByNative.js.map +1 -1
- package/runtime/lib/snapshot/renderToOpcodes/hydrate.js +0 -52
- package/runtime/lib/snapshot/renderToOpcodes/hydrate.js.map +1 -1
- package/runtime/lib/snapshot/renderToOpcodes/index.js +10 -10
- package/runtime/lib/snapshot/renderToOpcodes/index.js.map +1 -1
- package/runtime/lib/snapshot/renderToOpcodes/opcodes.js +0 -51
- package/runtime/lib/snapshot/renderToOpcodes/opcodes.js.map +1 -1
- package/runtime/lib/snapshot/snapshot/backgroundSnapshot.js +1 -1
- package/runtime/lib/snapshot/snapshot/backgroundSnapshot.js.map +1 -1
- package/runtime/lib/tsconfig.tsbuildinfo +1 -1
- package/runtime/lib/worklet-runtime/bindings/index.d.ts +1 -0
- package/runtime/lib/worklet-runtime/bindings/index.js +1 -0
- package/runtime/lib/worklet-runtime/bindings/index.js.map +1 -1
- package/runtime/lib/worklet-runtime/bindings/loadRuntime.d.ts +8 -0
- package/runtime/lib/worklet-runtime/bindings/loadRuntime.js +23 -0
- package/runtime/lib/worklet-runtime/bindings/loadRuntime.js.map +1 -0
- package/runtime/lib/worklet-runtime/jsFunctionLifecycle.d.ts +2 -0
- package/runtime/lib/worklet-runtime/jsFunctionLifecycle.js +6 -0
- package/runtime/lib/worklet-runtime/jsFunctionLifecycle.js.map +1 -1
- package/runtime/lib/worklet-runtime/workletRuntime.js +2 -0
- package/runtime/lib/worklet-runtime/workletRuntime.js.map +1 -1
- package/runtime/package.json +2 -3
- package/runtime/src/core/capture-main-thread-object.ts +2 -1
- package/runtime/src/core/compat/legacy-react-runtime.ts +1 -1
- package/runtime/src/core/globalProps.ts +1 -1
- package/runtime/src/core/lynx/lazy-bundle.ts +1 -1
- package/runtime/src/core/lynx-data-processors.ts +124 -1
- package/runtime/src/core/runtime-destroy.ts +1 -1
- package/runtime/src/document.ts +0 -6
- package/runtime/src/element-template/background/attr-slots.ts +5 -1
- package/runtime/src/element-template/background/commit-hook.ts +18 -12
- package/runtime/src/element-template/background/destroy.ts +16 -13
- package/runtime/src/element-template/background/hydrate.ts +23 -0
- package/runtime/src/element-template/client/root.ts +1 -3
- package/runtime/src/element-template/debug/alog.ts +5 -2
- package/runtime/src/element-template/index.ts +2 -2
- package/runtime/src/element-template/internal.ts +1 -20
- package/runtime/src/element-template/lynx/env.ts +1 -1
- package/runtime/src/element-template/native/callDestroyLifetimeFun.ts +5 -1
- package/runtime/src/element-template/native/flush.ts +10 -0
- package/runtime/src/element-template/native/main-thread-api.ts +3 -6
- package/runtime/src/element-template/native/mts-destroy.ts +7 -2
- package/runtime/src/element-template/native/patch-listener.ts +62 -57
- package/runtime/src/element-template/native/reload-background.ts +1 -1
- package/runtime/src/element-template/native/reload-main-thread.ts +32 -11
- package/runtime/src/element-template/prop-adapters/gesture.ts +144 -0
- package/runtime/src/element-template/prop-adapters/spread.ts +110 -2
- package/runtime/src/element-template/protocol/opcodes.ts +1 -0
- package/runtime/src/element-template/protocol/types.ts +8 -13
- package/runtime/src/element-template/runtime/page/root-instance.ts +0 -1
- package/runtime/src/element-template/runtime/patch.ts +38 -0
- package/runtime/src/element-template/runtime/render/render-components.ts +3 -3
- package/runtime/src/element-template/runtime/render/render-main-thread.ts +2 -0
- package/runtime/src/element-template/runtime/template/attr-slot-plan.ts +31 -4
- package/runtime/src/element-template/runtime/template/main-thread-background-function.ts +8 -1
- package/runtime/src/element-template/runtime/template/main-thread-dynamic-attr-state.ts +456 -50
- package/runtime/src/element-template/runtime/template/main-thread-ref-ctx.ts +14 -4
- package/runtime/src/element-template/runtime/template/typed-attributes.ts +3 -3
- package/runtime/src/element-template/types.d.ts +6 -0
- package/runtime/src/internal.ts +1 -1
- package/runtime/src/lynx-api.ts +2 -124
- package/runtime/src/shared/component-stack.ts +22 -5
- package/runtime/src/snapshot/lifecycle/destroy.ts +23 -18
- package/runtime/src/snapshot/lifecycle/patch/snapshotPatch.ts +0 -5
- package/runtime/src/snapshot/lifecycle/patch/snapshotPatchApply.ts +0 -10
- package/runtime/src/snapshot/lifecycle/patch/updateMainThread.ts +62 -58
- package/runtime/src/snapshot/lifecycle/ref/delay.ts +1 -1
- package/runtime/src/snapshot/list/listUpdateInfo.ts +0 -52
- package/runtime/src/snapshot/lynx/calledByNative.ts +11 -9
- package/runtime/src/snapshot/renderToOpcodes/hydrate.ts +0 -58
- package/runtime/src/snapshot/renderToOpcodes/index.ts +8 -8
- package/runtime/src/snapshot/renderToOpcodes/opcodes.ts +0 -57
- package/runtime/src/snapshot/snapshot/backgroundSnapshot.ts +1 -1
- package/runtime/src/worklet-runtime/README.md +30 -10
- package/runtime/src/worklet-runtime/bindings/index.ts +2 -0
- package/runtime/src/worklet-runtime/bindings/loadRuntime.ts +25 -0
- package/runtime/src/worklet-runtime/jsFunctionLifecycle.ts +6 -0
- package/runtime/src/worklet-runtime/workletRuntime.ts +2 -0
- package/runtime/types/types.d.ts +2 -1
- package/runtime/vitest.config.ts +0 -2
- package/testing-library/.turbo/turbo-build.log +7 -7
- package/testing-library/src/__tests__/act.test.jsx +0 -7
- package/testing-library/src/__tests__/alog.test.jsx +12 -54
- package/testing-library/src/__tests__/end-to-end.test.jsx +0 -7
- package/testing-library/src/__tests__/renderComponent.test.jsx +0 -14
- package/testing-library/src/__tests__/worklet.test.jsx +49 -0
- package/transform/.turbo/turbo-build$colon$wasm.log +133 -297
- package/transform/__test__/defines.spec.js +30 -0
- package/transform/__test__/fixture.spec.js +11 -11
- package/transform/__test__/main-thread-object-compat.spec.js +111 -0
- package/transform/__test__/object-methods.spec.js +79 -0
- package/transform/crates/swc_plugin_element_template/lib.rs +11 -0
- package/transform/crates/swc_plugin_element_template/tests/element_template_contract.rs +113 -0
- package/transform/crates/swc_plugin_worklet/extract_ident.rs +99 -49
- package/transform/crates/swc_plugin_worklet/gen_stmt.rs +63 -57
- package/transform/crates/swc_plugin_worklet/lib.rs +393 -589
- package/transform/crates/swc_plugin_worklet/napi.rs +4 -6
- package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_capture_env_js.js +3 -3
- package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_capture_env_lepus.js +3 -3
- package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_extract_member_expr_2_js.js +11 -6
- package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_extract_member_expr_2_lepus.js +12 -6
- package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_extract_member_expr_js.js +14 -5
- package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_extract_member_expr_lepus.js +17 -6
- package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_preserve_main_thread_objects_in_class_js.js +5 -5
- package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_preserve_main_thread_objects_in_class_lepus.js +5 -5
- package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_transform_js.js +1 -1
- package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_transform_lepus.js +1 -1
- package/transform/dist/wasm.cjs +1 -1
- package/transform/package.json +0 -1
- package/transform/src/lib.rs +1 -1
- package/transform/swc-plugin-reactlynx/.turbo/turbo-build.log +167 -69
- package/transform/swc-plugin-reactlynx/src/lib.rs +1 -4
- package/transform/swc-plugin-reactlynx/swc_plugin_reactlynx.wasm +0 -0
- package/transform/swc-plugin-reactlynx-compat/.turbo/turbo-build.log +226 -132
- package/runtime/lib/snapshot/gesture/processGestureBagkround.js.map +0 -1
- package/runtime/lib/snapshot/worklet/hmr.d.ts +0 -1
- package/runtime/lib/snapshot/worklet/hmr.js +0 -28
- package/runtime/lib/snapshot/worklet/hmr.js.map +0 -1
- package/runtime/src/snapshot/worklet/hmr.ts +0 -34
- /package/runtime/lib/snapshot/gesture/{processGestureBagkround.d.ts → processGestureBackground.d.ts} +0 -0
- /package/runtime/src/snapshot/gesture/{processGestureBagkround.ts → processGestureBackground.ts} +0 -0
|
@@ -177,6 +177,21 @@ describe('collected defines', () => {
|
|
|
177
177
|
expect(result.code).not.toContain('__CreateView');
|
|
178
178
|
});
|
|
179
179
|
|
|
180
|
+
it.each(['development', 'production'])(
|
|
181
|
+
'keeps worklet implementations in main-thread definitions in %s builds',
|
|
182
|
+
async mode => {
|
|
183
|
+
const result = await transformReactLynx(source, options('JS', { mode }));
|
|
184
|
+
|
|
185
|
+
expect(result.errors).toEqual([]);
|
|
186
|
+
expect(result.code).not.toContain('registerWorkletOnBackground');
|
|
187
|
+
expect(result.code).not.toContain('console.info');
|
|
188
|
+
expect(result.code).toContain('_wkltId');
|
|
189
|
+
expect(result.definesForWorklet).toHaveLength(1);
|
|
190
|
+
expect(result.definesForWorklet[0].code).toContain('__registerWorklet');
|
|
191
|
+
expect(result.definesForWorklet[0].code).toContain('console.info');
|
|
192
|
+
},
|
|
193
|
+
);
|
|
194
|
+
|
|
180
195
|
it('collects the same definitions the main-thread transform would emit', async () => {
|
|
181
196
|
const fromBackground = await transformReactLynx(
|
|
182
197
|
source,
|
|
@@ -439,6 +454,21 @@ describe('legacy slot codegen', () => {
|
|
|
439
454
|
`);
|
|
440
455
|
});
|
|
441
456
|
|
|
457
|
+
it.each(['development', 'production'])(
|
|
458
|
+
'keeps worklet implementations in main-thread definitions in %s builds',
|
|
459
|
+
async mode => {
|
|
460
|
+
const result = await transformReactLynx(source, { ...legacy('JS'), mode });
|
|
461
|
+
|
|
462
|
+
expect(result.errors).toEqual([]);
|
|
463
|
+
expect(result.code).not.toContain('registerWorkletOnBackground');
|
|
464
|
+
expect(result.code).not.toContain('console.info');
|
|
465
|
+
expect(result.code).toContain('_wkltId');
|
|
466
|
+
expect(result.definesForWorklet).toHaveLength(1);
|
|
467
|
+
expect(result.definesForWorklet[0].code).toContain('__registerWorklet');
|
|
468
|
+
expect(result.definesForWorklet[0].code).toContain('console.info');
|
|
469
|
+
},
|
|
470
|
+
);
|
|
471
|
+
|
|
442
472
|
it('collects the same definitions the main-thread transform would emit', async () => {
|
|
443
473
|
const fromBackground = await transformReactLynx(source, legacy('JS'));
|
|
444
474
|
const fromMainThread = await transformReactLynx(source, legacy('LEPUS'));
|
|
@@ -2186,8 +2186,8 @@ export function getCurrentDelta(event) {
|
|
|
2186
2186
|
var captureMainThreadObject = __captureMainThreadObject;
|
|
2187
2187
|
export let getCurrentDelta = {
|
|
2188
2188
|
_c: {
|
|
2189
|
-
foo: captureMainThreadObject(foo) ?? {
|
|
2190
|
-
bar: ((__mainThreadObjectSource)=>captureMainThreadObject(__mainThreadObjectSource) ?? {
|
|
2189
|
+
foo: captureMainThreadObject?.(foo) ?? {
|
|
2190
|
+
bar: ((__mainThreadObjectSource)=>captureMainThreadObject?.(__mainThreadObjectSource) ?? {
|
|
2191
2191
|
baz: __mainThreadObjectSource.baz
|
|
2192
2192
|
})(foo.bar)
|
|
2193
2193
|
}
|
|
@@ -2208,8 +2208,8 @@ export function getCurrentDelta(event) {
|
|
|
2208
2208
|
var captureMainThreadObject = __captureMainThreadObject;
|
|
2209
2209
|
export let getCurrentDelta = {
|
|
2210
2210
|
_c: {
|
|
2211
|
-
foo: captureMainThreadObject(foo) ?? {
|
|
2212
|
-
bar: ((__mainThreadObjectSource)=>captureMainThreadObject(__mainThreadObjectSource) ?? {
|
|
2211
|
+
foo: captureMainThreadObject?.(foo) ?? {
|
|
2212
|
+
bar: ((__mainThreadObjectSource)=>captureMainThreadObject?.(__mainThreadObjectSource) ?? {
|
|
2213
2213
|
baz: __mainThreadObjectSource.baz
|
|
2214
2214
|
})(foo.bar)
|
|
2215
2215
|
}
|
|
@@ -2225,8 +2225,8 @@ export function getCurrentDelta(event) {
|
|
|
2225
2225
|
var captureMainThreadObject = __captureMainThreadObject;
|
|
2226
2226
|
export let getCurrentDelta = {
|
|
2227
2227
|
_c: {
|
|
2228
|
-
foo: captureMainThreadObject(foo) ?? {
|
|
2229
|
-
bar: ((__mainThreadObjectSource)=>captureMainThreadObject(__mainThreadObjectSource) ?? {
|
|
2228
|
+
foo: captureMainThreadObject?.(foo) ?? {
|
|
2229
|
+
bar: ((__mainThreadObjectSource)=>captureMainThreadObject?.(__mainThreadObjectSource) ?? {
|
|
2230
2230
|
baz: __mainThreadObjectSource.baz
|
|
2231
2231
|
})(foo.bar)
|
|
2232
2232
|
}
|
|
@@ -2286,16 +2286,16 @@ export function foo(event) {
|
|
|
2286
2286
|
var captureMainThreadObject = __captureMainThreadObject;
|
|
2287
2287
|
export let foo = {
|
|
2288
2288
|
_c: {
|
|
2289
|
-
bar: captureMainThreadObject(bar) ?? {
|
|
2290
|
-
baz: ((__mainThreadObjectSource)=>captureMainThreadObject(__mainThreadObjectSource) ?? {
|
|
2289
|
+
bar: captureMainThreadObject?.(bar) ?? {
|
|
2290
|
+
baz: ((__mainThreadObjectSource)=>captureMainThreadObject?.(__mainThreadObjectSource) ?? {
|
|
2291
2291
|
'qux': __mainThreadObjectSource['qux']
|
|
2292
2292
|
})(bar.baz),
|
|
2293
|
-
qux: ((__mainThreadObjectSource)=>captureMainThreadObject(__mainThreadObjectSource) ?? {
|
|
2293
|
+
qux: ((__mainThreadObjectSource)=>captureMainThreadObject?.(__mainThreadObjectSource) ?? {
|
|
2294
2294
|
'baz': __mainThreadObjectSource['baz']
|
|
2295
2295
|
})(bar.qux)
|
|
2296
2296
|
},
|
|
2297
|
-
qux: captureMainThreadObject(qux) ?? {
|
|
2298
|
-
bar: ((__mainThreadObjectSource)=>captureMainThreadObject(__mainThreadObjectSource) ?? {
|
|
2297
|
+
qux: captureMainThreadObject?.(qux) ?? {
|
|
2298
|
+
bar: ((__mainThreadObjectSource)=>captureMainThreadObject?.(__mainThreadObjectSource) ?? {
|
|
2299
2299
|
baz: __mainThreadObjectSource.baz
|
|
2300
2300
|
})(qux.bar)
|
|
2301
2301
|
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
// Copyright 2026 The Lynx Authors. All rights reserved.
|
|
2
|
+
// Licensed under the Apache License Version 2.0 that can be found in the
|
|
3
|
+
// LICENSE file in the root directory of this source tree.
|
|
4
|
+
|
|
5
|
+
import { describe, expect, it } from 'vitest';
|
|
6
|
+
|
|
7
|
+
import { captureMainThreadObject } from '../../runtime/src/core/capture-main-thread-object.js';
|
|
8
|
+
import { registerMainThreadObjectHandle } from '../../runtime/src/core/main-thread-object-handle-registry.js';
|
|
9
|
+
import { transformReactLynx } from '../main.js';
|
|
10
|
+
|
|
11
|
+
async function compile(target, helper) {
|
|
12
|
+
const result = await transformReactLynx(
|
|
13
|
+
`
|
|
14
|
+
export function create(rect) {
|
|
15
|
+
return () => {
|
|
16
|
+
'main thread';
|
|
17
|
+
return rect.top + rect.bottom;
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
export function createNested(props) {
|
|
21
|
+
return () => {
|
|
22
|
+
'main thread';
|
|
23
|
+
return props?.rect?.top;
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
`,
|
|
27
|
+
{
|
|
28
|
+
mode: 'test',
|
|
29
|
+
pluginName: '',
|
|
30
|
+
filename: 'test.jsx',
|
|
31
|
+
sourcemap: false,
|
|
32
|
+
cssScope: false,
|
|
33
|
+
shake: false,
|
|
34
|
+
compat: false,
|
|
35
|
+
refresh: false,
|
|
36
|
+
defineDCE: false,
|
|
37
|
+
directiveDCE: false,
|
|
38
|
+
worklet: {
|
|
39
|
+
target,
|
|
40
|
+
filename: 'test.jsx',
|
|
41
|
+
runtimePkg: '@lynx-js/react/internal',
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
);
|
|
45
|
+
expect(result.errors).toEqual([]);
|
|
46
|
+
const functions = new Map();
|
|
47
|
+
const factories = new Function(
|
|
48
|
+
'__registerWorklet',
|
|
49
|
+
'__captureMainThreadObject',
|
|
50
|
+
`${result.code.replace(/^import .*;$/gm, '').replace(/^export /gm, '')}
|
|
51
|
+
return { create, createNested };`,
|
|
52
|
+
)(
|
|
53
|
+
(_type, id, fn) => functions.set(id, fn),
|
|
54
|
+
helper,
|
|
55
|
+
);
|
|
56
|
+
return {
|
|
57
|
+
...factories,
|
|
58
|
+
invoke: worklet => functions.get(worklet._wkltId).call(worklet),
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
describe.each(['JS', 'LEPUS', 'MIXED'])('MTO helper compatibility (%s)', target => {
|
|
63
|
+
it.each([undefined, null, captureMainThreadObject])(
|
|
64
|
+
'keeps ordinary MTS captures working with helper %s',
|
|
65
|
+
async helper => {
|
|
66
|
+
const { create, createNested, invoke } = await compile(target, helper);
|
|
67
|
+
const worklet = create({ top: 10, bottom: 20, unused: 30 });
|
|
68
|
+
expect(worklet._c).toEqual({ rect: { top: 10, bottom: 20 } });
|
|
69
|
+
|
|
70
|
+
let reads = 0;
|
|
71
|
+
const nested = createNested({
|
|
72
|
+
get rect() {
|
|
73
|
+
reads += 1;
|
|
74
|
+
return { top: 42, unused: 99 };
|
|
75
|
+
},
|
|
76
|
+
});
|
|
77
|
+
expect(reads).toBe(1);
|
|
78
|
+
expect(nested._c).toEqual({ props: { rect: { top: 42 } } });
|
|
79
|
+
expect(createNested(null)._c.props).toBeNull();
|
|
80
|
+
expect(createNested(undefined)._c.props).toBeUndefined();
|
|
81
|
+
expect(createNested({ rect: null })._c.props.rect).toBeNull();
|
|
82
|
+
expect(createNested({ rect: undefined })._c.props.rect).toBeUndefined();
|
|
83
|
+
|
|
84
|
+
if (target !== 'JS') {
|
|
85
|
+
expect(invoke(worklet)).toBe(30);
|
|
86
|
+
expect(invoke(nested)).toBe(42);
|
|
87
|
+
expect(invoke(createNested(null))).toBeUndefined();
|
|
88
|
+
expect(invoke(createNested({ rect: null }))).toBeUndefined();
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
it('preserves registered MTO handles without reading their properties', async () => {
|
|
94
|
+
const { create, createNested } = await compile(target, captureMainThreadObject);
|
|
95
|
+
const handle = Object.defineProperties({}, {
|
|
96
|
+
top: {
|
|
97
|
+
get() {
|
|
98
|
+
throw new Error('opaque handle must not be read');
|
|
99
|
+
},
|
|
100
|
+
},
|
|
101
|
+
bottom: {
|
|
102
|
+
get() {
|
|
103
|
+
throw new Error('opaque handle must not be read');
|
|
104
|
+
},
|
|
105
|
+
},
|
|
106
|
+
});
|
|
107
|
+
registerMainThreadObjectHandle(handle, 'compat-test');
|
|
108
|
+
expect(create(handle)._c.rect).toBe(handle);
|
|
109
|
+
expect(createNested({ rect: handle })._c.props.rect).toBe(handle);
|
|
110
|
+
});
|
|
111
|
+
});
|
|
@@ -79,4 +79,83 @@ describe('object method captures', () => {
|
|
|
79
79
|
expect(Object.getOwnPropertyDescriptor(valueType, 'create').get.call(other)._jsFn._jsFn1)
|
|
80
80
|
.toBe(other.callback);
|
|
81
81
|
});
|
|
82
|
+
|
|
83
|
+
it('preserves nullish bases and guards when narrowing optional member captures', async () => {
|
|
84
|
+
const result = await transformReactLynx(
|
|
85
|
+
`
|
|
86
|
+
export function create(enabled, handlers) {
|
|
87
|
+
return () => {
|
|
88
|
+
'main thread';
|
|
89
|
+
if (enabled && handlers?.start) handlers.start();
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export function createNested(enabled, handlers) {
|
|
94
|
+
return () => {
|
|
95
|
+
'main thread';
|
|
96
|
+
if (enabled && handlers?.nested?.start) handlers.nested.start();
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
`,
|
|
100
|
+
options('LEPUS'),
|
|
101
|
+
);
|
|
102
|
+
const functions = new Map();
|
|
103
|
+
const { create, createNested } = new Function(
|
|
104
|
+
'__registerWorklet',
|
|
105
|
+
'__captureMainThreadObject',
|
|
106
|
+
`${result.code.replace(/^import .*;$/gm, '').replace(/^export /gm, '')}\nreturn { create, createNested };`,
|
|
107
|
+
)(
|
|
108
|
+
(_type, id, fn) => functions.set(id, fn),
|
|
109
|
+
() => undefined,
|
|
110
|
+
);
|
|
111
|
+
const invoke = worklet => functions.get(worklet._wkltId).call(worklet);
|
|
112
|
+
|
|
113
|
+
const withNullDisabled = create(false, null);
|
|
114
|
+
expect(withNullDisabled._c.handlers).toBeNull();
|
|
115
|
+
expect(() => invoke(withNullDisabled)).not.toThrow();
|
|
116
|
+
|
|
117
|
+
const withUndefinedDisabled = create(false, undefined);
|
|
118
|
+
expect(withUndefinedDisabled._c.handlers).toBeUndefined();
|
|
119
|
+
expect(() => invoke(withUndefinedDisabled)).not.toThrow();
|
|
120
|
+
|
|
121
|
+
const withNullEnabled = create(true, null);
|
|
122
|
+
expect(withNullEnabled._c.handlers).toBeNull();
|
|
123
|
+
expect(() => invoke(withNullEnabled)).not.toThrow();
|
|
124
|
+
|
|
125
|
+
const withUndefinedEnabled = create(true, undefined);
|
|
126
|
+
expect(withUndefinedEnabled._c.handlers).toBeUndefined();
|
|
127
|
+
expect(() => invoke(withUndefinedEnabled)).not.toThrow();
|
|
128
|
+
|
|
129
|
+
let started = 0;
|
|
130
|
+
const handlers = {
|
|
131
|
+
start() {
|
|
132
|
+
started += 1;
|
|
133
|
+
},
|
|
134
|
+
extra: 'unreferenced',
|
|
135
|
+
};
|
|
136
|
+
const withHandlersDisabled = create(false, handlers);
|
|
137
|
+
expect(withHandlersDisabled._c.handlers).toEqual({ start: handlers.start });
|
|
138
|
+
expect('extra' in withHandlersDisabled._c.handlers).toBe(false);
|
|
139
|
+
invoke(withHandlersDisabled);
|
|
140
|
+
expect(started).toBe(0);
|
|
141
|
+
|
|
142
|
+
const withHandlersEnabled = create(true, handlers);
|
|
143
|
+
invoke(withHandlersEnabled);
|
|
144
|
+
expect(started).toBe(1);
|
|
145
|
+
|
|
146
|
+
const withNestedNull = createNested(true, { nested: null, extra: 1 });
|
|
147
|
+
expect(withNestedNull._c.handlers).toEqual({ nested: null });
|
|
148
|
+
expect(() => invoke(withNestedNull)).not.toThrow();
|
|
149
|
+
|
|
150
|
+
const withNestedUndefined = createNested(true, { nested: undefined, extra: 1 });
|
|
151
|
+
expect(withNestedUndefined._c.handlers).toEqual({ nested: undefined });
|
|
152
|
+
expect(() => invoke(withNestedUndefined)).not.toThrow();
|
|
153
|
+
|
|
154
|
+
const withNestedHandlers = createNested(true, { nested: handlers, extra: 1 });
|
|
155
|
+
expect(withNestedHandlers._c.handlers).toEqual({
|
|
156
|
+
nested: { start: handlers.start },
|
|
157
|
+
});
|
|
158
|
+
invoke(withNestedHandlers);
|
|
159
|
+
expect(started).toBe(2);
|
|
160
|
+
});
|
|
82
161
|
});
|
|
@@ -449,6 +449,7 @@ where
|
|
|
449
449
|
MTEvent,
|
|
450
450
|
Ref,
|
|
451
451
|
MTRef,
|
|
452
|
+
Gesture,
|
|
452
453
|
Spread,
|
|
453
454
|
}
|
|
454
455
|
|
|
@@ -475,6 +476,11 @@ where
|
|
|
475
476
|
slot_index,
|
|
476
477
|
..
|
|
477
478
|
} => Some((*slot_index, AttrPlanAdapter::MTRef)),
|
|
479
|
+
DynamicAttributePart::Attr {
|
|
480
|
+
attr_name: AttrName::Gesture,
|
|
481
|
+
slot_index,
|
|
482
|
+
..
|
|
483
|
+
} => Some((*slot_index, AttrPlanAdapter::Gesture)),
|
|
478
484
|
DynamicAttributePart::Spread { slot_index, .. } => {
|
|
479
485
|
Some((*slot_index, AttrPlanAdapter::Spread))
|
|
480
486
|
}
|
|
@@ -489,6 +495,7 @@ where
|
|
|
489
495
|
AttrPlanAdapter::MTEvent => "mt-event",
|
|
490
496
|
AttrPlanAdapter::Ref => "ref",
|
|
491
497
|
AttrPlanAdapter::MTRef => "mt-ref",
|
|
498
|
+
AttrPlanAdapter::Gesture => "gesture",
|
|
492
499
|
AttrPlanAdapter::Spread => "spread",
|
|
493
500
|
};
|
|
494
501
|
format!("{slot_index}:{adapter}")
|
|
@@ -641,6 +648,10 @@ where
|
|
|
641
648
|
"$internal_runtime_id.adaptMTRefAttrSlot" as Expr,
|
|
642
649
|
internal_runtime_id: Expr = internal_runtime_id.clone(),
|
|
643
650
|
),
|
|
651
|
+
AttrPlanAdapter::Gesture => quote!(
|
|
652
|
+
"$internal_runtime_id.adaptGestureAttrSlot" as Expr,
|
|
653
|
+
internal_runtime_id: Expr = internal_runtime_id.clone(),
|
|
654
|
+
),
|
|
644
655
|
AttrPlanAdapter::Spread => quote!(
|
|
645
656
|
"$internal_runtime_id.adaptSpreadAttrSlot" as Expr,
|
|
646
657
|
internal_runtime_id: Expr = internal_runtime_id.clone(),
|
|
@@ -309,6 +309,119 @@ fn should_emit_direct_main_thread_ref_attr_plan_for_lepus_target() {
|
|
|
309
309
|
);
|
|
310
310
|
}
|
|
311
311
|
|
|
312
|
+
#[test]
|
|
313
|
+
fn should_emit_direct_gesture_attr_plan_for_both_targets() {
|
|
314
|
+
use swc_plugins_shared::target::TransformTarget;
|
|
315
|
+
|
|
316
|
+
for target in [TransformTarget::JS, TransformTarget::LEPUS] {
|
|
317
|
+
let (code, template) = first_user_template_json_with_code(
|
|
318
|
+
r#"<view main-thread:gesture={composedGesture} />"#,
|
|
319
|
+
JSXTransformerConfig {
|
|
320
|
+
target,
|
|
321
|
+
..element_template_config()
|
|
322
|
+
},
|
|
323
|
+
);
|
|
324
|
+
let code = without_whitespace(&code);
|
|
325
|
+
|
|
326
|
+
assert_attr_plan_assignment(
|
|
327
|
+
&code,
|
|
328
|
+
"0,ReactLynxInternal.adaptGestureAttrSlot",
|
|
329
|
+
"direct Gesture should use the same ET adapter on both targets",
|
|
330
|
+
);
|
|
331
|
+
let raw_value_transport = if target == TransformTarget::JS {
|
|
332
|
+
"attributeSlots={[composedGesture]}"
|
|
333
|
+
} else {
|
|
334
|
+
",void0,[composedGesture],void0,void0)"
|
|
335
|
+
};
|
|
336
|
+
assert!(
|
|
337
|
+
code.contains(raw_value_transport),
|
|
338
|
+
"Gesture composition must remain a raw runtime adapter input, got: {code}"
|
|
339
|
+
);
|
|
340
|
+
assert_eq!(
|
|
341
|
+
template["attributesArray"][0],
|
|
342
|
+
serde_json::json!({
|
|
343
|
+
"kind": "slot",
|
|
344
|
+
"key": "main-thread:gesture",
|
|
345
|
+
"attrSlotIndex": 0.0,
|
|
346
|
+
}),
|
|
347
|
+
"Native must receive the full Gesture namespace key"
|
|
348
|
+
);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
#[test]
|
|
353
|
+
fn should_keep_gesture_targets_inside_one_compiled_template() {
|
|
354
|
+
use swc_plugins_shared::target::TransformTarget;
|
|
355
|
+
|
|
356
|
+
for target in [TransformTarget::JS, TransformTarget::LEPUS] {
|
|
357
|
+
let (templates, code) = transform_fixture(
|
|
358
|
+
r#"
|
|
359
|
+
<view id={rootId} main-thread:gesture={rootGesture}>
|
|
360
|
+
<text main-thread:gesture={textGesture}>Drag</text>
|
|
361
|
+
<view id={childId} main-thread:gesture={childGesture} />
|
|
362
|
+
</view>
|
|
363
|
+
"#,
|
|
364
|
+
JSXTransformerConfig {
|
|
365
|
+
target,
|
|
366
|
+
..element_template_config()
|
|
367
|
+
},
|
|
368
|
+
);
|
|
369
|
+
let user_templates = templates
|
|
370
|
+
.iter()
|
|
371
|
+
.filter(|template| template.template_id != BUILTIN_RAW_TEXT_TEMPLATE_ID)
|
|
372
|
+
.collect::<Vec<_>>();
|
|
373
|
+
assert_eq!(
|
|
374
|
+
user_templates.len(),
|
|
375
|
+
1,
|
|
376
|
+
"Gesture targets must not split the compiled tree or add wrappers"
|
|
377
|
+
);
|
|
378
|
+
let template = &user_templates[0].compiled_template;
|
|
379
|
+
let code = without_whitespace(&code);
|
|
380
|
+
assert_attr_plan_assignment(
|
|
381
|
+
&code,
|
|
382
|
+
"1,ReactLynxInternal.adaptGestureAttrSlot,2,ReactLynxInternal.adaptGestureAttrSlot,4,ReactLynxInternal.adaptGestureAttrSlot",
|
|
383
|
+
"Gesture adapters must retain the slot identity of each target",
|
|
384
|
+
);
|
|
385
|
+
assert!(
|
|
386
|
+
code.contains("[rootId,rootGesture,textGesture,childId,childGesture]"),
|
|
387
|
+
"runtime Gesture values must follow descriptor traversal order, got: {code}"
|
|
388
|
+
);
|
|
389
|
+
for (node, slot_index) in [
|
|
390
|
+
(template, 1.0),
|
|
391
|
+
(&template["children"][0], 2.0),
|
|
392
|
+
(&template["children"][1], 4.0),
|
|
393
|
+
] {
|
|
394
|
+
assert!(
|
|
395
|
+
node["attributesArray"]
|
|
396
|
+
.as_array()
|
|
397
|
+
.expect("target attributes")
|
|
398
|
+
.iter()
|
|
399
|
+
.any(|attr| *attr
|
|
400
|
+
== serde_json::json!({
|
|
401
|
+
"kind": "slot",
|
|
402
|
+
"key": "main-thread:gesture",
|
|
403
|
+
"attrSlotIndex": slot_index,
|
|
404
|
+
})),
|
|
405
|
+
"Gesture slot {slot_index} must remain on its declared node: {template:?}"
|
|
406
|
+
);
|
|
407
|
+
}
|
|
408
|
+
assert_eq!(template["children"][0]["type"], "text");
|
|
409
|
+
assert_eq!(template["children"][1]["type"], "view");
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
#[test]
|
|
414
|
+
fn should_only_adapt_direct_gesture_in_main_thread_namespace() {
|
|
415
|
+
let (code, _) = first_user_template_json_with_code(
|
|
416
|
+
r#"<view gesture={ordinaryValue} other:gesture={namespacedValue} />"#,
|
|
417
|
+
element_template_config(),
|
|
418
|
+
);
|
|
419
|
+
assert!(
|
|
420
|
+
!code.contains("adaptGestureAttrSlot"),
|
|
421
|
+
"ordinary and unrelated namespaced attributes must not become Gesture bindings: {code}"
|
|
422
|
+
);
|
|
423
|
+
}
|
|
424
|
+
|
|
312
425
|
#[test]
|
|
313
426
|
fn should_disable_flatten_for_direct_main_thread_ref_targets() {
|
|
314
427
|
let template = first_user_template_json(
|
|
@@ -6,7 +6,7 @@ use std::mem::{swap, take};
|
|
|
6
6
|
use std::ops::Deref;
|
|
7
7
|
use std::{borrow::Cow, cmp::max};
|
|
8
8
|
use swc_core::common::util::take::Take;
|
|
9
|
-
use swc_core::common::EqIgnoreSpan;
|
|
9
|
+
use swc_core::common::{EqIgnoreSpan, DUMMY_SP};
|
|
10
10
|
use swc_core::ecma::ast::*;
|
|
11
11
|
use swc_core::ecma::utils::quote_ident;
|
|
12
12
|
use swc_core::ecma::visit::{noop_visit_mut_type, VisitMut, VisitMutWith};
|
|
@@ -43,6 +43,7 @@ pub struct ExtractingIdentsCollector {
|
|
|
43
43
|
member_expr_depth: u32,
|
|
44
44
|
// when `d` in `a.b.c.d` is visited, `member_expr_path` should be [a, b, c].
|
|
45
45
|
member_expr_path: Vec<Box<Expr>>,
|
|
46
|
+
optional_member_sources: Vec<Box<Expr>>,
|
|
46
47
|
}
|
|
47
48
|
|
|
48
49
|
impl ExtractingIdentsCollector {
|
|
@@ -63,9 +64,14 @@ impl ExtractingIdentsCollector {
|
|
|
63
64
|
}],
|
|
64
65
|
member_expr_depth: 0,
|
|
65
66
|
member_expr_path: vec![],
|
|
67
|
+
optional_member_sources: vec![],
|
|
66
68
|
}
|
|
67
69
|
}
|
|
68
70
|
|
|
71
|
+
pub fn take_optional_member_sources(&mut self) -> Vec<Box<Expr>> {
|
|
72
|
+
take(&mut self.optional_member_sources)
|
|
73
|
+
}
|
|
74
|
+
|
|
69
75
|
pub fn take_values(&mut self) -> Box<Expr> {
|
|
70
76
|
self.values_extracted.take()
|
|
71
77
|
}
|
|
@@ -116,7 +122,7 @@ impl ExtractingIdentsCollector {
|
|
|
116
122
|
.contains(&String::from(s)))
|
|
117
123
|
}
|
|
118
124
|
|
|
119
|
-
fn add_if_needed(&mut self, name: Box<Expr
|
|
125
|
+
fn add_if_needed(&mut self, name: Box<Expr>) {
|
|
120
126
|
assert!(name.is_ident() || name.is_lit() || name.is_this());
|
|
121
127
|
|
|
122
128
|
if self.member_expr_path.is_empty()
|
|
@@ -143,15 +149,16 @@ impl ExtractingIdentsCollector {
|
|
|
143
149
|
}
|
|
144
150
|
|
|
145
151
|
if !name.is_this() {
|
|
152
|
+
let expr_to_extract = Self::expr_from_path(&self.member_expr_path);
|
|
146
153
|
if !self.member_expr_path.is_empty() && self.member_expr_path[0].is_this() {
|
|
147
154
|
Self::add_inner(
|
|
148
|
-
expr_to_extract,
|
|
155
|
+
&expr_to_extract,
|
|
149
156
|
&mut self.this_expr_to_extract,
|
|
150
157
|
&self.member_expr_path[1..],
|
|
151
158
|
);
|
|
152
159
|
} else {
|
|
153
160
|
Self::add_inner(
|
|
154
|
-
expr_to_extract,
|
|
161
|
+
&expr_to_extract,
|
|
155
162
|
&mut self.values_extracted,
|
|
156
163
|
&self.member_expr_path,
|
|
157
164
|
)
|
|
@@ -247,6 +254,85 @@ impl ExtractingIdentsCollector {
|
|
|
247
254
|
})
|
|
248
255
|
}
|
|
249
256
|
|
|
257
|
+
fn is_extractable_member_obj(expr: &Expr) -> bool {
|
|
258
|
+
match expr {
|
|
259
|
+
Expr::Ident(_) | Expr::Member(_) | Expr::This(_) => true,
|
|
260
|
+
Expr::OptChain(opt_chain) => matches!(&*opt_chain.base, OptChainBase::Member(_)),
|
|
261
|
+
_ => false,
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
fn expr_from_path(path: &[Box<Expr>]) -> Box<Expr> {
|
|
266
|
+
let mut expr = path[0].clone();
|
|
267
|
+
for segment in &path[1..] {
|
|
268
|
+
let prop = match &**segment {
|
|
269
|
+
Expr::Ident(id) => MemberProp::Ident(id.clone().into()),
|
|
270
|
+
Expr::Lit(Lit::Str(_)) => MemberProp::Computed(ComputedPropName {
|
|
271
|
+
span: DUMMY_SP,
|
|
272
|
+
expr: segment.clone(),
|
|
273
|
+
}),
|
|
274
|
+
_ => unreachable!("Unexpected property name type - expected Ident or Str"),
|
|
275
|
+
};
|
|
276
|
+
expr = Expr::Member(MemberExpr {
|
|
277
|
+
span: DUMMY_SP,
|
|
278
|
+
obj: expr,
|
|
279
|
+
prop,
|
|
280
|
+
})
|
|
281
|
+
.into();
|
|
282
|
+
}
|
|
283
|
+
expr
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
fn visit_member_expr(&mut self, n: &mut MemberExpr, optional: bool) {
|
|
287
|
+
let prop_is_computed = n.prop.is_computed();
|
|
288
|
+
let prop_is_computed_lit = prop_is_computed && n.prop.as_computed().unwrap().expr.is_lit();
|
|
289
|
+
let prop_is_computed_lit_str = prop_is_computed_lit
|
|
290
|
+
&& matches!(
|
|
291
|
+
n.prop.as_computed().unwrap().expr.as_lit().unwrap(),
|
|
292
|
+
Lit::Str(_)
|
|
293
|
+
);
|
|
294
|
+
|
|
295
|
+
if (prop_is_computed && !prop_is_computed_lit_str)
|
|
296
|
+
|| (n.prop.is_ident() && n.prop.as_ident().unwrap().sym == "current")
|
|
297
|
+
|| !Self::is_extractable_member_obj(&n.obj)
|
|
298
|
+
{
|
|
299
|
+
self.member_expr_depth = 0;
|
|
300
|
+
n.obj.visit_mut_with(self);
|
|
301
|
+
} else {
|
|
302
|
+
self.member_expr_depth += 1;
|
|
303
|
+
n.obj.visit_mut_with(self);
|
|
304
|
+
self.member_expr_depth = max(self.member_expr_depth, 1) - 1;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
let extracted_prop = match &n.prop {
|
|
308
|
+
MemberProp::Computed(comp) if prop_is_computed_lit_str => {
|
|
309
|
+
Some(Expr::Lit(comp.expr.as_lit().unwrap().clone()).into())
|
|
310
|
+
}
|
|
311
|
+
MemberProp::Computed(_) => {
|
|
312
|
+
n.prop.visit_mut_with(self);
|
|
313
|
+
None
|
|
314
|
+
}
|
|
315
|
+
MemberProp::Ident(ident) => Some(Expr::Ident(ident.clone().into()).into()),
|
|
316
|
+
_ => None,
|
|
317
|
+
};
|
|
318
|
+
|
|
319
|
+
if let Some(prop) = extracted_prop {
|
|
320
|
+
if !self.member_expr_path.is_empty() {
|
|
321
|
+
if optional {
|
|
322
|
+
let source = Self::expr_from_path(&self.member_expr_path);
|
|
323
|
+
if !self
|
|
324
|
+
.optional_member_sources
|
|
325
|
+
.iter()
|
|
326
|
+
.any(|existing| existing.eq_ignore_span(&source))
|
|
327
|
+
{
|
|
328
|
+
self.optional_member_sources.push(source);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
self.add_if_needed(prop);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
250
336
|
fn push_fn_scope(&mut self, ids_declared_in_scope: FxHashSet<Id>) {
|
|
251
337
|
let last_scope = self.scope_env.last().unwrap();
|
|
252
338
|
self.scope_env.push(ScopeEnv {
|
|
@@ -275,7 +361,7 @@ impl VisitMut for ExtractingIdentsCollector {
|
|
|
275
361
|
|
|
276
362
|
fn visit_mut_this_expr(&mut self, n: &mut ThisExpr) {
|
|
277
363
|
if self.member_expr_depth > 0 && !self.scope_env.last().unwrap().is_inner_fn_scope {
|
|
278
|
-
self.add_if_needed(Expr::This(*n).into()
|
|
364
|
+
self.add_if_needed(Expr::This(*n).into());
|
|
279
365
|
}
|
|
280
366
|
}
|
|
281
367
|
|
|
@@ -293,57 +379,21 @@ impl VisitMut for ExtractingIdentsCollector {
|
|
|
293
379
|
.iter()
|
|
294
380
|
.any(|env| env.ids_declared_locally.contains(&n.to_id()))
|
|
295
381
|
{
|
|
296
|
-
self.add_if_needed(
|
|
297
|
-
Expr::Ident(n.clone()).into(),
|
|
298
|
-
&Expr::Ident(n.clone()).into(),
|
|
299
|
-
);
|
|
382
|
+
self.add_if_needed(Expr::Ident(n.clone()).into());
|
|
300
383
|
}
|
|
301
384
|
|
|
302
385
|
n.visit_mut_children_with(self);
|
|
303
386
|
}
|
|
304
387
|
|
|
305
|
-
fn
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
&& matches!(
|
|
310
|
-
n.prop.as_computed().unwrap().expr.as_lit().unwrap(),
|
|
311
|
-
Lit::Str(_)
|
|
312
|
-
);
|
|
313
|
-
|
|
314
|
-
if (prop_is_computed && !prop_is_computed_lit_str)
|
|
315
|
-
|| (n.prop.is_ident() && n.prop.as_ident().unwrap().sym == "current")
|
|
316
|
-
|| !(n.obj.is_ident() || n.obj.is_member() || n.obj.is_this())
|
|
317
|
-
{
|
|
318
|
-
self.member_expr_depth = 0;
|
|
319
|
-
n.obj.visit_mut_with(self);
|
|
320
|
-
} else {
|
|
321
|
-
self.member_expr_depth += 1;
|
|
322
|
-
n.obj.visit_mut_with(self);
|
|
323
|
-
self.member_expr_depth = max(self.member_expr_depth, 1) - 1;
|
|
388
|
+
fn visit_mut_opt_chain_expr(&mut self, n: &mut OptChainExpr) {
|
|
389
|
+
match &mut *n.base {
|
|
390
|
+
OptChainBase::Member(member) => self.visit_member_expr(member, n.optional),
|
|
391
|
+
OptChainBase::Call(call) => call.visit_mut_with(self),
|
|
324
392
|
}
|
|
393
|
+
}
|
|
325
394
|
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
if prop_is_computed_lit_str {
|
|
329
|
-
if !self.member_expr_path.is_empty() {
|
|
330
|
-
self.add_if_needed(
|
|
331
|
-
Expr::Lit(comp.expr.as_lit().unwrap().clone()).into(),
|
|
332
|
-
&Expr::Member(n.clone()).into(),
|
|
333
|
-
);
|
|
334
|
-
}
|
|
335
|
-
} else {
|
|
336
|
-
n.prop.visit_mut_with(self);
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
MemberProp::Ident(ident) if !self.member_expr_path.is_empty() => {
|
|
340
|
-
self.add_if_needed(
|
|
341
|
-
Expr::Ident(ident.clone().into()).into(),
|
|
342
|
-
&Expr::Member(n.clone()).into(),
|
|
343
|
-
);
|
|
344
|
-
}
|
|
345
|
-
_ => {}
|
|
346
|
-
}
|
|
395
|
+
fn visit_mut_member_expr(&mut self, n: &mut MemberExpr) {
|
|
396
|
+
self.visit_member_expr(n, false);
|
|
347
397
|
}
|
|
348
398
|
|
|
349
399
|
/*
|