[d565449] | 1 | /* eslint-disable jsdoc/no-multi-asterisks -- needed to preserve original formatting of licences */
|
---|
| 2 |
|
---|
| 3 | /**
|
---|
| 4 | * @fileoverview Main Espree file that converts Acorn into Esprima output.
|
---|
| 5 | *
|
---|
| 6 | * This file contains code from the following MIT-licensed projects:
|
---|
| 7 | * 1. Acorn
|
---|
| 8 | * 2. Babylon
|
---|
| 9 | * 3. Babel-ESLint
|
---|
| 10 | *
|
---|
| 11 | * This file also contains code from Esprima, which is BSD licensed.
|
---|
| 12 | *
|
---|
| 13 | * Acorn is Copyright 2012-2015 Acorn Contributors (https://github.com/marijnh/acorn/blob/master/AUTHORS)
|
---|
| 14 | * Babylon is Copyright 2014-2015 various contributors (https://github.com/babel/babel/blob/master/packages/babylon/AUTHORS)
|
---|
| 15 | * Babel-ESLint is Copyright 2014-2015 Sebastian McKenzie <sebmck@gmail.com>
|
---|
| 16 | *
|
---|
| 17 | * Redistribution and use in source and binary forms, with or without
|
---|
| 18 | * modification, are permitted provided that the following conditions are met:
|
---|
| 19 | *
|
---|
| 20 | * * Redistributions of source code must retain the above copyright
|
---|
| 21 | * notice, this list of conditions and the following disclaimer.
|
---|
| 22 | * * Redistributions in binary form must reproduce the above copyright
|
---|
| 23 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 24 | * documentation and/or other materials provided with the distribution.
|
---|
| 25 | *
|
---|
| 26 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
---|
| 27 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
| 28 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
---|
| 29 | * ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
|
---|
| 30 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
---|
| 31 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
---|
| 32 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
---|
| 33 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 34 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 35 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 36 | *
|
---|
| 37 | * Esprima is Copyright (c) jQuery Foundation, Inc. and Contributors, All Rights Reserved.
|
---|
| 38 | *
|
---|
| 39 | * Redistribution and use in source and binary forms, with or without
|
---|
| 40 | * modification, are permitted provided that the following conditions are met:
|
---|
| 41 | *
|
---|
| 42 | * * Redistributions of source code must retain the above copyright
|
---|
| 43 | * notice, this list of conditions and the following disclaimer.
|
---|
| 44 | * * Redistributions in binary form must reproduce the above copyright
|
---|
| 45 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 46 | * documentation and/or other materials provided with the distribution.
|
---|
| 47 | *
|
---|
| 48 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
---|
| 49 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
| 50 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
---|
| 51 | * ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
|
---|
| 52 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
---|
| 53 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
---|
| 54 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
---|
| 55 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 56 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 57 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 58 | */
|
---|
| 59 |
|
---|
| 60 | /* eslint-enable jsdoc/no-multi-asterisks -- needed to preserve original formatting of licences */
|
---|
| 61 |
|
---|
| 62 | import * as acorn from "acorn";
|
---|
| 63 | import jsx from "acorn-jsx";
|
---|
| 64 | import espree from "./lib/espree.js";
|
---|
| 65 | import espreeVersion from "./lib/version.js";
|
---|
| 66 | import * as visitorKeys from "eslint-visitor-keys";
|
---|
| 67 | import { getLatestEcmaVersion, getSupportedEcmaVersions } from "./lib/options.js";
|
---|
| 68 |
|
---|
| 69 |
|
---|
| 70 | // To initialize lazily.
|
---|
| 71 | const parsers = {
|
---|
| 72 | _regular: null,
|
---|
| 73 | _jsx: null,
|
---|
| 74 |
|
---|
| 75 | get regular() {
|
---|
| 76 | if (this._regular === null) {
|
---|
| 77 | this._regular = acorn.Parser.extend(espree());
|
---|
| 78 | }
|
---|
| 79 | return this._regular;
|
---|
| 80 | },
|
---|
| 81 |
|
---|
| 82 | get jsx() {
|
---|
| 83 | if (this._jsx === null) {
|
---|
| 84 | this._jsx = acorn.Parser.extend(jsx(), espree());
|
---|
| 85 | }
|
---|
| 86 | return this._jsx;
|
---|
| 87 | },
|
---|
| 88 |
|
---|
| 89 | get(options) {
|
---|
| 90 | const useJsx = Boolean(
|
---|
| 91 | options &&
|
---|
| 92 | options.ecmaFeatures &&
|
---|
| 93 | options.ecmaFeatures.jsx
|
---|
| 94 | );
|
---|
| 95 |
|
---|
| 96 | return useJsx ? this.jsx : this.regular;
|
---|
| 97 | }
|
---|
| 98 | };
|
---|
| 99 |
|
---|
| 100 | //------------------------------------------------------------------------------
|
---|
| 101 | // Tokenizer
|
---|
| 102 | //------------------------------------------------------------------------------
|
---|
| 103 |
|
---|
| 104 | /**
|
---|
| 105 | * Tokenizes the given code.
|
---|
| 106 | * @param {string} code The code to tokenize.
|
---|
| 107 | * @param {Object} options Options defining how to tokenize.
|
---|
| 108 | * @returns {Token[]} An array of tokens.
|
---|
| 109 | * @throws {SyntaxError} If the input code is invalid.
|
---|
| 110 | * @private
|
---|
| 111 | */
|
---|
| 112 | export function tokenize(code, options) {
|
---|
| 113 | const Parser = parsers.get(options);
|
---|
| 114 |
|
---|
| 115 | // Ensure to collect tokens.
|
---|
| 116 | if (!options || options.tokens !== true) {
|
---|
| 117 | options = Object.assign({}, options, { tokens: true }); // eslint-disable-line no-param-reassign -- stylistic choice
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | return new Parser(options, code).tokenize();
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | //------------------------------------------------------------------------------
|
---|
| 124 | // Parser
|
---|
| 125 | //------------------------------------------------------------------------------
|
---|
| 126 |
|
---|
| 127 | /**
|
---|
| 128 | * Parses the given code.
|
---|
| 129 | * @param {string} code The code to tokenize.
|
---|
| 130 | * @param {Object} options Options defining how to tokenize.
|
---|
| 131 | * @returns {ASTNode} The "Program" AST node.
|
---|
| 132 | * @throws {SyntaxError} If the input code is invalid.
|
---|
| 133 | */
|
---|
| 134 | export function parse(code, options) {
|
---|
| 135 | const Parser = parsers.get(options);
|
---|
| 136 |
|
---|
| 137 | return new Parser(options, code).parse();
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | //------------------------------------------------------------------------------
|
---|
| 141 | // Public
|
---|
| 142 | //------------------------------------------------------------------------------
|
---|
| 143 |
|
---|
| 144 | export const version = espreeVersion;
|
---|
| 145 | export const name = "espree";
|
---|
| 146 |
|
---|
| 147 | /* istanbul ignore next */
|
---|
| 148 | export const VisitorKeys = (function() {
|
---|
| 149 | return visitorKeys.KEYS;
|
---|
| 150 | }());
|
---|
| 151 |
|
---|
| 152 | // Derive node types from VisitorKeys
|
---|
| 153 | /* istanbul ignore next */
|
---|
| 154 | export const Syntax = (function() {
|
---|
| 155 | let key,
|
---|
| 156 | types = {};
|
---|
| 157 |
|
---|
| 158 | if (typeof Object.create === "function") {
|
---|
| 159 | types = Object.create(null);
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | for (key in VisitorKeys) {
|
---|
| 163 | if (Object.hasOwnProperty.call(VisitorKeys, key)) {
|
---|
| 164 | types[key] = key;
|
---|
| 165 | }
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | if (typeof Object.freeze === "function") {
|
---|
| 169 | Object.freeze(types);
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | return types;
|
---|
| 173 | }());
|
---|
| 174 |
|
---|
| 175 | export const latestEcmaVersion = getLatestEcmaVersion();
|
---|
| 176 |
|
---|
| 177 | export const supportedEcmaVersions = getSupportedEcmaVersions();
|
---|