Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to sort lists by default #833

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions build/rewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,18 @@ func doNotSort(x Expr) bool {
return hasComment(x, "do not sort")
}

// keepSorted reports whether x is marked with a comment containing
// forceKeepSorted reports whether x is marked with a comment containing
// "keep sorted", case-insensitive.
func keepSorted(x Expr) bool {
func forceKeepSorted(x Expr) bool {
return hasComment(x, "keep sorted")
}

// keepSorted checks whether lists are to be sorted by default, and respects comments as overrides of the default table
// value
func keepSorted(x Expr) bool {
return !doNotSort(x) && (tables.SortListsByDefault || forceKeepSorted(x))
}

// fixLabels rewrites labels into a canonical form.
//
// First, it joins labels written as string addition, turning
Expand Down Expand Up @@ -450,7 +456,7 @@ func sortStringList(x Expr, context string) {
return
}

forceSort := keepSorted(list) || keepSorted(list.List[0])
forceSort := forceKeepSorted(list) || forceKeepSorted(list.List[0])
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this bit of logic requires users to explicitly comment "keep sorted" on lists with comments


// TODO(bazel-team): Decide how to recognize lists that cannot
// be sorted. Avoiding all lists with comments avoids sorting
Expand Down
5 changes: 3 additions & 2 deletions tables/jsonparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Definitions struct {
SortableWhitelist map[string]bool
NamePriority map[string]int
StripLabelLeadingSlashes bool
SortListsByDefault bool
ShortenAbsoluteLabelsToRelative bool
}

Expand All @@ -55,9 +56,9 @@ func ParseAndUpdateJSONDefinitions(file string, merge bool) error {
}

if merge {
MergeTables(definitions.IsLabelArg, definitions.LabelBlacklist, definitions.IsListArg, definitions.IsSortableListArg, definitions.SortableBlacklist, definitions.SortableWhitelist, definitions.NamePriority, definitions.StripLabelLeadingSlashes, definitions.ShortenAbsoluteLabelsToRelative)
MergeTables(definitions.IsLabelArg, definitions.LabelBlacklist, definitions.IsListArg, definitions.IsSortableListArg, definitions.SortableBlacklist, definitions.SortableWhitelist, definitions.NamePriority, definitions.StripLabelLeadingSlashes, definitions.SortListsByDefault, definitions.ShortenAbsoluteLabelsToRelative)
} else {
OverrideTables(definitions.IsLabelArg, definitions.LabelBlacklist, definitions.IsListArg, definitions.IsSortableListArg, definitions.SortableBlacklist, definitions.SortableWhitelist, definitions.NamePriority, definitions.StripLabelLeadingSlashes, definitions.ShortenAbsoluteLabelsToRelative)
OverrideTables(definitions.IsLabelArg, definitions.LabelBlacklist, definitions.IsListArg, definitions.IsSortableListArg, definitions.SortableBlacklist, definitions.SortableWhitelist, definitions.NamePriority, definitions.StripLabelLeadingSlashes, definitions.SortListsByDefault, definitions.ShortenAbsoluteLabelsToRelative)
}
return nil
}
1 change: 1 addition & 0 deletions tables/jsonparser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func TestParseJSONDefinitions(t *testing.T) {
SortableWhitelist: map[string]bool{},
NamePriority: map[string]int{"name": -1},
StripLabelLeadingSlashes: true,
SortListsByDefault: true,
}
if !reflect.DeepEqual(expected, definitions) {
t.Errorf("ParseJSONDefinitions() = %v; want %v", definitions, expected)
Expand Down
8 changes: 6 additions & 2 deletions tables/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ var NamePriority = map[string]int{

var StripLabelLeadingSlashes = false

var SortListsByDefault = false

var ShortenAbsoluteLabelsToRelative = false

// AndroidNativeRules lists all Android rules that are being migrated from Native to Starlark.
Expand Down Expand Up @@ -271,7 +273,7 @@ var ProtoNativeSymbols = []string{
var ProtoLoadPath = "@rules_proto//proto:defs.bzl"

// OverrideTables allows a user of the build package to override the special-case rules. The user-provided tables replace the built-in tables.
func OverrideTables(labelArg, blacklist, listArg, sortableListArg, sortBlacklist, sortWhitelist map[string]bool, namePriority map[string]int, stripLabelLeadingSlashes, shortenAbsoluteLabelsToRelative bool) {
func OverrideTables(labelArg, blacklist, listArg, sortableListArg, sortBlacklist, sortWhitelist map[string]bool, namePriority map[string]int, stripLabelLeadingSlashes, sortListsByDefault, shortenAbsoluteLabelsToRelative bool) {
IsLabelArg = labelArg
LabelBlacklist = blacklist
IsListArg = listArg
Expand All @@ -280,11 +282,12 @@ func OverrideTables(labelArg, blacklist, listArg, sortableListArg, sortBlacklist
SortableWhitelist = sortWhitelist
NamePriority = namePriority
StripLabelLeadingSlashes = stripLabelLeadingSlashes
SortListsByDefault = sortListsByDefault
ShortenAbsoluteLabelsToRelative = shortenAbsoluteLabelsToRelative
}

// MergeTables allows a user of the build package to override the special-case rules. The user-provided tables are merged into the built-in tables.
func MergeTables(labelArg, blacklist, listArg, sortableListArg, sortBlacklist, sortWhitelist map[string]bool, namePriority map[string]int, stripLabelLeadingSlashes, shortenAbsoluteLabelsToRelative bool) {
func MergeTables(labelArg, blacklist, listArg, sortableListArg, sortBlacklist, sortWhitelist map[string]bool, namePriority map[string]int, stripLabelLeadingSlashes, sortListsByDefault, shortenAbsoluteLabelsToRelative bool) {
for k, v := range labelArg {
IsLabelArg[k] = v
}
Expand All @@ -307,5 +310,6 @@ func MergeTables(labelArg, blacklist, listArg, sortableListArg, sortBlacklist, s
NamePriority[k] = v
}
StripLabelLeadingSlashes = stripLabelLeadingSlashes || StripLabelLeadingSlashes
SortListsByDefault = sortListsByDefault || SortListsByDefault
ShortenAbsoluteLabelsToRelative = shortenAbsoluteLabelsToRelative || ShortenAbsoluteLabelsToRelative
}
3 changes: 2 additions & 1 deletion tables/testdata/simple_tables.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@
"NamePriority": {
"name": -1
},
"StripLabelLeadingSlashes": true
"StripLabelLeadingSlashes": true,
"SortListsByDefault": true
}