Skip to content

Commit

Permalink
[Fix] no-import-module-exports: Don't crash if packages have no ent…
Browse files Browse the repository at this point in the history
…rypoint
  • Loading branch information
eps1lon authored and ljharb committed May 26, 2021
1 parent 20c373c commit 81b9d24
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Expand Up @@ -13,6 +13,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
- [`no-cycle`]: ignore imports where imported file only imports types of importing file ([#2083], thanks [@cherryblossom000])
- [`no-cycle`]: fix false negative when file imports a type after importing a value in Flow ([#2083], thanks [@cherryblossom000])
- [`order`]: restore default behavior unless `type` is in groups ([#2087], thanks [@grit96])
- [`no-import-module-exports`]: Don't crash if packages have no entrypoint ([#2099], thanks [@eps1lon])

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

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

[#2099]: https://github.com/benmosher/eslint-plugin-import/pull/2099
[#2090]: https://github.com/benmosher/eslint-plugin-import/pull/2090
[#2087]: https://github.com/benmosher/eslint-plugin-import/pull/2087
[#2083]: https://github.com/benmosher/eslint-plugin-import/pull/2083
Expand Down Expand Up @@ -1262,6 +1264,7 @@ for info on changes for earlier releases.
[@eelyafi]: https://github.com/eelyafi
[@Ephem]: https://github.com/Ephem
[@ephys]: https://github.com/ephys
[@eps1lon]: https://github.com/eps1lon
[@ernestostifano]: https://github.com/ernestostifano
[@fa93hws]: https://github.com/fa93hws
[@fengkfengk]: https://github.com/fengkfengk
Expand Down Expand Up @@ -1402,4 +1405,4 @@ for info on changes for earlier releases.
[@wtgtybhertgeghgtwtg]: https://github.com/wtgtybhertgeghgtwtg
[@xpl]: https://github.com/xpl
[@yordis]: https://github.com/yordis
[@zloirock]: https://github.com/zloirock
[@zloirock]: https://github.com/zloirock
8 changes: 7 additions & 1 deletion src/rules/no-import-module-exports.js
Expand Up @@ -4,7 +4,13 @@ import pkgUp from 'pkg-up';

function getEntryPoint(context) {
const pkgPath = pkgUp.sync(context.getFilename());
return require.resolve(path.dirname(pkgPath));
try {
return require.resolve(path.dirname(pkgPath));
} catch (error) {
// Assume the package has no entrypoint (e.g. CLI packages)
// in which case require.resolve would throw.
return null;
}
}

module.exports = {
Expand Down
3 changes: 3 additions & 0 deletions tests/files/missing-entrypoint/package.json
@@ -0,0 +1,3 @@
{
"bin": "./cli.js"
}
7 changes: 7 additions & 0 deletions tests/src/rules/no-import-module-exports.js
Expand Up @@ -57,6 +57,13 @@ ruleTester.run('no-import-module-exports', rule, {
filename: path.join(process.cwd(), 'tests/files/some/other/entry-point.js'),
options: [{ exceptions: ['**/*/other/entry-point.js'] }],
}),
test({
code: `
import * as process from 'process';
console.log(process.env);
`,
filename: path.join(process.cwd(), 'tests/files/missing-entrypoint/cli.js'),
}),
],
invalid: [
test({
Expand Down

0 comments on commit 81b9d24

Please sign in to comment.