question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

minify-type-constructors shouldn’t rewrite `String(symbol)`

See original GitHub issue

Input

var symbol = Symbol();
String(symbol);
// → 'Symbol()'

Current output:

var symbol = Symbol();
symbol+"";
// → throws TypeError: Cannot convert a Symbol value to a string

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
mathiasbynenscommented, Aug 27, 2016

Note that String(x) and x + "" (or "" + x) are not equivalent in general. Here’s another snippet that doesn’t use symbols but still produces different output after minification:

var object = {
  toString() { return '33'; },
  valueOf() { return 99; }
};

String(object);
// → '33'

// After minification, this becomes:
object+"";
// → '99'

Another example:

var object = {
  toString() { console.log('toString'); return '33'; },
  valueOf() { console.log('valueOf'); return 99; }
};

String(object);
// 1. logs `toString`
// 2. returns '33'

// After minification, this becomes:
object+"";
// 1. logs `valueOf`
// 2. returns '99'

Here’s a more elaborate example:

var object = {
  toString: _ => { console.log('toString'); return {}; },
  valueOf: _ => { console.log('valueOf'); return {}; }
};

try { String(object); } catch (exception) { console.log(exception); }
// 1. toString
// 2. valueOf
// 3. TypeError: Cannot convert object to primitive value

// After minification, this becomes something like:
try { object+""; } catch (exception) { console.log(exception); }
// 1. valueOf
// 2. toString
// 3. TypeError: Cannot convert object to primitive value
0reactions
boopathicommented, Jan 19, 2017

This is merged and now there is an option to disable only String() type constructors.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Class renaming breaks certain patterns #510 - evanw/esbuild
When the esbuild bundler hits a conflicting class name it appends a number, which breaks the pattern. There are several potential ways to ......
Read more >
TypeScript/JavaScript minification - Stack Overflow
Now, at some point in my code I do something like this: class Test { testProperty: any; constructor() { this.getMethods ...
Read more >
API Reference - Terser
Assuming installation via NPM, you can load Terser in your application like this: const { minify } = require("terser");.
Read more >
Documentation - tsc CLI Options - TypeScript
Flag Type Default ‑‑allowJs boolean false ‑‑allowUmdGlobalAccess boolean false ‑‑allowUnreachableCode boolean
Read more >
Code Golfing Tips & Tricks: How to Minify your JavaScript Code
Note: Do not use in fast loops, because the milliseconds might not change! Strings. Prefer slice over substr over substring. Prefer slice(start, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found