-
Notifications
You must be signed in to change notification settings - Fork 323
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'vibe3' into vibe3-remove-tooltip-max-width-prop
- Loading branch information
Showing
125 changed files
with
751 additions
and
288 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
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 |
---|---|---|
@@ -1,8 +1,19 @@ | ||
import { Collection, JSXElement } from "jscodeshift"; | ||
import { ASTPath, Collection, JSCodeshift, JSXElement } from "jscodeshift"; | ||
|
||
/** | ||
* Finds all JSX elements that match a component name. | ||
*/ | ||
export function findComponentElements(root: Collection, componentName: string): Collection<JSXElement> { | ||
return root.find(JSXElement, { openingElement: { name: { name: componentName } } }); | ||
return root.findJSXElements(componentName); | ||
} | ||
|
||
/** | ||
* Updates the name of a JSX element. | ||
*/ | ||
export function updateComponentName(j: JSCodeshift, elementPath: ASTPath<JSXElement>, newName: string) { | ||
const elements = [elementPath.node.openingElement, elementPath.node.closingElement]; | ||
elements.forEach(element => { | ||
if (!element?.name) return; | ||
element.name = j.jsxIdentifier(newName); | ||
}); | ||
} |
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
176 changes: 176 additions & 0 deletions
176
...codemod/transformations/core/v2-to-v3/__tests__/search-component-import-migration.test.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,176 @@ | ||
import { defineInlineTest } from "jscodeshift/src/testUtils"; | ||
import transform from "../search-component-import-migration"; | ||
|
||
describe("SearchComponent component migration", () => { | ||
defineInlineTest( | ||
transform, | ||
{}, | ||
` | ||
import { SearchComponent } from "monday-ui-react-core"; | ||
<SearchComponent autoFocus placeholder="Search imported regular" /> | ||
`, | ||
` | ||
import { Search } from "monday-ui-react-core"; | ||
<Search autoFocus placeholder="Search imported regular" /> | ||
`, | ||
"should update import and update self-closing jsx accordingly" | ||
); | ||
|
||
defineInlineTest( | ||
transform, | ||
{}, | ||
` | ||
import { SearchComponent } from "monday-ui-react-core"; | ||
<SearchComponent autoFocus placeholder="Search imported regular"></SearchComponent> | ||
`, | ||
` | ||
import { Search } from "monday-ui-react-core"; | ||
<Search autoFocus placeholder="Search imported regular"></Search> | ||
`, | ||
"should update import and update jsx accordingly" | ||
); | ||
|
||
defineInlineTest( | ||
transform, | ||
{}, | ||
` | ||
import { SearchComponent } from "monday-ui-react-core"; | ||
<> | ||
<SearchComponent autoFocus placeholder="Search imported regular" /> | ||
<SearchComponent /> | ||
<SearchComponent debounceRate={300} /> | ||
</> | ||
`, | ||
` | ||
import { Search } from "monday-ui-react-core"; | ||
<> | ||
<Search autoFocus placeholder="Search imported regular" /> | ||
<Search /> | ||
<Search debounceRate={300} /> | ||
</> | ||
`, | ||
"should update import and update multiple jsx usages" | ||
); | ||
|
||
defineInlineTest( | ||
transform, | ||
{}, | ||
` | ||
import { SearchComponent as SC } from "monday-ui-react-core"; | ||
<SC autoFocus placeholder="Search as alias" /> | ||
`, | ||
` | ||
import { Search as SC } from "monday-ui-react-core"; | ||
<SC autoFocus placeholder="Search as alias" /> | ||
`, | ||
"should update import but not update jsx when component is imported with an alias" | ||
); | ||
|
||
defineInlineTest( | ||
transform, | ||
{}, | ||
` | ||
import { SearchComponent as SC } from "monday-ui-react-core"; | ||
<> | ||
<SC autoFocus placeholder="Search as alias" /> | ||
<SC /> | ||
<SC debounceRate={300} /> | ||
</> | ||
`, | ||
` | ||
import { Search as SC } from "monday-ui-react-core"; | ||
<> | ||
<SC autoFocus placeholder="Search as alias" /> | ||
<SC /> | ||
<SC debounceRate={300} /> | ||
</> | ||
`, | ||
"should update import but not update any jsx usage when component is imported with an alias" | ||
); | ||
|
||
defineInlineTest( | ||
transform, | ||
{}, | ||
` | ||
import { Search, SearchComponent } from "monday-ui-react-core"; | ||
<SearchComponent autoFocus placeholder="Search imported regular" /> | ||
`, | ||
` | ||
import { Search } from "monday-ui-react-core"; | ||
<SearchComponent autoFocus placeholder="Search imported regular" /> | ||
`, | ||
"should remove 'SearchComponent' when both 'Search' and 'SearchComponent' are imported from core" | ||
); | ||
|
||
defineInlineTest( | ||
transform, | ||
{}, | ||
` | ||
import { Search } from "other-component-library"; | ||
import { SearchComponent } from "monday-ui-react-core"; | ||
<SearchComponent autoFocus placeholder="Search imported regular" /> | ||
`, | ||
` | ||
import { Search } from "other-component-library"; | ||
import { Search as SearchComponent } from "monday-ui-react-core"; | ||
<SearchComponent autoFocus placeholder="Search imported regular" /> | ||
`, | ||
"should update 'SearchComponent' with alias when both 'Search' and 'SearchComponent' are imported, and 'Search' is not imported from core" | ||
); | ||
|
||
defineInlineTest( | ||
transform, | ||
{}, | ||
` | ||
import { OtherComponent } from "monday-ui-react-core"; | ||
<OtherComponent autoFocus placeholder="I'm not Search" /> | ||
`, | ||
` | ||
import { OtherComponent } from "monday-ui-react-core"; | ||
<OtherComponent autoFocus placeholder="I'm not Search" /> | ||
`, | ||
"should not change unrelated imports" | ||
); | ||
|
||
defineInlineTest( | ||
transform, | ||
{}, | ||
` | ||
import { OtherComponent as SearchComponent } from "monday-ui-react-core"; | ||
<SearchComponent autoFocus placeholder="I'm not Search" /> | ||
`, | ||
` | ||
import { OtherComponent as SearchComponent } from "monday-ui-react-core"; | ||
<SearchComponent autoFocus placeholder="I'm not Search" /> | ||
`, | ||
"should not change unrelated imports that are aliased as SearchComponent" | ||
); | ||
|
||
defineInlineTest( | ||
transform, | ||
{}, | ||
` | ||
import { Search as SearchComponent } from "monday-ui-react-core/next"; | ||
<SearchComponent autoFocus placeholder="I'm Search" /> | ||
`, | ||
` | ||
import { Search as SearchComponent } from "monday-ui-react-core/next"; | ||
<SearchComponent autoFocus placeholder="I'm Search" /> | ||
`, | ||
"should not change Search from core/next import that is aliased as SearchComponent" | ||
); | ||
|
||
defineInlineTest( | ||
transform, | ||
{}, | ||
` | ||
import { SearchComponent } from "monday-ui-react-core"; | ||
<SearchComponent autoFocus placeholder="I'm SearchComponent" size={SearchComponent.sizes.other.SMALL} /> | ||
`, | ||
` | ||
import { Search } from "monday-ui-react-core"; | ||
<Search autoFocus placeholder="I'm SearchComponent" size={Search.sizes.other.SMALL} /> | ||
`, | ||
"should change props that uses the namespace of 'SearchComponent' to 'Search'" | ||
); | ||
}); |
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
60 changes: 60 additions & 0 deletions
60
packages/codemod/transformations/core/v2-to-v3/search-component-import-migration.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,60 @@ | ||
import { ImportDeclaration } from "jscodeshift"; | ||
import { | ||
getCoreImportsForFile, | ||
wrap, | ||
getComponentNameOrAliasFromImports, | ||
updateImportSpecifierName, | ||
findComponentElements, | ||
findImportsThatIncludesSpecifier, | ||
removeSpecifierFromImport, | ||
updateComponentName, | ||
updateComponentNamespaceProps | ||
} from "../../../src/utils"; | ||
import { TransformationContext } from "../../../types"; | ||
|
||
/** | ||
* 1. Remove the 'SearchComponent' import if 'Search' is already imported from 'monday-ui-react-core' | ||
* 2. Update the 'SearchComponent' import to 'Search' (or 'Search as SearchComponent' if 'Search' is already imported from other source) | ||
* 3. Update the component name jsx usage from 'SearchComponent' to 'Search' | ||
*/ | ||
function transform({ j, root }: TransformationContext) { | ||
const coreImports = getCoreImportsForFile(root); | ||
if (!coreImports.length) return; | ||
|
||
const coreOldSearchComponentImports = findImportsThatIncludesSpecifier(j, coreImports, "SearchComponent"); | ||
if (!coreOldSearchComponentImports.length) return; | ||
|
||
const allImports = root.find(ImportDeclaration); | ||
|
||
// This finds all imports that has 'Search' as imported specifier | ||
const rootSearchImports = findImportsThatIncludesSpecifier(j, allImports, "Search"); | ||
|
||
// Check in addition if the import of 'Search' that was found is from Vibe | ||
const isSearchAlreadyImportedFromCore = rootSearchImports.some( | ||
path => path.node.source?.value === "monday-ui-react-core" | ||
); | ||
|
||
// Keep the original alias if it exists | ||
const componentName = getComponentNameOrAliasFromImports(j, coreOldSearchComponentImports.at(0), "SearchComponent"); | ||
const hasAlias = !!componentName && componentName !== "SearchComponent"; | ||
|
||
// if 'Search' specifier already exists in file (not from Vibe!) alias should be added to the new import | ||
const newAlias = rootSearchImports.length ? "SearchComponent" : ""; | ||
const alias = hasAlias ? componentName : newAlias; | ||
|
||
if (isSearchAlreadyImportedFromCore) { | ||
removeSpecifierFromImport(j, coreOldSearchComponentImports.at(0).get(), "SearchComponent"); | ||
} else { | ||
updateImportSpecifierName(j, coreOldSearchComponentImports.at(0).get(), "SearchComponent", "Search", alias); | ||
} | ||
|
||
const elements = findComponentElements(root, componentName || "SearchComponent"); | ||
if (!elements.length) return; | ||
|
||
elements.forEach(elementPath => { | ||
updateComponentName(j, elementPath, alias || "Search"); | ||
updateComponentNamespaceProps(j, elementPath, "SearchComponent", alias || "Search"); | ||
}); | ||
} | ||
|
||
export default wrap(transform); |
Oops, something went wrong.