forked from import-js/eslint-plugin-import
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnewline-after-import.js
160 lines (136 loc) · 4.6 KB
/
newline-after-import.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
* @fileoverview Rule to enforce new line after import not followed by another import.
* @author Radek Benkel
*/
import isStaticRequire from '../core/staticRequire'
import docsUrl from '../docsUrl'
import debug from 'debug'
const log = debug('eslint-plugin-import:rules:newline-after-import')
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
function containsNodeOrEqual(outerNode, innerNode) {
return outerNode.range[0] <= innerNode.range[0] && outerNode.range[1] >= innerNode.range[1]
}
function getScopeBody(scope) {
if (scope.block.type === 'SwitchStatement') {
log('SwitchStatement scopes not supported')
return null
}
const { body } = scope.block
if (body && body.type === 'BlockStatement') {
return body.body
}
return body
}
function findNodeIndexInScopeBody(body, nodeToFind) {
return body.findIndex((node) => containsNodeOrEqual(node, nodeToFind))
}
function getLineDifference(node, nextNode) {
return nextNode.loc.start.line - node.loc.end.line
}
function isClassWithDecorator(node) {
return node.type === 'ClassDeclaration' && node.decorators && node.decorators.length
}
module.exports = {
meta: {
docs: {
url: docsUrl('newline-after-import'),
},
schema: [
{
'type': 'object',
'properties': {
'count': {
'type': 'integer',
'minimum': 1,
},
},
'additionalProperties': false,
},
],
fixable: 'whitespace',
},
create: function (context) {
let level = 0
const requireCalls = []
function checkForNewLine(node, nextNode, type) {
if (isClassWithDecorator(nextNode)) {
nextNode = nextNode.decorators[0]
}
const options = context.options[0] || { count: 1 }
const lineDifference = getLineDifference(node, nextNode)
const EXPECTED_LINE_DIFFERENCE = options.count + 1
if (lineDifference < EXPECTED_LINE_DIFFERENCE) {
let column = node.loc.start.column
if (node.loc.start.line !== node.loc.end.line) {
column = 0
}
context.report({
loc: {
line: node.loc.end.line,
column,
},
message: `Expected ${options.count} empty line${options.count > 1 ? 's' : ''} \
after ${type} statement not followed by another ${type}.`,
fix: fixer => fixer.insertTextAfter(
node,
'\n'.repeat(EXPECTED_LINE_DIFFERENCE - lineDifference)
),
})
}
}
function incrementLevel() {
level++
}
function decrementLevel() {
level--
}
return {
ImportDeclaration: function (node) {
const { parent } = node
const nodePosition = parent.body.indexOf(node)
const nextNode = parent.body[nodePosition + 1]
if (nextNode && nextNode.type !== 'ImportDeclaration') {
checkForNewLine(node, nextNode, 'import')
}
},
CallExpression: function(node) {
if (isStaticRequire(node) && level === 0) {
requireCalls.push(node)
}
},
'Program:exit': function () {
log('exit processing for', context.getFilename())
const scopeBody = getScopeBody(context.getScope())
log('got scope:', scopeBody)
requireCalls.forEach(function (node, index) {
const nodePosition = findNodeIndexInScopeBody(scopeBody, node)
log('node position in scope:', nodePosition)
const statementWithRequireCall = scopeBody[nodePosition]
const nextStatement = scopeBody[nodePosition + 1]
const nextRequireCall = requireCalls[index + 1]
if (nextRequireCall && containsNodeOrEqual(statementWithRequireCall, nextRequireCall)) {
return
}
if (nextStatement &&
(!nextRequireCall || !containsNodeOrEqual(nextStatement, nextRequireCall))) {
checkForNewLine(statementWithRequireCall, nextStatement, 'require')
}
})
},
FunctionDeclaration: incrementLevel,
FunctionExpression: incrementLevel,
ArrowFunctionExpression: incrementLevel,
BlockStatement: incrementLevel,
ObjectExpression: incrementLevel,
Decorator: incrementLevel,
'FunctionDeclaration:exit': decrementLevel,
'FunctionExpression:exit': decrementLevel,
'ArrowFunctionExpression:exit': decrementLevel,
'BlockStatement:exit': decrementLevel,
'ObjectExpression:exit': decrementLevel,
'Decorator:exit': decrementLevel,
}
},
}