-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComparisionOperatorLevel.js
195 lines (162 loc) · 6.01 KB
/
ComparisionOperatorLevel.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
const isEqual = require('lodash/isEqual');
const BaseRule = require('src/rules/Base');
class ComparisionOperatorLevelRule extends BaseRule {
/**
* allows to set expected equality/inequality convention. Bear in mind that rule does not implement any way to deduce whether part of patch is pure text or part of code (e.g. in case of Vue). An workaround to that could be to escape `=` characters in strings and use `=` unicode representation in HTML.
*
* **allowedLevels** options:
* - 0 - weak equality/inequality (==, !=)
* - 1 - strict equality/inequality (===, !==)
* - 2 - strict equality/inequality via `Object.is` (ES6)
*
* @param {PatchronContext} patchronContext
* @param {ComparisionOperatorLevelConfig} config
* @param {Patch} file
*
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals}
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness}
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Strict_inequality}
*/
constructor(patchronContext, config, file) {
super(patchronContext, file);
const { allowedLevels } = config;
allowedLevels.sort();
const defaultPatterns = [
{
level: 0,
text: '==, !=',
regexes: [
/[\sa-zA-Z0-9]==[\sa-zA-Z0-9]/g,
/[\sa-zA-Z0-9]!=[\sa-zA-Z0-9]/g
]
},
{
level: 1,
text: '===, !==',
regexes: [
/[\sa-zA-Z0-9]===[\sa-zA-Z0-9]/g,
/[\sa-zA-Z0-9]!==[\sa-zA-Z0-9]/g
]
},
{
level: 2,
text: 'Object.is',
regexes: [/(?:Object.is\(.*\))|(?:Object.is\()/g]
}
];
this.allowedLevels = allowedLevels;
this.defaultPatterns = defaultPatterns;
}
invoke() {
if (!this.allowedLevels?.length) {
return [];
}
if (isEqual(this.allowedLevels, [0, 1, 2])) {
this.log.warning(
__filename,
`All comparision styles are allowed, nothing to do.`,
this.file
);
return [];
}
const { splitPatch } = this.file;
const data = this.setupData(splitPatch, {
withBackticks: {
settings: {
abortOnUnevenBackticksCountInPatch: true
}
}
});
if (!this._hasAnyMatchingComparision(data)) {
return [];
}
const reviewComments = this._reviewData(data);
return reviewComments;
}
_hasAnyMatchingComparision(data) {
return data.some(({ trimmedContent }) =>
this.defaultPatterns.some((defaultPattern) =>
trimmedContent.match(defaultPattern)
)
);
}
_reviewData(data) {
const reviewComments = [];
const dataLength = data.length;
for (let index = 0; index < dataLength; index++) {
const row = data[index];
const backticks = row?.backticks;
const line = { index, content: row.trimmedContent };
if (
this.CUSTOM_LINES.includes(line.content) ||
line.content.startsWith(this.HUNK_HEADER_INDICATOR)
) {
continue;
}
if (backticks && backticks.index !== backticks.endLineIndex) {
const { endLineIndex } = backticks;
line.endIndex = endLineIndex;
line.content = this.convertMultiLineToSingleLine(
data,
line.index,
endLineIndex
);
index = endLineIndex;
}
const isLineValid = this._isLineValid(line.content);
if (!isLineValid) {
reviewComments.push(
line?.endIndex
? {
...this.getMultiLineComment({
body: this._getCommentBody(),
from: line.index,
to: line.endIndex
})
}
: {
...this.getSingleLineComment({
body: this._getCommentBody(),
index: line.index
})
}
);
}
}
return reviewComments;
}
_isLineValid(line) {
let isRowValid = true;
const defaultPatternsLength = this.defaultPatterns.length;
for (let index = 0; index < defaultPatternsLength; index++) {
const { regexes, level } = this.defaultPatterns[index];
if (this.allowedLevels.includes(level)) {
continue;
}
for (const regex of regexes) {
const matches = [...line.matchAll(regex)];
if (matches.length) {
isRowValid = false;
}
}
if (!isRowValid) {
break;
}
}
return isRowValid;
}
/**
* @returns {string}
*/
_getCommentBody() {
const allowedPatterns = this.defaultPatterns
.map(({ text, level }) => {
return this.allowedLevels.includes(level) ? text : null;
})
.filter((text) => text);
return this
.dedent(`It seems that marked fragment includes comparision pattern. If it's raw text, ignore this comment or consider using unicode representation of = character or escape it with backslash.
Allowed comparision patterns: (${allowedPatterns.join(', ')})`);
}
}
module.exports = ComparisionOperatorLevelRule;