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

feat: Add svelte/consistent-attribute-lines rule #591

Closed
jacob-8 opened this issue Sep 29, 2023 · 4 comments
Closed

feat: Add svelte/consistent-attribute-lines rule #591

jacob-8 opened this issue Sep 29, 2023 · 4 comments
Labels

Comments

@jacob-8
Copy link

jacob-8 commented Sep 29, 2023

Motivation

This rule is essentially the same as Anthony's recently published antfu/consistent-list-newline rule, but for element attributes in Svelte files. The purpose of it is to save spending time messing with whitespace. However I write the first attribute, the rest will automatically follow the pattern.

Description

Keep attributes on same line or each on their own line based on the first attributes position

Examples

<script>
</script>

<!-- ✓ GOOD -->
<div baz foo bar="2">Text</div>

<div
  baz
  foo
  bar="2">Text</div>

<div
  foo
  use:baz={{
    setting: 'value',
  }} />

<!-- ✗ BAD -->
<div baz
  foo
  bar="2">Text</div>

<div baz foo
  bar="2">Text</div>

<div
  baz foo bar="2">Text</div>

<div foo
  bar />

<div foo use:baz={{
    setting: 'value',
  }} />

<div foo use:baz={{
    setting: 'value',
  }} use:box />

From the rule tests at https://github.com/jacob-8/eslint-plugin-svelte-stylistic/blob/main/src/rules/consistent-attribute-lines.test.ts

Additional comments

I've already published it to npm as eslint-plugin-svelte-stylistic so I'm set, but I thought others might appreciate it so wanted to propose adding it here. What do you think? (rule 2 of 2, see the first at #590)

@ota-meshi
Copy link
Member

@jacob-8
Copy link
Author

jacob-8 commented Oct 3, 2023

I think that can be checked by combining two rules.

Great suggestion, but it's not what I'm looking for. What bothered me most about Prettier is that it only works off of fixed length parameters (lines are at most 80, 100, 120, characters long etc...). Many times something would be just how I want it and then Prettier takes 2 of the 10 lines that happen to be barely too long and then wraps them, breaking the uniformity with the other 8 lines. max-attributes-per-line is essentially the same problem in that it won't respect my intended formatting, but will simply count attributes and change things based on a fixed number. The desired count is different in different situations (for example, class="foo bar baz and-the-other-classes-that-will-make-this-really-long" is a much longer attribute than type="button"). What I want is a rule that lets me start things off how desired (the first attribute) and then the rest just automatically falls in line. Just like Anthony wrote in his consistent-list-newline rule: "It will check the newline style of the first property or item and apply to the rest of the properties or items."

Please feel free to let me know if you think what I'm describing is useful or if it would bloat the offerings you already have and lead to confusion. Like I said, I'm already set for myself, just wanting to share if helpful.

@ota-meshi
Copy link
Member

I don't really understand. What rules are missing? I think the code you wrote at the top of this thread is checked well, as you can see in the demo.
What kind of code specifically do you think is not checked?

@jacob-8
Copy link
Author

jacob-8 commented Oct 3, 2023

Ah, "singleline": 99 is the trick to getting the functionality I want. It is I who did not understand the possibilities of the max-attributes-per-line rule. I'll close this. Thanks for helping me understand better!

@jacob-8 jacob-8 closed this as completed Oct 3, 2023
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

2 participants