@emberbox/lint 0.0.0-stage → 0.2.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 +77 -2
- package/package.json +45 -4
- package/src/cli/browser-storage.mjs +87 -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 +207 -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 +41 -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,143 @@
|
|
|
1
|
+
import {resolve} from 'node:path'
|
|
2
|
+
import {parseVueFile, sourcePath, staticAttribute, vueFilesIn, walkElements} from './vue-template.mjs'
|
|
3
|
+
|
|
4
|
+
const MIN_CLASS_TEXT = 15
|
|
5
|
+
const MIN_CLASSES = 3
|
|
6
|
+
|
|
7
|
+
/** Classes that only lay things out, size or space them: a pattern of nothing else is not a component. */
|
|
8
|
+
const LAYOUT_PREFIX = /^(flex|inline-flex|grid|gap-|items-|justify-|self-|place-|col-span|row-span|sm:|md:|lg:|xl:)/
|
|
9
|
+
const STACKING_PREFIX = /^(space-[xy]-|order-|grow|shrink|basis-)/
|
|
10
|
+
const TEXT_SIZE = /^(text-xs|text-sm|text-base|text-lg|text-xl|text-center|text-right|text-left)$/
|
|
11
|
+
const BOX_PREFIX = /^(m[trblxy]?-|p[trblxy]?-|w-|h-|min-w-|max-w-|overflow-)/
|
|
12
|
+
const LAYOUT_WORD = /^(flex-wrap|flex-col|flex-row|flex-1|relative|absolute)$/
|
|
13
|
+
|
|
14
|
+
const projectCounts = new Map()
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Whether a class only places or sizes an element.
|
|
18
|
+
*
|
|
19
|
+
* @param name one class
|
|
20
|
+
* @returns true for a layout, spacing or text size utility
|
|
21
|
+
*/
|
|
22
|
+
function isLayout(name) {
|
|
23
|
+
return LAYOUT_PREFIX.test(name) || STACKING_PREFIX.test(name) || TEXT_SIZE.test(name)
|
|
24
|
+
|| BOX_PREFIX.test(name) || LAYOUT_WORD.test(name)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* The pattern an element stands for: its tag with its sorted classes, where the element opens its
|
|
29
|
+
* line and carries a plain class list on that line long and specific enough to be worth a name.
|
|
30
|
+
*
|
|
31
|
+
* @param element a template element
|
|
32
|
+
* @param lines the file's lines
|
|
33
|
+
* @returns the pattern, or null where the element is not one
|
|
34
|
+
*/
|
|
35
|
+
function patternOf(element, lines) {
|
|
36
|
+
const start = element.loc.start
|
|
37
|
+
if (lines[start.line - 1].slice(0, start.column).trim() !== '') return null
|
|
38
|
+
const attribute = staticAttribute(element, 'class')
|
|
39
|
+
const text = attribute?.value?.value
|
|
40
|
+
if (!text || text.length < MIN_CLASS_TEXT || attribute.loc.start.line !== start.line) return null
|
|
41
|
+
const classes = text.trim().split(/\s+/).filter(name => name.length > 0)
|
|
42
|
+
if (classes.length < MIN_CLASSES || classes.every(isLayout)) return null
|
|
43
|
+
return `<${element.rawName} class="${[...classes].sort().join(' ')}">`
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Every pattern-carrying element of a template.
|
|
48
|
+
*
|
|
49
|
+
* @param template the template root
|
|
50
|
+
* @param lines the file's lines
|
|
51
|
+
* @returns the elements with their patterns
|
|
52
|
+
*/
|
|
53
|
+
function patternsIn(template, lines) {
|
|
54
|
+
const found = []
|
|
55
|
+
walkElements(template, element => {
|
|
56
|
+
const pattern = patternOf(element, lines)
|
|
57
|
+
if (pattern) found.push({element, pattern})
|
|
58
|
+
})
|
|
59
|
+
return found
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Whether a file is one the rule reads: the components are where patterns are extracted to, so
|
|
64
|
+
* repeating there is expected.
|
|
65
|
+
*
|
|
66
|
+
* @param file an absolute path
|
|
67
|
+
* @param sourceDir the absolute source directory
|
|
68
|
+
* @returns true outside its `components` directory
|
|
69
|
+
*/
|
|
70
|
+
function isScanned(file, sourceDir) {
|
|
71
|
+
return !sourcePath(file, sourceDir).startsWith('components/')
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* How often each pattern occurs across the views and pages of a source directory, read once per
|
|
76
|
+
* process.
|
|
77
|
+
*
|
|
78
|
+
* @param sourceDir the absolute source directory
|
|
79
|
+
* @returns counts by pattern
|
|
80
|
+
*/
|
|
81
|
+
function countsAcrossProject(sourceDir) {
|
|
82
|
+
if (projectCounts.has(sourceDir)) return projectCounts.get(sourceDir)
|
|
83
|
+
const counts = new Map()
|
|
84
|
+
projectCounts.set(sourceDir, counts)
|
|
85
|
+
for (const file of vueFilesIn(sourceDir).filter(candidate => isScanned(candidate, sourceDir))) {
|
|
86
|
+
const {ast, lines} = parseVueFile(file)
|
|
87
|
+
if (!ast.templateBody) continue
|
|
88
|
+
for (const {pattern} of patternsIn(ast.templateBody, lines)) {
|
|
89
|
+
counts.set(pattern, (counts.get(pattern) ?? 0) + 1)
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return counts
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* An element whose tag and class list repeat so often across the views that they want a component.
|
|
97
|
+
*
|
|
98
|
+
* <p>Counted over every template outside the `components` directory of the source directory
|
|
99
|
+
* (option `src`, default `src`, relative to the working directory), and reported at each occurrence once the
|
|
100
|
+
* total reaches the threshold. With `scanProject` off only the file itself is counted, which is how
|
|
101
|
+
* the rule is put to a test.
|
|
102
|
+
*
|
|
103
|
+
* @type {import('eslint').Rule.RuleModule}
|
|
104
|
+
*/
|
|
105
|
+
export default {
|
|
106
|
+
meta: {
|
|
107
|
+
type: 'suggestion',
|
|
108
|
+
docs: {description: 'Require a component for an element pattern repeated across the views'},
|
|
109
|
+
schema: [{
|
|
110
|
+
type: 'object',
|
|
111
|
+
properties: {
|
|
112
|
+
threshold: {type: 'integer', minimum: 2},
|
|
113
|
+
scanProject: {type: 'boolean'},
|
|
114
|
+
src: {type: 'string'},
|
|
115
|
+
},
|
|
116
|
+
additionalProperties: false,
|
|
117
|
+
}],
|
|
118
|
+
messages: {
|
|
119
|
+
repeated: 'Repeated pattern ({{count}}x across the views): {{pattern}} - extract to a component.',
|
|
120
|
+
},
|
|
121
|
+
},
|
|
122
|
+
create(context) {
|
|
123
|
+
const threshold = context.options[0]?.threshold ?? 10
|
|
124
|
+
const scanProject = context.options[0]?.scanProject ?? true
|
|
125
|
+
const file = context.filename
|
|
126
|
+
const sourceDir = resolve(context.cwd, context.options[0]?.src ?? 'src')
|
|
127
|
+
return {
|
|
128
|
+
Program() {
|
|
129
|
+
const template = context.sourceCode.ast.templateBody
|
|
130
|
+
if (!template || !isScanned(file, sourceDir)) return
|
|
131
|
+
const found = patternsIn(template, context.sourceCode.lines)
|
|
132
|
+
const local = new Map()
|
|
133
|
+
for (const {pattern} of found) local.set(pattern, (local.get(pattern) ?? 0) + 1)
|
|
134
|
+
const counts = scanProject ? countsAcrossProject(sourceDir) : local
|
|
135
|
+
for (const {element, pattern} of found) {
|
|
136
|
+
const count = Math.max(counts.get(pattern) ?? 0, local.get(pattern))
|
|
137
|
+
if (count < threshold) continue
|
|
138
|
+
context.report({loc: element.startTag.loc, messageId: 'repeated', data: {count, pattern}})
|
|
139
|
+
}
|
|
140
|
+
},
|
|
141
|
+
}
|
|
142
|
+
},
|
|
143
|
+
}
|
|
@@ -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
|
+
}
|