Skip to content

feat(eslint-plugin): [array-type] detect Readonly<string[]> case #8752

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

Conversation

developer-bandi
Copy link
Contributor

PR Checklist

Overview

In the case of Readonly<string[]>, the same as Readonly Array<string>, an error is displayed and then changed to readonly string[].

However, on the contrary, readonly string[] is converted to ReadonlyArray<string> as before.

@typescript-eslint
Copy link
Contributor

Thanks for the PR, @developer-bandi!

typescript-eslint is a 100% community driven project, and we are incredibly grateful that you are contributing to that community.

The core maintainers work on this in their personal time, so please understand that it may not be possible for them to review your work immediately.

Thanks again!


🙏 Please, if you or your company is finding typescript-eslint valuable, help us sustain the project by sponsoring it transparently on https://opencollective.com/typescript-eslint.

Copy link

netlify bot commented Mar 22, 2024

Deploy Preview for typescript-eslint ready!

Name Link
🔨 Latest commit 1535f1f
🔍 Latest deploy log https://app.netlify.com/sites/typescript-eslint/deploys/667af030a886b10009f90975
😎 Deploy Preview https://deploy-preview-8752--typescript-eslint.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 96 (🔴 down 4 from production)
Accessibility: 100 (no change from production)
Best Practices: 92 (no change from production)
SEO: 90 (no change from production)
PWA: 80 (no change from production)
View the detailed breakdown and full score reports

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

Copy link

nx-cloud bot commented Mar 22, 2024

☁️ Nx Cloud Report

CI is running/has finished running commands for commit 1535f1f. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this CI Pipeline Execution


✅ Successfully ran 1 target

Sent with 💌 from NxCloud.

Copy link

codecov bot commented Mar 22, 2024

Codecov Report

Attention: Patch coverage is 50.00000% with 3 lines in your changes missing coverage. Please review.

Project coverage is 87.03%. Comparing base (3e19436) to head (76d9f36).
Report is 219 commits behind head on main.

Current head 76d9f36 differs from pull request most recent head 1535f1f

Please upload reports for the commit 1535f1f to get more accurate results.

Additional details and impacted files
@@            Coverage Diff            @@
##           main    #8752       +/-   ##
=========================================
+ Coverage      0   87.03%   +87.03%     
=========================================
  Files         0      254      +254     
  Lines         0    12492    +12492     
  Branches      0     3923     +3923     
=========================================
+ Hits          0    10873    +10873     
- Misses        0     1335     +1335     
- Partials      0      284      +284     
Flag Coverage Δ
unittest 87.03% <50.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
packages/eslint-plugin/src/rules/array-type.ts 36.61% <50.00%> (ø)

... and 253 files with indirect coverage changes

@auvred auvred added the awaiting response Issues waiting for a reply from the OP or another party label Apr 6, 2024
@developer-bandi developer-bandi requested a review from auvred April 7, 2024 03:31
@github-actions github-actions bot removed the awaiting response Issues waiting for a reply from the OP or another party label Apr 7, 2024
Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll defer to @auvred as they already started reviewing. Just fly-by leaving a couple of requests. Cheers!

Comment on lines 45 to 46
Always use `Array<T>` or `ReadonlyArray<T>` or `Readonly<T[]>` for all array types.
However, `readonly T[]` is modified to `ReadonlyArray<T>`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Docs] A bit of clarity ("however" implies contrast with the previous statement)

Suggested change
Always use `Array<T>` or `ReadonlyArray<T>` or `Readonly<T[]>` for all array types.
However, `readonly T[]` is modified to `ReadonlyArray<T>`.
Always use `Array<T>`, `ReadonlyArray<T>`, or `Readonly<T[]>` for all array types.
`readonly T[]` will be modified to `ReadonlyArray<T>`.

function fooFunction(foo: Array<ArrayClass<string>>) {
return foo.map(e => e.foo);
}
`,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Style] Sorry if I missed this before - could you please revert any unrelated changes, such as differences in indentation levels? It's harder to review the PR when there's a lot of irrelevant noise in the diff.

@JoshuaKGoldberg JoshuaKGoldberg added the awaiting response Issues waiting for a reply from the OP or another party label Apr 23, 2024
@github-actions github-actions bot removed the awaiting response Issues waiting for a reply from the OP or another party label Apr 28, 2024
auvred
auvred previously requested changes May 1, 2024
Comment on lines +212 to +214
const isReadonlyWithGenericArrayType =
node.typeName.name === 'Readonly' &&
node.typeArguments?.params[0].type === AST_NODE_TYPES.TSArrayType;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Bug] If the type name is Readonly and first type argument is not an array, we should return from the function. Because otherwise any Readonly<...> type will be reported.

const x: Readonly<string> = 'a'

playground

Comment on lines 1384 to 1386
line: 3,
column: 8,
column: 12,
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Tests] Could you revert these changed columns? These changes don't seem to be related to this PR.

@auvred auvred added the awaiting response Issues waiting for a reply from the OP or another party label May 1, 2024
@developer-bandi developer-bandi requested a review from auvred May 3, 2024 14:18
@github-actions github-actions bot removed the awaiting response Issues waiting for a reply from the OP or another party label May 3, 2024
@developer-bandi
Copy link
Contributor Author

Some of the current CI test cases are failing.

Since it is not failing locally, could you please help me resolve it?

@@ -58,6 +59,7 @@ const y: readonly string[] = ['a', 'b'];
```ts option='{ "default": "generic" }'
const x: Array<string> = ['a', 'b'];
const y: ReadonlyArray<string> = ['a', 'b'];
const z: Readonly<string[]> = ['a', 'b'];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re #8752 (comment)
(One of) the CI failure(s) is due to this line being flagged, despite being in the "Correct" tab. (you can try it for yourself in the deploy preview by clicking the "Open in playground" button)

 FAIL  tests/docs.test.ts (25.146 s, 827 MB heap size)
  ● Validating rule docs › array-type.mdx › code examples ESLint output
    assert(received)
    Expected value to be equal to:
      true
    Received:
      false
    
    Message:
      Expected not to contain lint errors:
    const x: Array<string> = ['a', 'b'];
    const y: ReadonlyArray<string> = ['a', 'b'];
    const z: Readonly<string[]> = ['a', 'b'];

Shoutout to @auvred's for his work in #8382 / https://github.com/typescript-eslint/typescript-eslint/pull/8497/files#diff-62842d0740c234c02c26a4ccd3b3fe920b872cff1479f0703c6ec44f9cfb2f3dR407 that caught this!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you for your advise!
스크린샷 2024-05-11 오후 11 22 13

but there is one problem. i use incorrect exampleconst z: Readonly<string[]> = ['a', 'b']; but lint cannot use this type. Is there no way?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, sorry for the slow response.

I'm not really familiar with this change, and, I apologize, but I'm having a bit of a hard time understanding what you mean in the question...

I'm wondering if the issue is that, now that the snapshot has been set, the snapshot tests are failing due to the examples being changed? If so, you may just need to run cd eslint-plugin; yarn test schemas -u (or similar); see #8997.

Let me know if that resolves your issue or if the problem is something else! 🙂

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW - looking closer at the screenshot, i think you might also need to run yarn lint-fix to fix the existing violations throughout the repo of the new cases that this rule reports.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem was that I had to apply the changed lint rule(array-type) to another file. Thank you!

@kirkwaiblinger kirkwaiblinger added the awaiting response Issues waiting for a reply from the OP or another party label May 8, 2024
@github-actions github-actions bot removed the awaiting response Issues waiting for a reply from the OP or another party label May 11, 2024
@bradzacher bradzacher added the enhancement New feature or request label May 28, 2024
kirkwaiblinger

This comment was marked as off-topic.

@kirkwaiblinger kirkwaiblinger added the awaiting response Issues waiting for a reply from the OP or another party label Jun 11, 2024
@github-actions github-actions bot removed the awaiting response Issues waiting for a reply from the OP or another party label Jun 22, 2024
Copy link
Member

@bradzacher bradzacher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will sadly false-negative on a case like this:

type T = string[];
type U = Readonly<T>;

However that seems like it is okay for two reasons:

  1. you can't convert Readonly<T> to readonly T - you would need to do readonly T[number][] which is just ew.
  2. we would need type info to even begin to catch it - defs not worth adding that to the rule.

This LGTM - thanks for adding this!
This has been a pet peeve of mine for a while.

@bradzacher bradzacher added the 1 approval >=1 team member has approved this PR; we're now leaving it open for more reviews before we merge label Jun 23, 2024
Copy link
Member

@kirkwaiblinger kirkwaiblinger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reading

@kirkwaiblinger kirkwaiblinger dismissed stale reviews from auvred and JoshuaKGoldberg June 25, 2024 19:44

stale

@kirkwaiblinger kirkwaiblinger merged commit 9dba021 into typescript-eslint:main Jun 25, 2024
60 of 61 checks passed
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 3, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
1 approval >=1 team member has approved this PR; we're now leaving it open for more reviews before we merge enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Enhancement: [array-type] Detect Readonly<string[]>
5 participants