Skip to content

Commit

Permalink
finish first fixer
Browse files Browse the repository at this point in the history
  • Loading branch information
fnknzzz committed Mar 21, 2018
1 parent c638fdd commit 449e461
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 55 deletions.
83 changes: 43 additions & 40 deletions src/rules/first.js
Expand Up @@ -23,15 +23,13 @@ module.exports = {
, sourceCode = context.getSourceCode()
, originSourceCode = sourceCode.getText()
, scopeManager = sourceCode.scopeManager
, moduleScope = scopeManager.scopes.find(function (scope) {
return scope.type === 'module'
})
let nonImportCount = 0
, anyExpressions = false
, anyRelative = false
, lastLegalImp = null
, errorInfos = []

, shouldSort = true
, lastSortNodesIndex = 0
body.forEach(function (node, index){
if (!anyExpressions && isPossibleDirective(node)) {
return
Expand All @@ -51,21 +49,20 @@ module.exports = {
}
}
if (nonImportCount > 0) {
let shouldSort = true
for (let variable of scopeManager.getDeclaredVariables(node)) {
if (!shouldSort) break
const references = variable.references
if (references.length) {
for (let reference of references) {
if (reference.identifier.range[0] >= node.range[1]) {
break
} else if (reference.from === moduleScope) {
if (reference.identifier.range[0] < node.range[1]) {
shouldSort = false
break
}
}
}
}
shouldSort && errorInfos.push({
shouldSort && (lastSortNodesIndex = errorInfos.length)
errorInfos.push({
node,
range: [body[index - 1].range[1], node.range[1]],
})
Expand All @@ -77,40 +74,46 @@ module.exports = {
}
})
if (!errorInfos.length) return
errorInfos.slice(0, -1).forEach(function (errorInfo) {
errorInfos.forEach(function (errorInfo, index) {
const node = errorInfo.node
context.report({
node,
message,
// fake fixer
fix: function (fixer) {
, infos = {
node,
message,
}
if (index < lastSortNodesIndex) {
infos.fix = function (fixer) {
return fixer.insertTextAfter(node, '')
},
})
})
context.report({
node: errorInfos[errorInfos.length - 1].node,
message,
// fixers batch
fix: function (fixer) {
const removeFixers = errorInfos.map(function (errorInfo) {
return fixer.removeRange(errorInfo.range)
})
, insertSourceCode = errorInfos.map(function (errorInfo) {
const nodeSourceCode = String.prototype.slice.apply(
originSourceCode, errorInfo.range
)
if (/\S/.test(nodeSourceCode[0])) {
return '\n' + nodeSourceCode
} else {
}
} else if (index === lastSortNodesIndex) {
const sortNodes = errorInfos.slice(0, lastSortNodesIndex + 1)
infos.fix = function (fixer) {
const removeFixers = sortNodes.map(function (_errorInfo) {
return fixer.removeRange(_errorInfo.range)
})
let insertSourceCode = sortNodes.map(function (_errorInfo) {
const nodeSourceCode = String.prototype.slice.apply(
originSourceCode, _errorInfo.range
)
if (/\S/.test(nodeSourceCode[0])) {
return '\n' + nodeSourceCode
}
return nodeSourceCode
}
}).join('')
, insertFixer = lastLegalImp ?
fixer.insertTextAfter(lastLegalImp, insertSourceCode) :
fixer.insertTextBefore(body[0], insertSourceCode.trim() + '\n')
return removeFixers.concat([insertFixer])
},
}).join('')
, insertFixer = null
if (!lastLegalImp && !(/\s$/.test(insertSourceCode))) {
if (/^(\s+)/.test(insertSourceCode)) {
insertSourceCode = insertSourceCode.trim() + RegExp.$1
} else {
insertSourceCode = insertSourceCode + '\n'
}
}
insertFixer = lastLegalImp ?
fixer.insertTextAfter(lastLegalImp, insertSourceCode) :
fixer.insertTextBefore(body[0], insertSourceCode)
return removeFixers.concat([insertFixer])
}
}
context.report(infos)
})
},
}
Expand Down
25 changes: 10 additions & 15 deletions tests/src/rules/first.js
Expand Up @@ -18,10 +18,10 @@ ruleTester.run('first', rule, {
invalid: [
test({ code: "import { x } from './foo';\
export { x };\
import { y } from './foo';"
import { y } from './bar';"
, errors: 1
, output: "import { x } from './foo';\
import { y } from './foo';\
import { y } from './bar';\
export { x };"
})
, test({ code: "import { x } from './foo';\
Expand Down Expand Up @@ -53,25 +53,20 @@ ruleTester.run('first', rule, {
, errors: 3
, output: "import { x } from './foo';\
import { y } from './bar';\
import { z } from './baz';\nvar foo = bar;"
import { z } from './baz';\
var foo = bar;"
})
, test({ code: "var a = x;\
import { x } from './foo';\
, test({ code: "var a = 1;\
import { y } from './bar';\
if (true) { x() };\
import { x } from './foo';\
import { z } from './baz';"
, errors: 2
, errors: 3
, output: "import { y } from './bar';\
import { z } from './baz';\nvar a = x;\
import { x } from './foo';"
})
, test({ code: "if (true) { x() };\
var a = 1;\
if (true) { x() };\
import { x } from './foo';\
import { y } from './bar';\
import { z } from './baz';"
, errors: 3
, output: "import { x } from './foo';\
import { y } from './bar';\
import { z } from './baz';\nif (true) { x() };"
})
,
]
Expand Down

0 comments on commit 449e461

Please sign in to comment.