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

πŸš€ feat(tokens|common): add typescript #669

Merged
merged 26 commits into from
Jul 26, 2023

Conversation

matheushdsbr
Copy link
Collaborator

JIRA Issue

Description πŸ§˜πŸ»β€β™€οΈ

In this PR was added Typescript in tokens and common packages.

Platforms πŸ“²

  • Web
  • Mobile

Type of change πŸ”

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested? πŸ§ͺ

[Enter the tips to test this PR]

  • Unit Test
  • Snapshot Test

Checklist: πŸ”

  • My code follows the contribution guide of this project Contributing Guide
  • Layout matches design prototype: FIGMA
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules
  • I have checked my code and corrected any misspellings

Video πŸ“Έ

Screen.Recording.2023-07-12.at.17.20.15.mov

@matheushdsbr matheushdsbr force-pushed the feat/add-typescript-to-tokens-and-common branch from eedab9b to 0bdaff2 Compare July 13, 2023 13:36
Comment on lines 70 to 74
if (level) {
return all[level];
}

return all;
Copy link
Contributor

Choose a reason for hiding this comment

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

The previous way seemed more clear, i would only using nullish coalescing operator. WDYT?

Suggested change
if (level) {
return all[level];
}
return all;
all[level] ?? all;

Copy link
Contributor

Choose a reason for hiding this comment

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

Hey, @MicaelRodrigues!
I don't think the previous way is more clear. Someone new to JS may have difficulty to understand all[level] ?? all. Contrary to what you said, I believe that this new implementation is easier to read and to understand the code.

Don't you think it's easier to read the statements if we were reading like it was an English text, instead of a code full of JS patterns and operators?

Copy link
Contributor

Choose a reason for hiding this comment

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

honestly, I understand both points but there's two things that seem more important to me here:

  • what if I give it a higher level? Right now, as we accept any number I could give it a 500 and would get an undefined. Should we create the array based on the given level?
  • this function signature is, IMO, weird. It can either return an array or a value. I'm not the biggest fan of doing this as it makes the usage a lot more complex. Maybe we should type the return differently based on the fact that a level is given or not using function overloads

Copy link
Contributor

@MicaelRodrigues MicaelRodrigues Jul 17, 2023

Choose a reason for hiding this comment

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

Hey, @MicaelRodrigues! I don't think the previous way is more clear. Someone new to JS may have difficulty to understand all[level] ?? all. Contrary to what you said, I believe that this new implementation is easier to read and to understand the code.

Don't you think it's easier to read the statements if we were reading like it was an English text, instead of a code full of JS patterns and operators?

  • could give it a 500 and would get an undefined. Should we create the arr

It's a question of flavour :) Both options are fine to me :) We expect people working in this code to know javascript fundamentals. If they don't we are here to help improve the code... :)
I agree with you in "as it was an English text" and both options read plain english to me (ex: my suggestion reads ("a" or "b").

Anyway, the important concerns in this function are the ones @goncalo-matos exposed ;)

Copy link
Contributor

Choose a reason for hiding this comment

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

Can we use the value "0" as level? If so the current implementation will ignore the level when it is "0".

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 @leonardokl πŸ˜•
Sorry about this guys!

@goncalo-matos good point πŸš€

packages/tokens/src/global/borders.ts Outdated Show resolved Hide resolved
packages/tokens/src/global/breakpoints.ts Outdated Show resolved Hide resolved
@@ -0,0 +1,27 @@
export interface FontSizesProps extends Array<number> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as above

Copy link
Contributor

Choose a reason for hiding this comment

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

Same as above, you don't need extends

packages/tokens/src/global/font-sizes.ts Outdated Show resolved Hide resolved
packages/tokens/src/global/font-weights.ts Outdated Show resolved Hide resolved
packages/tokens/src/global/line-heights.ts Outdated Show resolved Hide resolved
@matheushdsbr matheushdsbr marked this pull request as ready for review July 13, 2023 20:02
Comment on lines 70 to 74
if (level) {
return all[level];
}

return all;
Copy link
Contributor

Choose a reason for hiding this comment

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

Hey, @MicaelRodrigues!
I don't think the previous way is more clear. Someone new to JS may have difficulty to understand all[level] ?? all. Contrary to what you said, I believe that this new implementation is easier to read and to understand the code.

Don't you think it's easier to read the statements if we were reading like it was an English text, instead of a code full of JS patterns and operators?

margin: 71,
gutter: 24,
},
};
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
};
} as const;

So we can make the IntelliSense and the Editor/autocomplete smarter


/**
* @type {Color}
*/
const white = '#FFFFFF';

const colors = {
Copy link
Contributor

Choose a reason for hiding this comment

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

export default colors = {
  vibin,
  hope,
  energy,
  relax,
  peace,
  brandingVerve,
  verve,
  uplift,
  deepPurple,
  yoga,
  success,
  neutral,
  attention,
  stamina,
  deep,
  medium,
  light,
  clear,
  white,
} as const;

Copy link
Contributor

Choose a reason for hiding this comment

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

For the future, not for now, we should probably avoid exporting default. we're polluting the bundle with all of the colors if we only use one

(named exports scale a lot better than default exports :P)

packages/tokens/src/global/elevations.android.ts Outdated Show resolved Hide resolved
packages/tokens/src/global/font-sizes.ts Outdated Show resolved Hide resolved
packages/tokens/src/global/radii.ts Show resolved Hide resolved
packages/tokens/src/global/spacing.ts Show resolved Hide resolved
"build": "yarn build:cjs && yarn build:esm",
"build:cjs": "NODE_ENV=cjs babel ./src --out-dir dist/cjs",
"build:esm": "NODE_ENV=esm babel ./src --out-dir dist/esm",
"build": "yarn build:types && yarn build:cjs && yarn build:esm",
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion: wdyt of parallelizing this? Something like https://www.npmjs.com/package/npm-run-all can be helpfull

function elevate({
interface ElevateProps {
color?: string;
level?: number;
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion: since this only supports up to 3, shouldn't our types reflect this?

quick and dirty way would be to do this (we could build a Range util later):

Suggested change
level?: number;
level?: 0 | 1 | 2 | 3

Comment on lines 70 to 74
if (level) {
return all[level];
}

return all;
Copy link
Contributor

Choose a reason for hiding this comment

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

honestly, I understand both points but there's two things that seem more important to me here:

  • what if I give it a higher level? Right now, as we accept any number I could give it a 500 and would get an undefined. Should we create the array based on the given level?
  • this function signature is, IMO, weird. It can either return an array or a value. I'm not the biggest fan of doing this as it makes the usage a lot more complex. Maybe we should type the return differently based on the fact that a level is given or not using function overloads


const border: BorderProps = [0, 1, 2];

[border.zero, border.small, border.medium] = border;
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion: I think we can remove this line. I'm not really following what it's doing πŸ€”

@@ -0,0 +1,101 @@
export interface BreakpointsProps {
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion: these aren't really props. How about just Breakpoints?

Suggested change
export interface BreakpointsProps {
export interface Breakpoints {


/**
* @type {Color}
*/
const white = '#FFFFFF';

const colors = {
Copy link
Contributor

Choose a reason for hiding this comment

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

For the future, not for now, we should probably avoid exporting default. we're polluting the bundle with all of the colors if we only use one

(named exports scale a lot better than default exports :P)

};

const [zero, small, medium, large] =
elevate(elevateOptions) as [string, string, string, string];
Copy link
Contributor

Choose a reason for hiding this comment

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

question: as casts hide potential errors (it's us saying we know better than typescript). Do we really need this?

@@ -0,0 +1,27 @@
{
"compilerOptions": {
"target": "es2016",
Copy link
Contributor

Choose a reason for hiding this comment

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

question: could we make this a bit more recent? :P

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

For sure!

But I think it's better to do this update at another time, as there would be a lot to validate in this PR

Copy link
Contributor

@MicaelRodrigues MicaelRodrigues Jul 19, 2023

Choose a reason for hiding this comment

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

For sure!

But I think it's better to do this update at another time, as there would be a lot to validate in this PR

I don't think there would be much to validate in this PR, did you test the change locally? If indeed causes much trouble we can do in another PR

Copy link
Contributor

Choose a reason for hiding this comment

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

@matheushdsbr Pls don't forget to open a new PR with this :)

"forceConsistentCasingInFileNames": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"noImplicitAny": false,
Copy link
Contributor

Choose a reason for hiding this comment

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

one day, I'd love to have this as true :D

@matheushdsbr
Copy link
Collaborator Author

@MicaelRodrigues @goncalo-matos @leonardokl @caiotracera

Hey guys!

When changing the structure of the tokens by removing the arrays, this test broke: /packages/yoga/src/Theme/helpers/themeReader/base.test.js on line 44.

Investigating the theme (being imported on line 3) I realized that we have the createFakeObj() function and if we change the structure of the export with the arrays it will not work well and will generate breakchanges.

I believe that it would be better, at least for now, to continue with the old structure (with arrays) and in the future to investigate solutions to remove this structure and update with suggestions that you commented here.

I believe I wasn't very clear, I still don't quite understand how this function works.

I would like your opinion on the case!

@MicaelRodrigues
Copy link
Contributor

@MicaelRodrigues @goncalo-matos @leonardokl @caiotracera

Hey guys!

When changing the structure of the tokens by removing the arrays, this test broke: /packages/yoga/src/Theme/helpers/themeReader/base.test.js on line 44.

Investigating the theme (being imported on line 3) I realized that we have the createFakeObj() function and if we change the structure of the export with the arrays it will not work well and will generate breakchanges.

I believe that it would be better, at least for now, to continue with the old structure (with arrays) and in the future to investigate solutions to remove this structure and update with suggestions that you commented here.

I believe I wasn't very clear, I still don't quite understand how this function works.

I would like your opinion on the case!

packages/tokens/src/global/font-sizes.ts

I see, we need to keep "array" as the return in order to prevent a breaking change.
So, WDYT of using Object.values? Example:


const fontSizes = {
   xxsmall: 10,
   xsmall: 20
}

export default Object.values(fontSizes);

@matheushdsbr
Copy link
Collaborator Author

Great suggestion @MicaelRodrigues!! πŸš€πŸš€

But when testing I found a problem, the old structure [with array] allows us to use tokens in two ways:

tokens.fontSizes[0] or tokens.fontSizes.xxsmall

All consoles were made in this test file:

  packages/yoga/src/Theme/helpers/themeReader/base.test.js

  console.log(theme.fontSizes);

Consoling the old structure with arrays:

[Function: ProxyPolyfill] {
  '0': [Getter/Setter],
  '1': [Getter/Setter],
  '2': [Getter/Setter],
  xxsmall: [Getter/Setter],
  xsmall: [Getter/Setter],
  small: [Getter/Setter],
}

With your suggestion we can use just like:

tokens.fontSizes.xxsmall

Code

const sizes = {
  xxsmall: 10,
  xsmall: 20,
  small: 30
}

const fontSizes = Object.values(sizes);

Result

[Function: ProxyPolyfill] {
  '0': [Getter/Setter],
  '1': [Getter/Setter],
  '2': [Getter/Setter],
}

But!!! Your suggestion made me think better and I thought of this way:

const sizes = {
  xxsmall: 10,
  xsmall: 20,
  small: 30
}

const fontSizes = {...Object.values(sizes), ...sizes};

Result:

[Function: ProxyPolyfill] {
  '0': [Getter/Setter],
  '1': [Getter/Setter],
  '2': [Getter/Setter],
  xxsmall: [Getter/Setter],
  xsmall: [Getter/Setter],
  small: [Getter/Setter],
}

With that way we can continue using the same tokens as in the array structure:

tokens.fontSizes[0] or tokens.fontSizes.xxsmall

\o/

@@ -0,0 +1,27 @@
{
"compilerOptions": {
"target": "es2016",
Copy link
Contributor

Choose a reason for hiding this comment

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

@matheushdsbr Pls don't forget to open a new PR with this :)

@matheushdsbr matheushdsbr merged commit 8aa8478 into master Jul 26, 2023
3 checks passed
@matheushdsbr matheushdsbr deleted the feat/add-typescript-to-tokens-and-common branch July 26, 2023 12:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants