Skip to content

Commit 70d0025

Browse files
refactor: use includes where appropriate (#4412)
* refactor: use includes where appropriate * refactor: replace .substr with .substring * Extract some regular expressions Co-authored-by: Lukas Taegert-Atkinson <[email protected]> Co-authored-by: Lukas Taegert-Atkinson <[email protected]>
1 parent 213e721 commit 70d0025

24 files changed

Lines changed: 75 additions & 76 deletions

File tree

browser/path.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
1-
export const absolutePath = /^(?:\/|(?:[A-Za-z]:)?[\\|/])/;
2-
export const relativePath = /^\.?\.\//;
1+
const ABSOLUTE_PATH_REGEX = /^(?:\/|(?:[A-Za-z]:)?[\\|/])/;
2+
const RELATIVE_PATH_REGEX = /^\.?\.\//;
3+
const ALL_BACKSLASHES_REGEX = /\\/g;
4+
const ANY_SLASH_REGEX = /[/\\]/;
5+
const EXTNAME_REGEX = /\.[^.]+$/;
36

47
export function isAbsolute(path: string): boolean {
5-
return absolutePath.test(path);
8+
return ABSOLUTE_PATH_REGEX.test(path);
69
}
710

811
export function isRelative(path: string): boolean {
9-
return relativePath.test(path);
12+
return RELATIVE_PATH_REGEX.test(path);
1013
}
1114

1215
export function normalize(path: string): string {
13-
return path.replace(/\\/g, '/');
16+
return path.replace(ALL_BACKSLASHES_REGEX, '/');
1417
}
1518

1619
export function basename(path: string): string {
17-
return path.split(/[/\\]/).pop() || '';
20+
return path.split(ANY_SLASH_REGEX).pop() || '';
1821
}
1922

2023
export function dirname(path: string): string {
@@ -28,14 +31,13 @@ export function dirname(path: string): string {
2831
}
2932

3033
export function extname(path: string): string {
31-
const match = /\.[^.]+$/.exec(basename(path)!);
32-
if (!match) return '';
33-
return match[0];
34+
const match = EXTNAME_REGEX.exec(basename(path)!);
35+
return match ? match[0] : '';
3436
}
3537

3638
export function relative(from: string, to: string): string {
37-
const fromParts = from.split(/[/\\]/).filter(Boolean);
38-
const toParts = to.split(/[/\\]/).filter(Boolean);
39+
const fromParts = from.split(ANY_SLASH_REGEX).filter(Boolean);
40+
const toParts = to.split(ANY_SLASH_REGEX).filter(Boolean);
3941

4042
if (fromParts[0] === '.') fromParts.shift();
4143
if (toParts[0] === '.') toParts.shift();
@@ -62,13 +64,13 @@ export function resolve(...paths: string[]): string {
6264
if (!firstPathSegment) {
6365
return '/';
6466
}
65-
let resolvedParts = firstPathSegment.split(/[/\\]/);
67+
let resolvedParts = firstPathSegment.split(ANY_SLASH_REGEX);
6668

6769
for (const path of paths) {
6870
if (isAbsolute(path)) {
69-
resolvedParts = path.split(/[/\\]/);
71+
resolvedParts = path.split(ANY_SLASH_REGEX);
7072
} else {
71-
const parts = path.split(/[/\\]/);
73+
const parts = path.split(ANY_SLASH_REGEX);
7274

7375
while (parts[0] === '.' || parts[0] === '..') {
7476
const part = parts.shift();

cli/run/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ export default async function runRollup(command: Record<string, any>): Promise<v
2626
}
2727

2828
if (inputSource && inputSource.length > 0) {
29-
if (inputSource.some((input: string) => input.indexOf('=') !== -1)) {
29+
if (inputSource.some((input: string) => input.includes('='))) {
3030
command.input = {};
3131
inputSource.forEach((input: string) => {
3232
const equalsIndex = input.indexOf('=');
33-
const value = input.substr(equalsIndex + 1);
34-
let key = input.substr(0, equalsIndex);
35-
if (!key) key = getAliasName(input);
33+
const value = input.substring(equalsIndex + 1);
34+
const key = input.substring(0, equalsIndex) || getAliasName(input);
35+
3636
command.input[key] = value;
3737
});
3838
} else {

docs/05-plugin-development.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,7 @@ export default function addProxyPlugin() {
788788
if (resolution && !resolution.external) {
789789
// we pass on the entire resolution information
790790
const moduleInfo = await this.load(resolution);
791-
if (moduleInfo.code.indexOf('/* use proxy */') >= 0) {
791+
if (moduleInfo.code.includes('/* use proxy */')) {
792792
return `${resolution.id}?proxy`;
793793
}
794794
}

src/Chunk.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ export default class Chunk {
605605
const source = module.render(renderOptions).trim();
606606
renderedLength = source.length();
607607
if (renderedLength) {
608-
if (options.compact && source.lastLine().indexOf('//') !== -1) source.append('\n');
608+
if (options.compact && source.lastLine().includes('//')) source.append('\n');
609609
this.renderedModuleSources.set(module, source);
610610
magicString.addSource(source);
611611
this.usedModules.push(module);

src/finalisers/iife.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export default function iife(
3838
}: NormalizedOutputOptions
3939
): Bundle {
4040
const { _, cnst, getNonArrowFunctionIntro, getPropertyAccess, n } = snippets;
41-
const isNamespaced = name && name.indexOf('.') !== -1;
41+
const isNamespaced = name && name.includes('.');
4242
const useVariableAssignment = !extend && !isNamespaced;
4343

4444
if (name && useVariableAssignment && !isLegal(name)) {

src/utils/getExportMode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export default function getExportMode(
3333
}
3434
exportMode = 'default';
3535
} else {
36-
if (format !== 'es' && format !== 'system' && exportKeys.indexOf('default') !== -1) {
36+
if (format !== 'es' && format !== 'system' && exportKeys.includes('default')) {
3737
warn(errMixedExport(facadeModuleId, name));
3838
}
3939
exportMode = 'named';

src/utils/options/mergeOptions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ function getCommandOptions(rawCommandOptions: GenericConfigObject): CommandConfi
8787
? rawCommandOptions.globals.split(',').reduce((globals, globalDefinition) => {
8888
const [id, variableName] = globalDefinition.split(':');
8989
globals[id] = variableName;
90-
if (external.indexOf(id) === -1) {
90+
if (!external.includes(id)) {
9191
external.push(id);
9292
}
9393
return globals;
@@ -156,7 +156,7 @@ const getExternal = (
156156
const configExternal = config.external as ExternalOption | undefined;
157157
return typeof configExternal === 'function'
158158
? (source: string, importer: string | undefined, isResolved: boolean) =>
159-
configExternal(source, importer, isResolved) || overrides.external.indexOf(source) !== -1
159+
configExternal(source, importer, isResolved) || overrides.external.includes(source)
160160
: ensureArray(configExternal).concat(overrides.external);
161161
};
162162

src/utils/path.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
const absolutePath = /^(?:\/|(?:[A-Za-z]:)?[\\|/])/;
2-
const relativePath = /^\.?\.\//;
1+
const ABSOLUTE_PATH_REGEX = /^(?:\/|(?:[A-Za-z]:)?[\\|/])/;
2+
const RELATIVE_PATH_REGEX = /^\.?\.\//;
33

44
export function isAbsolute(path: string): boolean {
5-
return absolutePath.test(path);
5+
return ABSOLUTE_PATH_REGEX.test(path);
66
}
77

88
export function isRelative(path: string): boolean {
9-
return relativePath.test(path);
9+
return RELATIVE_PATH_REGEX.test(path);
1010
}
1111

12+
const BACKSLASH_REGEX = /\\/g;
13+
1214
export function normalize(path: string): string {
13-
if (path.indexOf('\\') == -1) return path;
14-
return path.replace(/\\/g, '/');
15+
return path.replace(BACKSLASH_REGEX, '/');
1516
}
1617

1718
export { basename, dirname, extname, relative, resolve } from 'path';

src/watch/watch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ export class Task {
163163
this.invalidated = true;
164164
if (details.isTransformDependency) {
165165
for (const module of this.cache.modules) {
166-
if (module.transformDependencies.indexOf(id) === -1) continue;
166+
if (!module.transformDependencies.includes(id)) continue;
167167
// effective invalidation
168168
module.originalCode = null as never;
169169
}

test/browser/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ runTestSuiteWithSamples('browser', resolve(__dirname, 'samples'), (dir, config)
1717
bundle = await rollup({
1818
input: 'main',
1919
onwarn: warning => {
20-
if (!(config.expectedWarnings && config.expectedWarnings.indexOf(warning.code) >= 0)) {
20+
if (!(config.expectedWarnings && config.expectedWarnings.includes(warning.code))) {
2121
throw new Error(
2222
`Unexpected warnings (${warning.code}): ${warning.message}\n` +
2323
'If you expect warnings, list their codes in config.expectedWarnings'

0 commit comments

Comments
 (0)