Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into feat/more-snapshot-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico committed Jul 30, 2024
2 parents 037482d + 399c6bb commit c8ac87a
Show file tree
Hide file tree
Showing 134 changed files with 4,249 additions and 3,020 deletions.
5 changes: 5 additions & 0 deletions .changeset/bright-plums-suffer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@astrojs/compiler": patch
---

Transform top level returns into throws in the TSX output
2 changes: 2 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# chore: format everything (#1020)
402060270f50fe273d5e7387241d7eb36f99ca11
18 changes: 18 additions & 0 deletions .github/workflows/issue-needs-repro.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Close Issues (needs repro)

on:
schedule:
- cron: "0 0 * * *"

jobs:
close-issues:
if: github.repository == 'withastro/compiler'
runs-on: ubuntu-latest
steps:
- name: needs repro
uses: actions-cool/issues-helper@v3
with:
actions: "close-issues"
token: ${{ secrets.GITHUB_TOKEN }}
labels: "needs repro"
inactive-day: 3
35 changes: 28 additions & 7 deletions biome.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"$schema": "https://biomejs.dev/schemas/1.8.1/schema.json",
"files": {
"ignore": ["**/dist", "**/pnpm-lock.yaml"],
"ignore": ["**/dist/**", "**/pnpm-lock.yaml", "wasm_exec.ts"],
"include": ["packages/**"]
},
"formatter": {
"enabled": true,
"indentStyle": "space",
"indentStyle": "tab",
"indentWidth": 2,
"lineWidth": 180
"lineWidth": 100
},
"organizeImports": {
"enabled": true
Expand All @@ -31,17 +31,38 @@
"fix": "safe"
}
}
},
"ignore": ["wasm_exec.ts"]
}
},
"javascript": {
"formatter": {
"quoteStyle": "single"
"trailingCommas": "es5",
"quoteStyle": "single",
"semicolons": "always"
}
},
"json": {
"parser": {
"allowComments": true,
"allowTrailingCommas": true
},
"formatter": {
"indentStyle": "space",
"trailingCommas": "none"
}
},
"overrides": [
{
"include": ["package.json", "biome.jsonc"],
"include": ["**/stress/**"],
"linter": {
"rules": {
"suspicious": {
"noConsoleLog": "off"
}
}
}
},
{
"include": ["package.json"],
"json": {
"formatter": {
"lineWidth": 1
Expand Down
35 changes: 34 additions & 1 deletion cmd/astro-wasm/astro-wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ func jsString(j js.Value) string {
return j.String()
}

func jsBoolOptional(j js.Value, defaultValue bool) bool {
if j.Equal(js.Undefined()) || j.Equal(js.Null()) {
return defaultValue
}
return j.Bool()
}

func jsBool(j js.Value) bool {
if j.Equal(js.Undefined()) || j.Equal(js.Null()) {
return false
Expand Down Expand Up @@ -148,6 +155,16 @@ func makeTransformOptions(options js.Value) transform.TransformOptions {
}
}

func makeTSXOptions(options js.Value) printer.TSXOptions {
includeScripts := jsBoolOptional(options.Get("includeScripts"), true)
includeStyles := jsBoolOptional(options.Get("includeStyles"), true)

return printer.TSXOptions{
IncludeScripts: includeScripts,
IncludeStyles: includeStyles,
}
}

type RawSourceMap struct {
File string `js:"file"`
Mappings string `js:"mappings"`
Expand All @@ -166,6 +183,7 @@ type HoistedScript struct {

type HydratedComponent struct {
ExportName string `js:"exportName"`
LocalName string `js:"localName"`
Specifier string `js:"specifier"`
ResolvedPath string `js:"resolvedPath"`
}
Expand All @@ -191,6 +209,7 @@ type TransformResult struct {
Scripts []HoistedScript `js:"scripts"`
HydratedComponents []HydratedComponent `js:"hydratedComponents"`
ClientOnlyComponents []HydratedComponent `js:"clientOnlyComponents"`
ServerComponents []HydratedComponent `js:"serverComponents"`
ContainsHead bool `js:"containsHead"`
StyleError []string `js:"styleError"`
Propagation bool `js:"propagation"`
Expand Down Expand Up @@ -260,7 +279,10 @@ func ConvertToTSX() any {
if err != nil {
h.AppendError(err)
}
result := printer.PrintToTSX(source, doc, transformOptions, h)

tsxOptions := makeTSXOptions(js.Value(args[1]))

result := printer.PrintToTSX(source, doc, tsxOptions, transformOptions, h)

// AFTER printing, exec transformations to pickup any errors/warnings
transform.Transform(doc, transformOptions, h)
Expand Down Expand Up @@ -338,6 +360,7 @@ func Transform() any {
scripts := []HoistedScript{}
hydratedComponents := []HydratedComponent{}
clientOnlyComponents := []HydratedComponent{}
serverComponents := []HydratedComponent{}
css_result := printer.PrintCSS(source, doc, transformOptions)
for _, bytes := range css_result.Output {
css = append(css, string(bytes))
Expand Down Expand Up @@ -418,6 +441,15 @@ func Transform() any {
})
}

for _, c := range doc.ServerComponents {
serverComponents = append(serverComponents, HydratedComponent{
ExportName: c.ExportName,
LocalName: c.LocalName,
Specifier: c.Specifier,
ResolvedPath: c.ResolvedPath,
})
}

var value vert.Value
result := printer.PrintToJS(source, doc, len(css), transformOptions, h)
transformResult := &TransformResult{
Expand All @@ -426,6 +458,7 @@ func Transform() any {
Scripts: scripts,
HydratedComponents: hydratedComponents,
ClientOnlyComponents: clientOnlyComponents,
ServerComponents: serverComponents,
ContainsHead: doc.ContainsHead,
StyleError: styleError,
Propagation: doc.HeadPropagation,
Expand Down
54 changes: 54 additions & 0 deletions internal/js_scanner/js_scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,60 @@ import (
"github.com/withastro/compiler/internal/loc"
)

func FindTopLevelReturns(source []byte) []int {
l := js.NewLexer(parse.NewInputBytes(source))
i := 0
returns := make([]int, 0)
pairs := make(map[byte]int)
inFunction := false

for {
token, value := l.Next()

if token == js.DivToken || token == js.DivEqToken {
lns := bytes.Split(source[i+1:], []byte{'\n'})
if bytes.Contains(lns[0], []byte{'/'}) {
token, value = l.RegExp()
}
}

if token == js.ErrorToken {
if l.Err() != io.EOF {
return returns
}
break
}

if js.IsPunctuator(token) {
if value[0] == '{' {
pairs[value[0]]++
i += len(value)
continue
} else if value[0] == '}' {
pairs['{']--
}
}

// Track function declarations
if token == js.FunctionToken {
inFunction = true
}

// Track end of function declarations
if inFunction && token == js.CloseBraceToken && pairs['{'] == 1 {
inFunction = false
}

if token == js.ReturnToken && !inFunction {
returns = append(returns, i)
}

i += len(value)
}

return returns
}

type HoistedScripts struct {
Hoisted [][]byte
HoistedLocs []loc.Loc
Expand Down
2 changes: 2 additions & 0 deletions internal/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ var scopeMarker = Node{Type: scopeMarkerNode}

type HydratedComponentMetadata struct {
ExportName string
LocalName string
Specifier string
ResolvedPath string
}
Expand Down Expand Up @@ -98,6 +99,7 @@ type Node struct {
ClientOnlyComponentNodes []*Node
ClientOnlyComponents []*HydratedComponentMetadata
HydrationDirectives map[string]bool
ServerComponents []*HydratedComponentMetadata
ContainsHead bool
HeadPropagation bool

Expand Down
15 changes: 12 additions & 3 deletions internal/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -903,8 +903,10 @@ func inHeadIM(p *parser) bool {
p.im = afterHeadIM
return true
case a.Body, a.Html, a.Br:
p.parseImpliedToken(EndTagToken, a.Head, a.Head.String())
p.addLoc()
p.oe.pop()
p.originalIM = nil
p.im = afterHeadIM
return false
case a.Template:
if !p.oe.contains(a.Template) {
Expand Down Expand Up @@ -1439,12 +1441,18 @@ func inBodyIM(p *parser) bool {
if p.elementInScope(defaultScope, a.Body) {
p.im = afterBodyIM
}
if p.literal {
p.oe.pop()
}
case a.Html:
p.addLoc()
if p.elementInScope(defaultScope, a.Body) {
p.parseImpliedToken(EndTagToken, a.Body, a.Body.String())
return false
}
if p.literal {
p.oe.pop()
}
return true
case a.Address, a.Article, a.Aside, a.Blockquote, a.Button, a.Center, a.Details, a.Dialog, a.Dir, a.Div, a.Dl, a.Fieldset, a.Figcaption, a.Figure, a.Footer, a.Header, a.Hgroup, a.Listing, a.Main, a.Menu, a.Nav, a.Ol, a.Pre, a.Section, a.Summary, a.Ul:
p.addLoc()
Expand Down Expand Up @@ -2702,9 +2710,10 @@ func inLiteralIM(p *parser) bool {
p.addLoc()
p.oe.pop()
p.acknowledgeSelfClosingTag()
} else {
// always continue `inLiteralIM`
return true
}
// always continue `inLiteralIM`
return true
case StartExpressionToken:
p.addExpression()
// always continue `inLiteralIM`
Expand Down
Loading

0 comments on commit c8ac87a

Please sign in to comment.