From ee00a1cc702cdfccbb2f10cddcb2797827115974 Mon Sep 17 00:00:00 2001 From: laurens-dg Date: Tue, 18 Jul 2023 15:35:15 +0200 Subject: [PATCH] [Fix] guard against empty parent --- CHANGELOG.md | 3 +++ src/rules/newline-after-import.js | 5 +++++ tests/src/rules/newline-after-import.js | 16 +++++++++++++--- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 68d715678..dcfb192d2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -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 @@ -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 diff --git a/src/rules/newline-after-import.js b/src/rules/newline-after-import.js index c63bb21b2..8855e26e5 100644 --- a/src/rules/newline-after-import.js +++ b/src/rules/newline-after-import.js @@ -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; diff --git a/tests/src/rules/newline-after-import.js b/tests/src/rules/newline-after-import.js index 5e14b570e..9ec6eb757 100644 --- a/tests/src/rules/newline-after-import.js +++ b/tests/src/rules/newline-after-import.js @@ -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 }, @@ -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: ` @@ -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 @@ -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; `,