Skip to content

Commit ba23743

Browse files
committed
Fix crash with newline-after-import related to the use of switch cases (fixes #386)
1 parent a66e7c7 commit ba23743

3 files changed

Lines changed: 21 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
88
- Added new rule [`no-restricted-paths`]. ([#155]/[#371], thanks [@lo1tuma])
99
- [`import/core-modules` setting]: allow configuration of additional module names,
1010
to be treated as builtin modules (a la `path`, etc. in Node). ([#275] + [#365], thanks [@sindresorhus] for driving)
11+
### Fixed
12+
- Fixed crash with `newline-after-import` related to the use of switch cases. (fixes [#386], thanks [@ljharb] for reporting) ([#395])
1113

1214
## [1.9.2] - 2016-06-21
1315
### Fixed
@@ -231,6 +233,7 @@ for info on changes for earlier releases.
231233
[`prefer-default-export`]: ./docs/rules/prefer-default-export.md
232234
[`no-restricted-paths`]: ./docs/rules/no-restricted-paths.md
233235

236+
[#395]: https://github.com/benmosher/eslint-plugin-import/pull/395
234237
[#371]: https://github.com/benmosher/eslint-plugin-import/pull/371
235238
[#365]: https://github.com/benmosher/eslint-plugin-import/pull/365
236239
[#359]: https://github.com/benmosher/eslint-plugin-import/pull/359
@@ -263,6 +266,7 @@ for info on changes for earlier releases.
263266
[#157]: https://github.com/benmosher/eslint-plugin-import/pull/157
264267
[#314]: https://github.com/benmosher/eslint-plugin-import/pull/314
265268

269+
[#386]: https://github.com/benmosher/eslint-plugin-import/issues/386
266270
[#373]: https://github.com/benmosher/eslint-plugin-import/issues/373
267271
[#370]: https://github.com/benmosher/eslint-plugin-import/issues/370
268272
[#348]: https://github.com/benmosher/eslint-plugin-import/issues/348
@@ -334,3 +338,4 @@ for info on changes for earlier releases.
334338
[@le0nik]: https://github.com/le0nik
335339
[@scottnonnenberg]: https://github.com/scottnonnenberg
336340
[@sindresorhus]: https://github.com/sindresorhus
341+
[@ljharb]: https://github.com/ljharb

src/rules/newline-after-import.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,19 @@ function containsNodeOrEqual(outerNode, innerNode) {
1515
}
1616

1717
function getScopeBody(scope) {
18-
const { body } = scope.block
18+
if (scope.block.type === 'SwitchStatement') {
19+
return []
20+
}
1921

20-
if (body.type === 'BlockStatement') {
22+
const { body } = scope.block
23+
if (body && body.type === 'BlockStatement') {
2124
return body.body
2225
}
2326

2427
return body
2528
}
2629

27-
function findNodeIndexInScopeBody(scope, nodeToFind) {
28-
const body = getScopeBody(scope)
29-
30+
function findNodeIndexInScopeBody(body, nodeToFind) {
3031
return findIndex(body, (node) => containsNodeOrEqual(node, nodeToFind))
3132
}
3233

@@ -87,7 +88,7 @@ module.exports = function (context) {
8788
scopes.forEach(function ({ scope, requireCalls }) {
8889
requireCalls.forEach(function (node, index) {
8990
const scopeBody = getScopeBody(scope)
90-
const nodePosition = findNodeIndexInScopeBody(scope, node)
91+
const nodePosition = findNodeIndexInScopeBody(scopeBody, node)
9192
const statementWithRequireCall = scopeBody[nodePosition]
9293
const nextStatement = scopeBody[nodePosition + 1]
9394
const nextRequireCall = requireCalls[index + 1]

tests/src/rules/newline-after-import.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
99
valid: [
1010
"var path = require('path');\nvar foo = require('foo');\n",
1111
"a(require('b'), require('c'), require('d'));",
12+
`function foo() {
13+
switch (renderData.modalViewKey) {
14+
case 'value':
15+
var bar = require('bar');
16+
return bar(renderData, options)
17+
default:
18+
return renderData.mainModalContent.clone()
19+
}
20+
}`,
1221
{
1322
code: "import path from 'path';\nimport foo from 'foo';\n",
1423
parserOptions: { sourceType: 'module' },

0 commit comments

Comments
 (0)