Skip to content

Commit

Permalink
Fixes #37639 - Use own lint instead of @theforeman
Browse files Browse the repository at this point in the history
  • Loading branch information
MariaAga committed Sep 13, 2024
1 parent 185f3b3 commit c12831e
Show file tree
Hide file tree
Showing 11 changed files with 465 additions and 208 deletions.
201 changes: 0 additions & 201 deletions .eslintrc

This file was deleted.

21 changes: 21 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const lintCoreConfig = require('./script/lint/lint_core_config.js');
const lintGenericConfig = require('./script/lint/lint_generic_config.js');

const combinedConfig = {
...lintCoreConfig,
...lintGenericConfig,
rules: {
...lintCoreConfig.rules,
...lintGenericConfig.rules,
},
plugins: [
...(lintCoreConfig.plugins || []),
...(lintGenericConfig.plugins || []),
],
extends: [
...(lintCoreConfig.extends || []),
...(lintGenericConfig.extends || []),
],
};

module.exports = combinedConfig;
2 changes: 0 additions & 2 deletions .github/workflows/js_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ jobs:
run: npm ci --no-audit
- name: Run linter
run: npm run lint
- name: Run custom eslint rules Spellcheck (only warnings) and missing ouia-ids
run: npm run lint:custom
- name: Run tests
run: npm run test
- name: Publish Coveralls
Expand Down
11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"node": ">=14.0.0 <21.0.0"
},
"scripts": {
"lint": "tfm-lint",
"lint:custom": "eslint ./webpack",
"lint": "eslint ./webpack ./script",
"prelint": "node ./script/lint/link-eslint-plugin.js",
"foreman-js:link": "./script/npm_link_foreman_js.sh",
"postlint": "./script/npm_lint_plugins.js",
"test": "npx jest --setupFilesAfterEnv ./global_test_setup.js ./core_test_setup.js --testPathIgnorePatterns '/node_modules/' '<rootDir>/.+fixtures.+' --config ./webpack/jest.config.js",
Expand All @@ -36,8 +36,6 @@
"@testing-library/react": "^10.0.2",
"@testing-library/react-hooks": "^3.4.2",
"@theforeman/builder": "^13.1.0",
"@theforeman/eslint-plugin-foreman": "^13.1.0",
"@theforeman/eslint-plugin-rules": "^13.1.0",
"@theforeman/vendor-core": "^13.1.0",
"@theforeman/vendor-dev": "^13.1.0",
"@types/jest": "<27.0.0",
Expand All @@ -57,12 +55,17 @@
"enzyme-to-json": "^3.4.3",
"eslint": "^6.7.2",
"eslint-plugin-spellcheck": "0.0.17",
"eslint-plugin-patternfly-react": "0.2.0",
"eslint-plugin-jquery": "^1.5.1",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-react-hooks": "^2.1.1",
"graphql": "^15.5.0",
"identity-obj-proxy": "^3.0.0",
"jest": "^26.4.0",
"jest-prop-type-error": "^1.1.0",
"jest-svg-transformer": "^1.0.0",
"jest-transform-graphql": "^2.1.0",
"jsx-ast-utils": "^3.3.3",
"path-browserify": "^1.0.1",
"prettier": "^1.19.1",
"pretty-format": "26.6.2",
Expand Down
7 changes: 7 additions & 0 deletions script/lint/@foreman/eslint-plugin-custom/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const requireOuiaidRule = require('./require-ouiaid');

module.exports = {
rules: {
'require-ouiaid': requireOuiaidRule,
},
};
84 changes: 84 additions & 0 deletions script/lint/@foreman/eslint-plugin-custom/require-ouiaid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
const getProp = require('jsx-ast-utils/getProp');

module.exports = {
create(context) {
const patternflyImports = new Set();
const options = context.options.length
? context.options
: [
'Alert',
'Breadcrumb',
'Button',
'Card',
'Checkbox',
'Chip',
'ChipGroup',
'ContextSelector',
'Dropdown',
'DropdownItem',
'DropdownSeparator',
'DropdownToggle',
'DropdownToggleCheckbox',
'FormSelect',
'Menu',
'Modal',
'ModalBoxCloseButton',
'ModalContent',
'Nav',
'NavExpandable',
'NavItem',
'OptionsMenu',
'Pagination',
'Radio',
'RowWrapper',
'Select',
'Switch',
'TabButton',
'TabContent',
'Tab',
'Tabs',
'Text',
'TextInput',
'Title',
'Toolbar',
'Table',
'TableComposable',
'Tr',
];

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);
}
});
}
}

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 in PatternFly component '${node.name.name}'`,
});
}
}
}
return {
ImportDeclaration: addPatternflyImport,
JSXOpeningElement: checkPatternflyComponent,
};
},
};
33 changes: 33 additions & 0 deletions script/lint/link-eslint-plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* eslint-disable no-console */
const fs = require('fs');
const path = require('path');

function linkEslintPlugin(runPath = process.cwd()) {
// instead of creating an npm package for the custom eslint plugin, we symlink it
// eslint will only search for plugins in node_modules, so we need to symlink it there
const sourceDir = path.join(__dirname, '@foreman');
const destinationDir = path.join(runPath, 'node_modules', '@foreman');
function createSymlink() {
fs.symlink(sourceDir, destinationDir, 'dir', err => {
if (err) {
console.error('Error creating symlink:', err);
}
});
}

// Check if the symlink exists and remove it if it does
fs.lstat(destinationDir, (err, stats) => {
if (!err && stats.isSymbolicLink()) {
fs.unlink(destinationDir, unlinkErr => {
if (unlinkErr) {
console.error('Error removing existing symlink:', unlinkErr);
return;
}
createSymlink();
});
} else {
createSymlink();
}
});
}
linkEslintPlugin();
Loading

0 comments on commit c12831e

Please sign in to comment.