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鈥檒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

Merged

Conversation

sharmilajesupaul
Copy link
Contributor

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)

  • Includes a change to stop warning if the if the file is importing Object.
  • Uses context.getScope() to determine is Object is being overwritten, instead of visiting every assignment and variable declaration node.
  • Adds more tests

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

@eslint-deprecated eslint-deprecated bot added the triage An ESLint team member will look at this issue soon label May 14, 2018
@sharmilajesupaul sharmilajesupaul force-pushed the shar-update-prefer-object-spread branch 2 times, most recently from e7f2d53 to 8138e9d Compare May 14, 2018 17:41
@ljharb
Copy link
Sponsor Contributor

ljharb commented May 14, 2018

This might collide a bit with #10347

@sharmilajesupaul
Copy link
Contributor Author

sharmilajesupaul commented May 14, 2018

it looks like I can't use object spreads in my test files:
https://github.com/eslint/eslint/pull/10351/files#diff-6088f3cff25518533d89b2e8a4d395faR57

will update.

@sharmilajesupaul sharmilajesupaul force-pushed the shar-update-prefer-object-spread branch from 8138e9d to 0cbd995 Compare May 14, 2018 19:25
const nativeObjectOverride = declarationNames.filter(identifier => identifier.name === "Object");
return childScope.type === "module" && varNamedObject.length;
});
const references = [].concat(...objectVariable.map(variable => variable.references || []));
Copy link
Member

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?

Copy link
Contributor Author

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.

function overwritingNativeObject(references) {
let objectRedefined = false;

references.forEach(ref => {
Copy link
Member

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.

@sharmilajesupaul sharmilajesupaul force-pushed the shar-update-prefer-object-spread branch 2 times, most recently from 750bd50 to 29f8642 Compare May 17, 2018 17:08
@platinumazure platinumazure added enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion chore This change is not user-facing and removed triage An ESLint team member will look at this issue soon labels May 22, 2018
Copy link
Member

@platinumazure platinumazure left a 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".

}

return node.type === "AssignmentExpression" ||
node.type === "VariableDeclarator" ||
Copy link
Member

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)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh weird, good catch

* @param {array} references - list of reference nodes
* @returns {boolean} - true if node is has been overwritten by an assignment or import
*/
function overwritingNativeObject(references) {
Copy link
Member

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?

Copy link
Contributor Author

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

@sharmilajesupaul
Copy link
Contributor Author

@platinumazure the only test that will fail when using sourceType: module is the test using an html comment. But I can add parser options specifically for that test instead of having to add them for every test using a module.

Also includes a check for whether the file is importing `Object` and skips warning if that is the case.
@sharmilajesupaul sharmilajesupaul force-pushed the shar-update-prefer-object-spread branch from 1933eb0 to 98f12cd Compare May 23, 2018 00:49
Copy link
Member

@platinumazure platinumazure left a 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 &&
Copy link
Member

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?

Copy link
Sponsor Contributor

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

Copy link
Member

Choose a reason for hiding this comment

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

ref: #8978

Copy link
Contributor Author

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)
    ));
}

Copy link
Member

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.

Copy link
Member

@platinumazure platinumazure left a 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.

@platinumazure platinumazure added accepted There is consensus among the team that this change meets the criteria for inclusion and removed enhancement This change enhances an existing feature of ESLint evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion labels May 28, 2018
Copy link
Member

@btmills btmills left a 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!

@btmills btmills merged commit c2e0398 into eslint:master May 28, 2018
@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Nov 26, 2018
@eslint-deprecated eslint-deprecated bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Nov 26, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion chore This change is not user-facing rule Relates to ESLint's core rules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants