-
Notifications
You must be signed in to change notification settings - Fork 193
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
fix: support constants with lambda functions #940
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// This constant should be ignored | ||
const foo = [].map(() => {}); | ||
|
||
export class A { | ||
a: string; | ||
} | ||
|
||
export class B { | ||
b: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"definitions": { | ||
"A": { | ||
"type": "object", | ||
"properties": { | ||
"a": { | ||
"type": "string" | ||
} | ||
}, | ||
"required": [ | ||
"a" | ||
], | ||
"additionalProperties": false | ||
}, | ||
"B": { | ||
"type": "object", | ||
"properties": { | ||
"b": { | ||
"type": "string" | ||
} | ||
}, | ||
"required": [ | ||
"b" | ||
], | ||
"additionalProperties": false | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about functions that are not arrow functions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll add a test case and see if it works.
Something like this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, I confirmed that function expressions do not work.
I added
ts.SyntaxKind.FunctionExpression
to the if statement, but I ran into the following error:It looks like the schema generator can handle
SyntaxKind.FunctionDeclaration
, but notSyntaxKind.FunctionExpression
which is an unsupported node type. Here's a AST viewer for the difference.While I don't think it will be difficult to support
FunctionExpression
, I feel like that's outside the scope of this PR. What do you think?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure exactly what the problem is in the first place to be honest. I use functions and arrow functions in Vega-Lite and the parser doesn't seem to have any issues with it. I think we can throw our hands up for functions but we recently added support for adding function parameters to the generated schema so I'm not sure what would happen if we just ignored functions entirely. Please take a closer look at related issues and prs and see what a good behavior is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like there are two ways to declare function types.
Since the second way of declaring functions is currently not supported, my thought is to leave functions out of this
if
statement for this pr and log an issue to add support for the second way to declare functions.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The two ways should be equivalent so I would say either support both or none.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that both should be supported, but should adding support for the second form be in this PR or in another PR?
If it were as easy as adding a condition to the if statement, I would, but adding support for the second form means creating a new node parser.
I could add the new node parser in this PR, but the work would split really nicely into 2 PRs.
Regardless of which route we take, normal functions still work correctly since this test still passes:
test/valid-data/function-parameters-declaration/main.ts
.What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. So are you saying that named functions don't cause the parser to break? If so, then could we make the function parameter parser work with lambda functions as well instead of ignoring them here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at how functions are parsed, it's actually pretty straightforward to add support for function expressions!
I pushed a commit to implement this.