|
| 1 | +import { Severity } from "entities/AppsmithConsole"; |
| 2 | +import { |
| 3 | + EvaluationError, |
| 4 | + PropertyEvaluationErrorType, |
| 5 | +} from "utils/DynamicBindingUtils"; |
| 6 | +import { |
| 7 | + getKeyPositionInString, |
| 8 | + getLintAnnotations, |
| 9 | + getAllWordOccurrences, |
| 10 | +} from "./lintHelpers"; |
| 11 | + |
| 12 | +describe("getAllWordOccurences()", function() { |
| 13 | + it("should get all the indexes", () => { |
| 14 | + const res = getAllWordOccurrences("this is a `this` string", "this"); |
| 15 | + expect(res).toEqual([0, 11]); |
| 16 | + }); |
| 17 | + |
| 18 | + it("should return empty array", () => { |
| 19 | + expect(getAllWordOccurrences("this is a string", "number")).toEqual([]); |
| 20 | + }); |
| 21 | +}); |
| 22 | + |
| 23 | +describe("getKeyPositionsInString()", () => { |
| 24 | + it("should return results for single line string", () => { |
| 25 | + const res = getKeyPositionInString("this is a `this` string", "this"); |
| 26 | + expect(res).toEqual([ |
| 27 | + { line: 0, ch: 0 }, |
| 28 | + { line: 0, ch: 11 }, |
| 29 | + ]); |
| 30 | + }); |
| 31 | + |
| 32 | + it("should return results for multiline string", () => { |
| 33 | + const res = getKeyPositionInString("this is a \n`this` string", "this"); |
| 34 | + expect(res).toEqual([ |
| 35 | + { line: 0, ch: 0 }, |
| 36 | + { line: 1, ch: 1 }, |
| 37 | + ]); |
| 38 | + }); |
| 39 | +}); |
| 40 | + |
| 41 | +describe("getLintAnnotations()", () => { |
| 42 | + const { LINT, PARSE } = PropertyEvaluationErrorType; |
| 43 | + const { ERROR, WARNING } = Severity; |
| 44 | + it("should return proper annotations", () => { |
| 45 | + const value = `Hello {{ world == test }}\n {{text}}`; |
| 46 | + const errors: EvaluationError[] = [ |
| 47 | + { |
| 48 | + errorMessage: "Expected '===' and instead saw '=='.", |
| 49 | + severity: WARNING, |
| 50 | + raw: |
| 51 | + "\n function closedFunction () {\n const result = world == test \n return result;\n }\n closedFunction()\n ", |
| 52 | + errorType: LINT, |
| 53 | + originalBinding: "world == test ", |
| 54 | + errorSegment: " const result = world == test ", |
| 55 | + variables: ["===", "==", null, null], |
| 56 | + code: "W116", |
| 57 | + }, |
| 58 | + { |
| 59 | + errorType: LINT, |
| 60 | + raw: |
| 61 | + "\n function closedFunction () {\n const result = world == test \n return result;\n }\n closedFunction()\n ", |
| 62 | + severity: WARNING, |
| 63 | + errorMessage: "'world' is not defined.", |
| 64 | + errorSegment: " const result = world == test ", |
| 65 | + originalBinding: "world == test ", |
| 66 | + variables: ["world", null, null, null], |
| 67 | + code: "W117", |
| 68 | + }, |
| 69 | + { |
| 70 | + errorType: LINT, |
| 71 | + raw: |
| 72 | + "\n function closedFunction () {\n const result = world == test \n return result;\n }\n closedFunction()\n ", |
| 73 | + severity: WARNING, |
| 74 | + errorMessage: "'test' is not defined.", |
| 75 | + errorSegment: " const result = world == test ", |
| 76 | + originalBinding: "world == test ", |
| 77 | + variables: ["test", null, null, null], |
| 78 | + code: "W117", |
| 79 | + }, |
| 80 | + { |
| 81 | + errorType: PARSE, |
| 82 | + raw: |
| 83 | + "\n function closedFunction () {\n const result = world == test \n return result;\n }\n closedFunction()\n ", |
| 84 | + severity: ERROR, |
| 85 | + errorMessage: "ReferenceError: world is not defined", |
| 86 | + originalBinding: " world == test ", |
| 87 | + }, |
| 88 | + { |
| 89 | + errorMessage: "'text' is not defined.", |
| 90 | + severity: WARNING, |
| 91 | + raw: |
| 92 | + "\n function closedFunction () {\n const result = text\n return result;\n }\n closedFunction()\n ", |
| 93 | + errorType: LINT, |
| 94 | + originalBinding: "text", |
| 95 | + errorSegment: " const result = text", |
| 96 | + variables: ["text", null, null, null], |
| 97 | + code: "W117", |
| 98 | + }, |
| 99 | + { |
| 100 | + errorMessage: "ReferenceError: text is not defined", |
| 101 | + severity: WARNING, |
| 102 | + raw: |
| 103 | + "\n function closedFunction () {\n const result = text\n return result;\n }\n closedFunction()\n ", |
| 104 | + errorType: PARSE, |
| 105 | + originalBinding: "text", |
| 106 | + }, |
| 107 | + ]; |
| 108 | + |
| 109 | + const res = getLintAnnotations(value, errors); |
| 110 | + expect(res).toEqual([ |
| 111 | + { |
| 112 | + from: { |
| 113 | + line: 0, |
| 114 | + ch: 15, |
| 115 | + }, |
| 116 | + to: { |
| 117 | + line: 0, |
| 118 | + ch: 17, |
| 119 | + }, |
| 120 | + message: "Expected '===' and instead saw '=='.", |
| 121 | + severity: "warning", |
| 122 | + }, |
| 123 | + { |
| 124 | + from: { |
| 125 | + line: 0, |
| 126 | + ch: 9, |
| 127 | + }, |
| 128 | + to: { |
| 129 | + line: 0, |
| 130 | + ch: 14, |
| 131 | + }, |
| 132 | + message: "'world' is not defined.", |
| 133 | + severity: "warning", |
| 134 | + }, |
| 135 | + { |
| 136 | + from: { |
| 137 | + line: 0, |
| 138 | + ch: 18, |
| 139 | + }, |
| 140 | + to: { |
| 141 | + line: 0, |
| 142 | + ch: 22, |
| 143 | + }, |
| 144 | + message: "'test' is not defined.", |
| 145 | + severity: "warning", |
| 146 | + }, |
| 147 | + { |
| 148 | + from: { |
| 149 | + line: 1, |
| 150 | + ch: 4, |
| 151 | + }, |
| 152 | + to: { |
| 153 | + line: 1, |
| 154 | + ch: 8, |
| 155 | + }, |
| 156 | + message: "'text' is not defined.", |
| 157 | + severity: "warning", |
| 158 | + }, |
| 159 | + ]); |
| 160 | + }); |
| 161 | +}); |
0 commit comments