forked from eslint-community/eslint-plugin-eslint-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequire-meta-schema.js
137 lines (120 loc) · 4.1 KB
/
require-meta-schema.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
'use strict';
const { findVariable } = require('eslint-utils');
const utils = require('../utils');
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'require rules to implement a `meta.schema` property',
category: 'Rules',
recommended: false, // TODO: enable it in a major release.
},
hasSuggestions: true,
schema: [
{
type: 'object',
properties: {
requireSchemaPropertyWhenOptionless: {
type: 'boolean',
default: true,
},
},
additionalProperties: false,
},
],
messages: {
addEmptySchema: 'Add empty schema indicating the rule has no options.',
foundOptionsUsage: '`meta.schema` has no schema defined but rule has options.',
missing: '`meta.schema` is required (use [] if rule has no schema).',
wrongType: '`meta.schema` should be an array or object (use [] if rule has no schema).',
},
},
create (context) {
const sourceCode = context.getSourceCode();
const { scopeManager } = sourceCode;
const info = utils.getRuleInfo(sourceCode);
if (info === null || info.meta === null) {
return {};
}
let contextIdentifiers;
const metaNode = info.meta;
let schemaNode;
// Options
const requireSchemaPropertyWhenOptionless = !context.options[0] || context.options[0].requireSchemaPropertyWhenOptionless;
let hasEmptySchema = false;
let isUsingOptions = false;
return {
Program (ast) {
contextIdentifiers = utils.getContextIdentifiers(context, ast);
schemaNode =
metaNode &&
metaNode.properties &&
metaNode.properties.find(p => p.type === 'Property' && utils.getKeyName(p) === 'schema');
if (!schemaNode) {
return;
}
let { value } = schemaNode;
if (value.type === 'Identifier' && value.name !== 'undefined') {
const variable = findVariable(
scopeManager.acquire(value) || scopeManager.globalScope,
value
);
// If we can't find the declarator, we have to assume it's in correct type
if (
!variable ||
!variable.defs ||
!variable.defs[0] ||
!variable.defs[0].node ||
variable.defs[0].node.type !== 'VariableDeclarator' ||
!variable.defs[0].node.init
) {
return;
}
value = variable.defs[0].node.init;
}
if (
(value.type === 'ArrayExpression' && value.elements.length === 0) ||
(value.type === 'ObjectExpression' && value.properties.length === 0)
) {
// Schema is explicitly defined as having no options.
hasEmptySchema = true;
}
if (!['ArrayExpression', 'ObjectExpression'].includes(value.type)) {
context.report({ node: value, messageId: 'wrongType' });
}
},
'Program:exit' () {
if (!schemaNode && requireSchemaPropertyWhenOptionless) {
context.report({
node: metaNode,
messageId: 'missing',
suggest: isUsingOptions ? [] : [
{
messageId: 'addEmptySchema',
fix (fixer) {
return utils.insertProperty(fixer, metaNode, 'schema: []', sourceCode);
},
},
],
});
}
},
MemberExpression (node) {
// Check if `context.options` was used when no options were defined in `meta.schema`.
if (
(hasEmptySchema || !schemaNode) &&
node.object.type === 'Identifier' &&
contextIdentifiers.has(node.object) &&
node.property.type === 'Identifier' &&
node.property.name === 'options'
) {
isUsingOptions = true;
context.report({ node: schemaNode || metaNode, messageId: 'foundOptionsUsage' });
}
},
};
},
};