Giter VIP home page Giter VIP logo

Comments (12)

dsherret avatar dsherret commented on May 13, 2024

Thanks for reporting this! I could have sworn I thought about function overloads. In this case, it's definitely better for us to stick exactly with the AST so I'll change getConstructor() to getConstructors().

from ts-morph.

sfrooster avatar sfrooster commented on May 13, 2024

But isn't the issue that those parameters should be available in... I'm guessing getInstanceMembers, since they're declared with public modifiers?

from ts-morph.

dsherret avatar dsherret commented on May 13, 2024

Oh yeah, completely glossed over that because I started thinking about overloads. Anyway, the overload issue is fixed in 0.36.0. I think it's right that the parameters would be returned in getInstanceProperties() and getInstanceMembers().

from ts-morph.

sfrooster avatar sfrooster commented on May 13, 2024

Great - I really appreciate your responsiveness!

from ts-morph.

dsherret avatar dsherret commented on May 13, 2024

Ok, implemented in 0.37.0. I also fixed up some of the return types for these methods on ClassDeclaration... they were sometimes including types that would never be returned (too bad that typescript can't implicitly figure this out atm via the filter method).

Closing this. Let me know if this is still an issue. Also, I just happened to be around right now and I'm only doing quick fast releases because this is still very much a work in progress and experimental :)

from ts-morph.

sfrooster avatar sfrooster commented on May 13, 2024

I'll give it a shot this evening!

from ts-morph.

sfrooster avatar sfrooster commented on May 13, 2024

It does look like I'm getting these values now. One distinction - and maybe I'm wrong here - but properties are constructs like:

    private _testField: string = null;
    get fieldName() : string {
        return this._testField;
    }
    set fieldName(name : string) {
        this._testField = name;
    }

This piece seemed to be working correctly before.

I don't think there's a way to define a property via the constructor parameters in which case those should only be appearing in the getAll/InstanceMembers return values whereas they are also being returned in the from the getInstanceProperties method call.

In any case, if I'm wrong, happy to be corrected but the "public" constructor params are now showing up as members.

from ts-morph.

sfrooster avatar sfrooster commented on May 13, 2024

The following error seems to have been introduced (I don't think it's my config):

node_modules/ts-simple-ast/dist/compiler/common/Node.d.ts(6,22): error TS2420: Class 'Node' incorrectly implements interface 'IDisposable'.
Property 'dispose' is missing in type 'Node'.

from ts-morph.

dsherret avatar dsherret commented on May 13, 2024

Sorry, Disposable error is gone in 0.37.1. I was trying to hide the dispose method because it's internal to the library, but forgot to hide it in the interface too. I still haven't implemented #33.

So parameters with a scope in a constructor are officially called "parameter properties" (see here). I think it's better to include them in getInstanceProperties() because it will cause people who use this library to not forget about them (like how I forgot about them in this issue :)). It will give them a reminder that constructor parameters can possibly be properties too and that might be something they need to handle. If they don't need to worry about them, they can filter the parameters out of the returned results.

from ts-morph.

dsherret avatar dsherret commented on May 13, 2024

I just re-read over that typescript doc I linked to. I need getInstanceProperties() to also return a result when people use the readonly keyword hah (forgot about that because I never use only the readonly keyword with parameter properties... usually I only make them private readonly). I'll fix that in 0.37.2.

Edit: Fixed. Actually fixed in 0.38.0 because I added isParameterProperty(): boolean to ParameterDeclaration.

from ts-morph.

sfrooster avatar sfrooster commented on May 13, 2024

Ok, I've just pulled this and will clean-up my code accordingly. Also, thanks for the clarification on parameter properties - very helpful!

from ts-morph.

dsherret avatar dsherret commented on May 13, 2024

@sfrooster fyi, in the upcoming 0.43.0, most methods in this library will only return the implementations of functions, methods, and constructors except when it's ambient. You can get the overloads by calling .getOverloads() on the functions.

So for example:

const code = "function myFunction(str: string); function myFunction(num: number); function myFunction(strOrNum: string | number) {}";
const ast = new Ast();
const sourceFile = ast.addSourceFile("file.ts", code);

// this returns only 1 function declaration (the implementation) instead of 3
sourceFile.getFunctions();

const myFunction = sourceFile.getFunction("myFunction")!;
myFunction.getOverloads(); // returns the 2 overloads

That said...

const code = "declare function myFunction(str: string); declare function myFunction(num: number);`";
const ast = new Ast();
const sourceFile = ast.addSourceFile("file.ts", code);

// this returns the two function declarations because there is no implementation
sourceFile.getFunctions();

^ I'm mentioning this here because the same applies to getConstructors()

from ts-morph.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.