Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] guard against empty parent #2832

Merged
merged 1 commit into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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