From 00b3042a6c5c2d57990fee6f8b737bc14821abc3 Mon Sep 17 00:00:00 2001 From: Annie Zhang Date: Fri, 2 Sep 2016 11:22:05 -0400 Subject: [PATCH] Update: Pass file path to parse function (fixes #5344) (#7024) --- lib/eslint.js | 9 ++++++--- tests/fixtures/parsers/stub-parser.js | 11 +++++++++++ tests/lib/eslint.js | 10 ++++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 tests/fixtures/parsers/stub-parser.js diff --git a/lib/eslint.js b/lib/eslint.js index 81483e1a261..efe1678dbc1 100755 --- a/lib/eslint.js +++ b/lib/eslint.js @@ -598,10 +598,11 @@ module.exports = (function() { * as possible * @param {string} text The text to parse. * @param {Object} config The ESLint configuration object. + * @param {string} filePath The path to the file being parsed. * @returns {ASTNode} The AST if successful or null if not. * @private */ - function parse(text, config) { + function parse(text, config, filePath) { let parser, parserOptions = { @@ -610,7 +611,8 @@ module.exports = (function() { raw: true, tokens: true, comment: true, - attachComment: true + attachComment: true, + filePath }; try { @@ -783,7 +785,8 @@ module.exports = (function() { shebang = captured; return "//" + captured; }), - config + config, + currentFilename ); if (ast) { diff --git a/tests/fixtures/parsers/stub-parser.js b/tests/fixtures/parsers/stub-parser.js new file mode 100644 index 00000000000..5c57bc5dfaf --- /dev/null +++ b/tests/fixtures/parsers/stub-parser.js @@ -0,0 +1,11 @@ +exports.parse = function(text, parserOptions) { + return { + "type": "Program", + "loc": {}, + "range": [], + "body": [], + "comments": [], + "errors": [], + "tokens": [] + }; +}; diff --git a/tests/lib/eslint.js b/tests/lib/eslint.js index 84226afe600..4f4644d9595 100644 --- a/tests/lib/eslint.js +++ b/tests/lib/eslint.js @@ -3821,6 +3821,16 @@ describe("eslint", function() { const parserFixtures = path.join(__dirname, "../fixtures/parsers"), errorPrefix = "Parsing error: "; + it("should have file path passed to it", function() { + const code = "/* this is code */"; + const parser = path.join(parserFixtures, "stub-parser.js"); + const parseSpy = sinon.spy(require(parser), "parse"); + + eslint.verify(code, { parser }, filename, true); + + sinon.assert.calledWithMatch(parseSpy, "", { filePath: filename }); + }); + it("should not report an error when JSX code contains a spread operator and JSX is enabled", function() { const code = "var myDivElement =
;"; const messages = eslint.verify(code, { parser: "esprima-fb" }, "filename");