Skip to content

Commit

Permalink
Merge pull request #1137 from justinanastos/fix/typescript-import-ord…
Browse files Browse the repository at this point in the history
…er-1086

Fix import/order autofixer when using typescript-eslint-parser
  • Loading branch information
ljharb committed Jul 16, 2018
2 parents a162af4 + 8d02f32 commit e5ee158
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/rules/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ function findRootNode(node) {
function findEndOfLineWithComments(sourceCode, node) {
const tokensToEndOfLine = takeTokensAfterWhile(sourceCode, node, commentOnSameLineAs(node))
let endOfTokens = tokensToEndOfLine.length > 0
? tokensToEndOfLine[tokensToEndOfLine.length - 1].end
: node.end
? tokensToEndOfLine[tokensToEndOfLine.length - 1].range[1]
: node.range[1]
let result = endOfTokens
for (let i = endOfTokens; i < sourceCode.text.length; i++) {
if (sourceCode.text[i] === '\n') {
Expand All @@ -121,7 +121,7 @@ function commentOnSameLineAs(node) {

function findStartOfLineWithComments(sourceCode, node) {
const tokensToEndOfLine = takeTokensBeforeWhile(sourceCode, node, commentOnSameLineAs(node))
let startOfTokens = tokensToEndOfLine.length > 0 ? tokensToEndOfLine[0].start : node.start
let startOfTokens = tokensToEndOfLine.length > 0 ? tokensToEndOfLine[0].range[0] : node.range[0]
let result = startOfTokens
for (let i = startOfTokens - 1; i > 0; i--) {
if (sourceCode.text[i] !== ' ' && sourceCode.text[i] !== '\t') {
Expand Down Expand Up @@ -296,11 +296,11 @@ function fixNewLineAfterImport(context, previousImport) {
const tokensToEndOfLine = takeTokensAfterWhile(
context.getSourceCode(), prevRoot, commentOnSameLineAs(prevRoot))

let endOfLine = prevRoot.end
let endOfLine = prevRoot.range[1]
if (tokensToEndOfLine.length > 0) {
endOfLine = tokensToEndOfLine[tokensToEndOfLine.length - 1].end
endOfLine = tokensToEndOfLine[tokensToEndOfLine.length - 1].range[1]
}
return (fixer) => fixer.insertTextAfterRange([prevRoot.start, endOfLine], '\n')
return (fixer) => fixer.insertTextAfterRange([prevRoot.range[0], endOfLine], '\n')
}

function removeNewLineAfterImport(context, currentImport, previousImport) {
Expand Down
16 changes: 16 additions & 0 deletions tests/src/rules/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -1260,5 +1260,21 @@ ruleTester.run('order', rule, {
message: '`fs` import should occur before import of `async`',
}],
})),
// fix incorrect order with typescript-eslint-parser
test({
code: `
var async = require('async');
var fs = require('fs');
`,
output: `
var fs = require('fs');
var async = require('async');
`,
parser: 'typescript-eslint-parser',
errors: [{
ruleId: 'order',
message: '`fs` import should occur before import of `async`',
}],
}),
],
})

0 comments on commit e5ee158

Please sign in to comment.