Skip to content

Commit

Permalink
Refactor: JS substr() is deprecated, using slice() instead (#37136)
Browse files Browse the repository at this point in the history
Summary:
Fixes: #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: #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
  • Loading branch information
Pranav-yadav authored and facebook-github-bot committed May 2, 2023
1 parent bffb307 commit 8a49754
Show file tree
Hide file tree
Showing 13 changed files with 20 additions and 23 deletions.
2 changes: 1 addition & 1 deletion packages/assets/path-support.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ function getAndroidResourceIdentifier(asset: PackagerAsset): string {

function getBasePath(asset: PackagerAsset): string {
const basePath = asset.httpServerLocation;
return basePath.startsWith('/') ? basePath.substr(1) : basePath;
return basePath.startsWith('/') ? basePath.slice(1) : basePath;
}

module.exports = {
Expand Down
4 changes: 2 additions & 2 deletions packages/hermes-inspector-msggen/src/Converters.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
*/

export function toCppNamespace(domain: string): string {
return domain.substr(0, 1).toLowerCase() + domain.substr(1);
return domain.slice(0, 1).toLowerCase() + domain.slice(1);
}

export function toCppType(type: string): string {
return type.substr(0, 1).toUpperCase() + type.substr(1);
return type.slice(0, 1).toUpperCase() + type.slice(1);
}

export type JsTypeString =
Expand Down
4 changes: 2 additions & 2 deletions packages/hermes-inspector-msggen/src/Property.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ function toDomainAndId(
domain = curDomain;
id = absOrRelRef;
} else {
domain = absOrRelRef.substr(0, i);
id = absOrRelRef.substr(i + 1);
domain = absOrRelRef.slice(0, i);
id = absOrRelRef.slice(i + 1);
}

return [domain, id];
Expand Down
4 changes: 2 additions & 2 deletions packages/polyfills/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ const inspect = (function () {
return ' ' + line;
})
.join('\n')
.substr(2);
.slice(2);
} else {
str =
'\n' +
Expand All @@ -274,7 +274,7 @@ const inspect = (function () {
}
name = JSON.stringify('' + key);
if (name.match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)) {
name = name.substr(1, name.length - 2);
name = name.slice(1, name.length - 1);
name = ctx.stylize(name, 'name');
} else {
name = name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,6 @@ export default class ReadOnlyCharacterData extends ReadOnlyNode {
);
}
let adjustedCount = count < 0 || count > data.length ? data.length : count;
return data.substr(offset, adjustedCount);
return data.slice(offset, offset + adjustedCount);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function getStringByValue(value: any): string {
}
if (typeof value === 'string' && value.length > 500) {
return String(value)
.substr(0, 500)
.slice(0, 500)
.concat('\n***TRUNCATED TO 500 CHARACTERS***');
}
return value;
Expand Down
2 changes: 1 addition & 1 deletion packages/react-native/Libraries/LogBox/UI/AnsiHighlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export default function Ansi({
return content.replace(/\| $/, ' ');
} else if (key === 2 && commonWhitespaceLength < Infinity) {
// Remove common whitespace at the beginning of the line
return content.substr(commonWhitespaceLength);
return content.slice(commonWhitespaceLength);
} else {
return content;
}
Expand Down
11 changes: 4 additions & 7 deletions packages/react-native/Libraries/LogBox/UI/LogBoxMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,25 +138,22 @@ function LogBoxMessage(props: Props): React.Node {
const key = String(index);

if (substitution.offset > prevOffset) {
const prevPart = content.substr(
prevOffset,
substitution.offset - prevOffset,
);
const prevPart = content.slice(prevOffset, substitution.offset);

createUnderLength(key, prevPart);
}

const substitutionPart = content.substr(
const substitutionPart = content.slice(
substitution.offset,
substitution.length,
substitution.offset + substitution.length,
);

createUnderLength(key + '.5', substitutionPart, substitutionStyle);
return substitution.offset + substitution.length;
}, 0);

if (lastOffset < content.length) {
const lastPart = content.substr(lastOffset);
const lastPart = content.slice(lastOffset);
createUnderLength('-1', lastPart);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function polyfillObjectProperty<T>(
): void {
const descriptor = Object.getOwnPropertyDescriptor<$FlowFixMe>(object, name);
if (__DEV__ && descriptor) {
const backupName = `original${name[0].toUpperCase()}${name.substr(1)}`;
const backupName = `original${name[0].toUpperCase()}${name.slice(1)}`;
Object.defineProperty(object, backupName, descriptor);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class TokenizedTextExample extends React.Component {
if (token[0].length === 0) {
index = 1;
}
parts.push(_text.substr(0, index));
parts.push(_text.slice(0, index));
parts.push(token[0]);
index = index + token[0].length;
_text = _text.slice(index);
Expand Down
2 changes: 1 addition & 1 deletion packages/rn-tester/js/components/ListExampleShared.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function genItemData(i: number): Item {
const itemHash = Math.abs(hashCode('Item ' + i));
return {
title: 'Item ' + i,
text: LOREM_IPSUM.substr(0, (itemHash % 301) + 20),
text: LOREM_IPSUM.slice(0, (itemHash % 301) + 20),
key: String(i),
pressed: false,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ class TokenizedTextExample extends React.Component<
if (token[0].length === 0) {
index = 1;
}
parts.push(_text.substr(0, index));
parts.push(_text.slice(0, index));
parts.push(token[0]);
index = index + token[0].length;
_text = _text.slice(index);
Expand Down
4 changes: 2 additions & 2 deletions tools/eslint/rules/valid-flow-typed-signature.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ module.exports = {
const sourceText = context.getSourceCode().getText();

const firstLineEndIndex = sourceText.indexOf('\n');
const firstLine = sourceText.substr(0, firstLineEndIndex);
const firstLine = sourceText.slice(0, firstLineEndIndex);

const match = firstLine.match(HASH_COMMENT_RE);
if (match == null) {
Expand All @@ -43,7 +43,7 @@ module.exports = {
}

const hash = match[1];
const versionedCode = sourceText.substr(firstLineEndIndex + 1);
const versionedCode = sourceText.slice(firstLineEndIndex + 1);
if (md5(versionedCode) === hash) {
return;
}
Expand Down

0 comments on commit 8a49754

Please sign in to comment.