From 6fd13d6c0bcc26ce3b5598b756b585c6bf3d5fc7 Mon Sep 17 00:00:00 2001 From: Godfrey Chan Date: Thu, 29 Feb 2024 20:48:00 -0800 Subject: [PATCH] Refactor import insertion code into its own module Ultimately the goal is to fix #71 (which this commit does *not* do) While working on that I decided to refactor the existing import related code and add some commentary to help others understand how it works. This just moves code around without changing behavior. --- Cargo.lock | 2 +- Cargo.toml | 2 +- package-lock.json | 4 +- src/importer.rs | 281 +++++++++++++++++++++++++++ src/lib.rs | 161 +++------------ src/{transform.rs => transformer.rs} | 115 +++++------ 6 files changed, 375 insertions(+), 190 deletions(-) create mode 100644 src/importer.rs rename src/{transform.rs => transformer.rs} (75%) diff --git a/Cargo.lock b/Cargo.lock index a8abab4..5c93903 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -292,7 +292,7 @@ dependencies = [ "swc_ecma_ast", "swc_ecma_codegen", "swc_ecma_parser", - "swc_ecma_transforms", + "swc_ecma_transforms_base", "swc_ecma_utils", "swc_ecma_visit", "swc_error_reporters", diff --git a/Cargo.toml b/Cargo.toml index 55f57e1..0c93841 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,7 +29,7 @@ swc_ecma_codegen = { git = "https://github.com/ef4/swc.git", branch = "content-t swc_ecma_ast = { git = "https://github.com/ef4/swc.git", branch = "content-tag" } swc_ecma_visit = { git = "https://github.com/ef4/swc.git", branch = "content-tag" } swc_ecma_utils = { git = "https://github.com/ef4/swc.git", branch = "content-tag" } -swc_ecma_transforms = { git = "https://github.com/ef4/swc.git", branch = "content-tag" } +swc_ecma_transforms_base = { git = "https://github.com/ef4/swc.git", branch = "content-tag" } swc_error_reporters = { git = "https://github.com/ef4/swc.git", branch = "content-tag" } lazy_static = "1.4.0" base64 = "0.21.4" diff --git a/package-lock.json b/package-lock.json index ee67ef5..4d6c947 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "content-tag", - "version": "1.2.2", + "version": "2.0.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "content-tag", - "version": "1.2.2", + "version": "2.0.1", "license": "MIT", "devDependencies": { "@arethetypeswrong/cli": "^0.13.2", diff --git a/src/importer.rs b/src/importer.rs new file mode 100644 index 0000000..210f5c7 --- /dev/null +++ b/src/importer.rs @@ -0,0 +1,281 @@ +use swc_common::Mark; +use swc_ecma_ast::{ + Ident, ImportDecl, ImportNamedSpecifier, ImportSpecifier, Module, ModuleDecl, ModuleExportName, + ModuleItem, +}; +use swc_ecma_transforms_base::{ + hygiene::{hygiene_with_config, Config}, + resolver, +}; +use swc_ecma_utils::private_ident; +use swc_ecma_visit::{Fold, VisitMut, VisitMutWith}; + +/// Logic for managing the insertion of the following import statement: +/// +/// ```js +/// import { template } from "@ember/template"; +/// ``` +/// +/// * We only want to insert this if there are any `