@bigbinary/babel-preset-neeto 1.0.4 → 1.0.5
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/.neetoci/branch.yml +18 -0
- package/.neetoci/default.yml +1 -0
- package/README.md +1 -0
- package/docs/zustand-pick-from.md +171 -0
- package/index.js +2 -1
- package/package.json +1 -1
- package/src/plugins/messages.js +4 -0
- package/src/plugins/zustand-pick/constants.js +1 -1
- package/src/plugins/zustand-pick-from/constants.js +64 -0
- package/src/plugins/zustand-pick-from/index.js +74 -0
- package/src/tests/zustand-pick/transformations/use-store-pick/input.js +1 -0
- package/src/tests/zustand-pick/transformations/use-store-pick/output.js +8 -0
- package/src/tests/zustand-pick-from/no-changes/non-pick-from-calls.js +1 -0
- package/src/tests/zustand-pick-from/no-changes/non-standard-zustand-store.js +1 -0
- package/src/tests/zustand-pick-from/syntax-errors/direct-pick-from-access1.js +1 -0
- package/src/tests/zustand-pick-from/syntax-errors/direct-pick-from-access2.js +1 -0
- package/src/tests/zustand-pick-from/syntax-errors/object-destructure.js +1 -0
- package/src/tests/zustand-pick-from/syntax-errors/pick-from-multiple-arguments.js +1 -0
- package/src/tests/zustand-pick-from/syntax-errors/pick-from-path-array-expressions.js +1 -0
- package/src/tests/zustand-pick-from/syntax-errors/pick-from-path-array-templates.js +1 -0
- package/src/tests/zustand-pick-from/transformations/array-pick-from/input.js +1 -0
- package/src/tests/zustand-pick-from/transformations/array-pick-from/output.js +3 -0
- package/src/tests/zustand-pick-from/transformations/dynamic-pick-from/input.js +1 -0
- package/src/tests/zustand-pick-from/transformations/dynamic-pick-from/output.js +1 -0
- package/src/tests/zustand-pick-from/transformations/empty-pick-from/input.js +1 -0
- package/src/tests/zustand-pick-from/transformations/empty-pick-from/output.js +1 -0
- package/src/tests/zustand-pick-from/transformations/string-pick-from/input.js +1 -0
- package/src/tests/zustand-pick-from/transformations/string-pick-from/output.js +1 -0
- package/src/tests/zustand-pick-from/transformations/use-store-pick-from/input.js +1 -0
- package/src/tests/zustand-pick-from/transformations/use-store-pick-from/output.js +1 -0
- package/src/tests/zustand-pick-from/zustand-pick-from.spec.js +18 -0
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
version: v1.0
|
|
2
|
+
triggers:
|
|
3
|
+
- event: branch
|
|
4
|
+
branch_name: main
|
|
5
|
+
plan: standard-2
|
|
6
|
+
name: neetoCi
|
|
7
|
+
global_job_config:
|
|
8
|
+
setup:
|
|
9
|
+
- checkout
|
|
10
|
+
- neetoci-version node 18.12
|
|
11
|
+
- cache restore
|
|
12
|
+
- yarn install
|
|
13
|
+
- cache store
|
|
14
|
+
jobs:
|
|
15
|
+
- name: CiChecks
|
|
16
|
+
commands:
|
|
17
|
+
- .scripts/run_eslint_on_modified_files.sh
|
|
18
|
+
- yarn test
|
package/.neetoci/default.yml
CHANGED
package/README.md
CHANGED
|
@@ -42,6 +42,7 @@ module.exports = {
|
|
|
42
42
|
## Available plugins
|
|
43
43
|
|
|
44
44
|
- [zustand-pick](https://github.com/bigbinary/babel-preset-neeto/blob/e8433f3a019cf96dcb0ea29d46393bcf5868406d/docs/zustand-pick.md)
|
|
45
|
+
- [zustand-pick-from](https://github.com/bigbinary/babel-preset-neeto/blob/e8433f3a019cf96dcb0ea29d46393bcf5868406d/docs/zustand-pick-from.md)
|
|
45
46
|
|
|
46
47
|
## Other links
|
|
47
48
|
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
# Zustand pickFrom transformer
|
|
2
|
+
|
|
3
|
+
## Motivation
|
|
4
|
+
|
|
5
|
+
When we created the Zustand pick transformer, we did not take into account, the
|
|
6
|
+
case to access a single property from a store. We were using the `pick`
|
|
7
|
+
transformer to access a single item.
|
|
8
|
+
|
|
9
|
+
```js
|
|
10
|
+
const { order } = useGlobalStore.pick([sessionId, "globals"]);
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
While there doesn't seem to be an issue, with using it this way, at the time of
|
|
14
|
+
bundling, the following is generated:
|
|
15
|
+
|
|
16
|
+
```js
|
|
17
|
+
import { shallow } from "zustand/shallow";
|
|
18
|
+
|
|
19
|
+
const { order } = useGlobalStore(
|
|
20
|
+
store => ({ order: store[sessionId]?.globals.order }),
|
|
21
|
+
shallow
|
|
22
|
+
);
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
This gets the job done, but, it's very inefficient. Moreover, we don't even need
|
|
26
|
+
the `shallow` import for picking a single property.
|
|
27
|
+
|
|
28
|
+
For this reason, we have implemented the Zustand `pickFrom` transformer. The
|
|
29
|
+
`pickFrom` transformer transpiles the code to an efficient version for picking a
|
|
30
|
+
single item.
|
|
31
|
+
|
|
32
|
+
```js
|
|
33
|
+
const order = useGlobalStore.pickFrom([sessionId, "globals"]);
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
This will generate the following:
|
|
37
|
+
|
|
38
|
+
```js
|
|
39
|
+
const order = useGlobalStore.pickFrom(
|
|
40
|
+
store => store[sessionId]?.["globals"]?.order
|
|
41
|
+
);
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Allowed syntaxes
|
|
45
|
+
|
|
46
|
+
Consider this zustand store as an example.
|
|
47
|
+
|
|
48
|
+
```js
|
|
49
|
+
const useGlobalStore = create((set, get) => ({
|
|
50
|
+
order: {
|
|
51
|
+
id: "#1230",
|
|
52
|
+
user: {
|
|
53
|
+
email: "oliver@example.com",
|
|
54
|
+
address: {
|
|
55
|
+
"location 1": { street: "293, 1st Floor", city: "Mumbai" }
|
|
56
|
+
"location 2": { street: "295, 3st Floor", city: "Chennai" }
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
items: {
|
|
60
|
+
// itemID: { ...properties }
|
|
61
|
+
"47e91f1a-0c4d-4a79-a939-1319d8659bd2": { quantity: 10, price: 100 },
|
|
62
|
+
"64b7e54c-5d8a-443a-b70b-fff3d6937eba": { quantity: 3, price: 34 },
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
|
|
66
|
+
setOrder: order => set({ order }),
|
|
67
|
+
}));
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
```js
|
|
71
|
+
// picks `order` data from the store
|
|
72
|
+
const order = useGlobalStore.pickFrom();
|
|
73
|
+
|
|
74
|
+
// picks `user` from inside store.order
|
|
75
|
+
const user = useGlobalStore.pickFrom("order");
|
|
76
|
+
|
|
77
|
+
// picks `quantity` of dynamically obtained item of id `itemId`
|
|
78
|
+
const quantity = useGlobalStore.pickFrom(["order", "items", itemId]);
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## Disallowed syntaxes
|
|
82
|
+
|
|
83
|
+
We have introduced a type definition for `pickFrom` method from zustand stores
|
|
84
|
+
from neeto-commons-frontend to make IDE capable of pointing out errors as you
|
|
85
|
+
type. But it is not 100% foolproof. So here is a list of usages that are flagged
|
|
86
|
+
as syntax errors during transpilation.
|
|
87
|
+
|
|
88
|
+
1. Destructuring is not allowed. If you want to use destructuring, use the
|
|
89
|
+
`pick` method.
|
|
90
|
+
|
|
91
|
+
**incorrect**
|
|
92
|
+
|
|
93
|
+
```js
|
|
94
|
+
const { user } = useGlobalStore.pickFrom();
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
**correct**
|
|
98
|
+
|
|
99
|
+
```js
|
|
100
|
+
const user = useGlobalStore.pickFrom("order");
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
2. Expressions are not allowed in picking path array
|
|
104
|
+
|
|
105
|
+
**incorrect**
|
|
106
|
+
|
|
107
|
+
```js
|
|
108
|
+
// expressions are not allowed
|
|
109
|
+
const user = useGlobalStore.pickFrom(["order", index + 1]);
|
|
110
|
+
// string templates are not allowed
|
|
111
|
+
const user = useGlobalStore.pickFrom(["order", `#${orderId}`]);
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
**correct**
|
|
115
|
+
|
|
116
|
+
```js
|
|
117
|
+
// only use literals of String, Numbers or Booleans
|
|
118
|
+
// and variables as elements of the path array
|
|
119
|
+
const indexPlusOne = index + 1;
|
|
120
|
+
const user = useGlobalStore.pickFrom(["order", indexPlusOne, 0, true]);
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
3. PickFrom only accepts a single parameter:
|
|
124
|
+
|
|
125
|
+
**incorrect**
|
|
126
|
+
|
|
127
|
+
```js
|
|
128
|
+
const name = useGlobalStore.pickFrom("order", "user");
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
**correct**
|
|
132
|
+
|
|
133
|
+
```js
|
|
134
|
+
// use arrays to specify nested path
|
|
135
|
+
const name = useGlobalStore.pickFrom(["order", "user"]);
|
|
136
|
+
// use key directly to get its nested properties.
|
|
137
|
+
const user = useGlobalStore.pickFrom("order");
|
|
138
|
+
// pass no arguments to get the top level properties
|
|
139
|
+
const order = useGlobalStore.pickFrom();
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
## Notes
|
|
143
|
+
|
|
144
|
+
#### All nested property access are optional chained
|
|
145
|
+
|
|
146
|
+
No need to worry if any of the properties in your store nesting may be
|
|
147
|
+
null/undefined. This transformer adds optional chaining in all levels of
|
|
148
|
+
nesting. That is:
|
|
149
|
+
|
|
150
|
+
```js
|
|
151
|
+
const order = useGlobalStore.pickFrom([sessionId, "globals"]);
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
Will be transformed to:
|
|
155
|
+
|
|
156
|
+
```js
|
|
157
|
+
const order = useGlobalStore(store => store[sessionId]?.["globals"]?.["order"]);
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
So your code won't fail if `store[sessionId]` is null or
|
|
161
|
+
`store[sessionId].globals` is null.
|
|
162
|
+
|
|
163
|
+
#### This plugin should run on user's code
|
|
164
|
+
|
|
165
|
+
You might encounter errors if some other plugin transforms the code before
|
|
166
|
+
babel-plugin-neeto. **babel-plugin-neeto expects that it runs first on the code
|
|
167
|
+
before any other plugin performs transformations.**
|
|
168
|
+
|
|
169
|
+
If your zustand-pick-from syntax is correct, and you are still getting errors,
|
|
170
|
+
check for other babel-plugins or babel-presets transforming your code before we
|
|
171
|
+
do.
|
package/index.js
CHANGED
package/package.json
CHANGED
package/src/plugins/messages.js
CHANGED
|
@@ -3,6 +3,10 @@
|
|
|
3
3
|
const INCORRECT_ZUSTAND_PICK_USAGE =
|
|
4
4
|
"Invalid use of zustand store pick method. You can find a list of allowed usages here: https://bit.ly/zustand-pick-syntax";
|
|
5
5
|
|
|
6
|
+
const INCORRECT_ZUSTAND_PICK_FROM_USAGE =
|
|
7
|
+
"Invalid use of zustand store pickFrom method. You can find a list of allowed usages here: https://bit.ly/zustand-pick-from-syntax";
|
|
8
|
+
|
|
6
9
|
module.exports = {
|
|
7
10
|
INCORRECT_ZUSTAND_PICK_USAGE,
|
|
11
|
+
INCORRECT_ZUSTAND_PICK_FROM_USAGE,
|
|
8
12
|
};
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
const { matches } = require("@bigbinary/neeto-commons-frontend/pure");
|
|
2
|
+
const { test, isEmpty, mergeDeepLeft, includes, __ } = require("ramda");
|
|
3
|
+
|
|
4
|
+
const { matchesWithLength } = require("../utils");
|
|
5
|
+
|
|
6
|
+
const VALID_ARRAY_ELEMENT_TYPES = [
|
|
7
|
+
"StringLiteral",
|
|
8
|
+
"Identifier",
|
|
9
|
+
"NumericLiteral",
|
|
10
|
+
"BooleanLiteral",
|
|
11
|
+
];
|
|
12
|
+
|
|
13
|
+
const VALID_PICK_FROM_ARGUMENT_TYPES = [
|
|
14
|
+
"StringLiteral",
|
|
15
|
+
"Identifier",
|
|
16
|
+
"ArrayExpression",
|
|
17
|
+
];
|
|
18
|
+
|
|
19
|
+
const VALID_PICK_FROM_ARGUMENT = {
|
|
20
|
+
type: includes(__, VALID_PICK_FROM_ARGUMENT_TYPES),
|
|
21
|
+
elements: (elements, root) =>
|
|
22
|
+
root.type !== "ArrayExpression" ||
|
|
23
|
+
elements.every(({ type }) => VALID_ARRAY_ELEMENT_TYPES.includes(type)),
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
// <any variable declaration> = useStore.pickFrom()
|
|
27
|
+
const PICK_FROM_GENERIC_PATTERN = {
|
|
28
|
+
type: "VariableDeclarator",
|
|
29
|
+
init: {
|
|
30
|
+
type: "CallExpression",
|
|
31
|
+
callee: {
|
|
32
|
+
type: "MemberExpression",
|
|
33
|
+
computed: false,
|
|
34
|
+
object: {
|
|
35
|
+
type: "Identifier",
|
|
36
|
+
name: test(/^use[a-zA-Z0-9]*Store$/),
|
|
37
|
+
},
|
|
38
|
+
property: {
|
|
39
|
+
type: "Identifier",
|
|
40
|
+
name: "pickFrom",
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
// const value = useStore.pickFrom() or const value = useStore.pickFrom(argument)
|
|
47
|
+
const PICK_FROM_STRICT_PATTERN = {
|
|
48
|
+
type: "VariableDeclaration",
|
|
49
|
+
declarations: matchesWithLength([
|
|
50
|
+
mergeDeepLeft(
|
|
51
|
+
{
|
|
52
|
+
id: { type: "Identifier" },
|
|
53
|
+
init: {
|
|
54
|
+
arguments: args =>
|
|
55
|
+
isEmpty(args) ||
|
|
56
|
+
(args.length === 1 && matches(VALID_PICK_FROM_ARGUMENT, args[0])),
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
PICK_FROM_GENERIC_PATTERN
|
|
60
|
+
),
|
|
61
|
+
]),
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
module.exports = { PICK_FROM_GENERIC_PATTERN, PICK_FROM_STRICT_PATTERN };
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
const { matches } = require("@bigbinary/neeto-commons-frontend/pure");
|
|
2
|
+
const { last } = require("ramda");
|
|
3
|
+
|
|
4
|
+
const {
|
|
5
|
+
PICK_FROM_GENERIC_PATTERN,
|
|
6
|
+
PICK_FROM_STRICT_PATTERN,
|
|
7
|
+
} = require("./constants");
|
|
8
|
+
|
|
9
|
+
const { INCORRECT_ZUSTAND_PICK_FROM_USAGE } = require("../messages");
|
|
10
|
+
|
|
11
|
+
const getPropertyAccessNode = (types, propertyPath) => {
|
|
12
|
+
if (propertyPath.length === 1) {
|
|
13
|
+
return types.memberExpression(
|
|
14
|
+
types.identifier("store"),
|
|
15
|
+
propertyPath[0],
|
|
16
|
+
true
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return types.optionalMemberExpression(
|
|
21
|
+
getPropertyAccessNode(
|
|
22
|
+
types,
|
|
23
|
+
propertyPath.slice(0, propertyPath.length - 1)
|
|
24
|
+
),
|
|
25
|
+
last(propertyPath),
|
|
26
|
+
true,
|
|
27
|
+
true
|
|
28
|
+
);
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
module.exports = function ({ types }) {
|
|
32
|
+
return {
|
|
33
|
+
name: "Zustand pick-from transformer",
|
|
34
|
+
visitor: {
|
|
35
|
+
VariableDeclaration(astPath) {
|
|
36
|
+
const declaratorPath = astPath.get("declarations.0");
|
|
37
|
+
const declarator = declaratorPath.node;
|
|
38
|
+
if (!matches(PICK_FROM_GENERIC_PATTERN, declarator)) return;
|
|
39
|
+
|
|
40
|
+
if (!matches(PICK_FROM_STRICT_PATTERN, astPath.node)) {
|
|
41
|
+
throw astPath.buildCodeFrameError(INCORRECT_ZUSTAND_PICK_FROM_USAGE);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const [argument] = declarator.init.arguments;
|
|
45
|
+
let propertyPath;
|
|
46
|
+
if (argument === undefined) {
|
|
47
|
+
propertyPath = [];
|
|
48
|
+
} else if (["Identifier", "StringLiteral"].includes(argument.type)) {
|
|
49
|
+
propertyPath = [argument];
|
|
50
|
+
} else if (argument.type === "ArrayExpression") {
|
|
51
|
+
propertyPath = argument.elements;
|
|
52
|
+
} else return;
|
|
53
|
+
|
|
54
|
+
declarator.init = types.callExpression(
|
|
55
|
+
types.identifier(declarator.init.callee.object.name),
|
|
56
|
+
[
|
|
57
|
+
types.arrowFunctionExpression(
|
|
58
|
+
[types.identifier("store")],
|
|
59
|
+
getPropertyAccessNode(types, [
|
|
60
|
+
...propertyPath,
|
|
61
|
+
types.stringLiteral(declarator.id.name),
|
|
62
|
+
])
|
|
63
|
+
),
|
|
64
|
+
]
|
|
65
|
+
);
|
|
66
|
+
},
|
|
67
|
+
CallExpression(astPath) {
|
|
68
|
+
if (matches(PICK_FROM_GENERIC_PATTERN.init, astPath.node)) {
|
|
69
|
+
throw astPath.buildCodeFrameError(INCORRECT_ZUSTAND_PICK_FROM_USAGE);
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const { order, customer } = useStore.pick();
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const name = useSampleStore.pock();
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const name = useSampleStor.pickFrom();
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const name = useGlobalStore.pickFrom("order").user;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const name = useGlobalStore.pickFrom("order")?.user.name;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const { order } = useGlobalStore.pickFrom();
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const user = useGlobalStore.pickFrom("order", "user");
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const user = useGlobalStore.pickFrom(["order", index + 1]);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const user = useGlobalStore.pickFrom(["order", `#${orderId}`]);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const order = useGlobalStore.pickFrom(["state", sessionId, "current", data]);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const customer = useGlobalStore.pickFrom(sessionId);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const customer = useGlobalStore(store => store[sessionId]?.["customer"]);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const order = useGlobalStore.pickFrom();
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const order = useGlobalStore(store => store["order"]);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const order = useGlobalStore.pickFrom("state");
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const order = useGlobalStore(store => store["state"]?.["order"]);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const order = useStore.pickFrom();
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const order = useStore(store => store["order"]);
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
const { pluginTester } = require("babel-plugin-tester");
|
|
2
|
+
|
|
3
|
+
const { INCORRECT_ZUSTAND_PICK_FROM_USAGE } = require("../../plugins/messages");
|
|
4
|
+
const zustandPickFrom = require("../../plugins/zustand-pick-from");
|
|
5
|
+
const {
|
|
6
|
+
getPositiveCases,
|
|
7
|
+
getNegativeCases,
|
|
8
|
+
getIncorrectSyntaxCases,
|
|
9
|
+
} = require("../utils");
|
|
10
|
+
|
|
11
|
+
pluginTester({
|
|
12
|
+
plugin: zustandPickFrom,
|
|
13
|
+
tests: {
|
|
14
|
+
...getPositiveCases(__dirname),
|
|
15
|
+
...getIncorrectSyntaxCases(__dirname, INCORRECT_ZUSTAND_PICK_FROM_USAGE),
|
|
16
|
+
...getNegativeCases(__dirname),
|
|
17
|
+
},
|
|
18
|
+
});
|