Skip to content

Commit

Permalink
chore: automatically set parser to ts for TypeScript files (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr authored Dec 20, 2022
1 parent d96cc97 commit 8ab815a
Show file tree
Hide file tree
Showing 50 changed files with 35 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/strange-hairs-look.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"aws-sdk-js-codemod": patch
---

Automatically set parser to ts for TypeScript files
28 changes: 13 additions & 15 deletions src/transforms/v2-to-v3/transformer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,24 @@ import { join } from "path";

import transformer from "./transformer";

const inputFileRegex = /(.*).input.[jt]sx?$/;

describe("v2-to-v3", () => {
const fixtureDir = join(__dirname, "__fixtures__");
const testFilePrefixes = readdirSync(fixtureDir)
.filter((fileName) => fileName.endsWith(".input.ts"))
.map((fileName) => fileName.replace(".input.ts", ""));
const testFiles: [string, string][] = readdirSync(fixtureDir)
.filter((fileName) => inputFileRegex.test(fileName))
.map((fileName) => [
(fileName.match(inputFileRegex) as RegExpMatchArray)[1],
fileName.split(".").pop() as string,
]);

describe.each(testFilePrefixes)(`transforms correctly using "%s" data`, (testFilePrefix) => {
const inputPath = join(fixtureDir, testFilePrefix + `.input.ts`);
const outputPath = join(fixtureDir, testFilePrefix + `.output.ts`);
it.each(testFiles)(`transforms correctly using "%s" data`, (filePrefix, fileExtension) => {
const inputPath = join(fixtureDir, [filePrefix, "input", fileExtension].join("."));
const outputPath = join(fixtureDir, [filePrefix, "output", fileExtension].join("."));
const inputCode = readFileSync(inputPath, "utf8");
const outputCode = readFileSync(outputPath, "utf8");
const input = { path: inputPath, source: inputCode };

// Some tests are tsonly as they fail with babel parser
// Refs: https://github.com/facebook/jscodeshift/issues/488
it.each([{ parser: "ts" }, ...(testFilePrefix.startsWith("tsonly-") ? [] : [{}])])(
"with testOptions: %o",
(testOptions) => {
runInlineTest(transformer, null, input, outputCode, testOptions);
}
);
const input = { path: inputPath, source: inputCode };
runInlineTest(transformer, null, input, outputCode);
});
});
3 changes: 2 additions & 1 deletion src/transforms/v2-to-v3/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
getV2ClientNames,
getV2DefaultModuleName,
getV2ServiceModuleNames,
isTypeScriptFile,
removeDefaultModuleIfNotUsed,
removePromiseCalls,
removeV2ClientModule,
Expand All @@ -14,7 +15,7 @@ import {
} from "./utils";

export default function transformer(file: FileInfo, api: API) {
const j = api.jscodeshift;
const j = isTypeScriptFile(file.path) ? api.jscodeshift.withParser("ts") : api.jscodeshift;
const source = j(file.source);

const v2DefaultModuleName = getV2DefaultModuleName(j, source) as string;
Expand Down
1 change: 1 addition & 0 deletions src/transforms/v2-to-v3/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export * from "./getClientMetadata";
export * from "./getV2ClientNames";
export * from "./getV2DefaultModuleName";
export * from "./getV2ServiceModuleNames";
export * from "./isTypeScriptFile";
export * from "./removeDefaultModuleIfNotUsed";
export * from "./removePromiseCalls";
export * from "./removeV2ClientModule";
Expand Down
12 changes: 12 additions & 0 deletions src/transforms/v2-to-v3/utils/isTypeScriptFile.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { isTypeScriptFile } from "./isTypeScriptFile";

describe(isTypeScriptFile.name, () => {
it.each([
[true, "foo.ts"],
[true, "foo.tsx"],
[false, "foo.js"],
[false, "foo.jsx"],
])("should return %b for %s", (output, input) => {
expect(isTypeScriptFile(input)).toBe(output);
});
});
2 changes: 2 additions & 0 deletions src/transforms/v2-to-v3/utils/isTypeScriptFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const isTypeScriptFile = (filePath: string): boolean =>
filePath.endsWith(".ts") || filePath.endsWith(".tsx");

0 comments on commit 8ab815a

Please sign in to comment.