| | use std::{collections::BTreeMap, fmt::Display}; |
| |
|
| | use once_cell::sync::Lazy; |
| | use rustc_hash::{FxHashMap, FxHashSet}; |
| | use swc_core::{ |
| | common::{BytePos, Span, Spanned, SyntaxContext, comments::Comments, source_map::SmallPos}, |
| | ecma::{ |
| | ast::*, |
| | atoms::{Atom, atom}, |
| | utils::{IsDirective, find_pat_ids}, |
| | visit::{Visit, VisitWith}, |
| | }, |
| | }; |
| | use turbo_rcstr::{RcStr, rcstr}; |
| | use turbo_tasks::{FxIndexMap, FxIndexSet, ResolvedVc}; |
| | use turbopack_core::{issue::IssueSource, source::Source}; |
| |
|
| | use super::{JsValue, ModuleValue, top_level_await::has_top_level_await}; |
| | use crate::{ |
| | SpecifiedModuleType, |
| | analyzer::{ConstantValue, ObjectPart}, |
| | magic_identifier, |
| | tree_shake::{PartId, find_turbopack_part_id_in_asserts}, |
| | }; |
| |
|
| | #[turbo_tasks::value] |
| | #[derive(Default, Debug, Clone, Hash)] |
| | pub struct ImportAnnotations { |
| | |
| | #[turbo_tasks(trace_ignore)] |
| | map: BTreeMap<Atom, Atom>, |
| | } |
| |
|
| | |
| | static ANNOTATION_TRANSITION: Lazy<Atom> = |
| | Lazy::new(|| crate::annotations::ANNOTATION_TRANSITION.into()); |
| |
|
| | |
| | static ANNOTATION_CHUNKING_TYPE: Lazy<Atom> = |
| | Lazy::new(|| crate::annotations::ANNOTATION_CHUNKING_TYPE.into()); |
| |
|
| | |
| | static ATTRIBUTE_MODULE_TYPE: Lazy<Atom> = Lazy::new(|| "type".into()); |
| |
|
| | impl ImportAnnotations { |
| | pub fn parse(with: Option<&ObjectLit>) -> ImportAnnotations { |
| | let Some(with) = with else { |
| | return ImportAnnotations::default(); |
| | }; |
| |
|
| | let mut map = BTreeMap::new(); |
| |
|
| | |
| | |
| | |
| | for (key, value) in with.props.iter().filter_map(|prop| { |
| | let kv = prop.as_prop()?.as_key_value()?; |
| |
|
| | let Lit::Str(str) = kv.value.as_lit()? else { |
| | return None; |
| | }; |
| |
|
| | Some((&kv.key, str)) |
| | }) { |
| | let key = match key { |
| | PropName::Ident(ident) => ident.sym.as_str(), |
| | PropName::Str(str) => str.value.as_str(), |
| | |
| | _ => continue, |
| | }; |
| |
|
| | map.insert(key.into(), value.value.as_str().into()); |
| | } |
| |
|
| | ImportAnnotations { map } |
| | } |
| |
|
| | pub fn parse_dynamic(with: &JsValue) -> Option<ImportAnnotations> { |
| | let mut map = BTreeMap::new(); |
| |
|
| | let JsValue::Object { parts, .. } = with else { |
| | return None; |
| | }; |
| |
|
| | for part in parts.iter() { |
| | let ObjectPart::KeyValue(key, value) = part else { |
| | continue; |
| | }; |
| | let ( |
| | JsValue::Constant(ConstantValue::Str(key)), |
| | JsValue::Constant(ConstantValue::Str(value)), |
| | ) = (key, value) |
| | else { |
| | continue; |
| | }; |
| |
|
| | map.insert(key.as_str().into(), value.as_str().into()); |
| | } |
| |
|
| | Some(ImportAnnotations { map }) |
| | } |
| |
|
| | |
| | pub fn transition(&self) -> Option<&str> { |
| | self.get(&ANNOTATION_TRANSITION) |
| | } |
| |
|
| | |
| | pub fn chunking_type(&self) -> Option<&str> { |
| | self.get(&ANNOTATION_CHUNKING_TYPE) |
| | } |
| |
|
| | |
| | pub fn module_type(&self) -> Option<&str> { |
| | self.get(&ATTRIBUTE_MODULE_TYPE) |
| | } |
| |
|
| | pub fn get(&self, key: &Atom) -> Option<&str> { |
| | self.map.get(key).map(|w| w.as_str()) |
| | } |
| | } |
| |
|
| | impl Display for ImportAnnotations { |
| | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| | let mut it = self.map.iter(); |
| | if let Some((k, v)) = it.next() { |
| | write!(f, "{{ {k}: {v}")? |
| | } else { |
| | return f.write_str("{}"); |
| | }; |
| | for (k, v) in it { |
| | write!(f, ", {k}: {v}")? |
| | } |
| | f.write_str(" }") |
| | } |
| | } |
| |
|
| | #[derive(Debug)] |
| | pub(crate) enum Reexport { |
| | Star, |
| | Namespace { exported: Atom }, |
| | Named { imported: Atom, exported: Atom }, |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | #[derive(Default, Debug)] |
| | pub(crate) struct ImportMap { |
| | |
| | imports: FxIndexMap<Id, (usize, Atom)>, |
| |
|
| | |
| | namespace_imports: FxIndexMap<Id, usize>, |
| |
|
| | |
| | reexports: Vec<(usize, Reexport)>, |
| |
|
| | |
| | references: FxIndexSet<ImportMapReference>, |
| |
|
| | |
| | has_imports: bool, |
| |
|
| | |
| | has_exports: bool, |
| |
|
| | |
| | has_top_level_await: bool, |
| |
|
| | |
| | pub(crate) strict: bool, |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | attributes: FxHashMap<BytePos, ImportAttributes>, |
| |
|
| | |
| | |
| | full_star_imports: FxHashSet<Atom>, |
| |
|
| | pub(crate) exports: FxHashMap<RcStr, Id>, |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | #[derive(Debug)] |
| | pub struct ImportAttributes { |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | pub ignore: bool, |
| | } |
| |
|
| | impl ImportAttributes { |
| | pub const fn empty() -> Self { |
| | ImportAttributes { ignore: false } |
| | } |
| |
|
| | pub fn empty_ref() -> &'static Self { |
| | |
| | static DEFAULT_VALUE: ImportAttributes = ImportAttributes::empty(); |
| | &DEFAULT_VALUE |
| | } |
| | } |
| |
|
| | impl Default for ImportAttributes { |
| | fn default() -> Self { |
| | ImportAttributes::empty() |
| | } |
| | } |
| |
|
| | impl Default for &ImportAttributes { |
| | fn default() -> Self { |
| | ImportAttributes::empty_ref() |
| | } |
| | } |
| |
|
| | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
| | pub(crate) enum ImportedSymbol { |
| | ModuleEvaluation, |
| | Symbol(Atom), |
| | Exports, |
| | Part(u32), |
| | PartEvaluation(u32), |
| | } |
| |
|
| | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
| | pub(crate) struct ImportMapReference { |
| | pub module_path: Atom, |
| | pub imported_symbol: ImportedSymbol, |
| | pub annotations: ImportAnnotations, |
| | pub issue_source: Option<IssueSource>, |
| | } |
| |
|
| | impl ImportMap { |
| | pub fn is_esm(&self, specified_type: SpecifiedModuleType) -> bool { |
| | if self.has_exports { |
| | return true; |
| | } |
| |
|
| | match specified_type { |
| | SpecifiedModuleType::Automatic => { |
| | self.has_exports || self.has_imports || self.has_top_level_await |
| | } |
| | SpecifiedModuleType::CommonJs => false, |
| | SpecifiedModuleType::EcmaScript => true, |
| | } |
| | } |
| |
|
| | pub fn get_import(&self, id: &Id) -> Option<JsValue> { |
| | if let Some((i, i_sym)) = self.imports.get(id) { |
| | let r = &self.references[*i]; |
| | return Some(JsValue::member( |
| | Box::new(JsValue::Module(ModuleValue { |
| | module: r.module_path.clone(), |
| | annotations: r.annotations.clone(), |
| | })), |
| | Box::new(i_sym.clone().into()), |
| | )); |
| | } |
| | if let Some(i) = self.namespace_imports.get(id) { |
| | let r = &self.references[*i]; |
| | return Some(JsValue::Module(ModuleValue { |
| | module: r.module_path.clone(), |
| | annotations: r.annotations.clone(), |
| | })); |
| | } |
| | None |
| | } |
| |
|
| | pub fn get_attributes(&self, span: Span) -> &ImportAttributes { |
| | self.attributes.get(&span.lo).unwrap_or_default() |
| | } |
| |
|
| | |
| | pub fn get_binding(&self, id: &Id) -> Option<(usize, Option<RcStr>)> { |
| | if let Some((i, i_sym)) = self.imports.get(id) { |
| | return Some((*i, Some(i_sym.as_str().into()))); |
| | } |
| | if let Some(i) = self.namespace_imports.get(id) { |
| | return Some((*i, None)); |
| | } |
| | None |
| | } |
| |
|
| | pub fn references(&self) -> impl ExactSizeIterator<Item = &ImportMapReference> { |
| | self.references.iter() |
| | } |
| |
|
| | pub fn reexports(&self) -> impl ExactSizeIterator<Item = (usize, &Reexport)> { |
| | self.reexports.iter().map(|(i, r)| (*i, r)) |
| | } |
| |
|
| | |
| | pub(super) fn analyze( |
| | m: &Program, |
| | source: Option<ResolvedVc<Box<dyn Source>>>, |
| | comments: Option<&dyn Comments>, |
| | ) -> Self { |
| | let mut data = ImportMap::default(); |
| |
|
| | |
| | |
| | |
| | |
| |
|
| | if let Program::Module(m) = m { |
| | let mut candidates = FxIndexMap::default(); |
| |
|
| | |
| | |
| | m.body.iter().for_each(|stmt| { |
| | if let ModuleItem::ModuleDecl(ModuleDecl::Import(import)) = stmt { |
| | for s in &import.specifiers { |
| | if let ImportSpecifier::Namespace(s) = s { |
| | candidates.insert(s.local.to_id(), import.src.value.clone()); |
| | } |
| | } |
| | } |
| | }); |
| |
|
| | let mut analyzer = StarImportAnalyzer { |
| | candidates, |
| | full_star_imports: &mut data.full_star_imports, |
| | }; |
| | m.visit_with(&mut analyzer); |
| | } |
| |
|
| | let mut analyzer = Analyzer { |
| | data: &mut data, |
| | source, |
| | comments, |
| | }; |
| | m.visit_with(&mut analyzer); |
| |
|
| | data |
| | } |
| |
|
| | pub(crate) fn should_import_all(&self, esm_reference_index: usize) -> bool { |
| | let r = &self.references[esm_reference_index]; |
| |
|
| | self.full_star_imports.contains(&r.module_path) |
| | } |
| | } |
| |
|
| | struct StarImportAnalyzer<'a> { |
| | |
| | candidates: FxIndexMap<Id, Atom>, |
| | full_star_imports: &'a mut FxHashSet<Atom>, |
| | } |
| |
|
| | impl Visit for StarImportAnalyzer<'_> { |
| | fn visit_expr(&mut self, node: &Expr) { |
| | if let Expr::Ident(i) = node |
| | && let Some(module_path) = self.candidates.get(&i.to_id()) |
| | { |
| | self.full_star_imports.insert(module_path.clone()); |
| | return; |
| | } |
| |
|
| | node.visit_children_with(self); |
| | } |
| |
|
| | fn visit_import_decl(&mut self, _: &ImportDecl) {} |
| |
|
| | fn visit_member_expr(&mut self, node: &MemberExpr) { |
| | match &node.prop { |
| | MemberProp::Ident(..) | MemberProp::PrivateName(..) => { |
| | if node.obj.is_ident() { |
| | return; |
| | } |
| | |
| | node.obj.visit_children_with(self); |
| | } |
| | MemberProp::Computed(..) => { |
| | node.obj.visit_with(self); |
| | node.prop.visit_with(self); |
| | } |
| | } |
| | } |
| |
|
| | fn visit_pat(&mut self, pat: &Pat) { |
| | if let Pat::Ident(i) = pat |
| | && let Some(module_path) = self.candidates.get(&i.to_id()) |
| | { |
| | self.full_star_imports.insert(module_path.clone()); |
| | return; |
| | } |
| |
|
| | pat.visit_children_with(self); |
| | } |
| |
|
| | fn visit_simple_assign_target(&mut self, node: &SimpleAssignTarget) { |
| | if let SimpleAssignTarget::Ident(i) = node |
| | && let Some(module_path) = self.candidates.get(&i.to_id()) |
| | { |
| | self.full_star_imports.insert(module_path.clone()); |
| | return; |
| | } |
| |
|
| | node.visit_children_with(self); |
| | } |
| | } |
| |
|
| | struct Analyzer<'a> { |
| | data: &'a mut ImportMap, |
| | source: Option<ResolvedVc<Box<dyn Source>>>, |
| | comments: Option<&'a dyn Comments>, |
| | } |
| |
|
| | impl Analyzer<'_> { |
| | fn ensure_reference( |
| | &mut self, |
| | span: Span, |
| | module_path: Atom, |
| | imported_symbol: ImportedSymbol, |
| | annotations: ImportAnnotations, |
| | ) -> Option<usize> { |
| | let issue_source = self |
| | .source |
| | .map(|s| IssueSource::from_swc_offsets(s, span.lo.to_u32(), span.hi.to_u32())); |
| |
|
| | let r = ImportMapReference { |
| | module_path, |
| | imported_symbol, |
| | issue_source, |
| | annotations, |
| | }; |
| | if let Some(i) = self.data.references.get_index_of(&r) { |
| | Some(i) |
| | } else { |
| | let i = self.data.references.len(); |
| | self.data.references.insert(r); |
| | Some(i) |
| | } |
| | } |
| | } |
| |
|
| | fn export_as_atom(name: &ModuleExportName) -> &Atom { |
| | match name { |
| | ModuleExportName::Ident(ident) => &ident.sym, |
| | ModuleExportName::Str(s) => &s.value, |
| | } |
| | } |
| |
|
| | impl Visit for Analyzer<'_> { |
| | fn visit_import_decl(&mut self, import: &ImportDecl) { |
| | self.data.has_imports = true; |
| |
|
| | let annotations = ImportAnnotations::parse(import.with.as_deref()); |
| |
|
| | let internal_symbol = parse_with(import.with.as_deref()); |
| |
|
| | if internal_symbol.is_none() { |
| | self.ensure_reference( |
| | import.span, |
| | import.src.value.clone(), |
| | ImportedSymbol::ModuleEvaluation, |
| | annotations.clone(), |
| | ); |
| | } |
| |
|
| | for s in &import.specifiers { |
| | let symbol = internal_symbol |
| | .clone() |
| | .unwrap_or_else(|| get_import_symbol_from_import(s)); |
| | let i = self.ensure_reference( |
| | import.span, |
| | import.src.value.clone(), |
| | symbol, |
| | annotations.clone(), |
| | ); |
| | let i = match i { |
| | Some(v) => v, |
| | None => continue, |
| | }; |
| |
|
| | let (local, orig_sym) = match s { |
| | ImportSpecifier::Named(ImportNamedSpecifier { |
| | local, imported, .. |
| | }) => match imported { |
| | Some(imported) => (local.to_id(), orig_name(imported)), |
| | _ => (local.to_id(), local.sym.clone()), |
| | }, |
| | ImportSpecifier::Default(s) => (s.local.to_id(), "default".into()), |
| | ImportSpecifier::Namespace(s) => { |
| | self.data.namespace_imports.insert(s.local.to_id(), i); |
| | continue; |
| | } |
| | }; |
| |
|
| | self.data.imports.insert(local, (i, orig_sym)); |
| | } |
| | if import.specifiers.is_empty() |
| | && let Some(internal_symbol) = internal_symbol |
| | { |
| | self.ensure_reference( |
| | import.span, |
| | import.src.value.clone(), |
| | internal_symbol, |
| | annotations, |
| | ); |
| | } |
| | } |
| |
|
| | fn visit_export_all(&mut self, export: &ExportAll) { |
| | self.data.has_exports = true; |
| |
|
| | let annotations = ImportAnnotations::parse(export.with.as_deref()); |
| |
|
| | self.ensure_reference( |
| | export.span, |
| | export.src.value.clone(), |
| | ImportedSymbol::ModuleEvaluation, |
| | annotations.clone(), |
| | ); |
| | let symbol = parse_with(export.with.as_deref()); |
| |
|
| | let i = self.ensure_reference( |
| | export.span, |
| | export.src.value.clone(), |
| | symbol.unwrap_or(ImportedSymbol::Exports), |
| | annotations, |
| | ); |
| | if let Some(i) = i { |
| | self.data.reexports.push((i, Reexport::Star)); |
| | } |
| | } |
| |
|
| | fn visit_named_export(&mut self, export: &NamedExport) { |
| | self.data.has_exports = true; |
| |
|
| | let Some(ref src) = export.src else { |
| | export.visit_children_with(self); |
| | return; |
| | }; |
| |
|
| | let annotations = ImportAnnotations::parse(export.with.as_deref()); |
| |
|
| | let internal_symbol = parse_with(export.with.as_deref()); |
| |
|
| | if internal_symbol.is_none() || export.specifiers.is_empty() { |
| | self.ensure_reference( |
| | export.span, |
| | src.value.clone(), |
| | ImportedSymbol::ModuleEvaluation, |
| | annotations.clone(), |
| | ); |
| | } |
| |
|
| | for spec in export.specifiers.iter() { |
| | let symbol = internal_symbol |
| | .clone() |
| | .unwrap_or_else(|| get_import_symbol_from_export(spec)); |
| |
|
| | let i = |
| | self.ensure_reference(export.span, src.value.clone(), symbol, annotations.clone()); |
| | let i = match i { |
| | Some(v) => v, |
| | None => continue, |
| | }; |
| |
|
| | match spec { |
| | ExportSpecifier::Namespace(n) => { |
| | self.data.reexports.push(( |
| | i, |
| | Reexport::Namespace { |
| | exported: export_as_atom(&n.name).clone(), |
| | }, |
| | )); |
| | } |
| | ExportSpecifier::Default(d) => { |
| | self.data.reexports.push(( |
| | i, |
| | Reexport::Named { |
| | imported: atom!("default"), |
| | exported: d.exported.sym.clone(), |
| | }, |
| | )); |
| | } |
| | ExportSpecifier::Named(n) => { |
| | self.data.reexports.push(( |
| | i, |
| | Reexport::Named { |
| | imported: export_as_atom(&n.orig).clone(), |
| | exported: export_as_atom(n.exported.as_ref().unwrap_or(&n.orig)) |
| | .clone(), |
| | }, |
| | )); |
| | } |
| | } |
| | } |
| | } |
| |
|
| | fn visit_export_decl(&mut self, n: &ExportDecl) { |
| | self.data.has_exports = true; |
| |
|
| | if self.comments.is_some() { |
| | |
| | n.visit_children_with(self); |
| | } |
| |
|
| | match &n.decl { |
| | Decl::Class(n) => { |
| | self.data |
| | .exports |
| | .insert(n.ident.sym.as_str().into(), n.ident.to_id()); |
| | } |
| | Decl::Fn(n) => { |
| | self.data |
| | .exports |
| | .insert(n.ident.sym.as_str().into(), n.ident.to_id()); |
| | } |
| | Decl::Var(..) | Decl::Using(..) => { |
| | let ids: Vec<Id> = find_pat_ids(&n.decl); |
| | for id in ids { |
| | self.data.exports.insert(id.0.as_str().into(), id); |
| | } |
| | } |
| | _ => {} |
| | } |
| | } |
| |
|
| | fn visit_export_default_decl(&mut self, n: &ExportDefaultDecl) { |
| | self.data.has_exports = true; |
| |
|
| | if self.comments.is_some() { |
| | |
| | n.visit_children_with(self); |
| | } |
| |
|
| | self.data.exports.insert( |
| | rcstr!("default"), |
| | |
| | |
| | match &n.decl { |
| | DefaultDecl::Class(ClassExpr { ident, .. }) |
| | | DefaultDecl::Fn(FnExpr { ident, .. }) => ident.as_ref().map_or_else( |
| | || { |
| | ( |
| | magic_identifier::mangle("default export").into(), |
| | SyntaxContext::empty(), |
| | ) |
| | }, |
| | |ident| (ident.to_id()), |
| | ), |
| | DefaultDecl::TsInterfaceDecl(_) => { |
| | |
| | ( |
| | magic_identifier::mangle("default export").into(), |
| | SyntaxContext::empty(), |
| | ) |
| | } |
| | }, |
| | ); |
| | } |
| |
|
| | fn visit_export_default_expr(&mut self, n: &ExportDefaultExpr) { |
| | self.data.has_exports = true; |
| |
|
| | if self.comments.is_some() { |
| | |
| | n.visit_children_with(self); |
| | } |
| |
|
| | self.data.exports.insert( |
| | rcstr!("default"), |
| | ( |
| | |
| | magic_identifier::mangle("default export").into(), |
| | SyntaxContext::empty(), |
| | ), |
| | ); |
| | } |
| |
|
| | fn visit_export_named_specifier(&mut self, n: &ExportNamedSpecifier) { |
| | let ModuleExportName::Ident(local) = &n.orig else { |
| | |
| | |
| | unreachable!("string reexports should have been already handled in visit_named_export"); |
| | }; |
| | let exported = n.exported.as_ref().unwrap_or(&n.orig); |
| | self.data |
| | .exports |
| | .insert(export_as_atom(exported).as_str().into(), local.to_id()); |
| | } |
| |
|
| | fn visit_export_default_specifier(&mut self, n: &ExportDefaultSpecifier) { |
| | self.data |
| | .exports |
| | .insert(rcstr!("default"), n.exported.to_id()); |
| | } |
| |
|
| | fn visit_program(&mut self, m: &Program) { |
| | self.data.has_top_level_await = has_top_level_await(m).is_some(); |
| | self.data.strict = match m { |
| | Program::Module(module) => module |
| | .body |
| | .iter() |
| | .take_while(|s| s.directive_continue()) |
| | .any(IsDirective::is_use_strict), |
| | Program::Script(script) => script |
| | .body |
| | .iter() |
| | .take_while(|s| s.directive_continue()) |
| | .any(IsDirective::is_use_strict), |
| | }; |
| |
|
| | m.visit_children_with(self); |
| | } |
| |
|
| | fn visit_stmt(&mut self, n: &Stmt) { |
| | if self.comments.is_some() { |
| | |
| | n.visit_children_with(self); |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | fn visit_call_expr(&mut self, n: &CallExpr) { |
| | |
| | if let Some(comments) = self.comments { |
| | let callee_span = match &n.callee { |
| | Callee::Import(Import { span, .. }) => Some(*span), |
| | Callee::Expr(e) => Some(e.span()), |
| | _ => None, |
| | }; |
| |
|
| | let ignore_directive = parse_ignore_directive(comments, n.args.first()); |
| |
|
| | if let Some((callee_span, ignore_directive)) = callee_span.zip(ignore_directive) { |
| | self.data.attributes.insert( |
| | callee_span.lo, |
| | ImportAttributes { |
| | ignore: ignore_directive, |
| | }, |
| | ); |
| | }; |
| | } |
| |
|
| | n.visit_children_with(self); |
| | } |
| |
|
| | fn visit_new_expr(&mut self, n: &NewExpr) { |
| | |
| | if let Some(comments) = self.comments { |
| | let callee_span = match &n.callee { |
| | box Expr::Ident(Ident { sym, .. }) if sym == "Worker" => Some(n.span), |
| | _ => None, |
| | }; |
| |
|
| | let ignore_directive = parse_ignore_directive(comments, n.args.iter().flatten().next()); |
| |
|
| | if let Some((callee_span, ignore_directive)) = callee_span.zip(ignore_directive) { |
| | self.data.attributes.insert( |
| | callee_span.lo, |
| | ImportAttributes { |
| | ignore: ignore_directive, |
| | }, |
| | ); |
| | }; |
| | } |
| |
|
| | n.visit_children_with(self); |
| | } |
| | } |
| |
|
| | fn parse_ignore_directive(comments: &dyn Comments, value: Option<&ExprOrSpread>) -> Option<bool> { |
| | |
| | value |
| | .map(|arg| arg.span_lo()) |
| | .and_then(|comment_pos| comments.get_leading(comment_pos)) |
| | .iter() |
| | .flatten() |
| | .rev() |
| | .filter_map(|comment| { |
| | let (directive, value) = comment.text.trim().split_once(':')?; |
| | |
| | match (directive.trim(), value.trim()) { |
| | ("webpackIgnore" | "turbopackIgnore", "true") => Some(true), |
| | ("webpackIgnore" | "turbopackIgnore", "false") => Some(false), |
| | _ => None, |
| | } |
| | }) |
| | .next() |
| | } |
| |
|
| | pub(crate) fn orig_name(n: &ModuleExportName) -> Atom { |
| | match n { |
| | ModuleExportName::Ident(v) => v.sym.clone(), |
| | ModuleExportName::Str(v) => v.value.clone(), |
| | } |
| | } |
| |
|
| | fn parse_with(with: Option<&ObjectLit>) -> Option<ImportedSymbol> { |
| | find_turbopack_part_id_in_asserts(with?).map(|v| match v { |
| | PartId::Internal(index, true) => ImportedSymbol::PartEvaluation(index), |
| | PartId::Internal(index, false) => ImportedSymbol::Part(index), |
| | PartId::ModuleEvaluation => ImportedSymbol::ModuleEvaluation, |
| | PartId::Export(e) => ImportedSymbol::Symbol(e.as_str().into()), |
| | PartId::Exports => ImportedSymbol::Exports, |
| | }) |
| | } |
| |
|
| | fn get_import_symbol_from_import(specifier: &ImportSpecifier) -> ImportedSymbol { |
| | match specifier { |
| | ImportSpecifier::Named(ImportNamedSpecifier { |
| | local, imported, .. |
| | }) => ImportedSymbol::Symbol(match imported { |
| | Some(imported) => orig_name(imported), |
| | _ => local.sym.clone(), |
| | }), |
| | ImportSpecifier::Default(..) => ImportedSymbol::Symbol(atom!("default")), |
| | ImportSpecifier::Namespace(..) => ImportedSymbol::Exports, |
| | } |
| | } |
| |
|
| | fn get_import_symbol_from_export(specifier: &ExportSpecifier) -> ImportedSymbol { |
| | match specifier { |
| | ExportSpecifier::Named(ExportNamedSpecifier { orig, .. }) => { |
| | ImportedSymbol::Symbol(orig_name(orig)) |
| | } |
| | ExportSpecifier::Default(..) => ImportedSymbol::Symbol(atom!("default")), |
| | ExportSpecifier::Namespace(..) => ImportedSymbol::Exports, |
| | } |
| | } |
| |
|