@bigbinary/babel-preset-neeto 1.0.13 → 1.0.15

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 (71) hide show
  1. package/package.json +5 -1
  2. package/.editorconfig +0 -10
  3. package/.eslintignore +0 -3
  4. package/.eslintrc.js +0 -6
  5. package/.github/pull_request_template.md +0 -18
  6. package/.github/release-drafter.yml +0 -8
  7. package/.husky/helpers/lint_staged.sh +0 -5
  8. package/.husky/pre-commit +0 -5
  9. package/.neetoci/bump_version.yml +0 -14
  10. package/.neetoci/default.yml +0 -21
  11. package/.node-version +0 -1
  12. package/.nvmrc +0 -1
  13. package/.prettierignore +0 -1
  14. package/.prettierrc.js +0 -1
  15. package/.scripts/bump_version.sh +0 -78
  16. package/.scripts/run_eslint_on_modified_files.sh +0 -5
  17. package/.semaphore/semaphore.yml +0 -32
  18. package/babel.config.js +0 -1
  19. package/docs/development-instructions.md +0 -58
  20. package/docs/references.md +0 -6
  21. package/docs/zustand-pick-from.md +0 -171
  22. package/docs/zustand-pick.md +0 -191
  23. package/jsconfig.json +0 -19
  24. package/src/tests/utils.js +0 -38
  25. package/src/tests/zustand-pick/no-changes/non-pick-calls.js +0 -1
  26. package/src/tests/zustand-pick/no-changes/non-standard-zustand-store.js +0 -1
  27. package/src/tests/zustand-pick/no-changes/normal-zustand-calls.js +0 -1
  28. package/src/tests/zustand-pick/syntax-errors/direct-pick-access1.js +0 -1
  29. package/src/tests/zustand-pick/syntax-errors/direct-pick-access2.js +0 -1
  30. package/src/tests/zustand-pick/syntax-errors/pick-multiple-arguments.js +0 -1
  31. package/src/tests/zustand-pick/syntax-errors/pick-nested-properties.js +0 -3
  32. package/src/tests/zustand-pick/syntax-errors/pick-path-array-expressions.js +0 -1
  33. package/src/tests/zustand-pick/syntax-errors/pick-path-array-templates.js +0 -1
  34. package/src/tests/zustand-pick/transformations/array-pick/input.js +0 -6
  35. package/src/tests/zustand-pick/transformations/array-pick/output.js +0 -7
  36. package/src/tests/zustand-pick/transformations/default-value/input.js +0 -1
  37. package/src/tests/zustand-pick/transformations/default-value/output.js +0 -7
  38. package/src/tests/zustand-pick/transformations/dynamic-declare/input.js +0 -2
  39. package/src/tests/zustand-pick/transformations/dynamic-declare/output.js +0 -11
  40. package/src/tests/zustand-pick/transformations/dynamic-pick/input.js +0 -1
  41. package/src/tests/zustand-pick/transformations/dynamic-pick/output.js +0 -6
  42. package/src/tests/zustand-pick/transformations/empty-pick/input.js +0 -1
  43. package/src/tests/zustand-pick/transformations/empty-pick/output.js +0 -7
  44. package/src/tests/zustand-pick/transformations/no-duplicate-import/input.js +0 -2
  45. package/src/tests/zustand-pick/transformations/no-duplicate-import/output.js +0 -7
  46. package/src/tests/zustand-pick/transformations/string-pick/input.js +0 -1
  47. package/src/tests/zustand-pick/transformations/string-pick/output.js +0 -8
  48. package/src/tests/zustand-pick/transformations/use-store-pick/input.js +0 -1
  49. package/src/tests/zustand-pick/transformations/use-store-pick/output.js +0 -7
  50. package/src/tests/zustand-pick/transformations/with-other-imports/input.js +0 -4
  51. package/src/tests/zustand-pick/transformations/with-other-imports/output.js +0 -9
  52. package/src/tests/zustand-pick/zustand-pick.spec.js +0 -18
  53. package/src/tests/zustand-pick-from/no-changes/non-pick-from-calls.js +0 -1
  54. package/src/tests/zustand-pick-from/no-changes/non-standard-zustand-store.js +0 -1
  55. package/src/tests/zustand-pick-from/syntax-errors/direct-pick-from-access1.js +0 -1
  56. package/src/tests/zustand-pick-from/syntax-errors/direct-pick-from-access2.js +0 -1
  57. package/src/tests/zustand-pick-from/syntax-errors/object-destructure.js +0 -1
  58. package/src/tests/zustand-pick-from/syntax-errors/pick-from-multiple-arguments.js +0 -1
  59. package/src/tests/zustand-pick-from/syntax-errors/pick-from-path-array-expressions.js +0 -1
  60. package/src/tests/zustand-pick-from/syntax-errors/pick-from-path-array-templates.js +0 -1
  61. package/src/tests/zustand-pick-from/transformations/array-pick-from/input.js +0 -1
  62. package/src/tests/zustand-pick-from/transformations/array-pick-from/output.js +0 -3
  63. package/src/tests/zustand-pick-from/transformations/dynamic-pick-from/input.js +0 -1
  64. package/src/tests/zustand-pick-from/transformations/dynamic-pick-from/output.js +0 -1
  65. package/src/tests/zustand-pick-from/transformations/empty-pick-from/input.js +0 -1
  66. package/src/tests/zustand-pick-from/transformations/empty-pick-from/output.js +0 -1
  67. package/src/tests/zustand-pick-from/transformations/string-pick-from/input.js +0 -1
  68. package/src/tests/zustand-pick-from/transformations/string-pick-from/output.js +0 -1
  69. package/src/tests/zustand-pick-from/transformations/use-store-pick-from/input.js +0 -1
  70. package/src/tests/zustand-pick-from/transformations/use-store-pick-from/output.js +0 -1
  71. package/src/tests/zustand-pick-from/zustand-pick-from.spec.js +0 -18
package/package.json CHANGED
@@ -1,8 +1,12 @@
1
1
  {
2
2
  "name": "@bigbinary/babel-preset-neeto",
3
- "version": "1.0.13",
3
+ "version": "1.0.15",
4
4
  "description": "A babel preset to transform short code to boilerplate at compile time",
5
5
  "main": "index.js",
6
+ "files": [
7
+ "index.js",
8
+ "src/plugins/**/*"
9
+ ],
6
10
  "scripts": {
7
11
  "test": "jest",
8
12
  "prepare": "husky install"
package/.editorconfig DELETED
@@ -1,10 +0,0 @@
1
- # VScode respects this. More info at editorconfig.org
2
- root = true
3
-
4
- [*]
5
- charset = utf-8
6
- end_of_line = lf
7
- indent_size = 2
8
- indent_style = space
9
- insert_final_newline = true
10
- trim_trailing_whitespace = true
package/.eslintignore DELETED
@@ -1,3 +0,0 @@
1
- src/tests/*/syntax-errors/*.js
2
- src/tests/*/no-changes/*.js
3
- src/tests/*/transformations/**/*.js
package/.eslintrc.js DELETED
@@ -1,6 +0,0 @@
1
- // eslint-disable-next-line import/extensions
2
- const defaultConfig = require("@bigbinary/neeto-commons-frontend/configs/nanos/eslint/index.js");
3
-
4
- defaultConfig.rules["@bigbinary/neeto/file-name-and-export-name-standards"] =
5
- "off";
6
- module.exports = defaultConfig;
@@ -1,18 +0,0 @@
1
- - Fixes #issue_number
2
-
3
- **Description**
4
-
5
- **Checklist**
6
-
7
- - [ ] I have made corresponding changes to the documentation.
8
- - [ ] I have verified the functionality in some of the neeto web-apps.
9
- - [ ] I have added the necessary label (patch/minor/major - If package publish
10
- is required).
11
-
12
- **Reviewers**
13
-
14
- <!---
15
- ------------- NOTES -------------
16
- 1. Do not add a patch/minor/major label if a release is not required.
17
- 2. Strike through the points ~~like this~~ if not applicable.
18
- --->
@@ -1,8 +0,0 @@
1
- name-template: "v$RESOLVED_VERSION"
2
- tag-template: "v$RESOLVED_VERSION"
3
- template: |
4
- ## What's Changed
5
- $CHANGES
6
-
7
-
8
- **Full Changelog**: https://github.com/bigbinary/$REPOSITORY/compare/$PREVIOUS_TAG...v$RESOLVED_VERSION
@@ -1,5 +0,0 @@
1
- #!/bin/sh
2
-
3
- lint_staged_files() {
4
- npx lint-staged
5
- }
package/.husky/pre-commit DELETED
@@ -1,5 +0,0 @@
1
- #!/bin/sh
2
- . "$(dirname "$0")/_/husky.sh"
3
- . "$(dirname "$0")/helpers/lint_staged.sh"
4
-
5
- lint_staged_files
@@ -1,14 +0,0 @@
1
- version: v1.0
2
- fail_fast: true
3
- is_cypress: false
4
- plan: standard-2
5
-
6
- triggers:
7
- - event: branch
8
- branch_name: main
9
-
10
- commands:
11
- - checkout
12
- - neetoci-version node 22.13
13
- - neetoci-version ruby 3.3.5
14
- - bash ./.scripts/bump_version.sh
@@ -1,21 +0,0 @@
1
- version: v1.0
2
-
3
- plan: standard-2
4
-
5
- global_job_config:
6
- setup:
7
- - checkout
8
- - neetoci-version node 22.13
9
- - cache restore
10
- - yarn install
11
- - cache store
12
- jobs:
13
- - name: CiChecks
14
- commands:
15
- - .scripts/run_eslint_on_modified_files.sh
16
- - yarn test
17
-
18
- triggers:
19
- - event: branch
20
- branch_name: main
21
- - event: pull_request
package/.node-version DELETED
@@ -1 +0,0 @@
1
- 22.13
package/.nvmrc DELETED
@@ -1 +0,0 @@
1
- 22.13
package/.prettierignore DELETED
@@ -1 +0,0 @@
1
- **/output.js
package/.prettierrc.js DELETED
@@ -1 +0,0 @@
1
- module.exports = require("@bigbinary/neeto-commons-frontend/configs/prettier.js");
@@ -1,78 +0,0 @@
1
- #!/bin/bash
2
-
3
- install_gh() {
4
- type -p curl >/dev/null || sudo apt install curl -y
5
- curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg
6
- sudo chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg
7
- echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list >/dev/null
8
- sudo apt update
9
- sudo apt install gh -y
10
- }
11
-
12
- handle_error() {
13
- if [ $? -ne 0 ]; then
14
- echo "Error: $1 failed. Please check the logs and resolve the issue before retrying."
15
- exit 1
16
- fi
17
- }
18
-
19
- bump_package() {
20
- echo "== Bump the NPM package version =="
21
- yarn install
22
- handle_error "yarn install"
23
- yarn config set version-tag-prefix "v"
24
- yarn version --"$VERSION_LABEL" --no-git-tag-version
25
- echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" >~/.npmrc
26
- yarn publish --non-interactive
27
- handle_error "yarn publish"
28
- }
29
-
30
- raise_pr() {
31
- git config user.name "Joseph Mathew"
32
- git config user.email "joseph.mathew@bigbinary.com"
33
- git config core.hooksPath /dev/null
34
- git push origin --delete bump-version
35
- git checkout -b bump-version
36
- git add -A
37
- git commit -m "Bump version"
38
- git push --set-upstream origin bump-version
39
- gh pr create -B main -H bump-version -t "Bump version" -b "This is an automated PR." -l instant-mergepr -a @me
40
- }
41
-
42
- install_gh
43
-
44
- LATEST_COMMIT_SHA=$(git log -1 --pretty=format:"%H")
45
- echo "Latest commit SHA: $LATEST_COMMIT_SHA"
46
-
47
- PR_NUMBER=$(gh pr list --state merged --search "$LATEST_COMMIT_SHA" --json number --jq ".[0].number")
48
- echo "Last merged PR number: $PR_NUMBER"
49
-
50
- PR_LABELS=$(gh pr view $PR_NUMBER --json labels --jq ".labels[].name" | tr "\n" " " | cut -d " " -f 1-)
51
- echo "PR labels: $PR_LABELS"
52
-
53
- [[ -z "$PR_LABELS" ]] && exit 0
54
-
55
- ALL_VERSION_LABELS=(major minor patch)
56
-
57
- PR_VERSION_LABELS=()
58
- for version_label in "${ALL_VERSION_LABELS[@]}"; do
59
- if echo "$PR_LABELS" | grep -q -o "$version_label"; then
60
- PR_VERSION_LABELS+=("$version_label")
61
- fi
62
- done
63
-
64
- PR_VERSION_LABELS_STR="${PR_VERSION_LABELS[*]}"
65
- echo "Semantic versions present in PR labels: $PR_VERSION_LABELS_STR"
66
-
67
- VERSION_LABEL=$(echo "${PR_VERSION_LABELS[0]}" | xargs)
68
- echo "Version label selected: $VERSION_LABEL"
69
-
70
- [[ -z "$VERSION_LABEL" ]] && exit 0
71
-
72
- if [[ -n "$VERSION_LABEL" ]]; then
73
- bump_package
74
- else
75
- echo "PR label doesn't exist"
76
- fi
77
-
78
- raise_pr
@@ -1,5 +0,0 @@
1
- checkout
2
- git fetch --unshallow 2> /dev/null
3
- git config remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*'
4
- git fetch origin main
5
- git diff-tree --diff-filter=a -r --no-commit-id --name-only HEAD remotes/origin/main | grep --color=none -i -e "\.js$" | xargs npx eslint
@@ -1,32 +0,0 @@
1
- version: v1.0
2
- name: cicheck
3
- agent:
4
- machine:
5
- type: e1-standard-2
6
- os_image: ubuntu2004
7
- auto_cancel:
8
- running:
9
- when: "branch != 'main'"
10
- fail_fast:
11
- stop:
12
- when: "true"
13
- global_job_config:
14
- prologue:
15
- commands:
16
- - checkout
17
- - sem-version node 22.13
18
- - yarn install
19
- env_vars:
20
- - name: ENV
21
- value: test
22
- blocks:
23
- - name: Code lints and tests
24
- task:
25
- jobs:
26
- - name: ESLint
27
- commands:
28
- - ".scripts/run_eslint_on_modified_files.sh"
29
- - name: Tests
30
- commands:
31
- - "yarn test"
32
- dependencies: []
package/babel.config.js DELETED
@@ -1 +0,0 @@
1
- module.exports = require("@bigbinary/neeto-commons-frontend/configs/babel.js");
@@ -1,58 +0,0 @@
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.
@@ -1,6 +0,0 @@
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
@@ -1,171 +0,0 @@
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.
@@ -1,191 +0,0 @@
1
- # Zustand pick transformer
2
-
3
- ## Motivation
4
-
5
- When we wanted to pick some properties from a zustand store, this is what we
6
- need to do:
7
-
8
- ```js
9
- import { useShallow } from "zustand/shallow";
10
-
11
- const { order, customer } = useGlobalStore(
12
- useShallow(store => ({
13
- order: store[sessionId]?.globals.order,
14
- customer: store[sessionId]?.globals.customer,
15
- }))
16
- );
17
- ```
18
-
19
- This involves a lot of boilerplate code just to access two properties from a
20
- store containing nested values.
21
-
22
- This transformer is capable of generating all these boilerplate at the time of
23
- transpiling (during bundling) so that we can focus on our business logic. If
24
- this plugin is added to your babel configuration, you can write the following
25
- code to get a similar effect as the above code:
26
-
27
- ```js
28
- const { order, customer } = useGlobalStore.pick([sessionId, "globals"]);
29
- ```
30
-
31
- You might also notice that we don't have the
32
- `import { useShallow } from "zustand/shallow"` statement in this piece of code.
33
- We don't need that. The plugin will automatically add it for us at the time of
34
- transpiling.
35
-
36
- The plugin detects that `order` and `customer` properties are to be fetched from
37
- the `store[sessionId].globals` property from `useGlobalStore` and it generates
38
- code for it. Note that the plugin will automatically add
39
- `import { useShallow } from "zustand/shallow"` for us. We don't need to add it
40
- ourselves.
41
-
42
- ## Allowed syntaxes
43
-
44
- To explain the allowed syntaxes take this zustand store as the example:
45
-
46
- ```js
47
- const useGlobalStore = create((set, get) => ({
48
- order: {
49
- id: "#1230",
50
- user: {
51
- email: "oliver@example.com",
52
- address: {
53
- "location 1": { street: "293, 1st Floor", city: "Mumbai" }
54
- "location 2": { street: "295, 3st Floor", city: "Chennai" }
55
- },
56
- },
57
- items: {
58
- // itemID: { ...properties }
59
- "47e91f1a-0c4d-4a79-a939-1319d8659bd2": { quantity: 10, price: 100 },
60
- "64b7e54c-5d8a-443a-b70b-fff3d6937eba": { quantity: 3, price: 34 },
61
- },
62
- },
63
-
64
- setOrder: order => set({ order }),
65
- }));
66
- ```
67
-
68
- These are the allowed syntaxes:
69
-
70
- ```js
71
- // picks `order` data and `setOrder` action from the store
72
- const { order, setOrder } = useGlobalStore.pick();
73
-
74
- // picks `id` and `user` from inside store.order
75
- const { id, user } = useGlobalStore.pick("order");
76
-
77
- // supports default values
78
- const { user = {} } = useGlobalStore.pick();
79
-
80
- // picks `quantity` of dynamically obtained item of id `itemId`
81
- const { quantity } = useGlobalStore.pick(["order", "items", itemId]);
82
-
83
- // picks all stored properties of dynamically obtained item of id `itemId`
84
- const { [itemId]: itemDetails } = useGlobalStore.pick(["order", "items"]);
85
-
86
- // picks all stored properties of the location at `index` from store.order.user.address
87
- const { [`location ${index}`]: address } = useGlobalStore.pick([
88
- "order",
89
- "user",
90
- "address",
91
- ]);
92
- ```
93
-
94
- ## Disallowed syntaxes
95
-
96
- We have introduced a type definition for `pick` method from zustand stores from
97
- neeto-commons-frontend to make IDE capable of pointing out errors as you type.
98
- But it is not 100% foolproof. So here is a list of usages that are flagged as
99
- syntax errors during transpilation.
100
-
101
- 1. Nesting is not allowed in the variable declaration.
102
-
103
- **incorrect**
104
-
105
- ```js
106
- const {
107
- order: { user },
108
- } = useGlobalStore.pick();
109
- ```
110
-
111
- **correct**
112
-
113
- ```js
114
- const { order } = useGlobalStore.pick();
115
- const { user } = useGlobalStore.pick("order"); // to get nested item
116
- ```
117
-
118
- 2. Expressions are not allowed in picking path array
119
-
120
- **incorrect**
121
-
122
- ```js
123
- // expressions are not allowed
124
- const { user } = useGlobalStore.pick(["order", index + 1]);
125
- // string templates are not allowed
126
- const { user } = useGlobalStore.pick(["order", `#${orderId}`]);
127
- ```
128
-
129
- **correct**
130
-
131
- ```js
132
- // only use literals of String, Numbers or Booleans
133
- // and variables as elements of the path array
134
- const indexPlusOne = index + 1;
135
- const { user } = useGlobalStore.pick(["order", indexPlusOne, 0, true]);
136
- ```
137
-
138
- 3. Pick only accepts a single parameter:
139
-
140
- **incorrect**
141
-
142
- ```js
143
- const { user } = useGlobalStore.pick("order", "user");
144
- ```
145
-
146
- **correct**
147
-
148
- ```js
149
- // use arrays to specify nested path
150
- const { name } = useGlobalStore.pick(["order", "user"]);
151
- // use key directly to get its nested properties.
152
- const { user } = useGlobalStore.pick("order");
153
- // pass no arguments to get the top level properties
154
- const { order, setOrder } = useGlobalStore.pick();
155
- ```
156
-
157
- ## Notes
158
-
159
- #### All nested property access are optional chained
160
-
161
- No need to worry if any of the properties in your store nesting may be
162
- null/undefined. This transformer adds optional chaining in all levels of
163
- nesting. That is:
164
-
165
- ```js
166
- const { order, customer } = useGlobalStore.pick([sessionId, "globals"]);
167
- ```
168
-
169
- Will be transformed to:
170
-
171
- ```js
172
- import { useShallow } from "zustand/shallow";
173
- const { order, customer } = useGlobalStore(
174
- useShallow(store => ({
175
- order: store[sessionId]?.["globals"]?.["order"],
176
- customer: store[sessionId]?.["globals"]?.["customer"],
177
- }))
178
- );
179
- ```
180
-
181
- So your code won't fail if `store[sessionId]` is null or
182
- `store[sessionId].globals` is null.
183
-
184
- #### This plugin should run on user's code
185
-
186
- You might encounter errors if some other plugin transforms the code before
187
- babel-plugin-neeto. **babel-plugin-neeto expects that it runs first on the code
188
- before any other plugin performs transformations.**
189
-
190
- If your zustand-pick syntax is correct, and you are still getting errors, check
191
- for other babel-plugins or babel-presets transforming your code before we do.
package/jsconfig.json DELETED
@@ -1,19 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ESNext",
4
- "allowSyntheticDefaultImports": true,
5
- "strictNullChecks": true,
6
- "lib": ["DOM", "DOM.Iterable", "ESNext"],
7
- "module": "ESNext",
8
- "moduleResolution": "NodeNext",
9
- "baseUrl": ".",
10
- "pretty": true,
11
- "experimentalDecorators": true,
12
- "esModuleInterop": true,
13
- "checkJs": true,
14
- "resolveJsonModule": true,
15
- "jsx": "preserve"
16
- },
17
- "exclude": ["**/node_modules", "**/.*/"],
18
- "include": ["./**/*.js"]
19
- }
@@ -1,38 +0,0 @@
1
- const fs = require("fs");
2
- const path = require("path");
3
-
4
- const { fromPairs } = require("ramda");
5
-
6
- const getPositiveCases = dirName =>
7
- fromPairs(
8
- fs
9
- .readdirSync(path.join(dirName, "no-changes"))
10
- .map(key => [`No change: ${key}`, { fixture: `no-changes/${key}` }])
11
- );
12
-
13
- const getNegativeCases = dirName =>
14
- fromPairs(
15
- fs.readdirSync(path.join(dirName, "transformations")).map(key => [
16
- `Transform: ${key}`,
17
- {
18
- fixture: `transformations/${key}/input.js`,
19
- outputFixture: `transformations/${key}/output.js`,
20
- },
21
- ])
22
- );
23
-
24
- const getIncorrectSyntaxCases = (dirName, error) =>
25
- fromPairs(
26
- fs
27
- .readdirSync(path.join(dirName, "syntax-errors"))
28
- .map(key => [
29
- `Raise syntax error: ${key}`,
30
- { fixture: `syntax-errors/${key}`, throws: error },
31
- ])
32
- );
33
-
34
- module.exports = {
35
- getNegativeCases,
36
- getPositiveCases,
37
- getIncorrectSyntaxCases,
38
- };
@@ -1 +0,0 @@
1
- const { name, email } = useSampleStore.pock();
@@ -1 +0,0 @@
1
- const { name, email } = useSampleStor.pick();
@@ -1 +0,0 @@
1
- const { name, email } = useSampleStore(pick(["name", "email"]));
@@ -1 +0,0 @@
1
- const { name } = useGlobalStore.pick("order").user;
@@ -1 +0,0 @@
1
- const name = useGlobalStore.pick("order")?.user.name;
@@ -1 +0,0 @@
1
- const { user } = useGlobalStore.pick("order", "user");
@@ -1,3 +0,0 @@
1
- const {
2
- order: { user },
3
- } = useGlobalStore.pick();
@@ -1 +0,0 @@
1
- const { user } = useGlobalStore.pick(["order", index + 1]);
@@ -1 +0,0 @@
1
- const { user } = useGlobalStore.pick(["order", `#${orderId}`]);
@@ -1,6 +0,0 @@
1
- const { order, customer } = useGlobalStore.pick([
2
- "state",
3
- sessionId,
4
- "current",
5
- data,
6
- ]);
@@ -1,7 +0,0 @@
1
- import { useShallow } from "zustand/shallow";
2
- const { order, customer } = useGlobalStore(
3
- useShallow(store => ({
4
- order: store["state"]?.[sessionId]?.["current"]?.[data]?.["order"],
5
- customer: store["state"]?.[sessionId]?.["current"]?.[data]?.["customer"],
6
- }))
7
- );
@@ -1 +0,0 @@
1
- const { orderId = "#1001", customer = {} } = useGlobalStore.pick();
@@ -1,7 +0,0 @@
1
- import { useShallow } from "zustand/shallow";
2
- const { orderId = "#1001", customer = {} } = useGlobalStore(
3
- useShallow(store => ({
4
- orderId: store["orderId"],
5
- customer: store["customer"],
6
- }))
7
- );
@@ -1,2 +0,0 @@
1
- const { [`customer ${id}`]: customer } = useGlobalStore.pick(sessionId);
2
- const { [orderId]: order } = useGlobalStore.pick([sessionId, "orders"]);
@@ -1,11 +0,0 @@
1
- import { useShallow } from "zustand/shallow";
2
- const { [`customer ${id}`]: customer } = useGlobalStore(
3
- useShallow(store => ({
4
- [`customer ${id}`]: store[sessionId]?.[`customer ${id}`],
5
- }))
6
- );
7
- const { [orderId]: order } = useGlobalStore(
8
- useShallow(store => ({
9
- [orderId]: store[sessionId]?.["orders"]?.[orderId],
10
- }))
11
- );
@@ -1 +0,0 @@
1
- const { customer } = useGlobalStore.pick(sessionId);
@@ -1,6 +0,0 @@
1
- import { useShallow } from "zustand/shallow";
2
- const { customer } = useGlobalStore(
3
- useShallow(store => ({
4
- customer: store[sessionId]?.["customer"],
5
- }))
6
- );
@@ -1 +0,0 @@
1
- const { order, customer } = useGlobalStore.pick();
@@ -1,7 +0,0 @@
1
- import { useShallow } from "zustand/shallow";
2
- const { order, customer } = useGlobalStore(
3
- useShallow(store => ({
4
- order: store["order"],
5
- customer: store["customer"],
6
- }))
7
- );
@@ -1,2 +0,0 @@
1
- import { useShallow } from "zustand/shallow";
2
- const { order, customer } = useGlobalStore.pick();
@@ -1,7 +0,0 @@
1
- import { useShallow } from "zustand/shallow";
2
- const { order, customer } = useGlobalStore(
3
- useShallow(store => ({
4
- order: store["order"],
5
- customer: store["customer"],
6
- }))
7
- );
@@ -1 +0,0 @@
1
- const { order, customer, address } = useGlobalStore.pick("state");
@@ -1,8 +0,0 @@
1
- import { useShallow } from "zustand/shallow";
2
- const { order, customer, address } = useGlobalStore(
3
- useShallow(store => ({
4
- order: store["state"]?.["order"],
5
- customer: store["state"]?.["customer"],
6
- address: store["state"]?.["address"],
7
- }))
8
- );
@@ -1 +0,0 @@
1
- const { order, customer } = useStore.pick();
@@ -1,7 +0,0 @@
1
- import { useShallow } from "zustand/shallow";
2
- const { order, customer } = useStore(
3
- useShallow(store => ({
4
- order: store["order"],
5
- customer: store["customer"],
6
- }))
7
- );
@@ -1,4 +0,0 @@
1
- import React from "react";
2
- import { sample } from "somewhere";
3
-
4
- const { order, customer } = useGlobalStore.pick();
@@ -1,9 +0,0 @@
1
- import { useShallow } from "zustand/shallow";
2
- import React from "react";
3
- import { sample } from "somewhere";
4
- const { order, customer } = useGlobalStore(
5
- useShallow(store => ({
6
- order: store["order"],
7
- customer: store["customer"],
8
- }))
9
- );
@@ -1,18 +0,0 @@
1
- const { pluginTester } = require("babel-plugin-tester");
2
-
3
- const { INCORRECT_ZUSTAND_PICK_USAGE } = require("../../plugins/messages");
4
- const zustandPick = require("../../plugins/zustand-pick");
5
- const {
6
- getPositiveCases,
7
- getNegativeCases,
8
- getIncorrectSyntaxCases,
9
- } = require("../utils");
10
-
11
- pluginTester({
12
- plugin: zustandPick,
13
- tests: {
14
- ...getPositiveCases(__dirname),
15
- ...getIncorrectSyntaxCases(__dirname, INCORRECT_ZUSTAND_PICK_USAGE),
16
- ...getNegativeCases(__dirname),
17
- },
18
- });
@@ -1 +0,0 @@
1
- const name = useSampleStore.pock();
@@ -1 +0,0 @@
1
- const name = useSampleStor.pickFrom();
@@ -1 +0,0 @@
1
- const name = useGlobalStore.pickFrom("order").user;
@@ -1 +0,0 @@
1
- const name = useGlobalStore.pickFrom("order")?.user.name;
@@ -1 +0,0 @@
1
- const { order } = useGlobalStore.pickFrom();
@@ -1 +0,0 @@
1
- const user = useGlobalStore.pickFrom("order", "user");
@@ -1 +0,0 @@
1
- const user = useGlobalStore.pickFrom(["order", index + 1]);
@@ -1 +0,0 @@
1
- const user = useGlobalStore.pickFrom(["order", `#${orderId}`]);
@@ -1 +0,0 @@
1
- const order = useGlobalStore.pickFrom(["state", sessionId, "current", data]);
@@ -1,3 +0,0 @@
1
- const order = useGlobalStore(
2
- store => store["state"]?.[sessionId]?.["current"]?.[data]?.["order"]
3
- );
@@ -1 +0,0 @@
1
- const customer = useGlobalStore.pickFrom(sessionId);
@@ -1 +0,0 @@
1
- const customer = useGlobalStore(store => store[sessionId]?.["customer"]);
@@ -1 +0,0 @@
1
- const order = useGlobalStore.pickFrom();
@@ -1 +0,0 @@
1
- const order = useGlobalStore(store => store["order"]);
@@ -1 +0,0 @@
1
- const order = useGlobalStore.pickFrom("state");
@@ -1 +0,0 @@
1
- const order = useGlobalStore(store => store["state"]?.["order"]);
@@ -1 +0,0 @@
1
- const order = useStore.pickFrom();
@@ -1 +0,0 @@
1
- const order = useStore(store => store["order"]);
@@ -1,18 +0,0 @@
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
- });