Skip to content

Commit

Permalink
fix compatibility issues
Browse files Browse the repository at this point in the history
  • Loading branch information
tihonove committed Aug 2, 2017
1 parent 31def14 commit c25bdcb
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 20 deletions.
62 changes: 47 additions & 15 deletions src/rules/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,40 @@ function reverse(array) {
}).reverse()
}

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)
}
return result
}
return sourceCode.getTokensAfter(node, { count: 100, includeComments: true })
}

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)
}
return result.reverse()
}
return sourceCode.getTokensBefore(node, { count: 100, includeComments: true })
}

function takeTokensAfterWhile(sourceCode, node, condition) {
const tokens = sourceCode.getTokensAfter(node, { count: 100, includeComments: true })
const tokens = getTokensOrCommentsAfter(sourceCode, node, 100)
const result = []
for (let i = 0; i < tokens.length; i++) {
if (condition(tokens[i])) {
Expand All @@ -32,7 +64,7 @@ function takeTokensAfterWhile(sourceCode, node, condition) {
}

function takeTokensBeforeWhile(sourceCode, node, condition) {
const tokens = sourceCode.getTokensBefore(node, { count: 100, includeComments: true })
const tokens = getTokensOrCommentsBefore(sourceCode, node, 100)
const result = []
for (let i = tokens.length - 1; i >= 0; i--) {
if (condition(tokens[i])) {
Expand Down Expand Up @@ -105,7 +137,7 @@ function findEndOfLineWithComments(sourceCode, node) {
}

function commentOnSameLineAs(node) {
return token => ['Block', 'Line'].includes(token.type) &&
return token => (token.type === 'Block' || token.type === 'Line') &&
token.loc.start.line === token.loc.end.line &&
token.loc.end.line === node.loc.end.line
}
Expand Down Expand Up @@ -146,19 +178,21 @@ function fixOutOfOrder(context, firstNode, secondNode, order) {
context.report({
node: secondNode.node,
message: message,
fix: fixer => [
fixer.removeRange([ secondRootStart, secondRootEnd ]),
fixer.insertTextBeforeRange([firstRootStart, firstRootEnd], newCode),
],
fix: fixer =>
fixer.replaceTextRange(
[firstRootStart, secondRootEnd],
newCode + sourceCode.text.substring(firstRootStart, secondRootStart)
),
})
} else if (order === 'after') {
context.report({
node: secondNode.node,
message: message,
fix: fixer => [
fixer.removeRange([ secondRootStart, secondRootEnd ]),
fixer.insertTextAfterRange([firstRootStart, firstRootEnd], newCode),
],
fix: fixer =>
fixer.replaceTextRange(
[secondRootStart, firstRootEnd],
sourceCode.text.substring(secondRootEnd, firstRootEnd) + newCode
),
})
}
}
Expand Down Expand Up @@ -241,10 +275,8 @@ function convertGroupsToRanks(groups) {

function fixNewLineAfterImport(context, previousImport) {
const prevRoot = findRootNode(previousImport.node)
const tokensToEndOfLine = takeTokensAfterWhile(context, prevRoot,
x => ['Block', 'Line'].includes(x.type) &&
x.loc.start.line === x.loc.end.line &&
x.loc.end.line ===prevRoot.loc.end.line)
const tokensToEndOfLine = takeTokensAfterWhile(
context.getSourceCode(), prevRoot, commentOnSameLineAs(prevRoot))

let endOfLine = prevRoot.end
if (tokensToEndOfLine.length > 0) {
Expand Down
5 changes: 0 additions & 5 deletions tests/src/rules/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -626,11 +626,6 @@ ruleTester.run('order', rule, {
var async = require('async');
var fs = require('fs');
`,
output: `
var async = require('async');
var sibling = require('./sibling');
var fs = require('fs');
`,
errors: [{
ruleId: 'order',
message: '`async` import should occur before import of `./sibling`',
Expand Down

0 comments on commit c25bdcb

Please sign in to comment.