Skip to content

Commit

Permalink
Fix null span in template literal arguments (#150)
Browse files Browse the repository at this point in the history
* Last minute fixes
- Fix null span used for template literal argument position
- Allow aliases in key lookup
- Add Record, at and join to env
- Add more clarification to the tests
  • Loading branch information
kaleidawave authored May 31, 2024
1 parent ab2fcba commit fbf6aae
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 42 deletions.
Binary file modified checker/definitions/internal.ts.d.bin
Binary file not shown.
39 changes: 25 additions & 14 deletions checker/definitions/overrides.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
@Constant
declare function debug_type_independent(t: any): void;

// Eventually this will be merged with existing TS es5.d.ts files but for now is the standalone see #121

interface ImportEnv {
[key: string]: string | undefined;
}
Expand Down Expand Up @@ -128,22 +130,31 @@ declare class Array<T> {
// return false
// }

// join(joiner: string = ","): string {
// const { length } = this;
// let i: number = 1;
// if (length === 0) {
// return ""
// }
// let s: string = "" + this[0];
// while (i < length) {
// s += joiner;
// s += this[i++];
// // debug_type_independent(s)
// }
// return s
// }
join(joiner: string = ","): string {
const { length } = this;
let i: number = 1;
if (length === 0) {
return ""
}
let s: string = "" + this[0];
while (i < length) {
s += joiner;
s += this[i++];
}
return s
}

at(index: number) {
if (index < 0) {
return this[index + this.length]
} else {
return this[index]
}
}
}

type Record<K extends string, T> = { [P in K]: T }

declare class Map<T, U> {
#keys: Array<T> = [];
#value: Array<T> = [];
Expand Down
16 changes: 16 additions & 0 deletions checker/definitions/simple.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,20 @@ declare class Array<T> {
// return false
// }

join(joiner: string = ","): string {
const { length } = this;
let i: number = 1;
if (length === 0) {
return ""
}
let s: string = "" + this[0];
while (i < length) {
s += joiner;
s += this[i++];
}
return s
}

at(index: number) {
if (index < 0) {
return this[index + this.length]
Expand All @@ -140,6 +154,8 @@ declare class Array<T> {
}
}

type Record<K extends string, T> = { [P in K]: T }

declare class Map<T, U> {
#keys: Array<T> = [];
#value: Array<T> = [];
Expand Down
20 changes: 9 additions & 11 deletions checker/specification/specification.md
Original file line number Diff line number Diff line change
Expand Up @@ -2777,6 +2777,8 @@ what(2, 3) satisfies string;

> Aka generic property keys
> Only works for `Record` atm. And while defined in the root environment, want to test the definition here as well
#### Simple key

```ts
Expand All @@ -2794,11 +2796,9 @@ myRecord.hello;
#### Assignment

```ts
type Record2<K extends string, T> = { [P in K]: T }

const x: Record2<"test", boolean> = { no: false },
y: Record2<"test", boolean> = { test: 6 },
z: Record2<"test", boolean> = { test: false };
const x: Record<"test", boolean> = { no: false },
y: Record<"test", boolean> = { test: 6 },
z: Record<"test", boolean> = { test: false };
```

- 'no' is not a property of { [\"test\"]: boolean }
Expand All @@ -2808,16 +2808,14 @@ const x: Record2<"test", boolean> = { no: false },
#### Union and types as keys

```ts
type Record2<K extends string, T> = { [P in K]: T }

declare let obj1: Record2<"hi" | "hello", boolean>;
declare let obj1: Record<"hi" | "hello", boolean>;

obj1.hi satisfies boolean;
obj1.hello satisfies boolean;

obj1.bye;

declare let obj2: Record2<string, boolean>;
declare let obj2: Record<string, boolean>;
obj2.fine satisfies boolean;
obj2[2];
```
Expand Down Expand Up @@ -3267,12 +3265,12 @@ safeDivide(10, 0);
```ts
function throwGreeting() {
throw "Hello";
return 5
return "Unreachable!"
}

function doSomething() {
throwGreeting()
const x = 2;
const unreachable = 2;
}
```

Expand Down
9 changes: 8 additions & 1 deletion checker/specification/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ use std::{
sync::{Arc, Mutex},
};

use checker::{diagnostics, synthesis::EznoParser};
use checker::{
diagnostics,
source_map::{Nullable, SourceId},
synthesis::EznoParser,
};

// This is here as it is used in the included `/specification.rs`
use parser::ASTNode;

mod specification {
Expand Down Expand Up @@ -96,6 +102,7 @@ fn check_errors(
.map(|diag| {
let (reason, pos) = diag.reason_and_position();
if let Some(pos) = pos {
assert_ne!(pos.source, SourceId::NULL);
// TODO position
reason
} else {
Expand Down
13 changes: 9 additions & 4 deletions checker/src/features/template_literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,16 @@ where
);
static_part_count += 1;
}
p @ TemplateLiteralPart::Dynamic(_) => {
TemplateLiteralPart::Dynamic(expression) => {
let position =
A::expression_position(expression).with_source(environment.get_source());
arguments.push(SynthesisedArgument {
value: part_to_type(p, environment, checking_data),
// TODO position
position: source_map::Nullable::NULL,
value: part_to_type(
TemplateLiteralPart::Dynamic(expression),
environment,
checking_data,
),
position,
spread: false,
});
}
Expand Down
13 changes: 3 additions & 10 deletions checker/src/synthesis/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,6 @@ pub(super) fn type_definition_file<T: crate::ReadFromFS>(
StatementOrDeclaration::Declaration(Declaration::Class(class)) => {
register_statement_class_with_members(&class.on, &mut environment, checking_data);
}
StatementOrDeclaration::Declaration(Declaration::TypeAlias(TypeAlias {
name: _,
references: _,
parameters,
position: _,
})) => {
if let Some(_parameters) = parameters {
todo!("set parameters")
}
}
StatementOrDeclaration::Declaration(Declaration::Function(function)) => {
crate::synthesis::variables::register_variable_identifier(
&function.on.name.identifier,
Expand All @@ -139,6 +129,9 @@ pub(super) fn type_definition_file<T: crate::ReadFromFS>(
},
);
}
StatementOrDeclaration::Declaration(Declaration::TypeAlias(TypeAlias { .. })) => {
crate::utilities::notify!("Don't think anything needed here");
}
StatementOrDeclaration::Statement(
Statement::Comment(..) | Statement::Empty(..) | Statement::AestheticSemiColon(..),
) => {}
Expand Down
12 changes: 11 additions & 1 deletion checker/src/types/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1439,7 +1439,8 @@ pub(crate) fn key_matches(
)
} else if let Type::RootPolyType(PolyNature::MappedGeneric {
eager_fixed: to, ..
}) = want_ty
})
| Type::AliasTo { to, .. } = want_ty
{
key_matches(
(key, key_type_arguments),
Expand Down Expand Up @@ -1470,6 +1471,15 @@ pub(crate) fn key_matches(

if let Type::RootPolyType(PolyNature::MappedGeneric { eager_fixed: to, .. }) = key_type
{
crate::utilities::notify!("Special behavior?");
return key_matches(
(&PropertyKey::Type(*to), key_type_arguments),
(want, want_type_arguments),
types,
);
}

if let Type::AliasTo { to, .. } = key_type {
return key_matches(
(&PropertyKey::Type(*to), key_type_arguments),
(want, want_type_arguments),
Expand Down
4 changes: 3 additions & 1 deletion checker/src/types/subtyping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1248,7 +1248,9 @@ fn subtype_properties(
if let Some(GenericChainLink::Link { ref from, parent_link, value: _ }) =
base_type_arguments
{
assert!(parent_link.is_none(), "TODO recursive get_from");
if parent_link.is_none() {
crate::utilities::notify!("TODO recursive get_from");
}
*from
} else {
base_type
Expand Down

0 comments on commit fbf6aae

Please sign in to comment.