-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: `only` helper for json test * fix: remove divergence from html spec * test: add test for #712 * `addLoc` before popping the stack of oe * test: add test * literal parsing after `body` and `html` * test: add test for style tag after body * test: add tsx tests * test: add missing semicolon in test * test: add more tests from duplicates * nit: formatting * chore: changeset * test: add AST tests --------- Co-authored-by: Nate Moore <[email protected]> Co-authored-by: Erika <[email protected]>
- Loading branch information
1 parent
d1ba06d
commit 9549bb7
Showing
5 changed files
with
180 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@astrojs/compiler": patch | ||
--- | ||
|
||
Fixes style and script tags sometimes being forcefully put into the body / head tags in the AST |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { parse } from '@astrojs/compiler'; | ||
import { test } from 'uvu'; | ||
import * as assert from 'uvu/assert'; | ||
import type { ElementNode } from '../../types.js'; | ||
|
||
test('preserve style tag position I', async () => { | ||
const input = `<html><body><h1>Hello world!</h1></body></html> | ||
<style></style>`; | ||
const { ast } = await parse(input); | ||
|
||
const lastChildren = ast.children.at(-1) as ElementNode; | ||
|
||
assert.equal(lastChildren.type, 'element', 'Expected last child node to be of type "element"'); | ||
assert.equal(lastChildren.name, 'style', 'Expected last child node to be of type "style"'); | ||
}); | ||
|
||
test('preserve style tag position II', async () => { | ||
const input = `<html></html> | ||
<style></style>`; | ||
const { ast } = await parse(input); | ||
|
||
const lastChildren = ast.children.at(-1) as ElementNode; | ||
|
||
assert.equal(lastChildren.type, 'element', 'Expected last child node to be of type "element"'); | ||
assert.equal(lastChildren.name, 'style', 'Expected last child node to be of type "style"'); | ||
}); | ||
|
||
test('preserve style tag position III', async () => { | ||
const input = `<html lang="en"><head><BaseHead /></head></html> | ||
<style>@use "../styles/global.scss";</style>`; | ||
const { ast } = await parse(input); | ||
|
||
const lastChildren = ast.children.at(-1) as ElementNode; | ||
|
||
assert.equal(lastChildren.type, 'element', 'Expected last child node to be of type "element"'); | ||
assert.equal(lastChildren.name, 'style', 'Expected last child node to be of type "style"'); | ||
assert.equal( | ||
lastChildren.children[0].type, | ||
'text', | ||
'Expected last child node to be of type "text"' | ||
); | ||
}); | ||
|
||
test('preserve style tag position IV', async () => { | ||
const input = `<html lang="en"><head><BaseHead /></head><body><Header /></body></html> | ||
<style>@use "../styles/global.scss";</style>`; | ||
const { ast } = await parse(input); | ||
|
||
const lastChildren = ast.children.at(-1) as ElementNode; | ||
|
||
assert.equal(lastChildren.type, 'element', 'Expected last child node to be of type "element"'); | ||
assert.equal(lastChildren.name, 'style', 'Expected last child node to be of type "style"'); | ||
assert.equal( | ||
lastChildren.children[0].type, | ||
'text', | ||
'Expected last child node to be of type "text"' | ||
); | ||
}); | ||
|
||
test('preserve style tag position V', async () => { | ||
const input = `<html lang="en"><head><BaseHead /></head><body><Header /></body><style>@use "../styles/global.scss";</style></html>`; | ||
const { ast } = await parse(input); | ||
|
||
const firstChild = ast.children.at(0) as ElementNode; | ||
const lastChild = firstChild.children.at(-1) as ElementNode; | ||
|
||
assert.equal(lastChild.type, 'element', 'Expected last child node to be of type "element"'); | ||
assert.equal(lastChild.name, 'style', 'Expected last child node to be of type "style"'); | ||
assert.equal(lastChild.children[0].type, 'text', 'Expected last child node to be of type "text"'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { convertToTSX } from '@astrojs/compiler'; | ||
import { test } from 'uvu'; | ||
import * as assert from 'uvu/assert'; | ||
import { TSXPrefix } from '../utils.js'; | ||
|
||
test('preserve style tag position I', async () => { | ||
const input = `<html><body><h1>Hello world!</h1></body></html> | ||
<style></style>`; | ||
const output = `${TSXPrefix}<Fragment> | ||
<html><body><h1>Hello world!</h1></body></html> | ||
<style></style> | ||
</Fragment> | ||
export default function __AstroComponent_(_props: Record<string, any>): any {}\n`; | ||
const { code } = await convertToTSX(input, { sourcemap: 'external' }); | ||
assert.snapshot(code, output, 'expected code to match snapshot'); | ||
}); | ||
|
||
test('preserve style tag position II', async () => { | ||
const input = `<html></html> | ||
<style></style>`; | ||
const output = `${TSXPrefix}<Fragment> | ||
<html></html> | ||
<style></style> | ||
</Fragment> | ||
export default function __AstroComponent_(_props: Record<string, any>): any {}\n`; | ||
const { code } = await convertToTSX(input, { sourcemap: 'external' }); | ||
assert.snapshot(code, output, 'expected code to match snapshot'); | ||
}); | ||
|
||
test('preserve style tag position III', async () => { | ||
const input = `<html lang="en"><head><BaseHead /></head></html> | ||
<style>@use "../styles/global.scss";</style>`; | ||
const output = `${TSXPrefix}<Fragment> | ||
<html lang="en"><head><BaseHead /></head></html> | ||
<style>{\`@use "../styles/global.scss";\`}</style> | ||
</Fragment> | ||
export default function __AstroComponent_(_props: Record<string, any>): any {}\n`; | ||
const { code } = await convertToTSX(input, { sourcemap: 'external' }); | ||
assert.snapshot(code, output, 'expected code to match snapshot'); | ||
}); | ||
|
||
test('preserve style tag position IV', async () => { | ||
const input = `<html lang="en"><head><BaseHead /></head><body><Header /></body></html> | ||
<style>@use "../styles/global.scss";</style>`; | ||
const output = `${TSXPrefix}<Fragment> | ||
<html lang="en"><head><BaseHead /></head><body><Header /></body></html> | ||
<style>{\`@use "../styles/global.scss";\`}</style> | ||
</Fragment> | ||
export default function __AstroComponent_(_props: Record<string, any>): any {}\n`; | ||
const { code } = await convertToTSX(input, { sourcemap: 'external' }); | ||
assert.snapshot(code, output, 'expected code to match snapshot'); | ||
}); | ||
|
||
test('preserve style tag position V', async () => { | ||
const input = `<html lang="en"><head><BaseHead /></head><body><Header /></body><style>@use "../styles/global.scss";</style></html>`; | ||
const output = `${TSXPrefix}<Fragment> | ||
<html lang="en"><head><BaseHead /></head><body><Header /></body><style>{\`@use "../styles/global.scss";\`}</style></html> | ||
</Fragment> | ||
export default function __AstroComponent_(_props: Record<string, any>): any {}\n`; | ||
const { code } = await convertToTSX(input, { sourcemap: 'external' }); | ||
assert.snapshot(code, output, 'expected code to match snapshot'); | ||
}); | ||
|
||
test.run(); |