react / wstein / node_modules / browserify / node_modules / syntax-error / node_modules / acorn / src / options.js
80550 viewsimport {has, isArray} from "./util"1import {SourceLocation} from "./location"23// A second optional argument can be given to further configure4// the parser process. These options are recognized:56export const defaultOptions = {7// `ecmaVersion` indicates the ECMAScript version to parse. Must8// be either 3, or 5, or 6. This influences support for strict9// mode, the set of reserved words, support for getters and10// setters and other features.11ecmaVersion: 5,12// Source type ("script" or "module") for different semantics13sourceType: "script",14// `onInsertedSemicolon` can be a callback that will be called15// when a semicolon is automatically inserted. It will be passed16// th position of the comma as an offset, and if `locations` is17// enabled, it is given the location as a `{line, column}` object18// as second argument.19onInsertedSemicolon: null,20// `onTrailingComma` is similar to `onInsertedSemicolon`, but for21// trailing commas.22onTrailingComma: null,23// By default, reserved words are not enforced. Disable24// `allowReserved` to enforce them. When this option has the25// value "never", reserved words and keywords can also not be26// used as property names.27allowReserved: true,28// When enabled, a return at the top level is not considered an29// error.30allowReturnOutsideFunction: false,31// When enabled, import/export statements are not constrained to32// appearing at the top of the program.33allowImportExportEverywhere: false,34// When enabled, hashbang directive in the beginning of file35// is allowed and treated as a line comment.36allowHashBang: false,37// When `locations` is on, `loc` properties holding objects with38// `start` and `end` properties in `{line, column}` form (with39// line being 1-based and column 0-based) will be attached to the40// nodes.41locations: false,42// A function can be passed as `onToken` option, which will43// cause Acorn to call that function with object in the same44// format as tokenize() returns. Note that you are not45// allowed to call the parser from the callback—that will46// corrupt its internal state.47onToken: null,48// A function can be passed as `onComment` option, which will49// cause Acorn to call that function with `(block, text, start,50// end)` parameters whenever a comment is skipped. `block` is a51// boolean indicating whether this is a block (`/* */`) comment,52// `text` is the content of the comment, and `start` and `end` are53// character offsets that denote the start and end of the comment.54// When the `locations` option is on, two more parameters are55// passed, the full `{line, column}` locations of the start and56// end of the comments. Note that you are not allowed to call the57// parser from the callback—that will corrupt its internal state.58onComment: null,59// Nodes have their start and end characters offsets recorded in60// `start` and `end` properties (directly on the node, rather than61// the `loc` object, which holds line/column data. To also add a62// [semi-standardized][range] `range` property holding a `[start,63// end]` array with the same numbers, set the `ranges` option to64// `true`.65//66// [range]: https://bugzilla.mozilla.org/show_bug.cgi?id=74567867ranges: false,68// It is possible to parse multiple files into a single AST by69// passing the tree produced by parsing the first file as70// `program` option in subsequent parses. This will add the71// toplevel forms of the parsed file to the `Program` (top) node72// of an existing parse tree.73program: null,74// When `locations` is on, you can pass this to record the source75// file in every node's `loc` object.76sourceFile: null,77// This value, if given, is stored in every node, whether78// `locations` is on or off.79directSourceFile: null,80// When enabled, parenthesized expressions are represented by81// (non-standard) ParenthesizedExpression nodes82preserveParens: false,83plugins: {}84}8586// Interpret and default an options object8788export function getOptions(opts) {89let options = {}90for (let opt in defaultOptions)91options[opt] = opts && has(opts, opt) ? opts[opt] : defaultOptions[opt]9293if (isArray(options.onToken)) {94let tokens = options.onToken95options.onToken = (token) => tokens.push(token)96}97if (isArray(options.onComment))98options.onComment = pushComment(options, options.onComment)99100return options101}102103function pushComment(options, array) {104return function (block, text, start, end, startLoc, endLoc) {105let comment = {106type: block ? 'Block' : 'Line',107value: text,108start: start,109end: end110}111if (options.locations)112comment.loc = new SourceLocation(this, startLoc, endLoc)113if (options.ranges)114comment.range = [start, end]115array.push(comment)116}117}118119120121