Skip to content

Commit

Permalink
Merge branch 'main' into simple-cfg
Browse files Browse the repository at this point in the history
  • Loading branch information
ekerfelt authored Nov 15, 2024
2 parents 1179cce + 3f026fe commit d4649aa
Show file tree
Hide file tree
Showing 22 changed files with 762 additions and 97 deletions.
3 changes: 2 additions & 1 deletion cli/daemon/export/infra_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ func buildAndValidateInfraConfig(params EmbeddedInfraConfigParams) (*infra.Infra
var svcDeps = map[string]struct{}{}
pkgs := fns.ToMap(md.Pkgs, (*meta.Package).GetRelPath)

// Add dependencies for all outbound RPCs for our hosted services.
// Add dependencies for all outbound RPCs for our hosted services
// and collect all required secrets.
for _, p := range md.Pkgs {
if p.ServiceName == "" {
secrets = append(secrets, p.Secrets...)
Expand Down
1 change: 1 addition & 0 deletions cli/daemon/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ func (r *Run) buildAndStart(ctx context.Context, tracker *optracker.OpTracker, i
OpTracker: tracker,
Experiments: expSet,
WorkingDir: r.Params.WorkingDir,
Environ: r.Params.Environ,
})
if err != nil {
return errors.Wrap(err, "compile error")
Expand Down
2 changes: 2 additions & 0 deletions pkg/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ type CompileParams struct {

// Override to explicitly allow the Encore version to be set.
EncoreVersion option.Option[string]

Environ []string
}

type ArtifactString string
Expand Down
2 changes: 1 addition & 1 deletion proto/encore/daemon/daemon.proto
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ message RunRequest {

// debug_mode specifies the debug mode to use.
DebugMode debug_mode = 11;

enum BrowserMode {
BROWSER_AUTO = 0;
BROWSER_NEVER = 1;
Expand Down
16 changes: 13 additions & 3 deletions tsparser/src/parser/module_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,18 @@ impl ModuleLoader {
self.by_path.borrow().get(&path).cloned()
}

pub fn resolve_import(
pub fn resolve_import_from_module(
&self,
module: &Module,
import_path: &str,
) -> Result<Option<Lrc<Module>>, Error> {
self.resolve_import(&module.swc_file_path, import_path)
}

pub fn resolve_import(
&self,
from_file: &swc_common::FileName,
import_path: &str,
) -> Result<Option<Lrc<Module>>, Error> {
// Special case for the generated clients.
// TODO: Fix this to do actual import path resolution.
Expand All @@ -119,10 +127,9 @@ impl ModuleLoader {

let target_file_path = {
// TODO: cache this
let file_name: FileName = module.file_path.clone().into();
let mod_path = self
.resolver
.resolve(&file_name, import_path)
.resolve(from_file, import_path)
.map_err(|err| Error::UnableToResolve(import_path.to_string(), err))?;
match mod_path {
FileName::Real(ref buf) => {
Expand Down Expand Up @@ -296,6 +303,7 @@ pub struct Module {
pub id: ModuleId,
pub ast: swc_ecma_ast::Module,
pub file_path: FilePath,
pub swc_file_path: swc_common::FileName,
/// How the module was imported, if it's an external module.
pub module_path: Option<String>,
pub comments: Box<dyn Comments>,
Expand All @@ -321,11 +329,13 @@ impl Module {
comments: Option<Box<dyn Comments>>,
) -> Lrc<Self> {
let comments: Box<dyn Comments> = comments.unwrap_or_else(|| Box::new(NoopComments {}));
let swc_file_path = file_path.clone().into();
Lrc::new(Self {
file_set,
id,
ast,
file_path,
swc_file_path,
module_path,
comments,
cached_imports: OnceCell::new(),
Expand Down
207 changes: 182 additions & 25 deletions tsparser/src/parser/types/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,23 @@ pub struct Using {
pub expr: Option<Box<ast::Expr>>,
}

#[derive(Debug)]
pub struct NamedReexport {
pub orig_name: String,
pub renamed: Option<String>,
}

#[derive(Debug)]
pub enum Reexport {
List {
items: Vec<NamedReexport>,
import_path: String,
},
All {
import_path: String,
},
}

#[derive(Debug)]
pub struct NSData {
/// The objects imported by the module.
Expand All @@ -156,6 +173,9 @@ pub struct NSData {
/// The default export, if any.
pub default_export: Option<Rc<Object>>,

// Reexports from other modules.
pub reexports: Vec<Reexport>,

/// Export items that haven't yet been processed.
#[allow(dead_code)]
pub unprocessed_exports: Vec<ast::ModuleItem>,
Expand Down Expand Up @@ -188,10 +208,53 @@ impl NSData {
top_level: HashMap::new(),
named_exports: HashMap::new(),
default_export: None,
reexports: vec![],
unprocessed_exports: vec![],
}
}

pub fn get_named_export(
&self,
ctx: &ResolveState,
curr_module: &swc_common::FileName,
needle: &str,
) -> Option<Rc<Object>> {
if needle == "default" {
if let Some(default) = &self.default_export {
return Some(default.clone());
}
}
if let Some(obj) = self.named_exports.get(needle) {
return Some(obj.clone());
}

for re in &self.reexports {
match re {
Reexport::List { import_path, items } => {
for item in items {
let export_name = item.renamed.as_ref().unwrap_or(&item.orig_name);
if export_name == needle {
let module = ctx.resolve_module_import(curr_module, import_path)?;
return module
.data
.get_named_export(ctx, curr_module, &item.orig_name);
}
}
}

Reexport::All { import_path } => {
if let Some(module) = ctx.resolve_module_import(curr_module, import_path) {
if let Some(export) = module.data.get_named_export(ctx, curr_module, needle)
{
return Some(export);
}
}
}
}
}
None
}

fn add_top_level(&mut self, id: AstId, obj: Rc<Object>) -> Rc<Object> {
if let Some(other) = self.top_level.get(&id) {
// Unhandled overload most likely, return the existing object for now.
Expand Down Expand Up @@ -231,41 +294,72 @@ fn process_module_items(ctx: &ResolveState, ns: &mut NSData, items: &[ast::Modul
}
}

// TODO implement
ast::ModuleDecl::ExportDefaultDecl(_) => {
log::debug!("TODO export default declaration");
ast::ModuleDecl::ExportDefaultDecl(decl) => {
let obj = process_default_decl(ctx, &decl.decl);
if ns.default_export.is_some() {
obj.range.err("duplicate default export");
}
ns.default_export = Some(obj);
}

// TODO(andre) Can this affect the module namespace?
ast::ModuleDecl::ExportDefaultExpr(_) => {
ast::ModuleDecl::ExportDefaultExpr(_expr) => {
log::debug!("TODO export default expr");
}

ast::ModuleDecl::ExportNamed(decl) => {
// Re-exporting from another module.
for spec in &decl.specifiers {
if let ast::ExportSpecifier::Named(named) = spec {
log::info!("re-export name {:?}", named.orig);
}
}
log::debug!("TODO re-export named declaration");
let Some(src) = &decl.src else {
log::debug!("ExportNamed without src");
continue;
};

ns.reexports.push(Reexport::List {
import_path: src.value.to_string(),
items: decl
.specifiers
.iter()
.filter_map(|spec| match spec {
ast::ExportSpecifier::Named(named) => {
let orig_name = module_export_name_to_string(&named.orig);
Some(NamedReexport {
orig_name,
renamed: named
.exported
.as_ref()
.map(module_export_name_to_string),
})
}
ast::ExportSpecifier::Default(_) => {
log::debug!("TODO: ExportNamed with default");
None
}
ast::ExportSpecifier::Namespace(_) => {
log::debug!("TODO: ExportNamed with namespace");
None
}
})
.collect(),
});
}

ast::ModuleDecl::ExportAll(_) => {
ast::ModuleDecl::ExportAll(decl) => {
// Re-exporting * from another module.
log::debug!("TODO re-export * declaration");
ns.reexports.push(Reexport::All {
import_path: decl.src.value.to_string(),
});
}

ast::ModuleDecl::TsImportEquals(_) => {
log::debug!("TODO ts import equals");
}

ast::ModuleDecl::TsExportAssignment(_) => {
log::debug!("TODO ts export =");
ast::ModuleDecl::TsExportAssignment(decl) => {
log::debug!("TsExportAssignment {:#?}", decl);
}

ast::ModuleDecl::TsNamespaceExport(_) => {
log::debug!("TODO ts namespace export");
ast::ModuleDecl::TsNamespaceExport(decl) => {
log::debug!("TsNamespaceExport {:#?}", decl);
}
},

Expand Down Expand Up @@ -477,6 +571,44 @@ fn process_decl(ctx: &ResolveState, ns: &mut NSData, decl: &ast::Decl) -> Vec<Rc
}
}

fn process_default_decl(ctx: &ResolveState, decl: &ast::DefaultDecl) -> Rc<Object> {
let range: Range = decl.span().into();
match decl {
ast::DefaultDecl::Class(d) => {
let name = d.ident.as_ref().map(|id| id.sym.to_string());
ctx.new_obj(
name,
range,
ObjectKind::Class(Class {
spec: d.class.clone(),
}),
)
}

ast::DefaultDecl::Fn(d) => {
let name = d.ident.as_ref().map(|id| id.sym.to_string());
ctx.new_obj(
name,
range,
ObjectKind::Func(Func {
spec: d.function.clone(),
}),
)
}

ast::DefaultDecl::TsInterfaceDecl(d) => {
let name = Some(d.id.sym.to_string());
ctx.new_obj(
name,
range,
ObjectKind::TypeName(TypeName {
decl: TypeNameDecl::Interface(*d.clone()),
}),
)
}
}
}

fn process_namespace_body(ctx: &ResolveState, ns: &mut NSData, body: &ast::TsNamespaceBody) {
match body {
ast::TsNamespaceBody::TsModuleBlock(block) => {
Expand Down Expand Up @@ -595,13 +727,11 @@ impl ResolveState {
}

fn module_id(&self) -> Result<ModuleId> {
let module_id = self
.module_stack
.borrow()
let stack = self.module_stack.borrow();
let module = stack
.last()
.ok_or_else(|| anyhow::anyhow!("internal error: no module on stack"))?
.to_owned();
Ok(module_id)
.ok_or_else(|| anyhow::anyhow!("internal error: no module on stack"))?;
Ok(module.to_owned())
}

pub(super) fn resolve_module_ident(
Expand Down Expand Up @@ -635,8 +765,23 @@ impl ResolveState {
None
}

pub(super) fn resolve_module_import(
&self,
from_file: &swc_common::FileName,
import_path: &str,
) -> Option<Rc<Module>> {
let ast_module = match self.loader.resolve_import(from_file, import_path) {
Ok(Some(ast_module)) => ast_module,
Ok(None) | Err(_) => return None,
};
Some(self.get_or_init_module(ast_module))
}

pub(super) fn resolve_import(&self, module: &Module, imp: &ImportedName) -> Option<Rc<Object>> {
let ast_module = match self.loader.resolve_import(&module.base, &imp.import_path) {
let ast_module = match self
.loader
.resolve_import(&module.base.swc_file_path, &imp.import_path)
{
Ok(None) => return None,
Ok(Some(ast_module)) => Ok(ast_module),
Err(err) => Err(err),
Expand All @@ -653,7 +798,9 @@ impl ResolveState {
match &imp.kind {
ImportKind::Named(name) => {
let imported = self.get_or_init_module(ast_module);
let obj = imported.data.named_exports.get(name).cloned();
let obj = imported
.data
.get_named_export(self, &imported.base.swc_file_path, name);

if obj.is_none() {
HANDLER.with(|handler| {
Expand All @@ -666,7 +813,10 @@ impl ResolveState {
}
ImportKind::Default => {
let imported = self.get_or_init_module(ast_module);
let obj = imported.data.default_export.as_ref().cloned();
let obj =
imported
.data
.get_named_export(self, &imported.base.swc_file_path, "default");

if obj.is_none() {
HANDLER.with(|handler| {
Expand All @@ -685,3 +835,10 @@ impl ResolveState {
}
}
}

fn module_export_name_to_string(name: &ast::ModuleExportName) -> String {
match name {
ast::ModuleExportName::Ident(i) => i.sym.to_string(),
ast::ModuleExportName::Str(str) => str.value.as_str().to_string(),
}
}
Loading

0 comments on commit d4649aa

Please sign in to comment.