Skip to content

Commit c5c54da

Browse files
authored
chore: default options.filename to "(unknown)" (#12997)
* chore: default options.filename to "(unknown)" * fix * regenerate
1 parent 21081d0 commit c5c54da

File tree

14 files changed

+38
-46
lines changed

14 files changed

+38
-46
lines changed

.changeset/mighty-poets-fix.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
chore: default options.filename to "(unknown)"

packages/svelte/src/compiler/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export function compileModule(source, options) {
106106
*/
107107
export function parse(source, { filename, rootDir, modern } = {}) {
108108
state.reset_warning_filter(() => false);
109-
state.reset(source, { filename, rootDir }); // TODO it's weird to require filename/rootDir here. reconsider the API
109+
state.reset(source, { filename: filename ?? '(unknown)', rootDir });
110110

111111
const ast = _parse(source);
112112
return to_public_ast(source, ast, modern);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ export function analyze_module(ast, options) {
243243

244244
return {
245245
module: { ast, scope, scopes },
246-
name: options.filename || 'module',
246+
name: options.filename,
247247
accessors: false,
248248
runes: true,
249249
immutable: true
@@ -349,7 +349,7 @@ export function analyze_component(root, source, options) {
349349
}
350350
}
351351

352-
const component_name = get_component_name(options.filename ?? 'Component');
352+
const component_name = get_component_name(options.filename);
353353

354354
const runes = options.runes ?? Array.from(module.scope.references.keys()).some(is_rune);
355355

@@ -390,7 +390,7 @@ export function analyze_component(root, source, options) {
390390
hash: root.css
391391
? options.cssHash({
392392
css: root.css.content.styles,
393-
filename: options.filename ?? '<unknown>',
393+
filename: options.filename,
394394
name: component_name,
395395
hash
396396
})

packages/svelte/src/compiler/phases/3-transform/client/transform-client.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -505,14 +505,12 @@ export function client_component(analysis, options) {
505505
}
506506

507507
if (dev) {
508-
if (filename) {
509-
// add `App[$.FILENAME] = 'App.svelte'` so that we can print useful messages later
510-
body.unshift(
511-
b.stmt(
512-
b.assignment('=', b.member(b.id(analysis.name), '$.FILENAME', true), b.literal(filename))
513-
)
514-
);
515-
}
508+
// add `App[$.FILENAME] = 'App.svelte'` so that we can print useful messages later
509+
body.unshift(
510+
b.stmt(
511+
b.assignment('=', b.member(b.id(analysis.name), '$.FILENAME', true), b.literal(filename))
512+
)
513+
);
516514

517515
body.unshift(b.stmt(b.call(b.id('$.mark_module_start'))));
518516
body.push(b.stmt(b.call(b.id('$.mark_module_end'), b.id(analysis.name))));

packages/svelte/src/compiler/phases/3-transform/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export function transform_module(analysis, source, options) {
8080
? server_module(analysis, options)
8181
: client_module(analysis, options);
8282

83-
const basename = (options.filename ?? 'Module').split(/[/\\]/).at(-1);
83+
const basename = options.filename.split(/[/\\]/).at(-1);
8484
if (program.body.length > 0) {
8585
program.body[0].leadingComments = [
8686
{

packages/svelte/src/compiler/phases/3-transform/server/transform-server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ export function server_component(analysis, options) {
356356
body.push(b.export_default(component_function));
357357
}
358358

359-
if (dev && filename) {
359+
if (dev) {
360360
// add `App[$.FILENAME] = 'App.svelte'` so that we can print useful messages later
361361
body.unshift(
362362
b.stmt(

packages/svelte/src/compiler/state.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import { getLocator } from 'locate-character';
88
export let warnings = [];
99

1010
/**
11-
* The filename (if specified in the compiler options) relative to the rootDir (if specified).
11+
* The filename relative to the rootDir (if specified).
1212
* This should not be used in the compiler output except in dev mode
13-
* @type {string | undefined}
13+
* @type {string}
1414
*/
1515
export let filename;
1616

@@ -77,20 +77,16 @@ export function is_ignored(node, code) {
7777

7878
/**
7979
* @param {string} _source
80-
* @param {{ dev?: boolean; filename?: string; rootDir?: string }} options
80+
* @param {{ dev?: boolean; filename: string; rootDir?: string }} options
8181
*/
8282
export function reset(_source, options) {
8383
source = _source;
8484
const root_dir = options.rootDir?.replace(/\\/g, '/');
85-
filename = options.filename?.replace(/\\/g, '/');
85+
filename = options.filename.replace(/\\/g, '/');
8686

8787
dev = !!options.dev;
8888

89-
if (
90-
typeof filename === 'string' &&
91-
typeof root_dir === 'string' &&
92-
filename.startsWith(root_dir)
93-
) {
89+
if (typeof root_dir === 'string' && filename.startsWith(root_dir)) {
9490
// make filename relative to rootDir
9591
filename = filename.replace(root_dir, '').replace(/^[/\\]/, '');
9692
}

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export interface CompileError extends ICompileDiagnostic {}
5656

5757
export type CssHashGetter = (args: {
5858
name: string;
59-
filename: string | undefined;
59+
filename: string;
6060
css: string;
6161
hash: (input: string) => string;
6262
}) => string;
@@ -219,11 +219,7 @@ export interface ModuleCompileOptions {
219219

220220
// The following two somewhat scary looking types ensure that certain types are required but can be undefined still
221221

222-
export type ValidatedModuleCompileOptions = Omit<
223-
Required<ModuleCompileOptions>,
224-
'filename' | 'rootDir'
225-
> & {
226-
filename: ModuleCompileOptions['filename'];
222+
export type ValidatedModuleCompileOptions = Omit<Required<ModuleCompileOptions>, 'rootDir'> & {
227223
rootDir: ModuleCompileOptions['rootDir'];
228224
};
229225

packages/svelte/src/compiler/utils/mapped_code.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ export function parse_attached_sourcemap(processed, tag_name) {
393393
*/
394394
export function merge_with_preprocessor_map(result, options, source_name) {
395395
if (options.sourcemap) {
396-
const file_basename = get_basename(options.filename || 'input.svelte');
396+
const file_basename = get_basename(options.filename);
397397
// The preprocessor map is expected to contain `sources: [basename_of_filename]`, but our own
398398
// map may contain a different file name. Patch our map beforehand to align sources so merging
399399
// with the preprocessor map works correctly.
@@ -442,11 +442,10 @@ export function get_basename(filename) {
442442
}
443443

444444
/**
445-
* @param {string | undefined} filename
445+
* @param {string} filename
446446
* @param {string | undefined} output_filename
447447
* @param {string} fallback
448448
*/
449449
export function get_source_name(filename, output_filename, fallback) {
450-
if (!filename) return fallback;
451450
return output_filename ? get_relative_path(output_filename, filename) : get_basename(filename);
452451
}

packages/svelte/src/compiler/validate-options.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import * as w from './warnings.js';
99
*/
1010

1111
const common = {
12-
filename: string(undefined),
12+
filename: string('(unknown)'),
1313

1414
// default to process.cwd() where it exists to replicate svelte4 behavior
1515
// see https://github.com/sveltejs/svelte/blob/b62fc8c8fd2640c9b99168f01b9d958cb2f7574f/packages/svelte/src/compiler/compile/Component.js#L211

packages/svelte/src/internal/client/dom/elements/events.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -310,11 +310,7 @@ export function apply(
310310
handler.apply(element, args);
311311
} else if (has_side_effects || handler != null) {
312312
const filename = component?.[FILENAME];
313-
const location = filename
314-
? loc
315-
? ` at ${filename}:${loc[0]}:${loc[1]}`
316-
: ` in ${filename}`
317-
: '';
313+
const location = loc ? ` at ${filename}:${loc[0]}:${loc[1]}` : ` in ${filename}`;
318314

319315
const event_name = args[0].type;
320316
const description = `\`${event_name}\` handler${location}`;

packages/svelte/src/internal/client/runtime.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -230,12 +230,14 @@ function handle_error(error, effect, component_context) {
230230
let current_context = component_context;
231231

232232
while (current_context !== null) {
233-
/** @type {string} */
234-
var filename = current_context.function?.[FILENAME];
233+
if (DEV) {
234+
/** @type {string} */
235+
var filename = current_context.function?.[FILENAME];
235236

236-
if (filename) {
237-
const file = filename.split('/').pop();
238-
component_stack.push(file);
237+
if (filename) {
238+
const file = filename.split('/').pop();
239+
component_stack.push(file);
240+
}
239241
}
240242

241243
current_context = current_context.p;

packages/svelte/src/internal/client/validate.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export function validate_binding(binding, get_object, get_property, line, column
102102
ran = true;
103103

104104
if (effect.deps === null) {
105-
var location = filename && `${filename}:${line}:${column}`;
105+
var location = `${filename}:${line}:${column}`;
106106
w.binding_property_non_reactive(binding, location);
107107

108108
warned = true;

packages/svelte/types/index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,7 @@ declare module 'svelte/compiler' {
740740

741741
type CssHashGetter = (args: {
742742
name: string;
743-
filename: string | undefined;
743+
filename: string;
744744
css: string;
745745
hash: (input: string) => string;
746746
}) => string;
@@ -2613,7 +2613,7 @@ declare module 'svelte/types/compiler/interfaces' {
26132613

26142614
type CssHashGetter = (args: {
26152615
name: string;
2616-
filename: string | undefined;
2616+
filename: string;
26172617
css: string;
26182618
hash: (input: string) => string;
26192619
}) => string;

0 commit comments

Comments
 (0)