[clang] [clang-format] fix: recognize `.mts` and `.cts` as TypeScript files (PR #236122)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Oct 27 06:57:54 PDT 2026
https://github.com/lumirlumir created https://github.com/llvm/llvm-project/pull/236122
Hello,
This PR fixes recognition of `.mts` and `.cts` as TypeScript in `clang-format`, `git-clang-format`, and `clang-format-diff.py`, matching [TypeScript 4.7's supported extensions](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-7.html#new-file-extensions).
Also include `.mjs` and `.cjs` in `clang-format-diff.py`'s default filter. Add regression tests and update documentation.
>From ecb9ec8b57cb42ab0efd7a8743565c1cfdb6ccb9 Mon Sep 17 00:00:00 2001
From: lumir <rpfos at naver.com>
Date: Tue, 27 Oct 2026 22:54:48 +0900
Subject: [PATCH] [clang-format] fix: recognize `.mts` and `.cts` as TypeScript
files
---
clang/docs/ClangFormat.md | 2 +-
clang/docs/ReleaseNotes.md | 3 +++
clang/lib/Format/Format.cpp | 4 +++-
clang/test/Format/language-detection.cpp | 6 ++++++
clang/test/Format/lit.local.cfg | 4 ++++
clang/tools/clang-format/ClangFormat.cpp | 2 +-
clang/tools/clang-format/clang-format-diff.py | 2 +-
clang/tools/clang-format/git-clang-format | 4 +++-
clang/unittests/Format/FormatTest.cpp | 11 +++++++++++
9 files changed, 33 insertions(+), 5 deletions(-)
diff --git a/clang/docs/ClangFormat.md b/clang/docs/ClangFormat.md
index 94fd51a3c1a0a2..dfeacfe4a2cf11 100644
--- a/clang/docs/ClangFormat.md
+++ b/clang/docs/ClangFormat.md
@@ -44,7 +44,7 @@ Clang-format options:
supported:
CSharp: .cs
Java: .java
- JavaScript: .js .mjs .cjs .ts
+ JavaScript: .js .mjs .cjs .ts .mts .cts
JSON: .json .ipynb
Objective-C: .m .mm
Proto: .proto .protodevel
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 200c41fb9e4a29..2dab3e52726999 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -1377,6 +1377,9 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
- Extend `IndentAccessModifiers` with `AfterFirstAccessModifier` to indent
members before the first explicit access modifier by one level. Existing
configuration values `true` and `false` remain supported.
+- Recognize `.mts` and `.cts` files as TypeScript, including in
+ `git-clang-format` and `clang-format-diff.py`. The latter also now includes
+ `.mjs` and `.cjs` files by default.
### libclang
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index d83ca4a558b7f8..d10e4acb7250b2 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -4652,7 +4652,9 @@ static FormatStyle::LanguageKind getLanguageByFileName(StringRef &FileName) {
if (FileName.ends_with_insensitive(".js") ||
FileName.ends_with_insensitive(".mjs") ||
FileName.ends_with_insensitive(".cjs") ||
- FileName.ends_with_insensitive(".ts")) {
+ FileName.ends_with_insensitive(".ts") ||
+ FileName.ends_with_insensitive(".mts") ||
+ FileName.ends_with_insensitive(".cts")) {
return FormatStyle::LK_JavaScript; // (module) JavaScript or TypeScript.
}
if (FileName.ends_with(".m") || FileName.ends_with(".mm"))
diff --git a/clang/test/Format/language-detection.cpp b/clang/test/Format/language-detection.cpp
index 72e2146c54a3ac..343c50a66909b2 100644
--- a/clang/test/Format/language-detection.cpp
+++ b/clang/test/Format/language-detection.cpp
@@ -2,6 +2,12 @@
// RUN: | clang-format -style=LLVM -assume-filename=foo.js \
// RUN: | FileCheck -strict-whitespace -check-prefix=CHECK1 %s
// RUN: grep -Ev "// *[A-Z0-9_]+:" %s \
+// RUN: | clang-format -style=LLVM -assume-filename=foo.mts \
+// RUN: | FileCheck -strict-whitespace -check-prefix=CHECK1 %s
+// RUN: grep -Ev "// *[A-Z0-9_]+:" %s \
+// RUN: | clang-format -style=LLVM -assume-filename=foo.cts \
+// RUN: | FileCheck -strict-whitespace -check-prefix=CHECK1 %s
+// RUN: grep -Ev "// *[A-Z0-9_]+:" %s \
// RUN: | clang-format -style=LLVM -assume-filename=foo.cpp \
// RUN: | FileCheck -strict-whitespace -check-prefix=CHECK2 %s
// CHECK1: {{^a >>>= b;$}}
diff --git a/clang/test/Format/lit.local.cfg b/clang/test/Format/lit.local.cfg
index 13e244ffd5d155..36542e31fb88dc 100644
--- a/clang/test/Format/lit.local.cfg
+++ b/clang/test/Format/lit.local.cfg
@@ -12,7 +12,11 @@ config.suffixes = [
".mm",
".java",
".js",
+ ".mjs",
+ ".cjs",
".ts",
+ ".mts",
+ ".cts",
".proto",
".protodevel",
".pb.txt",
diff --git a/clang/tools/clang-format/ClangFormat.cpp b/clang/tools/clang-format/ClangFormat.cpp
index 743c7c27a9f5d2..efdf566699691c 100644
--- a/clang/tools/clang-format/ClangFormat.cpp
+++ b/clang/tools/clang-format/ClangFormat.cpp
@@ -86,7 +86,7 @@ static cl::opt<std::string> AssumeFileName(
"supported:\n"
" CSharp: .cs\n"
" Java: .java\n"
- " JavaScript: .js .mjs .cjs .ts\n"
+ " JavaScript: .js .mjs .cjs .ts .mts .cts\n"
" JSON: .json .ipynb\n"
" Objective-C: .m .mm\n"
" Proto: .proto .protodevel\n"
diff --git a/clang/tools/clang-format/clang-format-diff.py b/clang/tools/clang-format/clang-format-diff.py
index 3059982ba231f1..4afabe7e7df47a 100755
--- a/clang/tools/clang-format/clang-format-diff.py
+++ b/clang/tools/clang-format/clang-format-diff.py
@@ -63,7 +63,7 @@ def main():
"-iregex",
metavar="PATTERN",
default=r".*\.(?:cpp|cc|c\+\+|cxx|cppm|ccm|cxxm|c\+\+m|c|cl|h|hh|hpp"
- r"|hxx|m|mm|inc|js|ts|proto|protodevel|java|cs|json|ipynb|s?vh?)",
+ r"|hxx|m|mm|inc|[mc]?js|[mc]?ts|proto|protodevel|java|cs|json|ipynb|s?vh?)",
help="custom pattern selecting file paths to reformat "
"(case insensitive, overridden by -regex)",
)
diff --git a/clang/tools/clang-format/git-clang-format b/clang/tools/clang-format/git-clang-format
index d9b2d501c00e39..50b35122bfb003 100755
--- a/clang/tools/clang-format/git-clang-format
+++ b/clang/tools/clang-format/git-clang-format
@@ -118,7 +118,9 @@ def main():
"js",
"mjs",
"cjs", # JavaScript
- "ts", # TypeScript
+ "ts",
+ "mts",
+ "cts", # TypeScript
"cs", # C Sharp
"json",
"ipynb", # JSON
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index f38287757ffba1..40d7e4c69c2a0d 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -22991,6 +22991,17 @@ TEST_F(FormatTest, FileAndCode) {
"#endif"));
}
+TEST_F(FormatTest, GuessJavaScriptLanguageByFileName) {
+ for (StringRef FileName :
+ {"foo.js", "foo.mjs", "foo.cjs", "foo.ts", "foo.mts", "foo.cts",
+ "foo.JS", "foo.MJS", "foo.CJS", "foo.TS", "foo.MTS", "foo.CTS",
+ "foo.d.ts", "foo.d.mts", "foo.d.cts", "foo.mts.in",
+ "foo.cts.template"}) {
+ SCOPED_TRACE(FileName);
+ EXPECT_EQ(FormatStyle::LK_JavaScript, guessLanguage(FileName, ""));
+ }
+}
+
TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) {
EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];"));
EXPECT_EQ(FormatStyle::LK_ObjC,
More information about the cfe-commits
mailing list