-
-
Notifications
You must be signed in to change notification settings - Fork 666
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New Add
vue/new-line-between-multiline-property
rule (#1080)
* feat: init commit * feat: add test, make rule configurable * docs: update docπ * chore: update version * lint fix * test: π add test case * test: π fix report when multi-line-property in callExpression * test: π fix test in change request * test: π js doc * test: π Vue.component() * docs: βοΈ docs * fix: π using sourceCode api * fix: π using getTokenBewteen instead of getAlltoken * fix: π lint error
- Loading branch information
1 parent
4a141ce
commit 95fd13f
Showing
7 changed files
with
837 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
--- | ||
pageClass: rule-details | ||
sidebarDepth: 0 | ||
title: vue/new-line-between-multi-line-property | ||
description: enforce new lines between multi-line properties in Vue components | ||
--- | ||
# vue/new-line-between-multi-line-property | ||
> enforce new lines between multi-line properties in Vue components | ||
- :wrench: The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) can automatically fix some of the problems reported by this rule. | ||
|
||
## :book: Rule Details | ||
|
||
This rule aims at enforcing new lines between multi-line properties in Vue components to help readability | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
<eslint-code-block fix :rules="{'vue/new-line-between-multi-line-property': ['error']}"> | ||
|
||
```vue | ||
<script> | ||
export default { | ||
props: { | ||
value: { | ||
type: String, | ||
required: true | ||
}, | ||
focused: { | ||
type: Boolean, | ||
default: false, | ||
required: true | ||
}, | ||
label: String, | ||
icon: String | ||
}, | ||
computed: { | ||
} | ||
} | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
|
||
Examples of **correct** code for this rule: | ||
|
||
<eslint-code-block fix :rules="{'vue/new-line-between-multi-line-property': ['error']}"> | ||
|
||
```vue | ||
<script> | ||
export default { | ||
props: { | ||
value: { | ||
type: String, | ||
required: true | ||
}, | ||
focused: { | ||
type: Boolean, | ||
default: false, | ||
required: true | ||
}, | ||
label: String, | ||
icon: String | ||
}, | ||
computed: { | ||
} | ||
} | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
## :wrench: Option | ||
```json | ||
{ | ||
"vue/new-line-between-multiline-property": ["error", { | ||
"minLineOfMultilineProperty": 2 | ||
}] | ||
} | ||
``` | ||
- `minLineOfMultilineProperty` ... `type: number`, Define the minimum number of rows for a multi-line property .`type:` number, `default:` 2 , `min:`: 2 | ||
## :books: Further Reading | ||
Nothing here | ||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/new-line-between-multi-line-property.js) | ||
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/new-line-between-multi-line-property.js) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/** | ||
* @fileoverview Enforce new lines between multi-line properties in Vue components. | ||
* @author IWANABETHATGUY | ||
*/ | ||
'use strict' | ||
|
||
const utils = require('../utils') | ||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
// @ts-ignore | ||
module.exports = { | ||
meta: { | ||
type: 'layout', | ||
docs: { | ||
description: | ||
'enforce new lines between multi-line properties in Vue components', | ||
categories: undefined, | ||
url: | ||
'https://eslint.vuejs.org/rules/new-line-between-multi-line-property.html' | ||
}, | ||
fixable: 'whitespace', // or "code" or "whitespace" | ||
schema: [ | ||
{ | ||
type: 'object', | ||
properties: { | ||
// number of line you want to insert after multi-line property | ||
minLineOfMultilineProperty: { | ||
type: 'number', | ||
minimum: 2 | ||
} | ||
} | ||
} | ||
] | ||
}, | ||
|
||
/** @param {RuleContext} context */ | ||
create(context) { | ||
// always insert one line | ||
const insertLine = 1 | ||
|
||
let minLineOfMultilineProperty = 2 | ||
if ( | ||
context.options && | ||
context.options[0] && | ||
context.options[0].minLineOfMultilineProperty | ||
) { | ||
minLineOfMultilineProperty = context.options[0].minLineOfMultilineProperty | ||
} | ||
|
||
/** @type {any[]} */ | ||
const callStack = [] | ||
const sourceCode = context.getSourceCode() | ||
return Object.assign( | ||
utils.defineVueVisitor(context, { | ||
CallExpression(node) { | ||
callStack.push(node) | ||
}, | ||
'CallExpression:exit'() { | ||
callStack.pop() | ||
}, | ||
|
||
/** | ||
* @param {ObjectExpression} node | ||
*/ | ||
ObjectExpression(node) { | ||
if (callStack.length) { | ||
return | ||
} | ||
const properties = node.properties | ||
for (let i = 1; i < properties.length; i++) { | ||
const cur = properties[i] | ||
const pre = properties[i - 1] | ||
|
||
const leadingComments = sourceCode | ||
.getTokensBetween(pre, cur, { includeComments: true }) | ||
.filter((token) => ['Line', 'Block'].includes(token.type)) | ||
const lineCountOfPreProperty = | ||
pre.loc.end.line - pre.loc.start.line + 1 | ||
let curStartLine = cur.loc.start.line | ||
if (leadingComments.length) { | ||
curStartLine = leadingComments[0].loc.start.line | ||
} | ||
const lineCountBetweenCurAndPreProperty = | ||
curStartLine - pre.loc.end.line - 1 | ||
if ( | ||
lineCountOfPreProperty >= minLineOfMultilineProperty && | ||
lineCountBetweenCurAndPreProperty < insertLine | ||
) { | ||
context.report({ | ||
node: pre, | ||
loc: pre.loc, | ||
message: | ||
'Enforce new lines between multi-line properties in Vue components.', | ||
// @ts-ignore | ||
fix(fixer) { | ||
let firstPositionOfLine = cur.range[0] - cur.loc.start.column | ||
if (leadingComments.length) { | ||
const firstComment = leadingComments[0] | ||
firstPositionOfLine = | ||
firstComment.range[0] - firstComment.loc.start.column | ||
} | ||
// this action equal to insert number of line before node | ||
return fixer.insertTextAfterRange( | ||
[firstPositionOfLine, firstPositionOfLine], | ||
'\n' | ||
// to avoid conflict with no-multiple-empty-lines, only insert one newline | ||
) | ||
} | ||
}) | ||
} | ||
} | ||
} | ||
}) | ||
) | ||
} | ||
} |
Oops, something went wrong.