Skip to content

Commit

Permalink
Simplify newline-after-import implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
jfmengels committed Sep 24, 2016
1 parent 08a8929 commit 821a02d
Showing 1 changed file with 18 additions and 40 deletions.
58 changes: 18 additions & 40 deletions src/rules/newline-after-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -68,7 +67,6 @@ module.exports = {
}
}

let level = 0
function incrementLevel() {
level++
}
Expand All @@ -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,
Expand Down

0 comments on commit 821a02d

Please sign in to comment.