Skip to content

Add useOptionalChaining option (default false) to no-get rule #845

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

Merged
merged 1 commit into from
Jun 14, 2020
Merged

Add useOptionalChaining option (default false) to no-get rule #845

merged 1 commit into from
Jun 14, 2020

Conversation

bmish
Copy link
Member

@bmish bmish commented Jun 12, 2020

Normally we cannot autofix usages with nested paths, but we can with this new option enabled.

Before autofix:

this.get('account.id')

After autofix:

this.account?.id

Fixes #843.

CC: @fivetanley @mongoose700

@bmish bmish requested review from rwjblue and Turbo87 June 12, 2020 19:55
Copy link
Collaborator

@mongoose700 mongoose700 left a comment

Choose a reason for hiding this comment

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

Looks good! I confirmed for myself that both get and optional chaining handle null and undefined the same way (I hadn't realized they both always use undefined).

`this.${property}`,
useOptionalChaining ? `this.${property.replace(/\./g, '?.')}` : undefined,
]
.filter((item) => Boolean(item))
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: .filter(Boolean)

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed.

return isImportedGet
? `Use \`this.${property}\` instead of \`get(this, '${property}')\``
: `Use \`this.${property}\` instead of \`this.get('${property}')\``;
function makeErrorMessageForGet(property, isImportedGet, useOptionalChaining) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you change this to makeErrorMessageForGet(property, { isImportedGet, useOptionalChaining })? Passing in multiple unnamed boolean parameters can become tricky.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good idea, fixed.

// Not safe to autofix nested properties because some properties in the path might be null.
return null;
}

if (!isValidJSVariableName(path)) {
if (!isValidJSPath(path)) {
// Do not autofix since the path would not be a valid JS variable name.
Copy link
Collaborator

Choose a reason for hiding this comment

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

This comment should also replace variable name with path.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed.

fix(fixer) {
if (path.includes('.')) {
if (path.includes('.') && !useOptionalChaining) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This fix for both paths seems to be identical. Should it be extracted?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point, extracted.

@bmish bmish merged commit 042c6c7 into ember-cli:master Jun 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

support optional chaining for ember/no-get
2 participants