@putout/plugin-package-json 1.0.3 → 4.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
@@ -3,7 +3,7 @@
3
3
  [NPMIMGURL]: https://img.shields.io/npm/v/@putout/plugin-package-json.svg?style=flat&longCache=true
4
4
  [NPMURL]: https://npmjs.org/package/@putout/plugin-package-json"npm"
5
5
 
6
- 🐊[`Putout`](https://github.com/coderaiser/putout) plugin helps to automate fixing `package-json` config.
6
+ 🐊[**Putout**](https://github.com/coderaiser/putout) plugin helps to automate fixing `package.json`.
7
7
 
8
8
  ## Install
9
9
 
@@ -16,7 +16,8 @@ npm i @putout/plugin-package-json -D
16
16
  ```json
17
17
  {
18
18
  "rules": {
19
- "package-json/remove-nyc": "on"
19
+ "package-json/remove-nyc": "on",
20
+ "package-json/add-type": "on"
20
21
  }
21
22
  }
22
23
  ```
@@ -26,9 +27,9 @@ npm i @putout/plugin-package-json -D
26
27
  - additional fields in `package.json` produces more traffic then users of your package really need;
27
28
  - [c8](https://github.com/bcoe/c8) uses [same config name and format](https://github.com/bcoe/c8/blob/v7.3.5/lib/parse-args.js#L8) so transition between tools will be much easier;
28
29
 
29
- ### ❌ Incorrect code example
30
+ ### ❌ Example of incorrect code
30
31
 
31
- `nyc` section in "package.json":
32
+ `nyc` section in `package.json`:
32
33
 
33
34
  ```json
34
35
  {
@@ -49,7 +50,7 @@ npm i @putout/plugin-package-json -D
49
50
  }
50
51
  ```
51
52
 
52
- ### ✅ Correct code Example
53
+ ### ✅ Example of correct code
53
54
 
54
55
  File `.nycrc.json`:
55
56
 
@@ -70,6 +71,18 @@ File `.nycrc.json`:
70
71
  }
71
72
  ```
72
73
 
74
+ ## add-type
75
+
76
+ Add [`type`](https://nodejs.org/dist/latest-v17.x/docs/api/packages.html#type) field to `package.json`:
77
+
78
+ ```diff
79
+ {
80
+ "name": "hello",
81
+ "version": "1.0.0",
82
+ + "type": "commonjs"
83
+ }
84
+ ```
85
+
73
86
  ## License
74
87
 
75
88
  MIT
@@ -0,0 +1,36 @@
1
+ 'use strict';
2
+
3
+ const {
4
+ operator,
5
+ types,
6
+ } = require('putout');
7
+
8
+ const {
9
+ ObjectProperty,
10
+ StringLiteral,
11
+ } = types;
12
+
13
+ const {getProperties} = operator;
14
+
15
+ module.exports.report = () => `Add 'type' of module to 'package.json'`;
16
+
17
+ module.exports.traverse = ({push}) => ({
18
+ '__putout_processor_json(__a)': (path) => {
19
+ const __aPath = path.get('arguments.0');
20
+ const {versionPath, typePath} = getProperties(__aPath, ['version', 'type']);
21
+
22
+ if (typePath)
23
+ return;
24
+
25
+ if (!versionPath)
26
+ return;
27
+
28
+ push(versionPath);
29
+ },
30
+ });
31
+
32
+ module.exports.fix = (path) => {
33
+ const node = ObjectProperty(StringLiteral('type'), StringLiteral('commonjs'));
34
+ path.insertAfter(node);
35
+ };
36
+
package/lib/index.js CHANGED
@@ -6,5 +6,6 @@ const getRule = (a) => ({
6
6
 
7
7
  module.exports.rules = {
8
8
  ...getRule('remove-nyc'),
9
+ ...getRule('add-type'),
9
10
  };
10
11
 
@@ -1,26 +1,22 @@
1
1
  'use strict';
2
2
 
3
- const getKey = (a) => a.key;
4
- const isNYC = (a) => a.value === 'nyc';
3
+ const {getProperties} = require('putout').operator;
5
4
 
6
5
  module.exports.report = () => `Remove 'nyc' section of 'package.json', use file '.nycrc.json' intead`;
7
6
 
8
- module.exports.match = () => ({
9
- '__putout_processor_json(__a)': ({__a}) => __a.properties
10
- .map(getKey)
11
- .filter(isNYC).length,
12
- });
7
+ module.exports.fix = (path) => {
8
+ path.remove();
9
+ };
13
10
 
14
- module.exports.replace = () => ({
15
- '__putout_processor_json(__a)': (vars, path) => {
16
- const properties = path.get('arguments.0.properties');
11
+ module.exports.traverse = ({push}) => ({
12
+ '__putout_processor_json(__a)': (path) => {
13
+ const __aPath = path.get('arguments.0');
14
+ const {nycPath} = getProperties(__aPath, ['nyc']);
17
15
 
18
- for (const propPath of properties) {
19
- if (propPath.get('key.value').node === 'nyc')
20
- propPath.remove();
21
- }
16
+ if (!nycPath)
17
+ return;
22
18
 
23
- return path;
19
+ push(nycPath);
24
20
  },
25
21
  });
26
22
 
package/package.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "name": "@putout/plugin-package-json",
3
- "version": "1.0.3",
3
+ "version": "4.0.0",
4
+ "type": "commonjs",
4
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
5
6
  "description": "putout plugin for package.json",
6
7
  "homepage": "https://github.com/coderaiser/putout/tree/master/packages/plugin-package-json#readme",
@@ -30,21 +31,21 @@
30
31
  "package-json"
31
32
  ],
32
33
  "devDependencies": {
33
- "@putout/test": "^4.0.0",
34
+ "@putout/test": "^5.0.0",
34
35
  "c8": "^7.5.0",
35
36
  "eslint": "^8.0.1",
36
37
  "eslint-plugin-node": "^11.0.0",
37
- "eslint-plugin-putout": "^12.0.0",
38
+ "eslint-plugin-putout": "^14.0.0",
38
39
  "lerna": "^4.0.0",
39
- "madrun": "^8.0.1",
40
+ "madrun": "^9.0.0",
40
41
  "nodemon": "^2.0.1"
41
42
  },
42
43
  "peerDependencies": {
43
- "putout": ">=11"
44
+ "putout": ">=25"
44
45
  },
45
46
  "license": "MIT",
46
47
  "engines": {
47
- "node": ">=12"
48
+ "node": ">=16"
48
49
  },
49
50
  "publishConfig": {
50
51
  "access": "public"