-
-
Notifications
You must be signed in to change notification settings - Fork 681
/
Copy pathmax-lines-per-block.js
111 lines (103 loc) · 2.67 KB
/
max-lines-per-block.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/**
* @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 sourceCode = context.getSourceCode()
const documentFragment =
sourceCode.parserServices.getDocumentFragment &&
sourceCode.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
}
})
}
}
}
}
}
}
}