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
92 lines (81 loc) · 2.5 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
'use strict';
const { findVariable } = require('eslint-utils');
const utils = require('../utils');
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
module.exports = {
meta: {
docs: {
description: 'require rules to implement a meta.schema property',
category: 'Rules',
recommended: false, // TODO: enable it in a major release.
},
type: 'suggestion',
fixable: 'code',
schema: [
{
type: 'object',
properties: {
exceptRange: {
type: 'boolean',
},
},
additionalProperties: false,
},
],
messages: {
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 { ast, scopeManager } = sourceCode;
const info = utils.getRuleInfo(ast, scopeManager);
return {
Program () {
if (info === null || info.meta === null) {
return;
}
const metaNode = info.meta;
const schemaNode =
metaNode &&
metaNode.properties &&
metaNode.properties.find(p => p.type === 'Property' && utils.getKeyName(p) === 'schema');
if (!schemaNode) {
context.report({
node: metaNode,
messageId: 'missing',
fix (fixer) {
return utils.insertProperty(fixer, metaNode, 'schema: []', sourceCode);
},
});
return;
}
let { value } = schemaNode;
if (value.type === 'Identifier') {
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 (!['ArrayExpression', 'ObjectExpression'].includes(value.type)) {
context.report({ node: value, messageId: 'wrongType' });
}
},
};
},
};