Skip to content

Commit eb530c8

Browse files
authored
chore: move stuff from analysis into global compiler state (#16268)
* break out locator stuff from the rest of global state * move some stuff around * tighten up * make runes globally available * make component_name globally available * unused
1 parent b673145 commit eb530c8

File tree

6 files changed

+57
-20
lines changed

6 files changed

+57
-20
lines changed

packages/svelte/src/compiler/index.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@ export { default as preprocess } from './preprocess/index.js';
2020
*/
2121
export function compile(source, options) {
2222
source = remove_bom(source);
23-
state.reset_warning_filter(options.warningFilter);
23+
state.reset_warnings(options.warningFilter);
2424
const validated = validate_component_options(options, '');
25-
state.reset(source, validated);
2625

2726
let parsed = _parse(source);
2827

@@ -64,9 +63,8 @@ export function compile(source, options) {
6463
*/
6564
export function compileModule(source, options) {
6665
source = remove_bom(source);
67-
state.reset_warning_filter(options.warningFilter);
66+
state.reset_warnings(options.warningFilter);
6867
const validated = validate_module_options(options, '');
69-
state.reset(source, validated);
7068

7169
const analysis = analyze_module(source, validated);
7270
return transform_module(analysis, source, validated);
@@ -96,6 +94,7 @@ export function compileModule(source, options) {
9694
* @returns {Record<string, any>}
9795
*/
9896

97+
// TODO 6.0 remove unused `filename`
9998
/**
10099
* The parse function parses a component, returning only its abstract syntax tree.
101100
*
@@ -104,14 +103,15 @@ export function compileModule(source, options) {
104103
*
105104
* The `loose` option, available since 5.13.0, tries to always return an AST even if the input will not successfully compile.
106105
*
106+
* The `filename` option is unused and will be removed in Svelte 6.0.
107+
*
107108
* @param {string} source
108109
* @param {{ filename?: string; rootDir?: string; modern?: boolean; loose?: boolean }} [options]
109110
* @returns {AST.Root | LegacyRoot}
110111
*/
111-
export function parse(source, { filename, rootDir, modern, loose } = {}) {
112+
export function parse(source, { modern, loose } = {}) {
112113
source = remove_bom(source);
113-
state.reset_warning_filter(() => false);
114-
state.reset(source, { filename: filename ?? '(unknown)', rootDir });
114+
state.reset_warnings(() => false);
115115

116116
const ast = _parse(source, loose);
117117
return to_public_ast(source, ast, modern);

packages/svelte/src/compiler/migrate/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { parse } from '../phases/1-parse/index.js';
99
import { regex_valid_component_name } from '../phases/1-parse/state/element.js';
1010
import { analyze_component } from '../phases/2-analyze/index.js';
1111
import { get_rune } from '../phases/scope.js';
12-
import { reset, reset_warning_filter } from '../state.js';
12+
import { reset, reset_warnings } from '../state.js';
1313
import {
1414
extract_identifiers,
1515
extract_all_identifiers_from_expression,
@@ -134,8 +134,7 @@ export function migrate(source, { filename, use_ts } = {}) {
134134
return start + style_placeholder + end;
135135
});
136136

137-
reset_warning_filter(() => false);
138-
reset(source, { filename: filename ?? '(unknown)' });
137+
reset_warnings(() => false);
139138

140139
let parsed = parse(source);
141140

packages/svelte/src/compiler/phases/1-parse/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { create_fragment } from './utils/create.js';
99
import read_options from './read/options.js';
1010
import { is_reserved } from '../../../utils.js';
1111
import { disallow_children } from '../2-analyze/visitors/shared/special-element.js';
12+
import * as state from '../../state.js';
1213

1314
const regex_position_indicator = / \(\d+:\d+\)$/;
1415

@@ -301,6 +302,8 @@ export class Parser {
301302
* @returns {AST.Root}
302303
*/
303304
export function parse(template, loose = false) {
305+
state.set_source(template);
306+
304307
const parser = new Parser(template, loose);
305308
return parser.root;
306309
}

packages/svelte/src/compiler/phases/2-analyze/index.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ import { UseDirective } from './visitors/UseDirective.js';
7676
import { VariableDeclarator } from './visitors/VariableDeclarator.js';
7777
import is_reference from 'is-reference';
7878
import { mark_subtree_dynamic } from './visitors/shared/fragment.js';
79+
import * as state from '../../state.js';
7980

8081
/**
8182
* @type {Visitors}
@@ -240,6 +241,7 @@ export function analyze_module(source, options) {
240241
/** @type {AST.JSComment[]} */
241242
const comments = [];
242243

244+
state.set_source(source);
243245
const ast = parse(source, comments, false, false);
244246

245247
const { scope, scopes } = create_scopes(ast, new ScopeRoot(), false, null);
@@ -269,6 +271,13 @@ export function analyze_module(source, options) {
269271
classes: new Map()
270272
};
271273

274+
state.reset({
275+
dev: options.dev,
276+
filename: options.filename,
277+
rootDir: options.rootDir,
278+
runes: true
279+
});
280+
272281
walk(
273282
/** @type {Node} */ (ast),
274283
{
@@ -506,6 +515,14 @@ export function analyze_component(root, source, options) {
506515
snippets: new Set()
507516
};
508517

518+
state.reset({
519+
component_name: analysis.name,
520+
dev: options.dev,
521+
filename: options.filename,
522+
rootDir: options.rootDir,
523+
runes: true
524+
});
525+
509526
if (!runes) {
510527
// every exported `let` or `var` declaration becomes a prop, everything else becomes an export
511528
for (const node of instance.ast.body) {

packages/svelte/src/compiler/phases/types.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ export interface ReactiveStatement {
3434
*/
3535
export interface Analysis {
3636
module: Js;
37+
/** @deprecated use `component_name` from `state.js` instead */
3738
name: string; // TODO should this be filename? it's used in `compileModule` as well as `compile`
39+
/** @deprecated use `runes` from `state.js` instead */
3840
runes: boolean;
3941
immutable: boolean;
4042
tracing: boolean;
@@ -90,6 +92,7 @@ export interface ComponentAnalysis extends Analysis {
9092
keyframes: string[];
9193
has_global: boolean;
9294
};
95+
/** @deprecated use `source` from `state.js` instead */
9396
source: string;
9497
undefined_exports: Map<string, Node>;
9598
/**

packages/svelte/src/compiler/state.js

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ export let warnings = [];
1616
*/
1717
export let filename;
1818

19+
export let component_name = '<unknown>';
20+
1921
/**
2022
* The original source code
2123
* @type {string}
@@ -28,8 +30,16 @@ export let source;
2830
*/
2931
export let dev;
3032

33+
export let runes = false;
34+
3135
export let locator = getLocator('', { offsetLine: 1 });
3236

37+
/** @param {string} value */
38+
export function set_source(value) {
39+
source = value;
40+
locator = getLocator(source, { offsetLine: 1 });
41+
}
42+
3343
/**
3444
* @param {AST.SvelteNode & { start?: number | undefined }} node
3545
*/
@@ -71,8 +81,9 @@ export function pop_ignore() {
7181
*
7282
* @param {(warning: Warning) => boolean} fn
7383
*/
74-
export function reset_warning_filter(fn = () => true) {
84+
export function reset_warnings(fn = () => true) {
7585
warning_filter = fn;
86+
warnings = [];
7687
}
7788

7889
/**
@@ -85,23 +96,27 @@ export function is_ignored(node, code) {
8596
}
8697

8798
/**
88-
* @param {string} _source
89-
* @param {{ dev?: boolean; filename: string; rootDir?: string }} options
99+
* @param {{
100+
* dev: boolean;
101+
* filename: string;
102+
* component_name?: string;
103+
* rootDir?: string;
104+
* runes: boolean;
105+
* }} state
90106
*/
91-
export function reset(_source, options) {
92-
source = _source;
93-
const root_dir = options.rootDir?.replace(/\\/g, '/');
94-
filename = options.filename.replace(/\\/g, '/');
107+
export function reset(state) {
108+
const root_dir = state.rootDir?.replace(/\\/g, '/');
109+
filename = state.filename.replace(/\\/g, '/');
95110

96-
dev = !!options.dev;
111+
dev = state.dev;
112+
runes = state.runes;
113+
component_name = state.component_name ?? '(unknown)';
97114

98115
if (typeof root_dir === 'string' && filename.startsWith(root_dir)) {
99116
// make filename relative to rootDir
100117
filename = filename.replace(root_dir, '').replace(/^[/\\]/, '');
101118
}
102119

103-
locator = getLocator(source, { offsetLine: 1 });
104-
warnings = [];
105120
ignore_stack = [];
106121
ignore_map.clear();
107122
}

0 commit comments

Comments
 (0)