Skip to content

Commit

Permalink
fix: Allow using types for named exports (#738)
Browse files Browse the repository at this point in the history
  • Loading branch information
blacktoast authored Feb 22, 2023
1 parent 7665136 commit 5d3aa0b
Show file tree
Hide file tree
Showing 28 changed files with 53 additions and 215 deletions.
1 change: 1 addition & 0 deletions crates/stc_ts_file_analyzer/src/analyzer/class/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1582,6 +1582,7 @@ impl Analyzer<'_, '_> {
// Then, we can expand super class

let super_type_params = try_opt!(c.super_type_params.validate_with(child));

match &c.super_class {
Some(box expr) => {
let need_base_class = !matches!(expr, RExpr::Ident(..));
Expand Down
15 changes: 7 additions & 8 deletions crates/stc_ts_file_analyzer/src/analyzer/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ impl Analyzer<'_, '_> {
}
RDecl::TsInterface(ref i) => {
i.visit_with(a);

a.export_type(i.span(), i.id.clone().into(), None)
}

Expand All @@ -65,15 +64,15 @@ impl Analyzer<'_, '_> {
let ty = ty.unwrap_or_else(|| Type::any(span, Default::default()));
a.register_type(e.id.clone().into(), ty);

a.storage.export_type(span, a.ctx.module_id, e.id.clone().into());
a.storage
.export_type(span, a.ctx.module_id, e.id.clone().into(), e.id.clone().into());
a.storage
.export_var(span, a.ctx.module_id, e.id.clone().into(), e.id.clone().into());
}
RDecl::TsModule(module) => match &module.id {
RTsModuleName::Ident(id) => {
module.visit_with(a);

a.storage.export_type(span, a.ctx.module_id, id.clone().into());
a.storage.export_type(span, a.ctx.module_id, id.clone().into(), id.clone().into());
}
RTsModuleName::Str(..) => {
let module: Option<Type> = module.validate_with(a)?;
Expand Down Expand Up @@ -236,12 +235,11 @@ impl Analyzer<'_, '_> {
};

let iter = types.into_iter().map(|v| v.into_owned()).map(|v| v.freezed()).collect::<Vec<_>>();

for ty in iter {
self.storage.store_private_type(self.ctx.module_id, name.clone(), ty, false);
}

self.storage.export_type(span, self.ctx.module_id, name);
self.storage.export_type(span, self.ctx.module_id, name, orig_name);
}

/// Exports a variable.
Expand Down Expand Up @@ -329,6 +327,7 @@ impl Analyzer<'_, '_> {
fn validate(&mut self, node: &RExportNamedSpecifier) {
let ctx = Ctx {
report_error_for_non_local_vars: true,
in_export_named: true,
..self.ctx
};
self.with_ctx(ctx).validate_with(|a| {
Expand Down Expand Up @@ -447,8 +446,8 @@ impl Analyzer<'_, '_> {
self.storage.export_var(span, ctxt, id.clone(), orig.clone());
}

if self.storage.get_local_type(ctxt, orig).is_some() {
self.storage.export_type(span, ctxt, id);
if self.storage.get_local_type(ctxt, orig.clone()).is_some() {
self.storage.export_type(span, ctxt, id, orig);
}
}

Expand Down
12 changes: 10 additions & 2 deletions crates/stc_ts_file_analyzer/src/analyzer/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2958,6 +2958,10 @@ impl Analyzer<'_, '_> {
tracker: Default::default(),
}));
}

if let Some(vars) = exports.vars.get(sym).cloned() {
return Ok(vars);
}
}
}
IdCtx::Var => {
Expand All @@ -2978,7 +2982,6 @@ impl Analyzer<'_, '_> {
}
}
}

// No property found
return Err(ErrorKind::NoSuchPropertyInModule {
span,
Expand Down Expand Up @@ -3879,10 +3882,15 @@ impl Analyzer<'_, '_> {
return Ok(ty.clone().into_owned());
}

if let Type::Module(..) = ty.normalize() {
if let Type::Module(..) | Type::Alias(..) = ty.normalize() {
return Ok(ty.clone().into_owned());
}

if self.ctx.in_export_named {
return Ok(ty.clone().into_owned());
}
}

Err(ErrorKind::TypeUsedAsVar {
span,
name: i.clone().into(),
Expand Down
3 changes: 2 additions & 1 deletion crates/stc_ts_file_analyzer/src/analyzer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ pub(crate) struct Ctx {
in_assign_rhs: bool,

in_export_decl: bool,

in_export_named: bool,
skip_identical_while_inference: bool,

super_references_super_class: bool,
Expand Down Expand Up @@ -521,6 +521,7 @@ impl<'scope, 'b> Analyzer<'scope, 'b> {
in_return_arg: false,
in_assign_rhs: false,
in_export_decl: false,
in_export_named: false,
skip_identical_while_inference: false,
super_references_super_class: false,
in_class_with_super: false,
Expand Down
4 changes: 2 additions & 2 deletions crates/stc_ts_file_analyzer/src/analyzer/scope/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,7 @@ impl Analyzer<'_, '_> {
self.storage.store_private_type(self.ctx.module_id, name.clone(), ty.clone(), false);

if !self.config.is_builtin {
self.storage.export_type(ty.span(), self.ctx.module_id, name.clone());
self.storage.export_type(ty.span(), self.ctx.module_id, name.clone(), name.clone());
}

self.scope.register_type(name, ty.clone(), false);
Expand Down Expand Up @@ -1884,9 +1884,9 @@ impl<'a> Scope<'a> {
if cfg!(debug_assertions) {
debug!("Analyzer.find_type('{}')", name);
}

if let Some(ty) = self.facts.types.get(name) {
debug_assert!(ty.is_clone_cheap(), "{:?}", ty);

// println!("({}) find_type({}): Found (cond facts)", self.depth(), name);
return Some(ItemRef::Single(iter::once(ty)));
}
Expand Down
12 changes: 6 additions & 6 deletions crates/stc_ts_storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub trait TypeStore: Send + Sync {
fn store_private_type(&mut self, ctxt: ModuleId, id: Id, ty: Type, should_override: bool);
fn store_private_var(&mut self, ctxt: ModuleId, id: Id, ty: Type);

fn export_type(&mut self, span: Span, ctxt: ModuleId, id: Id);
fn export_type(&mut self, span: Span, ctxt: ModuleId, id: Id, orig_name: Id);
fn export_var(&mut self, span: Span, ctxt: ModuleId, id: Id, orig_name: Id);

fn reexport_type(&mut self, span: Span, ctxt: ModuleId, id: JsWord, ty: Type);
Expand Down Expand Up @@ -136,10 +136,10 @@ impl TypeStore for Single<'_> {
}
}

fn export_type(&mut self, span: Span, ctxt: ModuleId, id: Id) {
fn export_type(&mut self, span: Span, ctxt: ModuleId, id: Id, orig_name: Id) {
debug_assert_eq!(ctxt, self.id);

match self.info.exports.private_types.get(&id).cloned() {
match self.info.exports.private_types.get(&orig_name).cloned() {
Some(ty) => {
*self.info.exports.types.entry(id.sym().clone()).or_default() = ty;
}
Expand Down Expand Up @@ -290,9 +290,9 @@ impl TypeStore for Group<'_> {
}
}

fn export_type(&mut self, span: Span, ctxt: ModuleId, id: Id) {
fn export_type(&mut self, span: Span, ctxt: ModuleId, id: Id, orig_name: Id) {
let e = self.info.entry(ctxt).or_default();
match e.private_types.get(&id) {
match e.private_types.get(&orig_name) {
Some(v) => {
e.types.insert(id.sym().clone(), v.clone());
}
Expand Down Expand Up @@ -433,7 +433,7 @@ impl TypeStore for Builtin {

fn export_var(&mut self, _: Span, _: ModuleId, _: Id, _: Id) {}

fn export_type(&mut self, _: Span, _: ModuleId, _: Id) {}
fn export_type(&mut self, _: Span, _: ModuleId, _: Id, _: Id) {}

fn get_local_type(&self, _ctxt: ModuleId, id: Id) -> Option<Type> {
let types = self.types.get(id.sym()).cloned()?;
Expand Down
8 changes: 8 additions & 0 deletions crates/stc_ts_type_checker/tests/conformance.pass.txt
Original file line number Diff line number Diff line change
Expand Up @@ -911,9 +911,15 @@ es6/modules/exportAndImport-es3-amd.ts
es6/modules/exportAndImport-es3.ts
es6/modules/exportAndImport-es5-amd.ts
es6/modules/exportAndImport-es5.ts
es6/modules/exportsAndImports1-amd.ts
es6/modules/exportsAndImports1-es6.ts
es6/modules/exportsAndImports1.ts
es6/modules/exportsAndImports2-amd.ts
es6/modules/exportsAndImports2-es6.ts
es6/modules/exportsAndImports2.ts
es6/modules/exportsAndImports3-amd.ts
es6/modules/exportsAndImports3-es6.ts
es6/modules/exportsAndImports3.ts
es6/modules/exportsAndImportsWithContextualKeywordNames02.ts
es6/modules/exportsAndImportsWithUnderscores4.ts
es6/modules/importEmptyFromModuleNotExisted.ts
Expand Down Expand Up @@ -1633,6 +1639,8 @@ externalModules/multipleExportDefault4.ts
externalModules/multipleExportDefault6.ts
externalModules/nameDelimitedBySlashes.ts
externalModules/typeOnly/nestedNamespace.ts
externalModules/typeOnly/preserveValueImports_errors.ts
externalModules/typeOnlyMerge1.ts
functions/functionOverloadCompatibilityWithVoid02.ts
functions/functionOverloadCompatibilityWithVoid03.ts
functions/functionParameterObjectRestAndInitializers.ts
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 0,
matched_error: 0,
extra_error: 5,
extra_error: 0,
panic: 0,
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 0,
matched_error: 0,
extra_error: 5,
extra_error: 0,
panic: 0,
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 0,
matched_error: 0,
extra_error: 5,
extra_error: 0,
panic: 0,
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 0,
matched_error: 0,
extra_error: 14,
extra_error: 0,
panic: 0,
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 0,
matched_error: 0,
extra_error: 14,
extra_error: 0,
panic: 0,
}
Loading

0 comments on commit 5d3aa0b

Please sign in to comment.