Skip to content

Commit cd5ef2b

Browse files
beefancohenyannickcr
authored andcommitted
Integrate jsx-ast-utils
1 parent 7586915 commit cd5ef2b

File tree

5 files changed

+27
-34
lines changed

5 files changed

+27
-34
lines changed

lib/rules/jsx-key.js

+4-11
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,17 @@
55
'use strict';
66

77
// var Components = require('../util/Components');
8+
var hasProp = require('jsx-ast-utils/hasProp');
9+
810

911
// ------------------------------------------------------------------------------
1012
// Rule Definition
1113
// ------------------------------------------------------------------------------
1214

1315
module.exports = function(context) {
1416

15-
function hasKeyProp(node) {
16-
return node.openingElement.attributes.some(function(decl) {
17-
if (decl.type === 'JSXSpreadAttribute') {
18-
return false;
19-
}
20-
return (decl.name.name === 'key');
21-
});
22-
}
23-
2417
function checkIteratorElement(node) {
25-
if (node.type === 'JSXElement' && !hasKeyProp(node)) {
18+
if (node.type === 'JSXElement' && !hasProp(node.openingElement.attributes, 'key')) {
2619
context.report({
2720
node: node,
2821
message: 'Missing "key" prop for element in iterator'
@@ -38,7 +31,7 @@ module.exports = function(context) {
3831

3932
return {
4033
JSXElement: function(node) {
41-
if (hasKeyProp(node)) {
34+
if (hasProp(node.openingElement.attributes, 'key')) {
4235
return;
4336
}
4437

lib/rules/jsx-no-bind.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
'use strict';
77

8+
var propName = require('jsx-ast-utils/propName');
9+
810
// -----------------------------------------------------------------------------
911
// Rule Definition
1012
// -----------------------------------------------------------------------------
@@ -39,7 +41,7 @@ module.exports = function(context) {
3941
},
4042

4143
JSXAttribute: function(node) {
42-
var isRef = configuration.ignoreRefs && node.name.name === 'ref';
44+
var isRef = configuration.ignoreRefs && propName(node) === 'ref';
4345
if (isRef || !node.value || !node.value.expression) {
4446
return;
4547
}

lib/rules/jsx-pascal-case.js

+12-17
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
'use strict';
77

8+
var elementType = require('jsx-ast-utils/elementType');
9+
810
// ------------------------------------------------------------------------------
911
// Constants
1012
// ------------------------------------------------------------------------------
@@ -25,29 +27,22 @@ module.exports = function(context) {
2527

2628
return {
2729
JSXOpeningElement: function(node) {
28-
switch (node.name.type) {
29-
case 'JSXIdentifier':
30-
node = node.name;
31-
break;
32-
case 'JSXMemberExpression':
33-
node = node.name.object;
34-
break;
35-
case 'JSXNamespacedName':
36-
node = node.name.namespace;
37-
break;
38-
default:
39-
break;
30+
var name = elementType(node);
31+
32+
// Get namespace if the type is JSXNamespacedName.
33+
if (name.indexOf(':') > -1) {
34+
name = name.substring(0, name.indexOf(':'));
4035
}
4136

42-
var isPascalCase = PASCAL_CASE_REGEX.test(node.name);
43-
var isCompatTag = COMPAT_TAG_REGEX.test(node.name);
44-
var isAllowedAllCaps = allowAllCaps && ALL_CAPS_TAG_REGEX.test(node.name);
45-
var isIgnored = ignore.indexOf(node.name) !== -1;
37+
var isPascalCase = PASCAL_CASE_REGEX.test(name);
38+
var isCompatTag = COMPAT_TAG_REGEX.test(name);
39+
var isAllowedAllCaps = allowAllCaps && ALL_CAPS_TAG_REGEX.test(name);
40+
var isIgnored = ignore.indexOf(name) !== -1;
4641

4742
if (!isPascalCase && !isCompatTag && !isAllowedAllCaps && !isIgnored) {
4843
context.report({
4944
node: node,
50-
message: 'Imported JSX component ' + node.name + ' must be in PascalCase'
45+
message: 'Imported JSX component ' + name + ' must be in PascalCase'
5146
});
5247
}
5348
}

lib/rules/jsx-sort-props.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
*/
55
'use strict';
66

7+
var propName = require('jsx-ast-utils/propName');
8+
79
// ------------------------------------------------------------------------------
810
// Rule Definition
911
// ------------------------------------------------------------------------------
1012

11-
function isCallbackPropName(propName) {
12-
return /^on[A-Z]/.test(propName);
13+
function isCallbackPropName(name) {
14+
return /^on[A-Z]/.test(name);
1315
}
1416

1517
module.exports = function(context) {
@@ -26,8 +28,8 @@ module.exports = function(context) {
2628
return attrs[idx + 1];
2729
}
2830

29-
var previousPropName = memo.name.name;
30-
var currentPropName = decl.name.name;
31+
var previousPropName = propName(memo);
32+
var currentPropName = propName(decl);
3133
var previousValue = memo.value;
3234
var currentValue = decl.value;
3335
var previousIsCallback = isCallbackPropName(previousPropName);

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
"homepage": "https://github.com/yannickcr/eslint-plugin-react",
2424
"bugs": "https://github.com/yannickcr/eslint-plugin-react/issues",
2525
"dependencies": {
26-
"doctrine": "^1.2.0"
26+
"doctrine": "^1.2.0",
27+
"jsx-ast-utils": "^1.1.1"
2728
},
2829
"devDependencies": {
2930
"babel-eslint": "6.0.4",

0 commit comments

Comments
 (0)