Skip to content

Conversation

@jpolavar
Copy link
Contributor

@jpolavar jpolavar commented Mar 3, 2025

Closes #116

Tested with internal services

@jpolavar jpolavar added the MINOR label Mar 3, 2025
@jpolavar jpolavar self-assigned this Mar 3, 2025
@jpolavar jpolavar requested review from carlansley and le-cong March 3, 2025 17:23
const sourceCode = context.sourceCode;
const typeAnnotation = sourceCode.getText(node.typeAnnotation);
const expression = sourceCode.getText(node.expression);
return fixer.replaceText(node, `${expression} satisfies ${typeAnnotation}`);
Copy link
Contributor

Choose a reason for hiding this comment

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

I have no idea if it's possible (maybe @le-cong knows) but ideally we'd only do this substitution if using satisfies is ok with the compiler.

Copy link
Contributor

Choose a reason for hiding this comment

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

also one other situation I can think of - x as unknown as Y should be an error but x satisfies unknown satisfies Y isn't going to work as a fix.

Copy link
Contributor

@le-cong le-cong Mar 18, 2025

Choose a reason for hiding this comment

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

i haven't get a chance to look into this, but i feel that this rule is more complex than what #116 describes. it might be necessary to use typescript's api to compare the source/target typing in details in order to tell whether or not the replacement is legit. we don't necessarily want to replace all as with satisfies i am afraid ...

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah i don't think the rule should introduce compiler errors. if we can't use the TS API to work it out, might be best not to auto-fix at all.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added to compare source/target typing to ensure type compatibility, verifying whether the originalType satisfies the constraints of the targetType and removed auto fix

@jpolavar jpolavar requested a review from carlansley March 18, 2025 19:08
Copy link
Contributor

@carlansley carlansley left a comment

Choose a reason for hiding this comment

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

branch needs to be updated from main

node.typeAnnotation.type === AST_NODE_TYPES.TSIntersectionType) &&
node.typeAnnotation.types.some((type) => type.type === AST_NODE_TYPES.TSUnknownKeyword);

if (
Copy link
Contributor

Choose a reason for hiding this comment

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

this is a complex bit of logic I don't quite understand, if there was just one context.report() it would help. also maybe some more isSomethingSomething booleans to help readability.

@jpolavar jpolavar requested a review from carlansley April 3, 2025 04:53
@github-actions
Copy link

Coverage after merging rule-satisfies into main will be

86.07%▴ +0.09%

Coverage Report
FileStmtsBranchesFuncsLinesUncovered Lines
src
   file-path-comment.ts100%100%100%100%
   get-documentation-url.ts100%100%100%100%
   index.ts0%0%0%0%1, 1, 10, 100–109, 11, 110–119, 12, 120–129, 13, 130–139, 14, 140–149, 15, 150–156, 16–19, 2, 20–29, 3, 30–39, 4, 40–49, 5, 50–59, 6, 60–69, 7, 70–79, 8, 80–89, 9, 90–99
   invalid-json-stringify.ts100%100%100%100%
   no-card-numbers.ts95.11%89.29%100%96.03%134–139, 144–146
   no-duplicated-imports.ts100%100%100%100%
   no-enum.ts100%100%100%100%
   no-legacy-service-typing.ts100%100%100%100%
   no-promise-instance-method.ts100%100%100%100%
   no-random-v4-uuid.ts100%100%100%100%
   no-serve-runtime.ts100%100%100%100%
   no-side-effects.ts98.83%97.73%100%99.16%171–173, 207
   no-status-code-assert.ts100%100%100%100%
   no-test-import.ts100%100%100%100%
   no-type-assertion-as.ts100%100%100%100%
   no-uuid.ts95.16%85.71%100%96.23%100–101, 89–91, 99
   no-wallaby-comment.ts97.50%85%100%100%43–44, 58
   object-literal-response.ts100%100%100%100%
   regular-expression-comment.ts95.51%89.47%100%97.06%35–37, 54
   require-assert-message.ts100%100%100%100%
   require-assert-predicate-rejects-throws.ts100%100%100%100%
   require-fixed-services-import.ts100%100%100%100%
   require-resolve-full-response.ts83.69%78.33%100%84.72%105–107, 109–111, 113–115, 122–124, 127, 129, 129–131, 147–149, 199–210, 46, 65, 65–67, 81–83, 94–99
   require-service-call-response-declaration.ts84.81%80%100%84.72%55–66
   require-strict-assert.ts98.40%92%100%100%44–45
   require-ts-extension-imports-exports.ts100%100%100%100%
   require-type-out-of-type-only-imports.ts100%100%100%100%
   service.ts100%100%100%100%
   tester.test.ts100%100%100%100%
   ts-tester.test.ts100%100%100%100%
src/library
   format.ts0%0%0%0%1, 1, 10–19, 2, 20, 3–9
   tree.ts0%0%0%0%1, 1, 10–19, 2, 20–29, 3, 30–39, 4, 40–49, 5, 50–59, 6, 60–69, 7, 70–79, 8, 80–89, 9, 90–97
   ts-tree.ts47.58%46.15%37.50%48.54%100–103, 27–29, 29–30, 33, 33–35, 35–37, 42–43, 46–47, 56–74, 77–92, 95–99
   variable.ts0%0%0%0%1, 1–5

Copy link
Contributor

@carlansley carlansley left a comment

Choose a reason for hiding this comment

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

To @le-cong's point, I think we shouldn't do this rule at all if we can't generally understand when as is replaceable, and fix at least a majority of the remaining issues automatically.

@jpolavar
Copy link
Contributor Author

jpolavar commented May 9, 2025

To @le-cong's point, I think we shouldn't do this rule at all if we can't generally understand when as is replaceable, and fix at least a majority of the remaining issues automatically.

Removed the auto-fix and report the error and will add parserServices and checker to access TypeScript's type-checking capabilities for source and target and determine whether to report or not

@github-actions
Copy link

Beta Published - Install Command: npm install @checkdigit/eslint-plugin@7.15.0-PR.117-a0b1

@jpolavar jpolavar requested a review from carlansley June 13, 2025 19:53
@carlansley
Copy link
Contributor

To @le-cong's point, I think we shouldn't do this rule at all if we can't generally understand when as is replaceable, and fix at least a majority of the remaining issues automatically.

@jpolavar what is the end result of this on some of our bigger services? (maybe Slack me the results)

@jpolavar
Copy link
Contributor Author

To @le-cong's point, I think we shouldn't do this rule at all if we can't generally understand when as is replaceable, and fix at least a majority of the remaining issues automatically.

@jpolavar what is the end result of this on some of our bigger services? (maybe Slack me the results)

I updated it to detect as type assertions and ensure the original type and target type are assignable using TypeScript's type checker. Only then report a violation if the types are assignable, as is unnecessary in such case. Since we have added type checker, i will enhance to fix as well instead of just reporting since it would be lot to manually fix.

Copy link
Contributor

@carlansley carlansley left a comment

Choose a reason for hiding this comment

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

the new rule seems ok to me, but can you give some data on how this works in practice with a few of our bigger, more problematic services?

@github-actions
Copy link

❌ PR review status - has 1 reviewer outstanding

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

add a rule that checks if an as can be converted to a satisfies

4 participants