@zohodesk/codestandard-validator 0.0.2-exp-9 → 0.0.2-exp-10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -135,15 +135,14 @@ function areAllPluginsInstalled() {
|
|
|
135
135
|
* @function {isOnlyWarningsPresentInFile} - method that checks if only eslint warnings are present in a file
|
|
136
136
|
* @returns {Boolean} - returns boolean based on only warnings present or not in file
|
|
137
137
|
*/
|
|
138
|
-
function isOnlyWarningsPresentInFile(
|
|
138
|
+
function isOnlyWarningsPresentInFile(eslintErrorsPresent) {
|
|
139
139
|
let severityOfEachErrorInFile = [];
|
|
140
|
-
|
|
141
|
-
let endIndex = eslintErrorsPresentInFile.length - 2;
|
|
142
|
-
eslintErrorsPresentInFile.slice(startIndex, endIndex).map(error => {
|
|
140
|
+
eslintErrorsPresent.map(error => {
|
|
143
141
|
let partsInString = error.split(" ");
|
|
144
|
-
|
|
142
|
+
let severityOfError = partsInString.find(word => word === 'error' || word === 'warning');
|
|
143
|
+
severityOfEachErrorInFile.push(severityOfError);
|
|
145
144
|
});
|
|
146
|
-
return severityOfEachErrorInFile.includes('error');
|
|
145
|
+
return !severityOfEachErrorInFile.includes('error');
|
|
147
146
|
}
|
|
148
147
|
|
|
149
148
|
/**
|
|
@@ -167,6 +166,7 @@ async function preCommitHook() {
|
|
|
167
166
|
let hasEslintErrorsInChangedLines = false;
|
|
168
167
|
let hasEslintErrorsInFiles = false;
|
|
169
168
|
let areFilesStaged = false;
|
|
169
|
+
let shouldAbortCommit = false;
|
|
170
170
|
try {
|
|
171
171
|
current_branch = await getBranchName();
|
|
172
172
|
} catch {
|
|
@@ -192,7 +192,6 @@ async function preCommitHook() {
|
|
|
192
192
|
try {
|
|
193
193
|
var eslintErrorsInFile = await findEslintErrors(staged_files[file]);
|
|
194
194
|
// eslintErrorsInFile = impactBasedPrecommit == false ? filterWarningInFile(eslintErrorsInFile) : eslintErrorsInFile
|
|
195
|
-
isOnlyEslintWarningsPresentInFile = isOnlyWarningsPresentInFile(eslintErrorsInFile);
|
|
196
195
|
if (staged_files[file] && typeof staged_files[file] == 'string') {
|
|
197
196
|
if (!eslintErrorsInFile.length == 0) {
|
|
198
197
|
//Calculating changed lines in a file and storing them in respective arrays
|
|
@@ -225,26 +224,39 @@ async function preCommitHook() {
|
|
|
225
224
|
}
|
|
226
225
|
}
|
|
227
226
|
if (eslintErrorsInChangedLines.length > 0) {
|
|
227
|
+
isOnlyEslintWarningsPresentInFile = isOnlyWarningsPresentInFile(eslintErrorsInChangedLines);
|
|
228
228
|
Logger.log(Logger.FAILURE_TYPE, `\x1b[1m${currentFileName}\x1b[0m`);
|
|
229
229
|
for (let eslintError of eslintErrorsInChangedLines) {
|
|
230
230
|
Logger.log(Logger.FAILURE_TYPE, `\x1b[37m${eslintError.trimEnd()}\x1b[0m`);
|
|
231
231
|
}
|
|
232
232
|
if (shouldWarningsAbortCommit) {
|
|
233
233
|
hasEslintErrorsInChangedLines = true;
|
|
234
|
+
shouldAbortCommit = true;
|
|
234
235
|
} else if (!shouldWarningsAbortCommit && isOnlyEslintWarningsPresentInFile) {
|
|
235
236
|
hasEslintErrorsInChangedLines = false;
|
|
237
|
+
} else if (!shouldWarningsAbortCommit && !isOnlyEslintWarningsPresentInFile) {
|
|
238
|
+
hasEslintErrorsInChangedLines = true;
|
|
239
|
+
shouldAbortCommit = true;
|
|
236
240
|
}
|
|
237
241
|
}
|
|
238
242
|
} else {
|
|
239
243
|
if (eslintErrorsInFile.length > 0) {
|
|
244
|
+
let startIndex = 1;
|
|
245
|
+
let endIndex = eslintErrorsInFile.length - 2;
|
|
246
|
+
let listOsEslintErrors = eslintErrorsInFile.slice(startIndex, endIndex);
|
|
247
|
+
isOnlyEslintWarningsPresentInFile = isOnlyWarningsPresentInFile(listOsEslintErrors);
|
|
240
248
|
Logger.log(Logger.FAILURE_TYPE, `\x1b[1m${currentFileName}\x1b[0m`);
|
|
241
|
-
for (let eslintError of
|
|
249
|
+
for (let eslintError of listOsEslintErrors) {
|
|
242
250
|
Logger.log(Logger.FAILURE_TYPE, `\x1b[37m${eslintError.trimEnd()}\x1b[0m`);
|
|
243
251
|
}
|
|
244
252
|
if (shouldWarningsAbortCommit) {
|
|
245
253
|
hasEslintErrorsInFiles = true;
|
|
254
|
+
shouldAbortCommit = true;
|
|
246
255
|
} else if (!shouldWarningsAbortCommit && isOnlyEslintWarningsPresentInFile) {
|
|
247
256
|
hasEslintErrorsInFiles = false;
|
|
257
|
+
} else if (!shouldWarningsAbortCommit && !isOnlyEslintWarningsPresentInFile) {
|
|
258
|
+
hasEslintErrorsInFiles = true;
|
|
259
|
+
shouldAbortCommit = true;
|
|
248
260
|
}
|
|
249
261
|
}
|
|
250
262
|
}
|
|
@@ -262,13 +274,18 @@ async function preCommitHook() {
|
|
|
262
274
|
} catch {
|
|
263
275
|
Logger.log(Logger.INFO_TYPE, 'Error executing pre commit hook');
|
|
264
276
|
}
|
|
265
|
-
if (
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
277
|
+
if (shouldAbortCommit) {
|
|
278
|
+
if (hasEslintErrorsInChangedLines && areFilesStaged) {
|
|
279
|
+
Logger.log(Logger.FAILURE_TYPE, `There are eslint errors present. So commit is aborted`);
|
|
280
|
+
process.exit(1);
|
|
281
|
+
} else if (hasEslintErrorsInFiles && areFilesStaged) {
|
|
282
|
+
Logger.log(Logger.FAILURE_TYPE, `There are eslint errors present. So commit is aborted`);
|
|
283
|
+
process.exit(1);
|
|
284
|
+
} else if (!hasEslintErrorsInFiles && !hasEslintErrorsInChangedLines && areFilesStaged) {
|
|
285
|
+
Logger.log(Logger.SUCCESS_TYPE, `Commit Successful`);
|
|
286
|
+
process.exit(0);
|
|
287
|
+
}
|
|
288
|
+
} else if (!shouldAbortCommit && areFilesStaged) {
|
|
272
289
|
Logger.log(Logger.SUCCESS_TYPE, `Commit Successful`);
|
|
273
290
|
process.exit(0);
|
|
274
291
|
}
|