-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-linter.ts
More file actions
186 lines (155 loc) · 5.96 KB
/
test-linter.ts
File metadata and controls
186 lines (155 loc) · 5.96 KB
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
// @ts-expect-error TS2307
import { expressionlanguage, ELLanguage, _linter } from "@valtzu/codemirror-lang-el";
import { EditorState } from "@codemirror/state";
import ist from "ist"
import { syntaxTree } from "@codemirror/language";
const config = {
types: {
"custom44": {
identifiers: [
{name: "property11", type: ["any"]},
{name: "property22", type: ["any"]},
],
functions: [
{name: "firstMethod", args: [], returnType: ["custom44"]},
]
}
},
identifiers: [
{name: "foobar"},
{name: "foobaz"},
{name: "obj", type: ["custom44"]}
],
functions: [
{name: "smh", args: [], returnType: ["string"]},
{name: "any_fn", args: [{name: "anything", type: ["any"]}, {name: "optional", optional: true}], returnType: ["any"]},
{name: "smash_my_head", args: [{name: "object", type: ["object"]}]},
{name: "getObject", returnType: ["custom44"]},
],
};
function get(doc: string) {
const state = EditorState.create({
doc,
selection: { anchor: 0 },
extensions: [
ELLanguage.data.of({
expressionLanguageConfig: config,
}),
expressionlanguage(config)
],
});
if (process.env.DEBUG) {
syntaxTree(state).cursor().iterate((node) => process.stdout.write(node.name + '('), () => process.stdout.write(')'));
process.stdout.write('\n');
}
return _linter.expressionLanguageLinterSource(state);
}
describe("Expression language linting", () => {
it("detects missing variables", () => {
const diagnostics = get("notfound / 5");
ist(diagnostics.length, 1);
ist(diagnostics[0].message, 'Variable <code>notfound</code> not found');
ist(diagnostics[0].from, 0);
ist(diagnostics[0].to, 8);
});
it("detects missing functions", () => {
const diagnostics = get("obj + notfound()");
ist(diagnostics.length, 1);
ist(diagnostics[0].message, 'Function <code>notfound</code> not found');
ist(diagnostics[0].from, 6);
ist(diagnostics[0].to, 14);
});
it("complains about variables after variables", () => {
const diagnostics = get("obj obj");
ist(diagnostics.length, 1);
ist(diagnostics[0].message, "Unexpected identifier <code>obj</code>");
ist(diagnostics[0].from, 4);
ist(diagnostics[0].to, 7);
});
it("complains about multiple binary operators in row", () => {
const diagnostics = get("obj +* obj");
ist(diagnostics.length, 1);
ist(diagnostics[0].from, 5);
ist(diagnostics[0].to, 10);
ist(diagnostics[0].message, "Expression expected");
});
it("complains about too many arguments", () => {
const diagnostics = get("smash_my_head({}, 2)");
ist(diagnostics.length, 1);
ist(diagnostics[0].from, 18);
ist(diagnostics[0].to, 19);
ist(diagnostics[0].message, "Unexpected argument – <code>smash_my_head</code> takes exactly 1 argument");
});
it("complains about too many arguments when there are optional arguments", () => {
const diagnostics = get("any_fn(1, 2, 3)");
ist(diagnostics.length, 1);
ist(diagnostics[0].from, 13);
ist(diagnostics[0].to, 14);
ist(diagnostics[0].message, "Unexpected argument – <code>any_fn</code> takes 1–2 arguments");
});
it("complains about too few arguments", () => {
const diagnostics = get("smash_my_head()");
ist(diagnostics.length, 1);
ist(diagnostics[0].from, 13);
ist(diagnostics[0].to, 15);
ist(diagnostics[0].message, "Too few arguments – <code>smash_my_head</code> takes exactly 1 argument");
});
it("complains about too few arguments when there are optional arguments", () => {
const diagnostics = get("any_fn()");
ist(diagnostics.length, 1);
ist(diagnostics[0].from, 6);
ist(diagnostics[0].to, 8);
ist(diagnostics[0].message, "Too few arguments – <code>any_fn</code> takes 1–2 arguments");
});
it("complains about wrong argument type using constant", () => {
const diagnostics = get("smash_my_head(5)");
ist(diagnostics.length, 1);
ist(diagnostics[0].from, 14);
ist(diagnostics[0].to, 15);
ist(diagnostics[0].message, "<code>object</code> expected, got <code>number</code>");
});
it("complains about wrong argument type using resolved type", () => {
const diagnostics = get("smash_my_head(smh())");
ist(diagnostics.length, 1);
ist(diagnostics[0].from, 14);
ist(diagnostics[0].to, 19);
ist(diagnostics[0].message, "<code>object</code> expected, got <code>string</code>");
});
it('does not complain about putting "wrong" argument type to any', () => {
const diagnostics = get("any_fn(smh())");
ist(diagnostics.length, 0);
});
it("comments ignored in arguments", () => {
const diagnostics = get("smh(/* comment */)");
ist(diagnostics.length, 0);
});
it("accepts comments", () => {
const diagnostics = get("1 /* comment */ + 2");
ist(diagnostics.length, 0);
});
it("complains about non-array after 'in' operator", () => {
const diagnostics = get("'foo' in 'foobar'");
ist(diagnostics.length, 1);
ist(diagnostics[0].from, 9);
ist(diagnostics[0].to, 17);
ist(diagnostics[0].message, "<code>array</code> expected, got <code>string</code>");
});
it("complains about non-string arguments for 'contains' operator", () => {
// Left side not string
let diagnostics = get("1 contains 'foo'");
ist(diagnostics.length, 1);
ist(diagnostics[0].message, "<code>string</code> expected, got <code>number</code>");
// Right side not string
diagnostics = get("'foo' contains 1");
ist(diagnostics.length, 1);
ist(diagnostics[0].message, "<code>string</code> expected, got <code>number</code>");
// Both sides not string
diagnostics = get("1 contains 2");
ist(diagnostics.length, 2);
ist(diagnostics[0].message, "<code>string</code> expected, got <code>number</code>");
ist(diagnostics[1].message, "<code>string</code> expected, got <code>number</code>");
// Both sides string: no error
diagnostics = get("'foo' contains 'bar'");
ist(diagnostics.length, 0);
});
});