Skip to content

Commit

Permalink
Preserve tag case (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
antonyfaris authored Oct 3, 2021
1 parent 333a10a commit 4b76988
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 7 deletions.
12 changes: 11 additions & 1 deletion src/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ const print = (path, opts, print) => {
}
// If we don't see any JSX expressions, this is just embedded HTML
// and we can skip a bunch of work. Hooray!
if (text.indexOf('{') === -1) {
const hasInlineComponent = node.children.filter((x) => x.type === 'InlineComponent').length > 0;
if (text.indexOf('{') === -1 && !hasInlineComponent) {
node.__isRawHTML = true;
node.content = text;
return path.call(print);
Expand Down Expand Up @@ -395,6 +396,15 @@ const embed = (path, print, textToDoc, opts) => {
return group(['---', hardline, textToDoc(node.content, { ...opts, parser: 'typescript' }), '---', hardline]);
}

// format <script type="module"> content
if (node.type === 'Text') {
const parent = path.getParentNode();
if (parent.type === 'Element' && parent.name === 'script') {
const [formatttedScript, _] = textToDoc(node.data, { ...opts, parser: 'typescript' });
return group(formatttedScript);
}
}

if (node.type === 'Style') {
let styleLang = '';
let parserLang = '';
Expand Down
2 changes: 1 addition & 1 deletion test/astro-prettier.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ test.failing('an Astro file with an invalidly unclosed tag is still formatted',

test.todo('test whether invalid files provide helpful support messages / still try to be parsed by prettier?');

test.failing('can format an Astro file with components that are the uppercase version of html elements', PrettierUnaltered, 'preserve-tag-case');
test('can format an Astro file with components that are the uppercase version of html elements', Prettier, 'preserve-tag-case');

test.failing('Autocloses open tags? BUG: RangeError { message: "Maximum call stack size exceeded" }', Prettier, 'maximum-call-size-exceeded');

Expand Down
7 changes: 5 additions & 2 deletions test/fixtures/in/preserve-tag-case.astro
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
---
import Footer from "./Footer.astro";
import {Body} from "lib";
---
<Footer>this is a footer component</Footer>
<footer>this is a regular footer element</footer>

<Footer>this is a footer component</Footer>
<footer>this is a regular footer element</footer>
<Body></Body>
7 changes: 5 additions & 2 deletions test/fixtures/in/with-script.astro
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<script type="module">
import confetti from 'https://cdn.skypack.dev/canvas-confetti';

confetti();
console.log("lorem")
function foo() {
return 0
}
confetti();
</script>
2 changes: 2 additions & 0 deletions test/fixtures/out/preserve-tag-case.astro
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
---
import Footer from "./Footer.astro";
import { Body } from "lib";
---

<Footer>this is a footer component</Footer>
<footer>this is a regular footer element</footer>
<Body />
5 changes: 4 additions & 1 deletion test/fixtures/out/with-script.astro
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<script type="module">
import confetti from "https://cdn.skypack.dev/canvas-confetti";

console.log("lorem");
function foo() {
return 0;
}
confetti();
</script>

0 comments on commit 4b76988

Please sign in to comment.