Skip to content

Commit b2ecf7a

Browse files
committed
Default allowHashBang to true when ecmaVersion >= 2023
FEATURE: Support hashbang comments by default in ECMAScript 2023 and later. Closes #1134
1 parent 05d86d8 commit b2ecf7a

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

acorn/README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ required):
5454

5555
- **ecmaVersion**: Indicates the ECMAScript version to parse. Must be
5656
either 3, 5, 6 (or 2015), 7 (2016), 8 (2017), 9 (2018), 10 (2019),
57-
11 (2020), 12 (2021), 13 (2022, partial support)
58-
or `"latest"` (the latest the library supports). This influences
59-
support for strict mode, the set of reserved words, and support
60-
for new syntax features.
57+
11 (2020), 12 (2021), 13 (2022), 14 (2023), or `"latest"` (the
58+
latest the library supports). This influences support for strict
59+
mode, the set of reserved words, and support for new syntax
60+
features.
6161

6262
**NOTE**: Only 'stage 4' (finalized) ECMAScript features are being
6363
implemented by Acorn. Other proposed new features must be
@@ -104,9 +104,9 @@ required):
104104
- **allowSuperOutsideMethod**: By default, `super` outside a method
105105
raises an error. Set this to `true` to accept such code.
106106

107-
- **allowHashBang**: When this is enabled (off by default), if the
108-
code starts with the characters `#!` (as in a shellscript), the
109-
first line will be treated as a comment.
107+
- **allowHashBang**: When this is enabled, if the code starts with the
108+
characters `#!` (as in a shellscript), the first line will be
109+
treated as a comment. Defaults to true when `ecmaVersion` >= 2023.
110110

111111
- **locations**: When `true`, each node has a `loc` object attached
112112
with `start` and `end` subobjects, each of which contains the

acorn/src/options.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import {SourceLocation} from "./locutil.js"
77
export const defaultOptions = {
88
// `ecmaVersion` indicates the ECMAScript version to parse. Must be
99
// either 3, 5, 6 (or 2015), 7 (2016), 8 (2017), 9 (2018), 10
10-
// (2019), 11 (2020), 12 (2021), 13 (2022), or `"latest"` (the
11-
// latest version the library supports). This influences support
12-
// for strict mode, the set of reserved words, and support for
13-
// new syntax features.
10+
// (2019), 11 (2020), 12 (2021), 13 (2022), 14 (2023), or `"latest"`
11+
// (the latest version the library supports). This influences
12+
// support for strict mode, the set of reserved words, and support
13+
// for new syntax features.
1414
ecmaVersion: null,
1515
// `sourceType` indicates the mode the code should be parsed in.
1616
// Can be either `"script"` or `"module"`. This influences global
@@ -44,8 +44,9 @@ export const defaultOptions = {
4444
// When enabled, super identifiers are not constrained to
4545
// appearing in methods and do not raise an error when they appear elsewhere.
4646
allowSuperOutsideMethod: null,
47-
// When enabled, hashbang directive in the beginning of file
48-
// is allowed and treated as a line comment.
47+
// When enabled, hashbang directive in the beginning of file is
48+
// allowed and treated as a line comment. Enabled by default when
49+
// `ecmaVersion` >= 2023.
4950
allowHashBang: false,
5051
// When `locations` is on, `loc` properties holding objects with
5152
// `start` and `end` properties in `{line, column}` form (with
@@ -120,6 +121,9 @@ export function getOptions(opts) {
120121
if (options.allowReserved == null)
121122
options.allowReserved = options.ecmaVersion < 5
122123

124+
if (opts.allowHashBang == null)
125+
options.allowHashBang = options.ecmaVersion >= 14
126+
123127
if (isArray(options.onToken)) {
124128
let tokens = options.onToken
125129
options.onToken = (token) => tokens.push(token)

0 commit comments

Comments
 (0)