Skip to content

Commit

Permalink
Fix #441 + added a bunch of debug logging for next time 😅
Browse files Browse the repository at this point in the history
  • Loading branch information
benmosher committed Jul 20, 2016
1 parent b2184f0 commit c23e763
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"dependencies": {
"builtin-modules": "^1.1.1",
"contains-path": "^0.1.0",
"debug": "^2.2.0",
"doctrine": "1.2.x",
"es6-map": "^0.1.3",
"es6-set": "^0.1.4",
Expand Down
17 changes: 15 additions & 2 deletions src/rules/newline-after-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
import isStaticRequire from '../core/staticRequire'
import findIndex from 'lodash.findindex'

import debug from 'debug'

const log = debug('eslint-plugin-import:rules:newline-after-import')

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
Expand All @@ -16,7 +20,8 @@ function containsNodeOrEqual(outerNode, innerNode) {

function getScopeBody(scope) {
if (scope.block.type === 'SwitchStatement') {
return []
log('SwitchStatement scopes not supported')
return null
}

const { body } = scope.block
Expand Down Expand Up @@ -85,14 +90,22 @@ module.exports = function (context) {
}
},
'Program:exit': function () {
log('exit processing for', context.getFilename())
scopes.forEach(function ({ scope, requireCalls }) {
const scopeBody = getScopeBody(scope)

// skip non-array scopes (i.e. arrow function expressions)
if (!(scopeBody instanceof Array)) return
if (!scopeBody || !(scopeBody instanceof Array)) {
log('invalid scope:', scopeBody)
return
}

log('got scope:', scopeBody)

requireCalls.forEach(function (node, index) {
const nodePosition = findNodeIndexInScopeBody(scopeBody, node)
log('node position in scope:', nodePosition)

const statementWithRequireCall = scopeBody[nodePosition]
const nextStatement = scopeBody[nodePosition + 1]
const nextRequireCall = requireCalls[index + 1]
Expand Down
35 changes: 34 additions & 1 deletion tests/src/rules/newline-after-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,39 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
return renderData.mainModalContent.clone()
}
}`,
{ code: `//issue 441
function bar() {
switch (foo) {
case '1':
return require('../path/to/file1.jst.hbs')(renderData, options);
case '2':
return require('../path/to/file2.jst.hbs')(renderData, options);
case '3':
return require('../path/to/file3.jst.hbs')(renderData, options);
case '4':
return require('../path/to/file4.jst.hbs')(renderData, options);
case '5':
return require('../path/to/file5.jst.hbs')(renderData, options);
case '6':
return require('../path/to/file6.jst.hbs')(renderData, options);
case '7':
return require('../path/to/file7.jst.hbs')(renderData, options);
case '8':
return require('../path/to/file8.jst.hbs')(renderData, options);
case '9':
return require('../path/to/file9.jst.hbs')(renderData, options);
case '10':
return require('../path/to/file10.jst.hbs')(renderData, options);
case '11':
return require('../path/to/file11.jst.hbs')(renderData, options);
case '12':
return something();
default:
return somethingElse();
}
}`,
parserOptions: { sourceType: 'module' },
},
{
code: "import path from 'path';\nimport foo from 'foo';\n",
parserOptions: { sourceType: 'module' },
Expand Down Expand Up @@ -196,5 +229,5 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
} ],
parserOptions: { sourceType: 'module' },
},
]
],
})

0 comments on commit c23e763

Please sign in to comment.