Skip to content

Commit

Permalink
feat: add typescript support
Browse files Browse the repository at this point in the history
  • Loading branch information
mkrds committed Nov 4, 2021
1 parent 7799474 commit efe9ec0
Show file tree
Hide file tree
Showing 21 changed files with 213 additions and 119 deletions.
10 changes: 10 additions & 0 deletions bin/__tests__/carbon-codemod.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ describe("run", () => {
const args = [
"--verbose=2",
"--ignore-pattern=**/node_modules/**",
"--parser=tsx",
"--extensions=tsx,ts,jsx,js",
"--transform",
path.join(
Cli.__transformsDir,
Expand All @@ -190,6 +192,8 @@ describe("run", () => {
const args = [
"--verbose=2",
"--ignore-pattern=**/node_modules/**",
"--parser=tsx",
"--extensions=tsx,ts,jsx,js",
"--transform",
path.join(
Cli.__transformsDir,
Expand All @@ -215,6 +219,8 @@ describe("run", () => {
const args = [
"--verbose=2",
"--ignore-pattern=**/node_modules/**",
"--parser=tsx",
"--extensions=tsx,ts,jsx,js",
"--transform",
path.join(
Cli.__transformsDir,
Expand Down Expand Up @@ -243,6 +249,8 @@ describe("run", () => {
const args = [
"--verbose=2",
"--ignore-pattern=**/node_modules/**",
"--parser=tsx",
"--extensions=tsx,ts,jsx,js",
"--transform",
path.join(Cli.__transformsDir, "rename-prop", "rename-prop.js"),
path.join(process.cwd(), "src"),
Expand All @@ -269,6 +277,8 @@ describe("run", () => {
const args = [
"--verbose=2",
"--ignore-pattern=**/node_modules/**",
"--parser=tsx",
"--extensions=tsx,ts,jsx,js",
"--transform",
path.join(Cli.__transformsDir, "remove-prop", "remove-prop.js"),
path.join(process.cwd(), "src"),
Expand Down
22 changes: 13 additions & 9 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,20 @@ const runTransform = (target, command, program, options = {}) => {
if (dry) {
args.push("--dry");
}

args.push("--parser=tsx");

args.push("--extensions=tsx,ts,jsx,js");

args.push("--transform", path.join(transformsDir, name, `${name}.js`));

args.push(path.resolve(process.cwd(), target));

Object.keys(options).forEach((key) => {
const type = typeof options[key];

if (type === "string") {
args.push(`--${key}=${options[key]}`);
} else {
} else if (type !== "undefined") {
console.error(
`Unable to use argument ${key}, ${type} arguments are not supported yet.`
);
Expand Down Expand Up @@ -257,19 +261,19 @@ Example
});

program
.command(
"replace-collapsible-pod-with-accordion <target>"
.command("replace-collapsible-pod-with-accordion <target>")
.description(
"replaces deprecated collapse Pod prop with the Accordion Component"
)
.description("replaces deprecated collapse Pod prop with the Accordion Component")
.action((target, command) => runTransform(target, command, program));

program
.command(
"move-pod-description-to-content <target>"
.command("move-pod-description-to-content <target>")
.description(
"removes deprecated description Pod prop and places it's value as part of the Pod content"
)
.description("removes deprecated description Pod prop and places it's value as part of the Pod content")
.action((target, command) => runTransform(target, command, program));

program.on("command:*", function () {
console.error(
"Invalid command: %s\nSee --help for a list of available commands.",
Expand Down
13 changes: 10 additions & 3 deletions defineTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,21 @@ export default function defineTest(
transformName,
options,
testFilePrefix,
testOptions
testOptions = {}
) {
testFilePrefix = Array.isArray(testFilePrefix)
? path.join(...testFilePrefix)
: testFilePrefix;
const fixtureDir = path.join(dirName, "..", "__testfixtures__");
const inputPath = path.join(fixtureDir, testFilePrefix + `.input.js`);
const outputPath = path.join(fixtureDir, testFilePrefix + `.output.js`);
const fileExtension = testOptions.parser || "js";
const inputPath = path.join(
fixtureDir,
testFilePrefix + `.input.` + fileExtension
);
const outputPath = path.join(
fixtureDir,
testFilePrefix + `.output.` + fileExtension
);
const source = fs.readFileSync(inputPath, "utf8");
const expectedOutput = fs.readFileSync(outputPath, "utf8");
const transform = require(path.join(dirName, "..", transformName));
Expand Down
4 changes: 0 additions & 4 deletions transforms/add-prop/__testfixtures__/Node.input.js

This file was deleted.

3 changes: 0 additions & 3 deletions transforms/add-prop/__testfixtures__/Object.input.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Button from "carbon-react/lib/components/button";

interface SomeInterface {
label: string;
}

export const asFalse = (arg: string): JSX.Element => <Button />
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Button from "carbon-react/lib/components/button";

interface SomeInterface {
label: string;
}

export const asFalse = (arg: string): JSX.Element => <Button hasBorder={false} />
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Button from "carbon-react/lib/components/button";

interface SomeInterface {
label: string;
}

export const asTrue = (arg: string): JSX.Element => <Button />
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Button from "carbon-react/lib/components/button";

interface SomeInterface {
label: string;
}

export const asTrue = (arg: string): JSX.Element => <Button hasBorder />
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Button } from "carbon-react/lib/components/button";

interface SomeInterface {
label: string;
}

export const asString = (arg: string): JSX.Element => <Button />
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Button } from "carbon-react/lib/components/button";

interface SomeInterface {
label: string;
}

export const asString = (arg: string): JSX.Element => <Button import="named" />
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Wrong Import
import Tile from "carbon-react/lib/components/NotTile";

interface SomeInterface {
label: string;
}

export default (arg: string): JSX.Element => <Tile />;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Button from "carbon-react/lib/components/button";

interface SomeInterface {
label: string;
}

export const App = (arg: string): JSX.Element => <Button ml="2" />
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Button from "carbon-react/lib/components/button";

interface SomeInterface {
label: string;
}

export const asNumber = (arg: string): JSX.Element => <Button />
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Button from "carbon-react/lib/components/button";

interface SomeInterface {
label: string;
}

export const asNumber = (arg: string): JSX.Element => <Button ml={2} />
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Button from "carbon-react/lib/components/button";

interface SomeInterface {
label: string;
}

export const asString = (arg: string): JSX.Element => <Button />
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Button from "carbon-react/lib/components/button";

interface SomeInterface {
label: string;
}

export const asString = (arg: string): JSX.Element => <Button ml="16px" />
Loading

0 comments on commit efe9ec0

Please sign in to comment.