1"use strict"; 2 3var babylonToEspree = require("./babylon-to-espree"); 4var parse = require("@babel/parser").parse; 5var tt = require("@babel/parser").tokTypes; 6var traverse = require("@babel/traverse").default; 7var codeFrameColumns = require("@babel/code-frame").codeFrameColumns; 8 9module.exports = function(code, options) { 10 const legacyDecorators = 11 options.ecmaFeatures && options.ecmaFeatures.legacyDecorators; 12 13 var opts = { 14 codeFrame: options.hasOwnProperty("codeFrame") ? options.codeFrame : true, 15 sourceType: options.sourceType, 16 allowImportExportEverywhere: options.allowImportExportEverywhere, // consistent with espree 17 allowReturnOutsideFunction: true, 18 allowSuperOutsideMethod: true, 19 ranges: true, 20 tokens: true, 21 plugins: [ 22 ["flow", { all: true }], 23 "jsx", 24 "estree", 25 "asyncFunctions", 26 "asyncGenerators", 27 "classConstructorCall", 28 "classProperties", 29 legacyDecorators 30 ? "decorators-legacy" 31 : ["decorators", { decoratorsBeforeExport: false }], 32 "doExpressions", 33 "exponentiationOperator", 34 "exportDefaultFrom", 35 "exportNamespaceFrom", 36 "functionBind", 37 "functionSent", 38 "objectRestSpread", 39 "trailingFunctionCommas", 40 "dynamicImport", 41 "numericSeparator", 42 "optionalChaining", 43 "importMeta", 44 "classPrivateProperties", 45 "bigInt", 46 "optionalCatchBinding", 47 "throwExpressions", 48 ["pipelineOperator", { proposal: "minimal" }], 49 "nullishCoalescingOperator", 50 "logicalAssignment", 51 ], 52 }; 53 54 var ast; 55 try { 56 ast = parse(code, opts); 57 } catch (err) { 58 if (err instanceof SyntaxError) { 59 err.lineNumber = err.loc.line; 60 err.column = err.loc.column; 61 62 if (opts.codeFrame) { 63 err.lineNumber = err.loc.line; 64 err.column = err.loc.column + 1; 65 66 // remove trailing "(LINE:COLUMN)" acorn message and add in esprima syntax error message start 67 err.message = 68 "Line " + 69 err.lineNumber + 70 ": " + 71 err.message.replace(/ \((\d+):(\d+)\)$/, "") + 72 // add codeframe 73 "\n\n" + 74 codeFrameColumns( 75 code, 76 { 77 start: { 78 line: err.lineNumber, 79 column: err.column, 80 }, 81 }, 82 { highlightCode: true } 83 ); 84 } 85 } 86 87 throw err; 88 } 89 90 babylonToEspree(ast, traverse, tt, code); 91 92 return ast; 93}; 94