Should ES begin to standardise pragmas?

# Claus Reinke (11 years ago)

Pragmas (ignorable source hints to an implementation) have become fairly wide-spread in Javascript, but their look and usage differs widely, and with an increasing number of tools defining their own pragma conventions, it is only a matter of time before they start stepping on each others toes.

This seems to call for some guidelines from the language standard (not to define individual pragmas, but to define a framework and namespacing for using and defining pragmas). There is a strawman for this

strawman:pragmas

but it doesn't seem to be part of Harmony yet, and it doesn't mention that pragmas are alread used in the wild, not just in ES5.

Some examples:

  1. ES5 "use strict" ignoreable expression statement

  2. IE conditional compilation @-prefixed, suggested in, but not limited to comments

  3. jslint options (partial overlaps with forks like jshint) in-comment /*jslint /*members ..

  4. google for javascript pragma for more examples (sometimes pragmas for embedded javascript in applications, sometimes options in comments for JS processors)

Apart from standardizing pragma syntax for options, it would be nice to annotate code with language extensions in use (rather than language versions): this is similar to feature- versus browser-version-testing.

There are also some standard pragmas I would like to see, to warn me when I am using features I do not like to use, something like

//OPTIONS: warn-asi
        // trace ASI semicolons, to simplify verification
//OPTIONS: warn-hoisting
        // warn if bindings are lifted out of their block
//OPTIONS: warn-non-standard
        // warn if browser-specific features are used

Below I add some references to examples from another language, Haskell.

Claus

Haskell has pragmas both for declarative optimization hints and for language extensions

www.haskell.org/onlinereport/haskell2010/haskellch12.html#x19-18800012

on top of this, Haskell packages specify the language extensions they use

haskell.org/cabal/release/cabal-latest/doc/users-guide/authors.html#pkg-descr

The currently known language extensions are listed here, for Cabal (package manager) and GHC (compiler)

haskell.org/cabal/release/cabal-latest/doc/API/Cabal/Language-Haskell-Extension.html#t:Extension

www.haskell.org/ghc/docs/7.0-latest/html/users_guide/flag-reference.html#id568865

In addition to language pragmas, there is an inofficial standard for compiler options, eg, GHC understands OPTIONS_GHC pragmas, which make most of GHC's options available in source files (those that make sense on a per-module basis):

www.haskell.org/ghc/docs/7.0-latest/html/users_guide/pragmas.html

The list of options useable in source pragmas includes many warning options

www.haskell.org/ghc/docs/7.0-latest/html/users_guide/flag-reference.html#id570580

# David Herman (11 years ago)

We've talked about this