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

prevent-abbreviations: Fix optional parameter #764

Merged
merged 1 commit into from Jun 2, 2020

Conversation

fisker
Copy link
Collaborator

@fisker fisker commented Jun 2, 2020

Fixes #763

@@ -28,7 +28,7 @@ function renameIdentifier(identifier, name, fixer, sourceCode) {

// `typeAnnotation`
if (identifier.typeAnnotation) {
return fixer.replaceText(identifier, `${name}${sourceCode.getText(identifier.typeAnnotation)}`);
return fixer.replaceText(identifier, `${name}${identifier.optional ? '?' : ''}${sourceCode.getText(identifier.typeAnnotation)}`);
Copy link
Owner

Choose a reason for hiding this comment

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

This is kinda fragile. I'm surprised ESLint doesn't have a fixer utility to simply rename a node. If we could just set the identifier.name property instead, we wouldn't have to specially handle identifier.optional and identifier.typeAnnotation.

What do you think about proposing such utility to ESLint?


There might also be other rules of ours where we need to handle identifier.optional

Copy link
Collaborator

Choose a reason for hiding this comment

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

There might be a way to modify the node, render it and then use teplaceText. The only tricky part is to choose the right renderer for JS/TS. This begs for a utility module

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@futpib What do you mean render? Get the original text?

@sindresorhus sindresorhus changed the title prevent-abbreviations: fix optional parameter prevent-abbreviations: Fix optional parameter Jun 2, 2020
@sindresorhus sindresorhus merged commit 7781c3f into sindresorhus:master Jun 2, 2020
@fisker fisker deleted the issue-763 branch June 3, 2020 02:39
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.

prevent-abbreviations removes optional declaration of TypeScript parameter
3 participants