-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Preserve import comments when transforming code (#514)
- Loading branch information
Showing
25 changed files
with
156 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
6 changes: 6 additions & 0 deletions
6
src/transforms/v2-to-v3/__fixtures__/import-comments/global-import-equals.block.input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
10 changes: 10 additions & 0 deletions
10
src/transforms/v2-to-v3/__fixtures__/import-comments/global-import-equals.block.output.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
4 changes: 4 additions & 0 deletions
4
src/transforms/v2-to-v3/__fixtures__/import-comments/global-import-equals.inline.input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
8 changes: 8 additions & 0 deletions
8
src/transforms/v2-to-v3/__fixtures__/import-comments/global-import-equals.inline.output.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
6 changes: 6 additions & 0 deletions
6
src/transforms/v2-to-v3/__fixtures__/import-comments/global-import.block.input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
6 changes: 6 additions & 0 deletions
6
src/transforms/v2-to-v3/__fixtures__/import-comments/global-import.block.output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
4 changes: 4 additions & 0 deletions
4
src/transforms/v2-to-v3/__fixtures__/import-comments/global-import.inline.input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
4 changes: 4 additions & 0 deletions
4
src/transforms/v2-to-v3/__fixtures__/import-comments/global-import.inline.output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
6 changes: 6 additions & 0 deletions
6
src/transforms/v2-to-v3/__fixtures__/import-comments/global-require.block.input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
8 changes: 8 additions & 0 deletions
8
src/transforms/v2-to-v3/__fixtures__/import-comments/global-require.block.output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
4 changes: 4 additions & 0 deletions
4
src/transforms/v2-to-v3/__fixtures__/import-comments/global-require.inline.input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
6 changes: 6 additions & 0 deletions
6
src/transforms/v2-to-v3/__fixtures__/import-comments/global-require.inline.output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
6 changes: 6 additions & 0 deletions
6
src/transforms/v2-to-v3/__fixtures__/import-comments/service-import.block.input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
6 changes: 6 additions & 0 deletions
6
src/transforms/v2-to-v3/__fixtures__/import-comments/service-import.block.output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
4 changes: 4 additions & 0 deletions
4
src/transforms/v2-to-v3/__fixtures__/import-comments/service-import.inline.input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
4 changes: 4 additions & 0 deletions
4
src/transforms/v2-to-v3/__fixtures__/import-comments/service-import.inline.output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
6 changes: 6 additions & 0 deletions
6
src/transforms/v2-to-v3/__fixtures__/import-comments/service-require.block.input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
8 changes: 8 additions & 0 deletions
8
src/transforms/v2-to-v3/__fixtures__/import-comments/service-require.block.output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
4 changes: 4 additions & 0 deletions
4
src/transforms/v2-to-v3/__fixtures__/import-comments/service-require.inline.input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
6 changes: 6 additions & 0 deletions
6
src/transforms/v2-to-v3/__fixtures__/import-comments/service-require.inline.output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
19
src/transforms/v2-to-v3/modules/removeImportDeclaration.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters