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

[WIP] feat: implement ambient comment #1100

Closed
wants to merge 2 commits into from
Closed
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
39 changes: 32 additions & 7 deletions crates/stc_ts_file_analyzer/src/analyzer/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ impl Analyzer<'_, '_> {
let dep_id = match dep_id {
Some(v) => v,
None => {
dbg!(&base, &dst);

self.storage.report(ErrorKind::ModuleNotFound { span }.into());

return (ctxt, Type::any(span, Default::default()));
Expand All @@ -40,12 +42,14 @@ impl Analyzer<'_, '_> {
let data = match self.data.imports.get(&(ctxt, dep_id)).cloned() {
Some(v) => v,
None => {
dbg!(&base, &dst, &dep_id);

self.storage.report(ErrorKind::ModuleNotFound { span }.into());

return (ctxt, Type::any(span, Default::default()));
}
};

dbg!(&dep_id, &data);
(dep_id, data)
}

Expand Down Expand Up @@ -79,20 +83,32 @@ impl Analyzer<'_, '_> {
if self.config.is_builtin {
return;
}
let loader = self.loader;

#[inline]
fn is_relative_path(path: &str) -> bool {
path.starts_with("./") || path.starts_with("../")
}
dbg!(&module_spans);
// We first load non-circular imports.
let imports = ImportFinder::find_imports(&self.comments, module_spans, &self.storage, items);
let (imports, reference) = ImportFinder::find_imports(&self.comments, module_spans, &self.storage, items);

let loader = self.loader;
// {
// let current_ctxt = self.ctx.module_id;
// let base = self.storage.path(current_ctxt);

// for (ctxt, src) in reference {
// let _ = loader.load_non_circular_dep(&base, &src);
// }
// }

dbg!(&reference, &imports);
let mut normal_imports = vec![];
for (ctxt, import) in imports {
dbg!(&ctxt, &import);
let span = import.span;
let base = self.storage.path(ctxt);
let dep_id = self.loader.module_id(&base, &import.src);
let dep_id = loader.module_id(&base, &import.src);
let dep_id = match dep_id {
Some(v) => v,
_ if !is_relative_path(&import.src) => {
Expand All @@ -110,7 +126,8 @@ impl Analyzer<'_, '_> {

normal_imports.push((ctxt, base.clone(), dep_id, import.src.clone(), import));
}

dbg!(&normal_imports);
dbg!("end!");
let import_results = if cfg!(feature = "no-threading") {
let iter = normal_imports.into_iter();

Expand Down Expand Up @@ -294,6 +311,7 @@ where
cur_ctxt: ModuleId,
to: Vec<(ModuleId, DepInfo)>,
comments: C,
references: Vec<(ModuleId, JsWord)>,
}

impl<'a, C> ImportFinder<'a, C>
Expand All @@ -308,11 +326,17 @@ where
let ctxt = self.cur_ctxt;
let deps = find_imports_in_comments(&self.comments, span);

self.references.extend(deps.clone().into_iter().map(|i| (ctxt, i.to_path())));
self.to
.extend(deps.into_iter().map(|src| (ctxt, DepInfo { span, src: src.to_path() })));
}

pub fn find_imports<T>(comments: C, module_span: Vec<(ModuleId, Span)>, storage: &'a Storage<'a>, node: &T) -> Vec<(ModuleId, DepInfo)>
pub fn find_imports<T>(
comments: C,
module_span: Vec<(ModuleId, Span)>,
storage: &'a Storage<'a>,
node: &T,
) -> (Vec<(ModuleId, DepInfo)>, Vec<(ModuleId, JsWord)>)
where
T: for<'any> VisitWith<ImportFinder<'any, C>>,
{
Expand All @@ -321,6 +345,7 @@ where
storage,
to: Default::default(),
cur_ctxt: ModuleId::builtin(),
references: Default::default(),
};

for (ctxt, span) in module_span {
Expand All @@ -332,7 +357,7 @@ where

node.visit_with(&mut v);

v.to
(v.to, v.references)
}
}

Expand Down
8 changes: 6 additions & 2 deletions crates/stc_ts_type_checker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ use rnode::{NodeIdGenerator, RNode, VisitWith};
use stc_ts_ast_rnode::{RModule, RStr, RTsModuleName};
use stc_ts_dts::{apply_mutations, cleanup_module_for_dts};
use stc_ts_env::Env;
use stc_ts_errors::{debug::debugger::Debugger, Error};
use stc_ts_errors::{
debug::{debugger::Debugger, print_backtrace},
Error,
};
use stc_ts_file_analyzer::{analyzer::Analyzer, loader::Load, validator::ValidateWith, ModuleTypeData, VResult};
use stc_ts_storage::{ErrorStore, File, Group, Single};
use stc_ts_types::{ModuleId, Type};
Expand Down Expand Up @@ -389,7 +392,8 @@ impl Load for Checker {

fn declare_module(&self, name: &JsWord, module: Type) -> ModuleId {
module.assert_clone_cheap();

dbg!(&name, &module);
print_backtrace();
let module_id = self
.module_loader
.load_module(&Arc::new(FileName::Custom(name.to_string())), false)
Expand Down
Loading