@pyreon/compiler 0.13.1 → 0.14.0
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/README.md +14 -4
- package/lib/analysis/index.js.html +1 -1
- package/lib/index.js +1113 -406
- package/lib/index.js.map +1 -1
- package/lib/types/index.d.ts +140 -14
- package/lib/types/index.d.ts.map +1 -1
- package/package.json +4 -1
- package/src/index.ts +10 -1
- package/src/jsx.ts +839 -782
- package/src/pyreon-intercept.ts +504 -0
- package/src/test-audit.ts +435 -0
- package/src/tests/depth-stress.test.ts +16 -0
- package/src/tests/detector-tag-consistency.test.ts +83 -0
- package/src/tests/jsx.test.ts +934 -0
- package/src/tests/native-equivalence.test.ts +654 -0
- package/src/tests/project-scanner.test.ts +30 -0
- package/src/tests/pyreon-intercept.test.ts +331 -0
- package/src/tests/react-intercept.test.ts +354 -0
- package/src/tests/test-audit.test.ts +549 -0
|
@@ -700,3 +700,357 @@ describe('diagnoseError', () => {
|
|
|
700
700
|
expect(diagnoseError('TypeError: Cannot freeze')).toBeNull()
|
|
701
701
|
})
|
|
702
702
|
})
|
|
703
|
+
|
|
704
|
+
// ─── Additional branch coverage for react-intercept ─────────────────────────
|
|
705
|
+
|
|
706
|
+
describe('detectReactPatterns — edge cases for branch coverage', () => {
|
|
707
|
+
test('useState with no arguments', () => {
|
|
708
|
+
const result = detectReactPatterns(`
|
|
709
|
+
import { useState } from 'react'
|
|
710
|
+
function App() {
|
|
711
|
+
const [value, setValue] = useState()
|
|
712
|
+
return <div>{value}</div>
|
|
713
|
+
}
|
|
714
|
+
`)
|
|
715
|
+
expect(result.length).toBeGreaterThan(0)
|
|
716
|
+
const useStateDiag = result.find((d) => d.code === 'use-state')
|
|
717
|
+
expect(useStateDiag).toBeDefined()
|
|
718
|
+
// No argument → init defaults to 'undefined'
|
|
719
|
+
expect(useStateDiag!.suggested).toContain('signal(undefined)')
|
|
720
|
+
})
|
|
721
|
+
|
|
722
|
+
test('useState not destructured (bare call)', () => {
|
|
723
|
+
const result = detectReactPatterns(`
|
|
724
|
+
import { useState } from 'react'
|
|
725
|
+
function App() {
|
|
726
|
+
const state = useState(0)
|
|
727
|
+
return <div>{state[0]}</div>
|
|
728
|
+
}
|
|
729
|
+
`)
|
|
730
|
+
expect(result.length).toBeGreaterThan(0)
|
|
731
|
+
const useStateDiag = result.find((d) => d.code === 'use-state')
|
|
732
|
+
expect(useStateDiag).toBeDefined()
|
|
733
|
+
// Non-destructured useState → generic suggestion
|
|
734
|
+
expect(useStateDiag!.suggested).toContain('signal(initialValue)')
|
|
735
|
+
})
|
|
736
|
+
|
|
737
|
+
test('useEffect with empty deps but no callback', () => {
|
|
738
|
+
const result = detectReactPatterns(`
|
|
739
|
+
import { useEffect } from 'react'
|
|
740
|
+
function App() {
|
|
741
|
+
useEffect(undefined, [])
|
|
742
|
+
return <div />
|
|
743
|
+
}
|
|
744
|
+
`)
|
|
745
|
+
// Should not crash, may or may not detect
|
|
746
|
+
expect(result).toBeDefined()
|
|
747
|
+
})
|
|
748
|
+
|
|
749
|
+
test('non-react import not flagged', () => {
|
|
750
|
+
const result = detectReactPatterns(`
|
|
751
|
+
import { signal } from '@pyreon/reactivity'
|
|
752
|
+
function App() {
|
|
753
|
+
const x = signal(0)
|
|
754
|
+
return <div>{x()}</div>
|
|
755
|
+
}
|
|
756
|
+
`)
|
|
757
|
+
expect(result).toHaveLength(0)
|
|
758
|
+
})
|
|
759
|
+
|
|
760
|
+
test('useEffect with arrow expression body (no block)', () => {
|
|
761
|
+
const result = detectReactPatterns(`
|
|
762
|
+
import { useEffect } from 'react'
|
|
763
|
+
function App() {
|
|
764
|
+
useEffect(() => console.log('hi'), [])
|
|
765
|
+
return <div />
|
|
766
|
+
}
|
|
767
|
+
`)
|
|
768
|
+
const effectDiag = result.find((d) => d.code === 'use-effect-mount')
|
|
769
|
+
expect(effectDiag).toBeDefined()
|
|
770
|
+
})
|
|
771
|
+
|
|
772
|
+
test('useEffect with non-function callback', () => {
|
|
773
|
+
const result = detectReactPatterns(`
|
|
774
|
+
import { useEffect } from 'react'
|
|
775
|
+
function App() {
|
|
776
|
+
useEffect(handler, [])
|
|
777
|
+
return <div />
|
|
778
|
+
}
|
|
779
|
+
`)
|
|
780
|
+
const effectDiag = result.find((d) => d.code === 'use-effect-mount')
|
|
781
|
+
expect(effectDiag).toBeDefined()
|
|
782
|
+
})
|
|
783
|
+
|
|
784
|
+
test('useEffect with deps array (non-empty)', () => {
|
|
785
|
+
const result = detectReactPatterns(`
|
|
786
|
+
import { useEffect } from 'react'
|
|
787
|
+
function App() {
|
|
788
|
+
useEffect(() => { console.log(x) }, [x])
|
|
789
|
+
return <div />
|
|
790
|
+
}
|
|
791
|
+
`)
|
|
792
|
+
const effectDiag = result.find((d) => d.code === 'use-effect-deps')
|
|
793
|
+
expect(effectDiag).toBeDefined()
|
|
794
|
+
})
|
|
795
|
+
|
|
796
|
+
test('useEffect with no deps array', () => {
|
|
797
|
+
const result = detectReactPatterns(`
|
|
798
|
+
import { useEffect } from 'react'
|
|
799
|
+
function App() {
|
|
800
|
+
useEffect(() => { console.log('every render') })
|
|
801
|
+
return <div />
|
|
802
|
+
}
|
|
803
|
+
`)
|
|
804
|
+
const effectDiag = result.find((d) => d.code === 'use-effect-no-deps')
|
|
805
|
+
expect(effectDiag).toBeDefined()
|
|
806
|
+
})
|
|
807
|
+
|
|
808
|
+
test('useMemo with no compute function arg', () => {
|
|
809
|
+
const result = detectReactPatterns(`
|
|
810
|
+
import { useMemo } from 'react'
|
|
811
|
+
function App() {
|
|
812
|
+
const x = useMemo()
|
|
813
|
+
return <div>{x}</div>
|
|
814
|
+
}
|
|
815
|
+
`)
|
|
816
|
+
const memoDiag = result.find((d) => d.code === 'use-memo')
|
|
817
|
+
expect(memoDiag).toBeDefined()
|
|
818
|
+
})
|
|
819
|
+
|
|
820
|
+
test('useCallback with no callback function arg', () => {
|
|
821
|
+
const result = detectReactPatterns(`
|
|
822
|
+
import { useCallback } from 'react'
|
|
823
|
+
function App() {
|
|
824
|
+
const fn = useCallback()
|
|
825
|
+
return <div onClick={fn}>click</div>
|
|
826
|
+
}
|
|
827
|
+
`)
|
|
828
|
+
const cbDiag = result.find((d) => d.code === 'use-callback')
|
|
829
|
+
expect(cbDiag).toBeDefined()
|
|
830
|
+
})
|
|
831
|
+
|
|
832
|
+
test('useRef with no argument (null ref)', () => {
|
|
833
|
+
const result = detectReactPatterns(`
|
|
834
|
+
import { useRef } from 'react'
|
|
835
|
+
function App() {
|
|
836
|
+
const ref = useRef()
|
|
837
|
+
return <div ref={ref}>text</div>
|
|
838
|
+
}
|
|
839
|
+
`)
|
|
840
|
+
const refDiag = result.find((d) => d.code === 'use-ref-dom' || d.code === 'use-ref-box')
|
|
841
|
+
expect(refDiag).toBeDefined()
|
|
842
|
+
})
|
|
843
|
+
|
|
844
|
+
test('memo() with no argument', () => {
|
|
845
|
+
const result = detectReactPatterns(`
|
|
846
|
+
import { memo } from 'react'
|
|
847
|
+
const App = memo()
|
|
848
|
+
`)
|
|
849
|
+
const memoDiag = result.find((d) => d.code === 'memo-wrapper')
|
|
850
|
+
expect(memoDiag).toBeDefined()
|
|
851
|
+
})
|
|
852
|
+
|
|
853
|
+
test('array.map in JSX detected', () => {
|
|
854
|
+
const result = detectReactPatterns(`
|
|
855
|
+
function App() {
|
|
856
|
+
return <ul>{items.map(item => <li>{item}</li>)}</ul>
|
|
857
|
+
}
|
|
858
|
+
`)
|
|
859
|
+
const mapDiag = result.find((d) => d.code === 'array-map-jsx')
|
|
860
|
+
expect(mapDiag).toBeDefined()
|
|
861
|
+
})
|
|
862
|
+
|
|
863
|
+
test('array.map in JSX with no callback argument', () => {
|
|
864
|
+
const result = detectReactPatterns(`
|
|
865
|
+
function App() {
|
|
866
|
+
return <ul>{items.map()}</ul>
|
|
867
|
+
}
|
|
868
|
+
`)
|
|
869
|
+
const mapDiag = result.find((d) => d.code === 'array-map-jsx')
|
|
870
|
+
expect(mapDiag).toBeDefined()
|
|
871
|
+
})
|
|
872
|
+
|
|
873
|
+
test('react-router-dom import detected', () => {
|
|
874
|
+
const result = detectReactPatterns(`
|
|
875
|
+
import { useNavigate, useParams } from 'react-router-dom'
|
|
876
|
+
`)
|
|
877
|
+
expect(result.some((d) => d.code === 'react-router-import')).toBe(true)
|
|
878
|
+
})
|
|
879
|
+
|
|
880
|
+
test('react-dom/client import detected', () => {
|
|
881
|
+
const result = detectReactPatterns(`
|
|
882
|
+
import { createRoot } from 'react-dom/client'
|
|
883
|
+
`)
|
|
884
|
+
expect(result.some((d) => d.code === 'react-dom-import')).toBe(true)
|
|
885
|
+
})
|
|
886
|
+
})
|
|
887
|
+
|
|
888
|
+
describe('migrateReactCode — edge cases for branch coverage', () => {
|
|
889
|
+
test('migrates useState without arguments', () => {
|
|
890
|
+
const result = migrateReactCode(`
|
|
891
|
+
import { useState } from 'react'
|
|
892
|
+
function App() {
|
|
893
|
+
const [count, setCount] = useState()
|
|
894
|
+
return <div>{count}</div>
|
|
895
|
+
}
|
|
896
|
+
`)
|
|
897
|
+
expect(result.code).toContain('signal(undefined)')
|
|
898
|
+
})
|
|
899
|
+
|
|
900
|
+
test('migrates code with no existing imports (inserts at top)', () => {
|
|
901
|
+
const result = migrateReactCode(`const [x, setX] = useState(0)`)
|
|
902
|
+
expect(result.code).toBeDefined()
|
|
903
|
+
})
|
|
904
|
+
|
|
905
|
+
test('dangerouslySetInnerHTML without expression', () => {
|
|
906
|
+
const result = migrateReactCode(`
|
|
907
|
+
import React from 'react'
|
|
908
|
+
function App() {
|
|
909
|
+
return <div dangerouslySetInnerHTML />
|
|
910
|
+
}
|
|
911
|
+
`)
|
|
912
|
+
// Should not crash, attr without value
|
|
913
|
+
expect(result.code).toBeDefined()
|
|
914
|
+
})
|
|
915
|
+
|
|
916
|
+
test('dangerouslySetInnerHTML with non-object expression', () => {
|
|
917
|
+
const result = migrateReactCode(`
|
|
918
|
+
import React from 'react'
|
|
919
|
+
function App() {
|
|
920
|
+
return <div dangerouslySetInnerHTML={getHtml()} />
|
|
921
|
+
}
|
|
922
|
+
`)
|
|
923
|
+
// Non-object expression → not migrated
|
|
924
|
+
expect(result.code).toBeDefined()
|
|
925
|
+
})
|
|
926
|
+
|
|
927
|
+
test('className attribute migrated to class', () => {
|
|
928
|
+
const result = migrateReactCode(`
|
|
929
|
+
import React from 'react'
|
|
930
|
+
function App() {
|
|
931
|
+
return <div className="foo">text</div>
|
|
932
|
+
}
|
|
933
|
+
`)
|
|
934
|
+
expect(result.code).toContain('class=')
|
|
935
|
+
expect(result.changes.length).toBeGreaterThan(0)
|
|
936
|
+
})
|
|
937
|
+
|
|
938
|
+
test('source file with import from non-react module not rewritten', () => {
|
|
939
|
+
const result = migrateReactCode(`
|
|
940
|
+
import { signal } from '@pyreon/reactivity'
|
|
941
|
+
const x = signal(0)
|
|
942
|
+
`)
|
|
943
|
+
expect(result.changes).toHaveLength(0)
|
|
944
|
+
})
|
|
945
|
+
|
|
946
|
+
test('migrates useRef with non-null initial value (mutable box)', () => {
|
|
947
|
+
const result = migrateReactCode(`
|
|
948
|
+
import { useRef } from 'react'
|
|
949
|
+
function App() {
|
|
950
|
+
const ref = useRef(42)
|
|
951
|
+
return <div>{ref.current}</div>
|
|
952
|
+
}
|
|
953
|
+
`)
|
|
954
|
+
expect(result.code).toContain('signal(42)')
|
|
955
|
+
})
|
|
956
|
+
|
|
957
|
+
test('migrates useMemo', () => {
|
|
958
|
+
const result = migrateReactCode(`
|
|
959
|
+
import { useMemo } from 'react'
|
|
960
|
+
function App() {
|
|
961
|
+
const doubled = useMemo(() => count * 2, [count])
|
|
962
|
+
return <div>{doubled}</div>
|
|
963
|
+
}
|
|
964
|
+
`)
|
|
965
|
+
expect(result.code).toContain('computed(')
|
|
966
|
+
})
|
|
967
|
+
|
|
968
|
+
test('migrates useCallback', () => {
|
|
969
|
+
const result = migrateReactCode(`
|
|
970
|
+
import { useCallback } from 'react'
|
|
971
|
+
function App() {
|
|
972
|
+
const handleClick = useCallback(() => console.log('click'), [])
|
|
973
|
+
return <button onClick={handleClick}>click</button>
|
|
974
|
+
}
|
|
975
|
+
`)
|
|
976
|
+
// useCallback → plain function (not needed in Pyreon)
|
|
977
|
+
expect(result.changes.length).toBeGreaterThan(0)
|
|
978
|
+
})
|
|
979
|
+
|
|
980
|
+
test('migrates React.memo wrapper', () => {
|
|
981
|
+
const result = migrateReactCode(`
|
|
982
|
+
import React from 'react'
|
|
983
|
+
const App = React.memo(function App() {
|
|
984
|
+
return <div>hello</div>
|
|
985
|
+
})
|
|
986
|
+
`)
|
|
987
|
+
expect(result.changes.length).toBeGreaterThan(0)
|
|
988
|
+
})
|
|
989
|
+
|
|
990
|
+
test('migrates standalone memo() wrapper', () => {
|
|
991
|
+
const result = migrateReactCode(`
|
|
992
|
+
import { memo } from 'react'
|
|
993
|
+
const App = memo(function App() {
|
|
994
|
+
return <div>hello</div>
|
|
995
|
+
})
|
|
996
|
+
`)
|
|
997
|
+
expect(result.changes.length).toBeGreaterThan(0)
|
|
998
|
+
})
|
|
999
|
+
|
|
1000
|
+
test('migrates code with no existing imports (inserts at beginning)', () => {
|
|
1001
|
+
// No import statement in the source — lastImportEnd === 0 → line 926 false branch
|
|
1002
|
+
const result = migrateReactCode(`
|
|
1003
|
+
const [count, setCount] = useState(0)
|
|
1004
|
+
const x = useMemo(() => count * 2)
|
|
1005
|
+
`)
|
|
1006
|
+
// Should insert pyreon imports at the top
|
|
1007
|
+
expect(result.code).toContain('import {')
|
|
1008
|
+
expect(result.code).toContain('@pyreon/')
|
|
1009
|
+
})
|
|
1010
|
+
|
|
1011
|
+
test('migrates dangerouslySetInnerHTML with __html property', () => {
|
|
1012
|
+
const result = migrateReactCode(`
|
|
1013
|
+
import React from 'react'
|
|
1014
|
+
function App() {
|
|
1015
|
+
return <div dangerouslySetInnerHTML={{ __html: '<b>bold</b>' }} />
|
|
1016
|
+
}
|
|
1017
|
+
`)
|
|
1018
|
+
expect(result.code).toContain('innerHTML')
|
|
1019
|
+
})
|
|
1020
|
+
|
|
1021
|
+
test('migrates className on JSX elements', () => {
|
|
1022
|
+
const result = migrateReactCode(`
|
|
1023
|
+
import React from 'react'
|
|
1024
|
+
function App() {
|
|
1025
|
+
return <div className="foo"><span className="bar">text</span></div>
|
|
1026
|
+
}
|
|
1027
|
+
`)
|
|
1028
|
+
expect(result.code).toContain('class=')
|
|
1029
|
+
expect(result.changes.length).toBeGreaterThanOrEqual(2)
|
|
1030
|
+
})
|
|
1031
|
+
|
|
1032
|
+
test('migrates htmlFor on label elements', () => {
|
|
1033
|
+
const result = migrateReactCode(`
|
|
1034
|
+
import React from 'react'
|
|
1035
|
+
function App() {
|
|
1036
|
+
return <label htmlFor="name">Name</label>
|
|
1037
|
+
}
|
|
1038
|
+
`)
|
|
1039
|
+
expect(result.code).toContain('for=')
|
|
1040
|
+
})
|
|
1041
|
+
|
|
1042
|
+
test('migrates useEffect with cleanup function', () => {
|
|
1043
|
+
const result = migrateReactCode(`
|
|
1044
|
+
import { useEffect } from 'react'
|
|
1045
|
+
function App() {
|
|
1046
|
+
useEffect(() => {
|
|
1047
|
+
const handler = () => {}
|
|
1048
|
+
window.addEventListener('resize', handler)
|
|
1049
|
+
return () => window.removeEventListener('resize', handler)
|
|
1050
|
+
}, [])
|
|
1051
|
+
return <div />
|
|
1052
|
+
}
|
|
1053
|
+
`)
|
|
1054
|
+
expect(result.code).toContain('onMount')
|
|
1055
|
+
})
|
|
1056
|
+
})
|