Git Product home page Git Product logo

Comments (20)

kdy1 avatar kdy1 commented on September 28, 2024 2

Of course, thank you!

from stc.

kdy1 avatar kdy1 commented on September 28, 2024 1

You have two options.
./scripts/test.sh yourTestName will work, but if you want to reduce it further, you can copy it to tests directory of the file analyzer

https://github.com/dudykr/stc/tree/2ab93ad46cd2683539300bde8f30c7bf89ac1e15/crates/stc_ts_file_analyzer/tests

from stc.

kdy1 avatar kdy1 commented on September 28, 2024 1

Oh, then there's Type::Optional

from stc.

kdy1 avatar kdy1 commented on September 28, 2024 1

Not sure. You can search for RPat::Assign using IDE to look at possible cases.

I expect it to be one of

RPat::Assign(pat) => return self.default_type_for_pat(&pat.left),

if let RPat::Assign(assign_pat) = p {
let ctx = Ctx {
cannot_be_tuple: true,
..self.ctx
};
let mut a = self.with_ctx(ctx);
assign_pat.right.validate_with_default(&mut *a).report(&mut a.storage)
} else {
None
}

if let RPat::Assign(assign_pat) = p {
// Handle default value
if let Some(default_value_ty) = default_value_ty.clone() {
let ty = assign_pat.left.get_ty().map(|v| v.validate_with(self));
// If pat mode is declare, assignment of default value will be handled by
// variable declator function.
if let PatMode::Assign = self.ctx.pat_mode {
if let Some(Ok(ty)) = &ty {
self.assign_with_opts(
&mut Default::default(),
&ty,
&default_value_ty,
AssignOpts {
span: assign_pat.span,
..Default::default()
},
)
.report(&mut self.storage);
}
}
let ty = ty.unwrap_or_else(|| {
let mut ty = default_value_ty.generalize_lit().foldable();
if matches!(ty.normalize(), Type::Tuple(..)) {
ty.normalize_mut();
match ty {
Type::Tuple(tuple) => {
let mut types = tuple.elems.into_iter().map(|element| *element.ty).collect::<Vec<_>>();
types.dedup_type();
ty = Type::Array(Array {
span: tuple.span,
elem_type: box Type::union(types),
metadata: ArrayMetadata {
common: tuple.metadata.common,
..Default::default()
},
});
}
_ => {
unreachable!();
}
}
}
Ok(ty)
})?;
// Remove default value.
if let Some(pat_node_id) = assign_pat.left.node_id() {
if let Some(m) = &mut self.mutations {
m.for_pats.entry(pat_node_id).or_default().ty = Some(ty)
}
}
}
}

RPat::Assign(p) => {
let type_ann = p.left.get_ty();
let type_ann: Option<Type> = match type_ann {
Some(v) => v.validate_with(self).report(&mut self.storage),
None => None,
};
let is_typed = type_ann.is_some();
let mut type_ann = type_ann.or(default);
type_ann.make_clone_cheap();
let mut right = p
.right
.validate_with_args(self, (TypeOfMode::RValue, None, type_ann.as_ref().or(ty.as_ref())))
.report(&mut self.storage)
.unwrap_or_else(|| Type::any(span, Default::default()));
if self.ctx.is_fn_param && type_ann.is_none() {
// If the declaration includes an initializer expression (which is permitted
// only when the parameter list occurs in conjunction with a
// function body), the parameter type is the widened form (section
// 3.11) of the type of the initializer expression.
right = right.fold_with(&mut Widen { tuple_to_array: true });
}
right.make_clone_cheap();
if let Some(left) = &type_ann {
self.assign_with_opts(
&mut Default::default(),
&left,
&right,
AssignOpts {
span: p.right.span(),
allow_assignment_to_param_constraint: false,
..Default::default()
},
)
.report(&mut self.storage);
}
let default = if is_typed {
type_ann
} else {
opt_union(span, type_ann, Some(right))
};
return self
.add_vars(
&p.left,
ty,
actual,
default,
DeclareVarsOpts {
use_iterator_for_array: true,
..opts
},
)
.context("tried to declare a variable with an assignment pattern");
}

RPat::Assign(p) => {
let type_ann = p.left.get_ty();
let type_ann: Option<Type> =
type_ann.and_then(|v| v.validate_with(self).report(&mut self.storage));
let type_ann = type_ann.or(default_ty.clone());
let right = p
.right
.validate_with_args(
self,
(TypeOfMode::RValue, None, type_ann.as_ref().or(Some(&ty))),
)
.report(&mut self.storage)
.unwrap_or_else(|| Type::any(span, Default::default()));
Ok(right)
}

from stc.

Beraliv avatar Beraliv commented on September 28, 2024

Hey @kdy1 !

Can I take this one? I'd like to work on it

from stc.

Beraliv avatar Beraliv commented on September 28, 2024

Thanks to your comment @hyp3rflow (link πŸ”— – #179 (comment)).

I've also decided to double check error list in TypeScript repo and it's exactly the same (conformance tests are also identical):

So tests are up-to-date

from stc.

Beraliv avatar Beraliv commented on September 28, 2024

Now I'm trying to understand how I can run one specific test and get the same output that you shared on the screenshot

Working with output

I've checked the typical flow in CONTRIBUTING.md, but it says:

Each error will contain lots of information required for debugging, with context: prefix. Read it, and if it's a wrong error, copy in to ./crates/stc_ts_file_analyzer/tests/pass.

Output is so huge, how can you work with it? Do you maybe write it to the file and read when ./scripts/check.sh is completed? I'm running it in VSCode and it's impossible to read anything. I would really appreciate any advice here.

Running single test

So even though I couldn't get context: when I ran ./scripts/check.sh, I still created a file in crates/stc_ts_file_analyzer/tests/pass.

I called it es6/destructuring/destructuringArrayBindingPatternAndAssignment3/.6.ts – in this commit https://github.com/Beraliv/stc/commit/aa917fe078694bf97e37fcb4f2d82ad6880d7584

When I run ./scripts/check.sh, it's still running all 500 tests but not the single one that I just added

Do I miss anything?

Get the same output

As I see on the screenshot, there's a DebugContext and the error below.

Can you please navigate me what I need to do to see it as well?

from stc.

Beraliv avatar Beraliv commented on September 28, 2024

I only continued it today, as didn't have time on the weekend

I was checking the whole fn assign_without_wrapping in crates/stc_ts_file_analyzer/src/analyzer/assign/mod.rs to be able to understand the general idea what we match with what.

The idea of the solution in my head looks like:

  1. if lhs.len >= rhs.len, let's check default parameters and exclude them
  2. if lhs_without_default_params.len === rhs.len, good, let's move on to other existing checks
  3. otherwise, error as previously

I will try to come up with the solution in the PR this week

I'm sorry for being a little slow as I'm learning a lot here

from stc.

Beraliv avatar Beraliv commented on September 28, 2024

The type of arguments are correctly inferred as [any, any, any, any]

I'm still thinking whether it should be [any, any?, any?, any?] instead? As default parameters mean that the parameter is optional. Do you have a representation for that?

At least that's what TS Playground infers for this example – https://tsplay.dev/Wkk2DW

from stc.

kdy1 avatar kdy1 commented on September 28, 2024

There's a representation, but any? is any

It's exactly the same type

from stc.

Beraliv avatar Beraliv commented on September 28, 2024

There's a representation, but any? is any

It's exactly the same type

Not exactly, the example is here – https://tsplay.dev/WvGJkw

  1. type A = [any, any, any, any]; – A has a fixed length, which equals to 4
  2. type B = [any, any?, any?, any?]; means tuple can be of length between 1 and 4

That's why assigning [1] to type B will work, but won't work for type A

from stc.

kdy1 avatar kdy1 commented on September 28, 2024

@Beraliv Are you actively working on it? I'm asking this because it's a good-first-issue

from stc.

Beraliv avatar Beraliv commented on September 28, 2024

@kdy1, sorry for leaving no progress for 8 days. I struggled with the fix so wasn't sure how to proceed with it.

Is it possible you give me some hints?

I'm happy to continue with it, but if anyone already has a fix, I'm happy to let her/him do it instead of me

from stc.

Beraliv avatar Beraliv commented on September 28, 2024

Just to be transparent, that's what I have now – #298

from stc.

Beraliv avatar Beraliv commented on September 28, 2024

You have two options. ./scripts/test.sh yourTestName will work, but if you want to reduce it further, you can copy it to tests directory of the file analyzer

https://github.com/dudykr/stc/tree/2ab93ad46cd2683539300bde8f30c7bf89ac1e15/crates/stc_ts_file_analyzer/tests

I can confirm that for crates/stc_ts_type_checker/tests/conformance/es6/destructuring/destructuringArrayBindingPatternAndAssignment3.ts running ./scripts/test.sh destructuringArrayBindingPatternAndAssignment3 worked for me

Thank you again for the help!

from stc.

kdy1 avatar kdy1 commented on September 28, 2024

Do you still need help?

from stc.

Beraliv avatar Beraliv commented on September 28, 2024

Do you still need help?

Let me come back to you in the evening, as I'm on the conference today.

I will explain what I try to do and where I need to be navigated

from stc.

Beraliv avatar Beraliv commented on September 28, 2024

I imagine any is implicit for function parameters so they're inferred as [any, any, any, any].

But as we have default parameters, I wonder if it would be possible to convert it to [any, any?, any?, any?].

If so, what is the best place to do it? Because for now I get elems already with the wrong type.

I know how to do it when it will be of a different type (match l as element of elems with Optional as you suggested above, if it's true for the rest of tuple, don't emit error - βœ…)

So the confusing part for me is that place for inferring type for function parameters. Can you please navigate me in the codebase so I know where to make a change?

from stc.

Beraliv avatar Beraliv commented on September 28, 2024

I keep looking at it.

Was there any change to main branch? I pulled changes from there and I'm trying to rebuild it and I see errors:

➜ cargo install --path .
  Installing stc v0.1.0 (/Users/beraliv/Documents/Code/stc)
    Updating crates.io index
warning: unused import: `std::cell::RefCell`
 --> crates/stc_utils/src/error.rs:1:5
  |
1 | use std::cell::RefCell;
  |     ^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

warning: unused import: `crate::panic_context`
 --> crates/stc_utils/src/error.rs:3:5
  |
3 | use crate::panic_context;
  |     ^^^^^^^^^^^^^^^^^^^^

warning: unused imports: `cell::RefCell`, `panic`, `sync::Once`
 --> crates/stc_utils/src/panic_context.rs:1:11
  |
1 | use std::{cell::RefCell, panic, sync::Once};
  |           ^^^^^^^^^^^^^  ^^^^^  ^^^^^^^^^^

warning: `stc_utils` (lib) generated 3 warnings
   Compiling stc_ts_errors v0.1.0 (/Users/beraliv/Documents/Code/stc/crates/stc_ts_errors)
error[E0432]: unresolved import `crate::context::with_ctx`
  --> crates/stc_ts_errors/src/lib.rs:28:5
   |
28 | use crate::context::with_ctx;
   |     ^^^^^^^^^^^^^^^^^^^^^^^^ no `with_ctx` in `context`

warning: unused import: `std::cell::RefCell`
 --> crates/stc_ts_errors/src/context.rs:1:5
  |
1 | use std::cell::RefCell;
  |     ^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

warning: unused import: `rnode::RNode`
 --> crates/stc_ts_errors/src/debug/debugger.rs:3:5
  |
3 | use rnode::RNode;
  |     ^^^^^^^^^^^^

warning: unused import: `stc_ts_ast_rnode::RTsType`
 --> crates/stc_ts_errors/src/debug/debugger.rs:4:5
  |
4 | use stc_ts_ast_rnode::RTsType;
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^

warning: unused imports: `Emitter`, `Node`, `text_writer::JsWriter`
 --> crates/stc_ts_errors/src/debug/debugger.rs:7:24
  |
7 | use swc_ecma_codegen::{text_writer::JsWriter, Emitter, Node};
  |                        ^^^^^^^^^^^^^^^^^^^^^  ^^^^^^^  ^^^^

error[E0599]: no method named `push` found for unit type `()` in the current scope
  --> crates/stc_ts_errors/src/lib.rs:70:23
   |
70 |         self.contexts.push(context.to_string());
   |                       ^^^^ method not found in `()`

error[E0599]: no method named `iter` found for unit type `()` in the current scope
  --> crates/stc_ts_errors/src/lib.rs:90:34
   |
90 |         for ctx in self.contexts.iter().rev() {
   |                                  ^^^^ method not found in `()`

error[E0599]: no method named `push` found for unit type `()` in the current scope
    --> crates/stc_ts_errors/src/lib.rs:2002:43
     |
2002 |                         buf[idx].contexts.push(format!("duplicate: {}", e.contexts.join("\n")));
     |                                           ^^^^ method not found in `()`

error[E0599]: no method named `join` found for unit type `()` in the current scope
    --> crates/stc_ts_errors/src/lib.rs:2002:84
     |
2002 |                         buf[idx].contexts.push(format!("duplicate: {}", e.contexts.join("\n")));
     |                                                                                    ^^^^ method not found in `()`

Some errors have detailed explanations: E0432, E0599.
For more information about an error, try `rustc --explain E0432`.
warning: `stc_ts_errors` (lib) generated 4 warnings
error: could not compile `stc_ts_errors` due to 5 previous errors; 4 warnings emitted
error: failed to compile `stc v0.1.0 (/Users/beraliv/Documents/Code/stc)`, intermediate artifacts can be found at `/Users/beraliv/Documents/Code/stc/target`

Not sure if I'm doing something wrong

from stc.

Beraliv avatar Beraliv commented on September 28, 2024

Thank you for merging my PR @kdy1!

I will keep looking at assignments and maybe raise a separate PR later this week to cover wider scope

Sorry again for taking a lot of time as I'm familiarising with the codebase and learning Rust at the same time

from stc.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.