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

Add prefer-modern-dom-apis rule #362

Merged
merged 14 commits into from Dec 22, 2019
Merged

Add prefer-modern-dom-apis rule #362

merged 14 commits into from Dec 22, 2019

Conversation

mcmxcdev
Copy link
Contributor

@mcmxcdev mcmxcdev commented Sep 9, 2019

Fixes #344.

The rule can be tested in this gist here: https://astexplorer.net/#/gist/338d492213d95b40122046fb29abd1bc/bb5030be4689f651524402efad8d24d2b310d588

The links in the description for prepend() and append() from MDN are returning 404 by the way, they should be updated.


IssueHunt Summary

Referenced issues

This pull request has been submitted to:


IssueHunt has been backed by the following sponsors. Become a sponsor

Signed-off-by: mhatvan <markus_hatvan@aon.at>
readme.md Outdated Show resolved Hide resolved
}

return null;
};
Copy link
Contributor

Choose a reason for hiding this comment

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

Move this helper function out of checkForReplaceChildOrInsertBefore. There may be a suitable util here: https://github.com/sindresorhus/eslint-plugin-unicorn/tree/master/rules/utils. Perhaps is-method-named?

Also, args is not a very good argument name. Perhaps nodeArgument?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

is-method-named would not help me, since i cannot say what the argument name will be.
There are multiple other rules with const args = node.arguments; and their own scoped helper functions, e.g: prefer-node-remove in the code already, but I am also fine with renaming it ;)

rules/prefer-modern-dom-apis.js Outdated Show resolved Hide resolved
rules/prefer-modern-dom-apis.js Outdated Show resolved Hide resolved
}

return null;
};
Copy link
Contributor

Choose a reason for hiding this comment

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

Same comment as before. This should be moved to the parent scope.

rules/prefer-modern-dom-apis.js Outdated Show resolved Hide resolved
rules/prefer-modern-dom-apis.js Show resolved Hide resolved
CallExpression(node) {
if (
node.callee.type === 'MemberExpression' &&
node.arguments.length === 2
Copy link
Contributor

@MrHen MrHen Sep 9, 2019

Choose a reason for hiding this comment

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

You can include these checks in the selector pattern for the rule. Not required for the PR but in case you hadn't seen it yet, here are the docs: https://eslint.org/docs/developer-guide/selectors

test/prefer-modern-dom-apis.js Show resolved Hide resolved
added additional test cases

Signed-off-by: mhatvan <markus_hatvan@aon.at>
@mcmxcdev
Copy link
Contributor Author

@MrHen I pushed some code adaptions in the newest commit.

@MrHen MrHen self-requested a review September 13, 2019 14:33
@MrHen MrHen self-assigned this Sep 13, 2019
@mcmxcdev
Copy link
Contributor Author

@MrHen can you take another look at the changes you requested? I implemented your recommendations a while ago.

@MrHen MrHen removed their assignment Oct 16, 2019
@MrHen
Copy link
Contributor

MrHen commented Oct 16, 2019

@mhatvan Sorry, I'm actually in the middle of changing employers right now and am transitioning a ton of stuff across teams. I probably won't get a chance to look at it in the next couple weeks.

readme.md Show resolved Hide resolved
readme.md Outdated Show resolved Hide resolved
index.js Outdated Show resolved Hide resolved
rules/prefer-modern-dom-apis.js Outdated Show resolved Hide resolved
@fisker
Copy link
Collaborator

fisker commented Oct 16, 2019

since childNode.replaceWith() and Node.replaceChild() returns different value

also ChildNode.before()

ChildNode.before()
Node.insertBefore()
Element.insertAdjacentElement()

so, maybe you want do a similar check like #321

mhatvan added 3 commits October 21, 2019 21:50
* Change getDocsUrl to getDocumentationUrl

* Change formatting of plugins property in index.js

Signed-off-by: mhatvan <markus_hatvan@aon.at>
…rule

Signed-off-by: mhatvan <markus_hatvan@aon.at>
@mcmxcdev
Copy link
Contributor Author

@fisker I pushed some code adaptions in the newest commit.

@fisker
Copy link
Collaborator

fisker commented Oct 22, 2019

what about this?

since childNode.replaceWith() and Node.replaceChild() returns different value

also ChildNode.before()

ChildNode.before()
Node.insertBefore()
Element.insertAdjacentElement()

so, maybe you want do a similar check like #321

@mcmxcdev
Copy link
Contributor Author

mcmxcdev commented Oct 22, 2019

so, maybe you want do a similar check like #321

you linked the complete PR without specifying what you exactly want me to change, I need more details here.

@MrHen MrHen removed their request for review October 29, 2019 01:35
@fisker
Copy link
Collaborator

fisker commented Oct 29, 2019

you linked the complete PR without specifying what you exactly want me to change, I need more details here.

@mhatvan I mean some methods returns different result, so we need check if the return value is used.

docs/rules/prefer-modern-dom-apis.md Outdated Show resolved Hide resolved
readme.md Outdated Show resolved Hide resolved
* change fenced code block language in readme.md from bash to console

Signed-off-by: mhatvan <markus_hatvan@aon.at>
@mcmxcdev
Copy link
Contributor Author

@sindresorhus I implemented the changes you requested, can we get this merged now?

I still don't understand @fisker feedback:

childNode.replaceWith() and Node.replaceChild() returns different value

I implemented it according to the examples in MDN and they return the expected result.

@sindresorhus
Copy link
Owner

@mhatvan Node.replaceChild() returns a value. If the user actually uses this value, we cannot safely auto-fix with with ChildNode.replaceWith() as it doesn't return any value. Can you handle this and add a test for that?

sindresorhus and others added 5 commits November 14, 2019 12:54
…Before() and .insertAdjacentElement() methods

* add additional test cases for .replaceWith(), .insertBefore() and .insertAdjacentElement() methods

Signed-off-by: mhatvan <markus_hatvan@aon.at>
@mcmxcdev
Copy link
Contributor Author

mcmxcdev commented Dec 8, 2019

@sindresorhus Sorry, I missed that you already left a comment a while ago. I pushed another commit which implements the requested changes and added additional tests.

@fisker
Copy link
Collaborator

fisker commented Dec 8, 2019

@mhatvan I think we should still report an error, just can't fix

@sindresorhus
Copy link
Owner

@mhatvan I think we should still report an error, just can't fix

👍

… methods to be reported but no autofix option when part of variable assignment

* update test cases to verify changed rule logic

Signed-off-by: mhatvan <markus_hatvan@aon.at>
@mcmxcdev
Copy link
Contributor Author

@sindresorhus @fisker feedback implemented, I pushed another commit ;)

@sindresorhus sindresorhus merged commit 44d14b9 into sindresorhus:master Dec 22, 2019
@sindresorhus
Copy link
Owner

Looks good. Thanks for contributing 👌

@fregante
Copy link
Collaborator

Awesome!

Comment on lines +15 to +21
const isPartOfVariableAssignment = nodeParentType => {
if (nodeParentType === 'VariableDeclarator' || nodeParentType === 'AssignmentExpression') {
return true;
}

return false;
};
Copy link
Collaborator

@fisker fisker Dec 23, 2019

Choose a reason for hiding this comment

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

I don't think this is enough,

simple one:

console.log(referenceNode.insertAdjacentElement("beforebegin", newNode));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rule proposal: prefer-modern-dom-apis
5 participants