Skip to content

Commit

Permalink
Rollback #674 (#678)
Browse files Browse the repository at this point in the history
* Revert "Improve JS Sourcemaps (#674)"

This reverts commit 20497f4.

* chore: add changeset

Co-authored-by: Nate Moore <nate@astro.build>
  • Loading branch information
natemoo-re and natemoo-re authored Dec 20, 2022
1 parent 744b30b commit fd5cb57
Show file tree
Hide file tree
Showing 29 changed files with 153 additions and 667 deletions.
5 changes: 5 additions & 0 deletions .changeset/polite-balloons-give.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/compiler': patch
---

Rollback https://github.com/withastro/compiler/pull/674
9 changes: 0 additions & 9 deletions .gitpod.yml

This file was deleted.

79 changes: 21 additions & 58 deletions internal/js_scanner/js_scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,24 @@ import (
)

type HoistedScripts struct {
Hoisted [][]byte
HoistedLocs []loc.Loc
Body [][]byte
BodyLocs []loc.Loc
Hoisted [][]byte
Body []byte
}

func HoistExports(source []byte) HoistedScripts {
shouldHoist := bytes.Contains(source, []byte("export"))
if !shouldHoist {
body := make([][]byte, 0)
body = append(body, source)
bodyLocs := make([]loc.Loc, 0)
bodyLocs = append(bodyLocs, loc.Loc{Start: 0})
return HoistedScripts{
Body: body,
BodyLocs: bodyLocs,
Body: source,
}
}

l := js.NewLexer(parse.NewInputBytes(source))
i := 0
end := 0

hoisted := make([][]byte, 0)
hoistedLocs := make([]loc.Loc, 0)
body := make([][]byte, 0)
bodyLocs := make([]loc.Loc, 0)
hoisted := make([][]byte, 1)
body := make([]byte, 0)
pairs := make(map[byte]int)

// Let's lex the script until we find what we need!
Expand All @@ -56,15 +47,8 @@ outer:

if token == js.ErrorToken {
if l.Err() != io.EOF {
body := make([][]byte, 0)
body = append(body, source)
bodyLocs := make([]loc.Loc, 0)
bodyLocs = append(bodyLocs, loc.Loc{Start: 0})
return HoistedScripts{
Hoisted: hoisted,
HoistedLocs: hoistedLocs,
Body: body,
BodyLocs: bodyLocs,
Body: source,
}
}
break
Expand Down Expand Up @@ -143,26 +127,17 @@ outer:

if foundIdent && foundSemicolonOrLineTerminator && pairs['{'] == 0 && pairs['('] == 0 && pairs['['] == 0 {
hoisted = append(hoisted, source[start:i])
hoistedLocs = append(hoistedLocs, loc.Loc{Start: start})
if end < start {
body = append(body, source[end:start])
bodyLocs = append(bodyLocs, loc.Loc{Start: end})
body = append(body, source[end:start]...)
}
end = i
continue outer
}

if next == js.ErrorToken {
if l.Err() != io.EOF {
body := make([][]byte, 0)
body = append(body, source)
bodyLocs := make([]loc.Loc, 0)
bodyLocs = append(bodyLocs, loc.Loc{Start: 0})
return HoistedScripts{
Hoisted: hoisted,
HoistedLocs: hoistedLocs,
Body: body,
BodyLocs: bodyLocs,
Body: source,
}
}
break outer
Expand All @@ -189,14 +164,11 @@ outer:
i += len(value)
}

body = append(body, source[end:])
bodyLocs = append(bodyLocs, loc.Loc{Start: end})
body = append(body, source[end:]...)

return HoistedScripts{
Hoisted: hoisted,
HoistedLocs: hoistedLocs,
Body: body,
BodyLocs: bodyLocs,
Hoisted: hoisted,
Body: body,
}
}

Expand All @@ -206,25 +178,18 @@ func isKeyword(value []byte) bool {

func HoistImports(source []byte) HoistedScripts {
imports := make([][]byte, 0)
importLocs := make([]loc.Loc, 0)
body := make([][]byte, 0)
bodyLocs := make([]loc.Loc, 0)
body := make([]byte, 0)
prev := 0
for i, statement := NextImportStatement(source, 0); i > -1 && i < len(source)+1; i, statement = NextImportStatement(source, i) {
bodyLocs = append(bodyLocs, loc.Loc{Start: prev})
body = append(body, source[prev:statement.Span.Start])
for i, statement := NextImportStatement(source, 0); i > -1; i, statement = NextImportStatement(source, i) {
body = append(body, source[prev:statement.Span.Start]...)
imports = append(imports, statement.Value)
importLocs = append(importLocs, loc.Loc{Start: statement.Span.Start})
prev = i
}
if prev == 0 {
bodyLocs = append(bodyLocs, loc.Loc{Start: 0})
body = append(body, source)
return HoistedScripts{Body: body, BodyLocs: bodyLocs}
return HoistedScripts{Body: source}
}
bodyLocs = append(bodyLocs, loc.Loc{Start: prev})
body = append(body, source[prev:])
return HoistedScripts{Hoisted: imports, HoistedLocs: importLocs, Body: body, BodyLocs: bodyLocs}
body = append(body, source[prev:]...)
return HoistedScripts{Hoisted: imports, Body: body}
}

type Props struct {
Expand Down Expand Up @@ -502,7 +467,7 @@ func NextImportStatement(source []byte, pos int) (int, ImportStatement) {
for {
token, value := l.Next()

if len(source) > i && token == js.DivToken || token == js.DivEqToken {
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()
Expand All @@ -529,7 +494,7 @@ func NextImportStatement(source []byte, pos int) (int, ImportStatement) {
pairs := make(map[byte]int)
for {
next, nextValue := l.Next()
if len(source) > i && (next == js.DivToken || next == js.DivEqToken) {
if next == js.DivToken || next == js.DivEqToken {
lns := bytes.Split(source[i+1:], []byte{'\n'})
if bytes.Contains(lns[0], []byte{'/'}) {
next, nextValue = l.RegExp()
Expand Down Expand Up @@ -559,10 +524,8 @@ func NextImportStatement(source []byte, pos int) (int, ImportStatement) {
}

if !foundSpecifier && next == js.StringToken {
if len(nextValue) > 1 {
specifier = string(nextValue[1 : len(nextValue)-1])
foundSpecifier = true
}
specifier = string(nextValue[1 : len(nextValue)-1])
foundSpecifier = true
continue
}

Expand Down
81 changes: 29 additions & 52 deletions internal/js_scanner/js_scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"strings"
"testing"
"unicode/utf8"

"github.com/withastro/compiler/internal/test_utils"
)
Expand All @@ -17,8 +16,8 @@ type testcase struct {
only bool
}

func fixturesHoistImport() []testcase {
return []testcase{
func TestHoistImport(t *testing.T) {
tests := []testcase{
{
name: "basic",
source: `const value = "test"`,
Expand All @@ -41,18 +40,18 @@ const article2 = await import('../markdown/article2.md')
{
name: "big import",
source: `import {
a,
b,
c,
d,
a,
b,
c,
d,
} from "package"
const b = await fetch();`,
want: `import {
a,
b,
c,
d,
a,
b,
c,
d,
} from "package"
`,
},
Expand All @@ -74,18 +73,18 @@ const b = await fetch();`,
name: "import assertion 2",
source: `// comment
import {
fn
fn
} from
"package" assert {
it: 'works'
};
"package" assert {
it: 'works'
};
const b = await fetch();`,
want: `import {
fn
fn
} from
"package" assert {
it: 'works'
};
"package" assert {
it: 'works'
};
`,
},
{
Expand All @@ -97,10 +96,10 @@ import Test from "../components/Test.astro";`,
{
name: "import.meta.env II",
source: `console.log(
import
.meta
.env
.FOO
import
.meta
.env
.FOO
);
import Test from "../components/Test.astro";`,
want: `import Test from "../components/Test.astro";`,
Expand All @@ -116,7 +115,7 @@ const b = await fetch()`,
name: "getStaticPaths",
source: `import { fn } from "package";
export async function getStaticPaths() {
const content = Astro.fetchContent('**/*.md');
const content = Astro.fetchContent('**/*.md');
}
const b = await fetch()`,
want: `import { fn } from "package";`,
Expand All @@ -125,7 +124,7 @@ const b = await fetch()`,
name: "getStaticPaths with comments",
source: `import { fn } from "package";
export async function getStaticPaths() {
const content = Astro.fetchContent('**/*.md');
const content = Astro.fetchContent('**/*.md');
}
const b = await fetch()`,
want: `import { fn } from "package";`,
Expand All @@ -134,29 +133,29 @@ const b = await fetch()`,
name: "getStaticPaths with semicolon",
source: `import { fn } from "package";
export async function getStaticPaths() {
const content = Astro.fetchContent('**/*.md');
const content = Astro.fetchContent('**/*.md');
}; const b = await fetch()`,
want: `import { fn } from "package";`,
},
{
name: "getStaticPaths with RegExp escape",
source: `export async function getStaticPaths() {
const pattern = /\.md$/g.test('value');
const pattern = /\.md$/g.test('value');
}
import a from "a";`,
want: `import a from "a";`,
},
{
name: "getStaticPaths with divider",
source: `export async function getStaticPaths() {
const pattern = a / b;
const pattern = a / b;
}`,
want: ``,
},
{
name: "getStaticPaths with divider and following content",
source: `export async function getStaticPaths() {
const value = 1 / 2;
const value = 1 / 2;
}
// comment
import { b } from "b";
Expand All @@ -166,7 +165,7 @@ const { a } = Astro.props;`,
{
name: "getStaticPaths with regex and following content",
source: `export async function getStaticPaths() {
const value = /2/g;
const value = /2/g;
}
// comment
import { b } from "b";
Expand Down Expand Up @@ -204,10 +203,6 @@ import { c } from "c";
`,
},
}
}

func TestHoistImport(t *testing.T) {
tests := fixturesHoistImport()
for _, tt := range tests {
if tt.only {
tests = make([]testcase, 0)
Expand All @@ -231,24 +226,6 @@ func TestHoistImport(t *testing.T) {
}
}

func FuzzHoistImport(f *testing.F) {
tests := fixturesHoistImport()
for _, tt := range tests {
f.Add(tt.source) // Use f.Add to provide a seed corpus
}
f.Fuzz(func(t *testing.T, source string) {
result := HoistImports([]byte(source))
got := []byte{}
for _, imp := range result.Hoisted {
got = append(got, bytes.TrimSpace(imp)...)
got = append(got, '\n')
}
if utf8.ValidString(source) && !utf8.ValidString(string(got)) {
t.Errorf("Import hoisting produced an invalid string: %q", got)
}
})
}

func TestHoistExport(t *testing.T) {
tests := []testcase{
{
Expand Down

This file was deleted.

Loading

0 comments on commit fd5cb57

Please sign in to comment.