Skip to content

Commit

Permalink
fix: handle AwaitExpression in the main tester
Browse files Browse the repository at this point in the history
  • Loading branch information
tmercswims committed May 2, 2021
1 parent 60aa73c commit 3c7fa49
Showing 1 changed file with 11 additions and 25 deletions.
36 changes: 11 additions & 25 deletions src/rules/padding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,36 +77,22 @@ interface PaddingContext {
}

// Creates a StatementTester to test an ExpressionStatement's first token name
const createTokenTester = (
tokenName: string,
expectedNodeType = 'ExpressionStatement',
): StatementTester => {
const createTokenTester = (tokenName: string): StatementTester => {
return (node: Node, sourceCode: SourceCode): boolean => {
const token = sourceCode.getFirstToken(node);
let activeNode = node;

return (
node.type === expectedNodeType &&
token.type === 'Identifier' &&
token.value === tokenName
);
};
};
if (activeNode.type === 'ExpressionStatement') {
// In the case of `await`, we actually care about its argument
if (activeNode.expression.type === 'AwaitExpression') {
activeNode = activeNode.expression.argument;
}

// Creates a StatementTester which digs into awaited ExpressionStatements
const createAwaitedTokenTester = (tokenName: string): StatementTester => {
return (node: Node, sourceCode: SourceCode): boolean => {
let activeNode = node;
let expectedType = 'ExpressionStatement';
const token = sourceCode.getFirstToken(activeNode);

if (
activeNode.type === 'ExpressionStatement' &&
activeNode.expression.type === 'AwaitExpression'
) {
activeNode = activeNode.expression.argument;
expectedType = 'CallExpression';
return token.type === 'Identifier' && token.value === tokenName;
}

return createTokenTester(tokenName, expectedType)(activeNode, sourceCode);
return false;
};
};

Expand All @@ -118,7 +104,7 @@ const statementTesters: { [T in StatementType]: StatementTester } = {
[StatementType.BeforeAllToken]: createTokenTester('beforeAll'),
[StatementType.BeforeEachToken]: createTokenTester('beforeEach'),
[StatementType.DescribeToken]: createTokenTester('describe'),
[StatementType.ExpectToken]: createAwaitedTokenTester('expect'),
[StatementType.ExpectToken]: createTokenTester('expect'),
[StatementType.FdescribeToken]: createTokenTester('fdescribe'),
[StatementType.FitToken]: createTokenTester('fit'),
[StatementType.ItToken]: createTokenTester('it'),
Expand Down

0 comments on commit 3c7fa49

Please sign in to comment.