-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheslint-plugin-project-limits.js
More file actions
127 lines (115 loc) · 3.49 KB
/
eslint-plugin-project-limits.js
File metadata and controls
127 lines (115 loc) · 3.49 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
const fs = require('fs');
const path = require('path');
const { ESLint } = require('eslint');
module.exports = {
rules: {
'max-lines': {
meta: {
type: 'problem',
docs: {
description: 'Enforce a maximum number of lines per file',
category: 'Best Practices',
recommended: false,
},
schema: [
{
type: 'object',
properties: {
max: { type: 'number' },
},
additionalProperties: false,
},
],
messages: {
maxLines: 'File has {{lineCount}} lines, which exceeds the maximum of {{max}} lines.',
},
},
create(context) {
const options = context.options[0] || {};
const max = options.max || 300;
const filename = context.getFilename();
// Skip node_modules and hidden files
if (filename.includes('node_modules') || path.basename(filename).startsWith('.')) {
return {};
}
const sourceCode = context.getSourceCode();
const lineCount = sourceCode.lines.length;
if (lineCount > max) {
context.report({
loc: { line: 1, column: 0 },
messageId: 'maxLines',
data: {
lineCount,
max,
},
});
}
return {};
},
},
'max-dir-files': {
meta: {
type: 'problem',
docs: {
description: 'Enforce a maximum number of flat files per directory',
category: 'Best Practices',
recommended: false,
},
schema: [
{
type: 'object',
properties: {
max: { type: 'number' },
},
additionalProperties: false,
},
],
messages: {
maxFiles: 'Directory {{directory}} contains {{fileCount}} flat files, which exceeds the maximum of {{max}}.',
},
},
create(context) {
const options = context.options[0] || {};
const max = options.max || 20;
const filename = context.getFilename();
const directory = path.dirname(filename);
// Skip node_modules
if (directory.includes('node_modules')) {
return {};
}
// Only report once per directory (on the first file ESLint processes in that dir)
if (!context.settings || !context.settings._checkedDirs) {
context.settings = context.settings || {};
context.settings._checkedDirs = new Set();
}
if (context.settings._checkedDirs.has(directory)) {
return {};
}
context.settings._checkedDirs.add(directory);
try {
const entries = fs.readdirSync(directory, { withFileTypes: true });
const flatFiles = entries.filter(entry =>
entry.isFile() &&
!entry.name.startsWith('.') &&
(entry.name.endsWith('.ts') || entry.name.endsWith('.tsx') ||
entry.name.endsWith('.js') || entry.name.endsWith('.jsx'))
);
if (flatFiles.length > max) {
context.report({
loc: { line: 1, column: 0 },
messageId: 'maxFiles',
data: {
directory,
fileCount: flatFiles.length,
max,
},
});
}
} catch (error) {
// Ignore errors reading directory
}
return {};
},
},
},
};