Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: refactor parser-loading out of linter.verify #10028

Merged
merged 1 commit into from Feb 27, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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