-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update: Improves the prefer-object-spread rule by removing extraneous visitors #10351
Update: Improves the prefer-object-spread rule by removing extraneous visitors #10351
Conversation
e7f2d53
to
8138e9d
Compare
This might collide a bit with #10347 |
it looks like I can't use object spreads in my test files: will update. |
8138e9d
to
0cbd995
Compare
const nativeObjectOverride = declarationNames.filter(identifier => identifier.name === "Object"); | ||
return childScope.type === "module" && varNamedObject.length; | ||
}); | ||
const references = [].concat(...objectVariable.map(variable => variable.references || [])); |
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.
Why do you use [].concat
instead of using the mapped array directly?
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.
references
is an array so the mapped array is a 2-dimensional array with nested arrays of references
, the spread with .concat
flattens it.
lib/rules/prefer-object-spread.js
Outdated
function overwritingNativeObject(references) { | ||
let objectRedefined = false; | ||
|
||
references.forEach(ref => { |
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.
Maybe we can use Array.prototype.some
here.
750bd50
to
29f8642
Compare
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.
Just requesting two small changes.
Also: I think it would be okay to put parserOptions: { ecmaVersion: 2018, ecmaFeatures: { experimentalObjectRestSpread: true }, sourceType: "module" }
in the RuleTester constructor. I don't think there are any tests which need to use sourceType: "script"
.
lib/rules/prefer-object-spread.js
Outdated
} | ||
|
||
return node.type === "AssignmentExpression" || | ||
node.type === "VariableDeclarator" || |
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.
Would you mind indenting this line and the next line? (Not 100% sure why the indent rule isn't flagging this)
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.
oh weird, good catch
lib/rules/prefer-object-spread.js
Outdated
* @param {array} references - list of reference nodes | ||
* @returns {boolean} - true if node is has been overwritten by an assignment or import | ||
*/ | ||
function overwritingNativeObject(references) { |
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 function isn't checking specifically that native Object is being overwritten, right? It just checks that any of the references passed in are being written?
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.
yes it might be better to name it modifyingObjectReference
or something
@platinumazure the only test that will fail when using |
Also includes a check for whether the file is importing `Object` and skips warning if that is the case.
1933eb0
to
98f12cd
Compare
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.
Missed something from last round, sorry, but otherwise everything LGTM. Thanks!
function modifyingObjectReference(references) { | ||
return references.some(ref => ( | ||
ref.identifier && | ||
ref.identifier.parent && |
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.
Ack, I missed this one. Same indent issue here, would you mind indenting this line and the next?
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 seems like something an automated tool could catch - know of a good one? :-D
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.
ref: #8978
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.
I'm a little unclear on what the correct indentation is for this, how is this one supposed to be written? the function code is:
function modifyingObjectReference(references) {
return references.some(ref => (
ref.identifier &&
ref.identifier.parent &&
isModifier(ref.identifier.parent)
));
}
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.
Hmm. What I had in mind was:
function modifyingObjectReference(references) {
return references.some(ref => (
ref.identifier &&
ref.identifier.parent &&
isModifier(ref.identifier.parent)
));
}
Looking back, I don't mind this one as much since the lines are already indented from statement position, so nobody will confuse these as potentially multiple statements. If you think this is a reasonable change, please make it; but if not, I can wait for others to review and go with consensus.
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.
I've changed my mind-- I don't mind the indent "issue" and if we decide to enforce that in future, we can always run autofix on this codebase later. This LGTM, but I'd like another set of eyes on this.
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 is pretty elegant, nice work @sharmilajesupaul!
What is the purpose of this pull request? (put an "X" next to item)
[ ] Documentation update
[ ] Bug fix (template)
[ ] New rule (template)
[X] Changes an existing rule (template)
[ ] Add autofixing to a rule
[ ] Add a CLI option
[ ] Add something to the core
[ ] Other, please explain:
What changes did you make? (Give an overview)
Object
.context.getScope()
to determine isObject
is being overwritten, instead of visiting every assignment and variable declaration node.Is there anything you'd like reviewers to focus on?
There may be a better way to get module and scope information, not sure 馃
cc. @ljharb @ilyavolodin