Skip to content

Commit

Permalink
Refactor: normalized encoding of ImportModule (#3068)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaslihotzki authored Sep 8, 2022
1 parent edc5adf commit f82f5c5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 27 deletions.
10 changes: 4 additions & 6 deletions crates/backend/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ impl Interner {
///
/// Note that repeated invocations of this function will be memoized, so the
/// same `id` will always return the same resulting unique `id`.
fn resolve_import_module(&self, id: &str, span: Span) -> Result<&str, Diagnostic> {
fn resolve_import_module(&self, id: &str, span: Span) -> Result<ImportModule, Diagnostic> {
let mut files = self.files.borrow_mut();
if let Some(file) = files.get(id) {
return Ok(self.intern_str(&file.new_identifier));
return Ok(ImportModule::Named(self.intern_str(&file.new_identifier)));
}
self.check_for_package_json();
let path = if id.starts_with("/") {
Expand All @@ -89,7 +89,7 @@ impl Interner {
let msg = "relative module paths aren't supported yet";
return Err(Diagnostic::span_error(span, msg));
} else {
return Ok(self.intern_str(&id));
return Ok(ImportModule::RawNamed(self.intern_str(id)));
};

// Generate a unique ID which is somewhat readable as well, so mix in
Expand Down Expand Up @@ -254,9 +254,7 @@ fn shared_module<'a>(
intern: &'a Interner,
) -> Result<ImportModule<'a>, Diagnostic> {
Ok(match m {
ast::ImportModule::Named(m, span) => {
ImportModule::Named(intern.resolve_import_module(m, *span)?)
}
ast::ImportModule::Named(m, span) => intern.resolve_import_module(m, *span)?,
ast::ImportModule::RawNamed(m, _span) => ImportModule::RawNamed(intern.intern_str(m)),
ast::ImportModule::Inline(idx, _) => ImportModule::Inline(*idx as u32),
})
Expand Down
33 changes: 12 additions & 21 deletions crates/cli-support/src/wit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -880,29 +880,24 @@ impl<'a> Context<'a> {
}

fn determine_import(&self, import: &decode::Import<'_>, item: &str) -> Result<JsImport, Error> {
let is_local_snippet = match import.module {
Some(decode::ImportModule::Named(s)) => self.aux.local_modules.contains_key(s),
Some(decode::ImportModule::RawNamed(_)) => false,
Some(decode::ImportModule::Inline(_)) => true,
None => false,
};

// Similar to `--target no-modules`, only allow vendor prefixes
// basically for web apis, shouldn't be necessary for things like npm
// packages or other imported items.
let vendor_prefixes = self.vendor_prefixes.get(item);
if let Some(vendor_prefixes) = vendor_prefixes {
assert!(vendor_prefixes.len() > 0);

if is_local_snippet {
if let Some(decode::ImportModule::Inline(_) | decode::ImportModule::Named(_)) =
&import.module
{
bail!(
"local JS snippets do not support vendor prefixes for \
the import of `{}` with a polyfill of `{}`",
item,
&vendor_prefixes[0]
);
}
if let Some(decode::ImportModule::Named(module)) = &import.module {
if let Some(decode::ImportModule::RawNamed(module)) = &import.module {
bail!(
"import of `{}` from `{}` has a polyfill of `{}` listed, but
vendor prefixes aren't supported when importing from modules",
Expand Down Expand Up @@ -938,18 +933,14 @@ impl<'a> Context<'a> {
};

let name = match import.module {
Some(decode::ImportModule::Named(module)) if is_local_snippet => {
JsImportName::LocalModule {
module: module.to_string(),
name: name.to_string(),
}
}
Some(decode::ImportModule::Named(module) | decode::ImportModule::RawNamed(module)) => {
JsImportName::Module {
module: module.to_string(),
name: name.to_string(),
}
}
Some(decode::ImportModule::Named(module)) => JsImportName::LocalModule {
module: module.to_string(),
name: name.to_string(),
},
Some(decode::ImportModule::RawNamed(module)) => JsImportName::Module {
module: module.to_string(),
name: name.to_string(),
},
Some(decode::ImportModule::Inline(idx)) => {
let offset = self
.aux
Expand Down

0 comments on commit f82f5c5

Please sign in to comment.