Skip to content

feat: add $log rune #9670

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

Closed
wants to merge 39 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
5280e00
feat: add $log rune
trueadm Nov 24, 2023
68068b5
fix issues
trueadm Nov 24, 2023
8805366
fix issues
trueadm Nov 24, 2023
c69353c
tune
trueadm Nov 24, 2023
7e347c6
avoid static state reference validation
trueadm Nov 27, 2023
df25640
work around unfortunate browser behavior
dummdidumm Nov 27, 2023
ae3ef2c
call it ExpectedError
dummdidumm Nov 27, 2023
5205bac
cleanup
dummdidumm Nov 27, 2023
9100430
Merge branch 'main' into log-rune
trueadm Nov 27, 2023
22b5289
Fix docs
trueadm Nov 27, 2023
f2162c0
tweaks
trueadm Nov 27, 2023
9d7e915
tweaks
trueadm Nov 27, 2023
aa6ef0f
lint
trueadm Nov 27, 2023
aac227d
merge conflicts
trueadm Nov 27, 2023
dac4fc4
repl, dev: true
trueadm Nov 27, 2023
801bfee
repl dev mode
trueadm Nov 27, 2023
e1299a6
Merge branch 'main' into log-rune
trueadm Nov 27, 2023
3b1e812
Update sites/svelte-5-preview/src/lib/Repl.svelte
Rich-Harris Nov 27, 2023
8964d1a
squelch static-state-reference warning
Rich-Harris Nov 27, 2023
fb813c2
simplify
Rich-Harris Nov 27, 2023
098ce67
remove redundant code
Rich-Harris Nov 27, 2023
902c168
Update packages/svelte/src/main/ambient.d.ts
trueadm Nov 27, 2023
be762fe
Update packages/svelte/src/main/ambient.d.ts
trueadm Nov 27, 2023
be07df0
Update packages/svelte/src/main/ambient.d.ts
trueadm Nov 27, 2023
1c10e14
only pause/trace on change
Rich-Harris Nov 27, 2023
f181547
Merge branch 'log-rune' of github.com:sveltejs/svelte into log-rune
Rich-Harris Nov 27, 2023
765c432
Update packages/svelte/src/main/ambient.d.ts
Rich-Harris Nov 27, 2023
376fc50
Update .changeset/chatty-hotels-grin.md
Rich-Harris Nov 27, 2023
93a2607
Update sites/svelte-5-preview/src/routes/docs/content/01-api/02-runes.md
trueadm Nov 27, 2023
6daa422
$log.break and $log.trace no-op during SSR
Rich-Harris Nov 27, 2023
6b2d267
Update sites/svelte-5-preview/src/routes/docs/content/01-api/02-runes.md
trueadm Nov 27, 2023
21985a3
update test
Rich-Harris Nov 27, 2023
e7d5cd6
Merge branch 'log-rune' of github.com:sveltejs/svelte into log-rune
Rich-Harris Nov 27, 2023
8122453
improve break experience
trueadm Nov 28, 2023
cd2fc8f
fix ts
trueadm Nov 28, 2023
2666e6d
remove unnecessary if (DEV) checks - log runes are removed in prod
Rich-Harris Nov 28, 2023
c6d7fca
ensure hoisting doesnt mess up source maps
dummdidumm Nov 28, 2023
e1cfa14
check visited for cyclical values
trueadm Nov 28, 2023
d89d54d
merge conflict
trueadm Nov 29, 2023
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/chatty-hotels-grin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

feat: add $log rune
9 changes: 9 additions & 0 deletions packages/svelte/src/compiler/phases/2-analyze/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,14 @@ const runes_scope_js_tweaker = {

/** @type {import('./types').Visitors} */
const runes_scope_tweaker = {
CallExpression(node, { state, next }) {
const rune = get_rune(node, state.scope);

// `$log(foo)` should not trigger the `static-state-reference` warning
if (rune?.startsWith('$log')) {
next({ ...state, function_depth: state.function_depth + 1 });
}
},
VariableDeclarator(node, { state }) {
const init = unwrap_ts_expression(node.init);
if (!init || init.type !== 'CallExpression') return;
Expand Down Expand Up @@ -880,6 +888,7 @@ const common_visitors = {
Identifier(node, context) {
const parent = /** @type {import('estree').Node} */ (context.path.at(-1));
if (!is_reference(node, parent)) return;

const binding = context.state.scope.get(node.name);

// if no binding, means some global variable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,8 @@ function get_hoistable_params(node, context) {
params.push(b.id(binding.node.name.slice(1)));
params.push(b.id(binding.node.name));
} else {
params.push(binding.node);
// create a copy to remove start/end tags which would mess up source maps
params.push(b.id(binding.node.name));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,12 @@ export const javascript_visitors_runes = {
for (const declarator of node.declarations) {
const init = unwrap_ts_expression(declarator.init);
const rune = get_rune(init, state.scope);
if (!rune || rune === '$effect.active' || rune === '$effect.root') {
if (
!rune ||
rune === '$effect.active' ||
rune === '$effect.root' ||
rune.startsWith('$log')
) {
if (init != null && is_hoistable_function(init)) {
const hoistable_function = visit(init);
state.hoisted.push(
Expand Down Expand Up @@ -307,6 +312,29 @@ export const javascript_visitors_runes = {
return b.call('$.user_root_effect', ...args);
}

if (rune?.startsWith('$log')) {
if (state.options.dev) {
const args = /** @type {import('estree').Expression[]} */ (
node.arguments.map((arg) => visit(arg))
);

if (rune === '$log.break') {
return b.call(
'$.log_break',
b.thunk(b.array(args)),
b.arrow(
[b.rest(b.id('values'))],
b.block([b.stmt(b.call('console.log', b.spread(b.id('values')))), b.debugger])
)
);
}
const callee = rune === '$log' ? '$.log' : `$.log_${rune.slice(5)}`;
return b.call(callee, b.thunk(b.array(args)));
}

return b.unary('void', b.literal(0));
}

next();
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ const javascript_visitors_runes = {
for (const declarator of node.declarations) {
const init = unwrap_ts_expression(declarator.init);
const rune = get_rune(init, state.scope);
if (!rune || rune === '$effect.active') {
if (!rune || rune === '$effect.active' || rune.startsWith('$log')) {
declarations.push(/** @type {import('estree').VariableDeclarator} */ (visit(declarator)));
continue;
}
Expand Down Expand Up @@ -630,13 +630,27 @@ const javascript_visitors_runes = {
}
context.next();
},
CallExpression(node, { state, next }) {
CallExpression(node, { state, next, visit }) {
const rune = get_rune(node, state.scope);

if (rune === '$effect.active') {
return b.literal(false);
}

if (rune?.startsWith('$log')) {
if ((rune === '$log' || rune === '$log.table') && state.options.dev) {
const callee = rune === '$log' ? 'console.log' : 'console.table';

const args = /** @type {import('estree').Expression[]} */ (
node.arguments.map((arg) => visit(arg))
);

return b.call(callee, ...args);
}

return b.unary('void', b.literal(0));
}

next();
}
};
Expand Down
6 changes: 5 additions & 1 deletion packages/svelte/src/compiler/phases/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ export const Runes = [
'$effect',
'$effect.pre',
'$effect.active',
'$effect.root'
'$effect.root',
'$log',
'$log.break',
'$log.trace',
'$log.table'
];

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/svelte/src/compiler/utils/builders.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export function labeled(name, body) {

/**
* @param {string | import('estree').Expression} callee
* @param {...import('estree').Expression} args
* @param {...(import('estree').Expression | import('estree').SpreadElement)} args
* @returns {import('estree').CallExpression}
*/
export function call(callee, ...args) {
Expand Down
Loading