Skip to content

Commit 779fcbd

Browse files
committed
Merge branch 'master' of github.com:mug1wara26/source-lsp
2 parents 46bfc3b + 58bcb48 commit 779fcbd

File tree

1 file changed

+382
-0
lines changed

1 file changed

+382
-0
lines changed

server/src/rules/README.md

Lines changed: 382 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,382 @@
1+
# Rules
2+
Documentation for all diagnostics sent back to the client.
3+
## Warnings
4+
5+
### [No unused variables](ast.ts)
6+
**Error message**: `Unused name`
7+
8+
Flags variables that are declared but not used within the code.
9+
```javascript
10+
const pi = 3.14;
11+
display(5*5*3.14);
12+
```
13+
The variable `pi` should have a warning.
14+
15+
## Errors
16+
17+
### [Missing semicolon](ast.ts)
18+
**Error message**: `Missing semicolon`
19+
20+
Identifies statements that are missing semicolons.
21+
```javascript
22+
const x = 1;
23+
```
24+
The line ending should have an error.
25+
26+
### [Trailing comma](ast.ts)
27+
**Error message**: `Trailing comma`
28+
29+
Identifies expressions that have trailing commas.
30+
```javascript
31+
const x = [1,2,3,];
32+
```
33+
The comma after `3` should have an error.
34+
35+
### [No namespace/default imports](ast.ts)
36+
**Error message**: `Only normal imports are allowed`
37+
38+
Bans namespace and default imports.
39+
```javascript
40+
import * as rune from "rune";
41+
import black from "rune";
42+
```
43+
Both lines will throw an error around the identifier.
44+
45+
### [Unexpected token in function parameters](ast.ts)
46+
**Error message**: `Unexpected token`
47+
48+
Only allows identifiers as function parameters
49+
```javascript
50+
function foo(a+b) {}
51+
```
52+
`a+b` will throw an error.
53+
54+
### [Redeclaration of variables](ast.ts)
55+
**Error message**: `Identifier '${name}' has already been declared`
56+
57+
Bans redeclaration of names in the same scope
58+
```javascript
59+
let x = 1;
60+
let x = 2;
61+
```
62+
The second `x` will throw an error.
63+
64+
### [No array expressions](rules/arrayExpression.ts)
65+
**Error message**: `Array expressions are not allowed`
66+
67+
Bans array expressions before Source 3
68+
```javascript
69+
const x = [1,2,3];
70+
```
71+
The array `[1,2,3];` will throw an error.
72+
73+
### [No empty elements](rules/arrayExpression.ts)
74+
**Error message**: `No holes are allowed in array literals`
75+
76+
Bans empty elements in array expressions
77+
```javascript
78+
[1,,3];
79+
```
80+
The array `[1,,3]` will throw an error.
81+
82+
### [Constant reassignment](rules/assignmentExpression.ts)
83+
**Error message**: `Cannot assign new value to constant ${name}`
84+
85+
Bans reassignment to constant variable
86+
```javascript
87+
const x = 1;
88+
x = 2;
89+
```
90+
The second `x` will throw an error
91+
92+
### [Incomplete binary expression](rules/binaryExpression.ts)
93+
**Error message**: `Incomplete binary expression`
94+
95+
Detects when a binary expression is missing a left/right node
96+
```javascript
97+
10 + ;
98+
5 === ;
99+
```
100+
Both lines will throw an error
101+
102+
### [Force strict equality](rules/binaryExpression.ts)
103+
**Error message**: `Use ===/!== instead of ==/!=`
104+
105+
Bans the use of loose equality checks `==` and `!=`
106+
```javascript
107+
true == 1;
108+
```
109+
110+
### [Disallowed operators](rules/binaryExpression.ts)
111+
**Error message**: `${operator} is not allowed`
112+
113+
Only permitted operators defined in Source is allowed. Binary operators like `^` or `instanceof` is banned, unary operators like `typeof` is also banned.
114+
```javascript
115+
100^5;
116+
typeof 1 === "number"
117+
```
118+
The two expressions will throw an error.
119+
120+
### [Only string or number in equality checks](rules/binaryExpression.ts)
121+
**Error message**: `Expected string or number on left/right hand side of operation, got ${type}`
122+
123+
Before Source 3, only strings or numbers can be in equality checks
124+
```javascript
125+
true === 1; // Expected string or number on left hand side of operation, got boolean
126+
true === (1 === 1); // Expected string or number on left hand side of operation, got boolean
127+
(1 === 1) === undefined; // Expected string or number on right hand side of operation, got undefined
128+
"string" === 1; // Expected string on left hand side of operation, got number
129+
```
130+
Errors are shown in the comments.
131+
132+
### [No break statements](rules/breakStatement.ts)
133+
**Error message**: `Break statements are not allowed;`
134+
135+
No break statements before Source 3
136+
```javascript
137+
break;
138+
```
139+
`break` will throw an error.
140+
141+
### [Calling a non function](rules/callExpression.ts)
142+
**Error message**: `${name} is not a function`
143+
144+
Cannot call identifiers that are not a function
145+
```javascript
146+
const x = 1;
147+
x();
148+
let y = 1;
149+
y()
150+
```
151+
The function application on `x` will throw an error, however the function application on `y` will not, as y
152+
can be reassigned, hence it will be a runtime error.
153+
154+
### [Not enough parameters](rules/callExpression.ts)
155+
**Error message**: `Expected ${num_args} or more arguments, but got ${num_supplied_args}`
156+
157+
Detects when a function application has not enough parameters. Currently does not work for anonymous lambdas.
158+
```javascript
159+
display();
160+
```
161+
The call to `display` needs at least 1 element.
162+
163+
### [Incomplete ternary](rules/conditionalExpression.ts)
164+
**Error message**: `Incomplete ternary`
165+
166+
Detects when a ternary does not have a consequent or alternate case.
167+
```javascript
168+
b => b ? 1;
169+
```
170+
`b ? 1` throws an error, as there is no alternate case.
171+
172+
### [No continue statements](rules/continueStatement.ts)
173+
**Error message**: `Continue statements are not allowed`
174+
175+
Bans continue statements before Source 3
176+
```javascript
177+
continue;
178+
```
179+
`continue` will throw an error.
180+
181+
### [No for statements](rules/forStatement.ts)
182+
**Error message**: `For statements are not allowed`
183+
184+
Bans for statements before Source 3
185+
```javascript
186+
for(let i = 0; i < 10; i++) {}
187+
```
188+
`for(...)` will throw an error.
189+
190+
### [Missing block statements](rules/forStatement.ts)
191+
**Error message**: `Missing curly braces around for/while/if/else`
192+
193+
Detects missing curly braces for some statements
194+
```javascript
195+
if (1 === 1)
196+
display("1 is equal to 1");
197+
```
198+
The whole expression will throw an error.
199+
200+
### [Incomplete for loop](rules/forStatement.ts)
201+
**Error message**: `Incomplete for loop`
202+
203+
Detects if a for loop is missing any parts
204+
```javascript
205+
for (let i = 0; i < 10;) {
206+
}
207+
```
208+
`for(...)` will throw an error.
209+
210+
### [Missing function name](rules/functionDeclaration.ts)
211+
**Error message**: `Missing function name`
212+
213+
Detects if function declaration is incomplete
214+
```javascript
215+
function () {}
216+
```
217+
The whole expression will throw an error.
218+
219+
### [Terminal rest element](rules/functionDeclaration.ts)
220+
**Error message**: `No params allowed after rest element`
221+
222+
If a function has a rest parameter, it must be the last parameter.
223+
```javascript
224+
function foo(arg1, ...arg2, arg3) {}
225+
(arg1, ...arg2, arg3) => {};
226+
```
227+
Both cases will throw an error around `arg3`
228+
229+
### [Identifier not declared](rules/identifier.ts)
230+
**Error message**: `Name ${name} not declared`
231+
232+
Detects if there is a declaration for an identifier in its scope.
233+
```javascript
234+
{
235+
const x = 1;
236+
{
237+
display(x);
238+
}
239+
}
240+
display(x);
241+
```
242+
The first `x` in `display(x)` should not throw an error, but the second one should.
243+
244+
### [Missing else case](rules/ifStatement.ts)
245+
**Error message**: `Missing "else" in "if-else" statement`
246+
247+
`else` case is required in an if statement before Source 3
248+
```javascript
249+
if (true) {
250+
display("hello world");
251+
}
252+
```
253+
An error will be thrown around the body of the if statement.
254+
255+
### [No module name](rules/importDeclaration.ts)
256+
**Error message**: `Expected module name`
257+
258+
Imports require the module name.
259+
```javascript
260+
import { black }
261+
```
262+
An error will be thrown around the whole import statement.
263+
264+
### [Missing closing string](rules/literal.ts)
265+
**Error message**: `Incomplete string expression`
266+
267+
Strings must be closed within the same line
268+
```javascript
269+
"hello world
270+
";
271+
```
272+
An error will be thrown on both lines.
273+
274+
### [No null literals](rules/literal.ts)
275+
**Error message**: `Null literals are not allowed`
276+
277+
Nulls are not allowed in Source 1.
278+
```javascript
279+
display(null);
280+
```
281+
An error will be thrown around `null`.
282+
283+
### [No member access](rules/memberExpression.ts)
284+
**Error message**: `Member access expressions are not allowed`
285+
286+
Member access with square brackets `[]` is not allowed before source 3.
287+
```javascript
288+
"hello world"[0];
289+
```
290+
An error will be thrown around the whole expression.
291+
292+
### [No dot abbreviations](rules/memberExpression.ts)
293+
**Error message**: `No dot abbreviations`
294+
295+
Source prohibits usage of dot access in any case.
296+
```javascript
297+
display("hello world".length);
298+
```
299+
An error will be thrown around the whole expression.
300+
301+
### [Non integer member access](rules/memberExpression.ts)
302+
**Error message**: `Expected integer as array index, got ${type}`
303+
304+
Array access must use non negative integers
305+
```javascript
306+
const arr = [1,2,3];
307+
arr[undefined]; // Expected non negative integer as array index, got undefined.
308+
arr["test"]; // Expected non negative integer as array index, got string.
309+
arr[1.1]; // Expected non negative integer as array index, got float.
310+
arr[-1]; // Expected non negative integer as array index, got negative number.
311+
```
312+
Errors are shown in the comments.
313+
314+
### [No return value](rules/returnStatement.ts)
315+
**Error message**: `Missing value in return statement`
316+
317+
Returns statements must return a value in Source.
318+
```javascript
319+
function foo() {
320+
return;
321+
}
322+
```
323+
An error will be thrown around the `return` statement.
324+
325+
### [No spread syntax](rules/spreadElement.ts)
326+
**Error message**: `Spread syntax is only allowed when supplying arguments to a function`
327+
328+
The spread syntax `...` can only be used directly inside a function call as a parameter.
329+
```javascript
330+
const x = [1,2,3];
331+
display(list(...x)); // Valid
332+
display(list((...x)); // Not valid
333+
```
334+
The second use of the spread syntax will throw an error.
335+
336+
### [No expressions in template literals](rules/templateLiteral.ts)
337+
**Error message**: `Expressions not allowed in template literal`
338+
339+
In Source, template literals are only used for multiline string, they cannot contain expressions.
340+
```javascript
341+
const x = `10 * 10 = ${10*10}`
342+
```
343+
The expression `10*10` will throw an error.
344+
345+
### [No multiple declarations](rules/variableDeclaration.ts)
346+
**Error message**: `Multiple declarations not allowed`
347+
348+
In Source, only 1 declaration is allowed in each variable declarator.
349+
```javascript
350+
const x = 1, y = 2, z = 3;
351+
```
352+
The whole statement will throw an error.
353+
354+
### [Incomplete variable declaration](rules/variableDeclaration.ts)
355+
**Error message**: `Incomplete variable declaration`
356+
357+
Detects whether left and right side of variable declaration in present;
358+
```javascript
359+
const x;
360+
```
361+
The whole statement will throw an error.
362+
363+
### [No let declaration](rules/variableDeclaration.ts)
364+
**Error message**: `Use keyword "const" instead to declare a constant`
365+
366+
Before Source 3, variables can only be declared with `const`
367+
```javascript
368+
let x = 1;
369+
```
370+
The whole statement will throw an error.
371+
372+
### [No while statement](rules/whileStatement.ts)
373+
**Error message**: `While statements are not allowed`
374+
375+
Before Source 3, while statements are not allowed.
376+
```javascript
377+
while(true) {
378+
display("hi")
379+
}
380+
```
381+
`while(...)` will throw an error.
382+

0 commit comments

Comments
 (0)