Skip to content

Commit 3677d11

Browse files
committed
Refactor Block.astProperties: use expression.astLocationData() to get location data, rather than extracting it from the whole AST object; move all the logic into one function, rather than spreading it out across several functions on the Block class that all appear to be internal
1 parent 2d2e98f commit 3677d11

File tree

2 files changed

+34
-70
lines changed

2 files changed

+34
-70
lines changed

lib/coffeescript/nodes.js

Lines changed: 18 additions & 40 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/nodes.coffee

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -773,28 +773,22 @@ exports.Block = class Block extends Base
773773
return nodes[0] if nodes.length is 1 and nodes[0] instanceof Block
774774
new Block nodes
775775

776-
# Wraps a non-statement expression’s AST in an `ExpressionStatement` AST node.
777-
asExpressionStatementAst: (ast) ->
778-
Object.assign
779-
type: 'ExpressionStatement'
780-
expression: ast
781-
,
782-
extractAstLocationData ast
783-
784-
# Returns the AST for a given top-level expression: either the expression's
785-
# AST as-is (if the expression is a statement) or the expression's AST wrapped
786-
# in an `ExpressionStatement` AST node.
787-
getExpressionAst: (expression) ->
788-
ast = expression.ast()
789-
# return ast if expression.isStatement o
790-
@asExpressionStatementAst ast
791-
792-
bodyToAst: ->
793-
@getExpressionAst(expression) for expression in @expressions
794-
795776
astType: -> 'Program'
796777

797-
astProperties: ->
778+
astProperties: (o) ->
779+
body = []
780+
for expression in @expressions
781+
# If an expression is a statement, it can be added to the body as is.
782+
if expression.isStatement o
783+
body.push expression.ast()
784+
# Otherwise, we need to wrap it in an `ExpressionStatement` AST node.
785+
else
786+
body.push Object.assign
787+
type: 'ExpressionStatement'
788+
expression: expression.ast()
789+
,
790+
expression.astLocationData()
791+
798792
return
799793
# For now, we’re not including `sourceType` on the `Program` AST node.
800794
# Its value could be either `'script'` or `'module'`, and there’s no way
@@ -809,8 +803,8 @@ exports.Block = class Block extends Base
809803
# then CoffeeScript can copy Node’s algorithm.
810804

811805
# sourceType: 'module'
812-
body: @bodyToAst()
813-
directives: []
806+
body: body
807+
directives: [] # Directives like `'use strict'` are coming soon.
814808

815809
#### Literal
816810

@@ -4756,14 +4750,6 @@ makeDelimitedLiteral = (body, options = {}) ->
47564750
when other then (if options.double then "\\#{other}" else other)
47574751
"#{options.delimiter}#{body}#{options.delimiter}"
47584752
4759-
# Extract location data fields from an AST node.
4760-
extractAstLocationData = (ast) ->
4761-
return
4762-
loc: ast.loc
4763-
range: ast.range
4764-
start: ast.start
4765-
end: ast.end
4766-
47674753
# Helpers for `mergeLocationData` and `mergeAstLocationData` below.
47684754
lesser = (a, b) -> if a < b then a else b
47694755
greater = (a, b) -> if a > b then a else b

0 commit comments

Comments
 (0)