Skip to content

Commit 9ba390d

Browse files
committed
Add single-line comment syntax to CJSX [Fixes #40]
Before this change it was not possible to comment inside CJSX templates. * #40 * jsdf/coffee-react#13 After this change, you can use the syntax: {#this is a cjsx comment} The syntax was designed to look like a CJSX_ESCAPE that contains a comment, but as the resulting coffee-script would be illegal this is special-cased by the parser to return nothing. Future work would be needed make this syntax work inside a tag: <Person {#comment} /> or to make multiline comments work: <Person>{### ###}</Person>
1 parent 3dcfd2b commit 9ba390d

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

src/parser.coffee

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ module.exports = class Parser
3131
) or
3232
@cjsxStart() or
3333
@cjsxAttribute() or
34+
@cjsxComment() or
3435
@cjsxEscape() or
3536
@cjsxUnescape() or
3637
@cjsxEnd() or
@@ -207,6 +208,13 @@ module.exports = class Parser
207208
"Invalid attribute #{input} in CJSX tag #{@peekActiveState(2).value}",
208209
first_line: @chunkLine, first_column: @chunkColumn
209210

211+
cjsxComment: ->
212+
match = @chunk.match(/^\{#(.*)\}/)
213+
214+
return 0 unless match
215+
@addLeafNodeToActiveBranch ParseTreeLeafNode($.CJSX_COMMENT, match[1])
216+
return match[0].length
217+
210218
cjsxEscape: ->
211219
return 0 unless @chunk.charAt(0) is '{' and
212220
@currentState() in [$.CJSX_EL, $.CJSX_ATTR_PAIR]

src/serialiser.coffee

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@ nodeSerialisers =
157157
element = node.value
158158
"#{@reactObject}.createElement(#{element}, #{joinList(serialisedChildren)})"
159159

160+
CJSX_COMMENT: (node) ->
161+
""
162+
160163
CJSX_ESC: (node) ->
161164
childrenSerialised = node.children
162165
.map((child) => @serialiseNode child)

src/symbols.coffee

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ module.exports =
2626
# they're just names for use in debugging
2727
CJSX_START: 'CJSX_START'
2828
CJSX_END: 'CJSX_END'
29+
CJSX_COMMENT: 'CJSX_COMMENT'
2930
CJSX_ESC_START: 'CJSX_ESC_START'
3031
CJSX_ESC_END: 'CJSX_ESC_END'
3132
CJSX_PRAGMA: 'CJSX_PRAGMA'

test/output-testcases.txt

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,52 @@ React.createElement(Person, null, """
477477
""")
478478
##end
479479

480+
##desc
481+
empty node is handled as expected
482+
##input
483+
<Person>
484+
</Person>
485+
##expected
486+
React.createElement(Person, null
487+
)
488+
##end
489+
490+
##desc
491+
cs comment at start of cjsx escape
492+
##input
493+
<Person>
494+
{# i am a comment
495+
"i am a string"
496+
}
497+
</Person>
498+
##expected
499+
React.createElement(Person, null,
500+
(# i am a comment
501+
"i am a string"
502+
)
503+
)
504+
##end
505+
506+
##desc
507+
cjsx comment is passed through
508+
##input
509+
<Person>
510+
{# i am a comment}
511+
</Person>
512+
##expected
513+
React.createElement(Person, null,
514+
515+
)
516+
##end
517+
518+
##desc
519+
comment syntax can be used inline
520+
##input
521+
<Person>{#comment inline}</Person>
522+
##expected
523+
React.createElement(Person, null, )
524+
##end
525+
480526
##desc
481527
string within cjsx is ignored by parser and escaped
482528
##input

0 commit comments

Comments
 (0)