Skip to content

Commit 87bec51

Browse files
chore: replace deprecated String.prototype.substr() (#3702)
.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 c4c41af commit 87bec51

6 files changed

Lines changed: 23 additions & 23 deletions

File tree

packages/less/src/less-node/plugin-loader.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const PluginLoader = function(less) {
99
this.require = prefix => {
1010
prefix = path.dirname(prefix);
1111
return id => {
12-
const str = id.substr(0, 2);
12+
const str = id.slice(0, 2);
1313
if (str === '..' || str === './') {
1414
return require(path.join(prefix, id));
1515
}

packages/less/src/less/less-error.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const LessError = function(e, fileContentMap, currentFilename) {
5454
/**
5555
* We have to figure out how this environment stringifies anonymous functions
5656
* so we can correctly map plugin errors.
57-
*
57+
*
5858
* Note, in Node 8, the output of anonymous funcs varied based on parameters
5959
* being present or not, so we inject dummy params.
6060
*/
@@ -133,7 +133,7 @@ LessError.prototype.toString = function(options) {
133133
let errorTxt = `${this.line} `;
134134
if (extract[1]) {
135135
errorTxt += extract[1].slice(0, this.column) +
136-
stylize(stylize(stylize(extract[1].substr(this.column, 1), 'bold') +
136+
stylize(stylize(stylize(extract[1].slice(this.column, this.column + 1), 'bold') +
137137
extract[1].slice(this.column + 1), 'red'), 'inverse');
138138
}
139139
error.push(errorTxt);
@@ -163,4 +163,4 @@ LessError.prototype.toString = function(options) {
163163
return message;
164164
};
165165

166-
export default LessError;
166+
export default LessError;

packages/less/src/less/parser/parser-input.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,15 @@ export default () => {
5656
nextNewLine = endIndex;
5757
}
5858
parserInput.i = nextNewLine;
59-
comment.text = inp.substr(comment.index, parserInput.i - comment.index);
59+
comment.text = inp.slice(comment.index, parserInput.i);
6060
parserInput.commentStore.push(comment);
6161
continue;
6262
} else if (nextChar === '*') {
6363
const nextStarSlash = inp.indexOf('*/', parserInput.i + 2);
6464
if (nextStarSlash >= 0) {
6565
comment = {
6666
index: parserInput.i,
67-
text: inp.substr(parserInput.i, nextStarSlash + 2 - parserInput.i),
67+
text: inp.slice(parserInput.i, nextStarSlash + 2),
6868
isLineComment: false
6969
};
7070
parserInput.i += comment.text.length - 1;
@@ -188,7 +188,7 @@ export default () => {
188188
case '\n':
189189
break;
190190
case startChar: {
191-
const str = input.substr(currentPosition, i + 1);
191+
const str = input.slice(currentPosition, currentPosition + i + 1);
192192
if (!loc && loc !== 0) {
193193
skipWhitespace(i + 1);
194194
return str
@@ -228,7 +228,7 @@ export default () => {
228228
do {
229229
let nextChar = input.charAt(i);
230230
if (blockDepth === 0 && testChar(nextChar)) {
231-
returnVal = input.substr(lastPos, i - lastPos);
231+
returnVal = input.slice(lastPos, i);
232232
if (returnVal) {
233233
parseGroups.push(returnVal);
234234
}
@@ -240,7 +240,7 @@ export default () => {
240240
loop = false
241241
} else {
242242
if (inComment) {
243-
if (nextChar === '*' &&
243+
if (nextChar === '*' &&
244244
input.charAt(i + 1) === '/') {
245245
i++;
246246
blockDepth--;
@@ -253,7 +253,7 @@ export default () => {
253253
case '\\':
254254
i++;
255255
nextChar = input.charAt(i);
256-
parseGroups.push(input.substr(lastPos, i - lastPos + 1));
256+
parseGroups.push(input.slice(lastPos, i + 1));
257257
lastPos = i + 1;
258258
break;
259259
case '/':
@@ -267,7 +267,7 @@ export default () => {
267267
case '"':
268268
quote = parserInput.$quoted(i);
269269
if (quote) {
270-
parseGroups.push(input.substr(lastPos, i - lastPos), quote);
270+
parseGroups.push(input.slice(lastPos, i), quote);
271271
i += quote[1].length - 1;
272272
lastPos = i + 1;
273273
}

packages/less/src/less/parser/parser.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ const Parser = function Parser(context, imports, fileInfo, currentIndex) {
406406
}
407407
parserInput.forget();
408408

409-
return new(tree.Quoted)(str.charAt(0), str.substr(1, str.length - 2), isEscaped, index + currentIndex, fileInfo);
409+
return new(tree.Quoted)(str.charAt(0), str.slice(1, -1), isEscaped, index + currentIndex, fileInfo);
410410
},
411411

412412
//
@@ -788,7 +788,7 @@ const Parser = function Parser(context, imports, fileInfo, currentIndex) {
788788
if (js) {
789789
warn('Inline JavaScript evaluation (backtick expressions) is deprecated and will be removed in Less 5.x. Use Less functions or custom plugins instead.', index, 'DEPRECATED', 'js-eval');
790790
parserInput.forget();
791-
return new(tree.JavaScript)(js.substr(0, js.length - 1), Boolean(escape), index + currentIndex, fileInfo);
791+
return new(tree.JavaScript)(js.slice(0, -1), Boolean(escape), index + currentIndex, fileInfo);
792792
}
793793
parserInput.restore('invalid javascript definition');
794794
}
@@ -1901,7 +1901,7 @@ const Parser = function Parser(context, imports, fileInfo, currentIndex) {
19011901
}
19021902
if (parserInput.$char(')')) {
19031903
if (p && !e) {
1904-
nodes.push(new (tree.Paren)(new (tree.QueryInParens)(p.op, p.lvalue, p.rvalue, rangeP ? rangeP.op : null, rangeP ? rangeP.rvalue : null, p._index)));
1904+
nodes.push(new (tree.Paren)(new (tree.QueryInParens)(p.op, p.lvalue, p.rvalue, rangeP ? rangeP.op : null, rangeP ? rangeP.rvalue : null, p._index)));
19051905
e = p;
19061906
} else if (p && e) {
19071907
nodes.push(new (tree.Paren)(new (tree.Declaration)(p, e, null, null, parserInput.i + currentIndex, fileInfo, true)));
@@ -1986,12 +1986,12 @@ const Parser = function Parser(context, imports, fileInfo, currentIndex) {
19861986
if (parserInput.$str('@media')) {
19871987
return this.prepareAndGetNestableAtRule(tree.Media, index, debugInfo, MediaSyntaxOptions);
19881988
}
1989-
1989+
19901990
if (parserInput.$str('@container')) {
19911991
return this.prepareAndGetNestableAtRule(tree.Container, index, debugInfo, ContainerSyntaxOptions);
19921992
}
19931993
}
1994-
1994+
19951995
parserInput.restore();
19961996
},
19971997

packages/less/src/less/tree/dimension.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ Dimension.prototype = Object.assign(new Node(), {
5656

5757
// Float values doesn't need a leading zero
5858
if (value > 0 && value < 1) {
59-
strValue = (strValue).substr(1);
59+
strValue = (strValue).slice(1);
6060
}
6161
}
6262

packages/less/src/less/tree/namespace-value.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ NamespaceValue.prototype = Object.assign(new Node(), {
1515

1616
eval(context) {
1717
let i, name, rules = this.value.eval(context);
18-
18+
1919
for (i = 0; i < this.lookups.length; i++) {
2020
name = this.lookups[i];
2121

@@ -33,12 +33,12 @@ NamespaceValue.prototype = Object.assign(new Node(), {
3333
}
3434
else if (name.charAt(0) === '@') {
3535
if (name.charAt(1) === '@') {
36-
name = `@${new Variable(name.substr(1)).eval(context).value}`;
36+
name = `@${new Variable(name.slice(1)).eval(context).value}`;
3737
}
3838
if (rules.variables) {
3939
rules = rules.variable(name);
4040
}
41-
41+
4242
if (!rules) {
4343
throw { type: 'Name',
4444
message: `variable ${name} not found`,
@@ -48,18 +48,18 @@ NamespaceValue.prototype = Object.assign(new Node(), {
4848
}
4949
else {
5050
if (name.substring(0, 2) === '$@') {
51-
name = `$${new Variable(name.substr(1)).eval(context).value}`;
51+
name = `$${new Variable(name.slice(1)).eval(context).value}`;
5252
}
5353
else {
5454
name = name.charAt(0) === '$' ? name : `$${name}`;
5555
}
5656
if (rules.properties) {
5757
rules = rules.property(name);
5858
}
59-
59+
6060
if (!rules) {
6161
throw { type: 'Name',
62-
message: `property "${name.substr(1)}" not found`,
62+
message: `property "${name.slice(1)}" not found`,
6363
filename: this.fileInfo().filename,
6464
index: this.getIndex() };
6565
}

0 commit comments

Comments
 (0)