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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion rules/utils/rename-identifier.js
Expand Up @@ -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?

}

return fixer.replaceText(identifier, name);
Expand Down
9 changes: 8 additions & 1 deletion test/prevent-abbreviations.js
Expand Up @@ -1720,6 +1720,13 @@ typescriptRuleTester.run('prevent-abbreviations', rule, {
code,
output: code.replace('prop', 'property'),
errors: createErrors()
}))
})),

// #763
{
code: 'const foo = (extraParams?: string) => {}',
output: 'const foo = (extraParameters?: string) => {}',
errors: createErrors()
}
]
});