@barqjs/compiler 0.0.0-stage → 0.2.0-next.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +225 -2
- package/docs/BARQ001.md +55 -0
- package/docs/BARQ002.md +53 -0
- package/docs/BARQ003.md +58 -0
- package/docs/BARQ004.md +116 -0
- package/docs/BARQ005.md +92 -0
- package/docs/BARQ006.md +23 -0
- package/docs/BARQ008.md +43 -0
- package/docs/BARQ009.md +48 -0
- package/docs/BARQ010.md +76 -0
- package/docs/BARQ011.md +88 -0
- package/docs/BARQ012.md +81 -0
- package/docs/BARQ013.md +63 -0
- package/docs/BARQ014.md +61 -0
- package/docs/BARQ015.md +64 -0
- package/docs/BARQ016.md +52 -0
- package/docs/BARQ017.md +63 -0
- package/docs/BARQ018.md +116 -0
- package/docs/BARQ019.md +109 -0
- package/docs/BARQ020.md +78 -0
- package/docs/BARQ021.md +79 -0
- package/docs/BARQ022.md +57 -0
- package/docs/BARQ023.md +55 -0
- package/docs/BARQ024.md +58 -0
- package/docs/BARQ025.md +74 -0
- package/docs/BARQ026.md +68 -0
- package/docs/BARQ027.md +53 -0
- package/docs/BARQ028.md +56 -0
- package/docs/BARQ029.md +57 -0
- package/docs/README.md +129 -0
- package/index.d.ts +563 -0
- package/index.js +89 -0
- package/package.json +86 -4
package/docs/BARQ018.md
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
# BARQ018: A component decides what to return from a reactive value it reads once
|
|
2
|
+
|
|
3
|
+
**Level:** warning · [all codes](README.md)
|
|
4
|
+
|
|
5
|
+
## Message
|
|
6
|
+
|
|
7
|
+
> `Greeting` reads `user()` while its body runs and builds what it returns from that one
|
|
8
|
+
> value. A component body is not a tracked scope: it does not run again when `user()`
|
|
9
|
+
> changes, so the page keeps what it showed first. Make the choice in the returned JSX,
|
|
10
|
+
> where it stays live: `<Show when={…} fallback={…}>…</Show>`.
|
|
11
|
+
|
|
12
|
+
## Failing example
|
|
13
|
+
|
|
14
|
+
```tsx
|
|
15
|
+
import { createSignal } from "@barqjs/core";
|
|
16
|
+
|
|
17
|
+
const [user, setUser] = createSignal<string | null>(null);
|
|
18
|
+
|
|
19
|
+
export function Greeting() {
|
|
20
|
+
if (!user()) return <button onClick={() => setUser("salama")}>Sign in</button>;
|
|
21
|
+
return <p>Hello, {user()}</p>;
|
|
22
|
+
}
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
```tsx
|
|
26
|
+
import { createSignal } from "@barqjs/core";
|
|
27
|
+
|
|
28
|
+
const [online] = createSignal(true);
|
|
29
|
+
|
|
30
|
+
function Status(props) {
|
|
31
|
+
return props.online() ? <b>online</b> : <i>offline</i>;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export const Bar = () => <Status online={online()} />;
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Fixed example
|
|
38
|
+
|
|
39
|
+
```tsx
|
|
40
|
+
import { Show, createSignal } from "@barqjs/core";
|
|
41
|
+
|
|
42
|
+
const [user, setUser] = createSignal<string | null>(null);
|
|
43
|
+
|
|
44
|
+
export function Greeting() {
|
|
45
|
+
return (
|
|
46
|
+
<Show when={user()} fallback={<button onClick={() => setUser("salama")}>Sign in</button>}>
|
|
47
|
+
<p>Hello, {user()}</p>
|
|
48
|
+
</Show>
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
```tsx
|
|
54
|
+
import { createSignal } from "@barqjs/core";
|
|
55
|
+
|
|
56
|
+
const [online] = createSignal(true);
|
|
57
|
+
|
|
58
|
+
function Status(props) {
|
|
59
|
+
return <>{props.online() ? <b>online</b> : <i>offline</i>}</>;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export const Bar = () => <Status online={online()} />;
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
A value the component should read once on purpose says so with `untrack`, which this rule
|
|
66
|
+
accepts:
|
|
67
|
+
|
|
68
|
+
```tsx
|
|
69
|
+
import { createSignal, untrack } from "@barqjs/core";
|
|
70
|
+
|
|
71
|
+
const [user] = createSignal<string | null>("salama");
|
|
72
|
+
|
|
73
|
+
export function Greeting() {
|
|
74
|
+
if (!untrack(user)) return <p>Signed out when this mounted</p>;
|
|
75
|
+
return <p>Hello, {user()}</p>;
|
|
76
|
+
}
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## Why this rule exists
|
|
80
|
+
|
|
81
|
+
A component body is not a tracked scope: it runs when the component is created and does
|
|
82
|
+
not run again. An `if`, a `switch` or a `return` that reads a signal there reads it once,
|
|
83
|
+
so the component keeps returning what it chose first while the signal moves on. Nothing
|
|
84
|
+
throws, and the page simply stops following the state. Where the component happens to be
|
|
85
|
+
created inside a reactive expression, the expression rebuilds the whole component instead,
|
|
86
|
+
and whatever the user did inside it is lost.
|
|
87
|
+
|
|
88
|
+
JSX is where a read stays live, because the compiler turns each JSX expression into its
|
|
89
|
+
own tracked scope. `<Show>`, `<Switch>` and `<For>`, or a plain `{cond ? <A /> : <B />}`
|
|
90
|
+
inside the returned element, make the same choice there.
|
|
91
|
+
|
|
92
|
+
The rule reports reads of signals, memos, stores and functions that read them. A prop is
|
|
93
|
+
reported only when this file renders the component with a value that changes, such as
|
|
94
|
+
`<Status online={online()} />`: every prop is a function you call, whether or not its
|
|
95
|
+
caller passes anything that moves, and reading a configuration prop such as an element
|
|
96
|
+
type once is ordinary. So a component rendered only from other files is not checked for
|
|
97
|
+
its props.
|
|
98
|
+
|
|
99
|
+
## Silencing one line
|
|
100
|
+
|
|
101
|
+
```tsx
|
|
102
|
+
import { createSignal } from "@barqjs/core";
|
|
103
|
+
|
|
104
|
+
const [user, setUser] = createSignal<string | null>(null);
|
|
105
|
+
|
|
106
|
+
export function Greeting() {
|
|
107
|
+
// barq-ignore-next-line BARQ018 (the parent remounts this when the user changes)
|
|
108
|
+
if (!user()) return <button onClick={() => setUser("salama")}>Sign in</button>;
|
|
109
|
+
return <p>Hello, {user()}</p>;
|
|
110
|
+
}
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
The reason in parentheses is required and has to be at least 10 characters. Between JSX
|
|
114
|
+
children, write the comment as `{/* barq-ignore-next-line BARQ018 (reason) */}` on the line
|
|
115
|
+
above: a `//` line there is text and would be rendered on the page. To change the level
|
|
116
|
+
for the whole project, see [the index](README.md#changing-a-level-for-the-whole-project).
|
package/docs/BARQ019.md
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
# BARQ019: State is written while a component body or a computation runs
|
|
2
|
+
|
|
3
|
+
**Level:** warning · [all codes](README.md)
|
|
4
|
+
|
|
5
|
+
## Message
|
|
6
|
+
|
|
7
|
+
> `setCount` writes reactive state while `Counter`'s body runs. Development builds throw
|
|
8
|
+
> `REACTIVE_WRITE_IN_OWNED_SCOPE` here, so this code stops working when it runs in
|
|
9
|
+
> development. Derive the value instead (an initial value, or `createMemo(() => …)`), or
|
|
10
|
+
> write it from an event handler, `onSettled`, or the second function of `createEffect`.
|
|
11
|
+
|
|
12
|
+
## Failing example
|
|
13
|
+
|
|
14
|
+
```tsx
|
|
15
|
+
import { createSignal } from "@barqjs/core";
|
|
16
|
+
|
|
17
|
+
export function Counter() {
|
|
18
|
+
const [count, setCount] = createSignal(0);
|
|
19
|
+
setCount(1);
|
|
20
|
+
return <button onClick={() => setCount(count() + 1)}>{count()}</button>;
|
|
21
|
+
}
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
```tsx
|
|
25
|
+
import { createMemo, createSignal } from "@barqjs/core";
|
|
26
|
+
|
|
27
|
+
const [price] = createSignal(10);
|
|
28
|
+
const [total, setTotal] = createSignal(0);
|
|
29
|
+
|
|
30
|
+
export const sync = createMemo(() => setTotal(price() * 2));
|
|
31
|
+
export const Total = () => <output>{total()}</output>;
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Fixed example
|
|
35
|
+
|
|
36
|
+
```tsx
|
|
37
|
+
import { createSignal } from "@barqjs/core";
|
|
38
|
+
|
|
39
|
+
export function Counter() {
|
|
40
|
+
const [count, setCount] = createSignal(1);
|
|
41
|
+
return <button onClick={() => setCount(count() + 1)}>{count()}</button>;
|
|
42
|
+
}
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
```tsx
|
|
46
|
+
import { createMemo, createSignal } from "@barqjs/core";
|
|
47
|
+
|
|
48
|
+
const [price] = createSignal(10);
|
|
49
|
+
const total = createMemo(() => price() * 2);
|
|
50
|
+
|
|
51
|
+
export const Total = () => <output>{total()}</output>;
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
A write that has to follow a change goes in the second function of `createEffect`, which
|
|
55
|
+
runs after the update has settled:
|
|
56
|
+
|
|
57
|
+
```tsx
|
|
58
|
+
import { createEffect, createSignal } from "@barqjs/core";
|
|
59
|
+
|
|
60
|
+
const [price] = createSignal(10);
|
|
61
|
+
const [history, setHistory] = createSignal<number[]>([]);
|
|
62
|
+
|
|
63
|
+
createEffect(
|
|
64
|
+
() => price(),
|
|
65
|
+
(value) => {
|
|
66
|
+
setHistory((list) => [...list, value]);
|
|
67
|
+
},
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
export const History = () => <output>{history().join(", ")}</output>;
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Why this rule exists
|
|
74
|
+
|
|
75
|
+
The engine refuses a write under an owner. A component body, a JSX expression, and the
|
|
76
|
+
function `createMemo` or `createEffect` runs to compute a value all run under one, and
|
|
77
|
+
development builds throw `REACTIVE_WRITE_IN_OWNED_SCOPE` (or `ACTION_CALLED_IN_OWNED_SCOPE`
|
|
78
|
+
for an action) the moment the write happens. Production builds do not check, so the same
|
|
79
|
+
code renders there and then fails as soon as it runs in development. A write in the
|
|
80
|
+
middle of an update is also how a computation ends up feeding itself.
|
|
81
|
+
|
|
82
|
+
The fix is almost always to derive: an initial value instead of a write in the body, a
|
|
83
|
+
`createMemo` that returns the value instead of one that writes it. A write that answers a
|
|
84
|
+
change belongs in an event handler, in `onSettled`, or in the second function of
|
|
85
|
+
`createEffect`.
|
|
86
|
+
|
|
87
|
+
The rule knows a setter by the `createSignal`, `createStore`, `createOptimistic` or
|
|
88
|
+
`createOptimisticStore` call it came from, and an action by its `action` call, in the same
|
|
89
|
+
file. A signal created with `{ ownedWrite: true }` opts into these writes and is not
|
|
90
|
+
reported, and neither is one whose options the compiler cannot read. A write inside a
|
|
91
|
+
helper function that the body calls is not seen.
|
|
92
|
+
|
|
93
|
+
## Silencing one line
|
|
94
|
+
|
|
95
|
+
```tsx
|
|
96
|
+
import { createSignal } from "@barqjs/core";
|
|
97
|
+
|
|
98
|
+
export function Counter() {
|
|
99
|
+
const [count, setCount] = createSignal(0);
|
|
100
|
+
// barq-ignore-next-line BARQ019 (production only, the dev check is known)
|
|
101
|
+
setCount(1);
|
|
102
|
+
return <button onClick={() => setCount(count() + 1)}>{count()}</button>;
|
|
103
|
+
}
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
The reason in parentheses is required and has to be at least 10 characters. Between JSX
|
|
107
|
+
children, write the comment as `{/* barq-ignore-next-line BARQ019 (reason) */}` on the line
|
|
108
|
+
above: a `//` line there is text and would be rendered on the page. To change the level
|
|
109
|
+
for the whole project, see [the index](README.md#changing-a-level-for-the-whole-project).
|
package/docs/BARQ020.md
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
# BARQ020: A store is destructured in a component body, so each name is a snapshot
|
|
2
|
+
|
|
3
|
+
**Level:** warning · [all codes](README.md)
|
|
4
|
+
|
|
5
|
+
## Message
|
|
6
|
+
|
|
7
|
+
> `Badge` takes the store `cart` apart in its body, so each name holds the value its
|
|
8
|
+
> property had when the body ran. A component body is not a tracked scope, and a later
|
|
9
|
+
> write to the store does not reach these names. Read the property where it is used, for
|
|
10
|
+
> example `cart.count` inside the JSX.
|
|
11
|
+
|
|
12
|
+
## Failing example
|
|
13
|
+
|
|
14
|
+
```tsx
|
|
15
|
+
import { createStore } from "@barqjs/core";
|
|
16
|
+
|
|
17
|
+
const [cart, setCart] = createStore({ count: 0, owner: "salama" });
|
|
18
|
+
|
|
19
|
+
export function Badge() {
|
|
20
|
+
const { count, owner } = cart;
|
|
21
|
+
return (
|
|
22
|
+
<button onClick={() => setCart((draft) => {
|
|
23
|
+
draft.count += 1;
|
|
24
|
+
})}>
|
|
25
|
+
{owner}: {count}
|
|
26
|
+
</button>
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Fixed example
|
|
32
|
+
|
|
33
|
+
```tsx
|
|
34
|
+
import { createStore } from "@barqjs/core";
|
|
35
|
+
|
|
36
|
+
const [cart, setCart] = createStore({ count: 0, owner: "salama" });
|
|
37
|
+
|
|
38
|
+
export function Badge() {
|
|
39
|
+
return (
|
|
40
|
+
<button onClick={() => setCart((draft) => {
|
|
41
|
+
draft.count += 1;
|
|
42
|
+
})}>
|
|
43
|
+
{cart.owner}: {cart.count}
|
|
44
|
+
</button>
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Why this rule exists
|
|
50
|
+
|
|
51
|
+
Reading a store property is a reactive read, and destructuring reads every property it
|
|
52
|
+
names, at that moment. In a component body nothing tracks that read, so `count` is the
|
|
53
|
+
number the store held when the component was created, and every later write to
|
|
54
|
+
`cart.count` passes it by. Read the property where it is used: `{cart.count}` in JSX is a
|
|
55
|
+
tracked read that follows the store.
|
|
56
|
+
|
|
57
|
+
Destructuring a store inside `createMemo`, an effect, or a JSX expression is a tracked
|
|
58
|
+
read and is not reported. Props are not stores: `const { title } = props` copies the
|
|
59
|
+
props' functions, which stay live, and is not reported either.
|
|
60
|
+
|
|
61
|
+
## Silencing one line
|
|
62
|
+
|
|
63
|
+
```tsx
|
|
64
|
+
import { createStore } from "@barqjs/core";
|
|
65
|
+
|
|
66
|
+
const [cart] = createStore({ count: 0, owner: "salama" });
|
|
67
|
+
|
|
68
|
+
export function Badge() {
|
|
69
|
+
// barq-ignore-next-line BARQ020 (a printed receipt shows the totals at mount)
|
|
70
|
+
const { count, owner } = cart;
|
|
71
|
+
return <p>{owner}: {count}</p>;
|
|
72
|
+
}
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
The reason in parentheses is required and has to be at least 10 characters. Between JSX
|
|
76
|
+
children, write the comment as `{/* barq-ignore-next-line BARQ020 (reason) */}` on the line
|
|
77
|
+
above: a `//` line there is text and would be rendered on the page. To change the level
|
|
78
|
+
for the whole project, see [the index](README.md#changing-a-level-for-the-whole-project).
|
package/docs/BARQ021.md
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# BARQ021: An async computation reads a source after its first `await`, which is not tracked
|
|
2
|
+
|
|
3
|
+
**Level:** warning · [all codes](README.md)
|
|
4
|
+
|
|
5
|
+
## Message
|
|
6
|
+
|
|
7
|
+
> `query()` is read after an `await`, and an async computation tracks only what it reads
|
|
8
|
+
> before its first `await`. When it changes, this `createMemo` does not run again and its
|
|
9
|
+
> value stays stale. Read it before the first `await` and keep the value in a local, or
|
|
10
|
+
> wrap the read in `untrack(() => …)` if one read is meant.
|
|
11
|
+
|
|
12
|
+
## Failing example
|
|
13
|
+
|
|
14
|
+
```tsx
|
|
15
|
+
import { createMemo, createSignal } from "@barqjs/core";
|
|
16
|
+
|
|
17
|
+
const [query] = createSignal("");
|
|
18
|
+
|
|
19
|
+
export const results = createMemo(async () => {
|
|
20
|
+
const response = await fetch("https://example.com/items");
|
|
21
|
+
const items = await response.json();
|
|
22
|
+
return items.filter((item) => item.tag === query());
|
|
23
|
+
});
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Fixed example
|
|
27
|
+
|
|
28
|
+
```tsx
|
|
29
|
+
import { createMemo, createSignal } from "@barqjs/core";
|
|
30
|
+
|
|
31
|
+
const [query] = createSignal("");
|
|
32
|
+
|
|
33
|
+
export const results = createMemo(async () => {
|
|
34
|
+
const tag = query();
|
|
35
|
+
const response = await fetch("https://example.com/items");
|
|
36
|
+
const items = await response.json();
|
|
37
|
+
return items.filter((item) => item.tag === tag);
|
|
38
|
+
});
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Why this rule exists
|
|
42
|
+
|
|
43
|
+
A computation records what it reads while it runs. An `async` function stops running at
|
|
44
|
+
its first `await`, and what it does after the promise settles happens in a later task,
|
|
45
|
+
outside the computation. A signal, memo, store or prop read there returns its current
|
|
46
|
+
value, but it is not a dependency: when it changes the computation does not run again,
|
|
47
|
+
and its value stays stale without any error.
|
|
48
|
+
|
|
49
|
+
Read every source before the first `await` and keep the value in a local. A source that is
|
|
50
|
+
also read before the first `await` is tracked and is not reported, and neither is a read
|
|
51
|
+
wrapped in `untrack(() => …)`, which says that one read is meant. A function created after
|
|
52
|
+
the `await` and passed to a call, such as the callback of `filter`, runs after it too and
|
|
53
|
+
is checked; a function returned for someone else to call is not.
|
|
54
|
+
|
|
55
|
+
The rule looks at the function passed to `createMemo`, `createProjection`, the derived
|
|
56
|
+
forms of `createSignal` and `createStore`, and the first function of `createEffect` and
|
|
57
|
+
`createRenderEffect`. Development builds of the engine have a runtime check for the same
|
|
58
|
+
mistake that can follow a read into another function; this one sees it in the file, before
|
|
59
|
+
anything runs.
|
|
60
|
+
|
|
61
|
+
## Silencing one line
|
|
62
|
+
|
|
63
|
+
```tsx
|
|
64
|
+
import { createMemo, createSignal } from "@barqjs/core";
|
|
65
|
+
|
|
66
|
+
const [query] = createSignal("");
|
|
67
|
+
|
|
68
|
+
export const results = createMemo(async () => {
|
|
69
|
+
const response = await fetch("https://example.com/items");
|
|
70
|
+
const items = await response.json();
|
|
71
|
+
// barq-ignore-next-line BARQ021 (the filter is fixed once the list arrives)
|
|
72
|
+
return items.filter((item) => item.tag === query());
|
|
73
|
+
});
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
The reason in parentheses is required and has to be at least 10 characters. Between JSX
|
|
77
|
+
children, write the comment as `{/* barq-ignore-next-line BARQ021 (reason) */}` on the line
|
|
78
|
+
above: a `//` line there is text and would be rendered on the page. To change the level
|
|
79
|
+
for the whole project, see [the index](README.md#changing-a-level-for-the-whole-project).
|
package/docs/BARQ022.md
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# BARQ022: A context is rendered through `.Provider`, which a context does not have
|
|
2
|
+
|
|
3
|
+
**Level:** error · [all codes](README.md)
|
|
4
|
+
|
|
5
|
+
## Message
|
|
6
|
+
|
|
7
|
+
> `Theme` is a context, and a context is its own provider: write `<Theme value={…}>`
|
|
8
|
+
> instead of `<Theme.Provider value={…}>`. A context has no `Provider` member.
|
|
9
|
+
|
|
10
|
+
## Failing example
|
|
11
|
+
|
|
12
|
+
```tsx
|
|
13
|
+
import { createContext } from "@barqjs/core";
|
|
14
|
+
|
|
15
|
+
const Theme = createContext("light");
|
|
16
|
+
|
|
17
|
+
export const App = (props) => <Theme.Provider value="dark">{props.children}</Theme.Provider>;
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Fixed example
|
|
21
|
+
|
|
22
|
+
```tsx
|
|
23
|
+
import { createContext } from "@barqjs/core";
|
|
24
|
+
|
|
25
|
+
const Theme = createContext("light");
|
|
26
|
+
|
|
27
|
+
export const App = (props) => <Theme value="dark">{props.children}</Theme>;
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Why this rule exists
|
|
31
|
+
|
|
32
|
+
In Solid 2.0, and in barq, `createContext` returns the provider component itself, so
|
|
33
|
+
`<Theme value>` provides the value to everything under it. There is no `Theme.Provider`:
|
|
34
|
+
the member is `undefined`, and rendering it throws when the page runs rather than when it
|
|
35
|
+
builds.
|
|
36
|
+
|
|
37
|
+
The check reports a tag whose object is bound to `createContext()` in the same file. A
|
|
38
|
+
context imported from another module is not known to be one, so `<Theme.Provider>` there
|
|
39
|
+
is left to TypeScript, which reports that `Provider` does not exist on `Context`. A
|
|
40
|
+
`Provider` member on anything that is not a context, such as a component namespace, is
|
|
41
|
+
not reported.
|
|
42
|
+
|
|
43
|
+
## Silencing one line
|
|
44
|
+
|
|
45
|
+
```tsx
|
|
46
|
+
import { createContext } from "@barqjs/core";
|
|
47
|
+
|
|
48
|
+
const Theme = createContext("light");
|
|
49
|
+
|
|
50
|
+
// barq-ignore-next-line BARQ022 (Theme.Provider is assigned a component at startup)
|
|
51
|
+
export const App = (props) => <Theme.Provider value="dark">{props.children}</Theme.Provider>;
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
The reason in parentheses is required and has to be at least 10 characters. Between JSX
|
|
55
|
+
children, write the comment as `{/* barq-ignore-next-line BARQ022 (reason) */}` on the line
|
|
56
|
+
above: a `//` line there is text and would be rendered on the page. To change the level
|
|
57
|
+
for the whole project, see [the index](README.md#changing-a-level-for-the-whole-project).
|
package/docs/BARQ023.md
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# BARQ023: `classList` is written on an element, where it is an ordinary attribute
|
|
2
|
+
|
|
3
|
+
**Level:** error · [all codes](README.md)
|
|
4
|
+
|
|
5
|
+
## Message
|
|
6
|
+
|
|
7
|
+
> `classList` is an ordinary attribute name, so this object would be written as the text
|
|
8
|
+
> `[object Object]`. Pass it to `class`, which takes a map of names to whether each
|
|
9
|
+
> applies, or a list of names and maps: `class={["card", { active: on() }]}`.
|
|
10
|
+
|
|
11
|
+
## Failing example
|
|
12
|
+
|
|
13
|
+
```tsx
|
|
14
|
+
import { createSignal } from "@barqjs/core";
|
|
15
|
+
|
|
16
|
+
const [active, setActive] = createSignal(false);
|
|
17
|
+
|
|
18
|
+
export const Card = () => (
|
|
19
|
+
<div class="card" classList={{ active: active() }} onClick={() => setActive(!active())} />
|
|
20
|
+
);
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Fixed example
|
|
24
|
+
|
|
25
|
+
```tsx
|
|
26
|
+
import { createSignal } from "@barqjs/core";
|
|
27
|
+
|
|
28
|
+
const [active, setActive] = createSignal(false);
|
|
29
|
+
|
|
30
|
+
export const Card = () => (
|
|
31
|
+
<div class={["card", { active: active() }]} onClick={() => setActive(!active())} />
|
|
32
|
+
);
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Why this rule exists
|
|
36
|
+
|
|
37
|
+
Solid 2.0 folds `classList` into `class`. A `class` value is a string, a map of class
|
|
38
|
+
names to whether each applies, or a list of strings, maps and further lists, joined in
|
|
39
|
+
order. `classList` has no meaning of its own any more, so on an element it is an attribute
|
|
40
|
+
like `title`, and the object it holds would be written as the attribute's text.
|
|
41
|
+
|
|
42
|
+
The check reports `classList` on an intrinsic element or a custom element. A component
|
|
43
|
+
that declares a `classList` prop of its own is not reported.
|
|
44
|
+
|
|
45
|
+
## Silencing one line
|
|
46
|
+
|
|
47
|
+
```tsx
|
|
48
|
+
// barq-ignore-next-line BARQ023 (the element reads a classList attribute itself)
|
|
49
|
+
export const Widget = () => <legacy-widget classList="compact" />;
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
The reason in parentheses is required and has to be at least 10 characters. Between JSX
|
|
53
|
+
children, write the comment as `{/* barq-ignore-next-line BARQ023 (reason) */}` on the line
|
|
54
|
+
above: a `//` line there is text and would be rendered on the page. To change the level
|
|
55
|
+
for the whole project, see [the index](README.md#changing-a-level-for-the-whole-project).
|
package/docs/BARQ024.md
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# BARQ024: An attribute uses a Solid 1.x namespace such as `on:` or `attr:`
|
|
2
|
+
|
|
3
|
+
**Level:** error · [all codes](README.md)
|
|
4
|
+
|
|
5
|
+
## Message
|
|
6
|
+
|
|
7
|
+
> `on:` is not a namespace, so `on:input` would be written as an attribute of that name.
|
|
8
|
+
> Bind the event with `onInput={handler}`. For an event name that is not one word, or for
|
|
9
|
+
> listener options, add the listener in a ref:
|
|
10
|
+
> `ref={(el) => el.addEventListener("input", handler)}`.
|
|
11
|
+
|
|
12
|
+
The second half names the replacement for the prefix that was written.
|
|
13
|
+
|
|
14
|
+
## Failing example
|
|
15
|
+
|
|
16
|
+
```tsx
|
|
17
|
+
export const Field = (props) => <input on:input={props.onInput} />;
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Fixed example
|
|
21
|
+
|
|
22
|
+
```tsx
|
|
23
|
+
export const Field = (props) => <input onInput={props.onInput} />;
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Every prefix the check reports, and what to write instead:
|
|
27
|
+
|
|
28
|
+
- `on:x`: `onX={handler}`, or `addEventListener` in a ref for a hyphenated name or options.
|
|
29
|
+
- `capture:x`, `oncapture:x`: `onXCapture={handler}`, or `addEventListener` with `{ capture: true }` in a ref.
|
|
30
|
+
- `attr:x`: `x={…}`.
|
|
31
|
+
- `bool:x`: `x={on()}`, where `true` adds the attribute and `false` removes it.
|
|
32
|
+
- `style:x`: a key of the `style` object.
|
|
33
|
+
- `class:x`: a key of the `class` map.
|
|
34
|
+
- `use:x`: `ref={x(…)}`, and `ref={[x(…), y]}` for several.
|
|
35
|
+
|
|
36
|
+
## Why this rule exists
|
|
37
|
+
|
|
38
|
+
Solid 2.0 removed these namespaces: an attribute is written by its own name, events are
|
|
39
|
+
camelCase `on` props, a directive is a ref, and a class or style toggle is a key of the
|
|
40
|
+
`class` or `style` value. A name that still carries one of these prefixes is an ordinary
|
|
41
|
+
attribute whose name contains a colon, so `on:click={handler}` writes the handler's source
|
|
42
|
+
text into an `on:click` attribute and never binds a listener.
|
|
43
|
+
|
|
44
|
+
The check reports these prefixes on an intrinsic element or a custom element. `prop:`,
|
|
45
|
+
which writes a property on a custom element, and `bind:` keep their meaning, and any other
|
|
46
|
+
prefix, such as `xlink:href`, is part of the attribute's name and is not reported.
|
|
47
|
+
|
|
48
|
+
## Silencing one line
|
|
49
|
+
|
|
50
|
+
```tsx
|
|
51
|
+
// barq-ignore-next-line BARQ024 (the widget reads an attribute literally named on:ready)
|
|
52
|
+
export const Widget = () => <legacy-widget on:ready="init" />;
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
The reason in parentheses is required and has to be at least 10 characters. Between JSX
|
|
56
|
+
children, write the comment as `{/* barq-ignore-next-line BARQ024 (reason) */}` on the line
|
|
57
|
+
above: a `//` line there is text and would be rendered on the page. To change the level
|
|
58
|
+
for the whole project, see [the index](README.md#changing-a-level-for-the-whole-project).
|
package/docs/BARQ025.md
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# BARQ025: `<Dynamic>` is rendered, where Solid 2.0 uses the `dynamic` factory
|
|
2
|
+
|
|
3
|
+
**Level:** error · [all codes](README.md)
|
|
4
|
+
|
|
5
|
+
## Message
|
|
6
|
+
|
|
7
|
+
> `<Dynamic>` is not exported by barq: Solid 2.0 deprecates it in favour of the `dynamic`
|
|
8
|
+
> factory, which is all barq has. Hoist one to the component body, or to the module when the
|
|
9
|
+
> choice reads only module state: `const Chosen = dynamic(() => …)` with what `component`
|
|
10
|
+
> held, then render `<Chosen …/>` with the other props.
|
|
11
|
+
|
|
12
|
+
## Failing example
|
|
13
|
+
|
|
14
|
+
```tsx
|
|
15
|
+
import { Dynamic } from "@barqjs/web";
|
|
16
|
+
import { Editor, Viewer } from "./fields";
|
|
17
|
+
|
|
18
|
+
export const Field = (props) => (
|
|
19
|
+
<Dynamic component={props.editing() ? Editor : Viewer} value={props.value()} />
|
|
20
|
+
);
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Fixed example
|
|
24
|
+
|
|
25
|
+
```tsx
|
|
26
|
+
import { dynamic } from "@barqjs/web";
|
|
27
|
+
import { Editor, Viewer } from "./fields";
|
|
28
|
+
|
|
29
|
+
export const Field = (props) => {
|
|
30
|
+
const Chosen = dynamic(() => (props.editing() ? Editor : Viewer));
|
|
31
|
+
return <Chosen value={props.value()} />;
|
|
32
|
+
};
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
A source may answer with a tag name as well as a component, or with `null`, `undefined` or
|
|
36
|
+
`false` to render nothing:
|
|
37
|
+
|
|
38
|
+
```tsx
|
|
39
|
+
import { dynamic } from "@barqjs/web";
|
|
40
|
+
|
|
41
|
+
export const Heading = (props) => {
|
|
42
|
+
const Tag = dynamic(() => `h${props.level()}`);
|
|
43
|
+
return <Tag class="title">{props.children}</Tag>;
|
|
44
|
+
};
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Why this rule exists
|
|
48
|
+
|
|
49
|
+
Solid 2.0 replaces `<Dynamic component={…}>` with `dynamic(source)`, a factory that returns
|
|
50
|
+
a component whose tag or component is whatever `source` answers, and swaps it when the
|
|
51
|
+
answer changes. `<Dynamic>` puts the choice in the same props as the element's own, so
|
|
52
|
+
every instance has to take `component` back out of them and build the factory again;
|
|
53
|
+
`dynamic` is built once where it is hoisted. Solid keeps `<Dynamic>` deprecated, and barq,
|
|
54
|
+
which follows Solid 2.0, does not export it, so the import is `undefined` and the page
|
|
55
|
+
throws when it renders rather than when it builds.
|
|
56
|
+
|
|
57
|
+
The check reports a `<Dynamic>` tag, or `<core.Dynamic>` on a namespace import, whose
|
|
58
|
+
binding is the one imported from `@barqjs/web` or `@barqjs/core`. A component of your own named `Dynamic` is
|
|
59
|
+
not reported.
|
|
60
|
+
|
|
61
|
+
## Silencing one line
|
|
62
|
+
|
|
63
|
+
```tsx
|
|
64
|
+
import { Dynamic } from "@barqjs/web";
|
|
65
|
+
import { Chart } from "./chart";
|
|
66
|
+
|
|
67
|
+
// barq-ignore-next-line BARQ025 (a compatibility shim provides Dynamic at startup)
|
|
68
|
+
export const Panel = () => <Dynamic component={Chart} />;
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
The reason in parentheses is required and has to be at least 10 characters. Between JSX
|
|
72
|
+
children, write the comment as `{/* barq-ignore-next-line BARQ025 (reason) */}` on the line
|
|
73
|
+
above: a `//` line there is text and would be rendered on the page. To change the level
|
|
74
|
+
for the whole project, see [the index](README.md#changing-a-level-for-the-whole-project).
|