-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: provide guidance in browser console when logging
$state
objec…
…ts (#13142) * feat: provide guidance in browser console when logging `$state` objects Wrap console.log/warn/error statements in DEV mode with a check whether or not they contain state objects. Closes #13123 This is an alternative or enhancement to #13070. Alternative if we deem it the better solution. Enhancement because it's not as robust as a custom formatter: We only check the top level of each entry (though we could maybe traverse a few levels), and if you're logging class instances, snapshot currently stops at the boundaries there and so you don't get snapshotted values for these (arguably this is a more general problem of $inspect and $state.snapshot), whereas with custom formatter it doesn't matter at which level you come across it. * lint * use normal warning mechanism, so we can link to docs etc * add a few more methods --------- Co-authored-by: Rich Harris <rich.harris@vercel.com>
- Loading branch information
1 parent
836bc60
commit ed7611b
Showing
7 changed files
with
84 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'svelte': patch | ||
--- | ||
|
||
feat: provide guidance in browser console when logging $state objects |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { STATE_SYMBOL } from '../constants.js'; | ||
import { snapshot } from '../../shared/clone.js'; | ||
import * as w from '../warnings.js'; | ||
|
||
/** | ||
* @param {string} method | ||
* @param {...any} objects | ||
*/ | ||
export function log_if_contains_state(method, ...objects) { | ||
let has_state = false; | ||
const transformed = []; | ||
|
||
for (const obj of objects) { | ||
if (obj && typeof obj === 'object' && STATE_SYMBOL in obj) { | ||
transformed.push(snapshot(obj, true)); | ||
has_state = true; | ||
} else { | ||
transformed.push(obj); | ||
} | ||
} | ||
|
||
if (has_state) { | ||
w.console_log_state(method); | ||
|
||
// eslint-disable-next-line no-console | ||
console.log('%c[snapshot]', 'color: grey', ...transformed); | ||
} | ||
|
||
return objects; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters