Skip to content

Commit

Permalink
Merge pull request #22 from adamzapasnik/fix-form-fors
Browse files Browse the repository at this point in the history
Fix formatting of regular form_for expressions
  • Loading branch information
adamzapasnik authored Feb 24, 2021
2 parents 3d1026a + ac98e9a commit c9d6d50
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 3 deletions.
4 changes: 3 additions & 1 deletion lib/liveview.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ const liveViewFormForPlaceholder = '<form>';
const encodeFormForExpressions = (text) => {
const liveViewFormExpressions = [];
const textWithPlaceholders = text.replace(/(<%=?[^%>]*=\s+form_for[\s\S]*?%>|<form>)/gm, (match) => {
if (match.match(/->\s*%>$/gm)) {
return match;
}
liveViewFormExpressions.push(match);

return liveViewFormForPlaceholder;
});

Expand Down
7 changes: 6 additions & 1 deletion lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ const expressionTypeMatcher = require('./expression_type_matcher');
const { encodeFormForExpressions } = require('./liveview');

function parse(text, parsers, options) {
const { liveViewFormExpressions, textWithPlaceholders } = encodeFormForExpressions(text);
let liveViewFormExpressions = [];
let textWithPlaceholders = text;

if (options.filepath.endsWith('.leex') && text.includes('</form>')) {
({ liveViewFormExpressions, textWithPlaceholders } = encodeFormForExpressions(text));
}

return {
tokens: tokenizeHTML(textWithPlaceholders, /<%[\s\S]*?%>/gm, expressionTypeMatcher),
Expand Down
2 changes: 1 addition & 1 deletion lib/printers.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function embed(path, _print, textToDoc, options) {
const callback = decodeExpressions(expressionMap);
return mapDoc(htmlDoc, (doc) => {
let newDoc = doc;
if (doc === liveViewFormForPlaceholder) {
if (doc === liveViewFormForPlaceholder && liveViewFormExpressions.length) {
newDoc = liveViewFormExpressions.shift();
}

Expand Down
36 changes: 36 additions & 0 deletions tests/expressions/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`form_for.html.eex 1`] = `
====================================options=====================================
parsers: ["eex"]
printWidth: 80
| printWidth
=====================================input======================================
<div>
<%= f = form_for a, fn -> %>
<%= text_input f, :name %>
<% end %>
<div>
<form></form>
</div>
</div>
=====================================output=====================================
<div>
<%= f = form_for a, fn -> %>
<%= text_input f, :name %>
<% end %>
<div>
<form></form>
</div>
</div>
================================================================================
`;

exports[`liveview_form_for.html.leex 1`] = `
====================================options=====================================
parsers: ["eex"]
Expand All @@ -23,6 +51,10 @@ printWidth: 80
</form>
</div>
</div>
<%= f = form_for a, fn -> %>
<%= text_input f, :name %>
<% end %>
</div>
=====================================output=====================================
Expand All @@ -43,6 +75,10 @@ printWidth: 80
</form>
</div>
</div>
<%= f = form_for a, fn -> %>
<%= text_input f, :name %>
<% end %>
</div>
================================================================================
Expand Down
8 changes: 8 additions & 0 deletions tests/expressions/form_for.html.eex
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<div>
<%= f = form_for a, fn -> %>
<%= text_input f, :name %>
<% end %>
<div>
<form></form>
</div>
</div>
4 changes: 4 additions & 0 deletions tests/expressions/liveview_form_for.html.leex
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@
</form>
</div>
</div>

<%= f = form_for a, fn -> %>
<%= text_input f, :name %>
<% end %>
</div>

0 comments on commit c9d6d50

Please sign in to comment.