atom-turbo-javascript icon indicating copy to clipboard operation
atom-turbo-javascript copied to clipboard

Add throw new Error('') snippet

Open piotrwitek opened this issue 9 years ago • 5 comments

Hello, I'm missing throw new snippets could you please add ones for following use cases:

  • throw new Error('${message}); -> tne
  • throw new ${ErrorType()}; -> tnet

piotrwitek avatar Aug 10 '16 13:08 piotrwitek

How about te for throw Error(...) and tne for throw new ErrorType(...)?

The new is necessary only for subclassed errors.

extrabacon avatar Aug 10 '16 14:08 extrabacon

Hi @extrabacon thanks for quick response :) Regarding new I cannot agree. The proper use of creating a new Error object should always be used with new keyword. Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error Please also check examples: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error#Error_types

I think what you are referring to is a generic throw expression; Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/throw

If you have some rationale for your side please reference them, from my perspective not using new is a bad practice and this great extension shouldn't propagate them.

piotrwitek avatar Aug 10 '16 14:08 piotrwitek

Yes, we should use new for Error, but I'm lazy and turbo javascript is all about speed.

Also, it's definitely safe. All built-in error constructors can be used as functions. http://stackoverflow.com/questions/13294658/throw-errormsg-vs-throw-new-errormsg

However, we cannot say the same for subclassed errors, as there is no guarantee that its constructor can be used as a function (unless the implementor added this behavior). Thus, it's only needed in this case. Bonus: it makes it really easy to spot custom errors VS builtin errors, just by using the new operator.

In terms of snippets, tne could have Error as its default value, supporting both scenarios. Just don't use te if you dislike it.

What do you think?

extrabacon avatar Aug 10 '16 14:08 extrabacon

@extrabacon Thanks for explanation you are right that you can use Error without new as it behaves as factory function. This is really fine with me, my concern is that I think they can promote practice that lack consistency, and because of the you could later make silly errors like e.g. forget to add new for subclassed errors. I'm using an eslint rule to always guard me from this kind of bugs.

Regarding snippets I think they look great :)

Thanks a lot!

piotrwitek avatar Aug 10 '16 18:08 piotrwitek

Using linting is definitely a must if you wish to catch such issues. I will go forward with adding the snippets.

extrabacon avatar Aug 10 '16 22:08 extrabacon