-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
index.js
212 lines (182 loc) · 4.75 KB
/
index.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/* @flow */
import path from 'path';
import fs from 'fs';
// $FlowIgnore
import findUp from 'find-up';
import {
type CollectOutputElement,
FlowSeverity,
collect,
coverage
} from './collect';
import getProgram, { type Program, type Loc } from './get-program';
type EslintContext = {
getAllComments: () => { value: string }[],
getFilename: () => string,
getSourceCode: () => Object,
report: ({ loc: Loc, message: string }) => void,
settings: ?{
'flowtype-errors': ?{
stopOnExit?: any
}
},
options: any[]
};
type Info = {
flowDir: string,
program: Program
};
const DEFAULT_LOC = {
start: {
line: 1,
column: 0
},
end: {
line: 1,
column: 0
}
};
function lookupInfo(
context: EslintContext,
source: Object,
node: Object
): ?Info {
const flowconfigFile = findUp.sync('.flowconfig', {
cwd: path.dirname(context.getFilename())
});
if (flowconfigFile == null) {
const program = getProgram(source, node);
context.report({
loc: program ? program.loc : DEFAULT_LOC,
message: "Could not find '.flowconfig' file"
});
return null;
}
const flowDir = path.dirname(flowconfigFile);
const runOnAllFiles = fs
.readFileSync(flowconfigFile, 'utf8')
.includes('all=true');
const shouldRun =
runOnAllFiles ||
source.getAllComments().some(comment => /@flow/.test(comment.value));
const program = shouldRun && getProgram(source, node);
if (program) {
return {
flowDir,
program
};
}
return null;
}
function stopOnExit(context: EslintContext): boolean {
return !!(
context.settings &&
context.settings['flowtype-errors'] &&
context.settings['flowtype-errors'].stopOnExit
);
}
function errorFlowCouldNotRun(loc) {
return {
loc,
message: `Flow could not be run. Possible causes include:
* Running on 32-bit OS (https://github.com/facebook/flow/issues/2262)
* Recent glibc version not available (https://github.com/flowtype/flow-bin/issues/49)
* FLOW_BIN environment variable ${
process.env.FLOW_BIN ? 'set incorrectly' : 'not set'
}
.`
};
}
function createFilteredErrorRule(filter: CollectOutputElement => any) {
return function showErrors(context: EslintContext) {
return {
Program(node: Object) {
const source = context.getSourceCode();
const info = lookupInfo(context, source, node);
if (!info) {
return;
}
const { flowDir, program } = info;
const collected = collect(
program.text,
flowDir,
stopOnExit(context),
context.getFilename(),
program.offset
);
if (collected === true) {
return;
}
if (collected === false) {
context.report(errorFlowCouldNotRun(program.loc));
return;
}
collected.filter(filter).forEach(({ loc, message }) => {
context.report({
loc: loc
? {
...loc,
start: {
...loc.start,
// Flow's column numbers are 1-based, while ESLint's are 0-based.
column: loc.start.column - 1
}
}
: loc,
message
});
});
}
};
};
}
module.exports = {
rules: {
'enforce-min-coverage': function enforceMinCoverage(
context: EslintContext
) {
return {
Program(node: Object) {
const source = context.getSourceCode();
const info = lookupInfo(context, source, node);
if (!info) {
return;
}
const { flowDir, program } = info;
const res = coverage(
program.text,
flowDir,
stopOnExit(context),
context.getFilename()
);
if (res === true) {
return;
}
if (res === false) {
context.report(errorFlowCouldNotRun(program.loc));
return;
}
const requiredCoverage = context.options[0];
const { coveredCount, uncoveredCount } = res;
/* eslint prefer-template: 0 */
const percentage = Number(
Math.round(coveredCount / (coveredCount + uncoveredCount) * 10000) +
'e-2'
);
if (percentage < requiredCoverage) {
context.report({
loc: program.loc,
message: `Expected coverage to be at least ${requiredCoverage}%, but is: ${percentage}%`
});
}
}
};
},
'show-errors': createFilteredErrorRule(
({ level }) => level !== FlowSeverity.Warning
),
'show-warnings': createFilteredErrorRule(
({ level }) => level === FlowSeverity.Warning
)
}
};