-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat (packages/codemod): Add codemod to rm experimental_StreamData.
- Loading branch information
Showing
6 changed files
with
126 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@ai-sdk/codemod': patch | ||
--- | ||
|
||
feat (packages/codemod): Add codemod to rm experimental_StreamData. |
65 changes: 65 additions & 0 deletions
65
packages/codemod/src/codemods/remove-experimental-streamdata.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,65 @@ | ||
import { API, FileInfo } from 'jscodeshift'; | ||
|
||
export default function transformer(file: FileInfo, api: API) { | ||
const j = api.jscodeshift; | ||
const root = j(file.source); | ||
|
||
// Track imports from 'ai' package only | ||
const targetImports = new Set<string>(); | ||
|
||
// First pass - collect imports from 'ai' package | ||
root | ||
.find(j.ImportDeclaration) | ||
.filter(path => path.node.source.value === 'ai') | ||
.forEach(path => { | ||
path.node.specifiers?.forEach(spec => { | ||
if ( | ||
spec.type === 'ImportSpecifier' && | ||
spec.imported.type === 'Identifier' && | ||
spec.imported.name === 'experimental_StreamData' | ||
) { | ||
// Track local name | ||
targetImports.add(spec.local?.name || 'experimental_StreamData'); | ||
} | ||
}); | ||
}); | ||
|
||
// Second pass - replace imports from 'ai' package only | ||
root | ||
.find(j.ImportDeclaration) | ||
.filter(path => path.node.source.value === 'ai') | ||
.forEach(path => { | ||
const newSpecifiers = path.node.specifiers?.map(spec => { | ||
if ( | ||
spec.type === 'ImportSpecifier' && | ||
spec.imported.type === 'Identifier' && | ||
spec.imported.name === 'experimental_StreamData' | ||
) { | ||
return j.importSpecifier( | ||
j.identifier('StreamData'), | ||
spec.local?.name === 'experimental_StreamData' ? null : spec.local, | ||
); | ||
} | ||
return spec; | ||
}); | ||
path.node.specifiers = newSpecifiers; | ||
}); | ||
|
||
// Replace type/class references only for tracked imports | ||
root | ||
.find(j.Identifier) | ||
.filter(path => { | ||
// Only replace if: | ||
// 1. It's one of our tracked imports from 'ai' | ||
// 2. It's not part of an import declaration (to avoid replacing other imports) | ||
return ( | ||
targetImports.has(path.node.name) && | ||
!j(path).closest(j.ImportDeclaration).size() | ||
); | ||
}) | ||
.forEach(path => { | ||
path.node.name = 'StreamData'; | ||
}); | ||
|
||
return root.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
23 changes: 23 additions & 0 deletions
23
packages/codemod/src/test/__testfixtures__/remove-experimental-streamdata.input.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,23 @@ | ||
// @ts-nocheck | ||
import { experimental_StreamData } from 'ai'; | ||
import { experimental_StreamData as StreamDataLegacy } from 'other-pkg'; | ||
|
||
// Should rename - class extension | ||
class CustomStream extends experimental_StreamData { | ||
// Custom implementation | ||
} | ||
|
||
// Should rename - type usage | ||
const createStream = (): experimental_StreamData => { | ||
return new experimental_StreamData(); | ||
}; | ||
|
||
// Should rename - instance check | ||
const isStreamData = (obj: unknown): obj is experimental_StreamData => { | ||
return obj instanceof experimental_StreamData; | ||
}; | ||
|
||
// Should NOT rename - different package | ||
class OtherStream extends StreamDataLegacy { | ||
// Custom implementation | ||
} |
23 changes: 23 additions & 0 deletions
23
packages/codemod/src/test/__testfixtures__/remove-experimental-streamdata.output.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,23 @@ | ||
// @ts-nocheck | ||
import { StreamData } from 'ai'; | ||
import { experimental_StreamData as StreamDataLegacy } from 'other-pkg'; | ||
|
||
// Should rename - class extension | ||
class CustomStream extends StreamData { | ||
// Custom implementation | ||
} | ||
|
||
// Should rename - type usage | ||
const createStream = (): StreamData => { | ||
return new StreamData(); | ||
}; | ||
|
||
// Should rename - instance check | ||
const isStreamData = (obj: unknown): obj is StreamData => { | ||
return obj instanceof StreamData; | ||
}; | ||
|
||
// Should NOT rename - different package | ||
class OtherStream extends StreamDataLegacy { | ||
// Custom implementation | ||
} |
9 changes: 9 additions & 0 deletions
9
packages/codemod/src/test/remove-experimental-streamdata.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,9 @@ | ||
import { describe, it } from 'vitest'; | ||
import transformer from '../codemods/remove-experimental-streamdata'; | ||
import { testTransform } from './test-utils'; | ||
|
||
describe('remove-experimental-streamdata', () => { | ||
it('transforms correctly', () => { | ||
testTransform(transformer, 'remove-experimental-streamdata'); | ||
}); | ||
}); |