Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ describe('updateInstUIImportVersions', () => {
)
})

it('rewrites an import that @instructure/ui re-exports under an alias', () => {
runInlineTest(
updateInstUIImportVersions,
{ versionTo: 'v11.7' },
makeFileInfo(`import { TreeBrowserCollection } from '@instructure/ui'`),
`import { TreeBrowserCollection } from '@instructure/ui/v11_7'`
)
})

it('rewrites versioned import from versionFrom to versionTo', () => {
runInlineTest(
updateInstUIImportVersions,
Expand Down
53 changes: 27 additions & 26 deletions packages/ui-codemods/scripts/generateVersionedExports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

import { resolve } from 'node:path'
import { mkdirSync, readdirSync, readFileSync, writeFileSync } from 'node:fs'
import ts from 'typescript'
import jscodeshift from 'jscodeshift'

// '@instructure/ui-button/v8_0' → matches; '@instructure/ui-button' or 'react' → no match
const VERSIONED_INSTUI = /^@instructure\/[^/]+\/v\d+_\d+$/
Expand Down Expand Up @@ -62,33 +62,34 @@ function findLatestVersionFile(dir: string): string {
return resolve(dir, latest)
}

function isVersionedInstUIExport(node: ts.ExportDeclaration): boolean {
const { moduleSpecifier } = node
return (
moduleSpecifier !== undefined &&
ts.isStringLiteral(moduleSpecifier) &&
VERSIONED_INSTUI.test(moduleSpecifier.text)
)
}

function getExportedNames(node: ts.ExportDeclaration): string[] {
const { exportClause } = node
if (!exportClause || !ts.isNamedExports(exportClause)) return []
return exportClause.elements.map((el) => el.name.text)
}

function parseVersionedComponents(filePath: string): string[] {
const source = readFileSync(filePath, 'utf-8')
const sourceFile = ts.createSourceFile(
filePath,
source,
ts.ScriptTarget.Latest
)

return sourceFile.statements
.filter(ts.isExportDeclaration)
.filter(isVersionedInstUIExport)
.flatMap(getExportedNames)
const j = jscodeshift.withParser('ts')
const root = j(source)
const components: string[] = []

root.find(j.ExportNamedDeclaration).forEach((path: any) => {
const node = path.value
const source = node.source

// Check if this is a versioned @instructure/ui import
if (
source &&
source.type === 'StringLiteral' &&
VERSIONED_INSTUI.test(source.value)
) {
// Extract exported names
if (node.specifiers && node.specifiers.length > 0) {
node.specifiers.forEach((spec: any) => {
if (spec.exported && spec.exported.name) {
components.push(spec.exported.name)
}
})
}
}
})

return components
}

function generateFileContent(components: string[]): string {
Expand Down
Loading