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

chore: add eslint-plugin-eslint-plugin #59

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
const globals = require('./index').environments.globals.globals;

module.exports = {
extends: ['eslint:recommended', 'prettier'],
plugins: ['prettier'],
extends: [
'eslint:recommended',
'plugin:eslint-plugin/recommended',
'prettier',
],
plugins: ['eslint-plugin', 'prettier'],
parserOptions: {
ecmaVersion: 2017,
},
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"@commitlint/config-conventional": "^6.0.2",
"eslint": "^4.10.0",
"eslint-config-prettier": "^2.7.0",
"eslint-plugin-eslint-plugin": "^1.4.0",
"eslint-plugin-prettier": "^2.3.1",
"husky": "^0.14.3",
"jest": "^22.0.4",
Expand Down
53 changes: 29 additions & 24 deletions rules/prefer_to_be_null.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,36 @@ const expectNotToBeCase = require('./util').expectNotToBeCase;
const method = require('./util').method;
const method2 = require('./util').method2;

module.exports = context => {
return {
CallExpression(node) {
const is = expectToBeCase(node, null) || expectToEqualCase(node, null);
const isNot =
expectNotToEqualCase(node, null) || expectNotToBeCase(node, null);
module.exports = {
meta: {
fixable: 'code',
Copy link
Member

Choose a reason for hiding this comment

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

Wee, awesome! This is a change I wanted to make, nice that this yells at us

Copy link
Member

Choose a reason for hiding this comment

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

should probably make all of the rules be an object with a create function, even if they don't have any other properties

},
create(context) {
return {
CallExpression(node) {
const is = expectToBeCase(node, null) || expectToEqualCase(node, null);
const isNot =
expectNotToEqualCase(node, null) || expectNotToBeCase(node, null);

if (is || isNot) {
context.report({
fix(fixer) {
if (is) {
if (is || isNot) {
context.report({
fix(fixer) {
if (is) {
return [
fixer.replaceText(method(node), 'toBeNull'),
fixer.remove(argument(node)),
];
}
return [
fixer.replaceText(method(node), 'toBeNull'),
fixer.remove(argument(node)),
fixer.replaceText(method2(node), 'toBeNull'),
fixer.remove(argument2(node)),
];
}
return [
fixer.replaceText(method2(node), 'toBeNull'),
fixer.remove(argument2(node)),
];
},
message: 'Use toBeNull() instead',
node: is ? method(node) : method2(node),
});
}
},
};
},
message: 'Use toBeNull() instead',
node: is ? method(node) : method2(node),
});
}
},
};
},
};
57 changes: 31 additions & 26 deletions rules/prefer_to_be_undefined.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,38 @@ const expectNotToEqualCase = require('./util').expectNotToEqualCase;
const method = require('./util').method;
const method2 = require('./util').method2;

module.exports = context => {
return {
CallExpression(node) {
const is =
expectToBeCase(node, undefined) || expectToEqualCase(node, undefined);
const isNot =
expectNotToEqualCase(node, undefined) ||
expectNotToBeCase(node, undefined);
module.exports = {
meta: {
fixable: 'code',
},
create(context) {
return {
CallExpression(node) {
const is =
expectToBeCase(node, undefined) || expectToEqualCase(node, undefined);
const isNot =
expectNotToEqualCase(node, undefined) ||
expectNotToBeCase(node, undefined);

if (is || isNot) {
context.report({
fix(fixer) {
if (is) {
if (is || isNot) {
context.report({
fix(fixer) {
if (is) {
return [
fixer.replaceText(method(node), 'toBeUndefined'),
fixer.remove(argument(node)),
];
}
return [
fixer.replaceText(method(node), 'toBeUndefined'),
fixer.remove(argument(node)),
fixer.replaceText(method2(node), 'toBeUndefined'),
fixer.remove(argument2(node)),
];
}
return [
fixer.replaceText(method2(node), 'toBeUndefined'),
fixer.remove(argument2(node)),
];
},
message: 'Use toBeUndefined() instead',
node: is ? method(node) : method2(node),
});
}
},
};
},
message: 'Use toBeUndefined() instead',
node: is ? method(node) : method2(node),
});
}
},
};
},
};
75 changes: 40 additions & 35 deletions rules/prefer_to_have_length.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,44 @@ const expectResolveCase = require('./util').expectResolveCase;
const expectRejectCase = require('./util').expectRejectCase;
const method = require('./util').method;

module.exports = context => {
return {
CallExpression(node) {
if (
!(
expectNotCase(node) ||
expectResolveCase(node) ||
expectRejectCase(node)
) &&
expectCase(node) &&
(method(node).name === 'toBe' || method(node).name === 'toEqual') &&
node.arguments[0].property &&
node.arguments[0].property.name === 'length'
) {
const propertyDot = context
.getSourceCode()
.getFirstTokenBetween(
node.arguments[0].object,
node.arguments[0].property,
token => token.value === '.'
);
context.report({
fix(fixer) {
return [
fixer.remove(propertyDot),
fixer.remove(node.arguments[0].property),
fixer.replaceText(method(node), 'toHaveLength'),
];
},
message: 'Use toHaveLength() instead',
node: method(node),
});
}
},
};
module.exports = {
meta: {
fixable: 'code',
},
create(context) {
return {
CallExpression(node) {
if (
!(
expectNotCase(node) ||
expectResolveCase(node) ||
expectRejectCase(node)
) &&
expectCase(node) &&
(method(node).name === 'toBe' || method(node).name === 'toEqual') &&
node.arguments[0].property &&
node.arguments[0].property.name === 'length'
) {
const propertyDot = context
.getSourceCode()
.getFirstTokenBetween(
node.arguments[0].object,
node.arguments[0].property,
token => token.value === '.'
);
context.report({
fix(fixer) {
return [
fixer.remove(propertyDot),
fixer.remove(node.arguments[0].property),
fixer.replaceText(method(node), 'toHaveLength'),
];
},
message: 'Use toHaveLength() instead',
node: method(node),
});
}
},
};
},
};
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1587,6 +1587,10 @@ eslint-config-prettier@^2.7.0:
dependencies:
get-stdin "^5.0.1"

eslint-plugin-eslint-plugin@^1.4.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/eslint-plugin-eslint-plugin/-/eslint-plugin-eslint-plugin-1.4.0.tgz#382dc6692a741a52072d5f3fff5dacdcf291ef49"

eslint-plugin-prettier@^2.3.1:
version "2.4.0"
resolved "https://registry.yarnpkg.com/eslint-plugin-prettier/-/eslint-plugin-prettier-2.4.0.tgz#85cab0775c6d5e3344ef01e78d960f166fb93aae"
Expand Down