react-remove-props-loader 1.0.4 → 1.1.3

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
@@ -2,6 +2,7 @@
2
2
 
3
3
  [![Version](https://img.shields.io/npm/v/react-remove-props-loader.svg)](https://www.npmjs.com/package/react-remove-props-loader)
4
4
  [![Downloads](https://img.shields.io/npm/dt/react-remove-props-loader.svg)](https://www.npmjs.com/package/react-remove-props-loader)
5
+ [![CI](https://github.com/Mercateo/react-remove-props-loader/actions/workflows/main.yml/badge.svg?branch=master)](https://github.com/Mercateo/react-remove-props-loader/actions/workflows/main.yml?query=branch%3Amaster)
5
6
 
6
7
  A webpack loader for removing React props or JSX attributes in TypeScript/JavaScript code.
7
8
 
@@ -30,8 +31,10 @@ module.exports = {
30
31
  loader: "react-remove-props-loader",
31
32
  options: {
32
33
  props: ["data-testid", "data-test-id"],
33
- // Optionally, set the kind of the script. Defaults to `ScriptKind.JSX`.
34
+ // (Optional) Set the kind of the script. Defaults to `ScriptKind.JSX`.
34
35
  scriptKind: ScriptKind.TSX,
36
+ // (Optional) Set whether to remove specified props from objects. Defaults to `false`.
37
+ removeFromObjects: true,
35
38
  },
36
39
  },
37
40
  ],
@@ -40,3 +43,37 @@ module.exports = {
40
43
  },
41
44
  };
42
45
  ```
46
+
47
+ ## Example
48
+
49
+ ### Source
50
+
51
+ ```tsx
52
+ import React, { FC } from "react";
53
+
54
+ export const ExampleComponent: FC = () => {
55
+ const propsObject = { "data-test-id": "example-prop" };
56
+ return (
57
+ <div data-testid="example-prop" {...propsObject}>
58
+ Example Component
59
+ </div>
60
+ );
61
+ };
62
+ ```
63
+
64
+ ### Transformed
65
+
66
+ Code transformed with the sample settings above.
67
+
68
+ ```tsx
69
+ import React, { FC } from "react";
70
+
71
+ export const ExampleComponent: FC = () => {
72
+ const propsObject = { };
73
+ return (
74
+ <div {...propsObject}>
75
+ Example Component
76
+ </div>
77
+ );
78
+ };
79
+ ```
package/dist/loader.d.ts CHANGED
@@ -5,6 +5,8 @@ export interface LoaderOptions {
5
5
  props: string[];
6
6
  /** The kind of script to remove the React props from. For example, `ScriptKind.TSX`. */
7
7
  scriptKind?: ScriptKind;
8
+ /** Whether to remove specified props from objects. */
9
+ removeFromObjects?: boolean;
8
10
  }
9
11
  declare const loader: LoaderDefinitionFunction<LoaderOptions>;
10
12
  export default loader;
package/dist/loader.js CHANGED
@@ -12,10 +12,15 @@ const loader = function (content) {
12
12
  scriptKind,
13
13
  overwrite: true,
14
14
  });
15
- const jsxAttributes = sourceFile.getDescendantsOfKind(ts_morph_1.SyntaxKind.JsxAttribute);
16
- for (const attribute of jsxAttributes) {
17
- if (propsToRemove.includes(attribute.getName())) {
18
- attribute.remove();
15
+ let descendants = sourceFile.getDescendantsOfKind(ts_morph_1.SyntaxKind.JsxAttribute);
16
+ if (options.removeFromObjects) {
17
+ const objectProps = sourceFile.getDescendantsOfKind(ts_morph_1.SyntaxKind.PropertyAssignment);
18
+ descendants = descendants.concat(objectProps);
19
+ }
20
+ for (const descendant of descendants) {
21
+ const descendantName = descendant.getName().replace(/['"]+/g, "");
22
+ if (propsToRemove.includes(descendantName)) {
23
+ descendant.remove();
19
24
  }
20
25
  }
21
26
  sourceFile.saveSync();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-remove-props-loader",
3
- "version": "1.0.4",
3
+ "version": "1.1.3",
4
4
  "description": "Webpack loader for removing React props or JSX attributes in TypeScript/JavaScript code.",
5
5
  "main": "dist/index",
6
6
  "repository": "https://github.com/Mercateo/react-remove-props-loader.git",
@@ -18,16 +18,16 @@
18
18
  "prepublishOnly": "yarn build"
19
19
  },
20
20
  "dependencies": {
21
- "ts-morph": "^11.0.3"
21
+ "ts-morph": "^13.0.2"
22
22
  },
23
23
  "devDependencies": {
24
- "@types/jest": "^26.0.24",
25
- "jest": "^27.0.6",
26
- "ts-jest": "^27.0.3",
27
- "typescript": "^4.3.5",
28
- "webpack": "^5.44.0"
24
+ "@types/jest": "^27.0.3",
25
+ "jest": "^27.4.4",
26
+ "ts-jest": "^27.1.1",
27
+ "typescript": "^4.5.3",
28
+ "webpack": "^5.65.0"
29
29
  },
30
30
  "peerDependencies": {
31
- "webpack": ">5.0.0"
31
+ "webpack": ">=5.0.0"
32
32
  }
33
33
  }