Skip to content

Commit c9540be

Browse files
committed
Update documentations
1 parent dc932c4 commit c9540be

File tree

3 files changed

+63
-20
lines changed

3 files changed

+63
-20
lines changed

docs/src/modules/1-getting-started/3-git.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Make sure that the branch is named appropriately. For example, if you were creat
3838

3939
## Procedure for Pull Requests
4040

41-
When you're ready for your work to be incorporated into the `master` branch, open a pull request with the modules repository to merge your
41+
When you're ready for your work to be incorporated into the `master` branch, [open](https://github.com/source-academy/modules/compare) a pull request with the modules repository to merge your
4242
changes into `master` branch. Make sure that your branch is up-to-date with the `master` branch (by performing a `git merge` from the `master` branch to your own branch).
4343

4444
This will automatically trigger the repository's workflows, which will verify that your changes don't break any of the existing code. If the
@@ -57,4 +57,5 @@ the `dist` folders are ignored for all bundles and tabs, but if you need to add
5757
directory.
5858

5959
> [!TIP]
60-
> If you want to remove a file that been committed in the past you can use `git rm`
60+
> If you want to remove a file that been committed in the past you can use `git rm` or\
61+
> `git rm --cached`.

docs/src/modules/2-bundle/4-conventions/1-basic.md

Lines changed: 54 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export function exposed_function() {
2727
Neither default nor rest parameters are currently supported due to an [issue](https://github.com/source-academy/js-slang/issues/1238) on the `js-slang` side.
2828
:::
2929

30-
## 2. Cadet facing functions should not use array destructuring for parameters
30+
## 2. Cadet facing functions should not use destructuring for parameters
3131

3232
Javascript allows us to destruct iterables directly within a function's parameters:
3333

@@ -48,36 +48,72 @@ function foo([x, y]) {
4848
TypeError: number 0 is not iterable (cannot read property Symbol(Symbol.iterator))
4949
```
5050
51-
If instead the parameter isn't destructured, it gives you the chance to perform type checking:
52-
51+
Javascript also supports object destructuring in parameters:
5352
```ts
54-
export function foo(arr: [string, string]) {
55-
if (!Array.isArray(arr)) throw new Error();
56-
return arr[0];
53+
interface BarParams {
54+
x: string;
55+
y: string;
5756
}
58-
```
5957

60-
More information about throwing errors and why this kind of type checking is important can be found [here](./3-errors#source-type-checking).
61-
62-
::: details What about Object Destructuring?
63-
Javascript also supports object destructuring in parameters:
64-
```js
65-
function foo({ x, y }) {
58+
function bar({ x, y }: BarParams) {
6659
return x;
6760
}
6861
```
6962
70-
However, Javascript doesn't actually throw an error if you pass an invalid object into this function:
71-
```js
72-
function foo({ x, y }) {
63+
However, Javascript doesn't actually throw an error if you pass an invalid object into the function:
64+
```ts
65+
function bar({ x, y }: BarParams) {
7366
return x;
7467
}
7568
76-
console.log(foo(0)); // prints undefined
69+
console.log(bar(0)); // prints undefined
7770
```
7871
7972
If an invalid argument gets passed, no error is thrown and the destructured values just take on the value of `undefined` (which you might want to check for).
80-
:::
73+
74+
However, if you use nested destructuring, Javascript _will_ throw an error:
75+
```ts
76+
interface Bar2Params {
77+
x: {
78+
a: string;
79+
b: string;
80+
};
81+
}
82+
83+
function bar2({ x: { a, b } }: Bar2Params) {
84+
return a;
85+
}
86+
87+
console.log(bar2(0));
88+
```
89+
90+
The call to `bar2` causes an error like the one below:
91+
```sh
92+
Uncaught TypeError: Cannot read properties of undefined (reading 'a')
93+
at bar2 (<anonymous>:1:21)
94+
at <anonymous>:1:1
95+
```
96+
because of course, when `bar2` is called with `0`, `x` becomes `undefined` and trying to destructure `undefined` causes the `TypeError`.
97+
98+
If instead the parameter isn't destructured, it gives you the chance to perform type checking:
99+
100+
```ts
101+
export function foo(arr: [string, string]) {
102+
if (!Array.isArray(arr)) throw new Error();
103+
return arr[0];
104+
}
105+
106+
export function bar2(obj: Bar2Params) {
107+
if (typeof obj !== 'object' || !('x' in obj)) {
108+
// throw an error....
109+
}
110+
111+
return obj.x;
112+
}
113+
```
114+
115+
> [!IMPORTANT]
116+
> More information about throwing errors and why this kind of type checking is important can be found [here](./3-errors#source-type-checking).
81117
82118
## 3. If your bundle requires specific Source features, make sure to indicate it in the manifest
83119

docs/src/modules/2-bundle/4-conventions/2-abstractions.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ but if your circumstances can't support it you can refer to the second method wh
9595
> This means that is is unnecessary to implement `ReplResult` for any of your top-level functions. You can still override this automatic functionality by implementing
9696
> `ReplResult`.
9797
98+
> [!INFO]
99+
> The `ReplResult` interface can be imported from `@sourceacademy/modules-lib/types`. To use it, you will have to
100+
> add the `@sourceacademy/modules-lib` package to your dependencies.
101+
>
102+
> Using the interface as exported will be helpful for type checking, but it is not necessary.
103+
98104
### Implementing `ReplResult` directly
99105
100106
The simplest way to implement the interface is to do it in Typescript. For example, the `curve` bundle has a `Point` class, which is an abstraction of a point in 3D space with a color value:

0 commit comments

Comments
 (0)