Upgrade to https://github.com/acornjs/acorn.git commit 84eda6bf
[jst.git] / src / options.js
index e7fa9c5..8467c1c 100644 (file)
@@ -1,23 +1,24 @@
-import {has, isArray} from "./util"
-import {SourceLocation} from "./locutil"
+import {hasOwn, isArray} from "./util.js"
+import {SourceLocation} from "./locutil.js"
 
-// 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 9.
-  ecmaVersion: 9,
+  // either 3, 5, 6 (or 2015), 7 (2016), 8 (2017), 9 (2018), 10
+  // (2019), 11 (2020), 12 (2021), 13 (2022), 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.
   sourceType: "script",
   // `onInsertedSemicolon` can be a callback that will be called
   // when a semicolon is automatically inserted. It will be passed
-  // th position of the comma as an offset, and if `locations` is
+  // the position of the comma as an offset, and if `locations` is
   // enabled, it is given the location as a `{line, column}` object
   // as second argument.
   onInsertedSemicolon: null,
@@ -33,11 +34,16 @@ export const defaultOptions = {
   // error.
   allowReturnOutsideFunction: false,
   // When enabled, import/export statements are not constrained to
-  // appearing at the top of the program.
+  // appearing at the top of the program, and an import.meta expression
+  // in a script isn't considered an error.
   allowImportExportEverywhere: false,
+  // By default, await identifiers are allowed to appear at the top-level scope only if ecmaVersion >= 2022.
   // When enabled, await identifiers are allowed to appear at the top-level scope,
   // but they are still not allowed in non-async functions.
-  allowAwaitOutsideFunction: false,
+  allowAwaitOutsideFunction: null,
+  // When enabled, super identifiers are not constrained to
+  // appearing in methods and do not raise an error when they appear elsewhere.
+  allowSuperOutsideMethod: null,
   // When enabled, hashbang directive in the beginning of file
   // is allowed and treated as a line comment.
   allowHashBang: false,
@@ -91,14 +97,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]
+    options[opt] = opts && hasOwn(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