Skip to content

Minor cleanup to symbolWalker #18549

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
2 commits merged into from
Sep 22, 2017
Merged

Minor cleanup to symbolWalker #18549

2 commits merged into from
Sep 22, 2017

Conversation

ghost
Copy link

@ghost ghost commented Sep 18, 2017

Instead of reviewing #17844 I thought I'd just make a pull request implementing some changes.

  • Use sparse arrays for number-keyed maps.
  • Clear the map after use to avoid leaving garbage around.
  • Use forEach instead of a null check followed by for-of; this reduces visitTypeList to a single line, so inlined it.
  • Inline other single-line functions that are only called once.

@ghost ghost requested a review from amcasey September 18, 2017 16:08
@ghost ghost force-pushed the symbolWalker branch 3 times, most recently from c7f4f99 to 6acb418 Compare September 18, 2017 16:14
@ghost ghost force-pushed the symbolWalker branch from 6acb418 to 91211a1 Compare September 18, 2017 16:15
@@ -994,11 +994,6 @@ namespace ts {

/**
* Gets the owned, enumerable property keys of a map-like.
*
* NOTE: This is intended for use with MapLike<T> objects. For Map<T> objects, use
Copy link
Author

Choose a reason for hiding this comment

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

Comment outdated; Map<T> is now a real ES6 map, so we would use the .keys() method.

@amcasey
Copy link
Member

amcasey commented Sep 18, 2017

FYI @weswigham, since this is his code. 😄

@RyanCavanaugh
Copy link
Member

What problem is this solving?

@@ -1011,6 +1006,17 @@ namespace ts {
return keys;
}

export function getOwnValues<T>(sparseArray: T[]): T[] {
const values: T[] = [];
for (const key in sparseArray) {
Copy link
Member

Choose a reason for hiding this comment

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

I'm sure I'm missing something, but how is this different from a for-of loop?

Copy link
Author

Choose a reason for hiding this comment

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

A for-in loop iterates over the keys of an object, while a for-of loop only works on an object implementing the iterator protocol. See MDN

Copy link
Member

@weswigham weswigham Sep 18, 2017

Choose a reason for hiding this comment

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

It's also supposedly identical to Object.values, which, much like hasOwnProperty, which we should probably check for the existence of and use if available.

@ghost
Copy link
Author

ghost commented Sep 18, 2017

@RyanCavanaugh This PR is in lieu of code review, since it was easier to just do it than to try to explain how to do it.

Copy link
Member

@weswigham weswigham left a comment

Choose a reason for hiding this comment

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

I'm generally fine with the spare array stuff; it was using an old-style map-like prior to being updated for the latest compiler, which is essentially the same thing. The inlining I agree less with - the functions are separate for readability, documentation, and logical separation of concerns; I'd prefer not to inline things juts because it's only one line and do away with all that.

@@ -1011,6 +1006,17 @@ namespace ts {
return keys;
}

export function getOwnValues<T>(sparseArray: T[]): T[] {
const values: T[] = [];
for (const key in sparseArray) {
Copy link
Member

@weswigham weswigham Sep 18, 2017

Choose a reason for hiding this comment

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

It's also supposedly identical to Object.values, which, much like hasOwnProperty, which we should probably check for the existence of and use if available.

@@ -66,43 +73,22 @@ namespace ts {
}
}
if (type.flags & TypeFlags.TypeParameter) {
visitTypeParameter(type as TypeParameter);
visitType(getConstraintFromTypeParameter(type as TypeParameter));
Copy link
Member

Choose a reason for hiding this comment

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

I do not really like the inlining of these short functions - it removes the ability to grep for the symbol handling that case, and makes the block less uniform (and potentially makes the outer function more polymorphic). TBQH, if TypeFlags wasn't a flags enum, I'd just have stored these visitors in a map and dispatched on kind; but you can't so 🤷‍♂️ .

@ghost ghost merged commit 38905f4 into master Sep 22, 2017
@ghost ghost deleted the symbolWalker branch September 22, 2017 21:07
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
This pull request was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants