Skip to content

Commit

Permalink
Chore: refactor parser-loading out of linter.verify (#10028)
Browse files Browse the repository at this point in the history
  • Loading branch information
not-an-aardvark committed Feb 27, 2018
1 parent f6901d0 commit 9df8653
Showing 1 changed file with 29 additions and 20 deletions.
49 changes: 29 additions & 20 deletions lib/linter.js
Expand Up @@ -513,13 +513,16 @@ function analyzeScope(ast, parserOptions, visitorKeys) {
* as possible
* @param {string} text The text to parse.
* @param {Object} providedParserOptions Options to pass to the parser
* @param {Object} parser The parser module
* @param {string} parserName The name of the parser
* @param {Map<string, Object>} parserMap A map from names to loaded parsers
* @param {string} filePath The path to the file being parsed.
* @returns {{success: false, error: Problem}|{success: true, sourceCode: SourceCode}}
* An object containing the AST and parser services if parsing was successful, or the error if parsing failed
* @private
*/
function parse(text, providedParserOptions, parser, filePath) {
function parse(text, providedParserOptions, parserName, parserMap, filePath) {


const textToParse = stripUnicodeBOM(text).replace(astUtils.SHEBANG_MATCHER, (match, captured) => `//${captured}`);
const parserOptions = Object.assign({}, providedParserOptions, {
loc: true,
Expand All @@ -532,6 +535,25 @@ function parse(text, providedParserOptions, parser, filePath) {
filePath
});

let parser;

try {
parser = parserMap.get(parserName) || require(parserName);
} catch (ex) {
return {
success: false,
error: {
ruleId: null,
fatal: true,
severity: 2,
source: null,
message: ex.message,
line: 0,
column: 0
}
};
}

/*
* Check for parsing errors first. If there's a parsing error, nothing
* else can happen. However, a parsing error does not throw an error
Expand Down Expand Up @@ -853,6 +875,7 @@ function runRules(sourceCode, configuredRules, ruleMapper, parserOptions, parser
}

const lastSourceCodes = new WeakMap();
const loadedParserMaps = new WeakMap();

//------------------------------------------------------------------------------
// Public Interface
Expand All @@ -866,10 +889,10 @@ module.exports = class Linter {

constructor() {
lastSourceCodes.set(this, null);
loadedParserMaps.set(this, new Map());
this.version = pkg.version;

this.rules = new Rules();
this._parsers = new Map();
this.environments = new Environments();
}

Expand Down Expand Up @@ -932,25 +955,11 @@ module.exports = class Linter {
return [];
}

let parser;

try {
parser = this._parsers.get(parserName) || require(parserName);
} catch (ex) {
return [{
ruleId: null,
fatal: true,
severity: 2,
source: null,
message: ex.message,
line: 0,
column: 0
}];
}
const parseResult = parse(
text,
parserOptions,
parser,
parserName,
loadedParserMaps.get(this),
options.filename
);

Expand Down Expand Up @@ -1084,7 +1093,7 @@ module.exports = class Linter {
* @returns {void}
*/
defineParser(parserId, parserModule) {
this._parsers.set(parserId, parserModule);
loadedParserMaps.get(this).set(parserId, parserModule);
}

/**
Expand Down

0 comments on commit 9df8653

Please sign in to comment.