git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 1/4] userdiff: add javascript diff driver

From
DFDerick W. de M. Frias <derick.william.moraes@gmail.com>
Date
Jun 23, 2025, 06:35 UTC
Message-ID
<20250623090538.154858-2-derick.william.moraes@gmail.com>
In-Reply-To
<20250623090538.154858-1-derick.william.moraes@gmail.com>
Add diff pattern for JavaScript programming language and documentation. 
Signed-off-by: Derick W. de M. Frias <derick.william.moraes@gmail.com>
---
 Documentation/gitattributes.adoc |  2 ++
 userdiff.c                       | 62 ++++++++++++++++++--------------
 2 files changed, 38 insertions(+), 26 deletions(-)
diff --git a/Documentation/gitattributes.adoc b/Documentation/gitattributes.adoc
index f20041a323..b7075ccb29 100644
--- a/Documentation/gitattributes.adoc
+++ b/Documentation/gitattributes.adoc
@@ -891,6 +891,8 @@ patterns are available:
 
 - `java` suitable for source code in the Java language.
 
+- `javascript` suitable for source code in the JavaScript language.
+
 - `kotlin` suitable for source code in the Kotlin language.
 
 - `markdown` suitable for Markdown documents.
diff --git a/userdiff.c b/userdiff.c
index 94134e5b09..0d352bc722 100644
--- a/userdiff.c
+++ b/userdiff.c
@@ -238,33 +238,43 @@ PATTERNS("java",
 	 "|[-+*/<>%&^|=!]="
 	 "|--|\\+\\+|<<=?|>>>?=?|&&|\\|\\|"),
 PATTERNS("javascript",
-     /* conventional named functions */
-     "^[ \t]*(async[ \t]+)?function[ \t]*\\*?[ \t]*([$_a-zA-Z][$_a-zA-Z0-9]*)[ \t]*\\(.*$|"
-     /* assigned functions */
-     "^[ \t]*(const|let|var)[ \t]+([$_a-zA-Z][$_a-zA-Z0-9]*)[ \t]*="
-     "[ \t]*(async[ \t]+)?function[ \t]*\\*?[ \t]*([$_a-zA-Z][$_a-zA-Z0-9]*)?[ \t]*\\(.*$|"
-     /* arrow functions */
-     "^[ \t]*(const|let|var)[ \t]+([$_a-zA-Z][$_a-zA-Z0-9]*)[ \t]*="
-     "[ \t]*(\\([^\\)]*\\)|[$_a-zA-Z][$_a-zA-Z0-9]*)[ \t]*=>[ \t]*\\{?.*$|"
-     /* functions declared inside classes and objects */
-     "^[ \t]*(static[ \t]+)?(async[ \t]+)?(get[ \t]+|set[ \t]+)?\\*?[ \t]*"
-     "([$_a-zA-Z][$_a-zA-Z0-9]*)[ \t]*\\([^)]*\\)[ \t]*\\{.*$",
-     /* identifiers */
-	 "[$_A-Za-z][$_A-Za-z0-9]*|"
-     /* hexadecimal and big hexadecimal */
-     "0[xX](?:[0-9a-fA-F](?:_?[0-9a-fA-F])*)n?|"
-     /* octa and big octa */
-     "0[oO](?:[0-7](?:_?[0-7])*)n?|"
-     /* binary and big binary */
-     "0[bB](?:[01](?:_?[01])*)n?|"
-     /* decimal, floting point and exponent notation (eE) */
-     "(?:0|[1-9](?:_?[0-9])*)(?:\\.(?:[0-9](?:_?[0-9])*))?(?:[eE][+-]?(?:[0-9](?:_?[0-9])*))?|"
-     /* big decimal */
-     "(?:0|[1-9](?:_?[0-9])*)n|"
+	 /* don't match reserved expressions that have function-like syntax */
+	 "!^[ \t]*(if|do|while|for|with|switch|catch|import|return)\n"
+	 /* matches conventional named functions, that can also be async and/or have export */
+	 "^[ \t]*(export[ \t]+)?(async[ \t]+)?function[ \t]*\\*?[ \t]*"
+	 "([$_a-zA-Z][$_a-zA-Z0-9]*)[ \t]*\\(.*$"
+	 /* matches assigned exports */
+	 "|^[ \t]*export[ \t]*(const|default)[ \t]*([$_a-zA-Z][$_a-zA-Z0-9]*)[ \t]*="
+	 /* matches assigned functions */
+	 "|^[ \t]*(const|let|var)[ \t]+([$_a-zA-Z][$_a-zA-Z0-9]*)[ \t]*=[ \t]*"
+	 "(async[ \t]+)?function[ \t]*\\*?[ \t]*([$_a-zA-Z][$_a-zA-Z0-9]*)?[ \t]*\\(.*$"
+	 /* arrow functions */
+	 "|^[ \t]*(const|let|var)[ \t]+([$_a-zA-Z][$_a-zA-Z0-9]*)[ \t]*=[ \t]*"
+	 "(\\([^\\)]*\\)|[$_a-zA-Z][$_a-zA-Z0-9]*)[ \t]*=>[ \t]*\\{?.*$"
+	 /* matches functions declared inside classes and objects */
+	 "|^[ \t]*(static[ \t]+)?(async[ \t]+)?(get[ \t]+|set[ \t]+)?\\*?[ \t]*"
+	 "([$_a-zA-Z][$_a-zA-Z0-9]*)[ \t]*\\([^)]*\\)[ \t]*\\{.*$"
+	 /* matches functions created or assigned in 'exports.' or 'module.exports.' context*/
+	 "|^[ \t]*(module.)?exports.([$_a-zA-Z][$_a-zA-Z0-9]*)[ \t]*="
+	 "([ \t]*(async[ \t]+)?(function)?[ \t]*\\*?[ \t]*"
+	 "([$_a-zA-Z][$_a-zA-Z0-9]*)?[ \t]*\\(.*$"
+	 "|[ \t]*([$_a-zA-Z][$_a-zA-Z0-9]*);"
+	 "|[ \t]*(async[ \t]+)?([$_a-zA-Z][$_a-zA-Z0-9]*)[ \t]*=>)",
+	 /* identifiers */
+	 "[$_A-Za-z][$_A-Za-z0-9]*"
+	 /* hexadecimal and big hexadecimal */
+	 "|0[xX](?:[0-9a-fA-F](?:_?[0-9a-fA-F])*)n?"
+	 /* octa and big octa */
+	 "|0[oO](?:[0-7](?:_?[0-7])*)n?"
+	 /* binary and big binary */
+	 "|0[bB](?:[01](?:_?[01])*)n?"
+	 /* decimal, floting point and exponent notation (eE) */
+	 "|(?:0|[1-9](?:_?[0-9])*)(?:\\.(?:[0-9](?:_?[0-9])*))?(?:[eE][+-]?(?:[0-9](?:_?[0-9])*))?"
+	 /* big decimal */
+	 "|(?:0|[1-9](?:_?[0-9])*)n"
 	 /* punctuation */
-	 "\\{|\\}|\\(|\\)|\\.|\\.{3}|;|,|<|>|<=|>=|==|!=|={3}|!==|\\+|-|\\*|/|%|\\*{2}|"
-	 "\\+{2}|--|<<|>>|>>>|&|\\||\\^|!|~|&&|\\|{2}|\\?{1,2}|:|=|\\+=|-=|\\*=|%=|\\*{2}=|"
-	 "<<=|>>=|>>>=|&=|\\|=|\\^=|&&=|\\|{2}=|\\?{2}=|=>"),
+	 "|\\.{3}|<=|>=|==|!=|===|!==|\\*{2}|\\+{2}|--|<<|>>|>>>|&&|\\|{2}|\\?{2}|\\+=|-="
+	 "|\\*=|%=|\\*{2}=|<<=|>>=|>>>=|&=|\\|=|\\^=|&&=|\\|{2}=|\\?{2}=|=>"),
 PATTERNS("kotlin",
 	 "^[ \t]*(([a-z]+[ \t]+)*(fun|class|interface)[ \t]+.*)$",
 	 /* -- */
-- 
2.50.0.rc0.62.g658f0ae201.dirty
Previous: Derick W. de M. FriasNext: Junio C Hamano
Message 8 of 11 in “userdiff: add javascript diff driver”
  1. 0/1 userdiff: add javascript diff driverDerick W. de M. Frias, Jun 4, 2025
  2. 1/1 userdiff: add javascript diff driverDerick W. de M. Frias, Jun 4, 2025
  3. D. Ben KnobleJun 4, 2025
  4. Johannes SixtJun 5, 2025
  5. 0/4 diff: create pattern for javascript languageDerick W. de M. Frias, Jun 23, 2025
  6. 2/4 t4034: add tests for javascript word literalsDerick W. de M. Frias, Jun 23, 2025
  7. 3/4 t4018: add tests for recognizing javascript function syntaxDerick W. de M. Frias, Jun 23, 2025
  8. 1/4 userdiff: add javascript diff driverDerick W. de M. Frias, Jun 23, 2025
  9. Junio C HamanoJun 23, 2025
  10. 4/4 t4018: add tests for javascript export type function declarationsDerick W. de M. Frias, Jun 23, 2025
  11. Johannes SixtJun 26, 2025

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.