From 3ffeb0f7b7b16f8a0939a1ae80be90cccbd98579 Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Fri, 23 Apr 2021 15:20:05 -0400 Subject: [PATCH] Restore parse errors (#130) --- package-lock.json | 6 ++++++ package.json | 2 ++ src/compiler/codegen/index.ts | 19 ++++++++++++++++--- src/runtime.ts | 2 +- 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index c79ee5ae3..b8355da39 100644 --- a/package-lock.json +++ b/package-lock.json @@ -334,6 +334,12 @@ "resolved": "https://registry.npmjs.org/@tootallnate/once/-/once-1.1.2.tgz", "integrity": "sha512-RbzJvlNzmRq5c3O09UipeuXno4tA1FE6ikOjxZK0tuxVv3412l64l5t1W5pj4+rJq9vpkm/kwiR07aZXnsKPxw==" }, + "@types/babel__code-frame": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@types/babel__code-frame/-/babel__code-frame-7.0.2.tgz", + "integrity": "sha512-imO+jT/yjOKOAS5GQZ8SDtwiIloAGGr6OaZDKB0V5JVaSfGZLat5K5/ZRtyKW6R60XHV3RHYPTFfhYb+wDKyKg==", + "dev": true + }, "@types/babel__generator": { "version": "7.6.2", "resolved": "https://registry.npmjs.org/@types/babel__generator/-/babel__generator-7.6.2.tgz", diff --git a/package.json b/package.json index 81a79ed0e..28a782a29 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ "publish-hidden": "npm run build && npm publish --tag shhhhh" }, "dependencies": { + "@babel/code-frame": "^7.12.13", "@babel/generator": "^7.13.9", "@babel/parser": "^7.13.15", "@babel/traverse": "^7.13.15", @@ -82,6 +83,7 @@ }, "devDependencies": { "@babel/types": "^7.13.14", + "@types/babel__code-frame": "^7.0.2", "@types/babel__generator": "^7.6.2", "@types/babel__parser": "^7.1.1", "@types/babel__traverse": "^7.11.1", diff --git a/src/compiler/codegen/index.ts b/src/compiler/codegen/index.ts index a10268883..bca24d81d 100644 --- a/src/compiler/codegen/index.ts +++ b/src/compiler/codegen/index.ts @@ -9,11 +9,12 @@ import path from 'path'; import { walk } from 'estree-walker'; import _babelGenerator from '@babel/generator'; import babelParser from '@babel/parser'; +import { codeFrameColumns } from "@babel/code-frame"; import * as babelTraverse from '@babel/traverse'; import { ImportDeclaration, ExportNamedDeclaration, VariableDeclarator, Identifier } from '@babel/types'; import { warn } from '../../logger.js'; import { fetchContent } from './content.js'; -import { isFetchContent, isImportMetaDeclaration } from './utils.js'; +import { isFetchContent } from './utils.js'; import { yellow } from 'kleur/colors'; const traverse: typeof babelTraverse.default = (babelTraverse.default as any).default; @@ -315,10 +316,22 @@ function compileModule(module: Script, state: CodegenState, compileOptions: Comp const componentPlugins = new Set(); if (module) { - const program = babelParser.parse(module.content, { + const parseOptions: babelParser.ParserOptions = { sourceType: 'module', plugins: ['jsx', 'typescript', 'topLevelAwait'], - }).program; + }; + let parseResult; + try { + parseResult = babelParser.parse(module.content, parseOptions) + } catch(err) { + const location = { start: err.loc }; + const frame = codeFrameColumns(module.content, location); + err.frame = frame; + err.filename = state.filename; + err.start = err.loc; + throw err; + } + const program = parseResult.program; const { body } = program; let i = body.length; diff --git a/src/runtime.ts b/src/runtime.ts index be609ed0a..330ca013b 100644 --- a/src/runtime.ts +++ b/src/runtime.ts @@ -224,7 +224,7 @@ async function load(config: RuntimeConfig, rawPathname: string | undefined): Pro }, }; } catch (err) { - if (err.code === 'parse-error') { + if (err.code === 'parse-error' || err instanceof SyntaxError) { return { statusCode: 500, type: 'parse-error',