From 9df865326616b9865ab186c9769e95bc0bf98a20 Mon Sep 17 00:00:00 2001 From: Teddy Katz Date: Tue, 27 Feb 2018 15:32:40 -0500 Subject: [PATCH] Chore: refactor parser-loading out of linter.verify (#10028) --- lib/linter.js | 49 +++++++++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/lib/linter.js b/lib/linter.js index c97dd6ef8eb..66f4bc35720 100644 --- a/lib/linter.js +++ b/lib/linter.js @@ -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} 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, @@ -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 @@ -853,6 +875,7 @@ function runRules(sourceCode, configuredRules, ruleMapper, parserOptions, parser } const lastSourceCodes = new WeakMap(); +const loadedParserMaps = new WeakMap(); //------------------------------------------------------------------------------ // Public Interface @@ -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(); } @@ -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 ); @@ -1084,7 +1093,7 @@ module.exports = class Linter { * @returns {void} */ defineParser(parserId, parserModule) { - this._parsers.set(parserId, parserModule); + loadedParserMaps.get(this).set(parserId, parserModule); } /**