Dark Mode

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Allow var statements to declare multiple symbols#9

Merged
imteekay merged 15 commits intomasterfrom
variable-declaration-list
Jul 17, 2023
Merged

Allow var statements to declare multiple symbols#9
imteekay merged 15 commits intomasterfrom
variable-declaration-list

Conversation

Copy link
Owner

imteekay commented May 21, 2023 *
edited
Loading

  • Add test to cover the new variable declaration list AST node
  • Update all tests that have the var statement
  • Create the comma token, parse the list AST node (with a list of declarations) -- followed this example
  • Type check each statement in the var declaration list

imteekay changed the title VariableDeclarationList: allow var to have multiple declarations Allow var to have multiple declarations May 21, 2023
imteekay commented May 21, 2023
src/check.ts Outdated
Copy link
Owner Author

imteekay May 21, 2023 *
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure which type I should return for VariableDeclarationList nodes.

But looking at this example on the TS AST, the type is a "any" type.

Copy link

sandersn Jul 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds about right for many nodes that don't have a sensible type. However, Var isn't really a statement anymore, so it should be handled in a different place from checkStatement. (this is true in the transformer and emitter too)

Copy link
Owner Author

imteekay Jul 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense. I just made a change handle var as not a statement. imteekay/mini-typescript@147c648

imteekay self-assigned this May 21, 2023
imteekay commented May 21, 2023
README.md Outdated
Copy link
Owner Author

imteekay May 21, 2023 *
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sandersn, what did you mean by all declarations with the same type? When defining multiple variables with a var, we can have different types, right?

For instance, this would work:

var var1: string = 'test', var2: number = 2;

Copy link

sandersn Jul 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whoops. I was unclear on this exercise. What I meant was that this is legal typescript:

var var1: string = 'test';
var var1: string = 'what, another one?'
var var1 = 'no type annotation needed'

But you cannot have a conflicting type or type annotation like var var1 = 12

This PR implements var statements that declare multiple symbols, not symbols with multiple declarations, each from different statements. I see now that both are valid interpretations of "Allow var to have multiple declarations".

Copy link
Owner Author

imteekay Jul 6, 2023 *
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oooh, now I got it! Thanks for clarifying.
I created a new exercise "Allow var statements to declare multiple symbols" and this PR will handle that. So I can handle the "Allow var to have multiple declarations" in another PR.

imteekay/mini-typescript@232fa06

sandersn reacted with thumbs up emoji
Copy link
Owner Author

imteekay commented May 21, 2023

cc @sandersn

imteekay force-pushed the variable-declaration-list branch from 4060228 to dcab74f Compare May 21, 2023 14:10
sandersn reviewed Jul 5, 2023
Copy link

sandersn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks about right. For long-term correctness, I would make Var not a statement, but it actually works fine for now.

README.md Outdated
Copy link

sandersn Jul 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whoops. I was unclear on this exercise. What I meant was that this is legal typescript:

var var1: string = 'test';
var var1: string = 'what, another one?'
var var1 = 'no type annotation needed'

But you cannot have a conflicting type or type annotation like var var1 = 12

This PR implements var statements that declare multiple symbols, not symbols with multiple declarations, each from different statements. I see now that both are valid interpretations of "Allow var to have multiple declarations".

src/types.ts Outdated
export type Statement = ExpressionStatement | Var | TypeAlias | EmptyStatement;
export type Statement =
| ExpressionStatement
| Var
Copy link

sandersn Jul 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Var isn't really a statement anymore, it's a child of VariableStatement.

imteekay reacted with thumbs up emoji
Copy link
Owner Author

imteekay Jul 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

src/check.ts Outdated
Copy link

sandersn Jul 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds about right for many nodes that don't have a sensible type. However, Var isn't really a statement anymore, so it should be handled in a different place from checkStatement. (this is true in the transformer and emitter too)

imteekay merged commit e5749be into master Jul 17, 2023
imteekay deleted the variable-declaration-list branch July 17, 2023 21:20
imteekay changed the title Allow var to have multiple declarations Allow var statements to declare multiple symbols Jul 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

1 more reviewer

sandersn sandersn left review comments

Reviewers whose approvals may not affect merge requirements

Assignees

imteekay

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

2 participants