forked from eslint-community/eslint-plugin-eslint-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequire-meta-schema-description.js
111 lines (92 loc) · 2.87 KB
/
require-meta-schema-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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
'use strict';
const { getStaticValue } = require('@eslint-community/eslint-utils');
const utils = require('../utils');
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'suggestion',
docs: {
description:
'require rules `meta.schema` properties to include descriptions',
category: 'Rules',
recommended: false,
url: 'https://github.com/eslint-community/eslint-plugin-eslint-plugin/tree/HEAD/docs/rules/require-meta-schema-description.md',
},
schema: [],
messages: {
missingDescription: 'Schema option is missing an ajv description.',
},
},
create(context) {
const sourceCode = context.sourceCode || context.getSourceCode(); // TODO: just use context.sourceCode when dropping eslint < v9
const { scopeManager } = sourceCode;
const ruleInfo = utils.getRuleInfo(sourceCode);
if (!ruleInfo) {
return {};
}
const schemaNode = utils.getMetaSchemaNode(ruleInfo.meta, scopeManager);
if (!schemaNode) {
return {};
}
const schemaProperty = utils.getMetaSchemaNodeProperty(
schemaNode,
scopeManager,
);
if (schemaProperty?.type !== 'ArrayExpression') {
return {};
}
for (const element of schemaProperty.elements) {
checkSchemaElement(element, true);
}
return {};
function checkSchemaElement(node, isRoot) {
if (node.type !== 'ObjectExpression') {
return;
}
let hadChildren = false;
let hadDescription = false;
for (const { key, value } of node.properties) {
const staticKey =
key.type === 'Identifier' ? { value: key.name } : getStaticValue(key);
if (!staticKey?.value) {
continue;
}
switch (key.name ?? key.value) {
case 'description': {
hadDescription = true;
break;
}
case 'oneOf': {
hadChildren = true;
if (value.type === 'ArrayExpression') {
for (const element of value.elements) {
checkSchemaElement(element, isRoot);
}
}
break;
}
case 'properties': {
hadChildren = true;
if (Array.isArray(value.properties)) {
for (const property of value.properties) {
if (property.value?.type === 'ObjectExpression') {
checkSchemaElement(property.value);
}
}
}
break;
}
}
}
if (!hadDescription && !(hadChildren && isRoot)) {
context.report({
messageId: 'missingDescription',
node,
});
}
}
},
};