[PATCH v2 1/4] userdiff: add javascript diff driver
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