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

Adding "Compact Constants" Option #1242

Open
wants to merge 5 commits 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
9 changes: 9 additions & 0 deletions build/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,10 @@ func (p *printer) compactStmt(s1, s2 Expr) bool {
// Standalone comment blocks shouldn't be attached to other statements
return false
} else if (p.formattingMode() == TypeBuild) && p.level == 0 {
if tables.CompactConstantDefinitions && isAssignmentExpr(s1) && isAssignmentExpr(s2) {
// Two constant definitions do not need an extra line (if compact option enabled).
return true
}
// Top-level statements in a BUILD or WORKSPACE file
return false
} else if isFunctionDefinition(s1) || isFunctionDefinition(s2) {
Expand Down Expand Up @@ -446,6 +450,11 @@ func isCommentBlock(x Expr) bool {
return ok
}

func isAssignmentExpr(x Expr) bool {
_, ok := x.(*AssignExpr)
return ok
}

// isFunctionDefinition checks if the statement is a def code block
func isFunctionDefinition(x Expr) bool {
_, ok := x.(*DefStmt)
Expand Down
4 changes: 4 additions & 0 deletions build/print_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,13 @@ func setFlags(file string) func() {
if strings.Contains(file, string(os.PathSeparator)+"050.") {
tables.ShortenAbsoluteLabelsToRelative = true
}
if strings.Contains(file, ".compactconst.") {
tables.CompactConstantDefinitions = true
}
return func() {
tables.StripLabelLeadingSlashes = false
tables.ShortenAbsoluteLabelsToRelative = false
tables.CompactConstantDefinitions = false
}
}

Expand Down
6 changes: 6 additions & 0 deletions build/testdata/004.compactconst.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
JAVA_FILES = [
"Foo.java",
"Bar.java",
"Baz.java",
"Quux.java",
]
7 changes: 7 additions & 0 deletions build/testdata/005.compactconst.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
JAVA_FILES = [
# Comment regarding Foo.java
"Foo.java",
"Bar.java",
"Baz.java", # Comment regarding Baz.java
"Quux.java",
]
30 changes: 30 additions & 0 deletions build/testdata/006.compactconst.build.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
set = {
1,
2,
3,
}
multiline_set = {
4,
5,
[6],
}
plus = lambda x, y: x + y
two = (lambda x: x(x))(lambda z: lambda y: z)(1)(2)(3)
make_one = lambda: 1
l = lambda x, y, *args, **kwargs: f(
y,
key = x,
*args,
**kwargs
)

TriggerActionAfterProbe(
action = lambda State, response: None,
predicate = lambda State, response: all([
response["request"]["method"] == "GET",
response["request"]["url"].find("/posts/") != -1,
response["request"]["url"].endswith("/comments"),
response["status_code"] in range(200, 299),
]),
probe = ("http", "response"),
)
21 changes: 21 additions & 0 deletions build/testdata/006.compactconst.bzl.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
set = {1, 2, 3}
multiline_set = {
4,
5,
[6],
}
plus = lambda x, y: x + y
two = (lambda x: x(x))(lambda z: lambda y: z)(1)(2)(3)
make_one = lambda: 1
l = lambda x, y, *args, **kwargs: f(y, key = x, *args, **kwargs)

TriggerActionAfterProbe(
probe = ("http", "response"),
predicate = lambda State, response: all([
response["request"]["method"] == "GET",
response["request"]["url"].find("/posts/") != -1,
response["request"]["url"].endswith("/comments"),
response["status_code"] in range(200, 299),
]),
action = lambda State, response: None,
)
37 changes: 37 additions & 0 deletions build/testdata/017.compactconst.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# c1
greeting = "hello " + \
"world" # c2
# c3

# c4
greeting = "hello " + \
"world" # c5
# c6

# c7
greeting = "hello " + \
"world" # c8
# c9

# c10
greeting = ("hello " + # c11
"world") # c12
# c13

# c14
greeting = ("hello " + # c15
"world") # c16
# c17

# c18
greeting = ("hello" + # c19
# c20
"world") # c21
# c22

greeting = "hello " + \
"world" # c23
greeting = ("hello " + # c24
"world")
greeting = ("hello " + # c25
"world")
12 changes: 12 additions & 0 deletions build/testdata/043.compactconst.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
bar = "bar"

# This gets an empty expr_opt in the parser, causing a nil to appear.
b = bar[:-2]

# Test that slices and partial slices are parsed properly
f = foo[-1:-2:-3]
f = foo[1::]
f = foo[:1:]
f = foo[::1]
f = foo[::]
f = foo[:]
31 changes: 31 additions & 0 deletions build/testdata/066.compactconst.build.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
st1 = "abc"
st2 = """
multiline"""
st3 = (
"multi"
)
n = 123
id = a
fct1 = foo(1)
fct2 = foo(
arg = 1,
)
fct3 = (
foo(
arg = 1,
)
)

macro(
name = "foo",
arg = ["a"],
)

nested = 1
comments = [
"a",
("b"), # end of line comment

# before comment
("c"),
] # comment
31 changes: 31 additions & 0 deletions build/testdata/066.compactconst.bzl.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
st1 = ("abc")
st2 = ("""
multiline""")
st3 = (
"multi"
)
n = (123)
id = (a)
fct1 = (foo(1))
fct2 = (foo(
arg = 1,
))
fct3 = (
foo(
arg = 1,
)
)

(macro(
name = ("foo"),
arg = ([("a")]),
))

nested = (((((1)))))
comments = ([
"a",
("b"), # end of line comment

# before comment
("c"),
]) # comment
53 changes: 53 additions & 0 deletions build/testdata/067.compactconst.build.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
load(":a", "b")
load(
# A
":foo.bzl",
# B
"bar",
)

cc_binary(
# A
name = "bin",
# B
srcs = ["bin.cc"],
# C
)

cc_binary(
name = "wibble",
srcs = ["wibble.cc"],
)

my_list = [
1,
# A
2,
# B
]
my_1tuple = (
# A
1,
# B
)
my_2tuple = (
# A
1,
# B
2,
# C
)
my_dict = {
"a": 1,
# A
"b": 2,
# B
}

func(a)

func(b)

func(c, d)

func(e, f)
46 changes: 46 additions & 0 deletions build/testdata/067.compactconst.bzl.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
load(":a", "b")
load(
# A
":foo.bzl",
# B
"bar",
)

cc_binary(
# A
name = "bin",
# B
srcs = ["bin.cc"],
# C
)
cc_binary(name = "wibble", srcs = ["wibble.cc"])

my_list = [
1,
# A
2,
# B
]
my_1tuple = (
# A
1,
# B
)
my_2tuple = (
# A
1,
# B
2,
# C
)
my_dict = {
"a": 1,
# A
"b": 2,
# B
}

func(a)
func(b)
func(c, d)
func(e, f)
5 changes: 3 additions & 2 deletions tables/jsonparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Definitions struct {
SortableDenylist map[string]bool
SortableAllowlist map[string]bool
NamePriority map[string]int
CompactConstantDefinitions bool
StripLabelLeadingSlashes bool
ShortenAbsoluteLabelsToRelative bool
}
Expand All @@ -55,9 +56,9 @@ func ParseAndUpdateJSONDefinitions(file string, merge bool) error {
}

if merge {
MergeTables(definitions.IsLabelArg, definitions.LabelDenylist, definitions.IsListArg, definitions.IsSortableListArg, definitions.SortableDenylist, definitions.SortableAllowlist, definitions.NamePriority, definitions.StripLabelLeadingSlashes, definitions.ShortenAbsoluteLabelsToRelative)
MergeTables(definitions.IsLabelArg, definitions.LabelDenylist, definitions.IsListArg, definitions.IsSortableListArg, definitions.SortableDenylist, definitions.SortableAllowlist, definitions.NamePriority, definitions.CompactConstantDefinitions, definitions.StripLabelLeadingSlashes, definitions.ShortenAbsoluteLabelsToRelative)
} else {
OverrideTables(definitions.IsLabelArg, definitions.LabelDenylist, definitions.IsListArg, definitions.IsSortableListArg, definitions.SortableDenylist, definitions.SortableAllowlist, definitions.NamePriority, definitions.StripLabelLeadingSlashes, definitions.ShortenAbsoluteLabelsToRelative)
OverrideTables(definitions.IsLabelArg, definitions.LabelDenylist, definitions.IsListArg, definitions.IsSortableListArg, definitions.SortableDenylist, definitions.SortableAllowlist, definitions.NamePriority, definitions.CompactConstantDefinitions, definitions.StripLabelLeadingSlashes, definitions.ShortenAbsoluteLabelsToRelative)
}
return nil
}
15 changes: 8 additions & 7 deletions tables/jsonparser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@ func TestParseJSONDefinitions(t *testing.T) {
}

expected := Definitions{
IsLabelArg: map[string]bool{"srcs": true},
LabelDenylist: map[string]bool{},
IsSortableListArg: map[string]bool{"srcs": true, "visibility": true},
SortableDenylist: map[string]bool{"genrule.srcs": true},
SortableAllowlist: map[string]bool{},
NamePriority: map[string]int{"name": -1},
StripLabelLeadingSlashes: true,
IsLabelArg: map[string]bool{"srcs": true},
LabelDenylist: map[string]bool{},
IsSortableListArg: map[string]bool{"srcs": true, "visibility": true},
SortableDenylist: map[string]bool{"genrule.srcs": true},
SortableAllowlist: map[string]bool{},
NamePriority: map[string]int{"name": -1},
CompactConstantDefinitions: true,
StripLabelLeadingSlashes: true,
}
if !reflect.DeepEqual(expected, definitions) {
t.Errorf("ParseJSONDefinitions(simple_tables.json) = %v; want %v", definitions, expected)
Expand Down
Loading