Skip to content

Commit

Permalink
Replace regex String.replace uses with String.replaceAll (drop Node.j…
Browse files Browse the repository at this point in the history
…s 14 compat)

Summary:
Since dropping Node.js 14 support, we can now use the `String.replaceAll` API, which gives better performance and readability.

Bulk applied to single-line path separator replacements only.

Changelog: [Internal]

Reviewed By: robhogan

Differential Revision: D43464208

fbshipit-source-id: a382d41e0a8757d96320c39898c98709f47912f6
  • Loading branch information
huntie authored and facebook-github-bot committed Mar 2, 2023
1 parent b80d9a0 commit ad31b16
Show file tree
Hide file tree
Showing 9 changed files with 14 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/metro-config/src/defaults/exclusionList.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function escapeRegExp(pattern) {
var escaped = pattern.replace(/[\-\[\]\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&');
// convert the '/' into an escaped local file separator. The separator needs
// to be escaped in the regular expression source string, hence the '\\' prefix.
return escaped.replace(/\//g, '\\' + path.sep);
return escaped.replaceAll('/', '\\' + path.sep);
} else {
throw new Error('Unexpected exclusion pattern: ' + pattern);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/metro-file-map/src/__tests__/FileSystem-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe.each([['win32'], ['posix']])(
// to system separators
const p: string => string = filePath =>
platform === 'win32'
? filePath.replace(/\//g, '\\').replace(/^\\/, 'C:\\')
? filePath.replaceAll('/', '\\').replace(/^\\/, 'C:\\')
: filePath;

describe.each([['TreeFS'], ['HasteFS']])('%s', label => {
Expand Down
10 changes: 6 additions & 4 deletions packages/metro-file-map/src/__tests__/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ jest.mock('../crawlers/watchman', () =>

for (const file in list) {
if (
new RegExp(roots.join('|').replace(/\\/g, '\\\\')).test(file) &&
new RegExp(roots.join('|').replaceAll('\\', '\\\\')).test(file) &&
!ignore(file)
) {
const relativeFilePath = path.relative(rootDir, file);
Expand Down Expand Up @@ -770,7 +770,7 @@ describe('HasteMap', () => {
}).build();
} catch {
expect(
console.error.mock.calls[0][0].replace(/\\/g, '/'),
console.error.mock.calls[0][0].replaceAll('\\', '/'),
).toMatchSnapshot();
}
});
Expand All @@ -788,7 +788,9 @@ describe('HasteMap', () => {
cacheContent.map.get('Strawberry')[H.GENERIC_PLATFORM],
).not.toBeDefined();

expect(console.warn.mock.calls[0][0].replace(/\\/g, '/')).toMatchSnapshot();
expect(
console.warn.mock.calls[0][0].replaceAll('\\', '/'),
).toMatchSnapshot();
});

it('warns on duplicate module ids only once', async () => {
Expand Down Expand Up @@ -1952,7 +1954,7 @@ describe('HasteMap', () => {
H.MODULE,
}),
);
expect(error.message.replace(/\\/g, '/')).toMatchSnapshot();
expect(error.message.replaceAll('\\', '/')).toMatchSnapshot();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jest.mock('fb-watchman', () => {
return {Client};
});

const forcePOSIXPaths = path => path.replace(/\\/g, '/');
const forcePOSIXPaths = path => path.replaceAll('\\', '/');
const pearMatcher = path => /pear/.test(path);

let watchman;
Expand Down
2 changes: 1 addition & 1 deletion packages/metro-file-map/src/getMockName.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const getMockName = (filePath: string): string => {
const mockPath = filePath.split(MOCKS_PATTERN)[1];
return mockPath
.substring(0, mockPath.lastIndexOf(path.extname(mockPath)))
.replace(/\\/g, '/');
.replaceAll('\\', '/');
};

export default getMockName;
2 changes: 1 addition & 1 deletion packages/metro-file-map/src/lib/TreeFS.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export default class TreeFS implements MutableFileSystem {
const relativePath =
path.sep === '/'
? relativePosixPath
: relativePosixPath.replace(/\//g, path.sep);
: relativePosixPath.replaceAll('/', path.sep);

files.push(contextRootAbsolutePath + path.sep + relativePath);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/metro/src/Assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ async function getAssetData(

// On Windows, change backslashes to slashes to get proper URL path from file path.
if (path.sep === '\\') {
assetUrlPath = assetUrlPath.replace(/\\/g, '/');
assetUrlPath = assetUrlPath.replaceAll('\\', '/');
}

const isImage = isAssetTypeAnImage(path.extname(assetPath).slice(1));
Expand Down
2 changes: 1 addition & 1 deletion packages/metro/src/lib/contextModuleTemplates.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function createFileMap(
let filePath = path.relative(modulePath, file);

if (os.platform() === 'win32') {
filePath = filePath.replace(/\\/g, '/');
filePath = filePath.replaceAll('\\', '/');
}

// NOTE(EvanBacon): I'd prefer we prevent the ability for a module to require itself (`require.context('./')`)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function getIgnorePattern(config: ConfigT): RegExp {
const combine = (regexes: Array<RegExp>) =>
new RegExp(
regexes
.map(regex => '(' + regex.source.replace(/\//g, path.sep) + ')')
.map(regex => '(' + regex.source.replaceAll('/', path.sep) + ')')
.join('|'),
);

Expand Down

0 comments on commit ad31b16

Please sign in to comment.