forked from eslint-community/eslint-plugin-eslint-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequire-meta-has-suggestions.js
79 lines (73 loc) · 3.36 KB
/
require-meta-has-suggestions.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
'use strict';
const utils = require('../utils');
const { getStaticValue } = require('eslint-utils');
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'require suggestable rules to implement a `meta.hasSuggestions` property',
category: 'Rules',
recommended: false,
},
schema: [],
messages: {
shouldBeSuggestable: 'Suggestable rules should specify a `meta.hasSuggestions` property with value `true`.',
shouldNotBeSuggestable: 'Non-suggestable rules should not specify a `meta.hasSuggestions` property with value `true`.',
},
},
create (context) {
const sourceCode = context.getSourceCode();
const ruleInfo = utils.getRuleInfo(sourceCode);
let contextIdentifiers;
let ruleReportsSuggestions;
return {
Program (node) {
contextIdentifiers = utils.getContextIdentifiers(context, node);
},
CallExpression (node) {
if (
node.callee.type === 'MemberExpression' &&
contextIdentifiers.has(node.callee.object) &&
node.callee.property.type === 'Identifier' &&
node.callee.property.name === 'report' &&
(node.arguments.length > 4 || (
node.arguments.length === 1 &&
node.arguments[0].type === 'ObjectExpression'
))
) {
const suggestProp = node.arguments[0].properties.find(prop => utils.getKeyName(prop) === 'suggest');
if (suggestProp) {
const staticValue = getStaticValue(suggestProp.value, context.getScope());
if (!staticValue || (Array.isArray(staticValue.value) && staticValue.value.length > 0)) {
// These are all considered reporting suggestions:
// suggest: [{...}]
// suggest: getSuggestions()
// suggest: MY_SUGGESTIONS
ruleReportsSuggestions = true;
}
}
}
},
'Program:exit' () {
const metaNode = ruleInfo && ruleInfo.meta;
const hasSuggestionsProperty = metaNode && metaNode.type === 'ObjectExpression' ? metaNode.properties.find(prop => utils.getKeyName(prop) === 'hasSuggestions') : undefined;
const hasSuggestionsStaticValue = hasSuggestionsProperty && getStaticValue(hasSuggestionsProperty.value, context.getScope());
if (ruleReportsSuggestions) {
if (!hasSuggestionsProperty) {
// Rule reports suggestions but is missing the `meta.hasSuggestions` property altogether.
context.report({ node: metaNode ? metaNode : ruleInfo.create, messageId: 'shouldBeSuggestable' });
} else if (hasSuggestionsStaticValue.value !== true) {
// Rule reports suggestions but does not have `meta.hasSuggestions` property enabled.
context.report({ node: hasSuggestionsProperty.value, messageId: 'shouldBeSuggestable' });
}
} else if (!ruleReportsSuggestions && hasSuggestionsProperty && hasSuggestionsStaticValue.value === true) {
// Rule does not report suggestions but has the `meta.hasSuggestions` property enabled.
context.report({ node: hasSuggestionsProperty.value, messageId: 'shouldNotBeSuggestable' });
}
},
};
},
};