-
-
Notifications
You must be signed in to change notification settings - Fork 681
Add vue/max-lines-per-block
rule
#2213
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
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
03e2aa2
Add `vue/max-lines-per-block` rule
lsdsjy 29e9aab
Fix lint problems
lsdsjy 2d687bf
Fix doc
lsdsjy f52b4b2
Fix doc
lsdsjy 40b9250
Use backtick to escape
lsdsjy 7d0cebf
Add title for examples & fix missing end tag
lsdsjy 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
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,48 @@ | ||
--- | ||
pageClass: rule-details | ||
sidebarDepth: 0 | ||
title: vue/max-lines-per-block | ||
description: enforce maximum number of lines in Vue SFC blocks | ||
--- | ||
# vue/max-lines-per-block | ||
|
||
> enforce maximum number of lines in Vue SFC blocks | ||
|
||
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge> | ||
|
||
## :book: Rule Details | ||
|
||
This rule enforces a maximum number of lines per block, in order to aid in maintainability and reduce complexity. | ||
|
||
## :wrench: Options | ||
|
||
This rule takes an object, where you can specify the maximum number of lines in each type of SFC block and customize the line counting behavior. | ||
The following properties can be specified for the object. | ||
|
||
- `script` ... Specify the maximum number of lines in <script> block. Won't enforce limitation if not specified. | ||
- `template` ... Specify the maximum number of lines in <template> block. Won't enforce limitation if not specified. | ||
- `style` ... Specify the maximum number of lines in <style> block. Won't enforce limitation if not specified. | ||
- `skipBlankLines` ... Ignore lines made up purely of whitespaces. | ||
|
||
<eslint-code-block :rules="{'vue/max-lines-per-block': ['error', { template: 2 }]}"> | ||
|
||
```vue | ||
<!-- ✗ BAD --> | ||
<template> | ||
<div> | ||
hi | ||
</div> | ||
</template> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
<eslint-code-block :rules="{'vue/max-lines-per-block': ['error', { script: 1, skipBlankLines: true }]}"> | ||
|
||
```vue | ||
<!-- ✓ GOOD --> | ||
<script> | ||
|
||
console.log(1) | ||
</script> | ||
``` | ||
lsdsjy marked this conversation as resolved.
Show resolved
Hide resolved
|
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,110 @@ | ||
/** | ||
* @author lsdsjy | ||
* @fileoverview Rule for checking the maximum number of lines in Vue SFC blocks. | ||
*/ | ||
'use strict' | ||
|
||
const { SourceCode } = require('eslint') | ||
const utils = require('../utils') | ||
|
||
/** | ||
* @param {string} text | ||
*/ | ||
function isEmptyLine(text) { | ||
return !text.trim() | ||
} | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'enforce maximum number of lines in Vue SFC blocks', | ||
categories: undefined, | ||
url: 'https://eslint.vuejs.org/rules/max-lines-per-block.html' | ||
}, | ||
fixable: null, | ||
schema: [ | ||
{ | ||
type: 'object', | ||
properties: { | ||
style: { | ||
type: 'integer', | ||
minimum: 1 | ||
}, | ||
template: { | ||
type: 'integer', | ||
minimum: 1 | ||
}, | ||
script: { | ||
type: 'integer', | ||
minimum: 1 | ||
}, | ||
skipBlankLines: { | ||
type: 'boolean', | ||
minimum: 0 | ||
} | ||
}, | ||
additionalProperties: false | ||
} | ||
], | ||
messages: { | ||
tooManyLines: | ||
'Block has too many lines ({{lineCount}}). Maximum allowed is {{limit}}.' | ||
} | ||
}, | ||
/** @param {RuleContext} context */ | ||
create(context) { | ||
const option = context.options[0] || {} | ||
/** | ||
* @type {Record<string, number>} | ||
*/ | ||
const limits = { | ||
template: option.template, | ||
script: option.script, | ||
style: option.style | ||
} | ||
|
||
const code = context.getSourceCode() | ||
const documentFragment = | ||
context.parserServices.getDocumentFragment && | ||
context.parserServices.getDocumentFragment() | ||
|
||
function getTopLevelHTMLElements() { | ||
if (documentFragment) { | ||
return documentFragment.children.filter(utils.isVElement) | ||
} | ||
return [] | ||
} | ||
|
||
return { | ||
/** @param {Program} node */ | ||
Program(node) { | ||
if (utils.hasInvalidEOF(node)) { | ||
return | ||
} | ||
for (const block of getTopLevelHTMLElements()) { | ||
if (limits[block.name]) { | ||
// We suppose the start tag and end tag occupy one single line respectively | ||
let lineCount = block.loc.end.line - block.loc.start.line - 1 | ||
|
||
if (option.skipBlankLines) { | ||
const lines = SourceCode.splitLines(code.getText(block)) | ||
lineCount -= lines.filter(isEmptyLine).length | ||
} | ||
|
||
if (lineCount > limits[block.name]) { | ||
context.report({ | ||
node: block, | ||
messageId: 'tooManyLines', | ||
data: { | ||
limit: limits[block.name], | ||
lineCount | ||
} | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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,84 @@ | ||
/** | ||
* @author lsdsjy | ||
*/ | ||
'use strict' | ||
|
||
const RuleTester = require('eslint').RuleTester | ||
const rule = require('../../../lib/rules/max-lines-per-block') | ||
|
||
const tester = new RuleTester({ | ||
parser: require.resolve('vue-eslint-parser'), | ||
parserOptions: { | ||
ecmaVersion: 2020, | ||
sourceType: 'module' | ||
} | ||
}) | ||
|
||
tester.run('max-lines-per-block', rule, { | ||
valid: [ | ||
{ | ||
code: ` | ||
<script> | ||
console.log(1) | ||
</script> | ||
<template> | ||
<div></div> | ||
</template> | ||
`, | ||
options: [{ template: 1 }] | ||
}, | ||
{ | ||
code: ` | ||
<template> | ||
|
||
<div></div> | ||
</template> | ||
`, | ||
options: [{ template: 1, skipBlankLines: true }] | ||
}, | ||
{ | ||
code: ` | ||
<template> | ||
<div> | ||
</div> | ||
</template> | ||
`, | ||
options: [{ script: 1, style: 1 }] | ||
} | ||
], | ||
invalid: [ | ||
{ | ||
code: ` | ||
<template> | ||
|
||
<div></div> | ||
</template> | ||
`, | ||
options: [{ template: 1 }], | ||
errors: [ | ||
{ | ||
message: 'Block has too many lines (2). Maximum allowed is 1.', | ||
line: 2, | ||
column: 7 | ||
} | ||
] | ||
}, | ||
{ | ||
code: ` | ||
<script> | ||
|
||
const a = 1 | ||
console.log(a) | ||
</script> | ||
`, | ||
options: [{ script: 1, skipBlankLines: true }], | ||
errors: [ | ||
{ | ||
message: 'Block has too many lines (2). Maximum allowed is 1.', | ||
line: 2, | ||
column: 7 | ||
} | ||
] | ||
} | ||
] | ||
}) |
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.