-
Notifications
You must be signed in to change notification settings - Fork 40
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
Refactor machine extractor so it exposes path, as well as node, when traversing #174
Open
mattpocock
wants to merge
3
commits into
main
Choose a base branch
from
matt/refactor-machine-extractor-so-it-exposes-path-as-well-as-node-when-traversing
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.
Open
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 |
---|---|---|
@@ -1,8 +1,5 @@ | ||
{ | ||
"editor.insertSpaces": false, | ||
"editor.formatOnSave": true, | ||
"editor.codeActionsOnSave": { | ||
"source.fixAll.eslint": true, | ||
"source.organizeImports": true | ||
} | ||
} | ||
"typescript.tsdk": "node_modules/typescript/lib" | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import { types as t } from "@babel/core"; | ||
import { NodePath, types as t } from "@babel/core"; | ||
import { Action, ChooseCondition } from "xstate"; | ||
import { assign, choose, forwardTo, send } from "xstate/lib/actions"; | ||
import { Cond, CondNode } from "./conds"; | ||
|
@@ -33,6 +33,7 @@ import { wrapParserResult } from "./wrapParserResult"; | |
|
||
export interface ActionNode { | ||
node: t.Node; | ||
path: NodePath<t.Node>; | ||
action: Action<any, any>; | ||
name: string; | ||
chooseConditions?: ParsedChooseCondition[]; | ||
|
@@ -49,63 +50,67 @@ export interface ParsedChooseCondition { | |
export const ActionAsIdentifier = maybeTsAsExpression( | ||
createParser({ | ||
babelMatcher: t.isIdentifier, | ||
parseNode: (node, context): ActionNode => { | ||
parsePath: (path, context): ActionNode => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this is being touched anyway - I'd rename this here. Perhaps |
||
return { | ||
action: node.name, | ||
node, | ||
name: node.name, | ||
action: path.node.name, | ||
node: path.node, | ||
path, | ||
name: path.node.name, | ||
declarationType: "identifier", | ||
inlineDeclarationId: context.getNodeHash(node), | ||
inlineDeclarationId: context.getNodeHash(path.node), | ||
}; | ||
}, | ||
}), | ||
}) | ||
); | ||
|
||
export const ActionAsFunctionExpression = maybeTsAsExpression( | ||
maybeIdentifierTo( | ||
createParser({ | ||
babelMatcher: isFunctionOrArrowFunctionExpression, | ||
parseNode: (node, context): ActionNode => { | ||
parsePath: (path, context): ActionNode => { | ||
const action = function actions() {}; | ||
const id = context.getNodeHash(node); | ||
const id = context.getNodeHash(path.node); | ||
|
||
action.toJSON = () => id; | ||
return { | ||
node, | ||
node: path.node, | ||
path, | ||
action, | ||
name: "", | ||
declarationType: "inline", | ||
inlineDeclarationId: id, | ||
}; | ||
}, | ||
}), | ||
), | ||
}) | ||
) | ||
); | ||
|
||
export const ActionAsString = maybeTsAsExpression( | ||
maybeIdentifierTo( | ||
createParser({ | ||
babelMatcher: t.isStringLiteral, | ||
parseNode: (node, context): ActionNode => { | ||
parsePath: (path, context): ActionNode => { | ||
return { | ||
action: node.value, | ||
node, | ||
name: node.value, | ||
path, | ||
action: path.node.value, | ||
node: path.node, | ||
name: path.node.value, | ||
declarationType: "named", | ||
inlineDeclarationId: context.getNodeHash(node), | ||
inlineDeclarationId: context.getNodeHash(path.node), | ||
}; | ||
}, | ||
}), | ||
), | ||
}) | ||
) | ||
); | ||
|
||
export const ActionAsNode = createParser({ | ||
babelMatcher: t.isNode, | ||
parseNode: (node, context): ActionNode => { | ||
const id = context.getNodeHash(node); | ||
parsePath: (path, context): ActionNode => { | ||
const id = context.getNodeHash(path.node); | ||
return { | ||
path, | ||
action: id, | ||
node, | ||
node: path.node, | ||
name: "", | ||
declarationType: "unknown", | ||
inlineDeclarationId: id, | ||
|
@@ -120,12 +125,12 @@ const ChooseFirstArg = arrayOf( | |
// too recursive | ||
// TODO - fix | ||
actions: maybeArrayOf(ActionAsString), | ||
}), | ||
}) | ||
); | ||
|
||
export const ChooseAction = wrapParserResult( | ||
namedFunctionCall("choose", ChooseFirstArg), | ||
(result, node, context): ActionNode => { | ||
(result, path, context): ActionNode => { | ||
const conditions: ParsedChooseCondition[] = []; | ||
|
||
result.argument1Result?.forEach((arg1Result) => { | ||
|
@@ -153,34 +158,37 @@ export const ChooseAction = wrapParserResult( | |
}); | ||
|
||
return { | ||
node: node, | ||
path, | ||
node: path.node, | ||
action: choose(conditions.map((condition) => condition.condition)), | ||
chooseConditions: conditions, | ||
name: "", | ||
declarationType: "inline", | ||
inlineDeclarationId: context.getNodeHash(node), | ||
inlineDeclarationId: context.getNodeHash(path.node), | ||
}; | ||
}, | ||
} | ||
); | ||
|
||
interface AssignFirstArg { | ||
path: NodePath<t.Node>; | ||
node: t.Node; | ||
value: {} | (() => {}); | ||
} | ||
|
||
const AssignFirstArgObject = createParser({ | ||
babelMatcher: t.isObjectExpression, | ||
parseNode: (node, context) => { | ||
parsePath: (path, context) => { | ||
return { | ||
node, | ||
path, | ||
node: path.node, | ||
value: {}, | ||
}; | ||
}, | ||
}); | ||
|
||
const AssignFirstArgFunction = createParser({ | ||
babelMatcher: isFunctionOrArrowFunctionExpression, | ||
parseNode: (node, context) => { | ||
parsePath: (path, context) => { | ||
const value = function anonymous() { | ||
return {}; | ||
}; | ||
|
@@ -189,7 +197,8 @@ const AssignFirstArgFunction = createParser({ | |
}; | ||
|
||
return { | ||
node, | ||
path, | ||
node: path.node, | ||
value, | ||
}; | ||
}, | ||
|
@@ -202,7 +211,7 @@ const AssignFirstArg = unionType<AssignFirstArg>([ | |
|
||
export const AssignAction = wrapParserResult( | ||
namedFunctionCall("assign", AssignFirstArg), | ||
(result, node, context): ActionNode => { | ||
(result, path, context): ActionNode => { | ||
const defaultAction = function anonymous() { | ||
return {}; | ||
}; | ||
|
@@ -211,13 +220,14 @@ export const AssignAction = wrapParserResult( | |
}; | ||
|
||
return { | ||
path, | ||
node: result.node, | ||
action: assign(result.argument1Result?.value || defaultAction), | ||
name: "", | ||
declarationType: "inline", | ||
inlineDeclarationId: context.getNodeHash(node), | ||
inlineDeclarationId: context.getNodeHash(path.node), | ||
}; | ||
}, | ||
} | ||
); | ||
|
||
export const SendActionSecondArg = objectTypeWithKnownKeys({ | ||
|
@@ -233,10 +243,11 @@ export const SendAction = wrapParserResult( | |
namedFunctionCall( | ||
"send", | ||
unionType<{ node: t.Node; value?: string }>([StringLiteral, AnyNode]), | ||
SendActionSecondArg, | ||
SendActionSecondArg | ||
), | ||
(result, node, context): ActionNode => { | ||
(result, path, context): ActionNode => { | ||
return { | ||
path, | ||
node: result.node, | ||
name: "", | ||
action: send( | ||
|
@@ -250,12 +261,12 @@ export const SendAction = wrapParserResult( | |
id: result.argument2Result?.id?.value, | ||
to: result.argument2Result?.to?.value, | ||
delay: result.argument2Result?.delay?.value, | ||
}, | ||
} | ||
), | ||
declarationType: "inline", | ||
inlineDeclarationId: context.getNodeHash(node), | ||
inlineDeclarationId: context.getNodeHash(path.node), | ||
}; | ||
}, | ||
} | ||
); | ||
|
||
export const ForwardToActionSecondArg = objectTypeWithKnownKeys({ | ||
|
@@ -264,17 +275,18 @@ export const ForwardToActionSecondArg = objectTypeWithKnownKeys({ | |
|
||
export const ForwardToAction = wrapParserResult( | ||
namedFunctionCall("forwardTo", StringLiteral, ForwardToActionSecondArg), | ||
(result, node, context): ActionNode => { | ||
(result, path, context): ActionNode => { | ||
return { | ||
node: result.node, | ||
action: forwardTo(result.argument1Result?.value || "", { | ||
to: result.argument2Result?.to?.value, | ||
}), | ||
path, | ||
name: "", | ||
declarationType: "inline", | ||
inlineDeclarationId: context.getNodeHash(node), | ||
inlineDeclarationId: context.getNodeHash(path.node), | ||
}; | ||
}, | ||
} | ||
); | ||
|
||
const NamedAction = unionType([ | ||
|
@@ -306,5 +318,5 @@ const BasicAction = unionType([ | |
export const ArrayOfBasicActions = maybeArrayOf(BasicAction); | ||
|
||
export const MaybeArrayOfActions = maybeArrayOf( | ||
unionType([NamedAction, BasicAction]), | ||
unionType([NamedAction, BasicAction]) | ||
); |
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
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.