Skip to content

Commit

Permalink
Switch to new eslint rule format (fixes #661)
Browse files Browse the repository at this point in the history
  • Loading branch information
petersendidit authored and yannickcr committed Jul 27, 2016
1 parent 54b674a commit 10c6f23
Show file tree
Hide file tree
Showing 50 changed files with 4,588 additions and 4,296 deletions.
390 changes: 198 additions & 192 deletions lib/rules/display-name.js

Large diffs are not rendered by default.

210 changes: 108 additions & 102 deletions lib/rules/forbid-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,122 +13,128 @@ var DEFAULTS = ['any', 'array', 'object'];
// Rule Definition
// ------------------------------------------------------------------------------

module.exports = function(context) {
module.exports = {
meta: {
docs: {},

schema: [{
type: 'object',
properties: {
forbid: {
type: 'array',
items: {
type: 'string'
}
}
},
additionalProperties: true
}]
},

function isForbidden(type) {
var configuration = context.options[0] || {};
create: function(context) {

var forbid = configuration.forbid || DEFAULTS;
return forbid.indexOf(type) >= 0;
}
function isForbidden(type) {
var configuration = context.options[0] || {};

/**
* Checks if node is `propTypes` declaration
* @param {ASTNode} node The AST node being checked.
* @returns {Boolean} True if node is `propTypes` declaration, false if not.
*/
function isPropTypesDeclaration(node) {

// Special case for class properties
// (babel-eslint does not expose property name so we have to rely on tokens)
if (node.type === 'ClassProperty') {
var tokens = context.getFirstTokens(node, 2);
if (tokens[0].value === 'propTypes' || (tokens[1] && tokens[1].value === 'propTypes')) {
return true;
}
return false;
var forbid = configuration.forbid || DEFAULTS;
return forbid.indexOf(type) >= 0;
}

return Boolean(
node &&
node.name === 'propTypes'
);
}


/**
* Checks if propTypes declarations are forbidden
* @param {Array} declarations The array of AST nodes being checked.
* @returns {void}
*/
function checkForbidden(declarations) {
declarations.forEach(function(declaration) {
if (declaration.type !== 'Property') {
return;
}
var target;
var value = declaration.value;
if (
value.type === 'MemberExpression' &&
value.property &&
value.property.name &&
value.property.name === 'isRequired'
) {
value = value.object;
}
if (
value.type === 'CallExpression' &&
value.callee.type === 'MemberExpression'
) {
value = value.callee;
}
if (value.property) {
target = value.property.name;
} else if (value.type === 'Identifier') {
target = value.name;
}
if (isForbidden(target)) {
context.report({
node: declaration,
message: 'Prop type `' + target + '` is forbidden'
});
/**
* Checks if node is `propTypes` declaration
* @param {ASTNode} node The AST node being checked.
* @returns {Boolean} True if node is `propTypes` declaration, false if not.
*/
function isPropTypesDeclaration(node) {

// Special case for class properties
// (babel-eslint does not expose property name so we have to rely on tokens)
if (node.type === 'ClassProperty') {
var tokens = context.getFirstTokens(node, 2);
if (tokens[0].value === 'propTypes' || (tokens[1] && tokens[1].value === 'propTypes')) {
return true;
}
return false;
}
});
}

return {
ClassProperty: function(node) {
if (isPropTypesDeclaration(node) && node.value && node.value.type === 'ObjectExpression') {
checkForbidden(node.value.properties);
}
},
return Boolean(
node &&
node.name === 'propTypes'
);
}

MemberExpression: function(node) {
if (isPropTypesDeclaration(node.property)) {
var right = node.parent.right;
if (right && right.type === 'ObjectExpression') {
checkForbidden(right.properties);
}
}
},

ObjectExpression: function(node) {
node.properties.forEach(function(property) {
if (!property.key) {
/**
* Checks if propTypes declarations are forbidden
* @param {Array} declarations The array of AST nodes being checked.
* @returns {void}
*/
function checkForbidden(declarations) {
declarations.forEach(function(declaration) {
if (declaration.type !== 'Property') {
return;
}

if (!isPropTypesDeclaration(property.key)) {
return;
var target;
var value = declaration.value;
if (
value.type === 'MemberExpression' &&
value.property &&
value.property.name &&
value.property.name === 'isRequired'
) {
value = value.object;
}
if (property.value.type === 'ObjectExpression') {
checkForbidden(property.value.properties);
if (
value.type === 'CallExpression' &&
value.callee.type === 'MemberExpression'
) {
value = value.callee;
}
if (value.property) {
target = value.property.name;
} else if (value.type === 'Identifier') {
target = value.name;
}
if (isForbidden(target)) {
context.report({
node: declaration,
message: 'Prop type `' + target + '` is forbidden'
});
}
});
}

};
};

module.exports.schema = [{
type: 'object',
properties: {
forbid: {
type: 'array',
items: {
type: 'string'
return {
ClassProperty: function(node) {
if (isPropTypesDeclaration(node) && node.value && node.value.type === 'ObjectExpression') {
checkForbidden(node.value.properties);
}
},

MemberExpression: function(node) {
if (isPropTypesDeclaration(node.property)) {
var right = node.parent.right;
if (right && right.type === 'ObjectExpression') {
checkForbidden(right.properties);
}
}
},

ObjectExpression: function(node) {
node.properties.forEach(function(property) {
if (!property.key) {
return;
}

if (!isPropTypesDeclaration(property.key)) {
return;
}
if (property.value.type === 'ObjectExpression') {
checkForbidden(property.value.properties);
}
});
}
}
},
additionalProperties: true
}];

};
}
};
81 changes: 44 additions & 37 deletions lib/rules/jsx-boolean-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,52 @@
// Rule Definition
// ------------------------------------------------------------------------------

module.exports = function(context) {
module.exports = {
meta: {
docs: {},
fixable: 'code',

var configuration = context.options[0] || 'never';
schema: [{
enum: ['always', 'never']
}]
},

var NEVER_MESSAGE = 'Value must be omitted for boolean attributes';
var ALWAYS_MESSAGE = 'Value must be set for boolean attributes';
create: function(context) {

return {
JSXAttribute: function(node) {
switch (configuration) {
case 'always':
if (node.value === null) {
context.report({
node: node,
message: ALWAYS_MESSAGE,
fix: function(fixer) {
return fixer.insertTextAfter(node, '={true}');
}
});
}
break;
case 'never':
if (node.value && node.value.type === 'JSXExpressionContainer' && node.value.expression.value === true) {
context.report({
node: node,
message: NEVER_MESSAGE,
fix: function(fixer) {
return fixer.removeRange([node.name.range[1], node.value.range[1]]);
}
});
}
break;
default:
break;
var configuration = context.options[0] || 'never';

var NEVER_MESSAGE = 'Value must be omitted for boolean attributes';
var ALWAYS_MESSAGE = 'Value must be set for boolean attributes';

return {
JSXAttribute: function(node) {
switch (configuration) {
case 'always':
if (node.value === null) {
context.report({
node: node,
message: ALWAYS_MESSAGE,
fix: function(fixer) {
return fixer.insertTextAfter(node, '={true}');
}
});
}
break;
case 'never':
if (node.value && node.value.type === 'JSXExpressionContainer' && node.value.expression.value === true) {
context.report({
node: node,
message: NEVER_MESSAGE,
fix: function(fixer) {
return fixer.removeRange([node.name.range[1], node.value.range[1]]);
}
});
}
break;
default:
break;
}
}
}
};
};
}
};

module.exports.schema = [{
enum: ['always', 'never']
}];
Loading

0 comments on commit 10c6f23

Please sign in to comment.