Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

infra(unicorn): permanently disable no-object-as-default-parameter #3203

Merged
merged 7 commits into from
Nov 3, 2024

Conversation

ST-DDT
Copy link
Member

@ST-DDT ST-DDT commented Oct 20, 2024

Ref: #2439


Permanently disables the [unicorn/disable no-object-as-default-parameter](https://github.com/sindresorhus/eslint-plugin-unicorn/blob/main/docs/rules/disable no-object-as-default-parameter.md) lint rule.

We assume that our users use typescript so they would always pass all required parameters.
Even if they don't all our methods handle that with basic defaults.

Affected code:

~/faker/src/modules/lorem/index.ts
  135:5  error  Do not use an object literal as default for parameter `wordCount`      unicorn/no-object-as-default-parameter
  203:5  error  Do not use an object literal as default for parameter `sentenceCount`  unicorn/no-object-as-default-parameter
  361:5  error  Do not use an object literal as default for parameter `lineCount`      unicorn/no-object-as-default-parameter

~/faker/test/scripts/apidocs/method.example.ts
  270:5  error  Do not use an object literal as default for parameter `a`  unicorn/no-object-as-default-parameter
  305:5  error  Do not use an object literal as default for parameter `a`  unicorn/no-object-as-default-parameter
  325:5  error  Do not use an object literal as default for parameter `a`  unicorn/no-object-as-default-parameter

Alternative Solution

Change the implementation of the methods to not use signature level defaults.

sentence(
-    wordCount:
+    wordCount?:
      | number
      | {
          /**
           * The minimum number of words to generate.
           */
          min: number;
          /**
           * The maximum number of words to generate.
           */
          max: number;
-        } = { min: 3, max: 10 }
+        }
  ): string {
-    const sentence = this.words(wordCount);
+    const sentence = this.words(wordCount ?? { min: 3, max: 10 });
...

@ST-DDT ST-DDT added p: 1-normal Nothing urgent c: infra Changes to our infrastructure or project setup labels Oct 20, 2024
@ST-DDT ST-DDT added this to the vAnytime milestone Oct 20, 2024
@ST-DDT ST-DDT requested review from a team October 20, 2024 18:12
@ST-DDT ST-DDT self-assigned this Oct 20, 2024
@ST-DDT
Copy link
Member Author

ST-DDT commented Oct 20, 2024

I'm actually not sure which variant I prefer.

Copy link

netlify bot commented Oct 20, 2024

Deploy Preview for fakerjs ready!

Name Link
🔨 Latest commit fa8c91f
🔍 Latest deploy log https://app.netlify.com/sites/fakerjs/deploys/6727544e4c713e0008b1a37d
😎 Deploy Preview https://deploy-preview-3203.fakerjs.dev
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

codecov bot commented Oct 20, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.96%. Comparing base (18f14c8) to head (fa8c91f).
Report is 1 commits behind head on next.

Additional details and impacted files
@@            Coverage Diff             @@
##             next    #3203      +/-   ##
==========================================
- Coverage   99.96%   99.96%   -0.01%     
==========================================
  Files        2805     2805              
  Lines      217098   217098              
  Branches      971      966       -5     
==========================================
- Hits       217025   217013      -12     
- Misses         73       85      +12     

see 1 file with indirect coverage changes

@xDivisionByZerox
Copy link
Member

The alternative solution would actually "catch" accidental null inputs from our users:

I compressed the signature for simplicitiy sake:

Object default Inline default
sentence({
    min: number;
    max: number;
} = { min: 3, max: 10 }): string {
    const sentence = this.words(wordCount);
    // ...
}
sentence(); // works
sentence(undefined); // works
sentence(null); // errors!!!
sentence({
    min: number;
    max: number;
}): string {
    const sentence = this.words(wordCount ?? { min: 3, max: 10 });
    // ...
}
sentence(); // works
sentence(undefined); // works
sentence(null); // works

I jsut wanted to point that out.

@ST-DDT
Copy link
Member Author

ST-DDT commented Oct 25, 2024

You could say the same for other methods such as faker.number.int(null).
That's why we recommend strict mode.

"strict": true // Optional, but recommended

@ST-DDT
Copy link
Member Author

ST-DDT commented Oct 26, 2024

Also AFAICT:

faker.lorem.sentence(null)
"Tenax vilitas harum."

const count = this.rangeToNumber(options.count ?? 3);

(That method doesn't follow our usual options deconstruction though)

@matthewmayer
Copy link
Contributor

I also don't have a strong preference. Just want to point out according to #3215 (comment) the faker.lorem.* methods are some of the most heavily used in Faker so we should be very careful not to accidentally change runtime behavior even if it's a subtle change for a edge case.

@ST-DDT ST-DDT added this pull request to the merge queue Nov 3, 2024
@ST-DDT ST-DDT removed this pull request from the merge queue due to the queue being cleared Nov 3, 2024
@ST-DDT ST-DDT enabled auto-merge (squash) November 3, 2024 10:45
@ST-DDT ST-DDT merged commit 2d34798 into next Nov 3, 2024
21 checks passed
@ST-DDT ST-DDT deleted the infra/unicorn/no-object-as-default-parameter branch November 3, 2024 10:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c: infra Changes to our infrastructure or project setup p: 1-normal Nothing urgent
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants