From 4963b6d90b2d002f4a4746bb22cbce6a0dd6a739 Mon Sep 17 00:00:00 2001 From: Reaper Gelera Date: Thu, 11 Jul 2024 17:16:45 +0530 Subject: [PATCH] fix: allow top level await parsing --- src/ast.js | 1 + tests/find-islands.test.js | 56 +++++++++++++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/src/ast.js b/src/ast.js index f3af3ec..f2a7a2c 100644 --- a/src/ast.js +++ b/src/ast.js @@ -20,6 +20,7 @@ const jsxParser = Parser.extend( function astFromCode(code) { const ast = jsxParser.parse(code, { sourceType: 'module', + allowAwaitOutsideFunction: true, ecmaVersion: '2020', }) return ast diff --git a/tests/find-islands.test.js b/tests/find-islands.test.js index a19ba52..47fe7ed 100644 --- a/tests/find-islands.test.js +++ b/tests/find-islands.test.js @@ -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( @@ -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\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()