Skip to content

Commit 81b9d24

Browse files
eps1lonljharb
authored andcommitted
[Fix] no-import-module-exports: Don't crash if packages have no entrypoint
1 parent 20c373c commit 81b9d24

File tree

4 files changed

+21
-2
lines changed

4 files changed

+21
-2
lines changed

CHANGELOG.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
1313
- [`no-cycle`]: ignore imports where imported file only imports types of importing file ([#2083], thanks [@cherryblossom000])
1414
- [`no-cycle`]: fix false negative when file imports a type after importing a value in Flow ([#2083], thanks [@cherryblossom000])
1515
- [`order`]: restore default behavior unless `type` is in groups ([#2087], thanks [@grit96])
16+
- [`no-import-module-exports`]: Don't crash if packages have no entrypoint ([#2099], thanks [@eps1lon])
1617

1718
### Changed
1819
- [Docs] Add `no-relative-packages` to list of to the list of rules ([#2075], thanks [@arvigeus])
@@ -794,6 +795,7 @@ for info on changes for earlier releases.
794795

795796
[`memo-parser`]: ./memo-parser/README.md
796797

798+
[#2099]: https://github.com/benmosher/eslint-plugin-import/pull/2099
797799
[#2090]: https://github.com/benmosher/eslint-plugin-import/pull/2090
798800
[#2087]: https://github.com/benmosher/eslint-plugin-import/pull/2087
799801
[#2083]: https://github.com/benmosher/eslint-plugin-import/pull/2083
@@ -1262,6 +1264,7 @@ for info on changes for earlier releases.
12621264
[@eelyafi]: https://github.com/eelyafi
12631265
[@Ephem]: https://github.com/Ephem
12641266
[@ephys]: https://github.com/ephys
1267+
[@eps1lon]: https://github.com/eps1lon
12651268
[@ernestostifano]: https://github.com/ernestostifano
12661269
[@fa93hws]: https://github.com/fa93hws
12671270
[@fengkfengk]: https://github.com/fengkfengk
@@ -1402,4 +1405,4 @@ for info on changes for earlier releases.
14021405
[@wtgtybhertgeghgtwtg]: https://github.com/wtgtybhertgeghgtwtg
14031406
[@xpl]: https://github.com/xpl
14041407
[@yordis]: https://github.com/yordis
1405-
[@zloirock]: https://github.com/zloirock
1408+
[@zloirock]: https://github.com/zloirock

src/rules/no-import-module-exports.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ import pkgUp from 'pkg-up';
44

55
function getEntryPoint(context) {
66
const pkgPath = pkgUp.sync(context.getFilename());
7-
return require.resolve(path.dirname(pkgPath));
7+
try {
8+
return require.resolve(path.dirname(pkgPath));
9+
} catch (error) {
10+
// Assume the package has no entrypoint (e.g. CLI packages)
11+
// in which case require.resolve would throw.
12+
return null;
13+
}
814
}
915

1016
module.exports = {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"bin": "./cli.js"
3+
}

tests/src/rules/no-import-module-exports.js

+7
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ ruleTester.run('no-import-module-exports', rule, {
5757
filename: path.join(process.cwd(), 'tests/files/some/other/entry-point.js'),
5858
options: [{ exceptions: ['**/*/other/entry-point.js'] }],
5959
}),
60+
test({
61+
code: `
62+
import * as process from 'process';
63+
console.log(process.env);
64+
`,
65+
filename: path.join(process.cwd(), 'tests/files/missing-entrypoint/cli.js'),
66+
}),
6067
],
6168
invalid: [
6269
test({

0 commit comments

Comments
 (0)