Skip to content

Commit d2d25f5

Browse files
committed
cjsx comments are stripped out
1 parent e99dd28 commit d2d25f5

File tree

4 files changed

+8
-6
lines changed

4 files changed

+8
-6
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Car = React.createClass
1313
<Parts.FrontSeat />
1414
<Parts.BackSeat />
1515
<p className="seat">Which seat can I take? {@props?.seat or 'none'}</p>
16+
{# also, this is a comment }
1617
</Vehicle>
1718
```
1819

src/parser.coffee

+3-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ module.exports = class Parser
209209
first_line: @chunkLine, first_column: @chunkColumn
210210

211211
cjsxComment: ->
212-
match = @chunk.match(/^\{#(.*)\}/)
212+
match = @chunk.match(CJSX_ESC_COMMENT)
213213

214214
return 0 unless match
215215
@addLeafNodeToActiveBranch ParseTreeLeafNode($.CJSX_COMMENT, match[1])
@@ -423,6 +423,8 @@ TAG_ATTRIBUTES = ///
423423

424424
PRAGMA = /^\s*#\s*@cjsx\s+(\S*)/i
425425

426+
CJSX_ESC_COMMENT = /^\{#(.*)\}/
427+
426428
# from coffeescript lexer
427429

428430
# The character code of the nasty Microsoft madness otherwise known as the BOM.

src/serialiser.coffee

+2-3
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ class Serialiser
8383
# to ensure line number parity
8484

8585
# sort children into whitespace and semantic (non whitespace) groups
86-
# this seems wrong :\
8786
[whitespaceChildren, semanticChildren] = children.reduce((partitionedChildren, child) ->
8887
if child.type is $.CJSX_WHITESPACE
8988
partitionedChildren[0].push child
@@ -141,7 +140,7 @@ nodeSerialisers =
141140
for child in node.children
142141
serialisedChild = @serialiseNode child
143142
if child? # filter empty text nodes
144-
if WHITESPACE_ONLY.test serialisedChild
143+
if serialisedChild.length is 0 or WHITESPACE_ONLY.test serialisedChild
145144
accumulatedWhitespace += serialisedChild
146145
else
147146
serialisedChildren.push(accumulatedWhitespace + serialisedChild)
@@ -160,7 +159,7 @@ nodeSerialisers =
160159
"#{@reactObject}.createElement(#{element}, #{joinList(serialisedChildren)})"
161160

162161
CJSX_COMMENT: (node) ->
163-
""
162+
'null'
164163

165164
CJSX_ESC: (node) ->
166165
childrenSerialised = node.children

test/output-testcases.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ cjsx comment is passed through
510510
{# i am a comment}
511511
</Person>
512512
##expected
513-
React.createElement(Person, null,
513+
React.createElement(Person, null
514514

515515
)
516516
##end
@@ -520,7 +520,7 @@ comment syntax can be used inline
520520
##input
521521
<Person>{#comment inline}</Person>
522522
##expected
523-
React.createElement(Person, null, )
523+
React.createElement(Person, null)
524524
##end
525525

526526
##desc

0 commit comments

Comments
 (0)