Skip to content
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

Work #19

Merged
merged 8 commits into from
Oct 10, 2024
Prev Previous commit
Next Next commit
refactor: Simplify expression statement of assignment expression to a…
…ssignment statement
tristanmenzel committed Oct 10, 2024
commit 967dcf9195ad69034519784e2c3b0ee12d1a7e6c
6 changes: 6 additions & 0 deletions src/awst/node-factory.ts
Original file line number Diff line number Diff line change
@@ -137,6 +137,12 @@ const explicitNodeFactory = {
})
},
expressionStatement({ expr }: { expr: Expression }) {
if (expr instanceof AssignmentExpression) {
return new AssignmentStatement({
...expr,
})
}

return new ExpressionStatement({
expr,
sourceLocation: expr.sourceLocation,
9 changes: 1 addition & 8 deletions src/awst_build/base-visitor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import ts from 'typescript'
import { awst } from '../awst'
import { nodeFactory } from '../awst/node-factory'
import type { Expression, LValue, Statement } from '../awst/nodes'
import type { SourceLocation } from '../awst/source-location'
@@ -453,13 +452,7 @@ export abstract class BaseVisitor implements Visitor<Expressions, NodeBuilder> {
}

handleAssignmentStatement(target: InstanceBuilder, source: InstanceBuilder, sourceLocation: SourceLocation): Statement {
const expr = this.handleAssignment(target, source, sourceLocation).resolve()
if (expr instanceof awst.AssignmentExpression) {
return nodeFactory.assignmentStatement({
...expr,
})
}
return nodeFactory.expressionStatement({ expr })
return nodeFactory.expressionStatement({ expr: this.handleAssignment(target, source, sourceLocation).resolve() })
}

handleAssignment(target: InstanceBuilder, source: InstanceBuilder, sourceLocation: SourceLocation): InstanceBuilder {