Skip to content

Commit 21f7c65

Browse files
refactor: replace deprecated String.prototype.substr() (#35205)
.substr() is deprecated so we replace it with .slice() which works similarily but isn't deprecated Signed-off-by: Tobias Speicher <rootcommander@gmail.com>
1 parent f4121fb commit 21f7c65

File tree

14 files changed

+21
-21
lines changed

14 files changed

+21
-21
lines changed

examples/using-remark/src/utils/typography.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import presets from "../utils/presets"
44

55
const { baseHsl, colors } = styleColors
66

7-
const linkRaw = colors.link.substr(1)
8-
const linkHoverRaw = colors.linkHover.substr(1)
7+
const linkRaw = colors.link.slice(1)
8+
const linkHoverRaw = colors.linkHover.slice(1)
99

1010
const options = {
1111
baseFontSize: `17px`,

packages/gatsby-cli/src/reporter/prepare-stack-trace.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ function getPosition({
136136
line: frame.getLineNumber(),
137137
source: frame
138138
.getFileName()
139-
.substr(frame.getFileName().indexOf(`webpack:`))
139+
.slice(frame.getFileName().indexOf(`webpack:`))
140140
.replace(/webpack:\/+/g, `webpack://`),
141141
name: null,
142142
}

packages/gatsby-graphiql-explorer/src/app/app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import "graphiql-code-exporter/CodeExporter.css"
1818

1919
const parameters = {}
2020
window.location.search
21-
.substr(1)
21+
.slice(1)
2222
.split(`&`)
2323
.forEach(function (entry) {
2424
const eq = entry.indexOf(`=`)

packages/gatsby-link/src/parse-path.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ export function parsePath(path) {
55

66
const hashIndex = pathname.indexOf(`#`)
77
if (hashIndex !== -1) {
8-
hash = pathname.substr(hashIndex)
9-
pathname = pathname.substr(0, hashIndex)
8+
hash = pathname.slice(hashIndex)
9+
pathname = pathname.slice(0, hashIndex)
1010
}
1111

1212
const searchIndex = pathname.indexOf(`?`)
1313
if (searchIndex !== -1) {
14-
search = pathname.substr(searchIndex)
15-
pathname = pathname.substr(0, searchIndex)
14+
search = pathname.slice(searchIndex)
15+
pathname = pathname.slice(0, searchIndex)
1616
}
1717

1818
return {

packages/gatsby-plugin-image/src/image-utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ export const getSrcSet = (images: Array<IImage>): string =>
138138
export function formatFromFilename(filename: string): ImageFormat | undefined {
139139
const dot = filename.lastIndexOf(`.`)
140140
if (dot !== -1) {
141-
const ext = filename.substr(dot + 1)
141+
const ext = filename.slice(dot + 1)
142142
if (ext === `jpeg`) {
143143
return `jpg`
144144
}

packages/gatsby-plugin-sharp/src/process-file.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,5 +178,5 @@ exports.processFile = async (file, transforms, options = {}) => {
178178
exports.createArgsDigest = args => {
179179
const argsDigest = createContentDigest(args)
180180

181-
return argsDigest.substr(argsDigest.length - 5)
181+
return argsDigest.slice(-5)
182182
}

packages/gatsby-remark-embed-snippet/src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ module.exports = ({ markdownAST, markdownNode }, { directory } = {}) => {
4747
const { value } = node
4848

4949
if (value.startsWith(`embed:`)) {
50-
const file = value.substr(6)
50+
const file = value.slice(6)
5151
let snippetPath = normalizePath(path.join(directory, file))
5252

5353
// Embed specific lines numbers of a file

packages/gatsby-transformer-react-docgen/src/doclets.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const isLiteral = str => /^('|"|true|false|\d+)/.test(str.trim())
1414
export const cleanDoclets = desc => {
1515
desc = desc || ``
1616
const idx = desc.search(DOCLET_PATTERN)
17-
return (idx === -1 ? desc : desc.substr(0, idx)).trim()
17+
return (idx === -1 ? desc : desc.slice(0, idx)).trim()
1818
}
1919

2020
/**

packages/gatsby/src/bootstrap/redirects-writer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export const writeRedirects = async (): Promise<void> => {
1919

2020
for (const redirect of redirects) {
2121
const alternativePath = redirect.fromPath.endsWith(`/`)
22-
? redirect.fromPath.substr(0, redirect.fromPath.length - 1)
22+
? redirect.fromPath.slice(0, -1)
2323
: redirect.fromPath + `/`
2424

2525
let hasSamePage: boolean

packages/gatsby/src/cache/cache-fs.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,8 @@ DiskStore.prototype._getFilePathByKey = function (key): string {
294294
// create subdirs with the first 3 chars of the hash
295295
return path.join(
296296
this.options.path,
297-
`diskstore-` + hash.substr(0, 3),
298-
hash.substr(3)
297+
`diskstore-` + hash.slice(0, 3),
298+
hash.slice(3)
299299
)
300300
} else {
301301
return path.join(this.options.path, `diskstore-` + hash)

packages/gatsby/src/utils/webpack.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ module.exports = async (
126126

127127
if (process.env.GATSBY_WEBPACK_PUBLICPATH) {
128128
const pubPath = process.env.GATSBY_WEBPACK_PUBLICPATH
129-
if (pubPath.substr(-1) === `/`) {
129+
if (pubPath.slice(-1) === `/`) {
130130
hmrBasePath = pubPath
131131
} else {
132132
hmrBasePath = withTrailingSlash(pubPath)
@@ -927,7 +927,7 @@ module.exports = async (
927927
// so loader rule test work well
928928
const queryParamStartIndex = modulePath.indexOf(`?`)
929929
if (queryParamStartIndex !== -1) {
930-
modulePath = modulePath.substr(0, queryParamStartIndex)
930+
modulePath = modulePath.slice(0, queryParamStartIndex)
931931
}
932932

933933
return fastRefreshIncludes.some(re => re.test(modulePath))

scripts/gatsby-changelog-generator/generate.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ function addChangelogEntries(packageName, entries, contents) {
237237
const updatedChangelogParts = [
238238
header,
239239
entries.trimRight(),
240-
contents.substr(header.length).trimStart(),
240+
contents.slice(header.length).trimStart(),
241241
]
242242
fs.writeFileSync(
243243
changelogPath(packageName),

scripts/gatsby-changelog-generator/render.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ function commitReference(commit, reference) {
8383

8484
function commitHash(commit) {
8585
if (!commit.hash) return ``
86-
const shortHash = commit.hash.substr(0, 7)
86+
const shortHash = commit.hash.slice(0, 7)
8787
return `([${shortHash}](https://github.com/gatsbyjs/gatsby/commit/${commit.hash}))`
8888
}
8989

scripts/i18n/sync.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const syncLabelName = `sync`
1919

2020
// get the git short hash
2121
function getShortHash(hash) {
22-
return hash.substr(0, 7)
22+
return hash.slice(0, 7)
2323
}
2424

2525
function cloneOrUpdateRepo(repoName, repoUrl) {
@@ -294,7 +294,7 @@ async function syncTranslationRepo(code) {
294294
// Message is of the form:
295295
// CONFLICT (content): Merge conflict in {file path}
296296
const conflictFiles = conflictLines.map(line =>
297-
line.substr(line.lastIndexOf(` `) + 1)
297+
line.slice(line.lastIndexOf(` `) + 1)
298298
)
299299
// Do a soft reset and unstage non-conflicted files
300300
shell.exec(`git reset`, { silent: true })

0 commit comments

Comments
 (0)