-
-
Notifications
You must be signed in to change notification settings - Fork 680
New Add vue/new-line-between-multiline-property
rule
#1080
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
ota-meshi
merged 20 commits into
vuejs:master
from
IWANABETHATGUY:feature/new-line-between-multi-line-property
Dec 16, 2020
Merged
Changes from 18 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
28ffe21
feat: init commit
IWANABETHATGUY dcce787
feat: add test, make rule configurable
IWANABETHATGUY df5a3f2
docs: update doc📖
IWANABETHATGUY fed017e
feat: merge from master
IWANABETHATGUY 0441bb0
chore: update version
IWANABETHATGUY a63853d
ierge branch 'master' into feature/new-line-between-multi-line-property
IWANABETHATGUY d03dbda
lint fix
IWANABETHATGUY 37654fc
test: 💍 add test case
IWANABETHATGUY 154b00a
test: 💍 fix report when multi-line-property in callExpression
IWANABETHATGUY be657ee
Merge branch 'master' into feature/new-line-between-multi-line-property
IWANABETHATGUY f948a95
test: 💍 fix test in change request
IWANABETHATGUY bc6d9a5
Merge branch 'master' of github.com:IWANABETHATGUY/eslint-plugin-vue …
IWANABETHATGUY 4b3d8c1
Merge branch 'feature/new-line-between-multi-line-property' of github…
IWANABETHATGUY 719c920
test: 💍 js doc
IWANABETHATGUY f7a7980
Merge branch 'feature/new-line-between-multi-line-property' of github…
IWANABETHATGUY 269a40e
test: 💍 Vue.component()
IWANABETHATGUY 26e9d00
docs: ✏️ docs
IWANABETHATGUY 12a9aaa
fix: 🐛 using sourceCode api
IWANABETHATGUY aa7d47a
fix: 🐛 using getTokenBewteen instead of getAlltoken
IWANABETHATGUY b556320
fix: 🐛 lint error
IWANABETHATGUY File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,140 @@ | ||
/** | ||
* @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 = [] | ||
/** @type {any[]} */ | ||
let comments = context.getSourceCode().getAllComments(); | ||
return Object.assign( | ||
{ | ||
/** | ||
* @param {Program} node | ||
*/ | ||
Program(node) { | ||
comments = node.comments | ||
IWANABETHATGUY marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
}, | ||
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] | ||
|
||
/** @type {any[]} */ | ||
const leadingComments = [] | ||
// getFirstLeadingComments is enough , after that break the loop. | ||
for ( | ||
let commentsIndex = 0; | ||
commentsIndex < comments.length; | ||
commentsIndex++ | ||
) { | ||
const comment = comments[commentsIndex] | ||
if ( | ||
comment.range[0] > pre.range[1] && | ||
comment.range[1] < cur.range[0] | ||
) { | ||
leadingComments.push(comments[commentsIndex]) | ||
IWANABETHATGUY marked this conversation as resolved.
Show resolved
Hide resolved
|
||
break | ||
} | ||
} | ||
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.