Subject: [GSOC][PATCH] Userdiff: add builtin patterns for JavaScript.

From: Sergius Nyah <[email-masked]>
Date: Wed, 28 Feb 2024 16:00:34 +0100

This commit introduces builtin patterns for JavaScript in userdiff and adds a new test
case in t4018-diff-funcname.sh to verify the enhanced JavaScript function detection in Git diffs.
Signed-off-by: Sergius Justus Chesami Nyah <[email-masked]>
---
...-PATCH-1-2-Add-builtin-patterns-for-.patch | 53 ++++++-------------
t/t4018-diff-funcname.sh                      | 25 ++++++++-
2 files changed, 38 insertions(+), 40 deletions(-)

diff --git a/Pattern/0001-Subject-GSOC-RFC-PATCH-1-2-Add-builtin-patterns-for-.patch b/Pattern/0001-Subject-GSOC-RFC-PATCH-1-2-Add-builtin-patterns-for-.patch
index 43593866bc..9c3b80665e 100644
--- a/Pattern/0001-Subject-GSOC-RFC-PATCH-1-2-Add-builtin-patterns-for-.patch
+++ b/Pattern/0001-Subject-GSOC-RFC-PATCH-1-2-Add-builtin-patterns-for-.patch
-This patch adds the regular expression for detecting JavaScript functions and expressions in Git diffs. The pattern accurately identifies function declerations, expressions, and definitions inside blocks.
----
+This patch adds the regular expression for detecting JavaScript functions and expressions in Git diffs.
userdiff.c | 17 +++++++++++++++--
1 file changed, 15 insertions(+), 2 deletions(-)

@@ -18,40 +16,19 @@ index e399543823..12e31ff14d 100644
#include "git-compat-util.h"
#include "config.h"
#include "userdiff.h"
#include "strbuf.h"

+PATTERNS("javascript",
-+     /*
++      * Looks for lines that start with optional whitespace, followed
++      * by 'function'* and any characters (for function declarations),
++      * or valid JavaScript identifiers, equals sign '=', 'function' keyword
+       * and any characters (for function expressions).
++      * Also considers functions defined inside blocks with '{...}'.
++      */
+      "^[ \t]*(function[ \t]*.*|[a-zA-Z_$][0-9a-zA-Z_$]*[ \t]*=[ \t]*function[ \t]*.*|(\\{[ \t]*)?)\n",
-+     /* This pattern matches JavaScript identifiers */
-+     "[a-zA-Z_$][0-9a-zA-Z_$]*"
-+     "|[-+0-9.eE]+|0[xX][0-9a-fA-F]+"
-+     "|[-+*/<>%&^|=!:]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\|"),

Here is the test file.

-test_done
+test_expect_success 'identify builtin patterns in Javascript' '
+    # setup
+    echo "function myFunction() { return true; }" > test.js &&
+    echo "var myVar = function() { return false; }" >> test.js &&
+    git add test.js &&
+    git commit -m "add test.js" &&
+
+    # modify the file
+    echo "function myFunction() { return false; }" > test.js &&
+    echo "var myVar = function() { return true; }" >> test.js &&
+
+    # command under test
+    git diff >output &&
+
+    # check results
+    test_i18ngrep "function myFunction() { return true; }" output &&
+    test_i18ngrep "function myFunction() { return false; }" output &&
+    test_i18ngrep "var myVar = function() { return false; }" output &&
+    test_i18ngrep "var myVar = function() { return true; }" output
+'
+
+test_done
--
2.43.2