Skip to content
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

Variables of kind ConditionalExpression have defaultValue set to condition #401

Closed
nicknisi opened this issue Feb 1, 2017 · 2 comments
Closed
Labels
bug Functionality does not match expectation

Comments

@nicknisi
Copy link
Collaborator

nicknisi commented Feb 1, 2017

Given the following TypeScript code, TypeDoc produces some incorrect output.

Source File:

/**
 * some function
 * @param str some string
 * @returns true or false
 */
export const assign = has('something') ? function(str: string): boolean { return true; } : function (str: string): boolean { return false; };

Output:

screen shot 2017-02-01 at 9 13 03 am

What seems to be happening is that this is going through the variable converter, and inside of the switch statement, the node.initializer.kind value is 193 ("ConditionalExpression"). This means that it hits the default portion of the switch statement and sets the whole conditional as the defaultValue, which seems incorrect given TypeScript can determine the type for the conditional expression.

I know that TypeScript can determine what the actual type is for the assign variable.

context.checker.typeToString(context.checker.getTypeAtLocation(node.initializer))

screen shot 2017-02-01 at 9 19 10 am

I'm looking into how TypeDoc can infer this as well. What I am initially looking at is treating ConditionalExpression variable kinds as a special case in that case statement, and creating a new node converter to handle converting the conditional into a type. I'm still trying to determine how to create modify the variable reflection to have the correct type and be of the correct kind (the above screenshot should be marked as a function but it isn't) from the conditional. Does this sound like a reasonable approach?

@aciccarello aciccarello added the bug Functionality does not match expectation label Feb 3, 2017
@nicknisi
Copy link
Collaborator Author

nicknisi commented Feb 8, 2017

Looking into this a bit more, I'm a bit confused on the best way to fix this. Adding a check for ts.SyntaxKind.ConditionalExpression in the switch statement in variable.ts does prevent the default value from being set to the entire ternary statement, but it gets complicated from there and I'm not sure how to correctly translate the node into declarations from there.

If the values on either side of the ternary are not functions, it seems like doing nothing works, and the correct type / union type is displayed.

If node.initializer.whenTrue and node.initializer.whenFalse are both method, then we need to determine if they have the same method signature, and if so, running just one through converter.convertNode outputs the correct method signature in the TypeDoc documents.

If whenTrue and whenFalse are both functions but they have different method signatures, then both of them can be displayed by running converter.convertNode on both of them.

But, if either one is a function when the other is not, I am not really sure how to correctly spit that information out, or what it should look like, given TypeDoc comments could have some @param tags that might map to just the function part of the expression.

Also, it looks like the same types of checking might need to occur when the node.initializer's kind is ts.SyntaxKind.BinaryExpression, where the checking is on the left and right properties, such as in

export const foo = someValue || someOtherValue;

@Gerrit0
Copy link
Collaborator

Gerrit0 commented Dec 26, 2020

In 0.20, we use the type checker better, and will document the code in the op as:

image

@Gerrit0 Gerrit0 closed this as completed Dec 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Functionality does not match expectation
Projects
None yet
Development

No branches or pull requests

3 participants