Subject: [bug#49946] [WIP PATCH v3 19/26] gnu: Add tree-sitter-typescript.

From: Pierre Langlois <[email-masked]>
Date: Thu, 10 Feb 2022 22:55:20 +0000

* gnu/packages/tree-sitter.scm (tree-sitter-typescript): New variable.
---
 gnu/packages/tree-sitter.scm | 87 ++++++++++++++++++++++++++++++++++++
 1 file changed, 87 insertions(+)

diff --git a/gnu/packages/tree-sitter.scm b/gnu/packages/tree-sitter.scm
index 63e558f50f..2dcf4dae87 100644
--- a/gnu/packages/tree-sitter.scm
+++ b/gnu/packages/tree-sitter.scm
@@ -488,3 +488,90 @@ (define-public tree-sitter-rust
     (description
       "This package provides a Rust grammar for the Tree-sitter library.")
     (license license:expat)))
+
+(define-public tree-sitter-typescript
+  (let ((commit "111b07762e86efab9a918b7c721f720c37e76b0a")
+        (revision "1"))
+    (package (inherit tree-sitter-c)
+      (name "tree-sitter-typescript")
+      (version (git-version "0.20.0" revision commit))
+      (source (origin
+                (method git-fetch)
+                (uri (git-reference
+                      (url "https://github.com/tree-sitter/tree-sitter-typescript")
+                      (commit commit)))
+                (file-name (git-file-name name version))
+                (sha256
+                 (base32 "1364bm3wbqqzvp03cvpx1w89bbqynb1v62i46gy7f6rhib9bf63a"))
+                (modules '((guix build utils)))
+                (snippet
+                 '(begin
+                    (for-each
+                     (lambda (lang)
+                       (with-directory-excursion lang
+                         (delete-file "src/grammar.json")
+                         (delete-file "src/node-types.json")
+                         (delete-file "src/parser.c")
+                         (delete-file-recursively "src/tree_sitter")))
+                     '("typescript" "tsx"))
+                    #t))))
+      (inputs
+       (list node-nan
+             `(,tree-sitter-javascript "js")))
+      (arguments
+       (substitute-keyword-arguments (package-arguments tree-sitter-c)
+        ((#:phases phases)
+         #~(modify-phases #$phases
+             ;; The parse-example script tries to clone git repositories.
+             (add-after 'unpack 'remove-parse-example
+               (lambda _
+                 (substitute* "package.json"
+                   ((" && script\\/parse-examples") ""))))
+             (add-after 'build 'build-gyp
+               (lambda* (#:key inputs #:allow-other-keys)
+                 (invoke
+                  (search-input-file
+                   inputs
+                   "/lib/node_modules/npm/bin/node-gyp-bin/node-gyp")
+                  "build")))
+             (replace 'tree-sitter-generate
+               (lambda _
+                 (for-each (lambda (dir)
+                             (with-directory-excursion dir
+                               (invoke "tree-sitter" "generate")))
+                           '("typescript" "tsx"))))
+             (replace 'build-and-install-lib
+               (lambda* (#:key outputs #:allow-other-keys)
+                 (use-modules (guix build json))
+                 (let ((name (assoc-ref
+                              (call-with-input-file "package.json" read-json)
+                              "name"))
+                       (lib (string-append #$output "/lib/tree-sitter/")))
+                   (mkdir-p lib)
+                   (define (source-file? basename)
+                     "document me"
+                     (let ((path (string-append src "/" basename)))
+                       (if (file-exists? path)
+                           path
+                           #f)))
+                   (for-each
+                    (lambda (lang)
+                      (let ((src (string-append #$output:js
+                                                "/lib/node_modules/"
+                                                name "/" lang "/src")))
+                        (invoke #$(cc-for-target)
+                                "-shared"
+                                "-fPIC"
+                                "-fno-exceptions"
+                                "-O2"
+                                "-g"
+                                "-o" (string-append lib "/" lang ".so")
+                                (string-append src "/scanner.c")
+                                (string-append src "/parser.c"))))
+                    '("typescript" "tsx")))))))))
+      (home-page "https://github.com/tree-sitter/tree-sitter-typescript")
+      (synopsis "Tree-sitter Typescript grammar")
+      (description
+        "This package provides Typescript and TSX grammars for the Tree-sitter
+library.")
+      (license license:expat))))
--
2.34.0