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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Pranav-yadav marked this conversation as resolved.
Show resolved Hide resolved
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
Pranav-yadav marked this conversation as resolved.
Show resolved Hide resolved
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