Skip to content

Commit 561eb07

Browse files
committed
feature: @putout/plugin-tape: add-t-end: speed up
1 parent 616c968 commit 561eb07

3 files changed

Lines changed: 27 additions & 21 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
test('message', (t) => {
2+
t.end();
3+
something();
4+
t.end();
5+
});

packages/plugin-tape/lib/add-t-end/index.js

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@ import {
44
operator,
55
} from 'putout';
66

7-
const {compare, traverse} = operator;
8-
const {expressionStatement} = types;
7+
const {compare} = operator;
8+
const {
9+
expressionStatement,
10+
isCallExpression,
11+
isFunction,
12+
} = types;
913

1014
export const report = () => `'t.end()' is missing at the end of the test`;
1115

@@ -19,32 +23,29 @@ export const replace = () => ({
1923
'test(__a, async (t) => __body)': transform,
2024
});
2125

22-
function check({__body}, path) {
26+
function check({__body}) {
2327
const {body} = __body;
2428
const {length} = body;
2529

2630
if (!length)
2731
return true;
2832

29-
for (const element of body) {
30-
if (compare(element, 't.end()'))
31-
return false;
32-
}
33+
const last = body.at(-1);
34+
35+
if (compare(last, 't.end()'))
36+
return false;
3337

34-
let found = false;
38+
if (compare(last, 'await t.__(__args)'))
39+
return false;
3540

36-
traverse(path, {
37-
'await t.__(__args)': () => {
38-
found = true;
39-
path.stop();
40-
},
41-
't.end()': (path) => {
42-
found = true;
43-
path.stop();
44-
},
45-
});
41+
const {expression} = last;
42+
43+
if (isCallExpression(expression)) {
44+
const lastArg = expression.arguments.at(-1);
45+
return !isFunction(lastArg);
46+
}
4647

47-
return !found;
48+
return true;
4849
}
4950

5051
function transform({__body}, path) {

packages/plugin-tape/lib/add-t-end/index.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ test('plugin-tape: add-t-end: transform: putout', (t) => {
4040
t.end();
4141
});
4242

43-
test('plugin-tape: add-t-end: no transform: contains', (t) => {
44-
t.noTransform('contains');
43+
test('plugin-tape: add-t-end: transform: contains', (t) => {
44+
t.transform('contains');
4545
t.end();
4646
});
4747

0 commit comments

Comments
 (0)