-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bfe4df7
commit 98faaac
Showing
17 changed files
with
231 additions
and
33 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
packages/codemods/src/transforms/v7/__testfixtures__/card-scroll/basic.input.tsx
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,59 @@ | ||
import { Card, CardScroll } from '@vkontakte/vkui'; | ||
import React from 'react'; | ||
|
||
const App = () => { | ||
return ( | ||
<React.Fragment> | ||
{/* noSpaces by default -> padding=true */} | ||
<CardScroll | ||
size="s" | ||
> | ||
<Card> | ||
<div style={{ paddingBottom: '66%' }} /> | ||
</Card> | ||
<Card> | ||
<div style={{ paddingBottom: '66%' }} /> | ||
</Card> | ||
</CardScroll> | ||
|
||
{/* noSpaces="false" -> padding="true" */} | ||
<CardScroll | ||
size="s" | ||
noSpaces={false} | ||
> | ||
<Card> | ||
<div style={{ paddingBottom: '66%' }} /> | ||
</Card> | ||
<Card> | ||
<div style={{ paddingBottom: '66%' }} /> | ||
</Card> | ||
</CardScroll> | ||
|
||
{/* noSpaces="true" -> padding="false" */} | ||
<CardScroll | ||
size="s" | ||
noSpaces={true} | ||
> | ||
<Card> | ||
<div style={{ paddingBottom: '66%' }} /> | ||
</Card> | ||
<Card> | ||
<div style={{ paddingBottom: '66%' }} /> | ||
</Card> | ||
</CardScroll> | ||
|
||
{/* noSpaces="true" -> padding="false" */} | ||
<CardScroll | ||
size="s" | ||
noSpaces | ||
> | ||
<Card> | ||
<div style={{ paddingBottom: '66%' }} /> | ||
</Card> | ||
<Card> | ||
<div style={{ paddingBottom: '66%' }} /> | ||
</Card> | ||
</CardScroll> | ||
</React.Fragment> | ||
); | ||
}; |
52 changes: 52 additions & 0 deletions
52
packages/codemods/src/transforms/v7/__tests__/__snapshots__/card-scroll.ts.snap
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,52 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`card-scroll transforms correctly 1`] = ` | ||
"import { Card, CardScroll } from '@vkontakte/vkui'; | ||
import React from 'react'; | ||
const App = () => { | ||
return ( | ||
(<React.Fragment> | ||
{/* noSpaces by default -> padding=true */} | ||
<CardScroll size="s" padding> | ||
<Card> | ||
<div style={{ paddingBottom: '66%' }} /> | ||
</Card> | ||
<Card> | ||
<div style={{ paddingBottom: '66%' }} /> | ||
</Card> | ||
</CardScroll> | ||
{/* noSpaces="false" -> padding="true" */} | ||
<CardScroll | ||
size="s" | ||
padding | ||
> | ||
<Card> | ||
<div style={{ paddingBottom: '66%' }} /> | ||
</Card> | ||
<Card> | ||
<div style={{ paddingBottom: '66%' }} /> | ||
</Card> | ||
</CardScroll> | ||
{/* noSpaces="true" -> padding="false" */} | ||
<CardScroll size="s"> | ||
<Card> | ||
<div style={{ paddingBottom: '66%' }} /> | ||
</Card> | ||
<Card> | ||
<div style={{ paddingBottom: '66%' }} /> | ||
</Card> | ||
</CardScroll> | ||
{/* noSpaces="true" -> padding="false" */} | ||
<CardScroll size="s"> | ||
<Card> | ||
<div style={{ paddingBottom: '66%' }} /> | ||
</Card> | ||
<Card> | ||
<div style={{ paddingBottom: '66%' }} /> | ||
</Card> | ||
</CardScroll> | ||
</React.Fragment>) | ||
); | ||
};" | ||
`; |
12 changes: 12 additions & 0 deletions
12
packages/codemods/src/transforms/v7/__tests__/card-scroll.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,12 @@ | ||
jest.autoMockOff(); | ||
|
||
import { defineSnapshotTestFromFixture } from '../../../testHelpers/testHelper'; | ||
|
||
const name = 'card-scroll'; | ||
const fixtures = ['basic'] as const; | ||
|
||
describe(name, () => { | ||
fixtures.forEach((test) => | ||
defineSnapshotTestFromFixture(__dirname, name, global.TRANSFORM_OPTIONS, `${name}/${test}`), | ||
); | ||
}); |
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,70 @@ | ||
import { API, FileInfo, JSXAttribute, JSXSpreadAttribute } from 'jscodeshift'; | ||
import { getImportInfo, removeAttribute } from '../../codemod-helpers'; | ||
import { report } from '../../report'; | ||
import { JSCodeShiftOptions } from '../../types'; | ||
|
||
export const parser = 'tsx'; | ||
|
||
export default function transformer(file: FileInfo, api: API, options: JSCodeShiftOptions) { | ||
const { alias } = options; | ||
const j = api.jscodeshift; | ||
const source = j(file.source); | ||
const { localName } = getImportInfo(j, file, 'CardScroll', alias); | ||
|
||
if (!localName) { | ||
return source.toSource(); | ||
} | ||
|
||
const getValueFromAttribute = (attribute: JSXAttribute): boolean | null => { | ||
if (attribute.value?.type === 'BooleanLiteral') { | ||
return attribute.value.value; | ||
} | ||
if (attribute.value?.type === 'JSXExpressionContainer') { | ||
const expression = attribute.value.expression; | ||
if (expression.type === 'BooleanLiteral') { | ||
return expression.value; | ||
} | ||
return null; | ||
} | ||
return true; | ||
}; | ||
|
||
const addPropPadding = (attributes: Array<JSXAttribute | JSXSpreadAttribute> | undefined) => { | ||
attributes?.push(j.jsxAttribute(j.jsxIdentifier('padding'))); | ||
}; | ||
|
||
source | ||
.find(j.JSXElement, { | ||
openingElement: { | ||
name: { | ||
name: localName, | ||
}, | ||
}, | ||
}) | ||
.forEach((path) => { | ||
const attributes = path.node.openingElement.attributes; | ||
const noSpacesAttr: JSXAttribute | undefined = attributes?.find( | ||
(attr) => attr.type === 'JSXAttribute' && attr.name.name === 'noSpaces', | ||
) as JSXAttribute | undefined; | ||
|
||
if (!noSpacesAttr) { | ||
addPropPadding(attributes); | ||
return; | ||
} | ||
const attrValue = getValueFromAttribute(noSpacesAttr); | ||
if (attrValue === null) { | ||
report( | ||
api, | ||
`Manual changes required for ${localName}'s "noSpaces" prop. Need to change it to "padding" prop`, | ||
); | ||
return; | ||
} | ||
removeAttribute(attributes, noSpacesAttr); | ||
|
||
if (!attrValue) { | ||
addPropPadding(attributes); | ||
} | ||
}); | ||
|
||
return source.toSource(); | ||
} |
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
4 changes: 2 additions & 2 deletions
4
...ents/CardScroll/__image_snapshots__/cardscroll-android-chromium-dark-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions
4
...nts/CardScroll/__image_snapshots__/cardscroll-android-chromium-light-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions
4
...components/CardScroll/__image_snapshots__/cardscroll-ios-webkit-dark-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions
4
...omponents/CardScroll/__image_snapshots__/cardscroll-ios-webkit-light-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions
4
...onents/CardScroll/__image_snapshots__/cardscroll-vkcom-chromium-dark-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions
4
...nents/CardScroll/__image_snapshots__/cardscroll-vkcom-chromium-light-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions
4
...ponents/CardScroll/__image_snapshots__/cardscroll-vkcom-firefox-dark-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions
4
...onents/CardScroll/__image_snapshots__/cardscroll-vkcom-firefox-light-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions
4
...mponents/CardScroll/__image_snapshots__/cardscroll-vkcom-webkit-dark-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions
4
...ponents/CardScroll/__image_snapshots__/cardscroll-vkcom-webkit-light-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.