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
Merged
88 changes: 88 additions & 0 deletions docs/rules/max-attributes-per-line.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# Define the number of attributes allows per line (max-attributes-per-line)

Limits the maximum number of props per line to improve readability
Copy link
Member

Choose a reason for hiding this comment

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

I'd say maximum number of attributes/properties and add . at the end of the sentence.



## 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.
Copy link
Member

Choose a reason for hiding this comment

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

Again attributes, unless we only care about components' props. But I think we should care about every attribute. Prop is just another type of property/attribute.

A prop is considered to be in a new line when there is a line break between two props.
Copy link
Member

@michalsnik michalsnik Jun 29, 2017

Choose a reason for hiding this comment

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

Maybe something like: There is configurable number of props that are acceptable in one-line case, as well as how many attributes should be acceptable per line in multi-line case.?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am a little lost here, should I add it or replace something with it?

Copy link
Member

Choose a reason for hiding this comment

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

Instead of text on line 9.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍


The default is one prop per line

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 foo="1" bar="3"></component>
Copy link
Member

Choose a reason for hiding this comment

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

I think default should be:

  • singleline: 3
  • multiline: 1

Then examples could look like this:
Incorrect:

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

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

Correct:

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

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

We should probably add additional option for specifying if we accept attribute in first line or not too, maybe something like firstline: 1, that would make it possible to have one property in the first line, or more if specified. WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense to me!


<component foo="1" bar="2"
foobar="3"></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
foo="1"
bar="3"
>
</component>

```

### Options
Copy link
Contributor

Choose a reason for hiding this comment

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

## :wrench: Options


```
{
"vue/max-attributes-per-line": ["error", {
"singleline": 2,
"multiline": 1
}]
}
```
### `singleline`
Number of maximum number of props when a tag is in a single line.
Copy link
Member

Choose a reason for hiding this comment

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

When the opening tag is in a single line to be precise.


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}]
<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}]
<component foo="John" bar="Smith" foobar={5555555}></component>;
```


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


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}]
<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}]
<component
foo="John"
bar="Smith"
foobar={5555555}
>
</component>;
```

## When Not To Use It

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

44 changes: 44 additions & 0 deletions lib/rules/max-attributes-per-line.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* @fileoverview Define the number of attributes allows per line
* @author Filipa Lacerda
*/
"use strict";

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------

module.exports = {
meta: {
docs: {
description: "Define the number of attributes allows per line",
category: "Fill me in",
recommended: false
},
fixable: null, // or "code" or "whitespace"
schema: [
// fill in your schema
]
},

create: function(context) {

// variables should be defined here

//----------------------------------------------------------------------
// Helpers
//----------------------------------------------------------------------

// any helper functions should go here or else delete this section

//----------------------------------------------------------------------
// Public
//----------------------------------------------------------------------

return {

// give me methods

};
}
};
37 changes: 37 additions & 0 deletions tests/lib/rules/max-attributes-per-line.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* @fileoverview Define the number of attributes allows per line
* @author Filipa Lacerda
*/
"use strict";

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

var rule = require("../../../lib/rules/max-attributes-per-line"),

RuleTester = require("eslint").RuleTester;


//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------

var ruleTester = new RuleTester();
ruleTester.run("max-attributes-per-line", rule, {

valid: [

// give me some code that won't trigger a warning
],

invalid: [
{
code: "",
errors: [{
message: "Fill me in.",
type: "Me too"
}]
}
]
});