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

Support parsing methods, output for Flow #18

Merged
merged 17 commits into from
May 8, 2018
Merged

Conversation

valscion
Copy link
Collaborator

@valscion valscion commented May 5, 2018

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.

@valscion
Copy link
Collaborator Author

valscion commented May 5, 2018

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?

@valscion
Copy link
Collaborator Author

valscion commented May 5, 2018

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.

@valscion valscion changed the title Support parsing methods, this time for real Support parsing methods, output for Flow May 5, 2018
@valscion
Copy link
Collaborator Author

valscion commented May 5, 2018

I think this PR is ready for reviewing now ☺️

@sergeysova
Copy link
Owner

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', {
Copy link
Owner

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)?

Copy link
Collaborator Author

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?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK)))

@valscion
Copy link
Collaborator Author

valscion commented May 7, 2018

eslint for flowtypes

I think prettier would be a better formatting option for flowtypes. Would it be OK to you if I'd setup prettier for this repository? Or if not the entire repository, maybe only for the flow type declarations?

@sergeysova
Copy link
Owner

sergeysova commented May 7, 2018

For all my repositories I use eslint with @atomix/eslint

For flow we can use https://github.com/gajus/eslint-plugin-flowtype

@valscion
Copy link
Collaborator Author

valscion commented May 7, 2018

Yeah sure ☺️

I think formatting flow and typescript is out of scope of this PR, though

@Piterden
Copy link
Collaborator

Piterden commented May 7, 2018

Formatting is always paramount!!!

I mean, working code is useful for certain tasks, but the formatted working code is also useful for learning.

@valscion
Copy link
Collaborator Author

valscion commented May 7, 2018

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') {
Copy link
Collaborator

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)) {
Copy link
Collaborator

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*/ = {}) {
Copy link
Collaborator

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...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this repository had prettier installed, we wouldn't have to care 😉. The Union and Interface constructors also have these kind of long lines and the linter does not complain, so I'd like to keep it like this. It looks similar to what the other constructors look like:

screen shot 2018-05-07 at 16 06 39

Copy link
Collaborator

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) {
Copy link
Collaborator

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...

@valscion
Copy link
Collaborator Author

valscion commented May 7, 2018

Is this good to go?

@sergeysova
Copy link
Owner

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 Returns object to *Response type:

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

@sergeysova
Copy link
Owner

Please, do not make any changes to this branch, just think about my proposal.

@sergeysova
Copy link
Owner

I want to refactor builders, from classes to functions

@valscion
Copy link
Collaborator Author

valscion commented May 8, 2018

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 ☺

@sergeysova sergeysova merged commit a056f9c into master May 8, 2018
@valscion valscion deleted the parse-methods-try-2 branch May 10, 2018 16:00
@valscion valscion mentioned this pull request May 10, 2018
valscion added a commit that referenced this pull request May 10, 2018
I accidentally caused a bug in f42d873
when doing #18 which
caused type generation to be broken
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants