Skip to content

Commit

Permalink
[Fix] guard against empty parent
Browse files Browse the repository at this point in the history
  • Loading branch information
laurens-dg authored and Laurens De Graeve committed Jul 19, 2023
1 parent 3a5ad34 commit ee00a1c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
- [`consistent-type-specifier-style`]: fix accidental removal of comma in certain cases ([#2754], thanks [@bradzacher])
- [Perf] `ExportMap`: Improve `ExportMap.for` performance on larger codebases ([#2756], thanks [@leipert])
- [`no-extraneous-dependencies`]/TypeScript: do not error when importing inline type from dev dependencies ([#1820], thanks [@andyogo])
- [`newline-after-import`]/TypeScript: do not error when re-exporting a namespaced import ([#2832], thanks [@laurens-dg])
* [`order`]: partial fix for [#2687] (thanks [@ljharb])

### Changed
Expand Down Expand Up @@ -1072,6 +1073,7 @@ for info on changes for earlier releases.

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

[#2832]: https://github.com/import-js/eslint-plugin-import/pull/2832
[#2756]: https://github.com/import-js/eslint-plugin-import/pull/2756
[#2754]: https://github.com/import-js/eslint-plugin-import/pull/2754
[#2748]: https://github.com/import-js/eslint-plugin-import/pull/2748
Expand Down Expand Up @@ -1746,6 +1748,7 @@ for info on changes for earlier releases.
[@KostyaZgara]: https://github.com/KostyaZgara
[@kylemh]: https://github.com/kylemh
[@laysent]: https://github.com/laysent
[@laurens-dg]: https://github.com/laurens-dg
[@le0nik]: https://github.com/le0nik
[@leipert]: https://github.com/leipert
[@lemonmade]: https://github.com/lemonmade
Expand Down
5 changes: 5 additions & 0 deletions src/rules/newline-after-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ module.exports = {

function checkImport(node) {
const { parent } = node;

if (!parent || !parent.body) {
return;
}

const nodePosition = parent.body.indexOf(node);
const nextNode = parent.body[nodePosition + 1];
const endLine = node.loc.end.line;
Expand Down
16 changes: 13 additions & 3 deletions tests/src/rules/newline-after-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
code: `
const x = () => require('baz')
, y = () => require('bar')
// some comment here
`,
parserOptions: { ecmaVersion: 6 },
Expand Down Expand Up @@ -273,6 +273,16 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
parser,
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
},
{
code: `
import { ns } from 'namespace';
import Bar = ns.baz.foo.Bar;
export import Foo = ns.baz.bar.Foo;
`,
parser,
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
},
)),
{
code: `
Expand All @@ -299,7 +309,7 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
{
code: `
import path from 'path';import foo from 'foo';
/**
* some multiline comment here
* another line of comment
Expand All @@ -313,7 +323,7 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
code: `
import path from 'path';
import foo from 'foo';
// Some random single line comment
var bar = 42;
`,
Expand Down

0 comments on commit ee00a1c

Please sign in to comment.