CS2 Discussion: Features: Flag for ESNext output from current compiler #34
Description
This is a proposal for generating ES2015+ syntax from the current compiler. It’s rather simple. We add a flag:
--ecmascript Output the latest ECMAScript syntax whenever possible.
To implement this, in the compiler itself we can either have if
blocks within node classes that generate different output based on whether this flag is set; or we can have a second set of node classes that override the first set if this flag is set.
This flag would only apply to features like await
, where we have a PR that implements an ES5 polyfill but others might prefer the await
keyword output as is; and features like =>
that are supported in ECMAScript now and people might prefer to output them as is rather than polyfilling them. Eventually this flag could allow us to automatically output let
or const
as appropriate, without worrying about breaking backward compatibility. Modules support would be unaffected by this flag, as using the import
or export
keywords would be opting in to ECMAScript for those lines; there’s no point in making such lines throw an error if --ecmascript
is unset. Likewise for generators. With regard to classes, I’m assuming based on #22 that we will likely implement a new keyword that produces ECMAScript classes, leaving the current class
keyword alone and allowing projects to contain both types; so those keywords would also be unaffected by this flag.
This flag isn’t meant to foreclose the effort at creating a new compiler; on the contrary, it might pave the way for it. We can implement this flag now, with maybe just one or two features compiled differently, with the list growing over time; but when the new compiler is ready and can handle all the same features that the legacy compiler plus --ecmascript
can handle, the --ecmascript
flag could trigger opting into the new compiler.