Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
8a00eb3
chore: enter pre
Jan 24, 2022
41b825a
Treat all documents as fragments which generate no implicit tags (#264)
natemoo-re Jan 24, 2022
8ce39c7
[DO NOT MERGE] add `renderHead` util to output (#265)
natemoo-re Jan 24, 2022
83fcc78
fix transform output
Feb 22, 2022
9e5e2f8
Add `parse` function, emit AST (#274)
natemoo-re Jan 31, 2022
483b34b
chore: add changeset
Jan 31, 2022
77398da
test: update tests for no implicit tags
Feb 15, 2022
845f981
test: update tests for no implicit tags
Feb 15, 2022
70f8026
chore: update tests for no implicit tags
Feb 16, 2022
8a3370b
chore: update build script
Feb 22, 2022
e08f22b
chore: update node bindings
Feb 22, 2022
26d0cd7
chore: update wasm file
Feb 22, 2022
16b167c
feat: expose AST types
Feb 24, 2022
3a2fd89
chore: update tests for no implicit tags
Feb 24, 2022
ef5d599
[ci] release (next) (#288)
github-actions[bot] Feb 24, 2022
a539d53
chore: update exposed types
Feb 24, 2022
bae54e2
[ci] release (next) (#304)
github-actions[bot] Feb 24, 2022
073b0f1
Warn when using an expression on hoist `src` attr (#303)
matthewp Feb 25, 2022
5e321cf
[ci] release (next) (#305)
github-actions[bot] Feb 25, 2022
ef69b74
fix: export `./types`
Feb 25, 2022
3438bf8
[ci] release (next) (#306)
github-actions[bot] Feb 25, 2022
e2061dd
fix: export all shared types
Feb 25, 2022
cc67776
fix: export named types
Feb 25, 2022
8ec2e2a
[ci] release (next) (#307)
github-actions[bot] Feb 25, 2022
97cf66b
Client only metadata (#308)
matthewp Feb 28, 2022
d16bbaa
[ci] release (next) (#309)
github-actions[bot] Feb 28, 2022
8f21318
Fix next `head` behavior (#310)
natemoo-re Mar 1, 2022
37ef1c1
chore: add changeset
Mar 1, 2022
9c05747
test: add parse-ii test
Mar 1, 2022
3f799e3
[ci] release (next) (#311)
github-actions[bot] Mar 1, 2022
e26b9d6
Add `fragment` to AST (#312)
natemoo-re Mar 1, 2022
5c969ff
[ci] release (next) (#313)
github-actions[bot] Mar 1, 2022
4497628
Improve `renderHead` behavior (#315)
natemoo-re Mar 2, 2022
9de7628
[ci] release (next) (#317)
github-actions[bot] Mar 2, 2022
90b2f61
Refactor WASM tests (#316)
natemoo-re Mar 2, 2022
95ec808
refactor: remove escapeHTML usage, fix slot in head behavior
Mar 9, 2022
d751ade
[ci] release (next) (#320)
github-actions[bot] Mar 9, 2022
750da08
fix: failing test
Mar 11, 2022
83189f2
Merge branch 'main' into next
natemoo-re Mar 11, 2022
d10088a
feat: expose fragment to AST (#314)
natemoo-re Mar 11, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/afraid-elephants-live.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/compiler': patch
---

Add `fragment` node types to AST definitions, expose Fragment helper to utils
5 changes: 5 additions & 0 deletions .changeset/chilled-colts-kick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/compiler': patch
---

Adds metadata on client:only components
5 changes: 5 additions & 0 deletions .changeset/long-kids-wait.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/compiler': patch
---

Expose AST types via `@astrojs/compiler/types`
5 changes: 5 additions & 0 deletions .changeset/modern-dots-destroy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/compiler': patch
---

Export `./types` rather than `./types.d.ts`
5 changes: 5 additions & 0 deletions .changeset/poor-maps-fly.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/compiler': patch
---

Fix edge case with Fragment parsing in head, add `fragment` node to AST output
24 changes: 24 additions & 0 deletions .changeset/pre.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"mode": "pre",
"tag": "next",
"initialVersions": {
"@astrojs/compiler": "0.9.2"
},
"changesets": [
"chilled-colts-kick",
"long-kids-wait",
"modern-dots-destroy",
"poor-maps-fly",
"purple-jobs-retire",
"quick-lamps-stare",
"short-tips-begin",
"strange-humans-roll",
"stupid-colts-grab",
"tasty-garlics-call",
"thirty-jobs-jam",
"unlucky-swans-destroy",
"wicked-forks-do",
"wise-shrimps-knock",
"yellow-numbers-leave"
]
}
5 changes: 5 additions & 0 deletions .changeset/purple-jobs-retire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/compiler': patch
---

Fix <slot> behavior inside of head
5 changes: 5 additions & 0 deletions .changeset/quick-lamps-stare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/compiler': patch
---

Improve head injection behavior
5 changes: 5 additions & 0 deletions .changeset/short-tips-begin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/compiler': patch
---

Update exposed types
5 changes: 5 additions & 0 deletions .changeset/strange-humans-roll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/compiler': patch
---

Remove usage of `escapeHTML` util
5 changes: 5 additions & 0 deletions .changeset/stupid-colts-grab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/compiler': patch
---

Export all types from shared types
5 changes: 5 additions & 0 deletions .changeset/tasty-garlics-call.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/compiler': minor
---

Do not render implicit tags created during the parsing process
5 changes: 5 additions & 0 deletions .changeset/thirty-jobs-jam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/compiler': patch
---

Fix `head` behavior and a bug related to ParseFragment
5 changes: 5 additions & 0 deletions .changeset/unlucky-swans-destroy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/compiler': patch
---

Adds a warning when using an expression with a hoisted script
5 changes: 5 additions & 0 deletions .changeset/wicked-forks-do.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/compiler': minor
---

Remove "as" option, treats all documents as fragments that generate no implicit tags
5 changes: 5 additions & 0 deletions .changeset/wise-shrimps-knock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/compiler': minor
---

Add `parse` function which generates an AST
5 changes: 5 additions & 0 deletions .changeset/yellow-numbers-leave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/compiler': minor
---

Adds support for `Astro.self` (as accepted in the [Recursive Components RFC](https://github.com/withastro/rfcs/blob/main/active-rfcs/0000-recursive-components.md)).
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: Test

on:
push:
branches: ['main']
branches: ['main', 'next']
pull_request:
branches: ['main']
branches: ['main', 'next']

# Automatically cancel in-progress actions on the same branch
concurrency:
Expand Down Expand Up @@ -53,7 +53,7 @@ jobs:
run: yarn build:compiler

- name: Test WASM
run: yarn test
run: yarn test:ci

lint:
runs-on: ubuntu-latest
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
push:
branches:
- main
- next

jobs:
release:
Expand Down
70 changes: 40 additions & 30 deletions cmd/astro-wasm/astro-wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import (
"github.com/norunners/vert"
astro "github.com/withastro/compiler/internal"
"github.com/withastro/compiler/internal/printer"
t "github.com/withastro/compiler/internal/t"
"github.com/withastro/compiler/internal/transform"
wasm_utils "github.com/withastro/compiler/internal_wasm/utils"
"golang.org/x/net/html/atom"
)

var done chan bool
Expand All @@ -25,6 +25,7 @@ func main() {
js.Global().Set("@astrojs/compiler", js.ValueOf(make(map[string]interface{})))
module := js.Global().Get("@astrojs/compiler")
module.Set("transform", Transform())
module.Set("parse", Parse())

<-make(chan struct{})
}
Expand All @@ -43,6 +44,19 @@ func jsBool(j js.Value) bool {
return j.Bool()
}

func makeParseOptions(options js.Value) t.ParseOptions {
position := true

pos := options.Get("position")
if !pos.IsNull() && !pos.IsUndefined() {
position = pos.Bool()
}

return t.ParseOptions{
Position: position,
}
}

func makeTransformOptions(options js.Value, hash string) transform.TransformOptions {
filename := jsString(options.Get("sourcefile"))
if filename == "" {
Expand All @@ -54,11 +68,6 @@ func makeTransformOptions(options js.Value, hash string) transform.TransformOpti
pathname = "<stdin>"
}

as := jsString(options.Get("as"))
if as == "" {
as = "document"
}

internalURL := jsString(options.Get("internalURL"))
if internalURL == "" {
internalURL = "astro/internal"
Expand Down Expand Up @@ -87,7 +96,6 @@ func makeTransformOptions(options js.Value, hash string) transform.TransformOpti
preprocessStyle := options.Get("preprocessStyle")

return transform.TransformOptions{
As: as,
Scope: hash,
Filename: filename,
Pathname: pathname,
Expand Down Expand Up @@ -115,6 +123,10 @@ type HoistedScript struct {
Type string `js:"type"`
}

type ParseResult struct {
AST string `js:"ast"`
}

type TransformResult struct {
Code string `js:"code"`
Map string `js:"map"`
Expand All @@ -141,6 +153,24 @@ func preprocessStyle(i int, style *astro.Node, transformOptions transform.Transf
style.FirstChild.Data = str
}

func Parse() interface{} {
return js.FuncOf(func(this js.Value, args []js.Value) interface{} {
source := jsString(args[0])
parseOptions := makeParseOptions(js.Value(args[1]))

var doc *astro.Node
doc, err := astro.Parse(strings.NewReader(source))
if err != nil {
fmt.Println(err)
}
result := printer.PrintToJSON(source, doc, parseOptions)

return vert.ValueOf(ParseResult{
AST: string(result.Output),
})
})
}

func Transform() interface{} {
return js.FuncOf(func(this js.Value, args []js.Value) interface{} {
source := jsString(args[0])
Expand All @@ -153,29 +183,9 @@ func Transform() interface{} {
go func() {
var doc *astro.Node

if transformOptions.As == "document" {
docNode, err := astro.Parse(strings.NewReader(source))
doc = docNode
if err != nil {
fmt.Println(err)
}
} else if transformOptions.As == "fragment" {
nodes, err := astro.ParseFragment(strings.NewReader(source), &astro.Node{
Type: astro.ElementNode,
Data: atom.Template.String(),
DataAtom: atom.Template,
})
if err != nil {
fmt.Println(err)
}
doc = &astro.Node{
Type: astro.DocumentNode,
HydrationDirectives: make(map[string]bool),
}
for i := 0; i < len(nodes); i++ {
n := nodes[i]
doc.AppendChild(n)
}
doc, err := astro.Parse(strings.NewReader(source))
if err != nil {
fmt.Println(err)
}

// Hoist styles and scripts to the top-level
Expand Down
25 changes: 25 additions & 0 deletions internal/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

package astro

import a "golang.org/x/net/html/atom"

// Section 12.2.4.2 of the HTML5 specification says "The following elements
// have varying levels of special parsing rules".
// https://html.spec.whatwg.org/multipage/syntax.html#the-stack-of-open-elements
Expand Down Expand Up @@ -109,3 +111,26 @@ func isSpecialElement(element *Node) bool {
}
return false
}

var knownDirectiveMap = map[string]bool{
"client:load": true,
"client:idle": true,
"client:visible": true,
"client:only": true,
"class:list": true,
"set:text": true,
"set:html": true,
}

func IsKnownDirective(element *Node, attr *Attribute) bool {
if knownDirectiveMap[attr.Key] {
return true
}
if element.DataAtom == a.Script {
return attr.Key == "hoist"
}
if element.DataAtom == a.Style {
return attr.Key == "global"
}
return false
}
23 changes: 23 additions & 0 deletions internal/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,29 @@ const (
ExpressionNode
)

func (t NodeType) String() string {
switch t {
case ErrorNode:
return "error"
case TextNode:
return "text"
case DocumentNode:
return "root"
case ElementNode:
return "element"
case CommentNode:
return "comment"
case DoctypeNode:
return "doctype"
case FrontmatterNode:
return "frontmatter"
case ExpressionNode:
return "expression"
default:
return ""
}
}

// Used as an Attribute Key to mark implicit nodes
const ImplicitNodeMarker = "\x00implicit"

Expand Down
14 changes: 14 additions & 0 deletions internal/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ func (p *parser) addFrontmatter(empty bool) {
}
if empty {
p.frontmatterState = FrontmatterClosed
p.fm.Attr = append(p.fm.Attr, Attribute{Key: ImplicitNodeMarker, Type: EmptyAttribute})
} else {
p.frontmatterState = FrontmatterOpen
p.oe = append(p.oe, p.fm)
Expand Down Expand Up @@ -749,6 +750,9 @@ func inHeadIM(p *parser) bool {
return true
}
p.tok.Data = s
} else if p.oe.top() != nil && (isComponent(p.oe.top().Data) || isFragment((p.oe.top().Data))) {
p.addText(p.tok.Data)
return true
}
case StartTagToken:
// Allow components in Head
Expand All @@ -769,6 +773,16 @@ func inHeadIM(p *parser) bool {
p.oe.pop()
p.acknowledgeSelfClosingTag()
return true
case a.Slot:
p.addElement()
p.setOriginalIM()
p.im = inBodyIM
if p.hasSelfClosingToken {
p.addLoc()
p.oe.pop()
p.acknowledgeSelfClosingTag()
}
return true
case a.Noscript:
if p.scripting {
p.parseGenericRawTextElement()
Expand Down
Loading