forked from eslint-community/eslint-plugin-eslint-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequire-meta-fixable.js
93 lines (82 loc) · 3.2 KB
/
require-meta-fixable.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
/**
* @fileoverview require rules to implement a meta.fixable property
* @author Teddy Katz
*/
'use strict';
const { getStaticValue } = require('eslint-utils');
const utils = require('../utils');
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'require rules to implement a meta.fixable property',
category: 'Rules',
recommended: true,
},
schema: [],
messages: {
invalid: '`meta.fixable` must be either `code`, `whitespace` or `null`.',
missing: 'Fixable rules must export a `meta.fixable` property.',
},
},
create (context) {
const sourceCode = context.getSourceCode();
const ruleInfo = utils.getRuleInfo(sourceCode);
let contextIdentifiers;
let usesFixFunctions;
// ----------------------------------------------------------------------
// Helpers
// ----------------------------------------------------------------------
// ----------------------------------------------------------------------
// Public
// ----------------------------------------------------------------------
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' &&
node.arguments[0].properties.some(prop => utils.getKeyName(prop) === 'fix')
))
) {
usesFixFunctions = true;
}
},
'Program:exit' () {
const metaFixableProp = ruleInfo &&
ruleInfo.meta &&
ruleInfo.meta.type === 'ObjectExpression' &&
ruleInfo.meta.properties.find(prop => utils.getKeyName(prop) === 'fixable');
if (metaFixableProp) {
const staticValue = getStaticValue(metaFixableProp.value, context.getScope());
if (!staticValue) {
// Ignore non-static values since we can't determine what they look like.
return;
}
if (!['code', 'whitespace', null, undefined].includes(staticValue.value)) {
// `fixable` property has an invalid value.
context.report({ node: metaFixableProp.value, messageId: 'invalid' });
return;
}
if (usesFixFunctions && !['code', 'whitespace'].includes(staticValue.value)) {
// Rule is fixable but `fixable` property does not have a fixable value.
context.report({ node: metaFixableProp.value, messageId: 'missing' });
}
} else if (!metaFixableProp && usesFixFunctions) {
// Rule is fixable but is missing the `fixable` property.
context.report({ node: ruleInfo.meta || ruleInfo.create, messageId: 'missing' });
}
},
};
},
};