pull/11639/head
Rich Harris 2 years ago
parent d1ba579802
commit 185ab80ab0

@ -1,5 +1,4 @@
import { walk as zimmerframe_walk } from 'zimmerframe'; import { walk as zimmerframe_walk } from 'zimmerframe';
import { CompileError } from './errors.js';
import { convert } from './legacy.js'; import { convert } from './legacy.js';
import { parse as parse_acorn } from './phases/1-parse/acorn.js'; import { parse as parse_acorn } from './phases/1-parse/acorn.js';
import { parse as _parse } from './phases/1-parse/index.js'; import { parse as _parse } from './phases/1-parse/index.js';
@ -22,7 +21,6 @@ export function compile(source, options) {
const validated = validate_component_options(options, ''); const validated = validate_component_options(options, '');
state.reset(source, validated); state.reset(source, validated);
try {
let parsed = _parse(source); let parsed = _parse(source);
const { customElement: customElementOptions, ...parsed_options } = parsed.options || {}; const { customElement: customElementOptions, ...parsed_options } = parsed.options || {};
@ -47,13 +45,6 @@ export function compile(source, options) {
const result = transform_component(analysis, source, combined_options); const result = transform_component(analysis, source, combined_options);
result.ast = to_public_ast(source, parsed, options.modernAst); result.ast = to_public_ast(source, parsed, options.modernAst);
return result; return result;
} catch (e) {
if (e instanceof CompileError) {
handle_compile_error(e);
}
throw e;
}
} }
/** /**
@ -68,34 +59,8 @@ export function compileModule(source, options) {
const validated = validate_module_options(options, ''); const validated = validate_module_options(options, '');
state.reset(source, validated); state.reset(source, validated);
try {
const analysis = analyze_module(parse_acorn(source, false), validated); const analysis = analyze_module(parse_acorn(source, false), validated);
const result = transform_module(analysis, source, validated); return transform_module(analysis, source, validated);
return result;
} catch (e) {
if (e instanceof CompileError) {
handle_compile_error(e);
}
throw e;
}
}
/**
* @param {import('#compiler').CompileError} error
*/
function handle_compile_error(error) {
error.filename = state.filename;
if (error.position) {
const start = state.locator(error.position[0]);
const end = state.locator(error.position[1]);
error.start = start;
error.end = end;
}
throw error;
} }
/** /**
@ -138,18 +103,7 @@ function handle_compile_error(error) {
export function parse(source, { filename, rootDir, modern } = {}) { export function parse(source, { filename, rootDir, modern } = {}) {
state.reset(source, { filename, rootDir }); // TODO it's weird to require filename/rootDir here. reconsider the API state.reset(source, { filename, rootDir }); // TODO it's weird to require filename/rootDir here. reconsider the API
/** @type {import('#compiler').Root} */ const ast = _parse(source);
let ast;
try {
ast = _parse(source);
} catch (e) {
if (e instanceof CompileError) {
handle_compile_error(e);
}
throw e;
}
return to_public_ast(source, ast, modern); return to_public_ast(source, ast, modern);
} }

Loading…
Cancel
Save