Skip to content

docs(website): add preview of scope manager to playground #4312

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
merged 17 commits into from
Dec 23, 2021

Conversation

armano2
Copy link
Collaborator

@armano2 armano2 commented Dec 15, 2021

PR Checklist

Overview

Rendering of ScopeManager in playground on website.

@armano2 armano2 added the documentation Documentation ("docs") that needs adding/updating label Dec 15, 2021
@nx-cloud
Copy link

nx-cloud bot commented Dec 15, 2021

☁️ Nx Cloud Report

CI ran the following commands for commit 6cc81df. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this branch


✅ Successfully ran 44 targets

Sent with 💌 from NxCloud.

@typescript-eslint

This comment has been minimized.

@netlify
Copy link

netlify bot commented Dec 15, 2021

✔️ Deploy Preview for typescript-eslint ready!

🔨 Explore the source changes: 6cc81df

🔍 Inspect the deploy log: https://app.netlify.com/sites/typescript-eslint/deploys/61c38d38d06e650008876847

😎 Browse the preview: https://deploy-preview-4312--typescript-eslint.netlify.app

@bradzacher
Copy link
Member

This is great! So many people don't understand scope analysis - so this will make it easier for people to figure out!

For the scope tree representation - the good thing to display would be the class type of the thing as it makes it a bit easier to understand. Similar to how we display the node type in the AST
image

EG I do this in the snapshots - https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/scope-manager/tests/fixtures/call-expression/call-expression.ts.shot

the serializers

IDK how hard it is to do - but it'd be good if we can expand the relevant scope for the cursor position as well (like we do for the AST when we expand the relevant node). We should be able to achieve this by inspecting the location of the scope object's .block node

@armano2
Copy link
Collaborator Author

armano2 commented Dec 15, 2021

great idea @bradzacher, i'm going to add selecting nodes and names

@armano2
Copy link
Collaborator Author

armano2 commented Dec 16, 2021

i did small poc locally and we can definitely do node selection, and display correct name, i'm going to have to do some refactoring, but, that's expected :P

image


current implementation doesn't allow me to do selection, as its going to fall into big loop (there is no check for references - infinite loop)

I'm going have to prepare mapped model before starting printing ast, this actually helps with printing AST as json :)

@bradzacher
Copy link
Member

yeah that's looking really awesome! The labels make it so much clearer!

@armano2 armano2 self-assigned this Dec 16, 2021
@armano2
Copy link
Collaborator Author

armano2 commented Dec 16, 2021

@bradzacher sadly ony one way selection make sense, let me know what do you think about this :)

image

image

selection works for variables, identifiers and scopes

@armano2
Copy link
Collaborator Author

armano2 commented Dec 17, 2021

i noticed weird behavior with scopes while working on this, $id is changing/increasing on every build, looks that something is not correctly cleared

@bradzacher
Copy link
Member

bradzacher commented Dec 20, 2021

This is looking really, really great!

A few changes I would suggest:


image

In the snapshots I "terminated" these duplicated things on purpose to keep the snapshots shorter and cause fewer LOC during updates. But in the AST viewer I think trimming these makes it harder to use. For example - Reference$3 in the screenshot; I don't know where it is or how to view it.

Clicking on it to expand it could work - but that's probably just code complication we don't really care about? Let's just print the duplicated object in those cases. We can stop cyclical/infinite printing by just disabling expansion of the upper scope (eg keep it trimmed? Or maybe hide it entirely?)


image

Same feedback for variables. Variables are easier to find because of the root "variables" array. But because we declare so many globals it can be pretty clunky to find the right variable.


id is changing/increasing on every build, looks that something is not correctly cleared

Yeah that makes sense because the IDs are just monotonically increasing forever. As long as this module's state doesn't reset - then the ID counter won't reset either.

https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/scope-manager/src/ID.ts

@armano2
Copy link
Collaborator Author

armano2 commented Dec 20, 2021

thank you for feedback, i'm going to work on it

Clicking on it to expand it could work - but that's probably just code complication we don't really care about? Let's just print the duplicated object in those cases. We can stop cyclical/infinite printing by just disabling expansion of the upper scope (eg keep it trimmed? Or maybe hide it entirely?)

there is actually way way more recursion in this structure,
Refference -> Variable -> Reference
Scope -> Variable -> Scope
Reference -> Scope -> Reference
etc..

i have idea how to solve this but its going to be tricky :)

@bradzacher
Copy link
Member

bradzacher commented Dec 20, 2021

Come to think of it recursion shouldn't be a problem as long as the react component doesn't render its children unless expanded. Then it's up to the user to just not infinitely expand a certain tree!


If we want to prevent cyclic expansion though - one could maintain a list of things in the current tree branch, and stop rendering when we encounter an item we've already seen in the branch.

That way you can expand the same thing in two different branches, but not in the same branch.

@armano2

This comment has been minimized.

@armano2
Copy link
Collaborator Author

armano2 commented Dec 22, 2021

Circular tree should be printed correctly now

image

@codecov
Copy link

codecov bot commented Dec 22, 2021

Codecov Report

Merging #4312 (e216f63) into main (0cd911a) will decrease coverage by 0.01%.
The diff coverage is n/a.

❗ Current head e216f63 differs from pull request most recent head 6cc81df. Consider uploading reports for the commit 6cc81df to get more accurate results

@@            Coverage Diff             @@
##             main    #4312      +/-   ##
==========================================
- Coverage   93.52%   93.51%   -0.02%     
==========================================
  Files         336      142     -194     
  Lines       11596     2867    -8729     
  Branches     3291      594    -2697     
==========================================
- Hits        10845     2681    -8164     
+ Misses        479      128     -351     
+ Partials      272       58     -214     
Flag Coverage Δ
unittest 93.51% <ø> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
...ages/eslint-plugin/src/rules/no-empty-interface.ts
...s/eslint-plugin/src/rules/no-unused-expressions.ts
...ackages/eslint-plugin/src/rules/no-for-in-array.ts
...experimental-utils/src/ts-eslint-scope/Variable.ts
.../src/rules/sort-type-union-intersection-members.ts
packages/eslint-plugin/src/rules/typedef.ts
packages/experimental-utils/src/json-schema.ts
...int-plugin/src/rules/require-array-sort-compare.ts
packages/eslint-plugin/src/rules/semi.ts
...eslint-plugin/src/rules/prefer-return-this-type.ts
... and 184 more

@armano2
Copy link
Collaborator Author

armano2 commented Dec 22, 2021

printing of function results should work now

image

@armano2 armano2 requested a review from bradzacher December 22, 2021 14:23
bradzacher
bradzacher previously approved these changes Dec 22, 2021
Copy link
Member

@bradzacher bradzacher left a comment

Choose a reason for hiding this comment

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

it's looking really good!
this is in a great spot now!

'functionExpressionScope',
'childScopes',
'upper',
];
Copy link
Member

Choose a reason for hiding this comment

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

nitpick - to ensure we don't accidentally add bad keys here or forget to remove keys - would this work to validate the keys are correct?

(if it does - we should do it for all of the arrays here)

Suggested change
];
] as (keyof Scope)[];

Copy link
Collaborator Author

@armano2 armano2 Dec 22, 2021

Choose a reason for hiding this comment

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

there is runtime validation if field is present

doing casts is going to be more complex as base node doesn't always declare all properties

rest property is present only in parameter definition

Copy link
Collaborator Author

@armano2 armano2 Dec 22, 2021

Choose a reason for hiding this comment

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

if we want to ensure that all properties are printed, we could remove this function completely and instead of

      let values: [string, unknown][];

      const props = getProps(nodeType);
      if (props) {
        values = props.map(key => {
          const res = data[key];
          return [key, typeof res === 'function' ? res.bind(data) : res];
        });
      } else {
        values = Object.entries(data);
      }

do

      let values = Object.entries(data);

      if (nodeName) {
        values = values.map(([key, res]) => [
          key,
          typeof res === 'function' ? res.bind(data) : res,
        ]);
      }

but that can cause execution of some additional functions, and unknown sorting of keys

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

as for functions i'm going to add in new PR support for lazy execution for typescript types

Co-authored-by: Brad Zacher <brad.zacher@gmail.com>
@armano2 armano2 changed the title docs(website): add scope manager to playground preview docs(website): add scope manager to playground Dec 23, 2021
@armano2 armano2 changed the title docs(website): add scope manager to playground docs(website): add preview of scope manager to playground Dec 23, 2021
Copy link
Member

@bradzacher bradzacher left a comment

Choose a reason for hiding this comment

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

brilliant! Thanks for building this!
This is awesome!

@bradzacher bradzacher merged commit 135f30a into main Dec 23, 2021
@bradzacher bradzacher deleted the docs/scope-manager-printer branch December 23, 2021 18:00
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
documentation Documentation ("docs") that needs adding/updating
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

2 participants