NativeScript / NativeScript/android

console.log should not call object methods

Open
#1,693 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
563
Forks
144
Avg merge
10h 46m
Merged PRs (30d)
14

Description

Environment
Provide version numbers for the following components (information can be retrieved by running tns info in your project folder or by inspecting the package.json of the project):

  • CLI: 8.1.5
  • Cross-platform modules: 6.5.11
  • Android Runtime: 6.5.3
  • iOS Runtime (if applicable): Possibly irrelevant
  • Plugin(s): Possibly irrelevant

Describe the bug
When an object contains a toString or toJSON method, and you print that object through console.log, it will automatically call that function. This is very different from what Chrome console.log does and may cause confusing behavior. For example:
The NativeScript HTTP client returns an object with a property called content that contains an object with a to toJSON method, when you console.log the return object, it will automatically call that toJSON method, while it might not even be JSON, causing the console.log to fail with a weird error from JSON.parse, like Unexpected end of JSON input.

To Reproduce

const randomVar = "test123";
const obj = {
	toJSON: () => "toJSON => this should not be logged " + randomVar
};

console.log(obj);

Expected behavior
I think it should print something like:

{
  "toJSON": ƒ
}

Or, it should catch any errors that happen during the format, and then fall back to it's normal behavior (going to toString, or not printing the property at all). It should not be breaking off the whole console.log and return the exception.

Additional context
Output when doing something like this in Chrome:
image

Output when doing something like this in NS:
image

When causing an error inside the function with:

const obj = {
	toJSON: () => JSON.parse("fakejson")
};

console.log(obj);

In Chrome:
image

In NS:
image

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the Android runtime's console.log object-formatting entry point and reproduce the provided TypeScript snippets, including the failing toJSON example. Compare the output with the Chrome behavior described in the issue; done means logging does not invoke object methods or abort when one throws.

Written by the indexing model from the issue text.

Assessment

Tech stack
android, javascript
Domain
developer-experience, mobile-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.