diff --git a/src/lib.rs b/src/lib.rs index ac4d20c..ad088c0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,22 +3,20 @@ #[macro_use] extern crate lazy_static; -use base64::{engine::general_purpose, Engine as _}; +use std::ops::{Deref, DerefMut}; use std::path::PathBuf; + +use base64::{Engine as _, engine::general_purpose}; +use swc_atoms::Atom; +use swc_common::{self, FileName, SourceMap, sync::Lrc}; use swc_common::comments::SingleThreadedComments; use swc_common::source_map::SourceMapGenConfig; -use swc_common::{self, sync::Lrc, FileName, SourceMap, Mark}; use swc_core::common::GLOBALS; -use swc_ecma_ast::{ - Ident, ImportDecl, ImportNamedSpecifier, ImportSpecifier, Module, ModuleDecl, - ModuleExportName, ModuleItem, -}; +use swc_ecma_ast::{Ident, ImportDecl, ImportNamedSpecifier, ImportSpecifier, Module, ModuleDecl, ModuleExportName, ModuleItem}; use swc_ecma_codegen::Emitter; use swc_ecma_parser::{lexer::Lexer, Parser, StringInput, Syntax, TsConfig}; -use swc_ecma_transforms::hygiene::hygiene_with_config; -use swc_ecma_transforms::resolver; use swc_ecma_utils::private_ident; -use swc_ecma_visit::{as_folder, VisitMutWith, VisitWith}; +use swc_ecma_visit::{as_folder, VisitMut, VisitMutWith, VisitWith}; mod bindings; mod snippets; @@ -48,6 +46,23 @@ impl SourceMapGenConfig for SourceMapConfig { } +pub struct IdentCollector { + pub idents: Vec +} + +impl IdentCollector { + pub fn new() -> Self { + Self { idents: vec![] } + } +} + +impl VisitMut for IdentCollector { + fn visit_mut_ident(&mut self, n: &mut Ident) { + self.idents.push(n.sym.clone()) + } +} + + impl Preprocessor { pub fn new() -> Self { Self { @@ -116,9 +131,25 @@ impl Preprocessor { GLOBALS.set(&Default::default(), || { let mut parsed_module = parser.parse_module()?; + let mut ident_collector = IdentCollector::new(); + parsed_module.visit_mut_with(&mut as_folder(&mut ident_collector)); + let found_id = find_existing_import(&parsed_module, target_module, target_specifier); let had_id_already = found_id.is_some(); - let id = found_id.unwrap_or_else(|| private_ident!(target_specifier)); + let mut new_specifier = String::from(target_specifier); + if !had_id_already { + let mut n = 1; + loop { + let current = Atom::from(new_specifier.clone()); + let found = ident_collector.idents.contains(¤t); + if found == false { + break + } + new_specifier = format!("{target_specifier}{n}"); + n = n + 1; + } + } + let id = found_id.unwrap_or_else(|| private_ident!(new_specifier)); let mut needs_import = false; parsed_module.visit_mut_with(&mut as_folder(transform::TransformVisitor::new( &id, @@ -129,19 +160,6 @@ impl Preprocessor { insert_import(&mut parsed_module, target_module, target_specifier, &id) } - let unresolved_mark = Mark::new(); - let top_level_mark = Mark::new(); - - parsed_module.visit_mut_with(&mut resolver(unresolved_mark, top_level_mark, false)); - - let mut h = hygiene_with_config(swc_ecma_transforms::hygiene::Config { - keep_class_names: true, - top_level_mark, - safari_10: false, - ignore_eval: false, - }); - parsed_module.visit_mut_with(&mut h); - simplify_imports(&mut parsed_module); Ok(self.print(&parsed_module, options.inline_source_map)) diff --git a/test/process.test.js b/test/process.test.js index e1d70de..c21c6ab 100644 --- a/test/process.test.js +++ b/test/process.test.js @@ -34,7 +34,7 @@ describe(`process`, function () { expect(output).to.equalCode( `import { template } from "@ember/template-compiler"; - let Foo = class Foo extends Component { + class Foo extends Component { greeting = 'Hello'; static{ template(\`{{this.greeting}}, \\\`lifeform\\\`!\`, {