Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
kaicataldo committed May 28, 2020
1 parent ddfdf00 commit adf5517
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 42 deletions.
50 changes: 41 additions & 9 deletions eslint/babel-eslint-parser/src/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import semver from "semver";
import { version as CURRENT_BABEL_VERSION } from "@babel/core";
import parseWithScope from "./parse-with-scope";
import { normalizeESLintConfig } from "./configuration";
import {
version as CURRENT_BABEL_VERSION,
parseSync as babelParse,
} from "@babel/core";
import packageJson from "../package.json";
import {
normalizeBabelParseConfig,
normalizeESLintConfig,
} from "./configuration";
import convert from "./convert";
import analyzeScope from "./analyze-scope";
import visitorKeys from "./visitor-keys";

const SUPPORTED_BABEL_VERSION_RANGE =
packageJson.peerDependencies["@babel/core"];
Expand All @@ -11,16 +19,40 @@ const IS_RUNNING_SUPPORTED_VERSION = semver.satisfies(
SUPPORTED_BABEL_VERSION_RANGE,
);

export function parse(code, options) {
return parseForESLint(code, options).ast;
}

export function parseForESLint(code, options = {}) {
function baseParse(code, options) {
if (!IS_RUNNING_SUPPORTED_VERSION) {
throw new Error(
`babel-eslint@${packageJson.version} does not support @babel/core@${CURRENT_BABEL_VERSION}. Please downgrade to babel-eslint@^10 or upgrade to @babel/core@${SUPPORTED_BABEL_VERSION_RANGE}`,
);
}

return parseWithScope(code, normalizeESLintConfig(options));
const parseOptions = normalizeBabelParseConfig(options);
let ast;

try {
ast = babelParse(code, parseOptions);
} catch (err) {
if (err instanceof SyntaxError) {
err.lineNumber = err.loc.line;
err.column = err.loc.column;
}

throw err;
}

convert(ast, code);

return ast;
}

export function parse(code, options = {}) {
return baseParse(code, normalizeESLintConfig(options));
}

export function parseForESLint(code, options = {}) {
const normalizedOptions = normalizeESLintConfig(options);
const ast = baseParse(code, normalizedOptions);
const scopeManager = analyzeScope(ast, normalizedOptions);

return { ast, scopeManager, visitorKeys };
}
10 changes: 0 additions & 10 deletions eslint/babel-eslint-parser/src/parse-with-scope.js

This file was deleted.

23 changes: 0 additions & 23 deletions eslint/babel-eslint-parser/src/parse.js

This file was deleted.

0 comments on commit adf5517

Please sign in to comment.