-
Notifications
You must be signed in to change notification settings - Fork 42
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
create @xstate/codemods
package and v4-to-v5
migration codemod
#332
Draft
with-heart
wants to merge
3
commits into
statelyai:main
Choose a base branch
from
with-heart:v4-to-v5-codemod
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+614
−0
Draft
Changes from all commits
Commits
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 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,31 @@ | ||
# `@xstate/codemods` | ||
|
||
A library of automatic codebase refactors for [XState](https://github.com/statelyai/xstate). | ||
|
||
## `v4-to-v5` | ||
|
||
This codemod migrates a v4 codebase to v5. | ||
|
||
### `machine-to-create-machine` | ||
|
||
```diff | ||
-import { Machine } from 'xstate'; | ||
+import { createMachine } from 'xstate'; | ||
|
||
-const machine = Machine({}); | ||
+const machine = createMachine({}); | ||
``` | ||
|
||
```diff | ||
-import { Machine as SomethingElse } from 'xstate'; | ||
+import { createMachine as SomethingElse } from 'xstate'; | ||
|
||
const machine = SomethingElse({}) | ||
``` | ||
|
||
```diff | ||
import xstate from 'xstate'; | ||
|
||
-const machine = xstate.Machine({}); | ||
+const machine = xstate.createMachine({}); | ||
``` |
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,6 @@ | ||
module.exports = { | ||
presets: [ | ||
['@babel/preset-env', { targets: { node: '16' } }], | ||
'@babel/preset-typescript', | ||
], | ||
}; |
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,17 @@ | ||
{ | ||
"name": "@xstate/codemods", | ||
"version": "0.0.0", | ||
"license": "MIT", | ||
"main": "dist/xstate-codemods.cjs.js", | ||
"scripts": { | ||
"test": "jest" | ||
}, | ||
"dependencies": { | ||
"typescript": "^4.9.4" | ||
}, | ||
"devDependencies": { | ||
"@tsconfig/node16": "^1.0.3", | ||
"@tsconfig/strictest": "^2.0.0", | ||
"outdent": "^0.8.0" | ||
} | ||
} |
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 @@ | ||
export { v4ToV5 } from './v4-to-v5'; |
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,3 @@ | ||
import { machineToCreateMachine } from './transformers/machine-to-create-machine'; | ||
|
||
export const v4ToV5 = [machineToCreateMachine]; |
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,226 @@ | ||
import outdent from 'outdent'; | ||
import ts from 'typescript'; | ||
import { | ||
isDescendantOfXStateImport, | ||
isMachineCallExpression, | ||
isMachineNamedImportSpecifier, | ||
isMachinePropertyAccessExpression, | ||
isXStateImportClause, | ||
isXStateImportDeclaration, | ||
} from './predicates'; | ||
import { parse } from './test-utils'; | ||
import { findDescendant } from './utils'; | ||
|
||
describe('isXStateImportDeclaration', () => { | ||
test.each<[code: string, expected: boolean]>([ | ||
[`import xstate from 'xstate'`, true], | ||
[`import { Machine } from 'xstate'`, true], | ||
[`import { Machine as M } from 'xstate'`, true], | ||
[`import xstate from 'not-xstate'`, false], | ||
[`import { Machine } from 'not-xstate'`, false], | ||
[`import { Machine as M } from 'not-xstate'`, false], | ||
[`console.log('not an ImportDeclaration')`, false], | ||
])('%s (%s)', (code, expected) => { | ||
const sourceFile = parse(code); | ||
const node = findDescendant(sourceFile, isXStateImportDeclaration); | ||
|
||
if (expected) { | ||
expect(node).not.toBeUndefined(); | ||
expect(node!.kind).toBe(ts.SyntaxKind.ImportDeclaration); | ||
} else { | ||
expect(node).toBeUndefined(); | ||
} | ||
}); | ||
}); | ||
|
||
describe('isDescendantOfXStateImport', () => { | ||
test.each<[code: string, expected: boolean]>([ | ||
[`import xstate from 'xstate'`, true], | ||
[`import { Machine } from 'xstate'`, true], | ||
[`import { Machine as M } from 'xstate'`, true], | ||
[`import xstate from 'not-xstate'`, false], | ||
[`import { Machine } from 'not-xstate'`, false], | ||
[`import { Machine as M } from 'not-xstate'`, false], | ||
])(`%s (%s)`, (code, expected) => { | ||
const sourceFile = parse(code); | ||
const node = findDescendant(sourceFile, isDescendantOfXStateImport); | ||
|
||
if (expected) { | ||
expect(node).not.toBeUndefined(); | ||
} else { | ||
expect(node).toBeUndefined(); | ||
} | ||
}); | ||
}); | ||
|
||
describe('isXStateImportClause', () => { | ||
test.each<[code: string, expected: boolean]>([ | ||
[`import xstate from 'xstate'`, true], | ||
[`import { Machine } from 'xstate'`, false], | ||
[`import { Machine as M } from 'xstate'`, false], | ||
[`import xstate from 'not-xstate'`, false], | ||
[`import { Machine } from 'not-xstate'`, false], | ||
[`import { Machine as M } from 'not-xstate'`, false], | ||
])('%s (%s)', (code, expected) => { | ||
const sourceFile = parse(code); | ||
const node = findDescendant(sourceFile, isXStateImportClause); | ||
|
||
if (expected) { | ||
expect(node).not.toBeUndefined(); | ||
expect(node!.kind).toBe(ts.SyntaxKind.ImportClause); | ||
} else { | ||
expect(node).toBeUndefined(); | ||
} | ||
}); | ||
}); | ||
|
||
describe('isMachineNamedImportSpecifier', () => { | ||
test.each<[code: string, expected: boolean]>([ | ||
[`import { Machine } from 'xstate'`, true], | ||
[`import { Machine as M } from 'xstate'`, true], | ||
[`import { NotMachine } from 'xstate'`, false], | ||
[`import { NotMachine as Machine } from 'xstate'`, false], | ||
[`console.log('not an ImportSpecifier')`, false], | ||
])('%s (%s)', (code, expected) => { | ||
const sourceFile = parse(code); | ||
const node = findDescendant(sourceFile, isMachineNamedImportSpecifier); | ||
|
||
if (expected) { | ||
expect(node).not.toBeUndefined(); | ||
expect(node!.kind).toBe(ts.SyntaxKind.ImportSpecifier); | ||
} else { | ||
expect(node).toBeUndefined(); | ||
} | ||
}); | ||
}); | ||
|
||
describe('isMachineCallExpression', () => { | ||
test.each<[code: string, expected: boolean]>([ | ||
[ | ||
outdent` | ||
import { Machine } from 'xstate' | ||
const machine = Machine({}) | ||
`, | ||
true, | ||
], | ||
[ | ||
outdent` | ||
import { Machine as M } from 'xstate' | ||
const machine = M({}) | ||
`, | ||
false, | ||
], | ||
[ | ||
outdent` | ||
import xstate from 'xstate' | ||
const machine = xstate.Machine({}) | ||
`, | ||
false, | ||
], | ||
[ | ||
outdent` | ||
import { Machine } from 'not-xstate' | ||
const machine = Machine({}) | ||
`, | ||
false, | ||
], | ||
[ | ||
outdent` | ||
function Machine() {} | ||
const machine = Machine({}) | ||
`, | ||
false, | ||
], | ||
[ | ||
outdent` | ||
console.log('non-xstate CallExpression') | ||
`, | ||
false, | ||
], | ||
[ | ||
outdent` | ||
const str = "not a CallExpression" | ||
`, | ||
false, | ||
], | ||
])('%s (%s)', (code, expected) => { | ||
const sourceFile = parse(code); | ||
const node = findDescendant(sourceFile, isMachineCallExpression); | ||
|
||
if (expected) { | ||
expect(node).not.toBeUndefined(); | ||
expect(node!.kind).toBe(ts.SyntaxKind.CallExpression); | ||
} else { | ||
expect(node).toBeUndefined(); | ||
} | ||
}); | ||
}); | ||
|
||
describe('isMachinePropertyAccessExpression', () => { | ||
test.each<[input: string, expected: boolean]>([ | ||
[ | ||
outdent` | ||
import xstate from 'xstate' | ||
const machine = xstate.Machine({}) | ||
`, | ||
true, | ||
], | ||
[ | ||
outdent` | ||
import xstate from 'xstate' | ||
const Machine = xstate.Machine | ||
`, | ||
true, | ||
], | ||
[ | ||
outdent` | ||
import { Machine } from 'xstate' | ||
const machine = Machine({}) | ||
`, | ||
false, | ||
], | ||
[ | ||
outdent` | ||
import { Machine as M } from 'xstate' | ||
const machine = M({}) | ||
`, | ||
false, | ||
], | ||
[ | ||
outdent` | ||
import { Machine } from 'not-xstate' | ||
const machine = Machine({}) | ||
`, | ||
false, | ||
], | ||
[ | ||
outdent` | ||
function Machine() {} | ||
const machine = Machine({}) | ||
`, | ||
false, | ||
], | ||
[ | ||
outdent` | ||
console.log('non-xstate PropertyAccessExpression') | ||
`, | ||
false, | ||
], | ||
[ | ||
outdent` | ||
const str = "not a CallExpression" | ||
`, | ||
false, | ||
], | ||
])('%s (%s)', (code, expected) => { | ||
const sourceFile = parse(code); | ||
const node = findDescendant(sourceFile, isMachinePropertyAccessExpression); | ||
|
||
if (expected) { | ||
expect(node).not.toBeUndefined(); | ||
expect(node!.kind).toBe(ts.SyntaxKind.PropertyAccessExpression); | ||
} else { | ||
expect(node).toBeUndefined(); | ||
} | ||
}); | ||
}); |
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,85 @@ | ||
import ts from 'typescript'; | ||
import { findDescendant } from './utils'; | ||
|
||
export function isXStateImportDeclaration( | ||
node: ts.Node, | ||
): node is ts.ImportDeclaration { | ||
return ( | ||
ts.isImportDeclaration(node) && | ||
ts.isStringLiteral(node.moduleSpecifier) && | ||
node.moduleSpecifier.text === 'xstate' | ||
); | ||
} | ||
|
||
export function isDescendantOfXStateImport(node: ts.Node): boolean { | ||
return Boolean(ts.findAncestor(node, isXStateImportDeclaration)); | ||
} | ||
|
||
export function isXStateImportClause( | ||
node: ts.Node, | ||
): node is ts.ImportClause & { name: ts.Identifier } { | ||
return Boolean( | ||
isDescendantOfXStateImport(node) && | ||
ts.isImportClause(node) && | ||
node.name && | ||
ts.isIdentifier(node.name), | ||
); | ||
} | ||
|
||
export function isMachineNamedImportSpecifier( | ||
node: ts.Node, | ||
): node is ts.ImportSpecifier { | ||
return ( | ||
isDescendantOfXStateImport(node) && | ||
ts.isImportSpecifier(node) && | ||
(node.propertyName | ||
? node.propertyName.text === 'Machine' | ||
: node.name.text === 'Machine') | ||
); | ||
} | ||
|
||
export function isMachineCallExpression( | ||
node: ts.Node, | ||
): node is ts.CallExpression & { expression: ts.Identifier } { | ||
if (!ts.isCallExpression(node) || !ts.isIdentifier(node.expression)) { | ||
return false; | ||
} | ||
|
||
const sourceFile = node.getSourceFile(); | ||
const namedImportSpecifier = findDescendant( | ||
sourceFile, | ||
isMachineNamedImportSpecifier, | ||
); | ||
|
||
if (!namedImportSpecifier) { | ||
return false; | ||
} | ||
|
||
return ( | ||
node.expression.text === | ||
(namedImportSpecifier.propertyName ?? namedImportSpecifier.name).text | ||
); | ||
} | ||
|
||
export function isMachinePropertyAccessExpression( | ||
node: ts.Node, | ||
): node is ts.PropertyAccessExpression { | ||
if ( | ||
!ts.isPropertyAccessExpression(node) || | ||
!ts.isIdentifier(node.expression) | ||
) { | ||
return false; | ||
} | ||
|
||
const sourceFile = node.getSourceFile(); | ||
const xstateImportClause = findDescendant(sourceFile, isXStateImportClause); | ||
|
||
if (!xstateImportClause) { | ||
return false; | ||
} | ||
|
||
return ( | ||
node.expression.text === xstateImportClause.name.text && | ||
node.name.text === 'Machine' | ||
); | ||
} |
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,13 @@ | ||
import ts from 'typescript'; | ||
import { isXStateImportDeclaration } from './predicates'; | ||
import { parse } from './test-utils'; | ||
import { findDescendant } from './utils'; | ||
|
||
test('parse', () => { | ||
const sourceFile = parse(`import { Machine } from 'xstate'`); | ||
|
||
expect(ts.isSourceFile(sourceFile)).toBe(true); | ||
expect( | ||
findDescendant(sourceFile, isXStateImportDeclaration), | ||
).not.toBeUndefined(); | ||
}); |
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,8 @@ | ||
import ts from 'typescript'; | ||
|
||
/** | ||
* Parse a string of code into a TypeScript `SourceFile` node. | ||
*/ | ||
export function parse(code: string, fileName: string = 'code.ts') { | ||
return ts.createSourceFile(fileName, code, ts.ScriptTarget.Latest, true); | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Predicates are functions that take a node and return a boolean. They're useful when traversing the tree with visitor functions because they allow us to select specific types of nodes that we want to operate on.
TypeScript itself exports many predicate fns—one for each type of node.
In this module, we combine the ts predicates with a few additional checks of node properties to create functions that allow us to select each type/shape of node our codemod needs in order to do its thing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The other cool thing about predicates is that all of the TypeScript predicates and some of these predicates return a type predicate (the
node is ts.XYZ
syntax).A type predicate narrows the type of its argument, meaning that when used with a conditional check, a less specific type (
ts.Node
) is narrowed to a more specific type (ts.XYZ
).Here's an example:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PS: These predicates will make a lot more sense if you explore the AST from this sample: https://ts-ast-viewer.com/#code/JYWwDg9gTgLgBAbzgWQIYGMAWwB2BTOAXzgDMoIQ4ByADwGcZUY8qAoUSWRFDbfOVHRRFS5SrQZMW7cNHj1GzURWoKpbVugg4GcEL1wEAvDyyGAFAkIBKTdt36z-E8ks27O+I77G4a5gB0aE54btZAA