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

@ -1,5 +1,4 @@
import { walk as zimmerframe_walk } from 'zimmerframe';
import { CompileError } from './errors.js';
import { convert } from './legacy.js';
import { parse as parse_acorn } from './phases/1-parse/acorn.js';
import { parse as _parse } from './phases/1-parse/index.js';
@ -22,38 +21,30 @@ export function compile(source, options) {
const validated = validate_component_options(options, '');
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 || {};
/** @type {import('#compiler').ValidatedCompileOptions} */
const combined_options = {
...validated,
...parsed_options,
customElementOptions
};
/** @type {import('#compiler').ValidatedCompileOptions} */
const combined_options = {
...validated,
...parsed_options,
customElementOptions
};
if (parsed.metadata.ts) {
parsed = {
...parsed,
fragment: parsed.fragment && remove_typescript_nodes(parsed.fragment),
instance: parsed.instance && remove_typescript_nodes(parsed.instance),
module: parsed.module && remove_typescript_nodes(parsed.module)
};
}
const analysis = analyze_component(parsed, source, combined_options);
const result = transform_component(analysis, source, combined_options);
result.ast = to_public_ast(source, parsed, options.modernAst);
return result;
} catch (e) {
if (e instanceof CompileError) {
handle_compile_error(e);
}
throw e;
if (parsed.metadata.ts) {
parsed = {
...parsed,
fragment: parsed.fragment && remove_typescript_nodes(parsed.fragment),
instance: parsed.instance && remove_typescript_nodes(parsed.instance),
module: parsed.module && remove_typescript_nodes(parsed.module)
};
}
const analysis = analyze_component(parsed, source, combined_options);
const result = transform_component(analysis, source, combined_options);
result.ast = to_public_ast(source, parsed, options.modernAst);
return result;
}
/**
@ -68,34 +59,8 @@ export function compileModule(source, options) {
const validated = validate_module_options(options, '');
state.reset(source, validated);
try {
const analysis = analyze_module(parse_acorn(source, false), validated);
const result = 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;
const analysis = analyze_module(parse_acorn(source, false), validated);
return transform_module(analysis, source, validated);
}
/**
@ -138,18 +103,7 @@ function handle_compile_error(error) {
export function parse(source, { filename, rootDir, modern } = {}) {
state.reset(source, { filename, rootDir }); // TODO it's weird to require filename/rootDir here. reconsider the API
/** @type {import('#compiler').Root} */
let ast;
try {
ast = _parse(source);
} catch (e) {
if (e instanceof CompileError) {
handle_compile_error(e);
}
throw e;
}
const ast = _parse(source);
return to_public_ast(source, ast, modern);
}

Loading…
Cancel
Save