Skip to content

Commit

Permalink
cleanup(misc): replace deprecated String.prototype.substr()
Browse files Browse the repository at this point in the history
* 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 <rootcommander@gmail.com>

* fix(js): fix slice change

Co-authored-by: Jason Jean <jasonjean1993@gmail.com>
  • Loading branch information
CommanderRoot and FrozenPandaz committed Apr 1, 2022
1 parent f922381 commit 05a9544
Show file tree
Hide file tree
Showing 24 changed files with 32 additions and 34 deletions.
2 changes: 1 addition & 1 deletion 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(
Expand Down
2 changes: 1 addition & 1 deletion packages/devkit/src/utils/names.ts
Expand Up @@ -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);
}
4 changes: 2 additions & 2 deletions packages/devkit/src/utils/string-change.ts
Expand Up @@ -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;
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/js/src/utils/code-frames/highlight.ts
Expand Up @@ -50,7 +50,7 @@ function getTokenType(match) {

if (
JSX_TAG.test(token.value) &&
(text[offset - 1] === '<' || text.substr(offset - 2, 2) == '</')
(text[offset - 1] === '<' || text.slice(offset - 2, 2) == '</')
) {
return 'jsx_tag';
}
Expand Down
4 changes: 1 addition & 3 deletions packages/nest/src/migrations/update-10-0-0/update-10-0-0.ts
Expand Up @@ -71,9 +71,7 @@ function updateImports(host: Tree) {
isStringLiteral(statement.moduleSpecifier)
) {
const nodeText = statement.moduleSpecifier.getText(sourceFile);
const modulePath = statement.moduleSpecifier
.getText(sourceFile)
.substr(1, nodeText.length - 2);
const modulePath = nodeText.slice(1, -1);
if (modulePath === 'type-graphql') {
changes.push(
new ReplaceChange(
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/adapter/decorate-cli.ts
Expand Up @@ -5,7 +5,7 @@ export function decorateCli() {
const angularCLIInit = readFileSync(path, 'utf-8');
const start = angularCLIInit.indexOf(`(options) {`) + 11;

const newContent = `${angularCLIInit.substr(0, start)}
const newContent = `${angularCLIInit.slice(0, start)}
if (!process.env['NX_CLI_SET']) {
require('nx/bin/nx');
return new Promise(function(res, rej) {});
Expand Down
8 changes: 4 additions & 4 deletions packages/nx/src/adapter/ngcli-adapter.ts
Expand Up @@ -134,7 +134,7 @@ async function createRecorder(
const actualConfigName = await host.workspaceConfigName();
return (event: import('@angular-devkit/schematics').DryRunEvent) => {
let eventPath = event.path.startsWith('/')
? event.path.substr(1)
? event.path.slice(1)
: event.path;

if (eventPath === 'workspace.json' || eventPath === 'angular.json') {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -1138,14 +1138,14 @@ const loggerColors: Partial<Record<logging.LogLevel, (s: string) => 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);
Expand Down
4 changes: 2 additions & 2 deletions packages/nx/src/command-line/generate.ts
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/hasher/file-hasher-base.ts
Expand Up @@ -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)) {
Expand Down
Expand Up @@ -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);
}
Expand Down
Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion packages/nx/src/utils/command-line-utils.ts
Expand Up @@ -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[] = [
Expand Down
4 changes: 2 additions & 2 deletions packages/nx/src/utils/logger.ts
Expand Up @@ -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 {
Expand All @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/utils/print-help.ts
Expand Up @@ -8,7 +8,7 @@ function formatOption(
maxPropertyNameLength: number
) {
const lengthOfKey = Math.max(maxPropertyNameLength + 4, 22);
return ` --${`${name} `.substr(
return ` --${`${name} `.slice(
0,
lengthOfKey
)}${description}`;
Expand Down
Expand Up @@ -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('/')
);
Expand Down
2 changes: 1 addition & 1 deletion packages/web/src/executors/rollup/rollup.impl.ts
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion packages/web/src/utils/serve-path.ts
Expand Up @@ -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}`;
Expand Down
2 changes: 1 addition & 1 deletion packages/web/src/utils/web.config.ts
Expand Up @@ -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),
}),
],
});
Expand Down
2 changes: 1 addition & 1 deletion packages/web/src/utils/webpack/partials/styles.ts
Expand Up @@ -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,
Expand Down
Expand Up @@ -70,15 +70,15 @@ 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);
if (cachedUrl) {
return cachedUrl;
}
if (inputUrl.startsWith('~')) {
inputUrl = inputUrl.substr(1);
inputUrl = inputUrl.slice(1);
}
if (inputUrl.startsWith('/')) {
let outputUrl = '';
Expand Down
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions packages/workspace/src/generators/move/lib/update-imports.ts
Expand Up @@ -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,
};
Expand All @@ -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,
};

Expand Down
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/workspace/src/utils/strings.ts
Expand Up @@ -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) {
Expand Down

0 comments on commit 05a9544

Please sign in to comment.