Skip to content

Commit

Permalink
fix(metro-resolver): package exports restricted paths (#1239)
Browse files Browse the repository at this point in the history
Summary:
This PR deals with restricting internal paths through the most specific subpath pattern edge case from #1236

- [x] - added test for implicit default condition to match Node.js resolution algorithm
- [x] - aligned implementation of `reduceConditionalExport` to account for implicit default condition

Changelog: [Experimental] Fix implicit default condition to be null for subpath patterns (edge case)

Pull Request resolved: #1239

Test Plan: - [x] - all tests pass

Reviewed By: EdmondChuiHW

Differential Revision: D55244268

Pulled By: huntie

fbshipit-source-id: e9cddbad2709a90a0282385a1b59ebc03735f975
  • Loading branch information
jbroma authored and facebook-github-bot committed Mar 27, 2024
1 parent f76924f commit ccf6256
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
11 changes: 10 additions & 1 deletion packages/metro-resolver/src/PackageExportsResolve.js
Expand Up @@ -373,7 +373,16 @@ function reduceConditionalExport(
let reducedValue = subpathValue;

while (reducedValue != null && typeof reducedValue !== 'string') {
let match: typeof subpathValue | 'no-match' = 'no-match';
let match: typeof subpathValue | 'no-match';

// when conditions are present and default is not specified
// the default condition is implicitly set to null, to allow
// for restricting access to unexported internals of a package.
if ('default' in reducedValue) {
match = 'no-match';
} else {
match = null;
}

for (const conditionName in reducedValue) {
if (conditionNames.has(conditionName)) {
Expand Down
12 changes: 7 additions & 5 deletions packages/metro-resolver/src/__tests__/package-exports-test.js
Expand Up @@ -539,10 +539,6 @@ describe('with package exports resolution enabled', () => {
'test-pkg/features/foo.js.js',
'/root/node_modules/test-pkg/src/features/foo.js.js',
],
[
'test-pkg/features/bar/Bar.js',
'/root/node_modules/test-pkg/src/features/bar/Bar.js',
],
]) {
expect(Resolver.resolve(baseContext, importSpecifier, null)).toEqual({
type: 'sourceFile',
Expand All @@ -558,7 +554,13 @@ describe('with package exports resolution enabled', () => {
).toThrowError();
});

test('should use most specific pattern base', () => {
test('should use the most specific pattern base - implicit default condition', () => {
expect(() =>
Resolver.resolve(baseContext, 'test-pkg/features/bar/Bar.js', null),
).toThrowError();
});

test('should use most specific pattern base - custom condition', () => {
const context = {
...baseContext,
unstable_conditionNames: ['react-native'],
Expand Down

0 comments on commit ccf6256

Please sign in to comment.