diff --git a/app/client/src/components/editorComponents/CodeEditor/lintHelpers.test.ts b/app/client/src/components/editorComponents/CodeEditor/lintHelpers.test.ts index 07c0dc443c..381429411c 100644 --- a/app/client/src/components/editorComponents/CodeEditor/lintHelpers.test.ts +++ b/app/client/src/components/editorComponents/CodeEditor/lintHelpers.test.ts @@ -258,6 +258,57 @@ describe("getLintAnnotations()", () => { }, ]); }); + + it("should use provided lintlength when available", () => { + const value = `{{ variable }}`; + const errors: LintError[] = [ + { + errorType: PropertyEvaluationErrorType.LINT, + raw: "variable", + severity: Severity.WARNING, + errorMessage: { + name: "LintingError", + message: "Lint error message.", + }, + errorSegment: "variable", + originalBinding: "variable", + variables: ["variable"], + code: "W001", + line: 0, + ch: 3, + lintLength: 8, // Provided lint length + }, + ]; + + const annotations = getLintAnnotations(value, errors, {}); + + expect(annotations[0].to?.ch).toBe(13); + }); + + it("should calculate lintlength when not provided", () => { + const value = `{{ variable }}`; + const errors: LintError[] = [ + { + errorType: PropertyEvaluationErrorType.LINT, + raw: "variable", + severity: Severity.WARNING, + errorMessage: { + name: "LintingError", + message: "Lint error message.", + }, + errorSegment: "variable", + originalBinding: "variable", + variables: ["variable"], + code: "W001", + line: 0, + ch: 3, + }, + ]; + + const annotations = getLintAnnotations(value, errors, {}); + + expect(annotations[0].to?.ch).toBe(13); + }); }); describe("getFirstNonEmptyPosition", () => { diff --git a/app/client/src/components/editorComponents/CodeEditor/lintHelpers.ts b/app/client/src/components/editorComponents/CodeEditor/lintHelpers.ts index 65a7e34c27..125e41ebaf 100644 --- a/app/client/src/components/editorComponents/CodeEditor/lintHelpers.ts +++ b/app/client/src/components/editorComponents/CodeEditor/lintHelpers.ts @@ -135,6 +135,7 @@ export const getLintAnnotations = ( code, errorMessage, line, + lintLength, originalBinding, severity, variables, @@ -157,16 +158,20 @@ export const getLintAnnotations = ( }); } - let variableLength = 1; + let calculatedLintLength = 1; + // If lint length is provided, then skip the length calculation logic + if (lintLength && lintLength > 0) { + calculatedLintLength = lintLength; + } // Find the variable with minimal length - if (variables) { + else if (variables) { for (const variable of variables) { if (variable) { - variableLength = - variableLength === 1 + calculatedLintLength = + calculatedLintLength === 1 ? String(variable).length - : Math.min(String(variable).length, variableLength); + : Math.min(String(variable).length, calculatedLintLength); } } } @@ -205,7 +210,7 @@ export const getLintAnnotations = ( }; const to = { line: from.line, - ch: from.ch + variableLength, + ch: from.ch + calculatedLintLength, }; annotations.push({ diff --git a/app/client/src/utils/DynamicBindingUtils.ts b/app/client/src/utils/DynamicBindingUtils.ts index 4e89443bcf..897a674652 100644 --- a/app/client/src/utils/DynamicBindingUtils.ts +++ b/app/client/src/utils/DynamicBindingUtils.ts @@ -436,6 +436,7 @@ export interface LintError extends DataTreeError { line: number; ch: number; originalPath?: string; + lintLength?: number; } export interface DataTreeEvaluationProps {