forked from eslint-community/eslint-plugin-eslint-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequire-meta-type.js
76 lines (65 loc) · 2.24 KB
/
require-meta-type.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
/**
* @fileoverview require rules to implement a `meta.type` property
* @author 薛定谔的猫<[email protected]>
*/
'use strict';
const { getStaticValue } = require('eslint-utils');
const utils = require('../utils');
const VALID_TYPES = new Set(['problem', 'suggestion', 'layout']);
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'require rules to implement a `meta.type` property',
category: 'Rules',
recommended: true,
url: 'https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/tree/HEAD/docs/rules/require-meta-type.md',
},
fixable: null,
schema: [],
messages: {
missing:
'`meta.type` is required (must be either `problem`, `suggestion`, or `layout`).',
unexpected:
'`meta.type` must be either `problem`, `suggestion`, or `layout`.',
},
},
create(context) {
// ----------------------------------------------------------------------
// Public
// ----------------------------------------------------------------------
return {
Program() {
const sourceCode = context.getSourceCode();
const { scopeManager } = sourceCode;
const info = utils.getRuleInfo(sourceCode);
if (info === null) {
return;
}
const metaNode = info.meta;
const typeNode = utils
.evaluateObjectProperties(metaNode, scopeManager)
.find((p) => p.type === 'Property' && utils.getKeyName(p) === 'type');
if (!typeNode) {
context.report({
node: metaNode || info.create,
messageId: 'missing',
});
return;
}
const staticValue = getStaticValue(typeNode.value, context.getScope());
if (!staticValue) {
// Ignore non-static values since we can't determine what they look like.
return;
}
if (!VALID_TYPES.has(staticValue.value)) {
context.report({ node: typeNode.value, messageId: 'unexpected' });
}
},
};
},
};