[format] Sort lists based on suffix match

Sort all lists with the "sources" or "deps" suffix (rather than requiring
an exact match) according to the existing "sources" and "deps" sorting
rules.

Change-Id: I152d8d18448f056c9e78ced5653ce13ebb8e6f3b
Reviewed-on: https://gn-review.googlesource.com/c/gn/+/6720
Reviewed-by: Brett Wilson <brettw@chromium.org>
Commit-Queue: Brett Wilson <brettw@chromium.org>
diff --git a/src/gn/command_format.cc b/src/gn/command_format.cc
index 99e7a62..03d9311 100644
--- a/src/gn/command_format.cc
+++ b/src/gn/command_format.cc
@@ -349,9 +349,10 @@
        binop->op().value() == "-=") &&
       ident && list) {
     const std::string_view lhs = ident->value().value();
-    if (lhs == "public" || lhs == "sources")
+    if (base::EndsWith(lhs, "sources", base::CompareCase::SENSITIVE) ||
+        lhs == "public")
       const_cast<ListNode*>(list)->SortAsStringsList();
-    else if (lhs == "deps" || lhs == "public_deps")
+    else if (base::EndsWith(lhs, "deps", base::CompareCase::SENSITIVE))
       const_cast<ListNode*>(list)->SortAsDepsList();
   }
 }
diff --git a/src/gn/format_test_data/062.gn b/src/gn/format_test_data/062.gn
index 8c4cc7d..c400591 100644
--- a/src/gn/format_test_data/062.gn
+++ b/src/gn/format_test_data/062.gn
@@ -120,3 +120,9 @@
   "these", "two"
   # alone!
 ]
+
+# Lists with "sources" suffix should also be sorted.
+foo_sources = [
+  "z",
+  "a",
+]
diff --git a/src/gn/format_test_data/062.golden b/src/gn/format_test_data/062.golden
index b554510..dc32658 100644
--- a/src/gn/format_test_data/062.golden
+++ b/src/gn/format_test_data/062.golden
@@ -130,3 +130,9 @@
 
   # alone!
 ]
+
+# Lists with "sources" suffix should also be sorted.
+foo_sources = [
+  "a",
+  "z",
+]
diff --git a/src/gn/format_test_data/063.gn b/src/gn/format_test_data/063.gn
index 9fd8211..418d90f 100644
--- a/src/gn/format_test_data/063.gn
+++ b/src/gn/format_test_data/063.gn
@@ -33,4 +33,10 @@
       a,
     ]
   }
+
+  # Sort lists with "deps" suffix as "deps".
+  foo_deps = [
+    "//a",
+    ":z",
+  ]
 }
diff --git a/src/gn/format_test_data/063.golden b/src/gn/format_test_data/063.golden
index 3dc4bed..3ae77b2 100644
--- a/src/gn/format_test_data/063.golden
+++ b/src/gn/format_test_data/063.golden
@@ -33,4 +33,10 @@
       a,
     ]
   }
+
+  # Sort lists with "deps" suffix as "deps".
+  foo_deps = [
+    ":z",
+    "//a",
+  ]
 }