Skip to content

Commit

Permalink
Merge branch 'main' into fix/asyn-callback-func
Browse files Browse the repository at this point in the history
  • Loading branch information
fossamagna committed Oct 7, 2024
2 parents dbbc18e + 778b05d commit 36005fb
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 3 deletions.
15 changes: 15 additions & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# 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. -->
52 changes: 52 additions & 0 deletions .github/workflows/pullRequest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -112,3 +120,47 @@ 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
with:
fetch-depth: 1

- name: Checkout main
uses: actions/checkout@v3
with:
ref: main
path: main
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_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_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 }}'
})
11 changes: 10 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -148,4 +158,3 @@ jobs:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_command: npm publish --access public
generate_release_notes: true

27 changes: 27 additions & 0 deletions __tests__/require-usememo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,33 @@ describe('Rule - Require-usememo', () => {
return {x, y};
}`,
},
// ignoredPropNames
{
code: `const Component = () => {
const myObject = {};
return <Child ignoreProp={myObject} />;
}`,
options: [{ ignoredPropNames: ["ignoreProp"] }],
},
{
code: `const Component = () => {
const myCallback = () => {};
return <button onClick={myCallback}>Click me</button>;
}`,
options: [{ ignoredPropNames: ["onClick"] }],
},
{
code: `const Component = () => {
return <div style={{ width: '200px' }} />;
}`,
options: [{ ignoredPropNames: ["style"] }],
},
{
code: `const Component = () => {
return <button onClick={() => {}}>Click me</button>;
}`,
options: [{ ignoredPropNames: ["onClick"] }],
},
],
invalid: [
{
Expand Down
4 changes: 4 additions & 0 deletions docs/rules/require-usememo.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ The rule takes an optional object:
"fix": { "addImports": true },
"checkHookCalls": true,
"ignoredHookCallsNames": { "useStateManagement": false },
"ignoredPropNames": ["style"]
}],
}
```
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@arthurgeron/eslint-plugin-react-usememo",
"version": "2.4.1",
"version": "2.4.3",
"description": "",
"main": "dist/index.js",
"author": "Stefano J. Attardi <[email protected]> & Arthur Geron <[email protected]",
Expand Down
8 changes: 7 additions & 1 deletion src/require-usememo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
],
Expand Down Expand Up @@ -67,10 +67,16 @@ const rule: Rule.RuleModule = {
}

function JSXAttribute<T extends Rule.Node | TSESTree.MethodDefinitionComputedName>(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;
Expand Down

0 comments on commit 36005fb

Please sign in to comment.