userdiff: adding typescript pattern
cover userdiff: adding typescript pattern
Description
===========
* Add builtin userdiff pattern for TypeScript files. Recognizes function declarations, class definitions, arrow functions, and method definitions.
* Handles common modifiers like export, async, static, etc
* Added tests for the typescript pattern in userdiff.
Dhruv Arora (2):
userdiff: adding typescript pattern
fix(userdiff): sorted pattern and tests
t/t4018/typescript-class-method | 7 +++++++ t/t4018/typescript-export-default-class | 7 +++++++ t/t4018/typescript-export-function | 7 +++++++ userdiff.c | 15 +++++++++++++++ 4 files changed, 36 insertions(+) create mode 100644 t/t4018/typescript-class-method create mode 100644 t/t4018/typescript-export-default-class create mode 100644 t/t4018/typescript-export-function
base-commit: ce74208c2fa13943fffa58f168ac27a76d0eb789
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2251%2FDhruv-0-Arora%2Fuserdiff%2Ftypescript-pattern-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2251/Dhruv-0-Arora/userdiff/typescript-pattern-v1
Pull-Request: https://github.com/git/git/pull/2251
Signature · 1 line
gitgitgadget
PATCH 1/2 userdiff: adding typescript pattern
From: Dhruv Arora <[email protected]>
Add builtin userdiff pattern for TypeScript files. Recognizes function declarations, class definitions, arrow functions, and method definitions.
Handles common modifiers like export, async, static, etc..
--- userdiff.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
userdiff.c +13 -0
@@ -337,6 +337,19 @@ PATTERNS("ruby","(@|@@|\\$)?[a-zA-Z_][a-zA-Z0-9_]*" "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+|\\?(\\\\C-)?(\\\\M-)?." "|//=?|[-+*/<>%&^|=!]=|<<=?|>>=?|===|\\.{1,3}|::|[!=]~"), +PATTERNS ("typescript" , +/* Starting with optional whitespace */ +"^[ \t]*" +/* Followed by an optional export and/or async keyword */ +"((export[ \t]+)?(async[ \t]+)?" +/* Followed by either a function or class declaration */ +"((function|class)[ \t]+[a-zA-Z_][a-zA-Z0-9_]*[^{]*)" +/* or */ +"|" +/* a variable declaration with const, let, or var */ +"([ \t]*(const|let|var)[ \t]+[a-zA-Z_][a-zA-Z0-9_]*[ \t]*=))" , +/* -- */ +"[a-zA-Z_][a-zA-Z0-9_]*" ), PATTERNS("rust", "^[\t ]*((pub(\\([^\\)]+\\))?[\t ]+)?((async|const|unsafe|extern([\t ]+\"[^\"]+\"))[\t ]+)?(struct|enum|union|mod|trait|fn|impl|macro_rules!)[< \t]+[^;]*)$",/* -- */
Signature · 1 line
gitgitgadget
PATCH 2/2 fix(userdiff): sorted pattern and tests
In reply to Dhruv Arora via GitGitGadgetFrom: Dhruv Arora <[email protected]>
- Typescript pattern was not in alphabetical order, causing failing tests.
- Added 3 new typescript tests.
- Fixed pattern bug - incorrectly identifying const|let|var instead of function or class.
--- t/t4018/typescript-class-method | 7 +++++++ t/t4018/typescript-export-default-class | 7 +++++++ t/t4018/typescript-export-function | 7 +++++++ userdiff.c | 28 +++++++++++++------------ 4 files changed, 36 insertions(+), 13 deletions(-) create mode 100644 t/t4018/typescript-class-method create mode 100644 t/t4018/typescript-export-default-class create mode 100644 t/t4018/typescript-export-function
t/t4018/typescript-class-method new file +7 -0
@@ -0,0 +1,7 @@ +export class RIGHT { + unique = 0 + constructor () { + this.doNothing() + } + function ChangeMe() { } +}
t/t4018/typescript-export-default-class new file +7 -0
@@ -0,0 +1,7 @@ +export default class RIGHT { + private x = 0; + private y = 0; + private z = 0; + // ChangeMe + render() {} +}
t/t4018/typescript-export-function new file +7 -0
@@ -0,0 +1,7 @@ +export async function RIGHT(url: string): Promise<string> { + const a = 1; + const b = 2; + const c = 3; + ChangeMe + return url; +}
userdiff.c +15 -13
@@ -337,19 +337,6 @@ PATTERNS("ruby","(@|@@|\\$)?[a-zA-Z_][a-zA-Z0-9_]*" "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+|\\?(\\\\C-)?(\\\\M-)?." "|//=?|[-+*/<>%&^|=!]=|<<=?|>>=?|===|\\.{1,3}|::|[!=]~"), -PATTERNS ("typescript" , -/* Starting with optional whitespace */ -"^[ \t]*" -/* Followed by an optional export and/or async keyword */ -"((export[ \t]+)?(async[ \t]+)?" -/* Followed by either a function or class declaration */ -"((function|class)[ \t]+[a-zA-Z_][a-zA-Z0-9_]*[^{]*)" -/* or */ -"|" -/* a variable declaration with const, let, or var */ -"([ \t]*(const|let|var)[ \t]+[a-zA-Z_][a-zA-Z0-9_]*[ \t]*=))" , -/* -- */ -"[a-zA-Z_][a-zA-Z0-9_]*" ), PATTERNS("rust", "^[\t ]*((pub(\\([^\\)]+\\))?[\t ]+)?((async|const|unsafe|extern([\t ]+\"[^\"]+\"))[\t ]+)?(struct|enum|union|mod|trait|fn|impl|macro_rules!)[< \t]+[^;]*)$",/* -- */
@@ -367,6 +354,21 @@ PATTERNS("scheme", "|([^][)(}{[ \t])+"),PATTERNS ("tex" ,"^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$" ,"\\\\[a-zA-Z@]+|\\\\.|([a-zA-Z0-9]|[^\x01-\x7f])+" ), +PATTERNS ("typescript" , +/* Starting with optional whitespace */ +"^[ \t]*" +"(" +/* Followed by an optional export and/or async and/or default keyword */ +"(export[ \t]+)?(default[ \t]+)?(async[ \t]+)?" +/* Followed by either a function or class declaration */ +"((function|class)[ \t]+[a-zA-Z_][a-zA-Z0-9_]*[^{]*)" +/* or */ +"|" +/* a variable declaration with const, let, or var */ +"^(const|let|var)[ \\t]+[a-zA-Z_][a-zA-Z0-9_]*[ \\t]*=" +")" , +/* -- */ +"[a-zA-Z_][a-zA-Z0-9_]*" ), { .name = "default", .binary = -1 }, };#undef PATTERNS
Signature · 1 line
gitgitgadget
Quoted 6 lines · Dhruv Arora via GitGitGadget
Description
===========* Add builtin userdiff pattern for TypeScript files. Recognizes function declarations, class definitions, arrow functions, and method definitions.
Quoted 7 lines
* Handles common modifiers like export, async, static, etc
* Added tests for the typescript pattern in userdiff.
Dhruv Arora (2):
userdiff: adding typescript pattern
fix(userdiff): sorted pattern and tests
We frown upon a patch series that makes mistakes in an earlier step, only to fix them in a later step. The "git rebase -i" command helps us pretend to be more perfect developers than we actually are, whipping your patch series into a shape that builds one small step on top of another in a logical succession. Such a patch series is easier to understand than a history that faithfully records all the stumbles the developer made until they reached the final solution.
Just have a single patch that adds the right pattern at the right place and add necessary tests.
When/if your reviewers suggest further changes, the way you should work on them is the same. Pretend as if you discarded everything you did, started from scratch, and reached the ideal result without making any mistakes or taking any detours along the way.
Thanks.
Quoted 14 lines
t/t4018/typescript-class-method | 7 +++++++ t/t4018/typescript-export-default-class | 7 +++++++ t/t4018/typescript-export-function | 7 +++++++ userdiff.c | 15 +++++++++++++++ 4 files changed, 36 insertions(+) create mode 100644 t/t4018/typescript-class-method create mode 100644 t/t4018/typescript-export-default-class create mode 100644 t/t4018/typescript-export-functionbase-commit:
ce74208c2fa13943fffa58f168ac27a76d0eb789
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2251%2FDhruv-0-Arora%2Fuserdiff%2Ftypescript-pattern-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2251/Dhruv-0-Arora/userdiff/typescript-pattern-v1
Pull-Request: https://github.com/git/git/pull/2251
Am 28.03.26 um 22:40 schrieb Dhruv Arora via GitGitGadget:
Quoted 25 lines
Description
===========* Add builtin userdiff pattern for TypeScript files. Recognizes function declarations, class definitions, arrow functions, and method definitions.
* Handles common modifiers like export, async, static, etc
* Added tests for the typescript pattern in userdiff.
Dhruv Arora (2):
userdiff: adding typescript pattern
fix(userdiff): sorted pattern and testst/t4018/typescript-class-method | 7 +++++++ t/t4018/typescript-export-default-class | 7 +++++++ t/t4018/typescript-export-function | 7 +++++++ userdiff.c | 15 +++++++++++++++ 4 files changed, 36 insertions(+) create mode 100644 t/t4018/typescript-class-method create mode 100644 t/t4018/typescript-export-default-class create mode 100644 t/t4018/typescript-export-function
Quoted 4 lines
base-commit:
ce74208c2fa13943fffa58f168ac27a76d0eb789
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2251%2FDhruv-0-Arora%2Fuserdiff%2Ftypescript-pattern-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2251/Dhruv-0-Arora/userdiff/typescript-pattern-v1
Pull-Request: https://github.com/git/git/pull/2251
Javascript and Typescript are related languages, if I am not mistaken. It would be appreciated if the new language driver could be used for both. Then the driver's name should be the superset language and the documentation (missing, BTW) should mention that the driver can be used for both languages.
We have had a number of submissions for Javascript or Typescript drivers in the past, but none of them were followed through to be integrated.
Typescript:
https://lore.kernel.org/git/[email protected]/
https://lore.kernel.org/git/[email protected]/
Javascript:
https://lore.kernel.org/git/[email protected]/
https://lore.kernel.org/git/[email protected]/
https://lore.kernel.org/git/[email protected]/
Please review these submission and the responses that they received. Perhaps you can find inspiration for improvement from them.
-- Hannes
Spelling JavaScript (was: Re: [PATCH 0/2] [GSoC] userdiff: adding typescript pattern)
Quoted 6 lines · Johannes Sixt
Javascript and Typescript are related languages, if I am not mistaken.
-- Hannes
FWIW, I believe both languages are officially spelled with a capital S: “JavaScript”, “TypeScript”. At least that is how Wikipedia and official sites present them.
I knew what you meant of course, and will now take my pedant hat off ;)