UNPKG

2.75 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = _default;
7
8var _invariant = _interopRequireDefault(require("../invariant.js"));
9
10var _completions = require("../completions.js");
11
12var _index = require("../values/index.js");
13
14var _construct = require("../methods/construct.js");
15
16var _traverseFast = _interopRequireDefault(require("./traverse-fast.js"));
17
18var _parser = require("@babel/parser");
19
20function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
21
22/**
23 * Copyright (c) 2017-present, Facebook, Inc.
24 * All rights reserved.
25 *
26 * This source code is licensed under the BSD-style license found in the
27 * LICENSE file in the root directory of this source tree. An additional grant
28 * of patent rights can be found in the PATENTS file in the same directory.
29 */
30
31/* strict-local */
32function _default(realm, code, filename, sourceType = "script", startLine = 1) {
33 try {
34 let plugins = ["objectRestSpread"];
35
36 if (realm.react.enabled) {
37 plugins.push("jsx");
38 }
39
40 if (realm.stripFlow) {
41 plugins.push("flow");
42 }
43
44 let ast = (0, _parser.parse)(code, {
45 filename,
46 sourceType,
47 startLine,
48 plugins
49 });
50 (0, _traverseFast.default)(ast, node => {
51 (0, _invariant.default)(node.loc);
52 node.loc.source = filename;
53 return false;
54 });
55 return ast;
56 } catch (e) {
57 if (e instanceof SyntaxError) {
58 // Babel reports all errors as syntax errors, even if a ReferenceError should be thrown.
59 // What we do here is a totally robust way to address that issue.
60 let referenceErrors = ["Invalid left-hand side in postfix operation", "Invalid left-hand side in prefix operation", "Invalid left-hand side in assignment expression"];
61 let error;
62
63 if (referenceErrors.some(msg => e.message.indexOf(msg) >= 0)) {
64 error = (0, _construct.Construct)(realm, realm.intrinsics.ReferenceError, [new _index.StringValue(realm, e.message)]);
65 } else {
66 error = (0, _construct.Construct)(realm, realm.intrinsics.SyntaxError, [new _index.StringValue(realm, e.message)]);
67 } // These constructors are currently guaranteed to produce an object with
68 // built-in error data. Append location information about the syntax error
69 // and the source code to it so that we can use it to print nicer errors.
70
71
72 (0, _invariant.default)(error.$ErrorData);
73 error.$ErrorData.locationData = {
74 filename: filename,
75 sourceCode: code,
76 loc: e.loc,
77 stackDecorated: false
78 };
79 throw new _completions.ThrowCompletion(error, undefined, e.loc);
80 } else {
81 throw e;
82 }
83 }
84}
85//# sourceMappingURL=parse.js.map
\No newline at end of file