forked from ember-tooling/ember-language-server
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathglimmer-utils-test.ts
161 lines (145 loc) · 6.71 KB
/
glimmer-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
import { Position } from 'vscode-languageserver';
import { ASTv1, preprocess } from '@glimmer/syntax';
import ASTPath, { getLocalScope, maybeComponentNameForPath, sourceForNode, focusedBlockParamName, maybeBlockParamDefinition } from '../src/glimmer-utils';
import { toPosition } from '../src/estree-utils';
describe('glimmer-utils', function () {
describe('ASTPath', function () {
it('works as expected', function () {
const input = `
<Component as |items|>
{{#let items as |item bar|}}
{{foo}}
{{/let}}
</Component>
`;
const astPath = ASTPath.toPosition(preprocess(input), toPosition(Position.create(3, 5)));
expect(astPath.node).toMatchSnapshot();
});
});
describe('getLocalScope', function () {
it('works as expected', function () {
const input = `
<Component as |items|>
{{#let items as |item bar|}}
{{foo}}
{{/let}}
</Component>
`;
const astPath = ASTPath.toPosition(preprocess(input), toPosition(Position.create(3, 5)));
expect(getLocalScope(astPath).map(({ name, index }) => [name, index])).toEqual([
['item', 0],
['bar', 1],
['items', 0],
]);
});
});
describe('maybeComponentNameForPath', function () {
it('works as expected', function () {
const input = `
<Component as |items|>
{{#let items as |item bar|}}
{{items}}
{{/let}}
</Component>
`;
const astPath = ASTPath.toPosition(preprocess(input), toPosition(Position.create(3, 5)));
expect(maybeComponentNameForPath(astPath)).toEqual('Component');
});
});
describe('sourceForNode', function () {
it('works as expected', function () {
const input = `
<Component as |items|>
{{#let items as |item bar|}}
{{items}}
{{/let}}
</Component>
`;
const astPath = ASTPath.toPosition(preprocess(input), toPosition(Position.create(2, 2)));
expect((astPath.node as ASTv1.ElementNode).tag).toEqual('Component');
expect(sourceForNode(astPath.node, input)).toEqual(input.trim());
});
it('works as expected for MustachePaths', function () {
const input = ['<Component as |items|>', '{{#let items as |item bar|}}', '{{items}}', '{{/let}}', '</Component>'].join('\n');
const astPath = ASTPath.toPosition(preprocess(input), toPosition(Position.create(2, 3)));
expect((astPath.node as ASTv1.PathExpression).original).toEqual('items');
expect(sourceForNode(astPath.node, input)).toEqual('items');
});
});
describe('maybeBlockParamDefinition', function () {
it('able to handle wrong paths', function () {
const input = ['<Component as |items|>', '{{foo}}', '</Component>'].join('\n');
const pos = toPosition(Position.create(1, 3));
const astPath = ASTPath.toPosition(preprocess(input), pos);
expect(maybeBlockParamDefinition(astPath, input, pos)).toEqual(undefined);
});
it('able to handle single param', function () {
const input = ['<Component as |items|>', '{{foo}}', '</Component>'].join('\n');
const pos = toPosition(Position.create(0, 16));
const astPath = ASTPath.toPosition(preprocess(input), pos);
expect(maybeBlockParamDefinition(astPath, input, pos)).toMatchSnapshot();
});
it('able to handle single fiew params', function () {
const input = ['<Component as |items boo zoo|>', '{{foo}}', '</Component>'].join('\n');
const pos = toPosition(Position.create(0, 22));
const astPath = ASTPath.toPosition(preprocess(input), pos);
expect(maybeBlockParamDefinition(astPath, input, pos)).toMatchSnapshot();
});
});
describe('focusedBlockParamName', function () {
it('works as expected for MustachePaths', function () {
const input = ['<Component as |items|>', '{{#let items as |item bar|}}', '{{items}}', '{{/let}}', '</Component>'].join('\n');
const paramName = focusedBlockParamName(input, toPosition(Position.create(1, 24)));
expect(paramName).toEqual('bar');
});
it('works as expected for corner cases [left]', function () {
const input = ['<C as |items|>'].join('\n');
let paramName = focusedBlockParamName(input, toPosition(Position.create(0, 7)));
expect(paramName).toEqual('items');
paramName = focusedBlockParamName(input, toPosition(Position.create(0, 8)));
expect(paramName).toEqual('items');
paramName = focusedBlockParamName(input, toPosition(Position.create(0, 6)));
expect(paramName).toEqual('');
});
it('works as expected for corner cases [right]', function () {
const input = ['<C as |items|>'].join('\n');
let paramName = focusedBlockParamName(input, toPosition(Position.create(0, 12)));
expect(paramName).toEqual('items');
paramName = focusedBlockParamName(input, toPosition(Position.create(0, 11)));
expect(paramName).toEqual('items');
paramName = focusedBlockParamName(input, toPosition(Position.create(0, 13)));
expect(paramName).toEqual('');
});
it('works as expected for corner cases for 1+ param [right]', function () {
const input = ['<C as |items foo|>'].join('\n');
let paramName = focusedBlockParamName(input, toPosition(Position.create(0, 6)));
expect(paramName).toEqual('');
paramName = focusedBlockParamName(input, toPosition(Position.create(0, 12)));
expect(paramName).toEqual('items');
paramName = focusedBlockParamName(input, toPosition(Position.create(0, 13)));
expect(paramName).toEqual('foo');
paramName = focusedBlockParamName(input, toPosition(Position.create(0, 16)));
expect(paramName).toEqual('foo');
paramName = focusedBlockParamName(input, toPosition(Position.create(0, 17)));
expect(paramName).toEqual('');
});
it('works as expected for corner cases for 1+ param and long blanks', function () {
const input = ['<C as | items foo |>'].join('\n');
let paramName = focusedBlockParamName(input, toPosition(Position.create(0, 7)));
expect(paramName).toEqual('');
paramName = focusedBlockParamName(input, toPosition(Position.create(0, 13)));
expect(paramName).toEqual('items');
paramName = focusedBlockParamName(input, toPosition(Position.create(0, 14)));
expect(paramName).toEqual('');
paramName = focusedBlockParamName(input, toPosition(Position.create(0, 15)));
expect(paramName).toEqual('foo');
paramName = focusedBlockParamName(input, toPosition(Position.create(0, 19)));
expect(paramName).toEqual('');
});
it('works as expected for Tags', function () {
const input = ['<Component as |items|>', '{{#let items as |item bar|}}', '{{items}}', '{{/let}}', '</Component>'].join('\n');
const paramName = focusedBlockParamName(input, toPosition(Position.create(0, 16)));
expect(paramName).toEqual('items');
});
});
});