From 821a02d394a29ca84c36bbae469291966d3cc79b Mon Sep 17 00:00:00 2001 From: Jeroen Engels Date: Fri, 23 Sep 2016 20:06:55 +0200 Subject: [PATCH] Simplify newline-after-import implementation --- src/rules/newline-after-import.js | 58 ++++++++++--------------------- 1 file changed, 18 insertions(+), 40 deletions(-) diff --git a/src/rules/newline-after-import.js b/src/rules/newline-after-import.js index 7adcbfceed..09f20af771 100644 --- a/src/rules/newline-after-import.js +++ b/src/rules/newline-after-import.js @@ -45,10 +45,9 @@ module.exports = { meta: { docs: {}, }, - create: function (context) { - const scopes = [] - let scopeIndex = 0 + let level = 0 + const requireCalls = [] function checkForNewLine(node, nextNode, type) { if (getLineDifference(node, nextNode) < 2) { @@ -68,7 +67,6 @@ module.exports = { } } - let level = 0 function incrementLevel() { level++ } @@ -86,53 +84,33 @@ module.exports = { checkForNewLine(node, nextNode, 'import') } }, - Program: function () { - scopes.push({ scope: context.getScope(), requireCalls: [] }) - }, CallExpression: function(node) { - const scope = context.getScope() if (isStaticRequire(node) && level === 0) { - const currentScope = scopes[scopeIndex] - - if (scope === currentScope.scope) { - currentScope.requireCalls.push(node) - } else { - scopes.push({ scope, requireCalls: [ node ] }) - scopeIndex += 1 - } + requireCalls.push(node) } }, 'Program:exit': function () { log('exit processing for', context.getFilename()) - scopes.forEach(function ({ scope, requireCalls }) { - const scopeBody = getScopeBody(scope) + const scopeBody = getScopeBody(context.getScope()) + log('got scope:', scopeBody) - // skip non-array scopes (i.e. arrow function expressions) - 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) + 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] + const statementWithRequireCall = scopeBody[nodePosition] + const nextStatement = scopeBody[nodePosition + 1] + const nextRequireCall = requireCalls[index + 1] - if (nextRequireCall && containsNodeOrEqual(statementWithRequireCall, nextRequireCall)) { - return - } + if (nextRequireCall && containsNodeOrEqual(statementWithRequireCall, nextRequireCall)) { + return + } - if (nextStatement && - (!nextRequireCall || !containsNodeOrEqual(nextStatement, nextRequireCall))) { + if (nextStatement && + (!nextRequireCall || !containsNodeOrEqual(nextStatement, nextRequireCall))) { - checkForNewLine(statementWithRequireCall, nextStatement, 'require') - } - }) + checkForNewLine(statementWithRequireCall, nextStatement, 'require') + } }) }, FunctionDeclaration: incrementLevel,