Skip to content

Commit

Permalink
feat: Added recommended-react config
Browse files Browse the repository at this point in the history
  • Loading branch information
tomerh2001 committed Sep 11, 2024
1 parent 4cb2915 commit 2238f59
Show file tree
Hide file tree
Showing 11 changed files with 2,528 additions and 289 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ This repository contains custom ESLint rules to enhance code quality and consist
✅ Set in the `recommended` configuration.\
🔧 Automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/user-guide/command-line-interface#--fix).

| Name              | Description | 💼 | 🔧 |
| :--------------------------------------------------- | :------------------------------------------------------------------------------- | :---------------------------------- | :- |
| [no-comments](docs/rules/no-comments.md) | Disallow comments except for specified allowed patterns. |![badge-recommended-typescript][] | 🔧 |
| [no-default-export](docs/rules/no-default-export.md) | Convert unnamed default exports to named default exports based on the file name. |![badge-recommended-typescript][] | 🔧 |
| [no-destructuring](docs/rules/no-destructuring.md) | Disallow destructuring that does not meet certain conditions |![badge-recommended-typescript][] | |
| Name              | Description | 💼 | 🔧 |
| :--------------------------------------------------- | :------------------------------------------------------------------------------- | :--------------------------------------------------------------- | :- |
| [no-comments](docs/rules/no-comments.md) | Disallow comments except for specified allowed patterns. |![badge-recommended-react][] ![badge-recommended-typescript][] | 🔧 |
| [no-default-export](docs/rules/no-default-export.md) | Convert unnamed default exports to named default exports based on the file name. |![badge-recommended-react][] ![badge-recommended-typescript][] | 🔧 |
| [no-destructuring](docs/rules/no-destructuring.md) | Disallow destructuring that does not meet certain conditions |![badge-recommended-react][] ![badge-recommended-typescript][] | |

<!-- end auto-generated rules list -->

Binary file modified bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion docs/rules/no-comments.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Disallow comments except for specified allowed patterns (`th-rules/no-comments`)

💼 This rule is enabled in the following configs: ✅ `recommended`, `recommended-typescript`.
💼 This rule is enabled in the following configs: ✅ `recommended`, `recommended-react`, `recommended-typescript`.

🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).

Expand Down
2 changes: 1 addition & 1 deletion docs/rules/no-default-export.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Convert unnamed default exports to named default exports based on the file name (`th-rules/no-default-export`)

💼 This rule is enabled in the following configs: ✅ `recommended`, `recommended-typescript`.
💼 This rule is enabled in the following configs: ✅ `recommended`, `recommended-react`, `recommended-typescript`.

🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).

Expand Down
2 changes: 1 addition & 1 deletion docs/rules/no-destructuring.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Disallow destructuring that does not meet certain conditions (`th-rules/no-destructuring`)

💼 This rule is enabled in the following configs: ✅ `recommended`, `recommended-typescript`.
💼 This rule is enabled in the following configs: ✅ `recommended`, `recommended-react`, `recommended-typescript`.

<!-- end auto-generated rule header -->

Expand Down
20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,42 +23,42 @@
"eslint-config-xo": "^0.46.0",
"eslint-config-xo-react": "^0.27.0",
"eslint-plugin-jsdoc": "^50.2.2",
"eslint-plugin-react": "^7.35.0",
"eslint-plugin-react": "^7.35.2",
"eslint-plugin-react-hooks": "^4.6.2",
"eslint-plugin-react-native": "^4.1.0",
"eslint-plugin-security": "^3.0.1",
"eslint-plugin-sonarjs": "^1.0.4",
"eslint-plugin-sonarjs": "^2.0.2",
"requireindex": "^1.2.0"
},
"devDependencies": {
"@codedependant/semantic-release-docker": "^5.0.3",
"@eslint/js": "^9.9.0",
"@eslint/js": "^9.10.0",
"@semantic-release/changelog": "^6.0.3",
"@semantic-release/commit-analyzer": "^13.0.0",
"@semantic-release/git": "^10.0.1",
"@semantic-release/github": "^10.1.7",
"@semantic-release/github": "^10.3.3",
"@semantic-release/npm": "^12.0.1",
"@semantic-release/release-notes-generator": "^14.0.1",
"@types/eslint-plugin-security": "^3.0.0",
"@types/eslint__js": "^8.42.3",
"@types/requireindex": "^1.2.4",
"@types/xo": "^0.39.9",
"bun-types": "latest",
"eslint": "^9.9.0",
"eslint": "^9.10.0",
"eslint-doc-generator": "^1.7.1",
"eslint-plugin-eslint-plugin": "^6.2.0",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-sonarjs": "^1.0.4",
"eslint-plugin-sonarjs": "^2.0.2",
"eslint-plugin-unicorn": "^55.0.0",
"mocha": "^10.7.3",
"npm-run-all": "^4.1.5",
"typescript": "^5.5.4",
"typescript-eslint": "^8.2.0",
"typescript": "^5.6.2",
"typescript-eslint": "^8.5.0",
"xo": "^0.59.3"
},
"peerDependencies": {
"eslint": ">=9.9.0",
"typescript": "^5.5.4"
"eslint": ">=9.10.0",
"typescript": "^5.6.2"
},
"license": "ISC",
"packageManager": "yarn@4.4.0"
Expand Down
72 changes: 42 additions & 30 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,37 @@
'use strict';
const requireIndex = require('requireindex');

const configs = {};
configs.recommended = {
plugins: [
'th-rules',
'sonarjs',
],
extends: [
'plugin:sonarjs/recommended-legacy',
'plugin:security/recommended-legacy',
],
rules: {
'th-rules/no-destructuring': 'error',
'th-rules/no-default-export': 'error',
'th-rules/no-comments': 'error',
'unicorn/prefer-module': 'warn',
'unicorn/filename-case': 'off',
'unicorn/no-array-callback-reference': 'off',
'import/extensions': 'off',
'unicorn/no-static-only-class': 'off',
'unicorn/no-await-expression-member': 'off',
'new-cap': 'off',
'no-await-in-loop': 'off',
'n/file-extension-in-import': 'off',
'import/no-cycle': 'off',
camelcase: 'warn',
},
env: {
node: true,
es2024: true,
jest: true,
const configs = {
recommended: {
plugins: [
'th-rules',
'sonarjs',
],
extends: [
'plugin:sonarjs/recommended-legacy',
'plugin:security/recommended-legacy',
],
rules: {
'th-rules/no-destructuring': 'error',
'th-rules/no-default-export': 'error',
'th-rules/no-comments': 'error',
'unicorn/prefer-module': 'warn',
'unicorn/filename-case': 'off',
'unicorn/no-array-callback-reference': 'off',
'import/extensions': 'off',
'unicorn/no-static-only-class': 'off',
'unicorn/no-await-expression-member': 'off',
'new-cap': 'off',
'no-await-in-loop': 'off',
'n/file-extension-in-import': 'off',
'import/no-cycle': 'off',
camelcase: 'warn',
},
env: {
node: true,
es2024: true,
jest: true,
},
},
};

Expand All @@ -59,6 +60,17 @@ for (const configName of Object.keys(configs)) {
'@typescript-eslint/no-unsafe-argument': 'off',
},
};
configs[`${configName}-react`] = {
...configs[configName],
extends: [
'plugin:react/recommended',
'plugin:react-hooks/recommended',
...configs[configName].extends,
],
rules: {
...configs[configName].rules,
},
};
}

module.exports = {
Expand Down
152 changes: 76 additions & 76 deletions src/rules/no-comments.js
Original file line number Diff line number Diff line change
@@ -1,97 +1,97 @@
/* eslint-disable unicorn/prefer-module */

const allowedPatterns = [
/todo/i, // Allow TODO (case-insensitive)
/warning/i, // Allow WARNING (case-insensitive)
/error/i, // Allow ERROR (case-insensitive)
/info/i, // Allow INFO (case-insensitive)
/^\s*eslint-(disable|enable|env|globals|ignore|directive)/, // Allow ESLint directives
/todo/i, // Allow TODO (case-insensitive)
/warning/i, // Allow WARNING (case-insensitive)
/error/i, // Allow ERROR (case-insensitive)
/info/i, // Allow INFO (case-insensitive)
/^\s*eslint-(disable|enable|env|globals|ignore|directive)/, // Allow ESLint directives
];

const meta = {
type: 'problem',
docs: {
description: 'Disallow comments except for specified allowed patterns.',
url: 'https://github.com/tomerh2001/eslint-plugin-th-rules/blob/main/docs/rules/no-comments.md'
},
fixable: 'code',
schema: [
{
type: 'object',
properties: {
allow: {
type: 'array',
items: {
type: 'string',
},
description: 'Additional patterns to allow in comments.',
},
disallow: {
type: 'array',
items: {
type: 'string',
},
description: 'Additional patterns to disallow in comments.',
},
},
additionalProperties: false,
},
],
type: 'problem',
docs: {
description: 'Disallow comments except for specified allowed patterns.',
url: 'https://github.com/tomerh2001/eslint-plugin-th-rules/blob/main/docs/rules/no-comments.md',
},
fixable: 'code',
schema: [
{
type: 'object',
properties: {
allow: {
type: 'array',
items: {
type: 'string',
},
description: 'Additional patterns to allow in comments.',
},
disallow: {
type: 'array',
items: {
type: 'string',
},
description: 'Additional patterns to disallow in comments.',
},
},
additionalProperties: false,
},
],
};

function create(context) {
const options = context.options[0] || {};
const userAllowedPatterns = (options.allow || []).map((pattern) => new RegExp(pattern));
const userDisallowedPatterns = (options.disallow || []).map((pattern) => new RegExp(pattern));
const options = context.options[0] || {};
const userAllowedPatterns = (options.allow || []).map(pattern => new RegExp(pattern));
const userDisallowedPatterns = (options.disallow || []).map(pattern => new RegExp(pattern));

function isCommentAllowed(comment) {
const text = comment.value.trim();
function isCommentAllowed(comment) {
const text = comment.value.trim();

// Check if the comment is a valid JSDoc comment
if (comment.type === 'Block' && comment.value.startsWith('*')) {
return true; // Allow any JSDoc-style block comment (/** ... */)
}
// Check if the comment is a valid JSDoc comment
if (comment.type === 'Block' && comment.value.startsWith('*')) {
return true; // Allow any JSDoc-style block comment (/** ... */)
}

// Check if the comment matches any allowed pattern
for (const pattern of [...allowedPatterns, ...userAllowedPatterns]) {
if (pattern.test(text)) {
return true;
}
}
// Check if the comment matches any allowed pattern
for (const pattern of [...allowedPatterns, ...userAllowedPatterns]) {
if (pattern.test(text)) {
return true;
}
}

// Check if the comment matches any disallowed pattern
for (const pattern of userDisallowedPatterns) {
if (pattern.test(text)) {
return false;
}
}
// Check if the comment matches any disallowed pattern
for (const pattern of userDisallowedPatterns) {
if (pattern.test(text)) {
return false;
}
}

return false; // Disallow by default if no match
}
return false; // Disallow by default if no match
}

return {
Program() {
const sourceCode = context.getSourceCode();
const comments = sourceCode.getAllComments();
return {
Program() {
const sourceCode = context.getSourceCode();
const comments = sourceCode.getAllComments();

comments.forEach((comment) => {
if (!isCommentAllowed(comment)) {
context.report({
node: comment,
message: 'Comment not allowed.',
fix(fixer) {
return fixer.remove(comment);
},
});
}
});
},
};
for (const comment of comments) {
if (!isCommentAllowed(comment)) {
context.report({
node: comment,
message: 'Comment not allowed.',
fix(fixer) {
return fixer.remove(comment);
},
});
}
}
},
};
}

const rule = {
meta,
create,
meta,
create,
};

module.exports = rule;
module.exports = rule;
2 changes: 1 addition & 1 deletion src/rules/no-default-export.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const meta = {
type: 'problem',
docs: {
description: 'Convert unnamed default exports to named default exports based on the file name.',
url: 'https://github.com/tomerh2001/eslint-plugin-th-rules/blob/main/docs/rules/no-default-export.md'
url: 'https://github.com/tomerh2001/eslint-plugin-th-rules/blob/main/docs/rules/no-default-export.md',
},
fixable: 'code',
schema: [],
Expand Down
2 changes: 1 addition & 1 deletion src/rules/no-destructuring.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const meta = {
description: 'Disallow destructuring that does not meet certain conditions',
category: 'Possible Errors',
recommended: true,
url: 'https://github.com/tomerh2001/eslint-plugin-th-rules/blob/main/docs/rules/no-destructuring.md'
url: 'https://github.com/tomerh2001/eslint-plugin-th-rules/blob/main/docs/rules/no-destructuring.md',
},
schema: [
{
Expand Down
Loading

0 comments on commit 2238f59

Please sign in to comment.