[clang] [clang-format] Add support for `.cjs` as JavaScript file extension (PR #118188)

Jim B via cfe-commits cfe-commits at lists.llvm.org
Mon Dec 2 01:27:20 PST 2024


https://github.com/d3x0r updated https://github.com/llvm/llvm-project/pull/118188

>From 8ce527dc2cc7ac285f782648838664491abffc30 Mon Sep 17 00:00:00 2001
From: d3x0r <d3x0r at no-reply.github.com>
Date: Sat, 30 Nov 2024 02:32:40 -0800
Subject: [PATCH 1/3] Add *.cjs handling for JavaScript Language.

---
 clang/lib/Format/Format.cpp | 1 +
 1 file changed, 1 insertion(+)

diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index ee52972ce66f4a..dcaac4b0d42cc5 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -3950,6 +3950,7 @@ static FormatStyle::LanguageKind getLanguageByFileName(StringRef FileName) {
     return FormatStyle::LK_Java;
   if (FileName.ends_with_insensitive(".js") ||
       FileName.ends_with_insensitive(".mjs") ||
+      FileName.ends_with_insensitive(".cjs") ||
       FileName.ends_with_insensitive(".ts")) {
     return FormatStyle::LK_JavaScript; // (module) JavaScript or TypeScript.
   }

>From 3f28f82d1dc87006aceb0e0c7bd95c460d70ccd0 Mon Sep 17 00:00:00 2001
From: d3x0r <d3x0r at no-reply.github.com>
Date: Sun, 1 Dec 2024 13:04:01 -0800
Subject: [PATCH 2/3] Also update docs, command line help info,
 git-clang-format

---
 clang/docs/ClangFormat.rst                | 2 +-
 clang/tools/clang-format/ClangFormat.cpp  | 2 +-
 clang/tools/clang-format/git-clang-format | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/clang/docs/ClangFormat.rst b/clang/docs/ClangFormat.rst
index e17d741b0a00eb..10ddca8685e941 100644
--- a/clang/docs/ClangFormat.rst
+++ b/clang/docs/ClangFormat.rst
@@ -49,7 +49,7 @@ to format C/C++/Java/JavaScript/JSON/Objective-C/Protobuf/C# code.
                                      supported:
                                        CSharp: .cs
                                        Java: .java
-                                       JavaScript: .mjs .js .ts
+                                       JavaScript: .mjs .js .ts .cjs
                                        Json: .json
                                        Objective-C: .m .mm
                                        Proto: .proto .protodevel
diff --git a/clang/tools/clang-format/ClangFormat.cpp b/clang/tools/clang-format/ClangFormat.cpp
index 5481bb6b87503c..a3e80964c68028 100644
--- a/clang/tools/clang-format/ClangFormat.cpp
+++ b/clang/tools/clang-format/ClangFormat.cpp
@@ -87,7 +87,7 @@ static cl::opt<std::string> AssumeFileName(
              "supported:\n"
              "  CSharp: .cs\n"
              "  Java: .java\n"
-             "  JavaScript: .mjs .js .ts\n"
+             "  JavaScript: .cjs .mjs .js .ts\n"
              "  Json: .json\n"
              "  Objective-C: .m .mm\n"
              "  Proto: .proto .protodevel\n"
diff --git a/clang/tools/clang-format/git-clang-format b/clang/tools/clang-format/git-clang-format
index 6a2a2a22ec5c2b..f965adae42f435 100755
--- a/clang/tools/clang-format/git-clang-format
+++ b/clang/tools/clang-format/git-clang-format
@@ -94,7 +94,7 @@ def main():
       # Other languages that clang-format supports
       'proto', 'protodevel',  # Protocol Buffers
       'java',  # Java
-      'mjs', 'js',  # JavaScript
+      'cjs', 'mjs', 'js',  # JavaScript
       'ts',  # TypeScript
       'cs',  # C Sharp
       'json',  # Json

>From 30634a1f6bba7de1c429d3d964c0f48b7fdb3dc3 Mon Sep 17 00:00:00 2001
From: d3x0r <d3x0r at no-reply.github.com>
Date: Mon, 2 Dec 2024 01:26:59 -0800
Subject: [PATCH 3/3] Reorder lists for Javascript to .js .mjs .cjs (.ts)

---
 clang/docs/ClangFormat.rst                | 2 +-
 clang/tools/clang-format/ClangFormat.cpp  | 2 +-
 clang/tools/clang-format/git-clang-format | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/clang/docs/ClangFormat.rst b/clang/docs/ClangFormat.rst
index 10ddca8685e941..c8f1d7f5a77581 100644
--- a/clang/docs/ClangFormat.rst
+++ b/clang/docs/ClangFormat.rst
@@ -49,7 +49,7 @@ to format C/C++/Java/JavaScript/JSON/Objective-C/Protobuf/C# code.
                                      supported:
                                        CSharp: .cs
                                        Java: .java
-                                       JavaScript: .mjs .js .ts .cjs
+                                       JavaScript: .js .mjs .cjs .ts
                                        Json: .json
                                        Objective-C: .m .mm
                                        Proto: .proto .protodevel
diff --git a/clang/tools/clang-format/ClangFormat.cpp b/clang/tools/clang-format/ClangFormat.cpp
index a3e80964c68028..28610052b9b74a 100644
--- a/clang/tools/clang-format/ClangFormat.cpp
+++ b/clang/tools/clang-format/ClangFormat.cpp
@@ -87,7 +87,7 @@ static cl::opt<std::string> AssumeFileName(
              "supported:\n"
              "  CSharp: .cs\n"
              "  Java: .java\n"
-             "  JavaScript: .cjs .mjs .js .ts\n"
+             "  JavaScript: .js .mjs .cjs .ts\n"
              "  Json: .json\n"
              "  Objective-C: .m .mm\n"
              "  Proto: .proto .protodevel\n"
diff --git a/clang/tools/clang-format/git-clang-format b/clang/tools/clang-format/git-clang-format
index f965adae42f435..06e2f8a8d9671d 100755
--- a/clang/tools/clang-format/git-clang-format
+++ b/clang/tools/clang-format/git-clang-format
@@ -94,7 +94,7 @@ def main():
       # Other languages that clang-format supports
       'proto', 'protodevel',  # Protocol Buffers
       'java',  # Java
-      'cjs', 'mjs', 'js',  # JavaScript
+      'js', 'mjs', 'cjs',   # JavaScript
       'ts',  # TypeScript
       'cs',  # C Sharp
       'json',  # Json



More information about the cfe-commits mailing list