Skip to content

Commit 7121cbd

Browse files
committed
Switch to new eslint rule format (Fixes jsx-eslint#661)
1 parent 419bd43 commit 7121cbd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+4588
-4296
lines changed

lib/rules/display-name.js

+198-192
Large diffs are not rendered by default.

lib/rules/forbid-prop-types.js

+108-102
Original file line numberDiff line numberDiff line change
@@ -13,122 +13,128 @@ var DEFAULTS = ['any', 'array', 'object'];
1313
// Rule Definition
1414
// ------------------------------------------------------------------------------
1515

16-
module.exports = function(context) {
16+
module.exports = {
17+
meta: {
18+
docs: {},
19+
20+
schema: [{
21+
type: 'object',
22+
properties: {
23+
forbid: {
24+
type: 'array',
25+
items: {
26+
type: 'string'
27+
}
28+
}
29+
},
30+
additionalProperties: true
31+
}]
32+
},
1733

18-
function isForbidden(type) {
19-
var configuration = context.options[0] || {};
34+
create: function(context) {
2035

21-
var forbid = configuration.forbid || DEFAULTS;
22-
return forbid.indexOf(type) >= 0;
23-
}
36+
function isForbidden(type) {
37+
var configuration = context.options[0] || {};
2438

25-
/**
26-
* Checks if node is `propTypes` declaration
27-
* @param {ASTNode} node The AST node being checked.
28-
* @returns {Boolean} True if node is `propTypes` declaration, false if not.
29-
*/
30-
function isPropTypesDeclaration(node) {
31-
32-
// Special case for class properties
33-
// (babel-eslint does not expose property name so we have to rely on tokens)
34-
if (node.type === 'ClassProperty') {
35-
var tokens = context.getFirstTokens(node, 2);
36-
if (tokens[0].value === 'propTypes' || (tokens[1] && tokens[1].value === 'propTypes')) {
37-
return true;
38-
}
39-
return false;
39+
var forbid = configuration.forbid || DEFAULTS;
40+
return forbid.indexOf(type) >= 0;
4041
}
4142

42-
return Boolean(
43-
node &&
44-
node.name === 'propTypes'
45-
);
46-
}
47-
48-
49-
/**
50-
* Checks if propTypes declarations are forbidden
51-
* @param {Array} declarations The array of AST nodes being checked.
52-
* @returns {void}
53-
*/
54-
function checkForbidden(declarations) {
55-
declarations.forEach(function(declaration) {
56-
if (declaration.type !== 'Property') {
57-
return;
58-
}
59-
var target;
60-
var value = declaration.value;
61-
if (
62-
value.type === 'MemberExpression' &&
63-
value.property &&
64-
value.property.name &&
65-
value.property.name === 'isRequired'
66-
) {
67-
value = value.object;
68-
}
69-
if (
70-
value.type === 'CallExpression' &&
71-
value.callee.type === 'MemberExpression'
72-
) {
73-
value = value.callee;
74-
}
75-
if (value.property) {
76-
target = value.property.name;
77-
} else if (value.type === 'Identifier') {
78-
target = value.name;
79-
}
80-
if (isForbidden(target)) {
81-
context.report({
82-
node: declaration,
83-
message: 'Prop type `' + target + '` is forbidden'
84-
});
43+
/**
44+
* Checks if node is `propTypes` declaration
45+
* @param {ASTNode} node The AST node being checked.
46+
* @returns {Boolean} True if node is `propTypes` declaration, false if not.
47+
*/
48+
function isPropTypesDeclaration(node) {
49+
50+
// Special case for class properties
51+
// (babel-eslint does not expose property name so we have to rely on tokens)
52+
if (node.type === 'ClassProperty') {
53+
var tokens = context.getFirstTokens(node, 2);
54+
if (tokens[0].value === 'propTypes' || (tokens[1] && tokens[1].value === 'propTypes')) {
55+
return true;
56+
}
57+
return false;
8558
}
86-
});
87-
}
8859

89-
return {
90-
ClassProperty: function(node) {
91-
if (isPropTypesDeclaration(node) && node.value && node.value.type === 'ObjectExpression') {
92-
checkForbidden(node.value.properties);
93-
}
94-
},
60+
return Boolean(
61+
node &&
62+
node.name === 'propTypes'
63+
);
64+
}
9565

96-
MemberExpression: function(node) {
97-
if (isPropTypesDeclaration(node.property)) {
98-
var right = node.parent.right;
99-
if (right && right.type === 'ObjectExpression') {
100-
checkForbidden(right.properties);
101-
}
102-
}
103-
},
10466

105-
ObjectExpression: function(node) {
106-
node.properties.forEach(function(property) {
107-
if (!property.key) {
67+
/**
68+
* Checks if propTypes declarations are forbidden
69+
* @param {Array} declarations The array of AST nodes being checked.
70+
* @returns {void}
71+
*/
72+
function checkForbidden(declarations) {
73+
declarations.forEach(function(declaration) {
74+
if (declaration.type !== 'Property') {
10875
return;
10976
}
110-
111-
if (!isPropTypesDeclaration(property.key)) {
112-
return;
77+
var target;
78+
var value = declaration.value;
79+
if (
80+
value.type === 'MemberExpression' &&
81+
value.property &&
82+
value.property.name &&
83+
value.property.name === 'isRequired'
84+
) {
85+
value = value.object;
11386
}
114-
if (property.value.type === 'ObjectExpression') {
115-
checkForbidden(property.value.properties);
87+
if (
88+
value.type === 'CallExpression' &&
89+
value.callee.type === 'MemberExpression'
90+
) {
91+
value = value.callee;
92+
}
93+
if (value.property) {
94+
target = value.property.name;
95+
} else if (value.type === 'Identifier') {
96+
target = value.name;
97+
}
98+
if (isForbidden(target)) {
99+
context.report({
100+
node: declaration,
101+
message: 'Prop type `' + target + '` is forbidden'
102+
});
116103
}
117104
});
118105
}
119106

120-
};
121-
};
122-
123-
module.exports.schema = [{
124-
type: 'object',
125-
properties: {
126-
forbid: {
127-
type: 'array',
128-
items: {
129-
type: 'string'
107+
return {
108+
ClassProperty: function(node) {
109+
if (isPropTypesDeclaration(node) && node.value && node.value.type === 'ObjectExpression') {
110+
checkForbidden(node.value.properties);
111+
}
112+
},
113+
114+
MemberExpression: function(node) {
115+
if (isPropTypesDeclaration(node.property)) {
116+
var right = node.parent.right;
117+
if (right && right.type === 'ObjectExpression') {
118+
checkForbidden(right.properties);
119+
}
120+
}
121+
},
122+
123+
ObjectExpression: function(node) {
124+
node.properties.forEach(function(property) {
125+
if (!property.key) {
126+
return;
127+
}
128+
129+
if (!isPropTypesDeclaration(property.key)) {
130+
return;
131+
}
132+
if (property.value.type === 'ObjectExpression') {
133+
checkForbidden(property.value.properties);
134+
}
135+
});
130136
}
131-
}
132-
},
133-
additionalProperties: true
134-
}];
137+
138+
};
139+
}
140+
};

lib/rules/jsx-boolean-value.js

+44-37
Original file line numberDiff line numberDiff line change
@@ -8,45 +8,52 @@
88
// Rule Definition
99
// ------------------------------------------------------------------------------
1010

11-
module.exports = function(context) {
11+
module.exports = {
12+
meta: {
13+
docs: {},
14+
fixable: "code",
1215

13-
var configuration = context.options[0] || 'never';
16+
schema: [{
17+
enum: ['always', 'never']
18+
}]
19+
},
1420

15-
var NEVER_MESSAGE = 'Value must be omitted for boolean attributes';
16-
var ALWAYS_MESSAGE = 'Value must be set for boolean attributes';
21+
create: function(context) {
1722

18-
return {
19-
JSXAttribute: function(node) {
20-
switch (configuration) {
21-
case 'always':
22-
if (node.value === null) {
23-
context.report({
24-
node: node,
25-
message: ALWAYS_MESSAGE,
26-
fix: function(fixer) {
27-
return fixer.insertTextAfter(node, '={true}');
28-
}
29-
});
30-
}
31-
break;
32-
case 'never':
33-
if (node.value && node.value.type === 'JSXExpressionContainer' && node.value.expression.value === true) {
34-
context.report({
35-
node: node,
36-
message: NEVER_MESSAGE,
37-
fix: function(fixer) {
38-
return fixer.removeRange([node.name.range[1], node.value.range[1]]);
39-
}
40-
});
41-
}
42-
break;
43-
default:
44-
break;
23+
var configuration = context.options[0] || 'never';
24+
25+
var NEVER_MESSAGE = 'Value must be omitted for boolean attributes';
26+
var ALWAYS_MESSAGE = 'Value must be set for boolean attributes';
27+
28+
return {
29+
JSXAttribute: function(node) {
30+
switch (configuration) {
31+
case 'always':
32+
if (node.value === null) {
33+
context.report({
34+
node: node,
35+
message: ALWAYS_MESSAGE,
36+
fix: function(fixer) {
37+
return fixer.insertTextAfter(node, '={true}');
38+
}
39+
});
40+
}
41+
break;
42+
case 'never':
43+
if (node.value && node.value.type === 'JSXExpressionContainer' && node.value.expression.value === true) {
44+
context.report({
45+
node: node,
46+
message: NEVER_MESSAGE,
47+
fix: function(fixer) {
48+
return fixer.removeRange([node.name.range[1], node.value.range[1]]);
49+
}
50+
});
51+
}
52+
break;
53+
default:
54+
break;
55+
}
4556
}
46-
}
47-
};
57+
};
58+
}
4859
};
49-
50-
module.exports.schema = [{
51-
enum: ['always', 'never']
52-
}];

0 commit comments

Comments
 (0)