Skip to content

Commit

Permalink
Integrate jsx-ast-utils
Browse files Browse the repository at this point in the history
  • Loading branch information
beefancohen authored and yannickcr committed Jun 16, 2016
1 parent 7586915 commit cd5ef2b
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 34 deletions.
15 changes: 4 additions & 11 deletions lib/rules/jsx-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,17 @@
'use strict';

// var Components = require('../util/Components');
var hasProp = require('jsx-ast-utils/hasProp');


// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------

module.exports = function(context) {

function hasKeyProp(node) {
return node.openingElement.attributes.some(function(decl) {
if (decl.type === 'JSXSpreadAttribute') {
return false;
}
return (decl.name.name === 'key');
});
}

function checkIteratorElement(node) {
if (node.type === 'JSXElement' && !hasKeyProp(node)) {
if (node.type === 'JSXElement' && !hasProp(node.openingElement.attributes, 'key')) {
context.report({
node: node,
message: 'Missing "key" prop for element in iterator'
Expand All @@ -38,7 +31,7 @@ module.exports = function(context) {

return {
JSXElement: function(node) {
if (hasKeyProp(node)) {
if (hasProp(node.openingElement.attributes, 'key')) {
return;
}

Expand Down
4 changes: 3 additions & 1 deletion lib/rules/jsx-no-bind.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
'use strict';

var propName = require('jsx-ast-utils/propName');

// -----------------------------------------------------------------------------
// Rule Definition
// -----------------------------------------------------------------------------
Expand Down Expand Up @@ -39,7 +41,7 @@ module.exports = function(context) {
},

JSXAttribute: function(node) {
var isRef = configuration.ignoreRefs && node.name.name === 'ref';
var isRef = configuration.ignoreRefs && propName(node) === 'ref';
if (isRef || !node.value || !node.value.expression) {
return;
}
Expand Down
29 changes: 12 additions & 17 deletions lib/rules/jsx-pascal-case.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

'use strict';

var elementType = require('jsx-ast-utils/elementType');

// ------------------------------------------------------------------------------
// Constants
// ------------------------------------------------------------------------------
Expand All @@ -25,29 +27,22 @@ module.exports = function(context) {

return {
JSXOpeningElement: function(node) {
switch (node.name.type) {
case 'JSXIdentifier':
node = node.name;
break;
case 'JSXMemberExpression':
node = node.name.object;
break;
case 'JSXNamespacedName':
node = node.name.namespace;
break;
default:
break;
var name = elementType(node);

// Get namespace if the type is JSXNamespacedName.
if (name.indexOf(':') > -1) {
name = name.substring(0, name.indexOf(':'));
}

var isPascalCase = PASCAL_CASE_REGEX.test(node.name);
var isCompatTag = COMPAT_TAG_REGEX.test(node.name);
var isAllowedAllCaps = allowAllCaps && ALL_CAPS_TAG_REGEX.test(node.name);
var isIgnored = ignore.indexOf(node.name) !== -1;
var isPascalCase = PASCAL_CASE_REGEX.test(name);
var isCompatTag = COMPAT_TAG_REGEX.test(name);
var isAllowedAllCaps = allowAllCaps && ALL_CAPS_TAG_REGEX.test(name);
var isIgnored = ignore.indexOf(name) !== -1;

if (!isPascalCase && !isCompatTag && !isAllowedAllCaps && !isIgnored) {
context.report({
node: node,
message: 'Imported JSX component ' + node.name + ' must be in PascalCase'
message: 'Imported JSX component ' + name + ' must be in PascalCase'
});
}
}
Expand Down
10 changes: 6 additions & 4 deletions lib/rules/jsx-sort-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
*/
'use strict';

var propName = require('jsx-ast-utils/propName');

// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------

function isCallbackPropName(propName) {
return /^on[A-Z]/.test(propName);
function isCallbackPropName(name) {
return /^on[A-Z]/.test(name);
}

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

var previousPropName = memo.name.name;
var currentPropName = decl.name.name;
var previousPropName = propName(memo);
var currentPropName = propName(decl);
var previousValue = memo.value;
var currentValue = decl.value;
var previousIsCallback = isCallbackPropName(previousPropName);
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"homepage": "https://github.com/yannickcr/eslint-plugin-react",
"bugs": "https://github.com/yannickcr/eslint-plugin-react/issues",
"dependencies": {
"doctrine": "^1.2.0"
"doctrine": "^1.2.0",
"jsx-ast-utils": "^1.1.1"
},
"devDependencies": {
"babel-eslint": "6.0.4",
Expand Down

0 comments on commit cd5ef2b

Please sign in to comment.