Skip to content

Commit

Permalink
Fix typo (#170)
Browse files Browse the repository at this point in the history
* fix: component only edge case

* test: update tests to hopefully catch more edge cases

* chore: fix lint
  • Loading branch information
natemoo-re authored Nov 23, 2021
1 parent a1bdbf7 commit eb0d17f
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/early-tips-lick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/compiler': patch
---

Fix edge case with files that contain a single component
2 changes: 1 addition & 1 deletion internal/transform/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func hasSiblings(n *tycho.Node) bool {

if sibling := n.PrevSibling; sibling != nil {
if sibling.Type == tycho.TextNode {
return strings.TrimSpace(n.NextSibling.Data) != ""
return strings.TrimSpace(n.PrevSibling.Data) != ""
} else {
return sibling.Type != tycho.CommentNode
}
Expand Down
5 changes: 5 additions & 0 deletions internal/transform/transform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ func TestFullTransform(t *testing.T) {
source: `<Component />`,
want: `<Component></Component>`,
},
{
name: "",
source: `<style></style><A><div><B /></div></A>`,
want: `<A><div><B></B></div></A>`,
},
}
var b strings.Builder
for _, tt := range tests {
Expand Down
62 changes: 62 additions & 0 deletions lib/compiler/test/component-only.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/* eslint-disable no-console */

import { transform } from '@astrojs/compiler';
import sass from 'sass';

function transformSass(value) {
return new Promise((resolve, reject) => {
sass.render({ data: value }, (err, result) => {
if (err) {
reject(err);
return;
}
resolve({ code: result.css.toString('utf8'), map: result.map });
return;
});
});
}

async function run() {
const result = await transform(
`---
import { Markdown } from 'astro/components';
import Layout from '../layouts/content.astro';
---
<style>
#root {
color: green;
}
</style>
<Layout>
<div id="root">
<Markdown>
## Interesting Topic
</Markdown>
</div>
</Layout>`, // NOTE: the lack of trailing space is important to this test!
{
sourcemap: true,
preprocessStyle: async (value, attrs) => {
if (attrs.lang === 'scss') {
try {
return transformSass(value);
} catch (err) {
console.error(err);
}
}
return null;
},
as: 'document',
}
);

console.log(result.code);

if (result.code.includes('html')) {
throw new Error('Result did not remove <html>');
}
}

await run();
4 changes: 2 additions & 2 deletions lib/compiler/test/empty-style.test.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable no-console */

import { transform } from '@astrojs/compiler';
import sass from 'sass';

Expand Down Expand Up @@ -43,8 +45,6 @@ let value = 'world';
},
}
);

console.log(result);
}

await run();
1 change: 1 addition & 0 deletions lib/compiler/test/test.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import './basic.test.mjs';
import './component-only.test.mjs';
import './empty-style.test.mjs';
import './output.test.mjs';

0 comments on commit eb0d17f

Please sign in to comment.