-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: refactor js_scanner to split at first non-exported token * chore: add changeset * refactor: add missing utils * refactor: cleanup printer tests to match new render body behavior * fix: lint * Gets tests passing Co-authored-by: Matthew Phillips <matthew@skypack.dev>
- Loading branch information
1 parent
d11c8c2
commit 7ab9148
Showing
8 changed files
with
293 additions
and
103 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 | ||
--- | ||
|
||
Improve JS scanning algorithm to be more fault tolerant, less error prone |
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
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,117 @@ | ||
package js_scanner | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestFindRenderBody(t *testing.T) { | ||
// note: the tests have hashes inlined because it’s easier to read | ||
// note: this must be valid CSS, hence the empty "{}" | ||
tests := []struct { | ||
name string | ||
source string | ||
want int | ||
}{ | ||
{ | ||
name: "basic", | ||
source: `const value = "test"`, | ||
want: 0, | ||
}, | ||
{ | ||
name: "import", | ||
source: `import { fn } from "package"; | ||
const b = await fetch();`, | ||
want: 30, | ||
}, | ||
{ | ||
name: "big import", | ||
source: `import { | ||
a, | ||
b, | ||
c, | ||
d, | ||
} from "package" | ||
const b = await fetch();`, | ||
want: 44, | ||
}, | ||
{ | ||
name: "import with comment", | ||
source: `// comment | ||
import { fn } from "package"; | ||
const b = await fetch();`, | ||
want: 41, | ||
}, | ||
{ | ||
name: "import assertion", | ||
source: `// comment | ||
import { fn } from "package" assert { it: 'works' }; | ||
const b = await fetch();`, | ||
want: 64, | ||
}, | ||
{ | ||
name: "import assertion", | ||
source: `// comment | ||
import { | ||
fn | ||
} from | ||
"package" | ||
assert { | ||
it: 'works' | ||
}; | ||
const b = await fetch();`, | ||
want: 74, | ||
}, | ||
{ | ||
name: "import/export", | ||
source: `import { fn } from "package"; | ||
export async fn() {} | ||
const b = await fetch()`, | ||
want: 51, | ||
}, | ||
{ | ||
name: "getStaticPaths", | ||
source: `import { fn } from "package"; | ||
export async function getStaticPaths() { | ||
const content = Astro.fetchContent('**/*.md'); | ||
} | ||
const b = await fetch()`, | ||
want: 121, | ||
}, | ||
{ | ||
name: "getStaticPaths with comments", | ||
source: `import { fn } from "package"; | ||
export async function getStaticPaths() { | ||
const content = Astro.fetchContent('**/*.md'); | ||
} | ||
const b = await fetch()`, | ||
want: 121, | ||
}, | ||
{ | ||
name: "getStaticPaths with semicolon", | ||
source: `import { fn } from "package"; | ||
export async function getStaticPaths() { | ||
const content = Astro.fetchContent('**/*.md'); | ||
}; const b = await fetch()`, | ||
want: 122, | ||
}, | ||
{ | ||
name: "multiple imports", | ||
source: `import { a } from "a"; | ||
import { b } from "b"; | ||
import { c } from "c"; | ||
const d = await fetch()`, | ||
want: 69, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got := FindRenderBody([]byte(tt.source)) | ||
if tt.want != got { | ||
t.Error(fmt.Sprintf("\nFAIL: %s\n want: %v\n got: %v", tt.name, tt.want, got)) | ||
fmt.Println(tt.source[got:]) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.