Skip to content

Conversation

@seanf
Copy link
Collaborator

@seanf seanf commented Jan 16, 2018

No description provided.

@seanf seanf requested a review from BANG88 January 16, 2018 14:09
@BANG88
Copy link
Owner

BANG88 commented Jan 16, 2018

Too many modifications. Its difficulty to understand what do you want from this PR or solve some problems?

@baktun14
Copy link
Contributor

Where are the descriptions going in the exported json file?

@baktun14
Copy link
Contributor

I have a question for you @seanf , as you seem to have a better understanding of the typescript reflection api.

I currently have a small issue with formatMessage, here's the example of what actually works:

const message = formatMessage({id: "test", defaultMessage: "test message"});

and what does not work:

let message;
if (condition) {
  message = formatMessage({id: "test_1", defaultMessage: "test message 1"});
} else {
  message = formatMessage({id: "test_2", defaultMessage: "test message 2"});
}

How do you debug this library's code? Do you simply use console.log to know what is what or do you have another technique?

Thank you in advance!

I'm still unsure if it would be a good idea to parse every formatMessage function calls because sometimes I use it with non static ids like this;

formatMessage({id: object.id, defaultMessage: object.defaultMessage})

Anyways let me know what you guys think!

@seanf
Copy link
Collaborator Author

seanf commented Jan 16, 2018 via email

@seanf
Copy link
Collaborator Author

seanf commented Jan 16, 2018 via email

@baktun14
Copy link
Contributor

@seanf Ah I didn't see the TODO. Yea I'm using defineMessages for now, and it wouldn't be such a bad thing to only include the ones assigned to a variable, like it is right now.

@seanf
Copy link
Collaborator Author

seanf commented Jan 16, 2018

@mbeauchamp7

How do you debug this library's code? Do you simply use console.log to know what is what or do you have another technique?

Mostly https://github.com/dsherret/ts-ast-viewer

I also found this article useful: http://blog.scottlogic.com/2017/05/02/typescript-compiler-api-revisited.html

@seanf
Copy link
Collaborator Author

seanf commented Jan 17, 2018

@seanf Ah I didn't see the TODO. Yea I'm using defineMessages for now, and it wouldn't be such a bad thing to only include the ones assigned to a variable, like it is right now.

We only pick up function calls which pass in an object declaration, where the keys and values are statically known (identifiers and literals). I think babel-plugin-react-intl goes one better and can pick up things like concatenated string constants&literals: https://github.com/yahoo/babel-plugin-react-intl/blob/6f4a533921c8632f53218c00cc3be3042814eed8/src/index.js#L31-L40

Either way, this would skip dynamic examples like this:

{id: object.id, defaultMessage: object.defaultMessage}

@BANG88 @mbeauchamp7
I think it might be better to follow the lead of babel-plugin-react-intl (making it easier to switch between them) and thus:

  • ignore formatMessage - I don't think it's intended to define messages as a side effect, just format them. But we could make this an option.
  • not require a variable declaration.
  • It might help performance/accuracy to skip files which don't import react-intl too. This module name should be configurable.

It may not be easy to replicate another one of babel-plugin-react-intl's features - I think it might be able to handle renamed imports, eg

import {defineMessages as defMsgs} from 'react-intl';

So it might be good to list that as a known limitation (along with the concatenated constants thing, which I already ran into).

@BANG88
Copy link
Owner

BANG88 commented Jan 17, 2018

@seanf @mbeauchamp7 This library may not covered every use case. at first my point is make it work so PR are always welcome.

If we want export the message interface I think should be something like this:

export interface Message {
  id: string
  defaultMessage: string 
  description?: string 
}

@baktun14
Copy link
Contributor

@seanf @BANG88 To be honest, I'm fine with how the library is actually behaving. Ignoring files that don't import react-intl would definitely help performance, but I have a pretty big project and it's actually pretty fast.

I would like to keep the parsing of formatMessage as is, since it's pretty much the same as <FormatMessage /> and yea not require a variable **declaration.** would be cool but not necessary. I rarely or never rename imports, so up to you!

@seanf
Copy link
Collaborator Author

seanf commented Jan 17, 2018 via email

@seanf seanf changed the title Export Message interface; check nulls wip: Export Message interface; check nulls Jan 17, 2018
@seanf seanf mentioned this pull request Jan 17, 2018
@seanf
Copy link
Collaborator Author

seanf commented Jan 17, 2018

Assuming #17 goes in, I'll rebase this on top, then the diff should just show Message-related changes.

@seanf seanf force-pushed the better-message-interface branch from e462d9d to b2e7645 Compare January 17, 2018 12:56
@seanf
Copy link
Collaborator Author

seanf commented Jan 17, 2018

I have another commit which cleans up the JSX types (no more any!), but I'll put it in another PR later.

seanf@e462d9d -> seanf@b03e14b

@seanf seanf changed the title wip: Export Message interface; check nulls Strongly type Message and use in main signature Jan 19, 2018
index.ts Outdated
interface Message {
[key: string]: string;
// a Message with nullable fields, still under construction
interface PartialMessage {
Copy link
Owner

Choose a reason for hiding this comment

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

This could be done via Partial

index.ts Outdated
// just a map of string to string
interface Message {
[key: string]: string;
// a Message with nullable fields, still under construction
Copy link
Owner

@BANG88 BANG88 Jan 19, 2018

Choose a reason for hiding this comment

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

This could be done via Partial<Message>

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, I just discovered that, thanks. I need to make things a bit more idiomatic.

index.ts Outdated
}

// are the required keys of a valid Message present?
function isValidMessage(obj: PartialMessage): obj is Message {
Copy link
Owner

Choose a reason for hiding this comment

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

difficulty to understand why need obj is Message can be inferred by TS

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think it's because the copying happens dynamically. There's no static way of knowing whether those property assignments will occur.

Copy link
Owner

Choose a reason for hiding this comment

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

No. from this method your result must be a boolean type. am I right ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is another of those user-defined type guards.

return jsxMessages.concat(dm).concat(fm);
}

function notNull<T>(value: T | null): value is T {
Copy link
Owner

Choose a reason for hiding this comment

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

Does this return a boolean value. if so why need value is T

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It does return a boolean at runtime, but this bit of typing enables the result of filter to be Message instead of Message | null as far as the type checker is concerned. It's a user-defined type guard: https://www.typescriptlang.org/docs/handbook/advanced-types.html

Copy link
Owner

Choose a reason for hiding this comment

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

@seanf Thanks. I didn't use this type guard before. 😄

@BANG88
Copy link
Owner

BANG88 commented Jan 19, 2018

could you rebase it so we can merge this PR.

@seanf
Copy link
Collaborator Author

seanf commented Jan 19, 2018

could you rebase it so we can merge this PR.

When you say rebase, do you mean I should rebase the 7 commits on top of master? They're kind of a mess, so I would advise "squash and merge" instead. But if you want to rebase then merge, GitHub usually lets you do that (hit the green drop-down and select "rebase and merge") - it leaves the original commits as they are in the PR for historical purposes.

Or did you want me to squash it into one commit, and force push to this branch before you merge? I'm happy to do that if that's what you mean, but again, GitHub can do that automatically if you "squash and merge" (again it leaves the original commits in the PR). (Whereas if I force push a squashed merge, the history of this code review pretty much goes away.)

Either way, let me know what you want. Unless you just want to "squash and merge", in which case go ahead!

@BANG88
Copy link
Owner

BANG88 commented Jan 19, 2018

Or did you want me to squash it into one commit, and force push to this branch before you merge? I'm happy to do that if that's what you mean,
Yep. I will use Github's button for merge. sometimes I prefer review one commit so.

@BANG88 BANG88 merged commit a3ee268 into BANG88:master Jan 19, 2018
@seanf seanf deleted the better-message-interface branch January 19, 2018 08:36
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.

3 participants