forked from microsoft/fluentui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmax-len.js
65 lines (61 loc) · 1.95 KB
/
max-len.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
// @ts-check
const createRule = require('../utils/createRule');
module.exports = createRule({
name: 'max-len',
meta: {
type: 'layout',
docs: {
// The default max-len rule is surprisingly slow, because:
// - it has options to detect and specially handle comments, strings, etc
// - it checks the ignore regex without even a preliminary length check first
description: 'Enforces a maximum line length, more cheaply than default ESLint version',
recommended: false,
},
messages: {
max: 'This line has a length of {{lineLength}}. Maximum allowed is {{max}}.',
},
schema: [
{
type: 'object',
properties: {
ignorePatterns: {
description: 'regular expressions to ignore',
type: 'array',
items: { type: 'string' },
},
max: {
type: 'integer',
minimum: 0,
},
},
additionalProperties: false,
},
],
},
defaultOptions: [],
create: context => {
const options = /** @type {{ max?: number; ignorePatterns?: string[]; }} */ (context.options[0] || {});
const { ignorePatterns = /** @type {string[]} */ ([]), max = 120 } = options;
const ignoreRegexes = ignorePatterns.map(pat => new RegExp(pat));
const sourceCode = context.getSourceCode();
return {
Program: program => {
sourceCode.getLines().forEach((line, i) => {
// Check length FIRST. Important for performance.
if (line.length > max && !ignoreRegexes.some(r => r.test(line))) {
const lineNumber = i + 1;
context.report({
node: program,
loc: {
start: { line: lineNumber, column: 0 },
end: { line: lineNumber, column: line.length },
},
messageId: 'max',
data: { lineLength: line.length, max },
});
}
});
},
};
},
});