break out locator stuff from the rest of global state

pull/16268/head
Rich Harris 3 months ago
parent 7c8be602be
commit 73b77cc744

@ -96,6 +96,7 @@ export function compileModule(source, options) {
* @returns {Record<string, any>} * @returns {Record<string, any>}
*/ */
// TODO 6.0 remove unused `filename`
/** /**
* The parse function parses a component, returning only its abstract syntax tree. * The parse function parses a component, returning only its abstract syntax tree.
* *
@ -104,14 +105,15 @@ export function compileModule(source, options) {
* *
* The `loose` option, available since 5.13.0, tries to always return an AST even if the input will not successfully compile. * The `loose` option, available since 5.13.0, tries to always return an AST even if the input will not successfully compile.
* *
* The `filename` option is unused and will be removed in Svelte 6.0.
*
* @param {string} source * @param {string} source
* @param {{ filename?: string; rootDir?: string; modern?: boolean; loose?: boolean }} [options] * @param {{ filename?: string; rootDir?: string; modern?: boolean; loose?: boolean }} [options]
* @returns {AST.Root | LegacyRoot} * @returns {AST.Root | LegacyRoot}
*/ */
export function parse(source, { filename, rootDir, modern, loose } = {}) { export function parse(source, { modern, loose } = {}) {
source = remove_bom(source); source = remove_bom(source);
state.reset_warning_filter(() => false); state.reset_warning_filter(() => false);
state.reset(source, { filename: filename ?? '(unknown)', rootDir });
const ast = _parse(source, loose); const ast = _parse(source, loose);
return to_public_ast(source, ast, modern); return to_public_ast(source, ast, modern);

@ -9,6 +9,7 @@ import { create_fragment } from './utils/create.js';
import read_options from './read/options.js'; import read_options from './read/options.js';
import { is_reserved } from '../../../utils.js'; import { is_reserved } from '../../../utils.js';
import { disallow_children } from '../2-analyze/visitors/shared/special-element.js'; import { disallow_children } from '../2-analyze/visitors/shared/special-element.js';
import * as state from '../../state.js';
const regex_position_indicator = / \(\d+:\d+\)$/; const regex_position_indicator = / \(\d+:\d+\)$/;
@ -301,6 +302,8 @@ export class Parser {
* @returns {AST.Root} * @returns {AST.Root}
*/ */
export function parse(template, loose = false) { export function parse(template, loose = false) {
state.set_source(template);
const parser = new Parser(template, loose); const parser = new Parser(template, loose);
return parser.root; return parser.root;
} }

@ -30,6 +30,12 @@ export let dev;
export let locator = getLocator('', { offsetLine: 1 }); export let locator = getLocator('', { offsetLine: 1 });
/** @param {string} value */
export function set_source(value) {
source = value;
locator = getLocator(source, { offsetLine: 1 });
}
/** /**
* @param {AST.SvelteNode & { start?: number | undefined }} node * @param {AST.SvelteNode & { start?: number | undefined }} node
*/ */
@ -100,7 +106,7 @@ export function reset(_source, options) {
filename = filename.replace(root_dir, '').replace(/^[/\\]/, ''); filename = filename.replace(root_dir, '').replace(/^[/\\]/, '');
} }
locator = getLocator(source, { offsetLine: 1 }); set_source(source);
warnings = []; warnings = [];
ignore_stack = []; ignore_stack = [];
ignore_map.clear(); ignore_map.clear();

Loading…
Cancel
Save