forked from eslint-community/eslint-plugin-eslint-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequire-meta-docs-description.js
87 lines (75 loc) · 2.79 KB
/
require-meta-docs-description.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
'use strict';
const { getStaticValue } = require('eslint-utils');
const utils = require('../utils');
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
const DEFAULT_PATTERN = new RegExp('^(enforce|require|disallow)');
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'require rules to implement a meta.docs.description property with the correct format',
category: 'Rules',
recommended: false, // TODO: enable it in a major release.
},
fixable: null,
schema: [
{
type: 'object',
properties: {
pattern: {
type: 'string',
},
},
additionalProperties: false,
},
],
messages: {
missing: '`meta.docs.description` is required.',
wrongType: '`meta.docs.description` must be a non-empty string.',
extraWhitespace: '`meta.docs.description` must not have leading nor trailing whitespace.',
},
},
create (context) {
const sourceCode = context.getSourceCode();
const info = utils.getRuleInfo(sourceCode);
return {
Program () {
if (info === null || info.meta === null) {
return;
}
const pattern = context.options[0] && context.options[0].pattern ? new RegExp(context.options[0].pattern) : DEFAULT_PATTERN;
const metaNode = info.meta;
const docsNode =
metaNode &&
metaNode.properties &&
metaNode.properties.find(p => p.type === 'Property' && utils.getKeyName(p) === 'docs');
const descriptionNode =
docsNode &&
docsNode.value.properties &&
docsNode.value.properties.find(p => p.type === 'Property' && utils.getKeyName(p) === 'description');
if (!descriptionNode) {
context.report({ node: docsNode ? docsNode : metaNode, messageId: 'missing' });
return;
}
const staticValue = getStaticValue(descriptionNode.value, context.getScope());
if (!staticValue) {
// Ignore non-static values since we can't determine what they look like.
return;
}
if (typeof staticValue.value !== 'string' || staticValue.value === '') {
context.report({ node: descriptionNode.value, messageId: 'wrongType' });
} else if (staticValue.value !== staticValue.value.trim()) {
context.report({ node: descriptionNode.value, messageId: 'extraWhitespace' });
} else if (!pattern.test(staticValue.value)) {
context.report({
node: descriptionNode.value,
message: '`meta.docs.description` must match the regexp {{pattern}}.',
data: { pattern },
});
}
},
};
},
};