NativeScript / NativeScript/android

console.log should not call object methods

Offen
#1,693 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Vorherrschende Sprache
C++
Sterne
563
Forks
144
Ø Merge
10 Std. 46 Min.
Gemergte PRs (30 T.)
14

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne am Einstiegspunkt der Objektformatierung von console.log in der Android runtime und reproduziere die bereitgestellten TypeScript-Snippets, einschließlich des fehlschlagenden toJSON-Beispiels. Vergleiche die Ausgabe mit dem im Issue beschriebenen Verhalten von Chrome; abgeschlossen ist die Aufgabe, wenn das Logging keine Objektmethoden aufruft und nicht abbricht, wenn eine davon eine Exception wirft.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
android, javascript
Bereich
developer-experience, mobile-dev
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.