Skip to content

Commit

Permalink
[ESLint] Prefer String#slice() over String#substr() and String#substr…
Browse files Browse the repository at this point in the history
…ing() (#3042)

* Prefer String#slice() over String#substr() and String#substring()

* fix tests

* format
  • Loading branch information
dimaMachina authored Mar 4, 2023
1 parent aab10b8 commit 881a202
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 4 deletions.
7 changes: 7 additions & 0 deletions .changeset/witty-owls-burn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@graphiql/react': patch
'@graphiql/toolkit': patch
'graphql-language-service': patch
---

Prefer String#slice() over String#substr() and String#substring()
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ module.exports = {
'require-await': 0,
'vars-on-top': 0,
yoda: 1,
'unicorn/prefer-string-slice': 'error',
'sonarjs/no-identical-functions': 'error',
'sonarjs/no-unused-collection': 'error',
'sonarjs/no-extra-arguments': 'error',
Expand Down
2 changes: 1 addition & 1 deletion packages/graphiql-react/src/editor/tabs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ function guid(): string {
const s4 = () => {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
.slice(1);
};
// return id of format 'aaaaaaaa'-'aaaa'-'aaaa'-'aaaa'-'aaaaaaaaaaaa'
return `${s4()}${s4()}-${s4()}-${s4()}-${s4()}-${s4()}${s4()}${s4()}`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ function getIndentation(str: string, index: number) {
indentEnd = indentStart;
}
}
return str.substring(indentStart, indentEnd);
return str.slice(indentStart, indentEnd);
}

function isFieldType(
Expand Down
2 changes: 1 addition & 1 deletion packages/graphiql/resources/renderExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// Parse the search string to get url parameters.
var parameters = {};
window.location.search
.substr(1)
.slice(1)
.split('&')
.forEach(function (entry) {
var eq = entry.indexOf('=');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ export default class CharacterStream implements CharacterStreamInterface {

if (typeof pattern === 'string') {
const regex = new RegExp(pattern, caseFold ? 'i' : 'g');
match = regex.test(this._sourceText.substr(this._pos, pattern.length));
match = regex.test(
this._sourceText.slice(this._pos, this._pos + pattern.length),
);
token = pattern;
} else if (pattern instanceof RegExp) {
match = this._sourceText.slice(this._pos).match(pattern);
Expand Down

0 comments on commit 881a202

Please sign in to comment.