xixiaofinland / afmt

Salesforce Apex code formatter written in Rust
20 stars 3 forks source link

Nested Cast statement is corrupted #8

Closed aheber closed 3 days ago

aheber commented 6 days ago

A cast statement wrapped inside of parenthesis are being "unwrapped". If that was used to force order of operations so I could interacted with the casted type then that is broken and the Apex class becomes invalid.

Example:

public class Me {
  {
    Account a = new Account(NumberOfEmployees = 7);
    Object objA = a;
    Integer totalRecords = ((Account) objA).NumberOfEmployees;
  }
}

Parse tree:

value: field_access [4, 27] - [4, 61]
  object: parenthesized_expression [4, 27] - [4, 43]
    cast_expression [4, 28] - [4, 42]
      type: type_identifier [4, 29] - [4, 36]
      value: identifier [4, 38] - [4, 42]
  field: identifier [4, 44] - [4, 61]

Formatted:

public class Me {
  {
    Account a = new Account(NumberOfEmployees = 7);
    Object objA = a;
    Integer totalRecords = (Account) objA.NumberOfEmployees;
  }
}

New Parse Tree:

value: cast_expression [4, 27] - [4, 59]
  type: type_identifier [4, 28] - [4, 35]
  value: field_access [4, 37] - [4, 59]
    object: identifier [4, 37] - [4, 41]
    field: identifier [4, 42] - [4, 59]

You can see the scope of the cast_expression is promoted to the entire expression instead of being isolated as expected.

xixiaofinland commented 5 days ago

this should have been fixed and guarded by the new test file.

It can be tested on latest v0.0.13