openrewrite / rewrite

Automated mass refactoring of source code.
https://docs.openrewrite.org
Apache License 2.0
2.29k stars 340 forks source link

Issue discovered with `deploy/basic/terraform/compute.tf` #1506

Closed kmccarp closed 2 years ago

kmccarp commented 2 years ago

Problem

Describe the issue you are experiencing.

HCL formatting appears to shift operators to the left when it shouldn't.

Expected behavior

Describe what you expected to see.

HCL operators not be shifted to the left.

Example diff

 # 

 resource "oci_core_instance" "app_instance" {
-  availability_domain                 = random_shuffle.compute_ad.result[count.index % length(random_shuffle.compute_ad.result)]
+  availability_domain                 = random_shuffle.compute_ad.result[count.index% length(random_shuffle.compute_ad.result)]
   compartment_id                      = var.compartment_ocid
   display_name                        = "mushop-${random_string.deploy_id.result}-${count.index}"
   shape                               = local.instance_shape
   create_vnic_details {
     subnet_id        = oci_core_subnet.mushop_main_subnet.id
     display_name     = "primaryvnic"
-    assign_public_ip = (var.instance_visibility == "Private") ? false : true
+    assign_public_ip = (var.instance_visibility== "Private") ? false : true
     hostname_label   = "mushop-${random_string.deploy_id.result}-${count.index}"
   }

Recipes in example diff:

References:

traceyyoshima commented 2 years ago

Update: This is a parsing issue, the prefix is missing before Normalize touches the AST.

Parsing the HCL in tests shows the space before binary operators is missing:

    @Test
    fun test() = assertParsePrintAndProcess(
        """
           create_vnic_details {
             assign_public_ip = (var.instance_visibility == "Private") ? false : true
           }
        """.trimIndent()
    )

    @Test
    fun test2() = assertParsePrintAndProcess(
        """
            resource "oci_core_instance" "app_instance" {
              availability_domain = random_shuffle.compute_ad.result[count.index % length(random_shuffle.compute_ad.result)]
            }
        """.trimIndent()
    )