Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
sor4chi committed Nov 9, 2024
1 parent d67cd1b commit 2e4cae0
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 3 deletions.
10 changes: 10 additions & 0 deletions checker/specification/staging.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
Currently implementing:

> This file is for work-in-progress and can help separating features that are being implemented to regressions
### Printing

#### Template Literal Type

```ts
const invalidStr: `Hi${string}` = 'Hello, there!';
```

- Type "Hello, there!" is not assignable to type `Hi${string}`
61 changes: 60 additions & 1 deletion checker/src/types/printing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,54 @@ pub fn print_type_with_type_arguments(
buf
}

pub fn print_template_literal_into_buf(
ty: TypeId,
buf: &mut String,
types: &TypeStore,
root: bool,
) {
let r#type = types.get_type_by_id(ty);
if root {
buf.push('`');
}

match r#type {
Type::Constant(cst) => {
buf.push_str(&cst.as_js_string());
}
Type::Class { name, type_parameters } => {
buf.push_str("${");
buf.push_str(name);
if let Some(type_parameters) = type_parameters {
buf.push('<');
for (not_at_end, param) in type_parameters.iter().nendiate() {
print_template_literal_into_buf(*param, buf, types, false);
if not_at_end {
buf.push_str(", ");
}
}
buf.push('>');
}
buf.push('}');
}
Type::AliasTo { to, .. } => {
print_template_literal_into_buf(*to, buf, types, false);
}
Type::Constructor(constructor) => match constructor {
Constructor::BinaryOperator { lhs, rhs, .. } => {
print_template_literal_into_buf(*lhs, buf, types, false);
print_template_literal_into_buf(*rhs, buf, types, false);
}
_ => {}
},
_ => {}
}

if root {
buf.push('`');
}
}

/// Recursion safe + reuses buffer
pub fn print_type_into_buf<C: InformationChain>(
ty: TypeId,
Expand Down Expand Up @@ -469,7 +517,18 @@ pub fn print_type_into_buf<C: InformationChain>(
unreachable!()
}
},
_constructor => {
constructor => {
if let Constructor::BinaryOperator { result: result_ty, .. } = constructor {
let result = types.get_type_by_id(*result_ty);
if *result_ty != TypeId::NUMBER_TYPE
&& !matches!(
result,
Type::PartiallyAppliedGenerics(_) | Type::RootPolyType(_)
) {
print_template_literal_into_buf(ty, buf, types, true);
return;
}
}
let base = get_constraint(ty, types).unwrap();
print_type_into_buf(base, buf, cycles, args, types, info, debug);
}
Expand Down
4 changes: 2 additions & 2 deletions parser/fuzz/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2e4cae0

Please sign in to comment.