CoffeeScript `use strict` #1547
|
+1 |
|
@josh: there's now a link at the bottom of issues that allows you to {,un}subscribe without posting. It's actually been there for a few months now. |
|
@michaelficarra: @josh works at GitHub ;) In any case, I think this ticket makes a very strong argument for having CoffeeScript be The only real downsides here are the inability to use the FutureReservedWords as identifiers ... but it seems like folks don't tend to do that: |
|
So let's clarify this proposal. Do we enforce strict mode restrictions everywhere or only in contexts that begin with a Side note: the |
|
edit: never mind, @geraldalewis already got it |
|
Good point @michaelficarra Since From @kangax's ES5 Browser Compatibility Table:
non-
That'd probably break some users' code if they're not expecting it (not that there isn't Related to the Would love to hear about the bug [1] ES5 Annotated Spec 10.6 Section 1 |
|
I think if we do this, we enforce our static strict mode restrictions everywhere, but we do not put any |
|
Agreed @jashkenas -- I just came back to clarify that the early |
|
In the interest of deepening the discussion, it was proposed in #1002 (duplicate parameter names) that instead of throwing an error, CoffeeScript could rewrite the offending code. The following CoffeeScript snippets show:
For the record, I think
13 Function Definition (13.1 Strict Mode Restrictions)
The
Obfuscation and IntentHowever, these code transformations would subvert the intention behind the |
I think the conversion is better for some, but not all, strict mode restrictions.
+1
+1
I'd prefer this conversion:
-1 no way to preserve semantics:
-1 no way to preserve semantics:
-1 no way to preserve semantics:
-1 no way to preserve semantics:
-1 no way to preserve semantics:
-1 no way to preserve semantics:
No, it should definitely be a
agreed. |
|
JSLint throws an error if you don't include the This may lead to what @BrendanEich calls "strict quirks mode". Raising * Unless the "sloppy" flag is set to true, something I doubt a dev who uses JSLint would like to do. By calling it "sloppy", Crockford has, frustratingly, created a moral imperative for employing "use strict" (like Knuth did [tongue-in-cheek] with Literate Programming -- "nobody wants to admit to an illiterate program.") |
|
|
|
I am also wondering about the "\0" octal escape sequences being disallowed as well. If @satyr is correct these are allowed in JS even in strict mode. I have an SQL escape function that needs to replace "\0" with "\0" and now it won't compile. Any reason why, or is this a bug? |
|
SpiderMonkey shell:
The special case is for "\0" only, not anything else truly octal or "noctal" (not quite octal, e.g. \08, \09, etc.). @devongovett: it's impossible to say what is going wrong without seeing that function, but it's not anything to do with strict mode as spec'ed or implemented in Firefox. /be |
|
@BrendanEich Well here is the function: escapeSQL = (val) ->
val = val.replace /[\0\n\r\b\t\\\'\"\x1a]/g, (s) ->
switch s
when "\0" then "\\0"
when "\n" then "\\n"
when "\r" then "\\r"
when "\b" then "\\b"
when "\t" then "\\t"
when "\x1a" then "\\Z"
else "\\" + s
return "'" + val + "'"The error is thrown by the CoffeeScript compiler, not the JS side of things. CoffeeScript apparently isn't special casing "\0" like SpiderMonkey, V8 and probably others do. Luckily, this is workaroundable for the time being by using "\u0000" instead of "\0", but it still should be fixed. |
|
@devongovett new issue on file? /be |
|
@devongovett: This should be fixed by 46ff770. Go ahead and try that out. |
|
+1 |
|
Related to #566 (that is open)... My question more is: would it be possible to at least add it as an option? I'd be glad to have it feature-flagged, but we'd want to give it a try (on server). Or is it |
|
+1 : Cannot delete any variable.
|
use strict
ECMAScript 5 introduces a
strictmode that reduces bugs, increases security, and eliminates some difficult to use language features. The coding standardsstrictmode requires should be adopted by CoffeeScript.Better Code
CoffeeScript emits lowest common denominator JavaScript. From a different angle: CoffeeScript emits JavaScript that is not implementation specific. Just as there are constraints on output imposed by IE6's nonconforming behavior (no
function declarations), there too should be constraints on input imposed by better engineered environments.User Expectations
CoffeeScript throws errors when it encounters syntax errors (e.g., one can't write
-> return return). A user employing theuse strictdirective will expect to see syntax errors at compile time (pre-evaluation), just as they would in a JavaScript environment.Best Practices
CoffeeScript emits JavaScript that adheres to best practices. ES5
strictcodifies many best practices, and fixes some unsafe and error-prone operations. CoffeeScript already shares some constraints withstrict(e.g., nowith) and fixes others thatstricttargets (e.g., in CoffeeScript, vars are auto-declared for variable safety).Early Errors
"Early errors" are syntax errors detected prior to evaluation (all others are runtime errors). Most
strictmode errors are syntax errors (of typeSyntaxError). CoffeeScript should only check for these types of errors. 16 Errors states: "An implementation shall not treat other kinds of errors as early errors even if the compiler can prove that a construct cannot execute without error under any circumstances" (emphasis mine).Early Strict Syntax Errors
The following CoffeeScript snippets would be considered "early errors" under ES5
strict.7.8.3 Numeric Literals
B.1.2 String Literals
11.1.5 Object Initialiser
11.4.1 The delete Operator
13 Function Definition (13.1 Strict Mode Restrictions)
7.6.1.2 Future Reserved Words
evalandargumentsrestrictions12.14 The try Statement (12.14.1 Strict Mode Restrictions)
13 Function Definition (13.1 Strict Mode Restrictions)
11.13.1 Simple Assignment
11.3.1 Postfix Increment Operator
as well as Postfix Decrement and Prefix Increment and Decrement
What Users Would Give Up
Some coding conventions would need to be abandoned, namely:
Identical param names
e.g.,
However, as @jashkenas stated "...is a serious antipattern. For the reader who comes across your function, intending to modify it ... not only are they unable to use the first three parameters -- they also have not the slightest clue what the parameters might be."
@satyr also offered a workaround:
({},{},{},buffer) -> #read bufferwould still be legalIdentical prop names in object literals
@satyr's example:
I'd argue that it's likely this feature is more often the cause of bugs
foo: 1, bar: 2, bar: 3 # user meant 'baz'and catching those errors would outweigh its other uses.(and one could still write):
Sources/More Information
Annotated ES5 Spec
ES5 Spec: Annex C "The Strict Mode of ECMAScript "
JavaScript Strict Mode Tests
ECMA-262-5 in detail. by Dmitry Soshnikov
ES5 Browser Compatibility Table by @kangax