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

* chore: default options.filename to "(unknown)"

* fix

* regenerate
pull/13008/head
Rich Harris 1 year ago committed by GitHub
parent 21081d0fa5
commit c5c54dabcc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
chore: default options.filename to "(unknown)"

@ -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);

@ -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
@ -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);
@ -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
})

@ -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))));

@ -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 = [
{

@ -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(

@ -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;
@ -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(/^[/\\]/, '');
}

@ -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;
@ -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'];
};

@ -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.
@ -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);
}

@ -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

@ -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}`;

@ -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;

@ -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;

@ -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;
@ -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;

Loading…
Cancel
Save