Skip to content

Commit

Permalink
Preserve import comments when transforming code (#514)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr authored Jul 26, 2023
1 parent c29b843 commit f4d2497
Show file tree
Hide file tree
Showing 25 changed files with 156 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/dirty-owls-turn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"aws-sdk-js-codemod": patch
---

Preserve import comments when transforming code
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/*
* Example comment which should not be removed.
*/
import AWS = require("aws-sdk");

const client = new AWS.DynamoDB();
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* Example comment which should not be removed.
*/
import AWS_DynamoDB = require("@aws-sdk/client-dynamodb");

const {
DynamoDB
} = AWS_DynamoDB;

const client = new DynamoDB();
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Example comment which should not be removed.
import AWS = require("aws-sdk");

const client = new AWS.DynamoDB();
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Example comment which should not be removed.
import AWS_DynamoDB = require("@aws-sdk/client-dynamodb");

const {
DynamoDB
} = AWS_DynamoDB;

const client = new DynamoDB();
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/*
* Example comment which should not be removed.
*/
import AWS from "aws-sdk";

const client = new AWS.DynamoDB();
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/*
* Example comment which should not be removed.
*/
import { DynamoDB } from "@aws-sdk/client-dynamodb";

const client = new DynamoDB();
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Example comment which should not be removed.
import AWS from "aws-sdk";

const client = new AWS.DynamoDB();
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Example comment which should not be removed.
import { DynamoDB } from "@aws-sdk/client-dynamodb";

const client = new DynamoDB();
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/*
* Example comment which should not be removed.
*/
const AWS = require("aws-sdk");

const client = new AWS.DynamoDB();
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* Example comment which should not be removed.
*/
const {
DynamoDB
} = require("@aws-sdk/client-dynamodb");

const client = new DynamoDB();
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Example comment which should not be removed.
const AWS = require("aws-sdk");

const client = new AWS.DynamoDB();
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Example comment which should not be removed.
const {
DynamoDB
} = require("@aws-sdk/client-dynamodb");

const client = new DynamoDB();
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/*
* Example comment which should not be removed.
*/
import { DynamoDB } from "aws-sdk";

const client = new DynamoDB();
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/*
* Example comment which should not be removed.
*/
import { DynamoDB } from "@aws-sdk/client-dynamodb";

const client = new DynamoDB();
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Example comment which should not be removed.
import { DynamoDB } from "aws-sdk";

const client = new DynamoDB();
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Example comment which should not be removed.
import { DynamoDB } from "@aws-sdk/client-dynamodb";

const client = new DynamoDB();
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/*
* Example comment which should not be removed.
*/
const { DynamoDB } = require("aws-sdk");

const client = new DynamoDB();
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* Example comment which should not be removed.
*/
const {
DynamoDB
} = require("@aws-sdk/client-dynamodb");

const client = new DynamoDB();
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Example comment which should not be removed.
const { DynamoDB } = require("aws-sdk");

const client = new DynamoDB();
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Example comment which should not be removed.
const {
DynamoDB
} = require("@aws-sdk/client-dynamodb");

const client = new DynamoDB();
19 changes: 19 additions & 0 deletions src/transforms/v2-to-v3/modules/removeImportDeclaration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { ASTPath, ImportDeclaration, JSCodeshift } from "jscodeshift";

/**
* Removes import declaration, but preserves comments by adding them to next sibling.
*/
export const removeImportDeclaration = (
j: JSCodeshift,
declarationPath: ASTPath<ImportDeclaration>
) => {
const { comments } = declarationPath.value;
if (comments?.length) {
const siblings = declarationPath.parent?.value.body;
if (siblings?.length) {
const nextSibling = siblings[siblings.indexOf(declarationPath.value) + 1];
nextSibling.comments = [...comments, ...(nextSibling.comments || [])];
}
}
j(declarationPath).remove();
};
3 changes: 2 additions & 1 deletion src/transforms/v2-to-v3/modules/removeImportDefault.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Collection, JSCodeshift } from "jscodeshift";
import { removeImportDeclaration } from "./removeImportDeclaration";

export interface RemoveImportDefaultOptions {
localName: string;
Expand Down Expand Up @@ -26,7 +27,7 @@ export const removeImportDefault = (

// Remove ImportDeclaration if there are no import specifiers.
if (declarationPath.value.specifiers?.length === 0) {
j(declarationPath).remove();
removeImportDeclaration(j, declarationPath);
}
});
};
22 changes: 12 additions & 10 deletions src/transforms/v2-to-v3/modules/removeImportEquals.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Collection, JSCodeshift } from "jscodeshift";
import { removeImportDeclaration } from "./removeImportDeclaration";

export interface RemoveImportEqualsOptions {
localName: string;
Expand All @@ -10,14 +11,15 @@ export const removeImportEquals = (
source: Collection<unknown>,
{ localName, sourceValue }: RemoveImportEqualsOptions
) => {
source
.find(j.TSImportEqualsDeclaration, {
type: "TSImportEqualsDeclaration",
id: { name: localName },
moduleReference: {
type: "TSExternalModuleReference",
expression: { type: "StringLiteral", value: sourceValue },
},
})
.remove();
const importEqualsDeclaration = source.find(j.TSImportEqualsDeclaration, {
type: "TSImportEqualsDeclaration",
id: { name: localName },
moduleReference: {
type: "TSExternalModuleReference",
expression: { type: "StringLiteral", value: sourceValue },
},
});
if (importEqualsDeclaration.length) {
removeImportDeclaration(j, importEqualsDeclaration.get());
}
};
3 changes: 2 additions & 1 deletion src/transforms/v2-to-v3/modules/removeImportNamed.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Collection, JSCodeshift } from "jscodeshift";
import { removeImportDeclaration } from "./removeImportDeclaration";

export interface RemoveImportNamedOptions {
importedName?: string;
Expand Down Expand Up @@ -36,7 +37,7 @@ export const removeImportNamed = (

// Remove ImportDeclaration if there are no import specifiers.
if (declarationPath.value.specifiers?.length === 0) {
j(declarationPath).remove();
removeImportDeclaration(j, declarationPath);
}
});
};

0 comments on commit f4d2497

Please sign in to comment.