-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFixedLoopLengthCondition.js
170 lines (135 loc) · 4.89 KB
/
FixedLoopLengthCondition.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
const BaseRule = require('src/rules/Base');
class FixedLoopLengthConditionRule extends BaseRule {
/**
* looks after `for`, `while`, `do while` loops and identifies whether condition statement includes reference to `length` property. If it does, line is commented out with description to not call `length` in each iteration suggesting to declare variable before loop.
*
* @param {PatchronContext} patchronContext
* @param {FixedLoopLengthConditionConfig} config
* @param {Patch} file
*/
constructor(patchronContext, config, file) {
super(patchronContext, file);
const { regex } = config;
this.regex = regex;
}
invoke() {
const data = this.setupData(this.file.splitPatch);
const dataLength = data.length;
const reviewComments = [];
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 fixedContent = trimmedContent.startsWith(this.BLOCK_END)
? trimmedContent.slice(1).trim()
: trimmedContent;
if (!this._startsWithStatement(fixedContent)) {
continue;
}
let result = { isValid: true };
if (fixedContent.startsWith('for')) {
result = this._validateForLoop(data, index, fixedContent);
} else if (fixedContent.startsWith('while')) {
result = this._validateWhileLoop(data, index, fixedContent);
}
if (result && !result.isValid) {
reviewComments.push(
this.getSingleLineComment({
body: this._getCommentBody(),
index
})
);
}
if (result?.lastIndex) {
index = result.lastIndex;
}
}
return reviewComments;
}
_startsWithStatement(content) {
const statements = ['for', 'while'];
return statements.some((statement) => content.startsWith(statement));
}
_validateForLoop(data, index, fixedContent) {
const isSingleLine = fixedContent.match(/for.*\(.*;.*;.*\)/);
if (isSingleLine) {
const firstSemicolon = fixedContent.indexOf(';');
const secondSemicolon = fixedContent.indexOf(
';',
firstSemicolon + 1
);
const conditionStatement = fixedContent.slice(
firstSemicolon,
secondSemicolon + 1
);
return {
isValid: this._matchLengthReference(conditionStatement) === null
};
} else {
return this._isFragmentedLoopValid(data, index, 'for');
}
}
_validateWhileLoop(data, index, fixedContent) {
const isSingleLine = fixedContent.match(/while.*\(.*\)/);
if (isSingleLine) {
return {
isValid: this._matchLengthReference(fixedContent) === null
};
} else {
return this._isFragmentedLoopValid(data, index, 'while');
}
}
_isFragmentedLoopValid(data, index, loopType) {
let isValid = true;
let lastIndex = index;
const dataLength = data.length;
const nextRowIndex =
loopType === 'for'
? this._findForLoopConditionStatement(data, index)
: index + 1;
if (nextRowIndex > dataLength || nextRowIndex < 0) {
return null;
}
const { indentation } = data[nextRowIndex];
for (
lastIndex = nextRowIndex + 1;
lastIndex < dataLength;
lastIndex++
) {
const row = data[lastIndex];
if (row.indentation !== indentation) {
break;
}
if (this._matchLengthReference(row.trimmedContent)) {
isValid = false;
break;
}
}
return {
isValid,
lastIndex
};
}
_findForLoopConditionStatement(data, startIndex) {
const conditionStatementIndex = data.findIndex(
({ trimmedContent, index: rowIndex }) =>
rowIndex > startIndex && trimmedContent.endsWith(';')
);
return conditionStatementIndex > 0 ? conditionStatementIndex + 1 : -1;
}
_matchLengthReference(content) {
return content.match(this.regex);
}
/**
* @returns {string}
*/
_getCommentBody() {
return `It seems that \`length\` property is initialized in condition statement. Please initialize it before loop.`;
}
}
module.exports = FixedLoopLengthConditionRule;