Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80559 views
1
import {has, isArray} from "./util"
2
import {SourceLocation} from "./location"
3
4
// A second optional argument can be given to further configure
5
// the parser process. These options are recognized:
6
7
export const defaultOptions = {
8
// `ecmaVersion` indicates the ECMAScript version to parse. Must
9
// be either 3, or 5, or 6. This influences support for strict
10
// mode, the set of reserved words, support for getters and
11
// setters and other features.
12
ecmaVersion: 5,
13
// Source type ("script" or "module") for different semantics
14
sourceType: "script",
15
// `onInsertedSemicolon` can be a callback that will be called
16
// when a semicolon is automatically inserted. It will be passed
17
// th position of the comma as an offset, and if `locations` is
18
// enabled, it is given the location as a `{line, column}` object
19
// as second argument.
20
onInsertedSemicolon: null,
21
// `onTrailingComma` is similar to `onInsertedSemicolon`, but for
22
// trailing commas.
23
onTrailingComma: null,
24
// By default, reserved words are not enforced. Disable
25
// `allowReserved` to enforce them. When this option has the
26
// value "never", reserved words and keywords can also not be
27
// used as property names.
28
allowReserved: true,
29
// When enabled, a return at the top level is not considered an
30
// error.
31
allowReturnOutsideFunction: false,
32
// When enabled, import/export statements are not constrained to
33
// appearing at the top of the program.
34
allowImportExportEverywhere: false,
35
// When enabled, hashbang directive in the beginning of file
36
// is allowed and treated as a line comment.
37
allowHashBang: false,
38
// When `locations` is on, `loc` properties holding objects with
39
// `start` and `end` properties in `{line, column}` form (with
40
// line being 1-based and column 0-based) will be attached to the
41
// nodes.
42
locations: false,
43
// A function can be passed as `onToken` option, which will
44
// cause Acorn to call that function with object in the same
45
// format as tokenize() returns. Note that you are not
46
// allowed to call the parser from the callback—that will
47
// corrupt its internal state.
48
onToken: null,
49
// A function can be passed as `onComment` option, which will
50
// cause Acorn to call that function with `(block, text, start,
51
// end)` parameters whenever a comment is skipped. `block` is a
52
// boolean indicating whether this is a block (`/* */`) comment,
53
// `text` is the content of the comment, and `start` and `end` are
54
// character offsets that denote the start and end of the comment.
55
// When the `locations` option is on, two more parameters are
56
// passed, the full `{line, column}` locations of the start and
57
// end of the comments. Note that you are not allowed to call the
58
// parser from the callback—that will corrupt its internal state.
59
onComment: null,
60
// Nodes have their start and end characters offsets recorded in
61
// `start` and `end` properties (directly on the node, rather than
62
// the `loc` object, which holds line/column data. To also add a
63
// [semi-standardized][range] `range` property holding a `[start,
64
// end]` array with the same numbers, set the `ranges` option to
65
// `true`.
66
//
67
// [range]: https://bugzilla.mozilla.org/show_bug.cgi?id=745678
68
ranges: false,
69
// It is possible to parse multiple files into a single AST by
70
// passing the tree produced by parsing the first file as
71
// `program` option in subsequent parses. This will add the
72
// toplevel forms of the parsed file to the `Program` (top) node
73
// of an existing parse tree.
74
program: null,
75
// When `locations` is on, you can pass this to record the source
76
// file in every node's `loc` object.
77
sourceFile: null,
78
// This value, if given, is stored in every node, whether
79
// `locations` is on or off.
80
directSourceFile: null,
81
// When enabled, parenthesized expressions are represented by
82
// (non-standard) ParenthesizedExpression nodes
83
preserveParens: false,
84
plugins: {}
85
}
86
87
// Interpret and default an options object
88
89
export function getOptions(opts) {
90
let options = {}
91
for (let opt in defaultOptions)
92
options[opt] = opts && has(opts, opt) ? opts[opt] : defaultOptions[opt]
93
94
if (isArray(options.onToken)) {
95
let tokens = options.onToken
96
options.onToken = (token) => tokens.push(token)
97
}
98
if (isArray(options.onComment))
99
options.onComment = pushComment(options, options.onComment)
100
101
return options
102
}
103
104
function pushComment(options, array) {
105
return function (block, text, start, end, startLoc, endLoc) {
106
let comment = {
107
type: block ? 'Block' : 'Line',
108
value: text,
109
start: start,
110
end: end
111
}
112
if (options.locations)
113
comment.loc = new SourceLocation(this, startLoc, endLoc)
114
if (options.ranges)
115
comment.range = [start, end]
116
array.push(comment)
117
}
118
}
119
120
121