@emberbox/lint 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +73 -2
- package/package.json +45 -4
- package/src/cli/browser-storage.mjs +80 -0
- package/src/cli/comment-scan.mjs +224 -0
- package/src/cli/comments.mjs +28 -0
- package/src/cli/em-dash.mjs +33 -0
- package/src/cli/lint-utils.mjs +104 -0
- package/src/cli/main.mjs +109 -0
- package/src/eslint.mjs +205 -0
- package/src/i18n/catalogue.mjs +74 -0
- package/src/i18n/locale-parser.mjs +271 -0
- package/src/i18n/locales.mjs +27 -0
- package/src/i18n/no-plural-messages.mjs +39 -0
- package/src/i18n/unused-keys.mjs +99 -0
- package/src/i18n/usage.mjs +267 -0
- package/src/plugin.mjs +20 -0
- package/src/restrictions.mjs +21 -0
- package/src/rules/max-function-lines.mjs +77 -0
- package/src/rules/repeated-class-pattern.mjs +143 -0
- package/src/rules/template-block-size.mjs +49 -0
- package/src/rules/vue-template.mjs +135 -0
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import {walkElements} from './vue-template.mjs'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The wrappers that define a page's shell. Its whole body lives inside them, so their size says
|
|
5
|
+
* nothing about a block that wants extracting.
|
|
6
|
+
*/
|
|
7
|
+
const ROOT_WRAPPERS = new Set([
|
|
8
|
+
'template',
|
|
9
|
+
])
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* A single template block of a view spanning so many lines that its inner content wants a
|
|
13
|
+
* component of its own.
|
|
14
|
+
*
|
|
15
|
+
* <p>The root template and the elements directly below it are the page itself, and the shell
|
|
16
|
+
* wrappers hold everything, so neither is measured. A block is measured from the line its opening
|
|
17
|
+
* tag starts on to the line its closing tag starts on; an element that closes itself has no body.
|
|
18
|
+
*
|
|
19
|
+
* @type {import('eslint').Rule.RuleModule}
|
|
20
|
+
*/
|
|
21
|
+
export default {
|
|
22
|
+
meta: {
|
|
23
|
+
type: 'suggestion',
|
|
24
|
+
docs: {description: 'Limit how many lines one template block of a view may span'},
|
|
25
|
+
schema: [{type: 'object', properties: {max: {type: 'integer', minimum: 1}}, additionalProperties: false}],
|
|
26
|
+
messages: {
|
|
27
|
+
tooLong: '<{{tag}}> block spans {{span}} lines (>= {{max}}). Extract to a component.',
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
create(context) {
|
|
31
|
+
const max = context.options[0]?.max ?? 50
|
|
32
|
+
return {
|
|
33
|
+
Program() {
|
|
34
|
+
const template = context.sourceCode.ast.templateBody
|
|
35
|
+
if (!template) return
|
|
36
|
+
walkElements(template, (element, depth) => {
|
|
37
|
+
if (!element.endTag || depth <= 2 || ROOT_WRAPPERS.has(element.rawName)) return
|
|
38
|
+
const span = element.endTag.loc.start.line - element.loc.start.line + 1
|
|
39
|
+
if (span < max) return
|
|
40
|
+
context.report({
|
|
41
|
+
loc: element.startTag.loc,
|
|
42
|
+
messageId: 'tooLong',
|
|
43
|
+
data: {tag: element.rawName, span, max},
|
|
44
|
+
})
|
|
45
|
+
})
|
|
46
|
+
},
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import {existsSync, readdirSync, readFileSync} from 'node:fs'
|
|
2
|
+
import {join, relative, sep} from 'node:path'
|
|
3
|
+
import tsParser from '@typescript-eslint/parser'
|
|
4
|
+
import {parseForESLint} from 'vue-eslint-parser'
|
|
5
|
+
|
|
6
|
+
const parsed = new Map()
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* A component file parsed the way ESLint parses it, once per process.
|
|
10
|
+
*
|
|
11
|
+
* @param file the absolute path of a `.vue` file
|
|
12
|
+
* @returns the program, carrying `templateBody` for the template, and the file's lines
|
|
13
|
+
*/
|
|
14
|
+
export function parseVueFile(file) {
|
|
15
|
+
if (!parsed.has(file)) {
|
|
16
|
+
const text = readFileSync(file, 'utf-8')
|
|
17
|
+
const {ast} = parseForESLint(text, {
|
|
18
|
+
parser: tsParser,
|
|
19
|
+
ecmaVersion: 'latest',
|
|
20
|
+
sourceType: 'module',
|
|
21
|
+
filePath: file,
|
|
22
|
+
})
|
|
23
|
+
parsed.set(file, {ast, lines: text.split('\n')})
|
|
24
|
+
}
|
|
25
|
+
return parsed.get(file)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Every `.vue` file below a directory; none when the directory does not exist.
|
|
30
|
+
*
|
|
31
|
+
* @param dir the directory to walk
|
|
32
|
+
* @returns absolute paths
|
|
33
|
+
*/
|
|
34
|
+
export function vueFilesIn(dir) {
|
|
35
|
+
if (!existsSync(dir)) return []
|
|
36
|
+
const found = []
|
|
37
|
+
for (const entry of readdirSync(dir, {withFileTypes: true})) {
|
|
38
|
+
const full = join(dir, entry.name)
|
|
39
|
+
if (entry.isDirectory()) found.push(...vueFilesIn(full))
|
|
40
|
+
else if (entry.name.endsWith('.vue')) found.push(full)
|
|
41
|
+
}
|
|
42
|
+
return found
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Where a file sits below the source directory, with forward slashes.
|
|
47
|
+
*
|
|
48
|
+
* @param file an absolute path
|
|
49
|
+
* @param sourceDir the absolute source directory
|
|
50
|
+
* @returns the path relative to the source directory
|
|
51
|
+
*/
|
|
52
|
+
export function sourcePath(file, sourceDir) {
|
|
53
|
+
return relative(sourceDir, file).split(sep).join('/')
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* The elements directly below an element, in source order.
|
|
58
|
+
*
|
|
59
|
+
* @param element a template element
|
|
60
|
+
* @returns its child elements
|
|
61
|
+
*/
|
|
62
|
+
export function childElements(element) {
|
|
63
|
+
return element.children.filter(child => child.type === 'VElement')
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Visits every element below a root, depth first, with its depth. The root has depth 1.
|
|
68
|
+
*
|
|
69
|
+
* @param root the element to start from
|
|
70
|
+
* @param visit called with each element and its depth; returning false skips its children
|
|
71
|
+
* @param depth the depth of the root
|
|
72
|
+
*/
|
|
73
|
+
export function walkElements(root, visit, depth = 1) {
|
|
74
|
+
if (visit(root, depth) === false) return
|
|
75
|
+
for (const child of childElements(root)) walkElements(child, visit, depth + 1)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* A plain attribute of an element.
|
|
80
|
+
*
|
|
81
|
+
* @param element a template element
|
|
82
|
+
* @param name the attribute name
|
|
83
|
+
* @returns the attribute node, or undefined
|
|
84
|
+
*/
|
|
85
|
+
export function staticAttribute(element, name) {
|
|
86
|
+
return element.startTag.attributes.find(attribute => !attribute.directive && attribute.key.name === name)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* A directive of an element, such as `v-else` or `:class`.
|
|
91
|
+
*
|
|
92
|
+
* @param element a template element
|
|
93
|
+
* @param name the directive name without `v-`, `bind` for `:`
|
|
94
|
+
* @param argument the argument after the colon, if any
|
|
95
|
+
* @returns the directive node, or undefined
|
|
96
|
+
*/
|
|
97
|
+
export function directive(element, name, argument) {
|
|
98
|
+
return element.startTag.attributes.find(attribute => attribute.directive
|
|
99
|
+
&& attribute.key.name.name === name
|
|
100
|
+
&& (argument === undefined || attribute.key.argument?.name === argument))
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* The text of an element's plain `class` attribute.
|
|
105
|
+
*
|
|
106
|
+
* @param element a template element
|
|
107
|
+
* @returns the classes, or an empty string
|
|
108
|
+
*/
|
|
109
|
+
export function staticClasses(element) {
|
|
110
|
+
return staticAttribute(element, 'class')?.value?.value ?? ''
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* The plain classes of an element and the source of its bound `:class`, as one string, for the
|
|
115
|
+
* checks that care about a class token wherever it is written.
|
|
116
|
+
*
|
|
117
|
+
* @param element a template element
|
|
118
|
+
* @param sourceCode the source the element belongs to, to read the bound expression from
|
|
119
|
+
* @returns the text to test class tokens against
|
|
120
|
+
*/
|
|
121
|
+
export function anyClasses(element, sourceCode) {
|
|
122
|
+
const bound = directive(element, 'bind', 'class')
|
|
123
|
+
const boundText = bound?.value ? sourceCode.getText(bound.value) : ''
|
|
124
|
+
return `${staticClasses(element)} ${boundText}`
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Whether an element is the other branch of a condition, and so never stands beside the one before.
|
|
129
|
+
*
|
|
130
|
+
* @param element a template element
|
|
131
|
+
* @returns true for `v-else` and `v-else-if`
|
|
132
|
+
*/
|
|
133
|
+
export function isAlternative(element) {
|
|
134
|
+
return Boolean(directive(element, 'else') || directive(element, 'else-if'))
|
|
135
|
+
}
|