Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ESLint rule to detect components contains prop href but not rel #3272

Merged
merged 17 commits into from
Apr 10, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintplugin.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
exports.rules = {
i18n: require('./scripts/eslint-plugin-i18n/i18n'),
'href-with-rel': require('./scripts/eslint-plugin-rel/rel')
};
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ module.exports = {
rules: {
"prefer-template": "error",
"local/i18n": "error",
"local/href-with-rel": "error",
"no-use-before-define": "off",
"quotes": ["warn", "single", "avoid-escape"],

Expand Down
41 changes: 41 additions & 0 deletions scripts/eslint-plugin-rel/rel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'Enforce rel prop if href exists',
},
},
create: function(context) {
return {
/**
* Props of any component is defined in ArrowFunctions
* Example: const EuiButton = ({ foo, bar }) => {};
*/
ArrowFunctionExpression(node) {
// Functional component contains only single argument
if (node.params && node.params.length === 1) {
// Extract object => { foo, bar }
const objectPattern = node.params[0];

if (objectPattern.properties && objectPattern.properties.length) {
// Iterate each Object property to find href or rel
let href = -1;
let rel = -1;
objectPattern.properties.forEach((property, index) => {
if (property.key && property.key.name === 'href') href = index;
if (property.key && property.key.name === 'rel') rel = index;
});

// Error => If href is preset and rel is not preset
if (href !== -1 && rel === -1) {
context.report({
node: objectPattern.properties[href],
message: 'Props must contain rel if href is defined',
});
}
}
}
},
};
},
};
27 changes: 27 additions & 0 deletions scripts/eslint-plugin-rel/rel.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const rule = require('./rel');
const RuleTester = require('eslint').RuleTester;

const ruleTester = new RuleTester({
parser: 'babel-eslint',
});

const valid = [
'const Component = ({ href, rel }) => {};',
'const Component = ({ foo, bar, baz}) => {};',
];

const invalid = [
{
code: 'const Component = ({ href }) => {};',
errors: [
{
message: 'Props must contain rel if href is defined',
},
],
},
];

ruleTester.run('href-with-rel', rule, {
valid,
invalid,
});