git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Derick W. de M. Frias" <derick.william.moraes@gmail.com>
To: j6t@kdbg.org
Cc: derick.william.moraes@gmail.com, git@vger.kernel.org
Subject: [PATCH v2 1/4] userdiff: add javascript diff driver
Date: Mon, 23 Jun 2025 03:35:46 -0300	[thread overview]
Message-ID: <20250623090538.154858-2-derick.william.moraes@gmail.com> (raw)
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


  reply	other threads:[~2025-06-23  9:06 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-04  9:35 [GSoC PATCH 0/1] userdiff: add javascript diff driver Derick W. de M. Frias
2025-06-04  9:35 ` [GSoC PATCH 1/1] " Derick W. de M. Frias
2025-06-04 21:19   ` D. Ben Knoble
2025-06-05  5:51   ` Johannes Sixt
2025-06-23  6:35     ` [PATCH v2 0/4] diff: create pattern for javascript language Derick W. de M. Frias
2025-06-23  6:35       ` Derick W. de M. Frias [this message]
2025-06-23 18:09         ` [PATCH v2 1/4] userdiff: add javascript diff driver Junio C Hamano
2025-06-23  6:35       ` [PATCH v2 2/4] t4034: add tests for javascript word literals Derick W. de M. Frias
2025-06-23  6:35       ` [PATCH v2 3/4] t4018: add tests for recognizing javascript function syntax Derick W. de M. Frias
2025-06-23  6:35       ` [PATCH v2 4/4] t4018: add tests for javascript export type function declarations Derick W. de M. Frias
2025-06-26  6:21         ` Johannes Sixt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250623090538.154858-2-derick.william.moraes@gmail.com \
    --to=derick.william.moraes@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=j6t@kdbg.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).