Skip to content

Commit

Permalink
feat: add prop codemod
Browse files Browse the repository at this point in the history
  • Loading branch information
DipperTheDan committed Jan 19, 2021
1 parent a038dba commit 3f4b1ef
Show file tree
Hide file tree
Showing 25 changed files with 172 additions and 167 deletions.
12 changes: 7 additions & 5 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,6 @@ function Cli() {
.description("Wrap children of DialogFullScreen in AppWrapper")
.action((target, command) => runTransform(target, command, program));

program
.command("add-button-margin-prop <target>")
.description("adds margin to button when there is more than one")
.action((target, command) => runTransform(target, command, program));

program
.command("message-remove-classic-theme <target>")
.description("Remove classic theme props from the message component")
Expand All @@ -128,6 +123,13 @@ function Cli() {
)
.action((target, command) => runTransform(target, command, program));

program
.command("add-prop <target> <component> <prop> <value>")
.description("adds a new prop and value to the specified component")
.action((target, component, prop, value, command) =>
runTransform(target, command, program, { component, prop, value })
);

program
.command("rename-prop <target> <component> <old> <replacement>")
.description(
Expand Down
23 changes: 0 additions & 23 deletions transforms/add-button-margin-prop/README.md

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

84 changes: 0 additions & 84 deletions transforms/add-button-margin-prop/add-button-margin-prop.js

This file was deleted.

58 changes: 50 additions & 8 deletions transforms/add-prop/README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,58 @@
# add-attribute
# add-prop

In order to accomodate the `styled-system` spacing api being added to Carbon components, the `padding` prop on `Tile` has been replaced with `p`.
This universal codemod provides possibility to add any prop to any component.

Currently `padding` accepts the values `XS`, `S`, `M`, `L` and `XL` which map to paddings of `8px` up to `40px`. This codemod will replace these with the `styled-system` equivalent. I.e. for `padding="XS"` this will be `p={1}`, both of which map to `8px`.
```diff
- <Component />
+ <Component newProp="info" />

```
## Usage

`npx carbon-codemod add-prop <target> <component-import-path> <prop> <value>`

### Examples

### String
`npx carbon-codemod add-prop src carbon-react/lib/components/button ml "16px"`

```js
import Button from "carbon-react/lib/components/button";
```
```diff
- <Button>Button</Button>
+ <Button ml="16px">Button</Button>
```

### Number
`npx carbon-codemod add-prop src carbon-react/lib/components/button ml 2`

```js
import Button from "carbon-react/lib/components/button";
```
```diff
- <Tile padding="XS">Content</Tile>
+ <Tile p={1}>Content</Tile>
- <Button>Button</Button>
+ <Button ml={2}>Button</Button>
```

If there is a pattern that you use that is not transformed, please file a feature request.
### Boolean - True
`npx carbon-codemod add-prop src carbon-react/lib/components/button hasBorder true`

## Usage
```js
import Button from "carbon-react/lib/components/button";
```
```diff
- <Button>Button</Button>
+ <Button hasBorder>Button</Button>
```

### Boolean - False
`npx carbon-codemod add-prop src carbon-react/lib/components/button hasBorder true`

`npx carbon-codemod tile-update-padding-prop <target>`
```js
import Button from "carbon-react/lib/components/button";
```
```diff
- <Button>Button</Button>
+ <Button hasBorder={false}>Button</Button>
```
3 changes: 3 additions & 0 deletions transforms/add-prop/__testfixtures__/BooleanFalse.input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Button from "carbon-react/lib/components/button";

export const asFalse = () => <Button />
3 changes: 3 additions & 0 deletions transforms/add-prop/__testfixtures__/BooleanFalse.output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Button from "carbon-react/lib/components/button";

export const asFalse = () => <Button hasBorder={false} />
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import Button from "carbon-react/lib/components/button";

export const App = () => <Button />
export const asTrue = () => <Button />
3 changes: 3 additions & 0 deletions transforms/add-prop/__testfixtures__/BooleanTrue.output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Button from "carbon-react/lib/components/button";

export const asTrue = () => <Button hasBorder />
2 changes: 0 additions & 2 deletions transforms/add-prop/__testfixtures__/Bug.input.js

This file was deleted.

2 changes: 0 additions & 2 deletions transforms/add-prop/__testfixtures__/Bug.output.js

This file was deleted.

Empty file.
4 changes: 4 additions & 0 deletions transforms/add-prop/__testfixtures__/Number.input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import Button from "carbon-react/lib/components/button";

export const asNumber = () => <Button />

3 changes: 3 additions & 0 deletions transforms/add-prop/__testfixtures__/Number.output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Button from "carbon-react/lib/components/button";

export const asNumber = () => <Button ml={2} />
3 changes: 3 additions & 0 deletions transforms/add-prop/__testfixtures__/String.input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Button from "carbon-react/lib/components/button";

export const asString = () => <Button />
3 changes: 3 additions & 0 deletions transforms/add-prop/__testfixtures__/String.output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Button from "carbon-react/lib/components/button";

export const asString = () => <Button ml="16px" />
70 changes: 65 additions & 5 deletions transforms/add-prop/__tests__/add-prop.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,67 @@
import defineTest from "../../../defineTest";

defineTest(__dirname, "add-prop", {
component: "carbon-react/lib/components/button",
prop: "ml",
value: "2",
}, "Basic");
defineTest(
__dirname,
"add-prop",
{
component: "carbon-react/lib/components/button",
prop: "ml",
value: 2,
},
"Number"
);

defineTest(
__dirname,
"add-prop",
{
component: "carbon-react/lib/components/button",
prop: "ml",
value: "16px",
},
"String"
);

defineTest(
__dirname,
"add-prop",
{
component: "carbon-react/lib/components/button",
prop: "hasBorder",
value: true,
},
"BooleanTrue"
);

defineTest(
__dirname,
"add-prop",
{
component: "carbon-react/lib/components/button",
prop: "hasBorder",
value: false,
},
"BooleanFalse"
);

defineTest(
__dirname,
"add-prop",
{
component: "carbon-react/lib/components/component",
prop: "newProp",
value: "value",
},
"NoImport"
);

defineTest(
__dirname,
"add-prop",
{
component: "carbon-react/lib/components/button",
prop: "ml",
value: "2",
},
"NoTransformRequired"
);
Loading

0 comments on commit 3f4b1ef

Please sign in to comment.