Skip to content

Commit

Permalink
chore: replace deprecated String.prototype.substr() (#4693)
Browse files Browse the repository at this point in the history
String.prototype.substr() is deprecated (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr) so we replace it with slice() which works similarily but isn't deprecated.
Signed-off-by: Tobias Speicher <rootcommander@gmail.com>
  • Loading branch information
CommanderRoot committed Mar 17, 2022
1 parent b5b5f41 commit f3a97ff
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 21 deletions.
4 changes: 2 additions & 2 deletions packages/eslint-plugin/src/rules/member-ordering.ts
Expand Up @@ -299,10 +299,10 @@ function getMemberRawName(
const { name, type } = util.getNameFromMember(member, sourceCode);

if (type === util.MemberNameType.Quoted) {
return name.substr(1, name.length - 2);
return name.slice(1, -1);
}
if (type === util.MemberNameType.Private) {
return name.substr(1);
return name.slice(1);
}
return name;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/typescript-estree/src/convert-comments.ts
Expand Up @@ -36,7 +36,7 @@ export function convertComments(
range[1] - textStart - 2;
comments.push({
type,
value: code.substr(textStart, textEnd),
value: code.slice(textStart, textStart + textEnd),
range,
loc,
});
Expand Down
2 changes: 1 addition & 1 deletion packages/typescript-estree/src/create-program/shared.ts
Expand Up @@ -57,7 +57,7 @@ const correctPathCasing = useCaseSensitiveFileNames
function getCanonicalFileName(filePath: string): CanonicalPath {
let normalized = path.normalize(filePath);
if (normalized.endsWith(path.sep)) {
normalized = normalized.substr(0, normalized.length - 1);
normalized = normalized.slice(0, -1);
}
return correctPathCasing(normalized) as CanonicalPath;
}
Expand Down
23 changes: 6 additions & 17 deletions packages/website-eslint/src/mock/path.js
Expand Up @@ -95,7 +95,7 @@ export function resolve() {
// posix version
export function normalize(path) {
let isPathAbsolute = isAbsolute(path),
trailingSlash = substr(path, -1) === '/';
trailingSlash = path.endsWith('/');

// Normalize the path
path = normalizeArray(
Expand Down Expand Up @@ -136,8 +136,8 @@ export function join() {
// path.relative(from, to)
// posix version
export function relative(from, to) {
from = resolve(from).substr(1);
to = resolve(to).substr(1);
from = resolve(from).slice(1);
to = resolve(to).slice(1);

function trim(arr) {
let start = 0;
Expand Down Expand Up @@ -191,7 +191,7 @@ export function dirname(path) {

if (dir) {
// It has a dirname, strip trailing slash
dir = dir.substr(0, dir.length - 1);
dir = dir.slice(0, -1);
}

return root + dir;
Expand All @@ -200,8 +200,8 @@ export function dirname(path) {
export function basename(path, ext) {
let f = splitPath(path)[2];
// TODO: make this comparison case-insensitive on windows?
if (ext && f.substr(-1 * ext.length) === ext) {
f = f.substr(0, f.length - ext.length);
if (ext && f.slice(-1 * ext.length) === ext) {
f = f.slice(0, f.length - ext.length);
}
return f;
}
Expand Down Expand Up @@ -231,14 +231,3 @@ function filter(xs, f) {
}
return res;
}

// String.prototype.substr - negative index don't work in IE8
const substr =
'ab'.substr(-1) === 'b'
? function (str, start, len) {
return str.substr(start, len);
}
: function (str, start, len) {
if (start < 0) start = str.length + start;
return str.substr(start, len);
};

0 comments on commit f3a97ff

Please sign in to comment.