Skip to content

docs(website): add simple ts ast viewer #4243

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 10, 2021
Merged

Conversation

armano2
Copy link
Collaborator

@armano2 armano2 commented Dec 1, 2021

PR Checklist

Overview

This is initial draft of ast viewer that supports / prints typescript ast

Notable changes

  • AST viewer for Typescript has been added
  • Rendering both ts and es ast is faster
  • query pram accepts now showAST= ts|es|true|false
  • highlighting of nodes work only when you hover over property names and node names
  • array elements display key (in most cases 0..*) but they can display custom properties (in case of ts)

TODO:

  • we should exclude some SyntaxKind values (eg. FirstStatement)
  • Add custom formatting for flags
  • Add support for selecting nodes (in editor and in AST)

Maybe we should split this PR to ast viewer changes and accessibility fixes

@typescript-eslint
Copy link
Contributor

Thanks for the PR, @armano2!

typescript-eslint is a 100% community driven project, and we are incredibly grateful that you are contributing to that community.

The core maintainers work on this in their personal time, so please understand that it may not be possible for them to review your work immediately.

Thanks again!


🙏 Please, if you or your company is finding typescript-eslint valuable, help us sustain the project by sponsoring it transparently on https://opencollective.com/typescript-eslint. As a thank you, your profile/company logo will be added to our main README which receives thousands of unique visitors per day.

@netlify
Copy link

netlify bot commented Dec 1, 2021

❌ Deploy Preview for typescript-eslint failed.

🔨 Explore the source changes: 137fd38

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

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

nx-cloud bot commented Dec 1, 2021

@armano2 armano2 self-assigned this Dec 1, 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.

right now the right panel has 3 states: off, ESTree AST, TS AST.
instead of "off", it'd probably good to instead make it show the lint results (eg similar to how the TS playground shows the TS typecheck results in the right panel).

This would probably make for a nicer UX as you can make the right-panel a tabbed view rather than putting right panel controls in the left bar. Also means you can represent them as a set of tabs (eg more "radio button" like), rather than the current UX of a "checkbox".

"radio button" is nicer because it's a singular selection whereas "checkboxes" are intended for multi-select.


In terms of the AST - have you used https://ts-ast-viewer.com/ ?
My problem with just dumping the TS AST like this is that there is so much cruft, and a lot of the state is stored as integer flags.

I also think there are different methods for traversing the AST which can reveal a different structure.

For example - this is what your traversal shows:

image

But this is what ts-ast-viewer shows:

image

Note how yours has the "useless" FirstStatement node. Different traversal styles? Or does ts-ast-viewer ignore some nodes?

Also for the integer flags - can we "decompose" them?
EG instead of displaying 256 - we could do something like ts-ast-viewer does:
image

@armano2
Copy link
Collaborator Author

armano2 commented Dec 2, 2021

thank you for your feedback

main difference between ts-ast-viewer and what i'm showing is, that i do not use functions / iterators to traverse nodes, (i also hide undefined fields)

in ts-ast-viewer iteration is either done by doing getChildren or ferEachChild, rather than that i just print entire object structure as is from what we get in ts

additionally i do hide few of internal properties, and everything that starts with _

export const propsToFilter = [
  'parent',
  'comments',
  'tokens',
  // 'loc',
  'jsDoc',
  'lineMap',
  'externalModuleIndicator',
  'bindDiagnostics',
  'modifierFlagsCache',
  'transformFlags',
  'resolvedModules',
];

as for FirstStatement they display it as VariableStatement, this is correct, as number represenation of node is same (maybe i should exclude some of them)

VariableStatement = 236,
FirstStatement = 236,

there is actualy lots of values in this enum that we should ignore

        FirstAssignment = 63,
        LastAssignment = 78,
        FirstCompoundAssignment = 64,
        LastCompoundAssignment = 78,
        FirstReservedWord = 81,
        LastReservedWord = 116,
        FirstKeyword = 81,
        LastKeyword = 159,
        FirstFutureReservedWord = 117,
        LastFutureReservedWord = 125,
        FirstTypeNode = 176,
        LastTypeNode = 199,
        FirstPunctuation = 18,
        LastPunctuation = 78,
        FirstToken = 0,
        LastToken = 159,
        FirstTriviaToken = 2,
        LastTriviaToken = 7,
        FirstLiteralToken = 8,
        LastLiteralToken = 14,
        FirstTemplateToken = 14,
        LastTemplateToken = 17,
        FirstBinaryOperator = 29,
        LastBinaryOperator = 78,
        FirstStatement = 236,
        LastStatement = 252,
        FirstNode = 160,
        FirstJSDocNode = 307,
        LastJSDocNode = 345,
        FirstJSDocTagNode = 325,
        LastJSDocTagNode = 345,

current logic to retrieve node name looks like this

function getTypeName(value: unknown): string | undefined {
  if (
    isRecord(value) &&
    typeof value.kind === 'number' &&
    window.ts.SyntaxKind[value.kind]
  ) {
    return String(window.ts.SyntaxKind[value.kind]);
  }
  return undefined;
}

as for displaying eslint lint result, i'm fine with that but not in this PR

@armano2 armano2 dismissed a stale review via 2d37e85 December 2, 2021 08:36
@armano2 armano2 dismissed a stale review via f14404a December 2, 2021 18:47
@armano2
Copy link
Collaborator Author

armano2 commented Dec 2, 2021

ok small update, FirstStatement is now a VariableStatement some goes for other types

i added tooltips to display flags:

image


do you prefer to hide or show undefined values in tree?

we could maybe add some options to it, but i'm not sure about design tho

image

@armano2
Copy link
Collaborator Author

armano2 commented Dec 3, 2021

update:

switching between different ast viewer options is now a drop-down instead of checkboxes

image

@bradzacher
Copy link
Member

do you prefer to hide or show undefined values in tree?

Let's hide them - there's so much junk in the AST so lets not pollute it with junk

@armano2
Copy link
Collaborator Author

armano2 commented Dec 3, 2021

let me know if you see something else design / functionality wise that has to be fixed.


as for arrays, instead of

image image

we are going now properly count only array values

image image

@armano2 armano2 marked this pull request as ready for review December 3, 2021 13:18
@armano2
Copy link
Collaborator Author

armano2 commented Dec 5, 2021

type informations are a little more tricky, and they are going to be added in separate PR (after figure out how to display them)

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.

quick eyeball of the code - LGTM - a few comments.

@armano2 armano2 dismissed a stale review via 137fd38 December 9, 2021 09:53
@bradzacher bradzacher merged commit b9407c5 into main Dec 10, 2021
@bradzacher bradzacher deleted the docs/playground-ast-viewer branch December 10, 2021 05:59
@bradzacher
Copy link
Member

fyi @G-Rath - you can use our playground instead of using astexplorer now!

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 30, 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