|
| 1 | +import { getLintAnnotations } from "components/editorComponents/CodeEditor/lintHelpers"; |
| 2 | +import { LINT_BINDING_LITERAL_MATCH_ERROR_CODE } from "plugins/Linting/constants"; |
| 3 | +import { DebuggerLogType } from "../../types"; |
| 4 | +import { |
| 5 | + getLintBindingPrefix, |
| 6 | + LINT_BINDING_PREFIX_LENGTH, |
| 7 | + mapCompileErrorsToLintErrors, |
| 8 | +} from "./mapCompileErrorsToLintErrors"; |
| 9 | + |
| 10 | +describe("getLintBindingPrefix", () => { |
| 11 | + it("returns a space for empty source", () => { |
| 12 | + expect(getLintBindingPrefix("")).toEqual({ |
| 13 | + value: " ", |
| 14 | + useLiteralMatch: false, |
| 15 | + }); |
| 16 | + }); |
| 17 | + |
| 18 | + it("returns the full string when shorter than the prefix length", () => { |
| 19 | + const js = "const a = 1;"; |
| 20 | + |
| 21 | + expect(getLintBindingPrefix(js)).toEqual({ |
| 22 | + value: js, |
| 23 | + useLiteralMatch: false, |
| 24 | + }); |
| 25 | + }); |
| 26 | + |
| 27 | + it("truncates to at most LINT_BINDING_PREFIX_LENGTH", () => { |
| 28 | + const js = "a".repeat(LINT_BINDING_PREFIX_LENGTH + 50); |
| 29 | + const result = getLintBindingPrefix(js); |
| 30 | + |
| 31 | + expect(result.value.length).toBeLessThanOrEqual(LINT_BINDING_PREFIX_LENGTH); |
| 32 | + expect(result.useLiteralMatch).toBe(true); |
| 33 | + }); |
| 34 | + |
| 35 | + it("trims a mid-word cut so the prefix still matches at index 0", () => { |
| 36 | + const js = |
| 37 | + 'import React from "https://esm.sh/react@18.2.0";\n' + |
| 38 | + "const filler = `" + |
| 39 | + "x".repeat(400) + |
| 40 | + "`;\n"; |
| 41 | + const { useLiteralMatch, value: prefix } = getLintBindingPrefix(js); |
| 42 | + |
| 43 | + expect(js.startsWith(prefix)).toBe(true); |
| 44 | + expect(prefix.length).toBeLessThanOrEqual(LINT_BINDING_PREFIX_LENGTH); |
| 45 | + // Must not end mid-run of x's while more x's follow |
| 46 | + expect(prefix.endsWith("x")).toBe(false); |
| 47 | + expect(useLiteralMatch).toBe(false); |
| 48 | + }); |
| 49 | + |
| 50 | + it("opts into literal match when the first 128 chars are one unbroken word", () => { |
| 51 | + const js = "a".repeat(LINT_BINDING_PREFIX_LENGTH + 200) + "\nfunction\n"; |
| 52 | + const result = getLintBindingPrefix(js); |
| 53 | + |
| 54 | + expect(result.value).toBe("a".repeat(LINT_BINDING_PREFIX_LENGTH)); |
| 55 | + expect(result.useLiteralMatch).toBe(true); |
| 56 | + }); |
| 57 | +}); |
| 58 | + |
| 59 | +describe("mapCompileErrorsToLintErrors", () => { |
| 60 | + const babelError = { |
| 61 | + line: 10, |
| 62 | + column: 4, |
| 63 | + message: "SyntaxError: Unexpected token", |
| 64 | + }; |
| 65 | + |
| 66 | + it("returns an empty array when debuggerLogs is undefined", () => { |
| 67 | + expect(mapCompileErrorsToLintErrors(undefined, "const a = 1;")).toEqual([]); |
| 68 | + }); |
| 69 | + |
| 70 | + it("maps located compile errors with a short binding prefix", () => { |
| 71 | + const largeJs = |
| 72 | + 'import React from "https://esm.sh/react@18.2.0";\n' + |
| 73 | + "const filler = `" + |
| 74 | + "x".repeat(40000) + |
| 75 | + "`;\nfunction\n"; |
| 76 | + |
| 77 | + const mapped = mapCompileErrorsToLintErrors( |
| 78 | + [ |
| 79 | + { |
| 80 | + type: DebuggerLogType.ERROR, |
| 81 | + args: [babelError], |
| 82 | + }, |
| 83 | + ], |
| 84 | + largeJs, |
| 85 | + ); |
| 86 | + |
| 87 | + expect(mapped).toHaveLength(1); |
| 88 | + expect(mapped[0].originalBinding.length).toBeLessThanOrEqual( |
| 89 | + LINT_BINDING_PREFIX_LENGTH, |
| 90 | + ); |
| 91 | + expect(mapped[0].originalBinding).not.toEqual(largeJs); |
| 92 | + expect(mapped[0].raw).toEqual(mapped[0].originalBinding); |
| 93 | + expect(mapped[0].errorSegment).toEqual(mapped[0].originalBinding); |
| 94 | + expect(mapped[0].line).toBe(9); |
| 95 | + expect(mapped[0].ch).toBe(6); |
| 96 | + expect(mapped[0].code).toBe(""); |
| 97 | + }); |
| 98 | + |
| 99 | + it("does not throw in getLintAnnotations for a large source with a syntax error", () => { |
| 100 | + const largeJs = |
| 101 | + 'import React from "https://esm.sh/react@18.2.0";\n' + |
| 102 | + "const filler = `" + |
| 103 | + "x".repeat(40000) + |
| 104 | + "`;\nfunction\n"; |
| 105 | + |
| 106 | + const mapped = mapCompileErrorsToLintErrors( |
| 107 | + [ |
| 108 | + { |
| 109 | + type: DebuggerLogType.ERROR, |
| 110 | + args: [{ line: 3, column: 0, message: "Unexpected token" }], |
| 111 | + }, |
| 112 | + ], |
| 113 | + largeJs, |
| 114 | + ); |
| 115 | + |
| 116 | + expect(() => getLintAnnotations(largeJs, mapped, {})).not.toThrow(); |
| 117 | + |
| 118 | + const annotations = getLintAnnotations(largeJs, mapped, {}); |
| 119 | + |
| 120 | + expect(annotations.length).toBeGreaterThan(0); |
| 121 | + expect(annotations[0].from?.line).toBe(2); |
| 122 | + }); |
| 123 | + |
| 124 | + it("still produces an annotation for a small default-template sized source", () => { |
| 125 | + const smallJs = `import React from "https://esm.sh/react@18.2.0"; |
| 126 | +import ReactDOM from "https://esm.sh/react-dom@18.2.0"; |
| 127 | +
|
| 128 | +function App() { |
| 129 | + return <div>hi</div>; |
| 130 | +} |
| 131 | +
|
| 132 | +appsmith.onReady(() => { |
| 133 | + ReactDOM.render(<App />, document.getElementById("root")); |
| 134 | +}); |
| 135 | +function |
| 136 | +`; |
| 137 | + |
| 138 | + const mapped = mapCompileErrorsToLintErrors( |
| 139 | + [ |
| 140 | + { |
| 141 | + type: DebuggerLogType.ERROR, |
| 142 | + args: [{ line: 11, column: 0, message: "Unexpected token" }], |
| 143 | + }, |
| 144 | + ], |
| 145 | + smallJs, |
| 146 | + ); |
| 147 | + |
| 148 | + expect(mapped[0].originalBinding.length).toBeLessThanOrEqual( |
| 149 | + LINT_BINDING_PREFIX_LENGTH, |
| 150 | + ); |
| 151 | + |
| 152 | + const annotations = getLintAnnotations(smallJs, mapped, {}); |
| 153 | + |
| 154 | + expect(annotations.length).toBeGreaterThan(0); |
| 155 | + expect(annotations[0].from?.line).toBe(10); |
| 156 | + }); |
| 157 | + |
| 158 | + it("produces an underline when the source starts with more than 128 word characters", () => { |
| 159 | + const js = "a".repeat(LINT_BINDING_PREFIX_LENGTH + 200) + "\nfunction\n"; |
| 160 | + const errorLine = 2; // 1-based Babel line of `function` |
| 161 | + |
| 162 | + const mapped = mapCompileErrorsToLintErrors( |
| 163 | + [ |
| 164 | + { |
| 165 | + type: DebuggerLogType.ERROR, |
| 166 | + args: [{ line: errorLine, column: 0, message: "Unexpected token" }], |
| 167 | + }, |
| 168 | + ], |
| 169 | + js, |
| 170 | + ); |
| 171 | + |
| 172 | + expect(mapped[0].code).toBe(LINT_BINDING_LITERAL_MATCH_ERROR_CODE); |
| 173 | + expect(mapped[0].originalBinding.length).toBe(LINT_BINDING_PREFIX_LENGTH); |
| 174 | + |
| 175 | + const annotations = getLintAnnotations(js, mapped, {}); |
| 176 | + |
| 177 | + expect(annotations.length).toBeGreaterThan(0); |
| 178 | + expect(annotations[0].from?.line).toBe(errorLine - 1); |
| 179 | + }); |
| 180 | + |
| 181 | + it("skips errors without line or column", () => { |
| 182 | + const mapped = mapCompileErrorsToLintErrors( |
| 183 | + [ |
| 184 | + { |
| 185 | + type: DebuggerLogType.ERROR, |
| 186 | + args: [{ message: "no location" }], |
| 187 | + }, |
| 188 | + ], |
| 189 | + "const a = 1;", |
| 190 | + ); |
| 191 | + |
| 192 | + expect(mapped).toEqual([]); |
| 193 | + }); |
| 194 | +}); |
0 commit comments