From a283712ba067a0693277ade433476119eeb17f97 Mon Sep 17 00:00:00 2001 From: Arthur Geron <3487334+arthurgeron@users.noreply.github.com> Date: Sat, 5 Oct 2024 23:52:42 -0300 Subject: [PATCH 01/11] feat: add a pull request template --- .github/pull_request_template.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 .github/pull_request_template.md diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md new file mode 100644 index 0000000..bbffc5f --- /dev/null +++ b/.github/pull_request_template.md @@ -0,0 +1,18 @@ +# Pull Request Template + +## Description + +Please provide a clear and concise description of the changes and the purpose they serve. + +## Checklist + +Before submitting your pull request, please confirm the following: + +- [ ] **Tests**: I have created multiple test case scenarios for my changes. +- [ ] **Passing Tests**: All existing and new tests are passing. +- [ ] **Version Bump**: I have increased the package version number in `package.json` following the [Semantic Versioning](https://semver.org/) (SEMVER) standard. +- [ ] **Focused Changes**: My code changes are focused solely on the matter described above. + +## Additional Information + +If applicable, please provide any additional information or context for your changes. From d0d321663927a652bc0000153c6e826cf2dc18ea Mon Sep 17 00:00:00 2001 From: Arthur Geron <3487334+arthurgeron@users.noreply.github.com> Date: Sat, 5 Oct 2024 23:56:31 -0300 Subject: [PATCH 02/11] feat: enforce semver changes --- .github/workflows/pullRequest.yml | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/.github/workflows/pullRequest.yml b/.github/workflows/pullRequest.yml index 31f87d3..01b7f5d 100644 --- a/.github/workflows/pullRequest.yml +++ b/.github/workflows/pullRequest.yml @@ -112,3 +112,27 @@ jobs: - name: Find dead code run: yarn deadCode + + version-check: + name: Check package version + runs-on: ubuntu-latest + steps: + - name: Checkout PR + uses: actions/checkout@v3 + + - name: Checkout main + uses: actions/checkout@v3 + with: + ref: main + path: main + + - name: Compare versions + run: | + PR_VERSION=$(node -p "require('./package.json').version") + MAIN_VERSION=$(node -p "require('./main/package.json').version") + if [ "$PR_VERSION" = "$MAIN_VERSION" ]; then + echo "Error: Package version has not been updated" + exit 1 + else + echo "Package version has been updated" + fi From afecb99269979f933731f6cd6fd170643cb58dd7 Mon Sep 17 00:00:00 2001 From: Arthur Geron <3487334+arthurgeron@users.noreply.github.com> Date: Sat, 5 Oct 2024 23:57:07 -0300 Subject: [PATCH 03/11] feat: only checkout latest commit --- .github/workflows/pullRequest.yml | 11 +++++++++++ .github/workflows/release.yml | 11 ++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/.github/workflows/pullRequest.yml b/.github/workflows/pullRequest.yml index 01b7f5d..c604fb5 100644 --- a/.github/workflows/pullRequest.yml +++ b/.github/workflows/pullRequest.yml @@ -9,6 +9,8 @@ jobs: steps: - name: Checkout uses: actions/checkout@v3 + with: + fetch-depth: 1 - name: Use Node.js 18.x uses: actions/setup-node@v3 @@ -39,6 +41,8 @@ jobs: steps: - name: Checkout uses: actions/checkout@v3 + with: + fetch-depth: 1 - name: Use Node.js 18.x uses: actions/setup-node@v3 @@ -70,6 +74,8 @@ jobs: steps: - name: Checkout uses: actions/checkout@v3 + with: + fetch-depth: 1 - name: Use Node.js 18.x uses: actions/setup-node@v3 @@ -95,6 +101,8 @@ jobs: steps: - name: Checkout uses: actions/checkout@v3 + with: + fetch-depth: 1 - name: Use Node.js 18.x uses: actions/setup-node@v3 @@ -119,12 +127,15 @@ jobs: steps: - name: Checkout PR uses: actions/checkout@v3 + with: + fetch-depth: 1 - name: Checkout main uses: actions/checkout@v3 with: ref: main path: main + fetch-depth: 1 - name: Compare versions run: | diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 72ef5a1..16307e2 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -11,6 +11,8 @@ jobs: steps: - name: Checkout uses: actions/checkout@v3 + with: + fetch-depth: 1 - name: Use Node.js 16.x uses: actions/setup-node@v3 @@ -41,6 +43,8 @@ jobs: steps: - name: Checkout uses: actions/checkout@v3 + with: + fetch-depth: 1 - name: Use Node.js 16.x uses: actions/setup-node@v3 @@ -72,6 +76,8 @@ jobs: steps: - name: Checkout uses: actions/checkout@v3 + with: + fetch-depth: 1 - name: Use Node.js 16.x uses: actions/setup-node@v3 @@ -97,6 +103,8 @@ jobs: steps: - name: Checkout uses: actions/checkout@v3 + with: + fetch-depth: 1 - name: Use Node.js 16.x uses: actions/setup-node@v3 @@ -122,6 +130,8 @@ jobs: steps: - name: Checkout uses: actions/checkout@v3 + with: + fetch-depth: 1 - name: Use Node.js 16.x uses: actions/setup-node@v3 @@ -148,4 +158,3 @@ jobs: github_token: ${{ secrets.GITHUB_TOKEN }} publish_command: npm publish --access public generate_release_notes: true - From e7a0198d95db363cb0568c9eef4dcfb28eb9623c Mon Sep 17 00:00:00 2001 From: Arthur Geron <3487334+arthurgeron@users.noreply.github.com> Date: Sat, 5 Oct 2024 23:59:37 -0300 Subject: [PATCH 04/11] feat: also check if new version is bigger than the current --- .github/workflows/pullRequest.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/pullRequest.yml b/.github/workflows/pullRequest.yml index c604fb5..6c7fd13 100644 --- a/.github/workflows/pullRequest.yml +++ b/.github/workflows/pullRequest.yml @@ -144,6 +144,9 @@ jobs: if [ "$PR_VERSION" = "$MAIN_VERSION" ]; then echo "Error: Package version has not been updated" exit 1 + elif ! npx semver -r ">$MAIN_VERSION" "$PR_VERSION" > /dev/null; then + echo "Error: New version ($PR_VERSION) is not greater than current version ($MAIN_VERSION)" + exit 1 else - echo "Package version has been updated" + echo "Package version has been updated correctly" fi From 4e57860a09d2e9608d17a49e4fc7f082d27ad431 Mon Sep 17 00:00:00 2001 From: Arthur Geron <3487334+arthurgeron@users.noreply.github.com> Date: Sun, 6 Oct 2024 00:02:54 -0300 Subject: [PATCH 05/11] feat: append comment with error to the PR --- .github/workflows/pullRequest.yml | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/.github/workflows/pullRequest.yml b/.github/workflows/pullRequest.yml index 6c7fd13..48e5f26 100644 --- a/.github/workflows/pullRequest.yml +++ b/.github/workflows/pullRequest.yml @@ -138,15 +138,29 @@ jobs: fetch-depth: 1 - name: Compare versions + id: version_check run: | PR_VERSION=$(node -p "require('./package.json').version") MAIN_VERSION=$(node -p "require('./main/package.json').version") if [ "$PR_VERSION" = "$MAIN_VERSION" ]; then - echo "Error: Package version has not been updated" + echo "ERROR_MESSAGE=Error: Package version has not been updated" >> $GITHUB_OUTPUT exit 1 elif ! npx semver -r ">$MAIN_VERSION" "$PR_VERSION" > /dev/null; then - echo "Error: New version ($PR_VERSION) is not greater than current version ($MAIN_VERSION)" + echo "ERROR_MESSAGE=Error: New version ($PR_VERSION) is not greater than current version ($MAIN_VERSION)" >> $GITHUB_OUTPUT exit 1 else echo "Package version has been updated correctly" fi + + - name: Comment PR + if: failure() + uses: actions/github-script@v6 + with: + github-token: ${{secrets.GITHUB_TOKEN}} + script: | + github.rest.issues.createComment({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + body: '${{ steps.version_check.outputs.ERROR_MESSAGE }}' + }) From b66f808844d9638054bbbb4ba6f7d3ef0dbd09cb Mon Sep 17 00:00:00 2001 From: Arthur Geron <3487334+arthurgeron@users.noreply.github.com> Date: Sun, 6 Oct 2024 00:05:35 -0300 Subject: [PATCH 06/11] chore: increase patch version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 18cdfdb..4c978c4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@arthurgeron/eslint-plugin-react-usememo", - "version": "2.4.1", + "version": "2.4.2", "description": "", "main": "dist/index.js", "author": "Stefano J. Attardi & Arthur Geron Date: Sun, 6 Oct 2024 00:08:06 -0300 Subject: [PATCH 07/11] chore: minor updates to PR template --- .github/pull_request_template.md | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index bbffc5f..36721cf 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -1,12 +1,10 @@ # Pull Request Template ## Description - -Please provide a clear and concise description of the changes and the purpose they serve. + ## Checklist - -Before submitting your pull request, please confirm the following: + - [ ] **Tests**: I have created multiple test case scenarios for my changes. - [ ] **Passing Tests**: All existing and new tests are passing. @@ -14,5 +12,4 @@ Before submitting your pull request, please confirm the following: - [ ] **Focused Changes**: My code changes are focused solely on the matter described above. ## Additional Information - -If applicable, please provide any additional information or context for your changes. + From 1973ddf45b1d4e4c7cd700f2900a495883a88764 Mon Sep 17 00:00:00 2001 From: MURAKAMI Masahiko Date: Tue, 1 Oct 2024 11:19:22 +0900 Subject: [PATCH 08/11] feat: add ignoredPropNames option to require-usememo rule --- __tests__/require-usememo.test.ts | 8 ++++++++ src/require-usememo/index.ts | 8 +++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/__tests__/require-usememo.test.ts b/__tests__/require-usememo.test.ts index 1b0bc7b..51cac8a 100644 --- a/__tests__/require-usememo.test.ts +++ b/__tests__/require-usememo.test.ts @@ -327,6 +327,14 @@ describe('Rule - Require-usememo', () => { return {x, y}; }`, }, + // ignoredPropNames + { + code: `const Component = () => { + const myObject = {}; + return ; + }`, + options: [{ ignoredPropNames: ["ignoreProp"] }], + }, ], invalid: [ { diff --git a/src/require-usememo/index.ts b/src/require-usememo/index.ts index 38f6cd1..41d1746 100644 --- a/src/require-usememo/index.ts +++ b/src/require-usememo/index.ts @@ -25,7 +25,7 @@ const rule: Rule.RuleModule = { type: "object", properties: { strict: { type: "boolean" }, checkHookReturnObject: { type: "boolean" }, checkHookCalls: { type: "boolean"}, ignoredHookCallsNames: {type: "object"}, fix: { addImports: "boolean", - } }, + }, ignoredPropNames: { type: "array" } }, additionalProperties: false, }, ], @@ -67,10 +67,16 @@ const rule: Rule.RuleModule = { } function JSXAttribute(node: T) { + + const ignoredPropNames = context.options?.[0]?.ignoredPropNames ?? []; + const { parent, value } = node as TSESTree.MethodDefinitionComputedName; if (value === null) return null; if (parent && !isComplexComponent(parent as TSESTree.JSXIdentifier)) return null; if ((value.type as string) === "JSXExpressionContainer") { + if (ignoredPropNames.includes((node as unknown as TSESTree.JSXAttribute).name?.name)) { + return null; + } process(node as TSESTree.MethodDefinitionComputedName, undefined, undefined, true); } return null; From 4484ba437a84024902bb1265eb1a0fe8e04e2364 Mon Sep 17 00:00:00 2001 From: MURAKAMI Masahiko Date: Tue, 1 Oct 2024 11:24:30 +0900 Subject: [PATCH 09/11] docs: update for `ignoredPropNamas` option --- docs/rules/require-usememo.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/rules/require-usememo.md b/docs/rules/require-usememo.md index b23f075..2f36654 100644 --- a/docs/rules/require-usememo.md +++ b/docs/rules/require-usememo.md @@ -18,6 +18,7 @@ The rule takes an optional object: "fix": { "addImports": true }, "checkHookCalls": true, "ignoredHookCallsNames": { "useStateManagement": false }, + "ignoredPropNames": ["style"] }], } ``` @@ -42,6 +43,9 @@ You can use strict 1:1 comparisons (e.g., `"useCustomHook"`) or employ Minimatch - `fix`: Contains rules that only apply during eslint's fix routine. - `addImports`: Creates imports for useMemo and useCallback when one or both are added by this rule. Will increment to a existing import declaration or create a new one. Setting this to false disables it, defaults to true. + +- `ignoredPropNames`: This allows you to add specific prop name, thereby disabling them to be checked when used. + ## Autofix Examples (Function Components & Hooks only) To illustrate the autofix feature in action, below are some examples with input code and the corresponding fixed output: From 7e8efeafe9b5d671cebc2c479a3664a728cf734a Mon Sep 17 00:00:00 2001 From: MURAKAMI Masahiko Date: Fri, 4 Oct 2024 00:14:42 +0900 Subject: [PATCH 10/11] test: add a few more case - inline object/functions - const functions --- __tests__/require-usememo.test.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/__tests__/require-usememo.test.ts b/__tests__/require-usememo.test.ts index 51cac8a..6b27a02 100644 --- a/__tests__/require-usememo.test.ts +++ b/__tests__/require-usememo.test.ts @@ -335,6 +335,25 @@ describe('Rule - Require-usememo', () => { }`, options: [{ ignoredPropNames: ["ignoreProp"] }], }, + { + code: `const Component = () => { + const myCallback = () => {}; + return ; + }`, + options: [{ ignoredPropNames: ["onClick"] }], + }, + { + code: `const Component = () => { + return
; + }`, + options: [{ ignoredPropNames: ["style"] }], + }, + { + code: `const Component = () => { + return ; + }`, + options: [{ ignoredPropNames: ["onClick"] }], + }, ], invalid: [ { From 778b05d601793798f684fe10c2f6250025c47c15 Mon Sep 17 00:00:00 2001 From: MURAKAMI Masahiko Date: Mon, 7 Oct 2024 09:01:15 +0900 Subject: [PATCH 11/11] chore: increase patch version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 4c978c4..1ce47dc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@arthurgeron/eslint-plugin-react-usememo", - "version": "2.4.2", + "version": "2.4.3", "description": "", "main": "dist/index.js", "author": "Stefano J. Attardi & Arthur Geron