Skip to content

Commit

Permalink
fix: allow top level await parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
barelyhuman committed Jul 11, 2024
1 parent 57b3939 commit 4963b6d
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const jsxParser = Parser.extend(
function astFromCode(code) {
const ast = jsxParser.parse(code, {
sourceType: 'module',
allowAwaitOutsideFunction: true,
ecmaVersion: '2020',
})
return ast
Expand Down
56 changes: 55 additions & 1 deletion tests/find-islands.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { test } from 'uvu'
import * as assert from 'uvu/assert'
import { findIslands } from '../src/index.js'
import {
DEFAULT_TRANSPILED_IDENTIFIERS,
findIslands,
isFunctionIsland,
} from '../src/index.js'

test('no islands', () => {
const islands = findIslands(
Expand Down Expand Up @@ -210,4 +214,54 @@ test('nested islands', () => {
assert.equal(islands.length, 1)
})

test('multiple exports with islands', () => {
const islands = findIslands(
'var _jsxFileName = "/Users/sid/code/adex/playground/src/components/app/MainSiderbar.jsx";\n' +
"import { Sidebar } from '../Sidebar';\n" +
"import { marked } from 'marked';\n" +
"import { signal } from '@preact/signals';\n" +
'import { jsxDEV as _jsxDEV } from "preact/jsx-dev-runtime";\n' +
'const md = String.raw;\n' +
'export const sidebarItems = [{\n' +
" key: 'introduction',\n" +
" label: 'Introduction',\n" +
' content: await marked.parse(md`\n' +
'### Introduction</h3>\n' +
'\n' +
'**_Adex_** is a vite plugin to simplify server rendered apps your development\n' +
'with preact.\n' +
' `)\n' +
'}, {\n' +
" key: 'getting-started',\n" +
" label: 'Getting Started',\n" +
' content: await marked.parse(md`\n' +
'### Getting Started\n' +
' `)\n' +
'}];\n' +
'export const activeSidebar = signal(sidebarItems[0].key);\n' +
'export const MainSidebar = () => {\n' +
' return _jsxDEV(Sidebar, {\n' +
' activeSidebar: activeSidebar,\n' +
' setSidebar: key => {\n' +
' activeSidebar.value = key;\n' +
' },\n' +
' sidebarItems: sidebarItems\n' +
' }, void 0, false, {\n' +
' fileName: _jsxFileName,\n' +
' lineNumber: 31,\n' +
' columnNumber: 5\n' +
' }, this);\n' +
'};',
{
isFunctionIsland: ast =>
isFunctionIsland(ast, {
transpiledIdentifiers:
DEFAULT_TRANSPILED_IDENTIFIERS.concat('_jsxDEV'),
}),
}
)

assert.equal(islands.length, 1)
})

test.run()

0 comments on commit 4963b6d

Please sign in to comment.