Skip to content

Port symbol walker to latest#9847

Closed
weswigham wants to merge 3 commits into
microsoft:masterfrom
weswigham:symbol-walker
Closed

Port symbol walker to latest#9847
weswigham wants to merge 3 commits into
microsoft:masterfrom
weswigham:symbol-walker

Conversation

@weswigham
Copy link
Copy Markdown
Member

@weswigham weswigham commented Jul 20, 2016

The kind folks over at @angular were wondering if we could port the type/symbol walker we produced for my declaration flattening prototype to our latest codebase and publicly expose it.

This is that.

@evmar @alexeagle

If one of you has a good test corpus for what you want to traverse, I can try to make a test out of it (so we don't break it in the future). You should also verify that it's capable of what you need - otherwise make some suggestions.

@mhegazy
You probably want to verify that we're OK with maintaining this API.

If anyone has any changes you'd like to see, you should probably propose them here.

@evmar
Copy link
Copy Markdown
Contributor

evmar commented Jul 20, 2016

Our current janky code for poking around in types is here:
https://github.com/angular/tsickle/blob/9c3b5a949a0b4355df854466dcceaa327d9eeb30/src/type-translator.ts#L106

One ominous comment from there:

        // This comes up when generating code for an arrow function as passed
        // to a generic function.  The passed-in type is tagged as anonymous
        // and has no properties so it's hard to figure out what to generate.
        // Just avoid it for now so we don't crash.
        this.warn('anonymous type has no symbol');

However, I can't remember the particular scenario this is avoiding, and I commented it out and attempted to reproduce the mentioned crash and can't seem to find it now.

The other thing we've had trouble with is mapping a type back to a qualified name. E.g. the compiler infers x to be a foo.Bar but via x we could only get Bar, which isn't enough to recreate the name. Not sure if it's at all relevant here though.

@weswigham
Copy link
Copy Markdown
Member Author

weswigham commented Jul 20, 2016

@evmar Interestingly enough, this chunk at the bottom of visitSymbol is for handling a similar scenario (anonymous types don't trip up this walker since it happily visits both symbols and types):

forEach(symbol.declarations, d => {
  // Type queries are too far resolved when we just visit the symbol's type
  //  (their type resolved directly to the member deeply referenced)
  // So to get the intervening symbols, we need to check if there's a type
  // query node on any of the symbol's declarations and get symbols there
  if ((d as any).type && (d as any).type.kind === SyntaxKind.TypeQuery) {
    const query = (d as any).type as TypeQueryNode;
    const entity = leftmostSymbol(query.exprName);
    visitSymbol(entity);
  }
});

The original intent for our declaration emitter was to find all the symbols you care about with this, and filter on accept by inspecting the symbol's declarations and checking where they are (user code, stdlib, or external library) and what their flags are (public, protected, private), and collect the visible declarations from the visited symbols. Then you just transform/rewrite the declarations you collected (also keeping a list of all those external references you encountered and filtered out so you can write imports for them).

@weswigham weswigham changed the title Port symbol walker to lastest Port symbol walker to latest Jul 21, 2016
@mprobst
Copy link
Copy Markdown
Contributor

mprobst commented Aug 9, 2016

Thanks @weswigham, this seems like it'd help our use case. We could use this to generate a tool that walks all exported symbols in a module, skipping private. We'd still have to collect what modules symbols are from and emit an appropriate import {Symbol} from 'ambient/module/declaration'; and declare module 'ambient/module/declaration' { ... } bits for our flattened .d.ts file, but that seems doable in a separate project.

amcasey pushed a commit to amcasey/TypeScript that referenced this pull request Aug 16, 2017
@weswigham
Copy link
Copy Markdown
Member Author

#17844 replaces this.

@weswigham weswigham closed this Aug 19, 2017
@weswigham weswigham deleted the symbol-walker branch August 19, 2017 00:20
amcasey pushed a commit to amcasey/TypeScript that referenced this pull request Aug 22, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
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