Skip to content

chore: default options.filename to "(unknown)" #12997

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/mighty-poets-fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

chore: default options.filename to "(unknown)"
2 changes: 1 addition & 1 deletion packages/svelte/src/compiler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export function compileModule(source, options) {
*/
export function parse(source, { filename, rootDir, modern } = {}) {
state.reset_warning_filter(() => false);
state.reset(source, { filename, rootDir }); // TODO it's weird to require filename/rootDir here. reconsider the API
state.reset(source, { filename: filename ?? '(unknown)', rootDir });

const ast = _parse(source);
return to_public_ast(source, ast, modern);
Expand Down
6 changes: 3 additions & 3 deletions packages/svelte/src/compiler/phases/2-analyze/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ export function analyze_module(ast, options) {

return {
module: { ast, scope, scopes },
name: options.filename || 'module',
name: options.filename,
accessors: false,
runes: true,
immutable: true
Expand Down Expand Up @@ -349,7 +349,7 @@ export function analyze_component(root, source, options) {
}
}

const component_name = get_component_name(options.filename ?? 'Component');
const component_name = get_component_name(options.filename);

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

Expand Down Expand Up @@ -390,7 +390,7 @@ export function analyze_component(root, source, options) {
hash: root.css
? options.cssHash({
css: root.css.content.styles,
filename: options.filename ?? '<unknown>',
filename: options.filename,
name: component_name,
hash
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -505,14 +505,12 @@ export function client_component(analysis, options) {
}

if (dev) {
if (filename) {
// add `App[$.FILENAME] = 'App.svelte'` so that we can print useful messages later
body.unshift(
b.stmt(
b.assignment('=', b.member(b.id(analysis.name), '$.FILENAME', true), b.literal(filename))
)
);
}
// add `App[$.FILENAME] = 'App.svelte'` so that we can print useful messages later
body.unshift(
b.stmt(
b.assignment('=', b.member(b.id(analysis.name), '$.FILENAME', true), b.literal(filename))
)
);

body.unshift(b.stmt(b.call(b.id('$.mark_module_start'))));
body.push(b.stmt(b.call(b.id('$.mark_module_end'), b.id(analysis.name))));
Expand Down
2 changes: 1 addition & 1 deletion packages/svelte/src/compiler/phases/3-transform/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export function transform_module(analysis, source, options) {
? server_module(analysis, options)
: client_module(analysis, options);

const basename = (options.filename ?? 'Module').split(/[/\\]/).at(-1);
const basename = options.filename.split(/[/\\]/).at(-1);
if (program.body.length > 0) {
program.body[0].leadingComments = [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ export function server_component(analysis, options) {
body.push(b.export_default(component_function));
}

if (dev && filename) {
if (dev) {
// add `App[$.FILENAME] = 'App.svelte'` so that we can print useful messages later
body.unshift(
b.stmt(
Expand Down
14 changes: 5 additions & 9 deletions packages/svelte/src/compiler/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import { getLocator } from 'locate-character';
export let warnings = [];

/**
* The filename (if specified in the compiler options) relative to the rootDir (if specified).
* The filename relative to the rootDir (if specified).
* This should not be used in the compiler output except in dev mode
* @type {string | undefined}
* @type {string}
*/
export let filename;

Expand Down Expand Up @@ -77,20 +77,16 @@ export function is_ignored(node, code) {

/**
* @param {string} _source
* @param {{ dev?: boolean; filename?: string; rootDir?: string }} options
* @param {{ dev?: boolean; filename: string; rootDir?: string }} options
*/
export function reset(_source, options) {
source = _source;
const root_dir = options.rootDir?.replace(/\\/g, '/');
filename = options.filename?.replace(/\\/g, '/');
filename = options.filename.replace(/\\/g, '/');

dev = !!options.dev;

if (
typeof filename === 'string' &&
typeof root_dir === 'string' &&
filename.startsWith(root_dir)
) {
if (typeof root_dir === 'string' && filename.startsWith(root_dir)) {
// make filename relative to rootDir
filename = filename.replace(root_dir, '').replace(/^[/\\]/, '');
}
Expand Down
8 changes: 2 additions & 6 deletions packages/svelte/src/compiler/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export interface CompileError extends ICompileDiagnostic {}

export type CssHashGetter = (args: {
name: string;
filename: string | undefined;
filename: string;
css: string;
hash: (input: string) => string;
}) => string;
Expand Down Expand Up @@ -219,11 +219,7 @@ export interface ModuleCompileOptions {

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

export type ValidatedModuleCompileOptions = Omit<
Required<ModuleCompileOptions>,
'filename' | 'rootDir'
> & {
filename: ModuleCompileOptions['filename'];
export type ValidatedModuleCompileOptions = Omit<Required<ModuleCompileOptions>, 'rootDir'> & {
rootDir: ModuleCompileOptions['rootDir'];
};

Expand Down
5 changes: 2 additions & 3 deletions packages/svelte/src/compiler/utils/mapped_code.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ export function parse_attached_sourcemap(processed, tag_name) {
*/
export function merge_with_preprocessor_map(result, options, source_name) {
if (options.sourcemap) {
const file_basename = get_basename(options.filename || 'input.svelte');
const file_basename = get_basename(options.filename);
// The preprocessor map is expected to contain `sources: [basename_of_filename]`, but our own
// map may contain a different file name. Patch our map beforehand to align sources so merging
// with the preprocessor map works correctly.
Expand Down Expand Up @@ -442,11 +442,10 @@ export function get_basename(filename) {
}

/**
* @param {string | undefined} filename
* @param {string} filename
* @param {string | undefined} output_filename
* @param {string} fallback
*/
export function get_source_name(filename, output_filename, fallback) {
if (!filename) return fallback;
return output_filename ? get_relative_path(output_filename, filename) : get_basename(filename);
}
2 changes: 1 addition & 1 deletion packages/svelte/src/compiler/validate-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import * as w from './warnings.js';
*/

const common = {
filename: string(undefined),
filename: string('(unknown)'),

// default to process.cwd() where it exists to replicate svelte4 behavior
// see https://github.com/sveltejs/svelte/blob/b62fc8c8fd2640c9b99168f01b9d958cb2f7574f/packages/svelte/src/compiler/compile/Component.js#L211
Expand Down
6 changes: 1 addition & 5 deletions packages/svelte/src/internal/client/dom/elements/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,11 +310,7 @@ export function apply(
handler.apply(element, args);
} else if (has_side_effects || handler != null) {
const filename = component?.[FILENAME];
const location = filename
? loc
? ` at ${filename}:${loc[0]}:${loc[1]}`
: ` in ${filename}`
: '';
const location = loc ? ` at ${filename}:${loc[0]}:${loc[1]}` : ` in ${filename}`;

const event_name = args[0].type;
const description = `\`${event_name}\` handler${location}`;
Expand Down
12 changes: 7 additions & 5 deletions packages/svelte/src/internal/client/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,12 +230,14 @@ function handle_error(error, effect, component_context) {
let current_context = component_context;

while (current_context !== null) {
/** @type {string} */
var filename = current_context.function?.[FILENAME];
if (DEV) {
/** @type {string} */
var filename = current_context.function?.[FILENAME];

if (filename) {
const file = filename.split('/').pop();
component_stack.push(file);
if (filename) {
const file = filename.split('/').pop();
component_stack.push(file);
}
}

current_context = current_context.p;
Expand Down
2 changes: 1 addition & 1 deletion packages/svelte/src/internal/client/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export function validate_binding(binding, get_object, get_property, line, column
ran = true;

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

warned = true;
Expand Down
4 changes: 2 additions & 2 deletions packages/svelte/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ declare module 'svelte/compiler' {

type CssHashGetter = (args: {
name: string;
filename: string | undefined;
filename: string;
css: string;
hash: (input: string) => string;
}) => string;
Expand Down Expand Up @@ -2613,7 +2613,7 @@ declare module 'svelte/types/compiler/interfaces' {

type CssHashGetter = (args: {
name: string;
filename: string | undefined;
filename: string;
css: string;
hash: (input: string) => string;
}) => string;
Expand Down
Loading