-
-
Notifications
You must be signed in to change notification settings - Fork 184
LuaIterable language extension #981
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
Perryvw
merged 10 commits into
TypeScriptToLua:master
from
tomblind:iterator-language-extension
Feb 20, 2021
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
46b9c9c
added LuaIterable and LuaMultiIterable language extensions
tomblind 7fdc39a
indentation fix
tomblind 33a5af7
remove LuaMultiIterable in favor of LuaIterable<LuaMultiReturn>
tomblind f81e97b
switched from expectToHaveDiagnostics to expectDiagnosticsToMatchSnap…
tomblind 1d0f944
updates based on feedback and discussions
tomblind 44645cd
fixed issue with no-state vs state iterables, and updated tests to ch…
tomblind 12ec435
updated extension kind checking to reduce complexity
tomblind 74d797b
updated new multi tests to actually use multiple values
tomblind f44c6c7
Revert "updated extension kind checking to reduce complexity"
tomblind a5c54ea
replaced raw brands with LuaExtension type
tomblind 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
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
82 changes: 82 additions & 0 deletions
82
src/transformation/visitors/language-extensions/iterable.ts
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,82 @@ | ||
| import * as ts from "typescript"; | ||
| import * as lua from "../../../LuaAST"; | ||
| import * as extensions from "../../utils/language-extensions"; | ||
| import { TransformationContext } from "../../context"; | ||
| import { getVariableDeclarationBinding, transformForInitializer } from "../loops/utils"; | ||
| import { transformArrayBindingElement } from "../variable-declaration"; | ||
| import { invalidMultiIterableWithoutDestructuring } from "../../utils/diagnostics"; | ||
| import { cast } from "../../../utils"; | ||
| import { isMultiReturnType } from "./multi"; | ||
|
|
||
| export function isIterableType(type: ts.Type): boolean { | ||
| return extensions.isExtensionType(type, extensions.ExtensionKind.IterableType); | ||
| } | ||
|
|
||
| export function returnsIterableType(context: TransformationContext, node: ts.CallExpression): boolean { | ||
| const signature = context.checker.getResolvedSignature(node); | ||
| const type = signature?.getReturnType(); | ||
| return type ? isIterableType(type) : false; | ||
| } | ||
|
|
||
| export function isIterableExpression(context: TransformationContext, expression: ts.Expression): boolean { | ||
| const type = context.checker.getTypeAtLocation(expression); | ||
| return isIterableType(type); | ||
| } | ||
|
|
||
| function transformForOfMultiIterableStatement( | ||
| context: TransformationContext, | ||
| statement: ts.ForOfStatement, | ||
| block: lua.Block | ||
| ): lua.Statement { | ||
| const luaIterator = context.transformExpression(statement.expression); | ||
| let identifiers: lua.Identifier[] = []; | ||
|
|
||
| if (ts.isVariableDeclarationList(statement.initializer)) { | ||
| // Variables declared in for loop | ||
| // for ${initializer} in ${iterable} do | ||
| const binding = getVariableDeclarationBinding(context, statement.initializer); | ||
| if (ts.isArrayBindingPattern(binding)) { | ||
| identifiers = binding.elements.map(e => transformArrayBindingElement(context, e)); | ||
| } else { | ||
| context.diagnostics.push(invalidMultiIterableWithoutDestructuring(binding)); | ||
| } | ||
| } else if (ts.isArrayLiteralExpression(statement.initializer)) { | ||
| // Variables NOT declared in for loop - catch iterator values in temps and assign | ||
| // for ____value0 in ${iterable} do | ||
| // ${initializer} = ____value0 | ||
| identifiers = statement.initializer.elements.map((_, i) => lua.createIdentifier(`____value${i}`)); | ||
| if (identifiers.length > 0) { | ||
| block.statements.unshift( | ||
| lua.createAssignmentStatement( | ||
| statement.initializer.elements.map(e => | ||
| cast(context.transformExpression(e), lua.isAssignmentLeftHandSideExpression) | ||
| ), | ||
| identifiers | ||
| ) | ||
| ); | ||
| } | ||
| } else { | ||
| context.diagnostics.push(invalidMultiIterableWithoutDestructuring(statement.initializer)); | ||
| } | ||
|
|
||
| if (identifiers.length === 0) { | ||
| identifiers.push(lua.createAnonymousIdentifier()); | ||
| } | ||
|
|
||
| return lua.createForInStatement(block, identifiers, [luaIterator], statement); | ||
| } | ||
|
|
||
| export function transformForOfIterableStatement( | ||
| context: TransformationContext, | ||
| statement: ts.ForOfStatement, | ||
| block: lua.Block | ||
| ): lua.Statement { | ||
| const type = context.checker.getTypeAtLocation(statement.expression); | ||
| if (type.aliasTypeArguments?.length === 2 && isMultiReturnType(type.aliasTypeArguments[0])) { | ||
| return transformForOfMultiIterableStatement(context, statement, block); | ||
| } | ||
|
|
||
| const luaIterator = context.transformExpression(statement.expression); | ||
| const identifier = transformForInitializer(context, statement.initializer, block); | ||
| return lua.createForInStatement(block, [identifier], [luaIterator], statement); | ||
| } |
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
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
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
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.