-
-
Notifications
You must be signed in to change notification settings - Fork 205
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
Conversation
There was a problem hiding this 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
).
lib/rules/no-get.js
Outdated
`this.${property}`, | ||
useOptionalChaining ? `this.${property.replace(/\./g, '?.')}` : undefined, | ||
] | ||
.filter((item) => Boolean(item)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: .filter(Boolean)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
lib/rules/no-get.js
Outdated
return isImportedGet | ||
? `Use \`this.${property}\` instead of \`get(this, '${property}')\`` | ||
: `Use \`this.${property}\` instead of \`this.get('${property}')\``; | ||
function makeErrorMessageForGet(property, isImportedGet, useOptionalChaining) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea, fixed.
lib/rules/no-get.js
Outdated
// 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. |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
lib/rules/no-get.js
Outdated
fix(fixer) { | ||
if (path.includes('.')) { | ||
if (path.includes('.') && !useOptionalChaining) { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, extracted.
Normally we cannot autofix usages with nested paths, but we can with this new option enabled.
Before autofix:
After autofix:
Fixes #843.
CC: @fivetanley @mongoose700