@bigbinary/babel-preset-neeto 1.0.3 → 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.
Files changed (33) hide show
  1. package/.neetoci/branch.yml +18 -0
  2. package/.neetoci/default.yml +15 -0
  3. package/README.md +42 -65
  4. package/docs/development-instructions.md +58 -0
  5. package/docs/references.md +6 -0
  6. package/docs/zustand-pick-from.md +171 -0
  7. package/index.js +2 -1
  8. package/package.json +1 -1
  9. package/src/plugins/messages.js +4 -0
  10. package/src/plugins/zustand-pick/constants.js +1 -1
  11. package/src/plugins/zustand-pick-from/constants.js +64 -0
  12. package/src/plugins/zustand-pick-from/index.js +74 -0
  13. package/src/tests/zustand-pick/transformations/use-store-pick/input.js +1 -0
  14. package/src/tests/zustand-pick/transformations/use-store-pick/output.js +8 -0
  15. package/src/tests/zustand-pick-from/no-changes/non-pick-from-calls.js +1 -0
  16. package/src/tests/zustand-pick-from/no-changes/non-standard-zustand-store.js +1 -0
  17. package/src/tests/zustand-pick-from/syntax-errors/direct-pick-from-access1.js +1 -0
  18. package/src/tests/zustand-pick-from/syntax-errors/direct-pick-from-access2.js +1 -0
  19. package/src/tests/zustand-pick-from/syntax-errors/object-destructure.js +1 -0
  20. package/src/tests/zustand-pick-from/syntax-errors/pick-from-multiple-arguments.js +1 -0
  21. package/src/tests/zustand-pick-from/syntax-errors/pick-from-path-array-expressions.js +1 -0
  22. package/src/tests/zustand-pick-from/syntax-errors/pick-from-path-array-templates.js +1 -0
  23. package/src/tests/zustand-pick-from/transformations/array-pick-from/input.js +1 -0
  24. package/src/tests/zustand-pick-from/transformations/array-pick-from/output.js +3 -0
  25. package/src/tests/zustand-pick-from/transformations/dynamic-pick-from/input.js +1 -0
  26. package/src/tests/zustand-pick-from/transformations/dynamic-pick-from/output.js +1 -0
  27. package/src/tests/zustand-pick-from/transformations/empty-pick-from/input.js +1 -0
  28. package/src/tests/zustand-pick-from/transformations/empty-pick-from/output.js +1 -0
  29. package/src/tests/zustand-pick-from/transformations/string-pick-from/input.js +1 -0
  30. package/src/tests/zustand-pick-from/transformations/string-pick-from/output.js +1 -0
  31. package/src/tests/zustand-pick-from/transformations/use-store-pick-from/input.js +1 -0
  32. package/src/tests/zustand-pick-from/transformations/use-store-pick-from/output.js +1 -0
  33. 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
@@ -0,0 +1,15 @@
1
+ version: v1.0
2
+ plan: standard-2
3
+ name: neetoCi
4
+ global_job_config:
5
+ setup:
6
+ - checkout
7
+ - neetoci-version node 18.12
8
+ - cache restore
9
+ - yarn install
10
+ - cache store
11
+ jobs:
12
+ - name: CiChecks
13
+ commands:
14
+ - .scripts/run_eslint_on_modified_files.sh
15
+ - yarn test
package/README.md CHANGED
@@ -2,72 +2,49 @@
2
2
 
3
3
  A babel transformer to minimize boilerplate code in neeto products.
4
4
 
5
- ## Available plugins
6
-
7
- - [zustand-pick](./docs/zustand-pick.md)
8
-
9
- ## Development instructions
5
+ ## Usage
6
+
7
+ babel-preset-neeto is designed in an assumption that it will be run before all
8
+ other presets or plugins. To make that possible, you need to add
9
+ `@bigbinary/neeto` towards the end of your `presets` array in `babel.config.js`
10
+ as shown below. Babel presets will run in last-to-first order.
11
+
12
+ ```js
13
+ module.exports = {
14
+ presets: [
15
+ // ...all other presets
16
+ "@bigbinary/neeto",
17
+ ];
18
+ }
19
+ ```
20
+
21
+ Babel runs the plugins before presets. But usually plugins do minor code
22
+ transformations which won't affect babel-preset-neeto. But if any of them
23
+ interfere with babel-preset-neeto, add plugins directly to the top of the
24
+ `plugins` array as shown below. Babel plugins are run in first-to-last order.
25
+
26
+ ```js
27
+ const zustandPickPlugin = require("@bigbinary/babel-preset-neeto/src/plugins/zustand-pick");
28
+ const anyOtherPluginFromNeeto = require("@bigbinary/babel-preset-neeto/src/plugins/other-plugin-name");
29
+
30
+ module.exports = {
31
+ plugins: [
32
+ zustandPickPlugin,
33
+ anyOtherPluginFromNeeto,
34
+ // ...all your plugins
35
+ ],
36
+ presets: [
37
+ // ...all your presets
38
+ ];
39
+ }
40
+ ```
10
41
 
11
- - We don't use bundling as of now in this repo. All newly added plugins should
12
- be exported from `index.js` in the repo's root folder to access it from host
13
- projects. Also, use CJS `require` style for importing functions & other
14
- elements.
15
- - Make sure to add names for all newly added plugins. See
16
- [this example](https://github.com/bigbinary/babel-preset-neeto/blob/f3f545493081473194c9187ea2493339a1ab3480/src/plugins/zustand-pick/index.js)
17
- - The plugins should be designed in a fail-fast way. It should throw syntax
18
- errors rather than fail at runtime for invalid usages. That is, if some
19
- proposed syntax is used in an invalid way, it should not be ignored. Refer
20
- [to this example](https://github.com/bigbinary/babel-preset-neeto/blob/f3f545493081473194c9187ea2493339a1ab3480/src/plugins/zustand-pick/index.js)
21
- - All the plugins should reside directly under `src/plugins` folder. It can
22
- either be a file or a folder with `index.js`. The name of file/folder should
23
- be kebab-cased to improve readability.
24
- - All syntax error messages should reside inside `src/plugins/messages.js` file.
25
- It should show a shortened link to the documentation of the failing plugin.
26
- - For each plugin, there should be a documentation file inside `docs` folder.
27
- The documentation should be an `.md` file with the same name as the plugin
28
- (kebab-cased). The doc should mention these things:
29
-
30
- - The motivation behind introducing the new syntax
31
- - Allowed syntaxes
32
- - Possible incorrect usages (Disallowed syntaxes)
33
-
34
- While writing the doc, it should be kept as simple as possible. The readers
35
- are general React developers and not Babel plugin developers. No need to
36
- expose project-specific technical aspects to them in the plugin doc.
42
+ ## Available plugins
37
43
 
38
- - Every transformation plugin should have detailed tests associated with it. It
39
- should cover all possible positive and negative test cases. We can specify an
40
- input file and an output file. When the input is transformed, it should
41
- generate the output code. Here is how tests can be written:
42
- - We use the library `babel-plugin-tester` for simplifying tests
43
- - Tests are classified into three categories.
44
- - transformations: we specify `input.js` and `output.js` for all allowed
45
- syntaxes.
46
- - no-changes: we specify cases where we won't make any changes even though
47
- the code is similar to the allowed syntax.
48
- - syntax-errors: all possible cases where we raise errors. This should cover
49
- all invalid usages of our proposed syntax.
50
- - A test will have its own folder under `src/tests` folder (with plugin name
51
- kebab-cased). It will contain 3 folders for fixtures (input/output file for
52
- each category of the test) and a spec file (also named the same as the
53
- plugin in kebab-case).
54
- - `no-changes` and `syntax-error` will have several possible cases inside
55
- them. Each case will be a separate JS file (with a brief kebab-cased
56
- description for the test case as the file name).
57
- - `transformations` folder will contain sub-folders for each test case. The
58
- folder name will be a brief description of the test case. Each subfolder
59
- will contain two files `input.js` and `output.js`. When Babel transforms
60
- the `input.js` file, the result should be identical to `output.js`
61
- (including white spaces).
62
- - Refer `src/tests/zustand-pick` folder for an example.
63
- - We can copy and paste the boilerplate from
64
- `src/tests/zustand-pick/zustand-pick.spec.js` for other tests as well for
65
- now. We can add a utility function later. You can run the tests using
66
- `yarn test` command.
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)
67
46
 
68
- ## References
47
+ ## Other links
69
48
 
70
- - Learning material:
71
- https://github.com/jamiebuilds/babel-handbook/blob/master/translations/en/plugin-handbook.md
72
- - Some important points to note while integrating babel-preset to host projects:
73
- https://youtu.be/zZfIIObnj_Q
49
+ - [Development instructions](./docs/development-instructions.md)
50
+ - [References and learning materials](./docs/references.md)
@@ -0,0 +1,58 @@
1
+ ## Development instructions
2
+
3
+ - We don't use bundling as of now in this repo. All newly added plugins should
4
+ be exported from `index.js` in the repo's root folder to access it from host
5
+ projects. Also, use CJS `require` style for importing functions & other
6
+ elements.
7
+ - Make sure to add names for all newly added plugins. See
8
+ [this example](https://github.com/bigbinary/babel-preset-neeto/blob/f3f545493081473194c9187ea2493339a1ab3480/src/plugins/zustand-pick/index.js)
9
+ - The plugins should be designed in a fail-fast way. It should throw syntax
10
+ errors rather than fail at runtime for invalid usages. That is, if some
11
+ proposed syntax is used in an invalid way, it should not be ignored. Refer
12
+ [to this example](https://github.com/bigbinary/babel-preset-neeto/blob/f3f545493081473194c9187ea2493339a1ab3480/src/plugins/zustand-pick/index.js)
13
+ - All the plugins should reside directly under `src/plugins` folder. It can
14
+ either be a file or a folder with `index.js`. The name of file/folder should
15
+ be kebab-cased to improve readability.
16
+ - All syntax error messages should reside inside `src/plugins/messages.js` file.
17
+ It should show a shortened link to the documentation of the failing plugin.
18
+ - For each plugin, there should be a documentation file inside `docs` folder.
19
+ The documentation should be an `.md` file with the same name as the plugin
20
+ (kebab-cased). The doc should mention these things:
21
+
22
+ - The motivation behind introducing the new syntax
23
+ - Allowed syntaxes
24
+ - Possible incorrect usages (Disallowed syntaxes)
25
+
26
+ While writing the doc, it should be kept as simple as possible. The readers
27
+ are general React developers and not Babel plugin developers. No need to
28
+ expose project-specific technical aspects to them in the plugin doc.
29
+
30
+ - Every transformation plugin should have detailed tests associated with it. It
31
+ should cover all possible positive and negative test cases. We can specify an
32
+ input file and an output file. When the input is transformed, it should
33
+ generate the output code. Here is how tests can be written:
34
+ - We use the library `babel-plugin-tester` for simplifying tests
35
+ - Tests are classified into three categories.
36
+ - transformations: we specify `input.js` and `output.js` for all allowed
37
+ syntaxes.
38
+ - no-changes: we specify cases where we won't make any changes even though
39
+ the code is similar to the allowed syntax.
40
+ - syntax-errors: all possible cases where we raise errors. This should cover
41
+ all invalid usages of our proposed syntax.
42
+ - A test will have its own folder under `src/tests` folder (with plugin name
43
+ kebab-cased). It will contain 3 folders for fixtures (input/output file for
44
+ each category of the test) and a spec file (also named the same as the
45
+ plugin in kebab-case).
46
+ - `no-changes` and `syntax-error` will have several possible cases inside
47
+ them. Each case will be a separate JS file (with a brief kebab-cased
48
+ description for the test case as the file name).
49
+ - `transformations` folder will contain sub-folders for each test case. The
50
+ folder name will be a brief description of the test case. Each subfolder
51
+ will contain two files `input.js` and `output.js`. When Babel transforms
52
+ the `input.js` file, the result should be identical to `output.js`
53
+ (including white spaces).
54
+ - Refer `src/tests/zustand-pick` folder for an example.
55
+ - We can copy and paste the boilerplate from
56
+ `src/tests/zustand-pick/zustand-pick.spec.js` for other tests as well for
57
+ now. We can add a utility function later. You can run the tests using
58
+ `yarn test` command.
@@ -0,0 +1,6 @@
1
+ ## References
2
+
3
+ - Learning material:
4
+ https://github.com/jamiebuilds/babel-handbook/blob/master/translations/en/plugin-handbook.md
5
+ - Some important points to note while integrating babel-preset to host projects:
6
+ https://youtu.be/zZfIIObnj_Q
@@ -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
@@ -1,5 +1,6 @@
1
1
  const zustandPick = require("./src/plugins/zustand-pick");
2
+ const zustandPickFrom = require("./src/plugins/zustand-pick-from");
2
3
 
3
4
  module.exports = () => ({
4
- plugins: [zustandPick],
5
+ plugins: [zustandPick, zustandPickFrom],
5
6
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bigbinary/babel-preset-neeto",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "A babel preset to transform short code to boilerplate at compile time",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -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
  };
@@ -32,7 +32,7 @@ const PICK_GENERIC_PATTERN = {
32
32
  computed: false,
33
33
  object: {
34
34
  type: "Identifier",
35
- name: test(/^use[a-zA-Z0-9]+Store$/),
35
+ name: test(/^use[a-zA-Z0-9]*Store$/),
36
36
  },
37
37
  property: {
38
38
  type: "Identifier",
@@ -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,8 @@
1
+ import { shallow } from "zustand/shallow";
2
+ const { order, customer } = useStore(
3
+ store => ({
4
+ order: store["order"],
5
+ customer: store["customer"],
6
+ }),
7
+ shallow
8
+ );
@@ -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,3 @@
1
+ const order = useGlobalStore(
2
+ store => store["state"]?.[sessionId]?.["current"]?.[data]?.["order"]
3
+ );
@@ -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
+ });