@zohodesk/codestandard-validator 0.0.2-exp-9 → 0.0.2-exp-11

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(eslintErrorsPresentInFile) {
138
+ function isOnlyWarningsPresentInFile(eslintErrorsPresent) {
139
139
  let severityOfEachErrorInFile = [];
140
- let startIndex = 1;
141
- let endIndex = eslintErrorsPresentInFile.length - 2;
142
- eslintErrorsPresentInFile.slice(startIndex, endIndex).map(error => {
140
+ eslintErrorsPresent.map(error => {
143
141
  let partsInString = error.split(" ");
144
- severityOfEachErrorInFile.push(partsInString[1]);
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 eslintErrorsInFile) {
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,10 @@ async function preCommitHook() {
262
274
  } catch {
263
275
  Logger.log(Logger.INFO_TYPE, 'Error executing pre commit hook');
264
276
  }
265
- if (hasEslintErrorsInChangedLines && areFilesStaged) {
266
- Logger.log(Logger.FAILURE_TYPE, `There are eslint errors present. So commit is aborted`);
267
- process.exit(1);
268
- } else if (hasEslintErrorsInFiles && areFilesStaged) {
269
- Logger.log(Logger.FAILURE_TYPE, `There are eslint errors present. So commit is aborted`);
277
+ if (shouldAbortCommit) {
278
+ Logger.log(Logger.FAILURE_TYPE, `There are eslint errors present. So commit is aborted. If possible try to fix the eslint warnings also.`);
270
279
  process.exit(1);
271
- } else if (!hasEslintErrorsInFiles && !hasEslintErrorsInChangedLines && areFilesStaged) {
280
+ } else {
272
281
  Logger.log(Logger.SUCCESS_TYPE, `Commit Successful`);
273
282
  process.exit(0);
274
283
  }
@@ -14,6 +14,7 @@ const path = require("path");
14
14
  * @property {string} tsConfigurationPath - The path of the ts configuration Path
15
15
  * @property {number} projectId - project id of repository
16
16
  * @property {boolean} impactBasedPrecommit - Indicates if the linting is impact-based in pre commit
17
+ * @property {boolean} shouldWarningsAbortCommit - Indicates if eslint warnings should abort the commit
17
18
  * @property {string} token - Encrypted Authentication Token
18
19
  * @property {string} compareBranch - Branch to compare diff
19
20
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zohodesk/codestandard-validator",
3
- "version": "0.0.2-exp-9",
3
+ "version": "0.0.2-exp-11",
4
4
  "description": "library to enforce code standard using eslint",
5
5
  "main": "index.js",
6
6
  "scripts": {