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 } = {}) { export function parse(source, { filename, rootDir, modern } = {}) {
state.reset_warning_filter(() => false); 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); const ast = _parse(source);
return to_public_ast(source, ast, modern); return to_public_ast(source, ast, modern);

@ -243,7 +243,7 @@ export function analyze_module(ast, options) {
return { return {
module: { ast, scope, scopes }, module: { ast, scope, scopes },
name: options.filename || 'module', name: options.filename,
accessors: false, accessors: false,
runes: true, runes: true,
immutable: 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); 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 hash: root.css
? options.cssHash({ ? options.cssHash({
css: root.css.content.styles, css: root.css.content.styles,
filename: options.filename ?? '<unknown>', filename: options.filename,
name: component_name, name: component_name,
hash hash
}) })

@ -505,14 +505,12 @@ export function client_component(analysis, options) {
} }
if (dev) { if (dev) {
if (filename) {
// add `App[$.FILENAME] = 'App.svelte'` so that we can print useful messages later // add `App[$.FILENAME] = 'App.svelte'` so that we can print useful messages later
body.unshift( body.unshift(
b.stmt( b.stmt(
b.assignment('=', b.member(b.id(analysis.name), '$.FILENAME', true), b.literal(filename)) 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.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)))); 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) ? server_module(analysis, options)
: client_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) { if (program.body.length > 0) {
program.body[0].leadingComments = [ program.body[0].leadingComments = [
{ {

@ -356,7 +356,7 @@ export function server_component(analysis, options) {
body.push(b.export_default(component_function)); 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 // add `App[$.FILENAME] = 'App.svelte'` so that we can print useful messages later
body.unshift( body.unshift(
b.stmt( b.stmt(

@ -8,9 +8,9 @@ import { getLocator } from 'locate-character';
export let warnings = []; 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 * This should not be used in the compiler output except in dev mode
* @type {string | undefined} * @type {string}
*/ */
export let filename; export let filename;
@ -77,20 +77,16 @@ export function is_ignored(node, code) {
/** /**
* @param {string} _source * @param {string} _source
* @param {{ dev?: boolean; filename?: string; rootDir?: string }} options * @param {{ dev?: boolean; filename: string; rootDir?: string }} options
*/ */
export function reset(_source, options) { export function reset(_source, options) {
source = _source; source = _source;
const root_dir = options.rootDir?.replace(/\\/g, '/'); const root_dir = options.rootDir?.replace(/\\/g, '/');
filename = options.filename?.replace(/\\/g, '/'); filename = options.filename.replace(/\\/g, '/');
dev = !!options.dev; dev = !!options.dev;
if ( if (typeof root_dir === 'string' && filename.startsWith(root_dir)) {
typeof filename === 'string' &&
typeof root_dir === 'string' &&
filename.startsWith(root_dir)
) {
// make filename relative to rootDir // make filename relative to rootDir
filename = filename.replace(root_dir, '').replace(/^[/\\]/, ''); filename = filename.replace(root_dir, '').replace(/^[/\\]/, '');
} }

@ -56,7 +56,7 @@ export interface CompileError extends ICompileDiagnostic {}
export type CssHashGetter = (args: { export type CssHashGetter = (args: {
name: string; name: string;
filename: string | undefined; filename: string;
css: string; css: string;
hash: (input: string) => string; hash: (input: string) => 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 // The following two somewhat scary looking types ensure that certain types are required but can be undefined still
export type ValidatedModuleCompileOptions = Omit< export type ValidatedModuleCompileOptions = Omit<Required<ModuleCompileOptions>, 'rootDir'> & {
Required<ModuleCompileOptions>,
'filename' | 'rootDir'
> & {
filename: ModuleCompileOptions['filename'];
rootDir: 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) { export function merge_with_preprocessor_map(result, options, source_name) {
if (options.sourcemap) { 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 // 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 // map may contain a different file name. Patch our map beforehand to align sources so merging
// with the preprocessor map works correctly. // 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 | undefined} output_filename
* @param {string} fallback * @param {string} fallback
*/ */
export function get_source_name(filename, output_filename, 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); return output_filename ? get_relative_path(output_filename, filename) : get_basename(filename);
} }

@ -9,7 +9,7 @@ import * as w from './warnings.js';
*/ */
const common = { const common = {
filename: string(undefined), filename: string('(unknown)'),
// default to process.cwd() where it exists to replicate svelte4 behavior // 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 // 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); handler.apply(element, args);
} else if (has_side_effects || handler != null) { } else if (has_side_effects || handler != null) {
const filename = component?.[FILENAME]; const filename = component?.[FILENAME];
const location = filename const location = loc ? ` at ${filename}:${loc[0]}:${loc[1]}` : ` in ${filename}`;
? loc
? ` at ${filename}:${loc[0]}:${loc[1]}`
: ` in ${filename}`
: '';
const event_name = args[0].type; const event_name = args[0].type;
const description = `\`${event_name}\` handler${location}`; const description = `\`${event_name}\` handler${location}`;

@ -230,6 +230,7 @@ function handle_error(error, effect, component_context) {
let current_context = component_context; let current_context = component_context;
while (current_context !== null) { while (current_context !== null) {
if (DEV) {
/** @type {string} */ /** @type {string} */
var filename = current_context.function?.[FILENAME]; var filename = current_context.function?.[FILENAME];
@ -237,6 +238,7 @@ function handle_error(error, effect, component_context) {
const file = filename.split('/').pop(); const file = filename.split('/').pop();
component_stack.push(file); component_stack.push(file);
} }
}
current_context = current_context.p; current_context = current_context.p;
} }

@ -102,7 +102,7 @@ export function validate_binding(binding, get_object, get_property, line, column
ran = true; ran = true;
if (effect.deps === null) { if (effect.deps === null) {
var location = filename && `${filename}:${line}:${column}`; var location = `${filename}:${line}:${column}`;
w.binding_property_non_reactive(binding, location); w.binding_property_non_reactive(binding, location);
warned = true; warned = true;

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

Loading…
Cancel
Save