Skip to content

Commit

Permalink
chore: migrate no-commented-out-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Jul 18, 2019
1 parent 782368b commit f5e658b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 42 deletions.
@@ -1,7 +1,8 @@
import { RuleTester } from 'eslint';
import { TSESLint } from '@typescript-eslint/experimental-utils';
import rule from '../no-commented-out-tests';

const ruleTester = new RuleTester({
const ruleTester = new TSESLint.RuleTester({
parser: '@typescript-eslint/parser',
parserOptions: {
sourceType: 'module',
},
Expand All @@ -24,6 +25,7 @@ ruleTester.run('no-commented-out-tests', rule, {
'testSomething()',
'// latest(dates)',
'// TODO: unify with Git implementation from Shipit (?)',
'#!/usr/bin/env node',
[
'import { pending } from "actions"',
'',
Expand Down Expand Up @@ -141,8 +143,8 @@ ruleTester.run('no-commented-out-tests', rule, {
{
code: `
foo()
/*
describe("has title but no callback", () => {})
/*
describe("has title but no callback", () => {})
*/
bar()`,
errors: [{ messageId: 'commentedTests', column: 7, line: 3 }],
Expand Down
38 changes: 0 additions & 38 deletions src/rules/no-commented-out-tests.js

This file was deleted.

45 changes: 45 additions & 0 deletions src/rules/no-commented-out-tests.ts
@@ -0,0 +1,45 @@
import { TSESTree } from '@typescript-eslint/experimental-utils';
import { createRule } from './tsUtils';

function hasTests(node: TSESTree.Comment) {
return /^\s*(x|f)?(test|it|describe)(\.\w+|\[['"]\w+['"]\])?\s*\(/m.test(
node.value,
);
}

export default createRule({
name: __filename,
meta: {
docs: {
category: 'Best Practices',
description:
"This rule raises a warning about commented out tests. It's similar to no-disabled-tests rule.",
recommended: false,
},
messages: {
commentedTests: 'Some tests seem to be commented',
},
schema: [],
type: 'suggestion',
} as const,
defaultOptions: [],
create(context) {
const sourceCode = context.getSourceCode();

function checkNode(node: TSESTree.Comment) {
if (!hasTests(node)) {
return;
}

context.report({ messageId: 'commentedTests', node });
}

return {
Program() {
const comments = sourceCode.getAllComments();

comments.forEach(checkNode);
},
};
},
});

0 comments on commit f5e658b

Please sign in to comment.