-
Notifications
You must be signed in to change notification settings - Fork 2k
[CS2] CSX spread attributes: <div {props…} /> #4607
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 16 commits
da086b9
8841e47
a6172c0
c62be6b
91a294b
4a318fc
2d06fb1
e3d1546
00861f6
df30657
3bed4a4
9da14a2
5908448
437ff70
b097188
e86285b
7de2eb9
ff8d4f2
bd3fd67
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -880,7 +880,19 @@ exports.Call = class Call extends Base | |
content?.base.csx = yes | ||
fragments = [@makeCode('<')] | ||
fragments.push (tag = @variable.compileToFragments(o, LEVEL_ACCESS))... | ||
fragments.push attributes.compileToFragments(o, LEVEL_PAREN)... | ||
if attributes.base instanceof Arr | ||
for obj in attributes.base.objects | ||
attr = obj.base | ||
attrProps = attr?.properties or [] | ||
# Catch invalid CSX attributes: <div {a:"b", props} {props} "value" /> | ||
if not (attr instanceof Obj or attr instanceof IdentifierLiteral) or (attr instanceof Obj and not attr.generated and (attrProps.length > 1 or not (attrProps[0] instanceof Splat))) | ||
obj.error """ | ||
Unexpected token. Allowed CSX attributes are: id="val", src={source}, {props...} or attribute. | ||
Example: <div id="val" src={getTheSource()} {props...} checked>hello world</div>. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don’t normally put examples in error messages. Please update the docs with this if the docs are insufficient. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll remove it. |
||
""" | ||
obj.base.csx = yes if obj.base instanceof Obj | ||
fragments.push @makeCode ' ' | ||
fragments.push obj.compileToFragments(o, LEVEL_PAREN)... | ||
if content | ||
fragments.push @makeCode('>') | ||
fragments.push content.compileNode(o, LEVEL_LIST)... | ||
|
@@ -1171,11 +1183,14 @@ exports.Obj = class Obj extends Base | |
node.error 'cannot have an implicit value in an implicit object' | ||
|
||
# Object spread properties. https://github.com/tc39/proposal-object-rest-spread/blob/master/Spread.md | ||
return @compileSpread o if @hasSplat() | ||
return @compileSpread o if @hasSplat() and not @csx | ||
|
||
idt = o.indent += TAB | ||
lastNoncom = @lastNonComment @properties | ||
|
||
# CSX attributes <div id="val" attr={aaa} {props...} /> | ||
return @compileCSXAttributes o if @csx | ||
|
||
# If this object is the left-hand side of an assignment, all its children | ||
# are too. | ||
if @lhs | ||
|
@@ -1189,19 +1204,17 @@ exports.Obj = class Obj extends Base | |
|
||
isCompact = yes | ||
for prop in @properties | ||
if prop instanceof Comment or (prop instanceof Assign and prop.context is 'object' and not @csx) | ||
if prop instanceof Comment or (prop instanceof Assign and prop.context is 'object') | ||
isCompact = no | ||
|
||
answer = [] | ||
answer.push @makeCode if isCompact then '' else '\n' | ||
for prop, i in props | ||
join = if i is props.length - 1 | ||
'' | ||
else if isCompact and @csx | ||
' ' | ||
else if isCompact | ||
', ' | ||
else if prop is lastNoncom or prop instanceof Comment or @csx | ||
else if prop is lastNoncom or prop instanceof Comment | ||
'\n' | ||
else | ||
',\n' | ||
|
@@ -1226,12 +1239,10 @@ exports.Obj = class Obj extends Base | |
else if not prop.bareLiteral?(IdentifierLiteral) | ||
prop = new Assign prop, prop, 'object' | ||
if indent then answer.push @makeCode indent | ||
prop.csx = yes if @csx | ||
answer.push @makeCode ' ' if @csx and i is 0 | ||
answer.push prop.compileToFragments(o, LEVEL_TOP)... | ||
if join then answer.push @makeCode join | ||
answer.push @makeCode if isCompact then '' else "\n#{@tab}" | ||
answer = @wrapInBraces answer if not @csx | ||
answer = @wrapInBraces answer | ||
if @front then @wrapInParentheses answer else answer | ||
|
||
assigns: (name) -> | ||
|
@@ -1266,7 +1277,18 @@ exports.Obj = class Obj extends Base | |
addSlice() | ||
slices.unshift new Obj unless slices[0] instanceof Obj | ||
(new Call new Literal('Object.assign'), slices).compileToFragments o | ||
|
||
|
||
compileCSXAttributes: (o) -> | ||
props = @properties | ||
answer = [] | ||
for prop, i in props | ||
prop.csx = yes | ||
join = if i is props.length - 1 then '' else ' ' | ||
prop = new Literal "{#{prop.compile(o)}}" if prop instanceof Splat | ||
answer.push prop.compileToFragments(o, LEVEL_TOP)... | ||
answer.push @makeCode join | ||
if @front then @wrapInParentheses answer else answer | ||
|
||
#### Arr | ||
|
||
# An array literal. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure what the right fix here is, but because this is limited to a single state, alternately nesting assignment and splats causes errors:
I suppose that's what
@pair
is for, but that will currently throw if it doesn't match... It would be nice to avoid a new stack though...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@connec the latest commit fixes this bug.