Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix identifiers renaming #74

Merged
merged 2 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 41 additions & 23 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -48,6 +46,23 @@ impl SourceMapGenConfig for SourceMapConfig {
}


pub struct IdentCollector {
pub idents: Vec<Atom>
}

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 {
Expand Down Expand Up @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic for this can go inside the closure for .unwrap_or_else so we don’t run it unnecessarily (and we probably don’t need private_ident! anymore)

if !had_id_already {
let mut n = 1;
loop {
let current = Atom::from(new_specifier.clone());
let found = ident_collector.idents.contains(&current);
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,
Expand All @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion test/process.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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\\\`!\`, {
Expand Down
Loading