@putout/plugin-optional-chaining 1.0.4 → 3.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/README.md CHANGED
@@ -114,7 +114,7 @@ maybeAnObj?.prop = theValue;
114
114
  maybeAnObj && (maybeAnObj.prop = theValue);
115
115
  ```
116
116
 
117
- ## convert-optioanl-to-logical
117
+ ## convert-optional-to-logical
118
118
 
119
119
  Disabled by default.
120
120
  Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/e0a4ccb41708ad37e34d527a978ebb88/482f15c954cdaa35e37da7a1dddb82338d7e93a2).
@@ -1,8 +1,6 @@
1
- 'use strict';
1
+ export const report = () => `Use optional expression ('a?.b = c') instead of 'condition' ('a && a.b = c')`;
2
2
 
3
- module.exports.report = () => `Use optional expression ('a?.b = c') instead of 'condition' ('a && a.b = c')`;
4
-
5
- module.exports.replace = () => ({
3
+ export const replace = () => ({
6
4
  'if (__a) {__a.__b = __c}': '__a?.__b = __c',
7
5
  'if (__a) __a.__b = __c': '__a?.__b = __c',
8
6
  '__a && (__a.__b = __c)': '__a?.__b = __c',
@@ -1,8 +1,6 @@
1
- 'use strict';
1
+ export const report = () => 'Use optional chaining';
2
2
 
3
- module.exports.report = () => 'Use optional chaining';
4
-
5
- module.exports.replace = () => ({
3
+ export const replace = () => ({
6
4
  '__a && __a.__b && __a.__b.__c && __a.__b.__c.__d && __a.__b.__c.__d.__e': '__a?.__b?.__c?.__d?.__e',
7
5
  '__a && __a.__b && __a.__b.__c && __a.__b.__c.__d': '__a?.__b?.__c?.__d',
8
6
  '__a && __a.__b && __a.__b.__c': '__a?.__b?.__c',
@@ -11,7 +9,6 @@ module.exports.replace = () => ({
11
9
  '__a && __a[__b]': '__a?.[__b]',
12
10
  '__a[__b] && __a[__b][__c]': '__a[__b]?.[__c]',
13
11
  'typeof __a === "function" && __a(__args)': '__a?.(__args)',
14
- 'isFn(__a) && __a(__args)': '__a?.(__args)',
15
12
  'if (typeof __a === "function") __a(__args)': '__a?.(__args)',
16
13
  'if (typeof __a === "function") {__a(__args)}': '__a?.(__args)',
17
14
  '__a && __a.__b(__args)': '__a?.__b(__args)',
@@ -1,13 +1,11 @@
1
- 'use strict';
1
+ import {template, operator} from 'putout';
2
+ import {getLogical} from '../get-logical.js';
2
3
 
3
- const {template, operator} = require('putout');
4
-
5
- const {getLogical} = require('../get-logical');
6
4
  const {replaceWith} = operator;
7
5
 
8
- module.exports.report = () => `Use Logical Expression ('a && a.b = c') instead of Optional Chaining ('a?.b = c')`;
6
+ export const report = () => `Use Logical Expression ('a && a.b = c') instead of Optional Chaining ('a?.b = c')`;
9
7
 
10
- module.exports.fix = (path) => {
8
+ export const fix = (path) => {
11
9
  const logical = getLogical(path, {
12
10
  assign: true,
13
11
  });
@@ -18,7 +16,7 @@ module.exports.fix = (path) => {
18
16
  replaceWith(path.parentPath, logicalNode);
19
17
  };
20
18
 
21
- module.exports.traverse = ({push, listStore}) => ({
19
+ export const traverse = ({push, listStore}) => ({
22
20
  'OptionalMemberExpression|OptionalCallExpression'(path) {
23
21
  if (!path.parentPath.isAssignmentExpression())
24
22
  return;
@@ -1,23 +1,21 @@
1
- 'use strict';
1
+ import {template, operator} from 'putout';
2
+ import {getLogical} from '../get-logical.js';
2
3
 
3
- const {template, operator} = require('putout');
4
- const {getLogical} = require('../get-logical');
5
-
6
- module.exports.report = () => `Use Logical Expression instead of Optional Chaining`;
4
+ export const report = () => `Use Logical Expression instead of Optional Chaining`;
7
5
 
8
6
  const {replaceWith} = operator;
9
7
 
10
- module.exports.fix = (path) => {
8
+ export const fix = (path) => {
11
9
  const logical = getLogical(path);
12
10
  replaceWith(path, template.ast(logical));
13
11
  };
14
12
 
15
- module.exports.include = () => [
13
+ export const include = () => [
16
14
  'OptionalMemberExpression',
17
15
  'OptionalCallExpression',
18
16
  ];
19
17
 
20
- module.exports.filter = ({parentPath}) => {
18
+ export const filter = ({parentPath}) => {
21
19
  if (parentPath.isOptionalMemberExpression())
22
20
  return false;
23
21
 
@@ -1,6 +1,4 @@
1
- 'use strict';
2
-
3
- module.exports.getLogical = (path, {assign} = {}) => {
1
+ export const getLogical = (path, {assign} = {}) => {
4
2
  const list = path
5
3
  .toString()
6
4
  .split('?.');
@@ -11,7 +9,7 @@ module.exports.getLogical = (path, {assign} = {}) => {
11
9
  const logical = [member];
12
10
 
13
11
  while (++i < n) {
14
- member += compute(member, list[i]);
12
+ member += compute(list[i]);
15
13
  logical.push(member);
16
14
  }
17
15
 
@@ -23,7 +21,7 @@ module.exports.getLogical = (path, {assign} = {}) => {
23
21
  return `${fullLogical} && (${logical.at(-1)} = __a)`;
24
22
  };
25
23
 
26
- function compute(member, current) {
24
+ function compute(current) {
27
25
  if (current[0] === '(')
28
26
  return current;
29
27
 
package/lib/index.js CHANGED
@@ -1,11 +1,9 @@
1
- 'use strict';
1
+ import * as convertLogicalAssignToOptional from './convert-logical-assign-to-optional/index.js';
2
+ import * as convertLogicalToOptional from './convert-logical-to-optional/index.js';
3
+ import * as convertOptionalAssignToLogical from './convert-optional-assign-to-logical/index.js';
4
+ import * as convertOptionalToLogical from './convert-optional-to-logical/index.js';
2
5
 
3
- const convertLogicalAssignToOptional = require('./convert-logical-assign-to-optional');
4
- const convertLogicalToOptional = require('./convert-logical-to-optional');
5
- const convertOptionalAssignToLogical = require('./convert-optional-assign-to-logical');
6
- const convertOptionalToLogical = require('./convert-optional-to-logical');
7
-
8
- module.exports.rules = {
6
+ export const rules = {
9
7
  'convert-logical-assign-to-optional': ['off', convertLogicalAssignToOptional],
10
8
  'convert-logical-to-optional': convertLogicalToOptional,
11
9
  'convert-optional-assign-to-logical': convertOptionalAssignToLogical,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@putout/plugin-optional-chaining",
3
- "version": "1.0.4",
4
- "type": "commonjs",
3
+ "version": "3.0.0",
4
+ "type": "module",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
6
  "description": "🐊Putout plugin adds ability apply optional chaining",
7
7
  "homepage": "https://github.com/coderaiser/putout/tree/master/packages/plugin-optional-chaining#readme",
@@ -31,21 +31,21 @@
31
31
  "optional-chaining"
32
32
  ],
33
33
  "devDependencies": {
34
- "@putout/test": "^11.0.0",
35
- "c8": "^10.0.0",
36
- "eslint": "^9.0.0",
34
+ "@putout/eslint-flat": "^4.0.0",
35
+ "@putout/test": "^15.0.0",
36
+ "eslint": "^10.0.0",
37
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"
38
+ "eslint-plugin-putout": "^31.0.0",
39
+ "madrun": "^13.0.0",
40
+ "nodemon": "^3.0.1",
41
+ "superc8": "^12.0.0"
42
42
  },
43
43
  "peerDependencies": {
44
- "putout": ">=37"
44
+ "putout": ">=42"
45
45
  },
46
46
  "license": "MIT",
47
47
  "engines": {
48
- "node": ">=18"
48
+ "node": ">=22"
49
49
  },
50
50
  "publishConfig": {
51
51
  "access": "public"