Skip to content

Conversation

BPScott
Copy link
Member

@BPScott BPScott commented Jan 7, 2020

WHY are these changes introduced?

Following up #2579.

WHAT is this pull request doing?

Make passing {foo: undefined} into translations replacements a runtime error, in addition to a TS type error for those fun cases where people power through mistyping things using any (looking at you enzyme's trigger function that isn't type-safe).

Make TextField's normalizedValue be an empty string if you pass anything other than a string into it. Once again this is limited to be a string only by typescript but people using those not-type-safe functions can pass in a number and not be warned. Having normalizedValue be a number mucks up a bunch of things so catch that at runtime too.

Make TextField a little lazier by only working out character count stuff if we're actually about to display it.

How to 🎩

tests pass

@BPScott BPScott added the 🤖Skip Changelog Causes CI to ignore changelog update check. label Jan 7, 2020
@BPScott BPScott requested a review from dleroux January 7, 2020 01:37
@github-actions
Copy link
Contributor

github-actions bot commented Jan 7, 2020

💦 Potential splash zone of changes introduced to src/**/*.tsx in this pull request:

Files modified4
Files potentially affected102

Details

All files potentially affected (total: 102)
📄 UNRELEASED.md (total: 0)

Files potentially affected (total: 0)

🧩 src/components/TextField/TextField.tsx (total: 9)

Files potentially affected (total: 9)

🧩 src/utilities/i18n/I18n.ts (total: 102)

Files potentially affected (total: 102)

🧩 src/utilities/i18n/tests/I18n.test.ts (total: 0)

Files potentially affected (total: 0)


This comment automatically updates as changes are made to this pull request.
Feedback, troubleshooting: open an issue or reach out on Slack in #polaris-tooling.

@BPScott BPScott changed the title Be explict about some i18n oddness around undefined keys Be explict about some i18n oddness around undefined keys, and TextField's value Jan 8, 2020
@BPScott BPScott force-pushed the explict-i18n-oddness branch from f28c066 to e013624 Compare January 8, 2020 01:36
// Use a typeof check here as Typescript mostly protects us from non-stringy
// values but overzealous usage of `any` in consuming apps means people have
// been known to pass a number in, so make it clear that doesn't work.
const normalizedValue = typeof value === 'string' ? value : '';
Copy link
Contributor

Choose a reason for hiding this comment

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

I like this approach.

// for now as JS can handle it ok. We should work out how to be
// stricter, or deliberatly allow undefined as a value
return replacements[replacement] as string;
return replacements[replacement].toString();
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

`No replacement found for key 'string'. The following replacements were passed: 'notString'`,
`Error in translation for key 'key'. No replacement found for key 'string'. The following replacements were passed: '{"notString":"bar"}'`,
);

Copy link
Contributor

Choose a reason for hiding this comment

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

Not 100% necessary but it might be best to split these 2 expect statements into 2 tests?

Copy link
Contributor

@dleroux dleroux left a comment

Choose a reason for hiding this comment

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

Thanks for taking care of this @BPScott

@BPScott BPScott force-pushed the explict-i18n-oddness branch from e013624 to d263c54 Compare January 8, 2020 17:52
Improve the error message, and have the error trigger if you have
`{foo: undefined}`
This ensures that normalizedValue is always a string - previously it
could be a number if you forced it through with `any` and that caused
problems further down the line
@BPScott BPScott force-pushed the explict-i18n-oddness branch from d263c54 to 29e86d1 Compare January 8, 2020 18:01
@BPScott
Copy link
Member Author

BPScott commented Jan 8, 2020

A bit or rebasing to split these changes into isolated commits

@BPScott BPScott merged commit 26148a9 into master Jan 8, 2020
@BPScott BPScott deleted the explict-i18n-oddness branch January 8, 2020 18:23
@LauraAubin LauraAubin temporarily deployed to production January 16, 2020 16:22 Inactive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🤖Skip Changelog Causes CI to ignore changelog update check.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants