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

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

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

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 ae91e94
🔍 Latest deploy log https://app.netlify.com/sites/typescript-eslint/deploys/663f855470b49f00088ccfb2
😎 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: 99 (🟢 up 3 from production)
Accessibility: 100 (no change from production)
Best Practices: 92 (no change from production)
SEO: 98 (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

codecov bot commented Mar 22, 2024

Codecov Report

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

Project coverage is 87.03%. Comparing base (f248e68) to head (76d9f36).
Report is 7 commits behind head on main.

❗ Current head 76d9f36 differs from pull request most recent head ae91e94. Consider uploading reports for the commit ae91e94 to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8752      +/-   ##
==========================================
- Coverage   87.40%   87.03%   -0.37%     
==========================================
  Files         260      254       -6     
  Lines       12608    12492     -116     
  Branches     3937     3923      -14     
==========================================
- Hits        11020    10873     -147     
- Misses       1313     1335      +22     
- Partials      275      284       +9     
Flag Coverage Δ
unittest 87.03% <50.00%> (-0.37%) ⬇️

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%> (-61.91%) ⬇️

... and 42 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
@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
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
@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?

@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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

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