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

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.
@@ -12,7 +12,8 @@ const {
12
12
  getNodeModulesPath
13
13
  } = require('../../utils/General/getNodeModulesPath');
14
14
  const {
15
- filterFiles
15
+ filterFiles,
16
+ filterWarningInFile
16
17
  } = require('../../utils/FileAndFolderOperations/filterFiles');
17
18
  const {
18
19
  Logger
@@ -28,7 +29,8 @@ const {
28
29
  getSupportedLanguage
29
30
  } = require('../../utils/General/getGeneralInfo');
30
31
  const {
31
- impactBasedPrecommit
32
+ impactBasedPrecommit,
33
+ shouldWarningsAbortCommit
32
34
  } = getConfigurationPrecommit();
33
35
 
34
36
  /**
@@ -129,6 +131,21 @@ function areAllPluginsInstalled() {
129
131
  return unInstalledPlugins.length === 0 ? true : false;
130
132
  }
131
133
 
134
+ /**
135
+ * @function {isOnlyWarningsPresentInFile} - method that checks if only eslint warnings are present in a file
136
+ * @returns {Boolean} - returns boolean based on only warnings present or not in file
137
+ */
138
+ function isOnlyWarningsPresentInFile(eslintErrorsPresentInFile) {
139
+ let severityOfEachErrorInFile = [];
140
+ let startIndex = 1;
141
+ let endIndex = eslintErrorsPresentInFile.length - 2;
142
+ eslintErrorsPresentInFile.slice(startIndex, endIndex).map(error => {
143
+ let partsInString = error.split(" ");
144
+ severityOfEachErrorInFile.push(partsInString[1]);
145
+ });
146
+ return severityOfEachErrorInFile.includes('error');
147
+ }
148
+
132
149
  /**
133
150
  * @function {preCommitHook} - method execute pre commit hook
134
151
  * @returns {void}
@@ -170,19 +187,21 @@ async function preCommitHook() {
170
187
  let currentFileName = staged_files[file];
171
188
  let changedLinesArray = [];
172
189
  let eslintErrorsInChangedLines = [];
190
+ let isOnlyEslintWarningsPresentInFile = false;
173
191
  if (getSupportedLanguage().includes(path.extname(staged_files[file]))) {
174
192
  try {
175
- let eslintErrorsInFile = await findEslintErrors(staged_files[file]);
193
+ var eslintErrorsInFile = await findEslintErrors(staged_files[file]);
194
+ // eslintErrorsInFile = impactBasedPrecommit == false ? filterWarningInFile(eslintErrorsInFile) : eslintErrorsInFile
195
+ isOnlyEslintWarningsPresentInFile = isOnlyWarningsPresentInFile(eslintErrorsInFile);
176
196
  if (staged_files[file] && typeof staged_files[file] == 'string') {
177
197
  if (!eslintErrorsInFile.length == 0) {
178
- //git diff is computed and stored in an array
179
- let git_diff = await calculateGitDiffForFile(current_branch, staged_files[file]);
180
- changedLinesArray = git_diff.filter(line => line.startsWith('@@'));
181
- let changedLinesStartArray = [];
182
- let changedLinesEndArray = [];
183
-
184
198
  //Calculating changed lines in a file and storing them in respective arrays
185
199
  if (impactBasedPrecommit) {
200
+ //git diff is computed and stored in an array
201
+ let git_diff = await calculateGitDiffForFile(current_branch, staged_files[file]);
202
+ changedLinesArray = git_diff.filter(line => line.startsWith('@@'));
203
+ let changedLinesStartArray = [];
204
+ let changedLinesEndArray = [];
186
205
  for (let number of changedLinesArray) {
187
206
  let changesStartLine = parseInt(number.split(' ')[2].split(',')[0]);
188
207
  changedLinesStartArray.push(changesStartLine);
@@ -210,15 +229,23 @@ async function preCommitHook() {
210
229
  for (let eslintError of eslintErrorsInChangedLines) {
211
230
  Logger.log(Logger.FAILURE_TYPE, `\x1b[37m${eslintError.trimEnd()}\x1b[0m`);
212
231
  }
213
- hasEslintErrorsInChangedLines = true;
232
+ if (shouldWarningsAbortCommit) {
233
+ hasEslintErrorsInChangedLines = true;
234
+ } else if (!shouldWarningsAbortCommit && isOnlyEslintWarningsPresentInFile) {
235
+ hasEslintErrorsInChangedLines = false;
236
+ }
214
237
  }
215
238
  } else {
216
239
  if (eslintErrorsInFile.length > 0) {
217
240
  Logger.log(Logger.FAILURE_TYPE, `\x1b[1m${currentFileName}\x1b[0m`);
218
- for (let eslintError of eslintErrorsInChangedLines) {
241
+ for (let eslintError of eslintErrorsInFile) {
219
242
  Logger.log(Logger.FAILURE_TYPE, `\x1b[37m${eslintError.trimEnd()}\x1b[0m`);
220
243
  }
221
- hasEslintErrorsInFiles = true;
244
+ if (shouldWarningsAbortCommit) {
245
+ hasEslintErrorsInFiles = true;
246
+ } else if (!shouldWarningsAbortCommit && isOnlyEslintWarningsPresentInFile) {
247
+ hasEslintErrorsInFiles = false;
248
+ }
222
249
  }
223
250
  }
224
251
  }
@@ -235,10 +262,10 @@ async function preCommitHook() {
235
262
  } catch {
236
263
  Logger.log(Logger.INFO_TYPE, 'Error executing pre commit hook');
237
264
  }
238
- if (hasEslintErrorsInChangedLines) {
265
+ if (hasEslintErrorsInChangedLines && areFilesStaged) {
239
266
  Logger.log(Logger.FAILURE_TYPE, `There are eslint errors present. So commit is aborted`);
240
267
  process.exit(1);
241
- } else if (hasEslintErrorsInFiles) {
268
+ } else if (hasEslintErrorsInFiles && areFilesStaged) {
242
269
  Logger.log(Logger.FAILURE_TYPE, `There are eslint errors present. So commit is aborted`);
243
270
  process.exit(1);
244
271
  } else if (!hasEslintErrorsInFiles && !hasEslintErrorsInChangedLines && areFilesStaged) {
@@ -30,6 +30,7 @@ module.exports = {
30
30
  tsConfigurationPath: path.resolve(process.cwd(), 'tsconfig.json'),
31
31
  projectId: `project-id`,
32
32
  impactBasedPrecommit: true,
33
+ shouldWarningsAbortCommit: false,
33
34
  token: "w-OkG3f5OOM1Rkly8phZ",
34
35
  compareBranch: 'release'
35
36
  };
@@ -36,6 +36,15 @@ function filterFiles(arrayOfFilesToBeFiltered, filesToBeRemoved, isConfigFileNee
36
36
  return arrayOfFilesToBeFiltered;
37
37
  }
38
38
  }
39
+
40
+ /**
41
+ * @function filterWarningInFile - filter warning in files
42
+ * @returns {Array<string>}
43
+ */
44
+ function filterWarningInFile(array) {
45
+ // handle error in file
46
+ }
39
47
  module.exports = {
40
- filterFiles
48
+ filterFiles,
49
+ filterWarningInFile
41
50
  };
@@ -35,7 +35,7 @@ function getEnv() {
35
35
  function getConfigPath() {
36
36
  const configPath = path.resolve(process.cwd(), 'lint.config.js');
37
37
  const defaultConfigPath = path.resolve(__dirname, '..', '..', 'setup', 'sample.config.js');
38
- return fs.exitSync(configPath) ? configPath : defaultConfigPath;
38
+ return fs.existsSync(configPath) ? configPath : defaultConfigPath;
39
39
  }
40
40
 
41
41
  /**
@@ -52,10 +52,10 @@ function getConfigurationPrecommit() {
52
52
  */
53
53
  function getSupportedLanguage() {
54
54
  const _language = [];
55
- _language.push('js');
56
- _language.push('jsx');
57
- _language.push('ts');
58
- _language.push('tsx');
55
+ _language.push('.js');
56
+ _language.push('.jsx');
57
+ _language.push('.ts');
58
+ _language.push('.tsx');
59
59
  return _language;
60
60
  }
61
61
  module.exports = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zohodesk/codestandard-validator",
3
- "version": "0.0.2-exp-7",
3
+ "version": "0.0.2-exp-9",
4
4
  "description": "library to enforce code standard using eslint",
5
5
  "main": "index.js",
6
6
  "scripts": {