Skip to content

Commit a34c0c9

Browse files
helixbassGeoffreyBooth
authored andcommitted
Return Root from nodes() (#5274)
* return Root from nodes() * add children
1 parent 2ab714b commit a34c0c9

File tree

8 files changed

+96
-87
lines changed

8 files changed

+96
-87
lines changed

lib/coffeescript/coffeescript.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/coffeescript/nodes.js

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

lib/coffeescript/repl.js

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

src/coffeescript.coffee

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ exports.tokens = withPrettyErrors (code, options) ->
209209
# or traverse it by using `.traverseChildren()` with a callback.
210210
exports.nodes = withPrettyErrors (source, options) ->
211211
source = lexer.tokenize source, options if typeof source is 'string'
212-
parser.parse(source).body
212+
parser.parse source
213213

214214
# This file used to export these methods; leave stubs that throw warnings
215215
# instead. These methods have been moved into `index.coffee` to provide

src/nodes.coffee

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,8 @@ exports.Root = class Root extends Base
512512
constructor: (@body) ->
513513
super()
514514

515+
children: ['body']
516+
515517
# Wrap everything in a safety closure, unless requested not to. It would be
516518
# better not to generate them in the first place, but for now, clean up
517519
# obvious double-parentheses.

src/repl.coffee

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ replDefaults =
2525
input = input.replace /^\s*try\s*{([\s\S]*)}\s*catch.*$/m, '$1'
2626

2727
# Require AST nodes to do some AST manipulation.
28-
{Block, Assign, Value, Literal, Call, Code} = require './nodes'
28+
{Block, Assign, Value, Literal, Call, Code, Root} = require './nodes'
2929

3030
try
3131
# Tokenize the clean input.
@@ -41,14 +41,14 @@ replDefaults =
4141
# Collect referenced variable names just like in `CoffeeScript.compile`.
4242
referencedVars = (token[1] for token in tokens when token[0] is 'IDENTIFIER')
4343
# Generate the AST of the tokens.
44-
ast = CoffeeScript.nodes tokens
44+
ast = CoffeeScript.nodes(tokens).body
4545
# Add assignment to `__` variable to force the input to be an expression.
4646
ast = new Block [new Assign (new Value new Literal '__'), ast, '=']
4747
# Wrap the expression in a closure to support top-level `await`.
4848
ast = new Code [], ast
4949
isAsync = ast.isAsync
5050
# Invoke the wrapping closure.
51-
ast = new Block [new Call ast]
51+
ast = new Root new Block [new Call ast]
5252
js = ast.compile {bare: yes, locals: Object.keys(context), referencedVars, sharedScope: yes}
5353
if transpile
5454
js = transpile.transpile(js, transpile.options).code

test/location.coffee

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ test 'Values with properties end up with a location that includes the properties
626626
a['b']
627627
a[b.c()].d
628628
'''
629-
block = CoffeeScript.nodes source
629+
{body: block} = CoffeeScript.nodes source
630630
[
631631
singleProperty
632632
twoProperties
@@ -656,7 +656,7 @@ test 'Values with properties end up with a location that includes the properties
656656

657657
test 'StringWithInterpolations::fromStringLiteral() assigns correct location to tagged template literal', ->
658658
checkLocationData = (source, {stringWithInterpolationsLocationData, stringLocationData}) ->
659-
block = CoffeeScript.nodes source
659+
{body: block} = CoffeeScript.nodes source
660660
taggedTemplateLiteral = block.expressions[0].unwrap()
661661
{args: [stringWithInterpolations]} = taggedTemplateLiteral
662662
{body} = stringWithInterpolations

test/parser.coffee

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ test "operator precedence for logical operators", ->
55
source = '''
66
a or b and c
77
'''
8-
block = CoffeeScript.nodes source
8+
{body: block} = CoffeeScript.nodes source
99
[expression] = block.expressions
1010
eq expression.first.base.value, 'a'
1111
eq expression.operator, '||'
@@ -17,7 +17,7 @@ test "operator precedence for bitwise operators", ->
1717
source = '''
1818
a | b ^ c & d
1919
'''
20-
block = CoffeeScript.nodes source
20+
{body: block} = CoffeeScript.nodes source
2121
[expression] = block.expressions
2222
eq expression.first.base.value, 'a'
2323
eq expression.operator, '|'
@@ -31,7 +31,7 @@ test "operator precedence for binary ? operator", ->
3131
source = '''
3232
a ? b and c
3333
'''
34-
block = CoffeeScript.nodes source
34+
{body: block} = CoffeeScript.nodes source
3535
[expression] = block.expressions
3636
eq expression.first.base.value, 'a'
3737
eq expression.operator, '?'
@@ -43,7 +43,7 @@ test "new calls have a range including the new", ->
4343
source = '''
4444
a = new B().c(d)
4545
'''
46-
block = CoffeeScript.nodes source
46+
{body: block} = CoffeeScript.nodes source
4747

4848
assertColumnRange = (node, firstColumn, lastColumn) ->
4949
eq node.locationData.first_line, 0
@@ -65,7 +65,7 @@ test "location data is properly set for nested `new`", ->
6565
source = '''
6666
new new A()()
6767
'''
68-
block = CoffeeScript.nodes source
68+
{body: block} = CoffeeScript.nodes source
6969

7070
assertColumnRange = (node, firstColumn, lastColumn) ->
7171
eq node.locationData.first_line, 0

0 commit comments

Comments
 (0)