-
-
Notifications
You must be signed in to change notification settings - Fork 9
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
Support parsing methods, output for Flow #18
Conversation
It's easier to later only tweak the builder methods if they've worked like interfaces have before.
My knowledge on how these should be done is limited
Supporting Rust for union argument types will require some work, which I am not able to do now.
Currently this PR outputs methods as functions for Flow and as interfaces for TypeScript. I had to skip Rust types as I wasn't able to figure out how the union argument types should be done. This skipping makes this PR safe to merge (nothing should be broken). Do you think it would be possible to later do TypeScript formatting with function types? We could also skip outputting methods for TypeScript in this PR so that nothing strange would get in the output, if that is better? |
As it is so easy to skip TypeScript in this PR for now, I'm going to do it. If at all possible, I'd want to avoid having to tackle it while I also tackled all the other stuff I had to do to get this to work. |
I think this PR is ready for reviewing now |
To format typescript we can use tslint, and eslint for flowtypes. |
@@ -45,6 +49,36 @@ function addBuiltins(store) { | |||
'InputVenueMessageContent', | |||
'InputContactMessageContent', | |||
])) | |||
store.add(new Union('InputMedia', { |
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 automaticall add unions to store (after parsing doc page)?
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.
There wasn't many unions on the page currently, so I figured it would be quicker to get started by defining the remaining two unions. I wanted to get to the feature that provides most value quickly, which is outputting types for methods.
It is definitely possible to automatically add unions, and get rid of these three store.add(new Union(...
lines. However, that could be done outside of this pull request. Would you be OK with that?
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.
OK)))
I think |
For all my repositories I use eslint with For flow we can use https://github.com/gajus/eslint-plugin-flowtype |
Yeah sure I think formatting flow and typescript is out of scope of this PR, though |
Formatting is always paramount!!! I mean, working code is useful for certain tasks, but the formatted working code is also useful for learning. |
Is there something specific in the output that would require formatting in your opinion? Or is there something in my code that would need to be reformatted? I am confused. |
lib/parser.js
Outdated
const fieldName = flName.text().trim() | ||
let fieldType = flType.text().trim() | ||
|
||
if (fieldName === 'Field' || fieldName === 'Parameters') { |
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'd use includes
here...
lib/parser.js
Outdated
fieldType = fieldType.split(' or ') | ||
} | ||
|
||
if (Array.isArray(fieldType)) { |
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.
debug(`${name.text().trim()}.${fieldName}: ${Array.isArray(fieldType) ? fieldType.join(' | ') : fieldType} ::: ${fieldDescription}`)
class Method extends Type { | ||
/*:: fields: FieldMap*/ | ||
|
||
constructor(name/*: string*/, attributes/*: Attributes*/, fields/*: FieldMap*/ = {}) { |
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'd write new lines 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.
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.
Ya, I know ))) Just I like the old-school 80 lettered rule.
@@ -113,7 +125,7 @@ class Store { | |||
serialize() { | |||
return [...this.types.values()] | |||
.reduce((acc, type) => { | |||
if (type instanceof Interface) { | |||
if (type instanceof Interface || type instanceof Method) { | |||
acc.types[type.name] = type | |||
} | |||
else if (type instanceof Union) { |
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 think else
is unnecessary here, but it is not this PR...
5864fc5
to
a713d93
Compare
a713d93
to
f42d873
Compare
Is this good to go? |
I think current flow declaration for methods is not usable. Ex.: import { sendChatAction } from 'telegram-typings'
// But I can't use it, because our library doesn't have implementation for function I propose that API (concept): import type { SendChatActionPayload, SendChatActionResponse } from 'telegram-typings'
// and declare implementation
function sendChatAction(action: SendChatActionPayload): Promise<SendChatActionResponse> {
// Send request
} For example: https://core.telegram.org/bots/api#getuserprofilephotos Parse parameters to *Payload type: type GetUserProfilePhotosPayload = {
user_id: number,
offset?: number,
limit?: number,
} Parse type GetUserProfilePhotosResponse = UserProfilePhotos
// In some cases just replace *Response with target type In that case telegram-typings library user can implement its functions and methods |
Please, do not make any changes to this branch, just think about my proposal. |
I want to refactor builders, from classes to functions |
Nice, that would make a lot of sense! Would you be OK if we could get this in with the output stubbed out, i.e. the flow output would be similar to typescript? This way we could get the code responsible for parsing functions in to the master branch and could iterate on proper output format in smaller pull requests ☺ |
This PR replaces #17. I re-wrote the method support after applying what I learned from there.
This PR adds methods to the generated Flow types while leaving a nifty slot to apply support for TypeScript and Rust.