Skip to content

Commit ef4e1d3

Browse files
committed
Docs are now online
1 parent a1022e4 commit ef4e1d3

File tree

8 files changed

+210
-25
lines changed

8 files changed

+210
-25
lines changed

docs/rules/array-init-style.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# array-init-style
2+
3+
> Enforce using Array constructor for empty array initialization
4+
5+
## Rule Details
6+
7+
In AssemblyScript, using the literal `[]` to create empty arrays creates a temporary object in the data section, which may lead to performance issues. This rule enforces using the `new Array<T>()` constructor to initialize empty arrays.
8+
9+
## Rule Options
10+
11+
This rule has no configuration options.
12+
13+
## Examples
14+
15+
### Incorrect
16+
17+
```ts
18+
let arr: i32[] = []; // not recommended
19+
```
20+
21+
### Correct
22+
23+
```ts
24+
let arr: i32[] = new Array<i32>(); // recommended
25+
```

docs/rules/dont-omit-else.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# dont-omit-else
2+
3+
> Enforce using else block when if branch doesn't contain control flow statement
4+
5+
## Rule Details
6+
7+
In AssemblyScript, it's recommended to explicitly handle all possible execution paths for better code clarity and safety. This rule enforces using an `else` block when the `if` branch doesn't contain a statement that alters the control flow, such as `return`, `throw`, `break`, or `continue`.
8+
9+
## Rule Options
10+
11+
This rule has no configuration options.
12+
13+
## Examples
14+
15+
### Incorrect
16+
17+
```ts
18+
if (condition) {
19+
doSomething();
20+
} // missing else
21+
```
22+
23+
### Correct
24+
25+
```ts
26+
// With else block
27+
if (condition) {
28+
doSomething();
29+
} else {
30+
doSomethingElse();
31+
}
32+
33+
// Or with early exit in if branch
34+
if (condition) {
35+
doSomething();
36+
return; // Control flow statement makes else unnecessary
37+
}
38+
// Execution continues here only if condition is false
39+
```
40+
41+
## When Not To Use
42+
43+
You can disable this rule if you prefer a coding style that doesn't require explicit else blocks for all conditional statements.
44+
45+
## Related Rules
46+
47+
None

docs/rules/no-spread.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# no-spread
2+
3+
> Disallow spread syntax which is not supported in AssemblyScript
4+
5+
## Rule Details
6+
7+
AssemblyScript does not support the spread syntax (`...var`) for function calls or array literals. This rule identifies and reports any use of spread syntax in your code.
8+
9+
## Rule Options
10+
11+
This rule has no configuration options.
12+
13+
## Examples
14+
15+
### Incorrect
16+
17+
```ts
18+
// Using spread in function calls
19+
foo(1, ...bar);
20+
21+
// Using spread in array literals
22+
const newArray = [...oldArray, 1, 2, 3];
23+
```
24+
25+
### Correct
26+
27+
```ts
28+
// Use direct arguments instead
29+
foo(1, bar);
30+
31+
// Use array methods or explicit assignments
32+
const newArray = new Array<i32>(oldArray.length + 3);
33+
for (let i = 0; i < oldArray.length; i++) {
34+
newArray[i] = oldArray[i];
35+
}
36+
newArray[oldArray.length] = 1;
37+
newArray[oldArray.length + 1] = 2;
38+
newArray[oldArray.length + 2] = 3;
39+
```
40+
41+
## When Not To Use
42+
43+
You should not disable this rule in AssemblyScript code, as using spread syntax will cause compilation errors.
44+
45+
## Related Rules
46+
47+
- [array-init-style](./array-init-style.md)

docs/rules/no-unsupported-keyword.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# no-unsupported-keyword
2+
3+
> Disallow TypeScript keywords that are not supported in AssemblyScript
4+
5+
## Rule Details
6+
7+
AssemblyScript is a subset of TypeScript that compiles to WebAssembly, but it doesn't support all TypeScript features. This rule identifies and reports the use of TypeScript keywords that are not supported in AssemblyScript, specifically:
8+
9+
- `never` type
10+
- `any` type
11+
- `undefined` type
12+
13+
## Rule Options
14+
15+
This rule has no configuration options.
16+
17+
## Examples
18+
19+
### Incorrect
20+
21+
```ts
22+
// Using 'never' type
23+
function alwaysThrows(): never {
24+
throw new Error('This function never returns');
25+
}
26+
27+
// Using 'any' type
28+
function acceptsAnything(param: any) {
29+
return param;
30+
}
31+
32+
// Using 'undefined' type
33+
let maybeValue: string | undefined;
34+
```
35+
36+
### Correct
37+
38+
```ts
39+
// Use specific return types instead of 'never'
40+
function alwaysThrows(): void {
41+
throw new Error('This function never returns');
42+
}
43+
44+
// Use explicit types instead of 'any'
45+
function acceptsString(param: string): string {
46+
return param;
47+
}
48+
49+
// Use nullable types instead of undefined
50+
let maybeValue: string | null;
51+
```
52+
53+
## When Not To Use
54+
55+
You should not disable this rule in AssemblyScript code, as using these unsupported keywords will cause compilation errors.
56+
57+
## Related Rules
58+
59+
None

plugins/rules/array-init-style.ts

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -36,32 +36,26 @@ const arrayInitStyle: ESLintUtils.RuleModule<
3636
const typeAnnotation = node.id.typeAnnotation?.typeAnnotation;
3737
if (
3838
typeAnnotation?.type === "TSArrayType" &&
39-
typeAnnotation.elementType?.type === "TSTypeReference"
39+
typeAnnotation.elementType?.type === "TSTypeReference" &&
40+
node.init?.type === "ArrayExpression" &&
41+
node.init.elements.length === 0
4042
) {
41-
if (
42-
node.init?.type === "ArrayExpression" &&
43-
node.init.elements.length === 0
44-
) {
45-
// Ensure node.init is not null before passing to fixer
46-
const initNode = node.init;
47-
const elementType = context.sourceCode.getText(
48-
typeAnnotation.elementType.typeName
49-
);
50-
context.report({
51-
node,
52-
messageId: "preferArrayConstructor",
53-
data: {
54-
type: elementType,
55-
},
56-
fix(fixer) {
57-
// initNode is guaranteed non-null here due to the outer check
58-
return fixer.replaceText(
59-
initNode,
60-
`new Array<${elementType}>()`
61-
);
62-
},
63-
});
64-
}
43+
// Ensure node.init is not null before passing to fixer
44+
const initNode = node.init;
45+
const elementType = context.sourceCode.getText(
46+
typeAnnotation.elementType.typeName
47+
);
48+
context.report({
49+
node,
50+
messageId: "preferArrayConstructor",
51+
data: {
52+
type: elementType,
53+
},
54+
fix(fixer) {
55+
// initNode is guaranteed non-null here due to the outer check
56+
return fixer.replaceText(initNode, `new Array<${elementType}>()`);
57+
},
58+
});
6559
}
6660
},
6761
};

plugins/rules/dont-omit-else.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import createRule from "../utils/create-rule.js";
55
/**
66
* Rule: Dont Omit Else
77
* Enforce using else block when if branch doesn't contain control flow statement.
8+
* BAD
9+
* if (x) { }
10+
* GOOD
11+
* if (x) { } else { }
812
*/
913
const dontOmitElse: RuleModule<"omittedElse", [], unknown, RuleListener> =
1014
createRule({

plugins/rules/no-spread.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import createRule from "../utils/create-rule.js";
55
* Rule: No Spread
66
* Reject usages of ...var on call expressions, as spread syntax
77
* is not supported in AssemblyScript.
8+
* BAD:
9+
* foo(1, ...bar)
10+
* GOOD:
11+
* foo(1, bar)
812
*/
913
const noSpread: ESLintUtils.RuleModule<
1014
"noSpreadMsg",

plugins/rules/no-unsupported-keyword.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ import createRule from "../utils/create-rule.js";
44
/**
55
* Rule: No Unsupported Keywords
66
* Reject usage of TypeScript keywords that are not supported in AssemblyScript.
7+
* BAD:
8+
* function foo(): never { throw new Error('never'); }
9+
* function foo(a: any) {}
10+
* GOOD:
11+
* let foo: string;
712
*/
813
const noUnsupportedKeyword: ESLintUtils.RuleModule<
914
string,

0 commit comments

Comments
 (0)