forked from jsx-eslint/eslint-plugin-react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforbid-component-props.js
69 lines (57 loc) · 1.55 KB
/
forbid-component-props.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
/**
* @fileoverview Forbid certain props on components
* @author Joe Lencioni
*/
'use strict';
// ------------------------------------------------------------------------------
// Constants
// ------------------------------------------------------------------------------
var DEFAULTS = ['className', 'style'];
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
module.exports = {
meta: {
docs: {
description: 'Forbid certain props on components',
category: 'Best Practices',
recommended: false
},
schema: [{
type: 'object',
properties: {
forbid: {
type: 'array',
items: {
type: 'string'
}
}
},
additionalProperties: true
}]
},
create: function(context) {
function isForbidden(prop) {
var configuration = context.options[0] || {};
var forbid = configuration.forbid || DEFAULTS;
return forbid.indexOf(prop) >= 0;
}
return {
JSXAttribute: function(node) {
var tag = node.parent.name.name;
if (tag && tag[0] !== tag[0].toUpperCase()) {
// This is a DOM node, not a Component, so exit.
return;
}
var prop = node.name.name;
if (!isForbidden(prop)) {
return;
}
context.report({
node: node,
message: `Prop \`${prop}\` is forbidden on Components`
});
}
};
}
};