Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compiling "throw" leads to unexpected behavior with mocha testing #2314

Closed
zemirco opened this issue May 11, 2012 · 5 comments
Closed

compiling "throw" leads to unexpected behavior with mocha testing #2314

zemirco opened this issue May 11, 2012 · 5 comments
Labels

Comments

@zemirco
Copy link

zemirco commented May 11, 2012

I'm having trouble testing an error message with mocha, should and coffeescript.

Why is the following statement compiled the way it is?

( -> throw new Error()).should.throw()

compiles into

(function() {
  throw new Error();
}).should["throw"]();

I thought it should be

(function(){
  throw new Error('fail');
}).should.throw();
@michaelficarra
Copy link
Collaborator

throw is a reserved word and in ES3, reserved words were not allowed as the right operand in a member access. Fortunately, you can access arbitrarily-named members of an object by giving the indexing operator a string. The compilations are functionally identical.

@zemirco
Copy link
Author

zemirco commented May 11, 2012

could you please give an example?

@michaelficarra
Copy link
Collaborator

a.b is equivalent to a['b']

@zemirco
Copy link
Author

zemirco commented May 11, 2012

cool, thank you!

@michaelficarra
Copy link
Collaborator

For those interested in the technical details behind this:

In the ECMAScript spec (section 7.6 of ES5; the end of 5.1.5 in ES3), an Identifier is defined as an IdentifierName that is not in the set of reserved words. ES5 section 11.2.1 states that the dot in a member access is followed by an IdentifierName. ES3 section 11.2.1 erroneously stated that it is followed by an Identifier, disallowing reserved words as the right operand of the member access operator.

@vendethiel vendethiel mentioned this issue Mar 25, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants