Skip to content

Commit

Permalink
Restrict implicit export of final mapping expression (fix #91)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: In .js/.coffee mapping files, for the final expression
to be implicitly exported, it must be an object expression (literal) or
function expression.

The idea is that such an expression wouldn't do much by itself.
(In particular, it cannot be an assignment or function call.
We were getting too many false positives in this case.)
  • Loading branch information
edemaine committed Sep 16, 2022
1 parent f8e0cd1 commit a9fb0e8
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 13 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,9 @@ The code specifies a `mapping` in one of three ways:

1. `export default mapping` (ECMAScript modules style)
2. `exports.default = mapping` (CommonJS modules style)
3. Writing a `mapping` expression at the end of the file (implicit export).
3. Writing a `mapping` expression at the end of the file (implicit export),
which must be a top-level object or function expression
(without e.g. being assigned to a variable).

In any case, `mapping` should be one of the following types of **mapping**
objects:
Expand Down
2 changes: 0 additions & 2 deletions examples/chess/graph.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,3 @@ svgtiler.afterRender (render) ->
/>

<svg>{edges}</svg>

null
2 changes: 1 addition & 1 deletion examples/mario/mario.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ map =
spiny_left: <symbol viewBox="0 0 16 16" z-index="1"><image y="1" xlink:href="spiny_left.png"/></symbol>
spiny_right: <symbol viewBox="0 0 16 16" z-index="1"><image y="1" xlink:href="spiny_right.png"/></symbol>

build = (key) ->
export default build = (key) ->
### Keys:
"a,b" expands to the equivalent of a beneath b
"a+x+y" expands to a shifted by (x, y)
Expand Down
1 change: 0 additions & 1 deletion examples/mario/palette_castle.js
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
share.palette = 'castle'
null
1 change: 0 additions & 1 deletion examples/mario/palette_overworld.js
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
share.palette = 'overworld'
null
1 change: 0 additions & 1 deletion examples/mario/palette_underground.js
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
share.palette = 'underground'
null
1 change: 0 additions & 1 deletion examples/mario/palette_underwater.js
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
share.palette = 'underwater'
null
15 changes: 10 additions & 5 deletions src/svgtiler.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,16 @@ unless window?
if types.isExportDefaultDeclaration part
return # already an export default, so don't add one
last = body[body.length-1]
if last.node.type == 'ExpressionStatement'
exportLast = types.exportDefaultDeclaration last.node.expression
exportLast.leadingComments = last.node.leadingComments
exportLast.innerComments = last.node.innerComments
exportLast.trailingComments = last.node.trailingComments
lastNode = last.node
if types.isExpressionStatement(last) and (
types.isObjectExpression(lastNode.expression) or
types.isFunctionExpression(lastNode.expression)
# not AssignmentExpression
)
exportLast = types.exportDefaultDeclaration lastNode.expression
exportLast.leadingComments = lastNode.leadingComments
exportLast.innerComments = lastNode.innerComments
exportLast.trailingComments = lastNode.trailingComments
last.replaceWith exportLast
undefined

Expand Down

0 comments on commit a9fb0e8

Please sign in to comment.