Skip to content

Commit

Permalink
Merge pull request #59 from Shinyaigeek/update/fix-test
Browse files Browse the repository at this point in the history
Fix: handling self closing tag
  • Loading branch information
Shinyaigeek authored Apr 18, 2021
2 parents bb8f73d + cee1a58 commit 80a86e0
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const SelfClosing = () => <input type="text" className="test" />;
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,30 @@ describe("convertJsxElementToTemplateLiteral", () => {
<div>asdf</div>
\`;`);
});

test("self closing jsx", () => {
const jsx = extractJsxElementFromSouceFile(
join(__dirname, "./__tests__/SelfClosing.tsx")
)[0];

// TODO fix type
const converter = new ConvertJSXElementToTemplateLiteral(jsx!, "" as any);

converter.traverse();

const raw = generate(
file(
program([
variableDeclaration("const", [
variableDeclarator(
identifier("SelfClosing"),
arrowFunctionExpression([], converter.render())
),
]),
])
)
).code;

expect(raw).toBe(`const SelfClosing = () => \`<input type="text" class="test" />\`;`);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,11 @@ export class ConvertJSXElementToTemplateLiteral {
}
});

this.query += " />";
if (jsx.openingElement.selfClosing) {
this.query += " />";
} else {
this.query += ">";
}

if (this.unsafeMarkup) {
this.queries.push(
Expand Down Expand Up @@ -265,8 +269,9 @@ export class ConvertJSXElementToTemplateLiteral {
"jsx closingElement shouldn't be jsx member expression"
);
}

this.query += `</${jsx.openingElement.name.name}>`;
if (!jsx.openingElement.selfClosing) {
this.query += `</${jsx.openingElement.name.name}>`;
}
}
}

Expand Down

0 comments on commit 80a86e0

Please sign in to comment.