Skip to content

Commit

Permalink
refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
pri1311 committed Mar 23, 2022
1 parent cc3fbcb commit 2464a9e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/rules/newline-after-import.js
Expand Up @@ -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)) {
Expand All @@ -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;

Expand All @@ -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;

Expand Down Expand Up @@ -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];
Expand Down
10 changes: 10 additions & 0 deletions tests/src/rules/newline-after-import.js
Expand Up @@ -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 } ,
Expand Down

0 comments on commit 2464a9e

Please sign in to comment.