Skip to content

Commit

Permalink
Fix crash with newline-after-import related to the use of switch ca…
Browse files Browse the repository at this point in the history
…ses (fixes #386)
  • Loading branch information
jfmengels committed Jun 23, 2016
1 parent a66e7c7 commit ba23743
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
- Added new rule [`no-restricted-paths`]. ([#155]/[#371], thanks [@lo1tuma])
- [`import/core-modules` setting]: allow configuration of additional module names,
to be treated as builtin modules (a la `path`, etc. in Node). ([#275] + [#365], thanks [@sindresorhus] for driving)
### Fixed
- Fixed crash with `newline-after-import` related to the use of switch cases. (fixes [#386], thanks [@ljharb] for reporting) ([#395])

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

[#395]: https://github.com/benmosher/eslint-plugin-import/pull/395
[#371]: https://github.com/benmosher/eslint-plugin-import/pull/371
[#365]: https://github.com/benmosher/eslint-plugin-import/pull/365
[#359]: https://github.com/benmosher/eslint-plugin-import/pull/359
Expand Down Expand Up @@ -263,6 +266,7 @@ for info on changes for earlier releases.
[#157]: https://github.com/benmosher/eslint-plugin-import/pull/157
[#314]: https://github.com/benmosher/eslint-plugin-import/pull/314

[#386]: https://github.com/benmosher/eslint-plugin-import/issues/386
[#373]: https://github.com/benmosher/eslint-plugin-import/issues/373
[#370]: https://github.com/benmosher/eslint-plugin-import/issues/370
[#348]: https://github.com/benmosher/eslint-plugin-import/issues/348
Expand Down Expand Up @@ -334,3 +338,4 @@ for info on changes for earlier releases.
[@le0nik]: https://github.com/le0nik
[@scottnonnenberg]: https://github.com/scottnonnenberg
[@sindresorhus]: https://github.com/sindresorhus
[@ljharb]: https://github.com/ljharb
13 changes: 7 additions & 6 deletions src/rules/newline-after-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,19 @@ function containsNodeOrEqual(outerNode, innerNode) {
}

function getScopeBody(scope) {
const { body } = scope.block
if (scope.block.type === 'SwitchStatement') {
return []
}

if (body.type === 'BlockStatement') {
const { body } = scope.block
if (body && body.type === 'BlockStatement') {
return body.body
}

return body
}

function findNodeIndexInScopeBody(scope, nodeToFind) {
const body = getScopeBody(scope)

function findNodeIndexInScopeBody(body, nodeToFind) {
return findIndex(body, (node) => containsNodeOrEqual(node, nodeToFind))
}

Expand Down Expand Up @@ -87,7 +88,7 @@ module.exports = function (context) {
scopes.forEach(function ({ scope, requireCalls }) {
requireCalls.forEach(function (node, index) {
const scopeBody = getScopeBody(scope)
const nodePosition = findNodeIndexInScopeBody(scope, node)
const nodePosition = findNodeIndexInScopeBody(scopeBody, node)
const statementWithRequireCall = scopeBody[nodePosition]
const nextStatement = scopeBody[nodePosition + 1]
const nextRequireCall = requireCalls[index + 1]
Expand Down
9 changes: 9 additions & 0 deletions tests/src/rules/newline-after-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
valid: [
"var path = require('path');\nvar foo = require('foo');\n",
"a(require('b'), require('c'), require('d'));",
`function foo() {
switch (renderData.modalViewKey) {
case 'value':
var bar = require('bar');
return bar(renderData, options)
default:
return renderData.mainModalContent.clone()
}
}`,
{
code: "import path from 'path';\nimport foo from 'foo';\n",
parserOptions: { sourceType: 'module' },
Expand Down

0 comments on commit ba23743

Please sign in to comment.