Restore parse errors (#130)

This commit is contained in:
Matthew Phillips 2021-04-23 15:20:05 -04:00 committed by GitHub
parent 9b9bdbf4a1
commit 3ffeb0f7b7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 4 deletions

6
package-lock.json generated
View file

@ -334,6 +334,12 @@
"resolved": "https://registry.npmjs.org/@tootallnate/once/-/once-1.1.2.tgz", "resolved": "https://registry.npmjs.org/@tootallnate/once/-/once-1.1.2.tgz",
"integrity": "sha512-RbzJvlNzmRq5c3O09UipeuXno4tA1FE6ikOjxZK0tuxVv3412l64l5t1W5pj4+rJq9vpkm/kwiR07aZXnsKPxw==" "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": { "@types/babel__generator": {
"version": "7.6.2", "version": "7.6.2",
"resolved": "https://registry.npmjs.org/@types/babel__generator/-/babel__generator-7.6.2.tgz", "resolved": "https://registry.npmjs.org/@types/babel__generator/-/babel__generator-7.6.2.tgz",

View file

@ -33,6 +33,7 @@
"publish-hidden": "npm run build && npm publish --tag shhhhh" "publish-hidden": "npm run build && npm publish --tag shhhhh"
}, },
"dependencies": { "dependencies": {
"@babel/code-frame": "^7.12.13",
"@babel/generator": "^7.13.9", "@babel/generator": "^7.13.9",
"@babel/parser": "^7.13.15", "@babel/parser": "^7.13.15",
"@babel/traverse": "^7.13.15", "@babel/traverse": "^7.13.15",
@ -82,6 +83,7 @@
}, },
"devDependencies": { "devDependencies": {
"@babel/types": "^7.13.14", "@babel/types": "^7.13.14",
"@types/babel__code-frame": "^7.0.2",
"@types/babel__generator": "^7.6.2", "@types/babel__generator": "^7.6.2",
"@types/babel__parser": "^7.1.1", "@types/babel__parser": "^7.1.1",
"@types/babel__traverse": "^7.11.1", "@types/babel__traverse": "^7.11.1",

View file

@ -9,11 +9,12 @@ import path from 'path';
import { walk } from 'estree-walker'; import { walk } from 'estree-walker';
import _babelGenerator from '@babel/generator'; import _babelGenerator from '@babel/generator';
import babelParser from '@babel/parser'; import babelParser from '@babel/parser';
import { codeFrameColumns } from "@babel/code-frame";
import * as babelTraverse from '@babel/traverse'; import * as babelTraverse from '@babel/traverse';
import { ImportDeclaration, ExportNamedDeclaration, VariableDeclarator, Identifier } from '@babel/types'; import { ImportDeclaration, ExportNamedDeclaration, VariableDeclarator, Identifier } from '@babel/types';
import { warn } from '../../logger.js'; import { warn } from '../../logger.js';
import { fetchContent } from './content.js'; import { fetchContent } from './content.js';
import { isFetchContent, isImportMetaDeclaration } from './utils.js'; import { isFetchContent } from './utils.js';
import { yellow } from 'kleur/colors'; import { yellow } from 'kleur/colors';
const traverse: typeof babelTraverse.default = (babelTraverse.default as any).default; 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<ValidExtensionPlugins>(); const componentPlugins = new Set<ValidExtensionPlugins>();
if (module) { if (module) {
const program = babelParser.parse(module.content, { const parseOptions: babelParser.ParserOptions = {
sourceType: 'module', sourceType: 'module',
plugins: ['jsx', 'typescript', 'topLevelAwait'], 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; const { body } = program;
let i = body.length; let i = body.length;

View file

@ -224,7 +224,7 @@ async function load(config: RuntimeConfig, rawPathname: string | undefined): Pro
}, },
}; };
} catch (err) { } catch (err) {
if (err.code === 'parse-error') { if (err.code === 'parse-error' || err instanceof SyntaxError) {
return { return {
statusCode: 500, statusCode: 500,
type: 'parse-error', type: 'parse-error',