Skip to content

Add rule max-attributes-per-line (resolves #47) #60

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

Merged
merged 16 commits into from
Aug 15, 2017
71 changes: 53 additions & 18 deletions docs/rules/max-attributes-per-line.md
Original file line number Diff line number Diff line change
@@ -1,33 +1,42 @@
# Define the number of attributes allows per line (max-attributes-per-line)

Limits the maximum number of props per line to improve readability
Limits the maximum number of attributes/properties per line to improve readability.


## Rule Details
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

## :book: Rule Details


This rule aims to enforce a number of props per line in templates. It checks all the elements in a template and verifies that the number of props per line does not exceed the defined maximum.
A prop is considered to be in a new line when there is a line break between two props.
This rule aims to enforce a number of attributes per line in templates. It checks all the elements in a template and verifies that the number of attributes per line does not exceed the defined maximum.
An attribute is considered to be in a new line when there is a line break between two attributes.

The default is one prop per line
There is predefined number of attributes that are acceptable in one-line case, as well as how many attributes are acceptable per line in multi-line case.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's update a little bit more:

There is a configurable number of attributes that are acceptable in one-line case (default 3), as well as how many attributes are acceptable per line in multi-line case (default 1).

And then we can remove the next sentence.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed ✅


The default is one attribute per line for multiline and three attributes per line in singleline.

Examples of **incorrect** code for this rule:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:-1: Examples of **incorrect** code for this rule:


```js
Copy link
Contributor

@armano2 armano2 Jul 30, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

```html

<component lorem="1" ipsum="2" dolor="3" sit="4">
</component>

<component foo="1" bar="3"></component>

<component foo="1" bar="2"
foobar="3"></component>

<component
lorem="1" ipsum="2"
dolor="3"
sit="4"
>
</component>
```

Examples of **correct** code for this rule:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:+1: Examples of **correct** code for this rule:


```js
Copy link
Contributor

@armano2 armano2 Jul 30, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

```html

<component lorem="1" ipsum="2" dolor="3">
</component>

<component
foo="1"
bar="3"
lorem="1"
ipsum="2"
dolor="3"
sit="4"
>
</component>

Expand All @@ -38,42 +47,68 @@ Examples of **correct** code for this rule:
```
{
"vue/max-attributes-per-line": ["error", {
"firstline": 1,
"singleline": 2,
"multiline": 1
}]
}
```

### `firstline`
For muliline declarations, defines if it accepts an attribute in the first line.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For multi-line declarations, defines how many attributes are acceptable to be put in the first line. (Default 0)

We can discuss default settings later :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed ✅


The following patterns is considered a warning:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's be consistent :)

Example of **incorrect** code for this setting:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed ✅

```js
Copy link
Contributor

@armano2 armano2 Jul 30, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

```html

// ["error", { "firstline": 1, "singleline": 2, "multiline": 1}]
<component
foo="John" bar="Smith"
foobar={5555555}>
</component>;
```

The following pattern is not considered a warning:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Example of **correct** code for this setting:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed them all ✅

```js
Copy link
Contributor

@armano2 armano2 Jul 30, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

```html

// ["error", { "firstline": 0, "singleline": 3, "multiline": 1}]
<component
foo="John"
bar="Smith"
foobar={5555555}
>
</component>;
```


### `singleline`
Number of maximum number of props when a tag is in a single line.
Number of maximum attributes per line when the opening tag is in a single line.

The following patterns is considered a warning:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above:

Example of **incorrect** code for this setting:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

```js
Copy link
Contributor

@armano2 armano2 Jul 30, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

```html

// ["error", { "singleline": 2, "multiline": 1}]
// ["error", { "firstline": 1, "singleline": 2, "multiline": 1}]
<component foo="John" bar="Smith" foobar={5555555}></component>;
```

The following pattern is not considered a warning:
```js
Copy link
Contributor

@armano2 armano2 Jul 30, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

```html

// ["error", { "singleline": 3, "multiline": 1}]
// ["error", { "firstline": 1, "singleline": 3, "multiline": 1}]
<component foo="John" bar="Smith" foobar={5555555}></component>;
```


### `multiline`
Number of maximum number of props when a tag is in a single line.
Number of maximum attributes per line when a tag is in multiple lines.


The following patterns is considered a warning:
```js
Copy link
Contributor

@armano2 armano2 Jul 30, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

```html

// ["error", { "singleline": 2, "multiline": 1}]
// ["error", { "firstline": 1, "singleline": 2, "multiline": 1}]
<component foo="John" bar="Smith"
foobar={5555555}>
</component>;
```

The following pattern is not considered a warning:
```js
Copy link
Contributor

@armano2 armano2 Jul 30, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

```html

// ["error", { "singleline": 3, "multiline": 1}]
// ["error", { "firstline": 0, "singleline": 3, "multiline": 1}]
<component
foo="John"
bar="Smith"
Expand All @@ -84,5 +119,5 @@ The following pattern is not considered a warning:

## When Not To Use It

If you do not want to check the number of props declared per line you can disable this rule.
If you do not want to check the number of attributes declared per line you can disable this rule.