Skip to content

refactor: Strengthen Type Safety and Fix Test File Compilation Errors #1015

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

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

Conversation

y-hsgw
Copy link
Contributor

@y-hsgw y-hsgw commented May 31, 2025

It's a minor change, but I refactored a few parts that caught my attention. Please feel free to close this PR if it doesn't meet your expectations. I appreciate your review!

Checks

Changes

  • update test case types to use rule-tester types.
    → Due to deprecation of TSESLint's InvalidTestCase and ValidTestCase.

  • apply as const for stricter type safety on query definitions.

  • add explicit return type to createTestingLibraryRule for better type safety.
    → Because a type error occurred in test files when passing the rule as the second argument to ruleTester.run.

Context

Copy link

codecov bot commented May 31, 2025

Codecov Report

Attention: Patch coverage is 96.77419% with 1 line in your changes missing coverage. Please review.

Project coverage is 96.28%. Comparing base (26d360e) to head (a90211d).
Report is 22 commits behind head on main.

Files with missing lines Patch % Lines
lib/create-testing-library-rule/index.ts 85.71% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1015   +/-   ##
=======================================
  Coverage   96.27%   96.28%           
=======================================
  Files          46       47    +1     
  Lines        2472     2531   +59     
  Branches     1025     1047   +22     
=======================================
+ Hits         2380     2437   +57     
- Misses         92       94    +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Belco90 Belco90 self-requested a review June 2, 2025 05:28
@Belco90 Belco90 added the chore Changes that affect the build system, CI config or other changes that don't modify src/test files label Jun 2, 2025
Copy link
Member

@Belco90 Belco90 left a comment

Choose a reason for hiding this comment

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

Thanks for taking care of this! Just added some comments.

throw new Error('Rule metadata must contain `docs` property');
}

return { ...rule, meta: { ...rule.meta, docs } };
Copy link
Member

@Belco90 Belco90 Jun 2, 2025

Choose a reason for hiding this comment

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

question(blocking): Why? I don't fully understand the reason to return docs within meta if it was already there.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I’m explicitly specifying docs because the following code results in a type error:

return { ...rule, meta: { ...rule.meta } };

Type '(TestingLibraryPluginDocs & RuleMetaDataDocs) | undefined' is not assignable to type 'TestingLibraryPluginDocs & RuleMetaDataDocs'.
Type 'undefined' is not assignable to type 'TestingLibraryPluginDocs'.

Copy link
Member

Choose a reason for hiding this comment

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

But why not returning just rule?

Copy link
Contributor Author

@y-hsgw y-hsgw Jun 3, 2025

Choose a reason for hiding this comment

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

rule.meta.docs is marked as optional in the return type of ESLintUtils.RuleCreator,
whereas rule.meta.docs is required in TestingLibraryPluginRuleModule<TMessageIds, TOptions>.
So I'm narrowing the type explicitly to ensure it satisfies the expected structure.

If we return rule directly, we get the following type error:

Type 'RuleModule<TMessageIds, TOptions, TestingLibraryPluginDocs, RuleListener>' is not assignable to type 'TestingLibraryPluginRuleModule<TMessageIds, TOptions>'.
Types of property 'meta.docs' are incompatible.
Type '(TestingLibraryPluginDocs & RuleMetaDataDocs) | undefined' is not assignable to type 'TestingLibraryPluginDocs & RuleMetaDataDocs'.
Type 'undefined' is not assignable to type 'TestingLibraryPluginDocs & RuleMetaDataDocs'.
Type 'undefined' is not assignable to type 'TestingLibraryPluginDocs'.ts(2322)

if (
ASTUtils.isIdentifier(node.property) &&
Copy link
Member

Choose a reason for hiding this comment

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

question(blocking): Why removing the node.property condition from this if statement? It worked as short-circuiting the evaluation of the expression.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I initially tried implementing it like this:

if (
  ASTUtils.isIdentifier(node.property) &&
  ALL_RETURNING_NODES.some(
    (allReturningNode) => allReturningNode === node.property.name
  )
)

However, accessing node.property.name caused a type error:

Property 'name' does not exist on type 'PrivateIdentifier | Expression'.
Property 'name' does not exist on type 'MemberExpressionComputedName'.

That’s why I went with the current approach instead.

Copy link
Member

Choose a reason for hiding this comment

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

But now that you already checked that ASTUtils.isIdentifier(node.property) for extracting the propertyName, you could use that for the condition:

if (
  propertyName &&
  ALL_RETURNING_NODES.some(
    (allReturningNode) => allReturningNode === propertyName
)

Copy link
Contributor Author

@y-hsgw y-hsgw Jun 3, 2025

Choose a reason for hiding this comment

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

Personally, I thought the propertyName && check was unnecessary, since the comparison inside some — allReturningNode === propertyName — would implicitly return false if propertyName is null.

But you're right — adding the explicit condition improves readability, so I updated it in a90211d.

@y-hsgw y-hsgw requested a review from Belco90 June 2, 2025 12:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
chore Changes that affect the build system, CI config or other changes that don't modify src/test files
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants