-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImplicitIndexFileImport.js
97 lines (76 loc) · 2.47 KB
/
ImplicitIndexFileImport.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
const BaseRule = require('src/rules/Base');
class ImplicitIndexFileImportRule extends BaseRule {
/**
* checks whether `import/require` lines that lead to `index.js` are not called **explicitly**
*
* @param {PatchronContext} patchronContext
* @param {ImplicitIndexFileImportConfig} config
* @param {Patch} file
*/
constructor(patchronContext, config, file) {
super(patchronContext, file);
const { type } = config;
this.type = type;
const MODULE = 'module';
const COMMONJS = 'commonjs';
this.MODULE = MODULE;
this.COMMONJS = COMMONJS;
this.availableTypes = [MODULE, COMMONJS];
}
invoke() {
if (!this.availableTypes.includes(this.type)) {
this.log.warning(
__filename,
'Unrecognized type in rule configuration',
this.file
);
return [];
}
const { splitPatch } = this.file;
const data = this.setupData(splitPatch);
const reviewComments = this._reviewData(
data,
this.type === this.MODULE
? /from.*[(|'|"|`].*[)|'|"|`]/
: /require.*\(.*\)/
);
return reviewComments;
}
_reviewData(data, regex) {
const reviewComments = [];
const dataLength = data.length;
for (let index = 0; index < dataLength; index++) {
const row = data[index];
const { trimmedContent } = row;
if (
this.CUSTOM_LINES.includes(trimmedContent) ||
trimmedContent.startsWith(this.HUNK_HEADER_INDICATOR)
) {
continue;
}
const matchResult = trimmedContent.match(regex);
if (!matchResult) {
continue;
}
const matchedFragment = matchResult[0];
if (matchedFragment.includes('index')) {
reviewComments.push(
this.getSingleLineComment({
body: this._getCommentBody(),
index
})
);
}
}
return reviewComments;
}
/**
* @returns {string}
*/
_getCommentBody() {
return `Please **do not** reference file named \`index\` explicitly in ${
this.type === this.MODULE ? 'import' : 'require'
}.`;
}
}
module.exports = ImplicitIndexFileImportRule;