Skip to content

chore: handle option warnings the same as others #11303

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 1 commit into from
Apr 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
24 changes: 22 additions & 2 deletions packages/svelte/messages/compile-warnings/options.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
## missing_custom_element_compile_option
## options_deprecated_immutable

The 'customElement' option is used when generating a custom element. Did you forget the 'customElement: true' compile option?
The `immutable` option has been deprecated. It will have no effect in runes mode

## options_missing_custom_element

The `customElement` option is used when generating a custom element. Did you forget the `customElement: true` compile option?

## options_renamed_ssr_dom

`generate: "dom"` and `generate: "ssr"` options have been renamed to "client" and "server" respectively

## options_removed_enable_sourcemap

The `enableSourcemap` option has been removed. Source maps are always generated now, and tooling can choose to ignore them

## options_removed_hydratable

The `hydratable` option has been removed. Svelte components are always hydratable now

## options_removed_loop_guard_timeout

The `loopGuardTimeout` option has been removed
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function reset_warnings(options) {
*/
function w(node, code, message) {
// @ts-expect-error
if (node.ignores?.has(code)) return;
if (node?.ignores?.has(code)) return;

warnings.push({
code,
Expand Down
2 changes: 1 addition & 1 deletion packages/svelte/src/compiler/phases/2-analyze/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ export function analyze_component(root, source, options) {
};

if (!options.customElement && root.options?.customElement) {
w.missing_custom_element_compile_option(root.options);
w.options_missing_custom_element(root.options);
}

if (analysis.runes) {
Expand Down
43 changes: 17 additions & 26 deletions packages/svelte/src/compiler/validate-options.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as e from './errors.js';
import * as w from './warnings.js';

/**
* @template [Input=any]
Expand All @@ -13,9 +14,7 @@ const common = {

generate: validator('client', (input, keypath) => {
if (input === 'dom' || input === 'ssr') {
warn(
'`generate: "dom"` and `generate: "ssr"` options have been renamed to "client" and "server" respectively'
);
warn_once(w.options_renamed_ssr_dom);
return input === 'dom' ? 'client' : 'server';
}

Expand Down Expand Up @@ -72,16 +71,13 @@ export const validate_component_options =

discloseVersion: boolean(true),

immutable: deprecate(
'The immutable option has been deprecated. It has no effect in runes mode.',
boolean(false)
),
immutable: deprecate(w.options_deprecated_immutable, boolean(false)),

legacy: object({
componentApi: boolean(false)
}),

loopGuardTimeout: warn_removed('The loopGuardTimeout option has been removed.'),
loopGuardTimeout: warn_removed(w.options_removed_loop_guard_timeout),

name: string(undefined),

Expand All @@ -103,12 +99,8 @@ export const validate_component_options =
return input;
}),

enableSourcemap: warn_removed(
'The enableSourcemap option has been removed. Source maps are always generated now, and tooling can choose to ignore them.'
),
hydratable: warn_removed(
'The hydratable option has been removed. Svelte components are always hydratable now.'
),
enableSourcemap: warn_removed(w.options_removed_enable_sourcemap),
hydratable: warn_removed(w.options_removed_hydratable),
format: removed(
'The format option has been removed in Svelte 4, the compiler only outputs ESM now. Remove "format" from your compiler options. ' +
'If you did not set this yourself, bump the version of your bundler plugin (vite-plugin-svelte/rollup-plugin-svelte/svelte-loader)'
Expand Down Expand Up @@ -149,34 +141,33 @@ function removed(msg) {

const warned = new Set();

/** @param {string} message */
function warn(message) {
if (!warned.has(message)) {
warned.add(message);
// eslint-disable-next-line no-console
console.warn(message);
/** @param {(node: null) => void} fn */
function warn_once(fn) {
if (!warned.has(fn)) {
warned.add(fn);
fn(null);
}
}

/**
* @param {string} message
* @param {(node: null) => void} fn
* @returns {Validator}
*/
function warn_removed(message) {
function warn_removed(fn) {
return (input) => {
if (input !== undefined) warn(message);
if (input !== undefined) warn_once(fn);
return /** @type {any} */ (undefined);
};
}

/**
* @param {string} message
* @param {(node: null) => void} fn
* @param {Validator} validator
* @returns {Validator}
*/
function deprecate(message, validator) {
function deprecate(fn, validator) {
return (input, keypath) => {
if (input !== undefined) warn(message);
if (input !== undefined) warn_once(fn);
return validator(input, keypath);
};
}
Expand Down
48 changes: 44 additions & 4 deletions packages/svelte/src/compiler/warnings.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function reset_warnings(options) {
*/
function w(node, code, message) {
// @ts-expect-error
if (node.ignores?.has(code)) return;
if (node?.ignores?.has(code)) return;

warnings.push({
code,
Expand Down Expand Up @@ -545,11 +545,51 @@ export function invalid_self_closing_tag(node, name) {
}

/**
* The 'customElement' option is used when generating a custom element. Did you forget the 'customElement: true' compile option?
* The `immutable` option has been deprecated. It will have no effect in runes mode
* @param {null | NodeLike} node
*/
export function missing_custom_element_compile_option(node) {
w(node, "missing_custom_element_compile_option", "The 'customElement' option is used when generating a custom element. Did you forget the 'customElement: true' compile option?");
export function options_deprecated_immutable(node) {
w(node, "options_deprecated_immutable", "The `immutable` option has been deprecated. It will have no effect in runes mode");
}

/**
* The `customElement` option is used when generating a custom element. Did you forget the `customElement: true` compile option?
* @param {null | NodeLike} node
*/
export function options_missing_custom_element(node) {
w(node, "options_missing_custom_element", "The `customElement` option is used when generating a custom element. Did you forget the `customElement: true` compile option?");
}

/**
* `generate: "dom"` and `generate: "ssr"` options have been renamed to "client" and "server" respectively
* @param {null | NodeLike} node
*/
export function options_renamed_ssr_dom(node) {
w(node, "options_renamed_ssr_dom", "`generate: \"dom\"` and `generate: \"ssr\"` options have been renamed to \"client\" and \"server\" respectively");
}

/**
* The `enableSourcemap` option has been removed. Source maps are always generated now, and tooling can choose to ignore them
* @param {null | NodeLike} node
*/
export function options_removed_enable_sourcemap(node) {
w(node, "options_removed_enable_sourcemap", "The `enableSourcemap` option has been removed. Source maps are always generated now, and tooling can choose to ignore them");
}

/**
* The `hydratable` option has been removed. Svelte components are always hydratable now
* @param {null | NodeLike} node
*/
export function options_removed_hydratable(node) {
w(node, "options_removed_hydratable", "The `hydratable` option has been removed. Svelte components are always hydratable now");
}

/**
* The `loopGuardTimeout` option has been removed
* @param {null | NodeLike} node
*/
export function options_removed_loop_guard_timeout(node) {
w(node, "options_removed_loop_guard_timeout", "The `loopGuardTimeout` option has been removed");
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[
{
"code": "missing_custom_element_compile_option",
"message": "The 'customElement' option is used when generating a custom element. Did you forget the 'customElement: true' compile option?",
"code": "options_missing_custom_element",
"message": "The `customElement` option is used when generating a custom element. Did you forget the `customElement: true` compile option?",
"start": {
"line": 1,
"column": 0
Expand Down