Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1.8k
ref(sveltekit): Replace recast + @babel/parser with acorn#19533
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
adf408c844ac1587f80a4bf9d78d60cf1eaFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,11 @@ | ||
| import { tsPlugin } from '@sveltejs/acorn-typescript'; | ||
| import * as acorn from 'acorn'; | ||
| import * as fs from 'fs'; | ||
| import * as path from 'path'; | ||
| import * as recast from 'recast'; | ||
| import type { Plugin } from 'vite'; | ||
| import { WRAPPED_MODULE_SUFFIX } from '../common/utils'; | ||
| import { parser } from './recastTypescriptParser'; | ||
| import t = recast.types.namedTypes; | ||
| const AcornParser = acorn.Parser.extend(tsPlugin()); | ||
| export type AutoInstrumentSelection = { | ||
| /** | ||
| @@ -123,23 +124,21 @@ export async function canWrapLoad(id: string, debug: boolean): Promise<boolean> | ||
| const code = (await fs.promises.readFile(id, 'utf8')).toString(); | ||
| const ast = recast.parse(code, { | ||
| parser, | ||
| }); | ||
| const program = (ast as { program?: t.Program }).program; | ||
| if (!program) { | ||
| let program: acorn.Program; | ||
| try { | ||
| program = AcornParser.parse(code, { | ||
| sourceType: 'module', | ||
| ecmaVersion: 'latest', | ||
| locations: true, | ||
| }); | ||
sentry[bot] marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } catch { | ||
| // eslint-disable-next-line no-console | ||
| debug && console.log(`Skipping wrapping ${id} because it doesn't contain valid JavaScript or TypeScript`); | ||
| return false; | ||
| } | ||
| const hasLoadDeclaration = program.body | ||
| .filter( | ||
| (statement): statement is recast.types.namedTypes.ExportNamedDeclaration => | ||
| statement.type === 'ExportNamedDeclaration', | ||
| ) | ||
| .filter((statement): statement is acorn.ExportNamedDeclaration => statement.type === 'ExportNamedDeclaration') | ||
| .find(exportDecl => { | ||
| // find `export const load = ...` | ||
| if (exportDecl.declaration?.type === 'VariableDeclaration') { | ||
| @@ -160,11 +159,8 @@ export async function canWrapLoad(id: string, debug: boolean): Promise<boolean> | ||
| return exportDecl.specifiers.find(specifier => { | ||
| return ( | ||
| (specifier.exported.type === 'Identifier' && specifier.exported.name === 'load') || | ||
| // Type casting here because somehow the 'exportExtensions' plugin isn't reflected in the possible types | ||
| // This plugin adds support for exporting something as a string literal (see comment above) | ||
| // Doing this to avoid adding another babel plugin dependency | ||
| ((specifier.exported.type as 'StringLiteral' | '') === 'StringLiteral' && | ||
| (specifier.exported as unknown as t.StringLiteral).value === 'load') | ||
| // ESTree/acorn represents `export { x as "load" }` with a Literal node (not Babel's StringLiteral) | ||
| (specifier.exported.type === 'Literal' && specifier.exported.value === 'load') | ||
| ); | ||
| }); | ||
| } | ||
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.