Skip to content

Commit

Permalink
feat(eslint-plugin-rules): checking for ouiaId in pf4 components only (
Browse files Browse the repository at this point in the history
…#470)

* feat(eslint-plugin-rules): checking for ouiaId in pf4 components only

* feat(eslint-plugin-rules): check ouia-id prop in Tab component
  • Loading branch information
MariaAga committed Jun 28, 2023
1 parent 81b8341 commit afe1c7f
Showing 1 changed file with 32 additions and 8 deletions.
40 changes: 32 additions & 8 deletions packages/eslint-plugin-rules/lib/require-ouiaid.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const getProp = require('jsx-ast-utils/getProp');

module.exports = {
create(context) {
const patternflyImports = new Set();
const options = context.options.length
? context.options
: [
Expand Down Expand Up @@ -34,6 +35,7 @@ module.exports = {
'Switch',
'TabButton',
'TabContent',
'Tab',
'Tabs',
'Text',
'TextInput',
Expand All @@ -44,17 +46,39 @@ module.exports = {
'Tr',
];

return {
JSXElement(node) {
if (!options.includes(node.openingElement.name.name)) {
return;
}
function addPatternflyImport(node) {
if (
node.type === 'ImportDeclaration' &&
node.source.value.startsWith('@patternfly/react')
) {
node.specifiers.forEach((specifier) => {
if (specifier.type === 'ImportSpecifier') {
patternflyImports.add(specifier.local.name);
}
});
}
}

const ouiaIdProp = getProp(node.openingElement.attributes, 'ouiaId');
function checkPatternflyComponent(node) {
if (!options.includes(node.name.name)) {
return;
}
if (
node.type === 'JSXOpeningElement' &&
patternflyImports.has(node.name.name)
) {
const ouiaIdProp = getProp(node.attributes, 'ouiaId');
if (!ouiaIdProp) {
context.report({ node, message: 'ouiaId property is missing' });
context.report({
node,
message: `ouiaId property is missing in PatternFly component '${node.name.name}'`,
});
}
},
}
}
return {
ImportDeclaration: addPatternflyImport,
JSXOpeningElement: checkPatternflyComponent,
};
},
};

0 comments on commit afe1c7f

Please sign in to comment.