/
tag-utils.test.ts
192 lines (183 loc) · 6.34 KB
/
tag-utils.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import ts from 'typescript';
import { findAllNodes } from './utilily-functions';
import type { TagConfig, StrictTagCondition } from './types';
import { parseTagConfig, getTemplateNodeUnder, getTagName } from './tag-utils';
describe(parseTagConfig, () => {
test.each([
{
config: undefined,
expected: {
names: [],
allowNotTaggedTemplate: true,
allowTaggedTemplateExpression: true,
allowFunctionCallExpression: false,
} as StrictTagCondition,
},
{
config: '',
expected: {
names: [],
allowNotTaggedTemplate: true,
allowTaggedTemplateExpression: true,
allowFunctionCallExpression: false,
} as StrictTagCondition,
},
{
config: 'gql',
expected: {
names: ['gql'],
allowNotTaggedTemplate: false,
allowTaggedTemplateExpression: true,
allowFunctionCallExpression: false,
} as StrictTagCondition,
},
{
config: ['gql', 'graphql'],
expected: {
names: ['gql', 'graphql'],
allowNotTaggedTemplate: false,
allowTaggedTemplateExpression: true,
allowFunctionCallExpression: false,
} as StrictTagCondition,
},
{
config: {},
expected: {
names: [],
allowNotTaggedTemplate: false,
allowTaggedTemplateExpression: true,
allowFunctionCallExpression: false,
} as StrictTagCondition,
},
{
config: {
name: 'gql',
} satisfies TagConfig,
expected: {
names: ['gql'],
allowNotTaggedTemplate: false,
allowTaggedTemplateExpression: true,
allowFunctionCallExpression: false,
} as StrictTagCondition,
},
{
config: {
name: ['gql', 'graphql'],
} satisfies TagConfig,
expected: {
names: ['gql', 'graphql'],
allowNotTaggedTemplate: false,
allowTaggedTemplateExpression: true,
allowFunctionCallExpression: false,
} as StrictTagCondition,
},
{
config: {
name: ['graphql'],
ignoreFunctionCallExpression: true,
} satisfies TagConfig,
expected: {
names: ['graphql'],
allowNotTaggedTemplate: false,
allowTaggedTemplateExpression: true,
allowFunctionCallExpression: false,
} as StrictTagCondition,
},
{
config: {
name: ['graphql'],
ignoreFunctionCallExpression: false,
} satisfies TagConfig,
expected: {
names: ['graphql'],
allowNotTaggedTemplate: false,
allowTaggedTemplateExpression: true,
allowFunctionCallExpression: true,
} as StrictTagCondition,
},
])('input: $config', ({ config, expected }) => {
expect(parseTagConfig(config)).toEqual(expected);
});
});
describe(getTemplateNodeUnder, () => {
const createFixture = (sourceText: string, tagConfig: TagConfig) => {
const source = ts.createSourceFile('input.ts', sourceText, ts.ScriptTarget.Latest, false);
return {
exec: () => findAllNodes(source, node => getTemplateNodeUnder(node, parseTagConfig(tagConfig))),
};
};
it.each([
{ source: '`query { }`', config: undefined },
{ source: 'gql`query { }`', config: 'gql' },
{ source: 'gql`query { }`', config: ['gql', 'graphql'] },
{
source: 'graphql(`query { }`)',
config: { name: 'graphql', ignoreFunctionCallExpression: false } satisfies TagConfig,
},
])('should return TemplateLiteralNode from $source with tagCongig: $config', ({ source, config }) => {
const [found] = createFixture(source, config).exec();
expect(found).toBeTruthy();
});
test.each([
{ source: '`query { }`', config: 'gql' },
{ source: 'graphql`query { }`', config: 'gql' },
{
source: 'fn(`query { }`)',
config: { name: 'graphql', ignoreFunctionCallExpression: false } satisfies TagConfig,
},
{
source: 'graphql(`query { }`)',
config: { name: 'graphql', ignoreFunctionCallExpression: true } satisfies TagConfig,
},
])('should not return undefind from $source with tagCongig: $config', ({ source, config }) => {
const actual = createFixture(source, config).exec();
expect(actual).toEqual([]);
});
});
describe(getTagName, () => {
const createFixture = (sourceText: string, tagConfig: TagConfig) => {
const source = ts.createSourceFile('input.ts', sourceText, ts.ScriptTarget.Latest, true);
return {
exec: () =>
findAllNodes(source, node => getTemplateNodeUnder(node, parseTagConfig(tagConfig))).map(n =>
getTagName(n, parseTagConfig(tagConfig)),
),
};
};
it.each([
{ source: '`query { }`', expected: '', config: undefined },
{ source: 'gql`query { }`', expected: 'gql', config: 'gql' },
{ source: 'gql`query { }`', expected: 'gql', config: ['gql', 'graphql'] },
{
source: 'graphql(`query { }`)',
expected: 'graphql',
config: { name: 'graphql', ignoreFunctionCallExpression: false } satisfies TagConfig,
},
])('should return TemplateLiteralNode from $source with tagCongig: $config', ({ source, expected, config }) => {
const actual = createFixture(source, config).exec();
expect(actual).toEqual([expected]);
});
it('return empty string when node is parsed without parent', () => {
const sourceText = 'gql`query { }`';
const source = ts.createSourceFile('input.ts', sourceText, ts.ScriptTarget.Latest, false);
const actual = findAllNodes(source, node => getTemplateNodeUnder(node, parseTagConfig('gql'))).map(n =>
getTagName(n, parseTagConfig('gql')),
);
expect(actual).toEqual([undefined]);
});
it('return empty string when node does not match condition', () => {
const sourceText = '`hoge`';
const source = ts.createSourceFile('input.ts', sourceText, ts.ScriptTarget.Latest, true);
const actual = getTagName(source.statements[0] as unknown as ts.TemplateLiteral, parseTagConfig('gql'));
expect(actual).toBe(undefined);
});
it('return empty string when node parent is not TaggedTemplateExpression nor CallExpression', () => {
const sourceText = 'const obj = { x: `hoge` }';
const source = ts.createSourceFile('input.ts', sourceText, ts.ScriptTarget.Latest, true);
const actual = getTagName(
findAllNodes(source, node => ts.isNoSubstitutionTemplateLiteral(node) && node)[0],
parseTagConfig('gql'),
);
expect(actual).toBe(undefined);
});
});