Skip to content

Commit 83c52af

Browse files
committed
[chore] replace deprecated String.prototype.substr()
.substr() is deprecated so we replace it with .slice() which works similarily but isn't deprecated Signed-off-by: Tobias Speicher <[email protected]>
1 parent 28fe541 commit 83c52af

File tree

9 files changed

+14
-10
lines changed

9 files changed

+14
-10
lines changed

.eslintrc.js

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ module.exports = {
2020
// project-specific settings
2121
curly: [2, 'all'],
2222
'max-len': 'off', // handled by prettier
23+
'no-restricted-properties': [
24+
'error',
25+
{ property: 'substr', message: 'Use .slice instead of .substr.' },
26+
],
2327
'no-trailing-spaces': 'error',
2428
'one-var': ['error', 'never'],
2529
'@typescript-eslint/no-unused-vars': ['error', { args: 'none' }],

packages/language-server/src/plugins/svelte/features/getCompletions.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export function getCompletions(
4343
const lastCharactersBeforePosition = svelteDoc
4444
.getText()
4545
// use last 10 characters, should cover 99% of all cases
46-
.substr(Math.max(offset - 10, 0), Math.min(offset, 10));
46+
.slice(Math.max(offset - 10, 0), Math.max(offset - 10, 0) + 10);
4747
const precededByOpeningBracket = /[\s\S]*{\s*[#:/@]\w*$/.test(lastCharactersBeforePosition);
4848
if (isInStyleOrScript) {
4949
return null;

packages/language-server/src/plugins/svelte/features/getHoverInfo.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export function getHoverInfo(
2626
const charactersAroundOffset = svelteDoc
2727
.getText()
2828
// use last 10 and next 10 characters, should cover 99% of all cases
29-
.substr(offsetStart, 20);
29+
.slice(offsetStart, offsetStart + 20);
3030
const isSvelteTag = tagRegexp.test(charactersAroundOffset);
3131

3232
if (isInStyleOrScript) {

packages/language-server/src/plugins/typescript/features/HoverProvider.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export class HoverProviderImpl implements HoverProvider {
7575
return null;
7676
}
7777

78-
const eventName = possibleEventName.substr('on:'.length);
78+
const eventName = possibleEventName.slice('on:'.length);
7979
const event = component.getEvents().find((event) => event.name === eventName);
8080
if (!event) {
8181
return null;

packages/language-server/src/plugins/typescript/features/RenameProvider.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -283,9 +283,9 @@ export class RenameProviderImpl implements RenameProvider {
283283
) {
284284
const regex = new RegExp(
285285
// no 'export let', only 'let', because that's what it's translated to in svelte2tsx
286-
`\\s+let\\s+(${fragment.text.substr(
286+
`\\s+let\\s+(${fragment.text.slice(
287287
updatePropLocation.textSpan.start,
288-
updatePropLocation.textSpan.length
288+
updatePropLocation.textSpan.start + updatePropLocation.textSpan.length
289289
)})($|\\s|;|:)`
290290
);
291291
const match = fragment.text.match(regex);
@@ -480,7 +480,7 @@ export class RenameProviderImpl implements RenameProvider {
480480
const originalStart = offsetAt(location.range.start, originalText);
481481

482482
const isShortHandBinding =
483-
originalText.substr(originalStart - bind.length, bind.length) === bind;
483+
originalText.slice(originalStart - bind.length, originalStart) === bind;
484484

485485
const directiveName = (isShortHandBinding ? bind : '') + identifierName;
486486
const prefixText = directiveName + '={';

packages/language-server/src/plugins/typescript/previewer.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ function getTagBodyText(tag: ts.JSDocTagInfo): string | undefined {
5353
return (
5454
captionTagMatches[1] +
5555
'\n\n' +
56-
makeCodeblock(text.substr(captionTagMatches[0].length))
56+
makeCodeblock(text.slice(captionTagMatches[0].length))
5757
);
5858
} else {
5959
return makeCodeblock(text);

packages/language-server/src/plugins/typescript/utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { pathToUrl } from '../../utils';
1313
import { SnapshotFragment, SvelteSnapshotFragment } from './DocumentSnapshot';
1414

1515
export function getScriptKindFromFileName(fileName: string): ts.ScriptKind {
16-
const ext = fileName.substr(fileName.lastIndexOf('.'));
16+
const ext = fileName.slice(fileName.lastIndexOf('.'));
1717
switch (ext.toLowerCase()) {
1818
case ts.Extension.Js:
1919
return ts.ScriptKind.JS;

packages/svelte2tsx/src/htmlxtojsx/nodes/attribute.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ function sanitizeLeadingChars(attrName: string): string {
208208
let sanitizedName = '';
209209
for (let i = 0; i < attrName.length; i++) {
210210
if (/[A-Za-z$_]/.test(attrName[i])) {
211-
sanitizedName += attrName.substr(i);
211+
sanitizedName += attrName.slice(i);
212212
return sanitizedName;
213213
} else {
214214
sanitizedName += '_';

packages/svelte2tsx/src/svelte2tsx/addComponentExport.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ function classNameFromFilename(filename: string, appendSuffix: boolean): string
196196
// Although _ and $ are valid first characters for classes, they are invalid first characters
197197
// for tag names. For a better import autocompletion experience, we therefore throw them out.
198198
.findIndex((char) => /[A-Za-z]/.test(char));
199-
const withoutLeadingInvalidCharacters = withoutInvalidCharacters.substr(firstValidCharIdx);
199+
const withoutLeadingInvalidCharacters = withoutInvalidCharacters.slice(firstValidCharIdx);
200200
const inPascalCase = pascalCase(withoutLeadingInvalidCharacters);
201201
const finalName = firstValidCharIdx === -1 ? `A${inPascalCase}` : inPascalCase;
202202
return `${finalName}${appendSuffix ? COMPONENT_SUFFIX : ''}`;

0 commit comments

Comments
 (0)