From 05a9544806e8573bac5eef542a8e8c1b6115dc18 Mon Sep 17 00:00:00 2001 From: CommanderRoot Date: Sat, 2 Apr 2022 01:04:26 +0200 Subject: [PATCH] cleanup(misc): replace deprecated String.prototype.substr() * cleanup(misc): replace deprecated String.prototype.substr() .substr() is deprecated so we replace it with .slice() which works similarily but isn't deprecated Signed-off-by: Tobias Speicher * fix(js): fix slice change Co-authored-by: Jason Jean --- packages/cypress/src/utils/project-name.ts | 2 +- packages/devkit/src/utils/names.ts | 2 +- packages/devkit/src/utils/string-change.ts | 4 ++-- packages/js/src/utils/code-frames/highlight.ts | 2 +- .../nest/src/migrations/update-10-0-0/update-10-0-0.ts | 4 +--- packages/nx/src/adapter/decorate-cli.ts | 2 +- packages/nx/src/adapter/ngcli-adapter.ts | 8 ++++---- packages/nx/src/command-line/generate.ts | 4 ++-- packages/nx/src/hasher/file-hasher-base.ts | 2 +- .../build-dependencies/implicit-project-dependencies.ts | 2 +- .../build-dependencies/typescript-import-locator.ts | 2 +- packages/nx/src/utils/command-line-utils.ts | 2 +- packages/nx/src/utils/logger.ts | 4 ++-- packages/nx/src/utils/print-help.ts | 2 +- .../migrate-stories-to-6-2/migrate-stories-to-6-2.ts | 2 +- packages/web/src/executors/rollup/rollup.impl.ts | 2 +- packages/web/src/utils/serve-path.ts | 2 +- packages/web/src/utils/web.config.ts | 2 +- packages/web/src/utils/webpack/partials/styles.ts | 2 +- .../src/utils/webpack/plugins/postcss-cli-resources.ts | 4 ++-- .../workspace/src/generators/move/lib/normalize-schema.ts | 2 +- .../workspace/src/generators/move/lib/update-imports.ts | 4 ++-- .../src/generators/remove/lib/update-tsconfig.ts | 2 +- packages/workspace/src/utils/strings.ts | 2 +- 24 files changed, 32 insertions(+), 34 deletions(-) diff --git a/packages/cypress/src/utils/project-name.ts b/packages/cypress/src/utils/project-name.ts index 18e39b1f24e2b..11ce9f47faced 100644 --- a/packages/cypress/src/utils/project-name.ts +++ b/packages/cypress/src/utils/project-name.ts @@ -1,7 +1,7 @@ import { names } from '@nrwl/devkit'; export function getUnscopedLibName(libRoot: string) { - return libRoot.substr(libRoot.lastIndexOf('/') + 1); + return libRoot.slice(libRoot.lastIndexOf('/') + 1); } export function getE2eProjectName( diff --git a/packages/devkit/src/utils/names.ts b/packages/devkit/src/utils/names.ts index 2648f5db45f96..5498319c36ca4 100644 --- a/packages/devkit/src/utils/names.ts +++ b/packages/devkit/src/utils/names.ts @@ -65,5 +65,5 @@ function toFileName(s: string): string { * Capitalizes the first letter of a string */ function toCapitalCase(s: string): string { - return s.charAt(0).toUpperCase() + s.substr(1); + return s.charAt(0).toUpperCase() + s.slice(1); } diff --git a/packages/devkit/src/utils/string-change.ts b/packages/devkit/src/utils/string-change.ts index 7236c369b60ce..406bb4be16048 100644 --- a/packages/devkit/src/utils/string-change.ts +++ b/packages/devkit/src/utils/string-change.ts @@ -84,10 +84,10 @@ export function applyChangesToString( for (const change of sortedChanges) { const index = getChangeIndex(change) + offset; if (isStringInsertion(change)) { - text = text.substr(0, index) + change.text + text.substr(index); + text = text.slice(0, index) + change.text + text.slice(index); offset += change.text.length; } else { - text = text.substr(0, index) + text.substr(index + change.length); + text = text.slice(0, index) + text.slice(index + change.length); offset -= change.length; } } diff --git a/packages/js/src/utils/code-frames/highlight.ts b/packages/js/src/utils/code-frames/highlight.ts index 243a170d552fe..bcd67a63b1141 100644 --- a/packages/js/src/utils/code-frames/highlight.ts +++ b/packages/js/src/utils/code-frames/highlight.ts @@ -50,7 +50,7 @@ function getTokenType(match) { if ( JSX_TAG.test(token.value) && - (text[offset - 1] === '<' || text.substr(offset - 2, 2) == ' { let eventPath = event.path.startsWith('/') - ? event.path.substr(1) + ? event.path.slice(1) : event.path; if (eventPath === 'workspace.json' || eventPath === 'angular.json') { @@ -1036,7 +1036,7 @@ export function wrapAngularDevkitSchematic( event: import('@angular-devkit/schematics').DryRunEvent ) => { let eventPath = event.path.startsWith('/') - ? event.path.substr(1) + ? event.path.slice(1) : event.path; const r = convertEventTypeToHandleMultipleConfigNames( @@ -1138,14 +1138,14 @@ const loggerColors: Partial string>> = { warn: (s) => chalk.bold(chalk.yellow(s)), error: (s) => { if (s.startsWith('NX ')) { - return `\n${NX_ERROR} ${chalk.bold(chalk.red(s.substr(3)))}\n`; + return `\n${NX_ERROR} ${chalk.bold(chalk.red(s.slice(3)))}\n`; } return chalk.bold(chalk.red(s)); }, info: (s) => { if (s.startsWith('NX ')) { - return `\n${NX_PREFIX} ${chalk.bold(s.substr(3))}\n`; + return `\n${NX_PREFIX} ${chalk.bold(s.slice(3))}\n`; } return chalk.white(s); diff --git a/packages/nx/src/command-line/generate.ts b/packages/nx/src/command-line/generate.ts index af655e09cfb0f..683c4bad240be 100644 --- a/packages/nx/src/command-line/generate.ts +++ b/packages/nx/src/command-line/generate.ts @@ -47,8 +47,8 @@ function convertToGenerateOptions( const separatorIndex = generatorDescriptor.lastIndexOf(':'); if (separatorIndex > 0) { - collectionName = generatorDescriptor.substr(0, separatorIndex); - generatorName = generatorDescriptor.substr(separatorIndex + 1); + collectionName = generatorDescriptor.slice(0, separatorIndex); + generatorName = generatorDescriptor.slice(separatorIndex + 1); } else { collectionName = defaultCollectionName; generatorName = generatorDescriptor; diff --git a/packages/nx/src/hasher/file-hasher-base.ts b/packages/nx/src/hasher/file-hasher-base.ts index 5c1a096841989..d95603a879ed2 100644 --- a/packages/nx/src/hasher/file-hasher-base.ts +++ b/packages/nx/src/hasher/file-hasher-base.ts @@ -63,7 +63,7 @@ export abstract class FileHasherBase { } const relativePath = normalizePath( path.startsWith(workspaceRoot) - ? path.substring(workspaceRoot.length + 1) + ? path.slice(workspaceRoot.length + 1) : path ); if (this.fileHashes.has(relativePath)) { diff --git a/packages/nx/src/project-graph/build-dependencies/implicit-project-dependencies.ts b/packages/nx/src/project-graph/build-dependencies/implicit-project-dependencies.ts index b8c9988444cf4..97e6409d725e5 100644 --- a/packages/nx/src/project-graph/build-dependencies/implicit-project-dependencies.ts +++ b/packages/nx/src/project-graph/build-dependencies/implicit-project-dependencies.ts @@ -10,7 +10,7 @@ export function buildImplicitProjectDependencies( if (p.implicitDependencies && p.implicitDependencies.length > 0) { p.implicitDependencies.forEach((target) => { if (target.startsWith('!')) { - builder.removeDependency(source, target.substr(1)); + builder.removeDependency(source, target.slice(1)); } else { builder.addImplicitDependency(source, target); } diff --git a/packages/nx/src/project-graph/build-dependencies/typescript-import-locator.ts b/packages/nx/src/project-graph/build-dependencies/typescript-import-locator.ts index 1e608f1e9bbeb..79eeb419aa3ac 100644 --- a/packages/nx/src/project-graph/build-dependencies/typescript-import-locator.ts +++ b/packages/nx/src/project-graph/build-dependencies/typescript-import-locator.ts @@ -156,6 +156,6 @@ export class TypeScriptImportLocator { } private getStringLiteralValue(node: ts.Node): string { - return node.getText().substr(1, node.getText().length - 2); + return node.getText().slice(1, -1); } } diff --git a/packages/nx/src/utils/command-line-utils.ts b/packages/nx/src/utils/command-line-utils.ts index 5c1ac8881b223..8a46cf32061b2 100644 --- a/packages/nx/src/utils/command-line-utils.ts +++ b/packages/nx/src/utils/command-line-utils.ts @@ -65,7 +65,7 @@ function toFileName(s: string): string { * Capitalizes the first letter of a string */ function toCapitalCase(s: string): string { - return s.charAt(0).toUpperCase() + s.substr(1); + return s.charAt(0).toUpperCase() + s.slice(1); } const runOne: string[] = [ diff --git a/packages/nx/src/utils/logger.ts b/packages/nx/src/utils/logger.ts index ba6e18dbc3ba3..16ea72492071c 100644 --- a/packages/nx/src/utils/logger.ts +++ b/packages/nx/src/utils/logger.ts @@ -10,7 +10,7 @@ export const logger = { warn: (s) => console.warn(chalk.bold(chalk.yellow(s))), error: (s) => { if (typeof s === 'string' && s.startsWith('NX ')) { - console.error(`\n${NX_ERROR} ${chalk.bold(chalk.red(s.substr(3)))}\n`); + console.error(`\n${NX_ERROR} ${chalk.bold(chalk.red(s.slice(3)))}\n`); } else if (s instanceof Error && s.stack) { console.error(chalk.bold(chalk.red(s.stack))); } else { @@ -19,7 +19,7 @@ export const logger = { }, info: (s) => { if (typeof s === 'string' && s.startsWith('NX ')) { - console.info(`\n${NX_PREFIX} ${chalk.bold(s.substr(3))}\n`); + console.info(`\n${NX_PREFIX} ${chalk.bold(s.slice(3))}\n`); } else { console.info(s); } diff --git a/packages/nx/src/utils/print-help.ts b/packages/nx/src/utils/print-help.ts index 8ff7d273ddffc..8bbbe1802b4a8 100644 --- a/packages/nx/src/utils/print-help.ts +++ b/packages/nx/src/utils/print-help.ts @@ -8,7 +8,7 @@ function formatOption( maxPropertyNameLength: number ) { const lengthOfKey = Math.max(maxPropertyNameLength + 4, 22); - return ` --${`${name} `.substr( + return ` --${`${name} `.slice( 0, lengthOfKey )}${description}`; diff --git a/packages/storybook/src/generators/migrate-stories-to-6-2/migrate-stories-to-6-2.ts b/packages/storybook/src/generators/migrate-stories-to-6-2/migrate-stories-to-6-2.ts index 5da923fcf6bfd..4d1d9f77242cf 100644 --- a/packages/storybook/src/generators/migrate-stories-to-6-2/migrate-stories-to-6-2.ts +++ b/packages/storybook/src/generators/migrate-stories-to-6-2/migrate-stories-to-6-2.ts @@ -167,7 +167,7 @@ function findAllComponentsWithStoriesForSpecificProject( const imports = file.statements?.filter( (statement) => statement.kind === SyntaxKind.ImportDeclaration ); - const modulePath = moduleFilePath.substr( + const modulePath = moduleFilePath.slice( 0, moduleFilePath?.lastIndexOf('/') ); diff --git a/packages/web/src/executors/rollup/rollup.impl.ts b/packages/web/src/executors/rollup/rollup.impl.ts index fe386b09bbe52..cc98fb42e1811 100644 --- a/packages/web/src/executors/rollup/rollup.impl.ts +++ b/packages/web/src/executors/rollup/rollup.impl.ts @@ -63,7 +63,7 @@ export default async function* rollupExecutor( const npmDeps = (projectGraph.dependencies[context.projectName] ?? []) .filter((d) => d.target.startsWith('npm:')) - .map((d) => d.target.substr(4)); + .map((d) => d.target.slice(4)); const rollupOptions = createRollupOptions( options, diff --git a/packages/web/src/utils/serve-path.ts b/packages/web/src/utils/serve-path.ts index 6565069f7ec84..8757646b07e6c 100644 --- a/packages/web/src/utils/serve-path.ts +++ b/packages/web/src/utils/serve-path.ts @@ -5,7 +5,7 @@ export function buildServePath(browserOptions: WebWebpackExecutorOptions) { _findDefaultServePath(browserOptions.baseHref, browserOptions.deployUrl) || '/'; if (servePath.endsWith('/')) { - servePath = servePath.substr(0, servePath.length - 1); + servePath = servePath.slice(0, -1); } if (!servePath.startsWith('/')) { servePath = `/${servePath}`; diff --git a/packages/web/src/utils/web.config.ts b/packages/web/src/utils/web.config.ts index 1570dfcf35177..898bda129a586 100644 --- a/packages/web/src/utils/web.config.ts +++ b/packages/web/src/utils/web.config.ts @@ -153,7 +153,7 @@ export function getStylesPartial( plugins: [ postcssImports({ addModulesDirectories: includePaths, - resolve: (url: string) => (url.startsWith('~') ? url.substr(1) : url), + resolve: (url: string) => (url.startsWith('~') ? url.slice(1) : url), }), ], }); diff --git a/packages/web/src/utils/webpack/partials/styles.ts b/packages/web/src/utils/webpack/partials/styles.ts index 10125c83fc1e6..384f770129ad4 100644 --- a/packages/web/src/utils/webpack/partials/styles.ts +++ b/packages/web/src/utils/webpack/partials/styles.ts @@ -39,7 +39,7 @@ export function getStylesConfig( plugins: [ postcssImports({ addModulesDirectories: includePaths, - resolve: (url: string) => (url.startsWith('~') ? url.substr(1) : url), + resolve: (url: string) => (url.startsWith('~') ? url.slice(1) : url), }), PostcssCliResources({ baseHref: buildOptions.baseHref, diff --git a/packages/web/src/utils/webpack/plugins/postcss-cli-resources.ts b/packages/web/src/utils/webpack/plugins/postcss-cli-resources.ts index 4ea14af6b6980..751440931181c 100644 --- a/packages/web/src/utils/webpack/plugins/postcss-cli-resources.ts +++ b/packages/web/src/utils/webpack/plugins/postcss-cli-resources.ts @@ -70,7 +70,7 @@ export function PostcssCliResources(options: PostcssCliResourcesOptions) { // If starts with a caret, remove and return remainder // this supports bypassing asset processing if (inputUrl.startsWith('^')) { - return inputUrl.substr(1); + return inputUrl.slice(1); } const cacheKey = path.resolve(context, inputUrl); const cachedUrl = resourceCache.get(cacheKey); @@ -78,7 +78,7 @@ export function PostcssCliResources(options: PostcssCliResourcesOptions) { return cachedUrl; } if (inputUrl.startsWith('~')) { - inputUrl = inputUrl.substr(1); + inputUrl = inputUrl.slice(1); } if (inputUrl.startsWith('/')) { let outputUrl = ''; diff --git a/packages/workspace/src/generators/move/lib/normalize-schema.ts b/packages/workspace/src/generators/move/lib/normalize-schema.ts index 85be123df070f..96bf046efe3a4 100644 --- a/packages/workspace/src/generators/move/lib/normalize-schema.ts +++ b/packages/workspace/src/generators/move/lib/normalize-schema.ts @@ -9,7 +9,7 @@ export function normalizeSchema( projectConfiguration: ProjectConfiguration ): NormalizedSchema { const destination = schema.destination.startsWith('/') - ? normalizeSlashes(schema.destination.substr(1)) + ? normalizeSlashes(schema.destination.slice(1)) : schema.destination; const newProjectName = getNewProjectName(destination); const { npmScope } = getWorkspaceLayout(tree); diff --git a/packages/workspace/src/generators/move/lib/update-imports.ts b/packages/workspace/src/generators/move/lib/update-imports.ts index f9cdf7055fd5f..1c96dfc1f8fcf 100644 --- a/packages/workspace/src/generators/move/lib/update-imports.ts +++ b/packages/workspace/src/generators/move/lib/update-imports.ts @@ -51,7 +51,7 @@ export function updateImports( from: fromPath || normalizeSlashes( - `@${npmScope}/${project.root.substr(libsDir.length + 1)}` + `@${npmScope}/${project.root.slice(libsDir.length + 1)}` ), to: schema.importPath, }; @@ -77,7 +77,7 @@ export function updateImports( } const projectRoot = { - from: project.root.substr(libsDir.length + 1), + from: project.root.slice(libsDir.length + 1), to: schema.destination, }; diff --git a/packages/workspace/src/generators/remove/lib/update-tsconfig.ts b/packages/workspace/src/generators/remove/lib/update-tsconfig.ts index a3f8145e7a1cf..84dbbe1d22751 100644 --- a/packages/workspace/src/generators/remove/lib/update-tsconfig.ts +++ b/packages/workspace/src/generators/remove/lib/update-tsconfig.ts @@ -20,7 +20,7 @@ export function updateTsconfig( const { appsDir, libsDir, npmScope } = getWorkspaceLayout(tree); const tsConfigPath = getRootTsConfigPathInTree(tree); - const defaultImportPath = `@${npmScope}/${project.root.substr( + const defaultImportPath = `@${npmScope}/${project.root.slice( project.projectType === 'application' ? appsDir.length + 1 : libsDir.length + 1 diff --git a/packages/workspace/src/utils/strings.ts b/packages/workspace/src/utils/strings.ts index f6a9beb7bc082..140fe1c3e80d4 100644 --- a/packages/workspace/src/utils/strings.ts +++ b/packages/workspace/src/utils/strings.ts @@ -132,7 +132,7 @@ export function underscore(str: string): string { @return {String} The capitalized string. */ export function capitalize(str: string): string { - return str.charAt(0).toUpperCase() + str.substr(1); + return str.charAt(0).toUpperCase() + str.slice(1); } export function group(name: string, group: string | undefined) {