@samitouri / QOS-React-2 / commits / 68b0effca3

[rust] Workspace hygiene

I've primarily used what Cargo calls virtual workspaces, where the top-level Cargo.toml just lists a bunch of packages and they each have their own dependencies. This is fine, but i've noticed that more repos are using real workspaces and they offer a bunch of benefits. You define the dependencies in the top-level Cargo.toml and then can easily refer to them from multiple crates, ensuring all the versions match up. It makes it easier to refer to other crates in the workspace, too, because you define the path once at the root, then every other crate can just say `forget_foo = { workspace = true }`. I also renamed all the crates to be prefixed with `forget_`, in some cases removing the redundant `hir` name, So `hir-optimization` became `forget_optimization`, `hir-ssa` became `forget_ssa`. Also note the switch from hyphenated names to underscores everywhere, since at the end of the day you have to write the name with underscores in source code. I also deleted the demo crate that i started with since we don't need it anymore. And finally, i added an explicit publish = false to all the crates just to prevent mistakes.

Joe Savona committed Jul 14, 2023 at 11:39 UTC 68b0effca3dd3ef64edb98662dd7f8d049958e6f
94 files changed +1051 -1134
compiler/forget/Cargo.lock
+150 -284
@@ -233,18 +233,6 @@ dependencies = [
233 "serde",
234 ]
235
236 -[[package]]
237 -name = "build-hir"
238 -version = "0.1.0"
239 -dependencies = [
240 - "bumpalo",
241 - "estree",
242 - "hir",
243 - "indexmap 2.0.0",
244 - "miette 5.9.0",
245 - "thiserror",
246 -]
247 -
236 [[package]]
237 name = "bumpalo"
238 version = "3.13.0"
@@ -393,15 +381,6 @@ version = "1.8.1"
381 source = "registry+https://github.com/rust-lang/crates.io-index"
382 checksum = "7fcaabb2fef8c910e7f4c7ce9f67a1283a1715879a7c230ca9d6d1ae31f16d91"
383
396 -[[package]]
397 -name = "elsa"
398 -version = "1.8.1"
399 -source = "registry+https://github.com/rust-lang/crates.io-index"
400 -checksum = "b5e0aca8dce8856e420195bd13b6a64de3334235ccc9214e824b86b12bf26283"
401 -dependencies = [
402 - "stable_deref_trait",
403 -]
404 -
384 [[package]]
385 name = "encode_unicode"
386 version = "0.3.6"
@@ -456,12 +435,35 @@ dependencies = [
435 ]
436
437 [[package]]
459 -name = "estree"
438 +name = "fixedbitset"
439 +version = "0.4.2"
440 +source = "registry+https://github.com/rust-lang/crates.io-index"
441 +checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80"
442 +
443 +[[package]]
444 +name = "fnv"
445 +version = "1.0.7"
446 +source = "registry+https://github.com/rust-lang/crates.io-index"
447 +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
448 +
449 +[[package]]
450 +name = "forget_build_hir"
451 +version = "0.1.0"
452 +dependencies = [
453 + "bumpalo",
454 + "forget_estree",
455 + "forget_hir",
456 + "indexmap 2.0.0",
457 + "miette 5.9.0",
458 + "thiserror",
459 +]
460 +
461 +[[package]]
462 +name = "forget_estree"
463 version = "0.1.0"
464 dependencies = [
465 "bumpalo",
463 - "elsa",
464 - "estree-codegen",
466 + "forget_estree_codegen",
467 "insta",
468 "serde",
469 "serde_json",
@@ -469,7 +471,7 @@ dependencies = [
471 ]
472
473 [[package]]
472 -name = "estree-codegen"
474 +name = "forget_estree_codegen"
475 version = "0.1.0"
476 dependencies = [
477 "indexmap 2.0.0",
@@ -481,40 +483,74 @@ dependencies = [
483 ]
484
485 [[package]]
484 -name = "estree-swc"
486 +name = "forget_estree_swc"
487 version = "0.1.0"
488 dependencies = [
487 - "estree",
489 + "forget_estree",
490 "swc",
489 - "swc_core 0.79.9",
491 + "swc_core",
492 ]
493
494 [[package]]
493 -name = "fixedbitset"
494 -version = "0.4.2"
495 -source = "registry+https://github.com/rust-lang/crates.io-index"
496 -checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80"
495 +name = "forget_fixtures"
496 +version = "0.1.0"
497 +dependencies = [
498 + "bumpalo",
499 + "forget_build_hir",
500 + "forget_estree",
501 + "forget_estree_swc",
502 + "forget_hir",
503 + "forget_optimization",
504 + "forget_ssa",
505 + "insta",
506 + "miette 5.9.0",
507 +]
508
509 [[package]]
499 -name = "fixtures"
510 +name = "forget_hir"
511 version = "0.1.0"
512 dependencies = [
502 - "build-hir",
513 "bumpalo",
504 - "estree",
505 - "estree-swc",
506 - "hir",
507 - "hir-optimization",
508 - "hir-ssa",
509 - "insta",
514 + "forget_estree",
515 + "forget_utils",
516 + "indexmap 2.0.0",
517 + "serde",
518 +]
519 +
520 +[[package]]
521 +name = "forget_optimization"
522 +version = "0.1.0"
523 +dependencies = [
524 + "bumpalo",
525 + "forget_build_hir",
526 + "forget_estree",
527 + "forget_hir",
528 + "forget_ssa",
529 + "forget_utils",
530 + "indexmap 2.0.0",
531 "miette 5.9.0",
532 + "thiserror",
533 ]
534
535 [[package]]
514 -name = "fnv"
515 -version = "1.0.7"
516 -source = "registry+https://github.com/rust-lang/crates.io-index"
517 -checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
536 +name = "forget_ssa"
537 +version = "0.1.0"
538 +dependencies = [
539 + "bumpalo",
540 + "forget_hir",
541 + "forget_utils",
542 + "indexmap 2.0.0",
543 + "miette 5.9.0",
544 + "thiserror",
545 +]
546 +
547 +[[package]]
548 +name = "forget_utils"
549 +version = "0.1.0"
550 +dependencies = [
551 + "bumpalo",
552 + "stacker",
553 +]
554
555 [[package]]
556 name = "form_urlencoded"
@@ -630,44 +666,6 @@ version = "0.3.2"
666 source = "registry+https://github.com/rust-lang/crates.io-index"
667 checksum = "443144c8cdadd93ebf52ddb4056d257f5b52c04d3c804e657d19eb73fc33668b"
668
633 -[[package]]
634 -name = "hir"
635 -version = "0.1.0"
636 -dependencies = [
637 - "bumpalo",
638 - "estree",
639 - "indexmap 2.0.0",
640 - "serde",
641 - "utils",
642 -]
643 -
644 -[[package]]
645 -name = "hir-optimization"
646 -version = "0.1.0"
647 -dependencies = [
648 - "build-hir",
649 - "bumpalo",
650 - "estree",
651 - "hir",
652 - "hir-ssa",
653 - "indexmap 2.0.0",
654 - "miette 5.9.0",
655 - "thiserror",
656 - "utils",
657 -]
658 -
659 -[[package]]
660 -name = "hir-ssa"
661 -version = "0.1.0"
662 -dependencies = [
663 - "bumpalo",
664 - "hir",
665 - "indexmap 2.0.0",
666 - "miette 5.9.0",
667 - "thiserror",
668 - "utils",
669 -]
670 -
669 [[package]]
670 name = "iana-time-zone"
671 version = "0.1.57"
@@ -1783,20 +1781,20 @@ dependencies = [
1781 "swc_cached",
1782 "swc_common",
1783 "swc_config",
1786 - "swc_ecma_ast 0.107.0",
1784 + "swc_ecma_ast",
1785 "swc_ecma_codegen",
1786 "swc_ecma_ext_transforms",
1787 "swc_ecma_lints",
1788 "swc_ecma_loader",
1789 "swc_ecma_minifier",
1792 - "swc_ecma_parser 0.137.1",
1790 + "swc_ecma_parser",
1791 "swc_ecma_preset_env",
1792 "swc_ecma_transforms",
1795 - "swc_ecma_transforms_base 0.130.1",
1793 + "swc_ecma_transforms_base",
1794 "swc_ecma_transforms_compat",
1795 "swc_ecma_transforms_optimization",
1798 - "swc_ecma_utils 0.120.1",
1799 - "swc_ecma_visit 0.93.0",
1796 + "swc_ecma_utils",
1797 + "swc_ecma_visit",
1798 "swc_error_reporters",
1799 "swc_node_comments",
1800 "swc_timer",
@@ -1805,18 +1803,6 @@ dependencies = [
1803 "url",
1804 ]
1805
1808 -[[package]]
1809 -name = "swc-demo"
1810 -version = "0.1.0"
1811 -dependencies = [
1812 - "swc_common",
1813 - "swc_core 0.78.28",
1814 - "swc_ecma_parser 0.136.8",
1815 - "swc_ecma_transforms_base 0.129.15",
1816 - "swc_ecma_visit 0.92.5",
1817 - "swc_visit",
1818 -]
1819 -
1806 [[package]]
1807 name = "swc_atoms"
1808 version = "0.5.6"
@@ -1852,9 +1838,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
1838 checksum = "c6414bd4e553f5638961d39b07075ffd37a3d63176829592f4a5900260d94ca1"
1839 dependencies = [
1840 "ahash 0.8.3",
1855 - "anyhow",
1841 "ast_node",
1857 - "atty",
1842 "better_scoped_tls",
1843 "cfg-if",
1844 "either",
@@ -1871,7 +1855,6 @@ dependencies = [
1855 "swc_atoms",
1856 "swc_eq_ignore_macros",
1857 "swc_visit",
1874 - "termcolor",
1858 "tracing",
1859 "unicode-width",
1860 "url",
@@ -1902,15 +1885,6 @@ dependencies = [
1885 "syn 2.0.23",
1886 ]
1887
1905 -[[package]]
1906 -name = "swc_core"
1907 -version = "0.78.28"
1908 -source = "registry+https://github.com/rust-lang/crates.io-index"
1909 -checksum = "7e38b2fd17f97d84f1152eb82d7ebc089e98ddd02b5b750af37a0c91dec81c87"
1910 -dependencies = [
1911 - "vergen",
1912 -]
1913 -
1888 [[package]]
1889 name = "swc_core"
1890 version = "0.79.9"
@@ -1920,29 +1894,13 @@ dependencies = [
1894 "swc",
1895 "swc_atoms",
1896 "swc_common",
1923 - "swc_ecma_ast 0.107.0",
1924 - "swc_ecma_parser 0.137.1",
1925 - "swc_ecma_transforms_base 0.130.1",
1926 - "swc_ecma_visit 0.93.0",
1897 + "swc_ecma_ast",
1898 + "swc_ecma_parser",
1899 + "swc_ecma_transforms_base",
1900 + "swc_ecma_visit",
1901 "vergen",
1902 ]
1903
1930 -[[package]]
1931 -name = "swc_ecma_ast"
1932 -version = "0.106.6"
1933 -source = "registry+https://github.com/rust-lang/crates.io-index"
1934 -checksum = "ebf4d6804b1da4146c4c0359d129e3dd43568d321f69d7953d9abbca4ded76ba"
1935 -dependencies = [
1936 - "bitflags 2.3.3",
1937 - "is-macro",
1938 - "num-bigint",
1939 - "scoped-tls",
1940 - "string_enum",
1941 - "swc_atoms",
1942 - "swc_common",
1943 - "unicode-id",
1944 -]
1945 -
1904 [[package]]
1905 name = "swc_ecma_ast"
1906 version = "0.107.0"
@@ -1974,7 +1932,7 @@ dependencies = [
1932 "sourcemap",
1933 "swc_atoms",
1934 "swc_common",
1977 - "swc_ecma_ast 0.107.0",
1935 + "swc_ecma_ast",
1936 "swc_ecma_codegen_macros",
1937 "tracing",
1938 ]
@@ -2001,9 +1959,9 @@ dependencies = [
1959 "phf",
1960 "swc_atoms",
1961 "swc_common",
2004 - "swc_ecma_ast 0.107.0",
2005 - "swc_ecma_utils 0.120.1",
2006 - "swc_ecma_visit 0.93.0",
1962 + "swc_ecma_ast",
1963 + "swc_ecma_utils",
1964 + "swc_ecma_visit",
1965 ]
1966
1967 [[package]]
@@ -2022,9 +1980,9 @@ dependencies = [
1980 "swc_atoms",
1981 "swc_common",
1982 "swc_config",
2025 - "swc_ecma_ast 0.107.0",
2026 - "swc_ecma_utils 0.120.1",
2027 - "swc_ecma_visit 0.93.0",
1983 + "swc_ecma_ast",
1984 + "swc_ecma_utils",
1985 + "swc_ecma_visit",
1986 ]
1987
1988 [[package]]
@@ -2072,38 +2030,18 @@ dependencies = [
2030 "swc_cached",
2031 "swc_common",
2032 "swc_config",
2075 - "swc_ecma_ast 0.107.0",
2033 + "swc_ecma_ast",
2034 "swc_ecma_codegen",
2077 - "swc_ecma_parser 0.137.1",
2078 - "swc_ecma_transforms_base 0.130.1",
2035 + "swc_ecma_parser",
2036 + "swc_ecma_transforms_base",
2037 "swc_ecma_transforms_optimization",
2038 "swc_ecma_usage_analyzer",
2081 - "swc_ecma_utils 0.120.1",
2082 - "swc_ecma_visit 0.93.0",
2039 + "swc_ecma_utils",
2040 + "swc_ecma_visit",
2041 "swc_timer",
2042 "tracing",
2043 ]
2044
2087 -[[package]]
2088 -name = "swc_ecma_parser"
2089 -version = "0.136.8"
2090 -source = "registry+https://github.com/rust-lang/crates.io-index"
2091 -checksum = "45d40421c607d7a48334f78a9b24a5cbde1f36250f9986746ec082208d68b39f"
2092 -dependencies = [
2093 - "either",
2094 - "lexical",
2095 - "num-bigint",
2096 - "serde",
2097 - "smallvec",
2098 - "smartstring",
2099 - "stacker",
2100 - "swc_atoms",
2101 - "swc_common",
2102 - "swc_ecma_ast 0.106.6",
2103 - "tracing",
2104 - "typed-arena",
2105 -]
2106 -
2045 [[package]]
2046 name = "swc_ecma_parser"
2047 version = "0.137.1"
@@ -2119,7 +2057,7 @@ dependencies = [
2057 "stacker",
2058 "swc_atoms",
2059 "swc_common",
2122 - "swc_ecma_ast 0.107.0",
2060 + "swc_ecma_ast",
2061 "tracing",
2062 "typed-arena",
2063 ]
@@ -2143,10 +2081,10 @@ dependencies = [
2081 "string_enum",
2082 "swc_atoms",
2083 "swc_common",
2146 - "swc_ecma_ast 0.107.0",
2084 + "swc_ecma_ast",
2085 "swc_ecma_transforms",
2148 - "swc_ecma_utils 0.120.1",
2149 - "swc_ecma_visit 0.93.0",
2086 + "swc_ecma_utils",
2087 + "swc_ecma_visit",
2088 ]
2089
2090 [[package]]
@@ -2157,39 +2095,16 @@ checksum = "a4f578a16ff264c1f306d38a10b900256b33f636426ebeb5eab980205e8fc9f5"
2095 dependencies = [
2096 "swc_atoms",
2097 "swc_common",
2160 - "swc_ecma_ast 0.107.0",
2161 - "swc_ecma_transforms_base 0.130.1",
2098 + "swc_ecma_ast",
2099 + "swc_ecma_transforms_base",
2100 "swc_ecma_transforms_compat",
2101 "swc_ecma_transforms_module",
2102 "swc_ecma_transforms_optimization",
2103 "swc_ecma_transforms_proposal",
2104 "swc_ecma_transforms_react",
2105 "swc_ecma_transforms_typescript",
2168 - "swc_ecma_utils 0.120.1",
2169 - "swc_ecma_visit 0.93.0",
2170 -]
2171 -
2172 -[[package]]
2173 -name = "swc_ecma_transforms_base"
2174 -version = "0.129.15"
2175 -source = "registry+https://github.com/rust-lang/crates.io-index"
2176 -checksum = "7707e724db259cd93150fd5bc04559ace997edbce968d29be9c881e317c9b3fb"
2177 -dependencies = [
2178 - "better_scoped_tls",
2179 - "bitflags 2.3.3",
2180 - "indexmap 1.9.3",
2181 - "once_cell",
2182 - "phf",
2183 - "rustc-hash",
2184 - "serde",
2185 - "smallvec",
2186 - "swc_atoms",
2187 - "swc_common",
2188 - "swc_ecma_ast 0.106.6",
2189 - "swc_ecma_parser 0.136.8",
2190 - "swc_ecma_utils 0.119.10",
2191 - "swc_ecma_visit 0.92.5",
2192 - "tracing",
2106 + "swc_ecma_utils",
2107 + "swc_ecma_visit",
2108 ]
2109
2110 [[package]]
@@ -2208,10 +2123,10 @@ dependencies = [
2123 "smallvec",
2124 "swc_atoms",
2125 "swc_common",
2211 - "swc_ecma_ast 0.107.0",
2212 - "swc_ecma_parser 0.137.1",
2213 - "swc_ecma_utils 0.120.1",
2214 - "swc_ecma_visit 0.93.0",
2126 + "swc_ecma_ast",
2127 + "swc_ecma_parser",
2128 + "swc_ecma_utils",
2129 + "swc_ecma_visit",
2130 "tracing",
2131 ]
2132
@@ -2223,10 +2138,10 @@ checksum = "a288e0d1a702f1944df7c1b57468f505a9549d7d7a51d89dfa18d8f956abdbf5"
2138 dependencies = [
2139 "swc_atoms",
2140 "swc_common",
2226 - "swc_ecma_ast 0.107.0",
2227 - "swc_ecma_transforms_base 0.130.1",
2228 - "swc_ecma_utils 0.120.1",
2229 - "swc_ecma_visit 0.93.0",
2141 + "swc_ecma_ast",
2142 + "swc_ecma_transforms_base",
2143 + "swc_ecma_utils",
2144 + "swc_ecma_visit",
2145 ]
2146
2147 [[package]]
@@ -2245,12 +2160,12 @@ dependencies = [
2160 "swc_atoms",
2161 "swc_common",
2162 "swc_config",
2248 - "swc_ecma_ast 0.107.0",
2249 - "swc_ecma_transforms_base 0.130.1",
2163 + "swc_ecma_ast",
2164 + "swc_ecma_transforms_base",
2165 "swc_ecma_transforms_classes",
2166 "swc_ecma_transforms_macros",
2252 - "swc_ecma_utils 0.120.1",
2253 - "swc_ecma_visit 0.93.0",
2167 + "swc_ecma_utils",
2168 + "swc_ecma_visit",
2169 "swc_trace_macro",
2170 "tracing",
2171 ]
@@ -2287,12 +2202,12 @@ dependencies = [
2202 "swc_atoms",
2203 "swc_cached",
2204 "swc_common",
2290 - "swc_ecma_ast 0.107.0",
2205 + "swc_ecma_ast",
2206 "swc_ecma_loader",
2292 - "swc_ecma_parser 0.137.1",
2293 - "swc_ecma_transforms_base 0.130.1",
2294 - "swc_ecma_utils 0.120.1",
2295 - "swc_ecma_visit 0.93.0",
2207 + "swc_ecma_parser",
2208 + "swc_ecma_transforms_base",
2209 + "swc_ecma_utils",
2210 + "swc_ecma_visit",
2211 "tracing",
2212 ]
2213
@@ -2311,12 +2226,12 @@ dependencies = [
2226 "serde_json",
2227 "swc_atoms",
2228 "swc_common",
2314 - "swc_ecma_ast 0.107.0",
2315 - "swc_ecma_parser 0.137.1",
2316 - "swc_ecma_transforms_base 0.130.1",
2229 + "swc_ecma_ast",
2230 + "swc_ecma_parser",
2231 + "swc_ecma_transforms_base",
2232 "swc_ecma_transforms_macros",
2318 - "swc_ecma_utils 0.120.1",
2319 - "swc_ecma_visit 0.93.0",
2233 + "swc_ecma_utils",
2234 + "swc_ecma_visit",
2235 "swc_fast_graph",
2236 "tracing",
2237 ]
@@ -2333,12 +2248,12 @@ dependencies = [
2248 "smallvec",
2249 "swc_atoms",
2250 "swc_common",
2336 - "swc_ecma_ast 0.107.0",
2337 - "swc_ecma_transforms_base 0.130.1",
2251 + "swc_ecma_ast",
2252 + "swc_ecma_transforms_base",
2253 "swc_ecma_transforms_classes",
2254 "swc_ecma_transforms_macros",
2340 - "swc_ecma_utils 0.120.1",
2341 - "swc_ecma_visit 0.93.0",
2255 + "swc_ecma_utils",
2256 + "swc_ecma_visit",
2257 ]
2258
2259 [[package]]
@@ -2358,12 +2273,12 @@ dependencies = [
2273 "swc_atoms",
2274 "swc_common",
2275 "swc_config",
2361 - "swc_ecma_ast 0.107.0",
2362 - "swc_ecma_parser 0.137.1",
2363 - "swc_ecma_transforms_base 0.130.1",
2276 + "swc_ecma_ast",
2277 + "swc_ecma_parser",
2278 + "swc_ecma_transforms_base",
2279 "swc_ecma_transforms_macros",
2365 - "swc_ecma_utils 0.120.1",
2366 - "swc_ecma_visit 0.93.0",
2280 + "swc_ecma_utils",
2281 + "swc_ecma_visit",
2282 ]
2283
2284 [[package]]
@@ -2375,11 +2290,11 @@ dependencies = [
2290 "serde",
2291 "swc_atoms",
2292 "swc_common",
2378 - "swc_ecma_ast 0.107.0",
2379 - "swc_ecma_transforms_base 0.130.1",
2293 + "swc_ecma_ast",
2294 + "swc_ecma_transforms_base",
2295 "swc_ecma_transforms_react",
2381 - "swc_ecma_utils 0.120.1",
2382 - "swc_ecma_visit 0.93.0",
2296 + "swc_ecma_utils",
2297 + "swc_ecma_visit",
2298 ]
2299
2300 [[package]]
@@ -2393,31 +2308,13 @@ dependencies = [
2308 "rustc-hash",
2309 "swc_atoms",
2310 "swc_common",
2396 - "swc_ecma_ast 0.107.0",
2397 - "swc_ecma_utils 0.120.1",
2398 - "swc_ecma_visit 0.93.0",
2311 + "swc_ecma_ast",
2312 + "swc_ecma_utils",
2313 + "swc_ecma_visit",
2314 "swc_timer",
2315 "tracing",
2316 ]
2317
2403 -[[package]]
2404 -name = "swc_ecma_utils"
2405 -version = "0.119.10"
2406 -source = "registry+https://github.com/rust-lang/crates.io-index"
2407 -checksum = "452c66399edeb88a97bfdc3bbf11e45db85fdf883bfd4fc8bdd93abb92152b9b"
2408 -dependencies = [
2409 - "indexmap 1.9.3",
2410 - "num_cpus",
2411 - "once_cell",
2412 - "rustc-hash",
2413 - "swc_atoms",
2414 - "swc_common",
2415 - "swc_ecma_ast 0.106.6",
2416 - "swc_ecma_visit 0.92.5",
2417 - "tracing",
2418 - "unicode-id",
2419 -]
2420 -
2318 [[package]]
2319 name = "swc_ecma_utils"
2320 version = "0.120.1"
@@ -2430,26 +2327,12 @@ dependencies = [
2327 "rustc-hash",
2328 "swc_atoms",
2329 "swc_common",
2433 - "swc_ecma_ast 0.107.0",
2434 - "swc_ecma_visit 0.93.0",
2330 + "swc_ecma_ast",
2331 + "swc_ecma_visit",
2332 "tracing",
2333 "unicode-id",
2334 ]
2335
2439 -[[package]]
2440 -name = "swc_ecma_visit"
2441 -version = "0.92.5"
2442 -source = "registry+https://github.com/rust-lang/crates.io-index"
2443 -checksum = "0f61da6cac0ec3b7e62d367cfbd9e38e078a4601271891ad94f0dac5ff69f839"
2444 -dependencies = [
2445 - "num-bigint",
2446 - "swc_atoms",
2447 - "swc_common",
2448 - "swc_ecma_ast 0.106.6",
2449 - "swc_visit",
2450 - "tracing",
2451 -]
2452 -
2336 [[package]]
2337 name = "swc_ecma_visit"
2338 version = "0.93.0"
@@ -2459,7 +2342,7 @@ dependencies = [
2342 "num-bigint",
2343 "swc_atoms",
2344 "swc_common",
2462 - "swc_ecma_ast 0.107.0",
2345 + "swc_ecma_ast",
2346 "swc_visit",
2347 "tracing",
2348 ]
@@ -2591,15 +2474,6 @@ dependencies = [
2474 "unicode-ident",
2475 ]
2476
2594 -[[package]]
2595 -name = "termcolor"
2596 -version = "1.2.0"
2597 -source = "registry+https://github.com/rust-lang/crates.io-index"
2598 -checksum = "be55cf8942feac5c765c2c993422806843c9a9a45d4d5c407ad6dd2ea95eb9b6"
2599 -dependencies = [
2600 - "winapi-util",
2601 -]
2602 -
2477 [[package]]
2478 name = "terminal_size"
2479 version = "0.1.17"
@@ -2802,14 +2676,6 @@ dependencies = [
2676 "percent-encoding",
2677 ]
2678
2805 -[[package]]
2806 -name = "utils"
2807 -version = "0.1.0"
2808 -dependencies = [
2809 - "bumpalo",
2810 - "stacker",
2811 -]
2812 -
2679 [[package]]
2680 name = "vergen"
2681 version = "7.5.1"
compiler/forget/Cargo.toml
+49 -12
@@ -1,17 +1,54 @@
1 [workspace]
2 resolver = "2"
3 -members = [
4 - "crates/build-hir",
5 - "crates/fixtures",
6 - "crates/hir",
7 - "crates/hir-ssa",
8 - "crates/hir-optimization",
9 - "crates/swc-demo",
10 - "crates/estree",
11 - "crates/estree-codegen",
12 - "crates/estree-swc",
13 - "crates/utils"
14 -]
3 +members = ["crates/*"]
4 +
5 +[workspace.package]
6 +authors = ["The React Team https://react.dev/community/team"]
7 +description = "React Forget Compiler"
8 +edition = "2021"
9 +homepage = "https://github.com/facebook/react-forget"
10 +keywords = ["JavaScript", "TypeScript", "React", "React Forget", "Forget", "Compiler"]
11 +license = "MIT"
12 +repository = "https://github.com/facebook/react-forget"
13 +
14 +[workspace.dependencies]
15 +# workspace crates
16 +forget_build_hir = { path = "crates/forget_build_hir" }
17 +forget_estree = { path = "crates/forget_estree" }
18 +forget_estree_codegen = { path = "crates/forget_estree_codegen" }
19 +forget_estree_swc = { path = "crates/forget_estree_swc" }
20 +forget_fixtures = { path = "crates/forget_fixtures" }
21 +forget_hir = { path = "crates/forget_hir" }
22 +forget_optimization = { path = "crates/forget_optimization" }
23 +forget_ssa = { path = "crates/forget_ssa" }
24 +forget_swc_demo = { path = "crates/forget_swc_demo" }
25 +forget_utils = { path = "crates/forget_utils" }
26 +
27 +# dependencies
28 +bumpalo = { version = "3.13.0", features = ["boxed", "collections"] }
29 +indexmap = { version = "2.0.0", features = ["serde"] }
30 +insta = { version = "1.30.0", features = ["glob"] }
31 +miette = { version = "5.9.0" }
32 +prettyplease = "0.2.10"
33 +quote = "1.0.29"
34 +serde = { version = "1.0.167", features = ["serde_derive"] }
35 +serde_json = "1.0.100"
36 +stacker = "0.1.15"
37 +static_assertions = "1.1.0"
38 +swc = "0.264.8"
39 +swc_core = { version = "0.79.9", features = ["swc_ecma_visit", "__ecma_transforms", "swc_ecma_ast", "swc_common", "swc", "swc_ecma_parser", "__ecma", "__common", "ecma_ast", "__visit", "__parser"] }
40 +syn = "2.0.23"
41 +thiserror = "1.0.41"
42 +
43 +[profile.release]
44 +# configuration adapted from oxc
45 +# https://github.com/Boshen/oxc/blob/ea85ee9f2d64dd284c5b7410f491d81fb879abae/Cargo.toml#L89-L97
46 +opt-level = 3
47 +lto = "fat"
48 +codegen-units = 1
49 +strip = "symbols"
50 +debug = false
51 +panic = "abort" # Let it crash and force ourselves to write safe Rust.
52
53 # Make insta run faster by compiling with release mode optimizations
54 # https://docs.rs/insta/latest/insta/#optional-faster-runs
compiler/forget/crates/build-hir/Cargo.toml deleted
-14
@@ -1,14 +0,0 @@
1 -[package]
2 -name = "build-hir"
3 -version = "0.1.0"
4 -edition = "2021"
5 -
6 -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7 -
8 -[dependencies]
9 -hir = { path = "../hir" }
10 -estree = { path = "../estree" }
11 -indexmap = "2.0.0"
12 -bumpalo = "3.13.0"
13 -miette = { version = "5.9.0" }
14 -thiserror = "1.0.41"
compiler/forget/crates/estree-codegen/Cargo.toml deleted
-14
@@ -1,14 +0,0 @@
1 -[package]
2 -name = "estree-codegen"
3 -version = "0.1.0"
4 -edition = "2021"
5 -
6 -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7 -
8 -[dependencies]
9 -indexmap = { version = "2.0.0", features = ["serde"] }
10 -prettyplease = "0.2.10"
11 -quote = "1.0.29"
12 -serde = { version = "1.0.167", features = ["serde_derive"] }
13 -serde_json = "1.0.100"
14 -syn = "2.0.23"
compiler/forget/crates/estree-swc/Cargo.toml deleted
-11
@@ -1,11 +0,0 @@
1 -[package]
2 -name = "estree-swc"
3 -version = "0.1.0"
4 -edition = "2021"
5 -
6 -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7 -
8 -[dependencies]
9 -estree = { path = "../estree" }
10 -swc = "0.264.8"
11 -swc_core = { version = "0.79.9", features = ["swc_ecma_visit", "__ecma_transforms", "swc_ecma_ast", "swc_common", "swc", "swc_ecma_parser", "__ecma", "__common", "ecma_ast", "__visit", "__parser"] }
compiler/forget/crates/estree-swc/src/lib.rs deleted
-522
@@ -1,522 +0,0 @@
1 -use std::{io::stderr, num::NonZeroU32, sync::Arc};
2 -
3 -use estree::{Binding, BindingId};
4 -use swc::Compiler;
5 -use swc_core::common::errors::Handler;
6 -use swc_core::common::source_map::Pos;
7 -use swc_core::common::{FileName, FilePathMapping, Mark, SourceMap, Span, SyntaxContext, GLOBALS};
8 -use swc_core::ecma::ast::{
9 - AssignOp, BinaryOp, BlockStmt, Decl, EsVersion, Expr, Function, Ident, Lit, MemberExpr,
10 - MemberProp, ModuleItem, Pat, PatOrExpr, Program, Stmt, UnaryOp, VarDecl, VarDeclKind,
11 - VarDeclOrExpr,
12 -};
13 -use swc_core::ecma::parser::Syntax;
14 -use swc_core::ecma::transforms::base::resolver;
15 -use swc_core::ecma::visit::FoldWith;
16 -
17 -/// Parses source text into an estree::Program via SWC, internally performing the parsing
18 -/// and SWC -> ESTree conversion.
19 -pub fn parse(source: &str, file: &str) -> Result<estree::Program, Box<dyn std::error::Error>> {
20 - GLOBALS.set(&Default::default(), || {
21 - let cm = Arc::new(SourceMap::new(FilePathMapping::empty()));
22 - let c = Compiler::new(cm);
23 - let fm =
24 - c.cm.new_source_file(FileName::Real(file.into()), source.to_string());
25 -
26 - let handler = Handler::with_emitter_writer(Box::new(stderr()), Some(c.cm.clone()));
27 -
28 - let comments = c.comments().clone();
29 - let module = c.parse_js(
30 - fm.clone(),
31 - &handler,
32 - EsVersion::Es5,
33 - Syntax::Typescript(Default::default()),
34 - swc::config::IsModule::Bool(true),
35 - Some(&comments),
36 - )?;
37 -
38 - let context = Context {
39 - top_level_mark: Mark::new(),
40 - unresolved_mark: Mark::new(),
41 - };
42 -
43 - let module = c.run_transform(&handler, false, || {
44 - module.fold_with(&mut resolver(
45 - context.unresolved_mark,
46 - context.top_level_mark,
47 - true,
48 - ))
49 - });
50 -
51 - Ok(convert_program(&context, &module))
52 - })
53 -}
54 -
55 -#[derive(Debug)]
56 -struct Context {
57 - unresolved_mark: Mark,
58 - top_level_mark: Mark,
59 -}
60 -
61 -fn convert_program(cx: &Context, program: &Program) -> estree::Program {
62 - let mut program_items: Vec<estree::ModuleItem>;
63 - match program {
64 - Program::Module(program) => {
65 - let body = &program.body;
66 - program_items = Vec::with_capacity(body.len());
67 - for item in body {
68 - program_items.push(convert_module_item(cx, item));
69 - }
70 - }
71 - Program::Script(program) => {
72 - let body = &program.body;
73 - program_items = Vec::with_capacity(body.len());
74 - for item in body {
75 - program_items.push(estree::ModuleItem::Statement(convert_statement(cx, item)));
76 - }
77 - }
78 - };
79 - estree::Program {
80 - source_type: if program.is_script() {
81 - estree::SourceType::Script
82 - } else {
83 - estree::SourceType::Module
84 - },
85 - body: program_items,
86 - // comments: None,
87 - loc: None,
88 - range: None,
89 - }
90 -}
91 -
92 -fn convert_module_item(cx: &Context, item: &ModuleItem) -> estree::ModuleItem {
93 - match item {
94 - ModuleItem::Stmt(item) => estree::ModuleItem::Statement(convert_statement(cx, item)),
95 - _ => todo!("translate module item {:#?}", item),
96 - }
97 -}
98 -
99 -fn convert_span(span: &Span) -> Option<estree::SourceRange> {
100 - Some(estree::SourceRange {
101 - start: span.lo().to_u32(),
102 - end: NonZeroU32::new(span.hi().to_u32())?,
103 - })
104 -}
105 -
106 -fn convert_decl_kind(kind: &VarDeclKind) -> estree::VariableDeclarationKind {
107 - match kind {
108 - VarDeclKind::Const => estree::VariableDeclarationKind::Const,
109 - VarDeclKind::Let => estree::VariableDeclarationKind::Let,
110 - VarDeclKind::Var => estree::VariableDeclarationKind::Var,
111 - }
112 -}
113 -
114 -fn convert_block_statement(cx: &Context, stmt: &BlockStmt) -> estree::BlockStatement {
115 - let mut body: Vec<estree::Statement> = Vec::with_capacity(stmt.stmts.len());
116 - for stmt in &stmt.stmts {
117 - body.push(convert_statement(cx, stmt));
118 - }
119 - estree::BlockStatement {
120 - body,
121 - loc: None,
122 - range: convert_span(&stmt.span),
123 - }
124 -}
125 -
126 -fn convert_function(cx: &Context, id: Option<&Ident>, fun: &Function) -> estree::Function {
127 - estree::Function {
128 - id: id.map(|id| estree::Identifier {
129 - name: id.sym.to_string(),
130 - binding: convert_binding(cx, id.span.ctxt),
131 - loc: None,
132 - range: convert_span(&id.span),
133 - }),
134 - params: fun
135 - .params
136 - .iter()
137 - .map(|param| convert_pattern(cx, &param.pat))
138 - .collect(),
139 - body: fun.body.as_ref().map(|body| {
140 - estree::FunctionBody::BlockStatement(Box::new(convert_block_statement(cx, body)))
141 - }),
142 - is_async: fun.is_async,
143 - is_generator: fun.is_generator,
144 - loc: None,
145 - range: convert_span(&fun.span),
146 - }
147 -}
148 -
149 -fn convert_statement(cx: &Context, stmt: &Stmt) -> estree::Statement {
150 - match stmt {
151 - Stmt::Decl(Decl::Fn(item)) => {
152 - estree::Statement::FunctionDeclaration(Box::new(estree::FunctionDeclaration {
153 - function: convert_function(cx, Some(&item.ident), &item.function),
154 - loc: None,
155 - range: convert_span(&item.function.span),
156 - }))
157 - }
158 - Stmt::Decl(Decl::Var(item)) => {
159 - estree::Statement::VariableDeclaration(Box::new(convert_variable_declaration(cx, item)))
160 - }
161 - Stmt::Block(item) => {
162 - estree::Statement::BlockStatement(Box::new(convert_block_statement(cx, item)))
163 - }
164 - Stmt::Break(item) => estree::Statement::BreakStatement(Box::new(estree::BreakStatement {
165 - label: item
166 - .label
167 - .as_ref()
168 - .map(|label| convert_identifier(cx, label)),
169 - loc: None,
170 - range: convert_span(&item.span),
171 - })),
172 - Stmt::Continue(item) => {
173 - estree::Statement::ContinueStatement(Box::new(estree::ContinueStatement {
174 - label: item
175 - .label
176 - .as_ref()
177 - .map(|label| convert_identifier(cx, label)),
178 - loc: None,
179 - range: convert_span(&item.span),
180 - }))
181 - }
182 - Stmt::Debugger(item) => {
183 - estree::Statement::DebuggerStatement(Box::new(estree::DebuggerStatement {
184 - loc: None,
185 - range: convert_span(&item.span),
186 - }))
187 - }
188 - Stmt::DoWhile(item) => {
189 - estree::Statement::DoWhileStatement(Box::new(estree::DoWhileStatement {
190 - body: convert_statement(cx, &item.body),
191 - test: convert_expression(cx, &item.test),
192 - loc: None,
193 - range: convert_span(&item.span),
194 - }))
195 - }
196 - Stmt::Empty(item) => estree::Statement::EmptyStatement(Box::new(estree::EmptyStatement {
197 - loc: None,
198 - range: convert_span(&item.span),
199 - })),
200 - Stmt::Expr(item) => {
201 - let expression = convert_expression(cx, &item.expr);
202 - estree::Statement::ExpressionStatement(Box::new(estree::ExpressionStatement {
203 - expression,
204 - directive: None,
205 - loc: None,
206 - range: convert_span(&item.span),
207 - }))
208 - }
209 - Stmt::For(item) => estree::Statement::ForStatement(Box::new(estree::ForStatement {
210 - init: item.init.as_ref().map(|init| match init {
211 - VarDeclOrExpr::Expr(init) => {
212 - estree::ForInit::Expression(convert_expression(cx, init))
213 - }
214 - VarDeclOrExpr::VarDecl(init) => {
215 - assert_eq!(init.decls.len(), 1);
216 - estree::ForInit::VariableDeclaration(Box::new(convert_variable_declaration(
217 - cx, init,
218 - )))
219 - }
220 - }),
221 - test: item.test.as_ref().map(|test| convert_expression(cx, test)),
222 - update: item
223 - .update
224 - .as_ref()
225 - .map(|update| convert_expression(cx, update)),
226 - body: convert_statement(cx, &item.body),
227 - loc: None,
228 - range: convert_span(&item.span),
229 - })),
230 - Stmt::Return(item) => {
231 - estree::Statement::ReturnStatement(Box::new(estree::ReturnStatement {
232 - argument: item.arg.as_ref().map(|arg| convert_expression(cx, arg)),
233 - loc: None,
234 - range: convert_span(&item.span),
235 - }))
236 - }
237 - Stmt::Throw(item) => estree::Statement::ThrowStatement(Box::new(estree::ThrowStatement {
238 - argument: convert_expression(cx, &item.arg),
239 - loc: None,
240 - range: convert_span(&item.span),
241 - })),
242 - Stmt::If(item) => estree::Statement::IfStatement(Box::new(estree::IfStatement {
243 - test: convert_expression(cx, &item.test),
244 - consequent: convert_statement(cx, &item.cons),
245 - alternate: item.alt.as_ref().map(|alt| convert_statement(cx, alt)),
246 - loc: None,
247 - range: convert_span(&item.span),
248 - })),
249 - _ => todo!("translate statement {:#?}", stmt),
250 - }
251 -}
252 -
253 -fn convert_variable_declaration(cx: &Context, decl: &VarDecl) -> estree::VariableDeclaration {
254 - estree::VariableDeclaration {
255 - kind: convert_decl_kind(&decl.kind),
256 - declarations: decl
257 - .decls
258 - .iter()
259 - .map(|declarator| estree::VariableDeclarator {
260 - id: convert_pattern(cx, &declarator.name),
261 - init: declarator
262 - .init
263 - .as_ref()
264 - .map(|init| convert_expression(cx, init)),
265 - loc: None,
266 - range: convert_span(&decl.span),
267 - })
268 - .collect(),
269 - loc: None,
270 - range: convert_span(&decl.span),
271 - }
272 -}
273 -
274 -fn convert_expression(cx: &Context, expr: &Expr) -> estree::Expression {
275 - match expr {
276 - Expr::Ident(expr) => estree::Expression::Identifier(Box::new(convert_identifier(cx, expr))),
277 - Expr::Array(expr) => {
278 - estree::Expression::ArrayExpression(Box::new(estree::ArrayExpression {
279 - elements: expr
280 - .elems
281 - .iter()
282 - .map(|item| {
283 - // TODO: represent holes in array expressions
284 - let value = item.as_ref()?;
285 - match value.spread {
286 - Some(spread) => Some(estree::ExpressionOrSpread::SpreadElement(
287 - Box::new(estree::SpreadElement {
288 - argument: convert_expression(cx, &value.expr),
289 - loc: None,
290 - range: convert_span(&spread),
291 - }),
292 - )),
293 - None => Some(estree::ExpressionOrSpread::Expression(
294 - convert_expression(cx, &value.expr),
295 - )),
296 - }
297 - })
298 - .collect(),
299 - loc: None,
300 - range: convert_span(&expr.span),
301 - }))
302 - }
303 - Expr::Await(_expr) => {
304 - // estree::Expression::AwaitExpression(Box::new(estree::AwaitExpression {
305 - // argument: convert_expression(cx, &expr.arg),
306 - // loc: None,
307 - // range: convert_span(&expr.span),
308 - // }))
309 - todo!("await expression")
310 - }
311 - Expr::Unary(expr) => {
312 - estree::Expression::UnaryExpression(Box::new(estree::UnaryExpression {
313 - operator: convert_unary_operator(expr.op),
314 - prefix: false,
315 - argument: convert_expression(cx, &expr.arg),
316 - loc: None,
317 - range: convert_span(&expr.span),
318 - }))
319 - }
320 - Expr::Bin(expr) => match convert_binary_operator(expr.op) {
321 - Operator::Binary(op) => {
322 - estree::Expression::BinaryExpression(Box::new(estree::BinaryExpression {
323 - operator: op,
324 - left: convert_expression(cx, &expr.left),
325 - right: convert_expression(cx, &expr.right),
326 - loc: None,
327 - range: convert_span(&expr.span),
328 - }))
329 - }
330 - Operator::Logical(op) => {
331 - estree::Expression::LogicalExpression(Box::new(estree::LogicalExpression {
332 - operator: op,
333 - left: convert_expression(cx, &expr.left),
334 - right: convert_expression(cx, &expr.right),
335 - loc: None,
336 - range: convert_span(&expr.span),
337 - }))
338 - }
339 - },
340 - Expr::Lit(expr) => {
341 - let (value, range) = match expr {
342 - Lit::Bool(expr) => (estree::JsValue::Bool(expr.value), convert_span(&expr.span)),
343 - Lit::Num(expr) => (
344 - estree::JsValue::Number(expr.value.into()),
345 - convert_span(&expr.span),
346 - ),
347 - Lit::Str(expr) => (
348 - estree::JsValue::String(expr.value.to_string()),
349 - convert_span(&expr.span),
350 - ),
351 - Lit::Null(expr) => (estree::JsValue::Null, convert_span(&expr.span)),
352 - _ => todo!(),
353 - };
354 - estree::Expression::Literal(Box::new(estree::Literal {
355 - value,
356 - raw: None,
357 - loc: None,
358 - regex: None,
359 - range,
360 - }))
361 - }
362 - Expr::Assign(expr) => {
363 - estree::Expression::AssignmentExpression(Box::new(estree::AssignmentExpression {
364 - operator: convert_assignment_operator(expr.op),
365 - left: convert_assignment_target(cx, &expr.left),
366 - right: convert_expression(cx, &expr.right),
367 - loc: None,
368 - range: convert_span(&expr.span),
369 - }))
370 - }
371 - Expr::Member(expr) => {
372 - estree::Expression::MemberExpression(Box::new(convert_member_expression(cx, expr)))
373 - }
374 - Expr::Fn(expr) => {
375 - estree::Expression::FunctionExpression(Box::new(estree::FunctionExpression {
376 - function: convert_function(cx, expr.ident.as_ref(), &expr.function),
377 - loc: None,
378 - range: convert_span(&expr.function.span),
379 - }))
380 - }
381 - _ => todo!("translate expression {:#?}", expr),
382 - }
383 -}
384 -
385 -fn convert_assignment_target(cx: &Context, target: &PatOrExpr) -> estree::AssignmentTarget {
386 - match target {
387 - PatOrExpr::Pat(target) => estree::AssignmentTarget::Pattern(convert_pattern(cx, target)),
388 - PatOrExpr::Expr(target) => {
389 - match target.as_ref() {
390 - Expr::Member(target) => {
391 - estree::AssignmentTarget::Expression(estree::Expression::MemberExpression(
392 - Box::new(convert_member_expression(cx, target)),
393 - ))
394 - }
395 - Expr::Ident(target) => estree::AssignmentTarget::Pattern(
396 - estree::Pattern::Identifier(Box::new(convert_identifier(cx, target))),
397 - ),
398 - _ => {
399 - panic!("Expected assignment target to be member expression or identifier, got {:#?}", target)
400 - }
401 - }
402 - }
403 - }
404 -}
405 -
406 -fn convert_member_expression(cx: &Context, expr: &MemberExpr) -> estree::MemberExpression {
407 - let (is_computed, property) = match &expr.prop {
408 - MemberProp::Ident(prop) => (
409 - false,
410 - estree::Expression::Identifier(Box::new(convert_identifier(cx, prop))),
411 - ),
412 - MemberProp::Computed(prop) => (true, convert_expression(cx, &prop.expr)),
413 - _ => {
414 - panic!("PrivateName member expression properties are not supported")
415 - }
416 - };
417 - estree::MemberExpression {
418 - object: estree::ExpressionOrSuper::Expression(convert_expression(cx, &expr.obj)),
419 - property,
420 - computed: is_computed,
421 - // optional: false, // TODO
422 - loc: None,
423 - range: convert_span(&expr.span),
424 - }
425 -}
426 -
427 -fn convert_unary_operator(op: UnaryOp) -> estree::UnaryOperator {
428 - match op {
429 - UnaryOp::Bang => estree::UnaryOperator::Negation,
430 - UnaryOp::Delete => estree::UnaryOperator::Delete,
431 - UnaryOp::Minus => estree::UnaryOperator::Minus,
432 - UnaryOp::Plus => estree::UnaryOperator::Plus,
433 - UnaryOp::Tilde => estree::UnaryOperator::Tilde,
434 - UnaryOp::TypeOf => estree::UnaryOperator::Typeof,
435 - UnaryOp::Void => estree::UnaryOperator::Void,
436 - }
437 -}
438 -
439 -fn convert_assignment_operator(op: AssignOp) -> estree::AssignmentOperator {
440 - match op {
441 - AssignOp::Assign => estree::AssignmentOperator::Equals,
442 - AssignOp::AddAssign => estree::AssignmentOperator::PlusEquals,
443 - _ => todo!("translate assignment operator"),
444 - }
445 -}
446 -
447 -enum Operator {
448 - Binary(estree::BinaryOperator),
449 - Logical(estree::LogicalOperator),
450 -}
451 -
452 -fn convert_binary_operator(op: BinaryOp) -> Operator {
453 - match op {
454 - BinaryOp::Add => Operator::Binary(estree::BinaryOperator::Add),
455 - BinaryOp::BitAnd => Operator::Binary(estree::BinaryOperator::BinaryAnd),
456 - BinaryOp::BitOr => Operator::Binary(estree::BinaryOperator::BinaryOr),
457 - BinaryOp::BitXor => Operator::Binary(estree::BinaryOperator::BinaryXor),
458 - BinaryOp::Div => Operator::Binary(estree::BinaryOperator::Divide),
459 - BinaryOp::EqEq => Operator::Binary(estree::BinaryOperator::Equals),
460 - BinaryOp::EqEqEq => Operator::Binary(estree::BinaryOperator::StrictEquals),
461 - // BinaryOp::Exp => Operator::Binary(estree::BinaryOperator::AsteriskAsterisk),
462 - BinaryOp::Gt => Operator::Binary(estree::BinaryOperator::GreaterThan),
463 - BinaryOp::GtEq => Operator::Binary(estree::BinaryOperator::GreaterThanOrEqual),
464 - BinaryOp::In => Operator::Binary(estree::BinaryOperator::In),
465 - BinaryOp::InstanceOf => Operator::Binary(estree::BinaryOperator::Instanceof),
466 - BinaryOp::LShift => Operator::Binary(estree::BinaryOperator::ShiftLeft),
467 - BinaryOp::Lt => Operator::Binary(estree::BinaryOperator::LessThan),
468 - BinaryOp::LtEq => Operator::Binary(estree::BinaryOperator::LessThanOrEqual),
469 - BinaryOp::Mod => Operator::Binary(estree::BinaryOperator::Modulo),
470 - BinaryOp::Mul => Operator::Binary(estree::BinaryOperator::Multiply),
471 - BinaryOp::NotEq => Operator::Binary(estree::BinaryOperator::NotEquals),
472 - BinaryOp::NotEqEq => Operator::Binary(estree::BinaryOperator::NotStrictEquals),
473 - BinaryOp::RShift => Operator::Binary(estree::BinaryOperator::ShiftRight),
474 - BinaryOp::Sub => Operator::Binary(estree::BinaryOperator::Subtract),
475 - BinaryOp::ZeroFillRShift => Operator::Binary(estree::BinaryOperator::UnsignedShiftRight),
476 -
477 - BinaryOp::LogicalAnd => Operator::Logical(estree::LogicalOperator::And),
478 - BinaryOp::LogicalOr => Operator::Logical(estree::LogicalOperator::Or),
479 - BinaryOp::NullishCoalescing => Operator::Logical(estree::LogicalOperator::NullCoalescing),
480 -
481 - _ => panic!("Unsupported binary operator `{}`", op),
482 - }
483 -}
484 -
485 -fn convert_pattern(cx: &Context, pat: &Pat) -> estree::Pattern {
486 - match pat {
487 - Pat::Ident(pat) => estree::Pattern::Identifier(Box::new(estree::Identifier {
488 - name: pat.id.sym.to_string(),
489 - binding: convert_binding(cx, pat.id.span.ctxt),
490 - loc: None,
491 - range: convert_span(&pat.span),
492 - })),
493 - _ => todo!("translate pattern {:#?}", pat),
494 - }
495 -}
496 -
497 -fn convert_binding(context: &Context, binding_cx: SyntaxContext) -> Option<Binding> {
498 - let id = BindingId::new(binding_cx.as_u32());
499 - if binding_cx.as_u32() == context.top_level_mark.as_u32() {
500 - Some(Binding::Global)
501 - } else if binding_cx.as_u32() == context.unresolved_mark.as_u32() {
502 - Some(Binding::Module(id))
503 - } else {
504 - Some(Binding::Local(id))
505 - }
506 -}
507 -
508 -fn convert_identifier(cx: &Context, identifier: &Ident) -> estree::Identifier {
509 - let name = identifier.sym.as_ref().to_string();
510 - estree::Identifier {
511 - name,
512 - binding: convert_binding(cx, identifier.span.ctxt),
513 - loc: None,
514 - range: convert_span(&identifier.span),
515 - }
516 -}
517 -
518 -#[cfg(test)]
519 -mod tests {
520 - #[test]
521 - fn it_works() {}
522 -}
compiler/forget/crates/estree/Cargo.toml deleted
-17
@@ -1,17 +0,0 @@
1 -[package]
2 -name = "estree"
3 -version = "0.1.0"
4 -edition = "2021"
5 -
6 -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7 -
8 -[dependencies]
9 -bumpalo = { version = "3.13.0", features = ["boxed", "collections"] }
10 -elsa = "1.8.1"
11 -insta = { version = "1.30.0", features = ["glob"] }
12 -serde = { version = "1.0.164", features = ["derive"] }
13 -serde_json = "1.0.99"
14 -static_assertions = "1.1.0"
15 -
16 -[build-dependencies]
17 -estree-codegen = { path = "../estree-codegen" }
\ No newline at end of file
compiler/forget/crates/estree/build.rs deleted
-13
@@ -1,13 +0,0 @@
1 -use estree_codegen::estree;
2 -
3 -// Example custom build script.
4 -fn main() {
5 - // Re-run if the codegen files change
6 - println!("cargo:rerun-if-changed=../estree-codegen/src/codegen.rs");
7 - println!("cargo:rerun-if-changed=../estree-codegen/src/lib.rs");
8 - println!("cargo:rerun-if-changed=../estree-codegen/src/ecmascript.json");
9 - println!("cargo:rerun-if-changed=../estree-codegen");
10 -
11 - let src = estree();
12 - std::fs::write("src/generated.rs", src).unwrap();
13 -}
compiler/forget/crates/fixtures/Cargo.toml deleted
-18
@@ -1,18 +0,0 @@
1 -[package]
2 -name = "fixtures"
3 -version = "0.1.0"
4 -edition = "2021"
5 -publish = false
6 -
7 -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8 -
9 -[dependencies]
10 -insta = "1.30.0"
11 -estree = { path = "../estree" }
12 -estree-swc = { path = "../estree-swc" }
13 -hir = { path = "../hir" }
14 -hir-optimization = { path = "../hir-optimization" }
15 -hir-ssa = { path = "../hir-ssa" }
16 -build-hir = { path = "../build-hir" }
17 -bumpalo = { version = "3.13.0", features = ["collections"] }
18 -miette = { version = "5.9.0", features = ["backtrace", "fancy"] }
compiler/forget/crates/forget_build_hir/Cargo.toml new
+21
@@ -0,0 +1,21 @@
1 +[package]
2 +name = "forget_build_hir"
3 +version = "0.1.0"
4 +publish = false
5 +authors.workspace = true
6 +description.workspace = true
7 +edition.workspace = true
8 +homepage.workspace = true
9 +keywords.workspace = true
10 +license.workspace = true
11 +repository.workspace = true
12 +
13 +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
14 +
15 +[dependencies]
16 +forget_hir = { workspace = true }
17 +forget_estree = { workspace = true}
18 +indexmap = { workspace = true }
19 +bumpalo = { workspace = true }
20 +miette = { workspace = true }
21 +thiserror = { workspace = true }
compiler/forget/crates/forget_build_hir/README.md renamed
compiler/forget/crates/forget_build_hir/src/build.rs renamed
+30 -30
@@ -1,12 +1,12 @@
1 use std::collections::HashSet;
2
3 use bumpalo::{boxed::Box, collections::String};
4 -use estree::{
4 +use forget_estree::{
5 AssignmentTarget, BinaryExpression, BlockStatement, Expression, ForInit, ForStatement,
6 Function, FunctionExpression, IfStatement, JsValue, Literal, Pattern, Statement,
7 VariableDeclarationKind,
8 };
9 -use hir::{
9 +use forget_hir::{
10 ArrayElement, BlockKind, BranchTerminal, Environment, ForTerminal, GotoKind, IdentifierOperand,
11 InstrIx, InstructionKind, InstructionValue, LValue, LoadGlobal, LoadLocal, Operand,
12 PrimitiveValue, TerminalValue,
@@ -28,14 +28,14 @@ use crate::{
28 pub fn build<'a>(
29 env: &'a Environment<'a>,
30 fun: Function,
31 -) -> Result<Box<'a, hir::Function<'a>>, BuildDiagnostic> {
31 +) -> Result<Box<'a, forget_hir::Function<'a>>, BuildDiagnostic> {
32 let mut builder = Builder::new(env);
33
34 match fun.body {
35 - Some(estree::FunctionBody::BlockStatement(body)) => {
35 + Some(forget_estree::FunctionBody::BlockStatement(body)) => {
36 lower_block_statement(env, &mut builder, *body)?
37 }
38 - Some(estree::FunctionBody::Expression(body)) => {
38 + Some(forget_estree::FunctionBody::Expression(body)) => {
39 lower_expression(env, &mut builder, body)?;
40 }
41 None => {
@@ -65,21 +65,21 @@ pub fn build<'a>(
65 // In case the function did not explicitly return, terminate the final
66 // block with an explicit `return undefined`. If the function *did* return,
67 // this will be unreachable and get pruned later.
68 - let implicit_return_value = builder.push(InstructionValue::Primitive(hir::Primitive {
68 + let implicit_return_value = builder.push(InstructionValue::Primitive(forget_hir::Primitive {
69 value: PrimitiveValue::Undefined,
70 }));
71 builder.terminate(
72 - TerminalValue::Return(hir::ReturnTerminal {
72 + TerminalValue::Return(forget_hir::ReturnTerminal {
73 value: Operand {
74 ix: implicit_return_value,
75 effect: None,
76 },
77 }),
78 - hir::BlockKind::Block,
78 + forget_hir::BlockKind::Block,
79 );
80
81 let body = builder.build()?;
82 - Ok(env.box_new(hir::Function {
82 + Ok(env.box_new(forget_hir::Function {
83 id: fun
84 .id
85 .map(|id| String::from_str_in(&id.name, &env.allocator)),
@@ -118,7 +118,7 @@ fn lower_statement<'a>(
118 Statement::BreakStatement(stmt) => {
119 let block = builder.resolve_break(stmt.label.as_ref())?;
120 builder.terminate(
121 - TerminalValue::Goto(hir::GotoTerminal {
121 + TerminalValue::Goto(forget_hir::GotoTerminal {
122 block,
123 kind: GotoKind::Break,
124 }),
@@ -128,7 +128,7 @@ fn lower_statement<'a>(
128 Statement::ContinueStatement(stmt) => {
129 let block = builder.resolve_continue(stmt.label.as_ref())?;
130 builder.terminate(
131 - TerminalValue::Goto(hir::GotoTerminal {
131 + TerminalValue::Goto(forget_hir::GotoTerminal {
132 block,
133 kind: GotoKind::Continue,
134 }),
@@ -138,12 +138,12 @@ fn lower_statement<'a>(
138 Statement::ReturnStatement(stmt) => {
139 let ix = match stmt.argument {
140 Some(argument) => lower_expression(env, builder, argument)?,
141 - None => builder.push(InstructionValue::Primitive(hir::Primitive {
141 + None => builder.push(InstructionValue::Primitive(forget_hir::Primitive {
142 value: PrimitiveValue::Undefined,
143 })),
144 };
145 builder.terminate(
146 - TerminalValue::Return(hir::ReturnTerminal {
146 + TerminalValue::Return(forget_hir::ReturnTerminal {
147 value: Operand { ix, effect: None },
148 }),
149 BlockKind::Block,
@@ -191,7 +191,7 @@ fn lower_statement<'a>(
191 ));
192 }
193 };
194 - builder.push(InstructionValue::DeclareLocal(hir::DeclareLocal {
194 + builder.push(InstructionValue::DeclareLocal(forget_hir::DeclareLocal {
195 lvalue: LValue {
196 identifier: IdentifierOperand {
197 identifier,
@@ -218,7 +218,7 @@ fn lower_statement<'a>(
218
219 let consequent_block = builder.enter(BlockKind::Block, |builder| {
220 lower_statement(env, builder, consequent, None)?;
221 - Ok(TerminalValue::Goto(hir::GotoTerminal {
221 + Ok(TerminalValue::Goto(forget_hir::GotoTerminal {
222 block: fallthrough_block.id,
223 kind: GotoKind::Break,
224 }))
@@ -228,14 +228,14 @@ fn lower_statement<'a>(
228 if let Some(alternate) = alternate {
229 lower_statement(env, builder, alternate, None)?;
230 }
231 - Ok(TerminalValue::Goto(hir::GotoTerminal {
231 + Ok(TerminalValue::Goto(forget_hir::GotoTerminal {
232 block: fallthrough_block.id,
233 kind: GotoKind::Break,
234 }))
235 })?;
236
237 let test = lower_expression(env, builder, test)?;
238 - let terminal = TerminalValue::If(hir::IfTerminal {
238 + let terminal = TerminalValue::If(forget_hir::IfTerminal {
239 test: Operand {
240 ix: test,
241 effect: None,
@@ -264,7 +264,7 @@ fn lower_statement<'a>(
264 let init_block = builder.enter(BlockKind::Loop, |builder| {
265 if let Some(ForInit::VariableDeclaration(decl)) = init {
266 lower_statement(env, builder, Statement::VariableDeclaration(decl), None)?;
267 - Ok(TerminalValue::Goto(hir::GotoTerminal {
267 + Ok(TerminalValue::Goto(forget_hir::GotoTerminal {
268 block: test_block.id,
269 kind: GotoKind::Break,
270 }))
@@ -281,7 +281,7 @@ fn lower_statement<'a>(
281 .map(|update| {
282 builder.enter(BlockKind::Loop, |builder| {
283 lower_expression(env, builder, update)?;
284 - Ok(TerminalValue::Goto(hir::GotoTerminal {
284 + Ok(TerminalValue::Goto(forget_hir::GotoTerminal {
285 block: test_block.id,
286 kind: GotoKind::Break,
287 }))
@@ -297,7 +297,7 @@ fn lower_statement<'a>(
297 };
298 builder.enter_loop(loop_, |builder| {
299 lower_statement(env, builder, body, None)?;
300 - Ok(TerminalValue::Goto(hir::GotoTerminal {
300 + Ok(TerminalValue::Goto(forget_hir::GotoTerminal {
301 block: update_block.unwrap_or(test_block.id),
302 kind: GotoKind::Continue,
303 }))
@@ -363,20 +363,20 @@ fn lower_expression<'a>(
363 }),
364 }
365 }
366 - Expression::Literal(expr) => InstructionValue::Primitive(hir::Primitive {
366 + Expression::Literal(expr) => InstructionValue::Primitive(forget_hir::Primitive {
367 value: lower_primitive(env, builder, *expr),
368 }),
369 Expression::ArrayExpression(expr) => {
370 let mut elements = env.vec_with_capacity(expr.elements.len());
371 for expr in expr.elements {
372 let element = match expr {
373 - Some(estree::ExpressionOrSpread::SpreadElement(expr)) => {
373 + Some(forget_estree::ExpressionOrSpread::SpreadElement(expr)) => {
374 Some(ArrayElement::Spread(Operand {
375 ix: lower_expression(env, builder, expr.argument)?,
376 effect: None,
377 }))
378 }
379 - Some(estree::ExpressionOrSpread::Expression(expr)) => {
379 + Some(forget_estree::ExpressionOrSpread::Expression(expr)) => {
380 Some(ArrayElement::Place(Operand {
381 ix: lower_expression(env, builder, expr)?,
382 effect: None,
@@ -386,11 +386,11 @@ fn lower_expression<'a>(
386 };
387 elements.push(element);
388 }
389 - InstructionValue::Array(hir::Array { elements })
389 + InstructionValue::Array(forget_hir::Array { elements })
390 }
391
392 Expression::AssignmentExpression(expr) => match expr.operator {
393 - estree::AssignmentOperator::Equals => {
393 + forget_estree::AssignmentOperator::Equals => {
394 let right = lower_expression(env, builder, expr.right)?;
395 return Ok(lower_assignment(
396 env,
@@ -412,7 +412,7 @@ fn lower_expression<'a>(
412 } = *expr;
413 let left = lower_expression(env, builder, left)?;
414 let right = lower_expression(env, builder, right)?;
415 - InstructionValue::Binary(hir::Binary {
415 + InstructionValue::Binary(forget_hir::Binary {
416 left: Operand {
417 ix: left,
418 effect: None,
@@ -438,7 +438,7 @@ fn lower_function<'a>(
438 env: &'a Environment<'a>,
439 builder: &mut Builder<'a>,
440 expr: FunctionExpression,
441 -) -> Result<hir::FunctionExpression<'a>, BuildDiagnostic> {
441 +) -> Result<forget_hir::FunctionExpression<'a>, BuildDiagnostic> {
442 let FunctionExpression { function, .. } = expr;
443 println!("get_context_identifiers() ...");
444 let context_identifiers = get_context_identifiers(env, &function);
@@ -461,7 +461,7 @@ fn lower_function<'a>(
461 }
462 let mut fun = build(env, function)?;
463 fun.context = context;
464 - Ok(hir::FunctionExpression {
464 + Ok(forget_hir::FunctionExpression {
465 // TODO: collect dependencies!
466 dependencies: env.vec_new(),
467 lowered_function: fun,
@@ -479,7 +479,7 @@ fn lower_assignment<'a>(
479 AssignmentTarget::Pattern(lvalue) => match lvalue {
480 Pattern::Identifier(lvalue) => {
481 let identifier = lower_identifier_for_assignment(env, builder, kind, *lvalue)?;
482 - builder.push(InstructionValue::StoreLocal(hir::StoreLocal {
482 + builder.push(InstructionValue::StoreLocal(forget_hir::StoreLocal {
483 lvalue: LValue { identifier, kind },
484 value: Operand {
485 ix: value,
@@ -497,7 +497,7 @@ fn lower_identifier_for_assignment<'a>(
497 _env: &'a Environment<'a>,
498 builder: &mut Builder<'a>,
499 _kind: InstructionKind,
500 - identifier: estree::Identifier,
500 + identifier: forget_estree::Identifier,
501 ) -> Result<IdentifierOperand<'a>, BuildDiagnostic> {
502 let binding = builder.resolve_identifier(&identifier)?;
503 match binding {
compiler/forget/crates/forget_build_hir/src/builder.rs renamed
+11 -11
@@ -4,7 +4,7 @@ use bumpalo::{
4 };
5 use std::{cell::RefCell, collections::HashSet, rc::Rc};
6
7 -use hir::{
7 +use forget_hir::{
8 BasicBlock, BlockId, BlockKind, Environment, GotoKind, Identifier, IdentifierData, InstrIx,
9 Instruction, InstructionIdGenerator, InstructionValue, Terminal, TerminalValue, Type, HIR,
10 };
@@ -197,7 +197,7 @@ impl<'a> Builder<'a> {
197 Err(error) => (
198 Err(error),
199 // TODO: add a `Terminal::Error` variant
200 - TerminalValue::Goto(hir::GotoTerminal {
200 + TerminalValue::Goto(forget_hir::GotoTerminal {
201 block: current.id,
202 kind: GotoKind::Break,
203 }),
@@ -242,8 +242,8 @@ impl<'a> Builder<'a> {
242 /// we synthesize a temporary identifier to store the possibly-missing value
243 /// into, and emit a later StoreLocal for the original identifier
244 #[allow(dead_code)]
245 - pub(crate) fn make_temporary(&self) -> hir::Identifier<'a> {
246 - hir::Identifier {
245 + pub(crate) fn make_temporary(&self) -> forget_hir::Identifier<'a> {
246 + forget_hir::Identifier {
247 id: self.environment.next_identifier_id(),
248 name: None,
249 data: Rc::new(RefCell::new(IdentifierData {
@@ -259,7 +259,7 @@ impl<'a> Builder<'a> {
259 /// provided but cannot be resolved.
260 pub(crate) fn resolve_break(
261 &self,
262 - label: Option<&estree::Identifier>,
262 + label: Option<&forget_estree::Identifier>,
263 ) -> Result<BlockId, BuildDiagnostic> {
264 for scope in self.scopes.iter().rev() {
265 match (label, scope.label()) {
@@ -285,7 +285,7 @@ impl<'a> Builder<'a> {
285 /// provided but cannot be resolved.
286 pub(crate) fn resolve_continue(
287 &self,
288 - label: Option<&estree::Identifier>,
288 + label: Option<&forget_estree::Identifier>,
289 ) -> Result<BlockId, BuildDiagnostic> {
290 for scope in self.scopes.iter().rev() {
291 match scope {
@@ -327,16 +327,16 @@ impl<'a> Builder<'a> {
327
328 pub(crate) fn resolve_identifier(
329 &mut self,
330 - identifier: &estree::Identifier,
330 + identifier: &forget_estree::Identifier,
331 ) -> Result<Binding<'a>, BuildDiagnostic> {
332 match &identifier.binding {
333 Some(binding) => Ok(match binding {
334 - estree::Binding::Global => Binding::Global,
335 - estree::Binding::Local(id) => Binding::Local(
334 + forget_estree::Binding::Global => Binding::Global,
335 + forget_estree::Binding::Local(id) => Binding::Local(
336 self.environment
337 .resolve_binding_identifier(&identifier.name, *id),
338 ),
339 - estree::Binding::Module(id) => Binding::Module(
339 + forget_estree::Binding::Module(id) => Binding::Module(
340 self.environment
341 .resolve_binding_identifier(&identifier.name, *id),
342 ),
@@ -454,7 +454,7 @@ pub fn remove_unreachable_do_while_statements<'a>(hir: &mut HIR<'a>) {
454 for block in hir.blocks.values_mut() {
455 if let TerminalValue::DoWhile(terminal) = &mut block.terminal.value {
456 if !block_ids.contains(&terminal.test) {
457 - block.terminal.value = TerminalValue::Goto(hir::GotoTerminal {
457 + block.terminal.value = TerminalValue::Goto(forget_hir::GotoTerminal {
458 block: terminal.body,
459 kind: GotoKind::Break,
460 });
compiler/forget/crates/forget_build_hir/src/context.rs renamed
+2 -2
@@ -1,7 +1,7 @@
1 use std::collections::HashSet;
2
3 -use estree::{Binding, BindingId, Expression, ExpressionOrSuper, Function, Identifier, Visitor};
4 -use hir::Environment;
3 +use forget_estree::{Binding, BindingId, Function, Identifier, Visitor};
4 +use forget_hir::Environment;
5
6 pub(crate) fn get_context_identifiers<'a, 'ast>(
7 _env: &'a Environment<'a>,
compiler/forget/crates/forget_build_hir/src/error.rs renamed
+2 -2
@@ -1,5 +1,5 @@
1 -use estree::SourceRange;
2 -use hir::BlockId;
1 +use forget_estree::SourceRange;
2 +use forget_hir::BlockId;
3 use miette::{ByteOffset, Diagnostic, SourceSpan};
4 use thiserror::Error;
5
compiler/forget/crates/forget_build_hir/src/lib.rs renamed
compiler/forget/crates/forget_estree/Cargo.toml new
+23
@@ -0,0 +1,23 @@
1 +[package]
2 +name = "forget_estree"
3 +version = "0.1.0"
4 +publish = false
5 +authors.workspace = true
6 +description.workspace = true
7 +edition.workspace = true
8 +homepage.workspace = true
9 +keywords.workspace = true
10 +license.workspace = true
11 +repository.workspace = true
12 +
13 +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
14 +
15 +[dependencies]
16 +bumpalo = { workspace = true }
17 +insta = { workspace = true }
18 +serde = { workspace = true }
19 +serde_json = { workspace = true }
20 +static_assertions = { workspace = true }
21 +
22 +[build-dependencies]
23 +forget_estree_codegen = { workspace = true }
\ No newline at end of file
compiler/forget/crates/forget_estree/build.rs new
+13
@@ -0,0 +1,13 @@
1 +use forget_estree_codegen::estree;
2 +
3 +// Example custom build script.
4 +fn main() {
5 + // Re-run if the codegen files change
6 + println!("cargo:rerun-if-changed=../forget_estree_codegen/src/codegen.rs");
7 + println!("cargo:rerun-if-changed=../forget_estree_codegen/src/lib.rs");
8 + println!("cargo:rerun-if-changed=../forget_estree_codegen/src/ecmascript.json");
9 + println!("cargo:rerun-if-changed=../forget_estree_codegen");
10 +
11 + let src = estree();
12 + std::fs::write("src/generated.rs", src).unwrap();
13 +}
compiler/forget/crates/forget_estree/src/binding.rs renamed
compiler/forget/crates/forget_estree/src/fixtures/for-statement.json renamed
compiler/forget/crates/forget_estree/src/fixtures/import.json renamed
compiler/forget/crates/forget_estree/src/fixtures/simple.json renamed
compiler/forget/crates/forget_estree/src/fixtures/test.json renamed
compiler/forget/crates/forget_estree/src/generated.rs renamed
compiler/forget/crates/forget_estree/src/generated_extensions.rs renamed
compiler/forget/crates/forget_estree/src/js_value.rs renamed
compiler/forget/crates/forget_estree/src/lib.rs renamed
compiler/forget/crates/forget_estree/src/old.lib.rs renamed
compiler/forget/crates/forget_estree/src/range.rs renamed
compiler/forget/crates/forget_estree/src/snapshots/forget_estree__tests__fixtures@for-statement.json.snap renamed
+2 -2
@@ -1,7 +1,7 @@
1 ---
2 -source: crates/estree/src/lib.rs
2 +source: crates/forget_estree/src/lib.rs
3 expression: "format!(\"Input:\\n{input}\\n\\nOutput:\\n{serialized}\")"
4 -input_file: crates/estree/src/fixtures/for-statement.json
4 +input_file: crates/forget_estree/src/fixtures/for-statement.json
5 ---
6 Input:
7 {
compiler/forget/crates/forget_estree/src/snapshots/forget_estree__tests__fixtures@import.json.snap renamed
+2 -2
@@ -1,7 +1,7 @@
1 ---
2 -source: crates/estree/src/lib.rs
2 +source: crates/forget_estree/src/lib.rs
3 expression: "format!(\"Input:\\n{input}\\n\\nOutput:\\n{serialized}\")"
4 -input_file: crates/estree/src/fixtures/import.json
4 +input_file: crates/forget_estree/src/fixtures/import.json
5 ---
6 Input:
7 {
compiler/forget/crates/forget_estree/src/snapshots/forget_estree__tests__fixtures@simple.json.snap renamed
+2 -2
@@ -1,7 +1,7 @@
1 ---
2 -source: crates/estree/src/lib.rs
2 +source: crates/forget_estree/src/lib.rs
3 expression: "format!(\"Input:\\n{input}\\n\\nOutput:\\n{serialized}\")"
4 -input_file: crates/estree/src/fixtures/simple.json
4 +input_file: crates/forget_estree/src/fixtures/simple.json
5 ---
6 Input:
7 {
compiler/forget/crates/forget_estree/src/snapshots/forget_estree__tests__fixtures@test.json.snap renamed
+2 -2
@@ -1,7 +1,7 @@
1 ---
2 -source: crates/estree/src/lib.rs
2 +source: crates/forget_estree/src/lib.rs
3 expression: "format!(\"Input:\\n{input}\\n\\nOutput:\\n{serialized}\")"
4 -input_file: crates/estree/src/fixtures/test.json
4 +input_file: crates/forget_estree/src/fixtures/test.json
5 ---
6 Input:
7 {
compiler/forget/crates/forget_estree/src/visit.rs renamed
compiler/forget/crates/forget_estree_codegen/Cargo.toml new
+21
@@ -0,0 +1,21 @@
1 +[package]
2 +name = "forget_estree_codegen"
3 +version = "0.1.0"
4 +publish = false
5 +authors.workspace = true
6 +description.workspace = true
7 +edition.workspace = true
8 +homepage.workspace = true
9 +keywords.workspace = true
10 +license.workspace = true
11 +repository.workspace = true
12 +
13 +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
14 +
15 +[dependencies]
16 +indexmap = { workspace = true }
17 +prettyplease = { workspace = true }
18 +quote = { workspace = true }
19 +serde = { workspace = true }
20 +serde_json = { workspace = true }
21 +syn = { workspace = true }
compiler/forget/crates/forget_estree_codegen/src/codegen.rs renamed
compiler/forget/crates/forget_estree_codegen/src/ecmascript.json renamed
compiler/forget/crates/forget_estree_codegen/src/lib.rs renamed
compiler/forget/crates/forget_estree_swc/Cargo.toml new
+18
@@ -0,0 +1,18 @@
1 +[package]
2 +name = "forget_estree_swc"
3 +version = "0.1.0"
4 +publish = false
5 +authors.workspace = true
6 +description.workspace = true
7 +edition.workspace = true
8 +homepage.workspace = true
9 +keywords.workspace = true
10 +license.workspace = true
11 +repository.workspace = true
12 +
13 +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
14 +
15 +[dependencies]
16 +forget_estree = { workspace = true }
17 +swc = { workspace = true }
18 +swc_core = { workspace = true }
compiler/forget/crates/forget_estree_swc/src/lib.rs new
+555
@@ -0,0 +1,555 @@
1 +use std::{io::stderr, num::NonZeroU32, sync::Arc};
2 +
3 +use forget_estree::{Binding, BindingId};
4 +use swc::Compiler;
5 +use swc_core::common::errors::Handler;
6 +use swc_core::common::source_map::Pos;
7 +use swc_core::common::{FileName, FilePathMapping, Mark, SourceMap, Span, SyntaxContext, GLOBALS};
8 +use swc_core::ecma::ast::{
9 + AssignOp, BinaryOp, BlockStmt, Decl, EsVersion, Expr, Function, Ident, Lit, MemberExpr,
10 + MemberProp, ModuleItem, Pat, PatOrExpr, Program, Stmt, UnaryOp, VarDecl, VarDeclKind,
11 + VarDeclOrExpr,
12 +};
13 +use swc_core::ecma::parser::Syntax;
14 +use swc_core::ecma::transforms::base::resolver;
15 +use swc_core::ecma::visit::FoldWith;
16 +
17 +/// Parses source text into an forget_estree::Program via SWC, internally performing the parsing
18 +/// and SWC -> ESTree conversion.
19 +pub fn parse(
20 + source: &str,
21 + file: &str,
22 +) -> Result<forget_estree::Program, Box<dyn std::error::Error>> {
23 + GLOBALS.set(&Default::default(), || {
24 + let cm = Arc::new(SourceMap::new(FilePathMapping::empty()));
25 + let c = Compiler::new(cm);
26 + let fm =
27 + c.cm.new_source_file(FileName::Real(file.into()), source.to_string());
28 +
29 + let handler = Handler::with_emitter_writer(Box::new(stderr()), Some(c.cm.clone()));
30 +
31 + let comments = c.comments().clone();
32 + let module = c.parse_js(
33 + fm.clone(),
34 + &handler,
35 + EsVersion::Es5,
36 + Syntax::Typescript(Default::default()),
37 + swc::config::IsModule::Bool(true),
38 + Some(&comments),
39 + )?;
40 +
41 + let context = Context {
42 + top_level_mark: Mark::new(),
43 + unresolved_mark: Mark::new(),
44 + };
45 +
46 + let module = c.run_transform(&handler, false, || {
47 + module.fold_with(&mut resolver(
48 + context.unresolved_mark,
49 + context.top_level_mark,
50 + true,
51 + ))
52 + });
53 +
54 + Ok(convert_program(&context, &module))
55 + })
56 +}
57 +
58 +#[derive(Debug)]
59 +struct Context {
60 + unresolved_mark: Mark,
61 + top_level_mark: Mark,
62 +}
63 +
64 +fn convert_program(cx: &Context, program: &Program) -> forget_estree::Program {
65 + let mut program_items: Vec<forget_estree::ModuleItem>;
66 + match program {
67 + Program::Module(program) => {
68 + let body = &program.body;
69 + program_items = Vec::with_capacity(body.len());
70 + for item in body {
71 + program_items.push(convert_module_item(cx, item));
72 + }
73 + }
74 + Program::Script(program) => {
75 + let body = &program.body;
76 + program_items = Vec::with_capacity(body.len());
77 + for item in body {
78 + program_items.push(forget_estree::ModuleItem::Statement(convert_statement(
79 + cx, item,
80 + )));
81 + }
82 + }
83 + };
84 + forget_estree::Program {
85 + source_type: if program.is_script() {
86 + forget_estree::SourceType::Script
87 + } else {
88 + forget_estree::SourceType::Module
89 + },
90 + body: program_items,
91 + // comments: None,
92 + loc: None,
93 + range: None,
94 + }
95 +}
96 +
97 +fn convert_module_item(cx: &Context, item: &ModuleItem) -> forget_estree::ModuleItem {
98 + match item {
99 + ModuleItem::Stmt(item) => forget_estree::ModuleItem::Statement(convert_statement(cx, item)),
100 + _ => todo!("translate module item {:#?}", item),
101 + }
102 +}
103 +
104 +fn convert_span(span: &Span) -> Option<forget_estree::SourceRange> {
105 + Some(forget_estree::SourceRange {
106 + start: span.lo().to_u32(),
107 + end: NonZeroU32::new(span.hi().to_u32())?,
108 + })
109 +}
110 +
111 +fn convert_decl_kind(kind: &VarDeclKind) -> forget_estree::VariableDeclarationKind {
112 + match kind {
113 + VarDeclKind::Const => forget_estree::VariableDeclarationKind::Const,
114 + VarDeclKind::Let => forget_estree::VariableDeclarationKind::Let,
115 + VarDeclKind::Var => forget_estree::VariableDeclarationKind::Var,
116 + }
117 +}
118 +
119 +fn convert_block_statement(cx: &Context, stmt: &BlockStmt) -> forget_estree::BlockStatement {
120 + let mut body: Vec<forget_estree::Statement> = Vec::with_capacity(stmt.stmts.len());
121 + for stmt in &stmt.stmts {
122 + body.push(convert_statement(cx, stmt));
123 + }
124 + forget_estree::BlockStatement {
125 + body,
126 + loc: None,
127 + range: convert_span(&stmt.span),
128 + }
129 +}
130 +
131 +fn convert_function(cx: &Context, id: Option<&Ident>, fun: &Function) -> forget_estree::Function {
132 + forget_estree::Function {
133 + id: id.map(|id| forget_estree::Identifier {
134 + name: id.sym.to_string(),
135 + binding: convert_binding(cx, id.span.ctxt),
136 + loc: None,
137 + range: convert_span(&id.span),
138 + }),
139 + params: fun
140 + .params
141 + .iter()
142 + .map(|param| convert_pattern(cx, &param.pat))
143 + .collect(),
144 + body: fun.body.as_ref().map(|body| {
145 + forget_estree::FunctionBody::BlockStatement(Box::new(convert_block_statement(cx, body)))
146 + }),
147 + is_async: fun.is_async,
148 + is_generator: fun.is_generator,
149 + loc: None,
150 + range: convert_span(&fun.span),
151 + }
152 +}
153 +
154 +fn convert_statement(cx: &Context, stmt: &Stmt) -> forget_estree::Statement {
155 + match stmt {
156 + Stmt::Decl(Decl::Fn(item)) => forget_estree::Statement::FunctionDeclaration(Box::new(
157 + forget_estree::FunctionDeclaration {
158 + function: convert_function(cx, Some(&item.ident), &item.function),
159 + loc: None,
160 + range: convert_span(&item.function.span),
161 + },
162 + )),
163 + Stmt::Decl(Decl::Var(item)) => forget_estree::Statement::VariableDeclaration(Box::new(
164 + convert_variable_declaration(cx, item),
165 + )),
166 + Stmt::Block(item) => {
167 + forget_estree::Statement::BlockStatement(Box::new(convert_block_statement(cx, item)))
168 + }
169 + Stmt::Break(item) => {
170 + forget_estree::Statement::BreakStatement(Box::new(forget_estree::BreakStatement {
171 + label: item
172 + .label
173 + .as_ref()
174 + .map(|label| convert_identifier(cx, label)),
175 + loc: None,
176 + range: convert_span(&item.span),
177 + }))
178 + }
179 + Stmt::Continue(item) => forget_estree::Statement::ContinueStatement(Box::new(
180 + forget_estree::ContinueStatement {
181 + label: item
182 + .label
183 + .as_ref()
184 + .map(|label| convert_identifier(cx, label)),
185 + loc: None,
186 + range: convert_span(&item.span),
187 + },
188 + )),
189 + Stmt::Debugger(item) => forget_estree::Statement::DebuggerStatement(Box::new(
190 + forget_estree::DebuggerStatement {
191 + loc: None,
192 + range: convert_span(&item.span),
193 + },
194 + )),
195 + Stmt::DoWhile(item) => {
196 + forget_estree::Statement::DoWhileStatement(Box::new(forget_estree::DoWhileStatement {
197 + body: convert_statement(cx, &item.body),
198 + test: convert_expression(cx, &item.test),
199 + loc: None,
200 + range: convert_span(&item.span),
201 + }))
202 + }
203 + Stmt::Empty(item) => {
204 + forget_estree::Statement::EmptyStatement(Box::new(forget_estree::EmptyStatement {
205 + loc: None,
206 + range: convert_span(&item.span),
207 + }))
208 + }
209 + Stmt::Expr(item) => {
210 + let expression = convert_expression(cx, &item.expr);
211 + forget_estree::Statement::ExpressionStatement(Box::new(
212 + forget_estree::ExpressionStatement {
213 + expression,
214 + directive: None,
215 + loc: None,
216 + range: convert_span(&item.span),
217 + },
218 + ))
219 + }
220 + Stmt::For(item) => {
221 + forget_estree::Statement::ForStatement(Box::new(forget_estree::ForStatement {
222 + init: item.init.as_ref().map(|init| match init {
223 + VarDeclOrExpr::Expr(init) => {
224 + forget_estree::ForInit::Expression(convert_expression(cx, init))
225 + }
226 + VarDeclOrExpr::VarDecl(init) => {
227 + assert_eq!(init.decls.len(), 1);
228 + forget_estree::ForInit::VariableDeclaration(Box::new(
229 + convert_variable_declaration(cx, init),
230 + ))
231 + }
232 + }),
233 + test: item.test.as_ref().map(|test| convert_expression(cx, test)),
234 + update: item
235 + .update
236 + .as_ref()
237 + .map(|update| convert_expression(cx, update)),
238 + body: convert_statement(cx, &item.body),
239 + loc: None,
240 + range: convert_span(&item.span),
241 + }))
242 + }
243 + Stmt::Return(item) => {
244 + forget_estree::Statement::ReturnStatement(Box::new(forget_estree::ReturnStatement {
245 + argument: item.arg.as_ref().map(|arg| convert_expression(cx, arg)),
246 + loc: None,
247 + range: convert_span(&item.span),
248 + }))
249 + }
250 + Stmt::Throw(item) => {
251 + forget_estree::Statement::ThrowStatement(Box::new(forget_estree::ThrowStatement {
252 + argument: convert_expression(cx, &item.arg),
253 + loc: None,
254 + range: convert_span(&item.span),
255 + }))
256 + }
257 + Stmt::If(item) => {
258 + forget_estree::Statement::IfStatement(Box::new(forget_estree::IfStatement {
259 + test: convert_expression(cx, &item.test),
260 + consequent: convert_statement(cx, &item.cons),
261 + alternate: item.alt.as_ref().map(|alt| convert_statement(cx, alt)),
262 + loc: None,
263 + range: convert_span(&item.span),
264 + }))
265 + }
266 + _ => todo!("translate statement {:#?}", stmt),
267 + }
268 +}
269 +
270 +fn convert_variable_declaration(
271 + cx: &Context,
272 + decl: &VarDecl,
273 +) -> forget_estree::VariableDeclaration {
274 + forget_estree::VariableDeclaration {
275 + kind: convert_decl_kind(&decl.kind),
276 + declarations: decl
277 + .decls
278 + .iter()
279 + .map(|declarator| forget_estree::VariableDeclarator {
280 + id: convert_pattern(cx, &declarator.name),
281 + init: declarator
282 + .init
283 + .as_ref()
284 + .map(|init| convert_expression(cx, init)),
285 + loc: None,
286 + range: convert_span(&decl.span),
287 + })
288 + .collect(),
289 + loc: None,
290 + range: convert_span(&decl.span),
291 + }
292 +}
293 +
294 +fn convert_expression(cx: &Context, expr: &Expr) -> forget_estree::Expression {
295 + match expr {
296 + Expr::Ident(expr) => {
297 + forget_estree::Expression::Identifier(Box::new(convert_identifier(cx, expr)))
298 + }
299 + Expr::Array(expr) => {
300 + forget_estree::Expression::ArrayExpression(Box::new(forget_estree::ArrayExpression {
301 + elements: expr
302 + .elems
303 + .iter()
304 + .map(|item| {
305 + // TODO: represent holes in array expressions
306 + let value = item.as_ref()?;
307 + match value.spread {
308 + Some(spread) => Some(forget_estree::ExpressionOrSpread::SpreadElement(
309 + Box::new(forget_estree::SpreadElement {
310 + argument: convert_expression(cx, &value.expr),
311 + loc: None,
312 + range: convert_span(&spread),
313 + }),
314 + )),
315 + None => Some(forget_estree::ExpressionOrSpread::Expression(
316 + convert_expression(cx, &value.expr),
317 + )),
318 + }
319 + })
320 + .collect(),
321 + loc: None,
322 + range: convert_span(&expr.span),
323 + }))
324 + }
325 + Expr::Await(_expr) => {
326 + // forget_estree::Expression::AwaitExpression(Box::new(forget_estree::AwaitExpression {
327 + // argument: convert_expression(cx, &expr.arg),
328 + // loc: None,
329 + // range: convert_span(&expr.span),
330 + // }))
331 + todo!("await expression")
332 + }
333 + Expr::Unary(expr) => {
334 + forget_estree::Expression::UnaryExpression(Box::new(forget_estree::UnaryExpression {
335 + operator: convert_unary_operator(expr.op),
336 + prefix: false,
337 + argument: convert_expression(cx, &expr.arg),
338 + loc: None,
339 + range: convert_span(&expr.span),
340 + }))
341 + }
342 + Expr::Bin(expr) => match convert_binary_operator(expr.op) {
343 + Operator::Binary(op) => forget_estree::Expression::BinaryExpression(Box::new(
344 + forget_estree::BinaryExpression {
345 + operator: op,
346 + left: convert_expression(cx, &expr.left),
347 + right: convert_expression(cx, &expr.right),
348 + loc: None,
349 + range: convert_span(&expr.span),
350 + },
351 + )),
352 + Operator::Logical(op) => forget_estree::Expression::LogicalExpression(Box::new(
353 + forget_estree::LogicalExpression {
354 + operator: op,
355 + left: convert_expression(cx, &expr.left),
356 + right: convert_expression(cx, &expr.right),
357 + loc: None,
358 + range: convert_span(&expr.span),
359 + },
360 + )),
361 + },
362 + Expr::Lit(expr) => {
363 + let (value, range) = match expr {
364 + Lit::Bool(expr) => (
365 + forget_estree::JsValue::Bool(expr.value),
366 + convert_span(&expr.span),
367 + ),
368 + Lit::Num(expr) => (
369 + forget_estree::JsValue::Number(expr.value.into()),
370 + convert_span(&expr.span),
371 + ),
372 + Lit::Str(expr) => (
373 + forget_estree::JsValue::String(expr.value.to_string()),
374 + convert_span(&expr.span),
375 + ),
376 + Lit::Null(expr) => (forget_estree::JsValue::Null, convert_span(&expr.span)),
377 + _ => todo!(),
378 + };
379 + forget_estree::Expression::Literal(Box::new(forget_estree::Literal {
380 + value,
381 + raw: None,
382 + loc: None,
383 + regex: None,
384 + range,
385 + }))
386 + }
387 + Expr::Assign(expr) => forget_estree::Expression::AssignmentExpression(Box::new(
388 + forget_estree::AssignmentExpression {
389 + operator: convert_assignment_operator(expr.op),
390 + left: convert_assignment_target(cx, &expr.left),
391 + right: convert_expression(cx, &expr.right),
392 + loc: None,
393 + range: convert_span(&expr.span),
394 + },
395 + )),
396 + Expr::Member(expr) => forget_estree::Expression::MemberExpression(Box::new(
397 + convert_member_expression(cx, expr),
398 + )),
399 + Expr::Fn(expr) => forget_estree::Expression::FunctionExpression(Box::new(
400 + forget_estree::FunctionExpression {
401 + function: convert_function(cx, expr.ident.as_ref(), &expr.function),
402 + loc: None,
403 + range: convert_span(&expr.function.span),
404 + },
405 + )),
406 + _ => todo!("translate expression {:#?}", expr),
407 + }
408 +}
409 +
410 +fn convert_assignment_target(cx: &Context, target: &PatOrExpr) -> forget_estree::AssignmentTarget {
411 + match target {
412 + PatOrExpr::Pat(target) => {
413 + forget_estree::AssignmentTarget::Pattern(convert_pattern(cx, target))
414 + }
415 + PatOrExpr::Expr(target) => {
416 + match target.as_ref() {
417 + Expr::Member(target) => forget_estree::AssignmentTarget::Expression(
418 + forget_estree::Expression::MemberExpression(Box::new(
419 + convert_member_expression(cx, target),
420 + )),
421 + ),
422 + Expr::Ident(target) => forget_estree::AssignmentTarget::Pattern(
423 + forget_estree::Pattern::Identifier(Box::new(convert_identifier(cx, target))),
424 + ),
425 + _ => {
426 + panic!("Expected assignment target to be member expression or identifier, got {:#?}", target)
427 + }
428 + }
429 + }
430 + }
431 +}
432 +
433 +fn convert_member_expression(cx: &Context, expr: &MemberExpr) -> forget_estree::MemberExpression {
434 + let (is_computed, property) = match &expr.prop {
435 + MemberProp::Ident(prop) => (
436 + false,
437 + forget_estree::Expression::Identifier(Box::new(convert_identifier(cx, prop))),
438 + ),
439 + MemberProp::Computed(prop) => (true, convert_expression(cx, &prop.expr)),
440 + _ => {
441 + panic!("PrivateName member expression properties are not supported")
442 + }
443 + };
444 + forget_estree::MemberExpression {
445 + object: forget_estree::ExpressionOrSuper::Expression(convert_expression(cx, &expr.obj)),
446 + property,
447 + computed: is_computed,
448 + // optional: false, // TODO
449 + loc: None,
450 + range: convert_span(&expr.span),
451 + }
452 +}
453 +
454 +fn convert_unary_operator(op: UnaryOp) -> forget_estree::UnaryOperator {
455 + match op {
456 + UnaryOp::Bang => forget_estree::UnaryOperator::Negation,
457 + UnaryOp::Delete => forget_estree::UnaryOperator::Delete,
458 + UnaryOp::Minus => forget_estree::UnaryOperator::Minus,
459 + UnaryOp::Plus => forget_estree::UnaryOperator::Plus,
460 + UnaryOp::Tilde => forget_estree::UnaryOperator::Tilde,
461 + UnaryOp::TypeOf => forget_estree::UnaryOperator::Typeof,
462 + UnaryOp::Void => forget_estree::UnaryOperator::Void,
463 + }
464 +}
465 +
466 +fn convert_assignment_operator(op: AssignOp) -> forget_estree::AssignmentOperator {
467 + match op {
468 + AssignOp::Assign => forget_estree::AssignmentOperator::Equals,
469 + AssignOp::AddAssign => forget_estree::AssignmentOperator::PlusEquals,
470 + _ => todo!("translate assignment operator"),
471 + }
472 +}
473 +
474 +enum Operator {
475 + Binary(forget_estree::BinaryOperator),
476 + Logical(forget_estree::LogicalOperator),
477 +}
478 +
479 +fn convert_binary_operator(op: BinaryOp) -> Operator {
480 + match op {
481 + BinaryOp::Add => Operator::Binary(forget_estree::BinaryOperator::Add),
482 + BinaryOp::BitAnd => Operator::Binary(forget_estree::BinaryOperator::BinaryAnd),
483 + BinaryOp::BitOr => Operator::Binary(forget_estree::BinaryOperator::BinaryOr),
484 + BinaryOp::BitXor => Operator::Binary(forget_estree::BinaryOperator::BinaryXor),
485 + BinaryOp::Div => Operator::Binary(forget_estree::BinaryOperator::Divide),
486 + BinaryOp::EqEq => Operator::Binary(forget_estree::BinaryOperator::Equals),
487 + BinaryOp::EqEqEq => Operator::Binary(forget_estree::BinaryOperator::StrictEquals),
488 + // BinaryOp::Exp => Operator::Binary(forget_estree::BinaryOperator::AsteriskAsterisk),
489 + BinaryOp::Gt => Operator::Binary(forget_estree::BinaryOperator::GreaterThan),
490 + BinaryOp::GtEq => Operator::Binary(forget_estree::BinaryOperator::GreaterThanOrEqual),
491 + BinaryOp::In => Operator::Binary(forget_estree::BinaryOperator::In),
492 + BinaryOp::InstanceOf => Operator::Binary(forget_estree::BinaryOperator::Instanceof),
493 + BinaryOp::LShift => Operator::Binary(forget_estree::BinaryOperator::ShiftLeft),
494 + BinaryOp::Lt => Operator::Binary(forget_estree::BinaryOperator::LessThan),
495 + BinaryOp::LtEq => Operator::Binary(forget_estree::BinaryOperator::LessThanOrEqual),
496 + BinaryOp::Mod => Operator::Binary(forget_estree::BinaryOperator::Modulo),
497 + BinaryOp::Mul => Operator::Binary(forget_estree::BinaryOperator::Multiply),
498 + BinaryOp::NotEq => Operator::Binary(forget_estree::BinaryOperator::NotEquals),
499 + BinaryOp::NotEqEq => Operator::Binary(forget_estree::BinaryOperator::NotStrictEquals),
500 + BinaryOp::RShift => Operator::Binary(forget_estree::BinaryOperator::ShiftRight),
501 + BinaryOp::Sub => Operator::Binary(forget_estree::BinaryOperator::Subtract),
502 + BinaryOp::ZeroFillRShift => {
503 + Operator::Binary(forget_estree::BinaryOperator::UnsignedShiftRight)
504 + }
505 +
506 + BinaryOp::LogicalAnd => Operator::Logical(forget_estree::LogicalOperator::And),
507 + BinaryOp::LogicalOr => Operator::Logical(forget_estree::LogicalOperator::Or),
508 + BinaryOp::NullishCoalescing => {
509 + Operator::Logical(forget_estree::LogicalOperator::NullCoalescing)
510 + }
511 +
512 + _ => panic!("Unsupported binary operator `{}`", op),
513 + }
514 +}
515 +
516 +fn convert_pattern(cx: &Context, pat: &Pat) -> forget_estree::Pattern {
517 + match pat {
518 + Pat::Ident(pat) => {
519 + forget_estree::Pattern::Identifier(Box::new(forget_estree::Identifier {
520 + name: pat.id.sym.to_string(),
521 + binding: convert_binding(cx, pat.id.span.ctxt),
522 + loc: None,
523 + range: convert_span(&pat.span),
524 + }))
525 + }
526 + _ => todo!("translate pattern {:#?}", pat),
527 + }
528 +}
529 +
530 +fn convert_binding(context: &Context, binding_cx: SyntaxContext) -> Option<Binding> {
531 + let id = BindingId::new(binding_cx.as_u32());
532 + if binding_cx.as_u32() == context.top_level_mark.as_u32() {
533 + Some(Binding::Global)
534 + } else if binding_cx.as_u32() == context.unresolved_mark.as_u32() {
535 + Some(Binding::Module(id))
536 + } else {
537 + Some(Binding::Local(id))
538 + }
539 +}
540 +
541 +fn convert_identifier(cx: &Context, identifier: &Ident) -> forget_estree::Identifier {
542 + let name = identifier.sym.as_ref().to_string();
543 + forget_estree::Identifier {
544 + name,
545 + binding: convert_binding(cx, identifier.span.ctxt),
546 + loc: None,
547 + range: convert_span(&identifier.span),
548 + }
549 +}
550 +
551 +#[cfg(test)]
552 +mod tests {
553 + #[test]
554 + fn it_works() {}
555 +}
compiler/forget/crates/forget_fixtures/Cargo.toml new
+24
@@ -0,0 +1,24 @@
1 +[package]
2 +name = "forget_fixtures"
3 +version = "0.1.0"
4 +publish = false
5 +authors.workspace = true
6 +description.workspace = true
7 +edition.workspace = true
8 +homepage.workspace = true
9 +keywords.workspace = true
10 +license.workspace = true
11 +repository.workspace = true
12 +
13 +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
14 +
15 +[dependencies]
16 +insta = { workspace = true }
17 +forget_estree = { workspace = true }
18 +forget_estree_swc = { workspace = true }
19 +forget_hir = { workspace = true }
20 +forget_optimization = { workspace = true }
21 +forget_ssa = { workspace = true }
22 +forget_build_hir = { workspace = true }
23 +bumpalo = { workspace = true }
24 +miette = { workspace = true, features = ["backtrace", "fancy"] }
compiler/forget/crates/forget_fixtures/README.md renamed
compiler/forget/crates/forget_fixtures/src/lib.rs renamed
compiler/forget/crates/forget_fixtures/tests/fixtures/constant-propagation-constant-if-condition.js renamed
compiler/forget/crates/forget_fixtures/tests/fixtures/constant-propagation.js renamed
compiler/forget/crates/forget_fixtures/tests/fixtures/error.assign-to-global.js renamed
compiler/forget/crates/forget_fixtures/tests/fixtures/for-statement.js renamed
compiler/forget/crates/forget_fixtures/tests/fixtures/function-expressions.js renamed
compiler/forget/crates/forget_fixtures/tests/fixtures/identifiers.js renamed
compiler/forget/crates/forget_fixtures/tests/fixtures/if-statement.js renamed
compiler/forget/crates/forget_fixtures/tests/fixtures/simple.js renamed
compiler/forget/crates/forget_fixtures/tests/fixtures/ssa-reassign-if.js renamed
compiler/forget/crates/forget_fixtures/tests/fixtures_test.rs renamed
+7 -7
@@ -1,12 +1,12 @@
1 use std::{env, fmt::Write};
2
3 -use build_hir::build;
3 use bumpalo::Bump;
5 -use estree::{ModuleItem, Statement};
6 -use estree_swc::parse;
7 -use hir::{Environment, Print, Registry};
8 -use hir_optimization::constant_propagation;
9 -use hir_ssa::{eliminate_redundant_phis, enter_ssa};
4 +use forget_build_hir::build;
5 +use forget_estree::{ModuleItem, Statement};
6 +use forget_estree_swc::parse;
7 +use forget_hir::{Environment, Features, Print, Registry};
8 +use forget_optimization::constant_propagation;
9 +use forget_ssa::{eliminate_redundant_phis, enter_ssa};
10 use insta::{assert_snapshot, glob};
11 use miette::{NamedSource, Report};
12
@@ -26,7 +26,7 @@ fn fixtures() {
26 let allocator = Bump::new();
27 let environment = allocator.alloc(Environment::new(
28 &allocator,
29 - hir::Features {
29 + Features {
30 validate_frozen_lambdas: true,
31 },
32 Registry,
compiler/forget/crates/forget_fixtures/tests/snapshots/fixtures_test__fixtures@constant-propagation-constant-if-condition.js.snap renamed
+2 -3
@@ -1,7 +1,7 @@
1 ---
2 -source: crates/fixtures/tests/fixtures_test.rs
2 +source: crates/forget_fixtures/tests/fixtures_test.rs
3 expression: "format!(\"Input:\\n{input}\\n\\nOutput:\\n{output}\")"
4 -input_file: crates/fixtures/tests/fixtures/constant-propagation-constant-if-condition.js
4 +input_file: crates/forget_fixtures/tests/fixtures/constant-propagation-constant-if-condition.js
5 ---
6 Input:
7 function Component() {
@@ -95,4 +95,3 @@ bb10 (block)
95 predecessors: bb11
96 [32] #32 = "ok"
97 [33] Return unknown #32
98 -
compiler/forget/crates/forget_fixtures/tests/snapshots/fixtures_test__fixtures@constant-propagation.js.snap renamed
+2 -3
@@ -1,7 +1,7 @@
1 ---
2 -source: crates/fixtures/tests/fixtures_test.rs
2 +source: crates/forget_fixtures/tests/fixtures_test.rs
3 expression: "format!(\"Input:\\n{input}\\n\\nOutput:\\n{output}\")"
4 -input_file: crates/fixtures/tests/fixtures/constant-propagation.js
4 +input_file: crates/forget_fixtures/tests/fixtures/constant-propagation.js
5 ---
6 Input:
7 function Component(props) {
@@ -196,4 +196,3 @@ bb4 (block)
196 [107] #101 = 18000000
197 [108] #102 = <undefined>
198 [109] Return unknown #102
199 -
compiler/forget/crates/forget_fixtures/tests/snapshots/fixtures_test__fixtures@error.assign-to-global.js.snap renamed
+2 -2
@@ -1,7 +1,7 @@
1 ---
2 -source: crates/fixtures/tests/fixtures_test.rs
2 +source: crates/forget_fixtures/tests/fixtures_test.rs
3 expression: "format!(\"Input:\\n{input}\\n\\nOutput:\\n{output}\")"
4 -input_file: crates/fixtures/tests/fixtures/error.assign-to-global.js
4 +input_file: crates/forget_fixtures/tests/fixtures/error.assign-to-global.js
5 ---
6 Input:
7 function foo() {
compiler/forget/crates/forget_fixtures/tests/snapshots/fixtures_test__fixtures@for-statement.js.snap renamed
+2 -3
@@ -1,7 +1,7 @@
1 ---
2 -source: crates/fixtures/tests/fixtures_test.rs
2 +source: crates/forget_fixtures/tests/fixtures_test.rs
3 expression: "format!(\"Input:\\n{input}\\n\\nOutput:\\n{output}\")"
4 -input_file: crates/fixtures/tests/fixtures/for-statement.js
4 +input_file: crates/forget_fixtures/tests/fixtures/for-statement.js
5 ---
6 Input:
7 function foo() {
@@ -52,4 +52,3 @@ bb2 (block)
52 predecessors: bb1
53 [20] #15 = LoadLocal unknown x$5
54 [21] Return unknown #15
55 -
compiler/forget/crates/forget_fixtures/tests/snapshots/fixtures_test__fixtures@function-expressions.js.snap renamed
+2 -2
@@ -1,7 +1,7 @@
1 ---
2 -source: crates/fixtures/tests/fixtures_test.rs
2 +source: crates/forget_fixtures/tests/fixtures_test.rs
3 expression: "format!(\"Input:\\n{input}\\n\\nOutput:\\n{output}\")"
4 -input_file: crates/fixtures/tests/fixtures/function-expressions.js
4 +input_file: crates/forget_fixtures/tests/fixtures/function-expressions.js
5 ---
6 Input:
7 function Component(props) {
compiler/forget/crates/forget_fixtures/tests/snapshots/fixtures_test__fixtures@identifiers.js.snap renamed
+2 -3
@@ -1,7 +1,7 @@
1 ---
2 -source: crates/fixtures/tests/fixtures_test.rs
2 +source: crates/forget_fixtures/tests/fixtures_test.rs
3 expression: "format!(\"Input:\\n{input}\\n\\nOutput:\\n{output}\")"
4 -input_file: crates/fixtures/tests/fixtures/identifiers.js
4 +input_file: crates/forget_fixtures/tests/fixtures/identifiers.js
5 ---
6 Input:
7 // import React from "react";
@@ -39,4 +39,3 @@ bb0 (block)
39 [8] #8 = LoadLocal unknown z$7
40 [9] #9 = LoadLocal unknown x$4
41 [10] Return unknown #9
42 -
compiler/forget/crates/forget_fixtures/tests/snapshots/fixtures_test__fixtures@if-statement.js.snap renamed
+2 -3
@@ -1,7 +1,7 @@
1 ---
2 -source: crates/fixtures/tests/fixtures_test.rs
2 +source: crates/forget_fixtures/tests/fixtures_test.rs
3 expression: "format!(\"Input:\\n{input}\\n\\nOutput:\\n{output}\")"
4 -input_file: crates/fixtures/tests/fixtures/if-statement.js
4 +input_file: crates/forget_fixtures/tests/fixtures/if-statement.js
5 ---
6 Input:
7 function foo(a, b, c, d) {
@@ -38,4 +38,3 @@ bb1 (block)
38 [6] #3 = LoadLocal unknown d$7
39 [7] #4 = <undefined>
40 [8] Return unknown #4
41 -
compiler/forget/crates/forget_fixtures/tests/snapshots/fixtures_test__fixtures@simple.js.snap renamed
+2 -3
@@ -1,7 +1,7 @@
1 ---
2 -source: crates/fixtures/tests/fixtures_test.rs
2 +source: crates/forget_fixtures/tests/fixtures_test.rs
3 expression: "format!(\"Input:\\n{input}\\n\\nOutput:\\n{output}\")"
4 -input_file: crates/fixtures/tests/fixtures/simple.js
4 +input_file: crates/forget_fixtures/tests/fixtures/simple.js
5 ---
6 Input:
7 function test() {
@@ -25,4 +25,3 @@ bb0 (block)
25 [7] #7 = Array [unknown #0, unknown #1, unknown #2, unknown #3, unknown #4, ...unknown #6]
26 [8] #8 = 2
27 [9] Return unknown #8
28 -
compiler/forget/crates/forget_fixtures/tests/snapshots/fixtures_test__fixtures@ssa-reassign-if.js.snap renamed
+2 -3
@@ -1,7 +1,7 @@
1 ---
2 -source: crates/fixtures/tests/fixtures_test.rs
2 +source: crates/forget_fixtures/tests/fixtures_test.rs
3 expression: "format!(\"Input:\\n{input}\\n\\nOutput:\\n{output}\")"
4 -input_file: crates/fixtures/tests/fixtures/ssa-reassign-if.js
4 +input_file: crates/forget_fixtures/tests/fixtures/ssa-reassign-if.js
5 ---
6 Input:
7 function Component(a, b) {
@@ -71,4 +71,3 @@ bb1 (block)
71 [24] #18 = LoadLocal unknown z$18
72 [25] #19 = Binary unknown #17 + unknown #18
73 [26] Return unknown #19
74 -
compiler/forget/crates/forget_hir/Cargo.toml new
+20
@@ -0,0 +1,20 @@
1 +[package]
2 +name = "forget_hir"
3 +version = "0.1.0"
4 +publish = false
5 +authors.workspace = true
6 +description.workspace = true
7 +edition.workspace = true
8 +homepage.workspace = true
9 +keywords.workspace = true
10 +license.workspace = true
11 +repository.workspace = true
12 +
13 +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
14 +
15 +[dependencies]
16 +bumpalo = { workspace = true }
17 +forget_estree = { workspace = true }
18 +indexmap = { workspace = true }
19 +serde = { workspace = true }
20 +forget_utils = { workspace = true }
compiler/forget/crates/forget_hir/README.md renamed
compiler/forget/crates/forget_hir/src/basic_block.rs renamed
compiler/forget/crates/forget_hir/src/environment.rs renamed
+1 -1
@@ -5,7 +5,7 @@ use std::{
5 };
6
7 use bumpalo::{boxed::Box, collections::Vec, Bump};
8 -use estree::BindingId;
8 +use forget_estree::BindingId;
9
10 use crate::{
11 BlockId, Features, Identifier, IdentifierData, IdentifierId, Registry, Type, TypeVarId,
compiler/forget/crates/forget_hir/src/features.rs renamed
compiler/forget/crates/forget_hir/src/function.rs renamed
compiler/forget/crates/forget_hir/src/id_types.rs renamed
compiler/forget/crates/forget_hir/src/instruction.rs renamed
+1 -1
@@ -4,7 +4,7 @@ use bumpalo::{
4 boxed::Box,
5 collections::{String, Vec},
6 };
7 -use estree::BinaryOperator;
7 +use forget_estree::BinaryOperator;
8
9 use crate::{Function, IdentifierId, InstrIx, InstructionId, ScopeId, Type};
10
compiler/forget/crates/forget_hir/src/lib.rs renamed
compiler/forget/crates/forget_hir/src/print.rs renamed
+1 -1
@@ -1,6 +1,6 @@
1 use std::fmt::{Result, Write};
2
3 -use utils::ensure_sufficient_stack;
3 +use forget_utils::ensure_sufficient_stack;
4
5 use crate::{
6 ArrayElement, BasicBlock, Function, Identifier, IdentifierOperand, Instruction,
compiler/forget/crates/forget_hir/src/registry.rs renamed
compiler/forget/crates/forget_hir/src/terminal.rs renamed
compiler/forget/crates/forget_hir/src/types.rs renamed
compiler/forget/crates/forget_optimization/Cargo.toml new
+24
@@ -0,0 +1,24 @@
1 +[package]
2 +name = "forget_optimization"
3 +version = "0.1.0"
4 +publish = false
5 +authors.workspace = true
6 +description.workspace = true
7 +edition.workspace = true
8 +homepage.workspace = true
9 +keywords.workspace = true
10 +license.workspace = true
11 +repository.workspace = true
12 +
13 +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
14 +
15 +[dependencies]
16 +forget_estree = { workspace = true }
17 +forget_hir = { workspace = true }
18 +forget_ssa = { workspace = true }
19 +forget_build_hir = { workspace = true }
20 +forget_utils = { workspace = true }
21 +bumpalo = { workspace = true }
22 +indexmap = { workspace = true }
23 +miette = { workspace = true }
24 +thiserror = { workspace = true }
\ No newline at end of file
compiler/forget/crates/forget_optimization/src/constant_propagation.rs renamed
+7 -7
@@ -1,12 +1,12 @@
1 use std::collections::HashMap;
2
3 -use build_hir::initialize_hir;
4 -use estree::BinaryOperator;
5 -use hir::{
6 - BlockKind, Environment, Function, GotoKind, IdentifierId, Instruction, InstructionValue,
7 - LoadGlobal, Operand, Primitive, PrimitiveValue, TerminalValue,
3 +use forget_build_hir::initialize_hir;
4 +use forget_estree::BinaryOperator;
5 +use forget_hir::{
6 + BlockKind, Environment, Function, GotoKind, GotoTerminal, IdentifierId, Instruction,
7 + InstructionValue, LoadGlobal, Operand, Primitive, PrimitiveValue, TerminalValue,
8 };
9 -use hir_ssa::eliminate_redundant_phis;
9 +use forget_ssa::eliminate_redundant_phis;
10
11 pub fn constant_propagation<'a>(env: &Environment<'a>, fun: &mut Function<'a>) {
12 let mut constants = Constants::new();
@@ -107,7 +107,7 @@ fn apply_constant_propagation<'a>(
107 } else {
108 terminal.alternate
109 };
110 - block.terminal.value = TerminalValue::Goto(hir::GotoTerminal {
110 + block.terminal.value = TerminalValue::Goto(GotoTerminal {
111 block: target_block_id,
112 kind: GotoKind::Break,
113 });
compiler/forget/crates/forget_optimization/src/lib.rs renamed
compiler/forget/crates/forget_ssa/Cargo.toml new
+21
@@ -0,0 +1,21 @@
1 +[package]
2 +name = "forget_ssa"
3 +version = "0.1.0"
4 +publish = false
5 +authors.workspace = true
6 +description.workspace = true
7 +edition.workspace = true
8 +homepage.workspace = true
9 +keywords.workspace = true
10 +license.workspace = true
11 +repository.workspace = true
12 +
13 +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
14 +
15 +[dependencies]
16 +forget_hir = { workspace = true }
17 +forget_utils = { workspace = true }
18 +bumpalo = { workspace = true }
19 +indexmap = { workspace = true }
20 +miette = { workspace = true }
21 +thiserror = { workspace = true }
\ No newline at end of file
compiler/forget/crates/forget_ssa/README.md new
+3
@@ -0,0 +1,3 @@
1 +# forget_ssa
2 +
3 +This crate handles conversion to/from SSA and normal form. This includes a pass to eliminate redundant phi nodes in SSA form.
\ No newline at end of file
compiler/forget/crates/forget_ssa/src/eliminate_redundant_phis.rs renamed
+2 -2
@@ -1,7 +1,7 @@
1 use std::collections::{HashMap, HashSet};
2
3 -use hir::{BlockId, Environment, Function, Identifier, IdentifierId, InstructionValue};
4 -use utils::RetainMut;
3 +use forget_hir::{BlockId, Environment, Function, Identifier, IdentifierId, InstructionValue};
4 +use forget_utils::RetainMut;
5
6 /// Pass to eliminate redundant phi nodes:
7 /// all operands are the same identifier, ie `x2 = phi(x1, x1, x1)`.
compiler/forget/crates/forget_ssa/src/enter.rs renamed
+1 -1
@@ -1,7 +1,7 @@
1 use std::{cell::RefCell, rc::Rc};
2
3 use bumpalo::collections::{CollectIn, Vec};
4 -use hir::{
4 +use forget_hir::{
5 BasicBlock, BlockId, Blocks, Environment, Function, Identifier, IdentifierData, IdentifierId,
6 IdentifierOperand, Instruction, InstructionValue, LValue, MutableRange, Phi,
7 };
compiler/forget/crates/forget_ssa/src/leave.rs renamed
+1 -1
@@ -1,4 +1,4 @@
1 -use hir::{Environment, HIR};
1 +use forget_hir::{Environment, HIR};
2
3 pub fn leave_ssa<'a>(_env: &'a Environment, _hir: &mut HIR<'a>) {
4 todo!("leave_ssa()");
compiler/forget/crates/forget_ssa/src/lib.rs renamed
compiler/forget/crates/forget_utils/Cargo.toml new
+17
@@ -0,0 +1,17 @@
1 +[package]
2 +name = "forget_utils"
3 +version = "0.1.0"
4 +publish = false
5 +authors.workspace = true
6 +description.workspace = true
7 +edition.workspace = true
8 +homepage.workspace = true
9 +keywords.workspace = true
10 +license.workspace = true
11 +repository.workspace = true
12 +
13 +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
14 +
15 +[dependencies]
16 +bumpalo = { workspace = true }
17 +stacker = { workspace = true }
compiler/forget/crates/forget_utils/src/lib.rs renamed
compiler/forget/crates/forget_utils/src/retain_mut.rs renamed
compiler/forget/crates/hir-optimization/Cargo.toml deleted
-17
@@ -1,17 +0,0 @@
1 -[package]
2 -name = "hir-optimization"
3 -version = "0.1.0"
4 -edition = "2021"
5 -
6 -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7 -
8 -[dependencies]
9 -estree = { path = "../estree" }
10 -hir = { path = "../hir" }
11 -hir-ssa = { path = "../hir-ssa" }
12 -build-hir = { path = "../build-hir" }
13 -utils = { path = "../utils" }
14 -bumpalo = "3.13.0"
15 -indexmap = "2.0.0"
16 -miette = { version = "5.9.0" }
17 -thiserror = "1.0.41"
\ No newline at end of file
compiler/forget/crates/hir-ssa/Cargo.toml deleted
-14
@@ -1,14 +0,0 @@
1 -[package]
2 -name = "hir-ssa"
3 -version = "0.1.0"
4 -edition = "2021"
5 -
6 -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7 -
8 -[dependencies]
9 -hir = { path = "../hir" }
10 -utils = { path = "../utils" }
11 -bumpalo = "3.13.0"
12 -indexmap = "2.0.0"
13 -miette = { version = "5.9.0" }
14 -thiserror = "1.0.41"
\ No newline at end of file
compiler/forget/crates/hir-ssa/README.md deleted
-3
@@ -1,3 +0,0 @@
1 -# hir-ssa
2 -
3 -This crate handles conversion to/from SSA and normal form. This includes a pass to eliminate redundant phi nodes in SSA form.
\ No newline at end of file
compiler/forget/crates/hir/Cargo.toml deleted
-13
@@ -1,13 +0,0 @@
1 -[package]
2 -name = "hir"
3 -version = "0.1.0"
4 -edition = "2021"
5 -
6 -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7 -
8 -[dependencies]
9 -bumpalo = { version = "3.13.0", features = ["boxed", "collections"] }
10 -estree = { path = "../estree" }
11 -indexmap = "2.0.0"
12 -serde = "1.0.164"
13 -utils = { path = "../utils" }
compiler/forget/crates/swc-demo/Cargo.toml deleted
-14
@@ -1,14 +0,0 @@
1 -[package]
2 -name = "swc-demo"
3 -version = "0.1.0"
4 -edition = "2021"
5 -
6 -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7 -
8 -[dependencies]
9 -swc_common = { version = "0.31.12", features = ["tty-emitter", "sourcemap", "anyhow"] }
10 -swc_core = "0.78.15"
11 -swc_ecma_parser = "0.136.2"
12 -swc_ecma_transforms_base = "0.129.8"
13 -swc_ecma_visit = "0.92.1"
14 -swc_visit = "0.5.6"
compiler/forget/crates/swc-demo/src/main.rs deleted
-59
@@ -1,59 +0,0 @@
1 -use swc_common::sync::Lrc;
2 -use swc_common::Mark;
3 -use swc_common::{
4 - errors::{ColorConfig, Handler},
5 - FileName, SourceMap,
6 -};
7 -use swc_ecma_parser::{lexer::Lexer, Parser, StringInput, Syntax};
8 -use swc_ecma_transforms_base::resolver;
9 -use swc_ecma_visit::swc_ecma_ast::Program;
10 -use swc_ecma_visit::VisitMutWith;
11 -
12 -fn main() {
13 - let cm: Lrc<SourceMap> = Default::default();
14 - let handler = Handler::with_tty_emitter(ColorConfig::Auto, true, false, Some(cm.clone()));
15 -
16 - // Real usage
17 - // let fm = cm
18 - // .load_file(Path::new("test.js"))
19 - // .expect("failed to load test.js");
20 - let fm = cm.new_source_file(
21 - FileName::Custom("test.js".into()),
22 - "function foo(x: number, y: number): number { return x + y}".into(),
23 - );
24 - let lexer = Lexer::new(
25 - // We want to parse ecmascript
26 - Syntax::Typescript(swc_ecma_parser::TsConfig {
27 - tsx: true,
28 - decorators: true,
29 - ..Default::default()
30 - }),
31 - // EsVersion defaults to es5
32 - Default::default(),
33 - StringInput::from(&*fm),
34 - None,
35 - );
36 -
37 - let mut parser = Parser::new_from(lexer);
38 -
39 - for e in parser.take_errors() {
40 - e.into_diagnostic(&handler).emit();
41 - }
42 -
43 - let module = parser
44 - .parse_module()
45 - .map_err(|e| {
46 - // Unrecoverable fatal error occurred
47 - e.into_diagnostic(&handler).emit()
48 - })
49 - .expect("failed to parser module");
50 -
51 - let mut module = Program::Module(module);
52 -
53 - let _result = swc_common::GLOBALS.set(&swc_common::Globals::new(), || {
54 - let mut resolved = resolver(Mark::new(), Mark::new(), true);
55 - module.visit_mut_with(&mut resolved)
56 - });
57 -
58 - println!("{:#?}", module);
59 -}
compiler/forget/crates/utils/Cargo.toml deleted
-10
@@ -1,10 +0,0 @@
1 -[package]
2 -name = "utils"
3 -version = "0.1.0"
4 -edition = "2021"
5 -
6 -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7 -
8 -[dependencies]
9 -bumpalo = { version = "3.13.0", features = ["boxed", "collections"] }
10 -stacker = "0.1.15"