-
Notifications
You must be signed in to change notification settings - Fork 43
[Migration]: box -> boxNew #3669
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
e0f8e98
:construction: box migration scaffolding
JulianNymark 1e4db81
Merge branch 'main' into darkside-migration-box
JulianNymark 0cd7ed0
Merge branch 'main' into darkside-migration-box
JulianNymark 59067ec
Merge branch 'main' into darkside-migration-box
JulianNymark fe7ff7d
:truck: boxnew -> darkside/boxnew
JulianNymark e94e708
:construction: wip
JulianNymark b81632e
:construction: working single substitution (pending lookup in replace…
JulianNymark 026edff
:white_check_mark: working test
JulianNymark c1753f4
import rename in output test
JulianNymark 88d9712
:white_check_mark: import rename + tests
JulianNymark db46476
use config comments, better import example
JulianNymark 88f249c
commentblock function (append to string output) + update tests (failing)
JulianNymark 9ecc6f3
:construction: almost good... needs to respect renamed imports
JulianNymark 80e59b9
:white_check_mark: passing tests
JulianNymark 0baee9d
:recycle: organize some utils
JulianNymark de8e38f
Merge branch 'main' into darkside-migration-box
JulianNymark ff30601
:test_tube: Added more tests
KenAJoh d54821d
Merge branch 'main' into darkside-migration-box
JulianNymark 4f7b7b2
cast JSXIdentifier type
JulianNymark 248f627
pass idempotency tests
JulianNymark 552e7fb
also refactor the usage when renaming a function
JulianNymark 1d119bd
Merge branch 'main' into darkside-migration-box
JulianNymark ceb7334
Update @navikt/aksel/src/codemod/migrations.ts
JulianNymark File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
193 changes: 193 additions & 0 deletions
193
@navikt/aksel/src/codemod/transforms/darkside/box-to-boxnew/box-to-boxnew.ts
JulianNymark marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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,193 @@ | ||
import type { API, FileInfo, JSCodeshift, JSXIdentifier } from "jscodeshift"; | ||
import { legacyTokenConfig } from "../../../../darkside/config/legacy.tokens"; | ||
import { | ||
findComponentImport, | ||
findJSXElement, | ||
findProps, | ||
} from "../../../utils/ast"; | ||
import { getLineTerminator } from "../../../utils/lineterminator"; | ||
import moveAndRenameImport, { | ||
addPackageImport, | ||
} from "../../../utils/packageImports"; | ||
|
||
const propsAffected = ["background", "borderColor", "shadow"]; | ||
|
||
export default function transformer(file: FileInfo, api: API) { | ||
const j = api.jscodeshift; | ||
const root = j(file.source); | ||
|
||
const toSourceOptions = getLineTerminator(file.source); | ||
|
||
if (file.source.includes("TODO: aksel box migration")) { | ||
return root.toSource(toSourceOptions); | ||
} | ||
|
||
const localName = findComponentImport({ | ||
root, | ||
j, | ||
name: "Box", | ||
packageType: "react", | ||
}); | ||
|
||
if (!localName) { | ||
return; | ||
} | ||
|
||
const astElements = root.find(j.JSXElement, { | ||
openingElement: { | ||
name: { | ||
type: "JSXIdentifier", | ||
name: localName, | ||
}, | ||
}, | ||
}); | ||
|
||
const tokenComments: TokenComments = []; | ||
|
||
for (const astElement of astElements.paths()) { | ||
let encounteredUnmigratableProp = false; | ||
for (const prop of propsAffected) { | ||
findProps({ j, path: astElement, name: prop }).forEach((attr) => { | ||
const attrvalue = attr.value.value; | ||
if (attrvalue.type === "StringLiteral") { | ||
const config = legacyTokenConfig[attrvalue.value]; | ||
if (config?.replacement) { | ||
attrvalue.value = config.replacement; | ||
} else { | ||
encounteredUnmigratableProp = true; | ||
const tokenComment: TokenComment = { | ||
prop, | ||
token: attrvalue.value, | ||
}; | ||
if (config?.comment) { | ||
tokenComment.comment = config.comment; | ||
} | ||
tokenComments.push(tokenComment); | ||
} | ||
} | ||
}); | ||
} | ||
if (!encounteredUnmigratableProp) { | ||
// TODO: ?? Box -> BoxNew type fail? (but works) | ||
(astElement.node.openingElement.name as JSXIdentifier).name = "BoxNew"; | ||
(astElement.node.closingElement.name as JSXIdentifier).name = "BoxNew"; | ||
} | ||
} | ||
|
||
const blockComment = createFileComments({ tokenComments }); | ||
|
||
const importAnalysis = analyzePartialMigration( | ||
j, | ||
root.toSource(toSourceOptions), | ||
); | ||
|
||
if (importAnalysis === "no new") { | ||
// WHY: we do nothing to the import statements if we couldn't migrate any Box | ||
} | ||
|
||
if (importAnalysis === "mixed") { | ||
// WHY: mixed Box and BoxNew == we keep old, and add the new import | ||
addPackageImport({ | ||
j, | ||
root, | ||
packageName: "@navikt/ds-react/Box", | ||
specifiers: ["BoxNew"], | ||
}); | ||
} | ||
|
||
if (importAnalysis === "all new") { | ||
// WHY: when we have only new boxes == we replace the old import with the new one | ||
moveAndRenameImport(j, root, { | ||
fromImport: "@navikt/ds-react", | ||
toImport: "@navikt/ds-react/Box", | ||
fromName: "Box", | ||
toName: "BoxNew", | ||
ignoreAlias: localName !== "Box", | ||
}); | ||
} | ||
|
||
const output = `${blockComment ? blockComment + "\n\n" : ""}${root.toSource( | ||
toSourceOptions, | ||
)}`; | ||
|
||
return output; | ||
} | ||
|
||
type TokenComment = { | ||
prop: string; | ||
token: string; | ||
comment?: string; | ||
}; | ||
|
||
type TokenComments = TokenComment[]; | ||
|
||
const createFileComments = ({ | ||
tokenComments, | ||
}: { | ||
tokenComments: TokenComments; | ||
}) => { | ||
if (tokenComments.length === 0) { | ||
return null; | ||
} | ||
|
||
let constructedComment = | ||
"/*\nTODO: aksel box migration:\nCould not migrate the following:\n"; | ||
|
||
for (const { prop, token, comment } of tokenComments.values()) { | ||
constructedComment += ` - ${prop}=${token}\n`; | ||
if (comment) { | ||
constructedComment += ` - ${comment}\n`; | ||
} | ||
} | ||
|
||
constructedComment += "*/"; | ||
|
||
return constructedComment; | ||
}; | ||
|
||
type MigrationResult = "all new" | "mixed" | "no new"; | ||
|
||
const analyzePartialMigration = ( | ||
j: JSCodeshift, | ||
source: string, | ||
): MigrationResult => { | ||
const root = j(source); | ||
|
||
const astNewElements = findJSXElement({ | ||
root, | ||
j, | ||
name: "BoxNew", | ||
originalName: "BoxNew", | ||
}); | ||
|
||
if (astNewElements.length === 0) { | ||
return "no new"; | ||
} | ||
|
||
const localName = findComponentImport({ | ||
root, | ||
j, | ||
name: "Box", | ||
packageType: "react", | ||
}); | ||
|
||
if (!localName) { | ||
// this should never happen | ||
throw new Error( | ||
'package imports have been tampered with before the package import "step" in the migration', | ||
); | ||
} | ||
|
||
const astOldElements = findJSXElement({ | ||
root, | ||
j, | ||
name: localName, | ||
originalName: "Box", | ||
}); | ||
|
||
if (astOldElements.length === 0) { | ||
return "all new"; | ||
} | ||
|
||
return "mixed"; | ||
}; |
21 changes: 21 additions & 0 deletions
21
@navikt/aksel/src/codemod/transforms/darkside/box-to-boxnew/tests/box.input.js
This file contains hidden or 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,21 @@ | ||
import { Box } from "@navikt/ds-react" | ||
|
||
export const MyComponent = () => { | ||
return (<> | ||
<Box background="bg-subtle" borderColor="border-alt-1"> | ||
migratable | ||
</Box> | ||
<Box background="bg-subtle" borderColor="border-alt-1" shadow="large"> | ||
migratable + unmigratable (no comment) | ||
</Box> | ||
<Box background="surface-alt-3-strong" > | ||
unmigratable (with comment) | ||
</Box> | ||
<Box borderWidth="4" padding={{ lg: "10", sm: "8" }} height="200rem"> | ||
old | ||
</Box> | ||
<Box borderWidth="4" background="bg-subtle"> | ||
old + migratable | ||
</Box> | ||
</>); | ||
} |
30 changes: 30 additions & 0 deletions
30
@navikt/aksel/src/codemod/transforms/darkside/box-to-boxnew/tests/box.output.js
This file contains hidden or 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,30 @@ | ||
/* | ||
TODO: aksel box migration: | ||
Could not migrate the following: | ||
- shadow=large | ||
- background=surface-alt-3-strong | ||
- Use 'bg-brand-blue-moderate' in theme 'dark'-mode. | ||
*/ | ||
|
||
import { BoxNew } from "@navikt/ds-react/Box" | ||
import { Box } from "@navikt/ds-react" | ||
|
||
export const MyComponent = () => { | ||
return (<> | ||
<BoxNew background="bg-neutral-soft" borderColor="border-meta-purple"> | ||
migratable | ||
</BoxNew> | ||
<Box background="bg-neutral-soft" borderColor="border-meta-purple" shadow="large"> | ||
migratable + unmigratable (no comment) | ||
</Box> | ||
<Box background="surface-alt-3-strong" > | ||
unmigratable (with comment) | ||
</Box> | ||
<BoxNew borderWidth="4" padding={{ lg: "10", sm: "8" }} height="200rem"> | ||
old | ||
</BoxNew> | ||
<BoxNew borderWidth="4" background="bg-neutral-soft"> | ||
old + migratable | ||
</BoxNew> | ||
</>); | ||
} |
20 changes: 20 additions & 0 deletions
20
@navikt/aksel/src/codemod/transforms/darkside/box-to-boxnew/tests/idempotent-2.input.js
This file contains hidden or 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,20 @@ | ||
/* | ||
TODO: aksel box migration: | ||
Could not migrate the following: | ||
- shadow=large | ||
- background=surface-alt-3-strong | ||
- Use 'bg-brand-blue-moderate' in theme 'dark'-mode. | ||
*/ | ||
|
||
import { Box } from "@navikt/ds-react" | ||
|
||
export const MyComponent = () => { | ||
return (<> | ||
<Box background="bg-neutral-soft" borderColor="border-meta-purple" shadow="large"> | ||
migratable + unmigratable (no comment) | ||
</Box> | ||
<Box background="surface-alt-3-strong" > | ||
unmigratable (with comment) | ||
</Box> | ||
</>); | ||
} |
20 changes: 20 additions & 0 deletions
20
@navikt/aksel/src/codemod/transforms/darkside/box-to-boxnew/tests/idempotent-2.output.js
This file contains hidden or 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,20 @@ | ||
/* | ||
TODO: aksel box migration: | ||
Could not migrate the following: | ||
- shadow=large | ||
- background=surface-alt-3-strong | ||
- Use 'bg-brand-blue-moderate' in theme 'dark'-mode. | ||
*/ | ||
|
||
import { Box } from "@navikt/ds-react" | ||
|
||
export const MyComponent = () => { | ||
return (<> | ||
<Box background="bg-neutral-soft" borderColor="border-meta-purple" shadow="large"> | ||
migratable + unmigratable (no comment) | ||
</Box> | ||
<Box background="surface-alt-3-strong" > | ||
unmigratable (with comment) | ||
</Box> | ||
</>); | ||
} |
30 changes: 30 additions & 0 deletions
30
@navikt/aksel/src/codemod/transforms/darkside/box-to-boxnew/tests/idempotent.input.js
This file contains hidden or 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,30 @@ | ||
/* | ||
TODO: aksel box migration: | ||
Could not migrate the following: | ||
- shadow=large | ||
- background=surface-alt-3-strong | ||
- Use 'bg-brand-blue-moderate' in theme 'dark'-mode. | ||
*/ | ||
|
||
import { BoxNew } from "@navikt/ds-react/Box" | ||
import { Box } from "@navikt/ds-react" | ||
|
||
export const MyComponent = () => { | ||
return (<> | ||
<BoxNew background="bg-neutral-soft" borderColor="border-meta-purple"> | ||
migratable | ||
</BoxNew> | ||
<Box background="bg-neutral-soft" borderColor="border-meta-purple" shadow="large"> | ||
migratable + unmigratable (no comment) | ||
</Box> | ||
<Box background="surface-alt-3-strong" > | ||
unmigratable (with comment) | ||
</Box> | ||
<BoxNew borderWidth="4" padding={{ lg: "10", sm: "8" }} height="200rem"> | ||
old | ||
</BoxNew> | ||
<BoxNew borderWidth="4" background="bg-neutral-soft"> | ||
old + migratable | ||
</BoxNew> | ||
</>); | ||
} |
30 changes: 30 additions & 0 deletions
30
@navikt/aksel/src/codemod/transforms/darkside/box-to-boxnew/tests/idempotent.output.js
This file contains hidden or 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,30 @@ | ||
/* | ||
TODO: aksel box migration: | ||
Could not migrate the following: | ||
- shadow=large | ||
- background=surface-alt-3-strong | ||
- Use 'bg-brand-blue-moderate' in theme 'dark'-mode. | ||
*/ | ||
|
||
import { BoxNew } from "@navikt/ds-react/Box" | ||
import { Box } from "@navikt/ds-react" | ||
|
||
export const MyComponent = () => { | ||
return (<> | ||
<BoxNew background="bg-neutral-soft" borderColor="border-meta-purple"> | ||
migratable | ||
</BoxNew> | ||
<Box background="bg-neutral-soft" borderColor="border-meta-purple" shadow="large"> | ||
migratable + unmigratable (no comment) | ||
</Box> | ||
<Box background="surface-alt-3-strong" > | ||
unmigratable (with comment) | ||
</Box> | ||
<BoxNew borderWidth="4" padding={{ lg: "10", sm: "8" }} height="200rem"> | ||
old | ||
</BoxNew> | ||
<BoxNew borderWidth="4" background="bg-neutral-soft"> | ||
old + migratable | ||
</BoxNew> | ||
</>); | ||
} |
12 changes: 12 additions & 0 deletions
12
@navikt/aksel/src/codemod/transforms/darkside/box-to-boxnew/tests/import.input.js
This file contains hidden or 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 @@ | ||
import { Box as AkselBox } from "@navikt/ds-react" | ||
|
||
export const MyComponent = () => { | ||
return (<> | ||
<AkselBox background="bg-subtle" borderColor="border-alt-1"> | ||
simple rename of import | ||
</AkselBox> | ||
<AkselBox shadow="medium"> | ||
simple rename of import | ||
</AkselBox> | ||
</>); | ||
} |
19 changes: 19 additions & 0 deletions
19
@navikt/aksel/src/codemod/transforms/darkside/box-to-boxnew/tests/import.output.js
This file contains hidden or 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 @@ | ||
/* | ||
TODO: aksel box migration: | ||
Could not migrate the following: | ||
- shadow=medium | ||
*/ | ||
|
||
import { BoxNew } from "@navikt/ds-react/Box"; | ||
import { Box as AkselBox } from "@navikt/ds-react" | ||
|
||
export const MyComponent = () => { | ||
return (<> | ||
<BoxNew background="bg-neutral-soft" borderColor="border-meta-purple"> | ||
simple rename of import | ||
</BoxNew> | ||
<AkselBox shadow="medium"> | ||
simple rename of import | ||
</AkselBox> | ||
</>); | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.