@putout/plugin-package-json 8.1.0 → 8.2.1

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
@@ -18,6 +18,7 @@ npm i @putout/plugin-package-json -D
18
18
  - ✅ [find-file](#find-file);
19
19
  - ✅ [remove-nyc](#remove-nyc);
20
20
  - ✅ [remove-commit-type](#remove-commit-type);
21
+ - ✅ [remove-exports-with-missing-files](#remove-exports-with-missing-files);
21
22
 
22
23
  ## Config
23
24
 
@@ -28,6 +29,7 @@ npm i @putout/plugin-package-json -D
28
29
  "package-json/apply-https-to-repository-url": "on",
29
30
  "package-json/remove-nyc": "on",
30
31
  "package-json/remove-commit-type": "on",
32
+ "package-json/remove-exports-with-missing-files": "off",
31
33
  "package-json/find-file": "off"
32
34
  }
33
35
  }
@@ -135,6 +137,31 @@ Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/325233d19f
135
137
  }
136
138
  ```
137
139
 
140
+ ## remove-exports-with-missing-files
141
+
142
+ Find `package.json` inside of `.filesystem.json` and removes `exports` with missing files.
143
+
144
+ Checkout in 🐊**Putout Editor**:
145
+
146
+ - [`remove-exports-with-missing-files`](https://putout.cloudcmd.io/#/gist/c79a69b797ccd2d94499349150e65f7c/37a48a054b98299cb15e71c3eaeb23b0b919d62c);
147
+ - [`find-keys`](https://putout.cloudcmd.io/#/gist/b138e991aa21ad0ffb562b4c6fe6290f/cca92b8b87ca9d91c0a280991a1558a5e3fc260b);
148
+ - [`remove-keys`](https://putout.cloudcmd.io/#/gist/b2fa6fba917a22bfc676f01532b4794e/e5970eb901ef5b29f7c143f76df9a10148b69d9e);
149
+
150
+ ```diff
151
+ __putout_processor_filesystem([
152
+ "/",
153
+ ["/package.json", `{
154
+ "exports": {
155
+ "./parse-options": "./lib/parse-options/index.js",
156
+ - "./loader": "./lib/loader.mjs"
157
+ }
158
+ }`],
159
+ "/lib/",
160
+ "/lib/parse-options/",
161
+ ["/lib/parse-options/index.js", "export const a = 5"],
162
+ ]);
163
+ ```
164
+
138
165
  ## License
139
166
 
140
167
  MIT
package/lib/index.js CHANGED
@@ -1,7 +1,9 @@
1
1
  import {rules as packageJson} from './package-json.js';
2
2
  import * as findFile from './find-file/index.js';
3
+ import * as removeExportsWithMissingFiles from './remove-exports-with-missing-files/index.js';
3
4
 
4
5
  export const rules = {
5
6
  ...packageJson,
6
7
  'find-file': ['off', findFile],
8
+ 'remove-exports-with-missing-files': ['off', removeExportsWithMissingFiles],
7
9
  };
@@ -0,0 +1,32 @@
1
+ import {operator, types} from 'putout';
2
+
3
+ const {isObjectExpression} = types;
4
+ const {getProperty, __json} = operator;
5
+
6
+ export const report = ({node}) => {
7
+ const key = node.key.value;
8
+ const {value} = node.value;
9
+
10
+ return `${key} -> ${value}`;
11
+ };
12
+
13
+ export const fix = () => {};
14
+
15
+ export const traverse = ({push}) => ({
16
+ [__json](path) {
17
+ const object = path.get('arguments.0');
18
+ const exportsPath = getProperty(object, 'exports');
19
+
20
+ if (!exportsPath)
21
+ return;
22
+
23
+ const objectPath = exportsPath.get('value');
24
+
25
+ if (!isObjectExpression(objectPath))
26
+ return;
27
+
28
+ for (const property of objectPath.get('properties')) {
29
+ push(property);
30
+ }
31
+ },
32
+ });
@@ -0,0 +1,67 @@
1
+ import {join} from 'node:path';
2
+ import {
3
+ operator,
4
+ parse,
5
+ print,
6
+ transform,
7
+ findPlaces,
8
+ } from 'putout';
9
+ import * as findKeys from './find-keys/index.js';
10
+ import * as removeKeys from './remove-keys/index.js';
11
+
12
+ const {
13
+ findFile,
14
+ getFilename,
15
+ getParentDirectory,
16
+ readFileContent,
17
+ toJS,
18
+ writeFileContent,
19
+ fromJS,
20
+ } = operator;
21
+
22
+ export const report = () => `Avoid exports with missing files`;
23
+
24
+ export const fix = (file, {ast, source}) => {
25
+ transform(ast, source, {
26
+ plugins: [
27
+ ['remove-keys', removeKeys],
28
+ ],
29
+ });
30
+
31
+ const code = print(ast);
32
+ writeFileContent(file, fromJS(code));
33
+ };
34
+
35
+ export const scan = (root, {push, trackFile}) => {
36
+ for (const file of trackFile(root, 'package.json')) {
37
+ const source = toJS(readFileContent(file));
38
+ const ast = parse(source);
39
+ const places = findPlaces(ast, source, {
40
+ plugins: [
41
+ ['find-keys', findKeys],
42
+ ],
43
+ });
44
+
45
+ const tuples = [];
46
+
47
+ for (const place of places) {
48
+ tuples.push(place.message.split(' -> '));
49
+ }
50
+
51
+ const dirPath = getParentDirectory(file);
52
+ const dir = getFilename(dirPath);
53
+
54
+ for (const [key, name] of tuples) {
55
+ const full = join(dir, name);
56
+
57
+ const [exportedFile] = findFile(dirPath, full);
58
+
59
+ if (!exportedFile)
60
+ push(file, {
61
+ key,
62
+ ast,
63
+ source,
64
+ });
65
+ }
66
+ }
67
+ };
@@ -0,0 +1,34 @@
1
+ import {operator} from 'putout';
2
+
3
+ const {
4
+ getProperty,
5
+ __json,
6
+ remove,
7
+ } = operator;
8
+
9
+ export const report = ({node}) => {
10
+ const key = node.key.value;
11
+ const {value} = node.value;
12
+
13
+ return `${key} -> ${value}`;
14
+ };
15
+
16
+ export const fix = (path) => {
17
+ remove(path);
18
+ };
19
+
20
+ export const traverse = ({push, options}) => ({
21
+ [__json](path) {
22
+ const {keys = ['./loader']} = options;
23
+
24
+ const object = path.get('arguments.0');
25
+ const exportsPath = getProperty(object, 'exports');
26
+
27
+ for (const property of exportsPath.get('value.properties')) {
28
+ const {value} = property.node.key;
29
+
30
+ if (keys.includes(value))
31
+ push(property);
32
+ }
33
+ },
34
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-package-json",
3
- "version": "8.1.0",
3
+ "version": "8.2.1",
4
4
  "type": "module",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
6
  "description": "🐊Putout plugin for package.json",