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

Refactor: JS substr() is deprecated, using slice() instead #37136

Closed

Conversation

Pranav-yadav
Copy link
Contributor

@Pranav-yadav Pranav-yadav commented Apr 28, 2023

Summary:

Fixes: #37135

Why slice() and not substring()?

Beacuse I like pizza ;) jk

The reason, that I'm not using the most obvious alternative substring() is;

  • It swaps the args passed, when; startIndex > endIndex —which I think is not a property of good fn
    and also does not reflects the same in it's name.
  • It doesn't support negative args, which I think reduces flexibility to work with it.
  • It could lead to more bugs in most of the cases.

Refecrences:

Changelog:

[GENERAL][FIXED] - Refactor: substr() is deprecated, using slice() instead across RN codebase

Test Plan:

  • yarn lint && yarn flow && yarn test-ci --> should be green

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Apr 28, 2023
@Pranav-yadav Pranav-yadav marked this pull request as draft April 28, 2023 08:00
@Pranav-yadav
Copy link
Contributor Author

Pranav-yadav commented Apr 28, 2023

Note: I haven't applied this change to some generated files e.g. https://github.com/facebook/react-native/blob/30da58733651b9c1e4104c17e37f46ace00b95a7/tools/eslint/rules/sort-imports.js

Seems, it needs to be regenerated at InternalFB side.

@Pranav-yadav Pranav-yadav changed the title Refactor: substr() is deprecated, using slice() instead Refactor: JS substr() is deprecated, using slice() instead Apr 28, 2023
@analysis-bot
Copy link

analysis-bot commented Apr 28, 2023

Platform Engine Arch Size (bytes) Diff
android hermes arm64-v8a 8,627,186 -90
android hermes armeabi-v7a 7,939,443 -86
android hermes x86 9,113,989 -80
android hermes x86_64 8,968,858 -75
android jsc arm64-v8a 9,191,743 -25
android jsc armeabi-v7a 8,381,454 -13
android jsc x86 9,249,909 -14
android jsc x86_64 9,508,409 -21

Base commit: 243a148
Branch: main

@jacdebug
Copy link
Contributor

I know this is a WIP but just let you know that there is API difference between two,

  • substr - Returns a substring based on a starting index and the length of the substring.
  • slice - Returns a portion of the string starting from the specified start index and up to but not including the specified end index.

@Pranav-yadav
Copy link
Contributor Author

Pranav-yadav commented Apr 28, 2023

Yup, also the reason that I'm not using the most obvious alternative substring() is; it swaps the args passed when;
startIndex > endIndex, which I think is not a property of good fn and also does not reflects in it's name and could lead to more bugs in most of the cases also, it doesn't support negative args, which I think reduces flexibility to work with it.

@Pranav-yadav Pranav-yadav force-pushed the using-slice-not-substr branch from a547ba2 to 24e5e32 Compare April 28, 2023 18:39
@Pranav-yadav Pranav-yadav marked this pull request as ready for review April 28, 2023 18:45
@Pranav-yadav
Copy link
Contributor Author

Pranav-yadav commented Apr 28, 2023

Looks like LogBox expects the specific behavior of substr in it's test-suite, can anyone just confirm? Hence, failing the tests I guess.

Nevermind, I overlooked the obvious thing because, the variable names were confusing 🥲

--

I know this is a WIP but just let you know that there is API difference between two,

@jacdebug done, also rebased!

- `substr()` is not part of the core JS since ~2018
- No wonder why no one noticed this :)
- Though its supported by modern browsers, its deprecated
- Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr

Why `slice()` and not `substring()`?
> Beacuse I like pizza ;) jk

The reason, that I'm not using the most obvious alternative substring() is; it swaps the args passed, when;
`startIndex > endIndex`, which I think is not a property of good fn and also does not reflects in it's name,
and could lead to more bugs in most of the cases
also, it doesn't support negative args, which I think reduces flexibility to work with it.

- Ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring#differences_between_substring_and_slice
- `slice`: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice
- `substring`: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring
@Pranav-yadav Pranav-yadav force-pushed the using-slice-not-substr branch from 24e5e32 to eee7a00 Compare April 28, 2023 19:06
@cortinico cortinico requested a review from jacdebug May 2, 2023 09:39
@facebook-github-bot
Copy link
Contributor

@jacdebug has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@Pranav-yadav
Copy link
Contributor Author

Pranav-yadav commented May 2, 2023

There are no tests in place for utility/helper functions. I forgot to modify the second param and still all tests passed in CI, such changes could be easily missed by anyone. #37136 (comment)

For each utility/helper function there should be test in-place. cc: @cortinico / @jacdebug WDYT? Should we work on increasing the test-coverage for utility/helper functions?

If there is no code coverage, testing the console as a whole entity is definitely better than testing each individual simple function. This way, the tests written should remain valid even after refactoring the source code. #37136 (comment)

Yeah, exactly, I was trying to convey that only. Testing them as "whole entity" is the way to go, to avoid individual tests and ensure everything works as intended; even after refactors like this one ;)

I'll try to come back to this, after I finish with my Uni. Exams 😅.
Thanks

@facebook-github-bot facebook-github-bot added the Merged This PR has been merged. label May 2, 2023
@facebook-github-bot
Copy link
Contributor

@jacdebug merged this pull request in 8a49754.

@Pranav-yadav Pranav-yadav deleted the using-slice-not-substr branch May 3, 2023 13:40
jeongshin pushed a commit to jeongshin/react-native that referenced this pull request May 7, 2023
…ook#37136)

Summary:
Fixes: facebook#37135

- `substr()` is not part of the core JS since ~2018
- No wonder why no one noticed this :)
- Though its supported by modern browsers, its deprecated
- Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr

### Why `slice()` and not `substring()`?
> Beacuse I like pizza ;) jk

The reason, that I'm not using the most obvious alternative `substring()` is;
- It _swaps the args_ passed, when; `startIndex > endIndex` —which I think is not a property of _good_ fn
  and also does not reflects the same in it's name.
- It _doesn't support negative args_, which I think reduces flexibility to work with it.
- It could lead to more bugs in most of the cases.

### Refecrences:
- Ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring#differences_between_substring_and_slice
- Ref. for `slice()`: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice
- Ref. for `substring()`: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring

## Changelog:

[GENERAL][FIXED] - Refactor: `substr()` is deprecated, using `slice()` instead across RN codebase

Pull Request resolved: facebook#37136

Test Plan: - `yarn lint && yarn flow && yarn test-ci` --> _should be green_

Reviewed By: christophpurrer

Differential Revision: D45477910

Pulled By: jacdebug

fbshipit-source-id: 96a80893477599b9a549918924157627b9b0c3f4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. JavaScript Merged This PR has been merged.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[SECURITY] substr() is deprecated and is not part of the core JS.
4 participants