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

Provide a quick fix to add missing enum member #40056

Open
DanielRosenwasser opened this issue Aug 14, 2020 · 4 comments
Open

Provide a quick fix to add missing enum member #40056

DanielRosenwasser opened this issue Aug 14, 2020 · 4 comments

Comments

@DanielRosenwasser
Copy link
Member

@DanielRosenwasser DanielRosenwasser commented Aug 14, 2020

enum TokenKind {
  LeftBrace,
}

function scan(text: string, pos: number) {
  let c = text[pos];
  switch (c) {
    case '{': return TokenKind.LeftBrace;
    case '}': return TokenKind.RightBrace;
  }
}

We should have a quick fix for the error message

Property 'RightBrace' does not exist on type 'typeof TokenKind'.

to add RightBrace as a missing enum member. The resulting code should look like

enum TokenKind {
  LeftBrace,
  RightBrace,
}

function scan(text: string, pos: number) {
  let c = text[pos];
  switch (c) {
    case '{': return TokenKind.LeftBrace;
    case '}': return TokenKind.RightBrace;
  }
}
@DanielRosenwasser
Copy link
Member Author

@DanielRosenwasser DanielRosenwasser commented Aug 14, 2020

One could probably fold in the logic into the existing "add missing member" quick fix, which would be preferable.

@DanielRosenwasser
Copy link
Member Author

@DanielRosenwasser DanielRosenwasser commented Aug 14, 2020

"What happens if you have something that's both a class and enum?"

I don't really know. Don't offer the quick fix in that case.

@JoshuaKGoldberg
Copy link
Contributor

@JoshuaKGoldberg JoshuaKGoldberg commented Aug 15, 2020

Err, @DanielRosenwasser this already exists, I think. Copy & pasting that sample into the playground does give a quick fix.

Playground Link: Provided

@a-tarasyuk
Copy link
Contributor

@a-tarasyuk a-tarasyuk commented Aug 15, 2020

if (info.kind === InfoKind.Enum) {
const { token, parentDeclaration } = info;
const changes = textChanges.ChangeTracker.with(context, t => addEnumMemberDeclaration(t, context.program.getTypeChecker(), token, parentDeclaration));
return [createCodeFixAction(fixName, changes, [Diagnostics.Add_missing_enum_member_0, token.text], fixId, Diagnostics.Add_all_missing_members)];
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.