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

generator: can't use iterator value in for loop #763

Closed
adonespitogo opened this issue May 28, 2024 · 8 comments
Closed

generator: can't use iterator value in for loop #763

adonespitogo opened this issue May 28, 2024 · 8 comments
Labels

Comments

@adonespitogo
Copy link

I can't use the i variable in this for loop example:

for i, nav := range navs {
  if nav.Active {
    <a id={ navId(i) } href="{ nav.Url }">{ nav.Text }</a>
  }
}

func navId(i int) {
  return fmt.Sprintf("navid-%d", i)
}

Error:

i declared and not used
@a-h
Copy link
Owner

a-h commented May 28, 2024

Oh, that's not right. I was surprised that this isn't covered in the test suites, but all of the tests use the empty value (_). I'll sort that too.

I suspect this issue was introduced when go expressions started being formatted, and the parser errors propagated.

I won't be able to fix it until next weekend though.

In the meantime, the classic 3 part for loop will work (for i := 0; i < len(z); i++), albeit it's less ergonomic.

I think the fix will likely be to ignore unused variable errors during formatting operations, and leave any unused variable issues to be discovered in the generated output, but I'll have to look into it more.

@adonespitogo
Copy link
Author

Thank you, great project by the way. I'll use the classic for loop for now.

@joerdav joerdav added bug Something isn't working generator labels May 29, 2024
@joerdav
Copy link
Collaborator

joerdav commented May 29, 2024

Hi @adonespitogo I'm taking a look at this. What version of templ are you using please?

@joerdav
Copy link
Collaborator

joerdav commented May 29, 2024

Second, what command are you running to see this error? Or is it in the editor?

@joerdav joerdav changed the title Bug: can't use iterator value in for loop generator: can't use iterator value in for loop May 29, 2024
@adonespitogo
Copy link
Author

Hi @joerdav

Hi @adonespitogo I'm taking a look at this. What version of templ are you using please?

v0.2.707

Second, what command are you running to see this error? Or is it in the editor?

cd templates && templ generate

@joerdav
Copy link
Collaborator

joerdav commented May 29, 2024

I'm struggling to reproduce this, your original code is not valid templ so I couldn't just copy it into a file. I changed it into a valid templ file and couldn't get it to error:

package adon

templ t() {
	for i, nav := range navs {
		if nav.Active {
			<a id={ navId(i) } href="{ nav.Url }">{ nav.Text }</a>
		}
	}
}

func navId(i int) {
	return fmt.Sprintf("navid-%d", i)
}

Please can you consider creating a minimal reproducible example, either as a github repo or a single templ file that I can copy?

@adonespitogo
Copy link
Author

Hi @joerdav

I can no longer reproduce this issue. Perhaps it was a mistake on my part, a lot has changed in my code since then. Sorry for bringing this up. Feel free to close this issue.

@joerdav
Copy link
Collaborator

joerdav commented May 29, 2024

No problem, thanks for coming back to update!

@joerdav joerdav closed this as not planned Won't fix, can't repro, duplicate, stale May 29, 2024
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

3 participants