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

New Code Action: Extract expression into a variable #1531

Open
8 tasks
radeksimko opened this issue Dec 20, 2023 · 0 comments
Open
8 tasks

New Code Action: Extract expression into a variable #1531

radeksimko opened this issue Dec 20, 2023 · 0 comments
Labels

Comments

@radeksimko
Copy link
Member

radeksimko commented Dec 20, 2023

Background

When refactoring existing configuration, one of the actions users may want to take is to extract an expression into a variable, to make the configuration more flexible/reusable and better communicate intentions. This is especially relevant if the configuration represents a published module or the refactoring has the intentions of turning it into one.

For example, they may start with:

resource "azurerm_app_service_environment" "name" {
  name                = "foo"
  resource_group_name = "toot"
  subnet_id           = "noot"
}

and want to end up with:

variable "resource_group_name" {
  type    = string
  default = "toot"
}

resource "azurerm_app_service_environment" "name" {
  name                = "foo"
  resource_group_name = var.resource_group_name
  subnet_id           = "noot"
}

Proposal

  • Implement refactor.extract code action for any interpolatable expression inside of resource
  • Implement refactor.extract code action for any interpolatable expression inside of data
  • Implement refactor.extract code action for any interpolatable expression (inputs) inside of module
  • Implement refactor.extract code action for any interpolatable expression (value) inside of output
  • Implement refactor.extract code action for any interpolatable expression inside of provider
  • Implement refactor.extract code action for any expression inside of locals
  • Implement refactor.extract code action for id inside of import
  • Implement refactor.extract code action for any interpolatable expression inside of check

Implementation Notes

There will be edge cases related to pre-existing interpolation involving function calls or references (which are not allowed inside of variable default).

For example, the user starts with:

resource "azurerm_app_service_environment" "name" {
  name                = "foo"
  resource_group_name = lower("Toot-${var.foot}")
  subnet_id           = "noot"
}

We can consider different solutions for such edge cases:

  1. Extract it as variable with corresponding constraint but lose the value in the process
  2. Avoid offering that code action in this context entirely

Related: Extraction into a local value may be a better choice here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant