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

Even more tests #96

Merged
merged 3 commits into from
Aug 24, 2022
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
6 changes: 3 additions & 3 deletions src/rules/missing-playwright-await.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import * as ESTree from 'estree';
import { Rule } from 'eslint';
import { getNodeName } from '../utils/ast';
import { getStringValue } from '../utils/ast';

type MemberExpression = ESTree.MemberExpression & Rule.NodeParentExtension;

function getMemberExpressionNode(
node: MemberExpression,
matchers: Set<string>
) {
const propertyName = getNodeName(node.property);
const propertyName = getStringValue(node.property);

if (getNodeName(node.object) === 'test') {
if (getStringValue(node.object) === 'test') {
return propertyName === 'step' ? { node, type: 'testStep' } : undefined;
}

Expand Down
24 changes: 12 additions & 12 deletions src/rules/no-force-option.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { Rule } from 'eslint';
import * as ESTree from 'estree';
import { isBooleanLiteral, isIdentifier } from '../utils/ast';
import { getStringValue, isBooleanLiteral } from '../utils/ast';

function isForceOptionEnabled(node: ESTree.CallExpression) {
return node.arguments.some(
(argument) =>
argument.type === 'ObjectExpression' &&
argument.properties.some(
(property) =>
property.type === 'Property' &&
isIdentifier(property.key, 'force') &&
isBooleanLiteral(property.value, true)
)
const arg = node.arguments[node.arguments.length - 1];

return (
arg?.type === 'ObjectExpression' &&
arg.properties.some(
(property) =>
property.type === 'Property' &&
getStringValue(property.key) === 'force' &&
isBooleanLiteral(property.value, true)
)
);
}

Expand All @@ -35,8 +36,7 @@ export default {
return {
MemberExpression(node) {
if (
node.property.type === 'Identifier' &&
methodsWithForceOption.has(node.property.name) &&
methodsWithForceOption.has(getStringValue(node.property)) &&
node.parent.type === 'CallExpression' &&
isForceOptionEnabled(node.parent)
) {
Expand Down
4 changes: 2 additions & 2 deletions src/rules/no-restricted-matchers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Rule } from 'eslint';
import { getMatchers, getNodeName, isExpectCall } from '../utils/ast';
import { getMatchers, getStringValue, isExpectCall } from '../utils/ast';

export default {
create(context) {
Expand All @@ -17,7 +17,7 @@ export default {
const permutations = matchers.map((_, i) => matchers.slice(0, i + 1));

for (const permutation of permutations) {
const chain = permutation.map(getNodeName).join('.');
const chain = permutation.map(getStringValue).join('.');

if (chain in restrictedChains) {
const message = restrictedChains[chain];
Expand Down
57 changes: 17 additions & 40 deletions src/rules/no-skipped-test.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,35 @@
import { AST, Rule } from 'eslint';
import * as ESTree from 'estree';
import { Rule } from 'eslint';
import {
isTest,
isDescribeCall,
isPropertyAccessor,
isTestIdentifier,
isObjectProperty,
isStringLiteral,
isBooleanLiteral,
isIdentifier,
} from '../utils/ast';

/**
* This function returns needed range to remove skip annotation.
*
* To cover the standalone cases:
* 1. test.skip() => when there's no arguments
* 2. test.skip(browserName === 'firefox', 'Working on it') => when there's first argument is a binary expression and the second argument is a string literal
* 3. test.skip(true, 'Working on it') => when there's first argument is a boolean literal and the second argument is a string literal
*
* So, if it's standalone skip then we need to remove the whole line
*
* Otherwise we need to remove the range of `.skip` annotation - 1 (dot notation).
*/
function getSkipRange(
node: ESTree.MemberExpression & Rule.NodeParentExtension,
parent: ESTree.CallExpression & Rule.NodeParentExtension
): AST.Range {
const [first, second] = parent.arguments;

const isStandaloneSkip =
!parent.arguments.length ||
((first.type === 'BinaryExpression' || isBooleanLiteral(first)) &&
isStringLiteral(second));

return isStandaloneSkip
? parent.parent.range!
: [node.property.range![0] - 1, node.property.range![1]];
}

export default {
create(context) {
return {
MemberExpression(node) {
const parent = node.parent;
CallExpression(node) {
const { callee } = node;

if (
(isTestIdentifier(node) || isObjectProperty(node, 'describe')) &&
isIdentifier(node.property, 'skip') &&
parent.type === 'CallExpression'
(isTestIdentifier(callee) || isDescribeCall(node)) &&
callee.type === 'MemberExpression' &&
isPropertyAccessor(callee, 'skip')
) {
context.report({
messageId: 'noSkippedTest',
suggest: [
{
messageId: 'removeSkippedTestAnnotation',
fix: (fixer) => fixer.removeRange(getSkipRange(node, parent)),
fix: (fixer) => {
return isTest(node) || isDescribeCall(node)
? fixer.removeRange([
callee.property.range![0] - 1,
callee.range![1],
])
: fixer.remove(node.parent);
},
},
],
node,
Expand Down
27 changes: 20 additions & 7 deletions src/rules/no-useless-not.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Rule } from 'eslint';
import { getNodeName, isIdentifier } from '../utils/ast';
import * as ESTree from 'estree';
import { getStringValue, isExpectCall, isPropertyAccessor } from '../utils/ast';

const matcherMap = {
toBeVisible: 'toBeHidden',
Expand All @@ -8,27 +9,39 @@ const matcherMap = {
toBeDisabled: 'toBeEnabled',
};

const getRangeOffset = (node: ESTree.Node) =>
node.type === 'Identifier' ? 0 : 1;

export default {
create(context) {
return {
MemberExpression(node) {
if (
node.object.type === 'MemberExpression' &&
node.object.object.type === 'CallExpression' &&
isIdentifier(node.object.object.callee, 'expect') &&
isIdentifier(node.object.property, 'not')
isExpectCall(node.object.object) &&
isPropertyAccessor(node.object, 'not')
) {
const matcher = getNodeName(node.property) as
const matcher = getStringValue(node.property) as
| keyof typeof matcherMap
| undefined;

if (matcher && matcher in matcherMap) {
const range = node.object.property.range!;
const { property } = node.object;

context.report({
fix: (fixer) => [
fixer.removeRange([range[0], range[1] + 1]),
fixer.replaceText(node.property, matcherMap[matcher]),
fixer.removeRange([
property.range![0] - getRangeOffset(property),
property.range![1] + 1,
]),
fixer.replaceTextRange(
[
node.property.range![0] + getRangeOffset(node.property),
node.property.range![1] - getRangeOffset(node.property),
],
matcherMap[matcher]
),
],
messageId: 'noUselessNot',
node: node,
Expand Down
16 changes: 13 additions & 3 deletions src/rules/prefer-to-have-length.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { Rule } from 'eslint';
import * as ESTree from 'estree';
import {
isExpectCall,
getNodeName,
getMatchers,
isPropertyAccessor,
getStringValue,
} from '../utils/ast';

const matchers = new Set(['toBe', 'toEqual', 'toStrictEqual']);

const getRangeOffset = (node: ESTree.Node) =>
node.type === 'Identifier' ? 0 : 1;

export default {
create(context) {
return {
Expand All @@ -21,7 +25,7 @@ export default {

if (
!matcher ||
!matchers.has(getNodeName(matcher) ?? '') ||
!matchers.has(getStringValue(matcher) ?? '') ||
argument?.type !== 'MemberExpression' ||
!isPropertyAccessor(argument, 'length')
) {
Expand All @@ -37,7 +41,13 @@ export default {
argument.range![1],
]),
// replace the current matcher with "toHaveLength"
fixer.replaceText(matcher, 'toHaveLength'),
fixer.replaceTextRange(
[
matcher.range![0] + getRangeOffset(matcher),
matcher.range![1] - getRangeOffset(matcher),
],
'toHaveLength'
),
];
},
messageId: 'useToHaveLength',
Expand Down
4 changes: 2 additions & 2 deletions src/rules/valid-expect.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Rule } from 'eslint';
import { isExpectCall, isIdentifier } from '../utils/ast';
import { isExpectCall, isPropertyAccessor } from '../utils/ast';
import { NodeWithParent } from '../utils/types';
import * as ESTree from 'estree';
import { getAmountData } from '../utils/misc';
Expand All @@ -10,7 +10,7 @@ function isMatcherFound(node: NodeWithParent) {
}

if (
isIdentifier(node.parent.property, 'not') &&
isPropertyAccessor(node.parent, 'not') &&
node.parent.parent.type !== 'MemberExpression'
) {
return { found: false, node: node.parent };
Expand Down
37 changes: 10 additions & 27 deletions src/utils/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@ import * as ESTree from 'estree';
import { NodeWithParent, TypedNodeWithParent } from './types';

export function getStringValue(node: ESTree.Node) {
return node.type === 'TemplateLiteral'
return node.type === 'Identifier'
? node.name
: node.type === 'TemplateLiteral'
? node.quasis[0].value.raw
: node.type === 'Literal' && typeof node.value === 'string'
? node.value
: '';
}

export function getNodeName(node: ESTree.Node) {
return node.type === 'Identifier' ? node.name : undefined;
}

export function isIdentifier(node: ESTree.Node, name: string) {
return getNodeName(node) === name;
function isIdentifier(node: ESTree.Node, name: string) {
return node.type === 'Identifier' && node.name === name;
}

function isLiteral<T>(node: ESTree.Node, type: string, value?: T) {
Expand All @@ -35,17 +33,11 @@ export function isBooleanLiteral(node: ESTree.Node, value?: boolean) {
return isLiteral(node, 'boolean', value);
}

export function getPropertyName(node: ESTree.MemberExpression) {
return node.property.type === 'Identifier'
? node.property.name
: getStringValue(node.property);
}

export function isPropertyAccessor(
node: ESTree.MemberExpression,
name: string
) {
return getPropertyName(node) === name;
return getStringValue(node.property) === name;
}

export function isCalleeObject(node: ESTree.CallExpression, name: string) {
Expand All @@ -69,13 +61,6 @@ export function isTestIdentifier(node: ESTree.Node) {
);
}

export function isObjectProperty(node: ESTree.MemberExpression, name: string) {
return (
node.object.type === 'MemberExpression' &&
isIdentifier(node.object.property, name)
);
}

const describeProperties = new Set([
'parallel',
'serial',
Expand All @@ -96,9 +81,9 @@ export function isDescribeCall(node: ESTree.Node): boolean {
return false;
}

return isIdentifier(inner.property, 'describe')
return isPropertyAccessor(inner, 'describe')
? true
: describeProperties.has(getPropertyName(inner))
: describeProperties.has(getStringValue(inner.property))
? isDescribeCall(inner.object)
: false;
}
Expand Down Expand Up @@ -130,8 +115,7 @@ export function isTestHook(node: ESTree.CallExpression) {
return (
node.callee.type === 'MemberExpression' &&
isIdentifier(node.callee.object, 'test') &&
node.callee.property.type === 'Identifier' &&
testHooks.has(node.callee.property.name)
testHooks.has(getStringValue(node.callee.property))
);
}

Expand All @@ -141,8 +125,7 @@ export function isExpectCall(node: ESTree.CallExpression) {
isIdentifier(node.callee, 'expect') ||
(node.callee.type === 'MemberExpression' &&
isIdentifier(node.callee.object, 'expect') &&
node.callee.property.type === 'Identifier' &&
expectSubCommands.has(node.callee.property.name))
expectSubCommands.has(getStringValue(node.callee.property)))
);
}

Expand Down
4 changes: 2 additions & 2 deletions test/spec/max-nested-describe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ runRuleTester('max-nested-describe', rule, {
`
test.describe('foo', () => {
test.describe('bar', () => {
test.describe('baz', () => {
test["describe"]('baz', () => {
test.describe('baz1', () => {
test.describe('baz2', () => {
test.describe('baz3', () => {
test[\`describe\`]('baz3', () => {
test('should get something', () => {
expect(getSomething()).toBe('Something');
});
Expand Down
Loading