From f960d52364e72fa7548cc8aaaf6367dfdf7b9a8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Sat, 18 May 2024 09:23:06 +0900 Subject: [PATCH] refactor(bindings): Make `@swc/core` a subpackage (#8625) **Description:** This is for consistency and future extensions. --- .github/workflows/CI.yml | 34 +- .github/workflows/publish-core.yml | 80 +- .github/workflows/publish-extra-bindings.yml | 4 + crates/swc/tests/exec.rs | 20 - crates/swc/tests/projects.rs | 2 +- crates/testing/src/lib.rs | 29 +- node-swc/__tests__/env/env_test.js | 20 - node-swc/__tests__/import_test.js | 5 - node-swc/__tests__/is_module_unknown_test.js | 16 - node-swc/__tests__/parse/api_test.js | 31 - node-swc/__tests__/script_test.js | 22 - node-swc/__tests__/transform/hidden_jest.js | 47 - node-swc/__tests__/transform/issue834_test.js | 29 - node-swc/__tests__/transform/issue846_test.js | 24 - node-swc/__tests__/transform/swc_node_335.js | 22 - .../plugin_transform_schema_vtest/Cargo.toml | 14 - node-swc/src/Visitor.ts | 1867 ----------- node-swc/src/binding.d.ts | 49 - node-swc/src/binding.js | 354 -- node-swc/src/index.ts | 479 --- node-swc/src/postinstall.ts | 124 - package.json | 152 +- {node-swc => packages/core}/.gitignore | 0 {node-swc => packages/core}/.npmignore | 0 .../core}/__tests__/dynamic_tsx.mjs | 2 +- packages/core/__tests__/env/env_test.js | 20 + .../core}/__tests__/error_test.mjs | 2 +- packages/core/__tests__/import_test.js | 3 + .../core/__tests__/is_module_unknown_test.js | 20 + .../__tests__/minify/issue_6996_test.mjs | 2 +- .../__tests__/minify/issue_8437_test.mjs | 48 +- .../core}/__tests__/minify_test.mjs | 2 +- .../core}/__tests__/module_test.js | 26 +- packages/core/__tests__/parse/api_test.js | 31 + .../core}/__tests__/paths_test.mjs | 2 +- .../core}/__tests__/plugin_test.mjs | 4 +- .../core}/__tests__/preserve_comments.mjs | 6 +- packages/core/__tests__/script_test.js | 22 + .../core}/__tests__/spack/simple_test.js | 2 +- .../core}/__tests__/transform/api_test.js | 2 +- .../__tests__/transform/experimental.mjs | 2 +- .../core/__tests__/transform/hidden_jest.js | 48 + .../core/__tests__/transform/issue834_test.js | 32 + .../core/__tests__/transform/issue846_test.js | 27 + .../__tests__/transform/issue_1581_test.js | 10 +- .../__tests__/transform/issue_4606_test.mjs | 2 +- .../__tests__/transform/issue_4621_test.mjs | 2 +- .../__tests__/transform/issue_4730_test.mjs | 2 +- .../__tests__/transform/issue_4734_test.mjs | 2 +- .../__tests__/transform/issue_5325_test.mjs | 2 +- .../__tests__/transform/issue_7806_test.mjs | 4 +- .../__tests__/transform/issue_8491_test.mjs | 2 +- .../__tests__/transform/issue_8674_test.mjs | 2 +- .../__tests__/transform/issue_8701_test.mjs | 2 +- .../core}/__tests__/transform/issues_test.mjs | 2 +- .../__tests__/transform/optimizer_test.js | 2 +- .../core}/__tests__/transform/plugin_test.js | 28 +- .../__tests__/transform/preset-env_test.mjs | 2 +- .../__tests__/transform/sourcemap_test.js | 86 +- .../core/__tests__/transform/swc_node_335.js | 25 + {node-swc => packages/core}/benches/load.mjs | 0 .../core}/benches/minify.mjs | 0 .../core}/benches/multicore.js | 0 {node-swc => packages/core}/benches/plugin.js | 0 .../core}/benches/transform.js | 0 .../core}/benches/typescript.js | 0 packages/core/binding.d.ts | 92 + packages/core/binding.js | 361 ++ ...pi.host.__plugin_transform_vtest.config.js | 0 ....host.plugin_transform_schema_v1.config.js | 0 .../plugin_transform_schema_v1/Cargo.lock | 0 .../plugin_transform_schema_v1/Cargo.toml | 2 +- .../plugin_transform_schema_v1/rust-toolchain | 0 .../plugin_transform_schema_v1/src/lib.rs | 0 .../plugin_transform_schema_vtest/Cargo.lock | 0 .../plugin_transform_schema_vtest/Cargo.toml | 16 + .../rust-toolchain | 0 .../plugin_transform_schema_vtest/src/lib.rs | 0 .../core}/e2e/plugins/plugins.compat.test.js | 0 .../core}/e2e/plugins/plugins.schema.test.js | 0 {node-swc => packages/core}/e2e/utils.js | 2 +- {node-swc => packages/core}/jest.config.js | 0 packages/core/package.json | 103 + packages/core/postinstall.d.ts | 1 + packages/core/postinstall.js | 227 ++ packages/core/postinstall.ts | 145 + .../core/scripts}/cli_artifacts.sh | 0 .../core/scripts}/npm/darwin-arm64/README.md | 0 .../scripts/npm/darwin-arm64/package.json | 42 + .../core/scripts}/npm/darwin-x64/README.md | 0 .../core/scripts/npm/darwin-x64/package.json | 42 + .../npm/linux-arm-gnueabihf/README.md | 0 .../npm/linux-arm-gnueabihf/package.json | 42 + .../scripts}/npm/linux-arm64-gnu/README.md | 0 .../scripts/npm/linux-arm64-gnu/package.json | 45 + .../scripts}/npm/linux-arm64-musl/README.md | 0 .../scripts/npm/linux-arm64-musl/package.json | 45 + .../core/scripts}/npm/linux-x64-gnu/README.md | 0 .../scripts/npm/linux-x64-gnu/package.json | 45 + .../scripts}/npm/linux-x64-musl/README.md | 0 .../scripts/npm/linux-x64-musl/package.json | 45 + .../scripts}/npm/win32-arm64-msvc/README.md | 0 .../scripts/npm/win32-arm64-msvc/package.json | 42 + .../scripts}/npm/win32-ia32-msvc/README.md | 0 .../scripts/npm/win32-ia32-msvc/package.json | 42 + .../scripts}/npm/win32-x64-msvc/README.md | 0 .../scripts/npm/win32-x64-msvc/package.json | 42 + packages/core/src/.gitignore | 2 + packages/core/src/Visitor.ts | 1907 +++++++++++ packages/core/src/index.ts | 540 +++ {node-swc => packages/core}/src/spack.ts | 67 +- {node-swc => packages/core}/src/util.ts | 34 +- .../core}/tests/dynamic-tsx/input.ts | 0 .../core}/tests/env/default/.swcrc | 0 .../core}/tests/env/default/input.js | 0 .../core}/tests/env/targets/.swcrc | 0 .../core}/tests/env/targets/input.js | 0 .../core}/tests/error/.swcrc | 0 .../core}/tests/error/simple.js | 0 .../core}/tests/issue-1581/.swcrc | 0 .../core}/tests/issue-1581/foo.ts | 0 .../core}/tests/issue-2120/.swcrc | 0 .../core}/tests/issue-2120/input.js | 0 .../core}/tests/issue-2546/.swcrc | 0 .../core}/tests/issue-2546/input.ts | 0 .../core}/tests/issue-2964/.swcrc | 0 .../core}/tests/issue-2964/input1.ts | 0 .../core}/tests/issue-2964/input2.ts | 0 .../core}/tests/issue-3834/.swcrc | 0 .../core}/tests/issue-3834/input.js | 0 .../core}/tests/issue-4606/1/.swcrc | 0 .../core}/tests/issue-4606/1/index.tsx | 0 .../core}/tests/issue-4606/2/.swcrc | 0 .../core}/tests/issue-4606/2/index.tsx | 0 .../core}/tests/issue-4621/1/.swcrc | 0 .../core}/tests/issue-4621/1/index.tsx | 0 .../tests/issue-4730/packages/a/src/index.ts | 0 .../tests/issue-4730/packages/b/src/index.ts | 0 .../core}/tests/issue-4730/src/index.ts | 0 .../core}/tests/issue-4734/1/.swcrc | 0 .../core}/tests/issue-4734/1/index.ts | 0 .../core}/tests/issue-8674/src/bar.ts | 0 .../core}/tests/issue-8674/src/foo.ts | 0 .../issue-8701/src/app.controller.spec.ts | 0 .../tests/issue-8701/src/app.controller.ts | 0 .../core}/tests/issue-8701/src/app.module.ts | 0 .../core}/tests/issue-8701/src/app.service.ts | 0 .../core}/tests/issue-8701/src/main.ts | 0 .../core}/tests/legacy/octal.js | 0 .../core}/tests/spack/config-swcrc/.swcrc | 0 .../core}/tests/spack/config-swcrc/common.js | 0 .../core}/tests/spack/config-swcrc/entry1.js | 0 .../core}/tests/spack/config-swcrc/entry2.js | 0 .../tests/spack/config-swcrc/spack.config.js | 0 .../core}/tests/spack/simple/a.js | 0 .../core}/tests/spack/simple/entry.js | 0 .../core}/tests/swc-path-bug-1/.swcrc | 0 .../core}/tests/swc-path-bug-1/src/index.ts | 0 .../src/utils/hello-world.utils.ts | 0 tsconfig.json => packages/core/tsconfig.json | 8 +- packages/package.json | 11 - packages/yarn.lock | 1964 ----------- postinstall.js | 147 - scripts/npm/darwin-arm64/package.json | 42 - scripts/npm/darwin-x64/package.json | 42 - scripts/npm/linux-arm-gnueabihf/package.json | 42 - scripts/npm/linux-arm64-gnu/package.json | 45 - scripts/npm/linux-arm64-musl/package.json | 45 - scripts/npm/linux-x64-gnu/package.json | 45 - scripts/npm/linux-x64-musl/package.json | 45 - scripts/npm/win32-arm64-msvc/package.json | 42 - scripts/npm/win32-ia32-msvc/package.json | 42 - scripts/npm/win32-x64-msvc/package.json | 42 - yarn.lock | 2958 +++++++++++------ 174 files changed, 6341 insertions(+), 7012 deletions(-) delete mode 100644 node-swc/__tests__/env/env_test.js delete mode 100644 node-swc/__tests__/import_test.js delete mode 100644 node-swc/__tests__/is_module_unknown_test.js delete mode 100644 node-swc/__tests__/parse/api_test.js delete mode 100644 node-swc/__tests__/script_test.js delete mode 100644 node-swc/__tests__/transform/hidden_jest.js delete mode 100644 node-swc/__tests__/transform/issue834_test.js delete mode 100644 node-swc/__tests__/transform/issue846_test.js delete mode 100644 node-swc/__tests__/transform/swc_node_335.js delete mode 100644 node-swc/e2e/fixtures/plugin_transform_schema_vtest/Cargo.toml delete mode 100644 node-swc/src/Visitor.ts delete mode 100644 node-swc/src/binding.d.ts delete mode 100644 node-swc/src/binding.js delete mode 100644 node-swc/src/index.ts delete mode 100644 node-swc/src/postinstall.ts rename {node-swc => packages/core}/.gitignore (100%) rename {node-swc => packages/core}/.npmignore (100%) rename {node-swc => packages/core}/__tests__/dynamic_tsx.mjs (98%) create mode 100644 packages/core/__tests__/env/env_test.js rename {node-swc => packages/core}/__tests__/error_test.mjs (95%) create mode 100644 packages/core/__tests__/import_test.js create mode 100644 packages/core/__tests__/is_module_unknown_test.js rename {node-swc => packages/core}/__tests__/minify/issue_6996_test.mjs (97%) rename {node-swc => packages/core}/__tests__/minify/issue_8437_test.mjs (63%) rename {node-swc => packages/core}/__tests__/minify_test.mjs (99%) rename {node-swc => packages/core}/__tests__/module_test.js (76%) create mode 100644 packages/core/__tests__/parse/api_test.js rename {node-swc => packages/core}/__tests__/paths_test.mjs (97%) rename {node-swc => packages/core}/__tests__/plugin_test.mjs (98%) rename {node-swc => packages/core}/__tests__/preserve_comments.mjs (93%) create mode 100644 packages/core/__tests__/script_test.js rename {node-swc => packages/core}/__tests__/spack/simple_test.js (96%) rename {node-swc => packages/core}/__tests__/transform/api_test.js (99%) rename {node-swc => packages/core}/__tests__/transform/experimental.mjs (96%) create mode 100644 packages/core/__tests__/transform/hidden_jest.js create mode 100644 packages/core/__tests__/transform/issue834_test.js create mode 100644 packages/core/__tests__/transform/issue846_test.js rename {node-swc => packages/core}/__tests__/transform/issue_1581_test.js (51%) rename {node-swc => packages/core}/__tests__/transform/issue_4606_test.mjs (99%) rename {node-swc => packages/core}/__tests__/transform/issue_4621_test.mjs (98%) rename {node-swc => packages/core}/__tests__/transform/issue_4730_test.mjs (98%) rename {node-swc => packages/core}/__tests__/transform/issue_4734_test.mjs (98%) rename {node-swc => packages/core}/__tests__/transform/issue_5325_test.mjs (88%) rename {node-swc => packages/core}/__tests__/transform/issue_7806_test.mjs (95%) rename {node-swc => packages/core}/__tests__/transform/issue_8491_test.mjs (96%) rename {node-swc => packages/core}/__tests__/transform/issue_8674_test.mjs (96%) rename {node-swc => packages/core}/__tests__/transform/issue_8701_test.mjs (98%) rename {node-swc => packages/core}/__tests__/transform/issues_test.mjs (90%) rename {node-swc => packages/core}/__tests__/transform/optimizer_test.js (94%) rename {node-swc => packages/core}/__tests__/transform/plugin_test.js (78%) rename {node-swc => packages/core}/__tests__/transform/preset-env_test.mjs (99%) rename {node-swc => packages/core}/__tests__/transform/sourcemap_test.js (57%) create mode 100644 packages/core/__tests__/transform/swc_node_335.js rename {node-swc => packages/core}/benches/load.mjs (100%) rename {node-swc => packages/core}/benches/minify.mjs (100%) rename {node-swc => packages/core}/benches/multicore.js (100%) rename {node-swc => packages/core}/benches/plugin.js (100%) rename {node-swc => packages/core}/benches/transform.js (100%) rename {node-swc => packages/core}/benches/typescript.js (100%) create mode 100644 packages/core/binding.d.ts create mode 100644 packages/core/binding.js rename {node-swc => packages/core}/e2e/fixtures/napi.host.__plugin_transform_vtest.config.js (100%) rename {node-swc => packages/core}/e2e/fixtures/napi.host.plugin_transform_schema_v1.config.js (100%) rename {node-swc => packages/core}/e2e/fixtures/plugin_transform_schema_v1/Cargo.lock (100%) rename {node-swc => packages/core}/e2e/fixtures/plugin_transform_schema_v1/Cargo.toml (92%) rename {node-swc => packages/core}/e2e/fixtures/plugin_transform_schema_v1/rust-toolchain (100%) rename {node-swc => packages/core}/e2e/fixtures/plugin_transform_schema_v1/src/lib.rs (100%) rename {node-swc => packages/core}/e2e/fixtures/plugin_transform_schema_vtest/Cargo.lock (100%) create mode 100644 packages/core/e2e/fixtures/plugin_transform_schema_vtest/Cargo.toml rename {node-swc => packages/core}/e2e/fixtures/plugin_transform_schema_vtest/rust-toolchain (100%) rename {node-swc => packages/core}/e2e/fixtures/plugin_transform_schema_vtest/src/lib.rs (100%) rename {node-swc => packages/core}/e2e/plugins/plugins.compat.test.js (100%) rename {node-swc => packages/core}/e2e/plugins/plugins.schema.test.js (100%) rename {node-swc => packages/core}/e2e/utils.js (95%) rename {node-swc => packages/core}/jest.config.js (100%) create mode 100644 packages/core/package.json create mode 100644 packages/core/postinstall.d.ts create mode 100644 packages/core/postinstall.js create mode 100644 packages/core/postinstall.ts rename {scripts => packages/core/scripts}/cli_artifacts.sh (100%) rename {scripts => packages/core/scripts}/npm/darwin-arm64/README.md (100%) create mode 100644 packages/core/scripts/npm/darwin-arm64/package.json rename {scripts => packages/core/scripts}/npm/darwin-x64/README.md (100%) create mode 100644 packages/core/scripts/npm/darwin-x64/package.json rename {scripts => packages/core/scripts}/npm/linux-arm-gnueabihf/README.md (100%) create mode 100644 packages/core/scripts/npm/linux-arm-gnueabihf/package.json rename {scripts => packages/core/scripts}/npm/linux-arm64-gnu/README.md (100%) create mode 100644 packages/core/scripts/npm/linux-arm64-gnu/package.json rename {scripts => packages/core/scripts}/npm/linux-arm64-musl/README.md (100%) create mode 100644 packages/core/scripts/npm/linux-arm64-musl/package.json rename {scripts => packages/core/scripts}/npm/linux-x64-gnu/README.md (100%) create mode 100644 packages/core/scripts/npm/linux-x64-gnu/package.json rename {scripts => packages/core/scripts}/npm/linux-x64-musl/README.md (100%) create mode 100644 packages/core/scripts/npm/linux-x64-musl/package.json rename {scripts => packages/core/scripts}/npm/win32-arm64-msvc/README.md (100%) create mode 100644 packages/core/scripts/npm/win32-arm64-msvc/package.json rename {scripts => packages/core/scripts}/npm/win32-ia32-msvc/README.md (100%) create mode 100644 packages/core/scripts/npm/win32-ia32-msvc/package.json rename {scripts => packages/core/scripts}/npm/win32-x64-msvc/README.md (100%) create mode 100644 packages/core/scripts/npm/win32-x64-msvc/package.json create mode 100644 packages/core/src/.gitignore create mode 100644 packages/core/src/Visitor.ts create mode 100644 packages/core/src/index.ts rename {node-swc => packages/core}/src/spack.ts (51%) rename {node-swc => packages/core}/src/util.ts (78%) rename {node-swc => packages/core}/tests/dynamic-tsx/input.ts (100%) rename {node-swc => packages/core}/tests/env/default/.swcrc (100%) rename {node-swc => packages/core}/tests/env/default/input.js (100%) rename {node-swc => packages/core}/tests/env/targets/.swcrc (100%) rename {node-swc => packages/core}/tests/env/targets/input.js (100%) rename {node-swc => packages/core}/tests/error/.swcrc (100%) rename {node-swc => packages/core}/tests/error/simple.js (100%) rename {node-swc => packages/core}/tests/issue-1581/.swcrc (100%) rename {node-swc => packages/core}/tests/issue-1581/foo.ts (100%) rename {node-swc => packages/core}/tests/issue-2120/.swcrc (100%) rename {node-swc => packages/core}/tests/issue-2120/input.js (100%) rename {node-swc => packages/core}/tests/issue-2546/.swcrc (100%) rename {node-swc => packages/core}/tests/issue-2546/input.ts (100%) rename {node-swc => packages/core}/tests/issue-2964/.swcrc (100%) rename {node-swc => packages/core}/tests/issue-2964/input1.ts (100%) rename {node-swc => packages/core}/tests/issue-2964/input2.ts (100%) rename {node-swc => packages/core}/tests/issue-3834/.swcrc (100%) rename {node-swc => packages/core}/tests/issue-3834/input.js (100%) rename {node-swc => packages/core}/tests/issue-4606/1/.swcrc (100%) rename {node-swc => packages/core}/tests/issue-4606/1/index.tsx (100%) rename {node-swc => packages/core}/tests/issue-4606/2/.swcrc (100%) rename {node-swc => packages/core}/tests/issue-4606/2/index.tsx (100%) rename {node-swc => packages/core}/tests/issue-4621/1/.swcrc (100%) rename {node-swc => packages/core}/tests/issue-4621/1/index.tsx (100%) rename {node-swc => packages/core}/tests/issue-4730/packages/a/src/index.ts (100%) rename {node-swc => packages/core}/tests/issue-4730/packages/b/src/index.ts (100%) rename {node-swc => packages/core}/tests/issue-4730/src/index.ts (100%) rename {node-swc => packages/core}/tests/issue-4734/1/.swcrc (100%) rename {node-swc => packages/core}/tests/issue-4734/1/index.ts (100%) rename {node-swc => packages/core}/tests/issue-8674/src/bar.ts (100%) rename {node-swc => packages/core}/tests/issue-8674/src/foo.ts (100%) rename {node-swc => packages/core}/tests/issue-8701/src/app.controller.spec.ts (100%) rename {node-swc => packages/core}/tests/issue-8701/src/app.controller.ts (100%) rename {node-swc => packages/core}/tests/issue-8701/src/app.module.ts (100%) rename {node-swc => packages/core}/tests/issue-8701/src/app.service.ts (100%) rename {node-swc => packages/core}/tests/issue-8701/src/main.ts (100%) rename {node-swc => packages/core}/tests/legacy/octal.js (100%) rename {node-swc => packages/core}/tests/spack/config-swcrc/.swcrc (100%) rename {node-swc => packages/core}/tests/spack/config-swcrc/common.js (100%) rename {node-swc => packages/core}/tests/spack/config-swcrc/entry1.js (100%) rename {node-swc => packages/core}/tests/spack/config-swcrc/entry2.js (100%) rename {node-swc => packages/core}/tests/spack/config-swcrc/spack.config.js (100%) rename {node-swc => packages/core}/tests/spack/simple/a.js (100%) rename {node-swc => packages/core}/tests/spack/simple/entry.js (100%) rename {node-swc => packages/core}/tests/swc-path-bug-1/.swcrc (100%) rename {node-swc => packages/core}/tests/swc-path-bug-1/src/index.ts (100%) rename {node-swc => packages/core}/tests/swc-path-bug-1/src/utils/hello-world.utils.ts (100%) rename tsconfig.json => packages/core/tsconfig.json (94%) delete mode 100644 packages/package.json delete mode 100644 packages/yarn.lock delete mode 100644 postinstall.js delete mode 100644 scripts/npm/darwin-arm64/package.json delete mode 100644 scripts/npm/darwin-x64/package.json delete mode 100644 scripts/npm/linux-arm-gnueabihf/package.json delete mode 100644 scripts/npm/linux-arm64-gnu/package.json delete mode 100644 scripts/npm/linux-arm64-musl/package.json delete mode 100644 scripts/npm/linux-x64-gnu/package.json delete mode 100644 scripts/npm/linux-x64-musl/package.json delete mode 100644 scripts/npm/win32-arm64-msvc/package.json delete mode 100644 scripts/npm/win32-ia32-msvc/package.json delete mode 100644 scripts/npm/win32-x64-msvc/package.json diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index d96f29cf2ae..7d855ce403e 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -146,6 +146,8 @@ jobs: # toolchain: stable # override: true + - run: corepack enable + - uses: actions/setup-node@v4 with: node-version: "18" @@ -154,6 +156,7 @@ jobs: - name: Install node dependencies shell: bash run: | + corepack enable yarn # Ensure that all components are compilable. @@ -168,6 +171,9 @@ jobs: steps: - uses: actions/checkout@v4 + - shell: bash + run: corepack enable + # We explicitly do this to cache properly. - uses: actions-rs/toolchain@v1 with: @@ -191,6 +197,7 @@ jobs: - name: Install node dependencies shell: bash run: | + corepack enable yarn - name: Build @@ -589,6 +596,8 @@ jobs: with: submodules: true + - run: corepack enable + # Source map format - uses: actions/setup-node@v4 with: @@ -613,6 +622,7 @@ jobs: - name: Install node dependencies shell: bash run: | + corepack enable yarn # I don't want to think deeply about this yarn global add jest@27 mocha || \ @@ -633,7 +643,7 @@ jobs: - name: Verify dependencies shell: bash run: | - jest --version && mocha --version + yarn jest --version && yarn mocha --version - name: Configure execution cache shell: bash @@ -725,8 +735,7 @@ jobs: steps: - uses: actions/checkout@v4 - - name: Corepack - shell: bash + - shell: bash run: corepack enable # We explicitly do this to cache properly. @@ -735,7 +744,7 @@ jobs: - uses: actions/setup-node@v4 with: - node-version: 20 + node-version: 18 cache: "yarn" - name: Patch @@ -761,7 +770,14 @@ jobs: corepack enable yarn + - name: Build + working-directory: packages/core + run: | yarn build:dev + + - name: Test + working-directory: packages/core + run: | yarn test integration-test: @@ -772,8 +788,7 @@ jobs: steps: - uses: actions/checkout@v4 - - name: Corepack - shell: bash + - shell: bash run: corepack enable - uses: actions/setup-node@v4 @@ -878,6 +893,9 @@ jobs: with: submodules: true + - shell: bash + run: corepack enable + - uses: actions/setup-node@v4 with: node-version: "18" @@ -885,7 +903,9 @@ jobs: - name: Install node dependencies shell: bash - run: yarn + run: | + corepack enable + yarn - name: Print rustup toolchain version shell: bash diff --git a/.github/workflows/publish-core.yml b/.github/workflows/publish-core.yml index 491daa4dd28..15549b93756 100644 --- a/.github/workflows/publish-core.yml +++ b/.github/workflows/publish-core.yml @@ -1,5 +1,8 @@ name: Publish (core) +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + env: DEBUG: napi:* APP_NAME: swc @@ -34,8 +37,8 @@ jobs: - host: macos-latest target: x86_64-apple-darwin build: | - cargo build --manifest-path ./bindings/swc_cli/Cargo.toml --release --features plugin --target x86_64-apple-darwin - cp ./bindings/target/x86_64-apple-darwin/release/swc . + cargo build --manifest-path ../../bindings/swc_cli/Cargo.toml --release --features plugin --target x86_64-apple-darwin + cp ../../bindings/target/x86_64-apple-darwin/release/swc . chmod +x ./swc yarn build --target x86_64-apple-darwin strip -x *.node @@ -43,16 +46,16 @@ jobs: build: | export CARGO_PROFILE_RELEASE_LTO=false yarn build --target x86_64-pc-windows-msvc - cargo build --manifest-path ./bindings/swc_cli/Cargo.toml --release --features plugin --target x86_64-pc-windows-msvc - cp ./bindings/target/x86_64-pc-windows-msvc/release/swc . + cargo build --manifest-path ../../bindings/swc_cli/Cargo.toml --release --features plugin --target x86_64-pc-windows-msvc + cp ../../bindings/target/x86_64-pc-windows-msvc/release/swc . target: x86_64-pc-windows-msvc - host: windows-latest # use npm to run test, because of corepack link the yarn with x64 Node.js build: | export DISABLE_PLUGIN_E2E_TESTS=true export CARGO_PROFILE_RELEASE_LTO=false - cargo build --manifest-path ./bindings/swc_cli/Cargo.toml --release --target i686-pc-windows-msvc - cp bindings/target/i686-pc-windows-msvc/release/swc . + cargo build --manifest-path ../../bindings/swc_cli/Cargo.toml --release --target i686-pc-windows-msvc + cp ../../bindings/target/i686-pc-windows-msvc/release/swc . yarn build --target i686-pc-windows-msvc --no-default-features --features swc_v1 npm run test target: i686-pc-windows-msvc @@ -61,9 +64,9 @@ jobs: docker: ghcr.io/napi-rs/napi-rs/nodejs-rust:lts-debian build: >- set -e && - unset CC_x86_64_unknown_linux_gnu && unset CC && RUSTFLAGS='-C target-feature=+sse2' cargo build --manifest-path ./bindings/swc_cli/Cargo.toml --release --features plugin --target x86_64-unknown-linux-gnu && - rm -rf bindings/target/target/x86_64-unknown-linux-gnu/release/.cargo-lock && - cp bindings/target/x86_64-unknown-linux-gnu/release/swc . && chmod +x ./swc && + unset CC_x86_64_unknown_linux_gnu && unset CC && RUSTFLAGS='-C target-feature=+sse2' cargo build --manifest-path ../../bindings/swc_cli/Cargo.toml --release --features plugin --target x86_64-unknown-linux-gnu && + rm -rf ../../bindings/target/target/x86_64-unknown-linux-gnu/release/.cargo-lock && + cp ../../bindings/target/x86_64-unknown-linux-gnu/release/swc . && chmod +x ./swc && yarn build --target x86_64-unknown-linux-gnu - host: ubuntu-latest target: x86_64-unknown-linux-musl @@ -72,9 +75,9 @@ jobs: build: >- set -e && export CARGO_PROFILE_RELEASE_LTO=false && - RUSTFLAGS='-C target-feature=+sse2' cargo build --manifest-path ./bindings/swc_cli/Cargo.toml --release --features plugin --target x86_64-unknown-linux-musl && + RUSTFLAGS='-C target-feature=+sse2' cargo build --manifest-path ../../bindings/swc_cli/Cargo.toml --release --features plugin --target x86_64-unknown-linux-musl && rm -rf target/release/.cargo-lock && - cp bindings/target/x86_64-unknown-linux-musl/release/swc . && chmod +x ./swc && + cp ../../bindings/target/x86_64-unknown-linux-musl/release/swc . && chmod +x ./swc && RUSTFLAGS='-C target-feature=+sse2 -C target-feature=-crt-static' yarn build --target x86_64-unknown-linux-musl - host: macos-latest target: aarch64-apple-darwin @@ -84,8 +87,8 @@ jobs: export CXX=$(xcrun -f clang++); SYSROOT=$(xcrun --sdk macosx --show-sdk-path); export CFLAGS="-isysroot $SYSROOT -isystem $SYSROOT"; - cargo build --manifest-path ./bindings/swc_cli/Cargo.toml --release --features plugin --target aarch64-apple-darwin - cp ./bindings/target/aarch64-apple-darwin/release/swc . + cargo build --manifest-path ../../bindings/swc_cli/Cargo.toml --release --features plugin --target aarch64-apple-darwin + cp ../../bindings/target/aarch64-apple-darwin/release/swc . chmod +x ./swc yarn build --target aarch64-apple-darwin - host: ubuntu-latest @@ -95,15 +98,15 @@ jobs: set -e && export JEMALLOC_SYS_WITH_LG_PAGE=16 && rustup target add aarch64-unknown-linux-gnu && - RUSTFLAGS='' cargo build --manifest-path ./bindings/swc_cli/Cargo.toml --release --features plugin --target aarch64-unknown-linux-gnu && - cp ./bindings/target/aarch64-unknown-linux-gnu/release/swc . && chmod +x ./swc && + RUSTFLAGS='' cargo build --manifest-path ../../bindings/swc_cli/Cargo.toml --release --features plugin --target aarch64-unknown-linux-gnu && + cp ../../bindings/target/aarch64-unknown-linux-gnu/release/swc . && chmod +x ./swc && export CC_aarch64_unknown_linux_gnu=/usr/aarch64-unknown-linux-gnu/bin/aarch64-unknown-linux-gnu-gcc && yarn build --target aarch64-unknown-linux-gnu - host: ubuntu-latest target: armv7-unknown-linux-gnueabihf build: | export DISABLE_PLUGIN_E2E_TESTS=true - yarn napi build --bin swc --release --target armv7-unknown-linux-gnueabihf --manifest-path ./bindings/swc_cli/Cargo.toml -x --target-dir ../release -o . + yarn napi build --bin swc --release --target armv7-unknown-linux-gnueabihf --manifest-path ../../bindings/swc_cli/Cargo.toml -x --target-dir ../release -o . chmod +x ./swc yarn build --target armv7-unknown-linux-gnueabihf --no-default-features --features swc_v1 --use-napi-cross - host: ubuntu-latest @@ -112,11 +115,11 @@ jobs: docker: ghcr.io/napi-rs/napi-rs/nodejs-rust:lts-alpine build: >- export JEMALLOC_SYS_WITH_LG_PAGE=16 && - rustup toolchain install $(cat ./rust-toolchain) && + rustup toolchain install $(cat ../../rust-toolchain) && rustup target add aarch64-unknown-linux-musl && - RUSTFLAGS='-C target-feature=+crt-static -C link-arg=-lgcc' cargo build --manifest-path ./bindings/swc_cli/Cargo.toml --release --features plugin --target aarch64-unknown-linux-musl && + RUSTFLAGS='-C target-feature=+crt-static -C link-arg=-lgcc' cargo build --manifest-path ../../bindings/swc_cli/Cargo.toml --release --features plugin --target aarch64-unknown-linux-musl && rm -rf target/release/.cargo-lock && - cp bindings/target/aarch64-unknown-linux-musl/release/swc . && chmod +x ./swc && + cp ../../bindings/target/aarch64-unknown-linux-musl/release/swc . && chmod +x ./swc && env RUSTFLAGS='-C target-feature=-crt-static' yarn build --target=aarch64-unknown-linux-musl - host: windows-latest target: aarch64-pc-windows-msvc @@ -125,8 +128,8 @@ jobs: export DISABLE_PLUGIN_E2E_TESTS=true export CARGO_PROFILE_RELEASE_CODEGEN_UNITS=256 export CARGO_PROFILE_RELEASE_LTO=false - cargo build --manifest-path ./bindings/swc_cli/Cargo.toml --release --target aarch64-pc-windows-msvc - cp bindings/target/aarch64-pc-windows-msvc/release/swc.exe . + cargo build --manifest-path ../../bindings/swc_cli/Cargo.toml --release --target aarch64-pc-windows-msvc + cp ../../bindings/target/aarch64-pc-windows-msvc/release/swc.exe . yarn build --target aarch64-pc-windows-msvc --no-default-features --features swc_v1 name: stable - ${{ matrix.settings.target }} - node@20 runs-on: ${{ matrix.settings.host }} @@ -197,10 +200,12 @@ jobs: image: ${{ matrix.settings.docker }} options: -v ${{ env.HOME }}/.cargo/git:/root/.cargo/git -v ${{ env.HOME }}/.cargo/registry:/root/.cargo/registry -v ${{ github.workspace }}:/build -w /build run: >- + cd ./packages/core && corepack enable && npm i -g wasm-pack && ${{ matrix.settings.build }} - name: Build + working-directory: ./packages/core if: ${{ !matrix.settings.docker }} run: ${{ matrix.settings.build }} shell: bash @@ -209,7 +214,7 @@ jobs: with: name: bindings-${{ matrix.settings.target }} path: | - swc* + packages/core/swc* if-no-files-found: error test-macOS-windows-binding: if: ${{ startsWith(github.ref, 'refs/tags/v') || startsWith(github.ref, 'refs/heads/ci') }} @@ -230,6 +235,8 @@ jobs: runs-on: ${{ matrix.settings.host }} steps: - uses: actions/checkout@v4 + - run: corepack enable + shell: bash - name: Setup node uses: actions/setup-node@v4 with: @@ -237,18 +244,20 @@ jobs: cache: yarn architecture: x64 - name: Install dependencies - run: yarn install --ignore-scripts --registry https://registry.npmjs.org --network-timeout 300000 + run: corepack enable && yarn install --network-timeout 300000 - name: Download artifacts uses: actions/download-artifact@v3 with: name: bindings-${{ matrix.settings.target }} - path: . + path: ./packages/core - name: List packages run: ls -R . shell: bash - name: Build TypeScript + working-directory: ./packages/core run: yarn build:ts - name: Test bindings + working-directory: ./packages/core run: yarn test test-linux-x64-gnu-binding: if: ${{ startsWith(github.ref, 'refs/tags/v') || startsWith(github.ref, 'refs/heads/ci') }} @@ -272,12 +281,12 @@ jobs: node-version: ${{ matrix.node }} cache: yarn - name: Install dependencies - run: yarn install + run: corepack enable && yarn install - name: Download artifacts uses: actions/download-artifact@v3 with: name: bindings-x86_64-unknown-linux-gnu - path: . + path: ./packages/core - name: List packages run: ls -R . shell: bash @@ -299,6 +308,8 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 + - run: corepack enable + shell: bash - name: Setup node uses: actions/setup-node@v4 with: @@ -306,13 +317,14 @@ jobs: cache: yarn - name: Install dependencies run: | + corepack enable yarn config set supportedArchitectures.libc "musl" yarn install - name: Download artifacts uses: actions/download-artifact@v3 with: name: bindings-x86_64-unknown-linux-musl - path: . + path: ./packages/core - name: List packages run: ls -R . shell: bash @@ -334,12 +346,13 @@ jobs: uses: actions/download-artifact@v3 with: name: bindings-aarch64-unknown-linux-musl - path: . + path: ./packages/core - name: List packages run: ls -R . shell: bash - name: Install dependencies run: | + corepack enable yarn config set supportedArchitectures.cpu "arm64" yarn config set supportedArchitectures.libc "musl" yarn install @@ -379,12 +392,13 @@ jobs: uses: actions/download-artifact@v3 with: name: bindings-armv7-unknown-linux-gnueabihf - path: . + path: ./packages/core - name: List packages run: ls -R . shell: bash - name: Install dependencies run: | + corepack enable yarn config set supportedArchitectures.cpu "arm" yarn install - name: Build TypeScript @@ -435,19 +449,20 @@ jobs: run: echo "NPM_TAG=nightly" >> $GITHUB_ENV - name: Install dependencies - run: yarn install + run: corepack enable && yarn install - name: Download all artifacts uses: actions/download-artifact@v3 with: - path: artifacts + path: ./packages/core/artifacts - name: List binaries - run: ls -R artifacts + run: ls -R ./packages/core/artifacts shell: bash - name: Move binaries shell: bash + working-directory: ./packages/core run: | ./scripts/cli_artifacts.sh ls -R ./artifacts_cli @@ -455,6 +470,7 @@ jobs: - name: List npm run: ls -R ./scripts/npm + working-directory: ./packages/core shell: bash - name: Set fallback dependencies diff --git a/.github/workflows/publish-extra-bindings.yml b/.github/workflows/publish-extra-bindings.yml index adc0662194a..2beffcece82 100644 --- a/.github/workflows/publish-extra-bindings.yml +++ b/.github/workflows/publish-extra-bindings.yml @@ -1,5 +1,9 @@ name: Publish (extra-bindings) +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: "${{ github.event_name == 'pull_request' }}" + env: DEBUG: napi:* APP_NAME: swc diff --git a/crates/swc/tests/exec.rs b/crates/swc/tests/exec.rs index 08550340de5..9fb3618f9ad 100644 --- a/crates/swc/tests/exec.rs +++ b/crates/swc/tests/exec.rs @@ -63,7 +63,6 @@ fn init_helpers() -> Arc { } let yarn = find_executable("yarn").expect("failed to find yarn"); - let npm = find_executable("npm").expect("failed to find npm"); { let mut cmd = if cfg!(target_os = "windows") { let mut c = Command::new("cmd"); @@ -90,25 +89,6 @@ fn init_helpers() -> Arc { assert!(status.success()); } - { - let mut cmd = if cfg!(target_os = "windows") { - let mut c = Command::new("cmd"); - c.arg("/C").arg(&npm); - c - } else { - Command::new(&npm) - }; - cmd.current_dir(&project_root) - .arg("install") - .arg("--no-save") - .arg("--no-package-lock") - .arg("./packages/helpers"); - let status = cmd - .status() - .expect("failed to install helper package from root"); - assert!(status.success()); - } - Arc::new(helper_dir) }); diff --git a/crates/swc/tests/projects.rs b/crates/swc/tests/projects.rs index fc20ec1e5d0..2bc29f0d8d2 100644 --- a/crates/swc/tests/projects.rs +++ b/crates/swc/tests/projects.rs @@ -1141,7 +1141,7 @@ fn issue_8674_1() { let base_url = current_dir() .unwrap() - .join("../../node-swc/tests/issue-8674") + .join("../../packages/core/tests/issue-8674") .canonicalize() .unwrap(); diff --git a/crates/testing/src/lib.rs b/crates/testing/src/lib.rs index d2de4a60238..219416edf55 100644 --- a/crates/testing/src/lib.rs +++ b/crates/testing/src/lib.rs @@ -1,9 +1,10 @@ use std::{ - env, fmt, - fmt::{Debug, Display, Formatter}, + env, + fmt::{self, Debug, Display, Formatter}, fs::{create_dir_all, rename, File}, io::Write, path::{Component, Path, PathBuf}, + process::Command, str::FromStr, sync::RwLock, thread, @@ -60,7 +61,7 @@ pub fn find_executable(name: &str) -> Option { } } - let path = env::var_os("PATH").and_then(|paths| { + let mut path = env::var_os("PATH").and_then(|paths| { env::split_paths(&paths) .filter_map(|dir| { let full_path = dir.join(name); @@ -73,6 +74,28 @@ pub fn find_executable(name: &str) -> Option { .next() }); + if path.is_none() { + // Run yarn bin $name + + path = Command::new("yarn") + .arg("bin") + .arg(name) + .output() + .ok() + .and_then(|output| { + if output.status.success() { + let path = String::from_utf8(output.stdout).ok()?; + let path = path.trim(); + let path = PathBuf::from(path); + if path.is_file() { + return Some(path); + } + } + + None + }); + } + if let Some(path) = path.clone() { let mut locked = CACHE.write().unwrap(); locked.insert(name.to_string(), path); diff --git a/node-swc/__tests__/env/env_test.js b/node-swc/__tests__/env/env_test.js deleted file mode 100644 index cb21c2e2da2..00000000000 --- a/node-swc/__tests__/env/env_test.js +++ /dev/null @@ -1,20 +0,0 @@ -const swc = require("../../../"); -const path = require("path"); - -// it("should handle browserslistrc", () => { -// const filename = path.resolve( -// __dirname + "/../../tests/env/default/input.js" -// ); -// expect(swc.transformFileSync(filename).code.trim()).toContain( -// `const a of foo` -// ); -// }); - -it("should handle targets in env", () => { - const filename = path.resolve( - __dirname + "/../../tests/env/targets/input.js" - ); - expect(swc.transformFileSync(filename).code.trim()).toContain( - `const a of foo` - ); -}); diff --git a/node-swc/__tests__/import_test.js b/node-swc/__tests__/import_test.js deleted file mode 100644 index 7b544ddad6e..00000000000 --- a/node-swc/__tests__/import_test.js +++ /dev/null @@ -1,5 +0,0 @@ - - -it('should work', () => { - require('../../') -}) \ No newline at end of file diff --git a/node-swc/__tests__/is_module_unknown_test.js b/node-swc/__tests__/is_module_unknown_test.js deleted file mode 100644 index 7a0373cb614..00000000000 --- a/node-swc/__tests__/is_module_unknown_test.js +++ /dev/null @@ -1,16 +0,0 @@ -const swc = require("../../"); - -it("should detect script", () => { - const script = swc.parseSync(`const fs = require('fs');`, { isModule: "unknown" }); - expect(script.type).toBe("Script"); -}); - -it("should default to isModule: true", () => { - const script = swc.parseSync(`foo;`, {}); - expect(script.type).toBe("Module"); -}); - -it("should detect module", () => { - const script = swc.parseSync(`import fs from "fs";`, { isModule: "unknown" }); - expect(script.type).toBe("Module"); -}); diff --git a/node-swc/__tests__/parse/api_test.js b/node-swc/__tests__/parse/api_test.js deleted file mode 100644 index f0d984e63b4..00000000000 --- a/node-swc/__tests__/parse/api_test.js +++ /dev/null @@ -1,31 +0,0 @@ -const swc = require("../../../"); - -it("should work asynchronously", async () => { - const m = await swc.parse(`class Foo {}`); - - expect(m.type).toBe(`Module`); - expect(m.body).toHaveLength(1); - expect(m.body[0].type).toBe(`ClassDeclaration`); -}); - -it("can work synchronously", () => { - const m = swc.parseSync(`class Foo {}`); - - expect(m.type).toBe(`Module`); - expect(m.body).toHaveLength(1); - expect(m.body[0].type).toBe(`ClassDeclaration`); -}); - -it("can be emit code back asynchronously", async () => { - const m = await swc.parse(`class Foo {}`); - const out = await swc.print(m); - - expect(out.code.trim().replace("\n", "")).toBe(`class Foo {}`); -}); - -it("can be emit code back synchronously", async () => { - const m = await swc.parseSync(`class Foo {}`); - const out = swc.printSync(m); - - expect(out.code.trim().replace("\n", "")).toBe(`class Foo {}`); -}); diff --git a/node-swc/__tests__/script_test.js b/node-swc/__tests__/script_test.js deleted file mode 100644 index 1895bf4e98e..00000000000 --- a/node-swc/__tests__/script_test.js +++ /dev/null @@ -1,22 +0,0 @@ -const swc = require("../../"); - -it("should handle script", () => { - const script = swc.transformSync(`foo;`, { isModule: false }); - expect(script.code.trim()).toBe(`foo;`); -}); - -it("should parse as script if required", () => { - const script = swc.parseSync(`foo;`, { isModule: false }); - expect(script.type).toBe("Script"); - - const out = swc.printSync(script); - expect(out.code.trim()).toBe(`foo;`); -}); - -it("should parse as module if required", () => { - const m = swc.parseSync(`foo;`); - expect(m.type).toBe("Module"); - - const out = swc.printSync(m); - expect(out.code.trim()).toBe(`foo;`); -}); diff --git a/node-swc/__tests__/transform/hidden_jest.js b/node-swc/__tests__/transform/hidden_jest.js deleted file mode 100644 index 606cfe07262..00000000000 --- a/node-swc/__tests__/transform/hidden_jest.js +++ /dev/null @@ -1,47 +0,0 @@ -const swc = require("../../../"); - -it("should hoist methods", () => { - const src = 'console.log("Hello"); jest.mock(); console.log("World")'; - - expect( - swc.transformSync(src, { - jsc: { - transform: { - hidden: { - jest: true - } - } - } - }) - .code.trim() - ).toBe(`jest.mock(); -console.log(\"Hello\"); -console.log(\"World\");`); -}); - - -it("should preserve calls", () => { - const src = `class Foo { - method() { - super.foo() - } - }`; - - expect( - swc.transformSync(src, { - jsc: { - transform: { - hidden: { - jest: true - } - }, - target: 'es2019' - } - }) - .code.trim() - ).toBe(`class Foo { - method() { - super.foo(); - } -}`); -}); diff --git a/node-swc/__tests__/transform/issue834_test.js b/node-swc/__tests__/transform/issue834_test.js deleted file mode 100644 index 79d9af1760c..00000000000 --- a/node-swc/__tests__/transform/issue834_test.js +++ /dev/null @@ -1,29 +0,0 @@ -const swc = require('../../../'); - -it('should work without parser.syntax', () => { - const out = swc.transformSync(`const someValue = "test" ?? "default value";`, { - jsc: { - parser: { - nullishCoalescing: true, - numericSeparator: true - }, - } - } - ); - expect(out.map).toBeFalsy(); -}); - - -it('should work with parser.syntax', () => { - const out = swc.transformSync(`const someValue = "test" ?? "default value";`, { - jsc: { - parser: { - syntax: 'ecmascript', - nullishCoalescing: true, - numericSeparator: true - }, - } - } - ); - expect(out.map).toBeFalsy(); -}); diff --git a/node-swc/__tests__/transform/issue846_test.js b/node-swc/__tests__/transform/issue846_test.js deleted file mode 100644 index f508978b582..00000000000 --- a/node-swc/__tests__/transform/issue846_test.js +++ /dev/null @@ -1,24 +0,0 @@ -const swc = require('../../../'); - - -it("should handle es2019", () => { - expect( - swc.transformSync(`class SomeClass { - someMethod() {} -} - -class OtherClass extends SomeClass { - anotherMethod() { - super.someMethod() - } -}`, { - jsc: { - parser: { - syntax: "ecmascript", - }, - target: "es2019", - } - }) - .code.trim() - ).toContain(`class `); -}); \ No newline at end of file diff --git a/node-swc/__tests__/transform/swc_node_335.js b/node-swc/__tests__/transform/swc_node_335.js deleted file mode 100644 index 0714d6e29ce..00000000000 --- a/node-swc/__tests__/transform/swc_node_335.js +++ /dev/null @@ -1,22 +0,0 @@ -const swc = require('../../..'); - - -it("should handle es2019", () => { - expect( - swc.transformSync(` - class Foo { - method() { - class Foo { - - } - } - } - `, { - jsc: { - keepClassNames: true, - target: 'es2017', - } - }) - .code.trim() - ).toContain(`let Foo = class Foo {`); -}); \ No newline at end of file diff --git a/node-swc/e2e/fixtures/plugin_transform_schema_vtest/Cargo.toml b/node-swc/e2e/fixtures/plugin_transform_schema_vtest/Cargo.toml deleted file mode 100644 index 86030b010bb..00000000000 --- a/node-swc/e2e/fixtures/plugin_transform_schema_vtest/Cargo.toml +++ /dev/null @@ -1,14 +0,0 @@ -[workspace] - -[package] -edition = "2021" -name = "plugin_transform_schema_vtest" -publish = false -version = "0.1.0" - -[lib] -crate-type = ["cdylib"] - -[dependencies] -serde = "1.0.140" -swc_core = { path = "../../../../crates/swc_core", features = ["__plugin_transform_schema_test"] } diff --git a/node-swc/src/Visitor.ts b/node-swc/src/Visitor.ts deleted file mode 100644 index 7b7b2b84eee..00000000000 --- a/node-swc/src/Visitor.ts +++ /dev/null @@ -1,1867 +0,0 @@ -import { - Accessibility, - Argument, - ArrayExpression, - ArrayPattern, - ArrowFunctionExpression, - AssignmentExpression, - AssignmentPattern, - AssignmentPatternProperty, - AssignmentProperty, - AwaitExpression, - BigIntLiteral, - BinaryExpression, - BindingIdentifier, - BlockStatement, - BooleanLiteral, - BreakStatement, - CallExpression, - CatchClause, - Class, - ClassDeclaration, - ClassExpression, - ClassMember, - ClassMethod, - ClassProperty, - ComputedPropName, - ConditionalExpression, - Constructor, - ContinueStatement, - DebuggerStatement, - Declaration, - Decorator, - DefaultDecl, - DoWhileStatement, - EmptyStatement, - ExportAllDeclaration, - ExportDeclaration, - ExportDefaultDeclaration, - ExportDefaultExpression, - ExportDefaultSpecifier, - ExportNamedDeclaration, - ExportNamespaceSpecifier, - ExportSpecifier, - ExprOrSpread, - Expression, - ExpressionStatement, - Fn, - ForInStatement, - ForOfStatement, - ForStatement, - FunctionDeclaration, - FunctionExpression, - GetterProperty, - Identifier, - IfStatement, - Import, - ImportDeclaration, - ImportDefaultSpecifier, - ImportNamespaceSpecifier, - ImportSpecifier, - JSXAttrValue, - JSXAttribute, - JSXAttributeName, - JSXAttributeOrSpread, - JSXClosingElement, - JSXClosingFragment, - JSXElement, - JSXElementChild, - JSXElementName, - JSXEmptyExpression, - JSXExpressionContainer, - JSXFragment, - JSXMemberExpression, - JSXNamespacedName, - JSXObject, - JSXOpeningElement, - JSXOpeningFragment, - JSXSpreadChild, - JSXText, - KeyValuePatternProperty, - KeyValueProperty, - LabeledStatement, - MemberExpression, - MetaProperty, - MethodProperty, - Module, - ModuleDeclaration, - ModuleExportName, - ModuleItem, - NamedExportSpecifier, - NamedImportSpecifier, - NewExpression, - NullLiteral, - NumericLiteral, - ObjectExpression, - ObjectPattern, - ObjectPatternProperty, - OptionalChainingCall, - OptionalChainingExpression, - Param, - ParenthesisExpression, - Pattern, - PrivateMethod, - PrivateName, - PrivateProperty, - Program, - Property, - PropertyName, - RegExpLiteral, - RestElement, - ReturnStatement, - Script, - SequenceExpression, - SetterProperty, - SpreadElement, - Statement, - StaticBlock, - StringLiteral, - Super, - SuperPropExpression, - SwitchCase, - SwitchStatement, - TaggedTemplateExpression, - TemplateLiteral, - ThisExpression, - ThrowStatement, - TryStatement, - TsAsExpression, - TsCallSignatureDeclaration, - TsConstAssertion, - TsConstructSignatureDeclaration, - TsEntityName, - TsEnumDeclaration, - TsEnumMember, - TsEnumMemberId, - TsExportAssignment, - TsExpressionWithTypeArguments, - TsExternalModuleReference, - TsFnParameter, - TsGetterSignature, - TsImportEqualsDeclaration, - TsIndexSignature, - TsInstantiation, - TsInterfaceBody, - TsInterfaceDeclaration, - TsMethodSignature, - TsModuleBlock, - TsModuleDeclaration, - TsModuleName, - TsModuleReference, - TsNamespaceBody, - TsNamespaceDeclaration, - TsNamespaceExportDeclaration, - TsNonNullExpression, - TsParameterProperty, - TsParameterPropertyParameter, - TsPropertySignature, - TsQualifiedName, - TsSatisfiesExpression, - TsSetterSignature, - TsType, - TsTypeAliasDeclaration, - TsTypeAnnotation, - TsTypeAssertion, - TsTypeElement, - TsTypeParameter, - TsTypeParameterDeclaration, - TsTypeParameterInstantiation, - UnaryExpression, - UpdateExpression, - VariableDeclaration, - VariableDeclarator, - WhileStatement, - WithStatement, - YieldExpression, -} from "@swc/types"; - - -/** - * @deprecated JavaScript API is deprecated. Please use Wasm plugin instead. - */ -export class Visitor { - visitProgram(n: Program): Program { - switch (n.type) { - case "Module": - return this.visitModule(n); - case "Script": - return this.visitScript(n) - } - } - - visitModule(m: Module): Module { - m.body = this.visitModuleItems(m.body); - return m; - } - - visitScript(m: Script): Script { - m.body = this.visitStatements(m.body); - return m; - } - - visitModuleItems(items: ModuleItem[]): ModuleItem[] { - return items.map(this.visitModuleItem.bind(this)); - } - - visitModuleItem(n: ModuleItem): ModuleItem { - switch (n.type) { - case "ExportDeclaration": - case "ExportDefaultDeclaration": - case "ExportNamedDeclaration": - case "ExportDefaultExpression": - case "ImportDeclaration": - case "ExportAllDeclaration": - case "TsImportEqualsDeclaration": - case "TsExportAssignment": - case "TsNamespaceExportDeclaration": - return this.visitModuleDeclaration(n); - default: - return this.visitStatement(n); - } - } - - visitModuleDeclaration(n: ModuleDeclaration): ModuleDeclaration { - switch (n.type) { - case "ExportDeclaration": - return this.visitExportDeclaration(n); - case "ExportDefaultDeclaration": - return this.visitExportDefaultDeclaration(n); - case "ExportNamedDeclaration": - return this.visitExportNamedDeclaration(n); - case "ExportDefaultExpression": - return this.visitExportDefaultExpression(n); - case "ImportDeclaration": - return this.visitImportDeclaration(n); - case "ExportAllDeclaration": - return this.visitExportAllDeclaration(n); - case "TsImportEqualsDeclaration": - return this.visitTsImportEqualsDeclaration(n); - case "TsExportAssignment": - return this.visitTsExportAssignment(n); - case "TsNamespaceExportDeclaration": - return this.visitTsNamespaceExportDeclaration(n); - } - } - - visitTsNamespaceExportDeclaration( - n: TsNamespaceExportDeclaration - ): ModuleDeclaration { - n.id = this.visitBindingIdentifier(n.id); - return n; - } - - visitTsExportAssignment(n: TsExportAssignment): TsExportAssignment { - n.expression = this.visitExpression(n.expression); - - return n; - } - - visitTsImportEqualsDeclaration( - n: TsImportEqualsDeclaration - ): ModuleDeclaration { - n.id = this.visitBindingIdentifier(n.id); - n.moduleRef = this.visitTsModuleReference(n.moduleRef); - return n; - } - - visitTsModuleReference(n: TsModuleReference): TsModuleReference { - switch (n.type) { - case "Identifier": - return this.visitIdentifierReference(n); - case "TsExternalModuleReference": - return this.visitTsExternalModuleReference(n); - case "TsQualifiedName": - return this.visitTsQualifiedName(n); - } - } - - visitTsExternalModuleReference( - n: TsExternalModuleReference - ): TsExternalModuleReference { - n.expression = this.visitStringLiteral(n.expression); - return n; - } - - visitExportAllDeclaration(n: ExportAllDeclaration): ModuleDeclaration { - n.source = this.visitStringLiteral(n.source); - return n; - } - - visitExportDefaultExpression(n: ExportDefaultExpression): ModuleDeclaration { - n.expression = this.visitExpression(n.expression); - return n; - } - - visitExportNamedDeclaration(n: ExportNamedDeclaration): ModuleDeclaration { - n.specifiers = this.visitExportSpecifiers(n.specifiers); - n.source = this.visitOptionalStringLiteral(n.source); - return n; - } - - visitExportSpecifiers(nodes: ExportSpecifier[]): ExportSpecifier[] { - return nodes.map(this.visitExportSpecifier.bind(this)); - } - - visitExportSpecifier(n: ExportSpecifier): ExportSpecifier { - switch (n.type) { - case "ExportDefaultSpecifier": - return this.visitExportDefaultSpecifier(n); - case "ExportNamespaceSpecifier": - return this.visitExportNamespaceSpecifier(n); - case "ExportSpecifier": - return this.visitNamedExportSpecifier(n); - } - } - visitNamedExportSpecifier(n: NamedExportSpecifier): ExportSpecifier { - if (n.exported) { - n.exported = this.visitModuleExportName(n.exported); - } - n.orig = this.visitModuleExportName(n.orig); - return n; - } - - visitModuleExportName(n: ModuleExportName): ModuleExportName { - switch (n.type) { - case "Identifier": - return this.visitIdentifier(n); - case "StringLiteral": - return this.visitStringLiteral(n); - } - } - - visitExportNamespaceSpecifier(n: ExportNamespaceSpecifier): ExportSpecifier { - n.name = this.visitModuleExportName(n.name); - return n; - } - - visitExportDefaultSpecifier(n: ExportDefaultSpecifier): ExportSpecifier { - n.exported = this.visitBindingIdentifier(n.exported); - return n; - } - - visitOptionalStringLiteral( - n: StringLiteral | undefined - ): StringLiteral | undefined { - if (n) { - return this.visitStringLiteral(n); - } - } - - visitExportDefaultDeclaration( - n: ExportDefaultDeclaration - ): ModuleDeclaration { - n.decl = this.visitDefaultDeclaration(n.decl); - return n; - } - - visitDefaultDeclaration(n: DefaultDecl): DefaultDecl { - switch (n.type) { - case "ClassExpression": - return this.visitClassExpression(n); - case "FunctionExpression": - return this.visitFunctionExpression(n); - case "TsInterfaceDeclaration": - return this.visitTsInterfaceDeclaration(n); - } - } - - visitFunctionExpression(n: FunctionExpression): FunctionExpression { - n = this.visitFunction(n); - if (n.identifier) { - n.identifier = this.visitBindingIdentifier(n.identifier); - } - return n; - } - - visitClassExpression(n: ClassExpression): ClassExpression { - n = this.visitClass(n); - if (n.identifier) { - n.identifier = this.visitBindingIdentifier(n.identifier); - } - return n; - } - - visitExportDeclaration(n: ExportDeclaration): ModuleDeclaration { - n.declaration = this.visitDeclaration(n.declaration); - return n; - } - - visitArrayExpression(e: ArrayExpression): Expression { - if (e.elements) { - e.elements = e.elements.map(this.visitArrayElement.bind(this)); - } - return e; - } - - visitArrayElement( - e: ExprOrSpread | undefined - ): ExprOrSpread | undefined { - if (e) { - return this.visitExprOrSpread(e); - } - } - - visitExprOrSpread(e: ExprOrSpread): ExprOrSpread { - return { - ...e, - expression: this.visitExpression(e.expression) - } - } - visitExprOrSpreads(nodes: ExprOrSpread[]): ExprOrSpread[] { - return nodes.map(this.visitExprOrSpread.bind(this)); - } - - visitSpreadElement(e: SpreadElement): SpreadElement { - e.arguments = this.visitExpression(e.arguments); - return e; - } - - visitOptionalExpression(e: Expression | undefined): Expression | undefined { - if (e) { - return this.visitExpression(e); - } - } - - visitArrowFunctionExpression(e: ArrowFunctionExpression): Expression { - e.body = this.visitArrowBody(e.body); - e.params = this.visitPatterns(e.params); - e.returnType = this.visitTsTypeAnnotation(e.returnType); - e.typeParameters = this.visitTsTypeParameterDeclaration(e.typeParameters); - - return e; - } - - visitArrowBody( - body: BlockStatement | Expression - ): BlockStatement | Expression { - switch (body.type) { - case "BlockStatement": - return this.visitBlockStatement(body); - default: - return this.visitExpression(body); - } - } - - visitBlockStatement(block: BlockStatement): BlockStatement { - block.stmts = this.visitStatements(block.stmts); - - return block; - } - - visitStatements(stmts: Statement[]): Statement[] { - return stmts.map(this.visitStatement.bind(this)); - } - - visitStatement(stmt: Statement): Statement { - switch (stmt.type) { - case "ClassDeclaration": - case "FunctionDeclaration": - case "TsEnumDeclaration": - case "TsInterfaceDeclaration": - case "TsModuleDeclaration": - case "TsTypeAliasDeclaration": - case "VariableDeclaration": - return this.visitDeclaration(stmt); - - case "BreakStatement": - return this.visitBreakStatement(stmt); - case "BlockStatement": - return this.visitBlockStatement(stmt); - case "ContinueStatement": - return this.visitContinueStatement(stmt); - case "DebuggerStatement": - return this.visitDebuggerStatement(stmt); - case "DoWhileStatement": - return this.visitDoWhileStatement(stmt); - case "EmptyStatement": - return this.visitEmptyStatement(stmt); - case "ForInStatement": - return this.visitForInStatement(stmt); - case "ForOfStatement": - return this.visitForOfStatement(stmt); - case "ForStatement": - return this.visitForStatement(stmt); - case "IfStatement": - return this.visitIfStatement(stmt); - case "LabeledStatement": - return this.visitLabeledStatement(stmt); - case "ReturnStatement": - return this.visitReturnStatement(stmt); - case "SwitchStatement": - return this.visitSwitchStatement(stmt); - case "ThrowStatement": - return this.visitThrowStatement(stmt); - case "TryStatement": - return this.visitTryStatement(stmt); - case "WhileStatement": - return this.visitWhileStatement(stmt); - case "WithStatement": - return this.visitWithStatement(stmt); - case "ExpressionStatement": - return this.visitExpressionStatement(stmt); - - default: - throw new Error(`Unknown statement type: ` + (stmt as any).type); - } - } - - visitSwitchStatement(stmt: SwitchStatement): Statement { - stmt.discriminant = this.visitExpression(stmt.discriminant); - stmt.cases = this.visitSwitchCases(stmt.cases); - return stmt; - } - - visitSwitchCases(cases: SwitchCase[]): SwitchCase[] { - return cases.map(this.visitSwitchCase.bind(this)); - } - - visitSwitchCase(c: SwitchCase): SwitchCase { - c.test = this.visitOptionalExpression(c.test); - c.consequent = this.visitStatements(c.consequent); - - return c; - } - - visitIfStatement(stmt: IfStatement): Statement { - stmt.test = this.visitExpression(stmt.test); - stmt.consequent = this.visitStatement(stmt.consequent); - stmt.alternate = this.visitOptionalStatement(stmt.alternate); - - return stmt; - } - - visitOptionalStatement(stmt: Statement | undefined): Statement | undefined { - if (stmt) { - return this.visitStatement(stmt); - } - } - - visitBreakStatement(stmt: BreakStatement): Statement { - if (stmt.label) { - stmt.label = this.visitLabelIdentifier(stmt.label); - } - - return stmt; - } - - visitWhileStatement(stmt: WhileStatement): Statement { - stmt.test = this.visitExpression(stmt.test); - stmt.body = this.visitStatement(stmt.body); - return stmt; - } - - visitTryStatement(stmt: TryStatement): Statement { - stmt.block = this.visitBlockStatement(stmt.block); - stmt.handler = this.visitCatchClause(stmt.handler); - if (stmt.finalizer) { - stmt.finalizer = this.visitBlockStatement(stmt.finalizer); - } - return stmt; - } - - visitCatchClause(handler: CatchClause | undefined): CatchClause | undefined { - if (handler) { - if (handler.param) { - handler.param = this.visitPattern(handler.param); - } - - handler.body = this.visitBlockStatement(handler.body); - } - - return handler; - } - - visitThrowStatement(stmt: ThrowStatement): Statement { - stmt.argument = this.visitExpression(stmt.argument); - return stmt; - } - - visitReturnStatement(stmt: ReturnStatement): Statement { - if (stmt.argument) { - stmt.argument = this.visitExpression(stmt.argument); - } - return stmt; - } - - visitLabeledStatement(stmt: LabeledStatement): Statement { - stmt.label = this.visitLabelIdentifier(stmt.label); - stmt.body = this.visitStatement(stmt.body); - - return stmt; - } - - visitForStatement(stmt: ForStatement): Statement { - if (stmt.init) { - if (stmt.init.type === "VariableDeclaration") { - stmt.init = this.visitVariableDeclaration(stmt.init); - } else { - stmt.init = this.visitOptionalExpression(stmt.init); - } - } - - stmt.test = this.visitOptionalExpression(stmt.test); - stmt.update = this.visitOptionalExpression(stmt.update); - stmt.body = this.visitStatement(stmt.body); - - return stmt; - } - - visitForOfStatement(stmt: ForOfStatement): Statement { - if (stmt.left.type === "VariableDeclaration") { - stmt.left = this.visitVariableDeclaration(stmt.left); - } else { - stmt.left = this.visitPattern(stmt.left); - } - stmt.right = this.visitExpression(stmt.right); - stmt.body = this.visitStatement(stmt.body); - return stmt; - } - - visitForInStatement(stmt: ForInStatement): Statement { - if (stmt.left.type === "VariableDeclaration") { - stmt.left = this.visitVariableDeclaration(stmt.left); - } else { - stmt.left = this.visitPattern(stmt.left); - } - stmt.right = this.visitExpression(stmt.right); - stmt.body = this.visitStatement(stmt.body); - return stmt; - } - - visitEmptyStatement(stmt: EmptyStatement): EmptyStatement { - return stmt; - } - - visitDoWhileStatement(stmt: DoWhileStatement): Statement { - stmt.body = this.visitStatement(stmt.body); - stmt.test = this.visitExpression(stmt.test); - return stmt; - } - - visitDebuggerStatement(stmt: DebuggerStatement): Statement { - return stmt; - } - - visitWithStatement(stmt: WithStatement): Statement { - stmt.object = this.visitExpression(stmt.object); - stmt.body = this.visitStatement(stmt.body); - return stmt; - } - - visitDeclaration(decl: Declaration): Declaration { - switch (decl.type) { - case "ClassDeclaration": - return this.visitClassDeclaration(decl); - case "FunctionDeclaration": - return this.visitFunctionDeclaration(decl); - case "TsEnumDeclaration": - return this.visitTsEnumDeclaration(decl); - case "TsInterfaceDeclaration": - return this.visitTsInterfaceDeclaration(decl); - case "TsModuleDeclaration": - return this.visitTsModuleDeclaration(decl); - case "TsTypeAliasDeclaration": - return this.visitTsTypeAliasDeclaration(decl); - case "VariableDeclaration": - return this.visitVariableDeclaration(decl); - } - } - - visitVariableDeclaration(n: VariableDeclaration): VariableDeclaration { - n.declarations = this.visitVariableDeclarators(n.declarations); - return n; - } - - visitVariableDeclarators(nodes: VariableDeclarator[]): VariableDeclarator[] { - return nodes.map(this.visitVariableDeclarator.bind(this)); - } - - visitVariableDeclarator(n: VariableDeclarator): VariableDeclarator { - n.id = this.visitPattern(n.id); - n.init = this.visitOptionalExpression(n.init); - return n; - } - - visitTsTypeAliasDeclaration(n: TsTypeAliasDeclaration): Declaration { - n.id = this.visitBindingIdentifier(n.id); - n.typeAnnotation = this.visitTsType(n.typeAnnotation); - n.typeParams = this.visitTsTypeParameterDeclaration(n.typeParams); - return n; - } - - visitTsModuleDeclaration(n: TsModuleDeclaration): Declaration { - n.id = this.visitTsModuleName(n.id); - if (n.body) { - n.body = this.visitTsNamespaceBody(n.body); - } - return n; - } - - visitTsModuleName(n: TsModuleName): TsModuleName { - switch (n.type) { - case "Identifier": - return this.visitBindingIdentifier(n); - case "StringLiteral": - return this.visitStringLiteral(n); - } - } - - visitTsNamespaceBody(n: TsNamespaceBody): TsNamespaceBody | undefined { - if (n) { - switch (n.type) { - case "TsModuleBlock": - return this.visitTsModuleBlock(n); - case "TsNamespaceDeclaration": - return this.visitTsNamespaceDeclaration(n); - } - } - } - - visitTsNamespaceDeclaration( - n: TsNamespaceDeclaration - ): TsModuleBlock | TsNamespaceDeclaration { - const body = this.visitTsNamespaceBody(n.body); - if (body) { - n.body = body; - } - n.id = this.visitBindingIdentifier(n.id); - return n; - } - - visitTsModuleBlock(n: TsModuleBlock): TsModuleBlock | TsNamespaceDeclaration { - n.body = this.visitModuleItems(n.body); - return n; - } - - visitTsInterfaceDeclaration( - n: TsInterfaceDeclaration - ): TsInterfaceDeclaration { - n.id = this.visitBindingIdentifier(n.id); - n.typeParams = this.visitTsTypeParameterDeclaration(n.typeParams); - n.extends = this.visitTsExpressionsWithTypeArguments(n.extends); - n.body = this.visitTsInterfaceBody(n.body); - return n; - } - - visitTsInterfaceBody(n: TsInterfaceBody): TsInterfaceBody { - n.body = this.visitTsTypeElements(n.body); - return n; - } - - visitTsTypeElements(nodes: TsTypeElement[]): TsTypeElement[] { - return nodes.map(this.visitTsTypeElement.bind(this)); - } - - visitTsTypeElement(n: TsTypeElement): TsTypeElement { - switch (n.type) { - case "TsCallSignatureDeclaration": - return this.visitTsCallSignatureDeclaration(n); - case "TsConstructSignatureDeclaration": - return this.visitTsConstructSignatureDeclaration(n); - case "TsPropertySignature": - return this.visitTsPropertySignature(n); - case "TsGetterSignature": - return this.visitTsGetterSignature(n); - case "TsSetterSignature": - return this.visitTsSetterSignature(n); - case "TsMethodSignature": - return this.visitTsMethodSignature(n); - case "TsIndexSignature": - return this.visitTsIndexSignature(n); - } - } - - visitTsCallSignatureDeclaration(n: TsCallSignatureDeclaration): TsCallSignatureDeclaration { - n.params = this.visitTsFnParameters(n.params); - n.typeAnnotation = this.visitTsTypeAnnotation(n.typeAnnotation); - return n; - } - visitTsConstructSignatureDeclaration(n: TsConstructSignatureDeclaration): TsConstructSignatureDeclaration { - n.params = this.visitTsFnParameters(n.params); - n.typeAnnotation = this.visitTsTypeAnnotation(n.typeAnnotation); - return n; - } - visitTsPropertySignature(n: TsPropertySignature): TsPropertySignature { - n.params = this.visitTsFnParameters(n.params); - n.typeAnnotation = this.visitTsTypeAnnotation(n.typeAnnotation); - return n; - } - visitTsGetterSignature(n: TsGetterSignature): TsGetterSignature { - n.typeAnnotation = this.visitTsTypeAnnotation(n.typeAnnotation); - return n; - } - visitTsSetterSignature(n: TsSetterSignature): TsSetterSignature { - n.param = this.visitTsFnParameter(n.param); - return n; - } - visitTsMethodSignature(n: TsMethodSignature): TsMethodSignature { - n.params = this.visitTsFnParameters(n.params); - n.typeAnn = this.visitTsTypeAnnotation(n.typeAnn); - return n; - } - - visitTsEnumDeclaration(n: TsEnumDeclaration): Declaration { - n.id = this.visitIdentifier(n.id); - n.members = this.visitTsEnumMembers(n.members); - return n; - } - - visitTsEnumMembers(nodes: TsEnumMember[]): TsEnumMember[] { - return nodes.map(this.visitTsEnumMember.bind(this)); - } - - visitTsEnumMember(n: TsEnumMember): TsEnumMember { - n.id = this.visitTsEnumMemberId(n.id); - n.init = this.visitOptionalExpression(n.init); - return n; - } - - visitTsEnumMemberId(n: TsEnumMemberId): TsEnumMemberId { - switch (n.type) { - case "Identifier": - return this.visitBindingIdentifier(n); - case "StringLiteral": - return this.visitStringLiteral(n); - } - } - - visitFunctionDeclaration(decl: FunctionDeclaration): Declaration { - decl.identifier = this.visitIdentifier(decl.identifier); - decl = this.visitFunction(decl); - - return decl; - } - - visitClassDeclaration(decl: ClassDeclaration): Declaration { - decl = this.visitClass(decl); - decl.identifier = this.visitIdentifier(decl.identifier); - return decl; - } - - visitClassBody(members: ClassMember[]): ClassMember[] { - return members.map(this.visitClassMember.bind(this)); - } - - visitClassMember(member: ClassMember): ClassMember { - switch (member.type) { - case "ClassMethod": - return this.visitClassMethod(member); - case "ClassProperty": - return this.visitClassProperty(member); - case "Constructor": - return this.visitConstructor(member); - case "PrivateMethod": - return this.visitPrivateMethod(member); - case "PrivateProperty": - return this.visitPrivateProperty(member); - case "TsIndexSignature": - return this.visitTsIndexSignature(member); - case "EmptyStatement": - return this.visitEmptyStatement(member); - case "StaticBlock": - return this.visitStaticBlock(member); - } - } - - visitTsIndexSignature(n: TsIndexSignature): TsIndexSignature { - n.params = this.visitTsFnParameters(n.params); - if (n.typeAnnotation) n.typeAnnotation = this.visitTsTypeAnnotation(n.typeAnnotation); - return n; - } - - visitTsFnParameters(params: TsFnParameter[]): TsFnParameter[] { - return params.map(this.visitTsFnParameter.bind(this)); - } - - visitTsFnParameter(n: TsFnParameter): TsFnParameter { - n.typeAnnotation = this.visitTsTypeAnnotation(n.typeAnnotation); - return n; - } - - visitPrivateProperty(n: PrivateProperty): ClassMember { - n.decorators = this.visitDecorators(n.decorators); - n.key = this.visitPrivateName(n.key); - n.typeAnnotation = this.visitTsTypeAnnotation(n.typeAnnotation); - n.value = this.visitOptionalExpression(n.value); - return n; - } - - visitPrivateMethod(n: PrivateMethod): ClassMember { - n.accessibility = this.visitAccessibility(n.accessibility); - n.function = this.visitFunction(n.function); - n.key = this.visitPrivateName(n.key); - return n; - } - - visitPrivateName(n: PrivateName): PrivateName { - return n; - } - - visitConstructor(n: Constructor): ClassMember { - n.accessibility = this.visitAccessibility(n.accessibility); - n.key = this.visitPropertyName(n.key); - n.params = this.visitConstructorParameters(n.params); - if (n.body) { - n.body = this.visitBlockStatement(n.body); - } - return n; - } - - visitConstructorParameters( - nodes: (Param | TsParameterProperty)[] - ): (Param | TsParameterProperty)[] { - return nodes.map(this.visitConstructorParameter.bind(this)); - } - - visitConstructorParameter( - n: Param | TsParameterProperty - ): Param | TsParameterProperty { - switch (n.type) { - case "TsParameterProperty": - return this.visitTsParameterProperty(n); - default: - return this.visitParameter(n); - } - } - - visitStaticBlock(n: StaticBlock): StaticBlock { - n.body = this.visitBlockStatement(n.body); - return n; - } - - visitTsParameterProperty( - n: TsParameterProperty - ): TsParameterProperty | Param { - n.accessibility = this.visitAccessibility(n.accessibility); - n.decorators = this.visitDecorators(n.decorators); - n.param = this.visitTsParameterPropertyParameter(n.param); - return n; - } - - visitTsParameterPropertyParameter( - n: TsParameterPropertyParameter - ): TsParameterPropertyParameter { - n.typeAnnotation = this.visitTsTypeAnnotation(n.typeAnnotation); - return n; - } - - visitPropertyName(key: PropertyName): PropertyName { - switch (key.type) { - case "Identifier": - return this.visitBindingIdentifier(key); - case "StringLiteral": - return this.visitStringLiteral(key); - case "NumericLiteral": - return this.visitNumericLiteral(key); - case "BigIntLiteral": - return this.visitBigIntLiteral(key); - default: - return this.visitComputedPropertyKey(key); - } - } - - visitAccessibility(n: Accessibility | undefined): Accessibility | undefined { - return n; - } - - visitClassProperty(n: ClassProperty): ClassMember { - n.accessibility = this.visitAccessibility(n.accessibility); - n.decorators = this.visitDecorators(n.decorators); - n.key = this.visitPropertyName(n.key); - n.typeAnnotation = this.visitTsTypeAnnotation(n.typeAnnotation); - n.value = this.visitOptionalExpression(n.value); - return n; - } - - visitClassMethod(n: ClassMethod): ClassMember { - n.accessibility = this.visitAccessibility(n.accessibility); - n.function = this.visitFunction(n.function); - n.key = this.visitPropertyName(n.key); - return n; - } - - visitComputedPropertyKey(n: ComputedPropName): ComputedPropName { - n.expression = this.visitExpression(n.expression); - return n; - } - - visitClass(n: T): T { - n.decorators = this.visitDecorators(n.decorators); - - n.superClass = this.visitOptionalExpression(n.superClass); - - n.superTypeParams = this.visitTsTypeParameterInstantiation( - n.superTypeParams - ); - if (n.implements) { - n.implements = this.visitTsExpressionsWithTypeArguments(n.implements); - } - - n.body = this.visitClassBody(n.body); - return n; - } - - visitFunction(n: T): T { - n.decorators = this.visitDecorators(n.decorators); - n.params = this.visitParameters(n.params); - if (n.body) { - n.body = this.visitBlockStatement(n.body); - } - n.returnType = this.visitTsTypeAnnotation(n.returnType); - n.typeParameters = this.visitTsTypeParameterDeclaration(n.typeParameters); - return n; - } - - visitTsExpressionsWithTypeArguments( - nodes: TsExpressionWithTypeArguments[] - ): TsExpressionWithTypeArguments[] { - return nodes.map(this.visitTsExpressionWithTypeArguments.bind(this)); - } - - visitTsExpressionWithTypeArguments( - n: TsExpressionWithTypeArguments - ): TsExpressionWithTypeArguments { - n.expression = this.visitExpression(n.expression); - n.typeArguments = this.visitTsTypeParameterInstantiation(n.typeArguments); - return n; - } - - visitTsTypeParameterInstantiation( - n: TsTypeParameterInstantiation | undefined - ): TsTypeParameterInstantiation | undefined { - if (n) { - n.params = this.visitTsTypes(n.params); - } - return n; - } - - visitTsTypes(nodes: TsType[]): TsType[] { - return nodes.map(this.visitTsType.bind(this)); - } - - visitTsEntityName(n: TsEntityName): TsEntityName { - switch (n.type) { - case "Identifier": - return this.visitBindingIdentifier(n); - case "TsQualifiedName": - return this.visitTsQualifiedName(n); - } - } - - visitTsQualifiedName(n: TsQualifiedName): TsQualifiedName { - n.left = this.visitTsEntityName(n.left); - n.right = this.visitIdentifier(n.right); - return n; - } - - visitDecorators(nodes: Decorator[] | undefined): Decorator[] | undefined { - if (nodes) { - return nodes.map(this.visitDecorator.bind(this)); - } - } - - visitDecorator(n: Decorator): Decorator { - n.expression = this.visitExpression(n.expression); - - return n; - } - - visitExpressionStatement(stmt: ExpressionStatement): Statement { - stmt.expression = this.visitExpression(stmt.expression); - return stmt; - } - - visitContinueStatement(stmt: ContinueStatement): Statement { - if (stmt.label) { - stmt.label = this.visitLabelIdentifier(stmt.label); - } - return stmt; - } - - visitExpression(n: Expression): Expression { - switch (n.type) { - case "ArrayExpression": - return this.visitArrayExpression(n); - case "ArrowFunctionExpression": - return this.visitArrowFunctionExpression(n); - case "AssignmentExpression": - return this.visitAssignmentExpression(n); - case "AwaitExpression": - return this.visitAwaitExpression(n); - case "BigIntLiteral": - return this.visitBigIntLiteral(n); - case "BinaryExpression": - return this.visitBinaryExpression(n); - case "BooleanLiteral": - return this.visitBooleanLiteral(n); - case "CallExpression": - return this.visitCallExpression(n); - case "ClassExpression": - return this.visitClassExpression(n); - case "ConditionalExpression": - return this.visitConditionalExpression(n); - case "FunctionExpression": - return this.visitFunctionExpression(n); - case "Identifier": - return this.visitIdentifierReference(n); - case "JSXElement": - return this.visitJSXElement(n); - case "JSXEmptyExpression": - return this.visitJSXEmptyExpression(n); - case "JSXFragment": - return this.visitJSXFragment(n); - case "JSXMemberExpression": - return this.visitJSXMemberExpression(n); - case "JSXNamespacedName": - return this.visitJSXNamespacedName(n); - case "JSXText": - return this.visitJSXText(n); - case "MemberExpression": - return this.visitMemberExpression(n); - case "SuperPropExpression": - return this.visitSuperPropExpression(n); - case "MetaProperty": - return this.visitMetaProperty(n); - case "NewExpression": - return this.visitNewExpression(n); - case "NullLiteral": - return this.visitNullLiteral(n); - case "NumericLiteral": - return this.visitNumericLiteral(n); - case "ObjectExpression": - return this.visitObjectExpression(n); - case "ParenthesisExpression": - return this.visitParenthesisExpression(n); - case "PrivateName": - return this.visitPrivateName(n); - case "RegExpLiteral": - return this.visitRegExpLiteral(n); - case "SequenceExpression": - return this.visitSequenceExpression(n); - case "StringLiteral": - return this.visitStringLiteral(n); - case "TaggedTemplateExpression": - return this.visitTaggedTemplateExpression(n); - case "TemplateLiteral": - return this.visitTemplateLiteral(n); - case "ThisExpression": - return this.visitThisExpression(n); - case "TsAsExpression": - return this.visitTsAsExpression(n); - case "TsSatisfiesExpression": - return this.visitTsSatisfiesExpression(n); - case "TsNonNullExpression": - return this.visitTsNonNullExpression(n); - case "TsTypeAssertion": - return this.visitTsTypeAssertion(n); - case "TsConstAssertion": - return this.visitTsConstAssertion(n); - case "TsInstantiation": - return this.visitTsInstantiation(n); - case "UnaryExpression": - return this.visitUnaryExpression(n); - case "UpdateExpression": - return this.visitUpdateExpression(n); - case "YieldExpression": - return this.visitYieldExpression(n); - case "OptionalChainingExpression": - return this.visitOptionalChainingExpression(n); - case "Invalid": - return n; - } - } - visitOptionalChainingExpression(n: OptionalChainingExpression): Expression { - n.base = this.visitMemberExpressionOrOptionalChainingCall(n.base); - return n; - } - - visitMemberExpressionOrOptionalChainingCall(n: MemberExpression | OptionalChainingCall): MemberExpression | OptionalChainingCall { - switch (n.type) { - case "MemberExpression": - return this.visitMemberExpression(n); - case "CallExpression": - return this.visitOptionalChainingCall(n); - } - } - - visitOptionalChainingCall(n: OptionalChainingCall): OptionalChainingCall { - n.callee = this.visitExpression(n.callee); - n.arguments = this.visitExprOrSpreads(n.arguments); - if (n.typeArguments) n.typeArguments = this.visitTsTypeParameterInstantiation(n.typeArguments); - return n; - } - - visitAssignmentExpression(n: AssignmentExpression): Expression { - n.left = this.visitPatternOrExpression(n.left); - n.right = this.visitExpression(n.right); - return n; - } - - visitPatternOrExpression(n: Pattern | Expression): Pattern | Expression { - switch (n.type) { - case "ObjectPattern": - case "ArrayPattern": - case "Identifier": - case "AssignmentPattern": - case "RestElement": - return this.visitPattern(n); - default: - return this.visitExpression(n); - } - } - - visitYieldExpression(n: YieldExpression): Expression { - n.argument = this.visitOptionalExpression(n.argument); - return n; - } - - visitUpdateExpression(n: UpdateExpression): Expression { - n.argument = this.visitExpression(n.argument); - return n; - } - - visitUnaryExpression(n: UnaryExpression): Expression { - n.argument = this.visitExpression(n.argument); - return n; - } - - visitTsTypeAssertion(n: TsTypeAssertion): Expression { - n.expression = this.visitExpression(n.expression); - n.typeAnnotation = this.visitTsType(n.typeAnnotation); - return n; - } - - - visitTsConstAssertion(n: TsConstAssertion): Expression { - n.expression = this.visitExpression(n.expression); - return n; - } - - visitTsInstantiation(n: TsInstantiation): TsInstantiation { - n.expression = this.visitExpression(n.expression); - return n; - } - - visitTsNonNullExpression(n: TsNonNullExpression): Expression { - n.expression = this.visitExpression(n.expression); - return n; - } - - visitTsAsExpression(n: TsAsExpression): Expression { - n.expression = this.visitExpression(n.expression); - n.typeAnnotation = this.visitTsType(n.typeAnnotation); - return n; - } - - visitTsSatisfiesExpression(n: TsSatisfiesExpression): Expression { - n.expression = this.visitExpression(n.expression); - n.typeAnnotation = this.visitTsType(n.typeAnnotation); - return n; - } - - visitThisExpression(n: ThisExpression): Expression { - return n; - } - - visitTemplateLiteral(n: TemplateLiteral): Expression { - n.expressions = n.expressions.map(this.visitExpression.bind(this)); - return n; - } - - visitParameters(n: Param[]): Param[] { - return n.map(this.visitParameter.bind(this)) - } - - visitParameter(n: Param): Param { - n.pat = this.visitPattern(n.pat); - return n; - } - - visitTaggedTemplateExpression(n: TaggedTemplateExpression): Expression { - n.tag = this.visitExpression(n.tag); - const template = this.visitTemplateLiteral(n.template); - if (template.type === "TemplateLiteral") { - n.template = template; - } - return n; - } - - visitSequenceExpression(n: SequenceExpression): Expression { - n.expressions = n.expressions.map(this.visitExpression.bind(this)); - return n; - } - - visitRegExpLiteral(n: RegExpLiteral): Expression { - return n; - } - - visitParenthesisExpression(n: ParenthesisExpression): Expression { - n.expression = this.visitExpression(n.expression); - return n; - } - - visitObjectExpression(n: ObjectExpression): Expression { - if (n.properties) { - n.properties = this.visitObjectProperties(n.properties); - } - return n; - } - - visitObjectProperties( - nodes: (Property | SpreadElement)[] - ): (Property | SpreadElement)[] { - return nodes.map(this.visitObjectProperty.bind(this)); - } - - visitObjectProperty(n: Property | SpreadElement): Property | SpreadElement { - switch (n.type) { - case "SpreadElement": - return this.visitSpreadElement(n); - default: - return this.visitProperty(n); - } - } - - visitProperty(n: Property): Property | SpreadElement { - switch (n.type) { - case "Identifier": - return this.visitIdentifier(n); - case "AssignmentProperty": - return this.visitAssignmentProperty(n); - case "GetterProperty": - return this.visitGetterProperty(n); - case "KeyValueProperty": - return this.visitKeyValueProperty(n); - case "MethodProperty": - return this.visitMethodProperty(n); - case "SetterProperty": - return this.visitSetterProperty(n); - } - } - - visitSetterProperty(n: SetterProperty): Property | SpreadElement { - n.key = this.visitPropertyName(n.key); - n.param = this.visitPattern(n.param); - if (n.body) { - n.body = this.visitBlockStatement(n.body); - } - return n; - } - - visitMethodProperty(n: MethodProperty): Property | SpreadElement { - n.key = this.visitPropertyName(n.key); - if (n.body) { - n.body = this.visitBlockStatement(n.body); - } - n.decorators = this.visitDecorators(n.decorators); - n.params = this.visitParameters(n.params); - n.returnType = this.visitTsTypeAnnotation(n.returnType); - n.typeParameters = this.visitTsTypeParameterDeclaration(n.typeParameters); - return n; - } - - visitKeyValueProperty(n: KeyValueProperty): Property | SpreadElement { - n.key = this.visitPropertyName(n.key); - n.value = this.visitExpression(n.value); - return n; - } - - visitGetterProperty(n: GetterProperty): Property | SpreadElement { - n.key = this.visitPropertyName(n.key); - if (n.body) { - n.body = this.visitBlockStatement(n.body); - } - n.typeAnnotation = this.visitTsTypeAnnotation(n.typeAnnotation); - return n; - } - - visitAssignmentProperty(n: AssignmentProperty): Property | SpreadElement { - n.key = this.visitIdentifier(n.key); - n.value = this.visitExpression(n.value); - return n; - } - - visitNullLiteral(n: NullLiteral): NullLiteral { - return n; - } - - visitNewExpression(n: NewExpression): Expression { - n.callee = this.visitExpression(n.callee); - if (n.arguments) { - n.arguments = this.visitArguments(n.arguments); - } - n.typeArguments = this.visitTsTypeArguments(n.typeArguments); - return n; - } - - visitTsTypeArguments( - n: TsTypeParameterInstantiation | undefined - ): TsTypeParameterInstantiation | undefined { - if (n) { - n.params = this.visitTsTypes(n.params); - } - return n; - } - - visitArguments(nodes: Argument[]): Argument[] { - return nodes.map(this.visitArgument.bind(this)); - } - - visitArgument(n: Argument): Argument { - n.expression = this.visitExpression(n.expression); - return n; - } - - visitMetaProperty(n: MetaProperty): Expression { - return n; - } - - visitMemberExpression(n: MemberExpression): MemberExpression { - n.object = this.visitExpression(n.object); - switch (n.property.type) { - case 'Computed': { - n.property = this.visitComputedPropertyKey(n.property); - return n; - } - case 'Identifier': { - n.property = this.visitIdentifier(n.property); - return n; - } - case 'PrivateName': { - n.property = this.visitPrivateName(n.property); - return n; - } - } - } - - visitSuperPropExpression(n: SuperPropExpression): Expression { - switch (n.property.type) { - case 'Computed': { - n.property = this.visitComputedPropertyKey(n.property); - return n; - } - case 'Identifier': { - n.property = this.visitIdentifier(n.property); - return n; - } - } - } - - visitCallee(n: Expression | Super | Import): Expression | Super | Import { - if (n.type === "Super" || n.type === "Import") { - return n; - } - return this.visitExpression(n); - } - - visitJSXText(n: JSXText): JSXText { - return n; - } - - visitJSXNamespacedName(n: JSXNamespacedName): JSXNamespacedName { - n.namespace = this.visitIdentifierReference(n.namespace); - n.name = this.visitIdentifierReference(n.name); - return n; - } - - visitJSXMemberExpression(n: JSXMemberExpression): JSXMemberExpression { - n.object = this.visitJSXObject(n.object); - n.property = this.visitIdentifierReference(n.property); - return n; - } - - visitJSXObject(n: JSXObject): JSXObject { - switch (n.type) { - case "Identifier": - return this.visitIdentifierReference(n); - case "JSXMemberExpression": - return this.visitJSXMemberExpression(n); - } - } - - visitJSXFragment(n: JSXFragment): JSXFragment { - n.opening = this.visitJSXOpeningFragment(n.opening); - if (n.children) { - n.children = this.visitJSXElementChildren(n.children); - } - n.closing = this.visitJSXClosingFragment(n.closing); - return n; - } - - visitJSXClosingFragment(n: JSXClosingFragment): JSXClosingFragment { - return n; - } - - visitJSXElementChildren(nodes: JSXElementChild[]): JSXElementChild[] { - return nodes.map(this.visitJSXElementChild.bind(this)); - } - - visitJSXElementChild(n: JSXElementChild): JSXElementChild { - switch (n.type) { - case "JSXElement": - return this.visitJSXElement(n); - case "JSXExpressionContainer": - return this.visitJSXExpressionContainer(n); - case "JSXFragment": - return this.visitJSXFragment(n); - case "JSXSpreadChild": - return this.visitJSXSpreadChild(n); - case "JSXText": - return this.visitJSXText(n); - } - } - - visitJSXExpressionContainer( - n: JSXExpressionContainer - ): JSXExpressionContainer { - n.expression = this.visitExpression(n.expression); - return n; - } - - visitJSXSpreadChild(n: JSXSpreadChild): JSXElementChild { - n.expression = this.visitExpression(n.expression); - return n; - } - - visitJSXOpeningFragment(n: JSXOpeningFragment): JSXOpeningFragment { - return n; - } - - visitJSXEmptyExpression(n: JSXEmptyExpression): Expression { - return n; - } - - visitJSXElement(n: JSXElement): JSXElement { - n.opening = this.visitJSXOpeningElement(n.opening); - n.children = this.visitJSXElementChildren(n.children); - n.closing = this.visitJSXClosingElement(n.closing); - return n; - } - - visitJSXClosingElement( - n: JSXClosingElement | undefined - ): JSXClosingElement | undefined { - if (n) { - n.name = this.visitJSXElementName(n.name); - } - return n; - } - - visitJSXElementName(n: JSXElementName): JSXElementName { - switch (n.type) { - case "Identifier": - return this.visitIdentifierReference(n); - case "JSXMemberExpression": - return this.visitJSXMemberExpression(n); - case "JSXNamespacedName": - return this.visitJSXNamespacedName(n); - } - } - - visitJSXOpeningElement(n: JSXOpeningElement): JSXOpeningElement { - n.name = this.visitJSXElementName(n.name); - n.typeArguments = this.visitTsTypeParameterInstantiation(n.typeArguments); - n.attributes = this.visitJSXAttributeOrSpreads(n.attributes); - return n; - } - - visitJSXAttributes( - attrs: JSXAttributeOrSpread[] | undefined - ): JSXAttributeOrSpread[] | undefined { - if (attrs) return attrs.map(this.visitJSXAttributeOrSpread.bind(this)); - } - - visitJSXAttributeOrSpread(n: JSXAttributeOrSpread): JSXAttributeOrSpread { - switch (n.type) { - case "JSXAttribute": - return this.visitJSXAttribute(n); - case "SpreadElement": - return this.visitSpreadElement(n); - } - } - visitJSXAttributeOrSpreads(nodes: JSXAttributeOrSpread[]): JSXAttributeOrSpread[] { - return nodes.map(this.visitJSXAttributeOrSpread.bind(this)); - } - - visitJSXAttribute(n: JSXAttribute): JSXAttributeOrSpread { - n.name = this.visitJSXAttributeName(n.name); - n.value = this.visitJSXAttributeValue(n.value); - return n; - } - - visitJSXAttributeValue( - n: JSXAttrValue | undefined - ): JSXAttrValue | undefined { - if (!n) return n; - - switch (n.type) { - case "BooleanLiteral": - return this.visitBooleanLiteral(n); - case "NullLiteral": - return this.visitNullLiteral(n); - case "NumericLiteral": - return this.visitNumericLiteral(n); - case "JSXText": - return this.visitJSXText(n); - case "StringLiteral": - return this.visitStringLiteral(n); - - case "JSXElement": - return this.visitJSXElement(n); - case "JSXExpressionContainer": - return this.visitJSXExpressionContainer(n); - case "JSXFragment": - return this.visitJSXFragment(n); - } - return n; - } - - visitJSXAttributeName(n: JSXAttributeName): JSXAttributeName { - switch (n.type) { - case "Identifier": - return this.visitIdentifierReference(n); - case "JSXNamespacedName": - return this.visitJSXNamespacedName(n); - } - } - - visitConditionalExpression(n: ConditionalExpression): Expression { - n.test = this.visitExpression(n.test); - n.consequent = this.visitExpression(n.consequent); - n.alternate = this.visitExpression(n.alternate); - return n; - } - - visitCallExpression(n: CallExpression): Expression { - n.callee = this.visitCallee(n.callee); - n.typeArguments = this.visitTsTypeParameterInstantiation(n.typeArguments); - if (n.arguments) { - n.arguments = this.visitArguments(n.arguments); - } - - return n; - } - - visitBooleanLiteral(n: BooleanLiteral): BooleanLiteral { - return n; - } - - visitBinaryExpression(n: BinaryExpression): Expression { - n.left = this.visitExpression(n.left); - n.right = this.visitExpression(n.right); - return n; - } - - visitAwaitExpression(n: AwaitExpression): Expression { - n.argument = this.visitExpression(n.argument); - return n; - } - - visitTsTypeParameterDeclaration( - n: TsTypeParameterDeclaration | undefined - ): TsTypeParameterDeclaration | undefined { - if (n) { - n.parameters = this.visitTsTypeParameters(n.parameters); - } - return n; - } - - visitTsTypeParameters(nodes: TsTypeParameter[]): TsTypeParameter[] { - return nodes.map(this.visitTsTypeParameter.bind(this)); - } - - visitTsTypeParameter(n: TsTypeParameter): TsTypeParameter { - if (n.constraint) { - n.constraint = this.visitTsType(n.constraint); - } - if (n.default) { - n.default = this.visitTsType(n.default); - } - n.name = this.visitIdentifierReference(n.name); - return n; - } - - visitTsTypeAnnotation( - a: TsTypeAnnotation | undefined - ): TsTypeAnnotation | undefined { - if (a) { - a.typeAnnotation = this.visitTsType(a.typeAnnotation); - } - return a; - } - - visitTsType(n: TsType): TsType { - throw new Error("Method visitTsType not implemented."); - } - - visitPatterns(nodes: Pattern[]): Pattern[] { - return nodes.map(this.visitPattern.bind(this)); - } - - visitImportDeclaration(n: ImportDeclaration): ImportDeclaration { - n.source = this.visitStringLiteral(n.source); - n.specifiers = this.visitImportSpecifiers(n.specifiers || []); - return n; - } - - visitImportSpecifiers(nodes: ImportSpecifier[]): ImportSpecifier[] { - return nodes.map(this.visitImportSpecifier.bind(this)); - } - - visitImportSpecifier(node: ImportSpecifier): ImportSpecifier { - switch (node.type) { - case "ImportDefaultSpecifier": - return this.visitImportDefaultSpecifier(node); - - case "ImportNamespaceSpecifier": - return this.visitImportNamespaceSpecifier(node); - - case "ImportSpecifier": - return this.visitNamedImportSpecifier(node); - } - } - visitNamedImportSpecifier(node: NamedImportSpecifier): NamedImportSpecifier { - node.local = this.visitBindingIdentifier(node.local); - - if (node.imported) { - node.imported = this.visitModuleExportName(node.imported); - } - - return node; - } - - visitImportNamespaceSpecifier( - node: ImportNamespaceSpecifier - ): ImportNamespaceSpecifier { - node.local = this.visitBindingIdentifier(node.local); - - return node; - } - - visitImportDefaultSpecifier(node: ImportDefaultSpecifier): ImportSpecifier { - node.local = this.visitBindingIdentifier(node.local); - - return node; - } - - visitBindingIdentifier(i: BindingIdentifier): BindingIdentifier { - if (i.typeAnnotation) { - i.typeAnnotation = this.visitTsTypeAnnotation(i.typeAnnotation); - } - return this.visitIdentifier(i); - } - - visitIdentifierReference(i: Identifier): Identifier { - return this.visitIdentifier(i); - } - - visitLabelIdentifier(label: Identifier): Identifier { - return this.visitIdentifier(label); - } - - visitIdentifier(n: Identifier): Identifier { - return n; - } - - visitStringLiteral(n: StringLiteral): StringLiteral { - return n; - } - - visitNumericLiteral(n: NumericLiteral): NumericLiteral { - return n; - } - - visitBigIntLiteral(n: BigIntLiteral): BigIntLiteral { - return n; - } - - visitPattern(n: Pattern): Pattern { - switch (n.type) { - case "Identifier": - return this.visitBindingIdentifier(n); - case "ArrayPattern": - return this.visitArrayPattern(n); - case "ObjectPattern": - return this.visitObjectPattern(n); - case "AssignmentPattern": - return this.visitAssignmentPattern(n); - case "RestElement": - return this.visitRestElement(n); - default: - return this.visitExpression(n); - } - } - - visitRestElement(n: RestElement): RestElement { - n.argument = this.visitPattern(n.argument); - n.typeAnnotation = this.visitTsTypeAnnotation(n.typeAnnotation); - return n; - } - - visitAssignmentPattern(n: AssignmentPattern): Pattern { - n.left = this.visitPattern(n.left); - n.right = this.visitExpression(n.right); - n.typeAnnotation = this.visitTsTypeAnnotation(n.typeAnnotation); - return n; - } - - visitObjectPattern(n: ObjectPattern): Pattern { - n.properties = this.visitObjectPatternProperties(n.properties || []); - n.typeAnnotation = this.visitTsTypeAnnotation(n.typeAnnotation); - return n; - } - - visitObjectPatternProperties( - nodes: ObjectPatternProperty[] - ): ObjectPatternProperty[] { - return nodes.map(this.visitObjectPatternProperty.bind(this)); - } - - visitObjectPatternProperty(n: ObjectPatternProperty): ObjectPatternProperty { - switch (n.type) { - case "AssignmentPatternProperty": - return this.visitAssignmentPatternProperty(n); - case "KeyValuePatternProperty": - return this.visitKeyValuePatternProperty(n); - case "RestElement": - return this.visitRestElement(n); - } - } - - visitKeyValuePatternProperty( - n: KeyValuePatternProperty - ): ObjectPatternProperty { - n.key = this.visitPropertyName(n.key); - n.value = this.visitPattern(n.value); - return n; - } - - visitAssignmentPatternProperty( - n: AssignmentPatternProperty - ): ObjectPatternProperty { - n.key = this.visitBindingIdentifier(n.key); - n.value = this.visitOptionalExpression(n.value); - return n; - } - - visitArrayPattern(n: ArrayPattern): Pattern { - n.typeAnnotation = this.visitTsTypeAnnotation(n.typeAnnotation); - n.elements = this.visitArrayPatternElements(n.elements); - return n; - } - - visitArrayPatternElements( - nodes: (Pattern | undefined)[] - ): (Pattern | undefined)[] { - return nodes.map(this.visitArrayPatternElement.bind(this)); - } - - visitArrayPatternElement(n: Pattern | undefined): Pattern | undefined { - if (n) { - n = this.visitPattern(n); - } - return n; - } -} - -export default Visitor; diff --git a/node-swc/src/binding.d.ts b/node-swc/src/binding.d.ts deleted file mode 100644 index 6277eb39ed6..00000000000 --- a/node-swc/src/binding.d.ts +++ /dev/null @@ -1,49 +0,0 @@ -/* auto-generated by NAPI-RS */ -/* eslint-disable */ - -export class Compiler { - constructor() -} -export type JsCompiler = Compiler - -export function bundle(confItems: Buffer, signal?: AbortSignal | undefined | null): Promise<{ [index: string]: { code: string, map?: string } }> - -export function getTargetTriple(): string - -export function initCustomTraceSubscriber(traceOutFilePath?: string | undefined | null): void - -export function minify(code: Buffer, opts: Buffer, signal?: AbortSignal | undefined | null): Promise - -export function minifySync(code: Buffer, opts: Buffer): TransformOutput - -export function parse(src: string, options: Buffer, filename?: string | undefined | null, signal?: AbortSignal | undefined | null): Promise - -export function parseFile(path: string, options: Buffer, signal?: AbortSignal | undefined | null): Promise - -export function parseFileSync(path: string, opts: Buffer): string - -export function parseSync(src: string, opts: Buffer, filename?: string | undefined | null): string - -export function print(programJson: string, options: Buffer, signal?: AbortSignal | undefined | null): Promise - -export function printSync(program: string, options: Buffer): TransformOutput - -export function transform(src: string, isModule: boolean, options: Buffer, signal?: AbortSignal | undefined | null): Promise - -export function transformFile(src: string, isModule: boolean, options: Buffer, signal?: AbortSignal | undefined | null): Promise - -export function transformFileSync(s: string, isModule: boolean, opts: Buffer): TransformOutput - -export interface TransformOutput { - code: string - map?: string -} - -/** Hack for `Type Generation` */ -export interface TransformOutput { - code: string - map?: string -} - -export function transformSync(s: string, isModule: boolean, opts: Buffer): TransformOutput - diff --git a/node-swc/src/binding.js b/node-swc/src/binding.js deleted file mode 100644 index 4da601ca9b7..00000000000 --- a/node-swc/src/binding.js +++ /dev/null @@ -1,354 +0,0 @@ -// prettier-ignore -/* eslint-disable */ -/* auto-generated by NAPI-RS */ - -const { readFileSync } = require('fs') - -let nativeBinding = null -const loadErrors = [] - -const isMusl = () => { - let musl = false - if (process.platform === 'linux') { - musl = isMuslFromFilesystem() - if (musl === null) { - musl = isMuslFromReport() - } - if (musl === null) { - musl = isMuslFromChildProcess() - } - } - return musl -} - -const isFileMusl = (f) => f.includes('libc.musl-') || f.includes('ld-musl-') - -const isMuslFromFilesystem = () => { - try { - return readFileSync('/usr/bin/ldd', 'utf-8').includes('musl') - } catch { - return null - } -} - -const isMuslFromReport = () => { - const report = typeof process.report.getReport === 'function' ? process.report.getReport() : null - if (!report) { - return null - } - if (report.header && report.header.glibcVersionRuntime) { - return false - } - if (Array.isArray(report.sharedObjects)) { - if (report.sharedObjects.some(isFileMusl)) { - return true - } - } - return false -} - -const isMuslFromChildProcess = () => { - try { - return require('child_process').execSync('ldd --version', { encoding: 'utf8' }).includes('musl') - } catch (e) { - // If we reach this case, we don't know if the system is musl or not, so is better to just fallback to false - return false - } -} - -function requireNative() { - if (process.platform === 'android') { - if (process.arch === 'arm64') { - try { - return require('./swc.android-arm64.node') - } catch (e) { - loadErrors.push(e) - } - try { - return require('@swc/core-android-arm64') - } catch (e) { - loadErrors.push(e) - } - - } else if (process.arch === 'arm') { - try { - return require('./swc.android-arm-eabi.node') - } catch (e) { - loadErrors.push(e) - } - try { - return require('@swc/core-android-arm-eabi') - } catch (e) { - loadErrors.push(e) - } - - } else { - loadErrors.push(new Error(`Unsupported architecture on Android ${process.arch}`)) - } - } else if (process.platform === 'win32') { - if (process.arch === 'x64') { - try { - return require('./swc.win32-x64-msvc.node') - } catch (e) { - loadErrors.push(e) - } - try { - return require('@swc/core-win32-x64-msvc') - } catch (e) { - loadErrors.push(e) - } - - } else if (process.arch === 'ia32') { - try { - return require('./swc.win32-ia32-msvc.node') - } catch (e) { - loadErrors.push(e) - } - try { - return require('@swc/core-win32-ia32-msvc') - } catch (e) { - loadErrors.push(e) - } - - } else if (process.arch === 'arm64') { - try { - return require('./swc.win32-arm64-msvc.node') - } catch (e) { - loadErrors.push(e) - } - try { - return require('@swc/core-win32-arm64-msvc') - } catch (e) { - loadErrors.push(e) - } - - } else { - loadErrors.push(new Error(`Unsupported architecture on Windows: ${process.arch}`)) - } - } else if (process.platform === 'darwin') { - try { - return require('./swc.darwin-universal.node') - } catch (e) { - loadErrors.push(e) - } - try { - return require('@swc/core-darwin-universal') - } catch (e) { - loadErrors.push(e) - } - - if (process.arch === 'x64') { - try { - return require('./swc.darwin-x64.node') - } catch (e) { - loadErrors.push(e) - } - try { - return require('@swc/core-darwin-x64') - } catch (e) { - loadErrors.push(e) - } - - } else if (process.arch === 'arm64') { - try { - return require('./swc.darwin-arm64.node') - } catch (e) { - loadErrors.push(e) - } - try { - return require('@swc/core-darwin-arm64') - } catch (e) { - loadErrors.push(e) - } - - } else { - loadErrors.push(new Error(`Unsupported architecture on macOS: ${process.arch}`)) - } - } else if (process.platform === 'freebsd') { - if (process.arch === 'x64') { - try { - return require('./swc.freebsd-x64.node') - } catch (e) { - loadErrors.push(e) - } - try { - return require('@swc/core-freebsd-x64') - } catch (e) { - loadErrors.push(e) - } - - } else if (process.arch === 'arm64') { - try { - return require('./swc.freebsd-arm64.node') - } catch (e) { - loadErrors.push(e) - } - try { - return require('@swc/core-freebsd-arm64') - } catch (e) { - loadErrors.push(e) - } - - } else { - loadErrors.push(new Error(`Unsupported architecture on FreeBSD: ${process.arch}`)) - } - } else if (process.platform === 'linux') { - if (process.arch === 'x64') { - if (isMusl()) { - try { - return require('./swc.linux-x64-musl.node') - } catch (e) { - loadErrors.push(e) - } - try { - return require('@swc/core-linux-x64-musl') - } catch (e) { - loadErrors.push(e) - } - - } else { - try { - return require('./swc.linux-x64-gnu.node') - } catch (e) { - loadErrors.push(e) - } - try { - return require('@swc/core-linux-x64-gnu') - } catch (e) { - loadErrors.push(e) - } - - } - } else if (process.arch === 'arm64') { - if (isMusl()) { - try { - return require('./swc.linux-arm64-musl.node') - } catch (e) { - loadErrors.push(e) - } - try { - return require('@swc/core-linux-arm64-musl') - } catch (e) { - loadErrors.push(e) - } - - } else { - try { - return require('./swc.linux-arm64-gnu.node') - } catch (e) { - loadErrors.push(e) - } - try { - return require('@swc/core-linux-arm64-gnu') - } catch (e) { - loadErrors.push(e) - } - - } - } else if (process.arch === 'arm') { - try { - return require('./swc.linux-arm-gnueabihf.node') - } catch (e) { - loadErrors.push(e) - } - try { - return require('@swc/core-linux-arm-gnueabihf') - } catch (e) { - loadErrors.push(e) - } - - } else if (process.arch === 'riscv64') { - if (isMusl()) { - try { - return require('./swc.linux-riscv64-musl.node') - } catch (e) { - loadErrors.push(e) - } - try { - return require('@swc/core-linux-riscv64-musl') - } catch (e) { - loadErrors.push(e) - } - - } else { - try { - return require('./swc.linux-riscv64-gnu.node') - } catch (e) { - loadErrors.push(e) - } - try { - return require('@swc/core-linux-riscv64-gnu') - } catch (e) { - loadErrors.push(e) - } - - } - } else if (process.arch === 's390x') { - try { - return require('./swc.linux-s390x-gnu.node') - } catch (e) { - loadErrors.push(e) - } - try { - return require('@swc/core-linux-s390x-gnu') - } catch (e) { - loadErrors.push(e) - } - - } else { - loadErrors.push(new Error(`Unsupported architecture on Linux: ${process.arch}`)) - } - } else { - loadErrors.push(new Error(`Unsupported OS: ${process.platform}, architecture: ${process.arch}`)) - } -} - -nativeBinding = requireNative() - -if (!nativeBinding || process.env.NAPI_RS_FORCE_WASI) { - try { - nativeBinding = require('./swc.wasi.cjs') - } catch (err) { - if (process.env.NAPI_RS_FORCE_WASI) { - console.error(err) - } - } - if (!nativeBinding) { - try { - nativeBinding = require('@swc/core-wasm32-wasi') - } catch (err) { - if (process.env.NAPI_RS_FORCE_WASI) { - console.error(err) - } - } - } -} - -if (!nativeBinding) { - if (loadErrors.length > 0) { - // TODO Link to documentation with potential fixes - // - The package owner could build/publish bindings for this arch - // - The user may need to bundle the correct files - // - The user may need to re-install node_modules to get new packages - throw new Error('Failed to load native binding', { cause: loadErrors }) - } - throw new Error(`Failed to load native binding`) -} - -module.exports.Compiler = nativeBinding.Compiler -module.exports.JsCompiler = nativeBinding.JsCompiler -module.exports.bundle = nativeBinding.bundle -module.exports.getTargetTriple = nativeBinding.getTargetTriple -module.exports.initCustomTraceSubscriber = nativeBinding.initCustomTraceSubscriber -module.exports.minify = nativeBinding.minify -module.exports.minifySync = nativeBinding.minifySync -module.exports.parse = nativeBinding.parse -module.exports.parseFile = nativeBinding.parseFile -module.exports.parseFileSync = nativeBinding.parseFileSync -module.exports.parseSync = nativeBinding.parseSync -module.exports.print = nativeBinding.print -module.exports.printSync = nativeBinding.printSync -module.exports.transform = nativeBinding.transform -module.exports.transformFile = nativeBinding.transformFile -module.exports.transformFileSync = nativeBinding.transformFileSync -module.exports.transformSync = nativeBinding.transformSync diff --git a/node-swc/src/index.ts b/node-swc/src/index.ts deleted file mode 100644 index 20ad7da9be6..00000000000 --- a/node-swc/src/index.ts +++ /dev/null @@ -1,479 +0,0 @@ -import { resolve } from "path"; -import type { - Plugin, - ParseOptions, - Module, - Output, - Options, - Script, - Program, - JsMinifyOptions, -} from "@swc/types"; -export * from "@swc/types"; -import { BundleInput, compileBundleOptions } from "./spack"; -import * as assert from "assert"; - -// Allow overrides to the location of the .node binding file -const bindingsOverride = process.env["SWC_BINARY_PATH"]; -// `@swc/core` includes d.ts for the `@swc/wasm` to provide typed fallback bindings -// todo: fix package.json scripts -let fallbackBindings: any; -const bindings: typeof import('./binding') = (() => { - let binding - try { - binding = !!bindingsOverride ? require(resolve(bindingsOverride)) : require('./binding') - - // If native binding loaded successfully, it should return proper target triple constant. - const triple = binding.getTargetTriple(); - assert.ok(triple, 'Failed to read target triple from native binary.'); - return binding; - } catch (_) { - // postinstall supposed to install `@swc/wasm` already - fallbackBindings = require('@swc/wasm'); - } finally { - return binding; - } -})(); - -/** - * Version of the swc binding. - */ -export const version: string = require("./package.json").version; - -/** - * @deprecated JavaScript API is deprecated. Please use Wasm plugin instead. - */ -export function plugins(ps: Plugin[]): Plugin { - return mod => { - let m = mod; - for (const p of ps) { - m = p(m); - } - return m; - }; -} - -export class Compiler { - private fallbackBindingsPluginWarningDisplayed = false; - - async minify(src: string, opts?: JsMinifyOptions): Promise { - if (bindings) { - return bindings.minify(toBuffer(src), toBuffer(opts ?? {})); - } else if (fallbackBindings) { - return fallbackBindings.minify(src, opts); - } - throw new Error('Bindings not found.'); - } - - minifySync(src: string, opts?: JsMinifyOptions): Output { - if (bindings) { - return bindings.minifySync(toBuffer(src), toBuffer(opts ?? {})); - } else if (fallbackBindings) { - return fallbackBindings.minifySync(src, opts); - } - throw new Error('Bindings not found.'); - } - - parse( - src: string, - options: ParseOptions & { isModule: false } - ): Promise