Skip to content

Commit

Permalink
fix: include meta.docs.url for all rules
Browse files Browse the repository at this point in the history
  • Loading branch information
macklinu authored and SimenB committed Feb 9, 2018
1 parent 5e5ceba commit 824c2a3
Show file tree
Hide file tree
Showing 12 changed files with 345 additions and 270 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module.exports = {
},
rules: {
strict: 'error',
'eslint-plugin/require-meta-docs-url': 'error',
'prettier/prettier': 'error',
},
overrides: [
Expand Down
2 changes: 1 addition & 1 deletion rules/__tests__/no_large_snapshots.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const noLargeSnapshots = require('../no_large_snapshots');
const noLargeSnapshots = require('../no_large_snapshots').create;

// was not able to use https://eslint.org/docs/developer-guide/nodejs-api#ruletester for these because there is no way to configure RuleTester to run non .js files
describe('no-large-snapshots', () => {
Expand Down
132 changes: 70 additions & 62 deletions rules/no_disabled_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,77 +17,85 @@ function getName(node) {
return null;
}

module.exports = context => {
let suiteDepth = 0;
let testDepth = 0;
module.exports = {
meta: {
docs: {
url:
'https://github.com/jest-community/eslint-plugin-jest/blob/master/docs/rules/no-disabled-tests.md',
},
},
create(context) {
let suiteDepth = 0;
let testDepth = 0;

return {
CallExpression: node => {
const functionName = getName(node.callee);
return {
CallExpression: node => {
const functionName = getName(node.callee);

switch (functionName) {
case 'describe':
suiteDepth++;
break;
switch (functionName) {
case 'describe':
suiteDepth++;
break;

case 'describe.skip':
context.report({ message: 'Skipped test suite', node });
break;
case 'describe.skip':
context.report({ message: 'Skipped test suite', node });
break;

case 'it':
case 'test':
testDepth++;
if (node.arguments.length < 2) {
context.report({
message: 'Test is missing function argument',
node,
});
}
break;
case 'it':
case 'test':
testDepth++;
if (node.arguments.length < 2) {
context.report({
message: 'Test is missing function argument',
node,
});
}
break;

case 'it.skip':
case 'test.skip':
context.report({ message: 'Skipped test', node });
break;
case 'it.skip':
case 'test.skip':
context.report({ message: 'Skipped test', node });
break;

case 'pending':
if (testDepth > 0) {
context.report({
message: 'Call to pending() within test',
node,
});
} else if (suiteDepth > 0) {
context.report({
message: 'Call to pending() within test suite',
node,
});
}
break;
case 'pending':
if (testDepth > 0) {
context.report({
message: 'Call to pending() within test',
node,
});
} else if (suiteDepth > 0) {
context.report({
message: 'Call to pending() within test suite',
node,
});
}
break;

case 'xdescribe':
context.report({ message: 'Disabled test suite', node });
break;
case 'xdescribe':
context.report({ message: 'Disabled test suite', node });
break;

case 'xit':
case 'xtest':
context.report({ message: 'Disabled test', node });
break;
}
},
case 'xit':
case 'xtest':
context.report({ message: 'Disabled test', node });
break;
}
},

'CallExpression:exit': node => {
const functionName = getName(node.callee);
'CallExpression:exit': node => {
const functionName = getName(node.callee);

switch (functionName) {
case 'describe':
suiteDepth--;
break;
switch (functionName) {
case 'describe':
suiteDepth--;
break;

case 'it':
case 'test':
testDepth--;
break;
}
},
};
case 'it':
case 'test':
testDepth--;
break;
}
},
};
},
};
60 changes: 34 additions & 26 deletions rules/no_focused_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,38 @@ const isPropertyNamedOnly = property =>
const isCallToTestOnlyFunction = callee =>
matchesTestFunction(callee.object) && isPropertyNamedOnly(callee.property);

module.exports = context => ({
CallExpression(node) {
const callee = node.callee;
if (!callee) {
return;
}

if (
callee.type === 'MemberExpression' &&
isCallToTestOnlyFunction(callee)
) {
context.report({
message: 'Unexpected focused test.',
node: callee.property,
});
return;
}

if (callee.type === 'Identifier' && isCallToFocusedTestFunction(callee)) {
context.report({
message: 'Unexpected focused test.',
node: callee,
});
return;
}
module.exports = {
meta: {
docs: {
url:
'https://github.com/jest-community/eslint-plugin-jest/blob/master/docs/rules/no-focused-tests.md',
},
},
});
create: context => ({
CallExpression(node) {
const callee = node.callee;
if (!callee) {
return;
}

if (
callee.type === 'MemberExpression' &&
isCallToTestOnlyFunction(callee)
) {
context.report({
message: 'Unexpected focused test.',
node: callee.property,
});
return;
}

if (callee.type === 'Identifier' && isCallToFocusedTestFunction(callee)) {
context.report({
message: 'Unexpected focused test.',
node: callee,
});
return;
}
},
}),
};
55 changes: 34 additions & 21 deletions rules/no_identical_title.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,26 +70,39 @@ const handleTestSuiteTitles = (context, titles, node, title) => {
const isFirstArgLiteral = node =>
node.arguments && node.arguments[0] && node.arguments[0].type === 'Literal';

module.exports = context => {
const contexts = [newDescribeContext()];
return {
CallExpression(node) {
const currentLayer = contexts[contexts.length - 1];
if (isDescribe(node)) {
contexts.push(newDescribeContext());
}
if (!isFirstArgLiteral(node)) {
return;
}

const title = node.arguments[0].value;
handleTestCaseTitles(context, currentLayer.testTitles, node, title);
handleTestSuiteTitles(context, currentLayer.describeTitles, node, title);
},
'CallExpression:exit'(node) {
if (isDescribe(node)) {
contexts.pop();
}
module.exports = {
meta: {
docs: {
url:
'https://github.com/jest-community/eslint-plugin-jest/blob/master/docs/rules/no-identical-title.md',
},
};
},
create(context) {
const contexts = [newDescribeContext()];
return {
CallExpression(node) {
const currentLayer = contexts[contexts.length - 1];
if (isDescribe(node)) {
contexts.push(newDescribeContext());
}
if (!isFirstArgLiteral(node)) {
return;
}

const title = node.arguments[0].value;
handleTestCaseTitles(context, currentLayer.testTitles, node, title);
handleTestSuiteTitles(
context,
currentLayer.describeTitles,
node,
title
);
},
'CallExpression:exit'(node) {
if (isDescribe(node)) {
contexts.pop();
}
},
};
},
};
49 changes: 29 additions & 20 deletions rules/no_large_snapshots.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,35 @@
'use strict';

module.exports = context => {
if (context.getFilename().endsWith('.snap')) {
const lineLimit = (context.options[0] && context.options[0].maxSize) || 50;
module.exports = {
meta: {
docs: {
url:
'https://github.com/jest-community/eslint-plugin-jest/blob/master/docs/rules/no-large-snapshots.md',
},
},
create(context) {
if (context.getFilename().endsWith('.snap')) {
const lineLimit =
(context.options[0] && context.options[0].maxSize) || 50;

return {
ExpressionStatement: node => {
const startLine = node.loc.start.line;
const endLine = node.loc.end.line;
const lineCount = endLine - startLine;
return {
ExpressionStatement: node => {
const startLine = node.loc.start.line;
const endLine = node.loc.end.line;
const lineCount = endLine - startLine;

if (lineCount > lineLimit) {
context.report({
message:
'Expected Jest snapshot to be smaller than {{ lineLimit }} lines but was {{ lineCount }} lines long',
data: { lineLimit, lineCount },
node,
});
}
},
};
}
if (lineCount > lineLimit) {
context.report({
message:
'Expected Jest snapshot to be smaller than {{ lineLimit }} lines but was {{ lineCount }} lines long',
data: { lineLimit, lineCount },
node,
});
}
},
};
}

return {};
return {};
},
};
36 changes: 22 additions & 14 deletions rules/prefer_expect_assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,30 @@ const reportMsg = (context, node) => {
});
};

module.exports = context => {
return {
CallExpression(node) {
if (isTestOrItFunction(node)) {
const testFuncBody = getTestFunctionBody(node);
if (testFuncBody) {
if (!isFirstLineExprStmt(testFuncBody)) {
reportMsg(context, node);
} else {
const testFuncFirstLine = getFunctionFirstLine(testFuncBody);
if (!isExpectAssertionsOrHasAssertionsCall(testFuncFirstLine)) {
module.exports = {
meta: {
docs: {
url:
'https://github.com/jest-community/eslint-plugin-jest/blob/master/docs/rules/prefer-expect-assertions.md',
},
},
create(context) {
return {
CallExpression(node) {
if (isTestOrItFunction(node)) {
const testFuncBody = getTestFunctionBody(node);
if (testFuncBody) {
if (!isFirstLineExprStmt(testFuncBody)) {
reportMsg(context, node);
} else {
const testFuncFirstLine = getFunctionFirstLine(testFuncBody);
if (!isExpectAssertionsOrHasAssertionsCall(testFuncFirstLine)) {
reportMsg(context, node);
}
}
}
}
}
},
};
},
};
},
};
Loading

0 comments on commit 824c2a3

Please sign in to comment.