]> cat aescling's git repositories - mastodon.git/commitdiff
Replace deprecated String.prototype.substr() (#17949)
authorCommanderRoot <CommanderRoot@users.noreply.github.com>
Mon, 4 Apr 2022 16:19:45 +0000 (18:19 +0200)
committerGitHub <noreply@github.com>
Mon, 4 Apr 2022 16:19:45 +0000 (12:19 -0400)
* Replace deprecated String.prototype.substr()

.substr() is deprecated so we replace it with .slice() which works similarily but isn't deprecated

* Change String.prototype.substring() to String.prototype.slice()

.substring() and .slice() work very similary but .slice() is a bit faster and stricter

* Add ESLint rule to forbid usage of .substr and .substring

.substr() is deprecated and .substring() is very similar to .slice() so better to use .slice() at all times

Signed-off-by: Tobias Speicher <rootcommander@gmail.com>
.eslintrc.js
app/javascript/mastodon/features/emoji/emoji_mart_search_light.js
app/javascript/mastodon/features/status/components/card.js
app/javascript/mastodon/features/video/index.js

index 7dda01108218e56322e4369b7154113a2e8f6274..2a882f59c6ca7c716114f2c45e898a2f4e083ac3 100644 (file)
@@ -79,6 +79,11 @@ module.exports = {
     'no-irregular-whitespace': 'error',
     'no-mixed-spaces-and-tabs': 'warn',
     'no-nested-ternary': 'warn',
+    'no-restricted-properties': [
+      'error',
+      { property: 'substring', message: 'Use .slice instead of .substring.' },
+      { property: 'substr', message: 'Use .slice instead of .substr.' },
+    ],
     'no-trailing-spaces': 'warn',
     'no-undef': 'error',
     'no-unreachable': 'error',
index e4519a13e62266127c91e848e7d0c1fb90c83e4d..70694ab6dde81dc04b5a774f1ef0de197bc9f059 100644 (file)
@@ -124,7 +124,7 @@ function search(value, { emojisToShowFilter, maxResults, include, exclude, custo
           for (let id in aPool) {
             let emoji = aPool[id],
               { search } = emoji,
-              sub = value.substr(0, length),
+              sub = value.slice(0, length),
               subIndex = search.indexOf(sub);
 
             if (subIndex !== -1) {
index 90f9ae7ae63d0cc435e8719268c3b57398b1f50d..3d81bcb29b626d66303b3098295926041ae953b6 100644 (file)
@@ -32,7 +32,7 @@ const trim = (text, len) => {
     return text;
   }
 
-  return text.substring(0, cut) + (text.length > len ? '…' : '');
+  return text.slice(0, cut) + (text.length > len ? '…' : '');
 };
 
 const domParser = new DOMParser();
index 8d47e479a84b557dd407006b7da2a9de4ced6217..4f90e955fe740c7aca139d408ef65dae9f656230 100644 (file)
@@ -91,7 +91,7 @@ export const fileNameFromURL = str => {
   const pathname = url.pathname;
   const index    = pathname.lastIndexOf('/');
 
-  return pathname.substring(index + 1);
+  return pathname.slice(index + 1);
 };
 
 export default @injectIntl