@putout/plugin-optional-chaining 1.0.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 +137 -0
- package/lib/convert-assign-to-optional/index.js +9 -0
- package/lib/convert-logical-to-optional/index.js +19 -0
- package/lib/convert-optiional-assign-to-logical/index.js +39 -0
- package/lib/convert-optional-to-logical/index.js +28 -0
- package/lib/get-logical.js +34 -0
- package/lib/index.js +13 -0
- package/package.json +53 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) coderaiser
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
# @putout/plugin-optional-chaining [![NPM version][NPMIMGURL]][NPMURL]
|
|
2
|
+
|
|
3
|
+
[NPMIMGURL]: https://img.shields.io/npm/v/@putout/plugin-apply-optional-chaining.svg?style=flat&longCache=true
|
|
4
|
+
[NPMURL]: https://npmjs.org/package/@putout/plugin-apply-optional-chaining "npm"
|
|
5
|
+
|
|
6
|
+
> The **optional chaining operator** (`?.`) enables you to read the value of a property located deep within a chain of connected objects without having to check that each reference in the chain is valid.
|
|
7
|
+
>
|
|
8
|
+
> The `?.` operator is like the `.` chaining operator, except that instead of causing an error if a reference is nullish (`null` or `undefined`), the expression short-circuits with a return value of `undefined`. When used with function calls, it returns `undefined` if the given function does not exist.
|
|
9
|
+
>
|
|
10
|
+
> This results in shorter and simpler expressions when accessing chained properties when the possibility exists that a reference may be missing. It can also be helpful while exploring the content of an object when there's no known guarantee as to which properties are required.
|
|
11
|
+
>
|
|
12
|
+
> (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining)
|
|
13
|
+
|
|
14
|
+
π[**Putout**](https://github.com/coderaiser/putout) plugin related to **Optional Chaining**.
|
|
15
|
+
|
|
16
|
+
## Install
|
|
17
|
+
|
|
18
|
+
```
|
|
19
|
+
npm i @putout/plugin-optional-chaining
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Rules
|
|
23
|
+
|
|
24
|
+
- β
[convert-logical-to-optional][#convert-logical-to-optional];
|
|
25
|
+
- β
[convert-optional-to-logical][#convert-optional-to-logical];
|
|
26
|
+
- β
[convert-logical-assign-to-optional][#convert-logical-assign-to-optional];
|
|
27
|
+
- β
[convert-optional-assign-to-logical][#convert-logical-assign-to-optional];
|
|
28
|
+
|
|
29
|
+
## Config
|
|
30
|
+
|
|
31
|
+
```json
|
|
32
|
+
{
|
|
33
|
+
"rules": {
|
|
34
|
+
"optional-chaining/convert-logical-to-optional": "on",
|
|
35
|
+
"optional-chaining/convert-optional-assign-to-logical": "on",
|
|
36
|
+
"optional-chaining/convert-logical-assign-to-optional": "off",
|
|
37
|
+
"optional-chaining/convert-optional-to-logical": "off"
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## convert-logical-to-optional
|
|
43
|
+
|
|
44
|
+
> The logical **AND** (`&&`) (logical conjunction) operator for a set of boolean operands will be true if and only if all the operands are true. Otherwise it will be false.
|
|
45
|
+
>
|
|
46
|
+
> More generally, the operator returns the value of the first falsy operand encountered when evaluating from left to right, or the value of the last operand if they are all truthy.
|
|
47
|
+
>
|
|
48
|
+
> (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_AND)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
Checkout out in π[**Putout Editor**](https://putout.cloudcmd.io/#/gist/d308302b95800920d324b799f1a948e3/99d03cb297d17446885829e8583b3cc7777367c5).
|
|
52
|
+
|
|
53
|
+
### β Example of incorrect code
|
|
54
|
+
|
|
55
|
+
```js
|
|
56
|
+
const result = hello && hello.world;
|
|
57
|
+
|
|
58
|
+
if (typeof a === 'function' && a(1, 2))
|
|
59
|
+
alert();
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### β
Example of correct code
|
|
63
|
+
|
|
64
|
+
```js
|
|
65
|
+
const result = hello?.world;
|
|
66
|
+
|
|
67
|
+
if (a?.(1, 2))
|
|
68
|
+
alert();
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## convert-logical-assign-to-optional
|
|
72
|
+
|
|
73
|
+
> Proposal to add support for optional chaining on the left of assignment operators: `a?.b = c`.
|
|
74
|
+
>
|
|
75
|
+
> (c) [Proposal of Optional Chaining Assignment](https://github.com/tc39/proposal-optional-chaining-assignment)
|
|
76
|
+
|
|
77
|
+
Disabled by default. Checkout out in π[**Putout Editor**](https://putout.cloudcmd.io/#/gist/74bde454f909b7f9d13e80da10e12a15/d31bd20ca18fefe474bb8ab73f963e47dffb89e7).
|
|
78
|
+
|
|
79
|
+
### β Example of incorrect code
|
|
80
|
+
|
|
81
|
+
```js
|
|
82
|
+
if (a) {
|
|
83
|
+
a.b = 5;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (a)
|
|
87
|
+
a.b = 5;
|
|
88
|
+
|
|
89
|
+
a && (a.b = 5);
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### β
Example of correct code
|
|
93
|
+
|
|
94
|
+
```js
|
|
95
|
+
a?.b = 5;
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
## convert-optional-assign-to-logical
|
|
99
|
+
|
|
100
|
+
> The JavaScript exception "invalid assignment left-hand side" occurs when there was an unexpected assignment somewhere. It may be triggered when a single `=` sign was used instead of `==` or `===`.
|
|
101
|
+
>
|
|
102
|
+
> (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Invalid_assignment_left-hand_side)
|
|
103
|
+
|
|
104
|
+
Checkout in π[**Putout Editor**](https://putout.cloudcmd.io/#/gist/29c00f1b02b592a3dcd7b7e185dfd354/50a1a416da21b02df3a76954b4d7a1ff7dac1eaf).
|
|
105
|
+
|
|
106
|
+
### β Example of incorrect code
|
|
107
|
+
|
|
108
|
+
```js
|
|
109
|
+
maybeAnObj?.prop = theValue;
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### β
Example of correct code
|
|
113
|
+
|
|
114
|
+
```js
|
|
115
|
+
maybeAnObj && (maybeAnObj.prop = theValue);
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## convert-optioanl-to-logical
|
|
119
|
+
|
|
120
|
+
Disabled by default.
|
|
121
|
+
Checkout in π[**Putout Editor**](https://putout.cloudcmd.io/#/gist/e0a4ccb41708ad37e34d527a978ebb88/482f15c954cdaa35e37da7a1dddb82338d7e93a2).
|
|
122
|
+
|
|
123
|
+
### β Example of incorrect code
|
|
124
|
+
|
|
125
|
+
```js
|
|
126
|
+
hello?.world?.();
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
### β
Example of correct code
|
|
130
|
+
|
|
131
|
+
```js
|
|
132
|
+
hello && hello.world && hello.world();
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
## License
|
|
136
|
+
|
|
137
|
+
MIT
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
module.exports.report = () => `Use optional expression ('a?.b = c') instead of 'condition' ('a && a.b = c')`;
|
|
4
|
+
|
|
5
|
+
module.exports.replace = () => ({
|
|
6
|
+
'if (__a) {__a.__b = __c}': '__a?.__b = __c',
|
|
7
|
+
'if (__a) __a.__b = __c': '__a?.__b = __c',
|
|
8
|
+
'__a && (__a.__b = __c)': '__a?.__b = __c',
|
|
9
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
module.exports.report = () => 'Use optional chaining';
|
|
4
|
+
|
|
5
|
+
module.exports.replace = () => ({
|
|
6
|
+
'__a && __a.__b && __a.__b.__c && __a.__b.__c.__d && __a.__b.__c.__d.__e': '__a?.__b?.__c?.__d?.__e',
|
|
7
|
+
'__a && __a.__b && __a.__b.__c && __a.__b.__c.__d': '__a?.__b?.__c?.__d',
|
|
8
|
+
'__a && __a.__b && __a.__b.__c': '__a?.__b?.__c',
|
|
9
|
+
'__a && __a.__b': '__a?.__b',
|
|
10
|
+
'__a && __a.__b.__c': '__a?.__b.__c',
|
|
11
|
+
'__a && __a[__b]': '__a?.[__b]',
|
|
12
|
+
'__a[__b] && __a[__b][__c]': '__a[__b]?.[__c]',
|
|
13
|
+
'typeof __a === "function" && __a(__args)': '__a?.(__args)',
|
|
14
|
+
'isFn(__a) && __a(__args)': '__a?.(__args)',
|
|
15
|
+
'if (typeof __a === "function") __a(__args)': '__a?.(__args)',
|
|
16
|
+
'if (typeof __a === "function") {__a(__args)}': '__a?.(__args)',
|
|
17
|
+
'__a && __a.__b(__args)': '__a?.__b(__args)',
|
|
18
|
+
'__a.__b && __a.__b[__c] && __a.__b[__c].__d(__args)': '__a.__b?.[__c]?.__d(__args)',
|
|
19
|
+
});
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const {template, operator} = require('putout');
|
|
4
|
+
|
|
5
|
+
const {getLogical} = require('../get-logical');
|
|
6
|
+
const {replaceWith} = operator;
|
|
7
|
+
|
|
8
|
+
module.exports.report = () => `Use Logical Expression ('a && a.b = c') instead of Optional Chaining ('a?.b = c')`;
|
|
9
|
+
|
|
10
|
+
module.exports.fix = (path) => {
|
|
11
|
+
const logical = getLogical(path, {
|
|
12
|
+
assign: true,
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
const logicalNode = template.ast(logical);
|
|
16
|
+
|
|
17
|
+
logicalNode.right.right = path.parentPath.node.right;
|
|
18
|
+
replaceWith(path.parentPath, logicalNode);
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
module.exports.traverse = ({push, listStore}) => ({
|
|
22
|
+
'OptionalMemberExpression|OptionalCallExpression'(path) {
|
|
23
|
+
if (!path.parentPath.isAssignmentExpression())
|
|
24
|
+
return;
|
|
25
|
+
|
|
26
|
+
const rightPath = path.parentPath.get('right');
|
|
27
|
+
|
|
28
|
+
if (path === rightPath)
|
|
29
|
+
return;
|
|
30
|
+
|
|
31
|
+
listStore(path);
|
|
32
|
+
},
|
|
33
|
+
Program: {
|
|
34
|
+
exit() {
|
|
35
|
+
for (const path of listStore().reverse())
|
|
36
|
+
push(path);
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
});
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const {template, operator} = require('putout');
|
|
4
|
+
const {getLogical} = require('../get-logical');
|
|
5
|
+
|
|
6
|
+
module.exports.report = () => `Use Logical Expression instead of Optional Chaining`;
|
|
7
|
+
|
|
8
|
+
const {replaceWith} = operator;
|
|
9
|
+
|
|
10
|
+
module.exports.fix = (path) => {
|
|
11
|
+
const logical = getLogical(path);
|
|
12
|
+
replaceWith(path, template.ast(logical));
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
module.exports.include = () => [
|
|
16
|
+
'OptionalMemberExpression',
|
|
17
|
+
'OptionalCallExpression',
|
|
18
|
+
];
|
|
19
|
+
|
|
20
|
+
module.exports.filter = ({parentPath}) => {
|
|
21
|
+
if (parentPath.isOptionalMemberExpression())
|
|
22
|
+
return false;
|
|
23
|
+
|
|
24
|
+
if (parentPath.isAssignmentExpression())
|
|
25
|
+
return false;
|
|
26
|
+
|
|
27
|
+
return !parentPath.isOptionalCallExpression();
|
|
28
|
+
};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
module.exports.getLogical = (path, {assign} = {}) => {
|
|
4
|
+
const list = path
|
|
5
|
+
.toString()
|
|
6
|
+
.split('?.');
|
|
7
|
+
|
|
8
|
+
const n = list.length;
|
|
9
|
+
let [member] = list;
|
|
10
|
+
let i = assign ? 1 : 0;
|
|
11
|
+
const logical = [member];
|
|
12
|
+
|
|
13
|
+
while (++i < n) {
|
|
14
|
+
member += compute(member, list[i]);
|
|
15
|
+
logical.push(member);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const fullLogical = logical.join(' && ');
|
|
19
|
+
|
|
20
|
+
if (!assign)
|
|
21
|
+
return fullLogical;
|
|
22
|
+
|
|
23
|
+
return `${fullLogical} && (${logical.at(-1)} = __a)`;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
function compute(member, current) {
|
|
27
|
+
if (current[0] === '(')
|
|
28
|
+
return current;
|
|
29
|
+
|
|
30
|
+
if (current[0] === '[')
|
|
31
|
+
return current;
|
|
32
|
+
|
|
33
|
+
return `.${current}`;
|
|
34
|
+
}
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const convertAssignToOptional = require('./convert-assign-to-optional');
|
|
4
|
+
const convertLogicalToOptional = require('./convert-logical-to-optional');
|
|
5
|
+
const convertOptionalAssignToLogical = require('./convert-optiional-assign-to-logical');
|
|
6
|
+
const convertOptionalToLogical = require('./convert-optional-to-logical');
|
|
7
|
+
|
|
8
|
+
module.exports.rules = {
|
|
9
|
+
'convert-assign-to-optional': ['off', convertAssignToOptional],
|
|
10
|
+
'convert-logical-to-optional': convertLogicalToOptional,
|
|
11
|
+
'convert-optiional-assign-to-logical': convertOptionalAssignToLogical,
|
|
12
|
+
'convert-optional-to-logical': ['off', convertOptionalToLogical],
|
|
13
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@putout/plugin-optional-chaining",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"type": "commonjs",
|
|
5
|
+
"author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
|
|
6
|
+
"description": "πPutout plugin adds ability apply optional chaining",
|
|
7
|
+
"homepage": "https://github.com/coderaiser/putout/tree/master/packages/plugin-apply-optional-chaining#readme",
|
|
8
|
+
"main": "lib/index.js",
|
|
9
|
+
"release": false,
|
|
10
|
+
"tag": false,
|
|
11
|
+
"changelog": false,
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/coderaiser/putout.git"
|
|
15
|
+
},
|
|
16
|
+
"scripts": {
|
|
17
|
+
"test": "madrun test",
|
|
18
|
+
"watch:test": "madrun watch:test",
|
|
19
|
+
"lint": "madrun lint",
|
|
20
|
+
"fresh:lint": "madrun fresh:lint",
|
|
21
|
+
"lint:fresh": "madrun lint:fresh",
|
|
22
|
+
"fix:lint": "madrun fix:lint",
|
|
23
|
+
"coverage": "madrun coverage",
|
|
24
|
+
"report": "madrun report"
|
|
25
|
+
},
|
|
26
|
+
"dependencies": {},
|
|
27
|
+
"keywords": [
|
|
28
|
+
"putout",
|
|
29
|
+
"putout-plugin",
|
|
30
|
+
"plugin",
|
|
31
|
+
"optional-chaining"
|
|
32
|
+
],
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@putout/test": "^11.0.0",
|
|
35
|
+
"c8": "^10.0.0",
|
|
36
|
+
"eslint": "^9.0.0",
|
|
37
|
+
"eslint-plugin-n": "^17.0.0",
|
|
38
|
+
"eslint-plugin-putout": "^23.0.0",
|
|
39
|
+
"lerna": "^6.0.1",
|
|
40
|
+
"madrun": "^10.0.0",
|
|
41
|
+
"nodemon": "^3.0.1"
|
|
42
|
+
},
|
|
43
|
+
"peerDependencies": {
|
|
44
|
+
"putout": ">=36"
|
|
45
|
+
},
|
|
46
|
+
"license": "MIT",
|
|
47
|
+
"engines": {
|
|
48
|
+
"node": ">=18"
|
|
49
|
+
},
|
|
50
|
+
"publishConfig": {
|
|
51
|
+
"access": "public"
|
|
52
|
+
}
|
|
53
|
+
}
|