Skip to content

Commit

Permalink
Line Comment Fixes
Browse files Browse the repository at this point in the history
Fix line comments at the beginning of classes with annotations.

Strip trailing whitespace from line comments.
  • Loading branch information
cwarden committed Jan 7, 2025
1 parent 9b55e31 commit a1abbc5
Show file tree
Hide file tree
Showing 4 changed files with 230 additions and 204 deletions.
12 changes: 9 additions & 3 deletions formatter/comments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

func TestComments(t *testing.T) {
if testing.Verbose() {
log.SetLevel(log.DebugLevel)
log.SetLevel(log.TraceLevel)
log.SetFormatter(&log.TextFormatter{
DisableQuote: true,
})
Expand Down Expand Up @@ -78,6 +78,12 @@ System.debug('I am on a separate line!');`,
// Country default value
contact.MailingCountry == 'United States');`,
},
{
`// Test trailing whitespace
go();`,
`// Test trailing whitespace
go();`,
},
}
for i, tt := range tests {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
Expand Down Expand Up @@ -105,7 +111,7 @@ System.debug('I am on a separate line!');`,

func TestTrailingComments(t *testing.T) {
if testing.Verbose() {
log.SetLevel(log.DebugLevel)
log.SetLevel(log.TraceLevel)
log.SetFormatter(&log.TextFormatter{
DisableQuote: true,
})
Expand All @@ -117,7 +123,7 @@ func TestTrailingComments(t *testing.T) {
}{
{
`private class T1Exception extends Exception {} //test`,
`private class T1Exception extends Exception {} //test`,
"private class T1Exception extends Exception {} //test\n",
},
{
`public class MyClass { public static void noop() {}
Expand Down
206 changes: 10 additions & 196 deletions formatter/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ func TestMemberDeclaration(t *testing.T) {

func TestCompilationUnit(t *testing.T) {
if testing.Verbose() {
log.SetLevel(log.DebugLevel)
log.SetLevel(log.TraceLevel)
log.SetFormatter(&log.TextFormatter{
DisableQuote: true,
})
Expand Down Expand Up @@ -585,6 +585,15 @@ public class Top {
};
}`,
},
{
`// Tests for Account triggers
@IsTest
public class A {
}`,
`// Tests for Account triggers
@IsTest
public class A {}`,
},
}
for i, tt := range tests {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
Expand All @@ -609,198 +618,3 @@ public class Top {
})
}
}

func TestSOQL(t *testing.T) {
if testing.Verbose() {
log.SetLevel(log.DebugLevel)

}
tests :=
[]struct {
input string
output string
}{
{
`[SELECT Account.Name, count(Id) FROM Contact WHERE AccountId IN : accounts.keySet() GROUP BY Account.Name]`,
`[
SELECT
Account.Name,
COUNT(Id)
FROM
Contact
WHERE
AccountId IN :accounts.keySet()
GROUP BY Account.Name
]`},
{
`[ SELECT
Id
FROM
Location__c
WHERE Id IN (
SELECT
Location__c
FROM
Clinic__c
WHERE
Clinic_Type__c IN ('Clinic', 'Clinic - Remote NP') AND
Status__c = 'Confirmed' AND
Location__c != null AND
Start__c = YESTERDAY
)
]`,
`[
SELECT
Id
FROM
Location__c
WHERE
Id IN (
SELECT
Location__c
FROM
Clinic__c
WHERE
Clinic_Type__c IN ('Clinic', 'Clinic - Remote NP') AND
Status__c = 'Confirmed' AND
Location__c != null AND
Start__c = YESTERDAY
)
]`},

{
`[
SELECT
Name
FROM
My_Object__c
WHERE
Type__c = 'Virtual' AND
(
Start__c = TODAY OR
Start__c = N_DAYS_AGO:7 OR
Start__c = N_DAYS_AGO:14 OR
Start__c = N_DAYS_AGO:21 OR
Start__c <= N_DAYS_AGO:28
) AND
Status__c = 'Confirmed'
ORDER BY
Start__c
];`, `[
SELECT
Name
FROM
My_Object__c
WHERE
Type__c = 'Virtual' AND
(
Start__c = TODAY OR
Start__c = N_DAYS_AGO:7 OR
Start__c = N_DAYS_AGO:14 OR
Start__c = N_DAYS_AGO:21 OR
Start__c <= N_DAYS_AGO:28
) AND
Status__c = 'Confirmed'
ORDER BY
Start__c
]`},
{
`[SELECT Id FROM ClinicalEncounter WHERE Id = :encounters[0].Id ALL ROWS]`,
`[SELECT Id FROM ClinicalEncounter WHERE Id = :encounters[0].Id ALL ROWS]`,
},
{
`[SELECT Id, SBQQ__Quote__c FROM SBQQ__QuoteLineGroup__c WHERE SBQQ__Quote__c IN :quoteIds ORDER BY SBQQ__Quote__c, SBQQ__Number__c]`,
`[
SELECT
Id,
SBQQ__Quote__c
FROM
SBQQ__QuoteLineGroup__c
WHERE
SBQQ__Quote__c IN :quoteIds
ORDER BY
SBQQ__Quote__c,
SBQQ__Number__c
]`},
{
`[SELECT OBJ1__c O1, OBJ2__c O2, OBJ3__c O3, SUM(OBJ4__c) O4, GROUPING(OBJ1__c) O1Group, GROUPING(OBJ2__c) O2Group, GROUPING(OBJ3__c) O3Group FROM OBJ4__c GROUP BY ROLLUP(OBJ1__c, OBJ2__c, OBJ3__c)]`,
`[
SELECT
OBJ1__c O1,
OBJ2__c O2,
OBJ3__c O3,
SUM(OBJ4__c) O4,
GROUPING(OBJ1__c) O1Group,
GROUPING(OBJ2__c) O2Group,
GROUPING(OBJ3__c) O3Group
FROM
OBJ4__c
GROUP BY ROLLUP(OBJ1__c, OBJ2__c, OBJ3__c)
]`},
{
`[SELECT Name, (SELECT Id, (SELECT Id, (SELECT Id, (SELECT Id FROM Child4 ) FROM Child3 ) FROM Child2 ) FROM Child1) FROM Parent]`,
`[
SELECT
Name,
(SELECT
Id,
(SELECT
Id,
(SELECT
Id,
(SELECT
Id
FROM
Child4)
FROM
Child3)
FROM
Child2)
FROM
Child1)
FROM
Parent
]`},
{
`[ SELECT convertCurrency(Amount) FROM Opportunity ]`,
`[SELECT CONVERTCURRENCY(Amount) FROM Opportunity]`,
},
{
`[SELECT Amount, FORMAT(amount) Amt, convertCurrency(amount) convertedAmount,
FORMAT(convertCurrency(amount)) convertedCurrency FROM Opportunity where Id = '006R00000024gDtIAI']`,
`[
SELECT
Amount,
FORMAT(amount) Amt,
CONVERTCURRENCY(amount) convertedAmount,
FORMAT(CONVERTCURRENCY(amount)) convertedCurrency
FROM
Opportunity
WHERE
Id = '006R00000024gDtIAI'
]`,
},
{
`[SELECT FORMAT(MIN(closedate)) Amt FROM opportunity]`,
`[SELECT FORMAT(MIN(closedate)) Amt FROM opportunity]`,
},
}
for _, tt := range tests {
input := antlr.NewInputStream(tt.input)
lexer := parser.NewApexLexer(input)
stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)

p := parser.NewApexParser(stream)
p.RemoveErrorListeners()
p.AddErrorListener(&testErrorListener{t: t})

v := NewFormatVisitor(stream)
out, ok := v.visitRule(p.SoqlLiteral()).(string)
if !ok {
t.Errorf("Unexpected result parsing apex")
}
if out != tt.output {
t.Errorf("unexpected format. expected:\n%s\ngot:\n%s\n", tt.output, out)
}
}
}
Loading

0 comments on commit a1abbc5

Please sign in to comment.