UNPKG

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