Skip to content

Commit

Permalink
Merge pull request #99 from brettz9/meta-type
Browse files Browse the repository at this point in the history
Add `meta.type`
  • Loading branch information
Turbo87 authored Jun 24, 2020
2 parents ea76565 + b6f02a8 commit c7e9349
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 143 deletions.
55 changes: 30 additions & 25 deletions lib/rules/missing-assertion.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,35 @@
'use strict';

module.exports = function(context) {
function checkCallee (callee, node) {
if (callee.type === 'Identifier' && callee.name === 'expect') {
context.report({
node,
message: 'expect(...) used without assertion'
})
}
}
return {
ReturnStatement(node) {
if (
node.argument && node.argument.type === 'CallExpression' &&
node.argument.callee
) {
checkCallee(node.argument.callee, node.argument);
module.exports = {
meta: {
type: 'problem'
},
create (context) {
function checkCallee (callee, node) {
if (callee.type === 'Identifier' && callee.name === 'expect') {
context.report({
node,
message: 'expect(...) used without assertion'
})
}
},
ExpressionStatement(node) {
let {expression} = node;
if (!expression || expression.type !== 'CallExpression')
return;

let {callee} = expression;
checkCallee(callee, node);
}
};
return {
ReturnStatement(node) {
if (
node.argument && node.argument.type === 'CallExpression' &&
node.argument.callee
) {
checkCallee(node.argument.callee, node.argument);
}
},
ExpressionStatement(node) {
let {expression} = node;
if (!expression || expression.type !== 'CallExpression')
return;

let {callee} = expression;
checkCallee(callee, node);
}
};
}
};
73 changes: 39 additions & 34 deletions lib/rules/no-inner-compare.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,43 +13,48 @@ const HINTS = {
'<=': 'to.be.at.most()'
};

module.exports = function(context) {
let checkMemberOrCallExpression = (expression) => {
if (expression.type !== 'MemberExpression' && expression.type !== 'CallExpression')
return;
module.exports = {
meta: {
type: 'suggestion'
},
create(context) {
let checkMemberOrCallExpression = (expression) => {
if (expression.type !== 'MemberExpression' && expression.type !== 'CallExpression')
return;

let expect = findExpectCall(expression);
if (!expect)
return;
let expect = findExpectCall(expression);
if (!expect)
return;

let args = expect.arguments;
let [firstArgument] = args;
if (!firstArgument || firstArgument.type !== 'BinaryExpression')
return;
let args = expect.arguments;
let [firstArgument] = args;
if (!firstArgument || firstArgument.type !== 'BinaryExpression')
return;

let hint = HINTS[firstArgument.operator];
if (!hint)
return;
let hint = HINTS[firstArgument.operator];
if (!hint)
return;

context.report({
node: firstArgument,
message: `operator "${firstArgument.operator}" used in expect(), use "${hint}" instead`
});
};
return {
ReturnStatement(node) {
if (
node.argument && node.argument.type === 'CallExpression' &&
node.argument.callee
) {
checkMemberOrCallExpression(node.argument);
} else if (node.argument && node.argument.type === 'MemberExpression') {
checkMemberOrCallExpression(node.argument);
context.report({
node: firstArgument,
message: `operator "${firstArgument.operator}" used in expect(), use "${hint}" instead`
});
};
return {
ReturnStatement(node) {
if (
node.argument && node.argument.type === 'CallExpression' &&
node.argument.callee
) {
checkMemberOrCallExpression(node.argument);
} else if (node.argument && node.argument.type === 'MemberExpression') {
checkMemberOrCallExpression(node.argument);
}
},
ExpressionStatement(node) {
let {expression} = node;
checkMemberOrCallExpression(expression);
}
},
ExpressionStatement(node) {
let {expression} = node;
checkMemberOrCallExpression(expression);
}
};
};
}
};
101 changes: 53 additions & 48 deletions lib/rules/no-inner-literal.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,63 @@

const findExpectCall = require('../util/find-expect-call');

module.exports = function(context) {
function checkMemberOrCallExpression (expression) {
if (expression.type !== 'MemberExpression' && expression.type !== 'CallExpression')
return;
module.exports = {
meta: {
type: 'problem'
},
create (context) {
function checkMemberOrCallExpression (expression) {
if (expression.type !== 'MemberExpression' && expression.type !== 'CallExpression')
return;

let expect = findExpectCall(expression);
if (!expect)
return;
let expect = findExpectCall(expression);
if (!expect)
return;

let args = expect.arguments;
let [firstArgument] = args;
if (!firstArgument)
return;
let args = expect.arguments;
let [firstArgument] = args;
if (!firstArgument)
return;

let value;
if (firstArgument.type === 'Literal' || firstArgument.type === 'BigIntLiteral') {
value = firstArgument.raw;
} else if (firstArgument.type === 'Identifier' && [
'undefined', 'NaN', 'Infinity'
].includes(firstArgument.name)) {
value = firstArgument.name;
} else if (firstArgument.type === 'ThisExpression') {
value = 'this';
} else if (
firstArgument.type === 'UnaryExpression' &&
firstArgument.argument.type === 'Identifier' &&
firstArgument.argument.name === 'Infinity'
) {
value = `${firstArgument.operator}Infinity`;
} else {
return;
}

context.report({
node: firstArgument,
message: `\`${value}\` used in expect()`
});
}
return {
ReturnStatement(node) {
if (
node.argument && node.argument.type === 'CallExpression' &&
node.argument.callee
let value;
if (firstArgument.type === 'Literal' || firstArgument.type === 'BigIntLiteral') {
value = firstArgument.raw;
} else if (firstArgument.type === 'Identifier' && [
'undefined', 'NaN', 'Infinity'
].includes(firstArgument.name)) {
value = firstArgument.name;
} else if (firstArgument.type === 'ThisExpression') {
value = 'this';
} else if (
firstArgument.type === 'UnaryExpression' &&
firstArgument.argument.type === 'Identifier' &&
firstArgument.argument.name === 'Infinity'
) {
checkMemberOrCallExpression(node.argument);
} else if (node.argument && node.argument.type === 'MemberExpression') {
checkMemberOrCallExpression(node.argument);
value = `${firstArgument.operator}Infinity`;
} else {
return;
}
},
ExpressionStatement(node) {
let {expression} = node;
checkMemberOrCallExpression(expression);

context.report({
node: firstArgument,
message: `\`${value}\` used in expect()`
});
}
};
return {
ReturnStatement(node) {
if (
node.argument && node.argument.type === 'CallExpression' &&
node.argument.callee
) {
checkMemberOrCallExpression(node.argument);
} else if (node.argument && node.argument.type === 'MemberExpression') {
checkMemberOrCallExpression(node.argument);
}
},
ExpressionStatement(node) {
let {expression} = node;
checkMemberOrCallExpression(expression);
}
};
}
};
77 changes: 41 additions & 36 deletions lib/rules/terminating-properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,49 +13,54 @@ const PROPERTY_TERMINATORS = [
'arguments'
];

module.exports = function(context) {
let options = context.options[0] || {};
let additionalTerminators = options.properties || [];
let validTerminators = [...PROPERTY_TERMINATORS, ...additionalTerminators];
module.exports = {
meta: {
type: 'problem'
},
create (context) {
let options = context.options[0] || {};
let additionalTerminators = options.properties || [];
let validTerminators = [...PROPERTY_TERMINATORS, ...additionalTerminators];

function checkUseAsFunction (expression) {
if (expression.type !== 'CallExpression')
return;
function checkUseAsFunction (expression) {
if (expression.type !== 'CallExpression')
return;

let {callee} = expression;
if (callee.type !== 'MemberExpression')
return;
let {callee} = expression;
if (callee.type !== 'MemberExpression')
return;

let {property} = callee;
if (property.type !== 'Identifier' || !validTerminators.includes(property.name))
return;
let {property} = callee;
if (property.type !== 'Identifier' || !validTerminators.includes(property.name))
return;

let expectCall = findExpectCall(callee.object);
if (!expectCall)
return;
let expectCall = findExpectCall(callee.object);
if (!expectCall)
return;

let source = context.getSourceCode();
let source = context.getSourceCode();

let calleeText = source.getText(callee);
let expectText = source.getText(expectCall);
let assertionText = calleeText.substr(expectText.length + 1);
let calleeText = source.getText(callee);
let expectText = source.getText(expectCall);
let assertionText = calleeText.substr(expectText.length + 1);

context.report({
node: property,
message: `"${assertionText}" used as function`
});
}
context.report({
node: property,
message: `"${assertionText}" used as function`
});
}

return {
ReturnStatement(node) {
let {argument} = node;
if (argument) {
checkUseAsFunction(argument);
return {
ReturnStatement(node) {
let {argument} = node;
if (argument) {
checkUseAsFunction(argument);
}
},
ExpressionStatement (node) {
let {expression} = node;
checkUseAsFunction(expression);
}
},
ExpressionStatement (node) {
let {expression} = node;
checkUseAsFunction(expression);
}
};
};
}
};

0 comments on commit c7e9349

Please sign in to comment.