Skip to content

Commit

Permalink
Make ecmaVersion a required option
Browse files Browse the repository at this point in the history
(With a warning that defaults it to 2020 for the time being.)
  • Loading branch information
marijnh committed Aug 3, 2020
1 parent 4b6f481 commit 3bf92f5
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 22 deletions.
2 changes: 1 addition & 1 deletion acorn-loose/README.md
Expand Up @@ -52,7 +52,7 @@ on the `acorn` package, because it uses the same tokenizer.

```javascript
var acornLoose = require("acorn-loose");
console.log(acornLoose.parse("1 / * 4 )[2]"));
console.log(acornLoose.parse("1 / * 4 )[2]", {ecmaVersion: 2020}));
```

Like the regular parser, the loose parser supports plugins. You can
Expand Down
27 changes: 14 additions & 13 deletions acorn/README.md
Expand Up @@ -32,14 +32,14 @@ npm install
## Interface

**parse**`(input, options)` is the main interface to the library. The
`input` parameter is a string, `options` can be undefined or an object
setting some of the options listed below. The return value will be an
abstract syntax tree object as specified by the [ESTree
`input` parameter is a string, `options` must be an object setting
some of the options listed below. The return value will be an abstract
syntax tree object as specified by the [ESTree
spec](https://github.com/estree/estree).

```javascript
let acorn = require("acorn");
console.log(acorn.parse("1 + 1"));
console.log(acorn.parse("1 + 1", {ecmaVersion: 2020}));
```

When encountering a syntax error, the parser will raise a
Expand All @@ -48,18 +48,19 @@ have a `pos` property that indicates the string offset at which the
error occurred, and a `loc` object that contains a `{line, column}`
object referring to that same position.

Options can be provided by passing a second argument, which should be
an object containing any of these fields:
Options are provided by in a second argument, which should be an
object containing any of these fields (only `ecmaVersion` is
required):

- **ecmaVersion**: Indicates the ECMAScript version to parse. Must be
either 3, 5, 6 (2015), 7 (2016), 8 (2017), 9 (2018), 10 (2019) or 11
(2020, partial support). This influences support for strict mode,
the set of reserved words, and support for new syntax features.
Default is 10.
either 3, 5, 6 (or 2015), 7 (2016), 8 (2017), 9 (2018), 10 (2019),
11 (2020), or 12 (2021, partial support), or `"latest"` (the latest
the library supports). This influences support for strict mode, the
set of reserved words, and support for new syntax features.

**NOTE**: Only 'stage 4' (finalized) ECMAScript features are being
implemented by Acorn. Other proposed new features can be implemented
through plugins.
implemented by Acorn. Other proposed new features must be
implemented through plugins.

- **sourceType**: Indicate the mode the code should be parsed in. Can be
either `"script"` or `"module"`. This influences global strict mode
Expand Down Expand Up @@ -224,7 +225,7 @@ you can use its static `extend` method.
var acorn = require("acorn");
var jsx = require("acorn-jsx");
var JSXParser = acorn.Parser.extend(jsx());
JSXParser.parse("foo(<bar/>)");
JSXParser.parse("foo(<bar/>)", {ecmaVersion: 2020});
```

The `extend` method takes any number of plugin values, and returns a
Expand Down
1 change: 1 addition & 0 deletions acorn/src/.eslintrc
Expand Up @@ -12,6 +12,7 @@
"new-parens": "off",
"no-case-declarations": "off",
"no-cond-assign": "off",
"no-console": ["error", { allow: ["warn", "error"] }],
"no-fallthrough": "off",
"no-labels": "off",
"no-mixed-operators": "off",
Expand Down
27 changes: 19 additions & 8 deletions acorn/src/options.js
@@ -1,16 +1,16 @@
import {has, isArray} from "./util"
import {SourceLocation} from "./locutil"

// A second optional argument can be given to further configure
// the parser process. These options are recognized:
// A second argument must be given to configure the parser process.
// These options are recognized (only `ecmaVersion` is required):

export const defaultOptions = {
// `ecmaVersion` indicates the ECMAScript version to parse. Must be
// either 3, 5, 6 (2015), 7 (2016), 8 (2017), 9 (2018), or 10
// (2019). This influences support for strict mode, the set of
// reserved words, and support for new syntax features. The default
// is 10.
ecmaVersion: 10,
// either 3, 5, 6 (or 2015), 7 (2016), 8 (2017), 9 (2018), 10
// (2019), 11 (2020), 12 (2021), or `"latest"` (the latest version
// the library supports). This influences support for strict mode,
// the set of reserved words, and support for new syntax features.
ecmaVersion: null,
// `sourceType` indicates the mode the code should be parsed in.
// Can be either `"script"` or `"module"`. This influences global
// strict mode and parsing of `import` and `export` declarations.
Expand Down Expand Up @@ -91,14 +91,25 @@ export const defaultOptions = {

// Interpret and default an options object

let warnedAboutEcmaVersion = false

export function getOptions(opts) {
let options = {}

for (let opt in defaultOptions)
options[opt] = opts && has(opts, opt) ? opts[opt] : defaultOptions[opt]

if (options.ecmaVersion >= 2015)
if (options.ecmaVersion === "latest") {
options.ecmaVersion = 1e8
} else if (options.ecmaVersion == null) {
if (!warnedAboutEcmaVersion && typeof console === "object" && console.warn) {
warnedAboutEcmaVersion = true
console.warn("Since Acorn 8.0.0, options.ecmaVersion is required.\nDefaulting to 2020, but this will stop working in the future.")
}
options.ecmaVersion = 11
} else if (options.ecmaVersion >= 2015) {
options.ecmaVersion -= 2009
}

if (options.allowReserved == null)
options.allowReserved = options.ecmaVersion < 5
Expand Down

0 comments on commit 3bf92f5

Please sign in to comment.