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

Smarter selection of the fix to apply if there are few #1176

Open
egorio opened this issue Jul 14, 2023 · 1 comment
Open

Smarter selection of the fix to apply if there are few #1176

egorio opened this issue Jul 14, 2023 · 1 comment
Labels
enhancement New feature or request

Comments

@egorio
Copy link
Contributor

egorio commented Jul 14, 2023

Though:

Rehearsal currently just looping through available codefixes and trying to apply the next only if current didn't work.
This is more hypothetical case then real, in reality the first fix should fix the issue.

// Use the first available codefix in automatic mode
let fix = fixes.shift();
while (fix && this.wasAttemptedToFix(diagnostic, fix)) {
// Try the next fix if we already tried the first one
fix = fixes.shift();
}

The sample of code has 2 fixes:

  • infer type from usage
  • add type based on inheritance
export class A {
  generate(dimensions: number[]): void {
    console.log(dimensions)
  }
}

export class B extends A {
  generate(dimensions): void {
    for (let y = 0; y < dimensions[1]; y++) {
      for (let x = 0; x < dimensions[0]; x++) {
        console.log(x, y)
      }
    }
  }
}

Instead of just blindly loop over, we can compare results and return only one fix with better types.

This will also allow to optimize drain mode to reduce number of attempts to process the same diagnostic with adopting this kind of exit criteria instead of checking if there are any additinal fixes left inside the cycle.

To achieve this we need to create an extendable list of comparison functions for codefixes (those can be returned together) and just call appropriate function to compare when we have more then 1 fix.

@egorio egorio added the enhancement New feature or request label Jul 14, 2023
@egorio
Copy link
Contributor Author

egorio commented Aug 1, 2023

Another example if to prioritize "Rename @param" over "Delete @param" in case [like this] (https://www.typescriptlang.org/play?ssl=8&ssc=2&pln=1&pc=1#code/PQKhCgAIUgBAHAhgJ0QW0gbwKIDcCmAdgC4C+kikAtFZAMoD2a+xAFgJaEDmUMCK6LAGdiyTl3KJKNSAEFCDNvmS84SVBkwhyAIx3VaASUgATBoQDkxSAGsFAd17BwAMwCuhAMbF25yMnwhFgAKRAAaSB0IzwBKAC5IXAZ2EywoCgBuSKzPDPBSIA) (aa, bb):

/**
 * @param {Event} a -- Something
 * @param {string} aa -- Another
 * @param {*} bb -- I don't know
 */
function reset(a, b, c): void {
  a; b; c;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant