AST: Refactor marking nodes that return (#5275)

AST: Refactor marking nodes that return
This commit is contained in:
Geoffrey Booth
2019-12-30 12:57:50 -08:00
committed by GitHub
2 changed files with 17 additions and 19 deletions

View File

@@ -378,9 +378,17 @@
// Only override the component `ast*` methods as needed.
ast(o, level) {
var astNode;
// Merge `level` into `o` and perform other universal checks.
o = this.astInitialize(o, level);
// Create serializable representation of this node.
astNode = this.astNode(o);
return this.astAddReturns(astNode);
if ((this.astNode != null) && this.canBeReturned) {
// Mark AST nodes that correspond to expressions that (implicitly) return.
// We cant do this as part of `astNode` because we need to assemble child
// nodes first before marking the parent being returned.
astNode.returns = true;
}
return astNode;
}
astInitialize(o, level) {
@@ -430,17 +438,6 @@
return jisonLocationDataToAstLocationData(this.locationData);
}
// Mark AST nodes that correspond to expressions that (implicitly) return.
astAddReturns(ast) {
if (ast == null) {
return ast;
}
if (this.canBeReturned) {
ast.returns = true;
}
return ast;
}
// Determines whether an AST node needs an `ExpressionStatement` wrapper.
// Typically matches our `isStatement()` logic but this allows overriding.
isStatementAst(o) {

View File

@@ -281,9 +281,16 @@ exports.Base = class Base
# **WARNING: DO NOT OVERRIDE THIS METHOD IN CHILD CLASSES.**
# Only override the component `ast*` methods as needed.
ast: (o, level) ->
# Merge `level` into `o` and perform other universal checks.
o = @astInitialize o, level
# Create serializable representation of this node.
astNode = @astNode o
@astAddReturns astNode
# Mark AST nodes that correspond to expressions that (implicitly) return.
# We cant do this as part of `astNode` because we need to assemble child
# nodes first before marking the parent being returned.
if @astNode? and @canBeReturned
Object.assign astNode, {returns: yes}
astNode
astInitialize: (o, level) ->
o = Object.assign {}, o
@@ -318,12 +325,6 @@ exports.Base = class Base
astLocationData: ->
jisonLocationDataToAstLocationData @locationData
# Mark AST nodes that correspond to expressions that (implicitly) return.
astAddReturns: (ast) ->
return ast unless ast?
ast.returns = yes if @canBeReturned
ast
# Determines whether an AST node needs an `ExpressionStatement` wrapper.
# Typically matches our `isStatement()` logic but this allows overriding.
isStatementAst: (o) ->