Skip to content

Commit

Permalink
fix: handle null from pnp resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber committed Dec 22, 2022
1 parent 8c337e5 commit 1b38367
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 5 deletions.
13 changes: 9 additions & 4 deletions src/parse-tsconfig/resolve-extends.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ export function resolveExtends(

const pnpApi = getPnpApi();
if (pnpApi) {
const { resolveRequest } = pnpApi;
const { resolveRequest: resolveWithPnp } = pnpApi;
const [first, second] = filePath.split('/');
const packageName = first.startsWith('@') ? `${first}/${second}` : first;

try {
if (packageName === filePath) {
const packageJsonPath = resolveRequest(
const packageJsonPath = resolveWithPnp(
path.join(packageName, 'package.json'),
directoryPath,
);
Expand All @@ -78,18 +78,23 @@ export function resolveExtends(
}
}
} else {
let resolved: string | null;
try {
return resolveRequest(
resolved = resolveWithPnp(
filePath,
directoryPath,
{ extensions: ['.json'] },
);
} catch {
return resolveRequest(
resolved = resolveWithPnp(
path.join(filePath, 'tsconfig.json'),
directoryPath,
);
}

if (resolved) {
return resolved;
}
}
} catch {}
}
Expand Down
4 changes: 3 additions & 1 deletion tests/fixtures/yarn-pnp/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ const tests = [
() => parseTsconfig('./tsconfig.package-path.json'),
() => parseTsconfig('./tsconfig.package-path-directory.json'),
() => parseTsconfig('./tsconfig.org-package.json'),
() => parseTsconfig('./tsconfig.missing-extends.json'),
() => parseTsconfig('./tsconfig.invalid-extends.json'),
];

for (const test of tests) {
try {
console.log(test());
} catch (error) {
console.log(error.message);
console.log('Error:', error.message);
process.exitCode = 1;
}
}
3 changes: 3 additions & 0 deletions tests/fixtures/yarn-pnp/tsconfig.invalid-extends.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "fs/promises"
}
3 changes: 3 additions & 0 deletions tests/fixtures/yarn-pnp/tsconfig.missing-extends.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "non-existent-package"
}
3 changes: 3 additions & 0 deletions tests/specs/parse-tsconfig/extends/resolves.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,13 +364,16 @@ export default testSuite(({ describe }) => {
const { stdout } = await execaNode('./index.js', [], {
nodeOptions: ['--require', './.pnp.cjs'],
cwd: './tests/fixtures/yarn-pnp',
reject: false,
});

expect(stdout).toBe([
'{ compilerOptions: { strict: true, jsx: \'react\' } }',
'{ compilerOptions: { strict: true, jsx: \'react\' } }',
'{ compilerOptions: { strict: true, jsx: \'react\' } }',
'{ compilerOptions: { strict: true, jsx: \'react\' } }',
'Error: File \'non-existent-package\' not found.',
'Error: File \'fs/promises\' not found.',
].join('\n'));
});
});
Expand Down

0 comments on commit 1b38367

Please sign in to comment.