-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
jsx-props-no-spreading.js
132 lines (121 loc) · 3.75 KB
/
jsx-props-no-spreading.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**
* @fileoverview Prevent JSX prop spreading
* @author Ashish Gambhir
*/
'use strict';
const docsUrl = require('../util/docsUrl');
// ------------------------------------------------------------------------------
// Constants
// ------------------------------------------------------------------------------
const OPTIONS = {ignore: 'ignore', enforce: 'enforce'};
const DEFAULTS = {
html: OPTIONS.enforce,
custom: OPTIONS.enforce,
explicitSpread: OPTIONS.enforce,
exceptions: []
};
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
module.exports = {
meta: {
docs: {
description: 'Prevent JSX prop spreading',
category: 'Best Practices',
recommended: false,
url: docsUrl('jsx-props-no-spreading')
},
messages: {
noSpreading: 'Prop spreading is forbidden'
},
schema: [{
allOf: [{
type: 'object',
properties: {
html: {
enum: [OPTIONS.enforce, OPTIONS.ignore]
},
custom: {
enum: [OPTIONS.enforce, OPTIONS.ignore]
},
exceptions: {
type: 'array',
items: {
type: 'string',
uniqueItems: true
}
}
}
}, {
not: {
type: 'object',
required: ['html', 'custom'],
properties: {
html: {
enum: [OPTIONS.ignore]
},
custom: {
enum: [OPTIONS.ignore]
},
exceptions: {
type: 'array',
minItems: 0,
maxItems: 0
}
}
}
}]
}]
},
create(context) {
const configuration = context.options[0] || {};
const ignoreHtmlTags = (configuration.html || DEFAULTS.html) === OPTIONS.ignore;
const ignoreCustomTags = (configuration.custom || DEFAULTS.custom) === OPTIONS.ignore;
const ignoreExplicitSpread = (configuration.explicitSpread || DEFAULTS.explicitSpread) === OPTIONS.ignore;
const exceptions = configuration.exceptions || DEFAULTS.exceptions;
const isException = (tag, allExceptions) => allExceptions.indexOf(tag) !== -1;
const isProperty = (property) => property.type === 'Property';
const getTagNameFromMemberExpression = (node) => `${node.property.parent.object.name}.${node.property.name}`;
return {
JSXSpreadAttribute(node) {
const jsxOpeningElement = node.parent.name;
const type = jsxOpeningElement.type;
let tagName;
if (type === 'JSXIdentifier') {
tagName = jsxOpeningElement.name;
} else if (type === 'JSXMemberExpression') {
tagName = getTagNameFromMemberExpression(jsxOpeningElement);
} else {
tagName = undefined;
}
const isHTMLTag = tagName && tagName[0] !== tagName[0].toUpperCase();
const isCustomTag = tagName && (tagName[0] === tagName[0].toUpperCase() || tagName.includes('.'));
if (
isHTMLTag
&& ((ignoreHtmlTags && !isException(tagName, exceptions))
|| (!ignoreHtmlTags && isException(tagName, exceptions)))
) {
return;
}
if (
isCustomTag
&& ((ignoreCustomTags && !isException(tagName, exceptions))
|| (!ignoreCustomTags && isException(tagName, exceptions)))
) {
return;
}
if (
ignoreExplicitSpread
&& node.argument.type === 'ObjectExpression'
&& node.argument.properties.every(isProperty)
) {
return;
}
context.report({
node,
messageId: 'noSpreading'
});
}
};
}
};