Skip to content

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

Closed
Pranav-yadav wants to merge 1 commit into
react:mainfrom
Pranav-yadav:using-slice-not-substr
Closed

Refactor: JS substr() is deprecated, using slice() instead#37136
Pranav-yadav wants to merge 1 commit into
react:mainfrom
Pranav-yadav:using-slice-not-substr

Conversation

@Pranav-yadav

@Pranav-yadavPranav-yadav commented Apr 28, 2023

Copy link
Copy Markdown
Contributor

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-botfacebook-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
Comment threadpackages/react-native/Libraries/LogBox/UI/LogBoxMessage.js Outdated
@Pranav-yadav

Pranav-yadav commented Apr 28, 2023

Copy link
Copy Markdown
ContributorAuthor

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-yadavPranav-yadav changed the title Refactor: substr() is deprecated, using slice() insteadRefactor: JS substr() is deprecated, using slice() insteadApr 28, 2023
@analysis-bot

analysis-bot commented Apr 28, 2023

Copy link
Copy Markdown
PlatformEngineArchSize (bytes)Diff
androidhermesarm64-v8a8,627,186-90
androidhermesarmeabi-v7a7,939,443-86
androidhermesx869,113,989-80
androidhermesx86_648,968,858-75
androidjscarm64-v8a9,191,743-25
androidjscarmeabi-v7a8,381,454-13
androidjscx869,249,909-14
androidjscx86_649,508,409-21

Base commit: 243a148
Branch: main

@jacdebug

Copy link
Copy Markdown
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

Pranav-yadav commented Apr 28, 2023

Copy link
Copy Markdown
ContributorAuthor

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-yadavforce-pushed the using-slice-not-substr branch from a547ba2 to 24e5e32CompareApril 28, 2023 18:39
@Pranav-yadav
Pranav-yadav marked this pull request as ready for review April 28, 2023 18:45
@Pranav-yadav

Pranav-yadav commented Apr 28, 2023

Copy link
Copy Markdown
ContributorAuthor

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-yadavforce-pushed the using-slice-not-substr branch from 24e5e32 to eee7a00CompareApril 28, 2023 19:06
Comment threadpackages/polyfills/console.js
@cortinico
cortinico requested a review from jacdebugMay 2, 2023 09:39
@facebook-github-bot

Copy link
Copy Markdown
Contributor

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

@Pranav-yadav

Pranav-yadav commented May 2, 2023

Copy link
Copy Markdown
ContributorAuthor

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-botfacebook-github-bot added the Merged This PR has been merged. label May 2, 2023
@facebook-github-bot

Copy link
Copy Markdown
Contributor

@jacdebug merged this pull request in 8a49754.

@Pranav-yadav
Pranav-yadav deleted the using-slice-not-substr branch May 3, 2023 13:40
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA SignedThis label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.JavaScriptMergedThis 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

@Pranav-yadav@analysis-bot@jacdebug@facebook-github-bot