You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/docs/curriculum-help.mdx
+359Lines changed: 359 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1722,3 +1722,362 @@ def add(
1722
1722
):
1723
1723
return x + y
1724
1724
```
1725
+
1726
+
## TypeScript Helpers
1727
+
1728
+
These helpers provide Abstract Syntax Tree (AST) analysis capabilities for TypeScript challenges.
1729
+
1730
+
### Basic Usage
1731
+
1732
+
`Explorer` is a chainable class that allows you to call methods on the result of parsing a string. Here's how to create an instance of `Explorer` that parses the camper's code:
1733
+
1734
+
```js
1735
+
constexplorer=await__helpers.Explorer(code);
1736
+
```
1737
+
1738
+
To access a specific statement within the code you need to chain method calls until you reach the desired scope. For example, if the camper has written the following code:
1739
+
1740
+
```ts
1741
+
classSpam {
1742
+
get42():number {
1743
+
return42;
1744
+
}
1745
+
}
1746
+
```
1747
+
1748
+
To check the return type annotation you would write:
Returns `true` if the Explorer instance has no AST node, otherwise returns `false`.
1762
+
1763
+
```js
1764
+
constexplorer=await__helpers.Explorer('const a = 1;');
1765
+
explorer.isEmpty(); // false
1766
+
```
1767
+
1768
+
#### `toString()`
1769
+
1770
+
Returns the source code representation of the current node, or `"no ast"` if the node is empty.
1771
+
1772
+
```js
1773
+
constexplorer=await__helpers.Explorer('const a = 1;');
1774
+
explorer.toString(); // "const a = 1;"
1775
+
```
1776
+
1777
+
#### `matches(other: string | Explorer)`
1778
+
1779
+
Compares the current tree with another tree or string, ignoring semicolons and irrelevant whitespace differences.
1780
+
1781
+
```js
1782
+
constexplorer=await__helpers.Explorer('const a = 1;');
1783
+
explorer.matches('const a = 1'); // true (ignores whitespace and semicolons)
1784
+
explorer.matches('const b = 1;'); // false
1785
+
```
1786
+
1787
+
#### `variables`
1788
+
1789
+
Returns an object mapping variable names to `Explorer` instances for all variables in the current scope.
1790
+
1791
+
```js
1792
+
constexplorer=await__helpers.Explorer(
1793
+
'var a = 1; let b = 2; const c = () => {}'
1794
+
);
1795
+
const { a, b, c } =explorer.variables; // { a: Explorer, b: Explorer, c: Explorer }
1796
+
a.matches('var a = 1;'); // true
1797
+
b.matches('let b = 2;'); // true
1798
+
c.matches('const c = () => {}'); // true
1799
+
```
1800
+
1801
+
#### `value`
1802
+
1803
+
Returns an `Explorer` instance representing the assigned value of a variable, property, parameter, or property assignment. Returns an empty `Explorer` if there is no initializer.
1804
+
1805
+
```js
1806
+
constexplorer=await__helpers.Explorer('const a = 1; const b = { x: 10 };');
1807
+
const { a, b } =explorer.variables;
1808
+
a.value.toString(); // "1"
1809
+
b.value.matches('{ x: 10 }'); // true
1810
+
```
1811
+
1812
+
#### `objectProps`
1813
+
1814
+
Returns an object mapping property names to `Explorer` instances for all properties in an object literal.
1815
+
1816
+
```js
1817
+
constexplorer=await__helpers.Explorer(
1818
+
"const obj = { x: 1, y: 'hello', z: true };"
1819
+
);
1820
+
const { obj } =explorer.variables;
1821
+
constprops=obj.value.objectProps; // { x: Explorer, y: Explorer, z: Explorer }
1822
+
```
1823
+
1824
+
#### `functions` and `allFunctions`
1825
+
1826
+
`functions` returns function declarations only. `allFunctions` also includes arrow functions and function expressions assigned to variables.
Returns an object mapping property names to `Explorer` instances for all properties in a type, interface, or type literal.
1964
+
1965
+
```js
1966
+
constexplorer=await__helpers.Explorer(
1967
+
'type Foo = { x: number; y: string; };'
1968
+
);
1969
+
const { Foo } =explorer.types;
1970
+
const { x, y } =Foo.typeProps; // { x: Explorer, y: Explorer }
1971
+
```
1972
+
1973
+
#### `hasTypeProps(props: TypeProp | TypeProp[])`
1974
+
1975
+
Returns `true` if all specified properties exist in a type, interface, or type literal, with optional type and optionality verification. `TypeProp` is an object with the form `{ name: string; type?: string; isOptional?: boolean }`.
0 commit comments