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

Expand reporting for prefer-node-remove #507

Merged
10 changes: 6 additions & 4 deletions rules/prefer-node-append.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
const getDocumentationUrl = require('./utils/get-documentation-url');
const isValueNotUsable = require('./utils/is-value-not-usable');

const getMethodName = memberExpression => memberExpression.property.name;

const create = context => {
return {
CallExpression(node) {
'CallExpression[callee.property.name="appendChild"]'(node) {
const {callee} = node;

if (callee.type === 'MemberExpression' && getMethodName(callee) === 'appendChild') {
if (node.arguments.length !== 1) {
return;
}

if (callee.type === 'MemberExpression') {
const fix = isValueNotUsable(node) ? fixer => fixer.replaceText(callee.property, 'append') : undefined;

context.report({
Expand Down
33 changes: 10 additions & 23 deletions rules/prefer-node-remove.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,6 @@
const getDocumentationUrl = require('./utils/get-documentation-url');
const isValueNotUsable = require('./utils/is-value-not-usable');

const getMethodName = callee => {
const {property} = callee;

if (property.type === 'Identifier') {
return property.name;
}

return null;
};

const getCallerName = callee => {
const {object} = callee;

Expand Down Expand Up @@ -46,31 +36,28 @@ const getArgumentName = arguments_ => {

const create = context => {
return {
CallExpression(node) {
'CallExpression[callee.property.name="removeChild"]'(node) {
const {callee} = node;

if (node.arguments.length === 0 ||
if (node.arguments.length !== 1 ||
callee.type !== 'MemberExpression' ||
fisker marked this conversation as resolved.
Show resolved Hide resolved
callee.computed
fisker marked this conversation as resolved.
Show resolved Hide resolved
) {
return;
}

const methodName = getMethodName(callee);
const callerName = getCallerName(callee);

if (methodName === 'removeChild') {
const argumentName = getArgumentName(node.arguments);
const argumentName = getArgumentName(node.arguments);

if (argumentName) {
const fix = isValueNotUsable(node) ? fixer => fixer.replaceText(node, `${argumentName}.remove()`) : undefined;
if (argumentName) {
const fix = isValueNotUsable(node) ? fixer => fixer.replaceText(node, `${argumentName}.remove()`) : undefined;
fisker marked this conversation as resolved.
Show resolved Hide resolved

context.report({
node,
message: `Prefer \`${argumentName}.remove()\` over \`${callerName}.removeChild(${argumentName})\`.`,
fix
});
}
context.report({
node,
message: `Prefer \`${argumentName}.remove()\` over \`${callerName}.removeChild(${argumentName})\`.`,
fix
});
}
}
};
Expand Down
9 changes: 3 additions & 6 deletions test/prefer-node-append.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ ruleTester.run('prefer-node-append', rule, {
'parent.append(child);',
'document.body.append(child, \'text\');',
'node.append()',
'node.append(null)'
'node.append(null)',
'node.appendChild(child, child2)',
'node.appendChild()'
],
invalid: [
{
Expand All @@ -31,11 +33,6 @@ ruleTester.run('prefer-node-append', rule, {
output: 'document.body.append(child);',
errors: [error]
},
{
code: 'node.appendChild()',
output: 'node.append()',
errors: [error]
},
{
code: 'node.appendChild(null)',
output: 'node.append(null)',
Expand Down
3 changes: 2 additions & 1 deletion test/prefer-node-remove.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ ruleTester.run('prefer-node-remove', rule, {
'foo.parentNode.removeChild(\'bar\')',
'foo.parentNode[\'bar\'](foo)',
'foo.parentNode[removeChild](foo)',
'foo.parentNode.removeChild()'
'foo.parentNode.removeChild()',
'foo.parentNode.removeChild(bar, baz)'
],
invalid: [
invalidTestCase(
Expand Down