From f8cb7461d4d0f40e6e15517279c0219214bbf1de Mon Sep 17 00:00:00 2001 From: Kai Cataldo Date: Tue, 6 Nov 2018 21:15:57 -0500 Subject: [PATCH] Implement parsing use @babel/core#parse --- lib/parse.js | 77 ++++++++++++++++++++++---------------------- test/babel-eslint.js | 2 +- 2 files changed, 40 insertions(+), 39 deletions(-) diff --git a/lib/parse.js b/lib/parse.js index d133b56b..71c0efa2 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -1,8 +1,7 @@ "use strict"; const babylonToEspree = require("./babylon-to-espree"); -const parse = require("@babel/parser").parse; -const traverse = require("@babel/core").traverse; +const { parseSync: parse, traverse } = require("@babel/core"); const tt = require("@babel/parser").tokTypes; module.exports = function(code, options) { @@ -11,42 +10,44 @@ module.exports = function(code, options) { const opts = { sourceType: options.sourceType, - allowImportExportEverywhere: options.allowImportExportEverywhere, // consistent with espree - allowReturnOutsideFunction: true, - allowSuperOutsideMethod: true, - ranges: true, - tokens: true, - plugins: [ - ["flow", { all: true }], - "jsx", - "estree", - "asyncFunctions", - "asyncGenerators", - "classConstructorCall", - "classProperties", - legacyDecorators - ? "decorators-legacy" - : ["decorators", { decoratorsBeforeExport: false }], - "doExpressions", - "exponentiationOperator", - "exportDefaultFrom", - "exportNamespaceFrom", - "functionBind", - "functionSent", - "objectRestSpread", - "trailingFunctionCommas", - "dynamicImport", - "numericSeparator", - "optionalChaining", - "importMeta", - "classPrivateProperties", - "bigInt", - "optionalCatchBinding", - "throwExpressions", - ["pipelineOperator", { proposal: "minimal" }], - "nullishCoalescingOperator", - "logicalAssignment", - ], + parserOpts: { + allowImportExportEverywhere: options.allowImportExportEverywhere, // consistent with espree + allowReturnOutsideFunction: true, + allowSuperOutsideMethod: true, + ranges: true, + tokens: true, + plugins: [ + ["flow", { all: true }], + "jsx", + "estree", + "asyncFunctions", + "asyncGenerators", + "classConstructorCall", + "classProperties", + legacyDecorators + ? "decorators-legacy" + : ["decorators", { decoratorsBeforeExport: false }], + "doExpressions", + "exponentiationOperator", + "exportDefaultFrom", + "exportNamespaceFrom", + "functionBind", + "functionSent", + "objectRestSpread", + "trailingFunctionCommas", + "dynamicImport", + "numericSeparator", + "optionalChaining", + "importMeta", + "classPrivateProperties", + "bigInt", + "optionalCatchBinding", + "throwExpressions", + ["pipelineOperator", { proposal: "minimal" }], + "nullishCoalescingOperator", + "logicalAssignment", + ], + }, }; let ast; diff --git a/test/babel-eslint.js b/test/babel-eslint.js index ce270f11..4fc181da 100644 --- a/test/babel-eslint.js +++ b/test/babel-eslint.js @@ -541,7 +541,7 @@ describe("Public API", () => { it("exports a parseNoPatch function", () => { assertImplementsAST( espree.parse("foo"), - babelEslint.parseNoPatch("foo", {}) + babelEslint.parseNoPatch("foo", { sourceType: "script" }) ); }); });