@sap/eslint-plugin-cds 2.1.1 → 2.3.2

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.
@@ -0,0 +1,87 @@
1
+ /**
2
+ * Levenshtein distance algorithm using recursive calls and cache
3
+ *
4
+ * @param input search the list for a best match for this string
5
+ * @param list a list of strings to match input against
6
+ * @param log logging method to use, might be null if no logging is wanted
7
+ * @returns array with best matches, is never null but might be empty in case no search was possible
8
+ */
9
+
10
+
11
+ const cache = {};
12
+
13
+
14
+ module.exports = (input, list, log) => {
15
+ let minDistWords = [];
16
+
17
+ if (input.length > 50 || list.length > 50) {
18
+ return minDistWords;
19
+ }
20
+
21
+ let minDist = Number.MAX_SAFE_INTEGER;
22
+
23
+ log && log('\nword\t\tlevDist\t\ttime(ms)');
24
+
25
+ let runtime = 0;
26
+
27
+ for (const word of list) {
28
+ const start = log && Date.now();
29
+ const levDist = levDistance(input, word);
30
+
31
+ if (log) {
32
+ const duration = Date.now() - start;
33
+ runtime = runtime + duration;
34
+ log(`${word}\t\t${levDist}\t\t${duration}`);
35
+ }
36
+
37
+ if (levDist === minDist) {
38
+ minDistWords.push(word);
39
+ }
40
+
41
+ if (levDist < minDist) {
42
+ minDist = levDist;
43
+ minDistWords = [word];
44
+ }
45
+ }
46
+
47
+ log && log(`runtime: ${runtime}ms`);
48
+
49
+ return minDistWords.sort();
50
+ }
51
+
52
+
53
+ const levDistance = (a, b) => {
54
+
55
+ if (cache[a] && cache[a][b]) {
56
+ return cache[a][b];
57
+ }
58
+
59
+ if (a.length === 0) {
60
+ return addToCache(a, b, b.length);
61
+ }
62
+
63
+ if (b.length === 0) {
64
+ return addToCache(a, b, a.length);
65
+ }
66
+
67
+ const tail_a = a.substring(1);
68
+ const tail_b = b.substring(1);
69
+
70
+ if (a[0] === b[0]) {
71
+ return levDistance(tail_a, tail_b);
72
+ }
73
+
74
+ const lev1 = levDistance(tail_a, b);
75
+ const lev2 = levDistance(a, tail_b);
76
+ const lev3 = levDistance(tail_a, tail_b);
77
+
78
+ const levDist = Math.min(lev1, lev2, lev3) + 1;
79
+ return addToCache(a, b, levDist);
80
+ }
81
+
82
+
83
+ const addToCache = (a, b, value) => {
84
+ cache[a] = cache[a] || {};
85
+ cache[a][b] = value;
86
+ return value;
87
+ }
@@ -1,17 +1,32 @@
1
+ const { files, envFiles, modelFiles } = require("../constants");
2
+
1
3
  module.exports = {
4
+
2
5
  /**
3
- * Checks whether plugin is used in 'test' mode
6
+ * Checks whether the given file path contains a file extension allowed by
7
+ * the plugin
8
+ * @param {string} filePath
4
9
  * @returns boolean
5
10
  */
6
- isTest: function () {
7
- let isTest = false;
8
- if (process.argv[1].includes("jest") || process.argv[1].includes("mocha")) {
9
- isTest = true;
11
+ isValidFile: function (filePath, key = "") {
12
+ function genRegex(key) {
13
+ return new RegExp(
14
+ `${key
15
+ .map((file) => {
16
+ return file.replace("*", "");
17
+ })
18
+ .join("$|")}$`
19
+ );
20
+ }
21
+ if (key === "model") {
22
+ return genRegex(modelFiles).test(filePath);
23
+ } else if (key === "env") {
24
+ return genRegex(envFiles).test(filePath);
25
+ } else {
26
+ return genRegex(files).test(filePath);
10
27
  }
11
- return isTest;
12
28
  },
13
29
 
14
-
15
30
  /**
16
31
  * Checks whether the plugin is run via the VS Code ESLint extension (editor)
17
32
  * @returns boolean
@@ -21,7 +36,16 @@ module.exports = {
21
36
  },
22
37
 
23
38
  /**
24
- * Prints a given message string in the styles provided
39
+ * Returns an array of allowed file extensions
40
+ * the plugin can parse of the form "*.ext"
41
+ * @returns {ConfigOverrideFiles} Array of file extensions
42
+ */
43
+ getFileExtensions: function () {
44
+ return files;
45
+ },
46
+
47
+ /**
48
+ * Prints a formatted message string according to the styles provided
25
49
  * @param msg message to print
26
50
  * @param styles array of styles for apply
27
51
  * @returns
@@ -41,5 +65,4 @@ module.exports = {
41
65
  });
42
66
  return `${msgStyle}${msg}${types.reset}`;
43
67
  },
44
-
45
68
  };