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

Refactor: normalized encoding of ImportModule #3068

Merged
merged 2 commits into from Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 4 additions & 6 deletions crates/backend/src/encode.rs
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
29 changes: 9 additions & 20 deletions crates/cli-support/src/wit/mod.rs
Expand Up @@ -880,21 +880,14 @@ 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(_)) = &import.module {
lukaslihotzki marked this conversation as resolved.
Show resolved Hide resolved
bail!(
"local JS snippets do not support vendor prefixes for \
the import of `{}` with a polyfill of `{}`",
Expand Down Expand Up @@ -938,18 +931,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