Skip to content

Commit

Permalink
chore: replace deprecated String.prototype.substr() (#4479)
Browse files Browse the repository at this point in the history
Co-authored-by: Zoltan Kochan <z@kochan.io>
  • Loading branch information
CommanderRoot and zkochan committed Mar 25, 2022
1 parent 426f751 commit bc35dff
Show file tree
Hide file tree
Showing 38 changed files with 76 additions and 68 deletions.
2 changes: 1 addition & 1 deletion .meta-updater/src/index.ts
Expand Up @@ -83,7 +83,7 @@ async function updateTSConfig (
const references = [] as Array<{ path: string }>
for (const [depName, spec] of Object.entries(deps)) {
if (!spec.startsWith('link:') || spec.length === 5) continue
const relativePath = spec.substr(5)
const relativePath = spec.slice(5)
if (!await exists(path.join(dir, relativePath, 'tsconfig.json'))) continue
if (
depName === '@pnpm/package-store' && (
Expand Down
2 changes: 1 addition & 1 deletion packages/cafs/src/addFilesFromTarball.ts
Expand Up @@ -20,7 +20,7 @@ export default async function (
// which are the same file.
// Hence, we are normalizing the file name, replacing // with / and checking for duplicates.
// Example of such package: @pnpm/colorize-semver-diff@1.0.1
const filename = header.name.substr(header.name.indexOf('/') + 1).replace(/\/\//g, '/')
const filename = header.name.slice(header.name.indexOf('/') + 1).replace(/\/\//g, '/')
if (header.type !== 'file' || ignore(filename) || filesIndex[filename]) {
fileStream.resume()
next()
Expand Down
2 changes: 1 addition & 1 deletion packages/cli-meta/src/index.ts
Expand Up @@ -30,7 +30,7 @@ const packageManager = {
name: pkgJson.name,
// Never a prerelease version
stableVersion: pkgJson.version.includes('-')
? pkgJson.version.substr(0, pkgJson.version.indexOf('-'))
? pkgJson.version.slice(0, pkgJson.version.indexOf('-'))
: pkgJson.version,
// This may be a 3.0.0-beta.2
version: pkgJson.version,
Expand Down
2 changes: 1 addition & 1 deletion packages/config/src/getScopeRegistries.ts
Expand Up @@ -4,7 +4,7 @@ export default function getScopeRegistries (rawConfig: Object) {
const registries = {}
for (const configKey of Object.keys(rawConfig)) {
if (configKey[0] === '@' && configKey.endsWith(':registry')) {
registries[configKey.substr(0, configKey.indexOf(':'))] = normalizeRegistryUrl(rawConfig[configKey])
registries[configKey.slice(0, configKey.indexOf(':'))] = normalizeRegistryUrl(rawConfig[configKey])
}
}
return registries
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/install/allProjectsAreUpToDate.ts
Expand Up @@ -90,7 +90,7 @@ async function linkedPackagesAreUpToDate (
continue
}
const linkedDir = isLinked
? path.join(project.dir, lockfileRef.substr(5))
? path.join(project.dir, lockfileRef.slice(5))
: workspacePackages?.[depName]?.[lockfileRef]?.dir
if (!linkedDir) continue
if (!linkWorkspacePackages && !currentSpec.startsWith('workspace:')) {
Expand All @@ -110,12 +110,12 @@ async function linkedPackagesAreUpToDate (
}

function getVersionRange (spec: string) {
if (spec.startsWith('workspace:')) return spec.substr(10)
if (spec.startsWith('workspace:')) return spec.slice(10)
if (spec.startsWith('npm:')) {
spec = spec.substr(4)
spec = spec.slice(4)
const index = spec.indexOf('@', 1)
if (index === -1) return '*'
return spec.substr(index + 1) || '*'
return spec.slice(index + 1) || '*'
}
return spec
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/install/link.ts
Expand Up @@ -457,7 +457,7 @@ async function linkAllModules (
Object.entries(childrenToLink)
.map(async ([childAlias, childDepPath]) => {
if (childDepPath.startsWith('link:')) {
await limitLinking(async () => symlinkDependency(path.resolve(opts.lockfileDir, childDepPath.substr(5)), modules, childAlias))
await limitLinking(async () => symlinkDependency(path.resolve(opts.lockfileDir, childDepPath.slice(5)), modules, childAlias))
return
}
const pkg = depGraph[childDepPath]
Expand Down
Expand Up @@ -206,7 +206,7 @@ function formatIndentedStatus (status: string) {

function highlightLastFolder (p: string) {
const lastSlash = p.lastIndexOf('/') + 1
return `${chalk.gray(p.substr(0, lastSlash))}${p.substr(lastSlash)}`
return `${chalk.gray(p.slice(0, lastSlash))}${p.slice(lastSlash)}`
}

const ANSI_ESCAPES_LENGTH_OF_PREFIX = hlValue(' ').length - 1
Expand Down Expand Up @@ -268,7 +268,7 @@ function formatLine (maxWidth: number, logObj: LifecycleLog) {
}

function cutLine (line: string, maxLength: number) {
return stripAnsi(line).substr(0, maxLength)
return stripAnsi(line).slice(0, maxLength)
}

function aggregateOutput (source: Rx.Observable<LifecycleLog>) {
Expand Down
Expand Up @@ -9,15 +9,15 @@ export default function formatPrefix (cwd: string, prefix: string) {
return prefix
}

const shortPrefix = prefix.substr(-PREFIX_MAX_LENGTH + 3)
const shortPrefix = prefix.slice(-PREFIX_MAX_LENGTH + 3)

const separatorLocation = shortPrefix.indexOf('/')

if (separatorLocation <= 0) {
return `...${shortPrefix}`
}

return `...${shortPrefix.substr(separatorLocation)}`
return `...${shortPrefix.slice(separatorLocation)}`
}

export function formatPrefixNoTrim (cwd: string, prefix: string) {
Expand Down
2 changes: 1 addition & 1 deletion packages/dependencies-hierarchy/src/index.ts
Expand Up @@ -395,7 +395,7 @@ function getPkgInfo (
isPeer: Boolean(opts.peers?.has(opts.alias)),
isSkipped,
name,
path: depPath ? path.join(opts.modulesDir, '.pnpm', depPathToFilename(depPath)) : path.join(opts.modulesDir, '..', opts.ref.substr(5)),
path: depPath ? path.join(opts.modulesDir, '.pnpm', depPathToFilename(depPath)) : path.join(opts.modulesDir, '..', opts.ref.slice(5)),
version,
}
if (resolved) {
Expand Down
11 changes: 6 additions & 5 deletions packages/dependency-path/src/index.ts
Expand Up @@ -14,7 +14,8 @@ export function resolve (
if (!isAbsolute(resolutionLocation)) {
let registryUrl!: string
if (resolutionLocation[1] === '@') {
const scope = resolutionLocation.substr(1, resolutionLocation.indexOf('/', 1) - 1)
const slashIndex = resolutionLocation.indexOf('/', 1)
const scope = resolutionLocation.slice(1, slashIndex !== -1 ? slashIndex : 0)
registryUrl = registries[scope] || registries.default
} else {
registryUrl = registries.default
Expand All @@ -31,7 +32,7 @@ export function tryGetPackageId (registries: Registries, relDepPath: string) {
}
const underscoreIndex = relDepPath.indexOf('_', relDepPath.lastIndexOf('/'))
if (underscoreIndex !== -1) {
return resolve(registries, relDepPath.substr(0, underscoreIndex))
return resolve(registries, relDepPath.slice(0, underscoreIndex))
}
return resolve(registries, relDepPath)
}
Expand All @@ -55,7 +56,7 @@ export function refToAbsolute (

export function getRegistryByPackageName (registries: Registries, packageName: string) {
if (packageName[0] !== '@') return registries.default
const scope = packageName.substr(0, packageName.indexOf('/'))
const scope = packageName.substring(0, packageName.indexOf('/'))
return registries[scope] || registries.default
}

Expand All @@ -67,7 +68,7 @@ export function relative (
const registryName = encodeRegistry(getRegistryByPackageName(registries, packageName))

if (absoluteResolutionLoc.startsWith(`${registryName}/`)) {
return absoluteResolutionLoc.substr(absoluteResolutionLoc.indexOf('/'))
return absoluteResolutionLoc.slice(absoluteResolutionLoc.indexOf('/'))
}
return absoluteResolutionLoc
}
Expand Down Expand Up @@ -142,7 +143,7 @@ function depPathToFilenameUnescaped (depPath: string) {
depPath = depPath.substring(1)
}
const index = depPath.lastIndexOf('/')
return `${depPath.substring(0, index)}@${depPath.substr(index + 1)}`
return `${depPath.substring(0, index)}@${depPath.slice(index + 1)}`
}
return depPath.replace(':', '+')
}
4 changes: 2 additions & 2 deletions packages/exportable-manifest/src/index.ts
Expand Up @@ -108,7 +108,7 @@ async function makePublishDependency (depName: string, depSpec: string, dir: str
return `${semverRangeToken}${manifest.version}`
}
if (depSpec.startsWith('workspace:./') || depSpec.startsWith('workspace:../')) {
const { manifest } = await tryReadProjectManifest(path.join(dir, depSpec.substr(10)))
const { manifest } = await tryReadProjectManifest(path.join(dir, depSpec.slice(10)))
if ((manifest == null) || !manifest.name || !manifest.version) {
throw new PnpmError(
'CANNOT_RESOLVE_WORKSPACE_PROTOCOL',
Expand All @@ -119,7 +119,7 @@ async function makePublishDependency (depName: string, depSpec: string, dir: str
if (manifest.name === depName) return `${manifest.version}`
return `npm:${manifest.name}@${manifest.version}`
}
depSpec = depSpec.substr(10)
depSpec = depSpec.slice(10)
if (depSpec.includes('@')) {
return `npm:${depSpec}`
}
Expand Down
10 changes: 5 additions & 5 deletions packages/filter-workspace-packages/src/parsePackageSelector.ts
Expand Up @@ -20,18 +20,18 @@ export default (rawSelector: string, prefix: string): PackageSelector => {
let excludeSelf = false
const includeDependencies = rawSelector.endsWith('...')
if (includeDependencies) {
rawSelector = rawSelector.substring(0, rawSelector.length - 3)
rawSelector = rawSelector.slice(0, -3)
if (rawSelector.endsWith('^')) {
excludeSelf = true
rawSelector = rawSelector.substr(0, rawSelector.length - 1)
rawSelector = rawSelector.slice(0, -1)
}
}
const includeDependents = rawSelector.startsWith('...')
if (includeDependents) {
rawSelector = rawSelector.substring(3)
if (rawSelector.startsWith('^')) {
excludeSelf = true
rawSelector = rawSelector.substr(1)
rawSelector = rawSelector.slice(1)
}
}
const matches = rawSelector.match(/^([^.][^{}[\]]*)?(\{[^}]+\})?(\[[^\]]+\])?$/)
Expand All @@ -50,13 +50,13 @@ export default (rawSelector: string, prefix: string): PackageSelector => {
}

return {
diff: matches[3]?.substr(1, matches[3].length - 2),
diff: matches[3]?.slice(1, -1),
exclude,
excludeSelf,
includeDependencies,
includeDependents,
namePattern: matches[1],
parentDir: matches[2] && path.join(prefix, matches[2].substr(1, matches[2].length - 2)),
parentDir: matches[2] && path.join(prefix, matches[2].slice(1, -1)),
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/git-resolver/src/index.ts
Expand Up @@ -121,6 +121,6 @@ function resolveRefFromRefs (refs: {[ref: string]: string}, repo: string, ref: s
}

function isSsh (gitSpec: string): boolean {
return gitSpec.substr(0, 10) === 'git+ssh://' ||
gitSpec.substr(0, 4) === 'git@'
return gitSpec.slice(0, 10) === 'git+ssh://' ||
gitSpec.slice(0, 4) === 'git@'
}
6 changes: 3 additions & 3 deletions packages/git-resolver/src/parsePref.ts
Expand Up @@ -36,7 +36,7 @@ export default async function parsePref (pref: string): Promise<HostedPackageSpe
}
const colonsPos = pref.indexOf(':')
if (colonsPos === -1) return null
const protocol = pref.substr(0, colonsPos)
const protocol = pref.slice(0, colonsPos)
if (protocol && gitProtocols.has(protocol.toLocaleLowerCase())) {
const urlparse = new URL(escapeColon(pref))
if (!urlparse || !urlparse.protocol) return null
Expand Down Expand Up @@ -69,7 +69,7 @@ function urlToFetchSpec (urlparse: URL) {
urlparse.hash = ''
const fetchSpec = url.format(urlparse)
if (fetchSpec.startsWith('git+')) {
return fetchSpec.substr(4)
return fetchSpec.slice(4)
}
return fetchSpec
}
Expand Down Expand Up @@ -107,7 +107,7 @@ async function fromHostedGit (hosted: any): Promise<HostedPackageSpec> { // esli
// npm instead tries git ls-remote directly which prompts user for login credentials.

// HTTP HEAD on https://domain/user/repo, strip out ".git"
const response = await fetch(httpsUrl.substr(0, httpsUrl.length - 4), { method: 'HEAD', follow: 0, retry: { retries: 0 } })
const response = await fetch(httpsUrl.slice(0, -4), { method: 'HEAD', follow: 0, retry: { retries: 0 } })
if (response.ok) {
fetchSpec = httpsUrl
}
Expand Down
2 changes: 1 addition & 1 deletion packages/headless/src/index.ts
Expand Up @@ -584,7 +584,7 @@ async function linkRootPackages (
if (allDeps[alias].startsWith('link:')) {
const isDev = Boolean(projectSnapshot.devDependencies?.[alias])
const isOptional = Boolean(projectSnapshot.optionalDependencies?.[alias])
const packageDir = path.join(opts.projectDir, allDeps[alias].substr(5))
const packageDir = path.join(opts.projectDir, allDeps[alias].slice(5))
const linkedPackage = await (async () => {
const importerId = getLockfileImporterId(opts.lockfileDir, packageDir)
if (importerManifestsByImporterId[importerId]) {
Expand Down
4 changes: 2 additions & 2 deletions packages/headless/src/lockfileToDepGraph.ts
Expand Up @@ -234,7 +234,7 @@ async function getChildrenPaths (
for (const alias of Object.keys(allDeps)) {
const childDepPath = dp.refToAbsolute(allDeps[alias], alias, ctx.registries)
if (childDepPath === null) {
children[alias] = path.resolve(ctx.lockfileDir, importerId, allDeps[alias].substr(5))
children[alias] = path.resolve(ctx.lockfileDir, importerId, allDeps[alias].slice(5))
continue
}
const childRelDepPath = dp.refToRelative(allDeps[alias], alias) as string
Expand All @@ -246,7 +246,7 @@ async function getChildrenPaths (
const pkgName = nameVerFromPkgSnapshot(childRelDepPath, childPkgSnapshot).name
children[alias] = path.join(ctx.virtualStoreDir, dp.depPathToFilename(childRelDepPath), 'node_modules', pkgName)
} else if (allDeps[alias].indexOf('file:') === 0) {
children[alias] = path.resolve(ctx.lockfileDir, allDeps[alias].substr(5))
children[alias] = path.resolve(ctx.lockfileDir, allDeps[alias].slice(5))
} else if (!ctx.skipped.has(childRelDepPath) && ((peerDeps == null) || !peerDeps.has(alias))) {
throw new Error(`${childRelDepPath} not found in ${WANTED_LOCKFILE}`)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/headless/src/lockfileToHoistedDepGraph.ts
Expand Up @@ -124,7 +124,7 @@ function pickLinkedDirectDeps (
const directDeps = {}
for (const [alias, ref] of Object.entries(rootDeps)) {
if (ref.startsWith('link:')) {
directDeps[alias] = path.resolve(importerDir, ref.substr(5))
directDeps[alias] = path.resolve(importerDir, ref.slice(5))
}
}
return directDeps
Expand Down
2 changes: 1 addition & 1 deletion packages/lockfile-to-pnp/src/index.ts
Expand Up @@ -143,7 +143,7 @@ function toPackageDependenciesMap (
): Array<[string, string | [string, string]]> {
return Object.entries(deps).map(([depAlias, ref]) => {
if (importerId && ref.startsWith('link:')) {
return [depAlias, path.join(importerId, ref.substr(5))]
return [depAlias, path.join(importerId, ref.slice(5))]
}
const relDepPath = refToRelative(ref, depAlias)
if (!relDepPath) return [depAlias, ref]
Expand Down
2 changes: 1 addition & 1 deletion packages/make-dedicated-lockfile/src/index.ts
Expand Up @@ -23,7 +23,7 @@ export default async function (lockfileDir: string, projectDir: string) {
const baseImporterId = getLockfileImporterId(lockfileDir, projectDir)
for (const [importerId, importer] of Object.entries(allImporters)) {
if (importerId.startsWith(`${baseImporterId}/`)) {
const newImporterId = importerId.substr(baseImporterId.length + 1)
const newImporterId = importerId.slice(baseImporterId.length + 1)
lockfile.importers[newImporterId] = projectSnapshotWithoutLinkedDeps(importer)
continue
}
Expand Down
2 changes: 1 addition & 1 deletion packages/npm-resolver/src/fetch.ts
Expand Up @@ -100,7 +100,7 @@ function toUri (pkgName: string, registry: string) {
let encodedName: string

if (pkgName[0] === '@') {
encodedName = `@${encodeURIComponent(pkgName.substr(1))}`
encodedName = `@${encodeURIComponent(pkgName.slice(1))}`
} else {
encodedName = encodeURIComponent(pkgName)
}
Expand Down
6 changes: 3 additions & 3 deletions packages/npm-resolver/src/parsePref.ts
Expand Up @@ -16,14 +16,14 @@ export default function parsePref (
): RegistryPackageSpec | null {
let name = alias
if (pref.startsWith('npm:')) {
pref = pref.substr(4)
pref = pref.slice(4)
const index = pref.lastIndexOf('@')
if (index < 1) {
name = pref
pref = defaultTag
} else {
name = pref.substr(0, index)
pref = pref.substr(index + 1)
name = pref.slice(0, index)
pref = pref.slice(index + 1)
}
}
if (name) {
Expand Down
2 changes: 1 addition & 1 deletion packages/package-bins/src/index.ts
Expand Up @@ -42,7 +42,7 @@ function commandsFromBin (bin: PackageBin, pkgName: string, pkgPath: string) {
if (typeof bin === 'string') {
return [
{
name: pkgName.startsWith('@') ? pkgName.substr(pkgName.indexOf('/') + 1) : pkgName,
name: pkgName.startsWith('@') ? pkgName.slice(pkgName.indexOf('/') + 1) : pkgName,
path: path.join(pkgPath, bin),
},
]
Expand Down
4 changes: 2 additions & 2 deletions packages/parse-wanted-dependency/src/index.ts
Expand Up @@ -10,11 +10,11 @@ export default function parseWantedDependency (
): Partial<ParsedWantedDependency> & (Omit<ParsedWantedDependency, 'pref'> | Omit<ParsedWantedDependency, 'alias'> | ParsedWantedDependency) {
const versionDelimiter = rawWantedDependency.indexOf('@', 1) // starting from 1 to skip the @ that marks scope
if (versionDelimiter !== -1) {
const alias = rawWantedDependency.substr(0, versionDelimiter)
const alias = rawWantedDependency.slice(0, versionDelimiter)
if (validateNpmPackageName(alias).validForOldPackages) {
return {
alias,
pref: rawWantedDependency.substr(versionDelimiter + 1),
pref: rawWantedDependency.slice(versionDelimiter + 1),
}
}
return {
Expand Down
6 changes: 3 additions & 3 deletions packages/pick-registry-for-package/src/index.ts
Expand Up @@ -7,13 +7,13 @@ export default (registries: Registries, packageName: string, pref?: string) => {

function getScope (pkgName: string, pref?: string): string | null {
if (pref?.startsWith('npm:')) {
pref = pref.substr(4)
pref = pref.slice(4)
if (pref[0] === '@') {
return pref.substr(0, pref.indexOf('/'))
return pref.substring(0, pref.indexOf('/'))
}
}
if (pkgName[0] === '@') {
return pkgName.substr(0, pkgName.indexOf('/'))
return pkgName.substring(0, pkgName.indexOf('/'))
}
return null
}
2 changes: 1 addition & 1 deletion packages/pkgs-graph/src/index.ts
Expand Up @@ -60,7 +60,7 @@ export default function <T> (pkgs: Array<Package & T>, opts?: {
const isWorkspaceSpec = rawSpec.startsWith('workspace:')
try {
if (isWorkspaceSpec) {
rawSpec = rawSpec.substr(10)
rawSpec = rawSpec.slice(10)
if (rawSpec === '^' || rawSpec === '~') {
rawSpec = '*'
};
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-commands-installation/src/recursive.ts
Expand Up @@ -508,8 +508,8 @@ export function createMatcher (params: string[]) {
pattern = param
spec = ''
} else {
pattern = param.substr(0, atIndex)
spec = param.substr(atIndex + 1)
pattern = param.slice(0, atIndex)
spec = param.slice(atIndex + 1)
}
return {
match: matcher(pattern),
Expand Down

0 comments on commit bc35dff

Please sign in to comment.