diff --git a/src/rules/newline-after-import.js b/src/rules/newline-after-import.js index bbfe87f5f2..5e042eb810 100644 --- a/src/rules/newline-after-import.js +++ b/src/rules/newline-after-import.js @@ -76,6 +76,20 @@ module.exports = { create(context) { let level = 0; const requireCalls = []; + let options = setOptions(context); + + function setOptions(context) { + if (context.options[0]) { + if ('count' in context.options[0] && 'considerComments' in context.options[0] ) { + return context.options[0] + } else if ( 'count' in context.options[0] ) { + return Object.assign({}, context.options[0], { considerComments: false }); + } else { + return Object.assign({}, context.options[0], { count: 1 }); + } + } + return options = { count: 1, considerComments: false }; + } function checkForNewLine(node, nextNode, type) { if (isExportDefaultClass(nextNode) || isExportNameClass(nextNode)) { @@ -88,7 +102,6 @@ module.exports = { nextNode = nextNode.decorators[0]; } - const options = context.options[0] && 'count' in context.options[0]? context.options[0] : { count: 1 }; const lineDifference = getLineDifference(node, nextNode); const EXPECTED_LINE_DIFFERENCE = options.count + 1; @@ -115,7 +128,6 @@ after ${type} statement not followed by another ${type}.`, } function commentAfterImport(node, nextComment) { - const options = context.options[0] && 'count' in context.options[0] ? context.options[0] : { count: 1 }; const lineDifference = getLineDifference(node, nextComment); const EXPECTED_LINE_DIFFERENCE = options.count + 1; @@ -149,7 +161,6 @@ after import statement not followed by another import.`, } function checkImport(node) { - const options = context.options[0] && 'considerComments' in context.options[0] ? context.options[0] : { considerComments: false }; const { parent } = node; const nodePosition = parent.body.indexOf(node); const nextNode = parent.body[nodePosition + 1]; diff --git a/tests/src/rules/newline-after-import.js b/tests/src/rules/newline-after-import.js index 5fd82d500d..9eb5677bda 100644 --- a/tests/src/rules/newline-after-import.js +++ b/tests/src/rules/newline-after-import.js @@ -24,6 +24,16 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), { , y = () => require('bar')`, parserOptions: { ecmaVersion: 6 } , }, + { + code: ` + const x = () => require('baz') + , y = () => require('bar') + + // some comment here + `, + parserOptions: { ecmaVersion: 6 } , + options: [{ considerComments: true }], + }, { code: `const x = () => require('baz') && require('bar')`, parserOptions: { ecmaVersion: 6 } ,