no-unexpected-plugin-keys.js
1.88 KB
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
'use strict';
const path = require('path');
const utils = require('../utils');
const VALID_KEYS = [ 'enable', 'package', 'path', 'env' ];
module.exports = {
meta: {
docs: {
description: 'Disallow unexpected plugin keys in config/plugin.*.js',
category: 'Possible Errors',
recommended: true,
url: 'https://github.com/eggjs/eslint-plugin-eggache#no-unexpected-plugin-keys',
},
messages: {
unexpectedKey: 'Unexpected key: {{ key }}',
},
},
create(context) {
// only `config/plugin.*.js`
if (!isPlugin(context)) return {};
return {
ExpressionStatement(node) {
// only consider the root scope
if (!node.parent || node.parent.type !== 'Program') return;
if (node.expression.type !== 'AssignmentExpression') return;
const { left, right } = node.expression;
// only conside object, cause `exports.view = false` always valid.
if (utils.isExports(left) && right.type === 'ObjectExpression') {
checkNode(context, right);
}
if (utils.isModule(left) && right.type === 'ObjectExpression') {
for (const item of right.properties) {
if (item.value.type === 'ObjectExpression') {
checkNode(context, item.value);
}
}
}
},
};
},
};
function checkNode(context, node) {
for (const testNode of node.properties) {
if (!VALID_KEYS.includes(testNode.key.name)) {
context.report({
node: testNode,
messageId: 'unexpectedKey',
data: {
key: testNode.key.name,
},
});
}
}
}
function isPlugin(context) {
const filePath = context.getFilename();
if (filePath === '<input>') return true;
const baseName = path.basename(filePath);
const dirname = path.basename(path.dirname(filePath));
return dirname === 'config' && baseName.startsWith('plugin.');
}