Skip to content

Commit

Permalink
fix code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
tihonove committed Aug 2, 2017
1 parent c25bdcb commit 2209e83
Showing 1 changed file with 20 additions and 44 deletions.
64 changes: 20 additions & 44 deletions src/rules/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,29 @@ function reverse(array) {
}

function getTokensOrCommentsAfter(sourceCode, node, count) {
if (typeof sourceCode.getTokenOrCommentAfter === 'function') {
let currentNodeOrToken = node
const result = []
for (let i = 0; i < count; i++) {
currentNodeOrToken = sourceCode.getTokenOrCommentAfter(currentNodeOrToken)
if (currentNodeOrToken == null) {
break
}
result.push(currentNodeOrToken)
let currentNodeOrToken = node
const result = []
for (let i = 0; i < count; i++) {
currentNodeOrToken = sourceCode.getTokenOrCommentAfter(currentNodeOrToken)
if (currentNodeOrToken == null) {
break
}
return result
result.push(currentNodeOrToken)
}
return sourceCode.getTokensAfter(node, { count: 100, includeComments: true })
return result
}

function getTokensOrCommentsBefore(sourceCode, node, count) {
if (typeof sourceCode.getTokenOrCommentBefore === 'function') {
let currentNodeOrToken = node
const result = []
for (let i = 0; i < count; i++) {
currentNodeOrToken = sourceCode.getTokenOrCommentBefore(currentNodeOrToken)
if (currentNodeOrToken == null) {
break
}
result.push(currentNodeOrToken)
let currentNodeOrToken = node
const result = []
for (let i = 0; i < count; i++) {
currentNodeOrToken = sourceCode.getTokenOrCommentBefore(currentNodeOrToken)
if (currentNodeOrToken == null) {
break
}
return result.reverse()
result.push(currentNodeOrToken)
}
return sourceCode.getTokensBefore(node, { count: 100, includeComments: true })
return result.reverse()
}

function takeTokensAfterWhile(sourceCode, node, condition) {
Expand Down Expand Up @@ -99,24 +93,6 @@ function findRootNode(node) {
return parent
}

function findStartOfLine(text, position) {
for(let i = position; i >= 0; i--) {
if (text[i] === '\n') {
return i + 1
}
}
return 0
}

function findEndOfLine(text, position) {
for(let i = position; i < text.length; i++) {
if (text[i] === '\n') {
return i + 1
}
}
return text.length
}

function findEndOfLineWithComments(sourceCode, node) {
const tokensToEndOfLine = takeTokensAfterWhile(sourceCode, node, commentOnSameLineAs(node))
let endOfTokens = tokensToEndOfLine.length > 0
Expand Down Expand Up @@ -286,14 +262,14 @@ function fixNewLineAfterImport(context, previousImport) {
}

function removeNewLineAfterImport(context, currentImport, previousImport) {
const sourceCodeText = context.getSourceCode().text
const sourceCode = context.getSourceCode()
const prevRoot = findRootNode(previousImport.node)
const currRoot = findRootNode(currentImport.node)
const rangeToRemove = [
findEndOfLine(sourceCodeText, prevRoot.end),
findStartOfLine(sourceCodeText, currRoot.start),
findEndOfLineWithComments(sourceCode, prevRoot),
findStartOfLineWithComments(sourceCode, currRoot),
]
if (/^\s*$/.test(sourceCodeText.substring(rangeToRemove[0], rangeToRemove[1]))) {
if (/^\s*$/.test(sourceCode.text.substring(rangeToRemove[0], rangeToRemove[1]))) {
return (fixer) => fixer.removeRange(rangeToRemove)
}
return undefined
Expand Down

0 comments on commit 2209e83

Please sign in to comment.