Skip to content

Commit 8f49d36

Browse files
author
0z5a
authored
Fix resolver.blockList: [] hiding every file (#2227)
flattenBlockList joined no patterns into new RegExp(''), which matches every path, so an empty blockList array produced an empty file map. Return a pattern that never matches for an empty array.
1 parent 6277af1 commit 8f49d36

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

‎packages/metro/src/DeltaBundler/__tests__/resolver-test.js‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,42 @@ function dep(name: string): TransformResultDependency {
451451
});
452452
});
453453

454+
describe('blockList config param', () => {
455+
test('hides files matching a pattern in a blockList array', async () => {
456+
setMockFileSystem({
457+
'index.js': '',
458+
'a.js': '',
459+
'blocked.js': '',
460+
});
461+
462+
resolver = await createResolver({
463+
resolver: {blockList: [/blocked\.js$/]},
464+
});
465+
466+
expect(resolver.resolve(p('/root/index.js'), dep('./a'))).toEqual({
467+
type: 'sourceFile',
468+
filePath: p('/root/a.js'),
469+
});
470+
expect(() =>
471+
resolver.resolve(p('/root/index.js'), dep('./blocked')),
472+
).toThrow(/Unable to resolve module \.\/blocked/);
473+
});
474+
475+
test('does not hide any file with an empty blockList array', async () => {
476+
setMockFileSystem({
477+
'index.js': '',
478+
'a.js': '',
479+
});
480+
481+
resolver = await createResolver({resolver: {blockList: []}});
482+
483+
expect(resolver.resolve(p('/root/index.js'), dep('./a'))).toEqual({
484+
type: 'sourceFile',
485+
filePath: p('/root/a.js'),
486+
});
487+
});
488+
});
489+
454490
describe('absolute paths', () => {
455491
test('supports requiring absolute paths', async () => {
456492
setMockFileSystem({

‎packages/metro/src/node-haste/DependencyGraph/createFileMap.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ const flattenBlockList = (regexes: ConfigT['resolver']['blockList']) => {
2222
if (!Array.isArray(regexes)) {
2323
return regexes;
2424
}
25+
// An empty list blocks nothing. Joining no patterns would give an empty
26+
// RegExp, which matches every path.
27+
if (regexes.length === 0) {
28+
return /.^/;
29+
}
2530
return new RegExp(
2631
regexes
2732
.map((regex, index) => {

0 commit comments

Comments
 (0)