From 3970353edca691c0995eab8c2fd0ab9241f52777 Mon Sep 17 00:00:00 2001 From: "Yang Wooseong (Andrew)" Date: Tue, 12 Mar 2024 17:49:25 +0900 Subject: [PATCH] Fix `hasNamedImportInImportDeclaration` util to check all named imports (#2060) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Self Checklist - [x] I wrote a PR title in **English** and added an appropriate **label** to the PR. - [x] I wrote the commit message in **English** and to follow [**the Conventional Commits specification**](https://www.conventionalcommits.org/en/v1.0.0/). - [x] I [added the **changeset**](https://github.com/changesets/changesets/blob/main/docs/adding-a-changeset.md) about the changes that needed to be released. (or didn't have to) - [x] I wrote or updated **documentation** related to the changes. (or didn't have to) - [x] I wrote or updated **tests** related to the changes. (or didn't have to) - [x] I tested the changes in various browsers. (or didn't have to) - Windows: Chrome, Edge, (Optional) Firefox - macOS: Chrome, Edge, Safari, (Optional) Firefox ## Related Issue - None ## Summary - `hasNamedImportInImportDeclaration` 함수는 특정 import 문에서 named import 가 있는지 확인하는 함수입니다. 그런데 가장 첫번째로 나오는 import 문만 확인하고 있어서 아래처럼 import 문이 2번 나오는 경우에는 결과가 잘못 나오는 버그가 있어서 이를 수정합니다. ```tsx import { type IconProps } from '@channel.io/bezier-react' import { OverlayPosition } from '@channel.io/bezier-react' console.log(hasNamedImportInImportDeclaration(sourceFile, 'OverlayPosition', '@channel.io/bezier-react') // false, but should be true ``` ### Breaking change? (Yes/No) - No ## References - None --- .changeset/perfect-parents-share.md | 5 +++++ packages/bezier-codemod/src/utils/import.ts | 8 ++++---- 2 files changed, 9 insertions(+), 4 deletions(-) create mode 100644 .changeset/perfect-parents-share.md diff --git a/.changeset/perfect-parents-share.md b/.changeset/perfect-parents-share.md new file mode 100644 index 0000000000..6940d3dba9 --- /dev/null +++ b/.changeset/perfect-parents-share.md @@ -0,0 +1,5 @@ +--- +"@channel.io/bezier-codemod": patch +--- + +Fix `hasNamedImportInImportDeclaration` util to check all named imports diff --git a/packages/bezier-codemod/src/utils/import.ts b/packages/bezier-codemod/src/utils/import.ts index 5baf10d103..33efe21111 100644 --- a/packages/bezier-codemod/src/utils/import.ts +++ b/packages/bezier-codemod/src/utils/import.ts @@ -14,10 +14,10 @@ export const getImportDeclarations = (sourceFile: SourceFile, specifier: string) .filter((declaration) => declaration.getModuleSpecifier().getLiteralValue() === specifier) export const hasNamedImportInImportDeclaration = (sourceFile: SourceFile, namedImport: string, moduleName: string) => { - const importDeclaration = getImportDeclaration(sourceFile, moduleName) - return importDeclaration - ?.getNamedImports() - .map((node) => node.getText()) + const importDeclarations = getImportDeclarations(sourceFile, moduleName) + return importDeclarations + .flatMap(v => v?.getNamedImports()) + .map(v => v.getText()) .includes(namedImport) }