forked from import-js/eslint-plugin-import
/
first.js
132 lines (127 loc) · 4.54 KB
/
first.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import docsUrl from '../docsUrl'
module.exports = {
meta: {
type: 'suggestion',
docs: {
url: docsUrl('first'),
},
fixable: 'code',
schema: [
{
type: 'string',
enum: ['absolute-first', 'disable-absolute-first'],
},
],
},
create: function (context) {
function isPossibleDirective (node) {
return node.type === 'ExpressionStatement' &&
node.expression.type === 'Literal' &&
typeof node.expression.value === 'string'
}
return {
'Program': function (n) {
const body = n.body
, absoluteFirst = context.options[0] === 'absolute-first'
, message = 'Import in body of module; reorder to top.'
, sourceCode = context.getSourceCode()
, originSourceCode = sourceCode.getText()
let nonImportCount = 0
, anyExpressions = false
, anyRelative = false
, lastLegalImp = null
, errorInfos = []
, shouldSort = true
, lastSortNodesIndex = 0
body.forEach(function (node, index){
if (!anyExpressions && isPossibleDirective(node)) {
return
}
anyExpressions = true
if (node.type === 'ImportDeclaration') {
if (absoluteFirst) {
if (/^\./.test(node.source.value)) {
anyRelative = true
} else if (anyRelative) {
context.report({
node: node.source,
message: 'Absolute imports should come before relative imports.',
})
}
}
if (nonImportCount > 0) {
for (let variable of context.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]) {
shouldSort = false
break
}
}
}
}
shouldSort && (lastSortNodesIndex = errorInfos.length)
errorInfos.push({
node,
range: [body[index - 1].range[1], node.range[1]],
})
} else {
lastLegalImp = node
}
} else {
nonImportCount++
}
})
if (!errorInfos.length) return
errorInfos.forEach(function (errorInfo, index) {
const node = errorInfo.node
, infos = {
node,
message,
}
if (index < lastSortNodesIndex) {
infos.fix = function (fixer) {
return fixer.insertTextAfter(node, '')
}
} 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)
})
, range = [0, removeFixers[removeFixers.length - 1].range[1]]
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 = null
, replaceSourceCode = ''
if (!lastLegalImp) {
insertSourceCode =
insertSourceCode.trim() + insertSourceCode.match(/^(\s+)/)[0]
}
insertFixer = lastLegalImp ?
fixer.insertTextAfter(lastLegalImp, insertSourceCode) :
fixer.insertTextBefore(body[0], insertSourceCode)
const fixers = [insertFixer].concat(removeFixers)
fixers.forEach(function (computedFixer, i) {
replaceSourceCode += (originSourceCode.slice(
fixers[i - 1] ? fixers[i - 1].range[1] : 0, computedFixer.range[0]
) + computedFixer.text)
})
return fixer.replaceTextRange(range, replaceSourceCode)
}
}
context.report(infos)
})
},
}
},
}